refactor(opencode): stop legacy v2 event emission (#33993)

This commit is contained in:
Kit Langton
2026-06-26 04:42:38 +02:00
committed by GitHub
parent 9859b19b93
commit a1f093a748
7 changed files with 111 additions and 555 deletions
@@ -13,14 +13,11 @@ import { Config } from "@/config/config"
import { NotFoundError } from "@/storage/storage"
import { Effect, Layer, Context } from "effect"
import * as DateTime from "effect/DateTime"
import { InstanceState } from "@/effect/instance-state"
import { isOverflow as overflow, usable } from "./overflow"
import { serviceUse } from "@opencode-ai/core/effect/service-use"
import { RuntimeFlags } from "@/effect/runtime-flags"
import { EventV2Bridge } from "@/event-v2-bridge"
import { SessionEvent } from "@opencode-ai/core/session/event"
import { SessionMessage } from "@opencode-ai/core/session/message"
import { ProviderV2 } from "@opencode-ai/core/provider"
import { ModelV2 } from "@opencode-ai/core/model"
import { buildPrompt } from "@opencode-ai/core/session/compaction"
@@ -355,18 +352,6 @@ export const layer = Layer.effect(
stripMedia: true,
toolOutputMaxChars: TOOL_OUTPUT_MAX_CHARS,
})
const tailIndex = selected.tail_start_id
? history.findIndex((message) => message.info.id === selected.tail_start_id)
: -1
const recent =
tailIndex < 0
? ""
: JSON.stringify(
yield* MessageV2.toModelMessagesEffect(history.slice(tailIndex), model, {
stripMedia: true,
toolOutputMaxChars: TOOL_OUTPUT_MAX_CHARS,
}),
)
const ctx = yield* InstanceState.context
const msg: SessionV1.Assistant = {
id: MessageID.ascending(),
@@ -520,25 +505,6 @@ export const layer = Layer.effect(
if (processor.message.error) return "stop"
if (result === "continue") {
const summary = summaryText(
(yield* session.messages({ sessionID: input.sessionID }).pipe(Effect.orDie)).find(
(item) => item.info.id === msg.id,
) ?? {
info: msg,
parts: [],
},
)
if (flags.experimentalEventSystem) {
if (summary)
yield* events.publish(SessionEvent.Compaction.Ended, {
sessionID: input.sessionID,
messageID: SessionMessage.ID.make(input.parentID),
timestamp: DateTime.makeUnsafe(Date.now()),
reason: input.auto ? "auto" : "manual",
text: summary ?? "",
recent,
})
}
yield* events.publish(Event.Compacted, { sessionID: input.sessionID })
}
return result
@@ -567,14 +533,6 @@ export const layer = Layer.effect(
auto: input.auto,
overflow: input.overflow,
})
if (flags.experimentalEventSystem) {
yield* events.publish(SessionEvent.Compaction.Started, {
sessionID: input.sessionID,
messageID: SessionMessage.ID.make(msg.id),
timestamp: DateTime.makeUnsafe(Date.now()),
reason: input.auto ? "auto" : "manual",
})
}
})
return Service.of({
+11 -366
View File
@@ -24,13 +24,7 @@ import { errorMessage } from "@/util/error"
import { isRecord } from "@/util/record"
import { EventV2Bridge } from "@/event-v2-bridge"
import { Database } from "@opencode-ai/core/database/database"
import { SessionEvent } from "@opencode-ai/core/session/event"
import { SessionMessage } from "@opencode-ai/core/session/message"
import { ModelV2 } from "@opencode-ai/core/model"
import { ProviderV2 } from "@opencode-ai/core/provider"
import * as DateTime from "effect/DateTime"
import { RuntimeFlags } from "@/effect/runtime-flags"
import { ToolOutput, Usage, type LLMEvent } from "@opencode-ai/llm"
import { Usage, type LLMEvent } from "@opencode-ai/llm"
const DOOM_LOOP_THRESHOLD = 3
export type Result = "compact" | "stop" | "continue"
@@ -64,13 +58,10 @@ export interface Interface {
}
type ToolCall = {
assistantMessageID?: SessionMessage.ID
partID: SessionV1.ToolPart["id"]
messageID: SessionV1.ToolPart["messageID"]
sessionID: SessionV1.ToolPart["sessionID"]
done: Deferred.Deferred<void>
inputEnded: boolean
raw: string
}
interface ProcessorContext extends Input {
@@ -80,9 +71,7 @@ interface ProcessorContext extends Input {
blocked: boolean
needsCompaction: boolean
currentText: SessionV1.TextPart | undefined
currentTextID: string | undefined
reasoningMap: Record<string, SessionV1.ReasoningPart>
v2AssistantMessageID: SessionMessage.ID | undefined
}
type StreamEvent = LLMEvent
@@ -104,7 +93,6 @@ export const layer = Layer.effect(
const status = yield* SessionStatus.Service
const image = yield* Image.Service
const events = yield* EventV2Bridge.Service
const flags = yield* RuntimeFlags.Service
const database = yield* Database.Service
const create = Effect.fn("SessionProcessor.create")(function* (input: Input) {
@@ -122,11 +110,8 @@ export const layer = Layer.effect(
blocked: false,
needsCompaction: false,
currentText: undefined,
currentTextID: undefined,
reasoningMap: {},
v2AssistantMessageID: undefined,
}
const mirrorAssistant = flags.experimentalEventSystem && !input.assistantMessage.summary
let aborted = false
const parse = (e: unknown) =>
@@ -141,34 +126,6 @@ export const layer = Layer.effect(
if (done) yield* Deferred.succeed(done, undefined).pipe(Effect.ignore)
})
const ensureV2AssistantMessage = Effect.fn("SessionProcessor.ensureV2AssistantMessage")(function* () {
if (ctx.v2AssistantMessageID) return ctx.v2AssistantMessageID
ctx.v2AssistantMessageID = SessionMessage.ID.create()
yield* events.publish(SessionEvent.Step.Started, {
sessionID: ctx.sessionID,
assistantMessageID: ctx.v2AssistantMessageID,
agent: input.assistantMessage.agent,
model: {
id: ModelV2.ID.make(ctx.model.id),
providerID: ProviderV2.ID.make(ctx.model.providerID),
variant: ModelV2.VariantID.make(input.assistantMessage.variant ?? "default"),
},
snapshot: ctx.snapshot,
timestamp: DateTime.makeUnsafe(Date.now()),
})
return ctx.v2AssistantMessageID
})
const requireV2AssistantMessage = (toolCall?: ToolCall) =>
toolCall?.assistantMessageID === undefined
? Effect.die("V2 tool settlement has no owning assistant message")
: Effect.succeed(toolCall.assistantMessageID)
const currentV2AssistantMessage = () =>
ctx.v2AssistantMessageID === undefined
? Effect.die("V2 step settlement has no owning assistant message")
: Effect.succeed(ctx.v2AssistantMessageID)
const readToolCall = Effect.fn("SessionProcessor.readToolCall")(function* (toolCallID: string) {
const call = ctx.toolcalls[toolCallID]
if (!call) return undefined
@@ -247,17 +204,6 @@ export const layer = Layer.effect(
const finishReasoning = Effect.fn("SessionProcessor.finishReasoning")(function* (reasoningID: string) {
if (!(reasoningID in ctx.reasoningMap)) return
// TODO(v2): Temporary dual-write while migrating session messages to v2 events.
if (mirrorAssistant) {
yield* events.publish(SessionEvent.Reasoning.Ended, {
sessionID: ctx.sessionID,
assistantMessageID: yield* currentV2AssistantMessage(),
reasoningID,
text: ctx.reasoningMap[reasoningID].text,
providerMetadata: ctx.reasoningMap[reasoningID].metadata,
timestamp: DateTime.makeUnsafe(Date.now()),
})
}
// oxlint-disable-next-line no-self-assign -- reactivity trigger
ctx.reasoningMap[reasoningID].text = ctx.reasoningMap[reasoningID].text
ctx.reasoningMap[reasoningID].time = { ...ctx.reasoningMap[reasoningID].time, end: Date.now() }
@@ -265,33 +211,6 @@ export const layer = Layer.effect(
delete ctx.reasoningMap[reasoningID]
})
const flushV2Fragments = Effect.fn("SessionProcessor.flushV2Fragments")(function* () {
if (!mirrorAssistant) return
if (!ctx.assistantMessage.summary && ctx.currentText && ctx.currentTextID) {
yield* events.publish(SessionEvent.Text.Ended, {
sessionID: ctx.sessionID,
assistantMessageID: yield* currentV2AssistantMessage(),
textID: ctx.currentTextID,
text: ctx.currentText.text,
timestamp: DateTime.makeUnsafe(Date.now()),
})
}
yield* Effect.forEach(Object.entries(ctx.reasoningMap), ([reasoningID, part]) =>
currentV2AssistantMessage().pipe(
Effect.flatMap((assistantMessageID) =>
events.publish(SessionEvent.Reasoning.Ended, {
sessionID: ctx.sessionID,
assistantMessageID,
reasoningID,
text: part.text,
providerMetadata: part.metadata,
timestamp: DateTime.makeUnsafe(Date.now()),
}),
),
),
)
})
const ensureToolCall = Effect.fn("SessionProcessor.ensureToolCall")(function* (input: {
id: string
name: string
@@ -312,17 +231,6 @@ export const layer = Layer.effect(
}
return { call: ctx.toolcalls[input.id], part }
}
// TODO(v2): Temporary dual-write while migrating session messages to v2 events.
const assistantMessageID = mirrorAssistant ? yield* ensureV2AssistantMessage() : undefined
if (assistantMessageID) {
yield* events.publish(SessionEvent.Tool.Input.Started, {
sessionID: ctx.sessionID,
assistantMessageID,
callID: input.id,
name: input.name,
timestamp: DateTime.makeUnsafe(Date.now()),
})
}
const part = yield* session.updatePart({
id: PartID.ascending(),
messageID: ctx.assistantMessage.id,
@@ -334,13 +242,10 @@ export const layer = Layer.effect(
metadata: input.providerExecuted ? { providerExecuted: true } : undefined,
} satisfies SessionV1.ToolPart)
ctx.toolcalls[input.id] = {
assistantMessageID,
done: yield* Deferred.make<void>(),
partID: part.id,
messageID: part.messageID,
sessionID: part.sessionID,
inputEnded: false,
raw: "",
}
return { call: ctx.toolcalls[input.id], part }
})
@@ -372,16 +277,6 @@ export const layer = Layer.effect(
switch (value.type) {
case "reasoning-start":
if (value.id in ctx.reasoningMap) return
// TODO(v2): Temporary dual-write while migrating session messages to v2 events.
if (mirrorAssistant) {
yield* events.publish(SessionEvent.Reasoning.Started, {
sessionID: ctx.sessionID,
assistantMessageID: yield* ensureV2AssistantMessage(),
reasoningID: value.id,
providerMetadata: value.providerMetadata,
timestamp: DateTime.makeUnsafe(Date.now()),
})
}
ctx.reasoningMap[value.id] = {
id: PartID.ascending(),
messageID: ctx.assistantMessage.id,
@@ -399,15 +294,6 @@ export const layer = Layer.effect(
if (!(value.id in ctx.reasoningMap)) return
ctx.reasoningMap[value.id].text += value.text
if (value.providerMetadata) ctx.reasoningMap[value.id].metadata = value.providerMetadata
if (mirrorAssistant) {
yield* events.publish(SessionEvent.Reasoning.Delta, {
sessionID: ctx.sessionID,
assistantMessageID: yield* currentV2AssistantMessage(),
reasoningID: value.id,
delta: value.text,
timestamp: DateTime.makeUnsafe(Date.now()),
})
}
yield* session.updatePartDelta({
sessionID: ctx.reasoningMap[value.id].sessionID,
messageID: ctx.reasoningMap[value.id].messageID,
@@ -432,36 +318,11 @@ export const layer = Layer.effect(
return
case "tool-input-delta":
{
const toolCall = yield* ensureToolCall(value)
const assistantMessageID = mirrorAssistant ? yield* requireV2AssistantMessage(toolCall.call) : undefined
if (assistantMessageID) {
yield* events.publish(SessionEvent.Tool.Input.Delta, {
sessionID: ctx.sessionID,
assistantMessageID,
callID: value.id,
delta: value.text,
timestamp: DateTime.makeUnsafe(Date.now()),
})
}
ctx.toolcalls[value.id] = { ...toolCall.call, raw: toolCall.call.raw + value.text }
}
yield* ensureToolCall(value)
return
case "tool-input-end": {
const toolCall = yield* ensureToolCall(value)
// TODO(v2): Temporary dual-write while migrating session messages to v2 events.
if (mirrorAssistant) {
const assistantMessageID = yield* requireV2AssistantMessage(toolCall.call)
yield* events.publish(SessionEvent.Tool.Input.Ended, {
sessionID: ctx.sessionID,
assistantMessageID,
callID: value.id,
text: toolCall.call.raw,
timestamp: DateTime.makeUnsafe(Date.now()),
})
}
ctx.toolcalls[value.id] = { ...toolCall.call, inputEnded: true }
yield* ensureToolCall(value)
return
}
@@ -469,37 +330,8 @@ export const layer = Layer.effect(
if (ctx.assistantMessage.summary) {
throw new Error(`Tool call not allowed while generating summary: ${value.name}`)
}
const toolCall = yield* ensureToolCall(value)
yield* ensureToolCall(value)
const input = isRecord(value.input) ? value.input : { value: value.input }
if (!toolCall.call.inputEnded) {
// TODO(v2): Temporary dual-write while migrating session messages to v2 events.
if (mirrorAssistant) {
const assistantMessageID = yield* requireV2AssistantMessage(toolCall.call)
yield* events.publish(SessionEvent.Tool.Input.Ended, {
sessionID: ctx.sessionID,
assistantMessageID,
callID: value.id,
text: toolCall.call.raw,
timestamp: DateTime.makeUnsafe(Date.now()),
})
}
}
// TODO(v2): Temporary dual-write while migrating session messages to v2 events.
if (mirrorAssistant) {
const assistantMessageID = yield* requireV2AssistantMessage(toolCall.call)
yield* events.publish(SessionEvent.Tool.Called, {
sessionID: ctx.sessionID,
assistantMessageID,
callID: value.id,
tool: value.name,
input,
provider: {
executed: toolCall.part.metadata?.providerExecuted === true,
...(value.providerMetadata ? { metadata: value.providerMetadata } : {}),
},
timestamp: DateTime.makeUnsafe(Date.now()),
})
}
yield* updateToolCall(value.id, (match) => ({
...match,
tool: value.name,
@@ -550,22 +382,6 @@ export const layer = Layer.effect(
const toolCall = yield* readToolCall(value.id)
if (!toolCall && value.result.type === "error") return
if (value.result.type === "error") {
// TODO(v2): Temporary dual-write while migrating session messages to v2 events.
if (mirrorAssistant) {
const assistantMessageID = yield* requireV2AssistantMessage(toolCall?.call)
yield* events.publish(SessionEvent.Tool.Failed, {
sessionID: ctx.sessionID,
assistantMessageID,
callID: value.id,
error: { type: "unknown", message: errorMessage(value.result.value) },
result: value.result,
provider: {
executed: value.providerExecuted === true || toolCall?.part.metadata?.providerExecuted === true,
...(value.providerMetadata ? { metadata: value.providerMetadata } : {}),
},
timestamp: DateTime.makeUnsafe(Date.now()),
})
}
yield* failToolCall(value.id, value.result.value)
return
}
@@ -591,81 +407,11 @@ export const layer = Layer.effect(
: `${rawOutput.output}\n\n[${omitted} image${omitted === 1 ? "" : "s"} omitted: could not be resized below the image size limit.]`,
attachments: attachments.length ? attachments : undefined,
}
// TODO(v2): Temporary dual-write while migrating session messages to v2 events.
if (mirrorAssistant) {
const assistantMessageID = yield* requireV2AssistantMessage(toolCall?.call)
const content = [
{ type: "text" as const, text: output.output },
...(output.attachments?.map(
(item: SessionV1.FilePart) =>
({
type: "file",
uri: item.url,
mime: item.mime,
name: item.filename,
}) as const,
) ?? []),
]
const unsupported = content.find((item) => item.type === "file" && !item.uri.startsWith("data:"))
if (unsupported?.type === "file") {
const error = new Error(
`Tool attachment URI "${unsupported.uri}" must be materialized before durable V2 settlement`,
)
yield* events.publish(SessionEvent.Tool.Failed, {
sessionID: ctx.sessionID,
assistantMessageID,
callID: value.id,
error: {
type: "unknown",
message: error.message,
},
provider: {
executed: value.providerExecuted === true || toolCall?.part.metadata?.providerExecuted === true,
...(value.providerMetadata ? { metadata: value.providerMetadata } : {}),
},
timestamp: DateTime.makeUnsafe(Date.now()),
})
yield* failToolCall(value.id, error)
return
} else
yield* events.publish(SessionEvent.Tool.Success, {
sessionID: ctx.sessionID,
assistantMessageID,
callID: value.id,
structured: output.metadata,
content,
result: value.result,
provider: {
executed: value.providerExecuted === true || toolCall?.part.metadata?.providerExecuted === true,
...(value.providerMetadata ? { metadata: value.providerMetadata } : {}),
},
timestamp: DateTime.makeUnsafe(Date.now()),
})
}
yield* completeToolCall(value.id, output)
return
}
case "tool-error": {
const toolCall = yield* readToolCall(value.id)
// TODO(v2): Temporary dual-write while migrating session messages to v2 events.
if (mirrorAssistant) {
const assistantMessageID = yield* requireV2AssistantMessage(toolCall?.call)
yield* events.publish(SessionEvent.Tool.Failed, {
sessionID: ctx.sessionID,
assistantMessageID,
callID: value.id,
error: {
type: "unknown",
message: value.message,
},
provider: {
executed: toolCall?.part.metadata?.providerExecuted === true,
...(value.providerMetadata ? { metadata: value.providerMetadata } : {}),
},
timestamp: DateTime.makeUnsafe(Date.now()),
})
}
yield* failToolCall(value.id, value.error ?? new Error(value.message))
return
}
@@ -675,12 +421,6 @@ export const layer = Layer.effect(
case "step-start":
if (!ctx.snapshot) ctx.snapshot = yield* snapshot.track()
if (!ctx.assistantMessage.summary) {
// TODO(v2): Temporary dual-write while migrating session messages to v2 events.
if (mirrorAssistant) {
yield* ensureV2AssistantMessage()
}
}
yield* session.updatePart({
id: PartID.ascending(),
messageID: ctx.assistantMessage.id,
@@ -698,21 +438,6 @@ export const layer = Layer.effect(
usage: value.usage ?? new Usage({}),
metadata: value.providerMetadata,
})
if (!ctx.assistantMessage.summary) {
// TODO(v2): Temporary dual-write while migrating session messages to v2 events.
if (mirrorAssistant) {
yield* events.publish(SessionEvent.Step.Ended, {
sessionID: ctx.sessionID,
assistantMessageID: yield* currentV2AssistantMessage(),
finish: value.reason,
cost: usage.cost,
tokens: usage.tokens,
snapshot: completedSnapshot,
timestamp: DateTime.makeUnsafe(Date.now()),
})
ctx.v2AssistantMessageID = undefined
}
}
ctx.assistantMessage.finish = value.reason
ctx.assistantMessage.cost += usage.cost
ctx.assistantMessage.tokens = usage.tokens
@@ -757,17 +482,6 @@ export const layer = Layer.effect(
}
case "text-start":
if (!ctx.assistantMessage.summary) {
// TODO(v2): Temporary dual-write while migrating session messages to v2 events.
if (mirrorAssistant) {
yield* events.publish(SessionEvent.Text.Started, {
sessionID: ctx.sessionID,
assistantMessageID: yield* ensureV2AssistantMessage(),
timestamp: DateTime.makeUnsafe(Date.now()),
textID: value.id,
})
}
}
ctx.currentText = {
id: PartID.ascending(),
messageID: ctx.assistantMessage.id,
@@ -777,7 +491,6 @@ export const layer = Layer.effect(
time: { start: Date.now() },
metadata: value.providerMetadata,
}
ctx.currentTextID = value.id
yield* session.updatePart(ctx.currentText)
return
@@ -785,15 +498,6 @@ export const layer = Layer.effect(
if (!ctx.currentText) return
ctx.currentText.text += value.text
if (value.providerMetadata) ctx.currentText.metadata = value.providerMetadata
if (mirrorAssistant) {
yield* events.publish(SessionEvent.Text.Delta, {
sessionID: ctx.sessionID,
assistantMessageID: yield* currentV2AssistantMessage(),
textID: value.id,
delta: value.text,
timestamp: DateTime.makeUnsafe(Date.now()),
})
}
yield* session.updatePartDelta({
sessionID: ctx.currentText.sessionID,
messageID: ctx.currentText.messageID,
@@ -816,18 +520,6 @@ export const layer = Layer.effect(
},
{ text: ctx.currentText.text },
)).text
if (!ctx.assistantMessage.summary) {
// TODO(v2): Temporary dual-write while migrating session messages to v2 events.
if (mirrorAssistant) {
yield* events.publish(SessionEvent.Text.Ended, {
sessionID: ctx.sessionID,
assistantMessageID: yield* currentV2AssistantMessage(),
text: ctx.currentText.text,
timestamp: DateTime.makeUnsafe(Date.now()),
textID: value.id,
})
}
}
{
const end = Date.now()
ctx.currentText.time = { start: ctx.currentText.time?.start ?? end, end }
@@ -835,7 +527,6 @@ export const layer = Layer.effect(
if (value.providerMetadata) ctx.currentText.metadata = value.providerMetadata
yield* session.updatePart(ctx.currentText)
ctx.currentText = undefined
ctx.currentTextID = undefined
return
case "finish":
@@ -864,7 +555,6 @@ export const layer = Layer.effect(
ctx.currentText.time = { start: ctx.currentText.time?.start ?? end, end }
yield* session.updatePart(ctx.currentText)
ctx.currentText = undefined
ctx.currentTextID = undefined
}
for (const part of Object.values(ctx.reasoningMap)) {
@@ -886,16 +576,6 @@ export const layer = Layer.effect(
const match = yield* readToolCall(toolCallID)
if (!match) continue
const part = match.part
if (mirrorAssistant && match.call.assistantMessageID) {
yield* events.publish(SessionEvent.Tool.Failed, {
sessionID: ctx.sessionID,
assistantMessageID: match.call.assistantMessageID,
callID: toolCallID,
error: { type: "unknown", message: "Tool execution aborted" },
provider: { executed: part.metadata?.providerExecuted === true },
timestamp: DateTime.makeUnsafe(Date.now()),
})
}
const end = Date.now()
const metadata = "metadata" in part.state && isRecord(part.state.metadata) ? part.state.metadata : {}
yield* session.updatePart({
@@ -922,7 +602,6 @@ export const layer = Layer.effect(
stack: e instanceof Error ? e.stack : undefined,
})
const error = parse(e)
yield* flushV2Fragments()
if (SessionV1.ContextOverflowError.isInstance(error)) {
if ((yield* config.get()).compaction?.auto === false && !ctx.assistantMessage.summary) {
ctx.assistantMessage.error = error
@@ -935,20 +614,6 @@ export const layer = Layer.effect(
yield* events.publish(Session.Event.Error, { sessionID: ctx.sessionID, error })
return
}
if (!ctx.assistantMessage.summary) {
// TODO(v2): Temporary dual-write while migrating session messages to v2 events.
if (mirrorAssistant) {
yield* events.publish(SessionEvent.Step.Failed, {
sessionID: ctx.sessionID,
assistantMessageID: yield* ensureV2AssistantMessage(),
error: {
type: "unknown",
message: errorMessage(e),
},
timestamp: DateTime.makeUnsafe(Date.now()),
})
}
}
ctx.assistantMessage.error = error
yield* events.publish(Session.Event.Error, {
sessionID: ctx.assistantMessage.sessionID,
@@ -968,7 +633,6 @@ export const layer = Layer.effect(
return yield* Effect.gen(function* () {
yield* Effect.gen(function* () {
ctx.currentText = undefined
ctx.currentTextID = undefined
ctx.reasoningMap = {}
yield* status.set(ctx.sessionID, { type: "busy" })
const stream = llm.stream(streamInput)
@@ -996,30 +660,13 @@ export const layer = Layer.effect(
provider: input.model.providerID,
parse,
set: (info) => {
// TODO(v2): Temporary dual-write while migrating session messages to v2 events.
const event = mirrorAssistant
? events.publish(SessionEvent.Retried, {
sessionID: ctx.sessionID,
attempt: info.attempt,
error: {
message: info.message,
isRetryable: true,
},
timestamp: DateTime.makeUnsafe(Date.now()),
})
: Effect.void
return flushV2Fragments().pipe(
Effect.andThen(event),
Effect.andThen(
status.set(ctx.sessionID, {
type: "retry",
attempt: info.attempt,
message: info.message,
action: info.action,
next: info.next,
}),
),
)
return status.set(ctx.sessionID, {
type: "retry",
attempt: info.attempt,
message: info.message,
action: info.action,
next: info.next,
})
},
}),
),
@@ -1059,7 +706,6 @@ export const defaultLayer = Layer.suspend(() =>
Layer.provide(SessionStatus.defaultLayer),
Layer.provide(Image.defaultLayer),
Layer.provide(Config.defaultLayer),
Layer.provide(RuntimeFlags.defaultLayer),
Layer.provide(Database.defaultLayer),
Layer.provide(EventV2Bridge.defaultLayer),
),
@@ -1080,7 +726,6 @@ export const node = LayerNode.make({
SessionStatus.node,
Image.node,
EventV2Bridge.node,
RuntimeFlags.node,
Database.node,
],
})
+11 -114
View File
@@ -49,12 +49,8 @@ import { SessionRunState } from "./run-state"
import { RuntimeFlags } from "@/effect/runtime-flags"
import { EventV2Bridge } from "@/event-v2-bridge"
import { Database } from "@opencode-ai/core/database/database"
import { SessionEvent } from "@opencode-ai/core/session/event"
import { SessionMessage } from "@opencode-ai/core/session/message"
import { ModelV2 } from "@opencode-ai/core/model"
import { ProviderV2 } from "@opencode-ai/core/provider"
import { AgentAttachment, FileAttachment, Prompt, Source } from "@opencode-ai/core/session/prompt"
import * as DateTime from "effect/DateTime"
import { eq } from "drizzle-orm"
import { SessionTable } from "@opencode-ai/core/session/sql"
import { SessionReminders } from "./reminders"
@@ -520,15 +516,6 @@ export const layer = Layer.effect(
},
}
yield* sessions.updatePart(part)
if (flags.experimentalEventSystem) {
yield* events.publish(SessionEvent.Shell.Started, {
sessionID: input.sessionID,
messageID: SessionMessage.ID.create(),
timestamp: DateTime.makeUnsafe(started),
callID: part.callID,
command: input.command,
})
}
return { msg, part, cwd: ctx.directory }
}).pipe(Effect.ensuring(markReady))
@@ -544,14 +531,6 @@ export const layer = Layer.effect(
output += "\n\n" + ["<metadata>", "User aborted the command", "</metadata>"].join("\n")
}
const completed = Date.now()
if (flags.experimentalEventSystem) {
yield* events.publish(SessionEvent.Shell.Ended, {
sessionID: input.sessionID,
timestamp: DateTime.makeUnsafe(completed),
callID: part.callID,
output,
})
}
if (!msg.time.completed) {
msg.time.completed = completed
yield* sessions.updateMessage(msg)
@@ -664,12 +643,6 @@ export const layer = Layer.effect(
throw error
}
const current = yield* db
.select({ agent: SessionTable.agent, model: SessionTable.model })
.from(SessionTable)
.where(eq(SessionTable.id, input.sessionID))
.get()
.pipe(Effect.orDie)
const model = input.model ?? ag.model ?? (yield* currentModel(input.sessionID))
const same = ag.model && model.providerID === ag.model.providerID && model.modelID === ag.model.modelID
const full =
@@ -696,28 +669,22 @@ export const layer = Layer.effect(
format: input.format,
}
if (current?.agent !== info.agent) {
yield* events.publish(SessionEvent.AgentSwitched, {
sessionID: input.sessionID,
messageID: SessionMessage.ID.create(),
timestamp: DateTime.makeUnsafe(info.time.created),
agent: info.agent,
})
}
const current = yield* sessions.get(input.sessionID).pipe(Effect.orDie)
if (
current?.model?.providerID !== info.model.providerID ||
current.model.id !== info.model.modelID ||
(current.model.variant === "default" ? undefined : current.model.variant) !== info.model.variant
current.agent !== info.agent ||
current.model?.providerID !== info.model.providerID ||
current.model?.id !== info.model.modelID ||
(current.model?.variant === "default" ? undefined : current.model?.variant) !== info.model.variant
) {
yield* events.publish(SessionEvent.ModelSwitched, {
yield* sessions.setAgentModel({
sessionID: input.sessionID,
messageID: SessionMessage.ID.create(),
timestamp: DateTime.makeUnsafe(info.time.created),
agent: info.agent,
model: {
id: ModelV2.ID.make(info.model.modelID),
providerID: ProviderV2.ID.make(info.model.providerID),
variant: ModelV2.VariantID.make(info.model.variant ?? "default"),
id: info.model.modelID,
providerID: info.model.providerID,
variant: info.model.variant ?? "default",
},
time: info.time.created,
})
}
@@ -1078,76 +1045,6 @@ export const layer = Layer.effect(
yield* sessions.updateMessage(info)
for (const part of parts) yield* sessions.updatePart(part)
const nextPrompt = parts.reduce(
(result, part) => {
if (part.type === "text") {
if (part.synthetic) result.synthetic.push(part.text)
else result.text.push(part.text)
}
if (part.type === "file") {
result.files.push(
FileAttachment.make({
uri: part.url,
mime: part.mime,
name: part.filename,
source: part.source
? Source.make({
start: part.source.text.start,
end: part.source.text.end,
text: part.source.text.value,
})
: undefined,
}),
)
}
if (part.type === "agent") {
result.agents.push(
AgentAttachment.make({
name: part.name,
source: part.source
? Source.make({
start: part.source.start,
end: part.source.end,
text: part.source.value,
})
: undefined,
}),
)
}
return result
},
{
text: [] as string[],
files: [] as FileAttachment[],
agents: [] as AgentAttachment[],
synthetic: [] as string[],
},
)
// TODO(v2): Temporary dual-write while migrating session messages to v2 events.
if (flags.experimentalEventSystem) {
yield* events.publish(SessionEvent.Prompted, {
sessionID: input.sessionID,
messageID: SessionMessage.ID.create(),
timestamp: DateTime.makeUnsafe(info.time.created),
delivery: "steer",
prompt: Prompt.make({
text: nextPrompt.text.join("\n"),
files: nextPrompt.files,
agents: nextPrompt.agents,
}),
})
}
for (const text of nextPrompt.synthetic) {
// TODO(v2): Temporary dual-write while migrating session messages to v2 events.
if (flags.experimentalEventSystem) {
yield* events.publish(SessionEvent.Synthetic, {
sessionID: input.sessionID,
messageID: SessionMessage.ID.create(),
timestamp: DateTime.makeUnsafe(info.time.created),
text,
})
}
}
return { info, parts }
}, Effect.scoped)
+20
View File
@@ -432,6 +432,12 @@ export interface Interface {
readonly setTitle: (input: { sessionID: SessionID; title: string }) => Effect.Effect<void>
readonly setArchived: (input: { sessionID: SessionID; time?: number }) => Effect.Effect<void>
readonly setMetadata: (input: typeof SetMetadataInput.Type) => Effect.Effect<void>
readonly setAgentModel: (input: {
sessionID: SessionID
agent: string
model: NonNullable<Info["model"]>
time: number
}) => Effect.Effect<void>
readonly setPermission: (input: { sessionID: SessionID; permission: PermissionV1.Ruleset }) => Effect.Effect<void>
readonly setRevert: (input: {
sessionID: SessionID
@@ -760,6 +766,19 @@ export const layer: Layer.Layer<
yield* patch(input.sessionID, { metadata: input.metadata, time: { updated: Date.now() } }).pipe(Effect.orDie)
})
const setAgentModel = Effect.fn("Session.setAgentModel")(function* (input: {
sessionID: SessionID
agent: string
model: NonNullable<Info["model"]>
time: number
}) {
yield* patch(input.sessionID, {
agent: input.agent,
model: input.model,
time: { updated: input.time },
}).pipe(Effect.orDie)
})
const setPermission = Effect.fn("Session.setPermission")(function* (input: {
sessionID: SessionID
permission: PermissionV1.Ruleset
@@ -898,6 +917,7 @@ export const layer: Layer.Layer<
setTitle,
setArchived,
setMetadata,
setAgentModel,
setPermission,
setRevert,
clearRevert,
@@ -854,12 +854,12 @@ describe("session.compaction.process", () => {
const msg = yield* createUserMessage(session.id, "hello")
const msgs = yield* ssn.messages({ sessionID: session.id })
const done = yield* Deferred.make<void, Error>()
let seen = false
const seen: string[] = []
const unsub = yield* events.listen((evt) => {
seen.push(evt.type)
if (evt.type !== SessionCompaction.Event.Compacted.type) return Effect.void
if ((evt.data as typeof SessionCompaction.Event.Compacted.data.Type).sessionID !== session.id)
return Effect.void
seen = true
Deferred.doneUnsafe(done, Effect.void)
return Effect.void
})
@@ -874,7 +874,8 @@ describe("session.compaction.process", () => {
yield* Deferred.await(done).pipe(Effect.timeout("500 millis"))
expect(result).toBe("continue")
expect(seen).toBe(true)
expect(seen).toContain(SessionCompaction.Event.Compacted.type)
expect(seen.filter((type) => type.startsWith("session.next."))).toEqual([])
}),
)
@@ -24,7 +24,6 @@ import { raw, reply, TestLLMServer } from "../lib/llm-server"
import { RuntimeFlags } from "@/effect/runtime-flags"
import { ProviderV2 } from "@opencode-ai/core/provider"
import { ModelV2 } from "@opencode-ai/core/model"
import { SessionEvent } from "@opencode-ai/core/session/event"
import { SessionProjector } from "@opencode-ai/core/session/projector"
import { LLMEvent } from "@opencode-ai/llm"
@@ -981,10 +980,9 @@ itProviderError.live("session.processor effect tests fail provider-executed erro
const parent = yield* user(chat.id, "provider tool error")
const msg = yield* assistant(chat.id, parent.id, path.resolve(dir))
const mdl = yield* provider.getModel(ref.providerID, ref.modelID)
const settlements: Array<typeof SessionEvent.Tool.Failed.Type> = []
const seen: string[] = []
const off = yield* events.listen((event) => {
if (event.type === SessionEvent.Tool.Failed.type)
settlements.push(event as typeof SessionEvent.Tool.Failed.Type)
seen.push(event.type)
return Effect.void
})
const handle = yield* processors.create({ assistantMessage: msg, sessionID: chat.id, model: mdl })
@@ -1011,19 +1009,15 @@ itProviderError.live("session.processor effect tests fail provider-executed erro
const call = parts.find((part): part is SessionV1.ToolPart => part.type === "tool")
expect(call?.state.status).toBe("error")
if (call?.state.status === "error") expect(call.state.error).toBe("provider boom")
expect(settlements).toHaveLength(1)
expect(settlements[0]?.data).toMatchObject({
callID: "call-1",
error: { type: "unknown", message: "provider boom" },
result: { type: "error", value: "provider boom" },
provider: { executed: true },
})
expect(seen).toContain(MessageV2.Event.PartUpdated.type)
expect(seen).toContain(MessageV2.Event.Updated.type)
expect(seen.filter((type) => type.startsWith("session.next."))).toEqual([])
}),
{ config: cfg },
),
)
itFragmentFailure.live("session.processor effect tests flush partial v2 fragments before step failure", () =>
itFragmentFailure.live("session.processor effect tests retain partial legacy parts without v2 events", () =>
provideTmpdirInstance(
(dir) =>
Effect.gen(function* () {
@@ -1035,14 +1029,8 @@ itFragmentFailure.live("session.processor effect tests flush partial v2 fragment
const msg = yield* assistant(chat.id, parent.id, path.resolve(dir))
const mdl = yield* provider.getModel(ref.providerID, ref.modelID)
const seen: string[] = []
let text: string | undefined
let reasoning: string | undefined
const off = yield* events.listen((event) => {
seen.push(event.type)
if (event.type === SessionEvent.Text.Ended.type)
text = (event.data as typeof SessionEvent.Text.Ended.data.Type).text
if (event.type === SessionEvent.Reasoning.Ended.type)
reasoning = (event.data as typeof SessionEvent.Reasoning.Ended.data.Type).text
return Effect.void
})
const handle = yield* processors.create({ assistantMessage: msg, sessionID: chat.id, model: mdl })
@@ -1067,12 +1055,16 @@ itFragmentFailure.live("session.processor effect tests flush partial v2 fragment
).toBe("stop")
yield* off
const failed = seen.indexOf(SessionEvent.Step.Failed.type)
expect(failed).toBeGreaterThan(-1)
expect(seen.indexOf(SessionEvent.Text.Ended.type)).toBeLessThan(failed)
expect(seen.indexOf(SessionEvent.Reasoning.Ended.type)).toBeLessThan(failed)
expect(text).toBe("partial")
expect(reasoning).toBe("thinking")
const parts = yield* MessageV2.parts(msg.id)
expect(parts).toEqual(
expect.arrayContaining([
expect.objectContaining({ type: "text", text: "partial" }),
expect.objectContaining({ type: "reasoning", text: "thinking" }),
]),
)
expect(seen).toContain(MessageV2.Event.PartUpdated.type)
expect(seen).toContain(Session.Event.Error.type)
expect(seen.filter((type) => type.startsWith("session.next."))).toEqual([])
}),
{ config: cfg },
),
+49 -6
View File
@@ -8,7 +8,7 @@ import { FetchHttpClient } from "effect/unstable/http"
import { expect } from "bun:test"
import { Cause, Deferred, Duration, Effect, Exit, Fiber, Layer } from "effect"
import path from "path"
import { fileURLToPath, pathToFileURL } from "url"
import { fileURLToPath } from "url"
import { NamedError } from "@opencode-ai/core/util/error"
import { Agent as AgentSvc } from "../../src/agent/agent"
import { BackgroundJob } from "@/background/job"
@@ -317,11 +317,6 @@ const writeText = Effect.fn("test.writeText")(function* (file: string, text: str
yield* fs.writeWithDirs(file, text)
})
const ensureDir = Effect.fn("test.ensureDir")(function* (dir: string) {
const fs = yield* FSUtil.Service
yield* fs.ensureDir(dir)
})
const writeConfig = Effect.fn("test.writeConfig")(function* (dir: string, config: Partial<ConfigV1.Info>) {
yield* writeText(
path.join(dir, "opencode.json"),
@@ -554,6 +549,54 @@ withMcpInstructions.instance(
15_000,
)
it.instance("legacy prompt emits message events without session.next events", () =>
Effect.gen(function* () {
const events = yield* EventV2Bridge.Service
const prompt = yield* SessionPrompt.Service
const sessions = yield* Session.Service
const chat = yield* sessions.create({
title: "Pinned",
agent: "plan",
model: { providerID: ProviderV2.ID.make("old"), id: ModelV2.ID.make("old-model") },
})
const seen: string[] = []
const off = yield* events.listen((event) => {
seen.push(event.type)
return Effect.void
})
const first = yield* prompt.prompt({
sessionID: chat.id,
agent: "build",
model: ref,
noReply: true,
parts: [{ type: "text", text: "hello" }],
})
const second = yield* prompt.prompt({
sessionID: chat.id,
agent: "build",
noReply: true,
parts: [{ type: "text", text: "again" }],
})
yield* off
expect(first.info.role).toBe("user")
expect(second.info.role).toBe("user")
if (first.info.role === "user" && second.info.role === "user") {
expect(first.info.model).toEqual(ref)
expect(second.info.model).toEqual(ref)
}
expect(yield* sessions.get(chat.id)).toMatchObject({
agent: "build",
model: { providerID: ref.providerID, id: ref.modelID },
})
expect(seen).toContain(Session.Event.Updated.type)
expect(seen).toContain(MessageV2.Event.Updated.type)
expect(seen).toContain(MessageV2.Event.PartUpdated.type)
expect(seen.filter((type) => type.startsWith("session.next."))).toEqual([])
}),
)
it.instance("loop surfaces content-filter finishes as session errors", () =>
Effect.gen(function* () {
const { llm } = yield* useServerConfig(providerCfg)