feat(opencode): track native shell jobs across turns #34141
MergedYash-Singh1(GitHub profile, opens in a new tab)merged 5 commits into
main fromrefs/pull/34141/headmerged by Yash-Singh1(GitHub profile, opens in a new tab) 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:L
- packages/provider-opencode/src/server/v2/adapter.ts line 2712OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2459 +2692,21 @@24592692 const handleEvent = Effect.fnUntraced(function* (event: OpenCode2Client.OpenCode2StreamEvent) {2693+ if (event.type === "shell.created") {2694+ const sessionId = stringField(event.data.info.metadata, "sessionID");2695+ if (sessionId !== undefined && ownerOf(sessionId) !== undefined) {2696+ shellInfo.set(event.data.info.id, event.data.info);2697+ if (shellInfo.size > 128) shellInfo.delete(shellInfo.keys().next().value!);2698+ }2699+ return;2700+ }2701+ if (event.type === "shell.exited" || event.type === "shell.deleted") {2702+ const known = shellInfo.get(event.data.id);2703+ if (known !== undefined)2704+ shellInfo.set(event.data.id, {2705+ ...known,2706+ status: event.type === "shell.deleted" ? "killed" : event.data.status,2707+ exit: "exit" in event.data ? event.data.exit : undefined,2708+ });2709+ const shell = shells.get(event.data.id);2710+ if (shell === undefined || !shell.running || shell.stopping === true) return;2711+ if (event.type === "shell.exited")2712+ yield* readShellOutput(event.data.id).pipe(Effect.ignore({ log: true }));packages/provider-opencode/src/server/v2/adapter.ts line 3802OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3508,3 +3798,5 @@35083798 });3509379935103800 const stopBackground = Effect.fnUntraced(function* (state: ThreadState) {3801+ for (const [id, shell] of shells) {3802+ if (shell.running && sessionsOf(state).includes(shell.state)) yield* stopShell(id);packages/provider-opencode/src/server/v2/adapter.ts line 1583OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1457,0 +1563,21 @@1563+ ? yield* connection.shellJobs(state.sessionId).pipe(Effect.option)1564+ : undefined;1565+ const data =1566+ jobs?._tag === "Some"1567+ ? jobs.value.data.map((job) => ({1568+ id: Shell.ID.make(job.shellID),1569+ command: job.command,1570+ metadata: { sessionID: job.sessionID },1571+ }))1572+ : (yield* client.shell.list({ location: { directory: state.directory } })).data;1573+ for (const info of data) {1574+ const sessionId = stringField(info.metadata, "sessionID");1575+ const owner =1576+ sessionId === undefined1577+ ? undefined1578+ : (threads.get(sessionId) ??1579+ ownerOf(sessionId) ??1580+ (jobs?._tag === "Some" ? state : undefined));1581+ if (owner === undefined || !sessionsOf(state).includes(owner)) continue;1582+ if (!shells.has(info.id))1583+ shells.set(info.id, {
- packages/provider-opencode/src/server/v2/adapter.ts lines 1571 to 1576Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1455,3 +1492,14 @@14551492 }14561493 });145714941495+ const readShellOutput = Effect.fn("OpenCode2Adapter.readShellOutput")(function* (id: string) {1496+ const shell = shells.get(id);1497+ if (shell === undefined) return;1498+ // Bound each read and retained UI text; the native byte cursor must never use JS string lengths.1499+ for (let page = 0; page < 4; page++) {1500+ const { data } = yield* client.shell.output({1501+ id: Shell.ID.make(id),1502+ location: { directory: shell.state.directory },1503+ cursor: shell.cursor,1504+ limit: 65536,1505+ });packages/provider-opencode/src/server/v2/adapter.ts lines 1659 to 1671Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1457,0 +1526,21 @@1526+ });1527+ shell.turn.tools.delete(shell.toolId);1528+ });1529+1530+ const stopShell = Effect.fn("OpenCode2Adapter.stopShell")(function* (id: string) {1531+ const shell = shells.get(id);1532+ if (shell === undefined || !shell.running) return;1533+ yield* readShellOutput(id).pipe(Effect.ignore({ log: true }));1534+ shell.stopping = true;1535+ yield* client.shell1536+ .remove({1537+ id: Shell.ID.make(id),1538+ location: { directory: shell.state.directory },1539+ })1540+ .pipe(1541+ Effect.onError(() =>1542+ Effect.sync(() => {1543+ shell.stopping = false;1544+ }),1545+ ),1546+ );packages/provider-opencode/src/server/v2/adapter.ts lines 1593 to 1602OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1457,0 +1582,21 @@1582+ if (!shells.has(info.id))1583+ shells.set(info.id, {1584+ state: owner,1585+ command: info.command,1586+ running: true,1587+ output: "",1588+ cursor: 0,1589+ });1590+ }1591+ for (const [id, shell] of shells) {1592+ if (!shell.running || !sessionsOf(state).includes(shell.state)) continue;1593+ const found = yield* client.shell1594+ .get({ id: Shell.ID.make(id), location: { directory: shell.state.directory } })1595+ .pipe(Effect.catchTags({ ShellNotFoundError: () => Effect.undefined }));1596+ if (found === undefined) {1597+ shell.running = false;1598+ yield* emitShell(id, "interrupted");1599+ continue;1600+ }1601+ const info = found.data;1602+ yield* readShellOutput(id);packages/provider-opencode/src/server/v2/adapter.ts lines 3498 to 3500Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -2909,3 +3177,9 @@29093177 { concurrency: 4, discard: true },29103178 ).pipe(Effect.timeout(INVENTORY_TIMEOUT), Effect.ignore({ log: true }));29113179 }3180+ for (const state of threads.values()) {3181+ yield* recoverShells(state).pipe(3182+ Effect.timeout(INVENTORY_TIMEOUT),3183+ Effect.ignore({ log: true }),3184+ );3185+ }packages/provider-opencode/src/server/v2/adapter.ts lines 4142 to 4145Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -3508,3 +3798,6 @@35083798 });3509379935103800 const stopBackground = Effect.fnUntraced(function* (state: ThreadState) {3801+ for (const [id, shell] of shells) {3802+ if (shell.running && sessionsOf(state).includes(shell.state)) yield* stopShell(id);3803+ }
- packages/provider-opencode/src/server/v2/adapter.ts line 3498OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3165,3 +3495,4 @@31653495 // Subscribed before any session or prompt call, so no event of theirs is missed.31663496 yield* follow(yield* connection.events).pipe(Effect.forkScoped);316734973498+ yield* lock
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- packages/provider-opencode/src/server/v2/adapter.ts lines 1706 to 1710Resolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -1517,0 +1687,21 @@1687+ jobs?._tag === "Some"1688+ ? jobs.value.data.map((job) => ({1689+ id: Shell.ID.make(job.shellID),1690+ command: job.command,1691+ metadata: { sessionID: job.sessionID },1692+ }))1693+ : (yield* client.shell.list({ location: { directory: state.directory } })).data;1694+ for (const info of data) {1695+ const sessionId = stringField(info.metadata, "sessionID");1696+ const owner =1697+ sessionId === undefined1698+ ? undefined1699+ : (threads.get(sessionId) ??1700+ ownerOf(sessionId) ??1701+ (jobs?._tag === "Some" ? state : undefined));1702+ if (owner === undefined || !sessionsOf(state).includes(owner)) continue;1703+ if (!shells.has(info.id)) {1704+ if (state.active === undefined && !hasBackground(state)) {1705+ state.sessionScopedBackground = true;1706+ delete state.publishedBackgroundWork;1707+ }packages/provider-opencode/src/server/v2/adapter.ts line 1779OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1517,0 +1759,21 @@1759+ ? "interrupted"1760+ : info.exit === 01761+ ? "completed"1762+ : "failed",1763+ info.exit,1764+ );1765+ if (!shell.running) shells.delete(id);1766+ }).pipe(1767+ Effect.catch((cause) =>1768+ Effect.logWarning("Could not recover an OpenCode shell.", cause).pipe(1769+ Effect.andThen(emitShell(id, "running")),1770+ ),1771+ ),1772+ );1773+ }1774+ },1775+ (effect, state) =>1776+ lock.withPermit(1777+ effect.pipe(1778+ Effect.timeout(INVENTORY_TIMEOUT),1779+ Effect.ensuring(syncRosterAncestors(state)),
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
Yash-Singh1(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed