Răsfoiți Sursa

scope filetimes to session

Dax Raad 8 luni în urmă
părinte
comite
54b99cd88a

+ 2 - 3
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)

+ 4 - 6
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 += additions
       totalRemovals += 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`

+ 16 - 12
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]
   }
 }

+ 2 - 2
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.filePath
     if (!path.isAbsolute(filePath)) {
       filePath = path.join(process.cwd(), filePath)
@@ -119,7 +119,7 @@ export const ViewTool = Tool.define({
 
     // just warms the lsp client
     LSP.file(filePath)
-    FileTimes.read(filePath)
+    FileTimes.read(ctx.sessionID, filePath)
 
     return {
       output,