66 * observe the checkout move underneath them.
77 */
88import path from "path"
9- import { Context , Effect , Layer , Schema } from "effect"
9+ import { Clock , Context , Duration , Effect , Layer , Option , Schema } from "effect"
1010import { FSUtil } from "@opencode-ai/util/fs-util"
1111import { Git } from "./git.js"
1212import { Global } from "@opencode-ai/util/global"
1313import { Repository } from "./repository.js"
1414import { AbsolutePath } from "./schema.js"
1515import { makeGlobalNode } from "@opencode-ai/util/effect/app-node"
1616import { EffectFlock } from "@opencode-ai/util/effect-flock"
17+ import { KV } from "./kv.js"
18+
19+ const Refresh = Schema . Struct ( {
20+ attemptedAt : Schema . Number ,
21+ refreshedAt : Schema . optionalKey ( Schema . Number ) ,
22+ } )
23+ const refreshInterval = Duration . toMillis ( Duration . days ( 1 ) )
1724
1825export type Result = {
1926 readonly repository : string
@@ -27,7 +34,8 @@ export type Result = {
2734
2835export type EnsureInput = {
2936 readonly reference : Repository . RemoteReference
30- readonly refresh ?: boolean
37+ /** `daily` throttles existing checkouts; `true` forces a refresh. */
38+ readonly refresh ?: boolean | "daily"
3139 readonly branch ?: string
3240}
3341
@@ -105,50 +113,55 @@ export const validateBranch = Effect.fn("RepositoryCache.validateBranch")(functi
105113 } )
106114} )
107115
108- const layer : Layer . Layer < Service , never , FSUtil . Service | Git . Service | EffectFlock . Service | Global . Service > =
109- Layer . effect (
110- Service ,
111- Effect . gen ( function * ( ) {
112- const fs = yield * FSUtil . Service
113- const git = yield * Git . Service
114- const flock = yield * EffectFlock . Service
115- const global = yield * Global . Service
116-
117- return Service . of ( {
118- ensure : Effect . fn ( "RepositoryCache.ensure" ) ( function * ( input ) {
119- if ( input . branch ) yield * validateBranch ( input . branch )
120-
121- const repository = input . reference . label
122- const localPath = Repository . cachePath ( global . repos , input . reference , input . branch )
123- const cloneTarget = Repository . parse ( input . reference . remote ) ?? input . reference
124-
125- return yield * flock
126- . withLock (
127- Effect . gen ( function * ( ) {
128- yield * cacheOperation ( fs . ensureDir ( path . dirname ( localPath ) ) , "ensure cache directory" , localPath )
129-
130- const existing = yield * git . repo . discover ( AbsolutePath . make ( localPath ) )
131- const origin = existing ? yield * git . remote . get ( existing ) : undefined
132- const originReference = origin ? Repository . parse ( origin ) : undefined
133- // Discovery walks upward, so an enclosing repository with a
134- // matching origin could masquerade as the cache entry; reuse
135- // requires the checkout to live exactly at the cache path.
136- const worktree = existing ? yield * fs . resolve ( localPath ) : undefined
137- const reuse = Boolean (
138- existing &&
139- existing . worktree === worktree &&
140- originReference &&
141- Repository . same ( originReference , cloneTarget ) ,
142- )
143- if ( ! reuse && ( yield * fs . existsSafe ( localPath ) ) ) {
144- yield * cacheOperation ( fs . remove ( localPath , { recursive : true } ) , "remove stale cache" , localPath )
145- }
146-
147- const status = ! reuse
148- ? ( "cloned" as const )
149- : input . refresh
150- ? ( "refreshed" as const )
151- : ( "cached" as const )
116+ const layer = Layer . effect (
117+ Service ,
118+ Effect . gen ( function * ( ) {
119+ const fs = yield * FSUtil . Service
120+ const git = yield * Git . Service
121+ const flock = yield * EffectFlock . Service
122+ const global = yield * Global . Service
123+ const kv = yield * KV . Service
124+
125+ return Service . of ( {
126+ ensure : Effect . fn ( "RepositoryCache.ensure" ) ( function * ( input ) {
127+ if ( input . branch ) yield * validateBranch ( input . branch )
128+
129+ const repository = input . reference . label
130+ const localPath = Repository . cachePath ( global . repos , input . reference , input . branch )
131+ const key = `repository-cache:${ localPath } `
132+ const cloneTarget = Repository . parse ( input . reference . remote ) ?? input . reference
133+
134+ return yield * flock
135+ . withLock (
136+ Effect . gen ( function * ( ) {
137+ yield * cacheOperation ( fs . ensureDir ( path . dirname ( localPath ) ) , "ensure cache directory" , localPath )
138+
139+ const existing = yield * git . repo . discover ( AbsolutePath . make ( localPath ) )
140+ const origin = existing ? yield * git . remote . get ( existing ) : undefined
141+ const originReference = origin ? Repository . parse ( origin ) : undefined
142+ // Discovery walks upward, so an enclosing repository with a
143+ // matching origin could masquerade as the cache entry; reuse
144+ // requires the checkout to live exactly at the cache path.
145+ const worktree = existing ? yield * fs . resolve ( localPath ) : undefined
146+ const reuse = Boolean (
147+ existing &&
148+ existing . worktree === worktree &&
149+ originReference &&
150+ Repository . same ( originReference , cloneTarget ) ,
151+ )
152+ if ( ! reuse && ( yield * fs . existsSafe ( localPath ) ) ) {
153+ yield * cacheOperation ( fs . remove ( localPath , { recursive : true } ) , "remove stale cache" , localPath )
154+ }
155+
156+ const now = yield * Clock . currentTimeMillis
157+ const previous = Option . getOrUndefined ( Schema . decodeUnknownOption ( Refresh ) ( yield * kv . get ( key ) ) )
158+ const refresh =
159+ input . refresh === "daily" ? ! previous || now - previous . attemptedAt >= refreshInterval : input . refresh
160+ const status = ! reuse ? ( "cloned" as const ) : refresh ? ( "refreshed" as const ) : ( "cached" as const )
161+
162+ if ( status !== "cached" ) {
163+ // Record attempts before network work so offline/auth failures don't retry on every prompt.
164+ yield * kv . set ( key , { ...previous , attemptedAt : now } )
152165
153166 if ( status === "cloned" ) {
154167 yield * git . repo
@@ -193,34 +206,37 @@ const layer: Layer.Layer<Service, never, FSUtil.Service | Git.Service | EffectFl
193206 . pipe ( Effect . mapError ( ( error ) => new ResetFailedError ( { repository, message : error . message } ) ) )
194207 }
195208
196- const checkout = yield * git . repo . discover ( AbsolutePath . make ( localPath ) )
197-
198- return {
199- repository,
200- host : input . reference . host ,
201- remote : input . reference . remote ,
202- localPath,
203- status,
204- head : checkout ? yield * git . history . head ( checkout ) : undefined ,
205- branch : checkout ? yield * git . history . branch ( checkout ) : undefined ,
206- } satisfies Result
207- } ) ,
208- `repository-cache:${ localPath } ` ,
209- )
210- . pipe (
211- Effect . mapError ( ( error ) =>
212- isError ( error ) ? error : new LockFailedError ( { localPath, message : errorMessage ( error ) } ) ,
213- ) ,
214- )
215- } ) ,
216- } )
217- } ) ,
218- )
209+ yield * kv . set ( key , { attemptedAt : now , refreshedAt : yield * Clock . currentTimeMillis } )
210+ }
211+
212+ const checkout = yield * git . repo . discover ( AbsolutePath . make ( localPath ) )
213+
214+ return {
215+ repository,
216+ host : input . reference . host ,
217+ remote : input . reference . remote ,
218+ localPath,
219+ status,
220+ head : checkout ? yield * git . history . head ( checkout ) : undefined ,
221+ branch : checkout ? yield * git . history . branch ( checkout ) : undefined ,
222+ } satisfies Result
223+ } ) ,
224+ key ,
225+ )
226+ . pipe (
227+ Effect . mapError ( ( error ) =>
228+ isError ( error ) ? error : new LockFailedError ( { localPath, message : errorMessage ( error ) } ) ,
229+ ) ,
230+ )
231+ } ) ,
232+ } )
233+ } ) ,
234+ )
219235
220236export const node = makeGlobalNode ( {
221237 service : Service ,
222238 layer,
223- deps : [ EffectFlock . node , FSUtil . node , Git . node , Global . node ] ,
239+ deps : [ EffectFlock . node , FSUtil . node , Git . node , Global . node , KV . node ] ,
224240} )
225241
226242function errorMessage ( error : unknown ) {
0 commit comments