Repository navigation
feat(terminal): dump confirmed divergence corpus #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
| import { mkdtemp, readFile, rm } from 'node:fs/promises' | ||
| import { tmpdir } from 'node:os' | ||
| import { basename, join } from 'node:path' | ||
| import { | ||
| TERM_FIDELITY_CORPUS_DUMP_GAP_MS, | ||
| TERM_FIDELITY_CORPUS_WARN_GAP_MS, | ||
| TermFidelityCorpusDumper | ||
| } from './term-fidelity-corpus' | ||
| import type { TermFidelityCorpusInput } from '../shared/types/ipc' | ||
|
|
||
| const temporaryRoots: string[] = [] | ||
|
|
||
| async function temporaryRoot(): Promise<string> { | ||
| const root = await mkdtemp(join(tmpdir(), 'pear-term-fidelity-corpus-')) | ||
| temporaryRoots.push(root) | ||
| return root | ||
| } | ||
|
|
||
| function corpusInput(agentName = 'codex-1'): TermFidelityCorpusInput { | ||
| return { | ||
| projectId: 'project-1', | ||
| agentName, | ||
| cli: 'codex', | ||
| renderer: { rows: 2, cols: 10, text: 'row one\nGARBAGE' }, | ||
| broker: { rows: 2, cols: 10, text: 'row one\nrow two' }, | ||
| telemetryLines: [ | ||
| '[terminal] viewport diverged from broker screen; confirmed after 2 quiet checks' | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| afterEach(async () => { | ||
| await Promise.all(temporaryRoots.splice(0).map((root) => rm(root, { recursive: true, force: true }))) | ||
| }) | ||
|
|
||
| describe('TermFidelityCorpusDumper', () => { | ||
| it('rejects malformed IPC payloads before accessing nested fields', () => { | ||
| const dumper = new TermFidelityCorpusDumper() | ||
| const options = { | ||
| rootDir: '/tmp/not-used', | ||
| capturePage: async () => Buffer.from('unused'), | ||
| relayVersions: async () => ({}) | ||
| } | ||
|
|
||
| expect(() => dumper.dump(null as unknown as TermFidelityCorpusInput, options)).toThrow( | ||
| 'input must be an object' | ||
| ) | ||
| expect(() => dumper.dump({ agentName: 'a', cli: 'codex' } as TermFidelityCorpusInput, options)).toThrow( | ||
| 'renderer grid must be an object' | ||
| ) | ||
| }) | ||
|
|
||
| it('writes the production bundle layout and exactly one log line', async () => { | ||
| const rootDir = await temporaryRoot() | ||
| const screenshot = Buffer.from([0x89, 0x50, 0x4e, 0x47]) | ||
| const capturePage = vi.fn(async () => screenshot) | ||
| const log = vi.fn() | ||
| const now = Date.parse('2026-07-17T12:34:56.789Z') | ||
| const dumper = new TermFidelityCorpusDumper({ now: () => now, log }) | ||
|
|
||
| const result = await dumper.dump(corpusInput(), { | ||
| rootDir, | ||
| capturePage, | ||
| relayVersions: async () => ({ | ||
| broker: '10.6.3', | ||
| '@agent-relay/sdk': '^10.6.3' | ||
| }) | ||
| }) | ||
|
|
||
| expect(result.dumped).toBe(true) | ||
| if (!result.dumped) throw new Error('Expected a corpus dump') | ||
| expect(basename(result.path)).toBe('2026-07-17T12-34-56-789Z') | ||
| expect(await readFile(join(result.path, 'renderer.txt'), 'utf8')).toBe('row one\nGARBAGE') | ||
| expect(await readFile(join(result.path, 'broker.txt'), 'utf8')).toBe('row one\nrow two') | ||
| expect(await readFile(join(result.path, 'screen.png'))).toEqual(screenshot) | ||
| expect(JSON.parse(await readFile(join(result.path, 'meta.json'), 'utf8'))).toEqual({ | ||
| capturedAt: '2026-07-17T12:34:56.789Z', | ||
| projectId: 'project-1', | ||
| agentName: 'codex-1', | ||
| cli: 'codex', | ||
| dims: { | ||
| renderer: { rows: 2, cols: 10 }, | ||
| broker: { rows: 2, cols: 10 } | ||
| }, | ||
| relayVersions: { | ||
| broker: '10.6.3', | ||
| '@agent-relay/sdk': '^10.6.3' | ||
| }, | ||
| reconcilerTelemetryLines: corpusInput().telemetryLines | ||
| }) | ||
| expect(capturePage).toHaveBeenCalledTimes(1) | ||
| expect(log).toHaveBeenCalledTimes(1) | ||
| expect(log).toHaveBeenCalledWith(expect.stringContaining(result.path)) | ||
| }) | ||
|
|
||
| it('coalesces concurrent triggers and rate-limits each project + agent for five minutes', async () => { | ||
| const rootDir = await temporaryRoot() | ||
| let currentTime = 1_000_000 | ||
| let releaseCapture: ((png: Buffer) => void) | undefined | ||
| const capturePage = vi.fn( | ||
| () => new Promise<Buffer>((resolve) => { | ||
| releaseCapture = resolve | ||
| }) | ||
| ) | ||
| const log = vi.fn() | ||
| const dumper = new TermFidelityCorpusDumper({ now: () => currentTime, log }) | ||
| const options = { rootDir, capturePage, relayVersions: async () => ({}) } | ||
|
|
||
| const first = dumper.dump(corpusInput(), options) | ||
| const duplicate = dumper.dump(corpusInput(), options) | ||
| expect(duplicate).toBe(first) | ||
| expect(capturePage).toHaveBeenCalledTimes(1) | ||
| releaseCapture?.(Buffer.from('png')) | ||
| expect((await first).dumped).toBe(true) | ||
| expect((await duplicate).dumped).toBe(true) | ||
|
|
||
| const limited = await dumper.dump(corpusInput(), options) | ||
| expect(limited).toEqual({ dumped: false, reason: 'rate-limited' }) | ||
| expect(capturePage).toHaveBeenCalledTimes(1) | ||
|
|
||
| // A distinct agent has an independent budget. | ||
| const otherAgent = dumper.dump(corpusInput('codex-2'), { | ||
| ...options, | ||
| capturePage: vi.fn(async () => Buffer.from('other')) | ||
| }) | ||
| expect((await otherAgent).dumped).toBe(true) | ||
|
|
||
| currentTime += TERM_FIDELITY_CORPUS_DUMP_GAP_MS | ||
| const afterGap = dumper.dump(corpusInput(), { | ||
| ...options, | ||
| capturePage: vi.fn(async () => Buffer.from('after-gap')) | ||
| }) | ||
| expect((await afterGap).dumped).toBe(true) | ||
| expect(log).toHaveBeenCalledTimes(3) | ||
| }) | ||
|
|
||
| it('degrades capture failures to a rate-limited warning and allows retry', async () => { | ||
| const rootDir = await temporaryRoot() | ||
| let currentTime = 2_000_000 | ||
| const warn = vi.fn() | ||
| const dumper = new TermFidelityCorpusDumper({ now: () => currentTime, warn }) | ||
| const failingOptions = { | ||
| rootDir, | ||
| capturePage: async (): Promise<Buffer> => { | ||
| throw new Error('capture\nfailed') | ||
| }, | ||
| relayVersions: async () => ({}) | ||
| } | ||
|
|
||
| expect(await dumper.dump(corpusInput(), failingOptions)).toEqual({ | ||
| dumped: false, | ||
| reason: 'failed' | ||
| }) | ||
| expect(await dumper.dump(corpusInput(), failingOptions)).toEqual({ | ||
| dumped: false, | ||
| reason: 'failed' | ||
| }) | ||
| expect(warn).toHaveBeenCalledTimes(1) | ||
| expect(warn).toHaveBeenCalledWith(expect.not.stringContaining('\nfailed')) | ||
|
|
||
| currentTime += TERM_FIDELITY_CORPUS_WARN_GAP_MS | ||
| expect(await dumper.dump(corpusInput(), failingOptions)).toEqual({ | ||
| dumped: false, | ||
| reason: 'failed' | ||
| }) | ||
| expect(warn).toHaveBeenCalledTimes(2) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
package.jsonparses tonull(which is technically valid JSON),manifestwill benull. Accessingmanifest.dependencieswould then throw aTypeError. Although this is wrapped in atry/catchblock, using optional chaining (manifest?.dependencies) and typingmanifestas nullable is more robust and prevents throwing unnecessary exceptions.