mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
section 3: Phase 3 — Mechanical changes stop
This commit is contained in:
1 parent
7bf6c35f29
commit
583a99ffb8
2 files changed
+221
-19
No files matched your search
@@ -19,7 +19,7 @@ import {
|
||||
type WalkthroughOmittedFile,
|
||||
type WalkthroughStop,
|
||||
} from '@opencode-manager/shared/schemas'
|
||||
import { splitDiffHunks } from '@opencode-manager/shared/utils'
|
||||
import { splitDiffHunks, type DiffHunk } from '@opencode-manager/shared/utils'
|
||||
import { deleteChangeWalkthrough, getChangeWalkthrough, saveChangeWalkthrough } from '../db/change-walkthroughs'
|
||||
import { getErrorMessage } from '../utils/error-utils'
|
||||
import { ServiceError } from '../utils/service-error'
|
||||
@@ -35,6 +35,41 @@ const HUNK_TRUNCATION_MARKER = '\n[hunk truncated]'
|
||||
const TEXT_TRUNCATION_MARKER = ''
|
||||
const REMAINING_STOP_TITLE = 'Remaining changes'
|
||||
const REMAINING_STOP_EXPLANATION = 'These changes were not covered by the generated walkthrough.'
|
||||
const MECHANICAL_STOP_TITLE = 'Mechanical changes'
|
||||
const MECHANICAL_STOP_EXPLANATION =
|
||||
'Lock files, snapshots and generated files. Their contents were not sent to the model.'
|
||||
const MECHANICAL_ONLY_SUMMARY = 'Only lock files, snapshots or generated files changed.'
|
||||
|
||||
const MECHANICAL_LOCKFILE_NAMES = new Set([
|
||||
'pnpm-lock.yaml',
|
||||
'package-lock.json',
|
||||
'npm-shrinkwrap.json',
|
||||
'yarn.lock',
|
||||
'bun.lock',
|
||||
'bun.lockb',
|
||||
'Cargo.lock',
|
||||
'Gemfile.lock',
|
||||
'composer.lock',
|
||||
'poetry.lock',
|
||||
'uv.lock',
|
||||
'Pipfile.lock',
|
||||
'go.sum',
|
||||
'flake.lock',
|
||||
])
|
||||
|
||||
const MECHANICAL_SUFFIXES = ['.snap', '.min.js', '.min.css', '.map']
|
||||
|
||||
export function isMechanicalChangePath(file: string): boolean {
|
||||
const segments = file.split('/')
|
||||
const basename = segments.at(-1) ?? file
|
||||
if (MECHANICAL_LOCKFILE_NAMES.has(basename)) {
|
||||
return true
|
||||
}
|
||||
if (MECHANICAL_SUFFIXES.some((suffix) => file.endsWith(suffix))) {
|
||||
return true
|
||||
}
|
||||
return segments.includes('__snapshots__')
|
||||
}
|
||||
|
||||
const modelStopSchema = z.object({
|
||||
title: z.string().catch(''),
|
||||
@@ -49,6 +84,7 @@ const modelResponseSchema = z.object({
|
||||
|
||||
export interface WalkthroughInput {
|
||||
hunks: WalkthroughHunk[]
|
||||
mechanicalHunks: WalkthroughHunk[]
|
||||
omittedFiles: WalkthroughOmittedFile[]
|
||||
}
|
||||
|
||||
@@ -109,24 +145,14 @@ export function formatHunkBlock(hunk: WalkthroughHunk): string {
|
||||
|
||||
export function buildWalkthroughInput(changes: FileDiffInfo[], title: string): WalkthroughInput {
|
||||
const hunks: WalkthroughHunk[] = []
|
||||
const mechanicalHunks: WalkthroughHunk[] = []
|
||||
const omittedFiles: WalkthroughOmittedFile[] = []
|
||||
const hunkIdCounts = new Map<string, number>()
|
||||
let total = buildWalkthroughInstructions(title).length
|
||||
let budgetExhausted = false
|
||||
|
||||
changes.forEach((change) => {
|
||||
if (budgetExhausted) {
|
||||
omittedFiles.push({ file: change.file, reason: 'budget' })
|
||||
return
|
||||
}
|
||||
|
||||
const fileHunks = splitDiffHunks(change.patch)
|
||||
if (fileHunks.length === 0) {
|
||||
omittedFiles.push({ file: change.file, reason: 'binary' })
|
||||
return
|
||||
}
|
||||
|
||||
const prepared = fileHunks.map((hunk) => {
|
||||
const prepareHunks = (change: FileDiffInfo, fileHunks: DiffHunk[]): WalkthroughHunk[] =>
|
||||
fileHunks.map((hunk) => {
|
||||
const truncated = truncateText(hunk.text, WALKTHROUGH_HUNK_MAX_CHARS, HUNK_TRUNCATION_MARKER)
|
||||
const baseId = computeHunkId(change.file, change.status, hunk.text)
|
||||
const count = (hunkIdCounts.get(baseId) ?? 0) + 1
|
||||
@@ -141,6 +167,26 @@ export function buildWalkthroughInput(changes: FileDiffInfo[], title: string): W
|
||||
} satisfies WalkthroughHunk
|
||||
})
|
||||
|
||||
changes.forEach((change) => {
|
||||
const mechanical = isMechanicalChangePath(change.file)
|
||||
|
||||
if (!mechanical && budgetExhausted) {
|
||||
omittedFiles.push({ file: change.file, reason: 'budget' })
|
||||
return
|
||||
}
|
||||
|
||||
const fileHunks = splitDiffHunks(change.patch)
|
||||
if (fileHunks.length === 0) {
|
||||
omittedFiles.push({ file: change.file, reason: 'binary' })
|
||||
return
|
||||
}
|
||||
|
||||
if (mechanical) {
|
||||
mechanicalHunks.push(...prepareHunks(change, fileHunks))
|
||||
return
|
||||
}
|
||||
|
||||
const prepared = prepareHunks(change, fileHunks)
|
||||
const fileTotal = prepared.reduce((sum, hunk) => sum + formatHunkBlock(hunk).length + 2, 0)
|
||||
if (total + fileTotal > WALKTHROUGH_DIFF_MAX_CHARS) {
|
||||
budgetExhausted = true
|
||||
@@ -152,7 +198,19 @@ export function buildWalkthroughInput(changes: FileDiffInfo[], title: string): W
|
||||
hunks.push(...prepared)
|
||||
})
|
||||
|
||||
return { hunks, omittedFiles }
|
||||
return { hunks, mechanicalHunks, omittedFiles }
|
||||
}
|
||||
|
||||
export function buildMechanicalStop(hunks: WalkthroughHunk[]): WalkthroughStop {
|
||||
const hunkIds = hunks.map((hunk) => hunk.id)
|
||||
return {
|
||||
id: computeStopId(hunkIds),
|
||||
title: MECHANICAL_STOP_TITLE,
|
||||
explanation: MECHANICAL_STOP_EXPLANATION,
|
||||
hunkIds,
|
||||
status: 'ready',
|
||||
explanationKey: null,
|
||||
}
|
||||
}
|
||||
|
||||
export function buildWalkthroughPrompt({ title, hunks }: { title: string; hunks: WalkthroughHunk[] }): string {
|
||||
@@ -401,8 +459,8 @@ export class ChangeWalkthroughService {
|
||||
}
|
||||
|
||||
const title = session.title ?? sessionId
|
||||
const { hunks, omittedFiles } = buildWalkthroughInput(changes, title)
|
||||
if (hunks.length === 0) {
|
||||
const { hunks, mechanicalHunks, omittedFiles } = buildWalkthroughInput(changes, title)
|
||||
if (hunks.length === 0 && mechanicalHunks.length === 0) {
|
||||
if (omittedFiles.some((file) => file.reason === 'budget')) {
|
||||
throw new ChangeWalkthroughError('These changes are too large to walk through', 413, {
|
||||
code: 'WALKTHROUGH_CONTEXT_LIMIT',
|
||||
@@ -414,6 +472,27 @@ export class ChangeWalkthroughService {
|
||||
})
|
||||
}
|
||||
|
||||
const mechanicalStop = mechanicalHunks.length > 0 ? buildMechanicalStop(mechanicalHunks) : null
|
||||
|
||||
if (hunks.length === 0) {
|
||||
const walkthrough: ChangeWalkthrough = {
|
||||
sessionId,
|
||||
diffHash,
|
||||
summary: MECHANICAL_ONLY_SUMMARY,
|
||||
stops: [mechanicalStop!],
|
||||
hunks: mechanicalHunks,
|
||||
omittedFiles,
|
||||
createdAt: Date.now(),
|
||||
}
|
||||
|
||||
if (this.deletedDuringGeneration.has(sessionId)) {
|
||||
throw new ChangeWalkthroughError('Session not found', 404)
|
||||
}
|
||||
|
||||
saveChangeWalkthrough(this.db, walkthrough)
|
||||
return { walkthrough, created: true }
|
||||
}
|
||||
|
||||
const prompt = buildWalkthroughPrompt({ title, hunks })
|
||||
|
||||
onModelStart()
|
||||
@@ -445,8 +524,8 @@ export class ChangeWalkthroughService {
|
||||
sessionId,
|
||||
diffHash,
|
||||
summary: parsed.summary,
|
||||
stops: parsed.stops,
|
||||
hunks,
|
||||
stops: mechanicalStop ? [...parsed.stops, mechanicalStop] : parsed.stops,
|
||||
hunks: [...hunks, ...mechanicalHunks],
|
||||
omittedFiles,
|
||||
createdAt: Date.now(),
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import {
|
||||
computeChangesHash,
|
||||
computeHunkId,
|
||||
computeStopId,
|
||||
isMechanicalChangePath,
|
||||
parseWalkthroughResponse,
|
||||
} from '../../src/services/change-walkthroughs'
|
||||
|
||||
@@ -171,6 +172,44 @@ describe('computeChangesHash', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('isMechanicalChangePath', () => {
|
||||
it('matches lockfiles by basename', () => {
|
||||
const lockfiles = [
|
||||
'pnpm-lock.yaml',
|
||||
'package-lock.json',
|
||||
'npm-shrinkwrap.json',
|
||||
'yarn.lock',
|
||||
'bun.lock',
|
||||
'bun.lockb',
|
||||
'Cargo.lock',
|
||||
'Gemfile.lock',
|
||||
'composer.lock',
|
||||
'poetry.lock',
|
||||
'uv.lock',
|
||||
'Pipfile.lock',
|
||||
'go.sum',
|
||||
'flake.lock',
|
||||
'deep/nested/pnpm-lock.yaml',
|
||||
]
|
||||
expect(lockfiles.every(isMechanicalChangePath)).toBe(true)
|
||||
})
|
||||
|
||||
it('matches generated suffixes and snapshot directories', () => {
|
||||
expect(isMechanicalChangePath('src/__snapshots__/a.ts.snap')).toBe(true)
|
||||
expect(isMechanicalChangePath('src/__snapshots__/a.ts')).toBe(true)
|
||||
expect(isMechanicalChangePath('dist/bundle.min.js')).toBe(true)
|
||||
expect(isMechanicalChangePath('dist/app.min.css')).toBe(true)
|
||||
expect(isMechanicalChangePath('dist/bundle.js.map')).toBe(true)
|
||||
})
|
||||
|
||||
it('does not match ordinary source and config files', () => {
|
||||
expect(isMechanicalChangePath('src/lock.ts')).toBe(false)
|
||||
expect(isMechanicalChangePath('package.json')).toBe(false)
|
||||
expect(isMechanicalChangePath('src/snapshots/a.ts')).toBe(false)
|
||||
expect(isMechanicalChangePath('src/map.ts')).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('buildWalkthroughInput', () => {
|
||||
it('assigns a distinct content id and the file to each hunk', () => {
|
||||
const { hunks, omittedFiles } = buildWalkthroughInput(
|
||||
@@ -292,6 +331,40 @@ describe('buildWalkthroughInput', () => {
|
||||
buildWalkthroughPrompt({ title: longTitle, hunks: withLongTitle.hunks }).length,
|
||||
).toBeLessThanOrEqual(WALKTHROUGH_DIFF_MAX_CHARS)
|
||||
})
|
||||
|
||||
it('separates mechanical hunks from the prompt hunks', () => {
|
||||
const { hunks, mechanicalHunks, omittedFiles } = buildWalkthroughInput(
|
||||
[change('src/a.ts', hunkPatch(1)), change('pnpm-lock.yaml', hunkPatch(2))],
|
||||
'My session',
|
||||
)
|
||||
|
||||
expect(hunks.map((hunk) => hunk.file)).toEqual(['src/a.ts'])
|
||||
expect(mechanicalHunks.map((hunk) => hunk.file)).toEqual(['pnpm-lock.yaml'])
|
||||
expect(omittedFiles).toEqual([])
|
||||
})
|
||||
|
||||
it('keeps mechanical hunks out of the prompt budget', () => {
|
||||
const changes = [
|
||||
...Array.from({ length: 10 }, (_, index) => change(`src/f${index}.ts`, bigHunk(index))),
|
||||
change('pnpm-lock.yaml', hunkPatch(1)),
|
||||
]
|
||||
|
||||
const { hunks, mechanicalHunks, omittedFiles } = buildWalkthroughInput(changes, 'My session')
|
||||
|
||||
expect(hunks.length).toBeLessThan(changes.length - 1)
|
||||
expect(mechanicalHunks.map((hunk) => hunk.file)).toEqual(['pnpm-lock.yaml'])
|
||||
expect(omittedFiles.some((file) => file.file === 'pnpm-lock.yaml')).toBe(false)
|
||||
})
|
||||
|
||||
it('omits a binary mechanical file as binary', () => {
|
||||
const { mechanicalHunks, omittedFiles } = buildWalkthroughInput(
|
||||
[change('pnpm-lock.yaml', 'diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml\nBinary files differ')],
|
||||
'My session',
|
||||
)
|
||||
|
||||
expect(mechanicalHunks).toEqual([])
|
||||
expect(omittedFiles).toEqual([{ file: 'pnpm-lock.yaml', reason: 'binary' }])
|
||||
})
|
||||
})
|
||||
|
||||
describe('buildWalkthroughPrompt', () => {
|
||||
@@ -519,6 +592,45 @@ describe('ChangeWalkthroughService', () => {
|
||||
expect(walkthrough.stops.at(-1)!.title).toBe('Remaining changes')
|
||||
})
|
||||
|
||||
it('keeps mechanical hunks out of the prompt and appends a mechanical stop', async () => {
|
||||
const lockText = hunkPatch(7)
|
||||
sessions[SESSION_ID]!.changes = [change('src/a.ts', hunkPatch(1)), change('pnpm-lock.yaml', lockText)]
|
||||
fake.setGenerateImpl(async () =>
|
||||
modelReply([{ title: 'A', explanation: 'x', hunkIds: [computeHunkId('src/a.ts', 'modified', hunkPatch(1))] }]),
|
||||
)
|
||||
|
||||
const { walkthrough } = await service.generate(SESSION_ID, {})
|
||||
|
||||
expect(fake.generateCalls).toHaveLength(1)
|
||||
expect(fake.generateCalls[0]).not.toContain('pnpm-lock.yaml')
|
||||
const mechanical = walkthrough.stops.at(-1)!
|
||||
expect(mechanical.title).toBe('Mechanical changes')
|
||||
expect(mechanical.hunkIds).toEqual([computeHunkId('pnpm-lock.yaml', 'modified', lockText)])
|
||||
|
||||
const referenced = walkthrough.stops.flatMap((stop) => stop.hunkIds)
|
||||
expect(new Set(referenced).size).toBe(referenced.length)
|
||||
expect(new Set(referenced)).toEqual(new Set(walkthrough.hunks.map((hunk) => hunk.id)))
|
||||
})
|
||||
|
||||
it('stores only a mechanical stop without a model call', async () => {
|
||||
sessions[SESSION_ID]!.changes = [
|
||||
change('pnpm-lock.yaml', hunkPatch(1)),
|
||||
change('src/__snapshots__/a.ts.snap', hunkPatch(2)),
|
||||
]
|
||||
|
||||
const { walkthrough, created } = await service.generate(SESSION_ID, {})
|
||||
|
||||
expect(created).toBe(true)
|
||||
expect(fake.generateCalls).toHaveLength(0)
|
||||
expect(walkthrough.summary).toBe('Only lock files, snapshots or generated files changed.')
|
||||
expect(walkthrough.stops).toHaveLength(1)
|
||||
expect(walkthrough.stops[0]!.title).toBe('Mechanical changes')
|
||||
expect(walkthrough.stops[0]!.hunkIds).toHaveLength(2)
|
||||
expect(new Set(walkthrough.stops[0]!.hunkIds)).toEqual(
|
||||
new Set(walkthrough.hunks.map((hunk) => hunk.id)),
|
||||
)
|
||||
})
|
||||
|
||||
it('returns the stored walkthrough without a model call when changes are unchanged', async () => {
|
||||
fake.setGenerateImpl(async () => modelReply([{ title: 'A', explanation: 'x', hunkIds: THREE_HUNK_IDS }]))
|
||||
const first = await service.generate(SESSION_ID, {})
|
||||
@@ -674,6 +786,17 @@ describe('ChangeWalkthroughService', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('stores a mechanical-only walkthrough without a model call', async () => {
|
||||
sessions[SESSION_ID]!.changes = [change('pnpm-lock.yaml', hunkPatch(1))]
|
||||
|
||||
const state = await service.startGeneration(SESSION_ID, {})
|
||||
|
||||
expect(fake.generateCalls).toHaveLength(0)
|
||||
expect(state.generating).toBe(false)
|
||||
expect(state.walkthrough?.stops).toHaveLength(1)
|
||||
expect(state.walkthrough?.stops[0]!.title).toBe('Mechanical changes')
|
||||
})
|
||||
|
||||
it('rejects with 409 when the session has no changes', async () => {
|
||||
sessions[SESSION_ID]!.changes = []
|
||||
|
||||
|
||||
Reference in new issue
Block a user