Skip to content
Merged
151 changes: 151 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2959,10 +2959,161 @@ describe("AcpAdapterV2", () => {
(error as ProviderAdapter.ProviderAdapterTurnStartError).cause,
ProviderAdapter.ProviderCompactionUnsupportedError,
);
assert.isTrue(
(error as ProviderAdapter.ProviderAdapterTurnStartError).refusedBeforePrompt,
);
}
}).pipe(Effect.provide(layerTest)),
);

// A start that cannot reopen its session may be on a session the agent lost. It is not a refusal
// before the prompt, so the server's recovery for an unusable session still runs.
it.live("does not mark a start whose session failed to reopen as refused before its prompt", () =>
Effect.gen(function* () {
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const selfInvocation = yield* resolveSelfInvocation();
const mockAgentPath = yield* bundledMockAgentPath;
const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
const instanceId = ProviderInstanceId.make("acp-test-reopen-failure");
const adapter = yield* makeAcpAdapterV2({
instanceId,
flavor: {
driver: ACP_TEST_DRIVER,
capabilities: AcpProviderCapabilitiesV2,
makeRuntime: makeMockRuntime({
childProcessSpawner,
mockAgentPath,
protocolEvents,
environment: () => ({ T3_ACP_FAIL_LOAD_SESSION: "1" }),
}),
},
selfInvocation,
});
const threadId = ThreadId.make("thread-acp-reopen-failure");
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
runtimeMode: "full-access",
interactionMode: "default",
cwd: process.cwd(),
});
const modelSelection = { instanceId, model: "default" } as const;
const runtime = yield* adapter.openSession({
threadId,
providerSessionId: ProviderSessionId.make("provider-session-acp-reopen-failure"),
modelSelection,
runtimePolicy,
});
const thread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
const lostThread: OrchestrationV2ProviderThread = {
...thread,
nativeThreadRef: { driver: ACP_TEST_DRIVER, nativeId: "lost-session", strength: "strong" },
};
const now = yield* DateTime.now;
const error = yield* runtime
.startTurn(
makeTurnInput({
threadId,
providerThread: lostThread,
instanceId,
runtimePolicy,
now,
ordinal: 1,
}),
)
.pipe(Effect.flip);
assert.instanceOf(error, ProviderAdapter.ProviderAdapterTurnStartError);
assert.notEqual(
(error as ProviderAdapter.ProviderAdapterTurnStartError).refusedBeforePrompt,
true,
);
const methods = yield* pollProtocolMethods(protocolEvents);
assert.isTrue(methods.includes("session/load") || methods.includes("session/resume"));
assert.notInclude(methods, "session/prompt");
}).pipe(Effect.provide(layerTest)),
);

// Only refusals that leave the session usable mark a start as refused before its prompt. A setup
// step other than the model can fail because the session is gone.
it.live("does not mark a start whose session setup failed as refused before its prompt", () =>
Effect.gen(function* () {
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const mockAgentPath = yield* bundledMockAgentPath;
const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
const instanceId = ProviderInstanceId.make("acp-test-setup-failure");
const threadId = ThreadId.make("thread-acp-setup-failure");
const adapter = yield* makeAcpAdapterV2({
instanceId,
flavor: {
driver: ACP_TEST_DRIVER,
capabilities: AcpProviderCapabilitiesV2,
makeRuntime: makeMockRuntime({
childProcessSpawner,
mockAgentPath,
protocolEvents,
environment: { T3_ACP_FAIL_SET_MODE_CONFIG_OPTION: "1" },
}),
},
selfInvocation: yield* resolveSelfInvocation(),
});
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
runtimeMode: "full-access",
interactionMode: "default",
cwd: process.cwd(),
});
const opened = { instanceId, model: "composer-2" } as const;
const withMode = {
instanceId,
model: "composer-2",
options: [{ id: ACP_SESSION_MODE_OPTION_ID, value: "code" }],
} as const;
const runtime = yield* adapter.openSession({
threadId,
providerSessionId: ProviderSessionId.make("provider-session-acp-setup-failure"),
modelSelection: opened,
runtimePolicy,
});
const providerThread = yield* runtime.ensureThread({
threadId,
modelSelection: opened,
runtimePolicy,
});
const now = yield* DateTime.now;
const error = yield* runtime
.startTurn(
makeTurnInput({
threadId,
providerThread,
instanceId,
runtimePolicy,
now,
ordinal: 1,
modelSelection: withMode,
}),
)
.pipe(Effect.flip);
assert.instanceOf(error, ProviderAdapter.ProviderAdapterTurnStartError);
assert.notInstanceOf(
(error as ProviderAdapter.ProviderAdapterTurnStartError).cause,
ProviderAdapter.ProviderCompactionUnsupportedError,
);
assert.notEqual(
(error as ProviderAdapter.ProviderAdapterTurnStartError).refusedBeforePrompt,
true,
);
// The start reached the mode step the agent rejected, and sent no prompt.
const outgoing = Array.from(yield* Queue.takeAll(protocolEvents)).filter(
(event) => event.direction === "outgoing",
);
assert.isTrue(
outgoing.some(
(event) =>
rawProtocolMethod(event) === "session/set_config_option" &&
rawProtocolRequestParam(event, "configId") === "mode",
),
);
assert.notInclude(outgoing.map(rawProtocolMethod), "session/prompt");
}).pipe(Effect.provide(layerTest)),
);

