@@ -5,6 +5,7 @@ import * as Log from "@opencode-ai/core/util/log"
55import { and , desc , eq , inArray , lte , sql } from "drizzle-orm"
66import { Database } from "@/storage/db"
77import { Instance } from "@/project/instance"
8+ import { InstanceRef } from "@/effect/instance-ref"
89import { EffectBridge } from "@/effect/bridge"
910import { InstanceState } from "@/effect/instance-state"
1011import { ProjectID } from "@/project/schema"
@@ -18,8 +19,8 @@ import { SessionSummary } from "@/session/summary"
1819import { SessionStatus } from "@/session/status"
1920import type { FileDiff } from "@/snapshot"
2021import { Worktree } from "@/worktree"
21- import { zod } from "@/util /effect-zod"
22- import { withStatics } from "@/util /schema"
22+ import { zod } from "@opencode-ai/core /effect-zod"
23+ import { withStatics } from "@opencode-ai/core /schema"
2324import { Cause , Context , Effect , Exit , Layer , Option , Schema , Types } from "effect"
2425import { AutomationFindingTable , AutomationRunTable , AutomationTable } from "./automation.sql"
2526import { AutomationFindingID , AutomationID , AutomationRunID } from "./schema"
@@ -34,7 +35,7 @@ export type Weekday = Schema.Schema.Type<typeof Weekday>
3435export const ScheduleConfig = Schema . Union ( [
3536 Schema . Struct ( {
3637 type : Schema . Literal ( "interval" ) ,
37- everyMinutes : Schema . Number ,
38+ everyMinutes : Schema . Finite ,
3839 } ) ,
3940 Schema . Struct ( {
4041 type : Schema . Literal ( "daily" ) ,
@@ -77,18 +78,18 @@ export const RunStatus = Schema.Literals([
7778export type RunStatus = Schema . Schema . Type < typeof RunStatus >
7879
7980const DiffStats = Schema . Struct ( {
80- additions : Schema . Number ,
81- deletions : Schema . Number ,
82- files : Schema . Number ,
81+ additions : Schema . Finite ,
82+ deletions : Schema . Finite ,
83+ files : Schema . Finite ,
8384} )
8485
8586const Time = Schema . Struct ( {
86- created : Schema . Number ,
87- updated : Schema . Number ,
88- lastRun : Schema . optional ( Schema . Number ) ,
89- nextRun : Schema . optional ( Schema . Number ) ,
90- starts : Schema . optional ( Schema . Number ) ,
91- ends : Schema . optional ( Schema . Number ) ,
87+ created : Schema . Finite ,
88+ updated : Schema . Finite ,
89+ lastRun : Schema . optional ( Schema . Finite ) ,
90+ nextRun : Schema . optional ( Schema . Finite ) ,
91+ starts : Schema . optional ( Schema . Finite ) ,
92+ ends : Schema . optional ( Schema . Finite ) ,
9293} )
9394
9495export const Finding = Schema . Struct ( {
@@ -100,8 +101,8 @@ export const Finding = Schema.Struct({
100101 filesChanged : Schema . mutable ( Schema . Array ( Schema . String ) ) ,
101102 recommendedNextAction : Schema . optional ( Schema . String ) ,
102103 time : Schema . Struct ( {
103- created : Schema . Number ,
104- updated : Schema . Number ,
104+ created : Schema . Finite ,
105+ updated : Schema . Finite ,
105106 } ) ,
106107} )
107108 . annotate ( { identifier : "AutomationFinding" } )
@@ -123,7 +124,7 @@ export const Info = Schema.Struct({
123124 reasoningEffort : Schema . optional ( Schema . Literals ( [ "none" , "low" , "medium" , "high" ] ) ) ,
124125 permissionProfile : PermissionProfile ,
125126 notificationBehavior : Schema . Literals ( [ "inbox" , "auto_archive_no_findings" ] ) ,
126- maxRuntimeMinutes : Schema . optional ( Schema . Number ) ,
127+ maxRuntimeMinutes : Schema . optional ( Schema . Finite ) ,
127128 time : Time ,
128129} )
129130 . annotate ( { identifier : "Automation" } )
@@ -145,17 +146,17 @@ export const RunInfo = Schema.Struct({
145146 branchName : Schema . optional ( Schema . String ) ,
146147 summary : Schema . optional ( Schema . String ) ,
147148 result : Schema . optional ( Schema . Literals ( [ "findings" , "no_findings" , "needs_approval" , "failed" ] ) ) ,
148- findingsCount : Schema . Number ,
149+ findingsCount : Schema . Finite ,
149150 diffStats : Schema . optional ( DiffStats ) ,
150151 error : Schema . optional ( Schema . String ) ,
151152 time : Schema . Struct ( {
152- created : Schema . Number ,
153- updated : Schema . Number ,
154- queued : Schema . Number ,
155- started : Schema . optional ( Schema . Number ) ,
156- completed : Schema . optional ( Schema . Number ) ,
157- read : Schema . optional ( Schema . Number ) ,
158- archived : Schema . optional ( Schema . Number ) ,
153+ created : Schema . Finite ,
154+ updated : Schema . Finite ,
155+ queued : Schema . Finite ,
156+ started : Schema . optional ( Schema . Finite ) ,
157+ completed : Schema . optional ( Schema . Finite ) ,
158+ read : Schema . optional ( Schema . Finite ) ,
159+ archived : Schema . optional ( Schema . Finite ) ,
159160 } ) ,
160161} )
161162 . annotate ( { identifier : "AutomationRun" } )
@@ -174,9 +175,9 @@ export const CreateInput = Schema.Struct({
174175 reasoningEffort : Schema . optional ( Schema . Literals ( [ "none" , "low" , "medium" , "high" ] ) ) ,
175176 permissionProfile : Schema . optional ( PermissionProfile ) ,
176177 notificationBehavior : Schema . optional ( Schema . Literals ( [ "inbox" , "auto_archive_no_findings" ] ) ) ,
177- maxRuntimeMinutes : Schema . optional ( Schema . Number ) ,
178- startsAt : Schema . optional ( Schema . Number ) ,
179- endsAt : Schema . optional ( Schema . Number ) ,
178+ maxRuntimeMinutes : Schema . optional ( Schema . Finite ) ,
179+ startsAt : Schema . optional ( Schema . Finite ) ,
180+ endsAt : Schema . optional ( Schema . Finite ) ,
180181} )
181182 . annotate ( { identifier : "AutomationCreateInput" } )
182183 . pipe ( withStatics ( ( s ) => ( { zod : zod ( s ) } ) ) )
@@ -194,9 +195,9 @@ export const UpdateInput = Schema.Struct({
194195 reasoningEffort : Schema . optional ( Schema . Literals ( [ "none" , "low" , "medium" , "high" ] ) ) ,
195196 permissionProfile : Schema . optional ( PermissionProfile ) ,
196197 notificationBehavior : Schema . optional ( Schema . Literals ( [ "inbox" , "auto_archive_no_findings" ] ) ) ,
197- maxRuntimeMinutes : Schema . optional ( Schema . Number ) ,
198- startsAt : Schema . optional ( Schema . Number ) ,
199- endsAt : Schema . optional ( Schema . Number ) ,
198+ maxRuntimeMinutes : Schema . optional ( Schema . Finite ) ,
199+ startsAt : Schema . optional ( Schema . Finite ) ,
200+ endsAt : Schema . optional ( Schema . Finite ) ,
200201} )
201202 . annotate ( { identifier : "AutomationUpdateInput" } )
202203 . pipe ( withStatics ( ( s ) => ( { zod : zod ( s ) } ) ) )
@@ -206,7 +207,7 @@ export const ListRunsInput = Schema.Struct({
206207 automationID : Schema . optional ( AutomationID ) ,
207208 inbox : Schema . optional ( Schema . Boolean ) ,
208209 archived : Schema . optional ( Schema . Boolean ) ,
209- limit : Schema . optional ( Schema . Number ) ,
210+ limit : Schema . optional ( Schema . Finite ) ,
210211} )
211212 . annotate ( { identifier : "AutomationListRunsInput" } )
212213 . pipe ( withStatics ( ( s ) => ( { zod : zod ( s ) } ) ) )
@@ -1035,7 +1036,7 @@ export const layer = Layer.effect(
10351036 details :
10361037 [ finalOutput . summary , finalOutput . diffSummary ] . filter ( Boolean ) . join ( "\n\n" ) ||
10371038 "Automation completed." ,
1038- filesChanged : diffs . map ( ( diff ) => diff . file ) ,
1039+ filesChanged : diffs . map ( ( diff ) => diff . file ) . filter ( ( file ) : file is string => file !== undefined ) ,
10391040 } ,
10401041 ]
10411042 const nextRunAt = computeNextRun ( {
@@ -1217,12 +1218,16 @@ export const layer = Layer.effect(
12171218 )
12181219 : runPromptEffect ( automation , runID )
12191220 const result = worktreeInfo
1220- ? yield * Effect . promise ( async ( ) => {
1221- const nested = await Instance . provide ( {
1222- directory : worktreeInfo ! . directory ,
1223- fn : ( ) => Effect . runPromise ( task . pipe ( Effect . provide ( WorktreeRunLayer ) ) ) ,
1224- } )
1225- return await nested
1221+ ? yield * Effect . gen ( function * ( ) {
1222+ const current = yield * InstanceState . context
1223+ const ctx = { ...current , directory : worktreeInfo ! . directory }
1224+ return yield * Effect . promise ( ( ) =>
1225+ Instance . restore ( ctx , ( ) =>
1226+ Effect . runPromise (
1227+ task . pipe ( Effect . provideService ( InstanceRef , ctx ) , Effect . provide ( WorktreeRunLayer ) ) ,
1228+ ) ,
1229+ ) ,
1230+ )
12261231 } )
12271232 : yield * task
12281233 const structured =
0 commit comments