fix(hooks): preserve transcript cache history across rebuilds

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
YeonGyu-Kim
2026-04-04 01:13:02 +09:00
parent 58e85960a1
commit 35c34ea06b
@@ -165,10 +165,12 @@ export async function buildTranscriptFromSession(
): Promise<string | null> { ): Promise<string | null> {
try { try {
let baseEntries: string[] let baseEntries: string[]
let previousTempPath: string | null = null
const cached = transcriptCache.get(sessionId) const cached = transcriptCache.get(sessionId)
if (cached && isCacheValid(cached)) { if (cached && isCacheValid(cached)) {
baseEntries = cached.baseEntries baseEntries = cached.baseEntries
previousTempPath = cached.tempPath
} else { } else {
// Fetch full session messages (only on first call or cache expiry) // Fetch full session messages (only on first call or cache expiry)
const response = await client.session.messages({ const response = await client.session.messages({
@@ -199,6 +201,10 @@ export async function buildTranscriptFromSession(
// Append current tool call // Append current tool call
const allEntries = [...baseEntries, buildCurrentEntry(currentToolName, currentToolInput)] const allEntries = [...baseEntries, buildCurrentEntry(currentToolName, currentToolInput)]
if (previousTempPath) {
try { unlinkSync(previousTempPath) } catch { /* ignore */ }
}
const tempPath = join( const tempPath = join(
tmpdir(), tmpdir(),
`opencode-transcript-${sessionId}-${randomUUID()}.jsonl` `opencode-transcript-${sessionId}-${randomUUID()}.jsonl`
@@ -208,7 +214,9 @@ export async function buildTranscriptFromSession(
// Update cache temp path for cleanup tracking // Update cache temp path for cleanup tracking
const cacheEntry = transcriptCache.get(sessionId) const cacheEntry = transcriptCache.get(sessionId)
if (cacheEntry) { if (cacheEntry) {
cacheEntry.baseEntries = allEntries
cacheEntry.tempPath = tempPath cacheEntry.tempPath = tempPath
cacheEntry.createdAt = Date.now()
} }
return tempPath return tempPath