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
42 changes: 41 additions & 1 deletion apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ const makePromptInactivityHarness = Effect.fnUntraced(function* (input: {
) => AcpSessionRuntime.AcpSessionRuntime["Service"]["prompt"];
readonly cancelled?: Deferred.Deferred<void>;
readonly followAgentWorkState?: boolean;
readonly sessionScope?: Scope.Scope;
}) {
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const natives: Array<AcpSessionRuntime.AcpSessionRuntime["Service"]> = [];
Expand Down Expand Up @@ -662,12 +663,15 @@ const makePromptInactivityHarness = Effect.fnUntraced(function* (input: {
cwd: process.cwd(),
});
const modelSelection = { instanceId, model: "default" } as const;
const runtime = yield* adapter.openSession({
const opened = adapter.openSession({
threadId,
providerSessionId: ProviderSessionId.make("provider-session-acp-prompt-inactivity"),
modelSelection,
runtimePolicy,
});
const runtime = yield* input.sessionScope === undefined
? opened
: opened.pipe(Effect.provideService(Scope.Scope, input.sessionScope));
const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
const startTurn = (ordinal: number) =>
DateTime.now.pipe(
Expand Down Expand Up @@ -5780,6 +5784,42 @@ describe("AcpAdapterV2", () => {
}).pipe(Effect.provide(layerTest)),
);

it.effect("ends a running T3 MCP tool call when its session closes mid-prompt", () =>
Effect.gen(function* () {
const sessionScope = yield* Scope.make();
const harness = yield* makePromptInactivityHarness({
sessionScope,
environment: () => ({ T3_ACP_EMIT_TRACKED_TOOL_CALL_KIND_THEN_HANG: "other" }),
});
const trackedToolStatus = (event: ProviderAdapter.ProviderAdapterV2Event) =>
event.type === "turn_item.updated" &&
event.turnItem.nativeItemRef?.nativeId === "tracked-tool-call"
? event.turnItem.status
: undefined;
yield* harness.startTurn(1);
yield* harness.runtime.events.pipe(
Stream.filter((event) => trackedToolStatus(event) !== undefined),
Stream.runHead,
);
// t3_worktree_handoff releases the live session while its own tool call is open.
yield* Scope.close(sessionScope, Exit.void);
// The event queue outlives the session; only events emitted by the close remain.
const after = yield* harness.runtime.events.pipe(
Stream.filter((event) => trackedToolStatus(event) !== undefined),
Stream.map(trackedToolStatus),
Stream.takeUntil((status) => status !== "pending" && status !== "running"),
Stream.runCollect,
Effect.map((collected) => Array.from(collected)),
Effect.timeoutOption("1 second"),
Effect.forkChild,
);
yield* TestClock.adjust("1 second");
const statuses = Option.getOrElse(yield* Fiber.join(after), () => []);
// regression: #39410 the handoff's tool call stayed running after the session closed.
assert.include(["completed", "failed", "interrupted", "cancelled"], statuses.at(-1));
}).pipe(Effect.provide(layerTest)),
);

it.live("does not poll the process ledger while a cgroup lease owns teardown", () =>
Effect.gen(function* () {
if ((yield* HostProcess.Platform) !== "linux") return;
Expand Down
6 changes: 5 additions & 1 deletion apps/server/src/orchestration-v2/RunExecutionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2055,7 +2055,11 @@ export const layer: Layer.Layer<
).pipe(Effect.as(false)),
),
);
if (handedOff) return;
if (handedOff) {
// Its tool calls, the handoff's own among them, died with that process.
yield* endWorkLostAfterRootRun(completedAt);
return;
}
}
yield* Effect.logWarning("orchestration V2 provider event ingestion failed", {
runId: input.run.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ProviderTurnId,
type RunId,
ThreadId,
TurnItemId,
} from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
Expand Down Expand Up @@ -462,19 +463,54 @@ it.live.each(["pooled", "exclusive"] as const)(
state,
mode === "pooled" ? pooledCapabilities : exclusiveCapabilities,
);
const handoffToolId = TurnItemId.make(`turn-item:workspace-handoff:${mode}:tool`);
// A session that closes on detach is never asked to interrupt: Claude's cannot answer.
// Its turn is inside the handoff's own tool call, which the closed session never ends.
const adapter: ProviderAdapter.ProviderAdapterV2["Service"] =
mode === "pooled"
? base
: {
...base,
openSession: (input) =>
base.openSession(input).pipe(
Effect.map((session) => ({
Effect.gen(function* () {
const session = yield* base.openSession(input);
const toolEvents =
yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
return {
...session,
events: Stream.merge(session.events, Stream.fromQueue(toolEvents)),
startTurn: (turn) =>
Effect.gen(function* () {
yield* session.startTurn(turn);
const now = yield* DateTime.now;
yield* Queue.offer(toolEvents, {
type: "turn_item.updated",
driver,
turnItem: {
id: handoffToolId,
type: "dynamic_tool",
threadId: turn.threadId,
runId: turn.runId,
nodeId: turn.rootNodeId,
providerThreadId: turn.providerThread.id,
providerTurnId: ProviderTurnId.make(`provider-turn:${turn.attemptId}`),
nativeItemRef: null,
parentItemId: null,
ordinal: 1,
status: "running",
title: "t3_worktree_handoff",
toolName: "t3-code.t3_worktree_handoff",
input: {},
output: null,
startedAt: now,
completedAt: null,
updatedAt: now,
},
});
}),
interruptTurn: () => Effect.die("an exclusive handoff must not interrupt"),
})),
),
};
}),
};
const layerRegistry = ProviderAdapterRegistry.layerSingle(adapter);

Expand Down Expand Up @@ -585,6 +621,13 @@ it.live.each(["pooled", "exclusive"] as const)(
),
"an intentional workspace handoff must not project an interrupt result or a failure",
);
if (mode === "exclusive") {
// regression: #39410 the handoff's tool call stayed running after its session closed.
assert.equal(
projection.turnItems.find((item) => item.id === handoffToolId)?.status,
"interrupted",
);
}
assert.deepEqual(
(yield* Ref.get(state)).started.map((turn) => turn.cwd),
[cwd, handoffCwd],
Expand Down
6 changes: 6 additions & 0 deletions packages/provider-acp/src/server/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9637,6 +9637,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
yield* Ref.update(continuationGeneration, (value) => value + 1);
}),
);
// A session released mid-prompt (worktree handoff) never sees its prompt settle.
const openTurn = yield* Ref.get(activeTurn);
if (openTurn !== null && !openTurn.finalized) {
openTurn.interrupted = true;
yield* finalizeTurn(openTurn, "interrupted");
}
const requests = [...(yield* Ref.get(pendingRuntimeRequests)).values()];
yield* Effect.forEach(
requests,
Expand Down
Loading