Skip to content

Commit bccbfc1

Browse files
committed
fix(session): add cross-project session lookup with fallback search
Session.get() was failing when sessions were accessed from a different project context (subagents, nested PTYs, SDK clients with different dir). - Add resolveProjectID() helper that searches all project directories - LRU cache (1000 entries) for resolved projectIDs - Update get(), update(), remove() to use the helper Fixes anomalyco#8538 Related to anomalyco#7773 Builds on approach from anomalyco#3018 by @zenyr
1 parent c2f9fd5 commit bccbfc1

1 file changed

Lines changed: 60 additions & 5 deletions

File tree

‎packages/opencode/src/session/index.ts‎

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,63 @@ import { Snapshot } from "@/snapshot"
2222
import type { Provider } from "@/provider/provider"
2323
import { PermissionNext } from "@/permission/next"
2424
import { Global } from "@/global"
25+
import fs from "fs/promises"
2526

2627
export namespace Session {
2728
const log = Log.create({ service: "session" })
2829

30+
// LRU-like cache for session→projectID mapping (avoids repeated directory scans)
31+
const PROJECT_ID_CACHE_MAX_SIZE = 1000
32+
const projectIDCache = new Map<string, string>()
33+
34+
function cacheProjectID(sessionID: string, projectID: string): void {
35+
if (projectIDCache.size >= PROJECT_ID_CACHE_MAX_SIZE) {
36+
const firstKey = projectIDCache.keys().next().value
37+
if (firstKey) projectIDCache.delete(firstKey)
38+
}
39+
projectIDCache.set(sessionID, projectID)
40+
}
41+
42+
async function resolveProjectID(sessionID: string): Promise<string> {
43+
// Check cache first
44+
const cached = projectIDCache.get(sessionID)
45+
if (cached) {
46+
const exists = await Storage.read<Info>(["session", cached, sessionID]).catch(() => null)
47+
if (exists) return cached
48+
projectIDCache.delete(sessionID)
49+
}
50+
51+
// Try current project
52+
const currentProjectID = Instance.project.id
53+
const existsInCurrent = await Storage.read<Info>(["session", currentProjectID, sessionID]).catch(() => null)
54+
if (existsInCurrent) {
55+
cacheProjectID(sessionID, currentProjectID)
56+
return currentProjectID
57+
}
58+
59+
// Fallback: search all project directories
60+
const storageDir = path.join(Global.Path.data, "storage", "session")
61+
let projectDirs: string[] = []
62+
try {
63+
projectDirs = await fs.readdir(storageDir)
64+
} catch {
65+
throw new Storage.NotFoundError({ message: `Session not found: ${sessionID}` })
66+
}
67+
68+
for (const projectID of projectDirs) {
69+
if (projectID === currentProjectID) continue
70+
if (projectID.startsWith(".")) continue
71+
const exists = await Storage.read<Info>(["session", projectID, sessionID]).catch(() => null)
72+
if (exists) {
73+
cacheProjectID(sessionID, projectID)
74+
log.info("resolved session to different project", { sessionID, projectID, currentProjectID })
75+
return projectID
76+
}
77+
}
78+
79+
throw new Storage.NotFoundError({ message: `Session not found: ${sessionID}` })
80+
}
81+
2982
const parentTitlePrefix = "New session - "
3083
const childTitlePrefix = "Child session - "
3184

@@ -240,7 +293,8 @@ export namespace Session {
240293
}
241294

242295
export const get = fn(Identifier.schema("session"), async (id) => {
243-
const read = await Storage.read<Info>(["session", Instance.project.id, id])
296+
const projectID = await resolveProjectID(id)
297+
const read = await Storage.read<Info>(["session", projectID, id])
244298
return read as Info
245299
})
246300

@@ -273,8 +327,8 @@ export namespace Session {
273327
})
274328

275329
export async function update(id: string, editor: (session: Info) => void) {
276-
const project = Instance.project
277-
const result = await Storage.update<Info>(["session", project.id, id], (draft) => {
330+
const projectID = await resolveProjectID(id)
331+
const result = await Storage.update<Info>(["session", projectID, id], (draft) => {
278332
editor(draft)
279333
draft.time.updated = Date.now()
280334
})
@@ -324,9 +378,9 @@ export namespace Session {
324378
})
325379

326380
export const remove = fn(Identifier.schema("session"), async (sessionID) => {
327-
const project = Instance.project
328381
try {
329382
const session = await get(sessionID)
383+
const projectID = await resolveProjectID(sessionID)
330384
for (const child of await children(sessionID)) {
331385
await remove(child.id)
332386
}
@@ -337,7 +391,8 @@ export namespace Session {
337391
}
338392
await Storage.remove(msg)
339393
}
340-
await Storage.remove(["session", project.id, sessionID])
394+
await Storage.remove(["session", projectID, sessionID])
395+
projectIDCache.delete(sessionID)
341396
Bus.publish(Event.Deleted, {
342397
info: session,
343398
})

0 commit comments

Comments
 (0)