返回 CodeWhale
review-adapters.test.mjs
根目录 / crates / engine / extension-host / test / review-adapters.test.mjs
1 import test from 'node:test'
2 import assert from 'node:assert/strict'
3 import { dirname, join } from 'node:path'
4 import { readFileSync } from 'node:fs'
5 import { createHash } from 'node:crypto'
6 import { transformStockSnapshot } from '../dist/stock-adapters.mjs'
7 import { createHarnessModule } from '../dist/builtin/harness.mjs'
8 import { startHost, BUNDLE } from './harness.mjs'
9 const LIMIT=16*1024*1024
10 const fixture=JSON.parse(readFileSync(new URL('../../tests/fixtures/review-host-parity.json',import.meta.url),'utf8'))
11 const snapshot=(operation,input)=>({kind:'stock_adapter',operation,input})
12 const transform=(operation,input)=>transformStockSnapshot(snapshot(operation,input))
13 const owner={plugin_id:'host:harness',generation:4,owner_token:'fixture-exact-review-job'}
14 const params=id=>({owner,execution_id:id,ticket:'opaque-core-execution-grant',deadline_ms:10000})
15 const part={number:1,diff_fingerprint:'sha256:'+'a'.repeat(64),diff_chars:42,file_count:1,files:['a/x b/x']}
16 const manifest={base_sha:'b'.repeat(40),head_sha:'a'.repeat(40),diff_fingerprint:'sha256:'+'c'.repeat(64),diff_chars:42,file_count:1,binary_file_patches:0,binary_contents_semantically_inspected:false,max_chars_per_pass:40000,passes:[part],skipped_files:[]}
17 const passInput=()=>({number:7,view:{title:'untrusted title',body:'untrusted body'},manifest,pass:part,diff:'diff --git a/x b/x\n+END\n',context:{files:[{path:'x',source:'漢字e\u0301',head_sha:manifest.head_sha}],limit:64},sort_keys:true})
18 const callerGuidance = 'If the prompt omits relevant caller bodies and read-only tools are offered, inspect those callers with the tools. Anchor findings to the commentable diff and scope coverage claims to the pass manifest.'
19 const sorted=value=>Array.isArray(value)?value.map(sorted):value&&typeof value==='object'?Object.fromEntries(Object.entries(value).sort(([a],[b])=>a<b?-1:a>b?1:0).map(([key,value])=>[key,sorted(value)])):value
20 const envelopeBytes=value=>8+Buffer.byteLength(`{"jsonrpc":"2.0","id":18446744073709551615,"result":${JSON.stringify(value)}}`)
21
22 test('frozen Core formatter counterpart matches all sixteen source interactive and local/posted report cases',()=>{
23 for(const {operation,input,content} of fixture.cases) assert.equal(transform(operation,input).result.content,content)
24 })
25 test('file numbering uses scalar source lines, CRLF and terminal lone CR without truncation',()=>{
26 const content=Array.from({length:10001},(_,i)=>i===10000?'漢字🐋e\u0301END':'x').join('\r\n')+'\r'
27 const output=transform('review_source_prompt',{kind:'file',display:'x',content}).result.content
28 assert.ok(output.includes('10000 | x\n10001 | 漢字🐋e\u0301END\r\n\nEnd of file.'))
29 assert.ok(!output.includes('...'))
30 })
31 test('complete pass preserves exact Core manifest, source provenance and serialized map order',()=>{
32 const input=passInput(), content=transform('review_pass_prompt',input).result.content,parsed=JSON.parse(content)
33 assert.deepEqual(parsed.manifest,input.manifest);assert.deepEqual(parsed.pass,input.pass);assert.deepEqual(parsed.repository_context,input.context)
34 assert.deepEqual(parsed.pull_request,{number:7,title:input.view.title,description:input.view.body});assert.equal(parsed.diff,input.diff)
35 assert.equal(parsed.untrusted_repository_data,true);assert.ok(parsed.task.includes('No build or tests have been run.'))
36 assert.ok(parsed.task.includes(callerGuidance))
37 assert.equal(content,JSON.stringify(sorted(parsed)))
38 input.sort_keys=false;const preserved=transform('review_pass_prompt',input).result.content
39 assert.ok(preserved.startsWith('{"task":'));assert.deepEqual(JSON.parse(preserved),parsed)
40 })
41 test('partial pass explicitly carries every unread file and never invents full coverage',()=>{
42 const input=passInput();input.manifest={...manifest,skipped_files:[{file:'a/unread b/unread',chars:30000,reason:'beyond the max_passes budget'}]};input.context=null
43 const parsed=JSON.parse(transform('review_pass_prompt',input).result.content)
44 assert.ok(parsed.task.includes('partial review (pass 1 of 1)'));assert.ok(parsed.task.includes('a/unread b/unread (30000 chars; beyond the max_passes budget)'));assert.ok(parsed.task.includes('Do not claim full coverage.'))
45 assert.ok(parsed.task.includes(callerGuidance))
46 assert.equal(parsed.repository_context,null);assert.deepEqual(parsed.manifest,input.manifest)
47 })
48 test('complete captured review larger than one MiB is accepted with the final patch intact',()=>{
49 const diff='漢'.repeat(400000)+'FINAL_PATCH_END',input={kind:'cli_diff',diff}
50 assert.ok(Buffer.byteLength(JSON.stringify(snapshot('review_source_prompt',input)))>1024*1024)
51 const result=transform('review_source_prompt',input).result
52 assert.ok(result.content.endsWith('FINAL_PATCH_END\n\nEnd of diff.'));assert.equal(result.success,true);assert.equal(result.metadata,null)
53 })
54 test('serialized review snapshot over its purpose cap refuses even when raw source fits',()=>{
55 const input={kind:'cli_diff',diff:'\0'.repeat(3*1024*1024)}
56 assert.ok(Buffer.byteLength(input.diff)<LIMIT);assert.ok(envelopeBytes(snapshot('review_source_prompt',input))>LIMIT)
57 assert.throws(()=>transform('review_source_prompt',input),/review.*16 MiB/)
58 })
59 test('nested prompt result escaping is separately bounded without dropping context',()=>{
60 const input=passInput();input.context={source:'\0'.repeat(2500000)}
61 assert.ok(envelopeBytes(snapshot('review_pass_prompt',input))<LIMIT)
62 assert.throws(()=>transform('review_pass_prompt',input),/review result exceeds 16 MiB/)
63 })
64 test('other helpers retain one MiB and unknown review operations never obtain a larger allowance',()=>{
65 for(const operation of ['speech_options','review_publish']) assert.throws(()=>transform(operation,{payload:'x'.repeat(1024*1024+1)}),/1 MiB/)
66 for(const [operation,input] of [['review_source_prompt',{kind:'url',url:'https://example.invalid'}],['review_pass_prompt',{...passInput(),sort_keys:null}],['review_pass_prompt',{...passInput(),context:undefined}],['review_report',{review:[],output:'',posted:false}],['review_interactive_pr',{number:2**53,view:{},diff:''}]]) assert.throws(()=>transform(operation,input))
67 })
68 test('raw prose is returned byte-exact and presenter cannot grant metadata or approval',()=>{
69 const input={review:null,output:'\0raw ```suggestion\nunknown\n```\n',posted:true},result=transform('review_report',input).result
70 assert.equal(result.content,input.output);assert.equal(result.metadata,null);assert.equal(result.success,true)
71 })
72 test('actual module redeems one opaque grant for every closed review operation',async()=>{
73 const cases=[fixture.cases[0],{operation:'review_pass_prompt',input:passInput()},fixture.cases.find(x=>x.operation==='review_interactive_pr'),fixture.cases.find(x=>x.operation==='review_report')]
74 for(const [i,entry] of cases.entries()) {
75 const calls=[],runner=createHarnessModule({async request(method,args){calls.push([method,args]);return snapshot(entry.operation,entry.input)}},owner)
76 assert.deepEqual(await runner.run(params(`case${i}`),new AbortController().signal),transform(entry.operation,entry.input))
77 assert.deepEqual(calls,[['exec/redeem',{owner,execution_id:`case${i}`,ticket:'opaque-core-execution-grant'}]]);await runner.dispose()
78 }
79 })
80 test('cancellation and disposed owner suppress late review completion',async()=>{
81 let finish;const runner=createHarnessModule({request(){return new Promise(resolve=>{finish=resolve})}},owner),controller=new AbortController()
82 const pending=runner.run(params('cancel'),controller.signal);controller.abort();await assert.rejects(pending,/cancelled/)
83 finish(snapshot('review_source_prompt',{kind:'cli_diff',diff:'late'}));await runner.dispose()
84 await assert.rejects(runner.run(params('disposed'),new AbortController().signal),/stale/)
85 })
86 test('real builtin Host process presents all four adopted review operations over existing wire',async t=>{
87 const host=await startHost({tier:'builtin'});t.after(()=>host.stop())
88 const entry=join(dirname(BUNDLE),'builtin','harness.mjs'),sha256=createHash('sha256').update(readFileSync(entry)).digest('hex')
89 assert.equal((await host.call('ext/activate',{owner,plugin_name:'Pinned review presenter',entry:{path:entry,sha256},config:{}})).status,'ok')
90 const cases=[fixture.cases[0],{operation:'review_pass_prompt',input:passInput()},fixture.cases.find(x=>x.operation==='review_interactive_pr'),fixture.cases.find(x=>x.operation==='review_report'),{operation:'review_source_prompt',input:{kind:'cli_diff',diff:'漢'.repeat(400000)+'WIRE_END'}}]
91 for(const [i,row] of cases.entries()) {
92 const redeemed=host.waitFor(message=>message.method==='exec/redeem'),result=host.call('harness/run',params(`wire${i}`)),request=await redeemed
93 assert.deepEqual(request.params,{owner,execution_id:`wire${i}`,ticket:'opaque-core-execution-grant'})
94 host.send({jsonrpc:'2.0',id:request.id,result:snapshot(row.operation,row.input)})
95 assert.deepEqual(await result,transform(row.operation,row.input))
96 }
97 assert.deepEqual(await host.call('ext/deactivate',{owner}),{disposed:true,leaked:[]})
98 })
99 test('real Host refuses encoded purpose overflow then remains usable without retrying the grant',async t=>{
100 const host=await startHost({tier:'builtin'});t.after(()=>host.stop())
101 const entry=join(dirname(BUNDLE),'builtin','harness.mjs'),sha256=createHash('sha256').update(readFileSync(entry)).digest('hex')
102 assert.equal((await host.call('ext/activate',{owner,plugin_name:'Bounded review presenter',entry:{path:entry,sha256},config:{}})).status,'ok')
103 for(const [i,diff] of ['\0'.repeat(3*1024*1024),'accepted_after_refusal'].entries()) {
104 const redeemed=host.waitFor(message=>message.method==='exec/redeem'),result=host.call('harness/run',params(`bounded${i}`)),request=await redeemed
105 host.send({jsonrpc:'2.0',id:request.id,result:snapshot('review_source_prompt',{kind:'cli_diff',diff})})
106 if(i===0) await assert.rejects(result,/review.*16 MiB/)
107 else assert.ok((await result).result.content.includes(diff))
108 }
109 assert.equal(host.coreCalls.length,0);assert.deepEqual(await host.call('ext/deactivate',{owner}),{disposed:true,leaked:[]})
110 })
111
111 lines Plain Text