The open source coding agent.
TypeScript
scope filetimes to session
Dax Raad54b99cd88a56
diff --git a/packages/opencode/src/tool/edit.ts b/packages/opencode/src/tool/edit.tsindex 3dd332aee80..856400a975f 100644--- a/packages/opencode/src/tool/edit.ts+++ b/packages/opencode/src/tool/edit.ts@@ -92,7 +92,7 @@ export const EditTool = Tool.define({return}- const read = FileTimes.get(filePath)+ const read = FileTimes.get(ctx.sessionID, filePath)if (!read)throw new Error(`You must read the file ${filePath} before editing it. Use the View tool first`,@@ -129,8 +129,7 @@ export const EditTool = Tool.define({const changes = diffLines(contentOld, contentNew)- FileTimes.write(filePath)- FileTimes.read(filePath)+ FileTimes.read(ctx.sessionID, filePath)let output = ""await LSP.file(filePath)diff --git a/packages/opencode/src/tool/patch.ts b/packages/opencode/src/tool/patch.tsindex 68d2cfeed13..aa733ddbac4 100644--- a/packages/opencode/src/tool/patch.ts+++ b/packages/opencode/src/tool/patch.ts@@ -269,7 +269,7 @@ export const PatchTool = Tool.define({id: "opencode.patch",description: DESCRIPTION,parameters: PatchParams,- execute: async (params) => {+ execute: async (params, ctx) => {if (!params.patchText) {throw new Error("patchText is required")}@@ -282,7 +282,7 @@ export const PatchTool = Tool.define({absPath = path.resolve(process.cwd(), absPath)}- if (!FileTimes.get(absPath)) {+ if (!FileTimes.get(ctx.sessionID, absPath)) {throw new Error(`you must read the file ${filePath} before patching it. Use the FileRead tool first`,)@@ -294,7 +294,7 @@ export const PatchTool = Tool.define({throw new Error(`path is a directory, not a file: ${absPath}`)}- const lastRead = FileTimes.get(absPath)+ const lastRead = FileTimes.get(ctx.sessionID, absPath)if (lastRead && stats.mtime > lastRead) {throw new Error(`file ${absPath} has been modified since it was last read (mod time: ${stats.mtime.toISOString()}, last read: ${lastRead.toISOString()})`,@@ -400,9 +400,7 @@ export const PatchTool = Tool.define({totalAdditions += additionstotalRemovals += removals- // Record file operations- FileTimes.write(absPath)- FileTimes.read(absPath)+ FileTimes.read(ctx.sessionID, absPath)}const result = `Patch applied successfully. ${changedFiles.length} files changed, ${totalAdditions} additions, ${totalRemovals} removals`diff --git a/packages/opencode/src/tool/util/file-times.ts b/packages/opencode/src/tool/util/file-times.tsindex 3eac09aab0d..19589d549ca 100644--- a/packages/opencode/src/tool/util/file-times.ts+++ b/packages/opencode/src/tool/util/file-times.ts@@ -1,20 +1,24 @@import { App } from "../../app/app"export namespace FileTimes {- export const state = App.state("tool.filetimes", () => ({- read: new Map<string, Date>(),- write: new Map<string, Date>(),- }))+ export const state = App.state("tool.filetimes", () => {+ const read: {+ [sessionID: string]: {+ [path: string]: Date | undefined+ }+ } = {}+ return {+ read,+ }+ })- export function read(filePath: string) {- state().read.set(filePath, new Date())+ export function read(sessionID: string, file: string) {+ const { read } = state()+ read[sessionID] = read[sessionID] || {}+ read[sessionID][file] = new Date()}- export function write(filePath: string) {- state().write.set(filePath, new Date())- }-- export function get(filePath: string): Date | null {- return state().read.get(filePath) || null+ export function get(sessionID: string, file: string) {+ return state().read[sessionID]?.[file]}}diff --git a/packages/opencode/src/tool/view.ts b/packages/opencode/src/tool/view.tsindex c17aa1d2290..6e29eb45034 100644--- a/packages/opencode/src/tool/view.ts+++ b/packages/opencode/src/tool/view.ts@@ -54,7 +54,7 @@ export const ViewTool = Tool.define({.describe("The number of lines to read (defaults to 2000)").optional(),}),- async execute(params) {+ async execute(params, ctx) {let filePath = params.filePathif (!path.isAbsolute(filePath)) {filePath = path.join(process.cwd(), filePath)@@ -119,7 +119,7 @@ export const ViewTool = Tool.define({// just warms the lsp clientLSP.file(filePath)- FileTimes.read(filePath)+ FileTimes.read(ctx.sessionID, filePath)return {output,