Skip to content

Commit 8bad789

Browse files
authored
fix(core): exclude commentary from generated session titles (#62267)
1 parent 10952cd commit 8bad789

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

‎packages/core/src/session/title.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ export const layer = Layer.effect(
5656
readonly text: string
5757
readonly model: SessionRunnerModel.Resolved
5858
}) {
59-
const chunks: string[] = []
59+
const chunks: Array<{ id: string; text: string }> = []
60+
const commentary = new Set<string>()
6061
let failed = false
6162
let usage: SessionUsage.Recorded | undefined
6263
const recordUsage = Effect.suspend(() =>
@@ -76,9 +77,16 @@ export const layer = Layer.effect(
7677
messages: [Message.user(input.text)],
7778
})
7879
if (prepared.event.result !== undefined) return prepared.event.result
80+
const metadataKey = prepared.request.model.route.providerMetadataKey ?? prepared.request.model.provider
7981
yield* llm.stream(prepared.request, prepared.options).pipe(
8082
Stream.runForEach((event) => {
81-
if (LLMEvent.is.textDelta(event)) chunks.push(event.text)
83+
// Responses can classify a text block only when it ends; filter after the stream completes.
84+
if (LLMEvent.is.textStart(event) || LLMEvent.is.textDelta(event) || LLMEvent.is.textEnd(event)) {
85+
const phase = event.providerMetadata?.[metadataKey]?.phase
86+
if (phase === "commentary") commentary.add(event.id)
87+
if (phase === "final_answer" || phase === null) commentary.delete(event.id)
88+
}
89+
if (LLMEvent.is.textDelta(event)) chunks.push({ id: event.id, text: event.text })
8290
if (LLMEvent.is.stepFinish(event)) {
8391
const step = SessionUsage.record(event.usage, input.model.cost)
8492
usage = usage ? SessionUsage.add(usage, step) : step
@@ -95,6 +103,8 @@ export const layer = Layer.effect(
95103
yield* recordUsage
96104
if (failed) return
97105
return chunks
106+
.filter((chunk) => !commentary.has(chunk.id))
107+
.map((chunk) => chunk.text)
98108
.join("")
99109
.split("\n")
100110
.map((line) => line.trim())

‎packages/core/test/session-title.test.ts‎

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
TransportError,
1111
type LLMRequest,
1212
} from "@opencode/ai/effect"
13-
import { OpenAIChat } from "@opencode/ai/protocols"
13+
import { OpenAIChat, OpenAIResponses } from "@opencode/ai/protocols"
1414
import { Agent } from "@opencode/core/agent"
1515
import { Database } from "@opencode/core/database/database"
1616
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
@@ -35,7 +35,7 @@ import { Model } from "@opencode/core/model"
3535
import { Provider } from "@opencode/core/provider"
3636
import { AbsolutePath } from "@opencode/core/schema"
3737
import { Money } from "@opencode/schema/money"
38-
import { Deferred, Effect, Fiber, Layer, Stream } from "effect"
38+
import { Deferred, Effect, Fiber, Layer, Schema, Stream } from "effect"
3939
import { testEffect } from "./lib/effect"
4040

4141
let requests: LLMRequest[] = []
@@ -84,11 +84,11 @@ const successfulTitle = () =>
8484
reason: { normalized: "stop" },
8585
}),
8686
)
87-
let titleStream: () => Stream.Stream<LLMEvent, AIError> = successfulTitle
87+
let titleStream: (request: LLMRequest) => Stream.Stream<LLMEvent, AIError> = successfulTitle
8888
const client = Layer.mock(LLMClient.Service)({
8989
stream: (request: LLMRequest) => {
9090
requests.push(request)
91-
return titleStream()
91+
return titleStream(request)
9292
},
9393
generate: () => Effect.die("unused"),
9494
})
@@ -237,6 +237,50 @@ it.effect("generates a title from the sole user message and renames the session"
237237
}),
238238
)
239239

240+
for (const timing of ["start", "end"] as const)
241+
it.effect(`excludes commentary from titles when the response phase arrives at ${timing}`, () =>
242+
Effect.gen(function* () {
243+
yield* enableTitleAgent
244+
const sessionID = Session.ID.make(`ses_title_phase_${timing}`)
245+
yield* insertSession(sessionID)
246+
yield* prompt(sessionID, "Investigate performance across four hosts without making changes")
247+
// Sanitized shape of a live Luna response: both messages are output_text, not reasoning.
248+
titleStream = (request) =>
249+
Stream.fromIterable([
250+
{
251+
type: "response.output_item.added",
252+
item: { type: "message", id: "commentary", ...(timing === "start" ? { phase: "commentary" } : {}) },
253+
},
254+
{
255+
type: "response.output_text.delta",
256+
item_id: "commentary",
257+
delta: "We need title only. Need capture investigation four machines performance read-only. Title concise.",
258+
},
259+
{
260+
type: "response.output_item.done",
261+
item: { type: "message", id: "commentary", phase: "commentary" },
262+
},
263+
{ type: "response.output_item.added", item: { type: "message", id: "final", phase: "final_answer" } },
264+
{ type: "response.output_text.delta", item_id: "final", delta: "Read-only performance investigation" },
265+
{ type: "response.output_item.done", item: { type: "message", id: "final", phase: "final_answer" } },
266+
{ type: "response.completed", response: { id: "response_title" } },
267+
]).pipe(
268+
Stream.map((event) => Schema.decodeUnknownSync(OpenAIResponses.protocol.stream.event)(JSON.stringify(event))),
269+
Stream.mapAccumEffect(
270+
() => OpenAIResponses.protocol.stream.initial(request),
271+
OpenAIResponses.protocol.stream.step,
272+
),
273+
)
274+
275+
const title = yield* SessionTitle.Service
276+
yield* title.generate(sessionID)
277+
278+
const store = yield* SessionStore.Service
279+
expect(requests).toHaveLength(1)
280+
expect((yield* store.get(sessionID))?.title).toBe("Read-only performance investigation")
281+
}),
282+
)
283+
240284
it.effect("runs title hooks instead of context hooks", () =>
241285
Effect.gen(function* () {
242286
yield* enableTitleAgent

0 commit comments

Comments
 (0)