Skip to content

Commit 77ab499

Browse files
only21milclaude
andcommitted
test(server): a hung native session releases the ACP import lock
The importer test now runs the import inside the real ACP session-mutation lock. When the session never answers, the import times out, the session closes, a waiting import or delete gets the lock, and a later import repairs the thread. The ACP session RPC test checks that the timeout reaches the client as session_import_failed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 62458d6 commit 77ab499

2 files changed

Lines changed: 70 additions & 16 deletions

File tree

‎apps/server/src/project/AgentSessionImporter.test.ts‎

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from "@t3tools/contracts";
1616
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
1717
import * as KeyedLock from "@t3tools/shared/KeyedLock";
18+
import * as AcpRegistryRuntimeCoordinator from "@t3tools/provider-acp-registry/server/AcpRegistryRuntimeCoordinator";
1819
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
1920
import * as Context from "effect/Context";
2021
import * as DateTime from "effect/DateTime";
@@ -530,33 +531,80 @@ it.effect(
530531
}).pipe(Effect.scoped, Effect.provide(persistence)),
531532
);
532533

533-
it.effect("gives up on a native session that never answers and closes it", () =>
534+
it.effect("releases the ACP import lock when a native session never answers", () =>
534535
Effect.gen(function* () {
535536
const projections = yield* ProjectionStore.ProjectionStoreV2;
537+
const coordinator = yield* AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator;
536538
const threadId = yield* seedOwner(candidate(CODEX_MODEL_SELECTION, "native-history-hang"));
539+
const opened = yield* Deferred.make<void>();
537540
let closed = false;
538-
// An agent that only resumes sessions and never answers session/resume.
541+
// An agent that only resumes sessions and never answers its first session/resume.
539542
const adapter = {
540543
openSession: () =>
541-
Effect.addFinalizer(() =>
542-
Effect.sync(() => {
543-
closed = true;
544-
}),
545-
).pipe(Effect.andThen(Effect.never)),
544+
Effect.suspend(() => {
545+
if (!closed) {
546+
return Effect.addFinalizer(() =>
547+
Effect.sync(() => {
548+
closed = true;
549+
}),
550+
).pipe(
551+
Effect.andThen(Deferred.succeed(opened, undefined)),
552+
Effect.andThen(Effect.never),
553+
);
554+
}
555+
return Effect.succeed({
556+
readThreadSnapshot: ({ providerThread }) =>
557+
Effect.succeed({
558+
providerThread,
559+
providerTurns: [],
560+
runtimeRequests: [],
561+
messages: [
562+
{
563+
createdBy: "user",
564+
creationSource: "provider",
565+
id: MessageId.make("replayed:hang"),
566+
threadId,
567+
runId: null,
568+
nodeId: null,
569+
role: "user",
570+
text: "Fix the build",
571+
attachments: [],
572+
streaming: false,
573+
createdAt: now,
574+
updatedAt: now,
575+
},
576+
],
577+
}),
578+
} as Pick<ProviderAdapter.ProviderAdapterV2SessionRuntime, "readThreadSnapshot">);
579+
}),
546580
} as unknown as ProviderAdapter.ProviderAdapterV2["Service"];
547581
const importer = yield* makeImporter([], Effect.void, () => Effect.succeed(adapter));
582+
const importHistory = coordinator.withSessionMutation(
583+
importer.importNativeHistory({ threadId, workspaceRoot: "/workspace/project" }),
584+
);
548585

549-
const importing = yield* importer
550-
.importNativeHistory({ threadId, workspaceRoot: "/workspace/project" })
551-
.pipe(Effect.flip, Effect.forkChild);
586+
const importing = yield* importHistory.pipe(Effect.flip, Effect.forkChild);
587+
yield* Deferred.await(opened);
588+
// Any other ACP import or delete waits for the same lock.
589+
const next = yield* coordinator
590+
.withSessionMutation(Effect.succeed("next"))
591+
.pipe(Effect.forkChild);
552592
yield* TestClock.adjust("2 minutes");
553593

554594
expect((yield* Fiber.join(importing))._tag).toBe("TimeoutError");
555595
expect(closed).toBe(true);
596+
expect(yield* Fiber.join(next)).toBe("next");
556597
expect(
557598
(yield* projections.getThreadRecords(threadId, [])).thread.historyOrigin,
558599
).toBeUndefined();
559-
}).pipe(Effect.scoped, Effect.provide(persistence)),
600+
expect(yield* importHistory).toBe(1);
601+
expect((yield* projections.getThreadRecords(threadId, [])).thread.historyOrigin).toBe(
602+
"v1_import",
603+
);
604+
}).pipe(
605+
Effect.scoped,
606+
Effect.provide(Layer.merge(persistence, AcpRegistryRuntimeCoordinator.layer)),
607+
),
560608
);
561609

562610
/**

‎apps/server/src/ws.test.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
3030
import type { ProviderInstance } from "@t3tools/provider-core/server/driver";
3131
import * as DateTime from "effect/DateTime";
32+
import * as Cause from "effect/Cause";
3233
import * as Deferred from "effect/Deferred";
3334
import * as Effect from "effect/Effect";
3435
import * as Layer from "effect/Layer";
@@ -1096,6 +1097,7 @@ it.effect("ACP session RPCs list a thread missing its history and repair it on i
10961097
const complete = yield* seedAcpThread("complete", { imported: true, ran: false });
10971098
const ran = yield* seedAcpThread("ran", { imported: false, ran: true });
10981099
const failing = yield* seedAcpThread("failing", { imported: false, ran: false });
1100+
const timingOut = yield* seedAcpThread("timing-out", { imported: false, ran: false });
10991101
const repaired: Array<ThreadId> = [];
11001102
const session = (sessionId: string) => ({
11011103
sessionId,
@@ -1166,7 +1168,9 @@ it.effect("ACP session RPCs list a thread missing its history and repair it on i
11661168
importNativeHistory: ({ threadId }) =>
11671169
threadId === failing
11681170
? Effect.fail(new Orchestrator.OrchestratorProjectionError({ threadId, cause: "read" }))
1169-
: Effect.sync(() => repaired.push(threadId)).pipe(Effect.as(1)),
1171+
: threadId === timingOut
1172+
? Effect.fail(new Cause.TimeoutError())
1173+
: Effect.sync(() => repaired.push(threadId)).pipe(Effect.as(1)),
11701174
}),
11711175
);
11721176
const client = yield* RpcTest.makeClient(acpSessionRpcs).pipe(
@@ -1216,10 +1220,12 @@ it.effect("ACP session RPCs list a thread missing its history and repair it on i
12161220
});
12171221
assert.deepEqual(yield* importSession("pending"), { threadId: pending, imported: true });
12181222
assert.deepEqual(repaired, [pending]);
1219-
const failure = yield* importSession("failing").pipe(Effect.flip);
1220-
assert.equal(failure._tag, "AcpRegistryOperationError");
1221-
if (failure._tag === "AcpRegistryOperationError") {
1222-
assert.equal(failure.reason, "session_import_failed");
1223+
for (const sessionId of ["failing", "timing-out"]) {
1224+
const failure = yield* importSession(sessionId).pipe(Effect.flip);
1225+
assert.equal(failure._tag, "AcpRegistryOperationError");
1226+
if (failure._tag === "AcpRegistryOperationError") {
1227+
assert.equal(failure.reason, "session_import_failed");
1228+
}
12231229
}
12241230
}).pipe(
12251231
Effect.provide(

0 commit comments

Comments
 (0)