feat(server): show cross-provider CLI agents across shells #11230
Closedlnieuwenhuis(GitHub profile, opens in a new tab)wants to merge 20 commits into
main fromrefs/pull/11230/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/server/src/orchestration/OpenCodeRunSubagents.ts lines 113 to 116OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +96,21 @@96+ continue;97+ }98+ if (char === "'" || char === '"') {99+ quote = char;100+ quoted = true;101+ inToken = true;102+ continue;103+ }104+ if (char === "\\" && index + 1 < command.length) {105+ index += 1;106+ const next = command[index]!;107+ if (next !== "\n") {108+ text += next;109+ inToken = true;110+ }111+ continue;112+ }113+ if (/\s/.test(char)) {114+ flush();115+ continue;116+ }apps/server/src/orchestration/OpenCodeRunSubagents.ts lines 594 to 594OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +574,21 @@574+ const base = {575+ provider: item.provider,576+ ...(item.providerInstanceId !== undefined577+ ? { providerInstanceId: item.providerInstanceId }578+ : {}),579+ threadId: item.threadId,580+ createdAt: item.createdAt,581+ ...(item.turnId !== undefined ? { turnId: item.turnId } : {}),582+ };583+ const push = (584+ event:585+ | { type: "task.started"; payload: TaskStartedEvent["payload"] }586+ | { type: "task.completed"; payload: TaskCompletedEvent["payload"] },587+ ) => {588+ events.push({589+ ...base,590+ eventId: EventId.make(`${item.eventId}:opencode-run:${events.length + 1}`),591+ ...event,592+ });593+ };594+ if (!options.started) {apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2172OutdatedResolvedby lnieuwenhuis(GitHub profile, opens in a new tab)@@ -2144,3 +2155,18 @@2144215521452156 const processDomainEvent = (_event: TurnStartRequestedDomainEvent) => Effect.void;214621572158+ // A shell item that runs `opencode run` is also a delegated agent: after2159+ // the item's own activity lands, feed the derived task.* events through the2160+ // same path so liveness, titles, and the Agents roster all follow.2161+ const processRuntimeEventWithOpenCodeRuns = (event: ProviderRuntimeEvent) =>2162+ Effect.gen(function* () {2163+ yield* processRuntimeEvent(event);2164+ const itemKey = openCodeRunItemKey(event);2165+ if (itemKey === undefined) {2166+ return;2167+ }2168+ const started = Option.getOrElse(2169+ yield* Cache.getOption(startedOpenCodeRunItemKeys, itemKey),2170+ () => false,2171+ );2172+ const derived = deriveOpenCodeRunEvents(event, { started });
- apps/server/src/orchestration/OpenCodeRunSubagents.ts lines 169 to 169OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +149,21 @@149+150+const SHELL_EXECUTABLES = new Set(["sh", "bash", "zsh", "dash", "ksh", "fish"]);151+const SHELL_COMMAND_FLAG = /^-[a-zA-Z]*c[a-zA-Z]*$/;152+const ENV_ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;153+const DURATION_OR_NUMBER = /^\d+(\.\d+)?[smhd]?$/;154+// Wrappers that run their trailing argv as the real command.155+const TRANSPARENT_WRAPPERS = new Set(["env", "exec", "nohup", "time", "timeout", "command"]);156+157+/**158+ * Whether the token at `index` begins a simple command: it is first on the159+ * line, follows a separator, or is only preceded by env assignments and160+ * transparent wrappers such as `timeout 60`. Keeps `echo opencode run …` and161+ * `grep opencode run .` from reading as invocations.162+ */163+function startsSimpleCommand(tokens: ReadonlyArray<ShellToken>, index: number): boolean {164+ for (let cursor = index - 1; cursor >= 0; cursor -= 1) {165+ const token = tokens[cursor]!;166+ if (token.quoted) {167+ return false;168+ }169+ if (COMMAND_SEPARATOR.test(token.text)) {apps/server/src/orchestration/OpenCodeRunSubagents.ts line 276OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +256,21 @@256+ prompt: normalizePrompt(positionals),257+ model: model && model.length > 0 ? model : undefined,258+ agent: agent && agent.length > 0 ? agent : undefined,259+ jsonOutput: options.get("--format") === "json",260+ };261+}262+263+/**264+ * Recognizes an `opencode run …` invocation anywhere in a shell command line,265+ * including behind `cd … &&`, env assignments, or a wrapper such as266+ * `zsh -lc "opencode run …"`.267+ */268+export function parseOpenCodeRunCommand(269+ command: string,270+ depth = 0,271+): OpenCodeRunInvocation | undefined {272+ if (!command.includes("opencode")) {273+ return undefined;274+ }275+ const tokens = tokenizeShell(command);276+ for (let index = 0; index + 1 < tokens.length; index += 1) {apps/server/src/orchestration/OpenCodeRunSubagents.ts lines 568 to 574OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +554,21 @@554+/**555+ * Derives the task.* events an `opencode run` shell item implies. `started`556+ * tells whether this item's run already has a task.started; the caller owns557+ * that memory because a Claude Bash item streams its input across several558+ * item.updated events before the command is readable.559+ */560+export function deriveOpenCodeRunEvents(561+ event: ProviderRuntimeEvent,562+ options: { readonly started: boolean },563+): ReadonlyArray<ProviderRuntimeEvent> {564+ const item = asCommandItemEvent(event);565+ if (!item) {566+ return [];567+ }568+ const data = asRecord(item.payload.data);569+ if (isBackgroundShellItem(data)) {570+ return [];571+ }572+ const command = commandFromItemData(data);573+ const invocation = command ? parseOpenCodeRunCommand(command) : undefined;574+ if (!invocation) {apps/server/src/orchestration/OpenCodeRunSubagents.ts line 646OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +626,21 @@626+ toolUseId,627+ } as const;628+ push({629+ type: "task.started",630+ payload: {631+ taskId: childTaskId,632+ ...(child.title ? { description: child.title } : {}),633+ ...childLinkage,634+ },635+ });636+ push({637+ type: "task.completed",638+ payload: {639+ taskId: childTaskId,640+ status: child.failed ? "failed" : "completed",641+ ...(child.summary ? { summary: child.summary } : {}),642+ ...childLinkage,643+ },644+ });645+ }646+ const itemFailed = item.payload.status === "failed" || item.payload.status === "declined";
- apps/server/src/orchestration/OpenCodeRunSubagents.ts line 309OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +289,21 @@289+ }290+ return undefined;291+}292+293+/**294+ * Recognizes an `opencode run …` invocation anywhere in a shell command line,295+ * including behind `cd … &&`, env assignments, or a wrapper such as296+ * `zsh -lc "opencode run …"`.297+ */298+export function parseOpenCodeRunCommand(299+ command: string,300+ depth = 0,301+): OpenCodeRunInvocation | undefined {302+ if (!command.includes("opencode")) {303+ return undefined;304+ }305+ const tokens = tokenizeShell(command);306+ for (let index = 0; index + 1 < tokens.length; index += 1) {307+ if (executableName(tokens[index]!) !== "opencode" || !startsSimpleCommand(tokens, index)) {308+ continue;309+ }
- apps/server/src/orchestration/OpenCodeRunSubagents.ts line 313OutdatedResolvedby lnieuwenhuis(GitHub profile, opens in a new tab)@@ -0,0 +293,21 @@293+/**294+ * Recognizes an `opencode run …` invocation anywhere in a shell command line,295+ * including behind `cd … &&`, env assignments, or a wrapper such as296+ * `zsh -lc "opencode run …"`.297+ */298+export function parseOpenCodeRunCommand(299+ command: string,300+ depth = 0,301+): OpenCodeRunInvocation | undefined {302+ if (!command.includes("opencode")) {303+ return undefined;304+ }305+ const tokens = tokenizeShell(command);306+ for (let index = 0; index + 1 < tokens.length; index += 1) {307+ if (executableName(tokens[index]!) !== "opencode" || !startsSimpleCommand(tokens, index)) {308+ continue;309+ }310+ const subcommand = subcommandIndex(tokens, index + 1);311+ const next = subcommand === undefined ? undefined : tokens[subcommand];312+ if (next !== undefined && !next.quoted && next.text === "run") {313+ return parseInvocationTokens(tokens.slice(subcommand! + 1));
- apps/server/src/orchestration/OpenCodeRunSubagents.ts lines 312 to 312OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +292,21 @@292+293+/**294+ * Recognizes an `opencode run …` invocation anywhere in a shell command line,295+ * including behind `cd … &&`, env assignments, or a wrapper such as296+ * `zsh -lc "opencode run …"`.297+ */298+export function parseOpenCodeRunCommand(299+ command: string,300+ depth = 0,301+): OpenCodeRunInvocation | undefined {302+ if (!command.includes("opencode")) {303+ return undefined;304+ }305+ const tokens = tokenizeShell(command);306+ for (let index = 0; index + 1 < tokens.length; index += 1) {307+ if (executableName(tokens[index]!) !== "opencode" || !startsSimpleCommand(tokens, index)) {308+ continue;309+ }310+ const subcommand = subcommandIndex(tokens, index + 1);311+ const next = subcommand === undefined ? undefined : tokens[subcommand];312+ if (next !== undefined && !next.quoted && next.text === "run") {
- apps/server/src/orchestration/OpenCodeRunSubagents.ts lines 528 to 528OutdatedResolvedby lnieuwenhuis(GitHub profile, opens in a new tab)@@ -0,0 +508,21 @@508+ const input = asNonNegativeInt(tokens.input) ?? 0;509+ const cached = asNonNegativeInt(asRecord(tokens.cache)?.read) ?? 0;510+ const outputCount = asNonNegativeInt(tokens.output) ?? 0;511+ const reasoning = asNonNegativeInt(tokens.reasoning) ?? 0;512+ inputTokens += input;513+ cachedInputTokens += cached;514+ outputTokens += outputCount;515+ reasoningTokens += reasoning;516+ totalTokens += asNonNegativeInt(tokens.total) ?? input + cached + outputCount + reasoning;517+ break;518+ }519+ case "text": {520+ const text = asString(part?.text)?.trim();521+ if (text) {522+ summary = text;523+ }524+ break;525+ }526+ case "tool_use": {527+ toolUses += 1;528+ if (part && part.tool === "task") {
- apps/server/src/orchestration/OpenCodeRunSubagents.ts lines 127 to 127OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +107,21 @@107+ const next = command[index]!;108+ if (next !== "\n") {109+ text += next;110+ inToken = true;111+ }112+ continue;113+ }114+ if (char === "\n") {115+ // An unquoted newline ends the command like `;` does, so a multi-line116+ // script still starts a fresh simple command on the next line.117+ flush();118+ tokens.push({ text: ";", quoted: false });119+ continue;120+ }121+ if (/\s/.test(char)) {122+ flush();123+ continue;124+ }125+ // Separators need no surrounding whitespace (`ls;opencode run`, `… |head`);126+ // `&` stays attached inside redirects such as `2>&1`.127+ if (char === ";" || char === "|" || (char === "&" && !text.endsWith(">"))) {
- apps/server/src/orchestration/OpenCodeRunSubagents.ts lines 240 to 240OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +220,21 @@220+): OpenCodeRunInvocation | undefined {221+ const positionals: string[] = [];222+ const options = new Map<string, string>();223+ let optionsEnded = false;224+ for (let index = 0; index < tokens.length; index += 1) {225+ const token = tokens[index]!;226+ if (!token.quoted) {227+ if (token.text === "&") {228+ return undefined;229+ }230+ if (COMMAND_SEPARATOR.test(token.text)) {231+ break;232+ }233+ if (REDIRECT_PREFIX.test(token.text)) {234+ if (REDIRECT_WITH_OPERAND.test(token.text)) {235+ index += 1;236+ }237+ continue;238+ }239+ }240+ if (optionsEnded || token.quoted || !token.text.startsWith("-")) {apps/server/src/orchestration/OpenCodeRunSubagents.ts lines 168 to 170OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +150,21 @@150+}151+152+const SHELL_EXECUTABLES = new Set(["sh", "bash", "zsh", "dash", "ksh", "fish"]);153+const SHELL_COMMAND_FLAG = /^-[a-zA-Z]*c[a-zA-Z]*$/;154+const ENV_ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;155+const DURATION_OR_NUMBER = /^\d+(\.\d+)?[smhd]?$/;156+// Wrappers that run their trailing argv as the real command.157+const TRANSPARENT_WRAPPERS = new Set(["env", "exec", "nohup", "time", "timeout", "command"]);158+159+/**160+ * Whether the token at `index` begins a simple command: it is first on the161+ * line, follows a separator, or is only preceded by env assignments and162+ * transparent wrappers such as `timeout 60`. Keeps `echo opencode run …` and163+ * `grep opencode run .` from reading as invocations.164+ */165+function startsSimpleCommand(tokens: ReadonlyArray<ShellToken>, index: number): boolean {166+ for (let cursor = index - 1; cursor >= 0; cursor -= 1) {167+ const token = tokens[cursor]!;168+ if (token.quoted) {169+ return false;170+ }apps/server/src/orchestration/OpenCodeRunSubagents.ts lines 191 to 198OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +178,21 @@178+ TRANSPARENT_WRAPPERS.has(executableName(token) ?? "")179+ ) {180+ continue;181+ }182+ return false;183+ }184+ return true;185+}186+187+/** `zsh -lc "<script>"` style wrappers, as Codex and some hooks spawn them. */188+function isShellWrapperScript(tokens: ReadonlyArray<ShellToken>, index: number): boolean {189+ const flag = tokens[index - 1];190+ const shell = tokens[index - 2];191+ return (192+ tokens[index]!.quoted &&193+ flag !== undefined &&194+ !flag.quoted &&195+ SHELL_COMMAND_FLAG.test(flag.text) &&196+ shell !== undefined &&197+ SHELL_EXECUTABLES.has(executableName(shell) ?? "")198+ );apps/server/src/orchestration/OpenCodeRunSubagents.ts lines 145 to 147OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +127,21 @@127+ if (char === ";" || char === "|" || (char === "&" && !text.endsWith(">"))) {128+ flush();129+ const doubled = char !== ";" && command[index + 1] === char;130+ tokens.push({ text: doubled ? char + char : char, quoted: false });131+ if (doubled) {132+ index += 1;133+ }134+ continue;135+ }136+ text += char;137+ inToken = true;138+ }139+ flush();140+ return tokens;141+}142+143+/** Basename of an unquoted token, so `/usr/local/bin/opencode` reads as `opencode`. */144+function executableName(token: ShellToken): string | undefined {145+ if (token.quoted) {146+ return undefined;147+ }
- apps/server/src/orchestration/OpenCodeRunSubagents.ts lines 125 to 137OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +117,21 @@117+ if (char === "\n") {118+ // An unquoted newline ends the command like `;` does, so a multi-line119+ // script still starts a fresh simple command on the next line.120+ flush();121+ tokens.push({ text: ";", quoted: false });122+ continue;123+ }124+ if (/\s/.test(char)) {125+ flush();126+ continue;127+ }128+ if (char === "&" && command[index + 1] === ">") {129+ flush();130+ const append = command[index + 2] === ">";131+ tokens.push({ text: append ? "&>>" : "&>", quoted: false });132+ index += append ? 2 : 1;133+ continue;134+ }135+ // Separators need no surrounding whitespace (`ls;opencode run`, `… |head`);136+ // `&` stays attached inside redirects such as `2>&1`.137+ if (char === ";" || char === "|" || (char === "&" && !text.endsWith(">"))) {
- apps/server/src/orchestration/OpenCodeRunSubagents.ts line 156OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +136,21 @@136+ if ((stripTabs ? line.replace(/^\t+/, "") : line) === delimiter) {137+ break;138+ }139+ start = end + 1;140+ }141+ }142+ hereDocuments.length = 0;143+ continue;144+ }145+ if (char === "<" && command[index + 1] === "<") {146+ flush();147+ const suffix = command[index + 2];148+ const operator = suffix === "<" ? "<<<" : suffix === "-" ? "<<-" : "<<";149+ tokens.push({ text: operator, quoted: false });150+ if (operator !== "<<<") {151+ awaitingHereDocument = { stripTabs: operator === "<<-" };152+ }153+ index += operator.length - 1;154+ continue;155+ }156+ if (/\s/.test(char)) {
- apps/server/src/orchestration/OpenCodeRunSubagents.ts lines 459 to 462OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +442,21 @@442+443+/** Claude's Bash tool returns at once for `run_in_background`, so its output is elsewhere. */444+function isBackgroundShellItem(data: Record<string, unknown> | undefined): boolean {445+ return asRecord(data?.input)?.run_in_background === true;446+}447+448+/** Text of a tool result `content`, which is a string or an array of text blocks. */449+function textFromContent(content: unknown): string | undefined {450+ if (typeof content === "string") {451+ return content;452+ }453+ if (Array.isArray(content)) {454+ const text = content455+ .map((block) => asString(asRecord(block)?.text))456+ .filter((value): value is string => value !== undefined)457+ .join("\n");458+ return text.length > 0 ? text : undefined;459+ }460+ return undefined;461+}462+
- apps/server/src/orchestration/OpenCodeRunSubagents.ts lines 306 to 308OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +288,21 @@288+289+/**290+ * Parses the argv after `opencode run`. Returns undefined when the shell291+ * backgrounds the run with a trailing `&`: the shell item then returns while292+ * OpenCode is still working, so there is no lifecycle to mirror.293+ */294+function parseInvocationTokens(295+ tokens: ReadonlyArray<ShellToken>,296+): OpenCodeRunInvocation | undefined {297+ const positionals: string[] = [];298+ const options = new Map<string, string>();299+ let optionsEnded = false;300+ for (let index = 0; index < tokens.length; index += 1) {301+ const token = tokens[index]!;302+ if (!token.quoted) {303+ if (token.text === "&") {304+ return undefined;305+ }306+ if (COMMAND_SEPARATOR.test(token.text)) {307+ break;308+ }
- apps/server/src/orchestration/OpenCodeRunSubagents.ts lines 453 to 453OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +433,21 @@433+ if (!data) {434+ return undefined;435+ }436+ return (437+ asString(data.command) ??438+ asString(asRecord(data.input)?.command) ??439+ asString(asRecord(data.item)?.command)440+ );441+}442+443+/** Claude's Bash tool returns at once for `run_in_background`, so its output is elsewhere. */444+function isBackgroundShellItem(data: Record<string, unknown> | undefined): boolean {445+ return asRecord(data?.input)?.run_in_background === true;446+}447+448+/** Text of a tool result `content`, which is a string or an array of text blocks. */449+function textFromContent(content: unknown): string | undefined {450+ if (typeof content === "string") {451+ return content;452+ }453+ if (Array.isArray(content)) {
- apps/server/src/orchestration/OpenCodeRunSubagents.ts line 193OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +173,21 @@173+ tokens.push({ text: descriptor + operator, quoted: false });174+ if (operator === "<<" || operator === "<<-") {175+ awaitingHereDocument = { stripTabs: operator === "<<-" };176+ }177+ index += operator.length - 1;178+ continue;179+ }180+ if (/\s/.test(char)) {181+ flush();182+ continue;183+ }184+ if (char === "&" && command[index + 1] === ">") {185+ flush();186+ const append = command[index + 2] === ">";187+ tokens.push({ text: append ? "&>>" : "&>", quoted: false });188+ index += append ? 2 : 1;189+ continue;190+ }191+ // Separators need no surrounding whitespace (`ls;opencode run`, `… |head`);192+ // `&` stays attached inside redirects such as `2>&1`.193+ if (char === ";" || char === "|" || (char === "&" && !text.endsWith(">"))) {
Sign in to join this conversation.
Bugbot is paused — on-demand spend limit reached
Bugbot uses usage-based billing for this team and has hit its on-demand spend limit.
A team admin can raise the spend limit in the Cursor dashboard, or wait for the next billing cycle to continue.