it.live("reapplies a selection after a switch away from it failed partway", () =>
Effect.gen(function* () {
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/orchestration-v2/ProviderTurnStartService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export function abandonedProviderTurns(

export const isProviderTurnStartError = Schema.is(ProviderTurnStartError);
const isProviderAdapterProtocolError = Schema.is(ProviderAdapter.ProviderAdapterProtocolError);
const isProviderAdapterTurnStartError = Schema.is(ProviderAdapter.ProviderAdapterTurnStartError);
const isProviderAdapterOpenSessionError = Schema.is(
ProviderAdapter.ProviderAdapterOpenSessionError,
);
Expand All @@ -160,6 +161,7 @@ const PROMPT_REFUSAL_TAGS = [

const refusedBeforePrompt = (error: unknown): boolean =>
PROMPT_REFUSAL_TAGS.some((tag) => Predicate.isTagged(error, tag)) ||
(isProviderAdapterTurnStartError(error) && error.refusedBeforePrompt === true) ||
(isProviderAdapterProtocolError(error) &&
error.driver === CLAUDE_PROVIDER &&
error.detail === CLAUDE_QUERY_ENDED_BEFORE_TURN_DETAIL) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ProviderSessionId,
ProviderThreadId,
ProviderTurnId,
RunId,
ScheduledTaskId,
ThreadId,
TurnItemId,
Expand Down Expand Up @@ -2794,6 +2795,17 @@ describe("orchestration v2 provider switching", () => {
detail: "Claude provider query ended before the turn could start.",
}),
],
// ACP rejects a model it cannot select before sending the prompt.
[
"an ACP start that rejected the model",
new ProviderAdapter.ProviderAdapterTurnStartError({
driver: GROK_DRIVER,
threadId,
providerThreadId: ProviderThreadId.make("provider-thread:acp-refused-start"),
runId: RunId.make("run:acp-refused-start"),
refusedBeforePrompt: true,
}),
],
// Injected history is text only, so a refusal after injection must not settle the image.
[
"background work after native history injection",
Expand Down
16 changes: 16 additions & 0 deletions packages/provider-acp/src/server/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,19 @@ class AcpModelSelectionError extends Schema.TaggedError<AcpModelSelectionError>(
}
}

const isAcpModelSelectionError = Schema.is(AcpModelSelectionError);
const isProviderCompactionUnsupportedError = Schema.is(
ProviderAdapter.ProviderCompactionUnsupportedError,
);

/**
* Turn-start refusals that leave the session usable. Other start failures, such as a session that
* cannot be reopened or a setup step that fails, can mean the agent lost the session, so the
* server keeps its recovery for them.
*/
const refusedWithSessionIntact = (cause: unknown) =>
isAcpModelSelectionError(cause) || isProviderCompactionUnsupportedError(cause);

/** A prompt-steered turn spans several prompts, each reporting its own usage. */
function addAcpUsage(
total: EffectAcpSchema.Usage | undefined,
Expand Down Expand Up @@ -10056,6 +10069,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
providerThreadId: turnInput.providerThread.id,
runId: turnInput.runId,
cause,
// The prompt is sent from a forked fiber, and its failures settle the turn
// instead of reaching here, so no prompt was sent when this fails.
...(refusedWithSessionIntact(cause) ? { refusedBeforePrompt: true } : {}),
}),
),
),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Expand Down
2 changes: 2 additions & 0 deletions packages/provider-core/src/server/ProviderAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ export class ProviderAdapterTurnStartError extends Schema.TaggedError<ProviderAd
providerThreadId: ProviderThreadId,
runId: RunId,
cause: Schema.optional(Schema.Defect()),
/** The agent received no part of the turn, so context staged for it is still unsent. */
refusedBeforePrompt: Schema.optional(Schema.Boolean),
},
) {
override get message(): string {
Expand Down
Loading
Loading