Skip to content

Commit 25baf2f

Browse files
fix(acp): report only the modes the agent changed, not the whole selection
The agent's mode report carried the selection applied at turn start, and the ingestor wrote it over the thread. A model or option the user saved during the turn was reverted to the stale one. The provider event now carries the instance and each changed option as { id, from, to }. The ingestor applies a change to the thread's current selection only where the thread still holds `from`, so newer settings and newer mode picks win. AI-assisted; verified with the tests and tools listed above.
1 parent 6bb0e0a commit 25baf2f

5 files changed

Lines changed: 84 additions & 56 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,13 +2572,19 @@ describe("AcpAdapterV2", () => {
25722572
Stream.runForEach((event) =>
25732573
Effect.sync(() => {
25742574
if (event.type === "thread_model_selection.updated") {
2575-
reported.push({ threadId: event.threadId, modelSelection: event.modelSelection });
2575+
reported.push({
2576+
threadId: event.threadId,
2577+
instanceId: event.instanceId,
2578+
changes: event.changes,
2579+
});
25762580
}
25772581
}),
25782582
),
25792583
);
25802584
}
2581-
assert.deepStrictEqual(reported, [{ threadId, modelSelection: ask }]);
2585+
assert.deepStrictEqual(reported, [
2586+
{ threadId, instanceId, changes: [{ id: "mode", from: "architect", to: "ask" }] },
2587+
]);
25822588
yield* makeAcpReplayCompletenessAssertion(fileSystem, statusPath, transcript);
25832589
}).pipe(Effect.provide(layerTest), Effect.scoped),
25842590
);

‎apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts‎

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,48 +2144,58 @@ layer("ProviderEventIngestorV2", (it) => {
21442144
}),
21452145
);
21462146

