Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions packages/opencode/src/file/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,29 @@ export namespace FileTime {
// All tools that overwrite existing files should run their
// assert/read/write/update sequence inside withLock(filepath, ...)
// so concurrent writes to the same file are serialized.
export const state = Instance.state(() => {
const read: {
[sessionID: string]: {
[path: string]: Date | undefined
export const state = Instance.state(
() => {
const read: {
[sessionID: string]: {
[path: string]: Date | undefined
}
} = {}
const locks = new Map<string, Promise<void>>()
return {
read,
locks,
}
} = {}
const locks = new Map<string, Promise<void>>()
return {
read,
locks,
}
})
},
// Explicit cleanup on instance disposal. While the state object is GC'd
// when the instance is disposed, this makes the lifecycle explicit for
// long-running sessions with many subtasks.
async (current) => {
for (const key of Object.keys(current.read)) {
delete current.read[key]
}
current.locks.clear()
},
)

export function read(sessionID: string, file: string) {
log.info("read", { sessionID, file })
Expand Down Expand Up @@ -61,4 +72,10 @@ export namespace FileTime {
)
}
}

// Clears read timestamps for a deleted session. Called by Session.remove()
// to prevent orphaned entries from accumulating when sessions/subtasks are deleted.
export function clearSession(sessionID: string) {
delete state().read[sessionID]
}
}
3 changes: 3 additions & 0 deletions packages/opencode/src/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { SessionPrompt } from "./prompt"
import { fn } from "@/util/fn"
import { Command } from "../command"
import { Snapshot } from "@/snapshot"
import { FileTime } from "@/file/time"

import type { Provider } from "@/provider/provider"
import { PermissionNext } from "@/permission/next"
Expand Down Expand Up @@ -319,6 +320,8 @@ export namespace Session {
await Storage.remove(msg)
}
await Storage.remove(["session", project.id, sessionID])
// Clear file read timestamps - session is gone, timestamps are orphaned
FileTime.clearSession(sessionID)
Bus.publish(Event.Deleted, {
info: session,
})
Expand Down