mirror of
https://github.com/stablyai/orca.git
synced 2026-10-08 08:02:32 +00:00
* feat(native-chat): publish each submission's journal positions A client never learned where in the journal a send was accepted or taken back, so drawing a stopped send had to guess from its own row and from clocks, and the guesses misfired after restarts. Each projected submission now carries `submittedSequence` (its submission row) and `resolvedSequence` (the dispatch row that resolved it; absent while pending): two optional fields, computed on every fold from rows already stored, never stored themselves. Older clients ignore them. The failure-fact schema moves to its own module to keep the journal schema file within its size limit. * test(native-chat): pin a submission's resolved position on the provider-echo accept path Also say which rows can resolve a submission, and that the submission schema, not the shared type, omits acceptedSequence. * refactor(native-chat): publish only where each submission was sent A rejected send's own row now moves to its rejection (#24710), so the row that resolved a send is the item's own position and needs no second copy. Keep submittedSequence, the one journal-order record of where a send was sent. * feat(native-chat): publish the turn a withdrawn Codex send was answered into A send Codex answered into a turn that then ended without taking it is rejected by that turn's end. The rejection row now names that turn's record, and the submission carries it as answeredInTurnItemId, so a client can tell which turn the send belonged to without guessing from journal order or clocks. Absent on every other send. * feat(native-chat): say how a withdrawn Codex send joined the turn it names The answered turn becomes { turnItemId, via }: 'start' when Codex answered the send's turn/start with that turn, 'steer' when Orca steered it into that running turn. A client can then tell the turn's opener from a send steered into it even when the opener is on an older page. * feat(native-chat): state on every rejection whether it names a turn A rejection a host writes now always carries answeredInTurn: the turn Codex answered the send into, or null for none. A reader can then tell 'answered into no turn' from a row written before the field existed, which keeps no answer and is placed by the older rules. * refactor(native-chat): give the answered-turn schema its own module Keeps the journal schema file within its line limit. * docs(native-chat): say an unreadable answered turn reads as none
236 lines
9.0 KiB
TypeScript
236 lines
9.0 KiB
TypeScript
import { agentSessionFailureFact, providerDiagnosticOf } from '../../shared/agent-session-failure'
|
|
import type {
|
|
AgentJournalMessageItem,
|
|
AgentJournalTurnJoin
|
|
} from '../../shared/agent-session-journal-types'
|
|
import type { NativeChatBlock } from '../../shared/native-chat-types'
|
|
import type { AgentSessionDispatchOutcome } from '../native-chat/agent-session-wire/structured-agent-session-adapter'
|
|
import {
|
|
isCodexAppServerRequestError,
|
|
type CodexAppServerConnection
|
|
} from './codex-app-server-connection'
|
|
import { isCodexAppServerUnsupportedError } from './codex-app-server-session'
|
|
import type { CodexDispatchEchoes } from './codex-structured-dispatch-echo'
|
|
import { codexTurnLifecycleIdentity } from './codex-structured-journal-translation-turns'
|
|
import { readCodexTurnId } from './codex-structured-thread-facts'
|
|
import {
|
|
codexRunningOrOpeningTurn,
|
|
type CodexTurnOpenWaits
|
|
} from './codex-structured-turn-open-wait'
|
|
import {
|
|
codexDispatchRejection,
|
|
codexTurnEndRejection
|
|
} from './codex-structured-turn-end-settlement'
|
|
import { decodeStructuredAgentSessionOptionValue } from '../../shared/structured-agent-session-option-codec'
|
|
|
|
// Writing a Codex turn and learning which message landed where, which are not
|
|
// the same event. The answer proves admission and nothing about identity, which
|
|
// the echo settles later; the turn it names is kept with the send, so that
|
|
// turn's end can settle it. A message sent while a turn is running goes in as
|
|
// `turn/steer` naming that turn, so the answer names the turn that carries it.
|
|
// `turn/start` would also steer it, with no second `turn/started`, but a Codex
|
|
// before 0.148 answers that with an id no turn ever opens or ends under. So a send
|
|
// made after Codex answered an earlier one, before it opened that turn, waits for
|
|
// the turn to open and steers it.
|
|
|
|
/** Keys Codex accepts as per-turn overrides. An unlisted key would otherwise
|
|
* become an arbitrary client-controlled `turn/start` parameter. Permission posture is owned by
|
|
* Agent Permissions and applied when the thread opens. */
|
|
const CODEX_TURN_OPTION_KEYS = new Set([
|
|
'model',
|
|
'effort',
|
|
'approvalsReviewer',
|
|
'personality',
|
|
'serviceTier',
|
|
'fastMode'
|
|
])
|
|
|
|
export function isCodexTurnOptionKey(key: string): boolean {
|
|
return CODEX_TURN_OPTION_KEYS.has(key)
|
|
}
|
|
|
|
/** The session state one turn needs. */
|
|
export type CodexTurnHost = {
|
|
connection: Pick<CodexAppServerConnection, 'request'>
|
|
threadId: string
|
|
options: Map<string, string>
|
|
reportedOptions?: { model?: string }
|
|
fastModeTierByModel: ReadonlyMap<string, string>
|
|
dispatchEchoes: CodexDispatchEchoes
|
|
activeTurnIds?: ReadonlySet<string>
|
|
turnOpenWaits: Pick<CodexTurnOpenWaits, 'wait'>
|
|
}
|
|
|
|
function turnInputFor(body: AgentJournalMessageItem): Record<string, unknown>[] {
|
|
const input: Record<string, unknown>[] = []
|
|
for (const block of body.blocks as NativeChatBlock[]) {
|
|
if (block.type === 'text' && block.text.length > 0) {
|
|
input.push({ type: 'text', text: block.text })
|
|
} else if (block.type === 'image-ref' && block.path) {
|
|
input.push({ type: 'localImage', path: block.path })
|
|
} else if (block.type === 'image-ref' && block.url) {
|
|
input.push({ type: 'image', url: block.url })
|
|
}
|
|
}
|
|
return input
|
|
}
|
|
|
|
function codexTurnOptions(host: CodexTurnHost): Record<string, string> {
|
|
const options = Object.fromEntries(
|
|
[...host.options].filter(([key]) => key !== 'fastMode' && key !== 'serviceTier')
|
|
)
|
|
const encodedFastMode = host.options.get('fastMode')
|
|
if (encodedFastMode === undefined) {
|
|
return options
|
|
}
|
|
const fastMode = decodeStructuredAgentSessionOptionValue('fastMode', encodedFastMode)
|
|
if (typeof fastMode !== 'boolean') {
|
|
throw new Error('codex fast mode must be encoded as true or false')
|
|
}
|
|
if (!fastMode) {
|
|
return { ...options, serviceTier: 'default' }
|
|
}
|
|
const model = host.options.get('model') ?? host.reportedOptions?.model
|
|
const tierId = model ? host.fastModeTierByModel.get(model) : undefined
|
|
// Fast is on but nothing has named the tier for this model yet, so there is no
|
|
// value to route to. Deliberately Standard rather than an omission: the tier
|
|
// persists on the thread, so omitting would silently keep routing a paid tier we
|
|
// cannot currently name, and discovery recovers the exact tier on a later turn.
|
|
if (!tierId) {
|
|
return { ...options, serviceTier: 'default' }
|
|
}
|
|
return { ...options, serviceTier: tierId }
|
|
}
|
|
|
|
/**
|
|
* Steers a send into the turn Codex last reported running. Null when Codex refused the steer,
|
|
* which it does before taking any input: that turn ended or changed, it cannot be steered, or
|
|
* this Codex has no `turn/steer`. Per-turn options ride on the next `turn/start`.
|
|
*/
|
|
async function steerCodexTurn(
|
|
host: CodexTurnHost,
|
|
expectedTurnId: string,
|
|
input: { clientMessageId: string; body: AgentJournalMessageItem; timeoutMs?: number }
|
|
): Promise<{ turnId: string; via: 'steer' } | null> {
|
|
try {
|
|
const answer = await host.connection.request(
|
|
'turn/steer',
|
|
{
|
|
threadId: host.threadId,
|
|
expectedTurnId,
|
|
clientUserMessageId: input.clientMessageId,
|
|
input: turnInputFor(input.body)
|
|
},
|
|
{ timeoutMs: input.timeoutMs }
|
|
)
|
|
return { turnId: readCodexTurnId(answer) ?? expectedTurnId, via: 'steer' }
|
|
} catch (error) {
|
|
if (isCodexAppServerRequestError(error) || isCodexAppServerUnsupportedError(error)) {
|
|
return null
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Hands one submission to Codex. False means the bounded correlation window
|
|
* refused it before the write; otherwise resolves with the turn Codex answered
|
|
* it into, or null when the answer named none.
|
|
*/
|
|
export async function startCodexTurn(
|
|
host: CodexTurnHost,
|
|
input: {
|
|
clientMessageId: string
|
|
body: AgentJournalMessageItem
|
|
requestedAt?: number
|
|
timeoutMs?: number
|
|
}
|
|
): Promise<{ turnId: string | null; via: AgentJournalTurnJoin } | false> {
|
|
// Armed before the write: the echo and `turn/started` can both land while the
|
|
// response is in flight, and the start must snapshot this send in its frontier.
|
|
if (!host.dispatchEchoes.arm(input.clientMessageId, input.requestedAt)) {
|
|
return false
|
|
}
|
|
const runningTurnId = await codexRunningOrOpeningTurn(host)
|
|
let steered = runningTurnId ? await steerCodexTurn(host, runningTurnId, input) : null
|
|
// Refused because a turn Orca heard of meanwhile is running: steer that one, once.
|
|
const runningSince = steered ? undefined : [...(host.activeTurnIds ?? [])].at(-1)
|
|
if (runningSince && runningSince !== runningTurnId) {
|
|
steered = await steerCodexTurn(host, runningSince, input)
|
|
}
|
|
if (steered) {
|
|
return steered
|
|
}
|
|
const answer = await host.connection.request(
|
|
'turn/start',
|
|
{
|
|
threadId: host.threadId,
|
|
clientUserMessageId: input.clientMessageId,
|
|
input: turnInputFor(input.body),
|
|
...codexTurnOptions(host)
|
|
},
|
|
{ timeoutMs: input.timeoutMs }
|
|
)
|
|
return { turnId: readCodexTurnId(answer), via: 'start' }
|
|
}
|
|
|
|
/**
|
|
* One submission's outcome as the wire must read it: admitted means Codex owns
|
|
* the message and its identity settles on the echo, rejected is Codex answering
|
|
* and declining. Elapsed time is never evidence here, because the wait a
|
|
* steered send would face is bounded only by the running turn.
|
|
*/
|
|
export async function dispatchCodexTurn(
|
|
session: CodexTurnHost,
|
|
input: {
|
|
sessionId: string
|
|
clientMessageId: string
|
|
body: AgentJournalMessageItem
|
|
requestedAt?: number
|
|
},
|
|
timeoutMs: number | undefined
|
|
): Promise<AgentSessionDispatchOutcome> {
|
|
let answer: { turnId: string | null; via: AgentJournalTurnJoin } | false
|
|
try {
|
|
answer = await startCodexTurn(session, { ...input, timeoutMs })
|
|
} catch (error) {
|
|
if (isCodexAppServerRequestError(error) || isCodexAppServerUnsupportedError(error)) {
|
|
// Codex answered and declined, so no echo for this write can arrive.
|
|
session.dispatchEchoes.disarm(input.clientMessageId)
|
|
// Codex's own words, when it gave any, are the one part of the error a person can use.
|
|
return {
|
|
state: 'rejected',
|
|
...codexDispatchRejection(
|
|
agentSessionFailureFact('providerRejected', { detail: providerDiagnosticOf(error) })
|
|
)
|
|
}
|
|
}
|
|
// A timeout or transport failure can happen after the frame was written.
|
|
// Keep the correlation armed so a later echo can prove delivery.
|
|
throw error
|
|
}
|
|
if (!answer) {
|
|
return { state: 'rejected', ...codexDispatchRejection(agentSessionFailureFact('queueFull')) }
|
|
}
|
|
// An answer read after the turn it names already ended is settled by that end.
|
|
const endedFirst = answer.turnId
|
|
? session.dispatchEchoes.bindTurn(
|
|
input.clientMessageId,
|
|
session.threadId,
|
|
answer.turnId,
|
|
answer.via
|
|
)
|
|
: null
|
|
const rejection = endedFirst ? codexTurnEndRejection(endedFirst) : null
|
|
return rejection && answer.turnId
|
|
? {
|
|
state: 'rejected',
|
|
answeredInTurn: {
|
|
turn: codexTurnLifecycleIdentity(input.sessionId, answer.turnId),
|
|
via: answer.via
|
|
},
|
|
...rejection
|
|
}
|
|
: { state: 'admitted' }
|
|
}
|