Files
canina/backend/src/orchestration-worker/editorial-execution.ts
T

537 lines
23 KiB
TypeScript

import {
BadRequestException,
ForbiddenException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { IsInt, Min, IsNotEmpty, IsString, IsObject, IsUUID, Matches } from 'class-validator';
import { createHash } from 'crypto';
import {
PrepareWorkflowService,
WorkflowIdentity,
} from '../orchestration/prepare-workflow.service';
import { PrismaService } from '../prisma/prisma.service';
import { EDITORIAL_PILOT_KIND } from './editorial-bridge';
export class EditorialClaimDto {
@IsUUID() taskId!: string;
}
export class EditorialQueueDto extends EditorialClaimDto {
@IsInt() @Min(1) expectedVersion!: number;
@IsString() @IsNotEmpty() idempotencyKey!: string;
}
export class EditorialResultDto extends EditorialClaimDto {
@IsUUID() runId!: string;
@IsUUID() leaseToken!: string;
@IsObject() brief!: Record<string, unknown>;
}
export class CompletedRecoveryDto {
@IsUUID() runId!: string;
@IsInt() @Min(1) expectedVersion!: number;
@Matches(/^[a-f0-9]{64}$/) evidenceSha256!: string;
@IsObject() evidence!: Record<string, unknown>;
}
const suffix =
'\nResearch and evidence-backed brief only. No publication, release, production changes or execution authorization.';
export function admittedEditorialTask(t: any): boolean {
const m = t?.metadata;
const topic =
typeof t?.title === 'string' && t.title.startsWith('Editorial research: ')
? t.title.slice(20)
: '';
return !!(
t &&
/^editorial-v1:[a-f0-9]{64}$/.test(t.key) &&
t.department === 'CONTENT' &&
t.operationMode === 'PREPARE' &&
t.budgetLimitCents === 20 &&
m?.createdBy === 'pmo' &&
m.workflowScope === 'STAGE_PREPARE' &&
m.environment === 'staging' &&
typeof m.commitSha === 'string' &&
/^[a-f0-9]{40}$/.test(m.commitSha) &&
topic.trim() &&
topic.length <= 180 &&
m.scope === `${EDITORIAL_PILOT_KIND}\nTopic: ${topic}${suffix}` &&
Array.isArray(m.acceptanceCriteria) &&
m.acceptanceCriteria.length >= 1 &&
m.acceptanceCriteria.length <= 10 &&
m.acceptanceCriteria.every(
(x: unknown) => typeof x === 'string' && x.trim() && x.length <= 500,
)
);
}
function keys(v: any, names: string[]) {
return (
v &&
typeof v === 'object' &&
!Array.isArray(v) &&
Object.keys(v).length === names.length &&
names.every((k) => Object.prototype.hasOwnProperty.call(v, k))
);
}
function text(v: any, max: number) {
return typeof v === 'string' && !!v.trim() && v.length <= max;
}
function date(v: any) {
return (
typeof v === 'string' &&
/^\d{4}-\d{2}-\d{2}$/.test(v) &&
Number.isFinite(Date.parse(v)) &&
new Date(v).toISOString().slice(0, 10) === v &&
v <= new Date().toISOString().slice(0, 10)
);
}
function source(v: any) {
try {
if (!text(v, 2000)) return false;
const u = new URL(v);
return (
u.protocol === 'https:' &&
!u.username &&
!u.password &&
!u.port &&
/^[a-z0-9.-]+\.[a-z]{2,}$/.test(u.hostname) &&
!/(^|\.)(localhost|local|internal|lan|home|invalid|test|example)$/.test(
u.hostname,
)
);
} catch {
return false;
}
}
export function validateEditorialBrief(b: any) {
const fail = () => {
throw new BadRequestException(
'Bounded research brief requires dated sources, input availability and 3..10 scored candidates',
);
};
if (
!keys(b, ['researchDate', 'inputAvailability', 'findings', 'candidates']) ||
Buffer.byteLength(JSON.stringify(b), 'utf8') > 24576 ||
!date(b.researchDate)
)
fail();
if (
!Array.isArray(b.inputAvailability) ||
b.inputAvailability.length < 1 ||
b.inputAvailability.length > 10 ||
!b.inputAvailability.every(
(x: any) =>
keys(x, ['input', 'status', 'details']) &&
text(x.input, 120) &&
['AVAILABLE', 'UNAVAILABLE', 'PARTIAL'].includes(x.status) &&
text(x.details, 500),
)
)
fail();
if (
!Array.isArray(b.findings) ||
b.findings.length < 1 ||
b.findings.length > 20 ||
!b.findings.every(
(x: any) =>
keys(x, ['sourceUrl', 'sourceDate', 'finding']) &&
source(x.sourceUrl) &&
date(x.sourceDate) &&
x.sourceDate <= b.researchDate &&
text(x.finding, 1000),
)
)
fail();
const urls = new Set(b.findings.map((x: any) => x.sourceUrl));
if (
!Array.isArray(b.candidates) ||
b.candidates.length < 3 ||
b.candidates.length > 10 ||
!b.candidates.every(
(x: any) =>
keys(x, ['title', 'score', 'rationale', 'sourceUrls']) &&
text(x.title, 180) &&
Number.isInteger(x.score) &&
x.score >= 0 &&
x.score <= 100 &&
text(x.rationale, 1000) &&
Array.isArray(x.sourceUrls) &&
x.sourceUrls.length >= 1 &&
x.sourceUrls.length <= 10 &&
x.sourceUrls.every((u: any) => urls.has(u)),
) ||
new Set(b.candidates.map((x: any) => x.title.trim().toLowerCase())).size !==
b.candidates.length
)
fail();
return JSON.parse(JSON.stringify(b));
}
const kind = 'EDITORIAL_RESEARCH_BRIEF';
const qaStatus = 'PENDING_INDEPENDENT_QA';
@Injectable()
export class EditorialExecutionService {
constructor(private readonly db: PrismaService) {}
private pmo(a: WorkflowIdentity) {
if (a?.role !== 'WORKFLOW_WORKER' || a.id !== 'pmo')
throw new ForbiddenException(
'Explicit canonical PMO worker grant required',
);
}
private async scope(tx: any, id: string) {
if (typeof id !== 'string' || !/^[0-9a-f-]{36}$/i.test(id))
throw new NotFoundException('Editorial proposal not found');
const t = await tx.orchestrationTask.findUnique({ where: { id } });
if (!admittedEditorialTask(t))
throw new NotFoundException('Editorial proposal not found');
const proposed = await tx.orchestrationEvent.findFirst({
where: {
taskId: id,
sequence: 1,
eventType: 'PREPARE_PROPOSED',
actorId: 'pmo',
toState: 'DRAFT',
},
});
const submitted = await tx.orchestrationEvent.findFirst({
where: {
taskId: id,
sequence: 2,
eventType: 'PREPARE_TRANSITION',
actorId: 'pmo',
fromState: 'DRAFT',
toState: 'GATE1_PENDING',
idempotencyKey: t.key + ':submit',
},
});
if (!proposed || !submitted)
throw new NotFoundException('Editorial proposal not found');
return t;
}
private async approval(tx: any, t: any) {
const approval = await tx.orchestrationApprovalDecision.findFirst({
where: { taskId: t.id, gate: 1 },
orderBy: { createdAt: 'desc' },
});
if (
!approval ||
approval.decision !== 'APPROVED' ||
approval.actorId === 'pmo' ||
approval.expiresAt <= new Date()
)
throw new ForbiddenException('Valid independent Gate1 required');
}
private async lock(tx: any, id: string, claim = false) {
const switches =
await tx.$queryRaw`SELECT level, active FROM orchestration_kill_switches WHERE scope = 'GLOBAL' FOR SHARE`;
if (
switches[0]?.active &&
(claim || switches[0].level !== 'FREEZE_NEW_WORK')
)
throw new ForbiddenException('Kill switch blocks workflow');
await tx.$queryRaw`SELECT id FROM orchestration_tasks WHERE id = ${id}::uuid FOR UPDATE`;
const t = await this.scope(tx, id);
await this.approval(tx, t);
return t;
}
async status(a: WorkflowIdentity, id: string) {
this.pmo(a);
return this.db.$transaction(async (tx: any) => {
const t = await this.scope(tx, id);
const artifacts = await tx.orchestrationArtifact.findMany({
where: { kind, run: { taskId: id } },
orderBy: { createdAt: 'desc' },
take: 1,
});
const artifact = artifacts[0];
let result: any = null;
if (artifact) {
const bytes = artifact.metadata?.bytes;
if (
typeof bytes !== 'string' ||
Buffer.byteLength(bytes) > 32768 ||
createHash('sha256').update(bytes).digest('hex') !== artifact.sha256
)
throw new ForbiddenException('Artifact integrity mismatch');
const parsed = JSON.parse(bytes);
if (
parsed.taskId !== t.id ||
parsed.runId !== artifact.runId ||
parsed.commitSha !== t.metadata.commitSha
)
throw new ForbiddenException('Artifact binding mismatch');
result = { kind, sha256: artifact.sha256, bytes, brief: parsed.brief };
}
const approval = await tx.orchestrationApprovalDecision.findFirst({
where: { taskId: t.id, gate: 1 }, orderBy: { createdAt: 'desc' },
});
const valid = approval?.decision === 'APPROVED' && approval.actorId !== 'pmo' && approval.expiresAt > new Date();
return {
displayState: t.state === 'READY' && !valid ? (approval?.decision === 'APPROVED' && approval.actorId !== 'pmo' && approval.expiresAt <= new Date() ? 'GATE1_RENEWAL_REQUIRED' : 'BLOCKED') : t.state,
gate1Validity: valid ? 'VALID' : 'BLOCKED',
taskId: t.id,
pilotKind: EDITORIAL_PILOT_KIND,
state: t.state,
version: t.version,
operationMode: t.operationMode,
budgetLimitCents: 20,
qaStatus,
metadata: {
scope: t.metadata.scope,
acceptanceCriteria: t.metadata.acceptanceCriteria,
commitSha: t.metadata.commitSha,
environment: 'staging',
},
artifact: result,
};
});
}
async queue(a: WorkflowIdentity, dto: EditorialQueueDto) {
this.pmo(a);
return this.db.$transaction(async (tx:any)=>{
await this.lock(tx,dto.taskId,true);
const core=new PrepareWorkflowService(new Proxy(tx, {get(target,key){
if(key==='$transaction') return (fn:any)=>fn(tx);
const value=target[key]; return typeof value==='function'?value.bind(target):value;
}}),{workerIds:['pmo'],stageCiIds:[]});
const t=await core.enqueueApproved(a,dto);
return {taskId:t.id,state:t.state,version:t.version};
});
}
async claim(a: WorkflowIdentity, dto: EditorialClaimDto) {
this.pmo(a);
return this.db.$transaction(async (tx: any) => {
const t = await this.lock(tx, dto.taskId, true);
if (!['READY', 'RUNNING'].includes(t.state))
throw new ForbiddenException('Ready editorial proposal required');
// Reuse core claim/reservation/lease algorithm in THIS scoped transaction. No
// nested transaction or check-then-claim gap: task lock spans admission/claim.
const core = new PrepareWorkflowService(
new Proxy(tx, {
get(target, key) {
if (key === '$transaction') return (fn: any) => fn(tx);
const value = target[key];
return typeof value === 'function' ? value.bind(target) : value;
},
}),
{ workerIds: ['pmo'], stageCiIds: [] },
);
const lease = await core.claim(a, t.id);
return {
lease: lease ? { ...lease, pilotKind: EDITORIAL_PILOT_KIND } : null,
};
});
}
async recoverCompleted(a: WorkflowIdentity, taskId: string, dto: CompletedRecoveryDto) {
if (a?.role !== 'SUPER_ADMIN' || !a.id || a.id === 'pmo')
throw new ForbiddenException('Independent Stage reviewer required');
const e: any = dto.evidence;
const evidenceBytes = JSON.stringify(e);
const digest = (s: string) => createHash('sha256').update(s).digest('hex');
if (Buffer.byteLength(evidenceBytes) > 262144 || digest(evidenceBytes) !== dto.evidenceSha256 ||
e?.taskId !== taskId || e.runId !== dto.runId || e.expectedVersion !== dto.expectedVersion ||
typeof e.originalText !== 'string' || !Array.isArray(e.sessions) || e.sessions.length !== 4 ||
!Array.isArray(e.outputs) || e.correction?.mode !== 'STORED_REPORTS_ONLY' ||
!Array.isArray(e.correction.statusChanges) || !Array.isArray(e.correction.omissions) ||
!text(e.correction.reason, 1000)) throw new BadRequestException('Bounded exact stored evidence required');
const profiles = ['canina-competitor-worker','canina-content-worker','canina-seo-worker','pmo'];
if (e.sessions.some((s: any, i: number) => s.profile !== profiles[i] ||
!/^\d{8}_\d{6}_[a-f0-9]{6}$/.test(s.sessionId) || s.outcome !== 'COMPLETED' ||
!/^[a-f0-9]{64}$/.test(s.outputSha256)) ||
e.sessions[3].outputSha256 !== digest(e.originalText))
throw new BadRequestException('Four immutable completed session bindings required');
const brief = validateEditorialBrief(e.brief);
let original: any;
try { original = JSON.parse(e.originalText); } catch { throw new BadRequestException('Original JSON required'); }
const same = (x: any, y: any) => JSON.stringify(x) === JSON.stringify(y);
if (!Array.isArray(original.inputAvailability) || !Array.isArray(original.findings) || !Array.isArray(original.candidates) ||
original.researchDate !== brief.researchDate || original.inputAvailability.length !== brief.inputAvailability.length)
throw new BadRequestException('Original meaning and dates must be preserved');
for (let i=0;i<brief.inputAvailability.length;i++) {
const before=original.inputAvailability[i], after=brief.inputAvailability[i];
if (before.input !== after.input || before.details !== after.details) throw new BadRequestException('Availability text must be preserved');
const changes=e.correction.statusChanges.filter((x:any)=>x.index===i);
if (before.status !== after.status && (changes.length!==1 || changes[0].from!==before.status || changes[0].to!==after.status ||
!text(changes[0].reason,1000))) throw new BadRequestException('Explicit grounded status correction required');
}
for (const collection of ['findings','candidates']) {
if (!brief[collection].every((x:any)=>original[collection].some((y:any)=>same(x,y))))
throw new BadRequestException('No rewritten findings, candidates or source lists');
for (let i=0;i<original[collection].length;i++) if (!brief[collection].some((x:any)=>same(x,original[collection][i])) &&
!e.correction.omissions.some((x:any)=>x.collection===collection && x.index===i && text(x.reason,1000)))
throw new BadRequestException('Explicit omission metadata required');
}
const retrieved = new Set<string>();
for (const output of e.outputs) {
const pending: any[] = [];
if (!Array.isArray(output.retrievalEvents)) throw new BadRequestException('Retrieval events required');
for (const event of output.retrievalEvents) {
if (event.type === 'tool_use' && event.name === 'web_extract') pending.push(event);
if (event.type !== 'tool_result' || event.name !== 'web_extract') continue;
const index = pending.findIndex(x => x.tool_call_id === event.tool_call_id);
if (index < 0) continue;
const use = pending.splice(index, 1)[0];
if (event.is_error || typeof event.output !== 'string') continue;
let entries: any;
try { entries = JSON.parse(event.output).results; } catch { continue; }
if (Array.isArray(entries) && Array.isArray(use.input?.urls))
for (const x of entries) if (use.input.urls.includes(x?.url) && !x.error &&
typeof x.content === 'string' && x.content.trim()) retrieved.add(x.url);
}
}
if (brief.findings.some((x: any) => !retrieved.has(x.sourceUrl)))
throw new BadRequestException('Finding lacks matched successful worker extraction');
return this.db.$transaction(async (tx: any) => {
const t = await this.lock(tx, taskId);
const o = await tx.prepareWorkflowOutbox.findUnique({where:{taskId}});
const r = await tx.orchestrationRun.findUnique({where:{id:dto.runId}});
if (t.state !== 'RUNNING' || t.version !== dto.expectedVersion || e.commitSha !== t.metadata.commitSha ||
!o || o.status !== 'PENDING' || o.leaseOwner !== 'pmo' || o.runId !== dto.runId ||
!o.leaseUntil || o.leaseUntil > new Date() || !r || r.taskId !== taskId ||
r.status !== 'RUNNING' || r.environment !== 'staging' || r.commitSha !== e.commitSha ||
await tx.orchestrationArtifact.count({where:{run:{taskId}}}))
throw new ForbiddenException('Exact expired artifact-free same run required');
const reserve = await tx.orchestrationBudgetLedger.findUnique({where:{id:o.reservationId}});
const ledger = await tx.orchestrationBudgetLedger.aggregate({where:{taskId},_sum:{amountCents:true}});
if (!reserve || reserve.taskId !== taskId || reserve.actorId !== 'pmo' || reserve.kind !== 'RESERVE' ||
reserve.amountCents !== 20 || reserve.idempotencyKey !== `prepare-reserve:${r.id}` ||
ledger._sum.amountCents !== 20 || await tx.orchestrationBudgetLedger.findFirst({where:{taskId,kind:'RECONCILE'}}))
throw new ForbiddenException('Existing unreconciled reservation required');
await tx.orchestrationArtifact.create({data:{runId:r.id,kind:'EDITORIAL_RECOVERY_EVIDENCE',sha256:dto.evidenceSha256,
uri:`urn:canina:editorial-recovery:sha256:${dto.evidenceSha256}`,sizeBytes:Buffer.byteLength(evidenceBytes),
metadata:{bytes:evidenceBytes,originalOutputSha256:digest(e.originalText),reviewerId:a.id,qaStatus}}});
const stored = await tx.orchestrationArtifact.findFirst({where:{runId:r.id,kind:'EDITORIAL_RECOVERY_EVIDENCE',sha256:dto.evidenceSha256}});
if (!stored || stored.metadata?.bytes !== evidenceBytes || digest(stored.metadata.bytes) !== dto.evidenceSha256)
throw new ForbiddenException('Stored canonical evidence hash required');
const bytes = JSON.stringify({kind,pilotKind:EDITORIAL_PILOT_KIND,schemaVersion:1,taskId,runId:r.id,
commitSha:r.commitSha,environment:'staging',qaStatus,costStatus:'upper-bound-reserved',brief,
recoveryEvidenceSha256:dto.evidenceSha256});
const hash = digest(bytes);
await tx.orchestrationArtifact.create({data:{runId:r.id,kind,sha256:hash,uri:`urn:canina:editorial-brief:sha256:${hash}`,
sizeBytes:Buffer.byteLength(bytes),metadata:{bytes,qaStatus,costStatus:'upper-bound-reserved'}}});
await tx.orchestrationRun.update({where:{id:r.id},data:{status:'AWAITING_QA',finishedAt:new Date()}});
await tx.prepareWorkflowOutbox.update({where:{id:o.id},data:{status:'DONE',leaseToken:null,leaseUntil:null}});
const updated = await tx.orchestrationTask.update({where:{id:taskId},data:{state:'VERIFYING',version:{increment:1}}});
await tx.orchestrationEvent.create({data:{taskId,sequence:updated.version,actorId:a.id,eventType:'EDITORIAL_COMPLETED_RUN_RECOVERED',
fromState:'RUNNING',toState:'VERIFYING',idempotencyKey:`editorial-recovery:${r.id}`,
payload:{artifactSha256:hash,evidenceSha256:dto.evidenceSha256,originalOutputSha256:digest(e.originalText),qaStatus,
retainedReservationCents:20,costStatus:'upper-bound-reserved',newResearch:false}}});
return {taskId,runId:r.id,state:updated.state,artifactSha256:hash,qaStatus,retainedReservationCents:20,costStatus:'upper-bound-reserved'};
});
}
async result(a: WorkflowIdentity, dto: EditorialResultDto) {
this.pmo(a);
const brief = validateEditorialBrief(dto.brief);
// Core complete writes a passed QA attestation and reconciles supplied costs.
// It is deliberately NOT reused for unreviewed research or unknown cost.
return this.db.$transaction(async (tx: any) => {
const t = await this.lock(tx, dto.taskId);
const o = await tx.prepareWorkflowOutbox.findUnique({
where: { taskId: t.id },
});
const r = await tx.orchestrationRun.findUnique({
where: { id: dto.runId },
});
if (
t.state !== 'RUNNING' ||
!o ||
o.status !== 'PENDING' ||
o.leaseOwner !== a.id ||
o.leaseToken !== dto.leaseToken ||
o.runId !== dto.runId ||
!o.leaseUntil ||
o.leaseUntil <= new Date() ||
!r ||
r.taskId !== t.id ||
r.status !== 'RUNNING' ||
r.environment !== 'staging' ||
r.commitSha !== t.metadata.commitSha
)
throw new ForbiddenException('Active owned editorial lease required');
const reserve = await tx.orchestrationBudgetLedger.findUnique({
where: { id: o.reservationId },
});
const ledger = await tx.orchestrationBudgetLedger.aggregate({
where: { taskId: t.id },
_sum: { amountCents: true },
});
if (
!reserve ||
reserve.taskId !== t.id ||
reserve.actorId !== a.id ||
reserve.kind !== 'RESERVE' ||
reserve.amountCents !== 20 ||
reserve.idempotencyKey !== `prepare-reserve:${r.id}` ||
ledger._sum.amountCents !== 20 ||
(await tx.orchestrationBudgetLedger.findFirst({
where: { taskId: t.id, kind: 'RECONCILE' },
}))
)
throw new ForbiddenException(
'Unreconciled owned 20 cent reservation required',
);
const bytes = JSON.stringify({
kind,
pilotKind: EDITORIAL_PILOT_KIND,
schemaVersion: 1,
taskId: t.id,
runId: r.id,
commitSha: r.commitSha,
environment: 'staging',
qaStatus,
costStatus: 'upper-bound-reserved',
brief,
});
const hash = createHash('sha256').update(bytes).digest('hex');
// Canonical bytes live in append-only DB artifact metadata, not a caller URI
// or mutable external file. PMO status is the authenticated retrieval path.
await tx.orchestrationArtifact.create({
data: {
runId: r.id,
kind,
sha256: hash,
uri: `urn:canina:editorial-brief:sha256:${hash}`,
sizeBytes: Buffer.byteLength(bytes),
metadata: { bytes, qaStatus, costStatus: 'upper-bound-reserved' },
},
});
await tx.orchestrationRun.update({
where: { id: r.id },
data: { status: 'AWAITING_QA', finishedAt: new Date() },
});
await tx.prepareWorkflowOutbox.update({
where: { id: o.id },
data: { status: 'DONE', leaseToken: null, leaseUntil: null },
});
const updated = await tx.orchestrationTask.update({
where: { id: t.id },
data: { state: 'VERIFYING', version: { increment: 1 } },
});
await tx.orchestrationEvent.create({
data: {
taskId: t.id,
sequence: updated.version,
actorId: a.id,
eventType: 'EDITORIAL_BRIEF_RECEIVED',
fromState: t.state,
toState: 'VERIFYING',
idempotencyKey: `editorial-result:${r.id}`,
payload: {
artifactSha256: hash,
qaStatus,
costStatus: 'upper-bound-reserved',
retainedReservationCents: 20,
},
},
});
return {
taskId: t.id,
runId: r.id,
state: updated.state,
artifactSha256: hash,
qaStatus,
costStatus: 'upper-bound-reserved',
retainedReservationCents: 20,
};
});
}
}