返回 CodeWhale
composition-host.test.mjs
根目录 / crates / engine / extension-host / test / composition-host.test.mjs
1 // Real Node/Bun host and reviewed source closure; fake core transport.
2 // Rust receipt/admission/membership/install qualification is a separate gate.
3 import { test } from 'node:test'
4 import assert from 'node:assert/strict'
5 import { spawnSync } from 'node:child_process'
6 import { createHash } from 'node:crypto'
7 import { existsSync, mkdtempSync, rmSync, writeFileSync, mkdirSync } from 'node:fs'
8 import { tmpdir } from 'node:os'
9 import { dirname, join } from 'node:path'
10 import { fileURLToPath } from 'node:url'
11 import { activate, startHost, FIXTURES, IS_BUN } from './harness.mjs'
12
13 const here = dirname(fileURLToPath(import.meta.url))
14 const hash = (value) => createHash('sha256').update(value).digest('hex')
15
16 function temporary(t) {
17 const root = mkdtempSync(join(tmpdir(), 'cw-composition-'))
18 t.after(() => rmSync(root, { recursive: true, force: true }))
19 return root
20 }
21
22 test('canonical Native composition adopts existing contributions and withdraws their exact handles', async (t) => {
23 const host = await startHost()
24 t.after(() => host.stop())
25 const data_dir = temporary(t)
26 const entry = join(FIXTURES, 'native-dsh-composition', 'native', 'index.mjs')
27 const { ref, result } = await activate(host, 'native-dsh-composition', entry, { data_dir })
28 assert.equal(result.status, 'ok', result.diagnostic)
29 const admitted = host.registry.filter(item => item.op === 'register')
30 assert.deepEqual(new Set(admitted.map(item => item.kind)), new Set(['tool', 'command', 'prompt_section', 'skill_root']))
31 assert.equal(admitted.find(item => item.kind === 'prompt_section').spec.description, 'hello from upstream')
32 assert.equal(admitted.find(item => item.kind === 'skill_root').spec.name, 'source/skills')
33 const tool = admitted.find(item => item.kind === 'tool')
34 const answer = await host.call('tool/call', { handle: tool.handle, input: {}, call_id: 'composition-call', deadline_ms: 5000, workspace: data_dir })
35 assert.deepEqual(answer.structured, { text: 'hello from upstream', count: null })
36 assert.deepEqual(await host.call('ext/deactivate', { owner: ref }), { disposed: true, leaked: [] })
37 for (const item of admitted) assert.ok(host.registry.some(row => row.op === 'unregister' && row.handle === item.handle))
38 })
39
40 function counterComposition(t, id) {
41 const root = temporary(t)
42 mkdirSync(join(root, 'source'))
43 const source = "export const inject = ['prompt']; export function apply(ctx, config) { ctx.prompt.registerSection({id:String(config.id),text:String(config.count)}) }"
44 writeFileSync(join(root, 'source', 'row.mjs'), source)
45 const layer = `- insert:\n - id: count\n name: ./row.mjs\n config:\n id: ${JSON.stringify(id)}\n count: !!js '(globalThis.__CW_COMPOSITION_COUNT = (globalThis.__CW_COMPOSITION_COUNT ?? 0) + 1)'\n`
46 const spec = { version: 1, layers: [{ path: 'base.yml', sha256: hash(layer), source: layer }], modules: [{ name: './row.mjs', path: 'row.mjs', sha256: hash(source) }], files: { 'row.mjs': hash(source) } }
47 writeFileSync(join(root, 'composition.json'), JSON.stringify(spec))
48 const entry = join(root, 'index.mjs')
49 writeFileSync(entry, `import {mountReviewedComposition} from '@codewhale/dsh-composition'; import spec from './composition.json' with {type:'json'}; export async function apply(ctx) {await mountReviewedComposition(ctx,new URL('./source/',import.meta.url).href,spec)}`)
50 return entry
51 }
52
53 test('two compositions share exactly one Loader and one teardown keeps the other owner live', async (t) => {
54 const host = await startHost()
55 t.after(() => host.stop())
56 const a = await activate(host, 'composition-a', counterComposition(t, 'note-a'))
57 const b = await activate(host, 'composition-b', counterComposition(t, 'note-b'))
58 assert.equal(a.result.status, 'ok', a.result.diagnostic)
59 assert.equal(b.result.status, 'ok', b.result.diagnostic)
60 const notes = host.registry.filter(item => item.op === 'register' && item.kind === 'prompt_section')
61 assert.deepEqual(notes.map(item => item.spec.description), ['1', '2'], 'duplicate global Loader observers evaluated a configuration twice')
62 assert.deepEqual(await host.call('ext/deactivate', { owner: a.ref }), { disposed: true, leaked: [] })
63 assert.ok(!host.registry.some(item => item.op === 'unregister' && item.handle === notes[1].handle))
64 const c = await activate(host, 'composition-c', counterComposition(t, 'note-c'))
65 assert.equal(c.result.status, 'ok', c.result.diagnostic)
66 assert.equal(host.registry.filter(item => item.op === 'register' && item.kind === 'prompt_section').at(-1).spec.description, '3')
67 })
68
69 test('trusted offline reviewer preserves expressions and never imports row modules', (t) => {
70 const root = temporary(t)
71 const sentinel = join(root, 'expression-executed')
72 const layer = JSON.stringify([{ insert: [{ id: 'unexecuted', name: './row.mjs', config: { __jsExpr: `process.getBuiltinModule('fs').writeFileSync(${JSON.stringify(sentinel)},'unsafe')` } }] }])
73 const spec = { version: 1, layers: [{ path: 'base.json', sha256: hash(layer), source: layer }], modules: [{ name: './row.mjs', path: 'row.mjs', sha256: '0'.repeat(64) }], files: { 'row.mjs': '0'.repeat(64) } }
74 const result = spawnSync(process.execPath, [join(here, '..', 'dist', 'dsh-composition-review.mjs')], { input: JSON.stringify(spec), encoding: 'utf8', timeout: 5000, maxBuffer: 8 * 1024 * 1024 })
75 assert.equal(result.status, 0, result.stderr)
76 const review = JSON.parse(result.stdout)
77 assert.deepEqual(review.entries[0].config, JSON.parse(layer)[0].insert[0].config)
78 assert.equal(existsSync(sentinel), false)
79 assert.equal(existsSync(join(root, 'row.mjs')), false, 'review did not need any row module to exist')
80 })
81
82 function moduleClosureComposition(t, rowSource, files, extraRows = {}, rowConfigs = {}) {
83 const root=temporary(t)
84 mkdirSync(join(root,'source'))
85 const rows={'row.mjs':rowSource,...extraRows}
86 for (const [name,source] of Object.entries({...rows,...files})) writeFileSync(join(root,'source',name),source)
87 const layer=JSON.stringify([{insert:Object.keys(rows).map((name,index)=>({id:`row-${index}`,name:`./${name}`,config:rowConfigs[name]??{}}))}])
88 const closure=Object.fromEntries(Object.entries({...rows,...files}).map(([name,source])=>[name,hash(source)]))
89 const spec={version:1,layers:[{path:'base.json',sha256:hash(layer),source:layer}],modules:Object.entries(rows).map(([name,source])=>({name:`./${name}`,path:name,sha256:hash(source)})),files:closure}
90 writeFileSync(join(root,'composition.json'),JSON.stringify(spec))
91 const entry=join(root,'index.mjs')
92 writeFileSync(entry,`import {mountReviewedComposition} from '@codewhale/dsh-composition';import spec from './composition.json' with {type:'json'};export async function apply(ctx){await mountReviewedComposition(ctx,new URL('./source/',import.meta.url).href,spec)}`)
93 return {root,entry}
94 }
95
96 test('a row that reads settings.describe() in a synchronous try/catch during apply activates and stays live',async t=>{
97 const host=await startHost();t.after(()=>host.stop())
98 const probe=moduleClosureComposition(t,"export const inject=['commands'];export function apply(ctx){let l='zh';try{const f=ctx.get('settings').describe();if(Array.isArray(f) && f.some(item=>item.ns==='locale'))l='host'}catch{}ctx.commands.register({name:'probe-'+l,description:'locale probe',handler(){return {kind:'success',text:l}}})}",{})
99 const {ref,result}=await activate(host,'settings-probe',probe.entry)
100 assert.equal(result.status,'ok',result.diagnostic)
101 assert.deepEqual(result.commands,['probe-host'])
102 await new Promise(resolve=>setTimeout(resolve,100))
103 assert.deepEqual(host.faulted,[])
104 assert.deepEqual(host.brokerCalls,[])
105 assert.deepEqual(await host.call('ext/deactivate',{owner:ref}),{disposed:true,leaked:[]})
106 })
107
108 test('a failed composition row names its import error, and a waiting row names only the services it lacks',async t=>{
109 const host=await startHost();t.after(()=>host.stop())
110 const peer=moduleClosureComposition(t,"import {setApprovalPolicy} from '@deepseek-ai/dsh-user-approval';export function apply(){return setApprovalPolicy}",{})
111 const imported=(await activate(host,'row-peer',peer.entry)).result
112 assert.equal(imported.status,'failed')
113 assert.match(imported.diagnostic,/composition row "row-0" failed activation: UnsupportedPeerError: requires `@deepseek-ai\/dsh-user-approval`/)
114 const web=moduleClosureComposition(t,"export const inject=['tools','@deepseek-ai/dsh-client-panel'];export function apply(){}",{})
115 const waiting=(await activate(host,'row-web',web.entry)).result
116 assert.equal(waiting.status,'failed')
117 assert.match(waiting.diagnostic,/composition row "row-0" requires `@deepseek-ai\/dsh-client-panel`, not provided by the Codewhale plugin host in this phase \(DSH client UI services\)/)
118 assert.doesNotMatch(waiting.diagnostic,/`tools`/)
119 const webOk=moduleClosureComposition(t,"export const inject=['tools','webServer','connection'];export function apply(){}",{})
120 assert.equal((await activate(host,'row-web-ok',webOk.entry)).result.status,'ok')
121 const spawn=moduleClosureComposition(t,"export const inject=['subprocess'];export function apply(){}",{})
122 const owned=(await activate(host,'row-subprocess',spawn.entry)).result
123 assert.equal(owned.status,'failed')
124 assert.match(owned.diagnostic,/composition row "row-0" requires `subprocess`, not provided .*; `subprocess` belongs to the Codewhale core and no reviewed bridge exposes it yet/)
125 })
126
127 test('a transitive reviewed relative helper executes but an undeclared helper never does',async t=>{
128 const host=await startHost();t.after(()=>host.stop())
129 const good=moduleClosureComposition(t,"import {text} from './helper.mjs';export const inject=['prompt'];export function apply(ctx){ctx.prompt.registerSection({id:'nested',text})}",{'helper.mjs':"export const text='reviewed helper'"})
130 const mounted=await activate(host,'closure-good',good.entry)
131 assert.equal(mounted.result.status,'ok',mounted.result.diagnostic)
132 assert.equal(host.registry.find(row=>row.op==='register' && row.kind==='prompt_section').spec.description,'reviewed helper')
133 const bad=moduleClosureComposition(t,"import './rogue.mjs';export function apply(){}",{})
134 const sentinel=join(bad.root,'rogue-executed')
135 writeFileSync(join(bad.root,'source','rogue.mjs'),`import {writeFileSync} from 'node:fs';writeFileSync(${JSON.stringify(sentinel)},'bad')`)
136 const refused=await activate(host,'closure-undeclared',bad.entry)
137 assert.equal(refused.result.status,'failed')
138 assert.equal(existsSync(sentinel),false)
139 assert.deepEqual(await host.call('ext/deactivate',{owner:mounted.ref}),{disposed:true,leaked:[]})
140 })
141
142 test('changed transitive module bytes and ambient bare dependencies refuse before execution',async t=>{
143 const host=await startHost();t.after(()=>host.stop())
144 const changed=moduleClosureComposition(t,"import './helper.mjs';export function apply(){}",{'helper.mjs':"export const original=true"})
145 const sentinel=join(changed.root,'changed-executed')
146 writeFileSync(join(changed.root,'source','helper.mjs'),`import {writeFileSync} from 'node:fs';writeFileSync(${JSON.stringify(sentinel)},'bad')`)
147 const refused=await activate(host,'closure-changed',changed.entry)
148 assert.equal(refused.result.status,'failed')
149 assert.equal(existsSync(sentinel),false)
150 const ambient=moduleClosureComposition(t,"import 'js-yaml';export function apply(){}",{})
151 const unavailable=await activate(host,'closure-ambient',ambient.entry)
152 assert.equal(unavailable.result.status,'failed')
153 })
154
155
156 test('reviewed composition refuses a row creation failure instead of acknowledging an empty tree', async t => {
157 const host=await startHost();t.after(()=>host.stop())
158 const entry=counterComposition(t,'creation-probe')
159 const source=String.raw`export const inject=['prompt'];export function apply(ctx){throw new Error('intentional row startup refusal')}`
160 // Full rows stay reviewed; the failure occurs inside activation, not the offline reviewer.
161 const root=dirname(entry)
162 writeFileSync(join(root,'source','row.mjs'),source)
163 const layer=JSON.stringify([{insert:[{id:'must-start',name:'./row.mjs',config:{}}]}])
164 writeFileSync(join(root,'composition.json'),JSON.stringify({version:1,layers:[{path:'base.json',sha256:hash(layer),source:layer}],modules:[{name:'./row.mjs',path:'row.mjs',sha256:hash(source)}],files:{'row.mjs':hash(source)}}))
165 const result=await activate(host,'startup-refusal',entry)
166 assert.equal(result.result.status,'failed')
167 assert.equal(host.registry.filter(row=>row.op==='register').length,0)
168 })
169
170
171 test('reviewed computed and URL imports retain query identity and exact JSON data', async t => {
172 const host=await startHost();t.after(()=>host.stop())
173 const row=`export const inject=['prompt'];export async function apply(ctx){
174 const name='./helper.mjs';const first=await import(name);
175 const url=new URL('./helper.mjs?variant=two',import.meta.url);
176 const queried=await import(url);const again=await import(String(url));
177 const data=await import('./data.json',{with:{type:'json'}});
178 ctx.prompt.registerSection({id:'dynamic',text:JSON.stringify({text:first.text,queryDistinct:first!==queried,queryCached:queried===again,protoData:Object.hasOwn(data.default,'__proto__'),value:data.default.__proto__.answer})});
179 }`
180 const composition=moduleClosureComposition(t,row,{'helper.mjs':"export const text='computed reviewed helper'",'data.json':'{"__proto__":{"answer":42}}'})
181 const mounted=await activate(host,'closure-computed',composition.entry)
182 if(IS_BUN){
183 assert.equal(mounted.result.status,'failed')
184 assert.match(mounted.result.diagnostic,/Bun does not preserve reviewed module query or fragment identity/)
185 assert.match(mounted.result.diagnostic,/runtime = "node"/)
186 assert.equal(host.registry.filter(row=>row.op==='register').length,0)
187 return
188 }
189 assert.equal(mounted.result.status,'ok',mounted.result.diagnostic)
190 assert.deepEqual(JSON.parse(host.registry.find(row=>row.op==='register' && row.kind==='prompt_section').spec.description),{text:'computed reviewed helper',queryDistinct:true,queryCached:true,protoData:true,value:42})
191 assert.deepEqual(await host.call('ext/deactivate',{owner:mounted.ref}),{disposed:true,leaked:[]})
192 })
193
194 test('unreviewed computed imports refuse before module side effects', async t => {
195 const host=await startHost();t.after(()=>host.stop())
196 const composition=moduleClosureComposition(t,"export async function apply(){const name='./rogue.mjs';await import(name)}",{})
197 const sentinel=join(composition.root,'computed-rogue-executed')
198 writeFileSync(join(composition.root,'source','rogue.mjs'),`import {writeFileSync} from 'node:fs';writeFileSync(${JSON.stringify(sentinel)},'bad')`)
199 const mounted=await activate(host,'closure-computed-rogue',composition.entry)
200 assert.equal(mounted.result.status,'failed')
201 assert.equal(existsSync(sentinel),false)
202 assert.equal(host.registry.filter(row=>row.op==='register').length,0)
203 })
204
205 test('separate reviewed rows retain the same transitive module instance', async t => {
206 const host=await startHost();t.after(()=>host.stop())
207 const sharedRow="import {next} from './helper.mjs';export const inject=['prompt'];export function apply(ctx,config){ctx.prompt.registerSection({id:String(config.id),text:String(next())})}"
208 const composition=moduleClosureComposition(t,sharedRow,{'helper.mjs':'let count=0;export function next(){return ++count}'},{'second.mjs':sharedRow},{'row.mjs':{id:'first'},'second.mjs':{id:'second'}})
209 const mounted=await activate(host,'closure-shared-instance',composition.entry)
210 assert.equal(mounted.result.status,'ok',mounted.result.diagnostic)
211 const registrations=host.registry.filter(row=>row.op==='register' && row.kind==='prompt_section')
212 assert.deepEqual(registrations.map(row=>row.spec.description),['1','2'])
213 assert.deepEqual(await host.call('ext/deactivate',{owner:mounted.ref}),{disposed:true,leaked:[]})
214 for(const row of registrations)assert.ok(host.registry.some(item=>item.op==='unregister' && item.handle===row.handle))
215 })
216
217
218 test('reviewed computed URL imports and JSON preserve data on both runtimes', async t => {
219 const host=await startHost();t.after(()=>host.stop())
220 const row=`export const inject=['prompt'];export async function apply(ctx){
221 const target=new URL('./helper.mjs',import.meta.url);const helper=await import(target);
222 const again=await import(String(target));const data=await import('./data.json',{with:{type:'json'}});
223 ctx.prompt.registerSection({id:'computed-url',text:JSON.stringify({text:helper.text,cached:helper===again,protoData:Object.hasOwn(data.default,'__proto__'),value:data.default.__proto__.answer})});
224 }`
225 const composition=moduleClosureComposition(t,row,{'helper.mjs':"export const text='reviewed URL helper'",'data.json':'{"__proto__":{"answer":42}}'})
226 const mounted=await activate(host,'closure-computed-url',composition.entry)
227 assert.equal(mounted.result.status,'ok',mounted.result.diagnostic)
228 assert.deepEqual(JSON.parse(host.registry.find(row=>row.op==='register' && row.kind==='prompt_section').spec.description),{text:'reviewed URL helper',cached:true,protoData:true,value:42})
229 assert.deepEqual(await host.call('ext/deactivate',{owner:mounted.ref}),{disposed:true,leaked:[]})
230 })
231
231 lines Plain Text