section 1: Phase 1 — Stop recomputing the session diff on every poll

This commit is contained in:
Chris Scott committed 2026-10-08 15:53:26 -04:00
1 parent e7dc106af7
commit 4b5f76f416
2 files changed
+91 -16

No files matched your search

+41 -16
View File
@@ -234,6 +234,7 @@ export class ChangeWalkthroughService {
private readonly inFlight = new Map<string, InFlightGeneration>()
private readonly failures = new Map<string, WalkthroughGenerationError>()
private readonly deletedDuringGeneration = new Set<string>()
private readonly currentHashes = new Map<string, string>()
private readonly timeoutMs: number
constructor(
@@ -247,13 +248,9 @@ export class ChangeWalkthroughService {
async getState(sessionId: string): Promise<ChangeWalkthroughState> {
await this.readSession(sessionId)
let currentDiffHash: string | null = null
try {
const changes = await readSessionChanges(this.openCodeClient, sessionId)
currentDiffHash = computeChangesHash(changes)
} catch {
currentDiffHash = null
}
const currentDiffHash = this.inFlight.has(sessionId)
? this.currentHashes.get(sessionId) ?? null
: await this.readCurrentDiffHash(sessionId)
const walkthrough = getChangeWalkthrough(this.db, sessionId)
return {
@@ -265,6 +262,22 @@ export class ChangeWalkthroughService {
}
}
private async readCurrentDiffHash(sessionId: string): Promise<string | null> {
const cached = this.currentHashes.get(sessionId)
if (cached !== undefined) {
return cached
}
try {
const changes = await readSessionChanges(this.openCodeClient, sessionId)
const hash = computeChangesHash(changes)
this.currentHashes.set(sessionId, hash)
return hash
} catch {
return null
}
}
generate(sessionId: string, request: GenerateChangeWalkthroughRequest): Promise<{ walkthrough: ChangeWalkthrough; created: boolean }> {
return this.begin(sessionId, request).result
}
@@ -277,16 +290,26 @@ export class ChangeWalkthroughService {
/** Removes the stored walkthrough of a deleted session, including one still being generated. */
handleEvent(event: SSEEvent): void {
if (event.type !== 'session.deleted') {
return
switch (event.type) {
case 'session.deleted': {
const { sessionID } = event.data
if (this.inFlight.has(sessionID)) {
this.deletedDuringGeneration.add(sessionID)
}
this.failures.delete(sessionID)
this.currentHashes.delete(sessionID)
deleteChangeWalkthrough(this.db, sessionID)
return
}
case 'session.execution.started':
case 'session.execution.succeeded':
case 'session.execution.failed':
case 'session.execution.interrupted':
this.currentHashes.delete(event.data.sessionID)
return
default:
return
}
const { sessionID } = event.data
if (this.inFlight.has(sessionID)) {
this.deletedDuringGeneration.add(sessionID)
}
this.failures.delete(sessionID)
deleteChangeWalkthrough(this.db, sessionID)
}
private begin(sessionId: string, request: GenerateChangeWalkthroughRequest): InFlightGeneration {
@@ -317,6 +340,7 @@ export class ChangeWalkthroughService {
.finally(() => {
this.inFlight.delete(sessionId)
this.deletedDuringGeneration.delete(sessionId)
this.currentHashes.delete(sessionId)
})
result.catch(() => {})
@@ -349,6 +373,7 @@ export class ChangeWalkthroughService {
}
const diffHash = computeChangesHash(changes)
this.currentHashes.set(sessionId, diffHash)
const stored = getChangeWalkthrough(this.db, sessionId)
if (stored && stored.diffHash === diffHash && !request.regenerate) {
return { walkthrough: stored, created: false }