mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
fix(app): settle quote sends with the composer submit (#62037)
This commit is contained in:
committed by
GitHub
Unverified
parent
d0045aaab5
commit
ee368d2ab0
@@ -0,0 +1,76 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import {
|
||||
SERVER_PROMPT_STATUS,
|
||||
transitionPromptStatus,
|
||||
type PromptStatusEvent,
|
||||
type PromptStatusState,
|
||||
type PromptStatusTransition,
|
||||
} from "./prompt-status-machine"
|
||||
|
||||
function replay(events: PromptStatusEvent[], state: PromptStatusState = SERVER_PROMPT_STATUS) {
|
||||
const initial: PromptStatusTransition = { state, effects: [] }
|
||||
|
||||
return events.reduce((result, event) => {
|
||||
const next = transitionPromptStatus(result.state, event)
|
||||
|
||||
return { state: next.state, effects: [...result.effects, ...next.effects] }
|
||||
}, initial)
|
||||
}
|
||||
|
||||
describe("transitionPromptStatus", () => {
|
||||
test("a prompt that fails after a newer one went out leaves the newer prompt's turn running", () => {
|
||||
const result = replay([
|
||||
{ type: "dispatched", id: "msg_first", working: false },
|
||||
// The first prompt's optimistic status shows the Session as running when the second goes out.
|
||||
{ type: "dispatched", id: "msg_second", working: true },
|
||||
{ type: "failed", id: "msg_first" },
|
||||
])
|
||||
|
||||
expect(result.state).toEqual({ type: "optimistic", latest: "msg_second" })
|
||||
expect(result.effects).toEqual([{ type: "status.set", status: "running" }])
|
||||
|
||||
// The newest prompt owns the optimistic status, so its own failure still puts the Session back to idle.
|
||||
expect(transitionPromptStatus(result.state, { type: "failed", id: "msg_second" })).toEqual({
|
||||
state: SERVER_PROMPT_STATUS,
|
||||
effects: [{ type: "status.set", status: "idle" }],
|
||||
})
|
||||
})
|
||||
|
||||
test("a prompt to an idle Session shows it running until the prompt fails", () => {
|
||||
expect(
|
||||
replay([
|
||||
{ type: "dispatched", id: "msg_first", working: false },
|
||||
{ type: "failed", id: "msg_first" },
|
||||
]),
|
||||
).toEqual({
|
||||
state: SERVER_PROMPT_STATUS,
|
||||
effects: [
|
||||
{ type: "status.set", status: "running" },
|
||||
{ type: "status.set", status: "idle" },
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
test("once any prompt is admitted, a failure leaves the status to the server", () => {
|
||||
const admitted = replay([
|
||||
{ type: "dispatched", id: "msg_first", working: false },
|
||||
{ type: "dispatched", id: "msg_second", working: true },
|
||||
{ type: "admitted" },
|
||||
])
|
||||
|
||||
expect(admitted.state).toBe(SERVER_PROMPT_STATUS)
|
||||
expect(transitionPromptStatus(admitted.state, { type: "failed", id: "msg_second" })).toEqual({
|
||||
state: admitted.state,
|
||||
effects: [],
|
||||
})
|
||||
})
|
||||
|
||||
test("a prompt to a Session the server already reports running never touches its status", () => {
|
||||
const result = replay([
|
||||
{ type: "dispatched", id: "msg_first", working: true },
|
||||
{ type: "failed", id: "msg_first" },
|
||||
])
|
||||
|
||||
expect(result).toEqual({ state: SERVER_PROMPT_STATUS, effects: [] })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Who owns a Session's busy status while this client's prompts to it are in flight.
|
||||
* - `server`: the server's `session.execution.*` events own the status.
|
||||
* - `optimistic`: a prompt dispatched to an idle Session marked it running before the server said so, and no prompt
|
||||
* has been admitted since. `latest` is the newest prompt dispatched to the Session; only its failure puts the status
|
||||
* back to idle, so an older prompt that fails cannot hide the turn a newer prompt started.
|
||||
*/
|
||||
export type PromptStatusState = { readonly type: "server" } | { readonly type: "optimistic"; readonly latest: string }
|
||||
|
||||
export type PromptStatusEvent =
|
||||
/** `working` is whether the Session already showed as running when the prompt went out. */
|
||||
| { readonly type: "dispatched"; readonly id: string; readonly working: boolean }
|
||||
/** The server holds a prompt, so its events report the status from now on. */
|
||||
| { readonly type: "admitted" }
|
||||
| { readonly type: "failed"; readonly id: string }
|
||||
|
||||
export type PromptStatusEffect = { readonly type: "status.set"; readonly status: "running" | "idle" }
|
||||
|
||||
export type PromptStatusTransition = {
|
||||
readonly state: PromptStatusState
|
||||
readonly effects: readonly PromptStatusEffect[]
|
||||
}
|
||||
|
||||
export const SERVER_PROMPT_STATUS: PromptStatusState = Object.freeze({ type: "server" })
|
||||
|
||||
const NO_EFFECTS: readonly PromptStatusEffect[] = Object.freeze([])
|
||||
|
||||
export function transitionPromptStatus(state: PromptStatusState, event: PromptStatusEvent): PromptStatusTransition {
|
||||
if (event.type === "dispatched") {
|
||||
if (!event.working)
|
||||
return {
|
||||
state: { type: "optimistic", latest: event.id },
|
||||
effects: [{ type: "status.set", status: "running" }],
|
||||
}
|
||||
|
||||
if (state.type === "server") return { state, effects: NO_EFFECTS }
|
||||
|
||||
return { state: { type: "optimistic", latest: event.id }, effects: NO_EFFECTS }
|
||||
}
|
||||
|
||||
if (state.type === "server") return { state, effects: NO_EFFECTS }
|
||||
|
||||
if (event.type === "admitted") return { state: SERVER_PROMPT_STATUS, effects: NO_EFFECTS }
|
||||
|
||||
if (event.id !== state.latest) return { state, effects: NO_EFFECTS }
|
||||
|
||||
return { state: SERVER_PROMPT_STATUS, effects: [{ type: "status.set", status: "idle" }] }
|
||||
}
|
||||
@@ -3,7 +3,6 @@ import { createRoot } from "solid-js"
|
||||
import { createMemoryComposerState } from "./state"
|
||||
import {
|
||||
INITIAL_QUOTE_COMMENT_STATE,
|
||||
QUOTE_SEND_TIMEOUT_MS,
|
||||
createQuoteCommentController,
|
||||
selectQuoteCommentKey,
|
||||
selectQuoteCommentView,
|
||||
@@ -41,35 +40,29 @@ describe("transitionQuoteComment", () => {
|
||||
expect(replay([{ type: "retained", keys: [key] }], result.state).state).toBe(result.state)
|
||||
})
|
||||
|
||||
test("a send that fails or times out settles to failed and keeps the draft", () => {
|
||||
test("a send that fails settles to failed and keeps the draft, and one that goes through closes", () => {
|
||||
const sending = replay([
|
||||
{ type: "opened", key },
|
||||
{ type: "send.requested", draft: "note" },
|
||||
])
|
||||
|
||||
expect(sending.state).toMatchObject({ status: "sending", attempt: 1, key, draft: "note" })
|
||||
expect(sending.effects).toEqual([
|
||||
{ type: "send.run", attempt: 1, key, draft: "note", timeoutMs: QUOTE_SEND_TIMEOUT_MS },
|
||||
])
|
||||
expect(sending.effects).toEqual([{ type: "send.run", attempt: 1, key, draft: "note" }])
|
||||
|
||||
for (const event of [
|
||||
{ type: "send.timed-out", attempt: 1 },
|
||||
{ type: "send.settled", attempt: 1, sent: false },
|
||||
] satisfies QuoteCommentEvent[]) {
|
||||
const result = transitionQuoteComment(sending.state, event)
|
||||
|
||||
expect(result.state).toEqual({ status: "failed", issued: 1, key, draft: "note", anchor: undefined })
|
||||
expect(result.effects).toEqual([{ type: "timer.clear" }])
|
||||
expect(selectQuoteCommentView(result.state, key, "")).toMatchObject({ draft: "note", failed: true })
|
||||
}
|
||||
const failed = transitionQuoteComment(sending.state, { type: "send.settled", attempt: 1, sent: false })
|
||||
|
||||
expect(failed).toEqual({
|
||||
state: { status: "failed", issued: 1, key, draft: "note", anchor: undefined },
|
||||
effects: [],
|
||||
})
|
||||
expect(selectQuoteCommentView(failed.state, key, "")).toMatchObject({ draft: "note", failed: true })
|
||||
expect(transitionQuoteComment(sending.state, { type: "send.settled", attempt: 1, sent: true }).state).toEqual({
|
||||
status: "closed",
|
||||
issued: 1,
|
||||
})
|
||||
})
|
||||
|
||||
test("ignores chip toggles, edits and a second send while a send is in flight", () => {
|
||||
test("ignores chip toggles, edits, a second send and a stale result while a send is in flight", () => {
|
||||
const sending = replay([
|
||||
{ type: "opened", key },
|
||||
{ type: "send.requested", draft: "note" },
|
||||
@@ -80,6 +73,7 @@ describe("transitionQuoteComment", () => {
|
||||
{ type: "opened", key: "quote:c=2" },
|
||||
{ type: "draft.changed", draft: "other" },
|
||||
{ type: "send.requested", draft: "note" },
|
||||
{ type: "send.settled", attempt: 0, sent: false },
|
||||
] satisfies QuoteCommentEvent[])
|
||||
expect(transitionQuoteComment(sending, event)).toEqual({ state: sending, effects: [] })
|
||||
})
|
||||
@@ -91,7 +85,6 @@ describe("transitionQuoteComment", () => {
|
||||
{ type: "retained", keys: [] },
|
||||
])
|
||||
|
||||
expect(closed.effects.at(-1)).toEqual({ type: "timer.clear" })
|
||||
expect(transitionQuoteComment(closed.state, { type: "send.settled", attempt: 1, sent: false }).state).toBe(
|
||||
closed.state,
|
||||
)
|
||||
@@ -106,17 +99,19 @@ describe("transitionQuoteComment", () => {
|
||||
expect(failed.state).toEqual({ status: "open", issued: 1, key, draft: "fixed", anchor: undefined })
|
||||
})
|
||||
|
||||
test("disposing clears a running send timer and ignores later events", () => {
|
||||
test("disposing during a send ignores its late result and later events", () => {
|
||||
const disposed = replay([{ type: "opened", key }, { type: "send.requested", draft: "note" }, { type: "disposed" }])
|
||||
|
||||
expect(disposed.state).toEqual({ status: "disposed" })
|
||||
expect(disposed.effects.at(-1)).toEqual({ type: "timer.clear" })
|
||||
expect(transitionQuoteComment(disposed.state, { type: "send.settled", attempt: 1, sent: false }).state).toBe(
|
||||
disposed.state,
|
||||
)
|
||||
expect(transitionQuoteComment(disposed.state, { type: "opened", key }).state).toBe(disposed.state)
|
||||
})
|
||||
})
|
||||
|
||||
describe("createQuoteCommentController", () => {
|
||||
test("a rejected or hung send fails instead of spinning forever", async () => {
|
||||
test("a rejected send fails, and a slow one keeps sending until it goes through", async () => {
|
||||
jest.useFakeTimers()
|
||||
const controller = createQuoteCommentController()
|
||||
|
||||
@@ -127,9 +122,16 @@ describe("createQuoteCommentController", () => {
|
||||
await Promise.resolve()
|
||||
expect(controller.state()).toMatchObject({ status: "failed", draft: "note" })
|
||||
|
||||
controller.send("note", () => new Promise<boolean>(() => {}))
|
||||
jest.advanceTimersByTime(QUOTE_SEND_TIMEOUT_MS)
|
||||
expect(controller.state()).toMatchObject({ status: "failed", draft: "note" })
|
||||
// The request layer gives up on a silent server after a minute and the prompt send retries once, so a send that
|
||||
// the composer still runs must not show as failed: the user would retry it while the first one may still land.
|
||||
const slow = Promise.withResolvers<boolean>()
|
||||
controller.send("note", () => slow.promise)
|
||||
jest.advanceTimersByTime(5 * 60_000)
|
||||
expect(controller.state()).toMatchObject({ status: "sending", draft: "note" })
|
||||
slow.resolve(true)
|
||||
await Promise.resolve()
|
||||
await Promise.resolve()
|
||||
expect(controller.state()).toEqual({ status: "closed", issued: 2 })
|
||||
})
|
||||
|
||||
test("removing a quote from the composer closes its editor and highlight before a failed send restores it", () => {
|
||||
|
||||
@@ -2,16 +2,15 @@ import { createSignal, getOwner, onCleanup, untrack } from "solid-js"
|
||||
import type { ComposerQuoteAnchor } from "@opencode/plugin/gui"
|
||||
import type { QuoteSource } from "./quote-source"
|
||||
|
||||
// A quote send that never answers turns into a failure the user can retry, rather than a spinner forever.
|
||||
export const QUOTE_SEND_TIMEOUT_MS = 30_000
|
||||
|
||||
/**
|
||||
* The quote whose comment editor is open, and its send.
|
||||
* - `closed`: no editor is open.
|
||||
* - `open`: the editor shows `key`; `draft` stays `undefined` until the user edits, so the saved comment shows.
|
||||
* - `sending`: a direct send of `draft` is in flight; the editor stays open and ignores chip toggles and edits.
|
||||
* It ends only when the composer's submit settles, which the request layer bounds. A timeout of its own would
|
||||
* report a failure while the composer still sends, and a retry then could post the quote twice.
|
||||
* - `failed`: the send did not go through; the editor keeps the draft and shows an alert until the next edit.
|
||||
* - `disposed`: the composer state was evicted; late results and timers are ignored.
|
||||
* - `disposed`: the composer state was evicted; late results are ignored.
|
||||
*/
|
||||
export type QuoteCommentState =
|
||||
| { readonly status: "closed"; readonly issued: number }
|
||||
@@ -45,20 +44,16 @@ export type QuoteCommentEvent =
|
||||
| { readonly type: "draft.changed"; readonly draft: string }
|
||||
| { readonly type: "send.requested"; readonly draft: string }
|
||||
| { readonly type: "send.settled"; readonly attempt: number; readonly sent: boolean }
|
||||
| { readonly type: "send.timed-out"; readonly attempt: number }
|
||||
/** The context items still in the draft; an editor whose quote is gone closes. */
|
||||
| { readonly type: "retained"; readonly keys: readonly string[] }
|
||||
| { readonly type: "disposed" }
|
||||
|
||||
export type QuoteCommentEffect =
|
||||
| {
|
||||
readonly type: "send.run"
|
||||
readonly attempt: number
|
||||
readonly key: string
|
||||
readonly draft: string
|
||||
readonly timeoutMs: number
|
||||
}
|
||||
| { readonly type: "timer.clear" }
|
||||
export type QuoteCommentEffect = {
|
||||
readonly type: "send.run"
|
||||
readonly attempt: number
|
||||
readonly key: string
|
||||
readonly draft: string
|
||||
}
|
||||
|
||||
export type QuoteCommentTransition = {
|
||||
readonly state: QuoteCommentState
|
||||
@@ -77,33 +72,25 @@ export const INITIAL_QUOTE_COMMENT_STATE: QuoteCommentState = Object.freeze({ st
|
||||
|
||||
const NO_EFFECTS: readonly QuoteCommentEffect[] = Object.freeze([])
|
||||
|
||||
const CLEAR_TIMER: readonly QuoteCommentEffect[] = Object.freeze([{ type: "timer.clear" }])
|
||||
|
||||
export function transitionQuoteComment(state: QuoteCommentState, event: QuoteCommentEvent): QuoteCommentTransition {
|
||||
if (state.status === "disposed") return stay(state)
|
||||
|
||||
if (event.type === "disposed")
|
||||
return { state: { status: "disposed" }, effects: state.status === "sending" ? CLEAR_TIMER : NO_EFFECTS }
|
||||
if (event.type === "disposed") return { state: { status: "disposed" }, effects: NO_EFFECTS }
|
||||
|
||||
if (event.type === "retained") {
|
||||
if (state.status === "closed" || event.keys.includes(state.key)) return stay(state)
|
||||
|
||||
return {
|
||||
state: { status: "closed", issued: state.issued },
|
||||
effects: state.status === "sending" ? CLEAR_TIMER : NO_EFFECTS,
|
||||
}
|
||||
return { state: { status: "closed", issued: state.issued }, effects: NO_EFFECTS }
|
||||
}
|
||||
|
||||
if (state.status === "sending") {
|
||||
if ((event.type !== "send.settled" && event.type !== "send.timed-out") || event.attempt !== state.attempt)
|
||||
return stay(state)
|
||||
if (event.type !== "send.settled" || event.attempt !== state.attempt) return stay(state)
|
||||
|
||||
if (event.type === "send.settled" && event.sent)
|
||||
return { state: { status: "closed", issued: state.issued }, effects: CLEAR_TIMER }
|
||||
if (event.sent) return { state: { status: "closed", issued: state.issued }, effects: NO_EFFECTS }
|
||||
|
||||
return {
|
||||
state: { status: "failed", issued: state.issued, key: state.key, draft: state.draft, anchor: state.anchor },
|
||||
effects: CLEAR_TIMER,
|
||||
effects: NO_EFFECTS,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,7 +125,7 @@ export function transitionQuoteComment(state: QuoteCommentState, event: QuoteCom
|
||||
|
||||
return {
|
||||
state: { status: "sending", issued: attempt, attempt, key: state.key, draft: event.draft, anchor: state.anchor },
|
||||
effects: [{ type: "send.run", attempt, key: state.key, draft: event.draft, timeoutMs: QUOTE_SEND_TIMEOUT_MS }],
|
||||
effects: [{ type: "send.run", attempt, key: state.key, draft: event.draft }],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,7 +166,6 @@ export type QuoteCommentController = ReturnType<typeof createQuoteCommentControl
|
||||
|
||||
export function createQuoteCommentController() {
|
||||
const [state, setState] = createSignal(INITIAL_QUOTE_COMMENT_STATE)
|
||||
let timer: ReturnType<typeof setTimeout> | undefined
|
||||
// Handed from `send` to the `send.run` effect it causes in the same dispatch.
|
||||
let submit: ((key: string, comment: string) => Promise<boolean>) | undefined
|
||||
|
||||
@@ -192,15 +178,10 @@ export function createQuoteCommentController() {
|
||||
}
|
||||
|
||||
const run = (effect: QuoteCommentEffect) => {
|
||||
clearTimeout(timer)
|
||||
timer = undefined
|
||||
|
||||
if (effect.type === "timer.clear") return
|
||||
const send = submit
|
||||
submit = undefined
|
||||
|
||||
if (!send) return dispatch({ type: "send.settled", attempt: effect.attempt, sent: false })
|
||||
timer = setTimeout(() => dispatch({ type: "send.timed-out", attempt: effect.attempt }), effect.timeoutMs)
|
||||
void Promise.try(() => send(effect.key, effect.draft)).then(
|
||||
(sent) => dispatch({ type: "send.settled", attempt: effect.attempt, sent }),
|
||||
() => dispatch({ type: "send.settled", attempt: effect.attempt, sent: false }),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { describe, expect, jest, test } from "bun:test"
|
||||
import type { ModelSelection } from "@/providers/models/selection"
|
||||
import type { SessionMessageHandoff } from "@/session/handoff"
|
||||
import { Skill } from "@opencode/schema/skill"
|
||||
@@ -10,6 +10,7 @@ import type {
|
||||
NewSessionComposerAdapter,
|
||||
} from "./adapter"
|
||||
import { createMemoryComposerState, type Prompt } from "./state"
|
||||
import { composerSubmitView } from "./machine"
|
||||
import { createComposerSubmit } from "./submit"
|
||||
import { createSteerBackground } from "@/session/requests/steer-background"
|
||||
import { setMediaBlobLoader } from "@/runtime/persistence/drafts"
|
||||
@@ -105,6 +106,8 @@ function submitInput(
|
||||
})
|
||||
}
|
||||
|
||||
const sessions = { count: 0 }
|
||||
|
||||
function session(input: {
|
||||
calls: string[]
|
||||
prompt?: (value: Parameters<ComposerSession["data"]["session"]["prompt"]>[0]) => Promise<void>
|
||||
@@ -119,7 +122,8 @@ function session(input: {
|
||||
background?: ComposerSession["background"]
|
||||
}): ComposerSession {
|
||||
return {
|
||||
id: "session-1",
|
||||
// Unique per test: what this client knows about a session outlives the test that sent to it.
|
||||
id: `session-${++sessions.count}`,
|
||||
background: input.background,
|
||||
directory: "C:/repo",
|
||||
handoff: input.handoff ?? { set() {}, settle() {}, clear() {} },
|
||||
@@ -359,6 +363,77 @@ describe("Composer submission", () => {
|
||||
expect(calls.filter((call) => call === "send")).toHaveLength(2)
|
||||
})
|
||||
|
||||
test("a quote send keeps sending while the composer sends it, and fails only once the composer can retry", async () => {
|
||||
const state = createMemoryComposerState().capture()
|
||||
state.context.add({ type: "quote", text: "Selected passage", commentID: "selected" })
|
||||
const release = Promise.withResolvers<void>()
|
||||
const failed = Promise.withResolvers<void>()
|
||||
|
||||
const submission = submitInput(active(state, session({ calls: [], prompt: () => release.promise })), {
|
||||
missingSelection() {},
|
||||
unqueueable() {},
|
||||
failed: () => failed.resolve(),
|
||||
})
|
||||
|
||||
jest.useFakeTimers()
|
||||
state.quoteComment.set("quote:c=selected")
|
||||
state.quoteComment.send("note", submission.submitQuote)
|
||||
// A silent server holds a prompt for a minute per attempt; the send has not failed until the composer says so.
|
||||
jest.advanceTimersByTime(5 * 60_000)
|
||||
jest.useRealTimers()
|
||||
expect(state.quoteComment.state()).toMatchObject({ status: "sending", draft: "note" })
|
||||
expect(composerSubmitView(submission.state()).canSubmit).toBe(false)
|
||||
|
||||
release.reject(new Error("offline"))
|
||||
await failed.promise
|
||||
await new Promise((resolve) => setTimeout(resolve, 0))
|
||||
expect(state.quoteComment.state()).toMatchObject({ status: "failed", draft: "note" })
|
||||
expect(composerSubmitView(submission.state()).canSubmit).toBe(true)
|
||||
})
|
||||
|
||||
test("a prompt that fails after a newer one went out leaves the Session running", async () => {
|
||||
const state = createMemoryComposerState({ prompt: "first" }).capture()
|
||||
const statuses: ("idle" | "running")[] = []
|
||||
const first = Promise.withResolvers<void>()
|
||||
const second = Promise.withResolvers<void>()
|
||||
const failed = { count: 0, settled: Promise.withResolvers<void>() }
|
||||
|
||||
const target = session({
|
||||
calls: [],
|
||||
statuses,
|
||||
prompt: async (value) => {
|
||||
if (value.text === "first") return first.promise
|
||||
await second.promise
|
||||
},
|
||||
})
|
||||
|
||||
const submission = submitInput(
|
||||
{ ...active(state, target), working: () => statuses.at(-1) === "running" },
|
||||
{
|
||||
missingSelection() {},
|
||||
unqueueable() {},
|
||||
failed: () => {
|
||||
failed.count++
|
||||
failed.settled.resolve()
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
await submission.submit(new Event("submit"))
|
||||
state.set([{ type: "text", content: "second", start: 0, end: 6 }])
|
||||
await submission.submit(new Event("submit"))
|
||||
first.reject(new Error("offline"))
|
||||
await failed.settled.promise
|
||||
// The second prompt's turn may already run; only the server's events may end it.
|
||||
expect(statuses).toEqual(["running"])
|
||||
|
||||
failed.settled = Promise.withResolvers<void>()
|
||||
second.reject(new Error("offline"))
|
||||
await failed.settled.promise
|
||||
expect(failed.count).toBe(2)
|
||||
expect(statuses).toEqual(["running", "idle"])
|
||||
})
|
||||
|
||||
test("resends a failed prompt under its ID only while what it sends is unchanged", async () => {
|
||||
const state = createMemoryComposerState({ prompt: "ship it" }).capture()
|
||||
|
||||
|
||||
@@ -29,10 +29,20 @@ import {
|
||||
type SubmitSnapshot,
|
||||
} from "./machine"
|
||||
import { retryFingerprint } from "./retry"
|
||||
import {
|
||||
SERVER_PROMPT_STATUS,
|
||||
transitionPromptStatus,
|
||||
type PromptStatusEvent,
|
||||
type PromptStatusState,
|
||||
} from "./prompt-status-machine"
|
||||
|
||||
// The model each session was last switched to from this client, by session ID.
|
||||
const switched = new Map<string, string>()
|
||||
|
||||
// The optimistic busy status of each session, by session ID. Module-wide, because a new session's first prompt and
|
||||
// the follow-ups sent from its own composer go through different submit machines. A session left out is `server`.
|
||||
const statuses = new Map<string, PromptStatusState>()
|
||||
|
||||
type ComposerSubmitInput = {
|
||||
adapter: ComposerAdapter
|
||||
mode: Accessor<"normal" | "shell">
|
||||
@@ -458,11 +468,11 @@ function dispatchPrompt(input: ComposerSubmitInput, session: ComposerSession, va
|
||||
},
|
||||
},
|
||||
})
|
||||
const optimisticBusy = !input.adapter.working()
|
||||
const working = input.adapter.working()
|
||||
|
||||
const background = value.delivery === "steer" && !optimisticBusy ? session.background?.() : undefined
|
||||
const background = value.delivery === "steer" && working ? session.background?.() : undefined
|
||||
|
||||
if (optimisticBusy) session.data.session.setStatus(session.id, "running")
|
||||
updateStatus(session, { type: "dispatched", id: value.id, working })
|
||||
|
||||
const sending = sendPrompt(session, value, input.adapter.controls().model.selection.trackSessionCommit).then(
|
||||
() => ({ ok: true as const }),
|
||||
@@ -470,21 +480,21 @@ function dispatchPrompt(input: ComposerSubmitInput, session: ComposerSession, va
|
||||
)
|
||||
|
||||
// Settled here rather than when the composer settles, which an aborted promotion never reaches.
|
||||
if (value.delivery === "steer")
|
||||
void sending.then((result) => {
|
||||
if (result.ok || session.admitted(value.id)) return session.handoff.settle(value.id)
|
||||
session.handoff.clear(value.id)
|
||||
})
|
||||
void sending.then((result) => {
|
||||
const admitted = result.ok || session.admitted(value.id)
|
||||
updateStatus(session, admitted ? { type: "admitted" } : { type: "failed", id: value.id })
|
||||
|
||||
if (value.delivery !== "steer") return
|
||||
|
||||
if (admitted) return session.handoff.settle(value.id)
|
||||
session.handoff.clear(value.id)
|
||||
})
|
||||
|
||||
return {
|
||||
// The composer settles after clearing its captured draft; a quote settles before removing only its context.
|
||||
settled: (restore) =>
|
||||
sending.then((result) => {
|
||||
const admitted =
|
||||
result.ok ||
|
||||
failSubmission(input, session, "prompt", result.error, restore, value.id, () => {
|
||||
if (optimisticBusy) session.data.session.setStatus(session.id, "idle")
|
||||
})
|
||||
const admitted = result.ok || failSubmission(input, session, "prompt", result.error, restore, value.id)
|
||||
|
||||
if (!admitted) return false
|
||||
|
||||
@@ -496,6 +506,15 @@ function dispatchPrompt(input: ComposerSubmitInput, session: ComposerSession, va
|
||||
}
|
||||
}
|
||||
|
||||
function updateStatus(session: ComposerSession, event: PromptStatusEvent) {
|
||||
const result = transitionPromptStatus(statuses.get(session.id) ?? SERVER_PROMPT_STATUS, event)
|
||||
|
||||
if (result.state.type === "server") statuses.delete(session.id)
|
||||
|
||||
if (result.state.type === "optimistic") statuses.set(session.id, result.state)
|
||||
result.effects.forEach((effect) => session.data.session.setStatus(session.id, effect.status))
|
||||
}
|
||||
|
||||
// Puts a failed submission back, unless the composer changed since it cleared: then the edit is the user's.
|
||||
function restoreSubmission(
|
||||
input: ComposerSubmitInput,
|
||||
@@ -733,10 +752,8 @@ function failSubmission(
|
||||
error: unknown,
|
||||
restore: () => boolean,
|
||||
messageID?: string,
|
||||
rollback?: () => void,
|
||||
) {
|
||||
if (messageID && session.admitted(messageID)) return true
|
||||
rollback?.()
|
||||
restore()
|
||||
input.notify.failed(kind, error)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user