Skip to content

Commit c481a9e

Browse files
test(server): cover a stop during the deletion's sweep and the open-terminal wait
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 86847d0 commit c481a9e

1 file changed

Lines changed: 81 additions & 1 deletion

File tree

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

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ const cleanupFixture = (
273273
let editBeforeRemoval = false;
274274
let openSessionCwd: string | null = null;
275275
let sessionLive = true;
276+
let onLiveCheck: Effect.Effect<void> = Effect.void;
276277
let outboxStatus: string | null = null;
277278
let fullStatusCalls = 0;
278279
let snapshotReads = 0;
@@ -342,7 +343,8 @@ const cleanupFixture = (
342343
Effect.sync(() => threads.find((thread) => thread.id === threadId) ?? null),
343344
} as unknown as ProjectionStore.ProjectionStoreV2["Service"]),
344345
Effect.provideService(ProviderSessionManager.ProviderSessionManagerV2, {
345-
isLive: () => Effect.sync(() => sessionLive),
346+
isLive: () =>
347+
Effect.sync(() => sessionLive).pipe(Effect.tap(() => Effect.suspend(() => onLiveCheck))),
346348
} as unknown as ProviderSessionManager.ProviderSessionManagerV2["Service"]),
347349
Effect.provideService(Orchestrator.OrchestratorV2, {
348350
streamDomainEvents: Stream.suspend(() => events),
@@ -438,6 +440,9 @@ const cleanupFixture = (
438440
setThreads: (next: typeof threads) => {
439441
threads = next;
440442
},
443+
setOnLiveCheck: (hook: Effect.Effect<void>) => {
444+
onLiveCheck = hook;
445+
},
441446
setOutboxStatus: (status: string | null) => {
442447
outboxStatus = status;
443448
},
@@ -1095,6 +1100,7 @@ describe("deleted thread worktrees", () => {
10951100
});
10961101
yield* fixture.setTerminals([]);
10971102
}),
1103+
fixture.repo,
10981104
);
10991105
expect(reads).toBeGreaterThan(0);
11001106
expect(yield* fixture.fs.exists(fixture.worktree)).toBe(false);
@@ -1186,6 +1192,80 @@ describe("deleted thread worktrees", () => {
11861192
}).pipe(Effect.provide(cleanupTestLayer), Effect.scoped),
11871193
);
11881194

1195+
it.effect(
1196+
"removes a deleted thread's worktree when its session stops during the deletion's sweep",
1197+
() =>
1198+
Effect.gen(function* () {
1199+
yield* TestClock.setTime(NOW_MS);
1200+
const fixture = yield* cleanupFixture({
1201+
...DEFAULT_SERVER_SETTINGS.storageCleanup,
1202+
worktreeAfterDays: null,
1203+
worktreeOnDelete: true,
1204+
});
1205+
const owner = parent(fixture.worktree);
1206+
fixture.setThreads([]);
1207+
const startupSwept = yield* Deferred.make<void>();
1208+
const sessionChecked = yield* Deferred.make<void>();
1209+
const delivered = yield* Deferred.make<void>();
1210+
const deleted = {
1211+
id: EventId.make("event:thread-deleted"),
1212+
type: "thread.deleted",
1213+
threadId: owner.id,
1214+
occurredAt: at(0),
1215+
payload: owner,
1216+
} as unknown as OrchestrationV2DomainEvent;
1217+
const stopped: OrchestrationV2DomainEvent = {
1218+
id: EventId.make("event:session-stopped"),
1219+
type: "provider-session.updated",
1220+
threadId: owner.id,
1221+
occurredAt: at(0),
1222+
payload: {
1223+
id: ProviderSessionId.make("session-1"),
1224+
driver: ProviderDriverKind.make("codex"),
1225+
providerInstanceId: ProviderInstanceId.make("codex"),
1226+
status: "stopped",
1227+
cwd: fixture.worktree,
1228+
model: null,
1229+
capabilities: CodexProviderCapabilitiesV2,
1230+
createdAt: at(-DAY_MS),
1231+
updatedAt: at(0),
1232+
lastError: null,
1233+
},
1234+
};
1235+
yield* fixture.activate(
1236+
Stream.fromEffect(Deferred.await(startupSwept)).pipe(
1237+
Stream.flatMap(() => Stream.make(deleted)),
1238+
Stream.concat(Stream.fromEffect(Deferred.await(sessionChecked)).pipe(Stream.drain)),
1239+
Stream.concat(Stream.make(stopped)),
1240+
Stream.concat(
1241+
Stream.fromEffect(Deferred.succeed(delivered, undefined)).pipe(Stream.drain),
1242+
),
1243+
),
1244+
);
1245+
yield* fixture.service.reports.pipe(
1246+
Stream.filter((report) => report !== null),
1247+
Stream.take(1),
1248+
Stream.runDrain,
1249+
);
1250+
yield* fixture.service.drain;
1251+
fixture.setDeletedThreads([owner]);
1252+
fixture.setOpenSessionCwd(fixture.worktree);
1253+
// The deletion's sweep sees the session live; it stops before that sweep publishes its result.
1254+
fixture.setOnLiveCheck(
1255+
Effect.gen(function* () {
1256+
fixture.setOnLiveCheck(Effect.void);
1257+
yield* Deferred.succeed(sessionChecked, undefined);
1258+
yield* Deferred.await(delivered);
1259+
fixture.setSessionLive(false);
1260+
}),
1261+
);
1262+
yield* Deferred.succeed(startupSwept, undefined);
1263+
yield* Deferred.await(delivered);
1264+
yield* fixture.service.drain;
1265+
expect(yield* fixture.fs.exists(fixture.worktree)).toBe(false);
1266+
}).pipe(Effect.provide(cleanupTestLayer), Effect.scoped),
1267+
);
1268+
11891269
it.effect("waits out the settle grace of a subagent that just settled", () =>
11901270
Effect.gen(function* () {
11911271
yield* TestClock.setTime(NOW_MS);

0 commit comments

Comments
 (0)