mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 13:05:14 +03:00
feat(core): page Session.wait back through history for an answer
This commit is contained in:
@@ -15,6 +15,7 @@ import {
|
||||
BusyError,
|
||||
CompactionConflictError,
|
||||
InboxConflictError,
|
||||
MessageDecodeError,
|
||||
NotFoundError,
|
||||
PromptConflictError,
|
||||
SyntheticConflictError,
|
||||
@@ -36,6 +37,9 @@ type PromptRequest = SessionPrompt.Input & {
|
||||
resume?: boolean
|
||||
}
|
||||
|
||||
/** Assistants read per page when {@link WaitResult.text} walks back through history. */
|
||||
const ANSWER_PAGE = 20
|
||||
|
||||
/** What a waiting parent gets back once its child goes idle. */
|
||||
export type WaitResult = {
|
||||
/**
|
||||
@@ -44,9 +48,9 @@ export type WaitResult = {
|
||||
*/
|
||||
readonly outcome: SessionSchema.Info["outcome"]
|
||||
/**
|
||||
* Text of the newest completed, error-free assistant answer, or `SubagentCompletion.NO_TEXT`.
|
||||
* Read regardless of outcome, so a failed or interrupted period can still return an earlier
|
||||
* good answer.
|
||||
* Text of the newest completed, error-free assistant answer anywhere in the history, or
|
||||
* `SubagentCompletion.NO_TEXT`. Read regardless of outcome, so a failed or interrupted period
|
||||
* can still return an earlier good answer.
|
||||
*/
|
||||
readonly text: string
|
||||
}
|
||||
@@ -291,17 +295,44 @@ export const make = Effect.fn("Session.make")(function* () {
|
||||
yield* execution.wake(sessionID)
|
||||
return admitted
|
||||
})
|
||||
/**
|
||||
* Newest completed, error-free assistant, paging back through history so a run of failed or
|
||||
* unfinished attempts cannot hide an earlier good answer.
|
||||
*/
|
||||
const lastAnswer = (
|
||||
sessionID: SessionSchema.ID,
|
||||
cursor?: SessionMessage.ID,
|
||||
): Effect.Effect<SessionMessage.Info | undefined, MessageDecodeError> =>
|
||||
store
|
||||
.messages({
|
||||
sessionID,
|
||||
order: "desc",
|
||||
limit: ANSWER_PAGE,
|
||||
type: "assistant",
|
||||
cursor: cursor === undefined ? undefined : { id: cursor, direction: "next" },
|
||||
})
|
||||
.pipe(
|
||||
Effect.flatMap((page) => {
|
||||
const answer = page.find(
|
||||
(message) =>
|
||||
message.type === "assistant" && message.time.completed !== undefined && message.error === undefined,
|
||||
)
|
||||
if (answer || page.length < ANSWER_PAGE) return Effect.succeed(answer)
|
||||
// The last row of a descending page is its oldest, and `direction: "next"` continues
|
||||
// strictly older than it.
|
||||
return lastAnswer(sessionID, page.at(-1)!.id)
|
||||
}),
|
||||
)
|
||||
const wait = Effect.fn("Session.wait")(function* (sessionID: SessionSchema.ID) {
|
||||
yield* get(sessionID)
|
||||
yield* execution.awaitIdle(sessionID)
|
||||
// Read again after idle: the terminal event writes `idle_outcome` before the deferred behind
|
||||
// `awaitIdle` resolves, so the first get would carry the previous period's outcome.
|
||||
const session = yield* get(sessionID)
|
||||
const messages = yield* store.messages({ sessionID, order: "desc", limit: 20, type: "assistant" })
|
||||
const assistant = messages.find(
|
||||
(message) => message.type === "assistant" && message.time.completed !== undefined && message.error === undefined,
|
||||
)
|
||||
return { outcome: session.outcome, text: SubagentCompletion.text(assistant) } satisfies WaitResult
|
||||
return {
|
||||
outcome: session.outcome,
|
||||
text: SubagentCompletion.text(yield* lastAnswer(sessionID)),
|
||||
} satisfies WaitResult
|
||||
})
|
||||
const resume = Effect.fn("Session.resume")(function* (sessionID: SessionSchema.ID) {
|
||||
yield* get(sessionID)
|
||||
|
||||
@@ -153,6 +153,69 @@ describe("Session.wait", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("finds a good answer behind more than a page of failed attempts", () =>
|
||||
Effect.gen(function* () {
|
||||
duringIdle = Effect.void
|
||||
const sessions = yield* Session.Service
|
||||
const session = yield* sessions.create({ location })
|
||||
yield* seedAssistant(session.id, { id: "msg_wait_deep_good", seq: 1, text: "good answer" })
|
||||
yield* Effect.forEach(
|
||||
Array.from({ length: 45 }, (_, i) => i),
|
||||
(i) =>
|
||||
i % 2 === 0
|
||||
? seedAssistant(session.id, { id: `msg_wait_deep_failed_${i}`, seq: i + 2, text: "failed", failed: true })
|
||||
: seedAssistant(session.id, {
|
||||
id: `msg_wait_deep_partial_${i}`,
|
||||
seq: i + 2,
|
||||
text: "partial",
|
||||
completed: false,
|
||||
}),
|
||||
{ discard: true },
|
||||
)
|
||||
|
||||
expect((yield* sessions.wait(session.id)).text).toBe("good answer")
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("finds a good answer exactly one full page back", () =>
|
||||
Effect.gen(function* () {
|
||||
duringIdle = Effect.void
|
||||
const sessions = yield* Session.Service
|
||||
const session = yield* sessions.create({ location })
|
||||
yield* seedAssistant(session.id, { id: "msg_wait_page_good", seq: 1, text: "good answer" })
|
||||
yield* Effect.forEach(
|
||||
Array.from({ length: 20 }, (_, i) => i),
|
||||
(i) => seedAssistant(session.id, { id: `msg_wait_page_failed_${i}`, seq: i + 2, text: "failed", failed: true }),
|
||||
{ discard: true },
|
||||
)
|
||||
|
||||
expect((yield* sessions.wait(session.id)).text).toBe("good answer")
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("returns NO_TEXT when no answer exists across several pages", () =>
|
||||
Effect.gen(function* () {
|
||||
duringIdle = Effect.void
|
||||
const sessions = yield* Session.Service
|
||||
const session = yield* sessions.create({ location })
|
||||
yield* Effect.forEach(
|
||||
Array.from({ length: 41 }, (_, i) => i),
|
||||
(i) =>
|
||||
i % 2 === 0
|
||||
? seedAssistant(session.id, { id: `msg_wait_none_failed_${i}`, seq: i + 1, text: "failed", failed: true })
|
||||
: seedAssistant(session.id, {
|
||||
id: `msg_wait_none_partial_${i}`,
|
||||
seq: i + 1,
|
||||
text: "partial",
|
||||
completed: false,
|
||||
}),
|
||||
{ discard: true },
|
||||
)
|
||||
|
||||
expect((yield* sessions.wait(session.id)).text).toBe(SubagentCompletion.NO_TEXT)
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("fails fast for an unknown session without waiting", () =>
|
||||
Effect.gen(function* () {
|
||||
awaited.length = 0
|
||||
|
||||
Reference in New Issue
Block a user