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
112 changes: 111 additions & 1 deletion apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,11 @@ describe("AcpAdapterV2", () => {
flavor: {
driver: ACP_TEST_DRIVER,
capabilities: AcpProviderCapabilitiesV2,
makeRuntime: makeMockRuntime({ childProcessSpawner, mockAgentPath, protocolEvents }),
makeRuntime: makeMockRuntime({
childProcessSpawner,
mockAgentPath,
protocolEvents,
}),
},
selfInvocation,
});
Expand Down Expand Up @@ -2324,6 +2328,99 @@ describe("AcpAdapterV2", () => {
}).pipe(Effect.provide(layerTest), Effect.scoped),
);

it.live("creates a fresh ACP identity when recovering an exhausted target", () =>
Effect.gen(function* () {
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const path = yield* Path.Path;
const selfInvocation = yield* resolveSelfInvocation();
const mockAgentPath = yield* path.fromFileUrl(
new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
);
const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
const instanceId = ProviderInstanceId.make("acp-test-exhausted-target");
const adapter = yield* makeAcpAdapterV2({
instanceId,
flavor: {
driver: ACP_TEST_DRIVER,
capabilities: AcpProviderCapabilitiesV2,
makeRuntime: makeMockRuntime({
childProcessSpawner,
mockAgentPath,
protocolEvents,
wrapRuntime: (runtime, ordinal) => ({
...runtime,
start: () =>
runtime.start().pipe(
Effect.map((started) => ({
...started,
sessionId: `mock-recovery-session-${ordinal}`,
})),
),
}),
}),
},
selfInvocation,
});
const threadId = ThreadId.make("thread-acp-exhausted-target");
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-exhausted-target"),
modelSelection,
runtimePolicy,
});
const original = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
yield* runtime.startTurn(
makeTurnInput({
threadId,
providerThread: original,
instanceId,
runtimePolicy,
now: yield* DateTime.now,
ordinal: 1,
}),
);
yield* runtime.events.pipe(
Stream.takeUntil((event) => event.type === "turn.terminal"),
Stream.runDrain,
);
const replacement = yield* runtime.ensureThread({
threadId,
modelSelection,
runtimePolicy,
existingProviderThread: { ...original, nativeThreadRef: null },
forceNewNativeThread: true,
});
assert.notEqual(replacement.nativeThreadRef?.nativeId, original.nativeThreadRef?.nativeId);
assert.equal(replacement.id, original.id);
assert.equal(replacement.nativeMetadata?.itemIdentityVersion, 2);
yield* runtime.startTurn(
makeTurnInput({
threadId,
providerThread: replacement,
instanceId,
runtimePolicy,
now: yield* DateTime.now,
ordinal: 2,
}),
);
const events = yield* runtime.events.pipe(
Stream.takeUntil((event) => event.type === "turn.terminal"),
Stream.runCollect,
);
assert.isTrue(
events.some((event) => event.type === "turn.terminal" && event.status === "completed"),
);
const methods = yield* pollProtocolMethods(protocolEvents);
assert.equal(methods.filter((method) => method === "session/new").length, 2);
}).pipe(Effect.provide(layerTest), Effect.scoped),
);

