Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions apps/server/scripts/acp-mock-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1414,10 +1414,6 @@ const program = Effect.gen(function* () {
}
}

if (failPrompt || promptCount === failPromptOrdinal) {
return yield* AcpError.AcpRequestError.internalError("Mock prompt failure");
}

if (emitTrackedToolCallKindThenHang !== undefined) {
yield* agent.client.sessionUpdate({
sessionId: requestedSessionId,
Expand All @@ -1430,7 +1426,11 @@ const program = Effect.gen(function* () {
rawInput: { command: "git status" },
},
});
return yield* Effect.never;
if (!failPrompt && promptCount !== failPromptOrdinal) return yield* Effect.never;
}

if (failPrompt || promptCount === failPromptOrdinal) {
return yield* AcpError.AcpRequestError.internalError("Mock prompt failure");
}

if (hangPromptForever || (emitEmptySuccessfulBashThenHang && promptCount === 2)) {
Expand Down
47 changes: 31 additions & 16 deletions apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5207,28 +5207,35 @@ describe("AcpAdapterV2", () => {
}).pipe(Effect.provide(layerTest)),
);

it.effect.each([false, true])(
"ends a running tool call when its agent process exits mid-prompt (followAgentWorkState: %s)",
(followAgentWorkState) =>
it.effect.each(
[false, true].flatMap((followAgentWorkState) => [
{ ending: "agent process exits mid-prompt", followAgentWorkState },
{ ending: "prompt returns an RPC error", followAgentWorkState },
]),
)(
"ends an open tool call when its $ending (followAgentWorkState: $followAgentWorkState)",
({ ending, followAgentWorkState }) =>
Effect.gen(function* () {
const agentExits = ending === "agent process exits mid-prompt";
const harness = yield* makePromptInactivityHarness({
followAgentWorkState,
environment: (runtimeOrdinal) =>
runtimeOrdinal === 1 ? { T3_ACP_EMIT_TRACKED_TOOL_CALL_KIND_THEN_HANG: "execute" } : {},
runtimeOrdinal === 1
? {
T3_ACP_EMIT_TRACKED_TOOL_CALL_KIND_THEN_HANG: "execute",
...(agentExits ? {} : { T3_ACP_FAIL_PROMPT: "1" }),
}
: {},
});
yield* harness.startTurn(1);
let exited = false;
const statuses = yield* harness.runtime.events.pipe(
let exited = !agentExits;
const events = yield* harness.runtime.events.pipe(
Stream.takeUntil((event) => event.type === "turn.terminal"),
Stream.flatMap((event) =>
event.type === "turn_item.updated" &&
event.turnItem.nativeItemRef?.nativeId === "tracked-tool-call"
? Stream.make(event.turnItem.status)
: Stream.empty,
),
// The agent dies while its tool call is open.
Stream.tap(() =>
exited
Stream.tap((event) =>
exited ||
event.type !== "turn_item.updated" ||
event.turnItem.nativeItemRef?.nativeId !== "tracked-tool-call"
? Effect.void
: Effect.sync(() => (exited = true)).pipe(
Effect.andThen(harness.natives[0]!.notify("_test/exit", {})),
Expand All @@ -5237,8 +5244,16 @@ describe("AcpAdapterV2", () => {
Stream.runCollect,
Effect.map((collected) => Array.from(collected)),
);
// regression: #37368 the dead agent's tool call stayed open in the failed turn.
assert.include(["failed", "interrupted", "cancelled"], statuses.at(-1));
const toolStatuses = events.flatMap((event) =>
event.type === "turn_item.updated" &&
event.turnItem.nativeItemRef?.nativeId === "tracked-tool-call"
? [event.turnItem.status]
: [],
);
// regression: #37368 the tool call stayed open in the failed turn.
assert.equal(toolStatuses.at(-1), "failed");
const terminal = events.at(-1);
assert.equal(terminal?.type === "turn.terminal" ? terminal.status : undefined, "failed");
}).pipe(Effect.provide(layerTest)),
);

Expand Down
12 changes: 8 additions & 4 deletions packages/provider-acp/src/server/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7957,10 +7957,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
const terminalizeOpenForegroundTools = Effect.fnUntraced(function* (
context: ActiveAcpTurn,
status: "completed" | "failed" = "completed",
options?: { readonly unstartedOnly?: boolean },
) {
for (const tool of context.tools.values()) {
if (!acpCompletedTurnShouldTerminalizeTool(tool, { ...flavor, extractSubagentUpdate }))
continue;
if (options?.unstartedOnly === true && tool.status !== "pending") continue;
yield* emitTool(context, tool, status);
}
});
Expand Down Expand Up @@ -8973,12 +8975,14 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
if (!interrupted && flavor.restartRuntimeAfterPromptError?.(failure) === true) {
yield* Ref.set(runtimeRestartRequired, true);
}
// An agent that answered with an error is still alive and may finish its
// tools; any other failure means its process or pipe is gone.
// An agent that answered with an error may still finish tools it started,
// never pending ones; any other failure means its process or pipe is gone.
const agentAnswered =
Result.isSuccess(error) && error.success._tag === "AcpRequestError";
if (!interrupted && !agentAnswered) {
yield* terminalizeOpenForegroundTools(context, "failed");
if (!interrupted) {
yield* terminalizeOpenForegroundTools(context, "failed", {
unstartedOnly: agentAnswered,
});
}
yield* finalizeTurn(
context,
Expand Down
Loading