From a1189affe692009af8791a76d8420b5386a5156c Mon Sep 17 00:00:00 2001 From: devops Date: Fri, 23 Oct 2026 19:32:19 +0000 Subject: [PATCH] Add separately admitted Tech source ingestion with retained unknown cost --- .../orchestration-worker/tech-ingestion.md | 26 +++++++ .../tech-ingestion.spec.ts | 74 ++++++++++++++++++ .../tech-pilot-validator.ts | 35 +++++++++ .../worker-transport.module.ts | 77 +++++++++++++++++-- 4 files changed, 207 insertions(+), 5 deletions(-) create mode 100644 backend/src/orchestration-worker/tech-ingestion.md create mode 100644 backend/src/orchestration-worker/tech-ingestion.spec.ts create mode 100644 backend/src/orchestration-worker/tech-pilot-validator.ts diff --git a/backend/src/orchestration-worker/tech-ingestion.md b/backend/src/orchestration-worker/tech-ingestion.md new file mode 100644 index 00000000..dbc2e160 --- /dev/null +++ b/backend/src/orchestration-worker/tech-ingestion.md @@ -0,0 +1,26 @@ +# Tech fixture ingestion adapter contract + +Separate fixed pilot, never application QA, deployment evidence, Gate2 or release authorization. + +## Frozen HTTP contract + +Authenticated worker Bearer credential (same guard/config as existing transport): + +- `POST /internal/prepare-worker/tech-claim` body `{taskId: UUID}` -> `{lease:null | {...existingLease,pilotKind:"tech-dev-clamp-quantity-v1"}}`. +- `POST /internal/prepare-worker/heartbeat` body `{taskId,runId,leaseToken}` unchanged. +- `POST /internal/prepare-worker/tech-complete` body **only** `{taskId:UUID,runId:UUID,leaseToken:UUID,source:string}`. Source max2048 characters AND UTF-8 bytes. Returns `{taskId,runId,state:"VERIFYING",artifactSha256,costStatus:"upper-bound-reserved"}`. +- `GET /internal/prepare-worker/artifacts/` authenticated own-worker or independent Stage CI. Returns canonical JSON containing actual source, run/commit binding, server QA9, sourceSha256, `applicationQa:false`, `retainedReservationCents:20`, and costStatus. Hash is over canonical stored JSON, not source bytes; source digest is in `qa.sourceSha256`. + +Stored task admission requires exact key AND metadata.scope `tech-dev-clamp-quantity-v1`, department TECH, PREPARE, budget20, workflowScope STAGE_PREPARE, environment staging and immutable40hex commit. Existing Gate1/READY workflow must be used. No body pilotKind/scope/pass/cost/URI/identity accepted. Task IDs never authorize themselves. Generic tracer completion refuses Tech key or scope so it cannot reconcile unknown provider cost as zero. + +## Parent adapter wiring + +After independent Gate1/READY and provider cap approval, call tech-claim, require pilotKind and existing lease bounds, and pass only the original executor lease fields to `infra/hermes-pilot/executor.py` (strip pilotKind because executor uses strict fields). Reuse authenticated heartbeat with <=5s network deadline/no retry and ongoing executor fencing. On successful local executor result, read actual quantity.py (not a report/QA claim) bounded2048 and submit exact source with original binding to tech-complete. Backend independently parses/interprets a fixed expression grammar without executing Python or any subprocess. It accepts only one clamp_quantity(quantity) function with a single nested min/max/0..99/quantity return. Depth32/nodes128/source2048 bound computation; nine boundary cases must pass. This intentionally narrower grammar can reject Python that local AST QA accepts; fail closed, do not rewrite model source. + +Retain the existing20-cent RESERVE unchanged: no RECONCILE row, actualCents or measured-cost claim. Server creates TECH_PILOT_SOURCE artifact and TECH_PILOT_FIXTURE_QA attestation signed server-bounded-minmax-v1. No generic QA/TEST attestation. State VERIFYING is fixture completion, not application acceptance. Independent CI remains separate and must not infer application QA from this artifact. + +Descriptor-relative O_EXCL/O_NOFOLLOW immutable content storage and bounded authenticated digest/binding checks are reused. Source max2048, artifact JSON read max8192 for escaping/envelope. Same-UID/root storage attackers remain outside boundary. File-before-transaction can leave an unregistered unreadable orphan on cancellation/races; no automatic overwrite/retry. Exact stored task mutation is rechecked at commit, but operational task metadata/key mutation must remain forbidden after review. + +## Verification + +Run existing `WORKER_EVIDENCE_DIR=/opt/services/canina/validation/v4-tech-ingestion bash infra/hermes-worker/test-isolated.sh green-final` from this worktree. Disposable PostgreSQL16/no published ports/no live volumes, real Nest TCP HTTP, existing child executor regression and backend build. Evidence includes red.log (missing route404), tracer-bypass-red.log (old endpoint201 instead of403), green-final.log and build.log. No model/provider call, live database/runtime edit, push, deployment or RELEASE_AUTHORIZE was performed. diff --git a/backend/src/orchestration-worker/tech-ingestion.spec.ts b/backend/src/orchestration-worker/tech-ingestion.spec.ts new file mode 100644 index 00000000..64508cd7 --- /dev/null +++ b/backend/src/orchestration-worker/tech-ingestion.spec.ts @@ -0,0 +1,74 @@ +import { Test } from '@nestjs/testing'; +import { INestApplication } from '@nestjs/common'; +import { PrismaClient } from '@prisma/client'; +import { PrismaService } from '../prisma/prisma.service'; +import { PrepareWorkflowService } from '../orchestration/prepare-workflow.service'; +import { WorkerTransportModule, tracerReport } from './worker-transport.module'; +import { createHash, randomUUID } from 'crypto'; +import { mkdtemp, rm, readFile } from 'fs/promises'; +import { join } from 'path'; +import { tmpdir } from 'os'; +const enabled = process.env.WORKER_TRANSPORT_DB === '1'; +(enabled ? describe : describe.skip)('real TCP tech artifact ingestion with PostgreSQL', () => { + const db = new PrismaClient(); + const token = 'test-tech-worker-secret-'.repeat(3), other = 'test-other-worker-secret-'.repeat(3), ci = 'test-ci-secret-'.repeat(3); + const sha = (s: string) => createHash('sha256').update(s).digest('hex'); + let app: INestApplication, workflow: PrepareWorkflowService, base: string, directory: string; + const kind = 'tech-dev-clamp-quantity-v1'; + const source = 'def clamp_quantity(quantity):\n return max(0, min(quantity, 99))\n'; + const owner = {id:'tech-author',role:'Admin'}, reviewer = {id:'tech-reviewer',role:'SUPER_ADMIN'}; + beforeAll(async () => { + directory = await mkdtemp(join(tmpdir(), 'tech-artifacts-')); + const mod = await Test.createTestingModule({imports:[WorkerTransportModule.register({enabled:true, grants:[{id:'tech',role:'WORKFLOW_WORKER',credentialSha256:sha(token)},{id:'other',role:'WORKFLOW_WORKER',credentialSha256:sha(other)},{id:'ci',role:'STAGE_CI',credentialSha256:sha(ci)}],workerIds:['tech','other'],stageCiIds:['ci'],artifactDirectory:directory,artifactBaseUrl:'https://isolated.invalid/internal/prepare-worker/artifacts'})]}).overrideProvider(PrismaService).useValue(db).compile(); + workflow = mod.get(PrepareWorkflowService); app = mod.createNestApplication(); await app.listen(0,'127.0.0.1'); base = await app.getUrl(); + }); + afterAll(async () => {await app?.close(); await db.$disconnect(); if(directory) await rm(directory,{recursive:true,force:true});}); + async function post(route: string, body: any, secret=token) {const r=await fetch(base+'/internal/prepare-worker/'+route,{method:'POST',headers:{authorization:'Bearer '+secret,'content-type':'application/json'},body:JSON.stringify(body)});return {status:r.status,body:await r.json()};} + async function get(hash: string, secret=token) {const r=await fetch(base+'/internal/prepare-worker/artifacts/'+hash,{headers:{authorization:'Bearer '+secret}});return {status:r.status,body:await r.json()};} + async function task(key=kind, scope=kind, ready=true) { + // Exact reviewed key is unique; old fixture rows are never reused as admission. + if(key===kind) { const existing=await db.orchestrationTask.findUnique({where:{key}}); if(existing) await db.orchestrationTask.update({where:{id:existing.id},data:{key:'used-'+randomUUID()}}); } + let t=await workflow.create({key,title:'quantity fixture only',department:'TECH',operationMode:'PREPARE',budgetLimitCents:20,metadata:{commitSha:'a'.repeat(40),scope,acceptanceCriteria:['quantity fixture only; not application QA']}},owner); + if(ready) {t=await workflow.transition(t.id,{toState:'GATE1_PENDING',expectedVersion:t.version,idempotencyKey:randomUUID()},owner);t=await workflow.decide(t.id,1,{decision:'APPROVED',expiresAt:new Date(Date.now()+60000).toISOString(),expectedVersion:t.version,idempotencyKey:randomUUID()},reviewer);t=await workflow.transition(t.id,{toState:'READY',expectedVersion:t.version,idempotencyKey:randomUUID()},owner);} + return t; + } + it('rejects unreviewed stored key/scope and cannot dispatch before Gate1',async()=>{ + for(const [key,scope] of [[randomUUID(),kind],[kind,'other scope']]) {const t=await task(key,scope);expect((await post('tech-claim',{taskId:t.id})).status).toBe(403);expect(await db.orchestrationRun.count({where:{taskId:t.id}})).toBe(0);} + const t=await task(kind,kind,false);const r=await post('tech-claim',{taskId:t.id});expect(r.status).toBe(201);expect(r.body.lease).toBeNull();expect(await db.orchestrationRun.count({where:{taskId:t.id}})).toBe(0); + }); + it('fences callers, rejects arbitrary source/fields and retains budget on denial',async()=>{ + const t=await task();const claims=await Promise.all([post('tech-claim',{taskId:t.id}),post('tech-claim',{taskId:t.id})]);const leases=claims.filter(x=>x.body.lease);expect(leases).toHaveLength(1);const l=leases[0].body.lease;const b={taskId:t.id,runId:l.runId,leaseToken:l.leaseToken}; + expect((await post('tech-complete',{...b,source},ci)).status).toBe(403);expect((await post('tech-complete',{...b,source},other)).status).toBe(403);expect((await post('tech-complete',{...b,leaseToken:randomUUID(),source})).status).toBe(403); + for(const bad of ['import os\n'+source, 'def clamp_quantity(quantity):\n return 0\n', 'def clamp_quantity(quantity):\n return __import__("os")\n', 'def clamp_quantity(quantity):\n return min(quantity,99)\n','x'.repeat(2049), 12]) expect((await post('tech-complete',{...b,source:bad})).status).toBe(400); + for(const extra of [{actualCents:0},{qa:{passed:true}},{uri:'https://fake.invalid'},{role:'STAGE_CI'}]) expect((await post('tech-complete',{...b,source,...extra})).status).toBe(400); + expect(await db.orchestrationArtifact.count({where:{runId:l.runId}})).toBe(0); + await db.prepareWorkflowOutbox.update({where:{taskId:t.id},data:{leaseUntil:new Date(Date.now()-1000)}});expect((await post('tech-complete',{...b,source})).status).toBe(403); + expect((await db.orchestrationBudgetLedger.findMany({where:{taskId:t.id}})).map(x=>[x.kind,x.amountCents])).toEqual([['RESERVE',20]]); + }); + it('honors cancellation and STOP_AGENTS before storing accepted evidence',async()=>{ + for(const stop of ['cancel','kill']) { + const t=await task();const {body}=await post('tech-claim',{taskId:t.id});const l=body.lease;const b={taskId:t.id,runId:l.runId,leaseToken:l.leaseToken}; + if(stop==='cancel') await db.orchestrationTask.update({where:{id:t.id},data:{state:'CANCELLED'}}); + else await db.orchestrationKillSwitch.upsert({where:{scope:'GLOBAL'},create:{scope:'GLOBAL',level:'STOP_AGENTS',active:true,reason:'disposable tech test',actorId:'fixture'},update:{level:'STOP_AGENTS',active:true}}); + try {expect((await post('tech-complete',{...b,source})).status).toBe(403);expect(await db.orchestrationArtifact.count({where:{runId:l.runId}})).toBe(0);} + finally {if(stop==='kill') await db.orchestrationKillSwitch.update({where:{scope:'GLOBAL'},data:{active:false}});} + } + }); + it('does not let tracer completion release a Tech reservation',async()=>{ + const t=await task();const {body}=await post('tech-claim',{taskId:t.id});const l=body.lease; + const result=await post('complete',{taskId:t.id,runId:l.runId,leaseToken:l.leaseToken,report:tracerReport(l)}); + expect(result.status).toBe(403); + expect((await db.orchestrationBudgetLedger.findMany({where:{taskId:t.id}})).map(x=>[x.kind,x.amountCents])).toEqual([['RESERVE',20]]); + }); + it('ingests actual source with independent fixture QA and retains unknown-cost reservation',async()=>{ + const t=await task(); const claim=await post('tech-claim',{taskId:t.id}); expect(claim.status).toBe(201); expect(claim.body.lease.pilotKind).toBe(kind); + const l=claim.body.lease, binding={taskId:t.id,runId:l.runId,leaseToken:l.leaseToken}; + const completed=await post('tech-complete',{...binding,source}); expect(completed.status).toBe(201); expect(completed.body).toEqual({taskId:t.id,runId:l.runId,state:'VERIFYING',artifactSha256:expect.stringMatching(/^[a-f0-9]{64}$/),costStatus:'upper-bound-reserved'}); + const hash=completed.body.artifactSha256; const artifact=await get(hash); expect(artifact.status).toBe(200); expect(artifact.body).toMatchObject({source,taskId:t.id,runId:l.runId,pilotKind:kind,applicationQa:false,costStatus:'upper-bound-reserved',qa:{passed:true,cases:9,validator:'bounded-minmax-v1'}}); + expect(sha(await readFile(join(directory,hash+'.json'),'utf8'))).toBe(hash); + expect((await get(hash,other)).status).toBe(404); expect((await get(hash,ci)).status).toBe(200); + const ledger=await db.orchestrationBudgetLedger.findMany({where:{taskId:t.id}}); expect(ledger.map(x=>[x.kind,x.amountCents])).toEqual([['RESERVE',20]]); + const attestations=await db.orchestrationAttestation.findMany({where:{runId:l.runId}}); expect(attestations).toHaveLength(1);expect(attestations[0].kind).toBe('TECH_PILOT_FIXTURE_QA');expect(attestations[0].predicate).toMatchObject({applicationQa:false,sourceSha256:sha(source),cases:9}); + expect((await post('tech-complete',{...binding,source})).status).toBe(403); + }); +}); diff --git a/backend/src/orchestration-worker/tech-pilot-validator.ts b/backend/src/orchestration-worker/tech-pilot-validator.ts new file mode 100644 index 00000000..273b2132 --- /dev/null +++ b/backend/src/orchestration-worker/tech-pilot-validator.ts @@ -0,0 +1,35 @@ +import { BadRequestException } from '@nestjs/common'; +import { createHash } from 'crypto'; +export const TECH_PILOT_KIND = 'tech-dev-clamp-quantity-v1'; +export function admittedTechTask(t: any): boolean { + return !!t && t.key === TECH_PILOT_KIND && t.department === 'TECH' && + t.operationMode === 'PREPARE' && t.budgetLimitCents === 20 && + t.metadata?.scope === TECH_PILOT_KIND && t.metadata?.workflowScope === 'STAGE_PREPARE' && + t.metadata?.environment === 'staging' && /^[a-f0-9]{40}$/.test(t.metadata?.commitSha ?? ''); +} +/** Separate bounded recognizer/interpreter. No Python, eval, subprocess or model QA claims. */ +export function validateTechSource(source: string) { + const deny = (): never => { throw new BadRequestException('Only bounded clamp_quantity min/max fixture source passing QA9 accepted'); }; + if(typeof source !== 'string' || Buffer.byteLength(source, 'utf8') > 2048) deny(); + const body = /^def clamp_quantity\(quantity\):[ \t]*\r?\n[ \t]+return ([^\r\n]+)[ \t]*\r?\n?$/.exec(source); + if(!body) deny(); + const text = body![1]; let pos=0, nodes=0; + type Expr = {kind:'q'} | {kind:'n';value:number} | {kind:'min'|'max';left:Expr;right:Expr}; + const ws=()=>{while(text[pos]===' ' || text[pos]==='\t') pos++;}; + function parse(depth:number): Expr { + if(depth>32 || ++nodes>128) return deny(); ws(); + if(text.slice(pos,pos+8)==='quantity') {pos+=8;return {kind:'q'};} + const number=/^(0|[1-9][0-9]?)(?![0-9])/.exec(text.slice(pos)); + if(number) {pos+=number[0].length;return {kind:'n',value:Number(number[0])};} + const name=text.slice(pos,pos+3); if(name!=='min' && name!=='max') return deny(); pos+=3;ws(); + if(text[pos++]!=='(') return deny(); const left=parse(depth+1);ws();if(text[pos++]!==',') return deny(); + const right=parse(depth+1);ws();if(text[pos++]!==')') return deny();return {kind:name,left,right}; + } + const tree=parse(0);ws();if(pos!==text.length) deny(); + function evaluate(n:Expr,q:number): number {if(n.kind==='q') return q;if(n.kind==='n') return n.value;const a=evaluate(n.left,q),b=evaluate(n.right,q);return n.kind==='min'?Math.min(a,b):Math.max(a,b);} + for(const q of [-1000,-1,0,1,50,98,99,100,1e30]) if(evaluate(tree,q)!==Math.max(0,Math.min(q,99))) deny(); + return {passed:true,cases:9,validator:'bounded-minmax-v1',scope:'quantity clamp fixture only',sourceSha256:createHash('sha256').update(source).digest('hex')}; +} +export function techArtifact(binding:{taskId:string;runId:string;commitSha:string},source:string) { + return JSON.stringify({pilotKind:TECH_PILOT_KIND,schemaVersion:1,...binding,environment:'staging',source,applicationQa:false,costStatus:'upper-bound-reserved',retainedReservationCents:20,qa:validateTechSource(source)}); +} diff --git a/backend/src/orchestration-worker/worker-transport.module.ts b/backend/src/orchestration-worker/worker-transport.module.ts index 3802abe1..3d5e2e97 100644 --- a/backend/src/orchestration-worker/worker-transport.module.ts +++ b/backend/src/orchestration-worker/worker-transport.module.ts @@ -32,6 +32,8 @@ import { WorkflowIdentity, } from "../orchestration/prepare-workflow.service"; +import { admittedTechTask, TECH_PILOT_KIND, techArtifact, validateTechSource } from "./tech-pilot-validator"; + // This is reviewed static configuration, not a URL fetched by the transport. function validArtifactBase(value: string): boolean { try { @@ -69,6 +71,9 @@ class LeaseDto extends ClaimDto { class CompleteDto extends LeaseDto { @IsString() @MaxLength(2048) report!: string; } +class TechCompleteDto extends LeaseDto { + @IsString() @MaxLength(2048) source!: string; +} class StageEvidenceDto extends ClaimDto { @IsUUID() runId!: string; @Matches(/^[a-f0-9]{40}$/) commitSha!: string; @@ -162,6 +167,9 @@ class WorkerTransportService { } async complete(a: WorkflowIdentity, dto: CompleteDto) { this.worker(a); + const task = await this.db.orchestrationTask.findUnique({ where: { id: dto.taskId } }); + if (task?.key === TECH_PILOT_KIND || (task?.metadata as any)?.scope === TECH_PILOT_KIND) + throw new ForbiddenException("Tech pilot requires separate source completion"); // Recheck stop/interrupt and fencing before artifact creation; core rechecks atomically at commit. await this.workflow.heartbeat(a, dto); const run = await this.db.orchestrationRun.findUniqueOrThrow({ @@ -196,12 +204,59 @@ class WorkerTransportService { actualCents: 0, }; } + async techClaim(a: WorkflowIdentity, dto: ClaimDto) { + this.worker(a); + const t = await this.db.orchestrationTask.findUnique({ where: { id: dto.taskId } }); + if (!admittedTechTask(t)) throw new ForbiddenException("Reviewed Tech pilot task required"); + const { lease } = await this.claim(a, dto); + return { lease: lease ? { ...lease, pilotKind: TECH_PILOT_KIND } : null }; + } + async techComplete(a: WorkflowIdentity, dto: TechCompleteDto) { + this.worker(a); + await this.workflow.heartbeat(a, dto); + const task = await this.db.orchestrationTask.findUnique({ where: { id: dto.taskId } }); + const run = await this.db.orchestrationRun.findUnique({ where: { id: dto.runId } }); + if (!admittedTechTask(task) || !run || run.taskId !== dto.taskId || run.commitSha !== (task!.metadata as any).commitSha || run.environment !== 'staging') + throw new ForbiddenException('Reviewed Tech pilot task required'); + const qa = validateTechSource(dto.source); + const bytes = techArtifact({ taskId: dto.taskId, runId: dto.runId, commitSha: run.commitSha }, dto.source); + const hash = createHash('sha256').update(bytes).digest('hex'); + let dir: FileHandle | undefined, file: FileHandle | undefined; + try { + dir = await this.artifactDir(); + file = await open(`/proc/self/fd/${dir.fd}/${hash}.json`, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600); + await file.writeFile(bytes, 'utf8'); await file.sync(); + } catch { throw new ConflictException('Artifact creation refused'); } + finally { await file?.close(); await dir?.close(); } + // This fixture path deliberately does not call core complete: its QA attestation and + // measured-cost reconciliation would misrepresent generated toy-source evidence. + return this.db.$transaction(async (tx: any) => { + const switches = await tx.$queryRaw`SELECT level, active FROM orchestration_kill_switches WHERE scope = 'GLOBAL' FOR SHARE`; + if (switches[0]?.active && switches[0].level !== 'FREEZE_NEW_WORK') throw new ForbiddenException('Kill switch blocks workflow'); + await tx.$queryRaw`SELECT id FROM orchestration_tasks WHERE id = ${dto.taskId}::uuid FOR UPDATE`; + const t = await tx.orchestrationTask.findUnique({ where: { id: dto.taskId } }); + const o = await tx.prepareWorkflowOutbox.findUnique({ where: { taskId: dto.taskId } }); + const r = await tx.orchestrationRun.findUnique({ where: { id: dto.runId } }); + if (!admittedTechTask(t) || 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.commitSha !== t.metadata.commitSha || r.commitSha !== run.commitSha || r.environment !== 'staging') + throw new ForbiddenException('Active owned Tech lease required'); + const reservation = await tx.orchestrationBudgetLedger.findUnique({ where: { id: o.reservationId } }); + if (!reservation || reservation.taskId !== t.id || reservation.kind !== 'RESERVE' || reservation.amountCents !== 20 || await tx.orchestrationBudgetLedger.findFirst({ where: { taskId: t.id, kind: 'RECONCILE', metadata: { path: ['reservationId'], equals: reservation.id } } })) + throw new ForbiddenException('Unreconciled 20 cent reservation required'); + await tx.orchestrationArtifact.create({ data: { runId: r.id, kind: 'TECH_PILOT_SOURCE', sha256: hash, uri: this.config.artifactBaseUrl + '/' + hash } }); + await tx.orchestrationAttestation.create({ data: { runId: r.id, kind: 'TECH_PILOT_FIXTURE_QA', subjectHash: hash, signer: 'server-bounded-minmax-v1', predicate: { ...qa, applicationQa: false, costStatus: 'upper-bound-reserved' } } }); + await tx.orchestrationRun.update({ where: { id: r.id }, data: { status: 'SUCCEEDED', 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: 'TECH_PILOT_FIXTURE_VALIDATED', fromState: t.state, toState: 'VERIFYING', idempotencyKey: `tech-pilot:${r.id}`, payload: { artifactSha256: hash, ...qa, applicationQa: false, costStatus: 'upper-bound-reserved', retainedReservationCents: 20 } } }); + return { taskId: t.id, runId: r.id, state: updated.state, artifactSha256: hash, costStatus: 'upper-bound-reserved' }; + }); + } async artifact(a: WorkflowIdentity, hash: string) { if (!/^[a-f0-9]{64}$/.test(hash)) throw new BadRequestException("Invalid artifact hash"); const registered = await this.db.orchestrationArtifact.findFirst({ where: { - sha256: hash, kind: "QA", uri: this.config.artifactBaseUrl + "/" + hash, + sha256: hash, kind: { in: ["QA", "TECH_PILOT_SOURCE"] }, uri: this.config.artifactBaseUrl + "/" + hash, run: { environment: "staging", status: "SUCCEEDED", task: { operationMode: "PREPARE", budgetLimitCents: 20, } }, @@ -217,6 +272,9 @@ class WorkerTransportService { const metadata = task.metadata as any; if (metadata?.workflowScope !== "STAGE_PREPARE" || metadata?.commitSha !== run.commitSha) throw new NotFoundException("Artifact not registered"); + const tech = registered.kind === "TECH_PILOT_SOURCE"; + if (tech && !admittedTechTask(task)) throw new NotFoundException("Artifact not registered"); + const limit = tech ? 8192 : 2048; let bytes: string; let dir: FileHandle | undefined, file: FileHandle | undefined; try { @@ -225,19 +283,22 @@ class WorkerTransportService { constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); const stat = await file.stat(); if (!stat.isFile() || stat.nlink !== 1 || stat.uid !== process.getuid!() || - (stat.mode & 0o077) !== 0 || stat.size > 2048) + (stat.mode & 0o077) !== 0 || stat.size > limit) throw new Error("Unsafe artifact file"); // Fixed-size descriptor read: concurrent growth cannot allocate unbounded memory. - const buffer = Buffer.alloc(2049); + const buffer = Buffer.alloc(limit + 1); const { bytesRead } = await file.read(buffer, 0, buffer.length, 0); - if (bytesRead > 2048) throw new Error("Oversized artifact"); + if (bytesRead > limit) throw new Error("Oversized artifact"); bytes = buffer.subarray(0, bytesRead).toString("utf8"); } catch { throw new NotFoundException("Artifact unavailable"); } finally { await file?.close(); await dir?.close(); } if (createHash("sha256").update(bytes).digest("hex") !== hash) throw new ConflictException("Artifact integrity mismatch"); - if (bytes !== tracerReport({ taskId: run.taskId, runId: run.id, commitSha: run.commitSha })) + let expected: string; + try { expected = tech ? techArtifact({ taskId: run.taskId, runId: run.id, commitSha: run.commitSha }, JSON.parse(bytes).source) : tracerReport({ taskId: run.taskId, runId: run.id, commitSha: run.commitSha }); } + catch { throw new ConflictException("Artifact binding mismatch"); } + if (bytes !== expected) throw new ConflictException("Artifact binding mismatch"); return JSON.parse(bytes); } @@ -259,6 +320,12 @@ class WorkerTransportController { @Post("claim") claim(@Req() req: any, @Body() dto: ClaimDto) { return this.service.claim(req.workerIdentity, dto); } + @Post("tech-claim") techClaim(@Req() req: any, @Body() dto: ClaimDto) { + return this.service.techClaim(req.workerIdentity, dto); + } + @Post("tech-complete") techComplete(@Req() req: any, @Body() dto: TechCompleteDto) { + return this.service.techComplete(req.workerIdentity, dto); + } @Post("heartbeat") heartbeat(@Req() req: any, @Body() dto: LeaseDto) { return this.service.heartbeat(req.workerIdentity, dto); }