diff --git a/packages/opencode/src/file/time.ts b/packages/opencode/src/file/time.ts index 770427abe..046f6f2fd 100644 --- a/packages/opencode/src/file/time.ts +++ b/packages/opencode/src/file/time.ts @@ -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(filepath: string, fn: () => Promise): Promise { @@ -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()) {