it.live("preserves new-session fallback when an eager ACP session load is stale", () =>
Effect.gen(function* () {
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
Expand Down Expand Up @@ -5158,6 +5255,19 @@ describe("AcpAdapterV2", () => {
yield* hasPendingBackgroundWork,
"carryover live subagent must pin hasPendingBackgroundWork after root settle",
);
const refusedReplacement = yield* runtime
.ensureThread({
threadId,
modelSelection,
runtimePolicy,
existingProviderThread: { ...providerThread, nativeThreadRef: null },
forceNewNativeThread: true,
})
.pipe(Effect.flip);
assert.equal(refusedReplacement._tag, "ProviderAdapterEnsureThreadError");
const preserved = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
assert.equal(preserved.nativeThreadRef?.nativeId, providerThread.nativeThreadRef?.nativeId);
assert.isTrue(yield* hasPendingBackgroundWork);
assert.isDefined(sessionUpdateHandler, "session update handler must be wired");

subagentPhase = "complete";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7652,7 +7652,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
),
);

it.effect("stops background work after the turn settled", () =>
it.effect("preserves settled background work until an explicit stop", () =>
Effect.scoped(
Effect.gen(function* () {
let closes = 0;
Expand Down Expand Up @@ -7681,6 +7681,29 @@ describe("ClaudeAdapterV2 background wake turns", () => {
assert.equal(settledThread?.pendingBackgroundTasks?.[0]?.taskId, WAKE_TASK_ID);
assert.isTrue(yield* harness.hasPendingBackgroundWork);

const replacementAttempt = yield* harness.runtime
.startTurn(
makeClaudeTestTurnInput({
threadId: harness.threadId,
providerThread: {
...harness.providerThread,
nativeThreadRef: {
...harness.providerThread.nativeThreadRef!,
nativeId: "replacement-session",
},
},
now,
attemptId: RunAttemptId.make("attempt-claude-background-replacement"),
text: "Continue in a fresh session.",
attachments: [],
providerTurnOrdinal: 2,
}),
)
.pipe(Effect.result);
assert.equal(replacementAttempt._tag, "Failure");
assert.equal(closes, 0);
assert.isTrue(yield* harness.hasPendingBackgroundWork);

yield* harness.runtime.interruptTurn({
providerThread: settledThread ?? harness.providerThread,
providerTurnId: harness.terminalEvents()[0]!.providerTurnId,
Expand Down Expand Up @@ -12108,6 +12131,25 @@ describe("ClaudeAdapterV2 background wake turns", () => {
yield* changes.until(() => terminals().length === 1);
const settledTurn = terminals()[0]!;

assert.isFalse(yield* runtime.hasPendingBackgroundWorkForThread!(providerThread));
assert.isTrue(
yield* runtime.hasPendingBackgroundWorkForThread!(providerThread, {
includeSubagents: true,
}),
);
assert.isFalse(
yield* runtime.hasPendingBackgroundWorkForThread!(
{
...providerThread,
nativeThreadRef: {
...providerThread.nativeThreadRef!,
nativeId: "sibling-native-session",
},
},
{ includeSubagents: true },
),
);

const alternateModel = CLAUDE_TEST_RESTART_MODEL_SELECTION;
const switchTurn = (attempt: string) =>
runtime.startTurn(
Expand Down
16 changes: 13 additions & 3 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9697,10 +9697,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
return existing;
}

// Selection changes kill CLI background work; refuse until it finishes or Stop closes the process.
// Replacing this thread's process kills its background work, even with a new native identity.
if (
existing !== null &&
existing.nativeThreadId === nativeThreadId &&
(existing.nativeThreadId === nativeThreadId ||
existing.lastTurn?.input.threadId === turnInput.threadId) &&
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
!existing.stopping &&
(yield* liveProcessRunsBackgroundWork(existing))
) {
Expand Down Expand Up @@ -10565,11 +10566,20 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
}),
hasPendingBackgroundWorkForThread: Effect.fn(
"ClaudeAdapterV2.hasPendingBackgroundWorkForThread",
)(function* (providerThread) {
)(function* (providerThread, options) {
const nativeThreadId = providerThread.nativeThreadRef?.nativeId;
if (nativeThreadId === undefined || nativeThreadId === null) {
return false;
}
if (options?.includeSubagents) {
const live = yield* Ref.get(queryContext);
if (
live?.nativeThreadId === nativeThreadId &&
!live.stopping &&
(yield* liveProcessRunsBackgroundWork(live))
)
return true;
}
return (
rosterForNativeThread(
yield* Ref.get(pendingBackgroundTasksByNativeThread),
Expand Down
Loading
Loading