This commit is contained in:
Rodney Shen 2025-12-23 15:42:04 +08:00 committed by GitHub
commit ef8763b5ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,5 +1,6 @@
import { Instance } from "../project/instance"
import { Log } from "../util/log"
import { Storage } from "../storage/storage"
export namespace FileTime {
const log = Log.create({ service: "file.time" })
@ -25,10 +26,31 @@ export namespace FileTime {
const { read } = state()
read[sessionID] = read[sessionID] || {}
read[sessionID][file] = new Date()
Storage.write(["filetime", sessionID, Buffer.from(file).toString("base64url")], {
time: read[sessionID][file]!.toISOString(),
path: file,
}).catch(() => {})
}
export function get(sessionID: string, file: string) {
return state().read[sessionID]?.[file]
export async function get(sessionID: string, file: string) {
const memTime = state().read[sessionID]?.[file]
if (memTime) return memTime
const stored = await Storage.read<{ time: string; path: string }>([
"filetime",
sessionID,
Buffer.from(file).toString("base64url"),
]).catch(() => undefined)
if (stored?.time) {
const date = new Date(stored.time)
const { read } = state()
read[sessionID] = read[sessionID] || {}
read[sessionID][file] = date
return date
}
return undefined
}
export async function withLock<T>(filepath: string, fn: () => Promise<T>): Promise<T> {
@ -52,7 +74,7 @@ export namespace FileTime {
}
export async function assert(sessionID: string, filepath: string) {
const time = get(sessionID, filepath)
const time = await get(sessionID, filepath)
if (!time) throw new Error(`You must read the file ${filepath} before overwriting it. Use the Read tool first`)
const stats = await Bun.file(filepath).stat()
if (stats.mtime.getTime() > time.getTime()) {