@@ -727,7 +727,9 @@ export const layer = Layer.effect(
727727 WHERE task_id = ${ input . id }
728728 AND last_run_status = 'running'
729729 AND last_run_at = ${ input . startedAtIso }
730+ RETURNING task_id
730731 ` . pipe (
732+ Effect . map ( ( rows ) => rows . length > 0 ) ,
731733 Effect . mapError ( ( cause ) =>
732734 taskError ( "Could not record schedule task run." , { taskId : input . id , cause } ) ,
733735 ) ,
@@ -943,9 +945,12 @@ export const layer = Layer.effect(
943945 const lastRunError = runSucceeded ? null : errorMessage ( result . cause ) ;
944946 const threadId = runSucceeded ? result . value : null ;
945947 // One transaction, so an edit cannot land between reading the schedule and
946- // writing the next run computed from it.
948+ // writing the next run computed from it. The origin is read first: it reads
949+ // files, and the transaction holds SQLite's writer lock.
950+ const origin = yield * readWebhookOrigin ;
947951 const record = Effect . gen ( function * ( ) {
948- const current = yield * findTask ( task . id ) ;
952+ const [ row ] = yield * getRows ( task . id ) ;
953+ const current = row === undefined ? null : yield * decodeRow ( row , origin ) ;
949954 const scheduleSource = current ?? task ;
950955 const completed : ScheduledTask = {
951956 ...scheduleSource ,
@@ -960,7 +965,7 @@ export const layer = Layer.effect(
960965 lastRunError,
961966 runCount : scheduleSource . runCount + 1 ,
962967 } ;
963- if ( current === null ) return { completed, written : false } ;
968+ if ( current === null ) return { recorded : completed , written : false } ;
964969 // startedAtIso in the guard ensures this writes only to the row this
965970 // run marked as running — a task deleted mid-run and recreated with
966971 // the same id (idempotent commandId replay) must not be stamped.
@@ -972,21 +977,23 @@ export const layer = Layer.effect(
972977 error : lastRunError ,
973978 startedAtIso,
974979 } ) ;
975- if ( webhook === undefined ) yield * completion ;
980+ let written = false ;
981+ if ( webhook === undefined ) written = yield * completion ;
976982 else {
977983 const pending =
978984 yield * sql `SELECT 1 FROM scheduled_task_webhook_pending WHERE delivery_id = ${ webhook . deliveryId } ` ;
979- if ( pending . length === 0 ) yield * completion ;
985+ if ( pending . length === 0 ) written = yield * completion ;
980986 else {
981987 const counted =
982988 yield * sql `UPDATE scheduled_task_webhook_pending SET state = ${ runSucceeded ? "completed" : "failed" }
983989 WHERE delivery_id = ${ webhook . deliveryId } AND state NOT IN ('completed', 'failed') RETURNING delivery_id` ;
984- if ( counted . length > 0 ) yield * completion ;
990+ if ( counted . length > 0 ) written = yield * completion ;
985991 }
986992 }
987- return { completed, written : true } ;
993+ // A row recreated mid-run is not this run's, so report what is stored.
994+ return { recorded : written ? completed : current , written } ;
988995 } ) ;
989- const { completed , written } = yield * sql
996+ const { recorded , written } = yield * sql
990997 . withTransaction ( record )
991998 . pipe (
992999 Effect . mapError ( ( cause ) =>
@@ -1001,7 +1008,7 @@ export const layer = Layer.effect(
10011008 ) ,
10021009 ) ;
10031010 if ( written ) yield * notifyChanged ;
1004- return { task : completed , threadId } ;
1011+ return { task : recorded , threadId } ;
10051012 } ) . pipe (
10061013 Effect . onError ( ( cause ) =>
10071014 releaseStuckRun ( task , startedAtIso , errorMessage ( cause ) , webhook ?. deliveryId ) ,
0 commit comments