Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
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
303 changes: 303 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4192,6 +4192,309 @@ describe("AcpAdapterV2", () => {
}).pipe(Effect.provide(layerTest)),
);

// regression: #29291 Devin ends its prompt while a background shell runs and
// reports the shell's completion outside any turn.
const openDevinBackgroundShell = Effect.fnUntraced(function* (name: string) {
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const path = yield* Path.Path;
const mockAgentPath = yield* path.fromFileUrl(
new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
);
type Runtime = AcpSessionRuntime.AcpSessionRuntime["Service"];
let handler: Parameters<Runtime["handleSessionUpdate"]>[0] | undefined;
let createTerminal: Parameters<Runtime["handleCreateTerminal"]>[0] | undefined;
let waitForExit: Parameters<Runtime["handleTerminalWaitForExit"]>[0] | undefined;
const runtimeInputs: Array<AcpAdapterV2RuntimeInput> = [];
const terminalIds: Array<string> = [];
let heldPrompt:
| { readonly started: Deferred.Deferred<void>; readonly release: Deferred.Deferred<void> }
| undefined;
const baseMakeRuntime = makeMockRuntime({
childProcessSpawner,
mockAgentPath,
wrapRuntime: (runtime) => ({
...runtime,
handleSessionUpdate: (next) =>
Effect.sync(() => {
handler = next;
}).pipe(Effect.andThen(runtime.handleSessionUpdate(next))),
handleCreateTerminal: (next) =>
Effect.sync(() => {
createTerminal = next;
}).pipe(Effect.andThen(runtime.handleCreateTerminal(next))),
handleTerminalWaitForExit: (next) =>
Effect.sync(() => {
waitForExit = next;
}).pipe(Effect.andThen(runtime.handleTerminalWaitForExit(next))),
prompt: () =>
Effect.gen(function* () {
const toolCallId = `exec-${terminalIds.length + 1}`;
const { terminalId } = yield* createTerminal!(
{ sessionId: "mock-session-1", command: "sleep 30" },
{ requestId: `${toolCallId}-terminal`, method: "terminal/create" },
);
terminalIds.push(terminalId);
const updates = [
{
sessionUpdate: "tool_call",
toolCallId,
title: "exec",
kind: "execute",
status: "pending",
rawInput: { command: "sleep 30" },
content: [{ type: "terminal", terminalId }],
_meta: { "cognition.ai/inferenceToolName": "exec" },
},
{
sessionUpdate: "tool_call_update",
toolCallId,
status: "in_progress",
_meta: {
"cognition.ai/inferenceToolName": "exec",
"cognition.ai/background": true,
"cognition.ai/backgroundShellId": terminalId,
},
},
// Devin follows with meta that no longer carries the background flag.
{
sessionUpdate: "tool_call_update",
toolCallId,
status: "in_progress",
_meta: { "cognition.ai/inferenceToolName": "exec" },
},
{
sessionUpdate: "agent_message_chunk",
content: { type: "text", text: "The command is running in the background." },
},
] satisfies Array<EffectAcpSchema.SessionUpdate>;
for (const update of updates) yield* handler!({ sessionId: "mock-session-1", update });
if (heldPrompt !== undefined) {
yield* Deferred.succeed(heldPrompt.started, undefined);
yield* Deferred.await(heldPrompt.release);
}
return { stopReason: "end_turn" as const };
}),
}),
});
const instanceId = ProviderInstanceId.make(name);
const adapter = yield* makeAcpRegistryAdapterV2({
instanceId,
selfInvocation: yield* resolveSelfInvocation(),
settings: DEVIN_SETTINGS,
environment: {},
continuationRequests: { offer: () => Effect.void },
makeRuntime: (runtimeInput) =>
Effect.sync(() => {
runtimeInputs.push(runtimeInput);
}).pipe(Effect.andThen(baseMakeRuntime(runtimeInput))),
}).pipe(
Effect.provide(
Layer.mock(AcpRegistryCatalog)({ resolve: () => Effect.die("runtime is injected") }),
),
);
const threadId = ThreadId.make(name);
const modelSelection = { instanceId, model: "default" } as const;
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
runtimeMode: "full-access",
interactionMode: "default",
cwd: process.cwd(),
});
const sessionScope = yield* Scope.make();
yield* Effect.addFinalizer(() => Scope.close(sessionScope, Exit.void));
const runtime = yield* adapter
.openSession({
threadId,
providerSessionId: ProviderSessionId.make(`${name}-session`),
modelSelection,
runtimePolicy,
})
.pipe(Effect.provideService(Scope.Scope, sessionScope));
const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
const events = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
yield* runtime.events.pipe(
Stream.runForEach((event) => Queue.offer(events, event)),
Effect.forkScoped,
);
const until = Effect.fnUntraced(function* (
predicate: (event: ProviderAdapter.ProviderAdapterV2Event) => boolean,
) {
const seen: Array<ProviderAdapter.ProviderAdapterV2Event> = [];
while (true) {
const event = yield* Queue.take(events);
seen.push(event);
if (predicate(event)) return seen;
}
});
const execItems = (seen: ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event>, id: string) =>
seen.flatMap((event) =>
event.type === "turn_item.updated" && event.turnItem.nativeItemRef?.nativeId === id
? [event.turnItem]
: [],
);
const startTurn = Effect.fnUntraced(function* () {
yield* runtime.startTurn(
makeTurnInput({
threadId,
providerThread,
instanceId,
runtimePolicy,
now: yield* DateTime.now,
}),
);
});
// Leaves the next prompt open after its updates; returns the effect that ends it.
const startHeldTurn = Effect.fnUntraced(function* () {
const held = { started: yield* Deferred.make<void>(), release: yield* Deferred.make<void>() };
heldPrompt = held;
yield* startTurn();
yield* Deferred.await(held.started);
return Deferred.succeed(held.release, undefined).pipe(
Effect.andThen(until((event) => event.type === "turn.terminal")),
);
});
const runTurn = Effect.fnUntraced(function* () {
yield* startTurn();
const seen = yield* until((event) => event.type === "turn.terminal");
const terminal = seen.at(-1);
assert.equal(terminal?.type === "turn.terminal" ? terminal.status : undefined, "completed");
return {
seen,
providerTurnId: terminal?.type === "turn.terminal" ? terminal.providerTurnId : undefined,
};
});
// A usage update is projected immediately, so it marks everything emitted before it.
let flushes = 0;
const sendAndFlush = Effect.fnUntraced(function* (update?: EffectAcpSchema.SessionUpdate) {
if (update !== undefined) yield* handler!({ sessionId: "mock-session-1", update });
const used = ++flushes;
yield* handler!({
sessionId: "mock-session-1",
update: { sessionUpdate: "usage_update", used, size: 100 },
});
return yield* until(
(event) =>
event.type === "provider_thread.updated" &&
event.providerThread.contextUsage?.usedTokens === used,
);
});
return {
runtime,
providerThread,
sessionScope,
runtimeInputs,
terminalIds,
until,
execItems,
runTurn,
startHeldTurn,
send: (update: EffectAcpSchema.SessionUpdate) =>
handler!({ sessionId: "mock-session-1", update }),
sendAndFlush,
waitForExit: (terminalId: string) =>
waitForExit!(
{ sessionId: "mock-session-1", terminalId },
{ requestId: `${terminalId}-wait`, method: "terminal/wait_for_exit" },
),
};
});

