From 4712433903c195c9bad7b73abcfe4250b50aaa50 Mon Sep 17 00:00:00 2001 From: devops Date: Sat, 24 Oct 2026 10:57:52 +0000 Subject: [PATCH] feat(admin): add fenced expired editorial run recovery control --- .../editorial-recovery-hint.spec.ts | 21 +++++++ .../orchestration/orchestration.controller.ts | 5 +- .../prepare-workflow.http.spec.ts | 10 +++ .../orchestration/prepare-workflow.service.ts | 14 +++++ .../components/EditorialRecoveryDialog.tsx | 26 ++++++++ .../src/pages/OrchestrationCommandCenter.tsx | 18 +++++- .../src/pages/editorialRecovery.test.tsx | 63 +++++++++++++++++++ .../admin-panel/src/services/orchestration.ts | 13 +++- infra/hermes-editorial/RECOVERY_UI.md | 9 +++ 9 files changed, 174 insertions(+), 5 deletions(-) create mode 100644 backend/src/orchestration/editorial-recovery-hint.spec.ts create mode 100644 frontend/admin-panel/src/components/EditorialRecoveryDialog.tsx create mode 100644 frontend/admin-panel/src/pages/editorialRecovery.test.tsx create mode 100644 infra/hermes-editorial/RECOVERY_UI.md diff --git a/backend/src/orchestration/editorial-recovery-hint.spec.ts b/backend/src/orchestration/editorial-recovery-hint.spec.ts new file mode 100644 index 00000000..d8bc5639 --- /dev/null +++ b/backend/src/orchestration/editorial-recovery-hint.spec.ts @@ -0,0 +1,21 @@ +import {PrepareWorkflowService} from './prepare-workflow.service'; +const sha='a'.repeat(40),runId='run'; +const task=()=>({id:'task',key:'editorial-v1:'+'a'.repeat(64),title:'Editorial research: Test',department:'CONTENT',operationMode:'PREPARE',budgetLimitCents:20,state:'RUNNING',version:6,metadata:{createdBy:'pmo',workflowScope:'STAGE_PREPARE',environment:'staging',commitSha:sha,scope:'EDITORIAL_KIND',acceptanceCriteria:['Research']},runs:[{id:runId,taskId:'task',status:'RUNNING',environment:'staging',commitSha:sha,artifacts:[]}],budgetLedger:[{id:'reserve',taskId:'task',actorId:'pmo',kind:'RESERVE',amountCents:20,idempotencyKey:'prepare-reserve:run'}]}); +// Use the real canonical proposal marker, not a permissive admission mock. +import {EDITORIAL_PILOT_KIND} from '../orchestration-worker/editorial-bridge'; +describe('safe editorial recovery detail hint',()=>{ + let service:any,row:any,db:any,outbox:any; + beforeEach(()=>{row=task();row.metadata.scope=EDITORIAL_PILOT_KIND+'\nTopic: Test\nResearch and evidence-backed brief only. No publication, release, production changes or execution authorization.';outbox={taskId:'task',status:'PENDING',runId,leaseOwner:'pmo',leaseToken:'NEVER_EXPOSE',leaseUntil:new Date(Date.now()-1000),reservationId:'reserve'};db={prepareWorkflowOutbox:{findUnique:jest.fn(async()=>outbox)}};service=Object.create(PrepareWorkflowService.prototype);service.db=db;service.config={reviewerIds:['owner']};}); + it('returns only matching run id to an independent reviewer',async()=>{expect(await service.editorialRecovery(row,{id:'owner',role:'SUPER_ADMIN'})).toEqual({runId});}); + it.each(['active','mismatch','artifact','reconciled','other-task','wrong-commit','wrong-mode'])('denies %s',async mode=>{ + if(mode==='active')outbox.leaseUntil=new Date(Date.now()+100000); + if(mode==='mismatch')outbox.runId='other'; + if(mode==='artifact')row.runs[0].artifacts=[{id:'qa'}]; + if(mode==='reconciled')row.budgetLedger.push({kind:'RECONCILE',amountCents:0}); + if(mode==='other-task')row.runs[0].taskId='other'; + if(mode==='wrong-commit')row.runs[0].commitSha='b'.repeat(40); + if(mode==='wrong-mode')row.operationMode='MUTATE_PRODUCTION'; + expect(await service.editorialRecovery(row,{id:'owner',role:'SUPER_ADMIN'})).toBeUndefined(); + }); + it('denies ordinary admin and creator',async()=>{expect(await service.editorialRecovery(row,{id:'admin',role:'Admin'})).toBeUndefined();expect(await service.editorialRecovery(row,{id:'pmo',role:'SUPER_ADMIN'})).toBeUndefined();}); +}); diff --git a/backend/src/orchestration/orchestration.controller.ts b/backend/src/orchestration/orchestration.controller.ts index 73e70723..d01a7b51 100644 --- a/backend/src/orchestration/orchestration.controller.ts +++ b/backend/src/orchestration/orchestration.controller.ts @@ -130,7 +130,10 @@ export class OrchestrationController { ); } @Get('tasks/:id') async one(@Param('id') id: string, @Req() req: any) { - return this.safe(await this.service.findOne(id), req); + const row = await this.service.findOne(id); + const editorialRecovery = this.workflow + ? await this.workflow.editorialRecovery(row, this.actor(req)) : undefined; + return { ...this.safe(row, req), ...(editorialRecovery ? { editorialRecovery } : {}) }; } @Get('overview') async overview() { const tasks = await this.service.findAll(); diff --git a/backend/src/orchestration/prepare-workflow.http.spec.ts b/backend/src/orchestration/prepare-workflow.http.spec.ts index 795f6382..4b3defce 100644 --- a/backend/src/orchestration/prepare-workflow.http.spec.ts +++ b/backend/src/orchestration/prepare-workflow.http.spec.ts @@ -22,6 +22,7 @@ describe('bounded workflow HTTP contracts', () => { }, }; const workflow = { + editorialRecovery: jest.fn(async () => undefined as undefined | {runId:string}), create: jest.fn(async () => t), transition: jest.fn(async () => t), decide: jest.fn(async () => t), @@ -90,6 +91,15 @@ describe('bounded workflow HTTP contracts', () => { expect(task.availableActions).toEqual(['SUBMIT_GATE1']); } }); + it('adds only the safe recovery hint to detail, never the list', async () => { + workflow.editorialRecovery.mockResolvedValueOnce({runId:'registered-run'}); + const detail=await request(app.getHttpServer()).get('/admin/orchestration/tasks/t').set('Authorization','SUPER_ADMIN').expect(200); + expect(detail.body.editorialRecovery).toEqual({runId:'registered-run'}); + expect(workflow.editorialRecovery).toHaveBeenCalledWith(t,{id:'verified-subject',role:'SUPER_ADMIN'}); + const list=await request(app.getHttpServer()).get('/admin/orchestration/tasks').set('Authorization','SUPER_ADMIN').expect(200); + expect(list.body[0].editorialRecovery).toBeUndefined(); + expect(JSON.stringify(detail.body)).not.toContain('secret'); + }); it('denies ordinary users before workflow projection', async () => { workflow.project.mockClear(); for (const path of ['tasks', 'tasks/t', 'overview']) { diff --git a/backend/src/orchestration/prepare-workflow.service.ts b/backend/src/orchestration/prepare-workflow.service.ts index ce5e5716..a459862d 100644 --- a/backend/src/orchestration/prepare-workflow.service.ts +++ b/backend/src/orchestration/prepare-workflow.service.ts @@ -517,6 +517,20 @@ export class PrepareWorkflowService { ); }); } + /** Detail-only eligibility hint. No lease credentials leave this service. Command rechecks atomically. */ + async editorialRecovery(t: any, a: WorkflowIdentity) { + const { admittedEditorialTask } = require('../orchestration-worker/editorial-execution'); + if (!this.reviewer(a) || t.metadata?.createdBy === a.id || t.state !== 'RUNNING' || !admittedEditorialTask(t)) return undefined; + const o = await (this.db as any).prepareWorkflowOutbox.findUnique({ where: { taskId: t.id } }); + if (!o || o.status !== 'PENDING' || o.leaseOwner !== 'pmo' || !o.leaseUntil || !Number.isFinite(new Date(o.leaseUntil).getTime()) || new Date(o.leaseUntil).getTime() > Date.now()) return undefined; + const r = t.runs?.find((r: any) => r.id === o.runId); + const ledger = t.budgetLedger ?? []; + const reserve = ledger.find((b: any) => b.id === o.reservationId); + if (!r || r.taskId !== t.id || r.status !== 'RUNNING' || r.environment !== 'staging' || r.commitSha !== t.metadata.commitSha || !Array.isArray(r.artifacts) || r.artifacts.length || + !reserve || reserve.actorId !== 'pmo' || reserve.kind !== 'RESERVE' || reserve.amountCents !== 20 || reserve.idempotencyKey !== `prepare-reserve:${r.id}` || + ledger.some((b: any) => b.kind === 'RECONCILE') || ledger.reduce((n: number, b: any) => n + b.amountCents, 0) !== 20) return undefined; + return { runId: r.id }; + } /** Stop only; never turns unknown spend into free retry capacity. */ async editorialFailure(id: string, dto: any, a: WorkflowIdentity) { const held = dto.leaseToken !== undefined; diff --git a/frontend/admin-panel/src/components/EditorialRecoveryDialog.tsx b/frontend/admin-panel/src/components/EditorialRecoveryDialog.tsx new file mode 100644 index 00000000..719b9e8b --- /dev/null +++ b/frontend/admin-panel/src/components/EditorialRecoveryDialog.tsx @@ -0,0 +1,26 @@ +import {useRef,useState} from 'react'; +import Button from './ui/Button'; +import {commandFailure,orchestration} from '../services/orchestration'; +import type {Task,EditorialFailureInput} from '../services/orchestration'; +export default function EditorialRecoveryDialog({task,onClose,onSuccess}:{task:Task;onClose:()=>void;onSuccess:()=>Promise}) { + const [reason,setReason]=useState('');const [confirmed,setConfirmed]=useState(false); + const [busy,setBusy]=useState(false);const [attempted,setAttempted]=useState(false);const [blocked,setBlocked]=useState(false);const [error,setError]=useState(''); + const payload=useRef(null);const inFlight=useRef(false); + const execute=async()=>{ + if(inFlight.current||blocked||!confirmed||!reason.trim()||reason.length>500)return; + if(!payload.current)payload.current={runId:task.editorialRecovery!.runId,expectedVersion:task.version,idempotencyKey:crypto.randomUUID(),reason}; + inFlight.current=true;setBusy(true);setAttempted(true);setError(''); + try {await orchestration.editorialFailure(task,payload.current);await onSuccess();} + catch(e){const failure=commandFailure(e);setBlocked(!failure.retryable);setError(failure.message)} + finally{inFlight.current=false;setBusy(false)} + }; + return
+

ثبت شکست اجرای متوقف‌شده

وظیفه: {task.title} · نسخه: {task.version}

شناسه اجرا: {task.editorialRecovery!.runId}

+

فقط اجرای منقضی‌شده به شکست ثبت می‌شود. رزرو ۲۰ سنت حفظ می‌شود؛ هزینه واقعی نامشخص است، نه هزینه صفر.

+

این اقدام QA یا تأیید خودکار ایجاد نمی‌کند؛ انتشار، اجرای دوباره، افزایش بودجه یا آزادسازی رزرو انجام نمی‌شود. مجوز بازبین مستقل و اتصال اجرای منقضی را سرور دوباره بررسی می‌کند.

+