feat(server): continue threads after usage limits reset #12831
Closeditpropro(GitHub profile, opens in a new tab)wants to merge 3 commits into
main fromrefs/pull/12831/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- apps/server/src/orchestration/UsageLimitContinuation.ts line 166Resolvedby itpropro(GitHub profile, opens in a new tab)@@ -0,0 +145,21 @@145+ session: { ...thread.session, lastError: wait.errorMessage, updatedAt: now },146+ expectedSession: thread.session,147+ createdAt: now,148+ });149+ }).pipe(150+ // An explicit stop can update the session while its waiting banner is cleared.151+ Effect.retry({152+ times: 1,153+ while: (error) => error._tag === "OrchestrationCommandInvariantError",154+ }),155+ Effect.ignore,156+ );157+ });158+159+ const canceledSince = (160+ threadId: ThreadId,161+ wait: PendingUsageLimitContinuation,162+ sequence: number,163+ ) =>164+ engine165+ .readThreadEvents({apps/server/src/orchestration/UsageLimitContinuation.ts line 329Resolvedby itpropro(GitHub profile, opens in a new tab)@@ -0,0 +307,21 @@307+ if (Date.parse(wait.nextCheckAt) <= now)308+ pending.set(threadId, { ...wait, nextCheckAt: iso(now + RECHECK_MS) });309+ }310+ yield* Effect.logWarning("usage-limit continuation failed", cause);311+ }),312+ ),313+ ),314+ );315+316+ const recordFailure = Effect.fn("UsageLimitContinuation.recordFailure")(function* (317+ event: FailedTurn,318+ snapshotSequence: number,319+ ) {320+ yield* enqueue(321+ Effect.gen(function* () {322+ if (!event.payload.usageLimit || !event.turnId || !event.providerInstanceId) return;323+ if (handledFailures.get(event.threadId) === event.turnId) return;324+ const thread = yield* getThread(event.threadId);325+ if (thread?.latestTurn?.turnId !== event.turnId || thread.latestTurn.state !== "error")326+ return;327+ handledFailures.set(event.threadId, TurnId.make(event.turnId));apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2704Resolvedby itpropro(GitHub profile, opens in a new tab)@@ -2689,4 +2699,6 @@26892699 Stream.runForEach(providerService.streamEvents, (event) =>26902700 event.type === "turn.diff.updated"26912701 ? diffWorker.enqueue(event)2692- : worker.enqueue({ source: "runtime", event }),2702+ : // Include cancellations during queueing and failure projection in the recovery guard.2703+ orchestrationEngine.latestSequence.pipe(2704+ Effect.flatMap((snapshotSequence) =>
- apps/server/src/orchestration/UsageLimitContinuation.ts lines 53 to 55Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +34,21 @@34+import { OrchestrationEngineService } from "./Services/OrchestrationEngine.ts";35+import { ProjectionSnapshotQuery } from "./Services/ProjectionSnapshotQuery.ts";36+import { cancelsUsageLimitContinuation } from "./decider.ts";37+38+type FailedTurn = Extract<ProviderRuntimeEvent, { type: "turn.completed" }>;39+const RECHECK_MS = 5 * 60_000;40+const iso = (millis: number) => DateTime.formatIso(DateTime.makeUnsafe(millis));41+const sameModel = Schema.toEquivalence(ModelSelection);42+43+export class UsageLimitContinuation extends Context.Service<44+ UsageLimitContinuation,45+ {46+ readonly start: () => Effect.Effect<void, never, Scope.Scope>;47+ readonly recordFailure: (event: FailedTurn, snapshotSequence: number) => Effect.Effect<void>;48+ readonly drain: Effect.Effect<void>;49+ }50+>()("t3/orchestration/UsageLimitContinuation") {}51+52+function waitingMessage(pending: PendingUsageLimitContinuation): string {53+ return `Usage limit reached. Automatic continuation will check usage at ${pending.nextCheckAt}.`;54+}
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed