Skip to content
Merged
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
2 changes: 2 additions & 0 deletions packages/core/src/codemode/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ export const create = (
...context,
// Progress replaces the execute call's own, so an inner tool's updates would hide the call rows.
progress: () => Effect.void,
// A stop is the execute call's to handle: a nested call never fails it with its own error.
cancel: undefined,
}).pipe(
Effect.mapError((error) => new ToolFailure({ message: toSessionError(error).message, error })),
)
Expand Down
12 changes: 11 additions & 1 deletion packages/core/src/session/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ export const layer = Layer.effect(
const releaseOnCommit = (sessionID: SessionSchema.ID) => ({
commit: () => store.release(sessionID),
})
// Why a Session's execution is being interrupted, read from the coordinator once it exists.
let interruption: (sessionID: SessionSchema.ID) => InterruptReason | undefined = () => undefined
const drain = Effect.fnUntraced(function* (
sessionID: SessionSchema.ID,
force: boolean,
Expand All @@ -91,7 +93,14 @@ export const layer = Layer.effect(
const session = yield* store.get(sessionID)
if (!session) return yield* Effect.die(new Error(`Session not found: ${sessionID}`))
const result = yield* SessionRunner.Service.use((runner) =>
runner.drain({ sessionID, force, continuation, promotable, stepped }),
runner.drain({
sessionID,
force,
continuation,
promotable,
stepped,
interruption: () => interruption(sessionID),
}),
).pipe(
instances.provide(session),
Effect.tapCause((cause) =>
Expand Down Expand Up @@ -145,6 +154,7 @@ export const layer = Layer.effect(
),
})

interruption = coordinator.interruption
return Service.of({
active: coordinator.active,
isActive: coordinator.isActive,
Expand Down
14 changes: 13 additions & 1 deletion packages/core/src/session/run-coordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface Coordinator<Key, E, Reason = never> {
* interrupted.
*/
readonly interrupt: (key: Key, reason?: Reason) => Effect.Effect<boolean>
/** Why the key's execution is being stopped, once `interrupt` stopped it; undefined for teardown or when running. */
readonly interruption: (key: Key) => Reason | undefined
/** Resolves once no execution is active for the key. Returns immediately when already idle and never starts work. */
readonly awaitIdle: (key: Key) => Effect.Effect<void>
}
Expand Down Expand Up @@ -170,5 +172,15 @@ export const make = <Key, E, Reason = never>(options: {
return Deferred.await(execution.done).pipe(Effect.ignoreCause, Effect.andThen(awaitIdle(key)))
})

return { active: Effect.sync(() => new Set(executions.keys())), isActive, run, wake, interrupt, awaitIdle }
const interruption = (key: Key) => executions.get(key)?.interruptionReason

return {
active: Effect.sync(() => new Set(executions.keys())),
isActive,
run,
wake,
interrupt,
interruption,
awaitIdle,
}
})
2 changes: 2 additions & 0 deletions packages/core/src/session/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface Interface {
readonly promotable?: Promotable
/** The previous drain ran a step before handing off, so reaching idle still runs the idle hook. */
readonly stepped?: boolean
/** Why the execution is being interrupted, once it is: `"user"` when someone stopped it. */
readonly interruption?: () => "user" | "shutdown" | undefined
}) => Effect.Effect<DrainResult, RunError>
}

Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/session/runner/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ const layer = Layer.effect(
while (true) {
const next = yield* advanceToStep()
if (next._tag !== "Ready") return next
continuing = yield* runStep(next.context, step)
continuing = yield* runStep(next.context, step, input.interruption)
stepped = true
step++
force = false
Expand All @@ -232,7 +232,11 @@ const layer = Layer.effect(
})

/** Owns logical Step policy; each attempt owns its streaming, tools, and durable settlement. */
const runStep = Effect.fn("SessionRunner.runStep")(function* (first: SessionContext.Loaded, step: number) {
const runStep = Effect.fn("SessionRunner.runStep")(function* (
first: SessionContext.Loaded,
step: number,
interruption: DrainInput["interruption"],
) {
const sessionID = first.session.id
let assistantMessageID = SessionMessage.ID.create()
const retry = yield* SessionRunnerRetry.make(bus, sessionID)
Expand Down Expand Up @@ -288,6 +292,7 @@ const layer = Layer.effect(
)
const outcome = yield* steps.attempt({
isLocationClosed: lifecycle.isClosed,
interruption,
sessionID,
assistantMessageID,
agent: loaded.agent.id,
Expand Down
79 changes: 66 additions & 13 deletions packages/core/src/session/runner/step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
type ToolCall,
} from "@opencode/ai/effect"
import type { Agent } from "@opencode/schema/agent"
import { Cause, Clock, Data, Effect, Exit, Fiber, Option, Stream } from "effect"
import { Cause, Clock, Data, Deferred, Effect, Exit, Fiber, Option, Stream } from "effect"
import { SessionError } from "@opencode/schema/session-error"
import { Bus } from "../../bus.js"
import { Permission } from "../../permission.js"
Expand Down Expand Up @@ -42,6 +42,8 @@ export const Outcome = Data.taggedEnum<Outcome>()

interface Input {
readonly isLocationClosed: () => boolean
/** Why the execution is being interrupted, once it is: `"user"` when someone stopped it. */
readonly interruption?: () => "user" | "shutdown" | undefined
readonly sessionID: SessionSchema.ID
readonly assistantMessageID: SessionMessage.ID
readonly agent: Agent.ID
Expand Down Expand Up @@ -97,9 +99,21 @@ export const make = Effect.gen(function* () {
const toolRuns: Array<{
readonly call: ToolCall
readonly fiber: Fiber.Fiber<void, Permission.DeclinedError | QuestionTool.CancelledError>
readonly cancel: Cancel
}> = []
const interruptTools = Effect.suspend(() => Fiber.interruptAll(toolRuns.map((run) => run.fiber)))
const executeTool = (call: ToolCall) => {
// A user's stop asks each tool awaiting `cancel` to stop and settle its own call, and interrupts every other
// tool. A tool that does not settle in time is interrupted too. Teardown interrupts every tool.
const stopTools = Effect.suspend(() => {
// A reloaded Location still serves the steps that borrow it, so its tools can stop on their own too.
const user = input.interruption?.() === "user"
const immediate = toolRuns.flatMap((run) => (user && run.cancel.trigger() ? [] : [run.fiber]))
return Fiber.interruptAll(immediate).pipe(
Effect.andThen(Fiber.awaitAll(toolRuns.map((run) => run.fiber)).pipe(Effect.timeoutOption(COOPERATIVE_STOP))),
Effect.flatMap((settled) => (Option.isSome(settled) ? Effect.void : interruptTools)),
)
})
const executeTool = (call: ToolCall, cancel: Cancel) => {
if (input.prepared.request.toolChoice?.type === "none")
return new Tool.Error({ message: "Tools are disabled after the maximum agent steps" })
return input.prepared.executeTool({
Expand All @@ -108,6 +122,7 @@ export const make = Effect.gen(function* () {
messageID: input.assistantMessageID,
call,
progress: (update) => publisher.progress(call.id, update),
cancel: cancel.effect,
})
}

Expand All @@ -126,10 +141,12 @@ export const make = Effect.gen(function* () {
if (firstOutputAt === undefined && publisher.record().outputStarted)
firstOutputAt = yield* Clock.currentTimeMillis
if (event.type !== "tool-call" || event.providerExecuted) return
const cancel = makeCancel()
toolRuns.push({
call: event,
cancel,
fiber: yield* Effect.uninterruptibleMask((restore) =>
restore(executeTool(event)).pipe(
restore(executeTool(event, cancel)).pipe(
Effect.flatMap(toolOutput.truncate),
Effect.flatMap((outcome) => publisher.toolExecution(event.id, event.name, outcome)),
Effect.catchTag("Tool.Error", (error) =>
Expand Down Expand Up @@ -158,10 +175,13 @@ export const make = Effect.gen(function* () {
)
return yield* Effect.failCause(stream.cause)
if (publisher.hasStarted()) yield* publisher.streamed()
if (streamInterrupted) yield* interruptTools
const joined = yield* restore(Fiber.awaitAll(toolRuns.map((run) => run.fiber))).pipe(Effect.exit)
if (Exit.isFailure(joined)) yield* interruptTools
const tools = classifyToolExits(joined, toolRuns)
if (streamInterrupted) yield* stopTools
const fibers = toolRuns.map((run) => run.fiber)
const joined = yield* restore(Fiber.awaitAll(fibers)).pipe(Effect.exit)
if (!streamInterrupted && Exit.isFailure(joined)) yield* stopTools
// Read each tool's exit once all have ended, so what they raised while stopping (a decline, a defect) counts.
const exits = Exit.isSuccess(joined) ? joined.value : yield* Fiber.awaitAll(fibers)
const tools = classifyToolExits(exits, toolRuns, Exit.hasInterrupts(joined))

if (
!publisher.record().outputStarted &&
Expand Down Expand Up @@ -311,26 +331,59 @@ const isInterruptedStream = (failure: AIError) => {

/** Tool.Error settles in each fiber; only user declines remain in the typed error channel. */
const classifyToolExits = (
settled: Exit.Exit<Array<Exit.Exit<void, Permission.DeclinedError | QuestionTool.CancelledError>>>,
exits: ReadonlyArray<Exit.Exit<void, Permission.DeclinedError | QuestionTool.CancelledError>>,
runs: ReadonlyArray<{ readonly call: ToolCall }>,
// The step was interrupted while it waited for its tools.
interruptedWhileWaiting: boolean,
) => {
const exits = Exit.isSuccess(settled) ? settled.value : []
const declines = exits.flatMap((exit, index) =>
Exit.isFailure(exit)
? exit.cause.reasons.flatMap((reason) =>
Cause.isFailReason(reason) ? [{ call: runs[index].call, reason: reason.error }] : [],
)
: [],
)
const causes = Exit.isFailure(settled)
? [settled.cause]
: exits.flatMap((exit) => (Exit.isFailure(exit) ? [exit.cause] : []))
const causes = exits.flatMap((exit) => (Exit.isFailure(exit) ? [exit.cause] : []))
const failure = causes
.flatMap((cause) => {
if (Cause.hasInterrupts(cause)) return []
const reasons = cause.reasons.filter(Cause.isDieReason)
return reasons.length > 0 ? [Cause.fromReasons<never>(reasons)] : []
})
.at(0)
return { interrupted: causes.some(Cause.hasInterrupts), declines, failure }
return { interrupted: interruptedWhileWaiting || causes.some(Cause.hasInterrupts), declines, failure }
}

// How long a user's stop waits for the tools it asked to stop before interrupting them.
const COOPERATIVE_STOP = "5 seconds"

type Cancel = ReturnType<typeof makeCancel>

/**
* A tool call's `cancel`. `trigger` completes it, and reports whether the tool was awaiting it, so would stop on its
* own; a tool that was not is interrupted instead.
*/
const makeCancel = () => {
let listeners = 0
let cancelled = false
const gate = Deferred.makeUnsafe<void>()
return {
effect: Effect.suspend(() => {
if (cancelled) return Effect.void
listeners++
return Deferred.await(gate).pipe(
Effect.ensuring(
Effect.sync(() => {
listeners--
}),
),
)
}),
trigger: () => {
cancelled = true
if (listeners === 0) return false
Deferred.doneUnsafe(gate, Exit.void)
return true
},
}
}
Loading
Loading