@devries-cameron20 / t3code-1 / commits / 68a564ef69
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): ACP follow-ups sent while the agent is still busy are delivered, not failed (#35463)

Julius Marminge committed Nov 12, 2026 at 10:15 UTC 68a564ef699cb4e46efca7b469c0a850da2f6722
2 files changed +130 -2
apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
+97 -1
@@ -521,6 +521,10 @@ const isProviderAdapterProtocolError = Schema.is(ProviderAdapter.ProviderAdapter
521 const makePromptInactivityHarness = Effect.fnUntraced(function* (input: {
522 readonly environment: (runtimeOrdinal: number) => Readonly<Record<string, string>>;
523 readonly runtimeMode?: "full-access" | "approval-required";
524 + readonly wrapPrompt?: (
525 + prompt: AcpSessionRuntime.AcpSessionRuntime["Service"]["prompt"],
526 + ) => AcpSessionRuntime.AcpSessionRuntime["Service"]["prompt"];
527 + readonly cancelled?: Deferred.Deferred<void>;
528 }) {
529 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
530 const path = yield* Path.Path;
@@ -540,7 +544,14 @@ const makePromptInactivityHarness = Effect.fnUntraced(function* (input: {
544 environment: input.environment,
545 wrapRuntime: (runtime) => {
546 natives.push(runtime);
543 - return runtime;
547 + return {
548 + ...runtime,
549 + prompt: input.wrapPrompt?.(runtime.prompt) ?? runtime.prompt,
550 + cancel:
551 + input.cancelled === undefined
552 + ? runtime.cancel
553 + : Deferred.succeed(input.cancelled, undefined).pipe(Effect.andThen(runtime.cancel)),
554 + };
555 },
556 }),
557 },
@@ -571,6 +582,7 @@ const makePromptInactivityHarness = Effect.fnUntraced(function* (input: {
582 return {
583 runtime,
584 natives,
585 + providerThread,
586 startTurn,
587 /** A turn still running makes the next start fail, without waiting on time. */
588 nextTurnRejected: (ordinal: number) =>
@@ -4097,6 +4109,90 @@ describe("AcpAdapterV2", () => {
4109 }).pipe(Effect.provide(layerTest), Effect.scoped),
4110 );
4111
4112 + it.effect("delivers a follow-up the agent rejects while it is still processing", () =>
4113 + Effect.gen(function* () {
4114 + let promptCalls = 0;
4115 + const rejected = yield* Deferred.make<void>();
4116 + const harness = yield* makePromptInactivityHarness({
4117 + environment: () => ({}),
4118 + // pi-agent-core based agents (oh-my-pi) end a turn but keep background work running.
4119 + wrapPrompt: (prompt) => (payload, options) => {
4120 + promptCalls += 1;
4121 + return promptCalls === 2
4122 + ? Deferred.succeed(rejected, undefined).pipe(
4123 + Effect.andThen(
4124 + Effect.fail(
4125 + EffectAcpErrors.AcpRequestError.internalError(
4126 + "Agent is already processing. Use steer() or followUp() to queue messages, or wait for completion.",
4127 + ),
4128 + ),
4129 + ),
4130 + )
4131 + : prompt(payload, options);
4132 + },
4133 + });
4134 + yield* harness.startTurn(1);
4135 + const [first] = yield* harness.terminals(1);
4136 + assert.equal(first?.status, "completed");
4137 +
4138 + const followUp = yield* harness.terminals(1).pipe(Effect.forkScoped);
4139 + yield* harness.startTurn(2);
4140 + yield* Deferred.await(rejected);
4141 + yield* TestClock.adjust("1 minute");
4142 + const [delivered] = yield* Fiber.join(followUp);
4143 + assert.equal(delivered?.status, "completed");
4144 + assert.equal(promptCalls, 3);
4145 + }).pipe(Effect.provide(layerTest), Effect.scoped),
4146 + );
4147 +
4148 + it.effect("sends no more prompts once a follow-up waiting on a busy agent is stopped", () =>
4149 + Effect.gen(function* () {
4150 + let promptCalls = 0;
4151 + const rejected = yield* Deferred.make<void>();
4152 + const cancelled = yield* Deferred.make<void>();
4153 + const harness = yield* makePromptInactivityHarness({
4154 + environment: () => ({}),
4155 + cancelled,
4156 + wrapPrompt: (prompt) => (payload, options) => {
4157 + promptCalls += 1;
4158 + return promptCalls === 1
4159 + ? prompt(payload, options)
4160 + : Deferred.succeed(rejected, undefined).pipe(
4161 + Effect.andThen(
4162 + Effect.fail(
4163 + EffectAcpErrors.AcpRequestError.internalError(
4164 + "Agent is already processing. Use steer() or followUp() to queue messages, or wait for completion.",
4165 + ),
4166 + ),
4167 + ),
4168 + );
4169 + },
4170 + });
4171 + yield* harness.startTurn(1);
4172 + yield* harness.terminals(1);
4173 +
4174 + const stopped = yield* harness.terminals(1).pipe(Effect.forkScoped);
4175 + yield* harness.startTurn(2);
4176 + yield* Deferred.await(rejected);
4177 + const providerTurnId = (yield* IdAllocator.IdAllocatorV2).derive.providerTurn({
4178 + driver: ACP_TEST_DRIVER,
4179 + nativeTurnId: acpScopedNativeId(
4180 + ProviderInstanceId.make("acp-prompt-inactivity"),
4181 + "mock-session-1:turn:2",
4182 + ),
4183 + });
4184 + const interrupt = yield* harness.runtime
4185 + .interruptTurn({ providerThread: harness.providerThread, providerTurnId })
4186 + .pipe(Effect.forkScoped);
4187 + yield* Deferred.await(cancelled);
4188 + yield* TestClock.adjust("1 second");
4189 + const [terminal] = yield* Fiber.join(stopped);
4190 + yield* Fiber.join(interrupt);
4191 + assert.equal(terminal?.status, "interrupted");
4192 + assert.equal(promptCalls, 2);
4193 + }).pipe(Effect.provide(layerTest), Effect.scoped),
4194 + );
4195 +
4196 it.effect("holds the inactivity timeout while the agent waits on an approval", () =>
4197 Effect.gen(function* () {
4198 const harness = yield* makePromptInactivityHarness({
packages/provider-acp/src/server/adapter.ts
+33 -1
@@ -143,6 +143,36 @@ export const ACP_PROTOCOL = "acp.ndjson-jsonrpc" as const;
143
144 // Grok summaries can trail hydrated tools by over two seconds; shorter waits split the wake.
145 const ACP_DEFERRED_FINALIZE_DEBOUNCE: Duration.Input = "3000 millis";
146 +const ACP_BUSY_PROMPT_RETRY_INTERVAL: Duration.Input = "1 second";
147 +
148 +// pi-agent-core agents (oh-my-pi) end a turn while background work runs and reject
149 +// prompts until it finishes; the prompt is resent rather than failing the turn.
150 +function isAgentBusyPromptError(error: EffectAcpErrors.AcpError): boolean {
151 + return (
152 + error._tag === "AcpRequestError" && error.errorMessage.startsWith("Agent is already processing")
153 + );
154 +}
155 +
156 +function sendPromptWhenIdle<A>(
157 + turn: { readonly interrupted: boolean; readonly finalized: boolean },
158 + prompt: () => Effect.Effect<A, EffectAcpErrors.AcpError>,
159 +): Effect.Effect<A, EffectAcpErrors.AcpError> {
160 + // A Stop can land during the wait, so the flags are rechecked before each resend.
161 + const stopped = () => turn.interrupted || turn.finalized;
162 + return prompt().pipe(
163 + Effect.catchIf(
164 + (error) => isAgentBusyPromptError(error) && !stopped(),
165 + (error) =>
166 + Effect.sleep(ACP_BUSY_PROMPT_RETRY_INTERVAL).pipe(
167 + Effect.andThen(
168 + Effect.suspend(() =>
169 + stopped() ? Effect.fail(error) : sendPromptWhenIdle(turn, prompt),
170 + ),
171 + ),
172 + ),
173 + ),
174 + );
175 +}
176
177 /**
178 * Emit first chunks and segment-close events immediately; coalesce later chunks into full-text snapshots.
@@ -8411,7 +8441,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
8441 : undefined;
8442 yield* (
8443 compactSession === undefined
8414 - ? runtime.prompt({ prompt: promptParts!.prompt }, promptOptions)
8444 + ? sendPromptWhenIdle(context, () =>
8445 + runtime.prompt({ prompt: promptParts!.prompt }, promptOptions),
8446 + )
8447 : Deferred.succeed(context.promptDispatched, undefined).pipe(
8448 Effect.andThen(compactSession(runtime, requestedSessionId)),
8449 Effect.as({ stopReason: "end_turn" } satisfies EffectAcpSchema.PromptResponse),