Skip to content

Commit d7900dc

Browse files
shivamhwpclaude
andcommitted
fix(server): decide a fork family's file cleanup after the deletion commits
When the last two members of a fork family were deleted at the same moment, each planned under its own thread lock, saw the other as live, and scheduled no cleanup, so the family's files were never removed. Thread deletion now always queues a thread-attachments.cleanup effect. The effect worker runs it after the commit, reads the family again, and removes every member's files only when none is live. Whichever deletion commits last therefore cleans up, and repeated runs are harmless. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8dd1ace commit d7900dc

13 files changed

Lines changed: 240 additions & 234 deletions

‎apps/server/src/guest/GuestShareService.test.ts‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,30 @@ const layerCleanup = Layer.effect(
9696
Effect.gen(function* () {
9797
const { attachmentsDir } = yield* ServerConfig.ServerConfig;
9898
const fileSystem = yield* FileSystem.FileSystem;
99+
const projections = yield* ProjectionStore.ProjectionStoreV2;
100+
const cleanupAttachments = (attachmentIds: ReadonlyArray<string>) =>
101+
Effect.forEach(
102+
attachmentIds,
103+
(attachmentId) => {
104+
const path = resolveAttachmentPathById({ attachmentsDir, attachmentId });
105+
return path === null ? Effect.void : fileSystem.remove(path).pipe(Effect.orDie);
106+
},
107+
{ discard: true },
108+
);
99109
return {
100110
cleanupScheduledTasks: () => Effect.void,
111+
cleanupThreadAttachments: (threadId) =>
112+
projections
113+
.getDeletableAttachmentIds(threadId)
114+
.pipe(Effect.orDie, Effect.flatMap(cleanupAttachments)),
101115
cleanupTerminals: () => Effect.void,
102116
cleanupPreviews: () => Effect.void,
103-
cleanupAttachments: (attachmentIds) =>
104-
Effect.forEach(
105-
attachmentIds,
106-
(attachmentId) => {
107-
const path = resolveAttachmentPathById({ attachmentsDir, attachmentId });
108-
return path === null ? Effect.void : fileSystem.remove(path).pipe(Effect.orDie);
109-
},
110-
{ discard: true },
111-
),
117+
cleanupAttachments,
112118
};
113119
}),
114120
).pipe(
115121
Layer.provide(layerConfig),
122+
Layer.provide(ProjectionStore.layer.pipe(Layer.provide(layerDatabase))),
116123
Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
117124
);
118125
class RecordedAnalytics extends Context.Service<