2147-
it.effect(
2148-
"moves a thread to the selection its agent reported, once and only for its instance",
2149-
() =>
2150-
Effect.gen(function* () {
2151-
const now = yield* DateTime.now;
2152-
const eventSink = yield* EventSink.EventSinkV2;
2153-
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
2154-
const ingestor = yield* ProviderEventIngestor.ProviderEventIngestorV2;
2155-
const idAllocator = yield* IdAllocator.IdAllocatorV2;
2156-
const rootEvent = yield* threadCreatedEvent(now);
2157-
yield* eventSink.write({ events: [rootEvent] });
2158-
const providerSessionId = yield* idAllocator.allocate.providerSession({
2147+
it.effect("applies an agent's mode change without undoing newer thread settings", () =>
2148+
Effect.gen(function* () {
2149+
const now = yield* DateTime.now;
2150+
const eventSink = yield* EventSink.EventSinkV2;
2151+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
2152+
const ingestor = yield* ProviderEventIngestor.ProviderEventIngestorV2;
2153+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
2154+
const rootEvent = yield* threadCreatedEvent(now);
2155+
if (rootEvent.type !== "thread.created") throw new Error("Expected thread fixture");
2156+
// Saved mid-turn: a newer model and effort the reporting turn never saw.
2157+
const saved = {
2158+
...modelSelection,
2159+
model: "gpt-6.1-sol",
2160+
options: [
2161+
{ id: "effort", value: "high" },
2162+
{ id: "mode", value: "plan" },
2163+
],
2164+
};
2165+
yield* eventSink.write({
2166+
events: [{ ...rootEvent, payload: { ...rootEvent.payload, modelSelection: saved } }],
2167+
});
2168+
const providerSessionId = yield* idAllocator.allocate.providerSession({
2169+
providerInstanceId: modelSelection.instanceId,
2170+
threadId: rootEvent.threadId,
2171+
});
2172+
const ingest = (instanceId: ProviderInstanceId, from: string) =>
2173+
ingestor.ingestNormalized({
2174+
providerSessionId,
21592175
providerInstanceId: modelSelection.instanceId,
21602176
threadId: rootEvent.threadId,
2161-
});
2162-
const ingest = (reported: ModelSelection) =>
2163-
ingestor.ingestNormalized({
2164-
providerSessionId,
2165-
providerInstanceId: modelSelection.instanceId,
2177+
event: {
2178+
type: "thread_model_selection.updated",
2179+
driver: CODEX_DRIVER,
21662180
threadId: rootEvent.threadId,
2167-
event: {
2168-
type: "thread_model_selection.updated",
2169-
driver: CODEX_DRIVER,
2170-
threadId: rootEvent.threadId,
2171-
modelSelection: reported,
2172-
},
2173-
});
2174-
const reported = { ...modelSelection, options: [{ id: "mode", value: "default" }] };
2175-
assert.deepEqual(
2176-
(yield* ingest(reported)).map((stored) => stored.event.type),
2177-
["thread.model-selection-updated"],
2178-
);
2179-
assert.deepEqual(
2180-
(yield* projectionStore.getThread(rootEvent.threadId)).modelSelection,
2181-
reported,
2182-
);
2183-
assert.deepEqual(yield* ingest(reported), []);
2184-
assert.deepEqual(
2185-
yield* ingest({ ...reported, instanceId: ProviderInstanceId.make("other-instance") }),
2186-
[],
2187-
);
2188-
}),
2181+
instanceId,
2182+
changes: [{ id: "mode", from, to: "default" }],
2183+
},
2184+
});
2185+
assert.deepEqual(
2186+
(yield* ingest(modelSelection.instanceId, "plan")).map((stored) => stored.event.type),
2187+
["thread.model-selection-updated"],
2188+
);
2189+
assert.deepEqual((yield* projectionStore.getThread(rootEvent.threadId)).modelSelection, {
2190+
...saved,
2191+
options: [
2192+
{ id: "effort", value: "high" },
2193+
{ id: "mode", value: "default" },
2194+
],
2195+
});
2196+
assert.deepEqual(yield* ingest(modelSelection.instanceId, "plan"), []);
2197+
assert.deepEqual(yield* ingest(ProviderInstanceId.make("other-instance"), "default"), []);
2198+
}),
21892199
);
21902200

21912201
it.effect(

‎apps/server/src/orchestration-v2/ProviderEventIngestor.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -494,24 +494,32 @@ export const layer: Layer.Layer<
494494
];
495495
}
496496
case "thread_model_selection.updated": {
497-
const { modelSelection } = input.event;
497+
const { instanceId, changes } = input.event;
498498
const thread = yield* projections.getThread(input.event.threadId).pipe(
499499
Effect.catchTags({
500500
ProjectionStoreThreadNotFoundError: () => Effect.succeed(null),
501501
}),
502502
);
503-
if (
504-
thread === null ||
505-
thread.modelSelection.instanceId !== modelSelection.instanceId ||
506-
modelSelectionsEqual(thread.modelSelection, modelSelection)
507-
)
503+
const current = thread?.modelSelection;
504+
if (thread === null || current?.instanceId !== instanceId || !current.options) {
508505
return [];
506+
}
507+
let changed = false;
508+
const options = current.options.map((option) => {
509+
const change = changes.find(
510+
(candidate) => candidate.id === option.id && candidate.from === option.value,
511+
);
512+
if (change === undefined) return option;
513+
changed = true;
514+
return { ...option, value: change.to };
515+
});
516+
if (!changed) return [];
509517
const now = yield* DateTime.now;
510518
return [
511519
yield* makeDomainEvent(input, {
512520
type: "thread.model-selection-updated",
513521
threadId: thread.id,
514-
payload: { ...thread, modelSelection, updatedAt: now },
522+
payload: { ...thread, modelSelection: { ...current, options }, updatedAt: now },
515523
occurredAt: now,
516524
}),
517525
];

‎packages/provider-acp/src/server/adapter.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7629,7 +7629,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
76297629
if (selection === null || selection.options === undefined) return;
76307630
const configured = yield* Ref.get(configuredModes);
76317631
const live = yield* readModes;
7632-
let changed = false;
7632+
const changes: Array<{ id: string; from: string; to: string }> = [];
76337633
const options = selection.options.map((option) => {
76347634
const value = live.get(option.id);
76357635
if (
@@ -7640,18 +7640,18 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
76407640
) {
76417641
return option;
76427642
}
7643-
changed = true;
7643+
changes.push({ id: option.id, from: option.value, to: value });
76447644
return { ...option, value };
76457645
});
7646-
if (!changed) return;
7647-
const reported = { ...selection, options };
7648-
yield* Ref.set(activeSelection, reported);
7646+
if (changes.length === 0) return;
7647+
yield* Ref.set(activeSelection, { ...selection, options });
76497648
yield* Ref.set(configuredModes, live);
76507649
yield* emitProviderEvent({
76517650
type: "thread_model_selection.updated",
76527651
driver,
76537652
threadId,
7654-
modelSelection: reported,
7653+
instanceId: selection.instanceId,
7654+
changes,
76557655
});
76567656
});
76577657

‎packages/provider-core/src/server/ProviderAdapter.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,16 @@ export const ProviderAdapterV2Event = Schema.Union([
102102
sessionScoped: Schema.optional(Schema.Boolean),
103103
retainedNativeTurnIds: Schema.optional(Schema.Array(Schema.String)),
104104
}),
105-
/** The agent changed a selected option itself, such as leaving plan mode once its plan ran. */
105+
/** The agent changed selected options itself, such as leaving plan mode once its plan ran. */
106106
Schema.Struct({
107107
type: Schema.Literal("thread_model_selection.updated"),
108108
driver: ProviderDriverKind,
109109
threadId: ThreadId,
110-
modelSelection: ModelSelection,
110+
instanceId: ProviderInstanceId,
111+
/** Applies only where the thread still holds `from`, so newer user picks win. */
112+
changes: Schema.Array(
113+
Schema.Struct({ id: Schema.String, from: Schema.String, to: Schema.String }),
114+
),
111115
}),
112116
Schema.Struct({
113117
type: Schema.Literal("provider_turn.updated"),

0 commit comments

Comments
 (0)