mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
section 1: Phase 1 — Stop recomputing the session diff on every poll
This commit is contained in:
1 parent
e7dc106af7
commit
4b5f76f416
2 files changed
+91
-16
No files matched your search
@@ -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 }
|
||||
|
||||
@@ -411,6 +411,55 @@ describe('ChangeWalkthroughService', () => {
|
||||
expect(state.stale).toBe(false)
|
||||
})
|
||||
|
||||
it('does not read changes while generating', async () => {
|
||||
let resolveGenerate: (text: string) => void = () => {}
|
||||
fake.setGenerateImpl(() => new Promise<string>((resolve) => {
|
||||
resolveGenerate = resolve
|
||||
}))
|
||||
|
||||
const pending = service.generate(SESSION_ID, {})
|
||||
await vi.waitFor(() => expect(fake.generateCalls).toHaveLength(1))
|
||||
|
||||
const diff = vi.mocked(fake.client.api.session.diff)
|
||||
const messages = vi.mocked(fake.client.api.message.list)
|
||||
const diffCalls = diff.mock.calls.length
|
||||
const messageCalls = messages.mock.calls.length
|
||||
|
||||
await service.getState(SESSION_ID)
|
||||
await service.getState(SESSION_ID)
|
||||
|
||||
expect(diff.mock.calls.length).toBe(diffCalls)
|
||||
expect(messages.mock.calls.length).toBe(messageCalls)
|
||||
|
||||
resolveGenerate(modelReply([{ title: 'A', explanation: 'x', hunkIds: ['f0h0', 'f0h1', 'f1h0'] }]))
|
||||
await pending
|
||||
})
|
||||
|
||||
it('caches the current hash until a session execution event', async () => {
|
||||
await service.getState(SESSION_ID)
|
||||
await service.getState(SESSION_ID)
|
||||
|
||||
expect(vi.mocked(fake.client.api.session.diff)).toHaveBeenCalledTimes(1)
|
||||
expect(vi.mocked(fake.client.api.message.list)).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('recomputes the hash after session.execution.succeeded', async () => {
|
||||
fake.setGenerateImpl(async () => modelReply([{ title: 'A', explanation: 'x', hunkIds: ['f0h0', 'f0h1', 'f1h0'] }]))
|
||||
await service.generate(SESSION_ID, {})
|
||||
|
||||
await service.getState(SESSION_ID)
|
||||
sessions[SESSION_ID]!.changes = [change('src/a.ts', hunkPatch(99))]
|
||||
|
||||
const cached = await service.getState(SESSION_ID)
|
||||
expect(cached.stale).toBe(false)
|
||||
|
||||
service.handleEvent(sessionEvent('session.execution.succeeded', SESSION_ID))
|
||||
|
||||
const refreshed = await service.getState(SESSION_ID)
|
||||
expect(refreshed.stale).toBe(true)
|
||||
expect(refreshed.currentDiffHash).toBe(computeChangesHash([change('src/a.ts', hunkPatch(99))]))
|
||||
})
|
||||
|
||||
it('stores stops covering every hunk exactly once', async () => {
|
||||
fake.setGenerateImpl(async () => modelReply([{ title: 'A', explanation: 'x', hunkIds: ['f0h0', 'f1h0'] }]))
|
||||
|
||||
@@ -568,6 +617,7 @@ describe('ChangeWalkthroughService', () => {
|
||||
expect(state.walkthrough).toBeNull()
|
||||
expect(state.error).toBeNull()
|
||||
|
||||
await vi.waitFor(() => expect(fake.generateCalls).toHaveLength(1))
|
||||
resolveGenerate(coveringReply)
|
||||
|
||||
await vi.waitFor(async () => {
|
||||
|
||||
Reference in new issue
Block a user