Compare commits

...
Author SHA1 Message Date
devops d052a9a962 feat(admin): add fenced expired editorial run recovery control 2026-10-24 10:57:52 +00:00
9 changed files with 174 additions and 5 deletions

No files matched your search

@@ -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();});
});
@@ -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();
@@ -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']) {
@@ -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;
@@ -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<void>}) {
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<EditorialFailureInput|null>(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 <div dir="rtl" lang="fa" role="dialog" aria-modal="true" aria-label="ثبت شکست اجرای متوقف‌شده" className="fixed inset-0 z-50 bg-black/50 overflow-y-auto p-8"><div className="bg-white rounded-2xl p-6 max-w-2xl mx-auto space-y-4">
<h2>ثبت شکست اجرای متوقف‌شده</h2><p>وظیفه: {task.title} · نسخه: {task.version}</p><p>شناسه اجرا: {task.editorialRecovery!.runId}</p>
<p>فقط اجرای منقضی‌شده به شکست ثبت می‌شود. رزرو ۲۰ سنت حفظ می‌شود؛ هزینه واقعی نامشخص است، نه هزینه صفر.</p>
<p>این اقدام QA یا تأیید خودکار ایجاد نمی‌کند؛ انتشار، اجرای دوباره، افزایش بودجه یا آزادسازی رزرو انجام نمی‌شود. مجوز بازبین مستقل و اتصال اجرای منقضی را سرور دوباره بررسی می‌کند.</p>
<label className="block">دلیل شکست<textarea aria-label="دلیل شکست" maxLength={500} value={reason} disabled={busy||attempted} onChange={e=>{setReason(e.target.value);setConfirmed(false)}} className="border p-2 w-full"/></label><p>{reason.length}/500</p>
<label className="block"><input type="checkbox" checked={confirmed} disabled={busy||attempted} onChange={e=>setConfirmed(e.target.checked)}/>ثبت شکست بدون تأیید کیفیت یا آزادسازی بودجه را تأیید می‌کنم</label>
{error&&<p role="alert">{error}</p>}
<div className="flex gap-3"><Button disabled={busy||blocked||!confirmed||!reason.trim()||reason.length>500} onClick={()=>void execute()}>ثبت شکست</Button><Button disabled={busy} variant="outline" onClick={onClose}>بازخوانی و بستن</Button></div>
</div></div>;
}
@@ -1,5 +1,7 @@
import { useEffect, useState, useRef } from 'react';
import Button from '../components/ui/Button';
import EditorialRecoveryDialog from '../components/EditorialRecoveryDialog';
import {editorialRecoveryRun} from '../services/orchestration';
import RouterUsagePanel from '../components/RouterUsagePanel';
import RuntimeReadPanel from '../components/RuntimeReadPanel';
import CreateOrchestrationTask from '../components/CreateOrchestrationTask';
@@ -14,6 +16,7 @@ const errorText=(e:unknown)=> {const status=(e as {response?:{status?:number}})?
function Json({value}:{value:unknown}) {return <Records value={value}/>;}
export default function OrchestrationCommandCenter() {
const detailEpoch=useRef(0);
const [recovery,setRecovery]=useState<Task|null>(null);
const [section,setSection]=useState<Section>('نمای کلی');
const [capabilities,setCapabilities]=useState({mutations:false,chat:false,runtime:false,killSwitch:false});
const [tasks,setTasks]=useState<Task[]>([]);
@@ -70,9 +73,17 @@ export default function OrchestrationCommandCenter() {
setPending(null);setSelected(null);setSuccess('فرمان توسط سرور پذیرفته شد؛ وضعیت مرجع بازخوانی شد.');await loadTasks();
} catch(e) {const failure=commandFailure(e);setBlocked(!failure.retryable);setCommandError(failure.message)} finally {setBusy(false)}
};
const refresh=()=>{detailEpoch.current++;setDecisionLoading(false);setSelected(null);setPending(null);void loadTasks()};
const openRecovery=async(task:Task)=>{
if(!capabilities.mutations||pending||recovery)return;
const epoch=++detailEpoch.current;setDecisionLoading(true);setSelected(null);setError('');
try{const detail=await orchestration.task(task.id);if(epoch!==detailEpoch.current)return;
if(detail.id!==task.id||!editorialRecoveryRun(detail))throw new Error('بازیابی دیگر مجاز نیست؛ وضعیت را بازخوانی کنید');
setRecovery(detail);
}catch(e){if(epoch===detailEpoch.current)setError(errorText(e))}finally{if(epoch===detailEpoch.current)setDecisionLoading(false)}
};
const refresh=()=>{detailEpoch.current++;setDecisionLoading(false);setSelected(null);setPending(null);setRecovery(null);void loadTasks()};
const filtered=tasks.filter(t=>(t.title+' '+t.department+' '+t.state).toLowerCase().includes(search.toLowerCase()));
return <div dir="rtl" lang="fa" className="space-y-5 font-vazir pb-12">
return <><div inert={!!recovery} dir="rtl" lang="fa" className="space-y-5 font-vazir pb-12">
<header className="bg-white border rounded-2xl p-5 flex justify-between gap-4"><div><h1 className="text-xl font-black">مرکز فرماندهی ارکستراسیون</h1><p className="text-sm text-gray-600">مرجع عملیات سرور است · Gate1 برای کار محدود · Gate2 و انتشار غیرفعال هستند</p></div><Button onClick={refresh} disabled={busy||loading}>بازخوانی وضعیت از سرور</Button></header>
<nav aria-label="بخش‌های ارکستراسیون" className="flex flex-wrap gap-2">{sections.map(s=><Button key={s} disabled={!!pending||busy} onClick={()=>{detailEpoch.current++;setDecisionLoading(false);setSection(s);setSuccess('');setReason('');setConfirmed(false)}} variant={section===s?'primary':'outline'}>{s}</Button>)}</nav>
{unavailable[section]&&<section className="bg-amber-50 border border-amber-200 rounded-2xl p-5 space-y-3"><h2 className="font-bold">این بخش در نسخه فعلی در دسترس نیست</h2><p>{unavailable[section]}</p><p>برای این بخش درخواست ناموفق تکراری به سرور ارسال نمی‌شود.</p></section>}
@@ -93,6 +104,7 @@ export default function OrchestrationCommandCenter() {
</section>}
{selected&&<section aria-label="جزئیات وظیفه" className="bg-white border rounded-2xl p-5 space-y-4"><h2 className="font-bold">{selected.title} · v{selected.version}</h2>{typeof selected.metadata?.scope==='string'&&selected.metadata.scope.trim()&&Array.isArray(selected.metadata.acceptanceCriteria)&&selected.metadata.acceptanceCriteria.length>0&&selected.metadata.acceptanceCriteria.every(value=>typeof value==='string'&&value.trim())?<h3 className="font-bold">محدوده و معیارهای پذیرش گزارش‌شده از سرور</h3>:<p>محدوده و معیارهای پذیرش در دسترس نیست؛ سرور این اطلاعات را در نمای امن ارسال نکرده است.</p>}<p>محدوده گردش کار سرور: در نمای امن در دسترس نیست.</p><Json value={{operationMode:selected.operationMode,budgetLimitCents:selected.budgetLimitCents,metadata:selected.metadata}}/>
{selected.displayState==='GATE1_RENEWAL_REQUIRED'&&<p role="status">مسدود — تأیید Gate1 منقضی شده؛ برای همان وظیفه و محدوده، تأیید مجدد مستقل لازم است. برنامه زمانی و سقف بودجه تغییر نمی‌کند.</p>}<h3 className="font-bold">اقدام‌های مجاز اعلام‌شده توسط سرور</h3>{!selected.availableActions.length&&<p>اقدامی اعلام نشده است. این نما فقط خواندنی است؛ مرورگر مجوز یا تغییر وضعیت ایجاد نمی‌کند.</p>}<div className="flex flex-wrap gap-2">{selected.availableActions.map(a=><Button key={a.id} disabled={!capabilities.mutations||!!actionBlockReason(selected,a)} title={!capabilities.mutations?'تغییرات از طرف سرور مجاز نیست':actionBlockReason(selected,a)} onClick={()=>open(selected,a)}>{actionLabel(a)}</Button>)}</div>
{editorialRecoveryRun(selected)&&<Button disabled={!capabilities.mutations||!!pending||busy||decisionLoading} onClick={()=>void openRecovery(selected)}>ثبت شکست اجرای متوقف‌شده</Button>}
<h3 className="font-bold">تصمیم‌های تأیید تغییرناپذیر</h3><Json value={selected.decisions??[]}/>
<h3 className="font-bold">اجراها و خروجی‌های ثبت‌شده</h3>{!selected.runs?.length&&<p>اجرای ثبت‌شده‌ای وجود ندارد.</p>}{selected.runs?.map(r=><article key={r.id} className="border rounded-xl p-3 space-y-2"><h4>{r.id} · {label(r.status)}</h4><p>کامیت: {r.commitSha} · محیط: {r.environment}</p>{r.artifacts.map(a=><Json key={a.id} value={a}/>)}</article>)}
<p>رزرو آزمایشی؛ هزینه واقعی نامشخص</p><h3 className="font-bold">دفتر بودجه (سنت؛ رزرو با هزینه واقعی متفاوت است)</h3><Json value={selected.budgetLedger??[]}/><h3 className="font-bold">رویدادهای ممیزی به‌ترتیب</h3><Json value={selected.events??[]}/>
@@ -112,5 +124,5 @@ export default function OrchestrationCommandCenter() {
<label className="block"><input type="checkbox" checked={confirmed} onChange={e=>setConfirmed(e.target.checked)} disabled={busy}/>محدوده و اتصال تغییرناپذیر را بررسی کردم</label>
{commandError&&<p role="alert">{commandError}</p>}<div className="flex gap-3"><Button disabled={busy||blocked||!capabilities.mutations||!confirmed||(pending.action.toState==='GATE1_HELD'&&!reason.trim())|| (!!expires&&!(Date.parse(expires)>now)) || (['APPROVE_GATE1','REJECT_GATE1'].includes(pending.action.id)&&Date.parse(expires)>now+30*86400000) || (pending.action.gate===2&&pending.action.decision==='APPROVED'&&!binding)} onClick={()=>void execute()}>تأیید فرمان</Button><Button variant="outline" disabled={busy} onClick={()=>setPending(null)}>انصراف</Button></div>
</div></div>}
</div>;
</div>{recovery&&<EditorialRecoveryDialog task={recovery} onClose={refresh} onSuccess={async()=>{detailEpoch.current++;setRecovery(null);setSelected(null);setSuccess('شکست ثبت شد؛ رزرو با هزینه نامشخص حفظ شد.');await loadTasks()}}/>}</>;
}
@@ -0,0 +1,63 @@
import {render,screen,fireEvent,within,waitFor} from '@testing-library/react';
import {beforeEach,it,expect,vi} from 'vitest';
import api from '../services/api';
import {orchestration} from '../services/orchestration';
import OrchestrationCommandCenter from './OrchestrationCommandCenter';
vi.mock('../services/api',()=>({default:{get:vi.fn(),post:vi.fn(),put:vi.fn()}}));
const run={id:'r-expired',status:'RUNNING',environment:'staging',commitSha:'a'.repeat(40),artifacts:[]};
const task={id:'editorial',key:'editorial-v1:'+ 'a'.repeat(64),title:'Editorial research: Test',department:'CONTENT',operationMode:'PREPARE',state:'RUNNING',version:6,budgetLimitCents:20,metadata:{commitSha:run.commitSha},availableActions:[],runs:[run],editorialRecovery:{runId:run.id}};
beforeEach(()=>{vi.resetAllMocks();vi.mocked(api.get).mockImplementation(async url=>({data:url.endsWith('/overview')?{items:[],measuredAt:'now',capabilities:{mutations:true}}:url.endsWith('/tasks')?[task]:task}));});
it('posts only the exact expired recovery payload through authenticated API',async()=>{
vi.mocked(api.post).mockResolvedValue({data:{}});
await orchestration.editorialFailure(task,{runId:run.id,expectedVersion:6,idempotencyKey:'stable',reason:'Process ended'});
expect(api.post).toHaveBeenCalledWith('/admin/orchestration/tasks/editorial/editorial-failure',{runId:run.id,expectedVersion:6,idempotencyKey:'stable',reason:'Process ended'});
});
it.each([{...task,editorialRecovery:undefined},{...task,runs:[]},{...task,runs:[{...run,status:'SUCCEEDED'}]},{...task,runs:[{...run,commitSha:'b'.repeat(40)}]}])('rejects missing or mismatched server recovery binding',async invalid=>{
await expect(orchestration.editorialFailure(invalid,{runId:run.id,expectedVersion:6,idempotencyKey:'k',reason:'Process ended'})).rejects.toThrow();expect(api.post).not.toHaveBeenCalled();
});
it.each(['',' ','x'.repeat(501)])('bounds required recovery reason',async reason=>{
await expect(orchestration.editorialFailure(task,{runId:run.id,expectedVersion:6,idempotencyKey:'k',reason})).rejects.toThrow();expect(api.post).not.toHaveBeenCalled();
});
it('uses fresh detail before modal; freezes retry bytes; blocks concurrency rejection until refresh',async()=>{
let details=0;
vi.mocked(api.get).mockImplementation(async url=>({data:url.endsWith('/overview')?{items:[],measuredAt:'now',capabilities:{mutations:true}}:url.endsWith('/tasks')?[task]:{...task,version:++details===1?6:7}}));
vi.mocked(api.post).mockRejectedValueOnce(new Error('offline')).mockRejectedValueOnce({response:{status:409}});
render(<OrchestrationCommandCenter/>);
fireEvent.click(await screen.findByRole('button',{name:task.title}));
fireEvent.click(await screen.findByRole('button',{name:'ثبت شکست اجرای متوقف‌شده'}));
const dialog=await screen.findByRole('dialog',{name:'ثبت شکست اجرای متوقف‌شده'});
expect(details).toBe(2);expect(api.post).not.toHaveBeenCalled();
expect(within(dialog).getByText(/۲۰ سنت.*نامشخص/)).toBeInTheDocument();
expect(within(dialog).getByText(/QA.*تأیید خودکار/)).toBeInTheDocument();
const submit=within(dialog).getByRole('button',{name:'ثبت شکست'});
expect(submit).toBeDisabled();
fireEvent.change(within(dialog).getByLabelText('دلیل شکست'),{target:{value:'Process ended'}});
fireEvent.click(within(dialog).getByLabelText('ثبت شکست بدون تأیید کیفیت یا آزادسازی بودجه را تأیید می‌کنم'));
fireEvent.click(submit);await within(dialog).findByRole('alert');
expect(within(dialog).getByLabelText('دلیل شکست')).toBeDisabled();
fireEvent.click(submit);await waitFor(()=>expect(submit).toBeDisabled());
expect(api.post).toHaveBeenCalledTimes(2);
const payload=vi.mocked(api.post).mock.calls[0][1];expect(payload).toEqual(expect.objectContaining({runId:run.id,expectedVersion:7,reason:'Process ended'}));expect(vi.mocked(api.post).mock.calls[1][1]).toEqual(payload);
fireEvent.click(within(dialog).getByRole('button',{name:'بازخوانی و بستن'}));await waitFor(()=>expect(screen.queryByRole('dialog')).not.toBeInTheDocument());
});
it('refreshes authoritative tasks after success without approval or budget commands',async()=>{
vi.mocked(api.post).mockResolvedValue({data:{state:'FAILED'}});
render(<OrchestrationCommandCenter/>);fireEvent.click(await screen.findByRole('button',{name:task.title}));fireEvent.click(await screen.findByRole('button',{name:'ثبت شکست اجرای متوقف‌شده'}));
const dialog=await screen.findByRole('dialog');
fireEvent.change(within(dialog).getByLabelText('دلیل شکست'),{target:{value:'Process ended'}});
fireEvent.click(within(dialog).getByLabelText('ثبت شکست بدون تأیید کیفیت یا آزادسازی بودجه را تأیید می‌کنم'));
fireEvent.click(within(dialog).getByRole('button',{name:'ثبت شکست'}));
await waitFor(()=>expect(screen.queryByRole('dialog')).not.toBeInTheDocument());
expect(api.post).toHaveBeenCalledTimes(1);expect(api.put).not.toHaveBeenCalled();
expect(vi.mocked(api.get).mock.calls.filter(([url])=>url.endsWith('/tasks'))).toHaveLength(2);
});
it('does not infer eligibility from RUNNING or guessed age',async()=>{
vi.mocked(api.get).mockImplementation(async url=>({data:url.endsWith('/tasks')?[task]:{...task,editorialRecovery:undefined}}));
render(<OrchestrationCommandCenter/>);fireEvent.click(await screen.findByRole('button',{name:task.title}));await screen.findByRole('region',{name:'جزئیات وظیفه'});
expect(screen.queryByRole('button',{name:'ثبت شکست اجرای متوقف‌شده'})).not.toBeInTheDocument();
});
it('rejects eligibility removed during fresh open without a mutation',async()=>{
let details=0;vi.mocked(api.get).mockImplementation(async url=>({data:url.endsWith('/overview')?{items:[],measuredAt:'now',capabilities:{mutations:true}}:url.endsWith('/tasks')?[task]:{...task,editorialRecovery:++details===1?task.editorialRecovery:undefined}}));
render(<OrchestrationCommandCenter/>);fireEvent.click(await screen.findByRole('button',{name:task.title}));fireEvent.click(await screen.findByRole('button',{name:'ثبت شکست اجرای متوقف‌شده'}));
expect(await screen.findByRole('alert')).toHaveTextContent(/بازخوانی/);expect(screen.queryByRole('dialog')).not.toBeInTheDocument();expect(api.post).not.toHaveBeenCalled();
});
@@ -34,7 +34,7 @@ export function normalizeActions(value:unknown):ServerAction[] {
}
export interface Artifact { id: string; sha256: string; kind?: string; uri?: string }
export interface Run { id: string; commitSha: string; environment: string; status: string; artifacts: Artifact[] }
export interface Task { id: string; key: string; title: string; department: string; state: string; displayState?: string; version: number; operationMode: string; budgetLimitCents: number; metadata?: Record<string, unknown>; availableActions: ServerAction[]; runs?: Run[]; decisions?: Record<string, unknown>[]; events?: Record<string, unknown>[]; budgetLedger?: Record<string, unknown>[] }
export interface Task { id: string; key: string; title: string; department: string; state: string; displayState?: string; editorialRecovery?: {runId:string}; version: number; operationMode: string; budgetLimitCents: number; metadata?: Record<string, unknown>; availableActions: ServerAction[]; runs?: Run[]; decisions?: Record<string, unknown>[]; events?: Record<string, unknown>[]; budgetLedger?: Record<string, unknown>[] }
export interface CommandInput { idempotencyKey: string; reason?: string; expiresAt?: string; notBefore?: string; runId?: string; artifactHash?: string }
export interface TransitionDto {toState:string;expectedVersion:number;idempotencyKey:string;reason?:string}
export interface GateDecisionDto {decision:'APPROVED'|'REJECTED';expectedVersion:number;idempotencyKey:string;expiresAt?:string;notBefore?:string;notes?:string;runId?:string;artifactHash?:string;commitSha?:string;environment?:string}
@@ -69,7 +69,18 @@ export function proposalError(input:CreateTaskInput):string|undefined {
if(!Array.isArray(input.metadata?.acceptanceCriteria)||input.metadata.acceptanceCriteria.length<1||input.metadata.acceptanceCriteria.length>20||input.metadata.acceptanceCriteria.some(value=>typeof value!=='string'||!value.trim()||value.length>500))return 'Acceptance criteria must contain 1..20 nonblank strings of at most 500 characters';
if(!input.key.trim()||input.key.length>120||!input.title.trim()||input.title.length>255||!input.department.trim()||input.department.length>50)return 'کلید (۱ تا ۱۲۰)، عنوان (۱ تا ۲۵۵) و دپارتمان (۱ تا ۵۰ نویسه) الزامی است';
}
export interface EditorialFailureInput {runId:string;expectedVersion:number;idempotencyKey:string;reason:string}
// Eligibility is a safe server hint, never inferred from elapsed browser time.
export function editorialRecoveryRun(task:Task):Run|undefined {
if(task.state!=='RUNNING'||task.operationMode!=='PREPARE'||task.department!=='CONTENT'||task.budgetLimitCents!==20||!/^editorial-v1:[a-f0-9]{64}$/.test(task.key))return;
const matches=task.runs?.filter(r=>r.id===task.editorialRecovery?.runId&&r.status==='RUNNING'&&r.environment==='staging'&&r.commitSha===task.metadata?.commitSha&&Array.isArray(r.artifacts)&&!r.artifacts.length);
return matches?.length===1?matches[0]:undefined;
}
export const orchestration = {
async editorialFailure(task:Task,input:EditorialFailureInput):Promise<unknown> {
if(!editorialRecoveryRun(task)||input.runId!==task.editorialRecovery?.runId||input.expectedVersion!==task.version||!Number.isInteger(input.expectedVersion)||!input.idempotencyKey||typeof input.reason!=='string'||!input.reason.trim()||input.reason.length>500)throw new Error('اتصال بازیابی معتبر نیست؛ وضعیت را بازخوانی کنید');
return (await api.post('/admin/orchestration/tasks/'+encodeURIComponent(task.id)+'/editorial-failure',{runId:input.runId,expectedVersion:input.expectedVersion,idempotencyKey:input.idempotencyKey,reason:input.reason})).data;
},
async create(input:CreateTaskInput):Promise<unknown> {
const error=proposalError(input);if(error)throw new Error(error);
// Preserve human evidence exactly; workflowScope/environment/identity remain server-owned.
+9
View File
@@ -0,0 +1,9 @@
# Owner recovery control
Command center task detail offers **ثبت شکست اجرای متوقف‌شده** only when the authenticated detail response supplies `editorialRecovery: {runId}` and that ID matches the registered RUNNING staging run/commit with no artifacts. The server derives the hint from the actual expired outbox lease, canonical editorial admission and unreconciled 20-cent reservation for an independent reviewer. Lists contain no hint. No lease owner/token/credentials are serialized. The existing failure command remains the atomic authority, including provenance, version and run/lease fencing.
Opening the control refetches detail. The modal requires a nonblank reason of at most 500 characters and explicit acknowledgement. It explains retained unknown 20-cent spend, not cost zero, no QA, no autoapproval, no release/retry/budget authorization. A first submit freezes exact runId/expectedVersion/idempotencyKey/reason bytes; ambiguous network retries reuse them. 4xx rejections require refresh. Successful commands and close reconcile with a fresh list. Background content is inert while the recovery modal is mounted.
This adds a backend **read-only detail hint** because the old safe run projection contains neither lease expiry nor a trustworthy recovery capability. A frontend-only age heuristic cannot establish actual expiry. Deploy both admin and backend changes through normal CI. No change to the failure endpoint, authorization, budgets or worker transport.
Validation: disposable source/dependency copies, no published ports or live state writes. Scoped admin/API tests and application tsc + Vite build; backend hint/failure/read/HTTP tests plus Prisma generation and tsc build. No push/deployment or live task recovery performed here; the owner must authenticate and click after integration.