The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 12:15 UTC
fix(acp): replace and remove identified plans across turns (#36978)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Tanav Poswal committed
Nov 15, 2026 at 07:08 UTC
bbda0c107110d9e179e6a3c908a1554fb0e83387
4 files changed
+271
-30
apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
+125
@@ -3075,6 +3075,131 @@ describe("AcpAdapterV2", () => {
3075
}).pipe(Effect.provide(layerTest)),
3076
);
3077
3078
+ it.live("replaces and removes identified ACP plans across turns", () =>
3079
+ Effect.gen(function* () {
3080
+ const instanceId = ProviderInstanceId.make("acp-cross-turn-plans");
3081
+ const threadId = ThreadId.make("acp-cross-turn-plans");
3082
+ let notify:
3083
+ | Parameters<AcpSessionRuntime.AcpSessionRuntime["Service"]["handleSessionUpdate"]>[0]
3084
+ | undefined;
3085
+ let ordinal = 0;
3086
+ const adapter = yield* makeAcpAdapterV2({
3087
+ instanceId,
3088
+ selfInvocation: yield* resolveSelfInvocation(),
3089
+ flavor: {
3090
+ driver: ACP_TEST_DRIVER,
3091
+ capabilities: AcpProviderCapabilitiesV2,
3092
+ makeRuntime: makeMockRuntime({
3093
+ childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
3094
+ mockAgentPath: yield* bundledMockAgentPath,
3095
+ wrapRuntime: (runtime) => ({
3096
+ ...runtime,
3097
+ handleSessionUpdate: (handler) =>
3098
+ Effect.sync(() => {
3099
+ notify = handler;
3100
+ }).pipe(Effect.andThen(runtime.handleSessionUpdate(handler))),
3101
+ prompt: () =>
3102
+ Effect.gen(function* () {
3103
+ ordinal++;
3104
+ const send = (update: EffectAcpSchema.SessionUpdate) =>
3105
+ notify!({ sessionId: "mock-session-1", update });
3106
+ const plan = (id: string, text: string) =>
3107
+ send({
3108
+ sessionUpdate: "plan_update",
3109
+ plan: {
3110
+ type: "items",
3111
+ planId: id,
3112
+ entries: [{ content: text, priority: "medium", status: "pending" }],
3113
+ },
3114
+ });
3115
+ yield* send({ sessionUpdate: "state_update", state: "running" });
3116
+ if (ordinal === 1) {
3117
+ yield* plan("control", "First");
3118
+ yield* plan("control", "Replacement");
3119
+ yield* send({ sessionUpdate: "plan_removed", planId: "control" });
3120
+ yield* plan("later-remove", "Remove on next turn");
3121
+ yield* plan("later-replace", "Old contents");
3122
+ } else {
3123
+ yield* send({ sessionUpdate: "plan_removed", planId: "later-remove" });
3124
+ yield* plan("later-replace", "New contents");
3125
+ }
3126
+ yield* send({
3127
+ sessionUpdate: "plan",
3128
+ entries: [{ content: "Anonymous plan", priority: "medium", status: "pending" }],
3129
+ });
3130
+ yield* send({
3131
+ sessionUpdate: "state_update",
3132
+ state: "idle",
3133
+ stopReason: "end_turn",
3134
+ });
3135
+ return { stopReason: "end_turn" as const };
3136
+ }),
3137
+ }),
3138
+ }),
3139
+ },
3140
+ });
3141
+ const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
3142
+ runtimeMode: "full-access",
3143
+ interactionMode: "default",
3144
+ cwd: process.cwd(),
3145
+ });
3146
+ const modelSelection = { instanceId, model: "default" } as const;
3147
+ const runtime = yield* adapter.openSession({
3148
+ threadId,
3149
+ providerSessionId: ProviderSessionId.make("acp-cross-turn-plans"),
3150
+ modelSelection,
3151
+ runtimePolicy,
3152
+ });
3153
+ const providerThread = yield* runtime.ensureThread({
3154
+ threadId,
3155
+ modelSelection,
3156
+ runtimePolicy,
3157
+ });
3158
+ const now = yield* DateTime.now;
3159
+ const run = (ordinal: number) =>
3160
+ Effect.gen(function* () {
3161
+ yield* runtime.startTurn(
3162
+ makeTurnInput({ threadId, providerThread, instanceId, runtimePolicy, now, ordinal }),
3163
+ );
3164
+ return Array.from(
3165
+ yield* runtime.events.pipe(
3166
+ Stream.takeUntil((event) => event.type === "turn.terminal"),
3167
+ Stream.runCollect,
3168
+ ),
3169
+ ).flatMap((event) => (event.type === "plan.updated" ? [event.plan] : []));
3170
+ });
3171
+ const first = yield* run(1);
3172
+ assert.equal(first.length, 6);
3173
+ assert.equal(first[0]!.id, first[1]!.id);
3174
+ assert.equal(first[1]!.id, first[2]!.id);
3175
+ assert.equal(first[1]!.kind, "todo_list");
3176
+ assert.deepEqual(first[1]!.kind === "todo_list" ? first[1]!.steps.map((s) => s.text) : [], [
3177
+ "Replacement",
3178
+ ]);
3179
+ assert.equal(first[2]!.status, "superseded");
3180
+ const second = yield* run(2);
3181
+ assert.equal(second.length, 3);
3182
+ assert.notEqual(first[5]!.id, second[2]!.id);
3183
+ const removed = second.find((p) => p.id === first[3]!.id && p.status === "superseded");
3184
+ const replacement = second.find(
3185
+ (p) => p.kind === "todo_list" && p.steps.some((s) => s.text === "New contents"),
3186
+ );
3187
+ assert.isDefined(
3188
+ removed,
3189
+ "plan_removed on a later turn must supersede the existing session plan",
3190
+ );
3191
+ assert.equal(removed?.runId, first[3]!.runId);
3192
+ assert.equal(replacement?.runId, first[4]!.runId);
3193
+ assert.equal(removed?.nodeId, first[3]!.nodeId);
3194
+ assert.equal(replacement?.nodeId, first[4]!.nodeId);
3195
+ assert.equal(
3196
+ replacement?.id,
3197
+ first[4]!.id,
3198
+ "the same session plan must be replaced, not duplicated",
3199
+ );
3200
+ }).pipe(Effect.provide(layerTest), Effect.scoped),
3201
+ );
3202
+
3203
it.effect("starts a new replay message after ACP v2 plan boundaries", () =>
3204
Effect.gen(function* () {
3205
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
apps/server/src/orchestration-v2/RunExecutionService.test.ts
+67
@@ -610,6 +610,73 @@ it("routes only exact same-thread background items inherited from settled runs",
610
);
611
});
612
613
+it("routes an earlier run's plan rows to this thread's active root turn only", () => {
614
+ const threadId = ThreadId.make("thread:earlier-plan-routing");
615
+ const priorRunId = RunId.make("run:earlier-plan-routing:prior");
616
+ const rootTurnId = ProviderTurnId.make("provider-turn:earlier-plan-routing:current");
617
+ const identity: RunExecutionService.ProviderEventRouteIdentity = {
618
+ threadId,
619
+ runId: RunId.make("run:earlier-plan-routing:current"),
620
+ attemptId: RunAttemptId.make("attempt:earlier-plan-routing:current"),
621
+ providerThreadId: ProviderThreadId.make("provider-thread:earlier-plan-routing"),
622
+ };
623
+ const active = RunExecutionService.makeProviderEventRoutingState({
624
+ identity,
625
+ providerTurnId: rootTurnId,
626
+ });
627
+ const planRow = {
628
+ type: "turn_item.updated",
629
+ driver,
630
+ turnItem: {
631
+ id: TurnItemId.make("turn-item:earlier-plan-routing"),
632
+ threadId,
633
+ runId: priorRunId,
634
+ type: "todo_list",
635
+ },
636
+ } as ProviderAdapter.ProviderAdapterV2Event;
637
+ const plan = {
638
+ type: "plan.updated",
639
+ driver,
640
+ plan: { threadId, runId: priorRunId },
641
+ } as ProviderAdapter.ProviderAdapterV2Event;
642
+ const otherThreadPlan = {
643
+ type: "plan.updated",
644
+ driver,
645
+ plan: { threadId: ThreadId.make("thread:earlier-plan-routing:other"), runId: priorRunId },
646
+ } as ProviderAdapter.ProviderAdapterV2Event;
647
+ const ordinaryRow = {
648
+ type: "turn_item.updated",
649
+ driver,
650
+ turnItem: {
651
+ id: TurnItemId.make("turn-item:earlier-plan-routing:tool"),
652
+ threadId,
653
+ runId: priorRunId,
654
+ type: "reasoning",
655
+ },
656
+ } as ProviderAdapter.ProviderAdapterV2Event;
657
+ const [, ended] = RunExecutionService.routeProviderEvent(
658
+ {
659
+ type: "turn.terminal",
660
+ driver,
661
+ providerThreadId: identity.providerThreadId,
662
+ providerTurnId: rootTurnId,
663
+ runOrdinal: 2,
664
+ status: "completed",
665
+ failure: null,
666
+ threadDisposition: "reusable",
667
+ },
668
+ identity,
669
+ active,
670
+ );
671
+
672
+ assert.isTrue(RunExecutionService.routeProviderEvent(planRow, identity, active)[0]);
673
+ assert.isTrue(RunExecutionService.routeProviderEvent(plan, identity, active)[0]);
674
+ assert.isFalse(RunExecutionService.routeProviderEvent(otherThreadPlan, identity, active)[0]);
675
+ assert.isFalse(RunExecutionService.routeProviderEvent(ordinaryRow, identity, active)[0]);
676
+ assert.isFalse(RunExecutionService.routeProviderEvent(planRow, identity, ended)[0]);
677
+ assert.isFalse(RunExecutionService.routeProviderEvent(plan, identity, ended)[0]);
678
+});
679
+
680
it("selects only live background items from non-completed settled prior runs", () => {
681
const threadId = ThreadId.make("thread:inherited-background-selection");
682
const otherThreadId = ThreadId.make("thread:inherited-background-selection:other");
apps/server/src/orchestration-v2/RunExecutionService.ts
+21
-3
@@ -370,6 +370,10 @@ export function routeProviderEvent(
370
const ownsChildThread = (threadId: ThreadId): boolean =>
371
threadId !== input.threadId && ownsThread(threadId);
372
const ownsRun = (runId: string | null): boolean => runId === input.runId;
373
+ // ACP plan ids are session-scoped, so a turn can update a plan an earlier turn of this
374
+ // thread reported. Only an active root turn adopts those rows, never a lingering run.
375
+ const adoptsEarlierPlan = (threadId: ThreadId): boolean =>
376
+ threadId === input.threadId && !state.rootTurnEnded;
377
const addProviderThread = (providerThreadId: ProviderThreadId): ProviderEventRoutingState => ({
378
...state,
379
ownedProviderThreadIds: new Set([...state.ownedProviderThreadIds, providerThreadId]),
@@ -431,7 +435,11 @@ export function routeProviderEvent(
435
: [false, state];
436
}
437
case "node.updated": {
434
- const belongs = ownsRun(event.node.runId) || ownsChildThread(event.node.threadId);
438
+ const belongs =
439
+ ownsRun(event.node.runId) ||
440
+ ownsChildThread(event.node.threadId) ||
441
+ ((event.node.kind === "todo_list" || event.node.kind === "plan") &&
442
+ adoptsEarlierPlan(event.node.threadId));
443
if (!belongs || event.node.providerThreadId === null) {
444
return [belongs, state];
445
}
@@ -492,7 +500,12 @@ export function routeProviderEvent(
500
case "steer.updated":
501
return [ownsRun(event.runId), state];
502
case "turn_item.updated": {
495
- if (ownsRun(event.turnItem.runId) || ownsChildThread(event.turnItem.threadId)) {
503
+ if (
504
+ ownsRun(event.turnItem.runId) ||
505
+ ownsChildThread(event.turnItem.threadId) ||
506
+ ((event.turnItem.type === "todo_list" || event.turnItem.type === "proposed_plan") &&
507
+ adoptsEarlierPlan(event.turnItem.threadId))
508
+ ) {
509
return [true, state];
510
}
511
const inheritedRunId = state.inheritedBackgroundTurnItems.get(event.turnItem.id);
@@ -512,7 +525,12 @@ export function routeProviderEvent(
525
return [true, { ...state, inheritedBackgroundTurnItems }];
526
}
527
case "plan.updated":
515
- return [ownsRun(event.plan.runId) || ownsChildThread(event.plan.threadId), state];
528
+ return [
529
+ ownsRun(event.plan.runId) ||
530
+ ownsChildThread(event.plan.threadId) ||
531
+ adoptsEarlierPlan(event.plan.threadId),
532
+ state,
533
+ ];
534
case "runtime_request.updated":
535
return [
536
(event.threadId !== undefined && ownsChildThread(event.threadId)) ||
packages/provider-acp/src/server/adapter.ts
+58
-27
@@ -1497,6 +1497,15 @@ interface ActiveAcpTurn {
1497
{
1498
readonly id: OrchestrationV2PlanArtifact["id"];
1499
readonly startedAt: DateTime.Utc;
1500
+ /** The turn that first reported the plan; later turns update its rows. */
1501
+ readonly origin: {
1502
+ readonly runId: ActiveAcpTurn["input"]["runId"];
1503
+ readonly rootNodeId: ActiveAcpTurn["input"]["rootNodeId"];
1504
+ readonly providerThreadId: ActiveAcpTurn["input"]["providerThread"]["id"];
1505
+ readonly providerTurnId: ActiveAcpTurn["providerTurnId"];
1506
+ readonly nativeItemId: string;
1507
+ readonly ordinal: number;
1508
+ };
1509
latest: OrchestrationV2PlanArtifact | null;
1510
}
1511
>;
@@ -2051,6 +2060,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
2060
new Map<string, OrchestrationV2ProviderThread>(),
2061
);
2062
const nativeBuildConfigurationBySessionId = new Map<string, AcpNativeBuildConfiguration>();
2063
+ const plansBySessionId = new Map<string, ActiveAcpTurn["plans"]>();
2064
const initialSessionActivationFailure = yield* Ref.make<{
2065
readonly sessionId: string;
2066
readonly error: EffectAcpErrors.AcpError;
@@ -4565,30 +4575,51 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
4575
const emitPlan = Effect.fnUntraced(function* (
4576
context: ActiveAcpTurn,
4577
update: AcpPlanUpdate,
4578
+ sessionScoped: boolean,
4579
) {
4580
yield* closeTextStreams(context);
4570
- const existing = context.plans.get(update.nativePlanId);
4581
+ let plans = sessionScoped ? plansBySessionId.get(context.nativeThreadId) : context.plans;
4582
+ if (plans === undefined) {
4583
+ plans = new Map();
4584
+ plansBySessionId.set(context.nativeThreadId, plans);
4585
+ }
4586
+ const existing = plans.get(update.nativePlanId);
4587
if (update.kind === "removed" && existing === undefined) return;
4572
- const nativeItemId = `${context.nativeTurnId}:plan:${encodeURIComponent(update.nativePlanId)}`;
4573
- const ordinal = yield* resolveItemOrdinal(context, nativeItemId);
4588
const now = yield* DateTime.now;
4575
- const nodeId = providerNodeId(nativeItemId);
4576
- const turnItemId = providerTurnItemId(nativeItemId);
4577
- const planState = existing ?? {
4578
- id: yield* idAllocator.allocate.plan({
4579
- threadId: context.input.threadId,
4580
- runId: context.input.runId,
4581
- driver,
4582
- }),
4583
- startedAt: now,
4584
- latest: null,
4585
- };
4589
+ let planState = existing;
4590
+ if (planState === undefined) {
4591
+ const nativeItemId = `${context.nativeTurnId}:plan:${encodeURIComponent(update.nativePlanId)}`;
4592
+ planState = {
4593
+ id: yield* idAllocator.allocate.plan({
4594
+ threadId: context.input.threadId,
4595
+ runId: context.input.runId,
4596
+ driver,
4597
+ }),
4598
+ startedAt: now,
4599
+ origin: {
4600
+ runId: context.input.runId,
4601
+ rootNodeId: context.input.rootNodeId,
4602
+ providerThreadId: context.input.providerThread.id,
4603
+ providerTurnId: context.providerTurnId,
4604
+ nativeItemId,
4605
+ ordinal: yield* resolveItemOrdinal(context, nativeItemId),
4606
+ },
4607
+ latest: null,
4608
+ };
4609
+ }
4610
+ const { origin } = planState;
4611
+ const nodeId = providerNodeId(origin.nativeItemId);
4612
+ const turnItemId = providerTurnItemId(origin.nativeItemId);
4613
const planId = planState.id;
4587
- const nativeItemRef = { driver, nativeId: nativeItemId, strength: "weak" as const };
4614
+ const nativeItemRef = {
4615
+ driver,
4616
+ nativeId: origin.nativeItemId,
4617
+ strength: "weak" as const,
4618
+ };
4619
const base = {
4620
id: planId,
4621
threadId: context.input.threadId,
4591
- runId: context.input.runId,
4622
+ runId: origin.runId,
4623
nodeId,
4624
} as const;
4625
let plan: OrchestrationV2PlanArtifact;
@@ -4639,7 +4670,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
4670
};
4671
}
4672
planState.latest = plan;
4642
- context.plans.set(update.nativePlanId, planState);
4673
+ plans.set(update.nativePlanId, planState);
4674
const completed = plan.status === "completed" || plan.status === "superseded";
4675
const nodeKind = plan.kind === "todo_list" ? "todo_list" : "plan";
4676
yield* emitProviderEvent({
@@ -4648,14 +4679,14 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
4679
node: {
4680
id: nodeId,
4681
threadId: context.input.threadId,
4651
- runId: context.input.runId,
4652
- parentNodeId: context.input.rootNodeId,
4653
- rootNodeId: context.input.rootNodeId,
4682
+ runId: origin.runId,
4683
+ parentNodeId: origin.rootNodeId,
4684
+ rootNodeId: origin.rootNodeId,
4685
kind: nodeKind,
4686
status: completed ? "completed" : "running",
4687
countsForRun: false,
4657
- providerThreadId: context.input.providerThread.id,
4658
- providerTurnId: context.providerTurnId,
4688
+ providerThreadId: origin.providerThreadId,
4689
+ providerTurnId: origin.providerTurnId,
4690
nativeItemRef,
4691
runtimeRequestId: null,
4692
checkpointScopeId: null,
@@ -4670,13 +4701,13 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
4701
turnItem: {
4702
id: turnItemId,
4703
threadId: context.input.threadId,
4673
- runId: context.input.runId,
4704
+ runId: origin.runId,
4705
nodeId,
4675
- providerThreadId: context.input.providerThread.id,
4676
- providerTurnId: context.providerTurnId,
4706
+ providerThreadId: origin.providerThreadId,
4707
+ providerTurnId: origin.providerTurnId,
4708
nativeItemRef,
4709
parentItemId: null,
4679
- ordinal,
4710
+ ordinal: origin.ordinal,
4711
status: completed ? "completed" : "running",
4712
title: null,
4713
startedAt: planState.startedAt,
@@ -5785,7 +5816,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
5816
if (event._tag === "ToolCallUpdated") {
5817
yield* emitTool(context, event.toolCall);
5818
} else if (event._tag === "PlanUpdated") {
5788
- yield* emitPlan(context, event.payload);
5819
+ yield* emitPlan(context, event.payload, update.sessionUpdate !== "plan");
5820
} else if (event._tag === "NoticeReceived") {
5821
yield* emitNotice(context, event);
5822
} else if (event._tag === "UnknownUpdate") {