‎apps/server/src/orchestration-v2/EffectOutbox.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ export const OrchestrationEffectRequestV2 = Schema.Union([
108108
type: Schema.Literal("attachment.cleanup"),
109109
attachmentIds: Schema.Array(Schema.String),
110110
}),
111+
// Decided when it runs: concurrent deletions each see the other live while planning.
112+
Schema.Struct({
113+
type: Schema.Literal("thread-attachments.cleanup"),
114+
}),
111115
Schema.Struct({
112116
type: Schema.Literal("thread-title.generate"),
113117
kind: Schema.Union([
@@ -131,6 +135,7 @@ export const REPLAY_SAFE_EFFECT_TYPES_AFTER_PROCESS_LOSS = [
131135
"terminal.cleanup",
132136
"preview.cleanup",
133137
"attachment.cleanup",
138+
"thread-attachments.cleanup",
134139
"thread-title.generate",
135140
"delegated-tasks.stop",
136141
] as const satisfies ReadonlyArray<OrchestrationEffectRequestV2["type"]>;

‎apps/server/src/orchestration-v2/EffectWorker.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,17 @@ export const layerExecutor: Layer.Layer<
538538
}),
539539
),
540540
);
541+
case "thread-attachments.cleanup":
542+
return resourceCleanup.cleanupThreadAttachments(effect.threadId).pipe(
543+
Effect.mapError(
544+
(cause) =>
545+
new OrchestrationEffectExecutionError({
546+
effectId: effect.id,
547+
effectType: effect.request.type,
548+
cause,
549+
}),
550+
),
551+
);
541552
case "thread-title.generate":
542553
return threadTitleRegeneration
543554
.execute({

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13409,7 +13409,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1340913409
const now = yield* DateTime.now;
1341013410
const planDeletion = Effect.fn("Orchestrator.planDeletion")(function* (
1341113411
deletion: typeof command,
13412-
attachmentIds: ReadonlyArray<string>,
1341313412
) {
1341413413
const projection = yield* projectionStore
1341513414
.getThreadRecords(deletion.threadId, [
@@ -13429,7 +13428,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1342913428
planThreadDeletion({
1343013429
command: deletion,
1343113430
projection,
13432-
attachmentIds,
1343313431
now,
1343413432
idAllocator,
1343513433
}),
@@ -13449,21 +13447,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1344913447
}
1345013448
}
1345113449
// The dispatch wrapper holds each side chat's lock, so none can commit after this plan.
13452-
// Side chats share the parent's fork family, so the parent's plan carries its files.
13453-
const attachmentIds = yield* projectionStore
13454-
.getDeletableAttachmentIds([command.threadId, ...sideChatIds])
13455-
.pipe(mapDispatchError(command));
1345613450
const plans = [
13457-
yield* planDeletion(command, attachmentIds),
13451+
yield* planDeletion(command),
1345813452
...(yield* Effect.forEach(sideChatIds, (threadId) =>
13459-
planDeletion(
13460-
{
13461-
...command,
13462-
commandId: CommandId.make(`${command.commandId}:side-chat:${threadId}`),
13463-
threadId,
13464-
},
13465-
[],
13466-
),
13453+
planDeletion({
13454+
...command,
13455+
commandId: CommandId.make(`${command.commandId}:side-chat:${threadId}`),
13456+
threadId,
13457+
}),
1346713458
)),
1346813459
];
1346913460
return {

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,11 @@ export interface ProjectionStoreV2Shape {
452452
threadId: ThreadId,
453453
) => Effect.Effect<ReadonlyArray<string>, ProjectionStoreV2Error>;
454454
/**
455-
* Files to remove when `threadIds` are deleted together. Forks show their source's history, so
456-
* a fork family (threads sharing `lineage.rootThreadId`) keeps every member's files until no
457-
* member outside `threadIds` is live.
455+
* Every file of this thread's fork family (threads sharing `lineage.rootThreadId`) once no
456+
* member is live, else none: forks show their source's history without copying its files.
458457
*/
459458
readonly getDeletableAttachmentIds: (
460-
threadIds: ReadonlyArray<ThreadId>,
459+
threadId: ThreadId,
461460
) => Effect.Effect<ReadonlyArray<string>, ProjectionStoreV2Error>;
462461
readonly getThreadHistoryPage: (
463462
threadId: ThreadId,
@@ -5624,31 +5623,20 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
56245623
);
56255624

56265625
const getDeletableAttachmentIds = Effect.fn("ProjectionStore.getDeletableAttachmentIds")(
5627-
function* (threadIds: ReadonlyArray<ThreadId>) {
5626+
function* (threadId: ThreadId) {
56285627
const members = yield* sql<{ thread_id: string }>`
5629-
WITH deleting AS (SELECT value AS thread_id FROM json_each(${encodeIdList(threadIds)})),
5630-
members AS (
5631-
SELECT member.thread_id, member.deleted_at,
5632-
json_extract(member.payload_json, '$.lineage.rootThreadId') AS root_thread_id
5628+
WITH members AS (
5629+
SELECT member.thread_id, member.deleted_at
56335630
FROM orchestration_v2_projection_threads AS member
5634-
WHERE json_extract(member.payload_json, '$.lineage.rootThreadId') IN (
5631+
WHERE json_extract(member.payload_json, '$.lineage.rootThreadId') = (
56355632
SELECT json_extract(thread.payload_json, '$.lineage.rootThreadId')
56365633
FROM orchestration_v2_projection_threads AS thread
5637-
WHERE thread.thread_id IN (SELECT thread_id FROM deleting)
5634+
WHERE thread.thread_id = ${threadId}
56385635
)
56395636
)
5640-
SELECT member.thread_id FROM members AS member
5641-
WHERE NOT EXISTS (
5642-
SELECT 1 FROM members AS live
5643-
WHERE live.root_thread_id = member.root_thread_id
5644-
AND live.deleted_at IS NULL
5645-
AND live.thread_id NOT IN (SELECT thread_id FROM deleting)
5646-
)
5647-
`.pipe(
5648-
Effect.mapError(
5649-
(cause) => new ProjectionStoreReadError({ threadId: threadIds[0]!, cause }),
5650-
),
5651-
);
5637+
SELECT thread_id FROM members
5638+
WHERE NOT EXISTS (SELECT 1 FROM members WHERE deleted_at IS NULL)
5639+
`.pipe(Effect.mapError((cause) => new ProjectionStoreReadError({ threadId, cause })));
56525640
const ids = yield* Effect.forEach(members, (member) =>
56535641
getThreadAttachmentIds(ThreadId.make(member.thread_id)),
56545642
);

‎apps/server/src/orchestration-v2/ResourceCleanupService.ts‎

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,21 @@ import * as Schema from "effect/Schema";
88
import { resolveAttachmentRelativePath } from "../attachmentPaths.ts";
99
import { parseAttachmentFileExtension, resolveAttachmentPathById } from "../attachmentStore.ts";
1010
import * as ServerConfig from "../config.ts";
11+
import * as ProjectionStore from "./ProjectionStore.ts";
1112
import * as PreviewManager from "../preview/Manager.ts";
1213
import * as ScheduledTaskService from "../scheduledTasks/ScheduledTaskService.ts";
1314
import * as TerminalManager from "../terminal/Manager.ts";
1415

1516
export class ResourceCleanupError extends Schema.TaggedError<ResourceCleanupError>()(
1617
"ResourceCleanupError",
1718
{
18-
operation: Schema.Literals(["terminal", "preview", "attachment", "scheduled-tasks"]),
19+
operation: Schema.Literals([
20+
"terminal",
21+
"preview",
22+
"attachment",
23+
"thread-attachments",
24+
"scheduled-tasks",
25+
]),
1926
threadId: Schema.optional(Schema.String),
2027
attachmentId: Schema.optional(Schema.String),
2128
cause: Schema.Defect(),
@@ -29,12 +36,17 @@ export class ResourceCleanupService extends Context.Reference<{
2936
readonly cleanupAttachments: (
3037
attachmentIds: ReadonlyArray<string>,
3138
) => Effect.Effect<void, ResourceCleanupError>;
39+
/** Removes a deleted thread's fork family's files once no member is live; idempotent. */
40+
readonly cleanupThreadAttachments: (
41+
threadId: ThreadId,
42+
) => Effect.Effect<void, ResourceCleanupError>;
3243
}>("t3/orchestration-v2/ResourceCleanupService", {
3344
defaultValue: () => ({
3445
cleanupScheduledTasks: () => Effect.void,
3546
cleanupTerminals: () => Effect.void,
3647
cleanupPreviews: () => Effect.void,
3748
cleanupAttachments: () => Effect.void,
49+
cleanupThreadAttachments: () => Effect.void,
3850
}),
3951
}) {}
4052

@@ -46,6 +58,38 @@ export const layer = Layer.effect(
4658
const fileSystem = yield* FileSystem.FileSystem;
4759
const config = yield* ServerConfig.ServerConfig;
4860
const scheduledTasks = yield* ScheduledTaskService.ScheduledTaskService;
61+
const projections = yield* ProjectionStore.ProjectionStoreV2;
62+
const cleanupAttachments = Effect.fn("ResourceCleanupService.cleanupAttachments")(function (
63+
attachmentIds: ReadonlyArray<string>,
64+
) {
65+
return Effect.forEach(
66+
attachmentIds,
67+
(attachmentId) => {
68+
const extension = parseAttachmentFileExtension(attachmentId);
69+
const path =
70+
resolveAttachmentPathById({
71+
attachmentsDir: config.attachmentsDir,
72+
attachmentId,
73+
}) ??
74+
(extension
75+
? resolveAttachmentRelativePath({
76+
attachmentsDir: config.attachmentsDir,
77+
relativePath: `${attachmentId}.${extension}`,
78+
})
79+
: null);
80+
return path === null
81+
? Effect.void
82+
: fileSystem.remove(path, { force: true }).pipe(
83+
Effect.andThen(fileSystem.remove(`${path}.txt`, { force: true })),
84+
Effect.mapError(
85+
(cause) =>
86+
new ResourceCleanupError({ operation: "attachment", attachmentId, cause }),
87+
),
88+
);
89+
},
90+
{ discard: true, concurrency: 4 },
91+
);
92+
});
4993
return {
5094
cleanupScheduledTasks: Effect.fn("ResourceCleanupService.cleanupScheduledTasks")(function (
5195
threadId: ThreadId,
@@ -81,37 +125,20 @@ export const layer = Layer.effect(
81125
),
82126
);
83127
}),
84-
cleanupAttachments: Effect.fn("ResourceCleanupService.cleanupAttachments")(function (
85-
attachmentIds: ReadonlyArray<string>,
86-
) {
87-
return Effect.forEach(
88-
attachmentIds,
89-
(attachmentId) => {
90-
const extension = parseAttachmentFileExtension(attachmentId);
91-
const path =
92-
resolveAttachmentPathById({
93-
attachmentsDir: config.attachmentsDir,
94-
attachmentId,
95-
}) ??
96-
(extension
97-
? resolveAttachmentRelativePath({
98-
attachmentsDir: config.attachmentsDir,
99-
relativePath: `${attachmentId}.${extension}`,
100-
})
101-
: null);
102-
return path === null
103-
? Effect.void
104-
: fileSystem.remove(path, { force: true }).pipe(
105-
Effect.andThen(fileSystem.remove(`${path}.txt`, { force: true })),
106-
Effect.mapError(
107-
(cause) =>
108-
new ResourceCleanupError({ operation: "attachment", attachmentId, cause }),
109-
),
110-
);
111-
},
112-
{ discard: true, concurrency: 4 },
113-
);
114-
}),
128+
cleanupAttachments,
129+
cleanupThreadAttachments: Effect.fn("ResourceCleanupService.cleanupThreadAttachments")(
130+
function* (threadId: ThreadId) {
131+
const attachmentIds = yield* projections
132+
.getDeletableAttachmentIds(threadId)
133+
.pipe(
134+
Effect.mapError(
135+
(cause) =>
136+
new ResourceCleanupError({ operation: "thread-attachments", threadId, cause }),
137+
),
138+
);
139+
yield* cleanupAttachments(attachmentIds);
140+
},
141+
),
115142
};
116143
}),
117144
);

‎apps/server/src/orchestration-v2/ThreadDeletion.test.ts‎

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,6 @@ it.effect("cancels active work without reviving a run while disposing delegated
182182
const plan = yield* planThreadDeletion({
183183
command,
184184
projection,
185-
attachmentIds: projection.messages.flatMap((message) =>
186-
message.attachments.map((attachment) => attachment.id),
187-
),
188185
now: deletedAt,
189186
idAllocator: yield* IdAllocator.IdAllocatorV2,
190187
});
@@ -243,35 +240,10 @@ it.effect("queues provider and resource cleanup and preserves an earlier deletio
243240
updatedAt: createdAt,
244241
lastError: null,
245242
})),
246-
messages: [0, 1].map((index) => ({
247-
id: MessageId.make(`message:attachment:${index}`),
248-
threadId,
249-
createdBy: "user",
250-
creationSource: "web",
251-
runId: null,
252-
nodeId: null,
253-
role: "user",
254-
text: "Inspect this file",
255-
attachments: [
256-
{
257-
type: "file",
258-
id: "shared_file",
259-
name: "input.txt",
260-
mimeType: "text/plain",
261-
sizeBytes: 10,
262-
},
263-
],
264-
streaming: false,
265-
createdAt,
266-
updatedAt: createdAt,
267-
})),
268243
};
269244
const plan = yield* planThreadDeletion({
270245
command,
271246
projection,
272-
attachmentIds: projection.messages.flatMap((message) =>
273-
message.attachments.map((attachment) => attachment.id),
274-
),
275247
now: deletedAt,
276248
idAllocator: yield* IdAllocator.IdAllocatorV2,
277249
});
@@ -293,7 +265,7 @@ it.effect("queues provider and resource cleanup and preserves an earlier deletio
293265
{ type: "scheduled-tasks.cleanup" },
294266
{ type: "terminal.cleanup" },
295267
{ type: "preview.cleanup" },
296-
{ type: "attachment.cleanup", attachmentIds: ["shared_file"] },
268+
{ type: "thread-attachments.cleanup" },
297269
],
298270
);
299271
}).pipe(Effect.provide(IdAllocator.layer)),

‎apps/server/src/orchestration-v2/ThreadDeletion.ts‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export const planThreadDeletion = Effect.fn("ThreadDeletion.planThreadDeletion")
2020
OrchestrationV2ThreadProjection,
2121
"thread" | "runs" | "attempts" | "nodes" | "runtimeRequests" | "subagents" | "providerSessions"
2222
>;
23-
readonly attachmentIds: ReadonlyArray<string>;
2423
readonly now: DateTime.Utc;
2524
readonly idAllocator: IdAllocator.IdAllocatorV2["Service"];
2625
}): Effect.fn.Return<ThreadDeletionPlan, IdAllocator.IdAllocatorV2Error> {
@@ -226,14 +225,11 @@ export const planThreadDeletion = Effect.fn("ThreadDeletion.planThreadDeletion")
226225
threadId: command.threadId,
227226
request: { type: "preview.cleanup" },
228227
});
229-
const attachmentIds = Array.from(new Set(input.attachmentIds));
230-
if (attachmentIds.length > 0) {
231-
effects.push({
232-
id: `effect:${command.commandId}:attachment.cleanup`,
233-
commandId: command.commandId,
234-
threadId: command.threadId,
235-
request: { type: "attachment.cleanup", attachmentIds },
236-
});
237-
}
228+
effects.push({
229+
id: `effect:${command.commandId}:thread-attachments.cleanup`,
230+
commandId: command.commandId,
231+
threadId: command.threadId,
232+
request: { type: "thread-attachments.cleanup" },
233+
});
238234
return { events, effects };
239235
});

0 commit comments

Comments
 (0)