@@ -79,7 +79,7 @@ import { MAX_STEPS_PROMPT } from "./max-steps" |
79 | 79 | * - [ ] Update title, summaries, compaction state, and cleanup in bounded background work. |
80 | 80 | * |
81 | 81 | * Use `llm.stream(request)` for each provider turn. Keep tool execution and continuation here. |
82 | | - * Durable activity recovery remains a separate future slice with an explicit retry policy. |
| 82 | + * Durable continuation recovery remains a separate future slice with an explicit retry policy. |
83 | 83 | * |
84 | 84 | * The current slice loads V2 history, translates it, resolves a model through a core service, and persists one |
85 | 85 | * provider turn. Registry definitions are advertised, local tool calls are settled durably, and an |
@@ -142,20 +142,19 @@ export const layer = Layer.effect( |
142 | 142 |
|
143 | 143 | type TurnTransition = |
144 | 144 | // Automatic compaction completed; rebuild the request from compacted history. |
145 | | - | { readonly _tag: "ContinueAfterCompaction" } |
| 145 | + | { readonly _tag: "ContinueAfterCompaction"; readonly step: number } |
146 | 146 | // Overflow compaction completed; rebuild once through the path without overflow recovery. |
147 | | - | { readonly _tag: "ContinueAfterOverflowCompaction" } |
| 147 | + | { readonly _tag: "ContinueAfterOverflowCompaction"; readonly step: number } |
148 | 148 |
|
149 | 149 | class TurnTransitionError extends Error { |
150 | 150 | constructor(readonly transition: TurnTransition) { |
151 | 151 | super() |
152 | 152 | } |
153 | 153 | } |
154 | 154 |
|
155 | | - const continueAfterCompaction = new TurnTransitionError({ _tag: "ContinueAfterCompaction" }) |
156 | | - const continueAfterOverflowCompaction = new TurnTransitionError({ |
157 | | - _tag: "ContinueAfterOverflowCompaction", |
158 | | - }) |
| 155 | + const continueAfterCompaction = (step: number) => new TurnTransitionError({ _tag: "ContinueAfterCompaction", step }) |
| 156 | + const continueAfterOverflowCompaction = (step: number) => |
| 157 | + new TurnTransitionError({ _tag: "ContinueAfterOverflowCompaction", step }) |
159 | 158 |
|
160 | 159 | const loadSystemContext = (agent: AgentV2.Selection) => |
161 | 160 | Effect.all([systemContext.load(), skillGuidance.load(agent), referenceGuidance.load()], { |
@@ -175,20 +174,23 @@ export const layer = Layer.effect( |
175 | 174 | const initialized = yield* SessionContextEpoch.initialize(db, loadSystemContext(agent), session.id) |
176 | 175 | const toolFibers = yield* FiberSet.make<void, ToolOutputStore.Error>() |
177 | 176 | let needsContinuation = false |
| 177 | + let currentStep = step |
178 | 178 | if (promotion) { |
179 | 179 | const cutoff = yield* EventV2.latestSequence(db, session.id) |
180 | | - if (promotion === "steer") yield* SessionInput.promoteSteers(db, events, session.id, cutoff) |
| 180 | + let promoted = 0 |
| 181 | + if (promotion === "steer") promoted = yield* SessionInput.promoteSteers(db, events, session.id, cutoff) |
181 | 182 | if (promotion === "queue") { |
182 | | - yield* SessionInput.promoteNextQueued(db, events, session.id) |
183 | | - yield* SessionInput.promoteSteers(db, events, session.id, cutoff) |
| 183 | + promoted += Number(yield* SessionInput.promoteNextQueued(db, events, session.id)) |
| 184 | + promoted += yield* SessionInput.promoteSteers(db, events, session.id, cutoff) |
184 | 185 | } |
| 186 | + if (promoted > 0) currentStep = 1 |
185 | 187 | } |
186 | 188 | const system = |
187 | 189 | initialized ?? (yield* SessionContextEpoch.prepare(db, events, loadSystemContext(agent), session.id)) |
188 | 190 | const model = yield* models.resolve(session) |
189 | 191 | const entries = yield* SessionHistory.entriesForRunner(db, session.id, system.baselineSeq) |
190 | 192 | const context = entries.map((entry) => entry.message) |
191 | | - const isLastStep = agent.info?.steps !== undefined && step >= agent.info.steps |
| 193 | + const isLastStep = agent.info?.steps !== undefined && currentStep >= agent.info.steps |
192 | 194 | const toolMaterialization = isLastStep ? undefined : yield* tools.materialize(agent.info?.permissions) |
193 | 195 | const promptCacheKey = /^ses_[0-9a-f]{64}$/.test(session.id) ? session.id.slice(4) : session.id |
194 | 196 | const request = LLM.request({ |
@@ -202,7 +204,7 @@ export const layer = Layer.effect( |
202 | 204 | toolChoice: isLastStep ? "none" : undefined, |
203 | 205 | }) |
204 | 206 | if (yield* compaction.compactIfNeeded({ sessionID: session.id, entries, model, request })) |
205 | | - return yield* Effect.die(continueAfterCompaction) |
| 207 | + return yield* Effect.die(continueAfterCompaction(currentStep)) |
206 | 208 | const publisher = createLLMEventPublisher(events, { |
207 | 209 | sessionID: session.id, |
208 | 210 | agent: agent.id, |
@@ -272,7 +274,7 @@ export const layer = Layer.effect( |
272 | 274 | isContextOverflowFailure(overflowFailure ?? failure) && |
273 | 275 | (yield* restore(recoverOverflow({ sessionID: session.id, entries, model, request }))) |
274 | 276 | ) |
275 | | - return yield* Effect.die(continueAfterOverflowCompaction) |
| 277 | + return yield* Effect.die(continueAfterOverflowCompaction(currentStep)) |
276 | 278 | if (overflowFailure) yield* publish(overflowFailure) |
277 | 279 | const llmFailure = failure instanceof LLMError ? failure : undefined |
278 | 280 | if (llmFailure && !publisher.hasProviderError()) { |
@@ -306,15 +308,15 @@ export const layer = Layer.effect( |
306 | 308 | yield* withPublication(publisher.failUnsettledTools("Provider did not return a tool result", true)) |
307 | 309 | if (stream._tag === "Failure") return yield* Effect.failCause(stream.cause) |
308 | 310 | if (settled._tag === "Failure") return yield* Effect.failCause(settled.cause) |
309 | | - return !publisher.hasProviderError() && needsContinuation |
| 311 | + return { needsContinuation: !publisher.hasProviderError() && needsContinuation, step: currentStep } |
310 | 312 | }), |
311 | 313 | ) |
312 | 314 | }, Effect.scoped) |
313 | 315 | type RunTurn = ( |
314 | 316 | sessionID: SessionSchema.ID, |
315 | 317 | promotion: SessionInput.Delivery | undefined, |
316 | 318 | step: number, |
317 | | - ) => Effect.Effect<boolean, RunError> |
| 319 | + ) => Effect.Effect<{ readonly needsContinuation: boolean; readonly step: number }, RunError> |
318 | 320 |
|
319 | 321 | const runAfterOverflowCompaction: RunTurn = Effect.fnUntraced(function* (sessionID, promotion, step) { |
320 | 322 | return yield* runTurnAttempt(sessionID, promotion, step).pipe( |
@@ -324,7 +326,7 @@ export const layer = Layer.effect( |
324 | 326 | if (defect.transition._tag === "ContinueAfterOverflowCompaction") |
325 | 327 | return yield* Effect.die("Post-compaction provider attempt cannot recover another overflow") |
326 | 328 | yield* Effect.yieldNow |
327 | | - return yield* runAfterOverflowCompaction(sessionID, undefined, step) |
| 329 | + return yield* runAfterOverflowCompaction(sessionID, undefined, defect.transition.step) |
328 | 330 | }), |
329 | 331 | ), |
330 | 332 | ) |
@@ -337,8 +339,8 @@ export const layer = Layer.effect( |
337 | 339 | if (!(defect instanceof TurnTransitionError)) return yield* Effect.die(defect) |
338 | 340 | yield* Effect.yieldNow |
339 | 341 | if (defect.transition._tag === "ContinueAfterOverflowCompaction") |
340 | | - return yield* runAfterOverflowCompaction(sessionID, undefined, step) |
341 | | - return yield* runTurn(sessionID, undefined, step) |
| 342 | + return yield* runAfterOverflowCompaction(sessionID, undefined, defect.transition.step) |
| 343 | + return yield* runTurn(sessionID, undefined, defect.transition.step) |
342 | 344 | }), |
343 | 345 | ), |
344 | 346 | ) |
@@ -353,16 +355,19 @@ export const layer = Layer.effect( |
353 | 355 | if (!input.force && !hasSteer && !hasQueue) return |
354 | 356 | yield* failInterruptedTools(input.sessionID) |
355 | 357 | let promotion: SessionInput.Delivery | undefined = hasSteer ? "steer" : hasQueue ? "queue" : undefined |
356 | | - let openActivity = input.force || hasSteer || hasQueue |
357 | | - while (openActivity) { |
| 358 | + let shouldRun = input.force || hasSteer || hasQueue |
| 359 | + while (shouldRun) { |
358 | 360 | let needsContinuation = true |
359 | | - for (let step = 1; needsContinuation; step++) { |
360 | | - needsContinuation = yield* runTurn(input.sessionID, promotion, step) |
| 361 | + let step = 1 |
| 362 | + while (needsContinuation) { |
| 363 | + const result = yield* runTurn(input.sessionID, promotion, step) |
| 364 | + needsContinuation = result.needsContinuation |
| 365 | + step = result.step + 1 |
361 | 366 | promotion = "steer" |
362 | 367 | if (!needsContinuation) needsContinuation = yield* SessionInput.hasPending(db, input.sessionID, "steer") |
363 | 368 | } |
364 | | - openActivity = yield* SessionInput.hasPending(db, input.sessionID, "queue") |
365 | | - promotion = openActivity ? "queue" : undefined |
| 369 | + shouldRun = yield* SessionInput.hasPending(db, input.sessionID, "queue") |
| 370 | + promotion = shouldRun ? "queue" : undefined |
366 | 371 | } |
367 | 372 | }) |
368 | 373 |
|
|