Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/acp/acp-message-execution-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export async function applyAcpMessageOptions(
for (const key of ['model', 'effort'] as const) {
const value = options?.[key] ?? (key === 'model' ? initial.model : defaultEffort)
if (value === undefined) {
if (session.options.picked.has(key) || session.options.reported()[key] !== undefined) {
const applied = session.options.applied.get(key)
if (applied !== undefined && applied !== initial[key]) {
throw new AgentSessionOptionRejectedError(
new Error(`The default ${key} for this model is unknown`)
)
Expand Down
5 changes: 4 additions & 1 deletion src/main/acp/acp-structured-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export class AcpStructuredOptions {
// A loaded session keeps the model it ran; only a new one resolves the agent's own config.
private resolvesConfig = false
readonly picked = new Set<string>()
/** Each key's value from Orca's last successful write; never the agent's report. */
readonly applied = new Map<string, string>()
private initialChoice: Readonly<Record<string, string>> = {}
currentProof = new Set<string>()
writeRevision = 0
Expand Down Expand Up @@ -253,7 +255,7 @@ export function writeAcpSessionOption(
connection: Pick<AcpStructuredConnection, 'setConfigOption' | 'setModel'>,
options: AcpStructuredOptions,
write: AcpOptionWrite,
bound: { agent: string; timeoutMs: number; signal?: AbortSignal; key: string }
bound: { agent: string; timeoutMs: number; signal?: AbortSignal; key: string; value: string }
): Promise<void> {
const { agent, timeoutMs, signal } = bound
if (signal?.aborted) {
Expand All @@ -270,6 +272,7 @@ export function writeAcpSessionOption(
}
apply()
options.currentProof.add(bound.key)
options.applied.set(bound.key, bound.value)
}
const applied =
write.method === 'config'
Expand Down
29 changes: 29 additions & 0 deletions src/main/acp/acp-structured-session-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,35 @@ describe('ACP structured session adapter: turns', () => {
})
})

it('sends model-only choices away from the opening model, back, and away again', async () => {
const rig = await openAcpAdapterRig()
await rig.acquire()
for (const [index, [clientMessageId, model]] of [
['away', 'grok-4.6'],
['back', 'grok-4.7'],
['again', 'grok-4.6']
].entries()) {
await expect(
rig.adapter.dispatch({
sessionId: SESSION,
fence: 1,
clientMessageId,
body: hello,
executionOptions: { model }
})
).resolves.toEqual({ state: 'admitted' })
rig.child().agent.reply(await rig.frame('session/prompt', index), { stopReason: 'end_turn' })
await waitFor(() => expect(rig.adapter.holdsDispatch(SESSION)).toBe(false))
}
// Back on the opening model, its opening effort is restored; elsewhere the agent keeps its own.
expect(rig.sent('session/set_config_option').map((frame) => frame.params)).toMatchObject([
{ configId: 'model', value: 'grok-4.6' },
{ configId: 'model', value: 'grok-4.7' },
{ configId: 'reasoning_effort', value: 'high' },
{ configId: 'model', value: 'grok-4.6' }
])
})

it("sends Grok's prompt identity only to an agent whose dialect echoes it", async () => {
const rig = await openAcpAdapterRig({
deps: { spec: { ...GROK, dialect: GENERIC_ACP_DIALECT } }
Expand Down
1 change: 1 addition & 0 deletions src/main/acp/acp-structured-session-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ export class AcpStructuredSessionAdapter implements StructuredAgentSessionAdapte
// Bounded, and abandoned by a close or Stop: the session's queue waits on it.
await writeAcpSessionOption(session.connection, session.options, write, {
key: input.key,
value: input.value,
agent: session.spec.agent,
timeoutMs: this.deps.optionWriteTimeoutMs ?? ACP_OPTION_WRITE_TIMEOUT_MS,
...(input.signal ? { signal: input.signal } : {})
Expand Down
3 changes: 2 additions & 1 deletion src/main/pi/rpc-message-dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ export async function dispatchPiRpcMessage(
(key === 'model' || model === session.initialOptions.model
? session.initialOptions[key]
: undefined)
if (value === undefined && session.optionProof.writes.has(key)) {
const applied = session.selected.get(key)
if (value === undefined && applied !== undefined && applied !== session.initialOptions[key]) {
throw new AgentSessionOptionRejectedError(
new Error(`The default ${key} for this model is unknown`)
)
Expand Down
12 changes: 8 additions & 4 deletions src/main/pi/rpc-session-adapter.test-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,18 @@ export class FakeConnection implements PiRpcConnection {
private readonly exitListeners: (() => void)[] = []
requestOverride?: (command: string) => Promise<unknown> | undefined
abortRequest?: () => void
constructor(readonly handlers: JsonlRpcAgentConnectionOptions) {}
constructor(
readonly handlers: JsonlRpcAgentConnectionOptions,
private readonly reported: { thinkingLevel?: string } = {}
) {}
async request(command: string): Promise<unknown> {
this.requests.push(command)
const override = this.requestOverride?.(command)
if (override) {
return override
}
if (command === 'get_state') {
return state
return { ...state, ...this.reported }
}
if (command === 'get_available_models') {
return { models: [state.model] }
Expand Down Expand Up @@ -121,7 +124,8 @@ export function reportedEvent<T extends PiLifecycleEvent['type']>(

export async function setup(
options: Readonly<Record<string, string>> = {},
eventSink?: (sink: StructuredAgentSessionEventSink) => StructuredAgentSessionEventSink
eventSink?: (sink: StructuredAgentSessionEventSink) => StructuredAgentSessionEventSink,
reported?: ConstructorParameters<typeof FakeConnection>[1]
) {
const rig = await openProviderTimelineRig({ agent: 'pi', sessionId })
const connections: FakeConnection[] = []
Expand Down Expand Up @@ -152,7 +156,7 @@ export async function setup(
}))
const openConnection = vi.fn(
(_launch: ProviderProcessLaunch, handlers: JsonlRpcAgentConnectionOptions) => {
const connection = new FakeConnection(handlers)
const connection = new FakeConnection(handlers, reported)
connections.push(connection)
return connection
}
Expand Down
37 changes: 37 additions & 0 deletions src/main/pi/rpc-session-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,43 @@ describe('Pi RPC session ownership and delivery', () => {
expect(h.connection.sent).toMatchObject([{ type: 'prompt', message: 'chosen' }])
})

it('sends model-only choices away from the opening model, back, and away again', async () => {
const h = await setup({}, undefined, { thinkingLevel: 'medium' })
h.connection.requestOverride = (command) =>
command === 'set_thinking_level' ? Promise.resolve({}) : undefined
for (const [clientMessageId, model] of [
['away', 'anthropic/model-2'],
['back', 'anthropic/model-1'],
['again', 'anthropic/model-2']
]) {
await expect(
h.adapter.dispatch({
sessionId,
fence: 7,
clientMessageId,
body: {
kind: 'message',
role: 'user',
blocks: [{ type: 'text', text: clientMessageId }]
},
executionOptions: { model }
})
).resolves.toEqual({ state: 'admitted' })
}
// Back on the opening model, its opening effort is restored; elsewhere Pi keeps its own.
expect(h.connection.requests.filter((command) => command.startsWith('set_'))).toEqual([
'set_model',
'set_model',
'set_thinking_level',
'set_model'
])
expect(h.connection.sent).toMatchObject([
{ message: 'away' },
{ message: 'back' },
{ message: 'again' }
])
})

it('rejects a known-unsent prompt when an option times out and preserves Stop reason', async () => {
const h = await setup()
const timeout = new Error('Pi did not answer set_model')
Expand Down
Loading