Keep Codex messages queued until their settings can be applied (#41476)

* Preserve queued Codex input through turn admission

* Correct admission regression assertions

* Establish accepted input in queued send receipt regression

* Preserve sparse and legacy Codex input intent across writes

* Narrow legacy option regression input type

* Use native Fast label in legacy tier regression fixture

* Assert goal settings eligibility through observable refusal outcomes

* Establish real input eligibility in queue lifecycle fixtures

* test(chat): establish pending mail handover boundaries

* test(chat): isolate stale mail cleanup at deletion boundary

* Keep successful turn mode and settings reply ownership current

* test: narrow queued mode regression input
This commit is contained in:
Brennan Benson
2026-11-12 14:52:16 -08:00
committed by GitHub
parent c679989e6d
commit 810d82d3e9
24 changed files with 1030 additions and 175 deletions
@@ -161,6 +161,7 @@ export class CodexStructuredSessionAdapter implements SessionAdapter {
return admission
}
if (event.type === 'notification') {
turnEnd.publishCodexTurnOpened(session, event, this.sessions)
// Only an admitted turn end settles; a refused one settles on the retry that lands.
turnEnd.settleCodexSendsInEndedTurn(session, event)
// Child records observe refused frames only after a retry reaches the journal.
@@ -340,7 +341,6 @@ export class CodexStructuredSessionAdapter implements SessionAdapter {
hasOwnedAcquisition = (sessionId: string): boolean =>
this.acquisitions.get(sessionId) !== undefined || this.sessions.has(sessionId)
private session(sessionId: string): CodexSession {
return requireLiveCodexSession(this.sessions, sessionId)
}
private session = (sessionId: string): CodexSession =>
requireLiveCodexSession(this.sessions, sessionId)
}
@@ -27,6 +27,7 @@ import {
reportedCodexSessionOptions
} from '../catalog/codex-structured-session-options'
import { CodexSkillReport } from '../catalog/codex-structured-skill-report'
import { codexMessageInputOptions } from '../turns/codex-structured-message-options'
import { codexAcquireCatalogAccess } from '../catalog/codex-structured-acquire-catalog'
import {
CODEX_DEFAULT_SERVICE_TIER,
@@ -183,6 +184,10 @@ export async function openCodexStructuredSession(
...(opened.effort ? { effort: opened.effort } : {}),
...(serviceTier ? { serviceTier } : {})
},
appliedTurnInputOptions: codexMessageInputOptions(input.options ?? {}, {
serviceTier: CODEX_DEFAULT_SERVICE_TIER,
interactionMode: 'default'
}),
appliedSettings: attempt.window.appliedSettings,
...(catalogAccess ? { catalogAccess } : {}),
dispatchEchoes: start.dispatchEchoes,
@@ -179,6 +179,8 @@ export type CodexSession = {
nativeDefaults?: { personality?: string; serviceTier?: string }
/** Acquisition facts overlaid by controls actually sent, independently of reports or saved picks. */
appliedTurnOptions?: Readonly<Record<string, string>>
/** Saved/start input choices before protocol shaping; later catalogs cannot change eligibility. */
appliedTurnInputOptions?: Readonly<Record<string, string>>
appliedSettings?: CodexAppliedSettings
/** Optional for adapters without a host catalog store. */
catalogAccess?: CodexSessionCatalogAccess
@@ -4,7 +4,10 @@ import { CodexAppliedSettings } from '../catalog/codex-structured-applied-settin
import { CodexAppServerRequestError } from '../../app-server/codex-app-server-request-error'
import { changeCodexThreadGoal } from './codex-structured-thread-goal'
import type { CodexSession } from '../session-lifecycle/codex-structured-session-state'
import { waitsForCodexTurnOptions } from '../../../native-chat/agent-session-wire/options/structured-agent-session-message-options'
import {
codexTurnOptionsDiffer,
waitsForCodexTurnOptions
} from '../../../native-chat/agent-session-wire/options/structured-agent-session-message-options'
const THREAD = 'thread-1'
@@ -89,31 +92,16 @@ describe('direct goal settings application', () => {
threadId: 'thread-abc',
threadSettings: { model, effort }
})
const goal = (model = 'gpt-live', effort?: string) =>
const goal = (model = 'gpt-live', effort?: string, signal?: AbortSignal) =>
adapter.changeThreadGoal({
sessionId: 'session-1',
fence: 7,
change: { kind: 'status', status: 'active' },
replacesGoal: false,
executionOptions: { model, ...(effort ? { effort } : {}) }
executionOptions: { model, ...(effort ? { effort } : {}) },
signal
})
const state = acquiredSettingsTestSeam(adapter)
return { codex, adapter, apply, goal, settings: state.settings, state, acquisition }
}
it('keeps literal goal settings separate from exact applied turn proof and recovers on a successful turn', async () => {
const { codex, adapter, apply, goal, acquisition } = await fixture()
const choices = { model: 'unlisted-model-a', effort: 'high' }
const turnProof = { model: 'unlisted-model-a', effort: 'high', interactionMode: 'default' }
const dispatch = () =>
adapter.dispatch({
sessionId: 'session-1',
fence: 7,
clientMessageId: `message-${codex.connections[0].calls.length}`,
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'Continue' }] },
executionOptions: choices
})
const held = (options = choices) =>
const held = (options: Record<string, string>) =>
waitsForCodexTurnOptions(
{ provider: 'codex', sessionId: 'session-1' },
{
@@ -138,16 +126,37 @@ describe('direct goal settings application', () => {
{ fence: 7, generation: acquisition.acquisitionGeneration, phase: 'ready' },
adapter
)
const proof = () =>
adapter.readAppliedInputOptions({
return {
codex,
adapter,
apply,
goal,
held,
get settings() {
return acquiredSettingsTestSeam(adapter).settings
},
get state() {
return acquiredSettingsTestSeam(adapter)
},
acquisition
}
}
it('preserves goal choice eligibility through readback, refusal and successful-turn recovery', async () => {
const { codex, adapter, apply, goal, held: heldOptions } = await fixture()
const choices = { model: 'unlisted-model-a', effort: 'high' }
const dispatch = () =>
adapter.dispatch({
sessionId: 'session-1',
fence: 7,
acquisitionGeneration: acquisition.acquisitionGeneration,
next: choices
clientMessageId: `message-${codex.connections[0].calls.length}`,
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'Continue' }] },
executionOptions: choices
})
const held = (options = choices) => heldOptions(options)
try {
expect(await dispatch()).toMatchObject({ state: 'admitted' })
expect(proof()?.active).toEqual(turnProof)
expect(held()).toBe(false)
codex.routes['thread/settings/update'] = (params) => {
apply(String(params?.model), String(params?.effort))
return {}
@@ -161,15 +170,18 @@ describe('direct goal settings application', () => {
params: { threadId: 'thread-abc', model: 'unlisted-model-b', effort: 'low' }
}
])
const appliedGoal = { model: 'unlisted-model-b', effort: 'low', interactionMode: 'default' }
expect(proof()).toEqual({ active: appliedGoal, next: turnProof })
const goalChoices = { model: 'unlisted-model-b', effort: 'low' }
expect(held(goalChoices)).toBe(false)
expect(held()).toBe(true)
apply('observation-only', 'medium')
expect(proof()).toEqual({ active: appliedGoal, next: turnProof })
expect(held(goalChoices)).toBe(false)
expect(held()).toBe(true)
codex.routes['thread/settings/update'] = () => {
throw new CodexAppServerRequestError('thread/settings/update', -32602, 'invalid model')
}
expect(await goal('refused-model', 'high')).toMatchObject({ ok: false })
expect(proof()).toEqual({ active: appliedGoal, next: turnProof })
expect(held(goalChoices)).toBe(false)
expect(held()).toBe(true)
expect(
codex.connections[0].calls.filter((call) => call.method === 'thread/goal/set')
).toHaveLength(1)
@@ -178,8 +190,8 @@ describe('direct goal settings application', () => {
(call) => call.method === 'thread/settings/update'
).length
expect(await goal('observation-only-c', 'medium')).toEqual({ ok: true })
expect(proof()).toEqual({ active: appliedGoal, next: turnProof })
expect(held(appliedGoal)).toBe(false)
expect(held()).toBe(true)
expect(held(goalChoices)).toBe(false)
expect(held({ model: 'observation-only-c', effort: 'medium' })).toBe(true)
expect(
codex.connections[0].calls.filter((call) => call.method === 'thread/settings/update')
@@ -188,35 +200,150 @@ describe('direct goal settings application', () => {
throw new Error('reply lost after settings enqueue')
}
expect(await goal('uncertain-model', 'low')).toMatchObject({ ok: false })
expect(proof()).toEqual({
active: { effort: 'low', interactionMode: 'default' },
next: turnProof
})
expect(held()).toBe(true)
expect(await dispatch()).toMatchObject({ state: 'admitted' })
expect(proof()).toEqual({ active: turnProof, next: turnProof })
expect(held()).toBe(false)
codex.routes['thread/settings/update'] = () => {
throw new CodexAppServerRequestError('thread/settings/update', -32601, 'method not found')
}
expect(await goal('unlisted-model-b', 'low')).toEqual({ ok: true })
expect(proof()).toEqual({ active: turnProof, next: turnProof })
expect(held()).toBe(false)
const callsBeforeCached = codex.connections[0].calls.filter(
(call) => call.method === 'thread/settings/update'
).length
expect(await goal('unlisted-model-b', 'low')).toEqual({ ok: true })
expect(proof()).toEqual({ active: turnProof, next: turnProof })
expect(held()).toBe(false)
expect(held(appliedGoal)).toBe(true)
expect(held(goalChoices)).toBe(true)
expect(
codex.connections[0].calls.filter((call) => call.method === 'thread/settings/update')
).toHaveLength(callsBeforeCached)
expect(await dispatch()).toMatchObject({ state: 'admitted' })
expect(proof()).toEqual({ active: turnProof, next: turnProof })
expect(held()).toBe(false)
} finally {
await adapter.closeAll()
}
})
it.each([
['success', false],
['refusal', false],
['success', true],
['refusal', true]
] as const)(
'preserves a newer start after late goal %s (replaced acquisition: %s)',
async (outcome, replaceAcquisition) => {
const { codex, adapter, goal, acquisition } = await fixture()
let generation = acquisition.acquisitionGeneration
const dispatch = (model: string) =>
adapter.dispatch({
sessionId: 'session-1',
fence: 7,
clientMessageId: model,
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'Continue' }] },
executionOptions: { model, effort: 'high' }
})
const answer = Promise.withResolvers<unknown>()
const authority = new AbortController()
try {
expect(await dispatch('model-a')).toMatchObject({ state: 'admitted' })
const request = vi.spyOn(codex.connections[0], 'request')
codex.routes['thread/settings/update'] = () => answer.promise
const pending = goal('model-b', 'high', authority.signal)
const rpc = request.mock.results.at(-1)?.value
expect(rpc).toBeInstanceOf(Promise)
authority.abort(new Error('stopped'))
expect(await pending).toMatchObject({ ok: false })
if (replaceAcquisition) {
await adapter.closeSession('session-1')
const { acquireReadyCodexForTest, identityFor } =
await import('../session-lifecycle/codex-structured-session-adapter-fixture')
const replacement = await acquireReadyCodexForTest(adapter, {
identity: identityFor('session-1'),
fence: 7,
spawnToken: 'replacement'
})
generation = replacement.acquisitionGeneration
expect(
adapter.readAppliedInputOptions({
sessionId: 'session-1',
fence: 7,
acquisitionGeneration: acquisition.acquisitionGeneration,
next: {}
})
).toBeNull()
}
expect(await dispatch('model-c')).toMatchObject({ state: 'admitted' })
if (outcome === 'success') {
answer.resolve({})
} else {
answer.reject(
new CodexAppServerRequestError('thread/settings/update', -32602, 'invalid model')
)
}
await Promise.allSettled([rpc])
const differs = (model: string) =>
codexTurnOptionsDiffer(
{ provider: 'codex', sessionId: 'session-1' },
{ model, effort: 'high' },
{ fence: 7, generation, phase: 'ready' },
adapter
)
expect(differs('model-c')).toBe(false)
expect(differs('model-a')).toBe(true)
expect(differs('model-b')).toBe(true)
expect(
codex.connections
.flatMap((connection) => connection.calls)
.some((call) => call.method.startsWith('thread/goal/'))
).toBe(false)
} finally {
await adapter.closeAll()
}
}
)
it.each(['success', 'refusal'] as const)(
'settles an owned late %s after abort without resuming the goal',
async (outcome) => {
const { codex, adapter, goal, held } = await fixture()
const answer = Promise.withResolvers<unknown>()
const authority = new AbortController()
try {
await adapter.dispatch({
sessionId: 'session-1',
fence: 7,
clientMessageId: 'prior',
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'Continue' }] },
executionOptions: { model: 'model-a', effort: 'high' }
})
const request = vi.spyOn(codex.connections[0], 'request')
codex.routes['thread/settings/update'] = () => answer.promise
const pending = goal('model-b', 'high', authority.signal)
const rpc = request.mock.results.at(-1)?.value
expect(rpc).toBeInstanceOf(Promise)
authority.abort(new Error('stopped'))
expect(await pending).toMatchObject({ ok: false })
if (outcome === 'success') {
answer.resolve({})
} else {
answer.reject(
new CodexAppServerRequestError('thread/settings/update', -32602, 'invalid model')
)
}
await Promise.allSettled([rpc])
expect(held({ model: 'model-b', effort: 'high' })).toBe(outcome !== 'success')
expect(held({ model: 'model-a', effort: 'high' })).toBe(outcome === 'success')
expect(
codex.connections
.flatMap((connection) => connection.calls)
.some((call) => call.method.startsWith('thread/goal/'))
).toBe(false)
} finally {
await adapter.closeAll()
}
}
)
it.each(['success', 'rejection', 'stale'] as const)(
'rechecks current application after its RPC reply, preserving pre-reply evidence (%s)',
async (mode) => {
@@ -120,7 +120,16 @@ export async function changeCodexThreadGoalWithSettings(
try {
if (startsWork) {
const requested = resolvedCodexGoalSettings(session, input.executionOptions)
const previousApplied = session.appliedTurnOptions
let previousApplied = session.appliedTurnOptions
let previousInput = session.appliedTurnInputOptions
let invalidatedApplied: CodexSession['appliedTurnOptions']
let invalidatedInput: CodexSession['appliedTurnInputOptions']
const ownsAcquisition = () =>
current() &&
!session.ended &&
!session.connection.closed &&
session.fence === input.fence &&
session.acquisitionGeneration === generation
const installed = await session.appliedSettings?.install(
session.connection,
session.threadId,
@@ -128,19 +137,44 @@ export async function changeCodexThreadGoalWithSettings(
timeoutMs,
input.signal,
() => {
session.appliedTurnOptions = { ...session.appliedTurnOptions, ...requested }
if (!ownsAcquisition()) {
return
}
if (session.appliedTurnOptions === invalidatedApplied) {
session.appliedTurnOptions = { ...invalidatedApplied, ...requested }
}
if (session.appliedTurnInputOptions === invalidatedInput) {
session.appliedTurnInputOptions = { ...invalidatedInput, ...requested }
}
},
() => {
session.appliedTurnOptions = previousApplied
if (!ownsAcquisition()) {
return
}
if (session.appliedTurnOptions === invalidatedApplied) {
session.appliedTurnOptions = previousApplied
}
if (session.appliedTurnInputOptions === invalidatedInput) {
session.appliedTurnInputOptions = previousInput
}
},
() => {
previousApplied = session.appliedTurnOptions
previousInput = session.appliedTurnInputOptions
const known = { ...previousApplied }
const knownInput = { ...previousInput }
for (const key of ['model', 'effort'] as const) {
if (requested[key] !== undefined && requested[key] !== known[key]) {
delete known[key]
}
if (requested[key] !== undefined && requested[key] !== knownInput[key]) {
delete knownInput[key]
}
}
session.appliedTurnOptions = known
invalidatedApplied = known
invalidatedInput = knownInput
session.appliedTurnOptions = invalidatedApplied
session.appliedTurnInputOptions = invalidatedInput
}
)
if (installed && !installed.ok) {
@@ -1,7 +1,13 @@
import { describe, expect, it } from 'vitest'
import { describe, expect, it, vi } from 'vitest'
import type { AgentJournalItemBody } from '../../../../shared/agent-session/journal/agent-session-journal-types'
import { agentJournalSubmissionKey } from '../../../../shared/agent-session/journal/agent-session-journal-item-key'
import type { StructuredAgentSessionEventSink } from '../../../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink'
import {
createDeferredStructuredAgentSessionEventSink,
type StructuredAgentSessionEventSink
} from '../../../native-chat/agent-session-wire/journal-ingress/structured-agent-session-event-sink'
import { createFixture } from '../../../native-chat/agent-session-journal/store-lifecycle/journal-store-contract-test-fixture'
import { testEventSinkLogging } from '../../../native-chat/agent-session-wire/host/testing/structured-agent-session-logger-test-support'
import { StructuredAgentSessionCurrentWork } from '../../../native-chat/agent-session-wire/observations/structured-agent-session-current-work'
import { MAX_CODEX_PENDING_DISPATCH_ECHOES } from './codex-structured-dispatch-echo'
import {
acquiredCodexAdapter,
@@ -43,6 +49,26 @@ function lifecycleRecorder(): {
}
describe('codex dispatch admission', () => {
it('releases acknowledged steers without an echo and retains each late receipt', async () => {
const codex = fakeCodexAppServer({ 'turn/steer': () => ({ turnId: 'turn-1' }) })
const settlements: LateSettlement[] = []
const adapter = await acquiredCodexAdapter({ codex, settlements })
const connection = codex.connections[0]!
startTurn(connection, 'turn-1')
expect(await send(adapter, 'client-1')).toMatchObject({ state: 'admitted' })
expect(adapter.heldSends('session-1')).toEqual(new Set())
expect(await send(adapter, 'client-2')).toMatchObject({ state: 'admitted' })
expect(adapter.heldSends('session-1')).toEqual(new Set())
echoUserMessage(connection, { turnId: 'turn-1', itemId: 'late-1', clientId: 'client-1' })
echoUserMessage(connection, { turnId: 'turn-1', itemId: 'late-2', clientId: 'client-2' })
expect(settlements.map(({ clientMessageId }) => clientMessageId)).toEqual([
'client-1',
'client-2'
])
expect(connection.calls.filter(({ method }) => method === 'turn/steer')).toHaveLength(2)
await adapter.closeSession('session-1')
})
it('correlates each send by client message id, not queue order', async () => {
const codex = fakeCodexAppServer({ 'turn/steer': () => ({ turnId: 'turn-1' }) })
const settlements: LateSettlement[] = []
@@ -190,3 +216,73 @@ describe('codex dispatch admission', () => {
expect(settlements).toEqual([])
})
})
describe('ordered Codex admission publication', () => {
const fixture = createFixture()
it('holds a replied start through refused and delayed journal writes, then releases without an echo', async () => {
const journal = await fixture.open()
const deferred = createDeferredStructuredAgentSessionEventSink(testEventSinkLogging())
deferred.bind({ journal, fence: 7, publish: () => {} })
let refuse = true
const revise = deferred.sink.tryReviseResolvedItem!
deferred.sink.tryReviseResolvedItem = (...args) =>
refuse ? { accepted: false, reason: 'backpressure' } : revise(...args)
const write = journal.appendResolvedItem.bind(journal)
const delay = Promise.withResolvers<void>()
let delayed = false
vi.spyOn(journal, 'appendResolvedItem').mockImplementation(async (...args) => {
delayed = true
await delay.promise
return write(...args)
})
const codex = fakeCodexAppServer({ 'turn/start': () => ({ turn: { id: 'ordered-turn' } }) })
let work: StructuredAgentSessionCurrentWork | undefined
const observed: { active: string | null; working: boolean }[] = []
const adapter = await acquiredCodexAdapter({
codex,
settlements: [],
sink: deferred.sink,
onHeldSendsChanged: () => {
if (work) {
observed.push({ active: work.activeTurnId(), working: work.working() })
}
}
})
work = new StructuredAgentSessionCurrentWork(journal, 7, 0, undefined, undefined, false, () =>
adapter.heldSends('session-1')
)
await journal.appendSubmission({
clientMessageId: 'ordered',
payloadFingerprint: 'ordered-input',
body: CODEX_TEST_USER_MESSAGE,
fence: 7
})
await send(adapter, 'ordered')
const submission = journal.submission('ordered')!
expect(work.publishedSubmission(submission)).toMatchObject({ dispatchState: 'pending' })
expect(work.working()).toBe(true)
startTurn(codex.connections[0]!, 'ordered-turn')
expect(work.activeTurnId()).toBeNull()
expect(work.working()).toBe(true)
refuse = false
await vi.waitFor(() => expect(delayed).toBe(true))
expect(work.activeTurnId()).toBeNull()
expect(work.publishedSubmission(submission)).toMatchObject({ dispatchState: 'pending' })
delay.resolve()
await deferred.sink.written!()
await vi.waitFor(() =>
expect(work!.publishedSubmission(submission)).toMatchObject({
dispatchState: 'unknown',
recovered: true,
reason: 'provider_write_outcome_unknown'
})
)
expect(work.activeTurnId()).toBe('ordered-turn')
expect(work.working()).toBe(true)
expect(observed).toEqual([{ active: 'ordered-turn', working: true }])
await adapter.closeSession('session-1')
deferred.close()
})
})
@@ -73,7 +73,7 @@ it.each(['start', 'steer'] as const)(
describe('the sends Codex holds', () => {
const held = (echoes: ReturnType<typeof createCodexDispatchEchoes>) => [...echoes.heldSends()]
it('holds a send from its write until the turn it was answered into ends, echo or not', () => {
it('holds a replied start until its turn is published without consuming its echo', () => {
const changes: string[] = []
const echoes = createCodexDispatchEchoes(() => changes.push(held(echoes).join(',')))
echoes.arm('sent')
@@ -82,12 +82,34 @@ describe('the sends Codex holds', () => {
echoes.bindTurn('sent', 'thread-1', 'turn-1', 'start', echoes.tick())
expect(held(echoes)).toEqual(['sent'])
// A hook can refuse input and complete the turn without echoing it.
echoes.endTurn('thread-1', 'turn-1', { status: 'completed' })
echoes.opened('thread-1', 'turn-1')
expect(held(echoes)).toEqual(['sent'])
echoes.published('thread-1', 'turn-1')
expect(held(echoes)).toEqual([])
expect(changes).toEqual([''])
})
it('releases a replied steer into a published turn while retaining exact late correlation', () => {
const echoes = createCodexDispatchEchoes()
echoes.published('thread-1', 'turn-1')
echoes.arm('steered')
echoes.expect('steered', 'thread-1', 'steer', [], 'turn-1')
expect(held(echoes)).toEqual(['steered'])
echoes.bindTurn('steered', 'thread-1', 'turn-1', 'steer', echoes.tick())
expect(held(echoes)).toEqual([])
expect(
readCodexDispatchEcho(
{
type: 'userMessage',
id: 'late-user',
clientId: 'steered'
},
CODEX_IDENTITY,
echoes
)?.clientMessageId
).toBe('steered')
})
it('holds a send answered into no turn only while a status read after its answer says running', () => {
const echoes = createCodexDispatchEchoes()
echoes.arm('timed-out')
@@ -8,10 +8,7 @@ import type { ProviderDiagnostic } from '../../../../shared/agent-session/failur
import type { AgentJournalItemIdentity } from '../../../../shared/agent-session/journal/agent-session-journal-identity'
import type { AgentJournalTurnJoin } from '../../../../shared/agent-session/journal/agent-session-journal-types'
export {
MAX_CODEX_PENDING_DISPATCH_ECHOES,
MAX_CODEX_RECORDED_TURN_ENDS
} from './codex-dispatch-receipt-correlation'
export { MAX_CODEX_PENDING_DISPATCH_ECHOES, MAX_CODEX_RECORDED_TURN_ENDS }
export type CodexTurnEnd =
| { status: 'completed' }
@@ -31,7 +28,7 @@ export type CodexDispatchEchoes = {
answered: (clientMessageId: string, at: number) => void
/** Only status received after a no-turn answer determines whether that send is held. */
observeStatus: (active: boolean) => void
/** Sends still executing; ended turns release them even without an echo. */
/** Sends awaiting admission to a published turn; exact echoes remain independently armed. */
heldSends: () => ReadonlySet<string>
/** False at capacity preserves older pending sends. */
arm: (clientMessageId: string, requestedAt?: number) => boolean
@@ -63,6 +60,8 @@ export type CodexDispatchEchoes = {
/** Mark a timed-out open wait so later sends do not wait again. */
leftUnopened: (threadId: string, turnId: string) => void
opened: (threadId: string, turnId: string) => void
/** The turn's admitted journal writes landed; receipt observation alone cannot release work. */
published: (threadId: string, turnId: string) => void
/** An error naming a never-opened turn counts as its end. */
hasOpened: (threadId: string, turnId: string) => boolean
/** Latest answered turn still unopened after its wait was exhausted. */
@@ -105,12 +104,14 @@ export function createCodexDispatchEchoes(
>()
let clock = 0
let status: { active: boolean; at: number } | null = null
const publishedTurns = new Set<string>()
const heldSends = (): Set<string> =>
new Set(
[...armed].flatMap(([clientMessageId, entry]) =>
entry.answeredAt === undefined ||
(entry.turn
? entry.turnEnded !== true
? entry.turnEnded !== true &&
!publishedTurns.has(turnKey(entry.turn.threadId, entry.turn.turnId))
: status?.active === true && status.at > entry.answeredAt)
? [clientMessageId]
: []
@@ -224,6 +225,16 @@ export function createCodexDispatchEchoes(
openedTurns.delete(oldest)
}
},
published: (threadId, turnId) =>
reporting(() => {
publishedTurns.add(turnKey(threadId, turnId))
for (const oldest of publishedTurns) {
if (publishedTurns.size <= MAX_CODEX_RECORDED_TURN_ENDS) {
break
}
publishedTurns.delete(oldest)
}
}),
hasOpened: (threadId, turnId) => openedTurns.has(turnKey(threadId, turnId)),
endTurn: (threadId, turnId, end) =>
reporting(() => {
@@ -286,6 +297,7 @@ export function createCodexDispatchEchoes(
receiptCorrelation.clear()
unopenedTurns.clear()
openedTurns.clear()
publishedTurns.clear()
nextSequence = 0
},
get size() {
@@ -99,6 +99,7 @@ export async function acquiredCodexAdapter(input: {
sink?: StructuredAgentSessionEventSink
requestTimeoutMs?: number
permissionMode?: AgentChatPermissionChoice
onHeldSendsChanged?: CodexStructuredSessionAdapterDeps['onHeldSendsChanged']
}): Promise<CodexStructuredSessionAdapter> {
const adapter = new CodexStructuredSessionAdapter({
resolveLaunch: async () => ({
@@ -114,6 +115,7 @@ export async function acquiredCodexAdapter(input: {
onDispatchSettledLate: (settlement) => {
input.settlements.push(settlement)
},
...(input.onHeldSendsChanged ? { onHeldSendsChanged: input.onHeldSendsChanged } : {}),
...(input.requestTimeoutMs === undefined ? {} : { requestTimeoutMs: input.requestTimeoutMs })
})
const identity: AgentSessionJournalIdentity = {
@@ -9,11 +9,28 @@ import { claudeAndCodexAgents } from '../../../native-chat/agent-session-wire/pr
import { describe, expect, it, vi } from 'vitest'
import { AgentModelCatalogStore } from '../../../native-chat/agent-model-catalog/agent-model-catalog-store'
import { optionSession } from '../catalog/codex-structured-option-session.test-fixture'
import { resolvedCodexMessageOptions } from './codex-structured-message-options'
import {
readCodexAppliedInputOptions,
resolvedCodexMessageOptions
} from './codex-structured-message-options'
import {
codexTurnOptionsDiffer,
waitsForCodexTurnOptions
} from '../../../native-chat/agent-session-wire/options/structured-agent-session-message-options'
import { startCodexTurn } from './codex-structured-turn-start'
import { CodexAppServerRequestError } from '../../app-server/codex-app-server-request-error'
import { CodexAppliedSettings } from '../catalog/codex-structured-applied-settings'
import { changeCodexThreadGoalWithSettings } from '../threads/codex-structured-thread-goal'
import type { CodexSession } from '../session-lifecycle/codex-structured-session-state'
import { USER_MESSAGE } from '../session-lifecycle/codex-structured-session-adapter-fixture'
import {
USER_MESSAGE,
fakeCodex,
adapterFor,
acquireReadyCodexForTest,
identityFor,
answerWithOpenedTurn
} from '../session-lifecycle/codex-structured-session-adapter-fixture'
import type { AgentJournalItemBody } from '../../../../shared/agent-session/journal/agent-session-journal-types'
function seedMessageDefaults(session: CodexSession): void {
const store = new AgentModelCatalogStore()
@@ -29,7 +46,7 @@ function seedMessageDefaults(session: CodexSession): void {
label: 'Model A',
isDefault: false,
defaultEffort: 'low',
serviceTiers: [{ value: 'priority', label: 'Priority' }],
serviceTiers: [{ value: 'priority', label: 'Fast' }],
efforts: [
{ value: 'low', label: 'Low' },
{ value: 'high', label: 'High' }
@@ -211,3 +228,237 @@ it('sends native Plan and Build on fresh turns while a steer keeps its running m
expect(request.mock.calls.at(-1)?.[0]).toBe('turn/steer')
expect(request.mock.calls.at(-1)?.[1]).not.toHaveProperty('collaborationMode')
})
it.each(['priority', 'default'])(
'keeps the same priority input eligible after an actual %s start and catalog change',
async (initialTier) => {
const request = vi.fn(async () => ({ turn: { id: 'tier-turn' } }))
const session = optionSession(request)
const store = new AgentModelCatalogStore()
const catalog = (priority: boolean) =>
store.recordSuccess(
'input-intent',
'codex',
{
origin: 'probe',
models: [
{
id: 'gpt-live',
label: 'GPT',
isDefault: true,
efforts: [{ value: 'high', label: 'High' }],
serviceTiers: priority ? [{ value: 'priority', label: 'Fast' }] : []
}
]
},
'discovery'
)
session.catalogAccess = { store, fingerprint: 'input-intent', accountHomePath: '/test-account' }
session.reportedOptions = { model: 'gpt-live', effort: 'high', serviceTier: 'default' }
catalog(initialTier === 'priority')
const choices = { model: 'gpt-live', effort: 'high', serviceTier: 'priority' }
await startCodexTurn(session, {
clientMessageId: 'priority-input',
body: USER_MESSAGE,
executionOptions: choices,
turnOptions: await resolvedCodexMessageOptions(session, choices)
})
expect(request).toHaveBeenCalledWith(
'turn/start',
expect.objectContaining({
serviceTier: initialTier
}),
expect.anything()
)
catalog(false)
const differs = (next: Record<string, string>) =>
codexTurnOptionsDiffer(
{ provider: 'codex', sessionId: 'session-1' },
next,
{ fence: 1, generation: 'generation-1', phase: 'ready' },
{ readAppliedInputOptions: (input) => readCodexAppliedInputOptions(session, input) }
)
expect(differs(choices)).toBe(false)
expect(differs({ ...choices, serviceTier: 'default' })).toBe(true)
}
)
it('holds Plan behind a successful sparse Default start, then starts Plan after completion', async () => {
const codex = fakeCodex()
const adapter = adapterFor(codex)
const bodies: AgentJournalItemBody[] = []
const acquisition = await acquireReadyCodexForTest(adapter, {
identity: identityFor('session-1'),
fence: 7,
spawnToken: 'spawn',
events: {
appendItem: (_identity, body) => bodies.push(body),
appendTombstone: () => {},
publish: () => {}
}
})
const dispatch = (id: string, options: Record<string, string>) =>
adapter.dispatch({
sessionId: 'session-1',
fence: 7,
clientMessageId: id,
body: USER_MESSAGE,
executionOptions: options
})
const complete = (turnId: string) =>
codex.connections[0].handlers.onNotification?.('turn/completed', {
threadId: 'thread-abc',
turn: { id: turnId, status: 'completed' }
})
const held = (interactionMode: 'default' | 'plan') =>
waitsForCodexTurnOptions(
{ provider: 'codex', sessionId: 'session-1' },
{
newestTurn: () => bodies.findLast((body) => body.kind === 'turn') ?? null,
itemBody: () => USER_MESSAGE
},
{
clientMessageId: 'next-plan',
fence: 7,
payloadFingerprint: 'fingerprint',
dispatchState: 'pending',
providerItemId: null,
reason: null,
submittedAt: 1,
resolvedAt: null,
options: { interactionMode }
},
{ fence: 7, generation: acquisition.acquisitionGeneration, phase: 'ready' },
adapter
)
try {
codex.routes['turn/start'] = answerWithOpenedTurn(codex, 'plan-first')
expect(await dispatch('first', { interactionMode: 'plan' })).toMatchObject({
state: 'admitted'
})
complete('plan-first')
codex.routes['turn/start'] = answerWithOpenedTurn(codex, 'default-second')
expect(await dispatch('second', {})).toMatchObject({ state: 'admitted' })
expect(
codex.connections[0].calls.findLast((call) => call.method === 'turn/start')?.params
).toMatchObject({ collaborationMode: { mode: 'default' } })
expect(held('plan')).toBe(true)
expect(held('default')).toBe(false)
complete('default-second')
expect(held('plan')).toBe(false)
codex.routes['turn/start'] = answerWithOpenedTurn(codex, 'plan-third')
expect(await dispatch('third', { interactionMode: 'plan' })).toMatchObject({
state: 'admitted'
})
expect(
codex.connections[0].calls.findLast((call) => call.method === 'turn/start')?.params
).toMatchObject({ collaborationMode: { mode: 'plan' } })
} finally {
await adapter.closeAll()
}
})
it('uses exact successful start controls when the first message omits model and effort', async () => {
const session = optionSession(vi.fn(async () => ({ turn: { id: 'first-turn' } })))
await startCodexTurn(session, {
clientMessageId: 'first',
body: USER_MESSAGE,
executionOptions: {},
turnOptions: {}
})
const read = (next: Record<string, string>) =>
readCodexAppliedInputOptions(session, {
sessionId: 'session-1',
fence: 1,
acquisitionGeneration: 'generation-1',
next
})
expect(read({ effort: 'high' })).toMatchObject({
active: { model: 'gpt-live', effort: 'high' },
next: { model: 'gpt-live', effort: 'high' }
})
expect(read({ effort: 'low' })).toMatchObject({
active: { effort: 'high' },
next: { effort: 'low' }
})
})
it('inherits retained sparse intent ahead of canonical execution controls', async () => {
const session = optionSession(vi.fn(async () => ({ turn: { id: 'sparse-turn' } })))
session.appliedTurnOptions = { model: 'gpt-canonical', effort: 'high' }
session.appliedTurnInputOptions = { model: 'gpt-alias', effort: 'low' }
const choices = { model: 'gpt-alias' }
await startCodexTurn(session, {
clientMessageId: 'sparse',
body: USER_MESSAGE,
executionOptions: choices,
turnOptions: await resolvedCodexMessageOptions(session, choices)
})
expect(
readCodexAppliedInputOptions(session, {
sessionId: 'session-1',
fence: 1,
acquisitionGeneration: 'generation-1',
next: { model: 'gpt-alias', effort: 'low' }
})
).toMatchObject({
active: { model: 'gpt-alias', effort: 'low' },
next: { model: 'gpt-alias', effort: 'low' }
})
})
it('retains legacy fast intent as the successful native tier after a standard turn', async () => {
const session = optionSession(vi.fn(async () => ({ turn: { id: 'fast-turn' } })))
seedMessageDefaults(session)
session.appliedTurnInputOptions = { model: 'model-a', serviceTier: 'default' }
const choices = { model: 'model-a', fastMode: 'true' as const }
await startCodexTurn(session, {
clientMessageId: 'legacy-fast',
body: USER_MESSAGE,
executionOptions: choices,
turnOptions: await resolvedCodexMessageOptions(session, choices)
})
expect(Object.fromEntries(session.options)).toEqual({ model: 'model-a', serviceTier: 'priority' })
expect(
readCodexAppliedInputOptions(session, {
sessionId: 'session-1',
fence: 1,
acquisitionGeneration: 'generation-1',
next: { serviceTier: 'priority' }
})
).toMatchObject({ active: { serviceTier: 'priority' }, next: { serviceTier: 'priority' } })
})
it('invalidates uncertain goal choices independently from shaped execution evidence', async () => {
const session = optionSession(
vi.fn(async () => {
throw new Error('lost after write')
})
)
session.appliedSettings = new CodexAppliedSettings()
session.appliedSettings.observe({ model: 'gpt-canonical', effort: 'high' })
session.appliedTurnOptions = { model: 'gpt-canonical', effort: 'high' }
session.appliedTurnInputOptions = { model: 'gpt-alias', effort: 'low', serviceTier: 'default' }
expect(
await changeCodexThreadGoalWithSettings(
session,
{
sessionId: 'session-1',
fence: 1,
change: { kind: 'set', objective: 'Ship it' },
replacesGoal: false,
executionOptions: { model: 'gpt-canonical', effort: 'low' }
},
1000,
() => true
)
).toMatchObject({ ok: false })
expect(
readCodexAppliedInputOptions(session, {
sessionId: 'session-1',
fence: 1,
acquisitionGeneration: 'generation-1',
next: {}
})?.active
).toEqual({ effort: 'low', serviceTier: 'default' })
})
@@ -37,8 +37,9 @@ export async function resolvedCodexMessageOptions(
return compileCodexMessageOptions(session, choices)
}
function compileCodexMessageOptions(
session: CodexSession,
export function compileCodexMessageOptions(
session: Pick<CodexSession, 'options' | 'catalogAccess'> &
Partial<Pick<CodexSession, 'reportedOptions' | 'appliedTurnOptions'>>,
choices: Readonly<Record<string, string | undefined>>
): Readonly<Record<string, string>> {
const compiled: Record<string, string> = {}
@@ -57,7 +58,29 @@ function compileCodexMessageOptions(
return codexTurnRequestOptions(session, compiled)
}
/** The same sparse controls as dispatch, compared under the exact acquisition that took them. */
/** Compare retained input choices, without reinterpreting them through a later catalog. */
export function codexMessageInputOptions(
choices: Readonly<Record<string, string | undefined>>,
inherited: Readonly<Record<string, string>> = {},
applied: Readonly<Record<string, string>> = {}
): Readonly<Record<string, string>> {
const next = { ...inherited }
for (const key of ['model', 'effort', 'personality', 'serviceTier', 'interactionMode'] as const) {
if (choices[key] !== undefined) {
next[key] = choices[key]
}
}
if (choices.serviceTier === undefined && choices.fastMode !== undefined) {
const legacy = codexServiceTierOfFastMode(choices.fastMode, undefined)
const inheritedTier = inherited.serviceTier
const tier =
inheritedTier === 'priority' || inheritedTier === 'fast' ? inheritedTier : applied.serviceTier
next.serviceTier = legacy === 'fast' && (tier === 'priority' || tier === 'fast') ? tier : legacy
}
return next
}
/** Intent belongs to this exact acquisition; catalog/readback differences are not send barriers. */
export function readCodexAppliedInputOptions(
session: CodexSession | undefined,
input: Parameters<NonNullable<StructuredAgentSessionAdapter['readAppliedInputOptions']>>[0]
@@ -68,13 +91,13 @@ export function readCodexAppliedInputOptions(
session.connection.closed ||
session.fence !== input.fence ||
session.acquisitionGeneration !== input.acquisitionGeneration ||
!session.appliedTurnOptions
!session.appliedTurnInputOptions
) {
return null
}
const active = session.appliedTurnOptions
const active = session.appliedTurnInputOptions
const requested = input.next ?? Object.fromEntries(session.options)
return { active, next: { ...active, ...compileCodexMessageOptions(session, requested) } }
return { active, next: codexMessageInputOptions(requested, active, session.appliedTurnOptions) }
}
/** Check the live binding after options settle and again whenever the provider is about to write. */
@@ -97,6 +97,36 @@ export function noteCodexTurnOpened(
}
}
export function publishCodexTurnOpened(
session: CodexSession,
frame: { sessionId: string; method: string; params: unknown },
sessions: ReadonlyMap<string, CodexSession>
): void {
const { method, params } = frame
const turnId = method === 'turn/started' ? readCodexTurnId(params) : null
if (!turnId || (readCodexThreadId(params) ?? session.threadId) !== session.threadId) {
return
}
const publish = (): void => {
if (!session.ended && sessions.get(frame.sessionId) === session) {
session.dispatchEchoes.published(session.threadId, turnId)
}
}
const written = session.events?.written?.()
if (written) {
void written.then(
(result) => {
if (result.ok) {
publish()
}
},
() => {}
)
} else {
publish()
}
}
export function settleCodexSendsInEndedTurn(
session: Pick<CodexSession, 'threadId' | 'dispatchEchoes' | 'account'>,
frame: { sessionId: string; method: string; params: unknown }
@@ -4,7 +4,11 @@ import {
StructuredAgentSessionInputNotAdmitted,
type StructuredAgentSessionAdmitFreshInput
} from '../../../native-chat/agent-session-wire/delivery/structured-agent-session-input-admission'
import { turnInputFor } from './codex-structured-message-options'
import {
codexMessageInputOptions,
compileCodexMessageOptions,
turnInputFor
} from './codex-structured-message-options'
import { reportedCodexPermissionMode } from '../threads/codex-structured-thread-open'
import { admitCodexTurnPermission } from './codex-structured-turn-permission'
import type { CodexSkillReport } from '../catalog/codex-structured-skill-report'
@@ -23,7 +27,7 @@ import type { CodexAppServerConnection } from '../../app-server/codex-app-server
import { isCodexAppServerUnsupportedError } from '../../app-server/codex-app-server-session'
import type { CodexDispatchEchoes } from './codex-structured-dispatch-echo'
import { readCodexTurnId } from '../../app-server/codex-structured-thread-facts'
import { codexTurnRequestOptions } from '../catalog/codex-structured-service-tier'
import type { AgentSessionExecutionOptions } from '../../../../shared/agent-session/options/agent-session-execution-options'
import type { CodexSession } from '../session-lifecycle/codex-structured-session-state'
import {
codexRunningOrOpeningTurn,
@@ -40,6 +44,7 @@ export type CodexTurnHost = {
permissions?: CodexSession['permissions']
options: Map<string, string>
appliedTurnOptions?: Readonly<Record<string, string>>
appliedTurnInputOptions?: Readonly<Record<string, string>>
appliedSettings?: CodexSession['appliedSettings']
reportedOptions?: CodexSession['reportedOptions']
catalogAccess?: CodexSession['catalogAccess']
@@ -50,18 +55,22 @@ export type CodexTurnHost = {
turnOpenWaits: Pick<CodexTurnOpenWaits, 'wait'>
}
type CodexTurnInput = {
clientMessageId: string
body: AgentJournalMessageItem
requestedAt?: number
turnOptions?: Readonly<Record<string, string>>
executionOptions?: AgentSessionExecutionOptions
signal?: AbortSignal
isCurrent?: () => boolean
admitFreshInput?: StructuredAgentSessionAdmitFreshInput
}
/** false means correlation capacity refused the send before writing; null turnId means an unnamed answer. */
export async function startCodexTurn(
host: CodexTurnHost,
input: {
clientMessageId: string
body: AgentJournalMessageItem
requestedAt?: number
input: CodexTurnInput & {
timeoutMs?: number
turnOptions?: Readonly<Record<string, string>>
signal?: AbortSignal
isCurrent?: () => boolean
admitFreshInput?: StructuredAgentSessionAdmitFreshInput
onReply?: () => void
}
): Promise<{ turnId: string | null; via: AgentJournalTurnJoin } | false> {
@@ -90,8 +99,11 @@ export async function startCodexTurn(
return steered
}
let previousApplied = host.appliedTurnOptions
let previousInput = host.appliedTurnInputOptions
let previousOptions: Map<string, string> | null | undefined
let requested: Readonly<Record<string, string>> = {}
let inputChoices: Readonly<Record<string, string | undefined>> = {}
let requestControls: Record<string, string> = {}
let enqueued = false
let discardSettingsAttempt: (() => void) | undefined
const enqueue = (
@@ -110,14 +122,21 @@ export async function startCodexTurn(
throw new StructuredAgentSessionInputNotAdmitted(undefined, 'permission')
}
previousApplied = host.appliedTurnOptions
previousInput = host.appliedTurnInputOptions
inputChoices = input.executionOptions ?? input.turnOptions ?? Object.fromEntries(host.options)
previousOptions = input.turnOptions ? new Map(host.options) : null
if (input.turnOptions) {
host.options.clear()
for (const [key, value] of Object.entries(input.turnOptions)) {
for (const [key, value] of Object.entries(
codexMessageInputOptions(inputChoices, {}, input.turnOptions)
)) {
host.options.set(key, value)
}
}
requested = { interactionMode: 'default', ...codexTurnRequestOptions(host) }
requested = {
interactionMode: 'default',
...compileCodexMessageOptions(host, input.turnOptions ?? Object.fromEntries(host.options))
}
const { interactionMode, ...nativeOptions } = requested
const collaborationMode = {
mode: interactionMode,
@@ -128,11 +147,21 @@ export async function startCodexTurn(
developer_instructions: null
}
}
requestControls = {
...requested,
...(collaborationMode.settings.model ? { model: collaborationMode.settings.model } : {}),
...(collaborationMode.settings.reasoning_effort
? { effort: collaborationMode.settings.reasoning_effort }
: {})
}
const known = { ...previousApplied }
for (const key of Object.keys(requested)) {
const knownInput = { ...previousInput }
for (const key of new Set([...Object.keys(requested), ...Object.keys(inputChoices)])) {
delete known[key]
delete knownInput[key]
}
host.appliedTurnOptions = known
host.appliedTurnInputOptions = knownInput
discardSettingsAttempt = host.appliedSettings?.attempt(requested)
enqueued = true
host.dispatchEchoes.expect(
@@ -202,9 +231,8 @@ export async function startCodexTurn(
}
if (isCodexAppServerRequestError(error) || isCodexAppServerUnsupportedError(error)) {
discardSettingsAttempt?.()
}
if (isCodexAppServerRequestError(error) || isCodexAppServerUnsupportedError(error)) {
host.appliedTurnOptions = previousApplied
host.appliedTurnInputOptions = previousInput
if (previousOptions) {
host.options.clear()
for (const [key, value] of previousOptions) {
@@ -215,22 +243,23 @@ export async function startCodexTurn(
throw error
}
host.appliedTurnOptions = { ...previousApplied, ...requested }
host.appliedTurnInputOptions = codexMessageInputOptions(
inputChoices,
{
serviceTier: 'default',
...requestControls,
...previousInput,
interactionMode: requestControls.interactionMode
},
requested
)
return { turnId: readCodexTurnId(answer), via: 'start' }
}
/** Replies establish admission; exact echoes establish identity. Timeouts cannot disprove delivery. */
export async function dispatchCodexTurn(
session: CodexTurnHost,
input: {
sessionId: string
clientMessageId: string
body: AgentJournalMessageItem
requestedAt?: number
turnOptions?: Readonly<Record<string, string>>
signal?: AbortSignal
isCurrent?: () => boolean
admitFreshInput?: StructuredAgentSessionAdmitFreshInput
},
input: CodexTurnInput & { sessionId: string },
timeoutMs: number | undefined
): Promise<AgentSessionDispatchOutcome> {
let answer: { turnId: string | null; via: AgentJournalTurnJoin } | false
@@ -270,12 +299,6 @@ export async function dispatchCodexTurn(
return { state: 'rejected', ...codexDispatchRejection(agentSessionFailureFact('queueFull')) }
}
// An answer read after the turn it names already ended is settled by that end.
if (!answer.turnId) {
session.dispatchEchoes.answered(
input.clientMessageId,
answeredAt ?? session.dispatchEchoes.tick()
)
}
if (answer.turnId) {
session.dispatchEchoes.bindTurn(
input.clientMessageId,
@@ -284,6 +307,11 @@ export async function dispatchCodexTurn(
answer.via,
answeredAt
)
} else {
session.dispatchEchoes.answered(
input.clientMessageId,
answeredAt ?? session.dispatchEchoes.tick()
)
}
return { state: 'admitted', ranOptions: { ...session.appliedTurnOptions } }
}
@@ -22,7 +22,14 @@ import {
type MutationPlan
} from '../operations/structured-agent-session-mutation-plans'
import type { CommandReceiptResult } from '../../agent-session-journal/writing/command-receipt-schema'
import { structuredQueueHold } from './structured-agent-session-queued-gate'
import {
canSteerStructuredQueuedMessage,
structuredQueueHold
} from './structured-agent-session-queued-gate'
import {
structuredInputExecutionWorking,
structuredInputHandoverWorking
} from './structured-agent-session-input-selection'
import { refuse } from '../../../../shared/agent-session/failures/agent-session-wire-refusals'
import { queuedMessageOwed } from './structured-agent-session-queued-mail'
import { resumeStructuredQueue } from './structured-agent-session-queued-pause'
@@ -124,6 +131,30 @@ export function sendQueuedStructuredAgentMessage(
if (row.body.command && work.working()) {
return invalid("A command can't be sent while the agent is working.")
}
if (
structuredInputExecutionWorking(ctx.journal, work) &&
(work.activeTurnId() === null ||
structuredInputHandoverWorking(ctx.journal, work) ||
!canSteerStructuredQueuedMessage(
{
record,
work,
child: context.sessions.get(ctx.sessionId)?.child,
stopping: context.readStopping(ctx.sessionId),
adapter: context.deps.adapter
},
row
))
) {
return {
ok: false,
refusal: refuse(
'agent_session_operation_invalid',
{ reason: 'turnActive' },
'The current turn cannot accept this queued message yet.'
)
}
}
const submissionId = operationId
const owed = queuedMessageOwed(context.deps, ctx.sessionId, row.body)
try {
@@ -1,4 +1,11 @@
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
import type { CodexAppServerConnection } from '../../../../codex/app-server/codex-app-server-connection-types'
import { optionSession } from '../../../../codex/structured/catalog/codex-structured-option-session.test-fixture'
import {
readCodexAppliedInputOptions,
resolvedCodexMessageOptions
} from '../../../../codex/structured/turns/codex-structured-message-options'
import { dispatchCodexTurn } from '../../../../codex/structured/turns/codex-structured-turn-start'
import {
createQueuedMessageTestRig,
QUEUED_RIG_CALLER
@@ -17,7 +24,24 @@ import { agentJournalSubmissionKey } from '../../../../../shared/agent-session/j
let rig: QueuedMessageTestRig
beforeEach(async () => {
const request = vi.fn<CodexAppServerConnection['request']>(async () => ({}))
const native = optionSession(request)
native.threadId = THREAD
rig = await createQueuedMessageTestRig({ restartable: true })
rig.host.deps.adapter.readAppliedInputOptions = (input) =>
input.sessionId === SESSION ? readCodexAppliedInputOptions(native, input) : null
rig.host.deps.adapter.holdsLiveProviderProcess = (sessionId, generation) =>
sessionId === SESSION &&
generation === native.acquisitionGeneration &&
!native.ended &&
!native.connection.closed
rig.dispatch.mockImplementation(async (input) =>
dispatchCodexTurn(
native,
{ ...input, turnOptions: await resolvedCodexMessageOptions(native, input.executionOptions) },
undefined
)
)
})
afterEach(() => rig.dispose())
@@ -108,3 +108,41 @@ it('holds a Plan choice until the goal turn ends', async () => {
await eventually(() => expect(rig.dispatch).toHaveBeenCalledTimes(2))
expect(rig.dispatch.mock.calls[1]?.[0].executionOptions).toEqual(choices)
})
it('keeps an incompatible Send-now card intact until a new turn can use its choices', async () => {
await startGoalTurn()
const choices = { model: 'gpt-ran', effort: 'high', serviceTier: 'priority' }
const queued = rig.send('Keep my selected speed', 'queue-if-active', { carried: choices })
expect(await queued.result).toMatchObject({ ok: true, value: { queued: {} } })
const operationId = hostTestOperationId()
for (let press = 0; press < 2; press += 1) {
expect(await rig.sendNow(queued.id, operationId)).toMatchObject({
ok: false,
refusal: { details: { reason: 'turnActive' } }
})
}
expect(await rig.handoff(queued.id)).toBeUndefined()
expect(
rig.host.collaboratorsForTests().sessions.get(SESSION)?.journal.queuedMessages.get(queued.id)
).toMatchObject({
state: 'waiting',
body: { blocks: [{ type: 'text', text: 'Keep my selected speed' }] },
options: choices
})
native.activeTurnIds = new Set()
rig
.providerEvents()
.appendItem(
{ provider: 'codex', threadId: THREAD, turnId: 'goal-turn', ordinal: 99 },
{ kind: 'turn', turnId: 'goal-turn', state: 'completed' },
{ turnScope: AGENT_JOURNAL_THREAD_SCOPE }
)
await rig.host.flushStreamedEvents(SESSION)
await eventually(async () => expect((await rig.handoff(queued.id))?.handedOverAt).toBeDefined())
expect(rig.dispatch.mock.calls[1]?.[0].executionOptions).toEqual(choices)
expect(await rig.sendNow(queued.id, operationId)).toMatchObject({
ok: true,
value: { submission: { queuedMessageId: queued.id } }
})
})
@@ -32,7 +32,9 @@ async function queuedDraft(text: string): Promise<string> {
describe('a repeated draft press under a fresh operation id', () => {
it('Send-now pressed twice sends the draft once and answers both presses with its submission', async () => {
await openRigTurnFor(rig, await rig.workingSend())
const opening = await rig.workingSend()
await rig.settleAccepted(opening, 'opening')
await openRigTurnFor(rig, opening)
const draftId = await queuedDraft('send me now')
const [first, second] = await Promise.all([rig.sendNow(draftId), rig.sendNow(draftId)])
expect(first).toMatchObject({ ok: true, replayed: false })
@@ -135,10 +135,11 @@ describe("Stop's event", () => {
it('a stopped unconfirmed hand-off stays ordinary while Resume drains waiting cards', async () => {
const working = await rig.workingSend()
await openRigTurnFor(rig, working)
await rig.settleAccepted(working, 'stopped')
const steered = await queuedDraft('sent now into the turn')
const opening = await queuedDraft('opens its own turn')
const last = await queuedDraft('waiting behind them')
await rig.sendNow(steered)
expect(await rig.sendNow(steered)).toMatchObject({ ok: true })
await eventually(async () => expect((await rig.handoff(steered))?.handedOverAt).toBeDefined())
const steer = await rig.handoffId(steered)
// Codex ends the stopped turn without taking the steer: the agent may have it, nothing proves
@@ -153,7 +154,6 @@ describe("Stop's event", () => {
},
via: 'steer'
})
await rig.settleAccepted(working, 'stopped')
await openRigTurnFor(rig, working, 'interrupted')
await expectHeld('stopped', opening, last)
expect(await rig.drafts()).toEqual([
@@ -25,7 +25,7 @@ import type { AgentSessionLatestTurn } from '../../../../shared/agent-session/ag
import type { StructuredAgentSessionWorkScope } from '../../../../shared/agent-session/view/structured-agent-session-main-agent-working'
import { isUnansweredStructuredAgentSessionDispatch } from '../../../../shared/agent-session/requests/structured-agent-session-unanswered-dispatch'
import type { AgentSessionJournal } from '../../agent-session-journal/journal-store'
import { DISPATCH_DOUBT_PROVIDER_ENDED_UNANSWERED } from '../../agent-session-journal/projection/journal-dispatch-doubt-reasons'
import { admittedStructuredSubmission } from './structured-agent-session-submission-presentation'
import {
continuingStructuredAgentSessionInput,
finalizingStructuredAgentSessionInput,
@@ -155,12 +155,7 @@ export class StructuredAgentSessionCurrentWork {
* answer, reads in doubt: no client counts it as work, and its echo or refusal still settles it. */
publishedSubmission = (submission: AgentJournalSubmission): AgentJournalSubmission =>
this.unanswered(submission) && !this.holds(submission)
? {
...submission,
dispatchState: 'unknown',
recovered: true,
reason: submission.reason ?? DISPATCH_DOUBT_PROVIDER_ENDED_UNANSWERED
}
? admittedStructuredSubmission(submission, this.generationEnded(submission))
: submission
private unanswered(submission: AgentJournalSubmission): boolean {
@@ -0,0 +1,22 @@
import type { AgentJournalSubmission } from '../../../../shared/agent-session/journal/agent-session-journal-types'
import {
DISPATCH_DOUBT_PROVIDER_ENDED_UNANSWERED,
DISPATCH_DOUBT_WRITE_OUTCOME_UNKNOWN
} from '../../agent-session-journal/projection/journal-dispatch-doubt-reasons'
/** Admission releases presentation only; the journal still awaits an exact receipt. */
export function admittedStructuredSubmission(
submission: AgentJournalSubmission,
generationEnded: boolean
): AgentJournalSubmission {
return {
...submission,
dispatchState: 'unknown',
recovered: true,
reason:
submission.reason ??
(generationEnded
? DISPATCH_DOUBT_PROVIDER_ENDED_UNANSWERED
: DISPATCH_DOUBT_WRITE_OUTCOME_UNKNOWN)
}
}
@@ -46,9 +46,12 @@ export function codexTurnOptionsDiffer(
next: options
})
if (!applied) {
return options === undefined || Object.values(options).some((value) => value !== undefined)
return false
}
return OPTION_ORDER.some(
(key) => applied.next[key] !== undefined && applied.next[key] !== applied.active[key]
(key) =>
applied.active[key] !== undefined &&
applied.next[key] !== undefined &&
applied.next[key] !== applied.active[key]
)
}
@@ -2,6 +2,8 @@
// its mailbox, and that mailbox still this chat's. The queue asks at handover; cleanup is visible only.
import { describe, expect, it, vi } from 'vitest'
import { computeAgentSessionPayloadFingerprint } from '../../../../../shared/agent-session/lifecycle/agent-session-mutation-envelope'
import type { AgentSessionJournal } from '../../../../native-chat/agent-session-journal/journal-store'
import { structuredAgentSessionMessageSendMutation } from '../../../../../shared/agent-session/requests/structured-agent-session-send-mutation'
import { formatOrcaSessionAddress } from '../../../../../shared/orca-session-address'
import { testOrcaSessionId } from '../../../../../shared/orca-session-address-test-fixture'
@@ -26,9 +28,19 @@ import {
turnText
} from './structured-chat-coordinator-mail-rig.test-fixture'
/** A chat with a turn running, so mail waits as cards behind it. */
async function busyChat(sessionId: string) {
/** A running turn, optionally with its first handover still unanswered. */
async function busyChat(sessionId: string, startReply?: Promise<void>) {
const chat = await openChat(sessionId)
if (startReply) {
const request = chat.request
chat.request = async (...args) => {
const response = await request(...args)
if (args[0] === 'turn/start') {
await startReply
}
return response
}
}
expect(await sendUserMessage(sessionId, 'go')).toMatchObject({ ok: true })
await vi.waitFor(() => expect(chat.turns).toHaveLength(1), WAIT)
chat.handlers.onNotification?.('turn/started', { turn: { id: 'turn-1' } })
@@ -36,6 +48,10 @@ async function busyChat(sessionId: string) {
return chat
}
function journalOf(sessionId: string): AgentSessionJournal {
return host.collaboratorsForTests().sessions.get(sessionId)!.journal
}
/** Queue a card behind the running turn: the person's own, or another agent's with `from`. */
async function queueCard(text: string, from?: AgentMessageSource): Promise<string> {
const mutation = structuredAgentSessionMessageSendMutation({
@@ -129,30 +145,37 @@ describe('a queued mail card runs only while its chat still owes it', () => {
})
it("lets the person's cards pass mail it cannot decide on now, then hands the mail over first once it can", async () => {
const chat = await busyChat(COORDINATOR)
const { taskId } = await coordinatorRunAndTask()
await finishWorker(taskId)
await vi.waitFor(async () => expect(await queuedCardTexts()).toHaveLength(1), WAIT)
await queueCard('first question of mine')
await queueCard('second question of mine')
const unreadable = vi.spyOn(db, 'areUnreadMessages').mockImplementation(() => {
throw new Error('database unavailable')
})
await settleTurn(COORDINATOR, 0)
await vi.waitFor(() => expect(chat.turns).toHaveLength(2), WAIT)
// The person's first card went, in their order; the report and their second card still wait.
expect(turnText(chat.turns[1]!)).toContain('first question of mine')
const waiting = await queuedCardTexts()
expect(waiting).toHaveLength(2)
expect(waiting[0]).toContain('(worker_done)')
unreadable.mockRestore()
await settleTurn(COORDINATOR, 1)
// Readable again: the older report goes before the person's later card.
await vi.waitFor(() => expect(chat.turns).toHaveLength(3), WAIT)
expect(isWholeMail(turnText(chat.turns[2]!))).toBe(true)
await settleTurn(COORDINATOR, 2)
await vi.waitFor(() => expect(chat.turns).toHaveLength(4), WAIT)
expect(turnText(chat.turns[3]!)).toContain('second question of mine')
const reply = Promise.withResolvers<void>()
try {
const chat = await busyChat(COORDINATOR, reply.promise)
const { taskId } = await coordinatorRunAndTask()
await finishWorker(taskId)
await vi.waitFor(async () => expect(await queuedCardTexts()).toHaveLength(1), WAIT)
await queueCard('first question of mine')
await queueCard('second question of mine')
const unreadable = vi.spyOn(db, 'areUnreadMessages').mockImplementation(() => {
throw new Error('database unavailable')
})
expect(chat.steers).toEqual([])
reply.resolve()
await settleTurn(COORDINATOR, 0)
await vi.waitFor(() => expect(chat.turns).toHaveLength(2), WAIT)
// The person's first card went, in their order; the report and their second card still wait.
expect(turnText(chat.turns[1]!)).toContain('first question of mine')
const waiting = await queuedCardTexts()
expect(waiting).toHaveLength(2)
expect(waiting[0]).toContain('(worker_done)')
unreadable.mockRestore()
await settleTurn(COORDINATOR, 1)
// Readable again: the older report goes before the person's later card.
await vi.waitFor(() => expect(chat.turns).toHaveLength(3), WAIT)
expect(isWholeMail(turnText(chat.turns[2]!))).toBe(true)
await settleTurn(COORDINATOR, 2)
await vi.waitFor(() => expect(chat.turns).toHaveLength(4), WAIT)
expect(turnText(chat.turns[3]!)).toContain('second question of mine')
} finally {
reply.resolve()
}
})
it('hands a Task over without opening the mail database', async () => {
@@ -197,4 +220,71 @@ describe('a queued mail card runs only while its chat still owes it', () => {
expect(db.getUnreadMessages(`run:${runId}`)).toMatchObject([{ read: 0, delivered_at: null }])
await vi.waitFor(() => expect(selectable).toContain(1), WAIT)
})
it('sends a report refused for queue room once the queue frees room', async () => {
const reply = Promise.withResolvers<void>()
try {
const chat = await busyChat(COORDINATOR, reply.promise)
const { runId, taskId } = await coordinatorRunAndTask()
// The person's queue holds almost all the room an agent's message may use.
for (let card = 0; card < 4; card += 1) {
const mutation = structuredAgentSessionMessageSendMutation({
sessionId: COORDINATOR,
clientOperationId: operationId(),
expectedRuntimeFence: host.deps.store.getConversation(COORDINATOR)!.currentFence,
body: {
kind: 'message',
role: 'user',
blocks: [{ type: 'text', text: 'x'.repeat(240_000) }]
},
delivery: 'queue-if-active'
})
expect(
await host.send({ callerKey: 'test-surface' }, { ...mutation, userSend: true })
).toMatchObject({ ok: true, value: { queued: { state: 'waiting' } } })
}
const send = vi.spyOn(host, 'send')
await finishWorker(taskId)
await vi.waitFor(() => expect(send).toHaveBeenCalledTimes(1), WAIT)
// The refusal releases the reservation made before the send.
await vi.waitFor(
() => expect(db.getUnreadMessages(`run:${runId}`)).toMatchObject([{ delivered_at: null }]),
WAIT
)
// Deleting the person's cards frees room: that journal edge alone sends the report.
for (const card of journalOf(COORDINATOR).queuedMessages.list()) {
const fields = { messageId: card.messageId }
expect(
await host.queuedMessageDelete(
{ callerKey: 'test-surface' },
{
envelope: {
sessionId: COORDINATOR,
clientOperationId: operationId(),
expectedRuntimeFence: host.deps.store.getConversation(COORDINATOR)!.currentFence,
payloadFingerprint: computeAgentSessionPayloadFingerprint({
method: 'agentSession.queuedMessageDelete',
sessionId: COORDINATOR,
fields
})
},
...fields
}
)
).toMatchObject({ ok: true })
}
await vi.waitFor(async () => {
const cards = await queuedCardTexts()
expect(cards).toHaveLength(1)
expect(cards[0]).toContain('(worker_done)')
}, WAIT)
expect(chat.steers).toEqual([])
reply.resolve()
await settleTurn(COORDINATOR, 0)
await vi.waitFor(() => expect(chat.turns).toHaveLength(2), WAIT)
expect(isWholeMail(turnText(chat.turns[1]!))).toBe(true)
} finally {
reply.resolve()
}
})
})
@@ -306,44 +306,59 @@ describe('a send to a finished structured worker', () => {
const { taskId } = await coordinatorRunAndTask()
const { identity, firstDispatchId } = await finishedStructuredWorker(taskId, 'none')
const worker = connectionFor(identity.sessionId)
expect(await sendUserMessage(identity.sessionId, 'go')).toMatchObject({ ok: true })
await vi.waitFor(() => expect(worker.turns).toHaveLength(1), WAIT)
worker.handlers.onNotification?.('turn/started', { turn: { id: 'turn-1' } })
await host.flushStreamedEvents(identity.sessionId)
await sendTo(firstDispatchId, 'and the docs')
// The mail waits as a card behind the running turn; the worker reports before it is handed over.
await vi.waitFor(
async () => expect(await queuedCardTexts(identity.sessionId)).toHaveLength(1),
WAIT
)
const reply = Promise.withResolvers<void>()
const request = worker.request
worker.request = async (...args) => {
const response = await request(...args)
if (args[0] === 'turn/start') {
await reply.promise
}
return response
}
try {
expect(await sendUserMessage(identity.sessionId, 'go')).toMatchObject({ ok: true })
await vi.waitFor(() => expect(worker.turns).toHaveLength(1), WAIT)
worker.handlers.onNotification?.('turn/started', { turn: { id: 'turn-1' } })
await host.flushStreamedEvents(identity.sessionId)
await sendTo(firstDispatchId, 'and the docs')
// The mail waits as a card behind the running turn; the worker reports before it is handed over.
await vi.waitFor(
async () => expect(await queuedCardTexts(identity.sessionId)).toHaveLength(1),
WAIT
)
expect(worker.steers).toEqual([])
expect(await reportDone(identity.sessionId, taskId, firstDispatchId)).toMatchObject({
ok: true
})
// The follow-up took the mail: the old card is gone, its Task waits first, then the mail itself.
await vi.waitFor(async () => {
const cards = await queuedCardTexts(identity.sessionId)
expect(cards).toHaveLength(2)
expect(cards[0]).toContain('arrive next, one at a time')
expect(isWholeMail(cards[1]!)).toBe(true)
expect(cards[1]).toContain('and the docs')
}, WAIT)
expect((await queuedCardTexts(identity.sessionId)).join('\n')).not.toContain('`check`')
worker.handlers.onNotification?.('turn/completed', { turn: { id: 'turn-1' } })
await host.flushStreamedEvents(identity.sessionId)
await vi.waitFor(() => expect(worker.turns).toHaveLength(2), WAIT)
expect(turnText(worker.turns[1]!)).toContain('arrive next, one at a time')
await settleTurn(identity.sessionId, 1)
await vi.waitFor(() => expect(worker.turns).toHaveLength(3), WAIT)
expect(isWholeMail(turnText(worker.turns[2]!))).toBe(true)
expect(turnText(worker.turns[2]!)).toContain('and the docs')
await settleTurn(identity.sessionId, 2)
expect(await reportDone(identity.sessionId, taskId, firstDispatchId)).toMatchObject({
ok: true
})
// The follow-up took the mail: the old card is gone, its Task waits first, then the mail itself.
await vi.waitFor(async () => {
const cards = await queuedCardTexts(identity.sessionId)
expect(cards).toHaveLength(2)
expect(cards[0]).toContain('arrive next, one at a time')
expect(isWholeMail(cards[1]!)).toBe(true)
expect(cards[1]).toContain('and the docs')
}, WAIT)
expect((await queuedCardTexts(identity.sessionId)).join('\n')).not.toContain('`check`')
reply.resolve()
worker.handlers.onNotification?.('turn/completed', { turn: { id: 'turn-1' } })
await host.flushStreamedEvents(identity.sessionId)
await vi.waitFor(() => expect(worker.turns).toHaveLength(2), WAIT)
expect(turnText(worker.turns[1]!)).toContain('arrive next, one at a time')
await settleTurn(identity.sessionId, 1)
await vi.waitFor(() => expect(worker.turns).toHaveLength(3), WAIT)
expect(isWholeMail(turnText(worker.turns[2]!))).toBe(true)
expect(turnText(worker.turns[2]!)).toContain('and the docs')
await settleTurn(identity.sessionId, 2)
const followUp = getFollowUpDispatch(db, firstDispatchId)!
await vi.waitFor(
() => expect(db.getUnreadMessages(`dispatch:${followUp.id}`)).toEqual([]),
WAIT
)
expect(db.getUnreadMessages(`dispatch:${firstDispatchId}`)).toEqual([])
const followUp = getFollowUpDispatch(db, firstDispatchId)!
await vi.waitFor(
() => expect(db.getUnreadMessages(`dispatch:${followUp.id}`)).toEqual([]),
WAIT
)
expect(db.getUnreadMessages(`dispatch:${firstDispatchId}`)).toEqual([])
} finally {
reply.resolve()
}
})
})
@@ -43,7 +43,10 @@ export function agentSessionWriteKindForMethod(
fingerprintMethod: string,
fields: Record<string, unknown>
): AgentSessionWriteKind {
if (fingerprintMethod === 'agentSession.send') {
if (
fingerprintMethod === 'agentSession.send' ||
fingerprintMethod === 'agentSession.queuedMessageSend'
) {
return 'send'
}
if (fingerprintMethod === 'agentSession.cancel') {