Files
orca/src/main/codex/structured/turns/codex-structured-message-options.ts
T
Brennan Benson a3eabcd730 Merge main into native chat SQLite storage
Carry retained Codex input intent, admission safety, account attribution and
idle confirmation through the SQL journal owners. Preserve main relocations
and deletions and the SQL queue, history and custody assertions.

Suppress publication of unchanged SQL command acknowledgments so empty queue
claims cannot renew a chat's idle clock. This SQL mismatch predates this sync;
main's new idle-retirement coverage exposed it. Mail custody fixtures now hold
native admission and use real edit leases without altering automatic delivery.

Validation: direct Node through tc-gate; merge hook, basic lint, tc:node,
e2e typecheck and pinned-main changed quality pass. All 148 owning tests in
25 files pass on both Node and configured Bun. O1 diagnostics remain excluded.
2026-11-12 18:41:39 -08:00

144 lines
5.5 KiB
TypeScript

import { pickedSkillsInText } from '../../../../shared/agent-session/requests/structured-agent-session-send-mutation'
import type { AgentJournalMessageItem } from '../../../../shared/agent-session/journal/agent-session-journal-types'
import type { NativeChatBlock } from '../../../../shared/native-chat/native-chat-types'
import type { CodexTurnHost } from './codex-structured-turn-contract'
import { waitForPromiseWithSignal } from '../../../../shared/abort-signal-reason'
import { StructuredAgentSessionInputNotAdmitted } from '../../../native-chat/agent-session-wire/delivery/structured-agent-session-input-admission'
import { agentSessionFailureFact } from '../../../../shared/agent-session/failures/agent-session-failure'
import { codexDispatchRejection } from './codex-structured-turn-end-settlement'
import type { AgentSessionExecutionOptions } from '../../../../shared/agent-session/options/agent-session-execution-options'
import type { StructuredAgentSessionAdapter } from '../../../native-chat/agent-session-wire/providers/structured-agent-session-adapter'
import type { CodexSession } from '../session-lifecycle/codex-structured-session-state'
import { captureCodexMessageCompilerInputs } from '../catalog/codex-structured-service-tier'
import {
compileCodexMessageOptions,
codexMessageInputOptions,
type StructuredMessageSteeringInputs
} from '../../../../shared/agent-session/options/codex-message-compiler'
export function resolvedCodexGoalSettings(
_session: CodexSession,
choices: AgentSessionExecutionOptions = {}
) {
return {
...(choices.model !== undefined ? { model: choices.model } : {}),
...(choices.effort !== undefined ? { effort: choices.effort } : {})
}
}
/** Captured fields are overrides; omitted fields inherit the native thread's state. */
export async function resolvedCodexMessageOptions(
session: CodexSession,
choices: AgentSessionExecutionOptions | undefined
): Promise<Readonly<Record<string, string>> | undefined> {
if (choices === undefined) {
return undefined
}
return compileCodexMessageOptions(captureCodexMessageCompilerInputs(session), choices)
}
/** Intent belongs to this exact acquisition; catalog/readback differences are not send barriers. */
export function readCodexAppliedInputOptions(
session: CodexSession | undefined,
input: {
fence: number
acquisitionGeneration: string | null
next: AgentSessionExecutionOptions | undefined
}
): { active: AgentSessionExecutionOptions; next: AgentSessionExecutionOptions } | null {
if (
!session ||
session.ended ||
session.connection.closed ||
session.fence !== input.fence ||
session.acquisitionGeneration !== input.acquisitionGeneration ||
!session.appliedTurnInputOptions
) {
return null
}
const active = session.appliedTurnInputOptions
const requested = input.next ?? Object.fromEntries(session.options)
return { active, next: codexMessageInputOptions(requested, active, session.appliedTurnOptions) }
}
/** An exact child-bound observation, with candidate-independent compiler inputs. */
export function readCodexMessageSteeringInputs(
session: CodexSession | undefined,
input: { fence: number; acquisitionGeneration: string | null }
): StructuredMessageSteeringInputs {
if (
!session ||
session.ended ||
session.connection.closed ||
session.fence !== input.fence ||
session.acquisitionGeneration !== input.acquisitionGeneration ||
!session.appliedTurnInputOptions
) {
return { kind: 'codex-unavailable' }
}
return { kind: 'codex', compiler: captureCodexMessageCompilerInputs(session) }
}
/** Check the live binding after options settle and again whenever the provider is about to write. */
export async function prepareCodexMessageDispatch(
session: CodexSession,
input: Parameters<StructuredAgentSessionAdapter['dispatch']>[0],
registered: () => boolean
) {
const checkAbort = () => {
if (input.signal?.aborted) {
throw new StructuredAgentSessionInputNotAdmitted({
state: 'rejected',
...codexDispatchRejection(
agentSessionFailureFact(session.ended ? 'providerExited' : 'cancelled')
)
})
}
}
await input.beforeDispatch?.()
checkAbort()
input.onInputPreparation?.()
const turnOptions = await waitForPromiseWithSignal(
resolvedCodexMessageOptions(session, input.executionOptions),
input.signal
).catch((error: unknown) => {
checkAbort()
throw error
})
checkAbort()
return {
...input,
turnOptions,
isCurrent: () =>
registered() &&
!session.ended &&
session.fence === input.fence &&
input.isCurrent?.() !== false
}
}
export function turnInputFor(
host: CodexTurnHost,
body: AgentJournalMessageItem
): Record<string, unknown>[] {
const input: Record<string, unknown>[] = []
let text = ''
for (const block of body.blocks as NativeChatBlock[]) {
if (block.type === 'text' && block.text.length > 0) {
input.push({ type: 'text', text: block.text })
text += `${block.text}\n`
} 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 })
}
}
// Unknown skill paths suppress plain $name resolution, so send only loaded files when known.
for (const skill of pickedSkillsInText(text, body.skills ?? [])) {
if (host.skillReport?.hasSkillPath(skill.path) !== false) {
input.push({ type: 'skill', name: skill.name, path: skill.path })
}
}
return input
}