it.effect("keeps a Devin background shell running until Devin reports it finished", () =>
Effect.gen(function* () {
const shell = yield* openDevinBackgroundShell("devin-background-shell");
const { seen } = yield* shell.runTurn();
const execUpdates = shell.execItems(seen, "exec-1");
assert.notInclude(
execUpdates.map((item) => item.status),
"completed",
"the background shell is still running",
);
assert.equal(execUpdates.at(-1)?.status, "running");
const finished = shell
.execItems(
yield* shell.sendAndFlush({
sessionUpdate: "tool_call_update",
toolCallId: "exec-1",
status: "completed",
rawOutput: "server exited",
_meta: { "cognition.ai/inferenceToolName": "exec" },
}),
"exec-1",
)
.at(-1);
assert.equal(finished?.status, "completed");
assert.equal(
finished?.type === "command_execution" ? finished.output : undefined,
"server exited",
);
assert.equal(finished?.id, execUpdates.at(-1)?.id);
assert.equal(finished?.runId, execUpdates.at(-1)?.runId);
assert.isFalse(yield* shell.runtime.hasPendingBackgroundWork!);
}).pipe(Effect.provide(layerTest), Effect.scoped),
);

it.effect("stops a settled Devin run's background shell and keeps it stopped", () =>
Effect.gen(function* () {
const shell = yield* openDevinBackgroundShell("devin-background-shell-stop");
const { providerTurnId } = yield* shell.runTurn();
// Later commands push the shell out of the bounded embedded-terminal lookup.
for (let index = 0; index < 256; index++) {
yield* shell.send({
sessionUpdate: "tool_call",
toolCallId: `later-${index}`,
title: "exec",
kind: "execute",
status: "completed",
content: [{ type: "terminal", terminalId: `later-terminal-${index}` }],
});
}
yield* shell.runtime.interruptTurn({
providerThread: shell.providerThread,
providerTurnId: providerTurnId!,
requestRuntimeRestart: true,
});
const stopped = shell.execItems(yield* shell.sendAndFlush(), "exec-1");
assert.deepEqual(
stopped.map((item) => item.status),
["interrupted"],
);
const exit = yield* shell.waitForExit(shell.terminalIds[0]!);
assert.notEqual(exit.exitCode, 0);
// The stopped shell's late report must not land on the next run.
const finishHeldTurn = yield* shell.startHeldTurn();
const late = yield* shell.sendAndFlush({
sessionUpdate: "tool_call_update",
toolCallId: "exec-1",
status: "completed",
_meta: { "cognition.ai/inferenceToolName": "exec" },
});
assert.deepEqual(shell.execItems(late, "exec-1"), []);
yield* finishHeldTurn;
}).pipe(Effect.provide(layerTest), Effect.scoped),
);

