Skip to content

Commit 1e5107d

Browse files
fix(session): don't let auto-title overwrite a title set mid-turn
First-turn auto-title generation guarded on the session title captured once at the start of the turn, so a plugin (or user) that renamed the session while the turn was in flight had its custom title clobbered when auto-title resolved. Re-read the session from storage immediately before setTitle and skip when the title is no longer the default. Adds a test (and a holdTitle gate in the LLM test harness to deterministically rename mid-generation). Closes anomalyco#32710 Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent 08096b5 commit 1e5107d

3 files changed

Lines changed: 72 additions & 1 deletion

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ const layer = Layer.effect(
247247
.find((line) => line.length > 0)
248248
if (!cleaned) return
249249
const t = cleaned.length > 100 ? cleaned.substring(0, 97) + "..." : cleaned
250+
// Re-read the session from storage: a plugin or user may have renamed it
251+
// (e.g. via session.rename/setTitle) mid-turn while the title was being
252+
// generated. The captured `input.session` is stale, so honour the latest
253+
// title and skip the auto-title if it is no longer the default.
254+
const current = yield* sessions.get(input.session.id).pipe(Effect.orDie)
255+
if (!Session.isDefaultTitle(current.title)) return
250256
yield* sessions
251257
.setTitle({ sessionID: input.session.id, title: t })
252258
.pipe(Effect.catchCause((cause) => Effect.logError("failed to generate title", { error: Cause.squash(cause) })))

‎packages/opencode/test/lib/llm-server.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ namespace TestLLMServer {
624624
readonly error: (status: number, body: unknown) => Effect.Effect<void>
625625
readonly hang: Effect.Effect<void>
626626
readonly hold: (value: string, wait: PromiseLike<unknown>) => Effect.Effect<void>
627+
readonly holdTitle: (wait: PromiseLike<unknown>) => Effect.Effect<void>
627628
readonly reset: Effect.Effect<void>
628629
readonly hits: Effect.Effect<Hit[]>
629630
readonly calls: Effect.Effect<number>
@@ -645,6 +646,10 @@ export class TestLLMServer extends Context.Service<TestLLMServer, TestLLMServer.
645646
let list: Queue[] = []
646647
let waits: Wait[] = []
647648
let misses: Hit[] = []
649+
// Optional gate for the auto-title request: when set, the title response is
650+
// held until this promise resolves. Lets tests open a window to mutate the
651+
// session (e.g. a plugin rename) while title generation is in flight.
652+
let titleHold: PromiseLike<unknown> | undefined
648653

649654
const queue = (...input: (Item | Reply)[]) => {
650655
list = [...list, ...input.map((value) => ({ item: item(value) }))]
@@ -676,7 +681,12 @@ export class TestLLMServer extends Context.Service<TestLLMServer, TestLLMServer.
676681
if (isTitleRequest(body)) {
677682
hits = [...hits, current]
678683
yield* notify()
679-
const auto: Sse = { type: "sse", head: [role()], tail: [textLine("E2E Title"), finishLine("stop")] }
684+
const auto: Sse = {
685+
type: "sse",
686+
head: [role()],
687+
tail: [textLine("E2E Title"), finishLine("stop")],
688+
wait: titleHold,
689+
}
680690
if (mode === "responses") return send(responses(auto, modelFrom(body)))
681691
return send(auto)
682692
}
@@ -756,11 +766,15 @@ export class TestLLMServer extends Context.Service<TestLLMServer, TestLLMServer.
756766
hold: Effect.fn("TestLLMServer.hold")(function* (value: string, wait: PromiseLike<unknown>) {
757767
queue(reply().wait(wait).text(value).stop().item())
758768
}),
769+
holdTitle: Effect.fn("TestLLMServer.holdTitle")(function* (wait: PromiseLike<unknown>) {
770+
titleHold = wait
771+
}),
759772
reset: Effect.sync(() => {
760773
hits = []
761774
list = []
762775
waits = []
763776
misses = []
777+
titleHold = undefined
764778
}),
765779
hits: Effect.sync(() => [...hits]),
766780
calls: Effect.sync(() => hits.length),

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,57 @@ it.instance("legacy prompt emits message events without session.next events", ()
588588
}),
589589
)
590590

591+
it.instance(
592+
"auto-title does not clobber a title set mid-turn",
593+
() =>
594+
Effect.gen(function* () {
595+
const { llm } = yield* useServerConfig(providerCfg)
596+
const prompt = yield* SessionPrompt.Service
597+
const sessions = yield* Session.Service
598+
599+
// No explicit title => session keeps its default title, so the first-turn
600+
// auto-title generation path runs (the guard sees a default title).
601+
const chat = yield* sessions.create({})
602+
expect(Session.isDefaultTitle((yield* sessions.get(chat.id)).title)).toBe(true)
603+
604+
// Hold the auto-title LLM response open so we get a deterministic window
605+
// to rename the session while title generation is in flight.
606+
const gate = yield* Deferred.make<void>()
607+
yield* llm.holdTitle(deferredAsPromise(gate))
608+
yield* llm.text("world")
609+
610+
yield* user(chat.id, "hello")
611+
const fiber = yield* prompt.loop({ sessionID: chat.id }).pipe(Effect.forkChild)
612+
613+
// Wait for both the assistant turn and the (held) auto-title request to
614+
// arrive. Once the title request is recorded the title fork is past its
615+
// start-of-turn guard and blocked on the gate.
616+
yield* llm.wait(2)
617+
618+
// Simulate a plugin / user renaming the session mid-turn.
619+
yield* sessions.setTitle({ sessionID: chat.id, title: "Custom plugin title" })
620+
621+
// Release the auto-title response; the fork now re-reads the session, sees
622+
// the non-default title, and must skip its setTitle.
623+
yield* Deferred.succeed(gate, void 0)
624+
625+
const exit = yield* Fiber.await(fiber)
626+
expect(Exit.isSuccess(exit)).toBe(true)
627+
628+
// The custom title must survive: auto-title generation ("E2E Title") must
629+
// not overwrite the mid-turn rename.
630+
yield* pollWithTimeout(
631+
Effect.gen(function* () {
632+
const title = (yield* sessions.get(chat.id)).title
633+
expect(title).not.toBe("E2E Title")
634+
return title === "Custom plugin title" ? (true as const) : undefined
635+
}),
636+
"session title was not preserved after mid-turn rename",
637+
)
638+
}),
639+
30_000,
640+
)
641+
591642
it.instance("loop surfaces content-filter finishes as session errors", () =>
592643
Effect.gen(function* () {
593644
const { llm } = yield* useServerConfig(providerCfg)

0 commit comments

Comments
 (0)