mirror of
https://github.com/anomalyco/opencode.git
synced 2026-04-27 16:25:50 +00:00
- Replace manual subscription Map with PubSub.unbounded per instance - Per-type PubSubs + wildcard PubSub, cleaned up via addFinalizer - InstanceDisposed published before PubSub shutdown so subscribers see it - Replace makeRunPromise with makeRuntime (single runtime with runPromise + runFork) - Update all 19 services to use makeRuntime destructuring - Legacy facade preserved: publish/subscribe/subscribeAll same signatures - subscribe/subscribeAll fork stream consumer fibers, return interrupt function - Extract Format.file() for explicit formatting, remove event-driven subscription - Inline Format.file() calls in write/edit/apply_patch tools - Drop Bus.once (zero callers)
129 lines
4.6 KiB
TypeScript
129 lines
4.6 KiB
TypeScript
import { DateTime, Effect, Layer, Semaphore, ServiceMap } from "effect"
|
|
import { InstanceState } from "@/effect/instance-state"
|
|
import { makeRuntime } from "@/effect/run-service"
|
|
import { Flag } from "@/flag/flag"
|
|
import type { SessionID } from "@/session/schema"
|
|
import { Filesystem } from "../util/filesystem"
|
|
import { Log } from "../util/log"
|
|
|
|
export namespace FileTime {
|
|
const log = Log.create({ service: "file.time" })
|
|
|
|
export type Stamp = {
|
|
readonly read: Date
|
|
readonly mtime: number | undefined
|
|
readonly ctime: number | undefined
|
|
readonly size: number | undefined
|
|
}
|
|
|
|
const stamp = Effect.fnUntraced(function* (file: string) {
|
|
const stat = Filesystem.stat(file)
|
|
const size = typeof stat?.size === "bigint" ? Number(stat.size) : stat?.size
|
|
return {
|
|
read: yield* DateTime.nowAsDate,
|
|
mtime: stat?.mtime?.getTime(),
|
|
ctime: stat?.ctime?.getTime(),
|
|
size,
|
|
}
|
|
})
|
|
|
|
const session = (reads: Map<SessionID, Map<string, Stamp>>, sessionID: SessionID) => {
|
|
const value = reads.get(sessionID)
|
|
if (value) return value
|
|
|
|
const next = new Map<string, Stamp>()
|
|
reads.set(sessionID, next)
|
|
return next
|
|
}
|
|
|
|
interface State {
|
|
reads: Map<SessionID, Map<string, Stamp>>
|
|
locks: Map<string, Semaphore.Semaphore>
|
|
}
|
|
|
|
export interface Interface {
|
|
readonly read: (sessionID: SessionID, file: string) => Effect.Effect<void>
|
|
readonly get: (sessionID: SessionID, file: string) => Effect.Effect<Date | undefined>
|
|
readonly assert: (sessionID: SessionID, filepath: string) => Effect.Effect<void>
|
|
readonly withLock: <T>(filepath: string, fn: () => Promise<T>) => Effect.Effect<T>
|
|
}
|
|
|
|
export class Service extends ServiceMap.Service<Service, Interface>()("@opencode/FileTime") {}
|
|
|
|
export const layer = Layer.effect(
|
|
Service,
|
|
Effect.gen(function* () {
|
|
const disableCheck = yield* Flag.OPENCODE_DISABLE_FILETIME_CHECK
|
|
const state = yield* InstanceState.make<State>(
|
|
Effect.fn("FileTime.state")(() =>
|
|
Effect.succeed({
|
|
reads: new Map<SessionID, Map<string, Stamp>>(),
|
|
locks: new Map<string, Semaphore.Semaphore>(),
|
|
}),
|
|
),
|
|
)
|
|
|
|
const getLock = Effect.fn("FileTime.lock")(function* (filepath: string) {
|
|
const locks = (yield* InstanceState.get(state)).locks
|
|
const lock = locks.get(filepath)
|
|
if (lock) return lock
|
|
|
|
const next = Semaphore.makeUnsafe(1)
|
|
locks.set(filepath, next)
|
|
return next
|
|
})
|
|
|
|
const read = Effect.fn("FileTime.read")(function* (sessionID: SessionID, file: string) {
|
|
const reads = (yield* InstanceState.get(state)).reads
|
|
log.info("read", { sessionID, file })
|
|
session(reads, sessionID).set(file, yield* stamp(file))
|
|
})
|
|
|
|
const get = Effect.fn("FileTime.get")(function* (sessionID: SessionID, file: string) {
|
|
const reads = (yield* InstanceState.get(state)).reads
|
|
return reads.get(sessionID)?.get(file)?.read
|
|
})
|
|
|
|
const assert = Effect.fn("FileTime.assert")(function* (sessionID: SessionID, filepath: string) {
|
|
if (disableCheck) return
|
|
|
|
const reads = (yield* InstanceState.get(state)).reads
|
|
const time = reads.get(sessionID)?.get(filepath)
|
|
if (!time) throw new Error(`You must read file ${filepath} before overwriting it. Use the Read tool first`)
|
|
|
|
const next = yield* stamp(filepath)
|
|
const changed = next.mtime !== time.mtime || next.ctime !== time.ctime || next.size !== time.size
|
|
if (!changed) return
|
|
|
|
throw new Error(
|
|
`File ${filepath} has been modified since it was last read.\nLast modification: ${new Date(next.mtime ?? next.read.getTime()).toISOString()}\nLast read: ${time.read.toISOString()}\n\nPlease read the file again before modifying it.`,
|
|
)
|
|
})
|
|
|
|
const withLock = Effect.fn("FileTime.withLock")(function* <T>(filepath: string, fn: () => Promise<T>) {
|
|
return yield* Effect.promise(fn).pipe((yield* getLock(filepath)).withPermits(1))
|
|
})
|
|
|
|
return Service.of({ read, get, assert, withLock })
|
|
}),
|
|
).pipe(Layer.orDie)
|
|
|
|
const { runPromise } = makeRuntime(Service, layer)
|
|
|
|
export function read(sessionID: SessionID, file: string) {
|
|
return runPromise((s) => s.read(sessionID, file))
|
|
}
|
|
|
|
export function get(sessionID: SessionID, file: string) {
|
|
return runPromise((s) => s.get(sessionID, file))
|
|
}
|
|
|
|
export async function assert(sessionID: SessionID, filepath: string) {
|
|
return runPromise((s) => s.assert(sessionID, filepath))
|
|
}
|
|
|
|
export async function withLock<T>(filepath: string, fn: () => Promise<T>): Promise<T> {
|
|
return runPromise((s) => s.withLock(filepath, fn))
|
|
}
|
|
}
|