Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

Merge #324: fix: sends wait during sandbox setup

ogsamratfcc8470ae5d1
diff --cc apps/app/src/react-app/domains/session/sync/global-queue-drainer.ts
index 79501062,8b5cf680..f03cd1ce
--- a/apps/app/src/react-app/domains/session/sync/global-queue-drainer.ts
+++ b/apps/app/src/react-app/domains/session/sync/global-queue-drainer.ts
@@@ -1,9 -1,8 +1,9 @@@
import type { SessionStatus } from "@opencode-ai/sdk/v2/client";
+import { draftHasLostPaste } from "../surface/composer/pasted-text";
import { isSessionReverted } from "./reverted-sessions";
import { markTaskRunStart } from "@/app/lib/analytics";
- import { createClient, createPromptMessageID, hasAcceptedPromptMessage, isPromptAdmissionUnknown } from "@/app/lib/opencode";
+ import { createClient, createPromptMessageID, hasAcceptedPromptMessage, isPromptAdmissionUnknown, isSendStopped } from "@/app/lib/opencode";
import { shellInSession } from "@/app/lib/opencode-session";
import { submitAfterInterruption } from "@/app/lib/opencode-interruption";
import { createClientV2, isOpencodeV2BaseUrl } from "@/app/lib/opencode-v2-adapter";
diff --cc apps/server/src/box/service.ts
index 7ad3cfde,5bee0175..b63dc6c7
--- a/apps/server/src/box/service.ts
+++ b/apps/server/src/box/service.ts
@@@ -222,19 -215,12 +222,21 @@@ export class BoxService
private readonly turns = new Map<string, number>();
/** Each chat's turn number as of its last prompt here (its turns before this run, then one per prompt). */
private readonly chatTurns = new Map<string, number>();
+ /** Per chat, its counts before the prompt beforePrompt last saw (promptCancelled puts them back). */
+ private readonly uncommitted = new Map<string, { chatTurns: number | undefined; boxed: boolean; turns: number | undefined }>();
/** Chats (and their project's key) that ran on this machine before the server's default turned the box on: they stay here. */
private readonly nativeChats = new Map<string, string>();
+ /** Of nativeChats, the ones on a model that runs only on this computer (launch reason native_model). */
+ private readonly nativeModelChats = new Set<string>();
/** Chats sent to their project's box in this run (a failed checkpoint included). */
private readonly boxedChats = new Set<string>();
+ /**
+ * Each chat's pin, from its first prompt: whether it runs in the box and the
+ * local_box policy revision it started under (model_start). A started chat
+ * never moves: a box chat whose project's box is turned off is refused
+ * (pinnedBoxGone), a chat that ran here stays here. Kept in chat-pins.json.
+ */
+ private pins: Map<string, { box: boolean; revision: string | null }> | null = null;
/** Chats whose box.plan event went out in this run. */
private readonly outcomeSent = new Set<string>();
private readonly home: string;
@@@ -800,16 -781,24 +802,32 @@@
* label returned for its uploads. Null when the chat runs natively. Never
* throws; waits at most FREEZE_WAIT_MS.
*/
- async beforePrompt(sessionId: string, workspace: BoxWorkspace, { capture = true, turnsBefore = 0, nativeModel = false }: { capture?: boolean; turnsBefore?: number; nativeModel?: boolean } = {}): Promise<BoxCaptureInfo | null> {
+ /**
+ * A prompt beforePrompt saw was not dispatched (stopped, refused before the
+ * engine): the chat's turn count, its in-the-box mark and turn number go back
+ * to what they were, so the next prompt is classified as this one was.
+ */
+ promptCancelled(sessionId: string): void {
+ const before = this.uncommitted.get(sessionId);
+ if (!before) return;
+ this.uncommitted.delete(sessionId);
+ if (before.chatTurns === undefined) this.chatTurns.delete(sessionId);
+ else this.chatTurns.set(sessionId, before.chatTurns);
+ if (!before.boxed) this.boxedChats.delete(sessionId);
+ if (before.turns === undefined) this.turns.delete(sessionId);
+ else this.turns.set(sessionId, before.turns);
+ }
+
- async beforePrompt(sessionId: string, workspace: BoxWorkspace, { capture = true, turnsBefore = 0, signal }: { capture?: boolean; turnsBefore?: number; signal?: AbortSignal } = {}): Promise<BoxCaptureInfo | null> {
++ async beforePrompt(sessionId: string, workspace: BoxWorkspace, { capture = true, turnsBefore = 0, nativeModel = false, signal }: { capture?: boolean; turnsBefore?: number; nativeModel?: boolean; signal?: AbortSignal } = {}): Promise<BoxCaptureInfo | null> {
if (!/^[A-Za-z0-9._:-]{8,128}$/.test(sessionId)) return null;
+ // A chat on a model that runs only on this computer (model-modes/: Kimi) runs here, exempt from
+ // the required sandbox, unless it already ran in the box (a chat's model never changes).
+ if (nativeModel && !this.nativeChats.has(sessionId) && !this.boxedChats.has(sessionId) && !this.turns.has(sessionId) && savedTurns(this.snapshotDir(sessionId)) === 0) {
+ this.nativeChats.set(sessionId, boxKey(boxRoot(workspace)));
+ this.nativeModelChats.add(sessionId);
+ await this.writeProjects();
+ this.log("info", "box: a chat on a model that runs only on this computer stays here", { sessionId });
+ }
// The chat's own turn number, counted with the box on or off: a chat that ran natively
// (reopened, or its box turned on mid-chat) has its first checkpoint (turn-1) before a later
// turn; the checkpoint names it as session_turn (CLI: the same).
@@@ -825,11 -816,14 +845,17 @@@
await this.writeProjects();
this.log("info", "box: a chat that ran on this machine stays there", { sessionId, turnsBefore, required: true });
}
- const run = required && !this.nativeChats.has(sessionId) ? await this.waitForBox(workspace) : await this.ensure(workspace).catch(() => null);
+ const run = required && !this.nativeChats.has(sessionId) ? await this.waitForBox(workspace, signal) : await this.ensure(workspace).catch(() => null);
- // The send was stopped while it waited: no checkpoint, the caller does not dispatch it, and the
++ // The send was stopped while it waited: no checkpoint and no pin, the caller does not dispatch it, and the
+ // chat counts as not having run this turn (a retry waits for the box again).
+ if (signal?.aborted) {
+ this.promptCancelled(sessionId);
+ return null;
+ }
- if (!run || run.native) return null;
+ if (!run || run.native) {
+ if (capture) await this.pin(sessionId, false);
+ return null;
+ }
// A chat with turns but no checkpoint ran on this machine (reopened, or started in this run before the
// server's default reached the app, or while the image downloaded): when only the server's default
// turned the box on, it goes on here (new chats get the box); a project turned on by its user moves it.
diff --cc apps/server/src/server.ts
index 8c67f8ed,8d483f15..b700b968
--- a/apps/server/src/server.ts
+++ b/apps/server/src/server.ts
@@@ -2131,11 -2125,10 +2137,12 @@@ export async function proxyOpencodeV2Re
// A reopened chat's turns before this prompt: its turn numbers (uploads and box checkpoints) carry on from them.
const turnsBefore = capture && uploadedSessionId && engine ? await turnsBeforeCapture(capture, uploadedSessionId, engine) : 0;
// The Linux sandbox (box/): a prompt of a workspace whose box is on starts the box, and takes the turn's checkpoint when it is captured.
+ // One pin per chat: a chat that runs in the box is not moved to this computer when its project's box is off.
+ if (promptDispatch && sessionId && await boxServiceFor(input.config).pinnedBoxGone(sessionId, input.workspace)) throw new ApiError(409, "chat_pinned_box", PINNED_BOX_MESSAGE);
const boxInfo = promptDispatch && sessionId
- ? await boxServiceFor(input.config).beforePrompt(sessionId, input.workspace, { capture: Boolean(uploadedSessionId && engine), turnsBefore, nativeModel: await promptsNativeOnlyModel(input.config, body) })
- ? await boxServiceFor(input.config).beforePrompt(sessionId, input.workspace, { capture: Boolean(uploadedSessionId && engine), turnsBefore, signal: input.request.signal })
++ ? await boxServiceFor(input.config).beforePrompt(sessionId, input.workspace, { capture: Boolean(uploadedSessionId && engine), turnsBefore, nativeModel: await promptsNativeOnlyModel(input.config, body), signal: input.request.signal })
: null;
+ if (promptDispatch && input.request.signal.aborted) return stoppedSendResponse(input.config, sessionId);
if (capture && uploadedSessionId && engine) {
// The turn before's background processes finish writing (at most 10 s after it ended), its project
// delta is taken (at most 5 s more, only when it ended a moment ago), then capture v2's session-start
@@@ -2572,10 -2566,13 +2580,14 @@@ export async function proxyOpencodeRequ
const turnsBefore = capture && captured && uploadedSessionId && workspace && !steer && isUploadPromptDispatch(method, proxyPath)
? await turnsBeforeCapture(capture, uploadedSessionId, engineTarget(baseUrl, headers, search))
: 0;
+ if (workspace && uploadedSessionId && isUploadPromptDispatch(method, proxyPath) && await boxServiceFor(input.config).pinnedBoxGone(uploadedSessionId, workspace)) throw new ApiError(409, "chat_pinned_box", PINNED_BOX_MESSAGE);
const boxInfo = workspace && uploadedSessionId && isUploadPromptDispatch(method, proxyPath)
- ? await boxServiceFor(input.config).beforePrompt(uploadedSessionId, workspace, { capture: captured && !steer, turnsBefore, nativeModel: await promptsNativeOnlyModel(input.config, body) })
- ? await boxServiceFor(input.config).beforePrompt(uploadedSessionId, workspace, { capture: captured && !steer, turnsBefore, signal: input.request.signal })
++ ? await boxServiceFor(input.config).beforePrompt(uploadedSessionId, workspace, { capture: captured && !steer, turnsBefore, nativeModel: await promptsNativeOnlyModel(input.config, body), signal: input.request.signal })
: null;
+ const dispatch = isUploadPromptDispatch(method, proxyPath) || isSessionCommandProxyRequest(method, proxyPath);
+ // The chat whose turn beforePrompt counted for this request (undone when it is stopped before the engine).
+ const boxSession = workspace && uploadedSessionId && isUploadPromptDispatch(method, proxyPath) ? uploadedSessionId : null;
+ if (dispatch && input.request.signal.aborted) return stoppedSendResponse(input.config, boxSession);
if (capture && captured && uploadedSessionId && workspace && !steer) {
const promptDispatch = isUploadPromptDispatch(method, proxyPath);
// The turn before's background processes finish writing (at most 10 s after it ended), its project