Skip to content

Commit d33e51a

Browse files
committed
follow up glm thinking recovery
1 parent b00d3d2 commit d33e51a

5 files changed

Lines changed: 137 additions & 8 deletions

File tree

‎packages/opencode/src/session/processor.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ export namespace SessionProcessor {
483483
const halt = Effect.fn("SessionProcessor.halt")(function* (e: unknown) {
484484
log.error("process", { error: e, stack: e instanceof Error ? e.stack : undefined })
485485
const error = parse(e)
486-
if (MessageV2.ContextOverflowError.isInstance(error)) {
486+
if (MessageV2.ContextOverflowError.isInstance(error) || e instanceof RepetitionError) {
487487
ctx.needsCompaction = true
488488
yield* bus.publish(Session.Event.Error, { sessionID: ctx.sessionID, error })
489489
return

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export namespace SessionPrompt {
126126
yield* status.set(sessionID, { type: "idle" })
127127
}),
128128
onBusy: status.set(sessionID, { type: "busy" }),
129-
onInterrupt: lastAssistant(sessionID),
129+
onInterrupt: latestAssistant(sessionID),
130130
busy: () => {
131131
throw new Session.BusyError(sessionID)
132132
},
@@ -250,6 +250,17 @@ export namespace SessionPrompt {
250250
)
251251
})
252252

253+
const sanitize = (msgs: MessageV2.WithParts[]) => {
254+
const drop = msgs.filter((msg) => msg.info.role === "assistant" && msg.info.summary !== true && !msg.info.finish)
255+
if (drop.length === 0) return msgs
256+
const ids = new Set(drop.map((msg) => msg.info.id))
257+
log.warn("ignoring incomplete assistant messages", {
258+
sessionID: drop[0]?.info.sessionID,
259+
ids: [...ids],
260+
})
261+
return msgs.filter((msg) => !ids.has(msg.info.id))
262+
}
263+
253264
const insertReminders = Effect.fn("SessionPrompt.insertReminders")(function* (input: {
254265
messages: MessageV2.WithParts[]
255266
agent: Agent.Info
@@ -1324,7 +1335,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the
13241335
},
13251336
)
13261337

1327-
const lastAssistant = (sessionID: SessionID) =>
1338+
const latestAssistant: (sessionID: SessionID) => Effect.Effect<MessageV2.WithParts> = (sessionID) =>
13281339
Effect.promise(async () => {
13291340
let latest: MessageV2.WithParts | undefined
13301341
for await (const item of MessageV2.stream(sessionID)) {
@@ -1346,7 +1357,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the
13461357
yield* status.set(sessionID, { type: "busy" })
13471358
log.info("loop", { step, sessionID })
13481359

1349-
let msgs = yield* MessageV2.filterCompactedEffect(sessionID)
1360+
let msgs = sanitize(yield* MessageV2.filterCompactedEffect(sessionID))
13501361

13511362
let lastUser: MessageV2.User | undefined
13521363
let lastAssistant: MessageV2.Assistant | undefined
@@ -1378,7 +1389,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the
13781389
lastUser.id < lastAssistant.id
13791390
) {
13801391
log.info("exiting loop", { sessionID })
1381-
break
1392+
return lastAssistantMsg ?? (yield* latestAssistant(sessionID))
13821393
}
13831394

13841395
step++
@@ -1563,7 +1574,8 @@ NOTE: At any point in time through this workflow you should feel free to ask the
15631574
}
15641575

15651576
yield* compaction.prune({ sessionID }).pipe(Effect.ignore, Effect.forkIn(scope))
1566-
return yield* lastAssistant(sessionID)
1577+
const msgs = sanitize(yield* MessageV2.filterCompactedEffect(sessionID))
1578+
return msgs.findLast((msg) => msg.info.role === "assistant") ?? (yield* latestAssistant(sessionID))
15671579
},
15681580
)
15691581

‎packages/opencode/test/scenario/harness.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ import { Todo } from "../../src/session/todo"
3030
import { ToolRegistry } from "../../src/tool/registry"
3131
import { Truncate } from "../../src/tool/truncate"
3232
import * as CrossSpawnSpawner from "../../src/effect/cross-spawn-spawner"
33-
import { Question } from "../../src/question"
34-
import { Todo } from "../../src/session/todo"
3533
import { Instance } from "../../src/project/instance"
3634
import { provideTmpdirInstance } from "../fixture/fixture"
3735
import { testEffect } from "../lib/effect"

‎packages/opencode/test/session/processor-effect.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,49 @@ it.live("session.processor effect tests compact on structured context overflow",
544544
),
545545
)
546546

547+
it.live("session.processor effect tests compact on repetition loops in text output", () =>
548+
provideTmpdirServer(
549+
({ dir, llm }) =>
550+
Effect.gen(function* () {
551+
const { processors, session, provider } = yield* boot()
552+
553+
yield* llm.push(reply().text("<invoke>".repeat(1200)).stop())
554+
555+
const chat = yield* session.create({})
556+
const parent = yield* user(chat.id, "glm loop")
557+
const msg = yield* assistant(chat.id, parent.id, path.resolve(dir))
558+
const mdl = yield* provider.getModel(ref.providerID, ref.modelID)
559+
const handle = yield* processors.create({
560+
assistantMessage: msg,
561+
sessionID: chat.id,
562+
model: mdl,
563+
})
564+
565+
const value = yield* handle.process({
566+
user: {
567+
id: parent.id,
568+
sessionID: chat.id,
569+
role: "user",
570+
time: parent.time,
571+
agent: parent.agent,
572+
model: { providerID: ref.providerID, modelID: ref.modelID },
573+
} satisfies MessageV2.User,
574+
sessionID: chat.id,
575+
model: mdl,
576+
agent: agent(),
577+
system: [],
578+
messages: [{ role: "user", content: "glm loop" }],
579+
tools: {},
580+
})
581+
582+
expect(value).toBe("compact")
583+
expect(yield* llm.calls).toBe(1)
584+
expect(handle.message.error).toBeUndefined()
585+
}),
586+
{ git: true, config: (url) => providerCfg(url) },
587+
),
588+
)
589+
547590
it.live("session.processor effect tests mark pending tools as aborted on cleanup", () =>
548591
provideTmpdirServer(
549592
({ dir, llm }) =>

‎packages/opencode/test/session/prompt-effect.test.ts‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,34 @@ const seed = Effect.fn("test.seed")(function* (sessionID: SessionID, opts?: { fi
284284
return { user: msg, assistant }
285285
})
286286

287+
const broken = Effect.fn("test.broken")(function* (sessionID: SessionID, text: string, parentID?: MessageID) {
288+
const session = yield* Session.Service
289+
const id = parentID ?? (yield* user(sessionID, "hello")).id
290+
const msg: MessageV2.Assistant = {
291+
id: MessageID.ascending(),
292+
role: "assistant",
293+
parentID: id,
294+
sessionID,
295+
mode: "build",
296+
agent: "build",
297+
cost: 0,
298+
path: { cwd: "/tmp", root: "/tmp" },
299+
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
300+
modelID: ref.modelID,
301+
providerID: ref.providerID,
302+
time: { created: Date.now(), completed: Date.now() },
303+
}
304+
yield* session.updateMessage(msg)
305+
yield* session.updatePart({
306+
id: PartID.ascending(),
307+
messageID: msg.id,
308+
sessionID,
309+
type: "text",
310+
text,
311+
})
312+
return msg
313+
})
314+
287315
const addSubtask = (sessionID: SessionID, messageID: MessageID, model = ref) =>
288316
Effect.gen(function* () {
289317
const session = yield* Session.Service
@@ -325,6 +353,54 @@ it.live("loop exits immediately when last assistant has stop finish", () =>
325353
),
326354
)
327355

356+
it.live("loop ignores unfinished assistant pollution when building the next turn", () =>
357+
provideTmpdirServer(
358+
Effect.fnUntraced(function* ({ llm }) {
359+
const prompt = yield* SessionPrompt.Service
360+
const sessions = yield* Session.Service
361+
const chat = yield* sessions.create({
362+
title: "Pinned",
363+
permission: [{ permission: "*", pattern: "*", action: "allow" }],
364+
})
365+
const msg = yield* user(chat.id, "check PR #412")
366+
yield* broken(
367+
chat.id,
368+
"`apps/liff` `StatusPage` `LiffProvider` `Wave 1B` `apps/liff` `StatusPage` `LiffProvider`",
369+
msg.id,
370+
)
371+
yield* llm.text("PR #412 is still open.")
372+
373+
const result = yield* prompt.loop({ sessionID: chat.id })
374+
expect(result.info.role).toBe("assistant")
375+
expect(result.parts.some((part) => part.type === "text" && part.text === "PR #412 is still open.")).toBe(true)
376+
377+
const [hit] = yield* llm.inputs
378+
expect(JSON.stringify(hit)).not.toContain("apps/liff")
379+
expect(JSON.stringify(hit)).not.toContain("LiffProvider")
380+
}),
381+
{ git: true, config: providerCfg },
382+
),
383+
)
384+
385+
it.live("loop returns the last finished assistant when a stale unfinished assistant trails it", () =>
386+
provideTmpdirServer(
387+
Effect.fnUntraced(function* ({ llm }) {
388+
const prompt = yield* SessionPrompt.Service
389+
const sessions = yield* Session.Service
390+
const chat = yield* sessions.create({ title: "Pinned" })
391+
const seeded = yield* seed(chat.id, { finish: "stop" })
392+
yield* broken(chat.id, "stale broken thinking output", seeded.user.id)
393+
394+
const result = yield* prompt.loop({ sessionID: chat.id })
395+
expect(result.info.role).toBe("assistant")
396+
expect(result.info.id).toBe(seeded.assistant.id)
397+
expect(result.parts.some((part) => part.type === "text" && part.text === "hi there")).toBe(true)
398+
expect(yield* llm.calls).toBe(0)
399+
}),
400+
{ git: true, config: providerCfg },
401+
),
402+
)
403+
328404
it.live("loop calls LLM and returns assistant message", () =>
329405
provideTmpdirServer(
330406
Effect.fnUntraced(function* ({ llm }) {

0 commit comments

Comments
 (0)