mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
fix(core): label untitled sessions by their first prompt
When title generation is disabled or fails, root sessions kept no title and clients showed a timestamped placeholder. Store the first prompt (whitespace collapsed, truncated to 100 characters) instead, and keep treating that label as untitled so the next turn still retries generation.
This commit is contained in:
parent
4b50f5cc6c
commit
55da71ced9
4 changed files with 98 additions and 20 deletions
|
|
@ -168,8 +168,8 @@ const layer = Layer.effect(
|
|||
)
|
||||
// A control admitted during context preparation owns this boundary.
|
||||
if (promoted === undefined) return undefined
|
||||
if (promoted > 0 && !selected.session.parentID && SessionTitle.isUntitled(selected.session))
|
||||
yield* FiberMap.run(titles, sessionID, title.generate(sessionID), {
|
||||
if (promoted > 0 && !selected.session.parentID)
|
||||
yield* FiberMap.run(titles, sessionID, title.ensure(sessionID), {
|
||||
onlyIfMissing: true,
|
||||
})
|
||||
if (promoted > 0) step = 1
|
||||
|
|
|
|||
|
|
@ -25,12 +25,17 @@ const titleChanged = Symbol("Session title changed")
|
|||
export interface Interface {
|
||||
/** Generates an initial title or regenerates one from bounded conversation history. */
|
||||
readonly generate: (sessionID: SessionSchema.ID) => Effect.Effect<void>
|
||||
/** Generates a title only while the session is untitled or still labeled by its first prompt. */
|
||||
readonly ensure: (sessionID: SessionSchema.ID) => Effect.Effect<void>
|
||||
}
|
||||
|
||||
export class Service extends Context.Service<Service, Interface>()("@opencode/SessionTitle") {}
|
||||
|
||||
const truncate = (value: string) => (value.length <= MAX_LENGTH ? value : `${value.slice(0, MAX_LENGTH - 3)}...`)
|
||||
export const isUntitled = (session: SessionSchema.Info) =>
|
||||
// Labels a session whose title cannot be generated, instead of leaving a timestamped fallback.
|
||||
const promptTitle = (text: string) => truncate(text.replace(/\s+/g, " ").trim())
|
||||
const isUntitled = (session: SessionSchema.Info, firstPrompt: string) =>
|
||||
session.title === promptTitle(firstPrompt) ||
|
||||
isExactRootFallback({
|
||||
title: session.title,
|
||||
time: { created: DateTime.toEpochMillis(session.time.created) },
|
||||
|
|
@ -97,12 +102,14 @@ export const layer = Layer.effect(
|
|||
.find((line) => line.length > 0)
|
||||
})
|
||||
|
||||
const generate = Effect.fn("SessionTitle.generate")(function* (sessionID: SessionSchema.ID) {
|
||||
const run = Effect.fnUntraced(function* (sessionID: SessionSchema.ID, untitledOnly: boolean) {
|
||||
const session = yield* store.get(sessionID)
|
||||
if (!session) return
|
||||
const firstUser = yield* SessionHistory.firstUserMessage(db, session.id)
|
||||
if (!firstUser) return
|
||||
const text = !isUntitled(session)
|
||||
const untitled = isUntitled(session, firstUser.text)
|
||||
if (untitledOnly && !untitled) return
|
||||
const text = !untitled
|
||||
? yield* store.context(session.id).pipe(
|
||||
Effect.map((messages) => {
|
||||
const original = `Original request:\n${firstUser.text.slice(0, MAX_FIRST_MESSAGE_LENGTH)}`
|
||||
|
|
@ -125,12 +132,13 @@ export const layer = Layer.effect(
|
|||
)
|
||||
: firstUser.text
|
||||
const selection = yield* context.selectTitle(session)
|
||||
if (!selection) return
|
||||
const title =
|
||||
(yield* attempt({ session, agent: selection.agent, text, model: selection.selected })) ??
|
||||
(selection.primary && !isDeepStrictEqual(selection.selected.ref, selection.primary.ref)
|
||||
? yield* attempt({ session, agent: selection.agent, text, model: selection.primary })
|
||||
: undefined)
|
||||
const generated = selection
|
||||
? ((yield* attempt({ session, agent: selection.agent, text, model: selection.selected })) ??
|
||||
(selection.primary && !isDeepStrictEqual(selection.selected.ref, selection.primary.ref)
|
||||
? yield* attempt({ session, agent: selection.agent, text, model: selection.primary })
|
||||
: undefined))
|
||||
: undefined
|
||||
const title = generated || (untitled ? promptTitle(firstUser.text) : undefined)
|
||||
if (!title) return
|
||||
const expectedSequence = (yield* Bus.latestSequence(db, sessionID)) + 1
|
||||
const current = yield* store.get(sessionID)
|
||||
|
|
@ -146,7 +154,10 @@ export const layer = Layer.effect(
|
|||
)
|
||||
.pipe(Effect.catchDefect((defect) => (defect === titleChanged ? Effect.void : Effect.die(defect))))
|
||||
})
|
||||
return Service.of({ generate })
|
||||
return Service.of({
|
||||
generate: Effect.fn("SessionTitle.generate")((sessionID: SessionSchema.ID) => run(sessionID, false)),
|
||||
ensure: Effect.fn("SessionTitle.ensure")((sessionID: SessionSchema.ID) => run(sessionID, true)),
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -1055,7 +1055,7 @@ describe("SessionRunnerLLM", () => {
|
|||
yield* s.resume
|
||||
yield* Deferred.await(titleFailed)
|
||||
yield* Effect.yieldNow
|
||||
expect((yield* s.session.get(sessionID)).title).toBeUndefined()
|
||||
expect((yield* s.session.get(sessionID)).title).toBe("First prompt")
|
||||
|
||||
const renamed = yield* watchRename(sessionID)
|
||||
yield* s.admit("Third prompt")
|
||||
|
|
|
|||
|
|
@ -505,7 +505,7 @@ it.effect("generates a title for an explicitly requested child session", () =>
|
|||
}),
|
||||
)
|
||||
|
||||
it.effect("does not generate when the title agent is removed", () =>
|
||||
it.effect("uses the first prompt without a model request when the title agent is removed", () =>
|
||||
Effect.gen(function* () {
|
||||
const sessionID = Session.ID.make("ses_title_no_agent")
|
||||
yield* insertSession(sessionID)
|
||||
|
|
@ -516,8 +516,52 @@ it.effect("does not generate when the title agent is removed", () =>
|
|||
yield* title.generate(sessionID)
|
||||
|
||||
expect(requests).toHaveLength(0)
|
||||
const untouched = yield* store.get(sessionID)
|
||||
expect(untouched?.title).toBeUndefined()
|
||||
expect((yield* store.get(sessionID))?.title).toBe("Help me debug the failing build")
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("collapses whitespace and truncates the first prompt fallback", () =>
|
||||
Effect.gen(function* () {
|
||||
const sessionID = Session.ID.make("ses_title_no_agent_long")
|
||||
yield* insertSession(sessionID)
|
||||
yield* prompt(sessionID, ` Fix the build\n\n in packages/core\t${"x".repeat(200)}`)
|
||||
|
||||
const store = yield* SessionStore.Service
|
||||
const title = yield* SessionTitle.Service
|
||||
yield* title.generate(sessionID)
|
||||
|
||||
const fallback = (yield* store.get(sessionID))?.title
|
||||
expect(fallback).toStartWith("Fix the build in packages/core xxx")
|
||||
expect(fallback).toEndWith("...")
|
||||
expect(fallback).toHaveLength(100)
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("keeps a blank first prompt untitled when the title agent is removed", () =>
|
||||
Effect.gen(function* () {
|
||||
const sessionID = Session.ID.make("ses_title_no_agent_blank")
|
||||
yield* insertSession(sessionID)
|
||||
yield* prompt(sessionID, " \n\t ")
|
||||
|
||||
const store = yield* SessionStore.Service
|
||||
const title = yield* SessionTitle.Service
|
||||
yield* title.generate(sessionID)
|
||||
|
||||
expect((yield* store.get(sessionID))?.title).toBeUndefined()
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("keeps an existing title when the title agent is removed", () =>
|
||||
Effect.gen(function* () {
|
||||
const sessionID = Session.ID.make("ses_title_no_agent_existing")
|
||||
yield* insertSession(sessionID, "Original title")
|
||||
yield* prompt(sessionID, "Help me debug the failing build")
|
||||
|
||||
const store = yield* SessionStore.Service
|
||||
const title = yield* SessionTitle.Service
|
||||
yield* title.generate(sessionID)
|
||||
|
||||
expect((yield* store.get(sessionID))?.title).toBe("Original title")
|
||||
}),
|
||||
)
|
||||
|
||||
|
|
@ -605,26 +649,49 @@ it.effect("preserves the existing title when regeneration fails", () =>
|
|||
}),
|
||||
)
|
||||
|
||||
it.effect("retries after a failed title request", () =>
|
||||
it.effect("uses the first prompt after a failed title request and can regenerate it", () =>
|
||||
Effect.gen(function* () {
|
||||
yield* enableTitleAgent
|
||||
const sessionID = Session.ID.make("ses_title_retry")
|
||||
yield* insertSession(sessionID)
|
||||
yield* prompt(sessionID, "Retry this title")
|
||||
const store = yield* SessionStore.Service
|
||||
const title = yield* SessionTitle.Service
|
||||
titleStream = () => Stream.make(LLMEvent.providerError({ message: "Provider unavailable" }))
|
||||
|
||||
yield* title.generate(sessionID)
|
||||
expect((yield* store.get(sessionID))?.title).toBe("Retry this title")
|
||||
titleStream = successfulTitle
|
||||
yield* title.generate(sessionID)
|
||||
|
||||
const store = yield* SessionStore.Service
|
||||
expect(requests).toHaveLength(2)
|
||||
expect((yield* store.get(sessionID))?.title).toBe("Generated Title")
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("does not rename after a failed title stream", () =>
|
||||
it.effect("ensures a title only while the session is untitled or labeled by its first prompt", () =>
|
||||
Effect.gen(function* () {
|
||||
yield* enableTitleAgent
|
||||
const sessionID = Session.ID.make("ses_title_ensure")
|
||||
yield* insertSession(sessionID)
|
||||
yield* prompt(sessionID, "Ensure this title")
|
||||
const store = yield* SessionStore.Service
|
||||
const title = yield* SessionTitle.Service
|
||||
titleStream = () => Stream.make(LLMEvent.providerError({ message: "Provider unavailable" }))
|
||||
|
||||
yield* title.ensure(sessionID)
|
||||
expect((yield* store.get(sessionID))?.title).toBe("Ensure this title")
|
||||
titleStream = successfulTitle
|
||||
yield* title.ensure(sessionID)
|
||||
expect((yield* store.get(sessionID))?.title).toBe("Generated Title")
|
||||
yield* title.ensure(sessionID)
|
||||
|
||||
expect(requests).toHaveLength(2)
|
||||
expect(JSON.stringify(requests[1]?.messages)).not.toContain("Original request")
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("uses the first prompt after a failed title stream", () =>
|
||||
Effect.gen(function* () {
|
||||
yield* enableTitleAgent
|
||||
const sessionID = Session.ID.make("ses_title_stream_failure")
|
||||
|
|
@ -642,7 +709,7 @@ it.effect("does not rename after a failed title stream", () =>
|
|||
|
||||
const store = yield* SessionStore.Service
|
||||
expect(requests).toHaveLength(1)
|
||||
expect((yield* store.get(sessionID))?.title).toBeUndefined()
|
||||
expect((yield* store.get(sessionID))?.title).toBe("Fail this title stream")
|
||||
}),
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue