Skip to content

Commit af319d3

Browse files
fix: reconcile accepted ACP session controls (#519)
Restore saved model and valid config selections on genuine reconnects, reconcile complete acknowledgements, and preserve notification ordering through initialization, idle controls, and active turns. Do not reuse a client after failed preference restoration. Consolidates the resume repair with accepted model/effort persistence and returns the accepted snapshot to embedded consumers. Co-authored-by: Firmansyah Otoluwa <[email protected]>
1 parent e131748 commit af319d3

23 files changed

Lines changed: 1259 additions & 661 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Repo: https://github.com/openclaw/acpx
1515
### Fixes
1616

1717
- Docs/JSON: fix raw ACP tool-call pipelines and message examples so automation reads nested session updates and handles partial tool updates. Fixes #520. Thanks @prateek.
18+
- Session controls: restore saved model/config selections after reconnect, keep reasoning effort aligned with accepted model changes, and return accepted configuration to embedded clients without pinning unselected defaults. Thanks @programmerlapar.
1819

1920
## 2026.8.18 (v0.13.1)
2021

‎agents/Claude.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
- Default command: `npx -y @agentclientprotocol/claude-agent-acp`
55
- Upstream: https://github.com/agentclientprotocol/claude-agent-acp
66
- ACPX pins the built-in package range so fresh installs pick up Claude model and ACP adapter fixes without depending on a global adapter binary.
7+
- Model switches can change the available effort controls. ACPX updates existing saved effort selections from the accepted response and removes selections whose controls disappear.
8+
- Saved model and config selections are restored after reconnect, without replacing the conversation.
79

810
## Settings isolation
911

‎agents/Codex.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
- ACPX owns the built-in package range so fresh launches use the repository-selected stable adapter line without requiring a global install.
77
- Runtime controls exposed by current codex-acp releases include ACP modes plus separate `model` and `reasoning_effort` session config options.
88
- Use the advertised base model id with `acpx --model <id> codex ...` or `acpx codex set model <id>`, then set reasoning effort separately with `acpx codex set reasoning_effort <value>`.
9+
- Switching models can adjust reasoning effort. ACPX saves the accepted effort for an existing selection, or removes that selection if the new model has no effort control.
10+
- Reconnecting restores the saved model and effort before prompting, even when the adapter resumes the conversation with different defaults.
911
- Legacy `models` metadata may encode both values in a combined id such as `gpt-5.6-sol[max]`; ACPX uses that form only when the adapter does not advertise the newer model config option.

‎docs/CLI.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ Behavior:
322322
- Routes through queue-owner IPC when an owner is active.
323323
- Falls back to a direct client reconnect when no owner is running.
324324
- **`set model <id>`**: Uses the advertised model config option through `session/set_config_option`; adapters that explicitly advertise legacy `models` metadata use `session/set_model`.
325+
- Saves accepted values for existing non-mode selections, including reasoning effort adjusted or removed by a model switch; does not pin unselected defaults.
326+
- Restores saved model and config selections after reconnect, before the next prompt; already loaded sessions are reused without replay.
325327
326328
## `sessions` subcommand
327329

‎docs/session-control.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ acpx claude set verbosity terse
4848
acpx set model gpt-5.4 # defaults to codex
4949
```
5050

51-
Calls ACP `session/set_config_option` with the literal `<key>` and `<value>`. Non-mode `set_config_option` values are persisted by `acpx` and replayed onto fresh adapter sessions when the adapter supports those config keys.
51+
Calls ACP `session/set_config_option` with the literal `<key>` and `<value>`. Non-mode selections are saved using the adapter's accepted values and restored after reconnect, before the next prompt. If a control changes another saved selection, such as reasoning effort after a model switch, ACPX saves the adjusted value or removes the selection when its control disappears. Unselected defaults are not pinned.
52+
53+
After a genuine resume or load, ACPX restores the saved model first when the adapter advertises model controls, then replays saved configuration. An already loaded, reusable session needs no replay. A failed replay stops the operation with an error instead of silently continuing with defaults.
54+
55+
For applications using `acpx/runtime`, `AcpxRuntime.setConfigOption(...)` returns the ACP response's complete `configOptions` after saving the accepted state. Use that response to update displayed controls, including changed or removed sibling options.
5256

5357
### `set model <id>`
5458

‎skills/acpx/SKILL.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ Behavior:
189189
- Current codex-acp releases expose `model` and `reasoning_effort` as separate config options.
190190
- `--model <id>`: Claude-compatible adapters may consume session creation metadata; other agents must advertise a model config option or legacy `models` metadata.
191191
- `set model <id>`: uses `session/set_config_option` for advertised model config options and preserves `session/set_model` for explicitly advertised legacy models.
192+
- Model switches can change or remove reasoning-effort controls. ACPX reconciles saved non-mode selections with the accepted response; select a supported effort again if needed.
193+
- After reconnect, ACPX restores the saved model when advertised and replays saved config selections before prompting. A replay failure is reported instead of silently using defaults.
192194
- `set-mode`/`set` route through queue-owner IPC when active, otherwise reconnect directly.
193195
194196
### Sessions

‎src/cli/session/prompt-runner.ts‎

Lines changed: 32 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
1-
import type { AcpClient } from "../../acp/client.js";
21
import { withTimeout } from "../../async-control.js";
32
import {
43
withConnectedSession,
54
type FullConnectedSessionController,
65
type WithConnectedSessionOptions,
76
type WithConnectedSessionResult,
87
} from "../../runtime/engine/connected-session.js";
9-
import { sessionOptionsFromRecord } from "../../runtime/engine/session-options.js";
10-
import { applyConfigOptionsToRecord } from "../../session/config-options.js";
11-
import {
12-
setCurrentModelId,
13-
setDesiredConfigOption,
14-
setDesiredModeId,
15-
setDesiredModelId,
16-
} from "../../session/mode-preference.js";
17-
import {
18-
applyRequestedModelIfAdvertised,
19-
currentModelIdFromSetModelResponse,
20-
} from "../../session/model-application.js";
8+
import { applyConfigOptionSelection, applyModelSelection } from "../../session/config-options.js";
9+
import { setDesiredModeId } from "../../session/mode-preference.js";
2110
import { advertisedModelState } from "../../session/model-state.js";
2211
import { resolveSessionRecord, writeSessionRecord } from "../../session/persistence.js";
2312
import type {
2413
AuthPolicy,
2514
McpServer,
2615
NonInteractivePermissionPolicy,
27-
SessionRecord,
2816
SessionSetConfigOptionResult,
2917
SessionSetModelResult,
3018
SessionSetModeResult,
@@ -81,6 +69,7 @@ export type RunSessionSetModelDirectOptions = {
8169

8270
type DirectConnectedSessionOptions = {
8371
sessionRecordId: string;
72+
replacingConfigOption?: WithConnectedSessionOptions<unknown>["replacingConfigOption"];
8473
mcpServers?: McpServer[];
8574
nonInteractivePermissions?: NonInteractivePermissionPolicy;
8675
authCredentials?: Record<string, string>;
@@ -99,6 +88,7 @@ function buildDirectConnectedSessionOptions<T>(
9988
): WithConnectedSessionOptions<T> {
10089
return {
10190
sessionRecordId: options.sessionRecordId,
91+
replacingConfigOption: options.replacingConfigOption,
10292
loadRecord: resolveSessionRecord,
10393
saveRecord: writeSessionRecord,
10494
mcpServers: options.mcpServers,
@@ -127,64 +117,11 @@ function toSessionMutationResult(
127117
};
128118
}
129119

130-
/**
131-
* Re-apply the session-pinned model after reconnect, matching the prompt path.
132-
*
133-
* Some agents (e.g. opencode) restore to their default model on session load and
134-
* advertise model-dependent config options for that default. Without replaying
135-
* `session_options.model` first, `set` fails because the option set belongs to
136-
* the wrong model, and the record is overwritten with default-model options.
137-
*/
138-
async function reapplyPinnedModelAfterConnect(params: {
139-
client: AcpClient;
140-
sessionId: string;
141-
record: SessionRecord;
142-
timeoutMs?: number;
143-
}): Promise<void> {
144-
const pinnedModel = sessionOptionsFromRecord(params.record)?.model;
145-
if (!pinnedModel) {
146-
return;
147-
}
148-
149-
const models = advertisedModelState(params.record.acpx);
150-
if (models?.currentModelId === pinnedModel) {
151-
// Keep the pin sticky when the agent already reports it after load.
152-
setDesiredModelId(params.record, pinnedModel, models.configId);
153-
setCurrentModelId(params.record, pinnedModel);
154-
return;
155-
}
156-
157-
const result = await applyRequestedModelIfAdvertised({
158-
client: params.client,
159-
sessionId: params.sessionId,
160-
requestedModel: pinnedModel,
161-
models,
162-
agentCommand: params.record.agentCommand,
163-
timeoutMs: params.timeoutMs,
164-
});
165-
if (result.response) {
166-
applyConfigOptionsToRecord(params.record, result.response);
167-
}
168-
if (result.applied) {
169-
setDesiredModelId(params.record, pinnedModel, models?.configId);
170-
setCurrentModelId(
171-
params.record,
172-
currentModelIdFromSetModelResponse(result.response, pinnedModel),
173-
);
174-
}
175-
}
176-
177120
export async function runSessionSetModeDirect(
178121
options: RunSessionSetModeDirectOptions,
179122
): Promise<SessionSetModeResult> {
180123
const result = await withConnectedSession(
181124
buildDirectConnectedSessionOptions(options, async ({ client, sessionId, record }) => {
182-
await reapplyPinnedModelAfterConnect({
183-
client,
184-
sessionId,
185-
record,
186-
timeoutMs: options.timeoutMs,
187-
});
188125
await withTimeout(client.setSessionMode(sessionId, options.modeId), options.timeoutMs);
189126
setDesiredModeId(record, options.modeId);
190127
}),
@@ -197,19 +134,18 @@ export async function runSessionSetModelDirect(
197134
options: RunSessionSetModelDirectOptions,
198135
): Promise<SessionSetModelResult> {
199136
const result = await withConnectedSession(
200-
buildDirectConnectedSessionOptions(options, async ({ client, sessionId, record }) => {
201-
// Explicit model switch replaces the saved pin. Do not re-apply the old
202-
// pin first: an unadvertised saved model would reject and block the switch.
203-
const models = advertisedModelState(record.acpx);
204-
const response = await withTimeout(
205-
client.setSessionModel(sessionId, options.modelId, models),
206-
options.timeoutMs,
207-
);
208-
applyConfigOptionsToRecord(record, response);
209-
setDesiredModelId(record, options.modelId, models?.configId);
210-
setCurrentModelId(record, currentModelIdFromSetModelResponse(response, options.modelId));
211-
return response;
212-
}),
137+
buildDirectConnectedSessionOptions(
138+
{ ...options, replacingConfigOption: { key: "model" } },
139+
async ({ client, sessionId, record }) => {
140+
const models = advertisedModelState(record.acpx);
141+
const response = await withTimeout(
142+
client.setSessionModel(sessionId, options.modelId, models),
143+
options.timeoutMs,
144+
);
145+
record.acpx = applyModelSelection(record.acpx, options.modelId, response);
146+
return response;
147+
},
148+
),
213149
);
214150

215151
return { ...toSessionMutationResult(result), response: result.value };
@@ -219,34 +155,22 @@ export async function runSessionSetConfigOptionDirect(
219155
options: RunSessionSetConfigOptionDirectOptions,
220156
): Promise<SessionSetConfigOptionResult> {
221157
const result = await withConnectedSession(
222-
buildDirectConnectedSessionOptions(options, async ({ client, sessionId, record }) => {
223-
const modelConfigId = advertisedModelState(record.acpx)?.configId;
224-
// Model-valued config updates replace the pin; skip pin replay so an
225-
// obsolete saved model cannot block the replacement. Other config keys
226-
// still re-apply the pin so model-dependent options see the right set.
227-
if (options.configId !== modelConfigId) {
228-
await reapplyPinnedModelAfterConnect({
229-
client,
230-
sessionId,
231-
record,
232-
timeoutMs: options.timeoutMs,
233-
});
234-
}
235-
const response = await withTimeout(
236-
client.setSessionConfigOption(sessionId, options.configId, options.value),
237-
options.timeoutMs,
238-
);
239-
applyConfigOptionsToRecord(record, response);
240-
if (options.configId === modelConfigId) {
241-
setDesiredModelId(record, options.value, options.configId);
242-
setCurrentModelId(record, currentModelIdFromSetModelResponse(response, options.value));
243-
} else if (options.configId === "mode") {
244-
setDesiredModeId(record, options.value);
245-
} else {
246-
setDesiredConfigOption(record, options.configId, options.value);
247-
}
248-
return response;
249-
}),
158+
buildDirectConnectedSessionOptions(
159+
{ ...options, replacingConfigOption: { key: options.configId } },
160+
async ({ client, sessionId, record }) => {
161+
const response = await withTimeout(
162+
client.setSessionConfigOption(sessionId, options.configId, options.value),
163+
options.timeoutMs,
164+
);
165+
record.acpx = applyConfigOptionSelection(
166+
record.acpx,
167+
options.configId,
168+
options.value,
169+
response,
170+
);
171+
return response;
172+
},
173+
),
250174
);
251175

252176
return {

0 commit comments

Comments
 (0)