// Explicit manual runner. Creates only a private copied schema and a short-lived // operator-restricted diagnostic endpoint; drops both in finally blocks. import fs from 'node:fs'; import {randomBytes} from 'node:crypto'; import {withGateway} from '../../perf/speed-round3-2026-11-10/server-util.mjs'; import {installPayrollSourceIndexConcurrency,payrollSourceIndexConcurrentAcceptance,payrollSourceIndexWriterCosts,payrollSourceIndexMembershipAcceptance} from './payroll-source-index-concurrency.mjs'; const schema='speed3_concurrency_'+randomBytes(5).toString('hex'),company='7pIbDBJmKx'; const definitions=JSON.parse(fs.readFileSync('/Users/mac/.local/state/xiaoshu/cloud-releases/2026-11-10-speed-round3/original-definitions.json','utf8')); const functions=['xs_payroll_time','xs_payroll_sources','xs_payroll_source_version','xs_payroll_cutoff_snapshot'].map(name=>definitions.find(d=>d.proname===name).definition.replaceAll('public.',schema+'.')).join(';\n'); const tables=['CommonModel','CourseAppointment','LessonRecord','LessonRecordFact','MemoryPracticeRecord','TeachingCompensationRule','CourseTeachingProfile','CoachPayRate','ReviewRewardIncome']; const config={schema,company,tables,functions,incrementalSql:fs.readFileSync('scripts/sql/payroll-incremental-reads.sql','utf8'),indexSql:fs.readFileSync('scripts/sql/payroll-source-index.sql','utf8')}; async function run(params){ if(params.action==='setup'){ const started=Date.now(); await Psql.tx(async tx=>{ await tx.none('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ'); await tx.none('CREATE SCHEMA '+config.schema);await tx.none('REVOKE ALL ON SCHEMA '+config.schema+' FROM PUBLIC'); await tx.none('SET LOCAL search_path='+config.schema+',public');await tx.none("SET LOCAL statement_timeout='120s';SET LOCAL jit=off"); for(const table of config.tables)await tx.none('CREATE TABLE "'+table+'" AS SELECT * FROM public."'+table+'" WHERE company=$1'+(table==='CommonModel'?' AND "modelId" IN(54,59,60)':''),[config.company]); for(const table of config.tables)await tx.none('CREATE UNIQUE INDEX ON "'+table+'" ("objectId")'); for(const table of ['CourseAppointment','LessonRecord','MemoryPracticeRecord'])await tx.none('CREATE INDEX ON "'+table+'" (company,id)'); await tx.none('CREATE INDEX ON "CommonModel" (company,"modelId","itemId","objectId");CREATE INDEX ON "CommonModel" (company,"modelId",("generalId"::text));CREATE INDEX ON "CommonModel" (company,"modelId","generalId")'); await tx.none(config.functions); // An empty private digest table with its real primary key is filled by the // real incremental migration, never copied or replaced with ready stubs. await tx.none('CREATE TABLE "PayrollReadDigest"(kind text NOT NULL,company text NOT NULL,"objectId" text NOT NULL,model numeric,hash text NOT NULL,PRIMARY KEY(kind,company,"objectId"))'); for(const table of config.tables)await tx.none('ANALYZE "'+table+'"'); }); const ready=await installPayrollSourceIndexConcurrency(Psql,config); return {schema:config.schema,setupMs:Date.now()-started,...ready}; } if(params.action==='race')return payrollSourceIndexConcurrentAcceptance(Psql,config); if(params.action==='membership')return payrollSourceIndexMembershipAcceptance(Psql,config); if(params.action==='writers')return payrollSourceIndexWriterCosts(Psql,config); if(params.action==='drop'){await Psql.none('DROP SCHEMA IF EXISTS '+config.schema+' CASCADE');return{dropped:true};} throw Error('Unknown fixed private-clone diagnostic'); } const source='const config='+JSON.stringify(config)+';\n'+installPayrollSourceIndexConcurrency.toString()+'\n'+payrollSourceIndexConcurrentAcceptance.toString()+'\n'+payrollSourceIndexWriterCosts.toString()+'\n'+payrollSourceIndexMembershipAcceptance.toString()+'\n'+run.toString(); const result={schema}; await withGateway(source,async call=>{ try{ for(const action of ['setup','race','membership','writers']){ result[action]=await call({action}); fs.writeFileSync('tmp/speed-round3-2026-11-10/source-index-concurrency.json',JSON.stringify(result,null,2),{mode:0o600}); console.log(JSON.stringify({action,result:result[action]})); } }finally{result.cleanup=await call({action:'drop'});fs.writeFileSync('tmp/speed-round3-2026-11-10/source-index-concurrency.json',JSON.stringify(result,null,2),{mode:0o600});console.log(JSON.stringify({action:'drop',...result.cleanup}));} });