@@ -8,14 +8,21 @@ import * as Schema from "effect/Schema";
88import { resolveAttachmentRelativePath } from "../attachmentPaths.ts" ;
99import { parseAttachmentFileExtension , resolveAttachmentPathById } from "../attachmentStore.ts" ;
1010import * as ServerConfig from "../config.ts" ;
11+ import * as ProjectionStore from "./ProjectionStore.ts" ;
1112import * as PreviewManager from "../preview/Manager.ts" ;
1213import * as ScheduledTaskService from "../scheduledTasks/ScheduledTaskService.ts" ;
1314import * as TerminalManager from "../terminal/Manager.ts" ;
1415
1516export 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) ;
0 commit comments