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
17 changes: 12 additions & 5 deletions packages/opencode/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,20 @@ export namespace Plugin {
}

// Subscribe to bus events, fiber interrupted when scope closes
// Run plugin event handlers in parallel so slow handlers don't block the main event stream
yield* bus.subscribeAll().pipe(
Stream.runForEach((input) =>
Effect.sync(() => {
for (const hook of hooks) {
hook["event"]?.({ event: input as any })
}
}),
Effect.forEach(
hooks,
(hook) =>
Effect.tryPromise({
try: () => Promise.resolve(hook["event"]?.({ event: input as any })),
catch: (err) => {
log.error("plugin event handler failed", { error: err })
},
}).pipe(Effect.ignore),
{ concurrency: "unbounded", discard: true },
),
),
Effect.forkScoped,
)
Expand Down
28 changes: 18 additions & 10 deletions packages/opencode/src/session/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ export namespace SessionProcessor {

interface ProcessorContext extends Input {
toolcalls: Record<string, MessageV2.ToolPart>
toolCallHistory: MessageV2.ToolPart[]
shouldBreak: boolean
snapshot: string | undefined
blocked: boolean
needsCompaction: boolean
currentText: MessageV2.TextPart | undefined
reasoningMap: Record<string, MessageV2.ReasoningPart>
stepCount: number
}

type StreamEvent = Event
Expand Down Expand Up @@ -89,12 +91,14 @@ export namespace SessionProcessor {
sessionID: input.sessionID,
model: input.model,
toolcalls: {},
toolCallHistory: [],
shouldBreak: false,
snapshot: undefined,
blocked: false,
needsCompaction: false,
currentText: undefined,
reasoningMap: {},
stepCount: 0,
}
let aborted = false

Expand Down Expand Up @@ -180,12 +184,13 @@ export namespace SessionProcessor {
metadata: value.providerMetadata,
} satisfies MessageV2.ToolPart)

const parts = yield* Effect.promise(() => MessageV2.parts(ctx.assistantMessage.id))
const recentParts = parts.slice(-DOOM_LOOP_THRESHOLD)
// Track tool call history in-memory for doom loop detection
ctx.toolCallHistory.push(ctx.toolcalls[value.toolCallId])
const recentCalls = ctx.toolCallHistory.slice(-DOOM_LOOP_THRESHOLD)

