Skip to content

Commit b2c7246

Browse files
authored
fix(core): refresh git references on daily activity (anomalyco#45575)
1 parent 9c39e75 commit b2c7246

9 files changed

Lines changed: 444 additions & 170 deletions

File tree

‎packages/core/src/reference.ts‎

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ type Draft = {
3636

3737
export interface Interface extends State.Transformable<Draft> {
3838
readonly list: () => Effect.Effect<Info[]>
39+
/** Schedules daily refresh checks in the Location scope without waiting for Git. */
40+
readonly refresh: () => Effect.Effect<void>
3941
}
4042

4143
export class Service extends Context.Service<Service, Interface>()("@opencode/Reference") {}
@@ -48,6 +50,25 @@ const layer = Layer.effect(
4850
const cache = yield* RepositoryCache.Service
4951
const scope = yield* Scope.Scope
5052
const materialized = new Map<string, Info>()
53+
const refresh = Effect.fn("Reference.refresh")(function* () {
54+
yield* Effect.forEach(
55+
Array.from(materialized.values()),
56+
(reference) =>
57+
Effect.gen(function* () {
58+
if (reference.source.type !== "git") return
59+
yield* cache.ensure({
60+
reference: Repository.parseRemote(reference.source.repository),
61+
branch: reference.source.branch,
62+
refresh: "daily",
63+
})
64+
}).pipe(
65+
Effect.catchCause((cause) =>
66+
Effect.logWarning("failed to materialize reference", { name: reference.name, cause }),
67+
),
68+
),
69+
{ concurrency: 4, discard: true },
70+
).pipe(Effect.forkIn(scope), Effect.asVoid)
71+
})
5172
const state = State.create<Data, Draft>({
5273
name: "reference",
5374
initial: () => ({ sources: new Map() }),
@@ -60,17 +81,14 @@ const layer = Layer.effect(
6081
Effect.gen(function* () {
6182
materialized.clear()
6283
for (const [name, source] of draft.list()) {
84+
const info = {
85+
name,
86+
source,
87+
...(source.description === undefined ? {} : { description: source.description }),
88+
...(source.hidden === undefined ? {} : { hidden: source.hidden }),
89+
}
6390
if (source.type === "local") {
64-
materialized.set(
65-
name,
66-
Info.make({
67-
name,
68-
path: source.path,
69-
...(source.description === undefined ? {} : { description: source.description }),
70-
...(source.hidden === undefined ? {} : { hidden: source.hidden }),
71-
source,
72-
}),
73-
)
91+
materialized.set(name, Info.make({ ...info, path: source.path }))
7492
continue
7593
}
7694
const repository = Repository.parse(source.repository)
@@ -85,31 +103,20 @@ const layer = Layer.effect(
85103
materialized.set(
86104
name,
87105
Info.make({
88-
name,
106+
...info,
89107
path: AbsolutePath.make(Repository.cachePath(global.repos, repository, source.branch)),
90-
...(source.description === undefined ? {} : { description: source.description }),
91-
...(source.hidden === undefined ? {} : { hidden: source.hidden }),
92-
source,
93108
}),
94109
)
95-
yield* cache.ensure({ reference: repository, branch: source.branch, refresh: true }).pipe(
96-
Effect.catchCause((cause) =>
97-
Effect.logWarning("failed to materialize reference", {
98-
name,
99-
repository: source.repository,
100-
cause,
101-
}),
102-
),
103-
Effect.forkIn(scope),
104-
)
105110
}
111+
yield* refresh()
106112
yield* bus.publish(Reference.Event.Updated, {})
107113
}),
108114
})
109115

110116
return Service.of({
111117
transform: state.transform,
112118
reload: state.reload,
119+
refresh,
113120
list: Effect.fn("Reference.list")(function* () {
114121
return Array.from(materialized.values())
115122
}),

‎packages/core/src/repository-cache.ts‎

Lines changed: 86 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@
66
* observe the checkout move underneath them.
77
*/
88
import path from "path"
9-
import { Context, Effect, Layer, Schema } from "effect"
9+
import { Clock, Context, Duration, Effect, Layer, Option, Schema } from "effect"
1010
import { FSUtil } from "@opencode-ai/util/fs-util"
1111
import { Git } from "./git.js"
1212
import { Global } from "@opencode-ai/util/global"
1313
import { Repository } from "./repository.js"
1414
import { AbsolutePath } from "./schema.js"
1515
import { makeGlobalNode } from "@opencode-ai/util/effect/app-node"
1616
import { 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

1825
export type Result = {
1926
readonly repository: string
@@ -27,7 +34,8 @@ export type Result = {
2734

2835
export 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

220236
export 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

226242
function errorMessage(error: unknown) {

‎packages/core/src/session/session.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { PluginSupervisor } from "../plugin/supervisor-service.js"
1010
import { Shell } from "../shell.js"
1111
import { ShellResult } from "../shell/result.js"
1212
import { Skill } from "../skill.js"
13+
import { Reference } from "../reference.js"
1314
import {
1415
BusyError,
1516
CompactionConflictError,
@@ -34,6 +35,7 @@ import { SessionStore } from "./store.js"
3435

3536
export type Services =
3637
| PluginSupervisor.Service
38+
| Reference.Service
3739
| SessionPrompt.Service
3840
| SessionRevert.Service
3941
| Shell.Service
@@ -167,18 +169,22 @@ export const make = Effect.fn("Session.make")(function* (servicesFor: (ref: Loca
167169
delivery: input.delivery ?? "steer",
168170
})
169171
if (existing) return existing
170-
const item = yield* restore(
171-
SessionPrompt.Service.use((preparation) => preparation.prepare({ sessionID, messageID, input })).pipe(
172-
Effect.provide(servicesFor(session.location)),
173-
),
172+
const prepared = yield* restore(
173+
Effect.gen(function* () {
174+
const preparation = yield* SessionPrompt.Service
175+
const references = yield* Reference.Service
176+
return { item: yield* preparation.prepare({ sessionID, messageID, input }), references }
177+
}).pipe(Effect.provide(servicesFor(session.location))),
174178
)
175179
// Commit a staged revert only after preparation succeeds, before admitting new work.
176180
if (session.revert) yield* SessionRevert.commit(bus, session)
177-
return yield* admission.admit({
181+
const admitted = yield* admission.admit({
178182
id: messageID,
179183
sessionID: session.id,
180-
item,
184+
item: prepared.item,
181185
})
186+
yield* prepared.references.refresh()
187+
return admitted
182188
}).pipe(
183189
Effect.catchTag("SessionInbox.LifecycleConflict", () => new PromptConflictError({ sessionID, messageID })),
184190
)

‎packages/core/test/fixture/prompt-location.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { LocationServiceMap } from "@opencode-ai/core/location-service-map"
44
import type { LocationServices } from "@opencode-ai/core/location-services"
55
import { PluginHooks } from "@opencode-ai/core/plugin/hooks"
66
import { PluginSupervisor } from "@opencode-ai/core/plugin/supervisor-service"
7+
import { Reference } from "@opencode-ai/core/reference"
78
import { SessionPrompt } from "@opencode-ai/core/session/prompt"
89
import { Skill } from "@opencode-ai/core/skill"
910
import type { Location } from "@opencode-ai/schema/location"
@@ -12,7 +13,7 @@ import { LayerNode } from "@opencode-ai/util/effect/layer-node"
1213
import { FSUtil } from "@opencode-ai/util/fs-util"
1314
import { Effect, Layer, LayerMap } from "effect"
1415

15-
// Plain-prompt unit fixtures use virtual directories and need only prompt preparation services.
16+
// Plain-prompt unit fixtures use virtual directories without configured references.
1617
export const promptLocationNode = makeGlobalNode({
1718
service: LocationServiceMap.Service,
1819
layer: Layer.effect(
@@ -30,6 +31,7 @@ export const promptLocationNode = makeGlobalNode({
3031
]),
3132
Layer.succeed(FSUtil.Service, fs),
3233
Layer.succeed(PluginSupervisor.Service, { flush: Effect.void }),
34+
Layer.mock(Reference.Service, { refresh: () => Effect.void }),
3335
),
3436
),
3537
) as Layer.Layer<LocationServices>,

0 commit comments

Comments
 (0)