Skip to content

Commit e946d30

Browse files
committed
fix(server): preserve background work and validate full handoff recovery
1 parent ec5a0cb commit e946d30

10 files changed

Lines changed: 450 additions & 164 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts‎

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,11 @@ describe("AcpAdapterV2", () => {
909909
flavor: {
910910
driver: ACP_TEST_DRIVER,
911911
capabilities: AcpProviderCapabilitiesV2,
912-
makeRuntime: makeMockRuntime({ childProcessSpawner, mockAgentPath, protocolEvents }),
912+
makeRuntime: makeMockRuntime({
913+
childProcessSpawner,
914+
mockAgentPath,
915+
protocolEvents,
916+
}),
913917
},
914918
selfInvocation,
915919
});
@@ -2324,6 +2328,99 @@ describe("AcpAdapterV2", () => {
23242328
}).pipe(Effect.provide(layerTest), Effect.scoped),
23252329
);
23262330

2331+
it.live("creates a fresh ACP identity when recovering an exhausted target", () =>
2332+
Effect.gen(function* () {
2333+
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
2334+
const path = yield* Path.Path;
2335+
const selfInvocation = yield* resolveSelfInvocation();
2336+
const mockAgentPath = yield* path.fromFileUrl(
2337+
new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
2338+
);
2339+
const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
2340+
const instanceId = ProviderInstanceId.make("acp-test-exhausted-target");
2341+
const adapter = yield* makeAcpAdapterV2({
2342+
instanceId,
2343+
flavor: {
2344+
driver: ACP_TEST_DRIVER,
2345+
capabilities: AcpProviderCapabilitiesV2,
2346+
makeRuntime: makeMockRuntime({
2347+
childProcessSpawner,
2348+
mockAgentPath,
2349+
protocolEvents,
2350+
wrapRuntime: (runtime, ordinal) => ({
2351+
...runtime,
2352+
start: () =>
2353+
runtime.start().pipe(
2354+
Effect.map((started) => ({
2355+
...started,
2356+
sessionId: `mock-recovery-session-${ordinal}`,
2357+
})),
2358+
),
2359+
}),
2360+
}),
2361+
},
2362+
selfInvocation,
2363+
});
2364+
const threadId = ThreadId.make("thread-acp-exhausted-target");
2365+
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
2366+
runtimeMode: "full-access",
2367+
interactionMode: "default",
2368+
cwd: process.cwd(),
2369+
});
2370+
const modelSelection = { instanceId, model: "default" } as const;
2371+
const runtime = yield* adapter.openSession({
2372+
threadId,
2373+
providerSessionId: ProviderSessionId.make("provider-session-acp-exhausted-target"),
2374+
modelSelection,
2375+
runtimePolicy,
2376+
});
2377+
const original = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
2378+
yield* runtime.startTurn(
2379+
makeTurnInput({
2380+
threadId,
2381+
providerThread: original,
2382+
instanceId,
2383+
runtimePolicy,
2384+
now: yield* DateTime.now,
2385+
ordinal: 1,
2386+
}),
2387+
);
2388+
yield* runtime.events.pipe(
2389+
Stream.takeUntil((event) => event.type === "turn.terminal"),
2390+
Stream.runDrain,
2391+
);
2392+
const replacement = yield* runtime.ensureThread({
2393+
threadId,
2394+
modelSelection,
2395+
runtimePolicy,
2396+
existingProviderThread: { ...original, nativeThreadRef: null },
2397+
forceNewNativeThread: true,
2398+
});
2399+
assert.notEqual(replacement.nativeThreadRef?.nativeId, original.nativeThreadRef?.nativeId);
2400+
assert.equal(replacement.id, original.id);
2401+
assert.equal(replacement.nativeMetadata?.itemIdentityVersion, 2);
2402+
yield* runtime.startTurn(
2403+
makeTurnInput({
2404+
threadId,
2405+
providerThread: replacement,
2406+
instanceId,
2407+
runtimePolicy,
2408+
now: yield* DateTime.now,
2409+
ordinal: 2,
2410+
}),
2411+
);
2412+
const events = yield* runtime.events.pipe(
2413+
Stream.takeUntil((event) => event.type === "turn.terminal"),
2414+
Stream.runCollect,
2415+
);
2416+
assert.isTrue(
2417+
events.some((event) => event.type === "turn.terminal" && event.status === "completed"),
2418+
);
2419+
const methods = yield* pollProtocolMethods(protocolEvents);
2420+
assert.equal(methods.filter((method) => method === "session/new").length, 2);
2421+
}).pipe(Effect.provide(layerTest), Effect.scoped),
2422+
);
2423+
23272424
it.live("preserves new-session fallback when an eager ACP session load is stale", () =>
23282425
Effect.gen(function* () {
23292426
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
@@ -5158,6 +5255,19 @@ describe("AcpAdapterV2", () => {
51585255
yield* hasPendingBackgroundWork,
51595256
"carryover live subagent must pin hasPendingBackgroundWork after root settle",
51605257
);
5258+
const refusedReplacement = yield* runtime
5259+
.ensureThread({
5260+
threadId,
5261+
modelSelection,
5262+
runtimePolicy,
5263+
existingProviderThread: { ...providerThread, nativeThreadRef: null },
5264+
forceNewNativeThread: true,
5265+
})
5266+
.pipe(Effect.flip);
5267+
assert.equal(refusedReplacement._tag, "ProviderAdapterEnsureThreadError");
5268+
const preserved = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
5269+
assert.equal(preserved.nativeThreadRef?.nativeId, providerThread.nativeThreadRef?.nativeId);
5270+
assert.isTrue(yield* hasPendingBackgroundWork);
51615271
assert.isDefined(sessionUpdateHandler, "session update handler must be wired");
51625272

51635273
subagentPhase = "complete";

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7652,7 +7652,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
76527652
),
76537653
);
76547654

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

7684+
const replacementAttempt = yield* harness.runtime
7685+
.startTurn(
7686+
makeClaudeTestTurnInput({
7687+
threadId: harness.threadId,
7688+
providerThread: {
7689+
...harness.providerThread,
7690+
nativeThreadRef: {
7691+
...harness.providerThread.nativeThreadRef!,
7692+
nativeId: "replacement-session",
7693+
},
7694+
},
7695+
now,
7696+
attemptId: RunAttemptId.make("attempt-claude-background-replacement"),
7697+
text: "Continue in a fresh session.",
7698+
attachments: [],
7699+
providerTurnOrdinal: 2,
7700+
}),
7701+
)
7702+
.pipe(Effect.result);
7703+
assert.equal(replacementAttempt._tag, "Failure");
7704+
assert.equal(closes, 0);
7705+
assert.isTrue(yield* harness.hasPendingBackgroundWork);
7706+
76847707
yield* harness.runtime.interruptTurn({
76857708
providerThread: settledThread ?? harness.providerThread,
76867709
providerTurnId: harness.terminalEvents()[0]!.providerTurnId,

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9697,10 +9697,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
96979697
return existing;
96989698
}
96999699

9700-
// Selection changes kill CLI background work; refuse until it finishes or Stop closes the process.
9700+
// Replacing this thread's process kills its background work, even with a new native identity.
97019701
if (
97029702
existing !== null &&
9703-
existing.nativeThreadId === nativeThreadId &&
9703+
(existing.nativeThreadId === nativeThreadId ||
9704+
existing.lastTurn?.input.threadId === turnInput.threadId) &&
97049705
!existing.stopping &&
97059706
(yield* liveProcessRunsBackgroundWork(existing))
97069707
) {

0 commit comments

Comments
 (0)