@@ -17,7 +17,7 @@ const database = await vi.hoisted(async () => {
1717 process . env . MOTHERSHIP_BENCHMARK_ENABLED = 'true'
1818 const { createServer } = await import ( 'node:http' )
1919 const requests : Record < string , unknown > [ ] = [ ]
20- const responses : { text : string ; onRequest ?: ( ) => void } [ ] = [ ]
20+ const responses : { text : string ; onRequest ?: ( ) => void | Promise < void > } [ ] = [ ]
2121 const worker = createServer ( async ( request , response ) => {
2222 let body = ''
2323 for await ( const chunk of request ) body += chunk
@@ -28,7 +28,7 @@ const database = await vi.hoisted(async () => {
2828 const payload = JSON . parse ( body ) as Record < string , unknown >
2929 requests . push ( payload )
3030 const scripted = responses . shift ( )
31- scripted ?. onRequest ?.( )
31+ await scripted ?. onRequest ?.( )
3232 const frames = [
3333 { type : 'text' , payload : { channel : 'assistant' , text : scripted ?. text ?? '{"ok":true}' } } ,
3434 { type : 'complete' , payload : { status : 'complete' } } ,
@@ -87,6 +87,7 @@ import {
8787import { prepareBenchmarkExecution } from '@/lib/benchmarks/application/prepare-execution'
8888import { prepareBenchmarkPlan } from '@/lib/benchmarks/application/prepare-plan'
8989import { runBenchmarkComparison } from '@/lib/benchmarks/application/run-comparison'
90+ import { runBenchmarkStage } from '@/lib/benchmarks/application/run-stage'
9091import {
9192 getBenchmarkRun ,
9293 listBenchmarkRuns ,
@@ -113,6 +114,11 @@ import {
113114} from '@/lib/benchmarks/repository'
114115import { type BenchmarkArtifacts , emptyBenchmarkArtifacts } from '@/lib/benchmarks/types'
115116import { executeBenchmarkJson } from '@/lib/benchmarks/worker'
117+ import {
118+ getAccessibleCopilotChatAuth ,
119+ getAccessibleCopilotChatForCancellation ,
120+ getAccessibleCopilotChatWithMessages ,
121+ } from '@/lib/mothership/chat/lifecycle'
116122import { listOrganizationChats } from '@/lib/mothership/chat/organization-chats'
117123
118124describe ( 'private benchmark persistence and attempt fencing' , ( ) => {
@@ -180,6 +186,7 @@ describe('private benchmark persistence and attempt fencing', () => {
180186 'user' ,
181187 'settings' ,
182188 'copilot_chats' ,
189+ 'copilot_messages' ,
183190 'copilot_runs' ,
184191 'copilot_request_stops' ,
185192 'copilot_organization_request_stops' ,
@@ -330,6 +337,68 @@ describe('private benchmark persistence and attempt fencing', () => {
330337 }
331338 }
332339
340+ it . each ( [ 'organization' , 'workspace' ] as const ) (
341+ 'hides persisted %s Plan chats after eligibility revocation while preserving Stop' ,
342+ async ( surface ) => {
343+ await connection `UPDATE member SET role = 'owner' WHERE id = 'owner-member'`
344+ const chat = await prepareBenchmarkPlan . execute ( {
345+ principal,
346+ input : { organizationId : 'org' , benchmarkId : 'benchmark' } ,
347+ } )
348+ if ( surface === 'workspace' )
349+ await connection `UPDATE copilot_chats SET organization_id = NULL, workspace_id = 'workspace' WHERE id = ${ chat . chatId } `
350+ const options = { principal }
351+ expect ( await getAccessibleCopilotChatAuth ( chat . chatId , 'owner' , options ) ) . toMatchObject ( {
352+ mode : 'plan' ,
353+ } )
354+ expect (
355+ await getAccessibleCopilotChatWithMessages ( chat . chatId , 'owner' , options )
356+ ) . toMatchObject ( { mode : 'plan' , messages : [ ] } )
357+ await connection `UPDATE settings SET super_user_mode_enabled = false WHERE user_id = 'owner'`
358+ expect ( await getAccessibleCopilotChatAuth ( chat . chatId , 'owner' , options ) ) . toBeNull ( )
359+ expect ( await getAccessibleCopilotChatWithMessages ( chat . chatId , 'owner' , options ) ) . toBeNull ( )
360+ expect (
361+ await getAccessibleCopilotChatForCancellation ( chat . chatId , 'owner' , options )
362+ ) . toMatchObject ( { mode : 'plan' } )
363+ }
364+ )
365+
366+ it ( 'refuses to publish a completed Plan after its selected target loses eligibility' , async ( ) => {
367+ await connection `UPDATE member SET role = 'owner' WHERE id = 'owner-member'`
368+ await connection `UPDATE "user" SET role = 'admin' WHERE id = 'target'`
369+ await connection `INSERT INTO settings (id, user_id, super_user_mode_enabled) VALUES ('target', 'target', true) ON CONFLICT (user_id) DO UPDATE SET super_user_mode_enabled = true`
370+ const current = await updateBenchmarkRecord ( {
371+ ...scope ,
372+ version : 1 ,
373+ name : 'Target revocation' ,
374+ artifacts : { ...graded , generatedSpec : null , reconstruction : null , grade : null } ,
375+ } )
376+ await connection `UPDATE mothership_benchmarks SET run_as_user_id = 'target' WHERE id = 'benchmark'`
377+ database . responses . push ( {
378+ text : 'Unpublished plan' ,
379+ onRequest : async ( ) => {
380+ await connection `UPDATE settings SET super_user_mode_enabled = false WHERE user_id = 'target'`
381+ } ,
382+ } )
383+ await expect (
384+ runBenchmarkStage . execute ( {
385+ principal,
386+ input : {
387+ organizationId : 'org' ,
388+ benchmarkId : 'benchmark' ,
389+ version : current . version ,
390+ stage : 'plan' ,
391+ } ,
392+ } )
393+ ) . rejects . toMatchObject ( { code : 'not_found' , message : 'Plan mode is unavailable' } )
394+ expect ( database . requests ) . toHaveLength ( 1 )
395+ expect ( await getBenchmarkRecord ( scope ) ) . toMatchObject ( {
396+ runningStage : null ,
397+ artifacts : { generatedSpec : null } ,
398+ } )
399+ expect ( await connection `SELECT id FROM copilot_chats` ) . toHaveLength ( 1 )
400+ } )
401+
333402 it ( 'executes selected planners in fresh conversations with a fixed evaluator and saves comparable model snapshots' , async ( ) => {
334403 const input = await prepareComparison ( )
335404 await runBenchmarkComparison . execute ( { principal, input } )
0 commit comments