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
Loading