if (
recentParts.length !== DOOM_LOOP_THRESHOLD ||
!recentParts.every(
recentCalls.length !== DOOM_LOOP_THRESHOLD ||
!recentCalls.every(
(part) =>
part.type === "tool" &&
part.tool === value.toolName &&
Expand Down Expand Up @@ -294,12 +299,15 @@ export namespace SessionProcessor {
}
ctx.snapshot = undefined
}
yield* Effect.promise(() =>
SessionSummary.summarize({
sessionID: ctx.sessionID,
messageID: ctx.assistantMessage.parentID,
}),
).pipe(Effect.ignoreCause({ log: true, message: "session summary failed" }), Effect.forkDetach)
ctx.stepCount++
if (ctx.stepCount === 1) {
yield* Effect.promise(() =>
SessionSummary.summarize({
sessionID: ctx.sessionID,
messageID: ctx.assistantMessage.parentID,
}),
).pipe(Effect.ignoreCause({ log: true, message: "session summary failed" }), Effect.forkDetach)
}
if (
!ctx.assistantMessage.summary &&
isOverflow({ cfg: yield* config.get(), tokens: usage.tokens, model: ctx.model })
Expand Down
56 changes: 45 additions & 11 deletions packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,21 @@ export namespace SessionPrompt {
const cache = yield* InstanceState.make(
Effect.fn("SessionPrompt.state")(function* () {
const runners = new Map<string, Runner<MessageV2.WithParts>>()
const cachedMessages = new Map<string, MessageV2.WithParts[]>()
const cachedTools = new Map<string, Record<string, AITool>>()
const lastAgentName = new Map<string, string>()
const lastModelKey = new Map<string, string>()
yield* Effect.addFinalizer(
Effect.fnUntraced(function* () {
yield* Effect.forEach(runners.values(), (r) => r.cancel, { concurrency: "unbounded", discard: true })
runners.clear()
cachedMessages.clear()
cachedTools.clear()
lastAgentName.clear()
lastModelKey.clear()
}),
)
return { runners }
return { runners, cachedMessages, cachedTools, lastAgentName, lastModelKey }
}),
)

Expand Down Expand Up @@ -1333,12 +1341,17 @@ NOTE: At any point in time through this workflow you should feel free to ask the
let structured: unknown | undefined
let step = 0
const session = yield* sessions.get(sessionID)
const s = yield* InstanceState.get(cache)

while (true) {
yield* status.set(sessionID, { type: "busy" })
log.info("loop", { step, sessionID })

let msgs = yield* Effect.promise(() => MessageV2.filterCompacted(MessageV2.stream(sessionID)))
// Use cached messages on subsequent iterations to avoid DB re-read
let msgs =
step > 0 && s.cachedMessages.has(sessionID)
? s.cachedMessages.get(sessionID)!
: yield* Effect.promise(() => MessageV2.filterCompacted(MessageV2.stream(sessionID)))

let lastUser: MessageV2.User | undefined
let lastAssistant: MessageV2.Assistant | undefined
Expand Down Expand Up @@ -1389,6 +1402,9 @@ NOTE: At any point in time through this workflow you should feel free to ask the
auto: task.auto,
overflow: task.overflow,
})
// Invalidate caches after compaction since messages change
s.cachedMessages.delete(sessionID)
s.cachedTools.delete(sessionID)
if (result === "stop") break
continue
}
Expand All @@ -1399,6 +1415,9 @@ NOTE: At any point in time through this workflow you should feel free to ask the
(yield* compaction.isOverflow({ tokens: lastFinished.tokens, model }))
) {
yield* compaction.create({ sessionID, agent: lastUser.agent, model: lastUser.model, auto: true })
// Invalidate caches after compaction
s.cachedMessages.delete(sessionID)
s.cachedTools.delete(sessionID)
continue
}

Expand Down Expand Up @@ -1441,15 +1460,23 @@ NOTE: At any point in time through this workflow you should feel free to ask the
const lastUserMsg = msgs.findLast((m) => m.info.role === "user")
const bypassAgentCheck = lastUserMsg?.parts.some((p) => p.type === "agent") ?? false

const tools = yield* resolveTools({
agent,
session,
model,
tools: lastUser.tools,
processor: handle,
bypassAgentCheck,
messages: msgs,
})
// Use cached tools if agent and model haven't changed
const modelKey = `${model.providerID}/${model.id}`
const toolsCacheValid =
s.cachedTools.has(sessionID) &&
s.lastAgentName.get(sessionID) === agent.name &&
s.lastModelKey.get(sessionID) === modelKey
const tools: Record<string, AITool> = toolsCacheValid
? s.cachedTools.get(sessionID)!
: yield* resolveTools({
agent,
session,
model,
tools: lastUser.tools,
processor: handle,
bypassAgentCheck,
messages: msgs,
})

if (lastUser.format?.type === "json_schema") {
tools["StructuredOutput"] = createStructuredOutputTool({
Expand Down Expand Up @@ -1534,6 +1561,13 @@ NOTE: At any point in time through this workflow you should feel free to ask the
overflow: !handle.message.finish,
})
}

// Cache messages and tools for next iteration to avoid DB re-read
s.cachedMessages.set(sessionID, msgs)
s.cachedTools.set(sessionID, tools)
s.lastAgentName.set(sessionID, agent.name)
s.lastModelKey.set(sessionID, `${model.providerID}/${model.id}`)

return "continue" as const
}),
Effect.fnUntraced(function* (exit) {
Expand Down
Loading