it.effect("ends carried Devin background shells when their session goes away", () =>
Effect.gen(function* () {
const shell = yield* openDevinBackgroundShell("devin-background-shell-session");
yield* shell.runTurn();
assert.isTrue(yield* shell.runtime.hasPendingBackgroundWork!);
yield* shell.runtimeInputs[0]!.onTermination!(
new EffectAcpErrors.AcpTransportError({ detail: "Injected termination", cause: "test" }),
);
const terminated = yield* shell.until(
(event) =>
event.type === "turn_item.updated" && event.turnItem.nativeItemRef?.nativeId === "exec-1",
);
assert.equal(shell.execItems(terminated, "exec-1").at(-1)?.status, "interrupted");
assert.isFalse(yield* shell.runtime.hasPendingBackgroundWork!);
const { seen } = yield* shell.runTurn();
assert.equal(shell.execItems(seen, "exec-2").at(-1)?.status, "running");
assert.isTrue(yield* shell.runtime.hasPendingBackgroundWork!);
const retired = yield* shell.runtime.retireBackgroundWork!;
assert.equal(shell.execItems(retired, "exec-2").at(-1)?.status, "interrupted");
assert.isFalse(yield* shell.runtime.hasPendingBackgroundWork!);
}).pipe(Effect.provide(layerTest), Effect.scoped),
);

it.effect("emits coalesced subagent text before a failed turn's terminal", () =>
Effect.gen(function* () {
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
Expand Down
Loading
Loading