Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(opencode): keep streamed text that arrives after a pending snapshot (#835)

Rudy Mizrahi Celekli3ac44e52542d
diff --git a/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts b/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts
index 071b2932..1e2715f4 100644
--- a/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts
+++ b/cli/beacon/internal/endpoint/hooks/assets/opencode/beacon.ts
@@ -173,6 +173,7 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
const next = structuredClone(part)
const delta = partDeltas.get(key)
if ((next.type === "text" || next.type === "reasoning") && !next.text && delta) next.text = delta
+ if ((next.type === "text" || next.type === "reasoning") && typeof next.text === "string") partDeltas.set(key, next.text)
if (next.type === "tool") {
const status = next.state?.status
if (status !== "completed" && status !== "error") return
@@ -329,8 +330,12 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
}
}
if (type === "message.part.delta") {
+ if (properties?.field && properties.field !== "text") return
const key = `${sid}:${properties?.messageID || ""}:${properties?.partID || ""}`
- partDeltas.set(key, (partDeltas.get(key) || "") + (properties?.delta || ""))
+ const text = (partDeltas.get(key) || "") + (properties?.delta || "")
+ partDeltas.set(key, text)
+ const pending = pendingParts.get(key)
+ if (pending && (pending.part.type === "text" || pending.part.type === "reasoning")) pending.part.text = text
return
}
if (type === "message.part.updated") {
diff --git a/plugins/opencode-beacon/src/beacon.test.ts b/plugins/opencode-beacon/src/beacon.test.ts
index d5204491..9832ce8c 100644
--- a/plugins/opencode-beacon/src/beacon.test.ts
+++ b/plugins/opencode-beacon/src/beacon.test.ts
@@ -23,6 +23,44 @@ async function plugin() {
}
describe("BeaconEndpointPlugin", () => {
+ test.each(["text", "reasoning"])("keeps later %s deltas when idle flushes a pending snapshot", async (type) => {
+ const hooks = await plugin()
+ await hooks.event!({ event: { type: "message.updated", properties: { sessionID: "ses_stream", info: { id: "msg_stream", role: "assistant" } } } } as any)
+ await hooks.event!({ event: { type: "message.part.updated", properties: { sessionID: "ses_stream", part: { id: "part_stream", messageID: "msg_stream", type, text: "prefix", time: { start: 1 } } } } } as any)
+ await hooks.event!({ event: { type: "message.part.delta", properties: { sessionID: "ses_stream", messageID: "msg_stream", partID: "part_stream", field: "metadata", delta: "not text" } } } as any)
+ await hooks.event!({ event: { type: "message.part.delta", properties: { sessionID: "ses_stream", messageID: "msg_stream", partID: "part_stream", field: "text", delta: " suffix" } } } as any)
+ await hooks.event!({ event: { type: "session.idle", properties: { sessionID: "ses_stream" } } } as any)
+ await hooks["command.execute.before"]!({ sessionID: "ses_stream", command: "drain" }, {})
+ expect(payloads.filter(item => item.type === "message.part.updated")).toEqual([
+ expect.objectContaining({ part: expect.objectContaining({ type, text: "prefix suffix" }) }),
+ ])
+ })
+
+ test("keeps deltas accumulated before and after an empty pending snapshot", async () => {
+ const hooks = await plugin()
+ const delta = (text: string) => ({ event: { type: "message.part.delta", properties: { sessionID: "ses_stream", messageID: "msg_stream", partID: "part_stream", field: "text", delta: text } } })
+ await hooks.event!(delta("first") as any)
+ await hooks.event!({ event: { type: "message.part.updated", properties: { sessionID: "ses_stream", part: { id: "part_stream", messageID: "msg_stream", type: "text", text: "", time: { start: 1 } } } } } as any)
+ await hooks.event!(delta(" second") as any)
+ await hooks.event!({ event: { type: "message.updated", properties: { sessionID: "ses_stream", info: { id: "msg_stream", role: "assistant", time: { completed: 2 } } } } } as any)
+ await hooks["command.execute.before"]!({ sessionID: "ses_stream", command: "drain" }, {})
+ expect(payloads.filter(item => item.type === "message.part.updated")).toEqual([
+ expect.objectContaining({ part: expect.objectContaining({ text: "first second" }) }),
+ ])
+ })
+
+ test("prefers a completed authoritative snapshot over earlier deltas", async () => {
+ const hooks = await plugin()
+ await hooks.event!({ event: { type: "message.updated", properties: { sessionID: "ses_stream", info: { id: "msg_stream", role: "assistant" } } } } as any)
+ await hooks.event!({ event: { type: "message.part.updated", properties: { sessionID: "ses_stream", part: { id: "part_stream", messageID: "msg_stream", type: "text", text: "prefix", time: { start: 1 } } } } } as any)
+ await hooks.event!({ event: { type: "message.part.delta", properties: { sessionID: "ses_stream", messageID: "msg_stream", partID: "part_stream", field: "text", delta: " delta" } } } as any)
+ await hooks.event!({ event: { type: "message.part.updated", properties: { sessionID: "ses_stream", part: { id: "part_stream", messageID: "msg_stream", type: "text", text: "canonical complete", time: { start: 1, end: 2 } } } } } as any)
+ await hooks["command.execute.before"]!({ sessionID: "ses_stream", command: "drain" }, {})
+ expect(payloads.filter(item => item.type === "message.part.updated")).toEqual([
+ expect.objectContaining({ part: expect.objectContaining({ text: "canonical complete" }) }),
+ ])
+ })
+
test("forwards prompts and correlated tool lifecycle payloads", async () => {
const hooks = await plugin()
diff --git a/plugins/opencode-beacon/src/beacon.ts b/plugins/opencode-beacon/src/beacon.ts
index 071b2932..1e2715f4 100644
--- a/plugins/opencode-beacon/src/beacon.ts
+++ b/plugins/opencode-beacon/src/beacon.ts
@@ -173,6 +173,7 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
const next = structuredClone(part)
const delta = partDeltas.get(key)
if ((next.type === "text" || next.type === "reasoning") && !next.text && delta) next.text = delta
+ if ((next.type === "text" || next.type === "reasoning") && typeof next.text === "string") partDeltas.set(key, next.text)
if (next.type === "tool") {
const status = next.state?.status
if (status !== "completed" && status !== "error") return
@@ -329,8 +330,12 @@ export const BeaconEndpointPlugin = async ({ project, directory, worktree, clien
}
}
if (type === "message.part.delta") {
+ if (properties?.field && properties.field !== "text") return
const key = `${sid}:${properties?.messageID || ""}:${properties?.partID || ""}`
- partDeltas.set(key, (partDeltas.get(key) || "") + (properties?.delta || ""))
+ const text = (partDeltas.get(key) || "") + (properties?.delta || "")
+ partDeltas.set(key, text)
+ const pending = pendingParts.get(key)
+ if (pending && (pending.part.type === "text" || pending.part.type === "reasoning")) pending.part.text = text
return
}
if (type === "message.part.updated") {