Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions packages/core/src/session/runner/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import { MAX_STEPS_PROMPT } from "./max-steps.js"

const CONTINUE_AFTER_INCOMPLETE_STREAM =
"The previous response was interrupted. Continue from where you left off without repeating completed content."
const CONTINUE_AFTER_MISSING_RESPONSE =
"The previous response stopped without producing an answer or tool call. Continue the task without repeating completed work."
const MISSING_RESPONSE_CONTINUATION_LIMIT = 1

const layer = Layer.effect(
Service,
Expand Down Expand Up @@ -83,6 +86,7 @@ const layer = Layer.effect(
let continuing = input.continuation !== undefined
let step = input.continuation?.step ?? 1
let entering = true
let missingResponseContinuations = 0
const promotable = input.promotable ?? "input"
if (!force && !continuing) {
const pending = yield* SessionInbox.nextPromotable(db, sessionID, "input")
Expand Down Expand Up @@ -177,7 +181,10 @@ const layer = Layer.effect(
yield* FiberMap.run(titles, sessionID, title.generate(sessionID).pipe(Effect.ignore), {
onlyIfMissing: true,
})
if (promoted > 0) step = 1
if (promoted > 0) {
step = 1
missingResponseContinuations = 0
}
return { _tag: "Ready" as const, context: yield* context.load(selected) }
}),
)
Expand All @@ -189,7 +196,19 @@ const layer = Layer.effect(
while (true) {
const next = yield* advanceToStep()
if (next._tag !== "Ready") return next
continuing = yield* runStep(next.context, step)
const result = yield* runStep(next.context, step)
const continueAfterMissingResponse =
result.responseMissing && missingResponseContinuations < MISSING_RESPONSE_CONTINUATION_LIMIT
if (continueAfterMissingResponse) {
missingResponseContinuations++
yield* bus.publish(SessionEvent.Synthetic, { sessionID, text: CONTINUE_AFTER_MISSING_RESPONSE })
} else if (result.responseMissing) {
yield* bus.publish(SessionEvent.Synthetic, {
sessionID,
text: "The model stopped again without producing an answer or tool call. Automatic continuation has stopped; await the user's next instruction.",
})
}
continuing = result.needsContinuation || continueAfterMissingResponse
step++
force = false
entering = false
Expand Down Expand Up @@ -257,7 +276,7 @@ const layer = Layer.effect(
: Effect.succeed(false),
),
})
if (outcome._tag === "Completed") return outcome.needsContinuation
if (outcome._tag === "Completed") return outcome
if (outcome._tag === "Retry" || outcome._tag === "Continue") {
yield* retry({ cause: outcome.cause, error: outcome.error, assistantMessageID }).pipe(
Pull.catchDone(() =>
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/session/runner/publish-llm-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const asRecord = (value: unknown): Record<string, unknown> =>
export interface StepRecord {
/** The model produced visible output this attempt, which bars transparent retries and overflow recovery. */
readonly outputStarted: boolean
/** The model produced a non-whitespace answer or accepted tool call. */
readonly responseProduced: boolean
/** The provider started any local or hosted tool input, accepted or partial. */
readonly hasToolActivity: boolean
readonly providerFailed: boolean
/** The step's recorded assistant failure, if any. */
readonly failure?: SessionError.Error
Expand Down Expand Up @@ -96,6 +100,7 @@ export const createLLMEventPublisher = (bus: Pick<Bus.Interface, "publish">, inp
let stepFailed = false
let providerFailed = false
let outputStarted = false
let responseProduced = false
let stepStreamed = false
let stepFailure: SessionError.Error | undefined
let stepSettlement: StepRecord["finish"]
Expand Down Expand Up @@ -393,6 +398,7 @@ export const createLLMEventPublisher = (bus: Pick<Bus.Interface, "publish">, inp
})
return
case "text-delta":
if (event.text.trim().length > 0) responseProduced = true
yield* text.append(event.id, event.text, providerState(event.providerMetadata))
return
case "text-end":
Expand Down Expand Up @@ -436,6 +442,7 @@ export const createLLMEventPublisher = (bus: Pick<Bus.Interface, "publish">, inp
return
case "tool-call": {
outputStarted = true
responseProduced = true
const tool = tools.get(event.id) ?? (yield* startToolInput(event))
if (toolInput.has(event.id)) yield* endToolInput(event)
if (tool.name !== event.name)
Expand Down Expand Up @@ -586,6 +593,8 @@ export const createLLMEventPublisher = (bus: Pick<Bus.Interface, "publish">, inp
/** Immutable snapshot of everything recorded for this step so far. */
record: (): StepRecord => ({
outputStarted,
responseProduced,
hasToolActivity: tools.size > 0,
providerFailed,
failure: stepFailure,
finish: stepSettlement,
Expand Down
52 changes: 49 additions & 3 deletions packages/core/src/session/runner/step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import { createLLMEventPublisher } from "./publish-llm-event.js"
import { SessionRunnerRetry } from "./retry.js"

export type Outcome = Data.TaggedEnum<{
Completed: { readonly needsContinuation: boolean }
Completed: {
readonly needsContinuation: boolean
readonly responseMissing: boolean
}
Retry: { readonly cause: AIError; readonly error: SessionError.Error }
Continue: { readonly cause: AIError; readonly error: SessionError.Error }
RecoverFull: {}
Expand All @@ -54,6 +57,16 @@ const TOOLS_INTERRUPTED = { type: "aborted", message: "Tool execution interrupte
const STEP_INTERRUPTED = { type: "aborted", message: "Step interrupted" } as const
const RESULT_MISSING = { type: "tool.result-missing", message: "Provider did not return a tool result" } as const

const incompleteResponse = (message: string) =>
new AIError({
module: "session",
method: "stream",
reason: new InvalidProviderOutputReason({
classification: "incomplete-stream",
message,
}),
})

/** Captures Location-scoped dependencies without introducing another service or execution loop. */
export const make = Effect.gen(function* () {
const bus = yield* Bus.Service
Expand Down Expand Up @@ -90,11 +103,12 @@ export const make = Effect.gen(function* () {
// Provider and tool fibers retain per-source order without a shared writer queue.
// A local execution starts only after its Tool.Called publication completes.
let overflowFailure: ProviderErrorEvent | undefined
let semanticEmptyProviderFailure: ProviderErrorEvent | undefined
// Read to the end, not just the finish event, so the next request can reuse this response.
const providerStream = llm.stream(input.prepared.request, input.prepared.options).pipe(
Stream.runForEach((event) =>
Effect.gen(function* () {
if (overflowFailure || publisher.hasProviderError()) return
if (overflowFailure || semanticEmptyProviderFailure || publisher.hasProviderError()) return
if (
LLMEvent.is.providerError(event) &&
isContextOverflowFailure(event) &&
Expand All @@ -103,6 +117,17 @@ export const make = Effect.gen(function* () {
overflowFailure = event
return
}
const record = publisher.record()
if (
LLMEvent.is.providerError(event) &&
record.outputStarted &&
!record.responseProduced &&
!record.hasToolActivity &&
!record.needsContinuation
) {
semanticEmptyProviderFailure = event
return
}
yield* publisher.publish(event)
if (event.type !== "tool-call" || event.providerExecuted) return
toolRuns.push({
Expand Down Expand Up @@ -157,7 +182,21 @@ export const make = Effect.gen(function* () {
}),
})
: undefined
const llmFailure = streamFailure instanceof AIError ? streamFailure : unknownFinish
const rawFailure = streamFailure instanceof AIError ? streamFailure : undefined
const semanticEmptyFailure =
recorded.outputStarted &&
!recorded.responseProduced &&
!recorded.hasToolActivity &&
!recorded.needsContinuation
? (semanticEmptyProviderFailure ?? rawFailure)
: undefined
const llmFailure = semanticEmptyFailure
? incompleteResponse(
semanticEmptyFailure instanceof AIError
? semanticEmptyFailure.reason.message
: semanticEmptyFailure.message,
)
: (rawFailure ?? unknownFinish)
const llmError = llmFailure && !recorded.providerFailed ? toSessionError(llmFailure) : undefined
if (
input.recoverContinuation &&
Expand Down Expand Up @@ -243,6 +282,13 @@ export const make = Effect.gen(function* () {
if (record.failure) return yield* new StepFailedError({ error: record.failure })
return Outcome.Completed({
needsContinuation: !input.toolsDisabled && record.needsContinuation,
responseMissing:
!input.toolsDisabled &&
record.finish?.finish === "stop" &&
record.finish.tokens.output + record.finish.tokens.reasoning > 0 &&
!record.responseProduced &&
!record.hasToolActivity &&
!record.needsContinuation,
})
}),
)
Expand Down
Loading
Loading