mirror of
https://github.com/NanmiCoder/cc-haha
synced 2026-07-16 13:03:31 +08:00
Deleting a desktop session removed the transcript but left IM adapter chat mappings and live WebSocket state able to point at the old session. The server now closes the active session socket and removes adapter mappings after deletion, while adapters refresh the shared session store before reads so a running process cannot reuse stale in-memory data. Constraint: Adapter session mappings are shared through adapter-sessions.json across long-running IM processes Rejected: Patch each adapter ensureSession path separately | shared store refresh fixes all current adapters and avoids drift Confidence: high Scope-risk: moderate Directive: Do not cache adapter session mappings without invalidating after server-side session deletion Tested: bun test common/__tests__/session-store.test.ts common/__tests__/ws-bridge.test.ts Tested: bun test src/server/__tests__/websocket-handler.test.ts src/server/__tests__/sessions.test.ts --timeout 30000 Tested: cd adapters && bunx tsc --noEmit Tested: bun run check:adapters Tested: bun run check:server Not-tested: bun run verify fails on pre-existing agent-utils coverage ratchet; changed-line coverage is 95.92% and affected lanes pass Related: https://github.com/NanmiCoder/cc-haha/issues/305
83 lines
2.0 KiB
TypeScript
83 lines
2.0 KiB
TypeScript
import * as fs from 'node:fs'
|
|
import * as path from 'node:path'
|
|
import * as os from 'node:os'
|
|
|
|
export type SessionEntry = {
|
|
sessionId: string
|
|
workDir: string
|
|
updatedAt: number
|
|
}
|
|
|
|
type StoreData = Record<string, SessionEntry>
|
|
|
|
function getDefaultPath(): string {
|
|
const configDir = process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), '.claude')
|
|
return path.join(configDir, 'adapter-sessions.json')
|
|
}
|
|
|
|
export class SessionStore {
|
|
private data: StoreData
|
|
private filePath: string
|
|
|
|
constructor(filePath?: string) {
|
|
this.filePath = filePath ?? getDefaultPath()
|
|
this.data = this.load()
|
|
}
|
|
|
|
get(chatId: string): SessionEntry | null {
|
|
this.refresh()
|
|
return this.data[chatId] ?? null
|
|
}
|
|
|
|
set(chatId: string, sessionId: string, workDir: string): void {
|
|
this.refresh()
|
|
this.data[chatId] = { sessionId, workDir, updatedAt: Date.now() }
|
|
this.save()
|
|
}
|
|
|
|
delete(chatId: string): void {
|
|
this.refresh()
|
|
delete this.data[chatId]
|
|
this.save()
|
|
}
|
|
|
|
deleteBySessionId(sessionId: string): string[] {
|
|
this.refresh()
|
|
const removed: string[] = []
|
|
for (const [chatId, entry] of Object.entries(this.data)) {
|
|
if (entry.sessionId !== sessionId) continue
|
|
delete this.data[chatId]
|
|
removed.push(chatId)
|
|
}
|
|
if (removed.length > 0) {
|
|
this.save()
|
|
}
|
|
return removed
|
|
}
|
|
|
|
listAll(): Array<{ chatId: string } & SessionEntry> {
|
|
this.refresh()
|
|
return Object.entries(this.data).map(([chatId, entry]) => ({ chatId, ...entry }))
|
|
}
|
|
|
|
private refresh(): void {
|
|
this.data = this.load()
|
|
}
|
|
|
|
private load(): StoreData {
|
|
try {
|
|
return JSON.parse(fs.readFileSync(this.filePath, 'utf-8'))
|
|
} catch {
|
|
return {}
|
|
}
|
|
}
|
|
|
|
private save(): void {
|
|
const dir = path.dirname(this.filePath)
|
|
fs.mkdirSync(dir, { recursive: true })
|
|
const tmp = `${this.filePath}.tmp.${Date.now()}`
|
|
fs.writeFileSync(tmp, JSON.stringify(this.data, null, 2) + '\n')
|
|
fs.renameSync(tmp, this.filePath)
|
|
}
|
|
}
|