Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

feat: the quota wrap offer on the new engine

ogsamrat613ec3249845
diff --git a/apps/server/src/engine-v2-budget-wrap.ts b/apps/server/src/engine-v2-budget-wrap.ts
new file mode 100644
index 00000000..920dd50e
--- /dev/null
+++ b/apps/server/src/engine-v2-budget-wrap.ts
@@ -0,0 +1,113 @@
+/**
+ * The quota wrap (handoff) on the 2.x engine, run by the server: the 1.x plugin
+ * (opencode-plugins/omnirush-budget-wrap.ts) read the gateway's wrap headers in
+ * the engine process; on 2.x the server's own gateway broker sees every model
+ * response, so the same rules (budget-wrap-rules.ts, shared with the CLI) run
+ * here. Asked only while the chat is idle, once per offer; an accepted offer
+ * compacts the chat (its compaction request is marked as the wrap); a failed
+ * wrap becomes a notice with a retry, never the question again.
+ */
+import { replacesWrapOffer, startWrapNow, wrapFinished } from "./opencode-plugins/budget-wrap-rules.js";
+
+const REQUIRED_HEADER = "x-omnirush-wrap-required";
+const WRAP_UP_HEADER = "x-omnirush-wrap-up";
+const OFFER_HEADER = "x-omnirush-wrap-offer-id";
+const SCOPES_HEADER = "x-omnirush-wrap-scopes";
+/** How long a failed wrap's Retry stays available (the window's notice). */
+const RETRY_WINDOW_MS = 30_000;
+const MAX_TRACKED = 256;
+
+type Offer = { id: string; scopes: string; watching: boolean; inFlight: boolean; approved: boolean; failedAt?: number };
+
+export type BudgetWrapDeps = {
+ /** The desktop window's answer to a wrap query (ui-control `query`): "pending", "accept", "decline", "done", "failed" or "unavailable". */
+ control(id: string, args: Record<string, string>): Promise<string>;
+ /** Whether the chat runs a turn now; null when the engine cannot say. */
+ busy(sessionId: string): Promise<boolean | null>;
+ /** Compacts the chat on the 2.x engine; resolves once its compaction completed (throws otherwise). */
+ compact(sessionId: string): Promise<void>;
+ sleep?: (ms: number) => Promise<void>;
+ now?: () => number;
+ idleSettleMs?: number;
+ pollMs?: number;
+};
+
+export function createV2BudgetWrap(deps: BudgetWrapDeps) {
+ const pending = new Map<string, Offer>();
+ const sleep = deps.sleep ?? ((ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms)));
+ const now = deps.now ?? Date.now;
+ const update = (sessionId: string, entry: Offer, operation: string) =>
+ deps.control("budget.wrap.update", { sessionID: sessionId, offerID: entry.id, scopes: entry.scopes, operation }).catch(() => "unavailable");
+
+ async function compact(sessionId: string, entry: Offer): Promise<void> {
+ entry.inFlight = true;
+ entry.approved = true;
+ let ok = false;
+ try {
+ await deps.compact(sessionId);
+ ok = true;
+ } catch {
+ ok = false;
+ }
+ if (wrapFinished({ ok, compacted: ok })) {
+ await update(sessionId, entry, "finish");
+ pending.delete(sessionId);
+ return;
+ }
+ entry.approved = false;
+ entry.inFlight = false;
+ entry.failedAt = now();
+ await update(sessionId, entry, "fail");
+ }
+
+ /** While the chat is idle: ask, and wrap once accepted (the 1.x plugin's watch loop). */
+ async function watch(sessionId: string): Promise<void> {
+ const entry = pending.get(sessionId);
+ if (!entry || entry.watching || entry.inFlight) return;
+ entry.watching = true;
+ try {
+ // The turn that carried the offer ends first; a prompt queued behind it gets a moment to start.
+ while ((await deps.busy(sessionId)) !== false) {
+ if (pending.get(sessionId) !== entry) return;
+ await sleep(deps.pollMs ?? 1_000);
+ }
+ await sleep(deps.idleSettleMs ?? 600);
+ while (pending.get(sessionId) === entry && !entry.inFlight && (await deps.busy(sessionId)) === false) {
+ const offered = await update(sessionId, entry, "offer");
+ if (offered === "decline" || offered === "done") { pending.delete(sessionId); break; }
+ if (offered === "failed" && now() - (entry.failedAt ?? 0) > RETRY_WINDOW_MS) { pending.delete(sessionId); break; }
+ if (startWrapNow({ choice: offered, busy: false, inFlight: entry.inFlight })) { await compact(sessionId, entry); continue; }
+ await sleep(3_000);
+ }
+ if (pending.get(sessionId) === entry && !entry.inFlight) void update(sessionId, entry, "hold");
+ } finally {
+ entry.watching = false;
+ }
+ }
+
+ return {
+ /** A model response the broker forwarded for `sessionId`. */
+ noteResponse(sessionId: string, headers: Headers): void {
+ const current = pending.get(sessionId);
+ if (headers.get(WRAP_UP_HEADER) === "1") return;
+ if (headers.get(REQUIRED_HEADER) !== "1") {
+ if (current && !current.inFlight) {
+ pending.delete(sessionId);
+ void update(sessionId, current, "clear");
+ }
+ return;
+ }
+ const id = headers.get(OFFER_HEADER) || `legacy:${headers.get(SCOPES_HEADER) ?? ""}`;
+ if (!replacesWrapOffer(current ? { offerID: current.id } : undefined, id) || current?.inFlight) return;
+ if (current) void update(sessionId, current, "clear");
+ pending.delete(sessionId);
+ pending.set(sessionId, { id, scopes: headers.get(SCOPES_HEADER) || "token", watching: false, inFlight: false, approved: false });
+ while (pending.size > MAX_TRACKED) pending.delete(pending.keys().next().value as string);
+ void watch(sessionId);
+ },
+ /** Whether a compaction request of `sessionId` is an accepted wrap (the gateway holds it as the wrap). */
+ contextWrap(sessionId: string): boolean {
+ return pending.get(sessionId)?.approved === true;
+ },
+ };
+}
diff --git a/apps/server/src/engine-v2-next.test.ts b/apps/server/src/engine-v2-next.test.ts
index 28959b9f..50ac9421 100644
--- a/apps/server/src/engine-v2-next.test.ts
+++ b/apps/server/src/engine-v2-next.test.ts
@@ -144,3 +144,33 @@ test("steering and automatic compaction use the 2.x engine's routes", async () =
expect(calls).toContain("DELETE /api/session/ses_busy/inbox/cmp_1");
expect(calls).toContain("POST /api/session/ses_busy/interrupt");
});
+
+test("the quota wrap on the 2.x engine: asked only while idle, an accepted offer compacts, a declined one is gone", async () => {
+ const { createV2BudgetWrap } = await import("./engine-v2-budget-wrap.js");
+ const asked: string[] = [];
+ const compacted: string[] = [];
+ let busy = true;
+ let answer = "accept";
+ const wrap = createV2BudgetWrap({
+ control: async (_id, args) => { asked.push(`${args.sessionID}:${args.operation}`); return args.operation === "offer" ? answer : "ok"; },
+ busy: async () => busy,
+ compact: async (sessionId) => { compacted.push(sessionId); },
+ sleep: async () => { busy = false; },
+ idleSettleMs: 0,
+ });
+ const offer = new Headers({ "x-omnirush-wrap-required": "1", "x-omnirush-wrap-offer-id": "off_1" });
+ wrap.noteResponse("ses_a", offer);
+ for (let i = 0; i < 50 && compacted.length === 0; i += 1) await Bun.sleep(1);
+ expect(compacted).toEqual(["ses_a"]);
+ expect(asked).toEqual(["ses_a:offer", "ses_a:finish"]);
+ expect(wrap.contextWrap("ses_a")).toBe(false);
+
+ answer = "decline";
+ asked.length = 0;
+ busy = false;
+ wrap.noteResponse("ses_b", new Headers({ "x-omnirush-wrap-required": "1", "x-omnirush-wrap-offer-id": "off_2" }));
+ for (let i = 0; i < 50 && asked.length === 0; i += 1) await Bun.sleep(1);
+ await Bun.sleep(5);
+ expect(asked).toEqual(["ses_b:offer"]);
+ expect(compacted).toEqual(["ses_a"]);
+});
diff --git a/apps/server/src/omnirush-gateway-broker.ts b/apps/server/src/omnirush-gateway-broker.ts
index dea5fe9b..fa4c451d 100644
--- a/apps/server/src/omnirush-gateway-broker.ts
+++ b/apps/server/src/omnirush-gateway-broker.ts
@@ -65,6 +65,10 @@ export type SubagentModelFallbackEvent = {
};
type BrokerOptions = {
+ /** Every model response forwarded for a chat (the 2.x engine's quota wrap, engine-v2-budget-wrap.ts). */
+ onModelResponse?: (sessionId: string, headers: Headers) => void;
+ /** Whether a chat's compaction is an accepted wrap: the gateway then holds it as the wrap. */
+ contextWrap?: (sessionId: string) => boolean;
credentials?: OmniRushGatewayCredentials;
engineToken?: string;
/** Durable model pins; production supplies the runtime-local state file. */
@@ -1355,6 +1359,9 @@ export class OmniRushGatewayBroker {
})).then(async (response) => ({ status: response.status, body: await response.json().catch(() => null) })), timeoutMs ? { timeoutMs } : {});
}
+ private readonly onModelResponse?: BrokerOptions["onModelResponse"];
+ private readonly contextWrap?: BrokerOptions["contextWrap"];
+
constructor(options: BrokerOptions) {
const gatewayUrl = options.credentials ? normalizedGatewayUrl(options.credentials.gatewayUrl) : null;
this.state = gatewayUrl && options.credentials?.accessToken && options.credentials.refreshToken
@@ -1365,6 +1372,8 @@ export class OmniRushGatewayBroker {
}
: null;
this.engineToken = options.engineToken?.trim() ?? "";
+ this.onModelResponse = options.onModelResponse;
+ this.contextWrap = options.contextWrap;
this.sessionModelsFile = options.sessionModelsFile;
if (this.sessionModelsFile) {
try {
@@ -1522,6 +1531,10 @@ export class OmniRushGatewayBroker {
response = await this.subagentFallback(request, normalizedPath, body, response, requestId);
}
await this.noteUpdateSignals(response, true);
+ const chat = request.headers.get("x-omnirush-session-id");
+ if (chat && this.onModelResponse) {
+ try { this.onModelResponse(chat, response.headers); } catch { /* the wrap offer is best effort */ }
+ }
const contentType = response.headers.get("content-type") ?? "";
// Sign-in failures keep their own answer above; every other gateway
// refusal reaches the user as readable copy instead of a bare code.
@@ -2168,6 +2181,9 @@ export class OmniRushGatewayBroker {
const value = request.headers.get(name);
if (value) headers.set(name, value);
}
+ // The 2.x engine's compaction of a chat whose wrap was accepted (its session plugin names the request's kind).
+ const wrapSession = request.headers.get("x-omnirush-session-id");
+ if (wrapSession && request.headers.get("x-omnirush-request-kind") === "compaction" && this.contextWrap?.(wrapSession)) headers.set("x-omnirush-context-wrap", "1");
// A box (sandbox) chat's request: the gateway judges its model in the box mode.
if (this.boxRequest(request)) headers.set(BOX_HEADER, "1");
// Strict sessions: every request of a chat says so (the gateway binds it itself if ours failed).
diff --git a/apps/server/src/opencode-plugins/omnirush-session-next.ts b/apps/server/src/opencode-plugins/omnirush-session-next.ts
index 951f0e7d..b714079b 100644
--- a/apps/server/src/opencode-plugins/omnirush-session-next.ts
+++ b/apps/server/src/opencode-plugins/omnirush-session-next.ts
@@ -36,6 +36,8 @@ export default {
await ctx.session.hook("model.request", async (event) => {
if (event.model.providerID !== PROVIDER_ID || !event.sessionID) return;
event.headers["x-omnirush-session-id"] = event.sessionID;
+ // The server's broker marks an accepted quota wrap's compaction (engine-v2-budget-wrap.ts).
+ if (event.kind === "compaction") event.headers["x-omnirush-request-kind"] = "compaction";
const session = await info(event.sessionID);
if (typeof session.parentID === "string" && session.parentID) event.headers["x-parent-session-id"] = session.parentID;
if (event.kind === "title") {
diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts
index 341b3ca7..90c16976 100644
--- a/apps/server/src/server.ts
+++ b/apps/server/src/server.ts
@@ -233,6 +233,7 @@ import { startThreadApprovalReplayer, type ThreadApprovalReplayer } from "./thre
import { CloudProviderSync, parseCloudProviderDenSession } from "./cloud-provider-sync.js";
import { createEngineV2Preview, resolveInitialEngineV2PreviewState, type EngineV2Preview } from "./engine-v2-preview.js";
import { MIGRATED_FROM } from "./engine-v2-migration.js";
+import { createV2BudgetWrap } from "./engine-v2-budget-wrap.js";
import { APP_SECRET_HEADER, appRequestRefusal } from "./app-request-gate.js";
import { isUploadFileDenied, sessionUploaderEnabled } from "./session-uploader.js";
import { editorUserEditEvent, editorUserReviewEvent } from "./editor-events.js";
@@ -1275,7 +1276,11 @@ export async function startServer(config: ServerConfig): Promise<ServeResult> {
}
: undefined
);
+ // The quota wrap on the 2.x engine (engine-v2-budget-wrap.ts): set once the engine below exists.
+ let budgetWrap: ReturnType<typeof createV2BudgetWrap> | undefined;
const gatewayBroker = new OmniRushGatewayBroker({
+ onModelResponse: (sessionId, headers) => budgetWrap?.noteResponse(sessionId, headers),
+ contextWrap: (sessionId) => budgetWrap?.contextWrap(sessionId) ?? false,
sessionModelsFile: join(runtimeStorageDir(config), "chat-models.json"),
log: (level, message, attributes) => logger.log(level, message, attributes),
credentials: gatewayCredentials
@@ -1540,6 +1545,33 @@ export async function startServer(config: ServerConfig): Promise<ServeResult> {
};
const engineV2Preview = createEngineV2Preview({ config, env, deferStart: true });
engineV2ByServer.set(config, engineV2Preview);
+ /** A chat on the 2.x engine, read where it lives (its folder), or null. */
+ const v2ChatTarget = async (sessionId: string): Promise<EngineTarget | null> => {
+ const v2 = engineV2Preview.connection();
+ if (!v2) return null;
+ const headers = new Headers({ authorization: `Basic ${Buffer.from(`opencode:${v2.password}`).toString("base64")}` });
+ const found: unknown = await loopbackFetch(buildOpencodeProxyUrl(v2.url, `/api/session/${encodeURIComponent(sessionId)}`, ""), { headers, signal: AbortSignal.timeout(5_000) })
+ .then(async (response) => (response.ok ? response.json() : null)).catch(() => null);
+ const info = isRecord(found) && isRecord(found.data) ? found.data : null;
+ const directory = info && isRecord(info.location) && typeof info.location.directory === "string" ? info.location.directory : null;
+ return directory ? engineTarget(v2.url, headers, `?${new URLSearchParams({ "location[directory]": directory })}`, "v2") : null;
+ };
+ budgetWrap = createV2BudgetWrap({
+ control: async (id, args) => {
+ const result: unknown = await uiControl.request("query", { id, args });
+ if (!isRecord(result) || result.ok !== true || !isRecord(result.result)) return "unavailable";
+ return typeof result.result.choice === "string" ? result.result.choice : "ok";
+ },
+ busy: async (sessionId) => {
+ const target = await v2ChatTarget(sessionId);
+ return target ? desktopAutoCompactEngine(target, sessionId).busy() : null;
+ },
+ compact: async (sessionId) => {
+ const target = await v2ChatTarget(sessionId);
+ if (!target) throw new Error("the chat is not on the engine");
+ await desktopAutoCompactEngine(target, sessionId).summarize({ providerID: "omnirush", modelID: "" }, AbortSignal.timeout(5 * 60_000));
+ },
+ });
const bestPracticesReloads = new BestPracticesEngineReloads({
isBusy: (workspace) => engineHasActiveSessions(config, workspace, true),
reload: (workspace) => reloadOpencodeEngine(config, workspace, engineMcpServerState, { reason: "engine_reload" }),