@@ -43,6 +43,35 @@ type TaskRegistryDatabase = {
4343 path : string ;
4444} ;
4545
46+ const TASK_RUN_SELECT_COLUMNS = [
47+ "task_id" ,
48+ "runtime" ,
49+ "task_kind" ,
50+ "source_id" ,
51+ "requester_session_key" ,
52+ "owner_key" ,
53+ "scope_kind" ,
54+ "child_session_key" ,
55+ "parent_flow_id" ,
56+ "parent_task_id" ,
57+ "agent_id" ,
58+ "run_id" ,
59+ "label" ,
60+ "task" ,
61+ "status" ,
62+ "delivery_status" ,
63+ "notify_policy" ,
64+ "created_at" ,
65+ "started_at" ,
66+ "ended_at" ,
67+ "last_event_at" ,
68+ "cleanup_after" ,
69+ "error" ,
70+ "progress_summary" ,
71+ "terminal_summary" ,
72+ "terminal_outcome" ,
73+ ] as const ;
74+
4675let cachedDatabase : TaskRegistryDatabase | null = null ;
4776
4877function normalizeNumber ( value : number | bigint | null ) : number | undefined {
@@ -174,34 +203,7 @@ function pruneRowsNotInSnapshot(params: {
174203function selectTaskRows ( db : DatabaseSync ) : TaskRegistryRow [ ] {
175204 const query = getTaskRegistryKysely ( db )
176205 . selectFrom ( "task_runs" )
177- . select ( [
178- "task_id" ,
179- "runtime" ,
180- "task_kind" ,
181- "source_id" ,
182- "requester_session_key" ,
183- "owner_key" ,
184- "scope_kind" ,
185- "child_session_key" ,
186- "parent_flow_id" ,
187- "parent_task_id" ,
188- "agent_id" ,
189- "run_id" ,
190- "label" ,
191- "task" ,
192- "status" ,
193- "delivery_status" ,
194- "notify_policy" ,
195- "created_at" ,
196- "started_at" ,
197- "ended_at" ,
198- "last_event_at" ,
199- "cleanup_after" ,
200- "error" ,
201- "progress_summary" ,
202- "terminal_summary" ,
203- "terminal_outcome" ,
204- ] )
206+ . select ( TASK_RUN_SELECT_COLUMNS )
205207 . orderBy ( "created_at" , "asc" )
206208 . orderBy ( "task_id" , "asc" ) ;
207209 return executeSqliteQuerySync ( db , query ) . rows ;
@@ -271,6 +273,15 @@ function replaceTaskDeliveryStateRow(
271273 ) ;
272274}
273275
276+ function deleteTaskRowsWithDeliveryState ( db : DatabaseSync , taskId : string ) : void {
277+ const kysely = getTaskRegistryKysely ( db ) ;
278+ executeSqliteQuerySync (
279+ db ,
280+ kysely . deleteFrom ( "task_delivery_state" ) . where ( "task_id" , "=" , taskId ) ,
281+ ) ;
282+ executeSqliteQuerySync ( db , kysely . deleteFrom ( "task_runs" ) . where ( "task_id" , "=" , taskId ) ) ;
283+ }
284+
274285function openTaskRegistryDatabase ( ) : TaskRegistryDatabase {
275286 const database = openOpenClawStateDatabase ( ) ;
276287 const pathname = database . path ;
@@ -312,34 +323,7 @@ export function listTaskRegistryRecordsByOwnerKeyFromSqlite(ownerKey: string): T
312323 const { db } = openTaskRegistryDatabase ( ) ;
313324 const query = getTaskRegistryKysely ( db )
314325 . selectFrom ( "task_runs" )
315- . select ( [
316- "task_id" ,
317- "runtime" ,
318- "task_kind" ,
319- "source_id" ,
320- "requester_session_key" ,
321- "owner_key" ,
322- "scope_kind" ,
323- "child_session_key" ,
324- "parent_flow_id" ,
325- "parent_task_id" ,
326- "agent_id" ,
327- "run_id" ,
328- "label" ,
329- "task" ,
330- "status" ,
331- "delivery_status" ,
332- "notify_policy" ,
333- "created_at" ,
334- "started_at" ,
335- "ended_at" ,
336- "last_event_at" ,
337- "cleanup_after" ,
338- "error" ,
339- "progress_summary" ,
340- "terminal_summary" ,
341- "terminal_outcome" ,
342- ] )
326+ . select ( TASK_RUN_SELECT_COLUMNS )
343327 . where ( "owner_key" , "=" , key )
344328 . orderBy ( "created_at" , "asc" )
345329 . orderBy ( "task_id" , "asc" ) ;
@@ -411,23 +395,13 @@ export function upsertTaskWithDeliveryStateToSqlite(params: {
411395
412396export function deleteTaskRegistryRecordFromSqlite ( taskId : string ) {
413397 withWriteTransaction ( ( { db } ) => {
414- const kysely = getTaskRegistryKysely ( db ) ;
415- executeSqliteQuerySync (
416- db ,
417- kysely . deleteFrom ( "task_delivery_state" ) . where ( "task_id" , "=" , taskId ) ,
418- ) ;
419- executeSqliteQuerySync ( db , kysely . deleteFrom ( "task_runs" ) . where ( "task_id" , "=" , taskId ) ) ;
398+ deleteTaskRowsWithDeliveryState ( db , taskId ) ;
420399 } ) ;
421400}
422401
423402export function deleteTaskAndDeliveryStateFromSqlite ( taskId : string ) {
424403 withWriteTransaction ( ( { db } ) => {
425- const kysely = getTaskRegistryKysely ( db ) ;
426- executeSqliteQuerySync (
427- db ,
428- kysely . deleteFrom ( "task_delivery_state" ) . where ( "task_id" , "=" , taskId ) ,
429- ) ;
430- executeSqliteQuerySync ( db , kysely . deleteFrom ( "task_runs" ) . where ( "task_id" , "=" , taskId ) ) ;
404+ deleteTaskRowsWithDeliveryState ( db , taskId ) ;
431405 } ) ;
432406}
433407
0 commit comments