Compare commits
49
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dc34c57a0c | ||
|
|
c97ea01ba9 | ||
|
|
00ba4360cf | ||
|
|
8669d183d5 | ||
|
|
66b9d75524 | ||
|
|
d3a0a61d79 | ||
|
|
0ad39bb92c | ||
|
|
0e952ef701 | ||
|
|
f1382e943c | ||
|
|
53fccc2d3c | ||
|
|
9ae839138c | ||
|
|
0fa6abc53d | ||
|
|
2a32f31097 | ||
|
|
52a7aebb51 | ||
|
|
af32ec14e1 | ||
|
|
e34156b2d3 | ||
|
|
721118787a | ||
|
|
292ed7f920 | ||
|
|
432f6a21c7 | ||
|
|
2087001ecd | ||
|
|
ec93af0064 | ||
|
|
effe30ee58 | ||
|
|
99f0c0d626 | ||
|
|
1e27387016 | ||
|
|
c733a806bd | ||
|
|
23cbd693a8 | ||
|
|
366bc9fc88 | ||
|
|
b4420e54b7 | ||
|
|
44d74d35e6 | ||
|
|
5897abcbad | ||
|
|
b3b9e617b5 | ||
|
|
6f9529d31f | ||
|
|
c25b10f7f3 | ||
|
|
c69a779ecc | ||
|
|
688a0e4de5 | ||
|
|
cf9b80bbb3 | ||
|
|
2a341d3969 | ||
|
|
9275233d09 | ||
|
|
13dceaa460 | ||
|
|
0ab2980ff4 | ||
|
|
fd14c05f3a | ||
|
|
cc0358bb12 | ||
|
|
f94ef580fb | ||
|
|
1bb6274b35 | ||
|
|
448c250944 | ||
|
|
5f5c3eb3b9 | ||
|
|
3805057923 | ||
|
|
f4678c48ca | ||
|
|
c40e931168 |
No files matched your search
@@ -12,3 +12,7 @@ uploads
|
||||
**/uploads
|
||||
*.pdf
|
||||
graphify-out
|
||||
**/.revalidation.env*
|
||||
**/.support*.env*
|
||||
**/*runtime.env
|
||||
**/release.override.json
|
||||
+33
-18
@@ -5,30 +5,45 @@ on:
|
||||
branches:
|
||||
- main
|
||||
- develop
|
||||
- release/blog-timestamp-limited-20261030
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
runs-on: canina
|
||||
steps:
|
||||
- name: Authenticated host Stage deployment (Production blocked)
|
||||
shell: bash
|
||||
- name: Run deploy.sh from within the scripts folder
|
||||
env:
|
||||
BRANCH: ${{ github.ref_name }}
|
||||
DEPLOY_REF: ${{ github.ref }}
|
||||
DEPLOY_SHA: ${{ github.sha }}
|
||||
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
|
||||
DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
|
||||
GITHUB_ACTIONS: 'true'
|
||||
run: |
|
||||
set -euo pipefail
|
||||
set +x
|
||||
[[ "${BRANCH}" == develop ]] || { echo 'Production blocked' >&2; exit 2; }
|
||||
[[ "${DEPLOY_SHA}" =~ ^[0-9a-f]{40}$ ]] || exit 2
|
||||
# Provision DEPLOY_KNOWN_HOSTS from an independently verified host key.
|
||||
# Never use baked-in /opt/services, runner SSH keys or docker.sock.
|
||||
[[ -n "${DEPLOY_KEY}" && -n "${DEPLOY_KNOWN_HOSTS}" ]] || { echo 'Host boundary requires DEPLOY_KEY and verified DEPLOY_KNOWN_HOSTS' >&2; exit 2; }
|
||||
export http_proxy="" https_proxy="" HTTP_PROXY="" HTTPS_PROXY=""
|
||||
set -eu
|
||||
case "${DEPLOY_REF}" in
|
||||
refs/heads/main) BRANCH=main; STAGE_SOURCE_REF= ;;
|
||||
refs/heads/develop|refs/heads/release/blog-timestamp-limited-20261030)
|
||||
BRANCH=develop; STAGE_SOURCE_REF="$DEPLOY_REF" ;;
|
||||
*) printf 'Unsupported deployment ref\n' >&2; exit 2 ;;
|
||||
esac
|
||||
# Bind downloads and both release dispatchers to the exact event commit.
|
||||
case "$DEPLOY_SHA" in
|
||||
*[!0-9a-f]*|'') exit 2 ;;
|
||||
esac
|
||||
[ "${#DEPLOY_SHA}" -eq 40 ] || exit 2
|
||||
export BRANCH STAGE_SOURCE_REF
|
||||
# 1. رفع مشکل DNS و غیرفعال کردن پروکسی برای دامنه محلی
|
||||
echo "87.248.133.138 git.parsaaghayi.ir" >> /etc/hosts
|
||||
export http_proxy=""
|
||||
export https_proxy=""
|
||||
export HTTP_PROXY=""
|
||||
export HTTPS_PROXY=""
|
||||
export no_proxy="git.parsaaghayi.ir,localhost,127.0.0.1,87.248.133.138"
|
||||
umask 077
|
||||
wrapper=$(mktemp)
|
||||
trap 'rm -f "$wrapper"' EXIT
|
||||
wget --no-proxy -qO "$wrapper" "https://git.parsaaghayi.ir/parsa/canina/raw/commit/${DEPLOY_SHA}/scripts/ci-host-deploy.sh"
|
||||
bash "$wrapper"
|
||||
|
||||
# 2. دانلود و اجرای اسکریپت deploy.sh بر اساس برنچ جاری
|
||||
mkdir -p scripts
|
||||
wget --no-proxy -qO scripts/deploy.sh https://git.parsaaghayi.ir/parsa/canina/raw/commit/${DEPLOY_SHA}/scripts/deploy.sh
|
||||
wget --no-proxy -qO scripts/release-cache-stage.sh https://git.parsaaghayi.ir/parsa/canina/raw/commit/${DEPLOY_SHA}/scripts/release-cache-stage.sh
|
||||
|
||||
wget --no-proxy -qO scripts/release-cache-prod.sh https://git.parsaaghayi.ir/parsa/canina/raw/commit/${DEPLOY_SHA}/scripts/release-cache-prod.sh
|
||||
chmod +x scripts/deploy.sh
|
||||
cd scripts
|
||||
./deploy.sh "${BRANCH}"
|
||||
@@ -1,29 +0,0 @@
|
||||
name: Hermes runtime contract review (NO DEPLOY)
|
||||
on:
|
||||
workflow_dispatch:
|
||||
permissions:
|
||||
contents: read
|
||||
jobs:
|
||||
contract-review:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.12'
|
||||
- name: Editorial Python protocol and deadline regressions (no inference)
|
||||
run: PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s infra/hermes-editorial/tests -v
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '22'
|
||||
cache: npm
|
||||
cache-dependency-path: backend/package-lock.json
|
||||
- run: npm ci --ignore-scripts --no-audit
|
||||
working-directory: backend
|
||||
- run: npm test -- --runInBand hermes-management
|
||||
working-directory: backend
|
||||
- run: npx tsc -p src/hermes-management/tsconfig.contract.json --noEmit
|
||||
working-directory: backend
|
||||
- run: npx tsc -p src/hermes-management/tsconfig.contract.json
|
||||
working-directory: backend
|
||||
# No SSH, Docker socket, secrets, deployments, dispatch or runtime mutation.
|
||||
@@ -1,41 +0,0 @@
|
||||
name: Owner authorized Stage test principal
|
||||
on:
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
approval:
|
||||
required: true
|
||||
description: "Exact owner grant: owner-authorized-stage-editorial"
|
||||
session:
|
||||
required: true
|
||||
description: "Unique session (8-64 lowercase letters/digits/hyphens)"
|
||||
ttl:
|
||||
required: true
|
||||
description: "Bounded session TTL, 60-3600 seconds"
|
||||
action:
|
||||
required: true
|
||||
description: "open or close"
|
||||
jobs:
|
||||
principal:
|
||||
if: github.ref == 'refs/heads/develop'
|
||||
runs-on: canina
|
||||
steps:
|
||||
- name: Explicit authenticated Stage CI seed authority
|
||||
shell: bash
|
||||
env:
|
||||
BRANCH: ${{ github.ref_name }}
|
||||
DEPLOY_SHA: ${{ github.sha }}
|
||||
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
|
||||
DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
|
||||
STAGE_TEST_APPROVAL: ${{ inputs.approval }}
|
||||
STAGE_TEST_SESSION: ${{ inputs.session }}
|
||||
STAGE_TEST_TTL_SECONDS: ${{ inputs.ttl }}
|
||||
STAGE_TEST_ACTION: ${{ inputs.action }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
set +x
|
||||
[[ "$DEPLOY_SHA" =~ ^[a-f0-9]{40}$ ]] || exit 2
|
||||
umask 077
|
||||
script=$(mktemp)
|
||||
trap 'rm -f "$script"' EXIT
|
||||
wget --no-proxy -qO "$script" "https://git.parsaaghayi.ir/parsa/canina/raw/commit/$DEPLOY_SHA/scripts/ci-stage-test-principal.sh"
|
||||
bash "$script"
|
||||
@@ -16,3 +16,7 @@ playwright-report/
|
||||
playwright/.auth/
|
||||
blob-report/
|
||||
|
||||
|
||||
# Python CI helpers must not dirty immutable release checkouts.
|
||||
__pycache__/
|
||||
*.pyc
|
||||
+7
-9
@@ -1,4 +1,4 @@
|
||||
FROM node:20-alpine AS app-builder
|
||||
FROM node:22-alpine AS app-builder
|
||||
USER root
|
||||
WORKDIR /app
|
||||
ENV NEXT_TELEMETRY_DISABLED=1
|
||||
@@ -7,18 +7,17 @@ ENV NEXT_PUBLIC_API_URL=https://api.canina.ir/api
|
||||
# Optional: Use Nexus as npm registry
|
||||
ARG NPM_REGISTRY=
|
||||
RUN if [ -n "$NPM_REGISTRY" ]; then \
|
||||
npm config set registry "$NPM_REGISTRY" && \
|
||||
npm config set strict-ssl false; \
|
||||
npm config set registry "$NPM_REGISTRY"; \
|
||||
fi
|
||||
|
||||
COPY frontend/application/package*.json ./
|
||||
RUN npm ci --include=dev --prefer-offline --no-audit
|
||||
RUN npm ci --include=dev --prefer-offline --no-audit && test -x node_modules/.bin/next
|
||||
COPY frontend/application ./
|
||||
ARG NEXT_PUBLIC_API_URL
|
||||
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
|
||||
RUN npm run build
|
||||
|
||||
FROM node:20-alpine AS admin-builder
|
||||
FROM node:22-alpine AS admin-builder
|
||||
USER root
|
||||
WORKDIR /app
|
||||
ENV VITE_API_URL=https://api.canina.ir/api
|
||||
@@ -26,18 +25,17 @@ ENV VITE_API_URL=https://api.canina.ir/api
|
||||
# Optional: Use Nexus as npm registry (inherits build-arg)
|
||||
ARG NPM_REGISTRY=
|
||||
RUN if [ -n "$NPM_REGISTRY" ]; then \
|
||||
npm config set registry "$NPM_REGISTRY" && \
|
||||
npm config set strict-ssl false; \
|
||||
npm config set registry "$NPM_REGISTRY"; \
|
||||
fi
|
||||
|
||||
COPY frontend/admin-panel/package*.json ./
|
||||
RUN npm ci --include=dev --prefer-offline --no-audit
|
||||
RUN npm ci --include=dev --prefer-offline --no-audit && test -x node_modules/.bin/vite
|
||||
COPY frontend/admin-panel ./
|
||||
ARG VITE_API_URL
|
||||
ENV VITE_API_URL=$VITE_API_URL
|
||||
RUN npm run build
|
||||
|
||||
FROM node:20-alpine
|
||||
FROM node:22-alpine
|
||||
USER root
|
||||
RUN addgroup -S nginx 2>/dev/null || true && adduser -S nginx -G nginx 2>/dev/null || true
|
||||
RUN mkdir -p /var/log/nginx /var/cache/nginx /run/nginx /etc/nginx/conf.d /etc/nginx/http.d
|
||||
|
||||
@@ -12,7 +12,6 @@ ENV npm_config_cache=/tmp/npm-cache
|
||||
ARG NPM_REGISTRY=
|
||||
RUN if [ -n "$NPM_REGISTRY" ]; then \
|
||||
npm config set registry "$NPM_REGISTRY" && \
|
||||
npm config set strict-ssl false && \
|
||||
echo "Using npm registry: $NPM_REGISTRY"; \
|
||||
fi
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
ALTER TABLE "users" ADD COLUMN "auth_expires_at" TIMESTAMPTZ, ADD COLUMN "stage_test_session" VARCHAR(64), ADD COLUMN "stage_test_generation" UUID;
|
||||
ALTER TABLE "users" ADD COLUMN IF NOT EXISTS "auth_expires_at" TIMESTAMPTZ, ADD COLUMN IF NOT EXISTS "stage_test_session" VARCHAR(64), ADD COLUMN IF NOT EXISTS "stage_test_generation" UUID;
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
-- Drop legacy governance and orchestration tables
|
||||
DROP TABLE IF EXISTS "prepare_workflow_outbox" CASCADE;
|
||||
DROP TABLE IF EXISTS "orchestration_kill_switches" CASCADE;
|
||||
DROP TABLE IF EXISTS "orchestration_budget_ledger" CASCADE;
|
||||
DROP TABLE IF EXISTS "orchestration_events" CASCADE;
|
||||
DROP TABLE IF EXISTS "orchestration_approval_decisions" CASCADE;
|
||||
DROP TABLE IF EXISTS "orchestration_dependencies" CASCADE;
|
||||
DROP TABLE IF EXISTS "orchestration_attestations" CASCADE;
|
||||
DROP TABLE IF EXISTS "orchestration_artifacts" CASCADE;
|
||||
DROP TABLE IF EXISTS "orchestration_runs" CASCADE;
|
||||
DROP TABLE IF EXISTS "orchestration_tasks" CASCADE;
|
||||
DROP TABLE IF EXISTS "department_tasks" CASCADE;
|
||||
DROP TABLE IF EXISTS "audit_logs" CASCADE;
|
||||
DROP TABLE IF EXISTS "approval_requests" CASCADE;
|
||||
@@ -1020,237 +1020,3 @@ model SmsCampaign {
|
||||
@@map("sms_campaigns")
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// 5. ApprovalRequest (CEO Governance & Decision Gate)
|
||||
// ------------------------------------------------------------------------------
|
||||
model ApprovalRequest {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
department String @db.VarChar(50)
|
||||
category String @db.VarChar(50)
|
||||
title String @db.VarChar(255)
|
||||
payload Json @db.JsonB
|
||||
testContext Json? @map("test_context") @db.JsonB
|
||||
status String @default("PENDING") @db.VarChar(30)
|
||||
ceoNotes String? @map("ceo_notes") @db.Text
|
||||
executedAt DateTime? @map("executed_at") @db.Timestamptz()
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
|
||||
updatedAt DateTime @default(now()) @updatedAt @map("updated_at") @db.Timestamptz()
|
||||
|
||||
@@index([department])
|
||||
@@index([category])
|
||||
@@index([status])
|
||||
@@index([createdAt])
|
||||
@@map("approval_requests")
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// 6. AuditLog (System & Database Changes History)
|
||||
// ------------------------------------------------------------------------------
|
||||
model AuditLog {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
entity String @db.VarChar(50) // e.g. product, blog, order, approval, settings
|
||||
entityId String? @map("entity_id") @db.VarChar(100)
|
||||
action String @db.VarChar(50) // CREATE, UPDATE, DELETE, EXECUTE, APPROVE, REJECT
|
||||
department String? @db.VarChar(50) // devops, seo, content, vet, commerce, pmo
|
||||
actor String @default("SYSTEM") @db.VarChar(100)
|
||||
summary String @db.VarChar(255)
|
||||
oldValues Json? @map("old_values") @db.JsonB
|
||||
newValues Json? @map("new_values") @db.JsonB
|
||||
metadata Json? @db.JsonB
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
|
||||
|
||||
@@index([entity])
|
||||
@@index([action])
|
||||
@@index([department])
|
||||
@@index([createdAt])
|
||||
@@map("audit_logs")
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// 7. DepartmentTask (Autonomous Corporate Task & Kanban Manager)
|
||||
// ------------------------------------------------------------------------------
|
||||
model DepartmentTask {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
code String @unique @db.VarChar(50)
|
||||
department String @db.VarChar(50) // devops, seo, content, vet, commerce, pmo
|
||||
title String @db.VarChar(255)
|
||||
description String? @db.Text
|
||||
status String @default("BACKLOG") @db.VarChar(30) // BACKLOG, IN_PROGRESS, IN_REVIEW, APPROVED, DEPLOYING, DONE
|
||||
priority String @default("MEDIUM") @db.VarChar(20) // LOW, MEDIUM, HIGH, CRITICAL
|
||||
assignee String? @db.VarChar(100)
|
||||
acceptanceCriteria Json? @map("acceptance_criteria") @db.JsonB
|
||||
executionLogs Json? @map("execution_logs") @db.JsonB
|
||||
deliverableUrl String? @map("deliverable_url") @db.VarChar(255)
|
||||
approvalId String? @map("approval_id") @db.Uuid
|
||||
startedAt DateTime? @map("started_at") @db.Timestamptz()
|
||||
completedAt DateTime? @map("completed_at") @db.Timestamptz()
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
|
||||
updatedAt DateTime @default(now()) @updatedAt @map("updated_at") @db.Timestamptz()
|
||||
|
||||
@@index([department])
|
||||
@@index([status])
|
||||
@@index([priority])
|
||||
@@index([createdAt])
|
||||
@@map("department_tasks")
|
||||
}
|
||||
|
||||
// Orchestration v4 additive models. Legacy governance tables remain untouched.
|
||||
model OrchestrationTask {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
key String @unique @db.VarChar(120)
|
||||
title String @db.VarChar(255)
|
||||
department String @db.VarChar(50)
|
||||
state String @default("DRAFT") @db.VarChar(40)
|
||||
operationMode String @map("operation_mode") @db.VarChar(30)
|
||||
modelTier String? @map("model_tier") @db.VarChar(2)
|
||||
budgetLimitCents Int @map("budget_limit_cents")
|
||||
version Int @default(1)
|
||||
metadata Json @default("{}") @db.JsonB
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
|
||||
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz()
|
||||
runs OrchestrationRun[]
|
||||
dependencies OrchestrationDependency[] @relation("task_dependencies")
|
||||
dependedOnBy OrchestrationDependency[] @relation("task_dependents")
|
||||
decisions OrchestrationApprovalDecision[]
|
||||
events OrchestrationEvent[]
|
||||
budgetLedger OrchestrationBudgetLedger[]
|
||||
@@index([state, createdAt])
|
||||
@@index([department, state])
|
||||
@@map("orchestration_tasks")
|
||||
}
|
||||
model OrchestrationRun {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
taskId String @map("task_id") @db.Uuid
|
||||
attempt Int @default(1)
|
||||
status String @default("PENDING") @db.VarChar(30)
|
||||
commitSha String @map("commit_sha") @db.VarChar(64)
|
||||
environment String @db.VarChar(50)
|
||||
retryClass String? @map("retry_class") @db.VarChar(30)
|
||||
idempotencyKey String @unique @map("idempotency_key") @db.VarChar(150)
|
||||
startedAt DateTime? @map("started_at") @db.Timestamptz()
|
||||
finishedAt DateTime? @map("finished_at") @db.Timestamptz()
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
|
||||
task OrchestrationTask @relation(fields:[taskId],references:[id],onDelete:Restrict)
|
||||
artifacts OrchestrationArtifact[]
|
||||
attestations OrchestrationAttestation[]
|
||||
approvals OrchestrationApprovalDecision[]
|
||||
@@unique([taskId, attempt])
|
||||
@@index([taskId, status])
|
||||
@@map("orchestration_runs")
|
||||
}
|
||||
model OrchestrationArtifact {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
runId String @map("run_id") @db.Uuid
|
||||
kind String @db.VarChar(50)
|
||||
uri String @db.Text
|
||||
sha256 String @db.Char(64)
|
||||
sizeBytes BigInt? @map("size_bytes")
|
||||
metadata Json @default("{}") @db.JsonB
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
|
||||
run OrchestrationRun @relation(fields:[runId],references:[id],onDelete:Restrict)
|
||||
@@unique([runId, sha256])
|
||||
@@map("orchestration_artifacts")
|
||||
}
|
||||
model OrchestrationAttestation {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
runId String @map("run_id") @db.Uuid
|
||||
kind String @db.VarChar(50)
|
||||
subjectHash String @map("subject_hash") @db.Char(64)
|
||||
predicate Json @db.JsonB
|
||||
signer String @db.VarChar(150)
|
||||
signature String? @db.Text
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
|
||||
run OrchestrationRun @relation(fields:[runId],references:[id],onDelete:Restrict)
|
||||
@@unique([runId, kind, subjectHash, signer])
|
||||
@@map("orchestration_attestations")
|
||||
}
|
||||
model OrchestrationDependency {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
taskId String @map("task_id") @db.Uuid
|
||||
dependsOnTaskId String @map("depends_on_task_id") @db.Uuid
|
||||
kind String @default("BLOCKS") @db.VarChar(30)
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
|
||||
task OrchestrationTask @relation("task_dependencies",fields:[taskId],references:[id],onDelete:Restrict)
|
||||
dependsOnTask OrchestrationTask @relation("task_dependents",fields:[dependsOnTaskId],references:[id],onDelete:Restrict)
|
||||
@@unique([taskId, dependsOnTaskId])
|
||||
@@map("orchestration_dependencies")
|
||||
}
|
||||
model OrchestrationApprovalDecision {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
taskId String @map("task_id") @db.Uuid
|
||||
gate Int
|
||||
decision String @db.VarChar(20)
|
||||
actorId String @map("actor_id") @db.VarChar(100)
|
||||
notes String? @db.Text
|
||||
expiresAt DateTime @map("expires_at") @db.Timestamptz()
|
||||
idempotencyKey String @unique @map("idempotency_key") @db.VarChar(150)
|
||||
runId String? @map("run_id") @db.Uuid
|
||||
artifactHash String? @map("artifact_hash") @db.Char(64)
|
||||
commitSha String? @map("commit_sha") @db.VarChar(64)
|
||||
environment String? @db.VarChar(50)
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
|
||||
task OrchestrationTask @relation(fields:[taskId],references:[id],onDelete:Restrict)
|
||||
run OrchestrationRun? @relation(fields:[runId],references:[id],onDelete:Restrict)
|
||||
@@index([taskId, gate, createdAt])
|
||||
@@map("orchestration_approval_decisions")
|
||||
}
|
||||
model OrchestrationEvent {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
taskId String? @map("task_id") @db.Uuid
|
||||
eventType String @map("event_type") @db.VarChar(60)
|
||||
fromState String? @map("from_state") @db.VarChar(40)
|
||||
toState String? @map("to_state") @db.VarChar(40)
|
||||
sequence Int
|
||||
actorId String @map("actor_id") @db.VarChar(100)
|
||||
idempotencyKey String? @unique @map("idempotency_key") @db.VarChar(150)
|
||||
payload Json @default("{}") @db.JsonB
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
|
||||
task OrchestrationTask? @relation(fields:[taskId],references:[id],onDelete:Restrict)
|
||||
@@unique([taskId, sequence])
|
||||
@@index([eventType, createdAt])
|
||||
@@map("orchestration_events")
|
||||
}
|
||||
model OrchestrationBudgetLedger {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
taskId String @map("task_id") @db.Uuid
|
||||
amountCents Int @map("amount_cents")
|
||||
kind String @db.VarChar(40)
|
||||
idempotencyKey String @unique @map("idempotency_key") @db.VarChar(150)
|
||||
actorId String @map("actor_id") @db.VarChar(100)
|
||||
metadata Json @default("{}") @db.JsonB
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
|
||||
task OrchestrationTask @relation(fields:[taskId],references:[id],onDelete:Restrict)
|
||||
@@index([taskId, createdAt])
|
||||
@@map("orchestration_budget_ledger")
|
||||
}
|
||||
model OrchestrationKillSwitch {
|
||||
scope String @id @db.VarChar(80)
|
||||
level String @db.VarChar(30)
|
||||
active Boolean @default(true)
|
||||
reason String @db.Text
|
||||
actorId String @map("actor_id") @db.VarChar(100)
|
||||
version Int @default(1)
|
||||
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz()
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
|
||||
@@map("orchestration_kill_switches")
|
||||
}
|
||||
|
||||
// Transactional dispatch intent. Lease tokens are server-only fencing credentials.
|
||||
model PrepareWorkflowOutbox {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
taskId String @unique @map("task_id") @db.Uuid
|
||||
status String @default("PENDING") @db.VarChar(20)
|
||||
attempts Int @default(0)
|
||||
notBefore DateTime? @map("not_before") @db.Timestamptz()
|
||||
leaseOwner String? @map("lease_owner") @db.VarChar(100)
|
||||
leaseToken String? @map("lease_token") @db.Uuid
|
||||
leaseUntil DateTime? @map("lease_until") @db.Timestamptz()
|
||||
heartbeatAt DateTime? @map("heartbeat_at") @db.Timestamptz()
|
||||
runId String? @map("run_id") @db.Uuid
|
||||
reservationId String? @map("reservation_id") @db.Uuid
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
|
||||
@@index([status, leaseUntil])
|
||||
@@index([status, notBefore, createdAt], map: "prepare_workflow_outbox_due_idx")
|
||||
@@map("prepare_workflow_outbox")
|
||||
}
|
||||
@@ -21,9 +21,6 @@ import { CompetitorPricingController } from "./competitor-pricing.controller";
|
||||
import { B2BLeadsController } from "./b2b-leads.controller";
|
||||
import { PricingSuggestionsController } from "./pricing-suggestions.controller";
|
||||
import { SmsCampaignsController } from "./sms-campaigns.controller";
|
||||
import { ApprovalsController } from "./approvals.controller";
|
||||
import { AuditLogsController } from "./audit-logs.controller";
|
||||
import { DepartmentTasksController } from "./department-tasks.controller";
|
||||
import { PrismaModule } from "../prisma/prisma.module";
|
||||
import { RedisModule } from "../redis/redis.module";
|
||||
import { SettingsModule } from "../settings/settings.module";
|
||||
@@ -46,9 +43,6 @@ import { SupportChatAnalyticsService } from "./support-chat-analytics.service";
|
||||
B2BLeadsController,
|
||||
PricingSuggestionsController,
|
||||
SmsCampaignsController,
|
||||
ApprovalsController,
|
||||
AuditLogsController,
|
||||
DepartmentTasksController,
|
||||
SupportChatAnalyticsController,
|
||||
],
|
||||
providers: [
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
import { GoneException } from '@nestjs/common';
|
||||
import { ApprovalsController } from './approvals.controller';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
describe('ApprovalsController retired mutation boundary',()=>{
|
||||
const access=jest.fn();
|
||||
const prisma=new Proxy({},{get:(_target,key)=>{access(key);throw new Error('Database access forbidden');}});
|
||||
const controller=new ApprovalsController(prisma as PrismaService);
|
||||
beforeEach(()=>access.mockClear());
|
||||
it('create {department:SEO,category:SEO,title:Legacy,payload:{}} returns 410 without reading or writing persistence',async()=>{
|
||||
await expect(controller.create({department:'SEO',category:'SEO',title:'Legacy',payload:{}})).rejects.toBeInstanceOf(GoneException);
|
||||
await expect(controller.create({department:'SEO',category:'SEO',title:'Legacy',payload:{}})).rejects.toMatchObject({status:410});
|
||||
expect(access).not.toHaveBeenCalled();
|
||||
});
|
||||
it('decide missing, {decision:APPROVED} returns 410 without reading or writing persistence',async()=>{
|
||||
await expect(controller.decide('missing', {decision:'APPROVED'})).rejects.toBeInstanceOf(GoneException);
|
||||
await expect(controller.decide('missing', {decision:'APPROVED'})).rejects.toMatchObject({status:410});
|
||||
expect(access).not.toHaveBeenCalled();
|
||||
});
|
||||
it('decide missing, {decision:REJECTED} returns 410 without reading or writing persistence',async()=>{
|
||||
await expect(controller.decide('missing', {decision:'REJECTED'})).rejects.toBeInstanceOf(GoneException);
|
||||
await expect(controller.decide('missing', {decision:'REJECTED'})).rejects.toMatchObject({status:410});
|
||||
expect(access).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -1,134 +0,0 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Put,
|
||||
Body,
|
||||
Param,
|
||||
Query,
|
||||
UseGuards,
|
||||
NotFoundException,
|
||||
GoneException,
|
||||
} from "@nestjs/common";
|
||||
import { ApiTags, ApiBearerAuth, ApiOperation } from "@nestjs/swagger";
|
||||
import { JwtAuthGuard } from "../auth/jwt-auth.guard";
|
||||
import { RolesGuard } from "../common/guards/roles.guard";
|
||||
import { Roles } from "../common/decorators/roles.decorator";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import {
|
||||
CreateApprovalRequestDto,
|
||||
DecideApprovalDto,
|
||||
QueryApprovalDto,
|
||||
} from "./dto/approval-request.dto";
|
||||
|
||||
@ApiTags("Admin - CEO Approvals & Governance")
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles("Admin", "SUPER_ADMIN")
|
||||
@Controller("admin/approvals")
|
||||
export class ApprovalsController {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: "لیست موارد کارتابل تاییدات مدیریت" })
|
||||
async findAll(@Query() query: QueryApprovalDto) {
|
||||
const page = Number(query.page) || 1;
|
||||
const limit = Number(query.limit) || 100;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const where: any = {};
|
||||
if (query.department) where.department = query.department;
|
||||
if (query.category) where.category = query.category;
|
||||
if (query.status) where.status = query.status;
|
||||
|
||||
const [total, items, pendingCount] = await Promise.all([
|
||||
(this.prisma as any).approvalRequest.count({ where }),
|
||||
(this.prisma as any).approvalRequest.findMany({
|
||||
where,
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { createdAt: "desc" },
|
||||
}),
|
||||
(this.prisma as any).approvalRequest.count({
|
||||
where: { status: "PENDING" },
|
||||
}),
|
||||
]);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: items,
|
||||
meta: {
|
||||
total,
|
||||
page,
|
||||
limit,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
pendingCount,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@Get(":id")
|
||||
@ApiOperation({ summary: "دریافت جزییات یک آیتم کارتابل" })
|
||||
async findOne(@Param("id") id: string) {
|
||||
const item = await (this.prisma as any).approvalRequest.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
if (!item) {
|
||||
throw new NotFoundException(`Approval request ${id} not found`);
|
||||
}
|
||||
return { success: true, data: item };
|
||||
}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: "ثبت آیتم جدید جهت تایید مدیریت (از طرف سیستم یا دپارتمانها)" })
|
||||
async create(@Body() dto: CreateApprovalRequestDto) {
|
||||
throw new GoneException("Legacy orchestration mutations are retired. Orchestration v4 remains read-only; no replacement mutation is enabled.");
|
||||
}
|
||||
|
||||
@Post(":id/test")
|
||||
@ApiOperation({ summary: "اجرای شبیهسازی تست زنده برای آیتم کارتابل" })
|
||||
async runTestSimulation(@Param("id") id: string) {
|
||||
const item = await (this.prisma as any).approvalRequest.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
if (!item) {
|
||||
throw new NotFoundException(`Approval request ${id} not found`);
|
||||
}
|
||||
|
||||
let testResult: any = { status: "SIMULATION_SUCCESS" };
|
||||
if (item.category === "PRICING") {
|
||||
const payload = item.payload as any;
|
||||
testResult = {
|
||||
impact: "Price change simulation",
|
||||
currentPrice: payload.currentPrice,
|
||||
newPrice: payload.newPrice,
|
||||
marginDiff: payload.newPrice && payload.buyPrice
|
||||
? (((payload.newPrice - payload.buyPrice) / payload.newPrice) * 100).toFixed(1) + "%"
|
||||
: "N/A",
|
||||
torobFeedReflectEstimate: "< 5 seconds",
|
||||
};
|
||||
} else if (item.category === "PURCHASE_ORDER") {
|
||||
const payload = item.payload as any;
|
||||
testResult = {
|
||||
document: "PO Pre-check",
|
||||
itemsCount: payload.items ? payload.items.length : 1,
|
||||
totalEstimatedCost: payload.totalCost || "محاسبه بر اساس نرخ خرید",
|
||||
fulfillmentCapacity: "انبار آماده پذیرش",
|
||||
};
|
||||
} else {
|
||||
testResult = {
|
||||
executionSafe: true,
|
||||
summary: "تست سلامت اولیه و اعتبارسنجی مقادیر با موفقیت انجام شد.",
|
||||
};
|
||||
}
|
||||
|
||||
return { success: true, testResult };
|
||||
}
|
||||
|
||||
@Put(":id/decide")
|
||||
@ApiOperation({ summary: "تصمیمگیری مدیرعامل (تایید یا رد همراه با یادداشت)" })
|
||||
async decide(@Param("id") id: string, @Body() dto: DecideApprovalDto) {
|
||||
throw new GoneException("Legacy orchestration mutations are retired. Orchestration v4 remains read-only; no replacement mutation is enabled.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Query,
|
||||
UseGuards,
|
||||
Param,
|
||||
} from "@nestjs/common";
|
||||
import { ApiTags, ApiBearerAuth, ApiOperation } from "@nestjs/swagger";
|
||||
import { JwtAuthGuard } from "../auth/jwt-auth.guard";
|
||||
import { RolesGuard } from "../common/guards/roles.guard";
|
||||
import { Roles } from "../common/decorators/roles.decorator";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
|
||||
@ApiTags("Admin - System Audit & History")
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles("Admin", "SUPER_ADMIN")
|
||||
@Controller("admin/audit-logs")
|
||||
export class AuditLogsController {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: "لیست لاگهای تغییرات دیتابیس و سیستم" })
|
||||
async findAll(@Query() query: any) {
|
||||
const page = Number(query.page) || 1;
|
||||
const limit = Number(query.limit) || 30;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const where: any = {};
|
||||
if (query.entity) where.entity = query.entity;
|
||||
if (query.action) where.action = query.action;
|
||||
if (query.department) where.department = query.department;
|
||||
if (query.search) {
|
||||
where.OR = [
|
||||
{ summary: { contains: query.search, mode: "insensitive" } },
|
||||
{ actor: { contains: query.search, mode: "insensitive" } },
|
||||
{ entityId: { contains: query.search, mode: "insensitive" } },
|
||||
];
|
||||
}
|
||||
|
||||
const [total, items] = await Promise.all([
|
||||
(this.prisma as any).auditLog.count({ where }),
|
||||
(this.prisma as any).auditLog.findMany({
|
||||
where,
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: { createdAt: "desc" },
|
||||
}),
|
||||
]);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: items,
|
||||
meta: {
|
||||
total,
|
||||
page,
|
||||
limit,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@Get("recent")
|
||||
@ApiOperation({ summary: "آخرین رخدادهای سیستم برای ویجت داشبورد" })
|
||||
async getRecent(@Query("limit") limitParam?: string) {
|
||||
const limit = Number(limitParam) || 10;
|
||||
const items = await (this.prisma as any).auditLog.findMany({
|
||||
take: limit,
|
||||
orderBy: { createdAt: "desc" },
|
||||
});
|
||||
return { success: true, data: items };
|
||||
}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: "ثبت لاگ جدید در تاریخچه سیستم" })
|
||||
async create(@Body() body: any) {
|
||||
const item = await (this.prisma as any).auditLog.create({
|
||||
data: {
|
||||
entity: body.entity,
|
||||
entityId: body.entityId ? String(body.entityId) : null,
|
||||
action: body.action || "UPDATE",
|
||||
department: body.department || "system",
|
||||
actor: body.actor || "ADMIN",
|
||||
summary: body.summary,
|
||||
oldValues: body.oldValues || null,
|
||||
newValues: body.newValues || null,
|
||||
metadata: body.metadata || null,
|
||||
},
|
||||
});
|
||||
return { success: true, data: item };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
import { BadRequestException, ConflictException, NotFoundException } from "@nestjs/common";
|
||||
import { BlogsService } from "./blogs.service";
|
||||
|
||||
// Stateful transactional double: deliberately interleave after snapshot read.
|
||||
// Complements the DB integration test; it does not assert PostgreSQL locking.
|
||||
describe("BlogsService atomic optimistic update", () => {
|
||||
const token = "2026-10-24T23:36:08.501Z";
|
||||
let row: any;
|
||||
let service: BlogsService;
|
||||
let prisma: any;
|
||||
let revalidate: jest.Mock;
|
||||
let afterRead: (() => void) | undefined;
|
||||
let failRelations: boolean;
|
||||
beforeEach(() => {
|
||||
row = { id: "b1", slug: "post", title: "Original", updatedAt: new Date(token), tags: ["old"] };
|
||||
afterRead = undefined;
|
||||
failRelations = false;
|
||||
const blog = {
|
||||
findUnique: jest.fn(async () => {
|
||||
const snapshot = { ...row };
|
||||
afterRead?.();
|
||||
return snapshot;
|
||||
}),
|
||||
updateMany: jest.fn(async ({ where, data }) => {
|
||||
if (row.updatedAt >= where.updatedAt.gte && row.updatedAt < where.updatedAt.lt) {
|
||||
row = { ...row, ...data };
|
||||
return { count: 1 };
|
||||
}
|
||||
return { count: 0 };
|
||||
}),
|
||||
update: jest.fn(async ({ data }) => {
|
||||
if (failRelations) throw new Error("relation failure");
|
||||
row = { ...row, ...data };
|
||||
return { ...row };
|
||||
}),
|
||||
};
|
||||
// Serialize transactions like row locks. Rollback restores only transaction writes.
|
||||
let tail = Promise.resolve();
|
||||
prisma = { blog, $transaction: jest.fn((cb) => {
|
||||
const next = tail.then(async () => {
|
||||
const before = { ...row };
|
||||
try { return await cb({ blog }); } catch (error) { row = before; throw error; }
|
||||
});
|
||||
tail = next.catch(() => undefined);
|
||||
return next;
|
||||
}) };
|
||||
revalidate = jest.fn().mockResolvedValue(true);
|
||||
service = new BlogsService(prisma, { revalidateBlog: revalidate } as any);
|
||||
});
|
||||
|
||||
it.each([null, 0, 123, true, {}, [], "", "garbage", "2026-02-30T00:00:00.000Z", "2026-10-24", "2026-10-24T23:36:08.501001Z"])("rejects invalid token %p without mutations", async (value) => {
|
||||
await expect(service.updateBlog("b1", { title: "bad", expectedUpdatedAt: value })).rejects.toBeInstanceOf(ConflictException);
|
||||
expect(prisma.blog.update).not.toHaveBeenCalled();
|
||||
expect(prisma.blog.updateMany).not.toHaveBeenCalled();
|
||||
expect(revalidate).not.toHaveBeenCalled();
|
||||
});
|
||||
it("rejects stale token and leaves all relations untouched", async () => {
|
||||
await expect(service.updateBlog("b1", { expectedUpdatedAt: "2026-10-20T00:00:00.000Z", tagNames: ["new"], categoryId: "c" })).rejects.toBeInstanceOf(ConflictException);
|
||||
expect(row.tags).toEqual(["old"]);
|
||||
expect(prisma.blog.update).not.toHaveBeenCalled();
|
||||
expect(revalidate).not.toHaveBeenCalled();
|
||||
});
|
||||
it.each([token, "2026-10-25T01:36:08.501+02:00"])("accepts current token %s and atomically includes relations", async (expectedUpdatedAt) => {
|
||||
const result = await service.updateBlog("b1", { title: "new", expectedUpdatedAt, authorId: "a", categoryId: "c", tagNames: ["new"] });
|
||||
expect(result.title).toBe("new");
|
||||
expect(prisma.$transaction).toHaveBeenCalledTimes(1);
|
||||
expect(prisma.blog.updateMany).toHaveBeenCalledWith({ where: { id: "b1", updatedAt: { gte: new Date(token), lt: new Date(new Date(token).getTime() + 1) } }, data: { updatedAt: expect.any(Date) } });
|
||||
expect(prisma.blog.update.mock.calls[0][0].data).toMatchObject({ author: { connect: { id: "a" } }, category: { connect: { id: "c" } }, tags: { set: [], connectOrCreate: expect.any(Array) } });
|
||||
expect(prisma.blog.update.mock.calls[0][0].data).not.toHaveProperty("expectedUpdatedAt");
|
||||
expect(result.updatedAt.getTime()).toBeGreaterThan(new Date(token).getTime());
|
||||
expect(revalidate).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
it("detects writer interleaving after read before write", async () => {
|
||||
afterRead = () => { row = { ...row, title: "winner", updatedAt: new Date("2026-10-24T23:36:08.502Z"), tags: ["winner"] }; };
|
||||
await expect(service.updateBlog("b1", { expectedUpdatedAt: token, title: "loser", tagNames: ["loser"] })).rejects.toBeInstanceOf(ConflictException);
|
||||
expect(row.title).toBe("winner");
|
||||
expect(row.tags).toEqual(["winner"]);
|
||||
expect(prisma.blog.update).not.toHaveBeenCalled();
|
||||
expect(revalidate).not.toHaveBeenCalled();
|
||||
});
|
||||
it("permits exactly one of two requests sharing a token", async () => {
|
||||
const results = await Promise.allSettled([service.updateBlog("b1", { expectedUpdatedAt: token, title: "a" }), service.updateBlog("b1", { expectedUpdatedAt: token, title: "b" })]);
|
||||
expect(results.filter(r => r.status === "fulfilled")).toHaveLength(1);
|
||||
const rejected = results.find(r => r.status === "rejected") as PromiseRejectedResult;
|
||||
expect(rejected.reason).toBeInstanceOf(ConflictException);
|
||||
expect(revalidate).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
it("advances token even when clock is within same millisecond", async () => {
|
||||
jest.useFakeTimers().setSystemTime(new Date(token));
|
||||
try {
|
||||
await service.updateBlog("b1", { expectedUpdatedAt: token, title: "first" });
|
||||
await expect(service.updateBlog("b1", { expectedUpdatedAt: token, title: "second" })).rejects.toBeInstanceOf(ConflictException);
|
||||
} finally { jest.useRealTimers(); }
|
||||
});
|
||||
it("rolls back claim on nested relation failure without invalidating cache", async () => {
|
||||
failRelations = true;
|
||||
await expect(service.updateBlog("b1", { expectedUpdatedAt: token, tagNames: ["new"] })).rejects.toThrow("relation failure");
|
||||
expect(row.updatedAt.toISOString()).toBe(token);
|
||||
expect(row.tags).toEqual(["old"]);
|
||||
expect(revalidate).not.toHaveBeenCalled();
|
||||
});
|
||||
it("preserves legacy omission without demanding a token", async () => {
|
||||
await service.updateBlog("b1", { title: "legacy" });
|
||||
expect(row.title).toBe("legacy");
|
||||
expect(prisma.$transaction).not.toHaveBeenCalled();
|
||||
});
|
||||
it("returns 404 for a missing blog", async () => {
|
||||
prisma.blog.findUnique.mockResolvedValue(null);
|
||||
await expect(service.updateBlog("missing", { expectedUpdatedAt: token })).rejects.toBeInstanceOf(NotFoundException);
|
||||
});
|
||||
it("does not fail committed writes if revalidation throws", async () => {
|
||||
revalidate.mockRejectedValue(new Error("cache down"));
|
||||
const result = await service.updateBlog("b1", { expectedUpdatedAt: token, title: "saved" });
|
||||
expect(result.cacheRevalidation).toBe("failed");
|
||||
expect(row.title).toBe("saved");
|
||||
});
|
||||
});
|
||||
@@ -54,6 +54,8 @@ describe("Admin BlogsService", () => {
|
||||
}).compile();
|
||||
|
||||
service = module.get<BlogsService>(BlogsService);
|
||||
mockRevalidationService.revalidateBlog.mockReset();
|
||||
mockRevalidationService.revalidateBlog.mockResolvedValue(true);
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
@@ -200,7 +202,10 @@ describe("Admin BlogsService", () => {
|
||||
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith(
|
||||
"new-health-post",
|
||||
);
|
||||
expect(result).toEqual(createdBlog);
|
||||
expect(result).toEqual({
|
||||
...createdBlog,
|
||||
cacheRevalidation: "success",
|
||||
});
|
||||
});
|
||||
|
||||
it("allowlists create fields and blocks server-owned and unknown fields", async () => {
|
||||
@@ -272,41 +277,65 @@ describe("Admin BlogsService", () => {
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("explicit author contract", () => {
|
||||
beforeEach(() => {
|
||||
mockPrisma.blog.create.mockResolvedValue({ id: "b1", slug: "post" });
|
||||
mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post" });
|
||||
mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" });
|
||||
it("should expose cacheRevalidation status on create result without failing persistence", async () => {
|
||||
const blogData = {
|
||||
title: "Health Post",
|
||||
slug: "health-post",
|
||||
content: "Content",
|
||||
};
|
||||
const createdBlog = { id: "blog-1", ...blogData };
|
||||
mockPrisma.blog.create.mockResolvedValue(createdBlog);
|
||||
mockRevalidationService.revalidateBlog.mockResolvedValue(true);
|
||||
|
||||
const result = await service.createBlog(blogData, "user-admin-1");
|
||||
|
||||
expect(mockPrisma.blog.create).toHaveBeenCalled();
|
||||
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("health-post");
|
||||
expect(result).toEqual({
|
||||
...createdBlog,
|
||||
cacheRevalidation: "success",
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
["omitted", {}, { connect: { id: "current-user" } }],
|
||||
["legacy empty", { authorId: "" }, { connect: { id: "current-user" } }],
|
||||
["selected", { authorId: "selected-user" }, { connect: { id: "selected-user" } }],
|
||||
["authorless", { authorId: null }, undefined],
|
||||
])("creates with %s author without inventing an identity", async (_label, input, expected) => {
|
||||
await service.createBlog({ title: "Post", slug: "post", content: "Content", ...input }, "current-user");
|
||||
const payload = mockPrisma.blog.create.mock.calls[0][0].data;
|
||||
expect(payload.author).toEqual(expected);
|
||||
if (expected === undefined) expect(payload).not.toHaveProperty("author");
|
||||
expect(payload).not.toHaveProperty("authorId");
|
||||
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("post");
|
||||
it("should return cacheRevalidation 'failed' when revalidation fails without failing persistence", async () => {
|
||||
const blogData = {
|
||||
title: "Health Post",
|
||||
slug: "health-post",
|
||||
content: "Content",
|
||||
};
|
||||
const createdBlog = { id: "blog-1", ...blogData };
|
||||
mockPrisma.blog.create.mockResolvedValue(createdBlog);
|
||||
mockRevalidationService.revalidateBlog.mockResolvedValue(false);
|
||||
|
||||
const result = await service.createBlog(blogData, "user-admin-1");
|
||||
|
||||
expect(mockPrisma.blog.create).toHaveBeenCalled();
|
||||
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("health-post");
|
||||
expect(result).toEqual({
|
||||
...createdBlog,
|
||||
cacheRevalidation: "failed",
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
["omitted", {}, undefined],
|
||||
["legacy empty", { authorId: "" }, undefined],
|
||||
["selected", { authorId: "selected-user" }, { connect: { id: "selected-user" } }],
|
||||
["authorless", { authorId: null }, { disconnect: true }],
|
||||
])("updates with %s author while preserving omitted values", async (_label, input, expected) => {
|
||||
await service.updateBlog("b1", input);
|
||||
const payload = mockPrisma.blog.update.mock.calls[0][0].data;
|
||||
expect(payload.author).toEqual(expected);
|
||||
if (expected === undefined) expect(payload).not.toHaveProperty("author");
|
||||
expect(payload).not.toHaveProperty("authorId");
|
||||
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("post");
|
||||
it("should catch unexpected revalidation exceptions on create and return cacheRevalidation 'failed'", async () => {
|
||||
const blogData = {
|
||||
title: "Health Post",
|
||||
slug: "health-post",
|
||||
content: "Content",
|
||||
};
|
||||
const createdBlog = { id: "blog-1", ...blogData };
|
||||
mockPrisma.blog.create.mockResolvedValue(createdBlog);
|
||||
mockRevalidationService.revalidateBlog.mockRejectedValue(new Error("Frontend offline"));
|
||||
|
||||
const result = await service.createBlog(blogData, "user-admin-1");
|
||||
|
||||
expect(mockPrisma.blog.create).toHaveBeenCalled();
|
||||
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("health-post");
|
||||
expect(result).toEqual({
|
||||
...createdBlog,
|
||||
cacheRevalidation: "failed",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -350,7 +379,61 @@ describe("Admin BlogsService", () => {
|
||||
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith(
|
||||
"my-post",
|
||||
);
|
||||
expect(result).toEqual(updated);
|
||||
expect(result).toEqual({
|
||||
...updated,
|
||||
cacheRevalidation: "success",
|
||||
});
|
||||
});
|
||||
|
||||
it("should expose cacheRevalidation status on update result without failing persistence", async () => {
|
||||
const existing = { id: "b1", slug: "my-post", title: "Old Title" };
|
||||
mockPrisma.blog.findUnique.mockResolvedValue(existing);
|
||||
const updated = { ...existing, title: "New Title" };
|
||||
mockPrisma.blog.update.mockResolvedValue(updated);
|
||||
mockRevalidationService.revalidateBlog.mockResolvedValue(true);
|
||||
|
||||
const result = await service.updateBlog("b1", { title: "New Title" });
|
||||
|
||||
expect(mockPrisma.blog.update).toHaveBeenCalled();
|
||||
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("my-post");
|
||||
expect(result).toEqual({
|
||||
...updated,
|
||||
cacheRevalidation: "success",
|
||||
});
|
||||
});
|
||||
|
||||
it("should return cacheRevalidation 'failed' when revalidation fails on update without failing persistence", async () => {
|
||||
const existing = { id: "b1", slug: "my-post", title: "Old Title" };
|
||||
mockPrisma.blog.findUnique.mockResolvedValue(existing);
|
||||
const updated = { ...existing, title: "New Title" };
|
||||
mockPrisma.blog.update.mockResolvedValue(updated);
|
||||
mockRevalidationService.revalidateBlog.mockResolvedValue(false);
|
||||
|
||||
const result = await service.updateBlog("b1", { title: "New Title" });
|
||||
|
||||
expect(mockPrisma.blog.update).toHaveBeenCalled();
|
||||
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("my-post");
|
||||
expect(result).toEqual({
|
||||
...updated,
|
||||
cacheRevalidation: "failed",
|
||||
});
|
||||
});
|
||||
|
||||
it("should catch unexpected revalidation exceptions on update and return cacheRevalidation 'failed'", async () => {
|
||||
const existing = { id: "b1", slug: "my-post", title: "Old Title" };
|
||||
mockPrisma.blog.findUnique.mockResolvedValue(existing);
|
||||
const updated = { ...existing, title: "New Title" };
|
||||
mockPrisma.blog.update.mockResolvedValue(updated);
|
||||
mockRevalidationService.revalidateBlog.mockRejectedValue(new Error("Revalidation timeout"));
|
||||
|
||||
const result = await service.updateBlog("b1", { title: "New Title" });
|
||||
|
||||
expect(mockPrisma.blog.update).toHaveBeenCalled();
|
||||
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("my-post");
|
||||
expect(result).toEqual({
|
||||
...updated,
|
||||
cacheRevalidation: "failed",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
OnModuleInit,
|
||||
@@ -185,9 +186,7 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
|
||||
}
|
||||
}
|
||||
|
||||
// Explicit null requests Organization attribution; omission/legacy empty
|
||||
// retains the authenticated-author default. No synthetic User is created.
|
||||
const targetAuthorId = authorId === null ? null : authorId || currentUserId;
|
||||
const targetAuthorId = authorId || currentUserId;
|
||||
const now = new Date();
|
||||
const isFutureSchedule = parsedScheduledAt && parsedScheduledAt > now;
|
||||
|
||||
@@ -241,9 +240,7 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
|
||||
publishedAt: finalPublishedAt,
|
||||
scheduledAt: parsedScheduledAt,
|
||||
contentUpdatedAt: parsedContentUpdatedAt,
|
||||
...(targetAuthorId !== null
|
||||
? { author: { connect: { id: targetAuthorId } } }
|
||||
: {}),
|
||||
author: { connect: { id: targetAuthorId } },
|
||||
...(categoryId ? { category: { connect: { id: categoryId } } } : {}),
|
||||
...(tagConnectOrCreate.length > 0
|
||||
? { tags: { connectOrCreate: tagConnectOrCreate } }
|
||||
@@ -259,8 +256,21 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
|
||||
},
|
||||
});
|
||||
|
||||
await this.revalidationService.revalidateBlog(created.slug);
|
||||
return created;
|
||||
let cacheRevalidation: "success" | "failed" = "failed";
|
||||
try {
|
||||
const revalidated = await this.revalidationService.revalidateBlog(created.slug);
|
||||
cacheRevalidation = revalidated ? "success" : "failed";
|
||||
} catch (err: any) {
|
||||
this.logger.warn(
|
||||
`Cache revalidation failed for created blog [${created.slug}]: ${err?.message}`,
|
||||
);
|
||||
cacheRevalidation = "failed";
|
||||
}
|
||||
|
||||
return {
|
||||
...created,
|
||||
cacheRevalidation,
|
||||
};
|
||||
}
|
||||
|
||||
async updateBlog(id: string, data: any) {
|
||||
@@ -276,8 +286,30 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
|
||||
readingTime,
|
||||
markContentUpdated,
|
||||
contentUpdatedAt,
|
||||
expectedUpdatedAt,
|
||||
} = data;
|
||||
|
||||
const conflict = () => new ConflictException(
|
||||
"CONCURRENT_MODIFICATION: Blog has been modified by another process",
|
||||
);
|
||||
let expectedTime: number | undefined;
|
||||
if (expectedUpdatedAt !== undefined) {
|
||||
// Only accept the millisecond, timezone-qualified ISO token returned by GET
|
||||
// (or an equivalent offset). Avoid Date's coercion of null, numbers, arrays,
|
||||
// and its normalization of impossible calendar dates.
|
||||
const match = typeof expectedUpdatedAt === "string" && expectedUpdatedAt.match(
|
||||
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,3}))?(Z|[+-]\d{2}:\d{2})$/,
|
||||
);
|
||||
if (!match) throw conflict();
|
||||
expectedTime = Date.parse(expectedUpdatedAt);
|
||||
const calendar = new Date(`${match[1]}-${match[2]}-${match[3]}T00:00:00.000Z`);
|
||||
if (!Number.isFinite(expectedTime) ||
|
||||
calendar.toISOString().slice(0, 10) !== `${match[1]}-${match[2]}-${match[3]}` ||
|
||||
Number(match[4]) > 23 || Number(match[5]) > 59 || Number(match[6]) > 59) {
|
||||
throw conflict();
|
||||
}
|
||||
}
|
||||
|
||||
const mutableScalarFields = [
|
||||
"title",
|
||||
"slug",
|
||||
@@ -348,12 +380,7 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
|
||||
...(readingTime !== undefined
|
||||
? { readingTime: Number(readingTime) }
|
||||
: {}),
|
||||
// null clears attribution; omitted/legacy empty leaves it unchanged.
|
||||
...(authorId === null
|
||||
? { author: { disconnect: true } }
|
||||
: authorId
|
||||
? { author: { connect: { id: authorId } } }
|
||||
: {}),
|
||||
...(authorId ? { author: { connect: { id: authorId } } } : {}),
|
||||
...(categoryId !== undefined
|
||||
? categoryId
|
||||
? { category: { connect: { id: categoryId } } }
|
||||
@@ -399,25 +426,68 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
|
||||
};
|
||||
}
|
||||
|
||||
const updated = await this.prisma.blog.update({
|
||||
where: { id },
|
||||
data: updatePayload,
|
||||
include: {
|
||||
author: { select: { firstName: true, lastName: true } },
|
||||
category: true,
|
||||
tags: true,
|
||||
},
|
||||
});
|
||||
const include = {
|
||||
author: { select: { firstName: true, lastName: true } },
|
||||
category: true,
|
||||
tags: true,
|
||||
};
|
||||
// Legacy UI requests without a token intentionally retain last-write-wins.
|
||||
// Guarded requests claim the row using a DB predicate, not the earlier read.
|
||||
const updated = expectedTime === undefined
|
||||
? await this.prisma.blog.update({ where: { id }, data: updatePayload, include })
|
||||
: await this.prisma.$transaction(async (tx) => {
|
||||
// PostgreSQL timestamptz defaults to microseconds; JSON/JS Dates expose
|
||||
// milliseconds. Match the entire represented millisecond, then move
|
||||
// strictly beyond it (including same-ms writes and clock rollback).
|
||||
const claimedAt = new Date(Math.max(Date.now(), expectedTime + 1));
|
||||
const claim = await tx.blog.updateMany({
|
||||
where: {
|
||||
id,
|
||||
updatedAt: {
|
||||
gte: new Date(expectedTime),
|
||||
lt: new Date(expectedTime + 1),
|
||||
},
|
||||
},
|
||||
data: { updatedAt: claimedAt },
|
||||
});
|
||||
if (claim.count !== 1) throw conflict();
|
||||
// The claim holds the row lock until this nested mutation commits.
|
||||
// Any relation failure rolls back both the claim and all data writes.
|
||||
return tx.blog.update({
|
||||
where: { id },
|
||||
data: { ...updatePayload, updatedAt: claimedAt },
|
||||
include,
|
||||
});
|
||||
});
|
||||
|
||||
await this.revalidationService.revalidateBlog(updated.slug);
|
||||
return updated;
|
||||
let cacheRevalidation: "success" | "failed" = "failed";
|
||||
try {
|
||||
const revalidated = await this.revalidationService.revalidateBlog(updated.slug);
|
||||
cacheRevalidation = revalidated ? "success" : "failed";
|
||||
} catch (err: any) {
|
||||
this.logger.warn(
|
||||
`Cache revalidation failed for updated blog [${updated.slug}]: ${err?.message}`,
|
||||
);
|
||||
cacheRevalidation = "failed";
|
||||
}
|
||||
|
||||
return {
|
||||
...updated,
|
||||
cacheRevalidation,
|
||||
};
|
||||
}
|
||||
|
||||
async deleteBlog(id: string) {
|
||||
const blog = await this.prisma.blog.findUnique({ where: { id } });
|
||||
if (!blog) throw new NotFoundException("Blog not found");
|
||||
const res = await this.prisma.blog.delete({ where: { id } });
|
||||
await this.revalidationService.revalidateBlog(blog.slug);
|
||||
try {
|
||||
await this.revalidationService.revalidateBlog(blog.slug);
|
||||
} catch (err: any) {
|
||||
this.logger.warn(
|
||||
`Cache revalidation failed for deleted blog [${blog.slug}]: ${err?.message}`,
|
||||
);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
import { BadRequestException } from "@nestjs/common";
|
||||
import { assertTaskCanComplete } from "./department-task-completion.policy";
|
||||
|
||||
describe("department task completion policy", () => {
|
||||
it("rejects model-only output without a live deliverable", () => {
|
||||
expect(() =>
|
||||
assertTaskCanComplete({
|
||||
deliverableUrl: null,
|
||||
acceptanceCriteria: { criteria: ["live URL responds 200"] },
|
||||
executionLogs: [{ status: "COMPLETED_BY_AGENT" }],
|
||||
}),
|
||||
).toThrow(BadRequestException);
|
||||
});
|
||||
|
||||
it("rejects completion without acceptance criteria", () => {
|
||||
expect(() =>
|
||||
assertTaskCanComplete({
|
||||
deliverableUrl: "https://canina.ir/blog/verified",
|
||||
acceptanceCriteria: null,
|
||||
executionLogs: [{ status: "QA_PASSED" }],
|
||||
}),
|
||||
).toThrow(BadRequestException);
|
||||
});
|
||||
|
||||
it("rejects completion without QA evidence", () => {
|
||||
expect(() =>
|
||||
assertTaskCanComplete({
|
||||
deliverableUrl: "https://canina.ir/blog/verified",
|
||||
acceptanceCriteria: { criteria: ["HTTP 200"] },
|
||||
executionLogs: [{ status: "COMPLETED_BY_AGENT" }],
|
||||
}),
|
||||
).toThrow(BadRequestException);
|
||||
});
|
||||
|
||||
it("allows completion with URL, criteria and QA evidence", () => {
|
||||
expect(() =>
|
||||
assertTaskCanComplete({
|
||||
deliverableUrl: "https://canina.ir/blog/verified",
|
||||
acceptanceCriteria: { criteria: ["HTTP 200"] },
|
||||
executionLogs: [{ status: "QA_PASSED" }],
|
||||
}),
|
||||
).not.toThrow();
|
||||
});
|
||||
|
||||
it("rejects pseudo-protocol canina:// deliverable URLs", () => {
|
||||
expect(() =>
|
||||
assertTaskCanComplete({
|
||||
deliverableUrl: "canina://blog/sample-article",
|
||||
acceptanceCriteria: { criteria: ["HTTP 200"] },
|
||||
executionLogs: [{ status: "QA_PASSED" }],
|
||||
}),
|
||||
).toThrow(BadRequestException);
|
||||
});
|
||||
|
||||
it("rejects git://stage staging or repo URLs as verifiable deliverable", () => {
|
||||
expect(() =>
|
||||
assertTaskCanComplete({
|
||||
deliverableUrl: "git://stage/canina-repo#commit123",
|
||||
acceptanceCriteria: { criteria: ["HTTP 200"] },
|
||||
executionLogs: [{ status: "QA_PASSED" }],
|
||||
}),
|
||||
).toThrow(BadRequestException);
|
||||
});
|
||||
|
||||
it("rejects missing or placeholder post-tsk article deliverables", () => {
|
||||
expect(() =>
|
||||
assertTaskCanComplete({
|
||||
deliverableUrl: "https://canina.ir/blog/post-tsk-49102",
|
||||
acceptanceCriteria: { criteria: ["HTTP 200"] },
|
||||
executionLogs: [{ status: "QA_PASSED" }],
|
||||
}),
|
||||
).toThrow(BadRequestException);
|
||||
});
|
||||
|
||||
it("rejects completion when QA evidence reports missing article or 404 error", () => {
|
||||
expect(() =>
|
||||
assertTaskCanComplete({
|
||||
deliverableUrl: "https://canina.ir/blog/verified-article",
|
||||
acceptanceCriteria: { criteria: ["HTTP 200"] },
|
||||
executionLogs: [{ status: "QA_PASSED", note: "HTTP 404 - مقاله یافت نشد" }],
|
||||
}),
|
||||
).toThrow(BadRequestException);
|
||||
});
|
||||
});
|
||||
@@ -1,93 +0,0 @@
|
||||
import { BadRequestException } from "@nestjs/common";
|
||||
|
||||
export interface CompletableTask {
|
||||
code?: string | null;
|
||||
title?: string | null;
|
||||
deliverableUrl?: string | null;
|
||||
acceptanceCriteria?: unknown;
|
||||
executionLogs?: unknown;
|
||||
}
|
||||
|
||||
function hasAcceptanceCriteria(criteria: unknown): boolean {
|
||||
if (!criteria) return false;
|
||||
if (typeof criteria === "string") return criteria.trim().length > 0;
|
||||
if (Array.isArray(criteria)) return criteria.length > 0;
|
||||
if (typeof criteria === "object") {
|
||||
const value = criteria as Record<string, unknown>;
|
||||
const items = value.criteria;
|
||||
return Array.isArray(items) ? items.length > 0 : Object.keys(value).length > 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function hasValidQaEvidence(logs: unknown): boolean {
|
||||
if (!Array.isArray(logs)) return false;
|
||||
const validPassStatuses = ["QA_PASSED", "VERIFIED", "PRODUCTION_VERIFIED"];
|
||||
|
||||
let foundValidPass = false;
|
||||
for (const log of logs) {
|
||||
if (!log || typeof log !== "object") continue;
|
||||
const entry = log as Record<string, unknown>;
|
||||
const status = String(entry.status || "").toUpperCase();
|
||||
const note = String(entry.note || "").toLowerCase();
|
||||
|
||||
// Rejection of invalid QA claims: 404, missing article, not found, post-tsk
|
||||
if (
|
||||
note.includes("404") ||
|
||||
note.includes("مقاله یافت نشد") ||
|
||||
note.includes("not found") ||
|
||||
note.includes("missing article") ||
|
||||
note.includes("post-tsk")
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (validPassStatuses.includes(status)) {
|
||||
foundValidPass = true;
|
||||
}
|
||||
}
|
||||
return foundValidPass;
|
||||
}
|
||||
|
||||
export function assertTaskCanComplete(task: CompletableTask): void {
|
||||
const rawUrl = task.deliverableUrl?.trim();
|
||||
if (!rawUrl) {
|
||||
throw new BadRequestException("DONE requires a verifiable deliverable URL");
|
||||
}
|
||||
|
||||
const lowerUrl = rawUrl.toLowerCase();
|
||||
|
||||
// Reject internal pseudo-protocols
|
||||
if (lowerUrl.startsWith("canina://") || lowerUrl.includes("canina://")) {
|
||||
throw new BadRequestException("Deliverable URL cannot use pseudo-protocol canina://; an independently verifiable HTTP(S) URL is required");
|
||||
}
|
||||
|
||||
// Reject git repository pointers or staging branches as production deliverables
|
||||
if (lowerUrl.startsWith("git://") || lowerUrl.includes("git://stage")) {
|
||||
throw new BadRequestException("Deliverable URL cannot be a git staging reference; an independently verifiable HTTP(S) URL is required");
|
||||
}
|
||||
|
||||
// Must be an HTTP or HTTPS URL
|
||||
if (!lowerUrl.startsWith("http://") && !lowerUrl.startsWith("https://")) {
|
||||
throw new BadRequestException("DONE requires an independently verifiable HTTP or HTTPS deliverable URL");
|
||||
}
|
||||
|
||||
// Reject deleted or placeholder post-tsk articles
|
||||
if (/post[-_]?tsk/i.test(rawUrl)) {
|
||||
throw new BadRequestException("Deliverable URL references an unverified or deleted post-tsk placeholder article");
|
||||
}
|
||||
if (task.title && /post[-_]?tsk/i.test(task.title)) {
|
||||
throw new BadRequestException("Task references an unverified or deleted post-tsk article");
|
||||
}
|
||||
if (task.code && /post[-_]?tsk/i.test(task.code)) {
|
||||
throw new BadRequestException("Task references an unverified or deleted post-tsk code");
|
||||
}
|
||||
|
||||
if (!hasAcceptanceCriteria(task.acceptanceCriteria)) {
|
||||
throw new BadRequestException("DONE requires explicit acceptance criteria");
|
||||
}
|
||||
|
||||
if (!hasValidQaEvidence(task.executionLogs)) {
|
||||
throw new BadRequestException("DONE requires QA/production verification evidence");
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
import { GoneException } from '@nestjs/common';
|
||||
import { DepartmentTasksController } from './department-tasks.controller';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
describe('DepartmentTasksController retired mutation boundary',()=>{
|
||||
const access=jest.fn();
|
||||
const prisma=new Proxy({},{get:(_target,key)=>{access(key);throw new Error('Database access forbidden');}});
|
||||
const controller=new DepartmentTasksController(prisma as PrismaService);
|
||||
beforeEach(()=>access.mockClear());
|
||||
it('create {department:SEO,title:Legacy} returns 410 without reading or writing persistence',async()=>{
|
||||
await expect(controller.create({department:'SEO',title:'Legacy'})).rejects.toBeInstanceOf(GoneException);
|
||||
await expect(controller.create({department:'SEO',title:'Legacy'})).rejects.toMatchObject({status:410});
|
||||
expect(access).not.toHaveBeenCalled();
|
||||
});
|
||||
it('updateStatus missing, {status:DONE} returns 410 without reading or writing persistence',async()=>{
|
||||
await expect(controller.updateStatus('missing', {status:'DONE'})).rejects.toBeInstanceOf(GoneException);
|
||||
await expect(controller.updateStatus('missing', {status:'DONE'})).rejects.toMatchObject({status:410});
|
||||
expect(access).not.toHaveBeenCalled();
|
||||
});
|
||||
it('updateStatus missing, {status:TODO} returns 410 without reading or writing persistence',async()=>{
|
||||
await expect(controller.updateStatus('missing', {status:'TODO'})).rejects.toBeInstanceOf(GoneException);
|
||||
await expect(controller.updateStatus('missing', {status:'TODO'})).rejects.toMatchObject({status:410});
|
||||
expect(access).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -1,127 +0,0 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Put,
|
||||
Body,
|
||||
Param,
|
||||
Query,
|
||||
UseGuards,
|
||||
NotFoundException,
|
||||
GoneException,
|
||||
} from "@nestjs/common";
|
||||
import { ApiTags, ApiBearerAuth, ApiOperation } from "@nestjs/swagger";
|
||||
import { JwtAuthGuard } from "../auth/jwt-auth.guard";
|
||||
import { RolesGuard } from "../common/guards/roles.guard";
|
||||
import { Roles } from "../common/decorators/roles.decorator";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
|
||||
@ApiTags("Admin - Department & Agent Tasks")
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles("Admin", "SUPER_ADMIN")
|
||||
@Controller("admin/department-tasks")
|
||||
export class DepartmentTasksController {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: "لیست تسکهای دپارتمانها و ایجنتها" })
|
||||
async findAll(@Query() query: any) {
|
||||
const page = Number(query.page) || 1;
|
||||
const limit = Number(query.limit) || 100;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const where: any = {};
|
||||
if (query.department) where.department = query.department;
|
||||
if (query.status) where.status = query.status;
|
||||
if (query.priority) where.priority = query.priority;
|
||||
if (query.search) {
|
||||
where.OR = [
|
||||
{ code: { contains: query.search, mode: "insensitive" } },
|
||||
{ title: { contains: query.search, mode: "insensitive" } },
|
||||
{ description: { contains: query.search, mode: "insensitive" } },
|
||||
];
|
||||
}
|
||||
|
||||
const [total, items] = await Promise.all([
|
||||
(this.prisma as any).departmentTask.count({ where }),
|
||||
(this.prisma as any).departmentTask.findMany({
|
||||
where,
|
||||
skip,
|
||||
take: limit,
|
||||
orderBy: [{ priority: "desc" }, { createdAt: "desc" }],
|
||||
}),
|
||||
]);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: items,
|
||||
meta: {
|
||||
total,
|
||||
page,
|
||||
limit,
|
||||
totalPages: Math.ceil(total / limit),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@Get("stats")
|
||||
@ApiOperation({ summary: "آمار تسکها به تفکیک استیت و دپارتمان" })
|
||||
async getStats() {
|
||||
const tasks = await (this.prisma as any).departmentTask.findMany({
|
||||
select: { department: true, status: true, priority: true },
|
||||
});
|
||||
|
||||
const statusCounts: Record<string, number> = {
|
||||
TODO: 0,
|
||||
IN_PROGRESS: 0,
|
||||
TEST: 0,
|
||||
DONE: 0,
|
||||
BACKLOG: 0,
|
||||
IN_REVIEW: 0,
|
||||
APPROVED: 0,
|
||||
DEPLOYING: 0,
|
||||
};
|
||||
const departmentCounts: Record<string, number> = {};
|
||||
|
||||
for (const t of tasks) {
|
||||
statusCounts[t.status] = (statusCounts[t.status] || 0) + 1;
|
||||
departmentCounts[t.department] = (departmentCounts[t.department] || 0) + 1;
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
total: tasks.length,
|
||||
byStatus: statusCounts,
|
||||
byDepartment: departmentCounts,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@Get(":id")
|
||||
@ApiOperation({ summary: "دریافت جزییات یک تسک" })
|
||||
async findOne(@Param("id") id: string) {
|
||||
const task = await (this.prisma as any).departmentTask.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
if (!task) throw new NotFoundException(`Task ${id} not found`);
|
||||
return { success: true, data: task };
|
||||
}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: "ایجاد تسک جدید سازمانی" })
|
||||
async create(@Body() body: any) {
|
||||
throw new GoneException("Legacy orchestration mutations are retired. Orchestration v4 remains read-only; no replacement mutation is enabled.");
|
||||
}
|
||||
|
||||
@Put(":id/status")
|
||||
@ApiOperation({ summary: "تغییر وضعیت تسک همراه با لاگ اجرا" })
|
||||
async updateStatus(
|
||||
@Param("id") id: string,
|
||||
@Body() body: { status: string; logNote?: string; deliverableUrl?: string }
|
||||
) {
|
||||
throw new GoneException("Legacy orchestration mutations are retired. Orchestration v4 remains read-only; no replacement mutation is enabled.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { IsString, IsOptional, IsObject } from "class-validator";
|
||||
|
||||
export class CreateApprovalRequestDto {
|
||||
@ApiProperty({ description: "دپارتمان مبدا (DEV, SUPPLY, FINANCE, SEO, CRM, CONTENT)" })
|
||||
@IsString()
|
||||
department: string;
|
||||
|
||||
@ApiProperty({ description: "دستهبندی (PRICING, PURCHASE_ORDER, CAMPAIGN, DEPLOY, etc)" })
|
||||
@IsString()
|
||||
category: string;
|
||||
|
||||
@ApiProperty({ description: "عنوان مورد تایید" })
|
||||
@IsString()
|
||||
title: string;
|
||||
|
||||
@ApiProperty({ description: "دادههای تغییرات پیشنهادی" })
|
||||
@IsObject()
|
||||
payload: Record<string, any>;
|
||||
|
||||
@ApiPropertyOptional({ description: "دادههای سناریوی تست و ارزیابی" })
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
testContext?: Record<string, any>;
|
||||
}
|
||||
|
||||
export class DecideApprovalDto {
|
||||
@ApiProperty({ description: "تصمیم: APPROVED یا REJECTED" })
|
||||
@IsString()
|
||||
decision: "APPROVED" | "REJECTED";
|
||||
|
||||
@ApiPropertyOptional({ description: "توضیحات و یادداشت مدیرعامل" })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
ceoNotes?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: "نتیجه اقدام مدیریتی یا استراتژیک" })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
outcome?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: "مستندات و مدرک اقدام مدیریتی" })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
evidence?: string;
|
||||
}
|
||||
|
||||
export class QueryApprovalDto {
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
department?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
category?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
status?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
page?: number;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
limit?: number;
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import request from 'supertest';
|
||||
import { ApprovalsController } from './approvals.controller';
|
||||
import { DepartmentTasksController } from './department-tasks.controller';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { RolesGuard } from '../common/guards/roles.guard';
|
||||
|
||||
describe('retired legacy orchestration HTTP boundary', () => {
|
||||
let app: INestApplication;
|
||||
const dbAccess = jest.fn();
|
||||
const prisma = new Proxy({}, {get: (_target, key) => {
|
||||
// Nest inspects provider thenables and lifecycle hooks during setup.
|
||||
if (["then", "onModuleInit", "onApplicationBootstrap", "onModuleDestroy", "beforeApplicationShutdown", "onApplicationShutdown"].includes(String(key))) return undefined;
|
||||
dbAccess(key);
|
||||
throw new Error('Persistence must not be accessed');
|
||||
}});
|
||||
const routes = [
|
||||
['post', '/admin/approvals'],
|
||||
['put', '/admin/approvals/missing/decide'],
|
||||
['post', '/admin/department-tasks'],
|
||||
['put', '/admin/department-tasks/missing/status'],
|
||||
] as const;
|
||||
beforeAll(async () => {
|
||||
const module = await Test.createTestingModule({
|
||||
controllers: [ApprovalsController, DepartmentTasksController],
|
||||
providers: [{provide: PrismaService, useValue: prisma}, RolesGuard],
|
||||
}).overrideGuard(JwtAuthGuard).useValue({canActivate(context: any) {
|
||||
const req = context.switchToHttp().getRequest();
|
||||
if (!req.headers.authorization) return false;
|
||||
req.user = {id: 'test-admin', role: req.headers.authorization};
|
||||
return true;
|
||||
}}).compile();
|
||||
app = module.createNestApplication();
|
||||
await app.init();
|
||||
dbAccess.mockClear();
|
||||
});
|
||||
beforeEach(() => dbAccess.mockClear());
|
||||
afterAll(async () => { if (app) await app.close(); });
|
||||
it.each(routes)('%s %s returns explicit 410 for authorized callers before database access', async (method, path) => {
|
||||
const response = await request(app.getHttpServer())[method](path)
|
||||
.set('Authorization', 'Admin').send({decision:'APPROVED',status:'DONE'}).expect(410);
|
||||
expect(response.body.message).toContain('retired');
|
||||
expect(dbAccess).not.toHaveBeenCalled();
|
||||
});
|
||||
it.each(routes)('%s %s retains anonymous and customer authorization guards', async (method, path) => {
|
||||
await request(app.getHttpServer())[method](path).send({}).expect(403);
|
||||
await request(app.getHttpServer())[method](path).set('Authorization','CUSTOMER').send({}).expect(403);
|
||||
expect(dbAccess).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,3 @@
|
||||
import { registerPrepareWorker } from './orchestration-worker/worker-registration';
|
||||
import { RouterUsageModule } from './router-usage/router-usage.module';
|
||||
import { runtimeReadRegistration } from './hermes-management/runtime-read.registration';
|
||||
import { TasksModule } from './common/services/tasks.module';
|
||||
import { Module, NestModule, MiddlewareConsumer } from "@nestjs/common";
|
||||
import { PrismaModule } from "./prisma/prisma.module";
|
||||
@@ -40,17 +37,11 @@ import { DoctorsModule } from "./doctors/doctors.module";
|
||||
import { FaqModule } from "./faq/faq.module";
|
||||
import { MenuModule } from "./menu/menu.module";
|
||||
import { RevalidationModule } from "./common/revalidation/revalidation.module";
|
||||
import { OrchestrationModule } from "./orchestration/orchestration.module";
|
||||
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
...registerPrepareWorker(),
|
||||
RouterUsageModule,
|
||||
// Deny-all registration: no request identity or runtime credentials are accepted yet.
|
||||
runtimeReadRegistration(),
|
||||
OrchestrationModule,
|
||||
SupportChatModule,
|
||||
RevalidationModule,
|
||||
SmsModule,
|
||||
|
||||
@@ -2,8 +2,15 @@ import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { PaginationDto } from "../common/dto/pagination.dto";
|
||||
import {
|
||||
createPaginationMeta,
|
||||
extractSearchTokens,
|
||||
normalizeSearchText,
|
||||
calculateRelevanceScore,
|
||||
} from "../common/utils/search.utils";
|
||||
|
||||
export interface BlogFilterDto extends PaginationDto {
|
||||
categoryId?: string;
|
||||
categorySlug?: string;
|
||||
tagSlug?: string;
|
||||
featured?: boolean | string;
|
||||
@@ -20,6 +27,7 @@ export class BlogsService {
|
||||
limit = 10,
|
||||
sortBy = "publishedAt",
|
||||
sortOrder = "desc",
|
||||
categoryId,
|
||||
categorySlug,
|
||||
tagSlug,
|
||||
featured,
|
||||
@@ -32,20 +40,23 @@ export class BlogsService {
|
||||
OR: [{ publishedAt: null }, { publishedAt: { lte: now } }],
|
||||
};
|
||||
|
||||
if (search) {
|
||||
const tokens = extractSearchTokens(search);
|
||||
if (tokens.length > 0) {
|
||||
whereClause.AND = [
|
||||
{
|
||||
OR: [
|
||||
{ title: { contains: search, mode: "insensitive" } },
|
||||
{ excerpt: { contains: search, mode: "insensitive" } },
|
||||
{ content: { contains: search, mode: "insensitive" } },
|
||||
{ keywords: { contains: search, mode: "insensitive" } },
|
||||
],
|
||||
OR: tokens.flatMap((token) => [
|
||||
{ title: { contains: token, mode: "insensitive" } },
|
||||
{ excerpt: { contains: token, mode: "insensitive" } },
|
||||
{ keywords: { contains: token, mode: "insensitive" } },
|
||||
{ tags: { some: { name: { contains: token, mode: "insensitive" } } } },
|
||||
]),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
if (categorySlug) {
|
||||
if (categoryId && categoryId !== "ALL") {
|
||||
whereClause.categoryId = categoryId;
|
||||
} else if (categorySlug && categorySlug !== "all") {
|
||||
whereClause.category = { slug: categorySlug };
|
||||
}
|
||||
|
||||
@@ -57,13 +68,15 @@ export class BlogsService {
|
||||
whereClause.featured = String(featured) === "true";
|
||||
}
|
||||
|
||||
const skip = (Number(page) - 1) * Number(limit);
|
||||
const safePage = Math.max(1, Number(page) || 1);
|
||||
const safeLimit = Math.max(1, Number(limit) || 10);
|
||||
const skip = (safePage - 1) * safeLimit;
|
||||
|
||||
const [data, total] = await Promise.all([
|
||||
const [rawBlogs, total] = await Promise.all([
|
||||
this.prisma.blog.findMany({
|
||||
where: whereClause,
|
||||
skip,
|
||||
take: Number(limit),
|
||||
skip: tokens.length > 0 ? undefined : skip,
|
||||
take: tokens.length > 0 ? 100 : safeLimit,
|
||||
orderBy: [
|
||||
{
|
||||
[sortBy === "createdAt" ? "createdAt" : "publishedAt"]: sortOrder,
|
||||
@@ -88,14 +101,30 @@ export class BlogsService {
|
||||
this.prisma.blog.count({ where: whereClause }),
|
||||
]);
|
||||
|
||||
let data = rawBlogs;
|
||||
if (tokens.length > 0 && search) {
|
||||
data = [...rawBlogs]
|
||||
.map((b) => ({
|
||||
blog: b,
|
||||
score: calculateRelevanceScore(
|
||||
{
|
||||
title: b.title,
|
||||
slug: b.slug,
|
||||
shortDescription: b.excerpt,
|
||||
tags: b.tags,
|
||||
},
|
||||
search,
|
||||
tokens,
|
||||
),
|
||||
}))
|
||||
.sort((a, b) => b.score - a.score)
|
||||
.map((item) => item.blog)
|
||||
.slice(skip, skip + safeLimit);
|
||||
}
|
||||
|
||||
return {
|
||||
data,
|
||||
meta: {
|
||||
total,
|
||||
page: Number(page),
|
||||
lastPage: Math.ceil(total / Number(limit)) || 1,
|
||||
limit: Number(limit),
|
||||
},
|
||||
meta: createPaginationMeta(total, safePage, safeLimit),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,16 @@ describe("RevalidationService", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
it("uses the Next server rather than the nginx API proxy as production fallback", async () => {
|
||||
delete process.env.FRONTEND_INTERNAL_URL;
|
||||
delete process.env.FRONTEND_URL;
|
||||
delete process.env.NEXT_PUBLIC_SITE_URL;
|
||||
process.env.NODE_ENV = "production";
|
||||
const fetchSpy = jest.spyOn(global, "fetch").mockResolvedValue({ ok: true } as Response);
|
||||
await service.revalidate(["blogs"], ["/blog"]);
|
||||
expect(fetchSpy).toHaveBeenCalledWith("http://frontend_prod:3000/api/revalidate", expect.anything());
|
||||
});
|
||||
|
||||
describe("revalidateTag", () => {
|
||||
it("should send POST request with tag and secret header and return true on 200", async () => {
|
||||
const mockFetch = jest.spyOn(global, "fetch" as any).mockResolvedValue({
|
||||
@@ -111,6 +121,48 @@ describe("RevalidationService", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("revalidateBlog", () => {
|
||||
it("should trigger all tags and paths for blog slug and return true when all succeed", async () => {
|
||||
const tagSpy = jest
|
||||
.spyOn(service, "revalidateTag")
|
||||
.mockResolvedValue(true);
|
||||
const pathSpy = jest
|
||||
.spyOn(service, "revalidatePath")
|
||||
.mockResolvedValue(true);
|
||||
|
||||
const result = await service.revalidateBlog("test-slug");
|
||||
|
||||
expect(tagSpy).toHaveBeenCalledWith("blogs");
|
||||
expect(tagSpy).toHaveBeenCalledWith("home-data");
|
||||
expect(tagSpy).toHaveBeenCalledWith("blog-test-slug");
|
||||
expect(pathSpy).toHaveBeenCalledWith("/blog");
|
||||
expect(pathSpy).toHaveBeenCalledWith("/sitemap.xml");
|
||||
expect(pathSpy).toHaveBeenCalledWith("/");
|
||||
expect(pathSpy).toHaveBeenCalledWith("/blog/test-slug");
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false when a tag or path revalidation fails rather than swallowing false", async () => {
|
||||
jest.spyOn(service, "revalidateTag").mockImplementation(async (tag: string) => {
|
||||
return tag !== "blogs";
|
||||
});
|
||||
jest.spyOn(service, "revalidatePath").mockResolvedValue(true);
|
||||
|
||||
const result = await service.revalidateBlog("test-slug");
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it("should catch unexpected revalidation errors and return false", async () => {
|
||||
jest.spyOn(service, "revalidateTag").mockRejectedValue(new Error("Network disconnected"));
|
||||
jest.spyOn(service, "revalidatePath").mockResolvedValue(true);
|
||||
|
||||
const result = await service.revalidateBlog("test-slug");
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("revalidate batch", () => {
|
||||
it("should send deduplicated tags and paths in one request", async () => {
|
||||
const mockFetch = jest.spyOn(global, "fetch" as any).mockResolvedValue({ ok: true, status: 200 } as any);
|
||||
|
||||
@@ -10,7 +10,7 @@ export class RevalidationService {
|
||||
process.env.FRONTEND_URL ||
|
||||
process.env.NEXT_PUBLIC_SITE_URL ||
|
||||
(process.env.NODE_ENV === "production"
|
||||
? "http://frontend_prod:8080"
|
||||
? "http://frontend_prod:3000"
|
||||
: "http://localhost:3000");
|
||||
return raw.replace(/\/+$/, "");
|
||||
}
|
||||
@@ -135,7 +135,7 @@ export class RevalidationService {
|
||||
await Promise.allSettled(tasks);
|
||||
}
|
||||
|
||||
async revalidateBlog(slug?: string): Promise<void> {
|
||||
async revalidateBlog(slug?: string): Promise<boolean> {
|
||||
const tasks: Promise<boolean>[] = [
|
||||
this.revalidateTag("blogs"),
|
||||
this.revalidateTag("home-data"),
|
||||
@@ -149,7 +149,18 @@ export class RevalidationService {
|
||||
tasks.push(this.revalidatePath(`/blog/${slug}`));
|
||||
}
|
||||
|
||||
await Promise.allSettled(tasks);
|
||||
try {
|
||||
const results = await Promise.allSettled(tasks);
|
||||
return (
|
||||
results.length > 0 &&
|
||||
results.every(
|
||||
(result) => result.status === "fulfilled" && result.value === true,
|
||||
)
|
||||
);
|
||||
} catch (err: any) {
|
||||
this.logger.debug(`Revalidation blog skipped or failed: ${err?.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async revalidateSettings(): Promise<void> {
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
import { Injectable, Logger } from "@nestjs/common";
|
||||
|
||||
@Injectable()
|
||||
export class HermesClientService {
|
||||
private readonly logger = new Logger(HermesClientService.name);
|
||||
private readonly baseUrl: string;
|
||||
private readonly apiKey: string;
|
||||
|
||||
constructor() {
|
||||
this.baseUrl = process.env.HERMES_GATEWAY_URL || "http://canina-bot:8642";
|
||||
this.apiKey = process.env.HERMES_API_KEY || process.env.API_SERVER_KEY || "";
|
||||
}
|
||||
|
||||
async checkHealth(): Promise<boolean> {
|
||||
try {
|
||||
const res = await fetch(`${this.baseUrl}/v1/health`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.apiKey}`,
|
||||
},
|
||||
});
|
||||
return res.status === 200;
|
||||
} catch (error: any) {
|
||||
this.logger.error(`Hermes health check failed: ${error.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async triggerEvent(eventName: string, payload: Record<string, any>) {
|
||||
try {
|
||||
this.logger.log(`Dispatching event ${eventName} to Hermes Gateway`);
|
||||
const res = await fetch(`${this.baseUrl}/v1/chat/completions`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.apiKey}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: "hermes-agent",
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: `[EVENT:${eventName}] ${JSON.stringify(payload)}`,
|
||||
},
|
||||
],
|
||||
}),
|
||||
});
|
||||
const data = await res.json();
|
||||
return { success: true, data };
|
||||
} catch (error: any) {
|
||||
this.logger.warn(`Could not dispatch event to Hermes: ${error.message}`);
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import {
|
||||
normalizeSearchText,
|
||||
extractSearchTokens,
|
||||
calculateRelevanceScore,
|
||||
createPaginationMeta,
|
||||
} from "./search.utils";
|
||||
|
||||
describe("search.utils", () => {
|
||||
describe("createPaginationMeta", () => {
|
||||
it("builds consistent pagination metadata", () => {
|
||||
const meta = createPaginationMeta(52, 2, 10);
|
||||
expect(meta).toEqual({
|
||||
total: 52,
|
||||
page: 2,
|
||||
limit: 10,
|
||||
totalPages: 6,
|
||||
lastPage: 6,
|
||||
hasNextPage: true,
|
||||
hasPrevPage: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("handles boundary zero and single page counts safely", () => {
|
||||
const metaZero = createPaginationMeta(0, 1, 10);
|
||||
expect(metaZero.totalPages).toBe(1);
|
||||
expect(metaZero.hasNextPage).toBe(false);
|
||||
expect(metaZero.hasPrevPage).toBe(false);
|
||||
|
||||
const metaOne = createPaginationMeta(5, 1, 10);
|
||||
expect(metaOne.totalPages).toBe(1);
|
||||
expect(metaOne.hasNextPage).toBe(false);
|
||||
expect(metaOne.hasPrevPage).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("normalizeSearchText", () => {
|
||||
it("normalizes Arabic kaf and yeh to Persian equivalents", () => {
|
||||
expect(normalizeSearchText("قطره قلب كنينای ايران")).toBe("قطره قلب کنینای ایران");
|
||||
});
|
||||
|
||||
it("strips parentheses, diacritics, and half-spaces", () => {
|
||||
expect(normalizeSearchText("ال-کارنیتین (L-Carnitine) \u200c دارویی")).toBe("ال کارنیتین l carnitine دارویی");
|
||||
expect(normalizeSearchText("مُکَمِّل")).toBe("مکمل");
|
||||
});
|
||||
|
||||
it("converts Persian digits to English digits", () => {
|
||||
expect(normalizeSearchText("محصول شماره ۱۲۳")).toBe("محصول شماره 123");
|
||||
});
|
||||
});
|
||||
|
||||
describe("extractSearchTokens", () => {
|
||||
it("extracts meaningful tokens and removes stop words", () => {
|
||||
const tokens = extractSearchTokens("بهترین قطره برای تقویت قلب سگ و گربه");
|
||||
expect(tokens).toContain("قطره");
|
||||
expect(tokens).toContain("تقویت");
|
||||
expect(tokens).toContain("قلب");
|
||||
expect(tokens).toContain("سگ");
|
||||
expect(tokens).toContain("گربه");
|
||||
expect(tokens).not.toContain("برای");
|
||||
expect(tokens).not.toContain("و");
|
||||
});
|
||||
});
|
||||
|
||||
describe("calculateRelevanceScore", () => {
|
||||
it("ranks exact title / ingredient matches far higher than general description mentions", () => {
|
||||
const herzVital = {
|
||||
title: "هیرتز ویتال (مکمل تقویت قلب) کنینا",
|
||||
slug: "canina-herz-vital-100ml",
|
||||
ingredients: [{ nameFa: "ال-کارنیتین (L-Carnitine)", nameEn: "L-Carnitine" }],
|
||||
symptoms: [{ symptom: "بیماری قلبی" }],
|
||||
shortDescription: "حمایت تخصصی از عضله قلب سگ و گربه",
|
||||
description: "توضیحات مفصل درباره کارکرد قلب و عروق...",
|
||||
};
|
||||
|
||||
const genericProduct = {
|
||||
title: "پودر ویتامین پلاس سگ",
|
||||
slug: "canina-vitamin-plus",
|
||||
ingredients: [{ nameFa: "ویتامین A", nameEn: "Vitamin A" }],
|
||||
symptoms: [],
|
||||
shortDescription: "تقویت عمومی بدن",
|
||||
description: "این مولتی ویتامین برای تمام اندامهای بدن از جمله قلب، کبد و مفاصل مفید است.",
|
||||
};
|
||||
|
||||
const tokens = extractSearchTokens("ال-کارنیتین قلب");
|
||||
const scoreHerz = calculateRelevanceScore(herzVital, "ال-کارنیتین قلب", tokens);
|
||||
const scoreGeneric = calculateRelevanceScore(genericProduct, "ال-کارنیتین قلب", tokens);
|
||||
|
||||
expect(scoreHerz).toBeGreaterThan(scoreGeneric * 4);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,251 @@
|
||||
export interface PaginationMeta {
|
||||
total: number;
|
||||
page: number;
|
||||
limit: number;
|
||||
totalPages: number;
|
||||
lastPage: number;
|
||||
hasNextPage: boolean;
|
||||
hasPrevPage: boolean;
|
||||
}
|
||||
|
||||
export function createPaginationMeta(
|
||||
total: number,
|
||||
page: number,
|
||||
limit: number,
|
||||
): PaginationMeta {
|
||||
const safeTotal = Math.max(0, Number(total) || 0);
|
||||
const safeLimit = Math.max(1, Number(limit) || 10);
|
||||
const safePage = Math.max(1, Number(page) || 1);
|
||||
const totalPages = Math.ceil(safeTotal / safeLimit) || 1;
|
||||
|
||||
return {
|
||||
total: safeTotal,
|
||||
page: safePage,
|
||||
limit: safeLimit,
|
||||
totalPages,
|
||||
lastPage: totalPages,
|
||||
hasNextPage: safePage < totalPages,
|
||||
hasPrevPage: safePage > 1,
|
||||
};
|
||||
}
|
||||
|
||||
const PERSIAN_ARABIC_DIGITS: Record<string, string> = {
|
||||
"۰": "0", "۱": "1", "۲": "2", "۳": "3", "۴": "4",
|
||||
"۵": "5", "۶": "6", "۷": "7", "۸": "8", "۹": "9",
|
||||
"٠": "0", "١": "1", "٢": "2", "٣": "3", "٤": "4",
|
||||
"٥": "5", "٦": "6", "٧": "7", "٨": "8", "٩": "9",
|
||||
};
|
||||
|
||||
export const COMMON_STOP_WORDS = new Set([
|
||||
"از", "به", "در", "با", "برای", "و", "یا", "که", "این", "آن", "ها", "های",
|
||||
"تر", "ترین", "روی", "توی", "را", "تا", "بر", "اما", "اگر", "ولی", "یک",
|
||||
"the", "a", "an", "and", "or", "for", "with", "in", "on", "at", "to", "of",
|
||||
]);
|
||||
|
||||
export const SEARCH_EXPANSION_MAP: Record<string, string[]> = {
|
||||
"تایورین": ["تورین", "taurine", "taurin"],
|
||||
"تائورین": ["تورین", "taurine", "taurin"],
|
||||
"تورین": ["تایورین", "تائورین", "taurine", "taurin"],
|
||||
"taurine": ["تورین", "تایورین", "تائورین", "taurin"],
|
||||
"taurin": ["تورین", "تایورین", "تائورین", "taurine"],
|
||||
"کارنیتین": ["carnitine", "l-carnitine"],
|
||||
"carnitine": ["کارنیتین", "l-carnitine"],
|
||||
"گلوکزامین": ["گلوکوزامین", "glucosamine"],
|
||||
"گلوکوزامین": ["گلوکزامین", "glucosamine"],
|
||||
"glucosamine": ["گلوکوزامین", "گلوکزامین"],
|
||||
"امگا": ["omega", "امگا ۳", "سالمون", "salmon"],
|
||||
"omega": ["امگا", "امگا ۳", "salmon"],
|
||||
"کندرویتین": ["chondroitin"],
|
||||
"chondroitin": ["کندرویتین"],
|
||||
"بیوتین": ["biotin"],
|
||||
"biotin": ["بیوتین"],
|
||||
"کلسیم": ["calcium"],
|
||||
"calcium": ["کلسیم"],
|
||||
};
|
||||
|
||||
/**
|
||||
* Normalizes Persian and English text by standardizing characters,
|
||||
* removing diacritics, half-spaces, punctuation, and folding case.
|
||||
*/
|
||||
export function normalizeSearchText(text?: string | null): string {
|
||||
if (!text) return "";
|
||||
|
||||
let s = String(text);
|
||||
|
||||
// Convert Persian/Arabic digits
|
||||
s = s.replace(/[۰-۹٠-٩]/g, (ch) => PERSIAN_ARABIC_DIGITS[ch] || ch);
|
||||
|
||||
// Normalize Arabic letters to standard Persian/English
|
||||
s = s
|
||||
.replace(/[يى]/g, "ی")
|
||||
.replace(/[ك]/g, "ک")
|
||||
.replace(/[ة]/g, "ه")
|
||||
.replace(/[آأإٱ]/g, "ا")
|
||||
.replace(/[ؤ]/g, "و")
|
||||
.replace(/[ئ]/g, "ی");
|
||||
|
||||
// Remove Arabic/Persian diacritics (fatha, damma, kasra, tanwin, sukun, tashdid)
|
||||
s = s.replace(/[\u064B-\u065F\u0670]/g, "");
|
||||
|
||||
// Replace half-space, non-breaking space, parentheses, brackets, quotes, punctuation with single space
|
||||
s = s.replace(/[\u200B-\u200D\uFEFF\u00A0\u200E\u200F_~`!@#$%^&*()+=[\]{}|\\;:'",.<>/?،؛؟]/g, " ");
|
||||
|
||||
// Collapse hyphens surrounded by spaces or replace hyphen with space
|
||||
s = s.replace(/-/g, " ");
|
||||
|
||||
// Lowercase & collapse whitespace
|
||||
return s.toLowerCase().replace(/\s+/g, " ").trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts meaningful tokens from a query, eliminating stop words
|
||||
* unless the entire query only consists of stop words.
|
||||
*/
|
||||
export function extractSearchTokens(rawQuery?: string | null): string[] {
|
||||
const normalized = normalizeSearchText(rawQuery);
|
||||
if (!normalized) return [];
|
||||
|
||||
const rawTokens = normalized.split(" ").filter((t) => t.length > 0);
|
||||
if (rawTokens.length === 0) return [];
|
||||
|
||||
// Filter out stop words
|
||||
const meaningful = rawTokens.filter(
|
||||
(t) => !COMMON_STOP_WORDS.has(t) && t.length >= 2,
|
||||
);
|
||||
|
||||
// If filtering left nothing, return original non-empty tokens
|
||||
const baseTokens = meaningful.length > 0 ? meaningful : rawTokens;
|
||||
|
||||
// Expand with domain aliases (e.g. تائورین <-> تورین <-> taurine)
|
||||
const expanded = new Set<string>(baseTokens);
|
||||
for (const token of baseTokens) {
|
||||
const directAliases =
|
||||
SEARCH_EXPANSION_MAP[token] ||
|
||||
SEARCH_EXPANSION_MAP[token.toLowerCase()] ||
|
||||
SEARCH_EXPANSION_MAP[normalizeSearchText(token)];
|
||||
if (directAliases) {
|
||||
for (const alias of directAliases) {
|
||||
expanded.add(normalizeSearchText(alias));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(expanded).filter((t) => t.length >= 2);
|
||||
}
|
||||
|
||||
export interface SearchScoreItem {
|
||||
artNo?: string | null;
|
||||
barcode?: string | null;
|
||||
slug?: string | null;
|
||||
title?: string | null;
|
||||
shortDescription?: string | null;
|
||||
description?: string | null;
|
||||
tags?: Array<{ name?: string | null } | string> | null;
|
||||
ingredients?: Array<
|
||||
| { ingredient?: string | null; nameFa?: string | null; nameEn?: string | null }
|
||||
| string
|
||||
> | null;
|
||||
symptoms?: Array<{ symptom?: string | null } | string> | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a relevance score for a product or blog item against search tokens.
|
||||
*/
|
||||
export function calculateRelevanceScore(
|
||||
item: SearchScoreItem,
|
||||
rawQuery: string,
|
||||
tokens: string[],
|
||||
): number {
|
||||
if (!rawQuery || tokens.length === 0) return 0;
|
||||
|
||||
const normalizedQuery = normalizeSearchText(rawQuery);
|
||||
const normalizedTitle = normalizeSearchText(item.title);
|
||||
const normalizedArtNo = normalizeSearchText(item.artNo);
|
||||
const normalizedBarcode = normalizeSearchText(item.barcode);
|
||||
const normalizedSlug = normalizeSearchText(item.slug);
|
||||
|
||||
let score = 0;
|
||||
|
||||
// Exact matches get massive priority
|
||||
if (normalizedArtNo && normalizedArtNo === normalizedQuery) score += 200;
|
||||
if (normalizedBarcode && normalizedBarcode === normalizedQuery) score += 180;
|
||||
if (normalizedTitle && normalizedTitle === normalizedQuery) score += 150;
|
||||
if (normalizedSlug && normalizedSlug === normalizedQuery) score += 140;
|
||||
|
||||
// Title starts with full query
|
||||
if (normalizedTitle && normalizedTitle.startsWith(normalizedQuery)) score += 80;
|
||||
|
||||
// Title contains exact full query phrase
|
||||
if (normalizedTitle && normalizedTitle.includes(normalizedQuery)) score += 60;
|
||||
|
||||
// Token level matching in title (highest relevance)
|
||||
const titleTokens = normalizedTitle.split(" ");
|
||||
for (const token of tokens) {
|
||||
if (titleTokens.includes(token)) {
|
||||
score += 35;
|
||||
} else if (normalizedTitle.includes(token)) {
|
||||
score += 20;
|
||||
}
|
||||
}
|
||||
|
||||
// Ingredient / scientific terms matching
|
||||
if (item.ingredients && Array.isArray(item.ingredients)) {
|
||||
for (const ing of item.ingredients) {
|
||||
let ingName = "";
|
||||
if (typeof ing === "string") {
|
||||
ingName = ing;
|
||||
} else if (ing) {
|
||||
ingName = `${ing.ingredient || ""} ${ing.nameFa || ""} ${ing.nameEn || ""}`.trim();
|
||||
}
|
||||
const normIng = normalizeSearchText(ingName);
|
||||
if (normIng.includes(normalizedQuery)) score += 40;
|
||||
for (const token of tokens) {
|
||||
if (normIng.includes(token)) score += 15;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Symptoms matching
|
||||
if (item.symptoms && Array.isArray(item.symptoms)) {
|
||||
for (const sym of item.symptoms) {
|
||||
const symName = typeof sym === "string" ? sym : sym.symptom || "";
|
||||
const normSym = normalizeSearchText(symName);
|
||||
if (normSym.includes(normalizedQuery)) score += 30;
|
||||
for (const token of tokens) {
|
||||
if (normSym.includes(token)) score += 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Tags matching
|
||||
if (item.tags && Array.isArray(item.tags)) {
|
||||
for (const tag of item.tags) {
|
||||
const tagName = typeof tag === "string" ? tag : tag.name || "";
|
||||
const normTag = normalizeSearchText(tagName);
|
||||
if (normTag.includes(normalizedQuery)) score += 25;
|
||||
for (const token of tokens) {
|
||||
if (normTag.includes(token)) score += 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Short description matching
|
||||
const normShort = normalizeSearchText(item.shortDescription);
|
||||
if (normShort) {
|
||||
for (const token of tokens) {
|
||||
if (normShort.includes(token)) score += 5;
|
||||
}
|
||||
}
|
||||
|
||||
// Long description (low weight, capped so it never overwhelms title/ingredients)
|
||||
const normDesc = normalizeSearchText(item.description);
|
||||
if (normDesc) {
|
||||
let descHits = 0;
|
||||
for (const token of tokens) {
|
||||
if (normDesc.includes(token)) descHits += 1;
|
||||
}
|
||||
score += Math.min(descHits * 2, 6);
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
@@ -1,149 +0,0 @@
|
||||
import { HermesIntegrationService } from './hermes-integration.service';
|
||||
import { HermesAdapter } from './hermes.adapter';
|
||||
describe('backend-owned runtime authorization', () => {
|
||||
const ctx = { subject: 'admin', profiles: ['seo'] };
|
||||
const make = (allow = true, enabled = false) => {
|
||||
const fetcher = jest
|
||||
.fn()
|
||||
.mockResolvedValue(
|
||||
new Response(
|
||||
JSON.stringify({ id: 'j', profile: 'seo', enabled: false }),
|
||||
),
|
||||
);
|
||||
const authorize = jest.fn().mockResolvedValue(allow);
|
||||
return {
|
||||
fetcher,
|
||||
authorize,
|
||||
s: new HermesIntegrationService(
|
||||
new HermesAdapter(
|
||||
{
|
||||
origin: 'http://canina-hermes:9119',
|
||||
token: () => Promise.resolve('secret'),
|
||||
},
|
||||
fetcher,
|
||||
),
|
||||
{ authorize },
|
||||
enabled,
|
||||
),
|
||||
};
|
||||
};
|
||||
it('denies missing principal, missing profile, denied policy before upstream IO', async () => {
|
||||
const { s, fetcher } = make(false);
|
||||
await expect(s.pauseCron(ctx, 'seo', 'j')).rejects.toMatchObject({
|
||||
code: 'FORBIDDEN',
|
||||
});
|
||||
await expect(
|
||||
s.pauseCron({ ...ctx, subject: '' }, 'seo', 'j'),
|
||||
).rejects.toMatchObject({ code: 'FORBIDDEN' });
|
||||
await expect(s.pauseCron(ctx, 'dev', 'j')).rejects.toMatchObject({
|
||||
code: 'FORBIDDEN',
|
||||
});
|
||||
expect(fetcher).not.toHaveBeenCalled();
|
||||
});
|
||||
it('allows authorized pause but leaves resume disabled by default', async () => {
|
||||
const { s, fetcher, authorize } = make();
|
||||
await s.pauseCron(ctx, 'seo', 'j');
|
||||
expect(authorize).toHaveBeenCalledWith(ctx, 'cron.pause', 'seo');
|
||||
await expect(s.resumeCron(ctx, 'seo', 'j')).rejects.toMatchObject({
|
||||
code: 'FORBIDDEN',
|
||||
});
|
||||
expect(fetcher).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
it('fails closed on all execution and never forwards arbitrary events or RPC', async () => {
|
||||
const { s, fetcher } = make();
|
||||
expect(s.capabilities()).toMatchObject({
|
||||
chat: false,
|
||||
control: false,
|
||||
eventDispatch: false,
|
||||
cronResume: false,
|
||||
});
|
||||
await expect(s.chat(ctx, 'seo', 'hello')).rejects.toMatchObject({
|
||||
code: 'UNSUPPORTED',
|
||||
});
|
||||
await expect(s.control(ctx, 'seo', 'interrupt')).rejects.toMatchObject({
|
||||
code: 'UNSUPPORTED',
|
||||
});
|
||||
await expect(
|
||||
s.dispatchEvent(ctx, 'seo', { type: 'order.created' }),
|
||||
).rejects.toMatchObject({ code: 'FORBIDDEN' });
|
||||
expect(fetcher).not.toHaveBeenCalled();
|
||||
});
|
||||
it('fails closed when authorization provider throws', async () => {
|
||||
const a = new HermesAdapter({
|
||||
origin: 'http://canina-hermes:9119',
|
||||
token: () => Promise.resolve('secret'),
|
||||
});
|
||||
const s = new HermesIntegrationService(a, {
|
||||
authorize: () => Promise.reject(new Error('private')),
|
||||
});
|
||||
await expect(s.health(ctx)).rejects.toMatchObject({ code: 'FORBIDDEN' });
|
||||
});
|
||||
});
|
||||
|
||||
it('checks every read capability and filters the global profile roster', async () => {
|
||||
const fetcher = jest.fn().mockImplementation((input: URL) => {
|
||||
const payloads: Record<string, unknown> = {
|
||||
'/api/health': {
|
||||
ok: true,
|
||||
version: 'x',
|
||||
commit: 'x',
|
||||
auth_required: true,
|
||||
},
|
||||
'/api/status': {
|
||||
version: 'x',
|
||||
gateway_running: false,
|
||||
gateway_state: null,
|
||||
overall: 'degraded',
|
||||
active_agents: 0,
|
||||
},
|
||||
'/api/profiles': {
|
||||
profiles: [
|
||||
{ name: 'seo', is_default: false, gateway_running: false },
|
||||
{ name: 'dev', is_default: false, gateway_running: false },
|
||||
],
|
||||
},
|
||||
'/api/model/info': {
|
||||
model: 'm',
|
||||
provider: 'p',
|
||||
effective_context_length: 1,
|
||||
},
|
||||
'/api/model/options': { providers: [] },
|
||||
'/api/analytics/usage': {
|
||||
period_days: 30,
|
||||
totals: {
|
||||
total_input: 0,
|
||||
total_output: 0,
|
||||
total_sessions: 0,
|
||||
total_estimated_cost: 0,
|
||||
},
|
||||
},
|
||||
'/api/sessions': { sessions: [], total: 0 },
|
||||
'/api/cron/jobs': [],
|
||||
};
|
||||
return Promise.resolve(
|
||||
new Response(JSON.stringify(payloads[input.pathname])),
|
||||
);
|
||||
});
|
||||
const authorize = jest.fn().mockResolvedValue(true);
|
||||
const s = new HermesIntegrationService(
|
||||
new HermesAdapter(
|
||||
{
|
||||
origin: 'http://canina-hermes:9119',
|
||||
token: () => Promise.resolve('secret'),
|
||||
},
|
||||
fetcher,
|
||||
),
|
||||
{ authorize },
|
||||
);
|
||||
const p = { subject: 'verified', profiles: ['seo'] };
|
||||
await s.health(p);
|
||||
await s.status(p, 'seo');
|
||||
expect(await s.profiles(p)).toHaveLength(1);
|
||||
await s.modelInfo(p, 'seo');
|
||||
await s.modelOptions(p, 'seo');
|
||||
await s.usage(p, 'seo');
|
||||
await s.sessions(p, 'seo');
|
||||
await s.crons(p, 'seo');
|
||||
expect(authorize).toHaveBeenCalledTimes(8);
|
||||
expect(authorize.mock.calls.every((c) => c[1] === 'runtime.read')).toBe(true);
|
||||
});
|
||||
@@ -1,126 +0,0 @@
|
||||
import { HermesAdapter, HermesError, Json } from './hermes.adapter';
|
||||
/** Context MUST be constructed from verified backend identity, never request bodies. */
|
||||
export interface RuntimePrincipal {
|
||||
subject: string;
|
||||
profiles: readonly string[];
|
||||
}
|
||||
export type RuntimePermission =
|
||||
| 'runtime.read'
|
||||
| 'cron.pause'
|
||||
| 'cron.resume'
|
||||
| 'chat'
|
||||
| 'control'
|
||||
| 'event.dispatch';
|
||||
export interface RuntimeAuthorizer {
|
||||
authorize(
|
||||
principal: RuntimePrincipal,
|
||||
permission: RuntimePermission,
|
||||
profile?: string,
|
||||
): Promise<boolean>;
|
||||
}
|
||||
/** No controller or socket is registered: only the backend gatekeeper can call this seam. */
|
||||
export class HermesIntegrationService {
|
||||
constructor(
|
||||
private readonly adapter: HermesAdapter,
|
||||
private readonly policy: RuntimeAuthorizer,
|
||||
private readonly resumeEnabled = false,
|
||||
) {}
|
||||
capabilities() {
|
||||
return {
|
||||
inventory: true,
|
||||
cronPause: true,
|
||||
cronResume: this.resumeEnabled,
|
||||
chat: false,
|
||||
control: false,
|
||||
eventDispatch: false,
|
||||
} as const;
|
||||
}
|
||||
private async check(
|
||||
principal: RuntimePrincipal,
|
||||
permission: RuntimePermission,
|
||||
profile?: string,
|
||||
): Promise<void> {
|
||||
if (
|
||||
!principal?.subject ||
|
||||
!Array.isArray(principal.profiles) ||
|
||||
(profile && !principal.profiles.includes(profile))
|
||||
)
|
||||
throw new HermesError('FORBIDDEN');
|
||||
try {
|
||||
if (
|
||||
(await this.policy.authorize(principal, permission, profile)) !== true
|
||||
)
|
||||
throw new HermesError('FORBIDDEN');
|
||||
} catch {
|
||||
throw new HermesError('FORBIDDEN');
|
||||
}
|
||||
}
|
||||
async health(p: RuntimePrincipal) {
|
||||
await this.check(p, 'runtime.read');
|
||||
return this.adapter.health();
|
||||
}
|
||||
async status(p: RuntimePrincipal, profile: string) {
|
||||
await this.check(p, 'runtime.read', profile);
|
||||
return this.adapter.status(profile);
|
||||
}
|
||||
async profiles(p: RuntimePrincipal) {
|
||||
await this.check(p, 'runtime.read');
|
||||
return (await this.adapter.profiles()).filter(
|
||||
(r) => typeof r.name === 'string' && p.profiles.includes(r.name),
|
||||
);
|
||||
}
|
||||
async modelInfo(p: RuntimePrincipal, profile: string) {
|
||||
await this.check(p, 'runtime.read', profile);
|
||||
return this.adapter.modelInfo(profile);
|
||||
}
|
||||
async modelOptions(p: RuntimePrincipal, profile: string) {
|
||||
await this.check(p, 'runtime.read', profile);
|
||||
return this.adapter.modelOptions(profile);
|
||||
}
|
||||
async usage(p: RuntimePrincipal, profile: string, days = 30) {
|
||||
await this.check(p, 'runtime.read', profile);
|
||||
return this.adapter.usage(profile, days);
|
||||
}
|
||||
async sessions(p: RuntimePrincipal, profile: string, limit = 20, offset = 0) {
|
||||
await this.check(p, 'runtime.read', profile);
|
||||
return this.adapter.sessions(profile, limit, offset);
|
||||
}
|
||||
async crons(p: RuntimePrincipal, profile: string) {
|
||||
await this.check(p, 'runtime.read', profile);
|
||||
return this.adapter.crons(profile);
|
||||
}
|
||||
async pauseCron(p: RuntimePrincipal, profile: string, id: string) {
|
||||
await this.check(p, 'cron.pause', profile);
|
||||
return this.adapter.pauseCron(profile, id);
|
||||
}
|
||||
async resumeCron(p: RuntimePrincipal, profile: string, id: string) {
|
||||
await this.check(p, 'cron.resume', profile);
|
||||
if (!this.resumeEnabled) throw new HermesError('FORBIDDEN');
|
||||
return this.adapter.resumeCron(profile, id);
|
||||
}
|
||||
async chat(
|
||||
p: RuntimePrincipal,
|
||||
profile: string,
|
||||
_text: string,
|
||||
): Promise<never> {
|
||||
await this.check(p, 'chat', profile);
|
||||
throw new HermesError('UNSUPPORTED');
|
||||
}
|
||||
async control(
|
||||
p: RuntimePrincipal,
|
||||
profile: string,
|
||||
_command: 'interrupt',
|
||||
): Promise<never> {
|
||||
await this.check(p, 'control', profile);
|
||||
throw new HermesError('UNSUPPORTED');
|
||||
}
|
||||
/** Disabled independently of policy: no unreviewed event may become an agent prompt. */
|
||||
async dispatchEvent(
|
||||
p: RuntimePrincipal,
|
||||
profile: string,
|
||||
_event: Json,
|
||||
): Promise<never> {
|
||||
await this.check(p, 'event.dispatch', profile);
|
||||
throw new HermesError('FORBIDDEN');
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { HermesManagementModule } from './hermes-management.module';
|
||||
import { HermesIntegrationService } from './hermes-integration.service';
|
||||
it('requires explicit registration and denies by default without publishing controllers', async () => {
|
||||
const module = await Test.createTestingModule({
|
||||
imports: [
|
||||
HermesManagementModule.register({
|
||||
origin: 'http://canina-hermes:9119',
|
||||
token: () => Promise.resolve('secret'),
|
||||
}),
|
||||
],
|
||||
}).compile();
|
||||
const service = module.get(HermesIntegrationService);
|
||||
await expect(
|
||||
service.health({ subject: 'admin', profiles: [] }),
|
||||
).rejects.toMatchObject({ code: 'FORBIDDEN' });
|
||||
await module.close();
|
||||
});
|
||||
|
||||
it('registers backend credential broker without acquiring credentials before policy authorization', async () => {
|
||||
const credentials=jest.fn().mockResolvedValue({username:'backend',password:'test-only'});
|
||||
const module=await Test.createTestingModule({imports:[HermesManagementModule.registerAuthenticated(credentials)]}).compile();
|
||||
await expect(module.get(HermesIntegrationService).health({subject:'admin',profiles:[]})).rejects.toMatchObject({code:'FORBIDDEN'});
|
||||
expect(credentials).not.toHaveBeenCalled();
|
||||
await module.close();
|
||||
});
|
||||
@@ -1,36 +0,0 @@
|
||||
import { DynamicModule, Module } from '@nestjs/common';
|
||||
import { HermesSessionBroker } from './hermes-session-broker';
|
||||
import { HermesAdapter, HermesConfig } from './hermes.adapter';
|
||||
import {
|
||||
HermesIntegrationService,
|
||||
RuntimeAuthorizer,
|
||||
} from './hermes-integration.service';
|
||||
@Module({})
|
||||
export class HermesManagementModule {
|
||||
static registerAuthenticated(
|
||||
credentials: () => Promise<{username:string;password:string}>,
|
||||
policy: RuntimeAuthorizer = {authorize: async () => false},
|
||||
): DynamicModule {
|
||||
const broker = new HermesSessionBroker(credentials);
|
||||
return this.register({origin:'http://canina-hermes-inventory:9119',token:()=>broker.token()},policy);
|
||||
}
|
||||
|
||||
static register(
|
||||
config: HermesConfig,
|
||||
policy: RuntimeAuthorizer = { authorize: () => Promise.resolve(false) },
|
||||
): DynamicModule {
|
||||
return {
|
||||
module: HermesManagementModule,
|
||||
providers: [
|
||||
{ provide: HermesAdapter, useFactory: () => new HermesAdapter(config) },
|
||||
{
|
||||
provide: HermesIntegrationService,
|
||||
useFactory: (adapter: HermesAdapter) =>
|
||||
new HermesIntegrationService(adapter, policy, false),
|
||||
inject: [HermesAdapter],
|
||||
},
|
||||
],
|
||||
exports: [HermesIntegrationService],
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
import { afterEach } from '@jest/globals';
|
||||
import { HermesSessionBroker } from './hermes-session-broker';
|
||||
const credentials = async () => ({username:'backend',password:'test-only'});
|
||||
const response = () => new Response('{"ok":true}', {headers:{'set-cookie':'hermes_session_at=verified-test-token; Max-Age=60; Path=/; HttpOnly'}});
|
||||
describe('server-only Hermes session broker',()=>{
|
||||
afterEach(()=>{jest.useRealTimers();});
|
||||
it('renews expired cached access without retaining the refresh cookie',async()=>{
|
||||
jest.useFakeTimers();
|
||||
const fetcher=jest.fn().mockImplementation(async()=>response());
|
||||
const broker=new HermesSessionBroker(credentials,fetcher);
|
||||
await broker.token();
|
||||
await jest.advanceTimersByTimeAsync(51000);
|
||||
await broker.token();
|
||||
expect(fetcher).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
it('bounds credential waits and releases the pending login after timeout',async()=>{
|
||||
jest.useFakeTimers();
|
||||
const load=jest.fn().mockImplementationOnce(()=>new Promise(()=>{})).mockImplementation(credentials);
|
||||
const broker=new HermesSessionBroker(load,jest.fn().mockImplementation(async()=>response()));
|
||||
const result=expect(broker.token()).rejects.toMatchObject({message:'Hermes unavailable'});
|
||||
await jest.advanceTimersByTimeAsync(5001);await result;
|
||||
expect(await broker.token()).toBe('verified-test-token');
|
||||
});
|
||||
it('rejects oversized response bodies and short cookie lifetimes',async()=>{
|
||||
for(const res of [new Response('x'.repeat(4097)),new Response('{"ok":true}',{headers:{'set-cookie':'hermes_session_at=token; Max-Age=1; Path=/'}})]){
|
||||
await expect(new HermesSessionBroker(credentials,jest.fn().mockResolvedValue(res)).token()).rejects.toMatchObject({message:'Hermes auth_required'});
|
||||
}
|
||||
});
|
||||
|
||||
it('coalesces login, caches only access token and uses installed password-login contract',async()=>{
|
||||
const fetcher=jest.fn().mockImplementation(async()=>response());
|
||||
const broker=new HermesSessionBroker(credentials,fetcher);
|
||||
expect(await Promise.all([broker.token(),broker.token()])).toEqual(['verified-test-token','verified-test-token']);
|
||||
expect(await broker.token()).toBe('verified-test-token');
|
||||
expect(fetcher).toHaveBeenCalledTimes(1);
|
||||
const [url,options]=fetcher.mock.calls[0];
|
||||
expect(url).toBe('http://canina-hermes-inventory:9119/auth/password-login');
|
||||
expect(JSON.parse(options.body)).toEqual({provider:'basic',username:'backend',password:'test-only'});
|
||||
expect(options.redirect).toBe('error');
|
||||
});
|
||||
it('fails closed without echoing credentials, response bodies or provider errors',async()=>{
|
||||
for(const result of [new Response('private secret',{status:401}),new Response('{"ok":true}'),new Response('{"ok":false}',{headers:{'set-cookie':'hermes_session_at=bad; Max-Age=60'}})]){
|
||||
const broker=new HermesSessionBroker(credentials,jest.fn().mockResolvedValue(result));
|
||||
await expect(broker.token()).rejects.toMatchObject({message:'Hermes auth_required'});
|
||||
}
|
||||
});
|
||||
it('does not cache failures and permits a later successful login',async()=>{
|
||||
const fetcher=jest.fn().mockRejectedValueOnce(new Error('sensitive upstream')).mockImplementationOnce(async()=>response());
|
||||
const broker=new HermesSessionBroker(credentials,fetcher);
|
||||
await expect(broker.token()).rejects.toMatchObject({message:'Hermes unavailable'});
|
||||
expect(await broker.token()).toBe('verified-test-token');
|
||||
});
|
||||
});
|
||||
@@ -1,36 +0,0 @@
|
||||
import { HermesError } from './hermes.adapter';
|
||||
export class HermesSessionBroker {
|
||||
private cached: {token:string;until:number}|undefined;
|
||||
private pending: Promise<string>|undefined;
|
||||
constructor(private readonly credentials:()=>Promise<{username:string;password:string}>,private readonly fetcher:typeof fetch=fetch) {}
|
||||
token():Promise<string>{
|
||||
if(this.cached && this.cached.until>Date.now()) return Promise.resolve(this.cached.token);
|
||||
if(this.pending) return this.pending;
|
||||
this.pending=this.login().finally(()=>{this.pending=undefined;});
|
||||
return this.pending;
|
||||
}
|
||||
private async login():Promise<string>{
|
||||
const controller=new AbortController();
|
||||
const timer=setTimeout(()=>controller.abort(),5000);
|
||||
try{
|
||||
const creds=await Promise.race([this.credentials(),new Promise<never>((_,reject)=>controller.signal.addEventListener('abort',()=>reject(new HermesError('UNAVAILABLE')),{once:true}))]);
|
||||
if(!creds.username || !creds.password) throw new HermesError('AUTH_REQUIRED');
|
||||
const response=await this.fetcher('http://canina-hermes-inventory:9119/auth/password-login',{method:'POST',redirect:'error',signal:controller.signal,headers:{'Content-Type':'application/json'},body:JSON.stringify({provider:'basic',...creds})});
|
||||
if(!response.ok) throw new HermesError('AUTH_REQUIRED');
|
||||
const reader=response.body?.getReader();
|
||||
if(!reader) throw new HermesError('AUTH_REQUIRED');
|
||||
const chunks:Uint8Array[]=[];let size=0;
|
||||
try{for(;;){const {done,value}=await reader.read();if(done)break;size+=value.byteLength;if(size>4096)throw new HermesError('AUTH_REQUIRED');chunks.push(value);}}finally{await reader.cancel().catch(()=>undefined);}
|
||||
const body=JSON.parse(Buffer.concat(chunks).toString('utf8')) as {ok?:unknown};
|
||||
const cookies=response.headers.getSetCookie();
|
||||
const cookie=cookies.find(c=>c.startsWith('hermes_session_at='));
|
||||
const tokenMatch=cookie?.match(/^hermes_session_at=(?:"([A-Za-z0-9_.=-]+)"|([A-Za-z0-9_.=-]+));/);
|
||||
const token=tokenMatch?.[1]??tokenMatch?.[2];
|
||||
const age=Number(cookie?.match(/(?:^|;)\s*Max-Age=(\d+)/i)?.[1]);
|
||||
if(body.ok!==true || !token || !Number.isSafeInteger(age) || age<30)throw new HermesError('AUTH_REQUIRED');
|
||||
this.cached={token,until:Date.now()+Math.min(age-10,300)*1000};
|
||||
return token;
|
||||
}catch(error){if(error instanceof HermesError)throw error;throw new HermesError('UNAVAILABLE');}
|
||||
finally{clearTimeout(timer);}
|
||||
}
|
||||
}
|
||||
@@ -1,246 +0,0 @@
|
||||
import { HermesAdapter, HermesError } from './hermes.adapter';
|
||||
const health = {
|
||||
ok: true,
|
||||
version: '0.13',
|
||||
commit: 'abc',
|
||||
auth_required: true,
|
||||
};
|
||||
describe('installed Hermes health contract', () => {
|
||||
it('uses pinned internal origin, bearer only server side and strips extra fields', async () => {
|
||||
const fetcher = jest
|
||||
.fn()
|
||||
.mockResolvedValue(
|
||||
new Response(
|
||||
JSON.stringify({ ...health, token: 'never', home: '/secret' }),
|
||||
),
|
||||
);
|
||||
const adapter = new HermesAdapter(
|
||||
{
|
||||
origin: 'http://canina-hermes:9119',
|
||||
token: () => Promise.resolve('private'),
|
||||
},
|
||||
fetcher,
|
||||
);
|
||||
expect(await adapter.health()).toEqual(health);
|
||||
expect(fetcher.mock.calls[0][0].toString()).toBe(
|
||||
'http://canina-hermes:9119/api/health',
|
||||
);
|
||||
expect(fetcher.mock.calls[0][1].headers.Authorization).toBe(
|
||||
'Bearer private',
|
||||
);
|
||||
expect(fetcher.mock.calls[0][1].redirect).toBe('error');
|
||||
});
|
||||
it('refuses a runtime reporting that authentication is disabled', async () => {
|
||||
const adapter = new HermesAdapter({origin:'http://canina-hermes:9119',token:async()=> 'private'},
|
||||
jest.fn().mockResolvedValue(new Response(JSON.stringify({...health,auth_required:false}))));
|
||||
await expect(adapter.health()).rejects.toMatchObject({code:'AUTH_REQUIRED'});
|
||||
});
|
||||
|
||||
it('fails closed for bad origin, auth errors, malformed contract and oversized bodies', async () => {
|
||||
expect(
|
||||
() =>
|
||||
new HermesAdapter({
|
||||
origin: 'https://evil.test',
|
||||
token: () => Promise.resolve(''),
|
||||
}),
|
||||
).toThrow(HermesError);
|
||||
for (const response of [
|
||||
new Response('private', { status: 401 }),
|
||||
new Response('{}'),
|
||||
new Response('x'.repeat(262145)),
|
||||
]) {
|
||||
const a = new HermesAdapter(
|
||||
{
|
||||
origin: 'http://canina-hermes:9119',
|
||||
token: () => Promise.resolve('secret'),
|
||||
},
|
||||
jest.fn().mockResolvedValue(response),
|
||||
);
|
||||
await expect(a.health()).rejects.toBeInstanceOf(HermesError);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('bounded profile-scoped REST inventory', () => {
|
||||
const create = (payload: unknown) => {
|
||||
const fetcher = jest
|
||||
.fn()
|
||||
.mockResolvedValue(new Response(JSON.stringify(payload)));
|
||||
return {
|
||||
a: new HermesAdapter(
|
||||
{
|
||||
origin: 'http://canina-hermes:9119',
|
||||
token: () => Promise.resolve('secret'),
|
||||
},
|
||||
fetcher,
|
||||
),
|
||||
fetcher,
|
||||
};
|
||||
};
|
||||
it('projects status, profiles, models and usage without raw paths, prompts or credentials', async () => {
|
||||
expect(
|
||||
await create({
|
||||
version: 'x',
|
||||
gateway_running: false,
|
||||
gateway_state: null,
|
||||
overall: 'degraded',
|
||||
active_agents: 0,
|
||||
}).a.status('seo'),
|
||||
).toEqual({
|
||||
version: 'x',
|
||||
gateway_running: false,
|
||||
gateway_state: null,
|
||||
overall: 'degraded',
|
||||
active_agents: 0,
|
||||
});
|
||||
expect(
|
||||
await create({
|
||||
profiles: [
|
||||
{
|
||||
name: 'seo',
|
||||
is_default: false,
|
||||
gateway_running: false,
|
||||
path: '/private',
|
||||
description: 'token=secret',
|
||||
},
|
||||
],
|
||||
}).a.profiles(),
|
||||
).toEqual([{ name: 'seo', is_default: false, gateway_running: false }]);
|
||||
expect(
|
||||
await create({
|
||||
model: 'm',
|
||||
provider: 'p',
|
||||
effective_context_length: 0,
|
||||
api_key: 'secret',
|
||||
}).a.modelInfo('seo'),
|
||||
).toEqual({ model: 'm', provider: 'p', effective_context_length: 0 });
|
||||
expect(
|
||||
await create({
|
||||
providers: [
|
||||
{ slug: 'p', name: 'Provider', models: ['m'], api_key: 'secret' },
|
||||
],
|
||||
current: { api_key: 'secret' },
|
||||
}).a.modelOptions('seo'),
|
||||
).toEqual([{ slug: 'p', name: 'Provider', models: ['m'] }]);
|
||||
expect(
|
||||
await create({
|
||||
period_days: 7,
|
||||
totals: {
|
||||
total_input: 0,
|
||||
total_output: 2,
|
||||
total_sessions: 1,
|
||||
total_estimated_cost: 0,
|
||||
},
|
||||
tools: { secret: 1 },
|
||||
}).a.usage('seo', 7),
|
||||
).toEqual({
|
||||
period_days: 7,
|
||||
totals: {
|
||||
total_input: 0,
|
||||
total_output: 2,
|
||||
total_sessions: 1,
|
||||
total_estimated_cost: 0,
|
||||
},
|
||||
});
|
||||
});
|
||||
it('uses scoped list contract and rejects unbounded queries or profile escape', async () => {
|
||||
const { a, fetcher } = create({
|
||||
sessions: [
|
||||
{
|
||||
id: 's',
|
||||
profile: 'seo',
|
||||
is_active: false,
|
||||
source: 'cron',
|
||||
title: 'secret',
|
||||
},
|
||||
],
|
||||
total: 1,
|
||||
storage: {},
|
||||
});
|
||||
expect(await a.sessions('seo', 20, 0)).toEqual({
|
||||
sessions: [{ id: 's', profile: 'seo', is_active: false, source: 'cron' }],
|
||||
total: 1,
|
||||
});
|
||||
expect(fetcher.mock.calls[0][0].pathname).toBe('/api/sessions');
|
||||
expect(fetcher.mock.calls[0][0].searchParams.get('full')).toBe('false');
|
||||
await expect(a.sessions('../escape', 20, 0)).rejects.toThrow();
|
||||
await expect(a.sessions('seo', 101, 0)).rejects.toThrow();
|
||||
await expect(a.usage('seo', 366)).rejects.toThrow();
|
||||
await expect(
|
||||
create({
|
||||
sessions: [],
|
||||
total: 0,
|
||||
storage: { seo: 'corrupt' },
|
||||
}).a.sessions('seo'),
|
||||
).rejects.toThrow();
|
||||
await expect(
|
||||
create({
|
||||
sessions: [
|
||||
{ id: 's', profile: 'dev', source: 'cli', is_active: false },
|
||||
],
|
||||
total: 1,
|
||||
}).a.sessions('seo'),
|
||||
).rejects.toThrow();
|
||||
});
|
||||
it('lists scoped cron array; pause/resume use exact POST routes without triggers', async () => {
|
||||
expect(
|
||||
await create([
|
||||
{ id: 'j', profile: 'seo', enabled: false, prompt: 'secret' },
|
||||
]).a.crons('seo'),
|
||||
).toEqual([{ id: 'j', profile: 'seo', enabled: false }]);
|
||||
const { a, fetcher } = create({ id: 'j', profile: 'seo', enabled: false });
|
||||
expect(await a.pauseCron('seo', 'j')).toEqual({
|
||||
id: 'j',
|
||||
profile: 'seo',
|
||||
enabled: false,
|
||||
});
|
||||
expect(fetcher.mock.calls[0][0].pathname).toBe('/api/cron/jobs/j/pause');
|
||||
expect(fetcher.mock.calls[0][1].method).toBe('POST');
|
||||
const resume = create({ id: 'j', profile: 'seo', enabled: true });
|
||||
await resume.a.resumeCron('seo', 'j');
|
||||
expect(resume.fetcher.mock.calls[0][0].pathname).toBe(
|
||||
'/api/cron/jobs/j/resume',
|
||||
);
|
||||
await expect(a.pauseCron('seo', '../trigger')).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('resource budgets and safe errors', () => {
|
||||
it('does not leak upstream errors or tokens and limits credential waits', async () => {
|
||||
jest.useFakeTimers();
|
||||
const a = new HermesAdapter({
|
||||
origin: 'http://canina-hermes:9119',
|
||||
token: () => new Promise(() => {}),
|
||||
});
|
||||
const result = expect(a.health()).rejects.toMatchObject({
|
||||
code: 'UNAVAILABLE',
|
||||
message: 'Hermes unavailable',
|
||||
});
|
||||
await jest.advanceTimersByTimeAsync(5001);
|
||||
await result;
|
||||
jest.useRealTimers();
|
||||
});
|
||||
it('caps concurrency before upstream IO', async () => {
|
||||
const resolvers: ((r: Response) => void)[] = [];
|
||||
const fetcher = jest.fn(
|
||||
() =>
|
||||
new Promise<Response>((r) => {
|
||||
resolvers.push(r);
|
||||
}),
|
||||
);
|
||||
const a = new HermesAdapter(
|
||||
{
|
||||
origin: 'http://canina-hermes:9119',
|
||||
token: () => Promise.resolve('secret'),
|
||||
},
|
||||
fetcher,
|
||||
);
|
||||
const calls = Array.from({ length: 4 }, () => a.health());
|
||||
const denied = expect(a.health()).rejects.toMatchObject({ code: 'LIMIT' });
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
resolvers.forEach((r) => r(new Response(JSON.stringify(health))));
|
||||
await denied;
|
||||
await Promise.all(calls);
|
||||
});
|
||||
});
|
||||
@@ -1,342 +0,0 @@
|
||||
export type Json =
|
||||
| null
|
||||
| boolean
|
||||
| number
|
||||
| string
|
||||
| Json[]
|
||||
| { [key: string]: Json };
|
||||
export type Row = Record<string, Json>;
|
||||
export type HermesErrorCode =
|
||||
| 'CONFIGURATION'
|
||||
| 'AUTH_REQUIRED'
|
||||
| 'UNAVAILABLE'
|
||||
| 'CONTRACT'
|
||||
| 'LIMIT'
|
||||
| 'FORBIDDEN'
|
||||
| 'UNSUPPORTED';
|
||||
export class HermesError extends Error {
|
||||
constructor(readonly code: HermesErrorCode) {
|
||||
super(`Hermes ${code.toLowerCase()}`);
|
||||
}
|
||||
}
|
||||
export interface HermesConfig {
|
||||
origin: string;
|
||||
token: () => Promise<string>;
|
||||
}
|
||||
export interface Health {
|
||||
ok: boolean;
|
||||
version: string;
|
||||
commit: string;
|
||||
auth_required: boolean;
|
||||
provenance?: Row;
|
||||
}
|
||||
function object(value: unknown): Record<string, unknown> {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value))
|
||||
throw new HermesError('CONTRACT');
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
function fields(
|
||||
value: unknown,
|
||||
schema: Record<string, 'string' | 'boolean' | 'number'>,
|
||||
): Row {
|
||||
const source = object(value);
|
||||
const result: Row = {};
|
||||
for (const [key, type] of Object.entries(schema)) {
|
||||
if (
|
||||
typeof source[key] !== type ||
|
||||
(type === 'number' && !Number.isFinite(source[key]))
|
||||
)
|
||||
throw new HermesError('CONTRACT');
|
||||
result[key] = source[key] as Json;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
export class HermesAdapter {
|
||||
private readonly origin: string;
|
||||
private inflight = 0;
|
||||
constructor(
|
||||
private readonly config: HermesConfig,
|
||||
private readonly fetcher: typeof fetch = fetch,
|
||||
) {
|
||||
if (
|
||||
![
|
||||
'http://canina-hermes:9119',
|
||||
'http://canina-hermes-inventory:9119',
|
||||
].includes(config.origin)
|
||||
)
|
||||
throw new HermesError('CONFIGURATION');
|
||||
this.origin = config.origin;
|
||||
}
|
||||
private async request(
|
||||
path: string,
|
||||
profile?: string,
|
||||
method = 'GET',
|
||||
params: Record<string, string> = {},
|
||||
): Promise<unknown> {
|
||||
if (
|
||||
profile !== undefined &&
|
||||
!/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$/.test(profile)
|
||||
)
|
||||
throw new HermesError('FORBIDDEN');
|
||||
const url = new URL(path, this.origin);
|
||||
if (profile) url.searchParams.set('profile', profile);
|
||||
for (const [k, v] of Object.entries(params)) url.searchParams.set(k, v);
|
||||
if (this.inflight >= 4) throw new HermesError('LIMIT');
|
||||
this.inflight++;
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), 5000);
|
||||
try {
|
||||
const token = await Promise.race([
|
||||
this.config.token(),
|
||||
new Promise<never>((_, reject) =>
|
||||
controller.signal.addEventListener(
|
||||
'abort',
|
||||
() => reject(new HermesError('UNAVAILABLE')),
|
||||
{ once: true },
|
||||
),
|
||||
),
|
||||
]);
|
||||
if (!token || /[\r\n]/.test(token))
|
||||
throw new HermesError('AUTH_REQUIRED');
|
||||
const response = await this.fetcher(url, {
|
||||
method,
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
Accept: 'application/json',
|
||||
},
|
||||
signal: controller.signal,
|
||||
redirect: 'error',
|
||||
});
|
||||
if ([401, 403].includes(response.status))
|
||||
throw new HermesError('AUTH_REQUIRED');
|
||||
if (response.status === 404) throw new HermesError('UNSUPPORTED');
|
||||
if (!response.ok) throw new HermesError('UNAVAILABLE');
|
||||
if (Number(response.headers.get('content-length')) > 262144)
|
||||
throw new HermesError('LIMIT');
|
||||
const reader = response.body?.getReader();
|
||||
if (!reader) throw new HermesError('CONTRACT');
|
||||
let bytes = 0;
|
||||
const chunks: Uint8Array[] = [];
|
||||
try {
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
bytes += value.byteLength;
|
||||
if (bytes > 262144) throw new HermesError('LIMIT');
|
||||
chunks.push(value);
|
||||
}
|
||||
} finally {
|
||||
await reader.cancel().catch(() => undefined);
|
||||
}
|
||||
const data: unknown = JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
||||
if (this.origin === 'http://canina-hermes-inventory:9119') {
|
||||
const rows = Array.isArray(data) ? data : [data];
|
||||
for (const row of rows) provenance(row, true);
|
||||
}
|
||||
return data;
|
||||
} catch (error) {
|
||||
if (error instanceof HermesError) throw error;
|
||||
throw new HermesError('UNAVAILABLE');
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
this.inflight--;
|
||||
}
|
||||
}
|
||||
async health(): Promise<Health> {
|
||||
const raw = await this.request('/api/health');
|
||||
const health = {
|
||||
...fields(raw, {
|
||||
ok: 'boolean',
|
||||
version: 'string',
|
||||
commit: 'string',
|
||||
auth_required: 'boolean',
|
||||
}),
|
||||
...provenance(raw),
|
||||
} as unknown as Health;
|
||||
if (!health.auth_required) throw new HermesError('AUTH_REQUIRED');
|
||||
return health;
|
||||
}
|
||||
async status(profile: string): Promise<Row> {
|
||||
const raw = object(await this.request('/api/status', profile));
|
||||
const row = fields(raw, {
|
||||
version: 'string',
|
||||
gateway_running: 'boolean',
|
||||
overall: 'string',
|
||||
active_agents: 'number',
|
||||
});
|
||||
if (raw.gateway_state !== null && typeof raw.gateway_state !== 'string')
|
||||
throw new HermesError('CONTRACT');
|
||||
return {
|
||||
...row,
|
||||
gateway_state: raw.gateway_state as Json,
|
||||
...provenance(raw),
|
||||
...(raw.provenance
|
||||
? {
|
||||
gateway_running: false,
|
||||
active_agents: 0,
|
||||
gateway_state: null,
|
||||
overall: 'unknown',
|
||||
liveGatewayState: 'unknown',
|
||||
liveActiveAgents: null,
|
||||
}
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
async profiles(): Promise<Row[]> {
|
||||
return list(object(await this.request('/api/profiles')).profiles).map(
|
||||
(r) => ({
|
||||
...fields(r, {
|
||||
name: 'string',
|
||||
is_default: 'boolean',
|
||||
gateway_running: 'boolean',
|
||||
}),
|
||||
...provenance(r),
|
||||
...(object(r).provenance
|
||||
? { gateway_running: false, liveGatewayState: 'unknown' }
|
||||
: {}),
|
||||
}),
|
||||
);
|
||||
}
|
||||
async modelInfo(profile: string): Promise<Row> {
|
||||
const raw = await this.request('/api/model/info', profile);
|
||||
return {
|
||||
...fields(raw, {
|
||||
model: 'string',
|
||||
provider: 'string',
|
||||
effective_context_length: 'number',
|
||||
}),
|
||||
...provenance(raw),
|
||||
};
|
||||
}
|
||||
async modelOptions(profile: string): Promise<Row[]> {
|
||||
return list(
|
||||
object(await this.request('/api/model/options', profile)).providers,
|
||||
).map((r) => {
|
||||
const row = fields(r, { slug: 'string', name: 'string' });
|
||||
const models = list(object(r).models);
|
||||
if (models.some((m) => typeof m !== 'string'))
|
||||
throw new HermesError('CONTRACT');
|
||||
return { ...row, models: models as string[] };
|
||||
});
|
||||
}
|
||||
async usage(profile: string, days = 30): Promise<Row> {
|
||||
bound(days, 1, 365);
|
||||
const raw = object(
|
||||
await this.request('/api/analytics/usage', profile, 'GET', {
|
||||
days: String(days),
|
||||
}),
|
||||
);
|
||||
return {
|
||||
...fields(raw, { period_days: 'number' }),
|
||||
...provenance(raw),
|
||||
totals: fields(raw.totals, {
|
||||
total_input: 'number',
|
||||
total_output: 'number',
|
||||
total_sessions: 'number',
|
||||
total_estimated_cost: 'number',
|
||||
}),
|
||||
};
|
||||
}
|
||||
async sessions(profile: string, limit = 20, offset = 0): Promise<Row> {
|
||||
bound(limit, 1, 100);
|
||||
bound(offset, 0, 10000);
|
||||
const raw = object(
|
||||
await this.request('/api/sessions', profile, 'GET', {
|
||||
limit: String(limit),
|
||||
offset: String(offset),
|
||||
full: 'false',
|
||||
order: 'recent',
|
||||
}),
|
||||
);
|
||||
if (raw.storage && Object.keys(object(raw.storage)).length)
|
||||
throw new HermesError('UNAVAILABLE');
|
||||
const rows = list(raw.sessions).map((r) =>
|
||||
scoped(r, profile, {
|
||||
id: 'string',
|
||||
profile: 'string',
|
||||
source: 'string',
|
||||
is_active: 'boolean',
|
||||
}),
|
||||
);
|
||||
return {
|
||||
...fields(raw, { total: 'number' }),
|
||||
sessions: rows,
|
||||
...provenance(raw),
|
||||
};
|
||||
}
|
||||
async crons(profile: string): Promise<Row[]> {
|
||||
return list(await this.request('/api/cron/jobs', profile)).map((r) =>
|
||||
scoped(r, profile, {
|
||||
id: 'string',
|
||||
profile: 'string',
|
||||
enabled: 'boolean',
|
||||
}),
|
||||
);
|
||||
}
|
||||
private async cron(
|
||||
profile: string,
|
||||
id: string,
|
||||
action: 'pause' | 'resume',
|
||||
): Promise<Row> {
|
||||
if (!/^[a-zA-Z0-9_-]{1,128}$/.test(id)) throw new HermesError('FORBIDDEN');
|
||||
return scoped(
|
||||
await this.request(`/api/cron/jobs/${id}/${action}`, profile, 'POST'),
|
||||
profile,
|
||||
{ id: 'string', profile: 'string', enabled: 'boolean' },
|
||||
);
|
||||
}
|
||||
pauseCron(profile: string, id: string): Promise<Row> {
|
||||
return this.cron(profile, id, 'pause');
|
||||
}
|
||||
resumeCron(profile: string, id: string): Promise<Row> {
|
||||
return this.cron(profile, id, 'resume');
|
||||
}
|
||||
}
|
||||
|
||||
function list(value: unknown): unknown[] {
|
||||
if (!Array.isArray(value) || value.length > 1000)
|
||||
throw new HermesError('CONTRACT');
|
||||
return value;
|
||||
}
|
||||
function bound(value: number, min: number, max: number): void {
|
||||
if (!Number.isInteger(value) || value < min || value > max)
|
||||
throw new HermesError('LIMIT');
|
||||
}
|
||||
function scoped(
|
||||
value: unknown,
|
||||
profile: string,
|
||||
schema: Record<string, 'string' | 'boolean' | 'number'>,
|
||||
): Row {
|
||||
const row = fields(value, schema);
|
||||
if (row.profile !== profile) throw new HermesError('FORBIDDEN');
|
||||
return { ...row, ...provenance(value) };
|
||||
}
|
||||
|
||||
/** Snapshot provenance is mandatory on inventory origin; never synthesize a capture time. */
|
||||
function provenance(value: unknown, required = false): Row {
|
||||
const raw = object(value);
|
||||
if (!raw.provenance && !required) return {};
|
||||
const p = object(raw.provenance);
|
||||
if (
|
||||
p.source !== 'snapshot' ||
|
||||
p.liveProcess !== 'unknown' ||
|
||||
typeof p.capturedAt !== 'string' ||
|
||||
!p.capturedAt.endsWith('Z') ||
|
||||
!Number.isFinite(Date.parse(p.capturedAt)) ||
|
||||
Date.parse(p.capturedAt) > Date.now() ||
|
||||
typeof p.ageSeconds !== 'number' ||
|
||||
!Number.isInteger(p.ageSeconds) ||
|
||||
p.ageSeconds < 0 ||
|
||||
!['fresh', 'stale'].includes(String(p.freshness))
|
||||
)
|
||||
throw new HermesError('CONTRACT');
|
||||
return {
|
||||
provenance: {
|
||||
source: 'snapshot',
|
||||
capturedAt: p.capturedAt,
|
||||
ageSeconds: p.ageSeconds,
|
||||
freshness: p.freshness as string,
|
||||
liveProcess: 'unknown',
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
import { createServer } from 'node:http';
|
||||
import type { AddressInfo } from 'node:net';
|
||||
import { HermesAdapter } from './hermes.adapter';
|
||||
it('exercises bounded HTTP transport against an isolated server', async () => {
|
||||
const seen: string[] = [];
|
||||
const server = createServer((req, res) => {
|
||||
seen.push(`${req.method} ${req.url}`);
|
||||
if (req.headers.authorization !== 'Bearer local-test') {
|
||||
res.writeHead(401).end();
|
||||
return;
|
||||
}
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
ok: true,
|
||||
version: 'test',
|
||||
commit: 'test',
|
||||
auth_required: true,
|
||||
api_key: 'redacted',
|
||||
}),
|
||||
);
|
||||
});
|
||||
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
|
||||
try {
|
||||
const port = (server.address() as AddressInfo).port;
|
||||
const transport: typeof fetch = (input, init) =>
|
||||
fetch(
|
||||
`http://127.0.0.1:${port}${new URL(input instanceof URL ? input.href : typeof input === 'string' ? input : input.url).pathname}`,
|
||||
init,
|
||||
);
|
||||
const adapter = new HermesAdapter(
|
||||
{
|
||||
origin: 'http://canina-hermes:9119',
|
||||
token: () => Promise.resolve('local-test'),
|
||||
},
|
||||
transport,
|
||||
);
|
||||
expect(await adapter.health()).toEqual({
|
||||
ok: true,
|
||||
version: 'test',
|
||||
commit: 'test',
|
||||
auth_required: true,
|
||||
});
|
||||
expect(seen).toEqual(['GET /api/health']);
|
||||
} finally {
|
||||
await new Promise<void>((resolve, reject) =>
|
||||
server.close((e) => (e ? reject(e) : resolve())),
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -1,31 +0,0 @@
|
||||
import { loadRuntimeGrants } from './runtime-read.module';
|
||||
import { mkdtemp, writeFile, rm } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
describe('reviewed runtime grants file', () => {
|
||||
it('loads an exact reviewed file and fails closed for malformed grants', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'runtime-grants-'));
|
||||
const file = join(dir, 'grants.json');
|
||||
try {
|
||||
await writeFile(file, JSON.stringify({ 'subject-1': ['default'] }));
|
||||
expect(await loadRuntimeGrants(file, dir)).toEqual({
|
||||
'subject-1': ['default'],
|
||||
});
|
||||
for (const invalid of [
|
||||
null,
|
||||
[],
|
||||
{ 'subject-1': [] },
|
||||
{ 'subject-1': ['default', 'default'] },
|
||||
{ 'subject-1': ['../x'] },
|
||||
{ 'subject-1': [3] },
|
||||
{ 'subject-1': ['default'], extra: true },
|
||||
]) {
|
||||
await writeFile(file, JSON.stringify(invalid));
|
||||
await expect(loadRuntimeGrants(file, dir)).rejects.toThrow();
|
||||
}
|
||||
await expect(loadRuntimeGrants('/etc/passwd')).rejects.toThrow();
|
||||
} finally {
|
||||
await rm(dir, { recursive: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,83 +0,0 @@
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { PassportModule } from '@nestjs/passport';
|
||||
import { Strategy, ExtractJwt } from 'passport-jwt';
|
||||
import passport from 'passport';
|
||||
import { sign } from 'jsonwebtoken';
|
||||
import request from 'supertest';
|
||||
import { RuntimeReadModule } from './runtime-read.module';
|
||||
import { HermesIntegrationService } from './hermes-integration.service';
|
||||
describe('runtime HTTP authentication and read contract', () => {
|
||||
let app: INestApplication;
|
||||
const health = jest.fn().mockResolvedValue({ ok: true, auth_required: true });
|
||||
beforeAll(async () => {
|
||||
passport.use(
|
||||
'jwt',
|
||||
new Strategy(
|
||||
{
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
secretOrKey: 'test-only-http-key',
|
||||
},
|
||||
(payload, done) => done(null, { id: payload.sub, role: payload.role }),
|
||||
),
|
||||
);
|
||||
const module = await Test.createTestingModule({
|
||||
imports: [
|
||||
PassportModule,
|
||||
RuntimeReadModule.register({ 'admin-1': ['default'] }),
|
||||
],
|
||||
})
|
||||
.overrideProvider(HermesIntegrationService)
|
||||
.useValue({ health })
|
||||
.compile();
|
||||
app = module.createNestApplication();
|
||||
await app.init();
|
||||
});
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
passport.unuse('jwt');
|
||||
});
|
||||
it('rejects missing and forged credentials before upstream', async () => {
|
||||
health.mockClear();
|
||||
await request(app.getHttpServer())
|
||||
.get('/admin/orchestration/runtime/health')
|
||||
.expect(401);
|
||||
await request(app.getHttpServer())
|
||||
.get('/admin/orchestration/runtime/health')
|
||||
.set(
|
||||
'Authorization',
|
||||
'Bearer ' + sign({ sub: 'admin-1', role: 'Admin' }, 'forged'),
|
||||
)
|
||||
.expect(401);
|
||||
expect(health).not.toHaveBeenCalled();
|
||||
});
|
||||
it('accepts verified allowlisted admin but rejects signed ungranted subject', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.get('/admin/orchestration/runtime/health')
|
||||
.set(
|
||||
'Authorization',
|
||||
'Bearer ' + sign({ sub: 'other', role: 'Admin' }, 'test-only-http-key'),
|
||||
)
|
||||
.expect(403);
|
||||
const r = await request(app.getHttpServer())
|
||||
.get('/admin/orchestration/runtime/health')
|
||||
.set(
|
||||
'Authorization',
|
||||
'Bearer ' +
|
||||
sign({ sub: 'admin-1', role: 'Admin' }, 'test-only-http-key'),
|
||||
)
|
||||
.expect(200);
|
||||
expect(r.body).toEqual({ ok: true, auth_required: true });
|
||||
expect(r.headers['set-cookie']).toBeUndefined();
|
||||
});
|
||||
it('does not register mutation handlers', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.post('/admin/orchestration/runtime/schedules')
|
||||
.set(
|
||||
'Authorization',
|
||||
'Bearer ' +
|
||||
sign({ sub: 'admin-1', role: 'Admin' }, 'test-only-http-key'),
|
||||
)
|
||||
.expect(404);
|
||||
});
|
||||
});
|
||||
@@ -1,183 +0,0 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Req,
|
||||
Query,
|
||||
UseGuards,
|
||||
Injectable,
|
||||
ForbiddenException,
|
||||
ServiceUnavailableException,
|
||||
HttpException,
|
||||
} from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import {
|
||||
HermesIntegrationService,
|
||||
RuntimePrincipal,
|
||||
} from './hermes-integration.service';
|
||||
import { HermesError } from './hermes.adapter';
|
||||
export interface VerifiedRuntimeRequest {
|
||||
user?: {
|
||||
id?: string;
|
||||
role?: string;
|
||||
isApiKey?: boolean;
|
||||
profiles?: string[];
|
||||
};
|
||||
}
|
||||
@Injectable()
|
||||
export class RuntimeReadAccess {
|
||||
private readonly grants: Readonly<Record<string, readonly string[]>>;
|
||||
constructor(grants: Readonly<Record<string, readonly string[]>>) {
|
||||
if (
|
||||
!grants ||
|
||||
typeof grants !== 'object' ||
|
||||
Array.isArray(grants) ||
|
||||
Object.keys(grants).length > 1000
|
||||
)
|
||||
throw new Error('Invalid runtime access configuration');
|
||||
const copy: Record<string, readonly string[]> = Object.create(null);
|
||||
for (const [subject, profiles] of Object.entries(grants)) {
|
||||
if (
|
||||
!/^[a-zA-Z0-9][a-zA-Z0-9@._:-]{0,127}$/.test(subject) ||
|
||||
['__proto__', 'constructor', 'prototype'].includes(subject) ||
|
||||
!Array.isArray(profiles) ||
|
||||
profiles.length === 0 ||
|
||||
profiles.length > 100 ||
|
||||
new Set(profiles).size !== profiles.length ||
|
||||
profiles.some(
|
||||
(p) =>
|
||||
typeof p !== 'string' ||
|
||||
!/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$/.test(p),
|
||||
)
|
||||
)
|
||||
throw new Error('Invalid runtime access configuration');
|
||||
copy[subject] = Object.freeze([...profiles]);
|
||||
}
|
||||
this.grants = Object.freeze(copy);
|
||||
}
|
||||
principal(req: VerifiedRuntimeRequest): RuntimePrincipal {
|
||||
const u = req.user;
|
||||
if (
|
||||
!u?.id ||
|
||||
u.isApiKey ||
|
||||
!['admin', 'superadmin'].includes(
|
||||
(u.role ?? '').toLowerCase().replace(/[_-]/g, ''),
|
||||
) ||
|
||||
!Object.hasOwn(this.grants, u.id)
|
||||
)
|
||||
throw new ForbiddenException('Runtime access denied');
|
||||
return { subject: u.id, profiles: this.grants[u.id] };
|
||||
}
|
||||
authorize(
|
||||
p: RuntimePrincipal,
|
||||
permission: string,
|
||||
profile?: string,
|
||||
): Promise<boolean> {
|
||||
return Promise.resolve(
|
||||
permission === 'runtime.read' &&
|
||||
Object.hasOwn(this.grants, p.subject) &&
|
||||
(!profile || this.grants[p.subject].includes(profile)),
|
||||
);
|
||||
}
|
||||
}
|
||||
@Controller('admin/orchestration/runtime')
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
export class RuntimeReadController {
|
||||
constructor(
|
||||
private readonly runtime: HermesIntegrationService,
|
||||
private readonly access: RuntimeReadAccess,
|
||||
) {}
|
||||
private async read(
|
||||
req: VerifiedRuntimeRequest,
|
||||
call: (p: RuntimePrincipal) => Promise<unknown>,
|
||||
) {
|
||||
const p = this.access.principal(req);
|
||||
try {
|
||||
return await call(p);
|
||||
} catch (e) {
|
||||
if (e instanceof HermesError) {
|
||||
if (e.code === 'FORBIDDEN')
|
||||
throw new ForbiddenException('Runtime access denied');
|
||||
if (e.code === 'LIMIT')
|
||||
throw new HttpException('Runtime request limit', 429);
|
||||
}
|
||||
throw new ServiceUnavailableException('Runtime unavailable');
|
||||
}
|
||||
}
|
||||
private profile(
|
||||
q: Record<string, unknown>,
|
||||
p: RuntimePrincipal,
|
||||
extras: string[] = [],
|
||||
): string {
|
||||
if (
|
||||
Object.keys(q).some((k) => !['profile', ...extras].includes(k)) ||
|
||||
typeof q.profile !== 'string' ||
|
||||
!p.profiles.includes(q.profile)
|
||||
)
|
||||
throw new HermesError('FORBIDDEN');
|
||||
return q.profile;
|
||||
}
|
||||
private number(
|
||||
q: Record<string, unknown>,
|
||||
key: string,
|
||||
fallback: number,
|
||||
min: number,
|
||||
max: number,
|
||||
): number {
|
||||
if (q[key] === undefined) return fallback;
|
||||
if (typeof q[key] !== 'string' || !/^\d+$/.test(q[key]))
|
||||
throw new HermesError('LIMIT');
|
||||
const n = Number(q[key]);
|
||||
if (!Number.isSafeInteger(n) || n < min || n > max)
|
||||
throw new HermesError('LIMIT');
|
||||
return n;
|
||||
}
|
||||
@Get('health') health(@Req() req: VerifiedRuntimeRequest) {
|
||||
return this.read(req, (p) => this.runtime.health(p));
|
||||
}
|
||||
@Get('profiles') profiles(@Req() req: VerifiedRuntimeRequest) {
|
||||
return this.read(req, (p) => this.runtime.profiles(p));
|
||||
}
|
||||
@Get('status') status(
|
||||
@Req() req: VerifiedRuntimeRequest,
|
||||
@Query() q: Record<string, unknown>,
|
||||
) {
|
||||
return this.read(req, (p) => this.runtime.status(p, this.profile(q, p)));
|
||||
}
|
||||
@Get('usage') usage(
|
||||
@Req() req: VerifiedRuntimeRequest,
|
||||
@Query() q: Record<string, unknown>,
|
||||
) {
|
||||
return this.read(req, (p) =>
|
||||
this.runtime.usage(
|
||||
p,
|
||||
this.profile(q, p, ['days']),
|
||||
this.number(q, 'days', 30, 1, 365),
|
||||
),
|
||||
);
|
||||
}
|
||||
@Get('sessions') sessions(
|
||||
@Req() req: VerifiedRuntimeRequest,
|
||||
@Query() q: Record<string, unknown>,
|
||||
) {
|
||||
return this.read(req, (p) =>
|
||||
this.runtime.sessions(
|
||||
p,
|
||||
this.profile(q, p, ['limit', 'offset']),
|
||||
this.number(q, 'limit', 20, 1, 100),
|
||||
this.number(q, 'offset', 0, 0, 10000),
|
||||
),
|
||||
);
|
||||
}
|
||||
@Get('schedules') schedules(
|
||||
@Req() req: VerifiedRuntimeRequest,
|
||||
@Query() q: Record<string, unknown>,
|
||||
) {
|
||||
return this.read(req, (p) => this.runtime.crons(p, this.profile(q, p)));
|
||||
}
|
||||
@Get('model-info') modelInfo(
|
||||
@Req() req: VerifiedRuntimeRequest,
|
||||
@Query() q: Record<string, unknown>,
|
||||
) {
|
||||
return this.read(req, (p) => this.runtime.modelInfo(p, this.profile(q, p)));
|
||||
}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
import { DynamicModule, Module } from '@nestjs/common';
|
||||
import { open, lstat } from 'node:fs/promises';
|
||||
import { constants } from 'node:fs';
|
||||
import { dirname, resolve } from 'node:path';
|
||||
import { HermesManagementModule } from './hermes-management.module';
|
||||
import {
|
||||
RuntimeReadAccess,
|
||||
RuntimeReadController,
|
||||
} from './runtime-read.controller';
|
||||
import { HermesError } from './hermes.adapter';
|
||||
export async function runtimeSecret(path: string): Promise<string> {
|
||||
if (!/^\/run\/secrets\/[a-zA-Z0-9_-]+$/.test(path))
|
||||
throw new HermesError('CONFIGURATION');
|
||||
try {
|
||||
const file = await open(path, 'r');
|
||||
try {
|
||||
const stat = await file.stat();
|
||||
if (!stat.isFile() || stat.size > 4096) throw new Error();
|
||||
const value = (await file.readFile('utf8')).trim();
|
||||
if (!value || /[\r\n]/.test(value)) throw new Error();
|
||||
return value;
|
||||
} finally {
|
||||
await file.close();
|
||||
}
|
||||
} catch {
|
||||
throw new HermesError('AUTH_REQUIRED');
|
||||
}
|
||||
}
|
||||
/** Exact fixed mount roots only; startup fails closed, never browser/JWT grants. */
|
||||
export async function loadRuntimeGrants(
|
||||
path = '/run/secrets/hermes_runtime_grants',
|
||||
reviewedRoot?: string,
|
||||
): Promise<Readonly<Record<string, readonly string[]>>> {
|
||||
const roots = reviewedRoot
|
||||
? [resolve(reviewedRoot)]
|
||||
: ['/run/secrets', '/etc/canina'];
|
||||
if (!roots.includes(dirname(path)) || resolve(path) !== path)
|
||||
throw new HermesError('CONFIGURATION');
|
||||
try {
|
||||
const stat = await lstat(path);
|
||||
if (
|
||||
!stat.isFile() ||
|
||||
stat.isSymbolicLink() ||
|
||||
stat.size > 65536 ||
|
||||
stat.mode & 0o022
|
||||
)
|
||||
throw new Error();
|
||||
const file = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
||||
try {
|
||||
const data: unknown = JSON.parse(await file.readFile('utf8'));
|
||||
if (!data || typeof data !== 'object' || Array.isArray(data))
|
||||
throw new Error();
|
||||
new RuntimeReadAccess(data as Record<string, readonly string[]>);
|
||||
return data as Record<string, readonly string[]>;
|
||||
} finally {
|
||||
await file.close();
|
||||
}
|
||||
} catch {
|
||||
throw new HermesError('CONFIGURATION');
|
||||
}
|
||||
}
|
||||
@Module({})
|
||||
export class RuntimeReadModule {
|
||||
static async registerFromFile(
|
||||
path = '/run/secrets/hermes_runtime_grants',
|
||||
): Promise<DynamicModule> {
|
||||
return this.register(await loadRuntimeGrants(path));
|
||||
}
|
||||
static register(
|
||||
grants: Readonly<Record<string, readonly string[]>>,
|
||||
): DynamicModule {
|
||||
const access = new RuntimeReadAccess(grants);
|
||||
return {
|
||||
module: RuntimeReadModule,
|
||||
imports: [
|
||||
HermesManagementModule.registerAuthenticated(
|
||||
async () => ({
|
||||
username: await runtimeSecret('/run/secrets/hermes_username'),
|
||||
password: await runtimeSecret('/run/secrets/hermes_password'),
|
||||
}),
|
||||
access,
|
||||
),
|
||||
],
|
||||
controllers: [RuntimeReadController],
|
||||
providers: [{ provide: RuntimeReadAccess, useValue: access }],
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
import { runtimeReadRegistration } from './runtime-read.registration';
|
||||
import { RuntimeReadModule } from './runtime-read.module';
|
||||
describe('application runtime registration',()=>{
|
||||
afterEach(()=>jest.restoreAllMocks());
|
||||
it('registers denied routes without reading files when not explicitly enabled',async()=>{
|
||||
const file=jest.spyOn(RuntimeReadModule,'registerFromFile');
|
||||
const register=jest.spyOn(RuntimeReadModule,'register');
|
||||
await runtimeReadRegistration({});
|
||||
expect(file).not.toHaveBeenCalled();expect(register).toHaveBeenCalledWith({});
|
||||
});
|
||||
it('uses only the fixed reviewed grants mount when enabled',async()=>{
|
||||
const file=jest.spyOn(RuntimeReadModule,'registerFromFile').mockResolvedValue({module:RuntimeReadModule});
|
||||
await runtimeReadRegistration({CANINA_RUNTIME_READ_ENABLED:'true'});
|
||||
expect(file).toHaveBeenCalledWith('/run/secrets/hermes_runtime_grants');
|
||||
});
|
||||
});
|
||||
@@ -1,6 +0,0 @@
|
||||
import { RuntimeReadModule } from './runtime-read.module';
|
||||
export function runtimeReadRegistration(env:Record<string,string|undefined>=process.env) {
|
||||
return env.CANINA_RUNTIME_READ_ENABLED === 'true'
|
||||
? RuntimeReadModule.registerFromFile('/run/secrets/hermes_runtime_grants')
|
||||
: RuntimeReadModule.register({});
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
import { Test } from '@nestjs/testing';
|
||||
import {
|
||||
RuntimeReadController,
|
||||
RuntimeReadAccess,
|
||||
} from './runtime-read.controller';
|
||||
import { HermesIntegrationService } from './hermes-integration.service';
|
||||
import { HermesError } from './hermes.adapter';
|
||||
import {
|
||||
ForbiddenException,
|
||||
ServiceUnavailableException,
|
||||
} from '@nestjs/common';
|
||||
describe('runtime read boundary', () => {
|
||||
const health = jest.fn();
|
||||
const profiles = jest.fn();
|
||||
const status = jest.fn();
|
||||
const service = {
|
||||
health,
|
||||
profiles,
|
||||
status,
|
||||
} as unknown as HermesIntegrationService;
|
||||
const access = new RuntimeReadAccess({ 'admin-1': ['default'] });
|
||||
const controller = new RuntimeReadController(service, access);
|
||||
beforeEach(() => jest.clearAllMocks());
|
||||
it('denies unconfigured identity without IO', async () => {
|
||||
await expect(
|
||||
controller.health({ user: { id: 'other', role: 'Admin' } }),
|
||||
).rejects.toBeInstanceOf(ForbiddenException);
|
||||
expect(health).not.toHaveBeenCalled();
|
||||
});
|
||||
it('uses server scope not body or user supplied profiles', async () => {
|
||||
status.mockResolvedValue({ overall: 'ok' });
|
||||
await expect(
|
||||
controller.status(
|
||||
{ user: { id: 'admin-1', role: 'Admin', profiles: ['other'] } },
|
||||
{ profile: 'other' },
|
||||
),
|
||||
).rejects.toBeInstanceOf(ForbiddenException);
|
||||
expect(status).not.toHaveBeenCalled();
|
||||
await controller.status(
|
||||
{ user: { id: 'admin-1', role: 'Admin' } },
|
||||
{ profile: 'default' },
|
||||
);
|
||||
expect(status).toHaveBeenCalledWith(
|
||||
{ subject: 'admin-1', profiles: ['default'] },
|
||||
'default',
|
||||
);
|
||||
});
|
||||
it('rejects duplicate/missing/unknown query parameters before IO', async () => {
|
||||
for (const q of [
|
||||
{},
|
||||
{ profile: ['default', 'other'] },
|
||||
{ profile: 'default', token: 'leak' },
|
||||
])
|
||||
await expect(
|
||||
controller.status({ user: { id: 'admin-1', role: 'Admin' } }, q),
|
||||
).rejects.toBeInstanceOf(ForbiddenException);
|
||||
expect(status).not.toHaveBeenCalled();
|
||||
});
|
||||
it('maps credential failure without leaking upstream errors', async () => {
|
||||
health.mockRejectedValue(new HermesError('AUTH_REQUIRED'));
|
||||
await expect(
|
||||
controller.health({ user: { id: 'admin-1', role: 'Admin' } }),
|
||||
).rejects.toThrow('Runtime unavailable');
|
||||
});
|
||||
it('rejects API keys and non-admins even when subject is configured', async () => {
|
||||
for (const user of [
|
||||
{ id: 'admin-1', role: 'User' },
|
||||
{ id: 'admin-1', role: 'Admin', isApiKey: true },
|
||||
])
|
||||
await expect(controller.health({ user })).rejects.toBeInstanceOf(
|
||||
ForbiddenException,
|
||||
);
|
||||
expect(health).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -1,68 +0,0 @@
|
||||
import { HermesAdapter } from './hermes.adapter';
|
||||
const provenance = {
|
||||
source: 'snapshot',
|
||||
capturedAt: '2020-01-01T00:00:00Z',
|
||||
ageSeconds: 100,
|
||||
freshness: 'stale',
|
||||
liveProcess: 'unknown',
|
||||
};
|
||||
function adapter(data: unknown) {
|
||||
return new HermesAdapter(
|
||||
{
|
||||
origin: 'http://canina-hermes-inventory:9119',
|
||||
token: () => Promise.resolve('test'),
|
||||
},
|
||||
jest.fn(() =>
|
||||
Promise.resolve(new Response(JSON.stringify(data), { status: 200 })),
|
||||
) as typeof fetch,
|
||||
);
|
||||
}
|
||||
describe('snapshot provenance boundary', () => {
|
||||
it('rejects missing provenance from dedicated inventory origin', async () => {
|
||||
await expect(
|
||||
adapter({
|
||||
ok: true,
|
||||
version: '1',
|
||||
commit: 'x',
|
||||
auth_required: true,
|
||||
}).health(),
|
||||
).rejects.toMatchObject({ code: 'CONTRACT' });
|
||||
});
|
||||
it('cannot present copied gateway scalars as live telemetry', async () => {
|
||||
const row = await adapter({
|
||||
version: '1',
|
||||
gateway_running: true,
|
||||
active_agents: 12,
|
||||
gateway_state: 'running',
|
||||
overall: 'ok',
|
||||
provenance,
|
||||
}).status('default');
|
||||
expect(row).toMatchObject({
|
||||
gateway_running: false,
|
||||
active_agents: 0,
|
||||
gateway_state: null,
|
||||
overall: 'unknown',
|
||||
liveGatewayState: 'unknown',
|
||||
liveActiveAgents: null,
|
||||
provenance,
|
||||
});
|
||||
});
|
||||
it('profile readiness cannot be inferred from copied PID state', async () => {
|
||||
const rows = await adapter({
|
||||
profiles: [
|
||||
{
|
||||
name: 'default',
|
||||
is_default: true,
|
||||
gateway_running: true,
|
||||
provenance,
|
||||
},
|
||||
],
|
||||
provenance,
|
||||
}).profiles();
|
||||
expect(rows[0]).toMatchObject({
|
||||
gateway_running: false,
|
||||
liveGatewayState: 'unknown',
|
||||
provenance,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,11 +0,0 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": ".",
|
||||
"outDir": "../../../.hermes-contract-build",
|
||||
"incremental": false,
|
||||
"strict": true
|
||||
},
|
||||
"include": ["./*.ts"],
|
||||
"exclude": []
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { createHash, randomUUID } from 'crypto';
|
||||
import { sign } from 'jsonwebtoken';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { PrepareWorkflowService } from '../orchestration/prepare-workflow.service';
|
||||
import { OrchestrationController } from '../orchestration/orchestration.controller';
|
||||
import { OrchestrationService } from '../orchestration/orchestration.service';
|
||||
import { JwtStrategy } from '../auth/jwt.strategy';
|
||||
import { UsersService } from '../users/users.service';
|
||||
import { RedisService } from '../redis/redis.service';
|
||||
import { WorkerTransportModule } from './worker-transport.module';
|
||||
import { EditorialExecutionService } from './editorial-execution';
|
||||
|
||||
(process.env.EDITORIAL_EXECUTION_DB === '1' ? describe : describe.skip)('completed recovery real JWT HTTP/PostgreSQL (synthetic evidence only)', () => {
|
||||
const db = new PrismaClient();
|
||||
const sha = (s: string) => createHash('sha256').update(s).digest('hex');
|
||||
const workerToken = 'disposable-pmo-'.repeat(5);
|
||||
let app: INestApplication, base: string, workflow: PrepareWorkflowService;
|
||||
let reviewer: any, admin: any, jwt: string, adminJwt: string;
|
||||
const generation = randomUUID(), session = 'disposable-recovery-http';
|
||||
const brief = () => ({researchDate:'2026-10-24',inputAvailability:[{input:'property',status:'UNAVAILABLE',details:'No authenticated property supplied'}],findings:[{sourceUrl:'https://www.who.int/news',sourceDate:'2026-10-20',finding:'Synthetic contract fixture, not research'}],candidates:[0,1,2].map(i=>({title:'Synthetic '+i,score:80-i,rationale:'Contract fixture only',sourceUrls:['https://www.who.int/news']}))});
|
||||
const token = (u: any, extra: any = {}) => sign({sub:u.id,stageTestSession:session,stageTestGeneration:generation,...extra},process.env.JWT_SECRET!,{expiresIn:'5m'});
|
||||
beforeAll(async () => {
|
||||
const module = await Test.createTestingModule({
|
||||
imports:[WorkerTransportModule.register({enabled:true,grants:[{id:'pmo',role:'WORKFLOW_WORKER',credentialSha256:sha(workerToken)}],workerIds:['pmo'],stageCiIds:[],artifactDirectory:'/work/tmp/recovery',artifactBaseUrl:'https://isolated.invalid/internal/prepare-worker/artifacts'})],
|
||||
controllers:[OrchestrationController],
|
||||
providers:[JwtStrategy,EditorialExecutionService,{provide:OrchestrationService,useValue:{}},{provide:UsersService,useValue:{findById:(id:string)=>db.user.findUnique({where:{id}})}},{provide:RedisService,useValue:{get:async()=>null}}],
|
||||
}).overrideProvider(PrismaService).useValue(db).compile();
|
||||
app=module.createNestApplication(); app.setGlobalPrefix('api'); await app.listen(0,'127.0.0.1'); base=await app.getUrl(); workflow=app.get(PrepareWorkflowService);
|
||||
reviewer=await db.user.create({data:{firstName:'Disposable',lastName:'Reviewer',mobile:'fixture-'+randomUUID().slice(0,7),role:'SUPER_ADMIN',stageTestSession:session,stageTestGeneration:generation,authExpiresAt:new Date(Date.now()+300000)}});
|
||||
admin=await db.user.create({data:{firstName:'Disposable',lastName:'Admin',mobile:'fixture-'+randomUUID().slice(0,7),role:'Admin',stageTestSession:session,stageTestGeneration:generation,authExpiresAt:new Date(Date.now()+300000)}});
|
||||
jwt=token(reviewer); adminJwt=token(admin);
|
||||
});
|
||||
afterAll(async()=>{await app?.close();await db.$disconnect();});
|
||||
async function request(path:string,body:any,secret=jwt) {const r=await fetch(base+'/api/'+path,{method:'POST',headers:{authorization:'Bearer '+secret,'content-type':'application/json'},body:JSON.stringify(body)});return {status:r.status,body:await r.json()};}
|
||||
async function fixture(ttl = 60000) {
|
||||
const p=await request('internal/prepare-worker/editorial-proposals',{requestId:randomUUID(),pilotKind:'editorial-research-to-brief-v1',operationMode:'PREPARE',environment:'staging',scope:'EDITORIAL_RESEARCH_TO_BRIEF',researchTopic:'Disposable recovery contract',acceptanceCriteria:['Dated evidence'],commitSha:'a'.repeat(40),budgetLimitCents:20},workerToken);
|
||||
expect(p.status).toBe(201);
|
||||
let t=await db.orchestrationTask.findUniqueOrThrow({where:{id:p.body.taskId}});
|
||||
t=await workflow.decide(t.id,1,{decision:'APPROVED',expiresAt:new Date(Date.now()+ttl).toISOString(),expectedVersion:t.version,idempotencyKey:randomUUID()},{id:'independent-approver',role:'SUPER_ADMIN'});
|
||||
const c=await request('internal/prepare-worker/editorial-claim',{taskId:t.id},workerToken);expect(c.status).toBe(201);
|
||||
const l=c.body.lease;
|
||||
await db.prepareWorkflowOutbox.update({where:{taskId:t.id},data:{leaseUntil:new Date(Date.now()-1000)}});
|
||||
const original=brief(); original.inputAvailability[0].status='missing';
|
||||
const originalText=JSON.stringify(original);
|
||||
const evidence:any={taskId:t.id,runId:l.runId,expectedVersion:t.version+1,commitSha:'a'.repeat(40),originalText,sessions:['canina-competitor-worker','canina-content-worker','canina-seo-worker','pmo'].map((profile,i)=>({profile,sessionId:'20261024_120000_aaaaa'+i,outputSha256:i===3?sha(originalText):sha('synthetic '+i),outcome:'COMPLETED'})),outputs:[{retrievalEvents:[{type:'tool_use',name:'web_extract',tool_call_id:'fixture-extract',input:{urls:['https://www.who.int/news']}},{type:'tool_result',name:'web_extract',tool_call_id:'fixture-extract',output:JSON.stringify({results:[{url:'https://www.who.int/news',content:'Synthetic retrieved bytes'}]})}]}],brief:brief(),correction:{mode:'STORED_REPORTS_ONLY',statusChanges:[{index:0,from:'missing',to:'UNAVAILABLE',reason:'Stored text explicitly says no authenticated property supplied'}],omissions:[],reason:'Synthetic stored-reports-only schema correction'}};
|
||||
return {t,l,evidence,path:'admin/orchestration/tasks/'+t.id+'/editorial-completed-recovery'};
|
||||
}
|
||||
const payload=(e:any)=>({runId:e.runId,expectedVersion:e.expectedVersion,evidenceSha256:sha(JSON.stringify(e)),evidence:e});
|
||||
it('allows narrowly scoped temporary reviewer, atomically ingests same run once, retains cost and pending QA',async()=>{
|
||||
const {t,l,evidence,path}=await fixture(); const body=payload(evidence);
|
||||
expect((await request(path,body,adminJwt)).status).toBe(403);
|
||||
expect((await request(path,body,token(reviewer,{stageTestGeneration:randomUUID()}))).status).toBe(401);
|
||||
expect((await request(path,body,'not-a-jwt')).status).toBe(401);
|
||||
expect((await request(path,{...body,actualCents:0})).status).toBe(400);
|
||||
expect((await request('admin/orchestration/tasks/'+t.id+'/budget/reconcile',{actualCents:0})).status).toBe(401);
|
||||
const results=await Promise.all([request(path,body),request(path,body)]);
|
||||
expect(results.map(x=>x.status).sort()).toEqual([201,403]);
|
||||
expect(results.find(x=>x.status===201)!.body).toMatchObject({state:'VERIFYING',qaStatus:'PENDING_INDEPENDENT_QA',retainedReservationCents:20,costStatus:'upper-bound-reserved'});
|
||||
const artifacts=await db.orchestrationArtifact.findMany({where:{runId:l.runId}});expect(artifacts).toHaveLength(2);
|
||||
const stored=artifacts.find(x=>x.kind==='EDITORIAL_RECOVERY_EVIDENCE')!;expect((stored.metadata as any).bytes).toBe(JSON.stringify(evidence));expect(sha((stored.metadata as any).bytes)).toBe(stored.sha256);
|
||||
const recovered=artifacts.find(x=>x.kind!=='EDITORIAL_RECOVERY_EVIDENCE')!;expect(sha((recovered.metadata as any).bytes)).toBe(recovered.sha256);
|
||||
expect(JSON.parse((recovered.metadata as any).bytes)).toMatchObject({runId:l.runId,recoveryEvidenceSha256:stored.sha256,qaStatus:'PENDING_INDEPENDENT_QA'});
|
||||
expect((await db.orchestrationRun.findUniqueOrThrow({where:{id:l.runId}})).status).toBe('AWAITING_QA');
|
||||
expect(await db.orchestrationAttestation.count({where:{runId:l.runId}})).toBe(0);
|
||||
expect((await db.orchestrationBudgetLedger.findMany({where:{taskId:t.id}})).map(x=>[x.kind,x.amountCents])).toEqual([['RESERVE',20]]);
|
||||
expect((await db.prepareWorkflowOutbox.findUniqueOrThrow({where:{taskId:t.id}}))).toMatchObject({status:'DONE',leaseToken:null,leaseUntil:null});
|
||||
expect((await request('internal/prepare-worker/heartbeat',{taskId:t.id,runId:l.runId,leaseToken:l.leaseToken},workerToken)).status).toBe(403);
|
||||
expect((await request('internal/prepare-worker/editorial-result',{taskId:t.id,runId:l.runId,leaseToken:l.leaseToken,brief:brief()},workerToken)).status).toBe(403);
|
||||
await expect(db.orchestrationArtifact.update({where:{id:stored.id},data:{metadata:{bytes:'tampered'}}})).rejects.toThrow('append-only');
|
||||
});
|
||||
it('rolls back evidence insertion when canonical database read-back is corrupted',async()=>{
|
||||
const f=await fixture();
|
||||
await db.$executeRawUnsafe(`CREATE FUNCTION fixture_corrupt_evidence() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.kind = 'EDITORIAL_RECOVERY_EVIDENCE' THEN NEW.metadata = jsonb_set(NEW.metadata, '{bytes}', '\"corrupt\"'::jsonb); END IF; RETURN NEW; END; $$`);
|
||||
await db.$executeRawUnsafe(`CREATE TRIGGER fixture_corrupt BEFORE INSERT ON orchestration_artifacts FOR EACH ROW EXECUTE FUNCTION fixture_corrupt_evidence()`);
|
||||
try {
|
||||
expect((await request(f.path,payload(f.evidence))).status).toBe(403);
|
||||
expect(await db.orchestrationArtifact.count({where:{runId:f.l.runId}})).toBe(0);
|
||||
expect((await db.orchestrationTask.findUniqueOrThrow({where:{id:f.t.id}})).state).toBe('RUNNING');
|
||||
expect((await db.orchestrationRun.findUniqueOrThrow({where:{id:f.l.runId}})).status).toBe('RUNNING');
|
||||
expect((await db.prepareWorkflowOutbox.findUniqueOrThrow({where:{taskId:f.t.id}})).leaseToken).toBe(f.l.leaseToken);
|
||||
expect(await db.orchestrationEvent.count({where:{taskId:f.t.id,eventType:'EDITORIAL_COMPLETED_RUN_RECOVERED'}})).toBe(0);
|
||||
expect((await db.orchestrationBudgetLedger.findMany({where:{taskId:f.t.id}})).map(x=>[x.kind,x.amountCents])).toEqual([['RESERVE',20]]);
|
||||
} finally {
|
||||
await db.$executeRawUnsafe('DROP TRIGGER fixture_corrupt ON orchestration_artifacts');
|
||||
await db.$executeRawUnsafe('DROP FUNCTION fixture_corrupt_evidence()');
|
||||
}
|
||||
});
|
||||
it('rejects hash/provenance/meaning changes, active leases, expired approvals, cancellation and kills without writes',async()=>{
|
||||
for(const mode of ['hash','retrieval','rewrite','version','commit','lease','approval','cancel','kill']) {
|
||||
const f=await fixture(mode==='approval'?1500:60000); const e=structuredClone(f.evidence);
|
||||
if(mode==='retrieval')e.outputs[0].retrievalEvents[1].tool_call_id='unmatched';
|
||||
if(mode==='rewrite')e.brief.candidates[0].rationale='Invented replacement';
|
||||
if(mode==='version')e.expectedVersion++;
|
||||
if(mode==='commit')e.commitSha='b'.repeat(40);
|
||||
if(mode==='lease')await db.prepareWorkflowOutbox.update({where:{taskId:f.t.id},data:{leaseUntil:new Date(Date.now()+60000)}});
|
||||
if(mode==='approval')await new Promise(resolve=>setTimeout(resolve,1600));
|
||||
if(mode==='cancel')await db.orchestrationTask.update({where:{id:f.t.id},data:{state:'CANCELLED'}});
|
||||
if(mode==='kill')await db.orchestrationKillSwitch.upsert({where:{scope:'GLOBAL'},create:{scope:'GLOBAL',level:'STOP_AGENTS',active:true,reason:'Disposable test',actorId:'fixture'},update:{active:true,level:'STOP_AGENTS'}});
|
||||
try {const b=payload(e);if(mode==='hash')b.evidenceSha256='0'.repeat(64); const r=await request(f.path,b);expect([400,403]).toContain(r.status);expect(await db.orchestrationArtifact.count({where:{runId:f.l.runId}})).toBe(0);expect(await db.orchestrationBudgetLedger.count({where:{taskId:f.t.id}})).toBe(1);}
|
||||
finally {if(mode==='kill')await db.orchestrationKillSwitch.update({where:{scope:'GLOBAL'},data:{active:false}});}
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,45 +0,0 @@
|
||||
import { EditorialExecutionService } from './editorial-execution';
|
||||
import { createHash, randomUUID } from 'crypto';
|
||||
const sha=(x:string)=>createHash('sha256').update(x).digest('hex');
|
||||
describe('completed same-run recovery admission',()=>{
|
||||
let s:EditorialExecutionService,tx:any,t:any,o:any,r:any,evidence:any,d:any;
|
||||
const reviewer={id:'independent',role:'SUPER_ADMIN'};
|
||||
beforeEach(()=>{
|
||||
t={id:randomUUID(),state:'RUNNING',version:6,metadata:{commitSha:'a'.repeat(40)}};
|
||||
r={id:randomUUID(),taskId:t.id,status:'RUNNING',environment:'staging',commitSha:t.metadata.commitSha};
|
||||
o={id:randomUUID(),runId:r.id,status:'PENDING',leaseOwner:'pmo',leaseUntil:new Date(Date.now()-1000),reservationId:'reserve',leaseToken:randomUUID()};
|
||||
const brief={researchDate:'2026-10-24',inputAvailability:[{input:'Analytics',status:'UNAVAILABLE',details:'Not provided'}],findings:[{sourceUrl:'https://canina.de/a',sourceDate:'2026-10-24',finding:'Stored report'}],candidates:['A','B','C'].map(title=>({title,score:50,rationale:'Stored report',sourceUrls:['https://canina.de/a']}))};
|
||||
const sessions=['20261024_164035_8d20ed','20261024_164323_5b66bf','20261024_164531_02c075','20261024_164737_96755a'];
|
||||
evidence={taskId:t.id,runId:r.id,expectedVersion:6,commitSha:r.commitSha,originalText:JSON.stringify(brief),sessions:sessions.map((sessionId,i)=>({profile:['canina-competitor-worker','canina-content-worker','canina-seo-worker','pmo'][i],sessionId,outputSha256:sha(i===3?JSON.stringify(brief):'stored report'),outcome:'COMPLETED'})),outputs:[{retrievalEvents:[{type:'tool_use',name:'web_extract',tool_call_id:'1',input:{urls:['https://canina.de/a']}},{type:'tool_result',name:'web_extract',tool_call_id:'1',is_error:false,output:JSON.stringify({results:[{url:'https://canina.de/a',content:'Stored report'}]})}]}],brief,correction:{mode:'STORED_REPORTS_ONLY',statusChanges:[],omissions:[],reason:'No new research; original preserved'}};
|
||||
d={expectedVersion:6,runId:r.id,evidenceSha256:sha(JSON.stringify(evidence)),evidence};
|
||||
tx={$queryRaw:async()=>[],orchestrationArtifact:{count:async()=>0,create:jest.fn(async({data}:any)=>data),findFirst:async()=>({metadata:{bytes:JSON.stringify(evidence)}})},orchestrationRun:{findUnique:async()=>r,update:jest.fn()},prepareWorkflowOutbox:{findUnique:async()=>o,update:jest.fn()},orchestrationTask:{update:jest.fn(async()=>({...t,state:'VERIFYING',version:7}))},orchestrationBudgetLedger:{findUnique:async()=>({taskId:t.id,actorId:'pmo',kind:'RESERVE',amountCents:20,idempotencyKey:'prepare-reserve:'+r.id}),aggregate:async()=>({_sum:{amountCents:20}}),findFirst:async()=>null},orchestrationEvent:{create:jest.fn()}};
|
||||
s=new EditorialExecutionService({$transaction:async(fn:any)=>fn(tx)} as any);jest.spyOn(s as any,'scope').mockResolvedValue(t);jest.spyOn(s as any,'approval').mockResolvedValue(undefined);
|
||||
});
|
||||
it('stores original evidence and unreviewed artifact without lease renewal or cost writes',async()=>{
|
||||
await expect((s as any).recoverCompleted(reviewer,t.id,d)).resolves.toMatchObject({state:'VERIFYING',qaStatus:'PENDING_INDEPENDENT_QA',retainedReservationCents:20});
|
||||
expect(tx.orchestrationArtifact.create).toHaveBeenCalledTimes(2);
|
||||
expect(tx.orchestrationArtifact.create.mock.calls[0][0].data.metadata.bytes).toBe(JSON.stringify(evidence));
|
||||
expect(tx.prepareWorkflowOutbox.update.mock.calls[0][0].data.leaseToken).toBeNull();
|
||||
});
|
||||
it.each(['actor','version','hash','run','lease','cancel','artifact','source','evidenceBinding','session','approval','kill','storedHash','rewrite','omission'])('rejects %s before writes',async(mode)=>{
|
||||
let a=reviewer;
|
||||
if(mode==='actor')a={id:'pmo',role:'WORKFLOW_WORKER'};
|
||||
if(mode==='version')d.expectedVersion=5;
|
||||
if(mode==='hash')d.evidenceSha256='0'.repeat(64);
|
||||
if(mode==='run')r.taskId=randomUUID();
|
||||
if(mode==='lease')o.leaseUntil=new Date(Date.now()+60000);
|
||||
if(mode==='cancel')t.state='CANCELLED';
|
||||
if(mode==='artifact')tx.orchestrationArtifact.count=async()=>1;
|
||||
if(mode==='source')evidence.brief.findings[0].sourceUrl='https://canina.de/search-only';
|
||||
if(mode==='evidenceBinding')evidence.taskId=randomUUID();
|
||||
if(mode==='session')evidence.sessions[0].outcome='RUNNING';
|
||||
if(mode==='approval')jest.spyOn(s as any,'approval').mockRejectedValue(new Error('expired approval'));
|
||||
if(mode==='kill')tx.$queryRaw=async()=>[{active:true,level:'STOP_AGENTS'}];
|
||||
if(mode==='storedHash')tx.orchestrationArtifact.findFirst=async()=>({metadata:{bytes:'changed'}});
|
||||
if(mode==='rewrite')evidence.brief.candidates[0].rationale='Changed meaning';
|
||||
if(mode==='omission')evidence.brief.candidates.pop();
|
||||
if(mode!=='hash')d.evidenceSha256=sha(JSON.stringify(evidence));
|
||||
await expect((s as any).recoverCompleted(a,t.id,d)).rejects.toThrow();if(mode!=='storedHash')expect(tx.orchestrationArtifact.create).not.toHaveBeenCalled();
|
||||
else expect(tx.orchestrationArtifact.create).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
@@ -1,19 +0,0 @@
|
||||
# Opt-in fragment ONLY: merge into the reviewed Stage backend service definition.
|
||||
# Not a standalone compose file; does not enable the worker automatically.
|
||||
# Host files/dirs must already exist, with reviewed ownership and modes.
|
||||
x-prepare-worker-backend:
|
||||
environment:
|
||||
PREPARE_WORKER_ENABLED: "${PREPARE_WORKER_ENABLED:-false}"
|
||||
secrets:
|
||||
- source: prepare_worker_grants
|
||||
target: prepare_worker_grants
|
||||
mode: 0400
|
||||
volumes:
|
||||
- type: bind
|
||||
source: ${PREPARE_WORKER_ARTIFACT_HOST_DIRECTORY:?review private artifact directory}
|
||||
target: /var/lib/canina/prepare-artifacts
|
||||
bind:
|
||||
create_host_path: false
|
||||
secrets:
|
||||
prepare_worker_grants:
|
||||
file: ${PREPARE_WORKER_GRANTS_FILE:?review mounted grants JSON file}
|
||||
@@ -1,44 +0,0 @@
|
||||
# Bounded PMO editorial proposal bridge
|
||||
|
||||
No live configuration, deployment, publication or execution is introduced. Opt-in transport/startup flag remains unchanged. Core services/controllers are untouched.
|
||||
|
||||
## Parent contract
|
||||
|
||||
`POST /internal/prepare-worker/editorial-proposals` (plus the application's existing `/api` global prefix when present), Bearer service credential. Strict JSON body, all fields required, no extra fields:
|
||||
|
||||
```json
|
||||
{
|
||||
"requestId": "a client-generated UUID retained across retries",
|
||||
"pilotKind": "editorial-research-to-brief-v1",
|
||||
"operationMode": "PREPARE",
|
||||
"environment": "staging",
|
||||
"scope": "EDITORIAL_RESEARCH_TO_BRIEF",
|
||||
"researchTopic": "Evidence-based senior dog nutrition research",
|
||||
"acceptanceCriteria": ["Cite primary sources with dates", "Return a research brief; do not publish"],
|
||||
"commitSha": "40 lowercase hexadecimal characters",
|
||||
"budgetLimitCents": 20
|
||||
}
|
||||
```
|
||||
|
||||
Topic 1..180 characters, criteria 1..10 items each 1..500 characters; whitespace-only rejected. commitSha must be a string. requestId must be a UUID. Fixed pilot/scope/environment/mode/budget are checked, not caller-controlled options. Department is server-owned CONTENT. Human research topic and criteria are owner-reviewable proposal data, not instructions executed by this endpoint.
|
||||
|
||||
201 returns `{taskId,key,pilotKind,state,version,department,operationMode,budgetLimitCents,metadata:{scope,acceptanceCriteria,environment,commitSha}}`. First success is GATE1_PENDING; replay returns current state, does not undo owner decisions or requeue work. Scope includes pilot kind, topic and explicit no-publication/release/production restriction. `key` is `editorial-v1:` plus SHA256 of JSON `[authenticatedWorkerId,requestId]`. Same request UUID + differing immutable proposal is 409. Concurrent identical requests produce one task, one PREPARE_PROPOSED event, one PREPARE_TRANSITION event. Crash after create can resume DRAFT submission. No arbitrary reads or mutations are exposed.
|
||||
|
||||
## Explicit grant and author authority
|
||||
|
||||
Only canonical server identity `pmo`, authenticated by a configured unique digest grant with role WORKFLOW_WORKER and included in workerIds, can submit. There was no canonical PMO grant mapping in repository transport configuration: the operator must explicitly provision this grant through the existing fixed `/run/secrets/prepare_worker_grants` configuration, using a new random dedicated credential, **without runtime defaults**. Never copy an arbitrary agent/request actor ID or reuse CI identity. Missing pmo grant denies; other valid workers and Stage CI get 403. Transport disabled remains denied/unregistered according to existing registration boundary. No secrets were read or provisioned by this change.
|
||||
|
||||
Existing core create/transition APIs accept only Admin/SUPER_ADMIN. This bounded adapter delegates **author-only Admin capability**, binding id to the authenticated pmo grant, for exactly create PREPARE and DRAFT→GATE1_PENDING. It never uses SUPER_ADMIN, calls decide, approves, queues READY, writes through Prisma, creates outbox/runs, reserves/reconciles costs or submits evidence. Prisma reads only support idempotency/payload reconciliation. Core remains authoritative for kill switch, immutable metadata/events, versioning and independent Gate1 reviewer. No approval bypass or arbitrary Admin endpoint is exposed.
|
||||
|
||||
Owner uses existing Backend owner Gate1 queue and independent authenticated SUPER_ADMIN review. This bridge does not replace that UI/API. Current deterministic/Tech transports are not editorial executors: editorial keys are explicitly refused by generic claim/complete even after independent approval. Actual research execution/completion needs a separately reviewed executor/ingestion contract; no toy QA, fake zero cost, release or publication is claimed.
|
||||
|
||||
## Verification
|
||||
|
||||
Disposable PostgreSQL16 (no published ports/live volumes), Node22 private copied dependencies from readonly orchestration-v4-runtime donor, regenerated private Prisma client and real listening Nest TCP HTTP boundary. Evidence directory `/opt/services/canina/validation/v4-editorial-bridge/`:
|
||||
|
||||
- red.log: missing endpoint 404 vs required 201.
|
||||
- tracer-red.log: approved editorial mistakenly admitted by generic tracer (201 vs required 403), fixed with transport fence.
|
||||
- green.log: five bridge tests pass, including race/replay/crash recovery, immutable payload conflict, grant/body/scope/production denial, kill switch and independent approval with tracer refusal.
|
||||
- regression-final.log / build.log: all 4 worker suites, 46 tests pass (exit 0); full backend tsc -b exits 0. New bridge files scoped ESLint exits 0 (lint.log). Initial combined regression hit a shared fixture kill-switch uniqueness collision; tests now preserve and restore the fixture row.
|
||||
|
||||
Tests use disposable fixture commit hashes, not deployed commit evidence. No AppModule, production, live DB, release authorization, push or deploy changes. Parent owns ingress service-network/TLS/body/rate limits and reviewed grant provisioning. This commit is the proposal slice only.
|
||||
@@ -1,271 +0,0 @@
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { createHash, randomUUID } from 'crypto';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { PrepareWorkflowService } from '../orchestration/prepare-workflow.service';
|
||||
import { WorkerTransportModule } from './worker-transport.module';
|
||||
|
||||
(process.env.EDITORIAL_BRIDGE_DB === '1' ? describe : describe.skip)(
|
||||
'editorial proposal real TCP boundary',
|
||||
() => {
|
||||
const db = new PrismaClient();
|
||||
const token = 'disposable-pmo-token-'.repeat(3),
|
||||
other = 'disposable-other-token-'.repeat(3),
|
||||
ci = 'disposable-ci-token-'.repeat(3);
|
||||
const digest = (s: string) => createHash('sha256').update(s).digest('hex');
|
||||
let app: INestApplication, base: string, workflow: PrepareWorkflowService;
|
||||
const config = (enabled = true, withPmo = true) => ({
|
||||
enabled,
|
||||
grants: [
|
||||
...(withPmo
|
||||
? [
|
||||
{
|
||||
id: 'pmo',
|
||||
role: 'WORKFLOW_WORKER' as const,
|
||||
credentialSha256: digest(token),
|
||||
},
|
||||
]
|
||||
: []),
|
||||
{
|
||||
id: 'other',
|
||||
role: 'WORKFLOW_WORKER' as const,
|
||||
credentialSha256: digest(other),
|
||||
},
|
||||
{ id: 'ci', role: 'STAGE_CI' as const, credentialSha256: digest(ci) },
|
||||
],
|
||||
workerIds: withPmo ? ['pmo', 'other'] : ['other'],
|
||||
stageCiIds: ['ci'],
|
||||
artifactDirectory: '/work/tmp/editorial-artifacts',
|
||||
artifactBaseUrl:
|
||||
'https://isolated.invalid/internal/prepare-worker/artifacts',
|
||||
});
|
||||
async function start(c = config()) {
|
||||
const m = await Test.createTestingModule({
|
||||
imports: [WorkerTransportModule.register(c)],
|
||||
})
|
||||
.overrideProvider(PrismaService)
|
||||
.useValue(db)
|
||||
.compile();
|
||||
const a = m.createNestApplication();
|
||||
await a.listen(0, '127.0.0.1');
|
||||
return a;
|
||||
}
|
||||
beforeAll(async () => {
|
||||
app = await start();
|
||||
base = await app.getUrl();
|
||||
workflow = app.get(PrepareWorkflowService);
|
||||
});
|
||||
afterAll(async () => {
|
||||
await app?.close();
|
||||
await db.$disconnect();
|
||||
});
|
||||
const dto = () => ({
|
||||
requestId: randomUUID(),
|
||||
pilotKind: 'editorial-research-to-brief-v1',
|
||||
operationMode: 'PREPARE',
|
||||
environment: 'staging',
|
||||
scope: 'EDITORIAL_RESEARCH_TO_BRIEF',
|
||||
researchTopic: 'Evidence-based senior dog nutrition research',
|
||||
acceptanceCriteria: [
|
||||
'Cite primary sources with dates',
|
||||
'Return a research brief, not a published article',
|
||||
],
|
||||
commitSha: 'a'.repeat(40),
|
||||
budgetLimitCents: 20,
|
||||
});
|
||||
async function post(body: any, secret = token, url = base) {
|
||||
const r = await fetch(
|
||||
url + '/internal/prepare-worker/editorial-proposals',
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
authorization: 'Bearer ' + secret,
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
},
|
||||
);
|
||||
return { status: r.status, body: await r.json() };
|
||||
}
|
||||
it('submits immutable owner-reviewable proposal, never approves or dispatches, retries idempotently', async () => {
|
||||
const body = dto();
|
||||
const r = await post(body);
|
||||
expect(r.status).toBe(201);
|
||||
expect(r.body).toMatchObject({
|
||||
pilotKind: body.pilotKind,
|
||||
state: 'GATE1_PENDING',
|
||||
operationMode: 'PREPARE',
|
||||
department: 'CONTENT',
|
||||
budgetLimitCents: 20,
|
||||
metadata: {
|
||||
environment: 'staging',
|
||||
commitSha: body.commitSha,
|
||||
acceptanceCriteria: body.acceptanceCriteria,
|
||||
},
|
||||
});
|
||||
const task = await db.orchestrationTask.findUniqueOrThrow({
|
||||
where: { id: r.body.taskId },
|
||||
});
|
||||
expect((task.metadata as any).createdBy).toBe('pmo');
|
||||
expect(task.key).toMatch(/^editorial-v1:/);
|
||||
expect((await post(body)).body.taskId).toBe(task.id);
|
||||
expect(
|
||||
await db.orchestrationEvent.count({ where: { taskId: task.id } }),
|
||||
).toBe(2);
|
||||
for (const model of [
|
||||
db.orchestrationApprovalDecision,
|
||||
db.orchestrationRun,
|
||||
db.prepareWorkflowOutbox,
|
||||
db.orchestrationBudgetLedger,
|
||||
] as any[])
|
||||
expect(await model.count({ where: { taskId: task.id } })).toBe(0);
|
||||
expect(
|
||||
(await post({ ...body, researchTopic: 'Changed topic' })).status,
|
||||
).toBe(409);
|
||||
expect((await post({ ...body, commitSha: 'b'.repeat(40) })).status).toBe(
|
||||
409,
|
||||
);
|
||||
await expect(
|
||||
workflow.decide(task.id, 1, {}, { id: 'pmo', role: 'Admin' }),
|
||||
).rejects.toThrow('Authenticated workflow identity required');
|
||||
});
|
||||
it('rejects untrusted workers, CI, impersonation, unbounded scope and disabled or absent PMO grants', async () => {
|
||||
for (const secret of [other, ci])
|
||||
expect((await post(dto(), secret)).status).toBe(403);
|
||||
expect((await post(dto(), 'incorrect')).status).toBe(401);
|
||||
for (const patch of [
|
||||
{ actorId: 'owner' },
|
||||
{ role: 'SUPER_ADMIN' },
|
||||
{ department: 'ENGINEERING' },
|
||||
{ scope: 'arbitrary' },
|
||||
{ pilotKind: 'other' },
|
||||
{ environment: 'production' },
|
||||
{ operationMode: 'RELEASE' },
|
||||
{ budgetLimitCents: 21 },
|
||||
{ researchTopic: ' ' },
|
||||
{ acceptanceCriteria: [' '] },
|
||||
{ commitSha: 123 },
|
||||
{ acceptanceCriteria: [] },
|
||||
{ taskId: randomUUID() },
|
||||
])
|
||||
expect((await post({ ...dto(), ...patch })).status).toBe(400);
|
||||
for (const c of [config(false), config(true, false)]) {
|
||||
const isolated = await start(c);
|
||||
try {
|
||||
const r = await post(dto(), token, await isolated.getUrl());
|
||||
expect([401, 403]).toContain(r.status);
|
||||
} finally {
|
||||
await isolated.close();
|
||||
}
|
||||
}
|
||||
});
|
||||
it('races identical requests with one proposal and submission event, and resumes a draft', async () => {
|
||||
const body = dto();
|
||||
const responses = await Promise.all(
|
||||
Array.from({ length: 5 }, () => post(body)),
|
||||
);
|
||||
expect(responses.map((r) => r.status)).toEqual([201, 201, 201, 201, 201]);
|
||||
expect(new Set(responses.map((r) => r.body.taskId)).size).toBe(1);
|
||||
expect(
|
||||
await db.orchestrationEvent.count({
|
||||
where: { taskId: responses[0].body.taskId },
|
||||
}),
|
||||
).toBe(2);
|
||||
const pending = await post(dto());
|
||||
const task = await db.orchestrationTask.findUniqueOrThrow({
|
||||
where: { id: pending.body.taskId },
|
||||
});
|
||||
// Interrupted transaction fixture: core create exists but no submission.
|
||||
const retry = dto();
|
||||
const key =
|
||||
'editorial-v1:' + digest(JSON.stringify(['pmo', retry.requestId]));
|
||||
const metadata = task.metadata as any;
|
||||
await workflow.create(
|
||||
{
|
||||
key,
|
||||
title: task.title,
|
||||
department: 'CONTENT',
|
||||
operationMode: 'PREPARE',
|
||||
budgetLimitCents: 20,
|
||||
metadata,
|
||||
},
|
||||
{ id: 'pmo', role: 'Admin' },
|
||||
);
|
||||
expect((await post(retry)).body.state).toBe('GATE1_PENDING');
|
||||
});
|
||||
it('honors kill switches without creating work', async () => {
|
||||
const previous = await db.orchestrationKillSwitch.findUnique({
|
||||
where: { scope: 'GLOBAL' },
|
||||
});
|
||||
await db.orchestrationKillSwitch.upsert({
|
||||
where: { scope: 'GLOBAL' },
|
||||
create: {
|
||||
scope: 'GLOBAL',
|
||||
level: 'FREEZE_NEW_WORK',
|
||||
active: true,
|
||||
reason: 'disposable test',
|
||||
actorId: 'fixture',
|
||||
},
|
||||
update: { level: 'FREEZE_NEW_WORK', active: true },
|
||||
});
|
||||
try {
|
||||
expect((await post(dto())).status).toBe(403);
|
||||
} finally {
|
||||
if (previous)
|
||||
await db.orchestrationKillSwitch.update({
|
||||
where: { scope: 'GLOBAL' },
|
||||
data: { level: previous.level, active: previous.active },
|
||||
});
|
||||
else
|
||||
await db.orchestrationKillSwitch.delete({
|
||||
where: { scope: 'GLOBAL' },
|
||||
});
|
||||
}
|
||||
});
|
||||
it('never admits an editorial proposal into the deterministic tracer even after real independent Gate1 approval', async () => {
|
||||
const created = await post(dto());
|
||||
let t = await db.orchestrationTask.findUniqueOrThrow({
|
||||
where: { id: created.body.taskId },
|
||||
});
|
||||
t = await workflow.decide(
|
||||
t.id,
|
||||
1,
|
||||
{
|
||||
decision: 'APPROVED',
|
||||
expiresAt: new Date(Date.now() + 60000).toISOString(),
|
||||
expectedVersion: t.version,
|
||||
idempotencyKey: randomUUID(),
|
||||
},
|
||||
{ id: 'independent-fixture-owner', role: 'SUPER_ADMIN' },
|
||||
);
|
||||
t = await workflow.transition(
|
||||
t.id,
|
||||
{
|
||||
toState: 'READY',
|
||||
expectedVersion: t.version,
|
||||
idempotencyKey: randomUUID(),
|
||||
},
|
||||
{ id: 'pmo', role: 'Admin' },
|
||||
);
|
||||
const r = await fetch(base + '/internal/prepare-worker/claim', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
authorization: 'Bearer ' + token,
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ taskId: t.id }),
|
||||
});
|
||||
expect(r.status).toBe(403);
|
||||
expect(await db.orchestrationRun.count({ where: { taskId: t.id } })).toBe(
|
||||
0,
|
||||
);
|
||||
expect(
|
||||
await db.orchestrationBudgetLedger.count({ where: { taskId: t.id } }),
|
||||
).toBe(0);
|
||||
expect((await post({ ...dto(), requestId: randomUUID() })).status).toBe(
|
||||
201,
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -1,145 +0,0 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
ForbiddenException,
|
||||
Injectable,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
ArrayMaxSize,
|
||||
ArrayMinSize,
|
||||
IsArray,
|
||||
IsIn,
|
||||
IsInt,
|
||||
IsString,
|
||||
IsUUID,
|
||||
Matches,
|
||||
MaxLength,
|
||||
MinLength,
|
||||
} from 'class-validator';
|
||||
import { createHash } from 'crypto';
|
||||
import {
|
||||
PrepareWorkflowService,
|
||||
WorkflowIdentity,
|
||||
} from '../orchestration/prepare-workflow.service';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
export const EDITORIAL_PILOT_KIND = 'editorial-research-to-brief-v1';
|
||||
// No new implicit grant. Only an explicitly provisioned existing worker grant with
|
||||
// canonical id pmo is admitted; absence of that grant means denial, never fallback.
|
||||
export const EDITORIAL_PMO_WORKER_ID = 'pmo';
|
||||
export class EditorialProposalDto {
|
||||
@IsUUID() requestId!: string;
|
||||
@IsIn([EDITORIAL_PILOT_KIND]) pilotKind!: string;
|
||||
@IsIn(['PREPARE']) operationMode!: string;
|
||||
@IsIn(['staging']) environment!: string;
|
||||
@IsIn(['EDITORIAL_RESEARCH_TO_BRIEF']) scope!: string;
|
||||
@IsString() @MinLength(1) @MaxLength(180) researchTopic!: string;
|
||||
@IsArray()
|
||||
@ArrayMinSize(1)
|
||||
@ArrayMaxSize(10)
|
||||
@IsString({ each: true })
|
||||
@MinLength(1, { each: true })
|
||||
@MaxLength(500, { each: true })
|
||||
acceptanceCriteria!: string[];
|
||||
@IsString() @Matches(/^[a-f0-9]{40}$/) commitSha!: string;
|
||||
@IsInt() @IsIn([20]) budgetLimitCents!: number;
|
||||
}
|
||||
export function isEditorialProposal(task: any): boolean {
|
||||
return typeof task?.key === 'string' && task.key.startsWith('editorial-v1:');
|
||||
}
|
||||
@Injectable()
|
||||
export class EditorialProposalBridge {
|
||||
constructor(
|
||||
private readonly workflow: PrepareWorkflowService,
|
||||
private readonly db: PrismaService,
|
||||
) {}
|
||||
async submit(identity: WorkflowIdentity, dto: EditorialProposalDto) {
|
||||
if (
|
||||
identity.role !== 'WORKFLOW_WORKER' ||
|
||||
identity.id !== EDITORIAL_PMO_WORKER_ID
|
||||
)
|
||||
throw new ForbiddenException(
|
||||
'Explicit canonical PMO worker grant required',
|
||||
);
|
||||
if (
|
||||
!dto.researchTopic.trim() ||
|
||||
dto.acceptanceCriteria.some((s) => !s.trim())
|
||||
)
|
||||
throw new BadRequestException('Nonblank topic and criteria required');
|
||||
// Narrow server-owned author capability, NEVER a reviewer/SUPER_ADMIN identity.
|
||||
// The network credential still authenticates WORKFLOW_WORKER; only these two
|
||||
// fixed create/submit operations are delegated to core's existing Admin API.
|
||||
const author: WorkflowIdentity = { id: identity.id, role: 'Admin' };
|
||||
const key =
|
||||
'editorial-v1:' +
|
||||
createHash('sha256')
|
||||
.update(JSON.stringify([identity.id, dto.requestId]))
|
||||
.digest('hex');
|
||||
const scope = `${EDITORIAL_PILOT_KIND}\nTopic: ${dto.researchTopic}\nResearch and evidence-backed brief only. No publication, release, production changes or execution authorization.`;
|
||||
const proposal = {
|
||||
key,
|
||||
title: `Editorial research: ${dto.researchTopic}`,
|
||||
department: 'CONTENT',
|
||||
operationMode: 'PREPARE',
|
||||
budgetLimitCents: 20,
|
||||
metadata: {
|
||||
scope,
|
||||
acceptanceCriteria: dto.acceptanceCriteria,
|
||||
commitSha: dto.commitSha,
|
||||
},
|
||||
};
|
||||
let task = await this.db.orchestrationTask.findUnique({ where: { key } });
|
||||
if (!task) {
|
||||
try {
|
||||
task = await this.workflow.create(proposal, author);
|
||||
} catch (e) {
|
||||
if (!(e instanceof ConflictException)) throw e;
|
||||
task = await this.db.orchestrationTask.findUnique({ where: { key } });
|
||||
if (!task) throw e;
|
||||
}
|
||||
}
|
||||
const m = task!.metadata as any;
|
||||
if (
|
||||
task!.operationMode !== 'PREPARE' ||
|
||||
task!.department !== 'CONTENT' ||
|
||||
task!.budgetLimitCents !== 20 ||
|
||||
task!.title !== proposal.title ||
|
||||
m.createdBy !== identity.id ||
|
||||
m.workflowScope !== 'STAGE_PREPARE' ||
|
||||
m.environment !== 'staging' ||
|
||||
m.scope !== scope ||
|
||||
m.commitSha !== dto.commitSha ||
|
||||
JSON.stringify(m.acceptanceCriteria) !==
|
||||
JSON.stringify(dto.acceptanceCriteria)
|
||||
)
|
||||
throw new ConflictException('Editorial request payload mismatch');
|
||||
// Fixed idempotency token/version permits concurrent submits and recovery from
|
||||
// a crash between existing core create and transition transactions.
|
||||
if (task!.state === 'DRAFT')
|
||||
task = await this.workflow.transition(
|
||||
task!.id,
|
||||
{
|
||||
toState: 'GATE1_PENDING',
|
||||
expectedVersion: 1,
|
||||
idempotencyKey: `${key}:submit`,
|
||||
},
|
||||
author,
|
||||
);
|
||||
return {
|
||||
taskId: task!.id,
|
||||
key,
|
||||
pilotKind: EDITORIAL_PILOT_KIND,
|
||||
state: task!.state,
|
||||
version: task!.version,
|
||||
department: 'CONTENT',
|
||||
operationMode: 'PREPARE',
|
||||
budgetLimitCents: 20,
|
||||
metadata: {
|
||||
scope,
|
||||
acceptanceCriteria: m.acceptanceCriteria,
|
||||
environment: 'staging',
|
||||
commitSha: m.commitSha,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
# Bounded editorial approved-run contract
|
||||
|
||||
Only the explicitly configured WORKFLOW_WORKER grant `pmo` may use these routes under `/api/internal/prepare-worker` (the application's prefix supplies `/api`). No new credential or reviewer grant is provisioned here.
|
||||
|
||||
- `GET editorial-proposals/:taskId`: exact bridge-created proposal status and, after ingestion, canonical immutable brief bytes/digest. No lease token, raw events, reviewer identities or arbitrary task projection.
|
||||
- `POST editorial-claim`: `{ "taskId": "<uuid>" }`. Requires exact bridge key/title/scope/metadata plus original PMO creation/submission events, READY/RUNNING, latest independent unexpired approved Gate1, no blocking kill switch. Core claim runs inside the same admission/task-lock transaction, preserving reservations, ownership and 30-second leases. Competing claim returns null lease. Expired attempts retain unknown reservation; a 20-cent task cannot reserve a second 20 cents.
|
||||
- `POST heartbeat`: existing `{taskId,runId,leaseToken}` while working. Extends lease by 30 seconds. Result rechecks Gate1 independently; heartbeat is not approval extension.
|
||||
- `POST editorial-result`: `{taskId,runId,leaseToken,brief}`. No cost, QA pass, role, URI or caller identity fields accepted.
|
||||
|
||||
`brief` has exactly:
|
||||
|
||||
```json
|
||||
{
|
||||
"researchDate": "YYYY-MM-DD",
|
||||
"inputAvailability": [{"input":"input name","status":"AVAILABLE|UNAVAILABLE|PARTIAL","details":"availability explanation"}],
|
||||
"findings": [{"sourceUrl":"https://public-source.example.org/path","sourceDate":"YYYY-MM-DD","finding":"source-backed finding"}],
|
||||
"candidates": [
|
||||
{"title":"Distinct candidate A","score":80,"rationale":"Reason","sourceUrls":["https://public-source.example.org/path"]},
|
||||
{"title":"Distinct candidate B","score":70,"rationale":"Reason","sourceUrls":["https://public-source.example.org/path"]},
|
||||
{"title":"Distinct candidate C","score":60,"rationale":"Reason","sourceUrls":["https://public-source.example.org/path"]}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Limits: canonical brief UTF-8 <=24,576 bytes; 1..10 inputs (name 120/details 500 characters); 1..20 findings (HTTPS DNS URL 2000/finding 1000 characters); 3..10 unique candidates (title 180/rationale 1000, integer score 0..100, 1..10 source references all in findings). Dates must be valid calendar dates, not future; source dates cannot exceed research date. Unknown nested keys rejected. Example URLs above illustrate shape only, not real research evidence.
|
||||
|
||||
Canonical artifact kind `EDITORIAL_RESEARCH_BRIEF` is stored in append-only artifact metadata with SHA-256 and a URN; authenticated status retrieves actual bytes. No filesystem/external URL persistence dependency. Transaction atomically checks task/approval/lease/run/reservation, inserts evidence/event, closes outbox and moves task to VERIFYING. Run is AWAITING_QA, quality PENDING_INDEPENDENT_QA; no attestation, success, publication, Gate2 or release authorization is created. Unknown cost retains RESERVE 20 without RECONCILE. Replay after lease closure is rejected, not silently overwritten.
|
||||
|
||||
Validation checks structure/source references, not source truth, retrieval, novelty, scoring quality, SEO value or factuality. No research/article/LLM execution is performed by this contract. Independent QA ingestion is deliberately not implemented; generic tracer/tech and Stage evidence cannot certify this evidence.
|
||||
|
||||
Verification: disposable Node22 + PostgreSQL16, source/dependency donors read-only and privately copied, generated Prisma client. New suite observed RED (missing routes) then GREEN. Final combined bridge/execution/tech/transport suites: 22/22; `npm run build`: exit 0. Full historical migrations fail on fresh database because pre-existing blog migration expects missing blogs table; final tests instead applied only real orchestration core, immutable-ledger and outbox migrations directly to empty disposable DB, including append-only triggers. No live database, proposal approval, cron, deployment or publication used.
|
||||
@@ -1,407 +0,0 @@
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { createHash, randomUUID } from 'crypto';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { PrepareWorkflowService } from '../orchestration/prepare-workflow.service';
|
||||
import { WorkerTransportModule, tracerReport } from './worker-transport.module';
|
||||
|
||||
(process.env.EDITORIAL_EXECUTION_DB === '1' ? describe : describe.skip)(
|
||||
'editorial approved-run real TCP/PostgreSQL',
|
||||
() => {
|
||||
const db = new PrismaClient();
|
||||
const token = 'disposable-editorial-pmo-'.repeat(3),
|
||||
other = 'disposable-other-editor-'.repeat(3),
|
||||
ci = 'disposable-editorial-ci-'.repeat(3);
|
||||
const sha = (s: string) => createHash('sha256').update(s).digest('hex');
|
||||
let app: INestApplication, base: string, workflow: PrepareWorkflowService;
|
||||
const brief = () => ({
|
||||
researchDate: '2026-10-24',
|
||||
inputAvailability: [
|
||||
{
|
||||
input: 'search console',
|
||||
status: 'UNAVAILABLE',
|
||||
details: 'No authenticated property supplied',
|
||||
},
|
||||
],
|
||||
findings: [
|
||||
{
|
||||
sourceUrl: 'https://www.who.int/news',
|
||||
sourceDate: '2026-10-20',
|
||||
finding: 'Fixture finding for contract testing, not real research',
|
||||
},
|
||||
],
|
||||
candidates: [0, 1, 2].map((i) => ({
|
||||
title: 'Fixture candidate ' + i,
|
||||
score: 80 - i,
|
||||
rationale: 'Contract fixture only',
|
||||
sourceUrls: ['https://www.who.int/news'],
|
||||
})),
|
||||
});
|
||||
beforeAll(async () => {
|
||||
const m = await Test.createTestingModule({
|
||||
imports: [
|
||||
WorkerTransportModule.register({
|
||||
enabled: true,
|
||||
grants: [
|
||||
{
|
||||
id: 'pmo',
|
||||
role: 'WORKFLOW_WORKER',
|
||||
credentialSha256: sha(token),
|
||||
},
|
||||
{
|
||||
id: 'other',
|
||||
role: 'WORKFLOW_WORKER',
|
||||
credentialSha256: sha(other),
|
||||
},
|
||||
{ id: 'ci', role: 'STAGE_CI', credentialSha256: sha(ci) },
|
||||
],
|
||||
workerIds: ['pmo', 'other'],
|
||||
stageCiIds: ['ci'],
|
||||
artifactDirectory: '/work/tmp/editorial-execution',
|
||||
artifactBaseUrl:
|
||||
'https://isolated.invalid/internal/prepare-worker/artifacts',
|
||||
}),
|
||||
],
|
||||
})
|
||||
.overrideProvider(PrismaService)
|
||||
.useValue(db)
|
||||
.compile();
|
||||
app = m.createNestApplication();
|
||||
await app.listen(0, '127.0.0.1');
|
||||
base = await app.getUrl();
|
||||
workflow = app.get(PrepareWorkflowService);
|
||||
});
|
||||
afterAll(async () => {
|
||||
await app?.close();
|
||||
await db.$disconnect();
|
||||
});
|
||||
async function request(route: string, body?: any, secret = token) {
|
||||
const r = await fetch(base + '/internal/prepare-worker/' + route, {
|
||||
method: body === undefined ? 'GET' : 'POST',
|
||||
headers: {
|
||||
authorization: 'Bearer ' + secret,
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
||||
});
|
||||
return { status: r.status, body: await r.json() };
|
||||
}
|
||||
async function proposal(ready = true, ttl = 60000) {
|
||||
const r = await request('editorial-proposals', {
|
||||
requestId: randomUUID(),
|
||||
pilotKind: 'editorial-research-to-brief-v1',
|
||||
operationMode: 'PREPARE',
|
||||
environment: 'staging',
|
||||
scope: 'EDITORIAL_RESEARCH_TO_BRIEF',
|
||||
researchTopic: 'Disposable research contract',
|
||||
acceptanceCriteria: ['Evidence with dates'],
|
||||
commitSha: 'a'.repeat(40),
|
||||
budgetLimitCents: 20,
|
||||
});
|
||||
expect(r.status).toBe(201);
|
||||
let t = await db.orchestrationTask.findUniqueOrThrow({
|
||||
where: { id: r.body.taskId },
|
||||
});
|
||||
if (ready) {
|
||||
t = await workflow.decide(
|
||||
t.id,
|
||||
1,
|
||||
{
|
||||
decision: 'APPROVED',
|
||||
expiresAt: new Date(Date.now() + ttl).toISOString(),
|
||||
expectedVersion: t.version,
|
||||
idempotencyKey: randomUUID(),
|
||||
},
|
||||
{ id: 'independent-disposable-reviewer', role: 'SUPER_ADMIN' },
|
||||
);
|
||||
// Gate decision now atomically queues READY; no second queue transition.
|
||||
}
|
||||
return t;
|
||||
}
|
||||
async function lease(ttl = 60000) {
|
||||
const t = await proposal(true, ttl);
|
||||
const r = await request('editorial-claim', { taskId: t.id });
|
||||
expect(r.status).toBe(201);
|
||||
expect(r.body.lease).toBeTruthy();
|
||||
const l = r.body.lease;
|
||||
return {
|
||||
t,
|
||||
l,
|
||||
b: { taskId: t.id, runId: l.runId, leaseToken: l.leaseToken },
|
||||
};
|
||||
}
|
||||
it('stops a held run through authenticated failure API, fences late writes and preserves unknown reservation', async () => {
|
||||
const {t,l,b}=await lease();
|
||||
const d={...b,expectedVersion: t.version+1,idempotencyKey:'stop:'+l.runId,reason:'Disposable subprocess failure'};
|
||||
expect((await request('editorial-failure',{...d,leaseToken:randomUUID()})).status).toBe(403);
|
||||
expect((await request('editorial-failure',d,other)).status).toBe(403);
|
||||
expect((await request('editorial-failure',{...d,actualCents:0})).status).toBe(400);
|
||||
const stopped=await Promise.all([request('editorial-failure',d),request('editorial-failure',d)]);
|
||||
expect(stopped.map(x=>x.status)).toEqual([201,201]);
|
||||
expect(stopped[0].body).toMatchObject({state:'FAILED',retainedReservationCents:20});
|
||||
expect((await request('heartbeat',b)).status).toBe(403);
|
||||
expect((await request('editorial-result',{...b,brief:brief()})).status).toBe(403);
|
||||
expect((await request('editorial-claim',{taskId:t.id})).status).toBe(403);
|
||||
expect(await db.orchestrationArtifact.count({where:{runId:l.runId}})).toBe(0);
|
||||
expect(await db.orchestrationAttestation.count({where:{runId:l.runId}})).toBe(0);
|
||||
expect((await db.orchestrationBudgetLedger.findMany({where:{taskId:t.id}})).map(x=>[x.kind,x.amountCents])).toEqual([['RESERVE',20]]);
|
||||
});
|
||||
it('recovers an expired run only as independent reviewer and fences original token', async () => {
|
||||
const {t,l,b}=await lease();
|
||||
const d={runId:l.runId,expectedVersion:t.version+1,idempotencyKey:'stale-stop:'+l.runId,reason:'Disposable ended process'};
|
||||
await expect(workflow.editorialFailure(t.id,d,{id:'reviewer',role:'SUPER_ADMIN'})).rejects.toThrow();
|
||||
await db.prepareWorkflowOutbox.update({where:{taskId:t.id},data:{leaseUntil:new Date(Date.now()-1000)}});
|
||||
const results=await Promise.all([workflow.editorialFailure(t.id,d,{id:'reviewer',role:'SUPER_ADMIN'}),workflow.editorialFailure(t.id,d,{id:'reviewer',role:'SUPER_ADMIN'})]);
|
||||
expect(results.map(x=>x.state)).toEqual(['FAILED','FAILED']);
|
||||
expect((await request('heartbeat',b)).status).toBe(403);
|
||||
expect((await request('editorial-result',{...b,brief:brief()})).status).toBe(403);
|
||||
expect(await db.orchestrationEvent.count({where:{taskId:t.id,eventType:'EDITORIAL_RUN_STOPPED'}})).toBe(1);
|
||||
expect(await db.orchestrationBudgetLedger.count({where:{taskId:t.id}})).toBe(1);
|
||||
});
|
||||
it('projects only owned exact proposals and refuses pending dispatch', async () => {
|
||||
const t = await proposal(false);
|
||||
const p = await request('editorial-proposals/' + t.id);
|
||||
expect(p.status).toBe(200);
|
||||
expect(p.body).toMatchObject({
|
||||
taskId: t.id,
|
||||
state: 'GATE1_PENDING',
|
||||
qaStatus: 'PENDING_INDEPENDENT_QA',
|
||||
});
|
||||
expect(JSON.stringify(p.body)).not.toMatch(
|
||||
/leaseToken|credential|createdBy|fingerprint/,
|
||||
);
|
||||
expect((await request('editorial-claim', { taskId: t.id })).status).toBe(
|
||||
403,
|
||||
);
|
||||
expect(
|
||||
(await request('editorial-proposals/' + t.id, undefined, other)).status,
|
||||
).toBe(403);
|
||||
expect(
|
||||
(await request('editorial-proposals/' + randomUUID())).status,
|
||||
).toBe(404);
|
||||
});
|
||||
it('races claims, ingests immutable bounded brief, never self-attests QA or reconciles unknown cost', async () => {
|
||||
const t = await proposal();
|
||||
const claims = await Promise.all([
|
||||
request('editorial-claim', { taskId: t.id }),
|
||||
request('editorial-claim', { taskId: t.id }),
|
||||
]);
|
||||
const leases = claims.filter((x) => x.body.lease);
|
||||
expect(leases).toHaveLength(1);
|
||||
const l = leases[0].body.lease,
|
||||
b = { taskId: t.id, runId: l.runId, leaseToken: l.leaseToken };
|
||||
expect((await request('heartbeat', b)).status).toBe(201);
|
||||
expect((await request('claim', { taskId: t.id })).status).toBe(403);
|
||||
expect((await request('tech-claim', { taskId: t.id })).status).toBe(403);
|
||||
expect(
|
||||
(await request('complete', { ...b, report: tracerReport(l) })).status,
|
||||
).toBe(403);
|
||||
const r = await request('editorial-result', { ...b, brief: brief() });
|
||||
expect(r.status).toBe(201);
|
||||
expect(r.body).toMatchObject({
|
||||
state: 'VERIFYING',
|
||||
qaStatus: 'PENDING_INDEPENDENT_QA',
|
||||
retainedReservationCents: 20,
|
||||
});
|
||||
const p = await request('editorial-proposals/' + t.id);
|
||||
expect(p.body.artifact.brief).toEqual(brief());
|
||||
expect(sha(p.body.artifact.bytes)).toBe(r.body.artifactSha256);
|
||||
await expect(
|
||||
db.orchestrationArtifact.updateMany({
|
||||
where: { runId: l.runId },
|
||||
data: { metadata: { bytes: 'tampered' } },
|
||||
}),
|
||||
).rejects.toThrow('append-only');
|
||||
expect(JSON.parse(p.body.artifact.bytes)).toMatchObject({
|
||||
taskId: t.id,
|
||||
runId: l.runId,
|
||||
brief: brief(),
|
||||
});
|
||||
expect(
|
||||
await db.orchestrationAttestation.count({ where: { runId: l.runId } }),
|
||||
).toBe(0);
|
||||
expect(
|
||||
(
|
||||
await db.orchestrationRun.findUniqueOrThrow({
|
||||
where: { id: l.runId },
|
||||
})
|
||||
).status,
|
||||
).toBe('AWAITING_QA');
|
||||
expect(
|
||||
(
|
||||
await db.orchestrationBudgetLedger.findMany({
|
||||
where: { taskId: t.id },
|
||||
})
|
||||
).map((x) => [x.kind, x.amountCents]),
|
||||
).toEqual([['RESERVE', 20]]);
|
||||
expect(
|
||||
(await request('editorial-result', { ...b, brief: brief() })).status,
|
||||
).toBe(403);
|
||||
expect(
|
||||
(
|
||||
await request(
|
||||
'stage-evidence',
|
||||
{
|
||||
taskId: t.id,
|
||||
runId: l.runId,
|
||||
commitSha: l.commitSha,
|
||||
sha256: r.body.artifactSha256,
|
||||
evidenceSha256: 'b'.repeat(64),
|
||||
uri: 'https://isolated.invalid/fixture',
|
||||
},
|
||||
ci,
|
||||
)
|
||||
).status,
|
||||
).toBe(403);
|
||||
});
|
||||
it('rejects impersonation, unsafe or incomplete briefs, and expired ownership without artifacts', async () => {
|
||||
const { l, b } = await lease();
|
||||
for (const secret of [other, ci])
|
||||
expect(
|
||||
(await request('editorial-result', { ...b, brief: brief() }, secret))
|
||||
.status,
|
||||
).toBe(403);
|
||||
for (const patch of [
|
||||
{ actualCents: 0 },
|
||||
{ qa: { passed: true } },
|
||||
{ actorId: 'pmo' },
|
||||
])
|
||||
expect(
|
||||
(
|
||||
await request('editorial-result', {
|
||||
...b,
|
||||
brief: brief(),
|
||||
...patch,
|
||||
})
|
||||
).status,
|
||||
).toBe(400);
|
||||
for (const bad of [
|
||||
{ ...brief(), candidates: brief().candidates.slice(0, 2) },
|
||||
{ ...brief(), researchDate: '2026-02-30' },
|
||||
{
|
||||
...brief(),
|
||||
findings: [
|
||||
{
|
||||
sourceUrl: 'http://127.0.0.1',
|
||||
sourceDate: '2026-10-20',
|
||||
finding: 'x',
|
||||
},
|
||||
],
|
||||
},
|
||||
{ ...brief(), inputAvailability: [] },
|
||||
{
|
||||
...brief(),
|
||||
candidates: brief().candidates.map((c) => ({ ...c, score: 101 })),
|
||||
},
|
||||
{ ...brief(), article: 'not allowed' },
|
||||
])
|
||||
expect(
|
||||
(await request('editorial-result', { ...b, brief: bad })).status,
|
||||
).toBe(400);
|
||||
expect(
|
||||
(
|
||||
await request('editorial-result', {
|
||||
...b,
|
||||
leaseToken: randomUUID(),
|
||||
brief: brief(),
|
||||
})
|
||||
).status,
|
||||
).toBe(403);
|
||||
await db.prepareWorkflowOutbox.update({
|
||||
where: { taskId: b.taskId },
|
||||
data: { leaseUntil: new Date(Date.now() - 1000) },
|
||||
});
|
||||
expect(
|
||||
(await request('editorial-result', { ...b, brief: brief() })).status,
|
||||
).toBe(403);
|
||||
expect(
|
||||
(await request('editorial-claim', { taskId: b.taskId })).status,
|
||||
).toBe(403);
|
||||
expect(
|
||||
await db.orchestrationArtifact.count({ where: { runId: l.runId } }),
|
||||
).toBe(0);
|
||||
expect(
|
||||
await db.orchestrationBudgetLedger.count({
|
||||
where: { taskId: b.taskId },
|
||||
}),
|
||||
).toBe(1);
|
||||
});
|
||||
it('enforces cancellation, approval expiry and kill switch inside ingestion', async () => {
|
||||
for (const mode of ['cancel', 'approval', 'kill']) {
|
||||
const { l, b } = await lease(mode === 'approval' ? 1500 : 60000);
|
||||
if (mode === 'cancel')
|
||||
await db.orchestrationTask.update({
|
||||
where: { id: b.taskId },
|
||||
data: { state: 'CANCELLED' },
|
||||
});
|
||||
if (mode === 'approval')
|
||||
await new Promise((resolve) => setTimeout(resolve, 1600));
|
||||
if (mode === 'kill')
|
||||
await db.orchestrationKillSwitch.upsert({
|
||||
where: { scope: 'GLOBAL' },
|
||||
create: {
|
||||
scope: 'GLOBAL',
|
||||
level: 'STOP_AGENTS',
|
||||
active: true,
|
||||
reason: 'disposable execution test',
|
||||
actorId: 'fixture',
|
||||
},
|
||||
update: { level: 'STOP_AGENTS', active: true },
|
||||
});
|
||||
try {
|
||||
expect(
|
||||
(await request('editorial-result', { ...b, brief: brief() }))
|
||||
.status,
|
||||
).toBe(403);
|
||||
expect(
|
||||
await db.orchestrationArtifact.count({ where: { runId: l.runId } }),
|
||||
).toBe(0);
|
||||
} finally {
|
||||
if (mode === 'kill')
|
||||
await db.orchestrationKillSwitch.update({
|
||||
where: { scope: 'GLOBAL' },
|
||||
data: { active: false },
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
it('rejects arbitrary CONTENT tasks and forged stored scope/provenance', async () => {
|
||||
const raw = await workflow.create(
|
||||
{
|
||||
key: 'arbitrary-' + randomUUID(),
|
||||
title: 'arbitrary',
|
||||
department: 'CONTENT',
|
||||
operationMode: 'PREPARE',
|
||||
budgetLimitCents: 20,
|
||||
metadata: {
|
||||
scope: 'editorial-research-to-brief-v1',
|
||||
commitSha: 'a'.repeat(40),
|
||||
acceptanceCriteria: ['x'],
|
||||
},
|
||||
},
|
||||
{ id: 'pmo', role: 'Admin' },
|
||||
);
|
||||
expect((await request('editorial-proposals/' + raw.id)).status).toBe(404);
|
||||
expect(
|
||||
(await request('editorial-claim', { taskId: raw.id })).status,
|
||||
).toBe(404);
|
||||
const t = await proposal();
|
||||
await db.orchestrationTask.update({
|
||||
where: { id: t.id },
|
||||
data: {
|
||||
metadata: {
|
||||
...(t.metadata as any),
|
||||
scope: 'editorial-research-to-brief-v1\nforged',
|
||||
},
|
||||
},
|
||||
});
|
||||
expect((await request('editorial-claim', { taskId: t.id })).status).toBe(
|
||||
404,
|
||||
);
|
||||
expect(await db.orchestrationRun.count({ where: { taskId: t.id } })).toBe(
|
||||
0,
|
||||
);
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -1,536 +0,0 @@
|
||||
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,
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
import { EditorialExecutionService } from './editorial-execution';
|
||||
describe('editorial expiry read/claim consistency',()=>{
|
||||
it('rejects the actual expired claim while status explains blocked READY',async()=>{
|
||||
jest.useFakeTimers().setSystemTime(new Date('2026-10-24T09:15:00Z'));
|
||||
const t={id:'565ace1f-8cfc-4447-b978-66c36e7b767b',state:'READY',version:4,metadata:{commitSha:'a'.repeat(40)}};
|
||||
const tx:any={$queryRaw:jest.fn().mockResolvedValue([]),orchestrationApprovalDecision:{findFirst:jest.fn().mockResolvedValue({decision:'APPROVED',actorId:'owner',expiresAt:new Date('2026-10-24T06:37:00Z')})},orchestrationArtifact:{findMany:jest.fn().mockResolvedValue([])}};
|
||||
const s=new EditorialExecutionService({$transaction:async(fn:any)=>fn(tx)} as any);
|
||||
jest.spyOn(s as any,'scope').mockResolvedValue(t);
|
||||
const a={id:'pmo',role:'WORKFLOW_WORKER'};
|
||||
await expect(s.claim(a,{taskId:t.id})).rejects.toMatchObject({status:403,message:'Valid independent Gate1 required'});
|
||||
await expect(s.status(a,t.id)).resolves.toMatchObject({state:'READY',version:4,displayState:'GATE1_RENEWAL_REQUIRED',gate1Validity:'BLOCKED',artifact:null});
|
||||
jest.useRealTimers();
|
||||
});
|
||||
});
|
||||
@@ -1,26 +0,0 @@
|
||||
# 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/<artifactSha256>` 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.
|
||||
@@ -1,74 +0,0 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
@@ -1,35 +0,0 @@
|
||||
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)});
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
import { constants, openSync, closeSync, fstatSync, readSync } from 'fs';
|
||||
import { WorkerTransportConfig, WorkerTransportModule } from './worker-transport.module';
|
||||
|
||||
export const PREPARE_WORKER_CONFIG_PATH = '/run/secrets/prepare_worker_grants';
|
||||
export const PREPARE_WORKER_ARTIFACT_DIRECTORY = '/var/lib/canina/prepare-artifacts';
|
||||
const MAX_BYTES = 16384;
|
||||
const fail = () => { throw new Error('Prepare worker startup configuration rejected'); };
|
||||
function exact(value: any, keys: string[]): boolean {
|
||||
return value !== null && typeof value === 'object' && !Array.isArray(value) &&
|
||||
Object.keys(value).length === keys.length && keys.every(k => Object.hasOwn(value, k));
|
||||
}
|
||||
|
||||
/** Fixed-path, descriptor-pinned, bounded startup read; never logs file contents. */
|
||||
export function loadPrepareWorkerConfig(): WorkerTransportConfig {
|
||||
let dir: number | undefined, file: number | undefined;
|
||||
try {
|
||||
dir = openSync('/', constants.O_RDONLY | constants.O_DIRECTORY);
|
||||
for (const part of ['run', 'secrets']) {
|
||||
const next = openSync(`/proc/self/fd/${dir}/${part}`, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
|
||||
closeSync(dir); dir = next;
|
||||
if ((fstatSync(dir).mode & 0o022) !== 0) fail();
|
||||
}
|
||||
file = openSync(`/proc/self/fd/${dir}/prepare_worker_grants`, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
||||
const stat = fstatSync(file);
|
||||
if (!stat.isFile() || stat.nlink !== 1 || (stat.mode & 0o022) !== 0 || stat.size > MAX_BYTES ||
|
||||
(stat.uid !== 0 && stat.uid !== process.getuid!())) fail();
|
||||
const bytes = Buffer.alloc(MAX_BYTES + 1);
|
||||
let length = 0, n: number;
|
||||
do { n = readSync(file, bytes, length, bytes.length - length, length); length += n; }
|
||||
while (n > 0 && length < bytes.length);
|
||||
if (length > MAX_BYTES) fail();
|
||||
const input = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes.subarray(0, length)));
|
||||
if (!exact(input, ['grants', 'workerIds', 'stageCiIds', 'artifactBaseUrl']) ||
|
||||
!Array.isArray(input.grants) || input.grants.length < 1 || input.grants.length > 32 ||
|
||||
!Array.isArray(input.workerIds) || !Array.isArray(input.stageCiIds) ||
|
||||
input.workerIds.length < 1 || input.stageCiIds.length < 1 ||
|
||||
input.workerIds.length + input.stageCiIds.length !== input.grants.length ||
|
||||
typeof input.artifactBaseUrl !== 'string' || input.artifactBaseUrl.length > 2048 ||
|
||||
input.grants.some((g: any) => !exact(g, ['id', 'role', 'credentialSha256']))) fail();
|
||||
// Transport registration performs shared role, SHA256, ID, overlap and HTTPS validation.
|
||||
const config: WorkerTransportConfig = { ...input, enabled: true, artifactDirectory: PREPARE_WORKER_ARTIFACT_DIRECTORY };
|
||||
WorkerTransportModule.register(config); // Reuse the real transport's reviewed validation, no IO.
|
||||
config.grants.forEach(Object.freeze);
|
||||
Object.freeze(config.grants); Object.freeze(config.workerIds); Object.freeze(config.stageCiIds);
|
||||
return Object.freeze(config);
|
||||
} catch { return fail(); }
|
||||
finally { if (file !== undefined) closeSync(file); if (dir !== undefined) closeSync(dir); }
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
# Prepare worker startup boundary
|
||||
|
||||
Parent integration API (AppModule remains untouched here):
|
||||
|
||||
```ts
|
||||
import { registerPrepareWorker } from './orchestration-worker/worker-registration';
|
||||
// In AppModule imports:
|
||||
...registerPrepareWorker(),
|
||||
```
|
||||
|
||||
Only literal `PREPARE_WORKER_ENABLED=true` registers the transport. Missing or other values register nothing (404 routes), do not read configuration, and do not instantiate Prisma. Enabled malformed/missing configuration throws synchronously before Nest starts, with one generic error and no secret logging.
|
||||
|
||||
The fixed mount `/run/secrets/prepare_worker_grants` contains JSON with exactly `grants`, `workerIds`, `stageCiIds`, `artifactBaseUrl`. Each grant contains exactly `id`, `role` (`WORKFLOW_WORKER` or `STAGE_CI`), `credentialSha256` (64 lowercase hexadecimal characters; never the bearer credential). Require at least one worker and CI, distinct role-bound IDs/digests, exact allowlist coverage, and at most 32 grants. No enabled flag, directory, token, user, or request-derived fields accepted in JSON.
|
||||
|
||||
`artifactBaseUrl` must be explicitly reviewed by the operator: canonical public-DNS HTTPS origin plus `/internal/prepare-worker/artifacts`, no credentials, IP, alternate port, query or fragment. Shared transport validation is authoritative. This loader does not fetch the URL or perform DNS resolution. Directory is always `/var/lib/canina/prepare-artifacts`.
|
||||
|
||||
Descriptor-relative Linux `/proc/self/fd` traversal refuses symlink directories/files, nonregular files, hardlinks, files over 16 KiB, group/world writable files/directories, and foreign-owned files (root/runtime UID only). Reads are bounded even on concurrent growth. Configuration is frozen recursively and transport takes its own frozen snapshot. Trusted operator owns mounted configuration; edits require restart and review.
|
||||
|
||||
`compose.prepare-worker.fragment.yml` is an extension fragment, not a deployable compose file. Parent must merge its `x-prepare-worker-backend` into the correct reviewed Stage backend service and merge `secrets`. Compose bind-backed secrets may ignore mode/UID declarations: provision actual host grants file as root/runtime-owned mode 0400 and artifact directory runtime-owned mode 0700 first. No mounts or live configuration are created by this change.
|
||||
|
||||
Tests deliberately use the fixed `/run/secrets` path. Run ONLY in a fresh disposable container with source and donor dependencies read-only, private dependency copy, and private `prisma generate`; never on a live service. Tests need a disposable Linux root filesystem and do not require DB credentials. Registration test uses a real Nest HTTP listener to prove disabled routes are absent. Existing DB-backed transport tracer remains separately gated by `WORKER_TRANSPORT_DB=1`.
|
||||
@@ -1,84 +0,0 @@
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { writeFileSync, chmodSync, symlinkSync, mkdirSync, rmSync } from 'fs';
|
||||
import { execFileSync } from 'child_process';
|
||||
import { registerPrepareWorker } from './worker-registration';
|
||||
import { loadPrepareWorkerConfig } from './worker-config';
|
||||
import { linkSync } from 'fs';
|
||||
|
||||
|
||||
describe('reviewed prepare-worker startup registration', () => {
|
||||
const path = '/run/secrets/prepare_worker_grants';
|
||||
const valid = () => ({ grants: [{ id: 'worker', role: 'WORKFLOW_WORKER', credentialSha256: 'a'.repeat(64) }, { id: 'ci', role: 'STAGE_CI', credentialSha256: 'b'.repeat(64) }], workerIds: ['worker'], stageCiIds: ['ci'], artifactBaseUrl: 'https://stage.example.com/internal/prepare-worker/artifacts' });
|
||||
it('loader itself rejects malformed SHA grants', () => {
|
||||
mkdirSync('/run/secrets', { recursive: true });
|
||||
const c = valid(); c.grants[0].credentialSha256 = 'secret-not-a-sha';
|
||||
writeFileSync(path, JSON.stringify(c), { mode: 0o400 });
|
||||
try { expect(() => loadPrepareWorkerConfig()).toThrow('Prepare worker startup configuration rejected'); }
|
||||
finally { rmSync(path, { force: true }); }
|
||||
});
|
||||
it.each(['1', 'TRUE', 'yes', ' true', 'false', ''])('does not opt in for %s', value => {
|
||||
expect(registerPrepareWorker({ PREPARE_WORKER_ENABLED: value })).toEqual([]);
|
||||
});
|
||||
it.each(['missing', 'symlink', 'directory', 'fifo', 'oversize', 'group-write', 'world-write', 'hardlink'])('fails startup on unsafe file: %s', kind => {
|
||||
mkdirSync('/run/secrets', { recursive: true });
|
||||
const other = '/run/secrets/test-other';
|
||||
try {
|
||||
if (kind === 'fifo') execFileSync('mkfifo', [path]);
|
||||
else if (kind === 'directory') mkdirSync(path);
|
||||
else if (kind === 'symlink') symlinkSync(other, path);
|
||||
else if (kind !== 'missing') {
|
||||
writeFileSync(path, kind === 'oversize' ? ' '.repeat(16385) : JSON.stringify(valid()), { mode: 0o400 });
|
||||
if (kind === 'group-write') chmodSync(path, 0o420);
|
||||
if (kind === 'world-write') chmodSync(path, 0o402);
|
||||
if (kind === 'hardlink') linkSync(path, other);
|
||||
}
|
||||
expect(() => registerPrepareWorker({ PREPARE_WORKER_ENABLED: 'true' })).toThrow('Prepare worker startup configuration rejected');
|
||||
} finally { rmSync(path, { recursive: true, force: true }); rmSync(other, { force: true }); }
|
||||
});
|
||||
it.each(['extra', 'raw-token', 'wrong-role', 'overlap', 'duplicate-sha', 'missing-grant', 'http', 'credentials', 'localhost', 'query', 'bad-json', 'null'])('rejects unreviewed JSON: %s without leaking contents or logging', kind => {
|
||||
mkdirSync('/run/secrets', { recursive: true });
|
||||
const c: any = valid();
|
||||
if (kind === 'extra') c.artifactDirectory = '/user-selected';
|
||||
if (kind === 'raw-token') c.grants[0].token = 'DO_NOT_LOG_THIS_SECRET';
|
||||
if (kind === 'wrong-role') c.grants[0].role = 'Admin';
|
||||
if (kind === 'overlap') c.stageCiIds = ['worker'];
|
||||
if (kind === 'duplicate-sha') c.grants[1].credentialSha256 = c.grants[0].credentialSha256;
|
||||
if (kind === 'missing-grant') c.workerIds.push('unbound');
|
||||
if (kind === 'http') c.artifactBaseUrl = 'http://stage.example.com/internal/prepare-worker/artifacts';
|
||||
if (kind === 'credentials') c.artifactBaseUrl = 'https://DO_NOT_LOG_THIS_SECRET@stage.example.com/internal/prepare-worker/artifacts';
|
||||
if (kind === 'localhost') c.artifactBaseUrl = 'https://127.0.0.1/internal/prepare-worker/artifacts';
|
||||
if (kind === 'query') c.artifactBaseUrl += '?secret=DO_NOT_LOG_THIS_SECRET';
|
||||
writeFileSync(path, kind === 'bad-json' ? 'DO_NOT_LOG_THIS_SECRET' : kind === 'null' ? 'null' : JSON.stringify(c), { mode: 0o400 });
|
||||
const logs = [jest.spyOn(console, 'log'), jest.spyOn(console, 'warn'), jest.spyOn(console, 'error')];
|
||||
try {
|
||||
expect(() => registerPrepareWorker({ PREPARE_WORKER_ENABLED: 'true' })).toThrow(/^Prepare worker startup configuration rejected$/);
|
||||
logs.forEach(log => expect(log).not.toHaveBeenCalled());
|
||||
} finally { logs.forEach(log => log.mockRestore()); rmSync(path, { force: true }); }
|
||||
});
|
||||
it('defaults to no module/no config IO and exposes no worker routes', async () => {
|
||||
expect(registerPrepareWorker({})).toEqual([]);
|
||||
expect(registerPrepareWorker({ PREPARE_WORKER_ENABLED: 'false' })).toEqual([]);
|
||||
const module = await Test.createTestingModule({ imports: registerPrepareWorker({}) }).compile();
|
||||
const app = module.createNestApplication();
|
||||
await app.listen(0, '127.0.0.1');
|
||||
try {
|
||||
const response = await fetch((await app.getUrl()) + '/internal/prepare-worker/claim', { method: 'POST' });
|
||||
expect(response.status).toBe(404);
|
||||
} finally { await app.close(); }
|
||||
});
|
||||
it('loads immutable reviewed JSON into the real transport registration', async () => {
|
||||
mkdirSync('/run/secrets', { recursive: true });
|
||||
const file = '/run/secrets/prepare_worker_grants';
|
||||
const config = { grants: [{ id: 'worker', role: 'WORKFLOW_WORKER', credentialSha256: 'a'.repeat(64) }, { id: 'ci', role: 'STAGE_CI', credentialSha256: 'b'.repeat(64) }], workerIds: ['worker'], stageCiIds: ['ci'], artifactBaseUrl: 'https://stage.example.com/internal/prepare-worker/artifacts' };
|
||||
writeFileSync(file, JSON.stringify(config), { mode: 0o400 });
|
||||
try {
|
||||
const [registered] = registerPrepareWorker({ PREPARE_WORKER_ENABLED: 'true' });
|
||||
expect(registered.module.name).toBe('WorkerTransportModule');
|
||||
expect(registered.controllers?.length).toBe(1);
|
||||
const snapshot = (registered.providers as any[]).find(p => p.useValue?.artifactDirectory)?.useValue;
|
||||
expect(snapshot).toEqual({ ...config, enabled: true, artifactDirectory: '/var/lib/canina/prepare-artifacts' });
|
||||
expect(Object.isFrozen(snapshot)).toBe(true);
|
||||
expect(Object.isFrozen(snapshot.grants[0])).toBe(true);
|
||||
} finally { rmSync(file, { force: true }); }
|
||||
});
|
||||
});
|
||||
@@ -1,10 +0,0 @@
|
||||
import { DynamicModule } from '@nestjs/common';
|
||||
import { loadPrepareWorkerConfig } from './worker-config';
|
||||
import { WorkerTransportModule } from './worker-transport.module';
|
||||
|
||||
/** AppModule imports: ...registerPrepareWorker(); disabled means no module or IO. */
|
||||
export function registerPrepareWorker(env: NodeJS.ProcessEnv = process.env): DynamicModule[] {
|
||||
if (env.PREPARE_WORKER_ENABLED !== 'true') return [];
|
||||
try { return [WorkerTransportModule.register(loadPrepareWorkerConfig())]; }
|
||||
catch { throw new Error('Prepare worker startup configuration rejected'); }
|
||||
}
|
||||
@@ -1,444 +0,0 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Body,
|
||||
CanActivate,
|
||||
ConflictException,
|
||||
Controller,
|
||||
DynamicModule,
|
||||
ForbiddenException,
|
||||
Get,
|
||||
Inject,
|
||||
Injectable,
|
||||
Module,
|
||||
NotFoundException,
|
||||
Param,
|
||||
Post,
|
||||
Req,
|
||||
UnauthorizedException,
|
||||
UseGuards,
|
||||
UsePipes,
|
||||
ValidationPipe,
|
||||
} from "@nestjs/common";
|
||||
import { IsString, IsUUID, Matches, MaxLength } from "class-validator";
|
||||
import { createHash, timingSafeEqual } from "crypto";
|
||||
import { mkdir, open, FileHandle } from "fs/promises";
|
||||
import { constants } from "fs";
|
||||
import { isAbsolute } from "path";
|
||||
import { PrismaModule } from "../prisma/prisma.module";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import {
|
||||
PREPARE_WORKFLOW_CONFIG,
|
||||
PrepareWorkflowService,
|
||||
WorkflowIdentity,
|
||||
} from "../orchestration/prepare-workflow.service";
|
||||
|
||||
import { EditorialFailureDto } from '../orchestration/dto/orchestration.dto';
|
||||
import { EditorialProposalBridge, EditorialProposalDto, isEditorialProposal } from "./editorial-bridge";
|
||||
import { EditorialExecutionService, EditorialClaimDto, EditorialQueueDto, EditorialResultDto } from './editorial-execution';
|
||||
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 {
|
||||
const u = new URL(value);
|
||||
// DNS name only: reject all IP literals, localhost/private single-label names,
|
||||
// noncanonical paths/escapes, credentials, query, fragment and alternate ports.
|
||||
return u.protocol === "https:" && !u.username && !u.password && !u.port &&
|
||||
/^[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?$/.test(u.hostname) &&
|
||||
u.hostname.includes(".") && !/^[0-9.]+$/.test(u.hostname) &&
|
||||
!/(^|\.)(localhost|local|internal|lan|home)$/.test(u.hostname) &&
|
||||
["/internal/prepare-worker/artifacts", "/api/internal/prepare-worker/artifacts"].includes(u.pathname) &&
|
||||
value === u.origin + u.pathname;
|
||||
} catch { return false; }
|
||||
}
|
||||
const CONFIG = Symbol("WORKER_TRANSPORT_CONFIG");
|
||||
export interface WorkerTransportConfig {
|
||||
enabled: boolean;
|
||||
grants: {
|
||||
id: string;
|
||||
role: "WORKFLOW_WORKER" | "STAGE_CI";
|
||||
credentialSha256: string;
|
||||
}[];
|
||||
workerIds: string[];
|
||||
stageCiIds: string[];
|
||||
artifactDirectory: string;
|
||||
artifactBaseUrl: string;
|
||||
}
|
||||
class ClaimDto {
|
||||
@IsUUID() taskId!: string;
|
||||
}
|
||||
class LeaseDto extends ClaimDto {
|
||||
@IsUUID() runId!: string;
|
||||
@IsUUID() leaseToken!: string;
|
||||
}
|
||||
class EditorialHeldFailureDto extends EditorialFailureDto {
|
||||
@IsUUID() taskId!: string;
|
||||
@IsUUID() leaseToken!: string;
|
||||
}
|
||||
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;
|
||||
@Matches(/^[a-f0-9]{64}$/) sha256!: string;
|
||||
@Matches(/^[a-f0-9]{64}$/) evidenceSha256!: string;
|
||||
@Matches(/^https:\/\/[^\s]+$/) @MaxLength(2000) uri!: string;
|
||||
}
|
||||
export function tracerReport(lease: {
|
||||
taskId: string;
|
||||
runId: string;
|
||||
commitSha: string;
|
||||
}) {
|
||||
return JSON.stringify({
|
||||
kind: "DETERMINISTIC_PREPARE_TRACER",
|
||||
schemaVersion: 1,
|
||||
taskId: lease.taskId,
|
||||
runId: lease.runId,
|
||||
commitSha: lease.commitSha,
|
||||
environment: "staging",
|
||||
llmPilot: false,
|
||||
browserExecuted: false,
|
||||
shellExecuted: false,
|
||||
deploymentExecuted: false,
|
||||
checks: ["lease-bound identity", "deterministic JSON serialization"],
|
||||
actualCents: 0,
|
||||
});
|
||||
}
|
||||
@Injectable()
|
||||
class WorkerIdentityGuard implements CanActivate {
|
||||
constructor(@Inject(CONFIG) private readonly config: WorkerTransportConfig) {}
|
||||
canActivate(context: any) {
|
||||
if (!this.config.enabled)
|
||||
throw new ForbiddenException("Worker transport disabled");
|
||||
const req = context.switchToHttp().getRequest();
|
||||
const auth = req.headers.authorization;
|
||||
if (typeof auth !== "string" || !/^Bearer [\x21-\x7e]{32,512}$/.test(auth))
|
||||
throw new UnauthorizedException();
|
||||
const digest = createHash("sha256").update(auth.slice(7)).digest();
|
||||
const grant = this.config.grants.find((g) =>
|
||||
timingSafeEqual(Buffer.from(g.credentialSha256, "hex"), digest),
|
||||
);
|
||||
if (!grant) throw new UnauthorizedException();
|
||||
req.workerIdentity = { id: grant.id, role: grant.role };
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@Injectable()
|
||||
class WorkerTransportService {
|
||||
constructor(
|
||||
private readonly workflow: PrepareWorkflowService,
|
||||
private readonly db: PrismaService,
|
||||
@Inject(CONFIG) private readonly config: WorkerTransportConfig,
|
||||
) {}
|
||||
// Linux descriptor-relative traversal pins each directory across renames. Never
|
||||
// realpath-check then reopen by name: that reintroduces a symlink race.
|
||||
private async artifactDir(): Promise<FileHandle> {
|
||||
let dir = await open("/", constants.O_RDONLY | constants.O_DIRECTORY);
|
||||
try {
|
||||
for (const part of this.config.artifactDirectory.split("/").filter(Boolean)) {
|
||||
const path = `/proc/self/fd/${dir.fd}/${part}`;
|
||||
try { await mkdir(path, { mode: 0o700 }); }
|
||||
catch (e: any) { if (e.code !== "EEXIST") throw e; }
|
||||
const next = await open(path, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
|
||||
await dir.close();
|
||||
dir = next;
|
||||
}
|
||||
const stat = await dir.stat();
|
||||
if (stat.uid !== process.getuid!() || (stat.mode & 0o077) !== 0)
|
||||
throw new Error("Private owned artifact directory required");
|
||||
return dir;
|
||||
} catch (e) { await dir.close(); throw e; }
|
||||
}
|
||||
private worker(a: WorkflowIdentity) {
|
||||
if (a.role !== "WORKFLOW_WORKER" || !this.config.workerIds.includes(a.id))
|
||||
throw new ForbiddenException("Worker allowlist required");
|
||||
}
|
||||
async claim(a: WorkflowIdentity, dto: ClaimDto) {
|
||||
this.worker(a);
|
||||
const t = await this.db.orchestrationTask.findUnique({
|
||||
where: { id: dto.taskId },
|
||||
});
|
||||
if (isEditorialProposal(t)) throw new ForbiddenException("Editorial execution requires a separately reviewed executor");
|
||||
// Explicit per-task pilot scope; never opportunistically consume arbitrary queued work.
|
||||
if (!t || !["READY", "RUNNING"].includes(t.state)) return { lease: null };
|
||||
if (t.budgetLimitCents !== 20)
|
||||
throw new ForbiddenException("Pilot hard task cap is 20 cents");
|
||||
return { lease: await this.workflow.claim(a, dto.taskId) };
|
||||
}
|
||||
heartbeat(a: WorkflowIdentity, dto: LeaseDto) {
|
||||
this.worker(a);
|
||||
return this.workflow.heartbeat(a, dto);
|
||||
}
|
||||
async complete(a: WorkflowIdentity, dto: CompleteDto) {
|
||||
this.worker(a);
|
||||
const task = await this.db.orchestrationTask.findUnique({ where: { id: dto.taskId } });
|
||||
if (isEditorialProposal(task)) throw new ForbiddenException("Editorial execution requires a separately reviewed executor");
|
||||
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({
|
||||
where: { id: dto.runId },
|
||||
});
|
||||
const expected = tracerReport({ ...dto, commitSha: run.commitSha });
|
||||
if (dto.report !== expected)
|
||||
throw new BadRequestException(
|
||||
"Only deterministic harmless tracer report accepted",
|
||||
);
|
||||
const sha256 = createHash("sha256").update(expected).digest("hex");
|
||||
let dir: FileHandle | undefined, file: FileHandle | undefined;
|
||||
try {
|
||||
dir = await this.artifactDir();
|
||||
file = await open(`/proc/self/fd/${dir.fd}/${sha256}.json`,
|
||||
constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
|
||||
await file.writeFile(expected, "utf8");
|
||||
await file.sync();
|
||||
} catch { throw new ConflictException("Artifact creation refused"); }
|
||||
finally { await file?.close(); await dir?.close(); }
|
||||
const result = await this.workflow.complete(a, {
|
||||
...dto,
|
||||
sha256,
|
||||
uri: this.config.artifactBaseUrl + "/" + sha256,
|
||||
actualCents: 0,
|
||||
});
|
||||
return {
|
||||
state: result.state,
|
||||
taskId: result.id,
|
||||
runId: dto.runId,
|
||||
sha256,
|
||||
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: { in: ["QA", "TECH_PILOT_SOURCE"] }, uri: this.config.artifactBaseUrl + "/" + hash,
|
||||
run: { environment: "staging", status: "SUCCEEDED", task: {
|
||||
operationMode: "PREPARE", budgetLimitCents: 20,
|
||||
} },
|
||||
},
|
||||
});
|
||||
if (!registered) throw new NotFoundException("Artifact not registered");
|
||||
const run = await this.db.orchestrationRun.findUniqueOrThrow({ where: { id: registered.runId } });
|
||||
const task = await this.db.orchestrationTask.findUniqueOrThrow({ where: { id: run.taskId } });
|
||||
const outbox = await this.db.prepareWorkflowOutbox.findUnique({ where: { taskId: run.taskId } });
|
||||
if (!outbox || outbox.status !== "DONE" || outbox.runId !== run.id ||
|
||||
(a.role === "WORKFLOW_WORKER" && outbox.leaseOwner !== a.id))
|
||||
throw new NotFoundException("Artifact not registered");
|
||||
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 {
|
||||
dir = await this.artifactDir();
|
||||
file = await open(`/proc/self/fd/${dir.fd}/${hash}.json`,
|
||||
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 > limit)
|
||||
throw new Error("Unsafe artifact file");
|
||||
// Fixed-size descriptor read: concurrent growth cannot allocate unbounded memory.
|
||||
const buffer = Buffer.alloc(limit + 1);
|
||||
const { bytesRead } = await file.read(buffer, 0, buffer.length, 0);
|
||||
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");
|
||||
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);
|
||||
}
|
||||
async editorialFailure(a: WorkflowIdentity, dto: EditorialHeldFailureDto) {
|
||||
const { taskId, ...failure } = dto;
|
||||
const t = await this.workflow.editorialFailure(taskId, failure, a);
|
||||
return { taskId: t.id, state: t.state, version: t.version, costStatus: 'unknown-upper-bound-reserved', retainedReservationCents: 20 };
|
||||
}
|
||||
stage(a: WorkflowIdentity, dto: StageEvidenceDto) {
|
||||
return this.workflow.stageEvidence(a, dto);
|
||||
}
|
||||
}
|
||||
@Controller("internal/prepare-worker")
|
||||
@UseGuards(WorkerIdentityGuard)
|
||||
@UsePipes(
|
||||
new ValidationPipe({
|
||||
whitelist: true,
|
||||
forbidNonWhitelisted: true,
|
||||
transform: true,
|
||||
}),
|
||||
)
|
||||
class WorkerTransportController {
|
||||
constructor(private readonly service: WorkerTransportService, private readonly editorial: EditorialProposalBridge, private readonly execution: EditorialExecutionService) {}
|
||||
@Get('editorial-proposals/:taskId') editorialStatus(@Req() req: any, @Param('taskId') taskId: string) {
|
||||
return this.execution.status(req.workerIdentity, taskId);
|
||||
}
|
||||
@Post('editorial-queue') editorialQueue(@Req() req: any, @Body() dto: EditorialQueueDto) {
|
||||
return this.execution.queue(req.workerIdentity, dto);
|
||||
}
|
||||
@Post('editorial-claim') editorialClaim(@Req() req: any, @Body() dto: EditorialClaimDto) {
|
||||
return this.execution.claim(req.workerIdentity, dto);
|
||||
}
|
||||
@Post('editorial-failure') editorialFailure(@Req() req: any, @Body() dto: EditorialHeldFailureDto) {
|
||||
return this.service.editorialFailure(req.workerIdentity, dto);
|
||||
}
|
||||
@Post('editorial-result') editorialResult(@Req() req: any, @Body() dto: EditorialResultDto) {
|
||||
return this.execution.result(req.workerIdentity, dto);
|
||||
}
|
||||
@Post("editorial-proposals") editorialProposal(@Req() req: any, @Body() dto: EditorialProposalDto) {
|
||||
return this.editorial.submit(req.workerIdentity, dto);
|
||||
}
|
||||
@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);
|
||||
}
|
||||
@Post("complete") complete(@Req() req: any, @Body() dto: CompleteDto) {
|
||||
return this.service.complete(req.workerIdentity, dto);
|
||||
}
|
||||
@Post("stage-evidence") stage(
|
||||
@Req() req: any,
|
||||
@Body() dto: StageEvidenceDto,
|
||||
) {
|
||||
return this.service.stage(req.workerIdentity, dto);
|
||||
}
|
||||
@Get("artifacts/:hash") artifact(@Req() req: any, @Param("hash") hash: string) {
|
||||
return this.service.artifact(req.workerIdentity, hash);
|
||||
}
|
||||
}
|
||||
@Module({})
|
||||
export class WorkerTransportModule {
|
||||
static register(config: WorkerTransportConfig): DynamicModule {
|
||||
if (typeof config.enabled !== "boolean" || !Array.isArray(config.grants) ||
|
||||
!Array.isArray(config.workerIds) || !Array.isArray(config.stageCiIds))
|
||||
throw new Error("Explicit boolean enable and credential allowlists required");
|
||||
const allIds = [...config.workerIds, ...config.stageCiIds];
|
||||
if (allIds.some(id => typeof id !== "string" || !/^[a-zA-Z0-9_-]{1,100}$/.test(id)) ||
|
||||
new Set(allIds).size !== allIds.length)
|
||||
throw new Error("Invalid or overlapping worker/CI allowlists");
|
||||
const ids = new Set<string>(),
|
||||
digests = new Set<string>();
|
||||
if (
|
||||
!isAbsolute(config.artifactDirectory) || config.artifactDirectory === "/" ||
|
||||
config.artifactDirectory.split("/").some(p => p === "." || p === "..") ||
|
||||
!validArtifactBase(config.artifactBaseUrl)
|
||||
)
|
||||
throw new Error(
|
||||
"Absolute artifact directory and fixed HTTPS artifact base required",
|
||||
);
|
||||
for (const g of config.grants) {
|
||||
const allowed =
|
||||
g.role === "WORKFLOW_WORKER"
|
||||
? config.workerIds
|
||||
: g.role === "STAGE_CI"
|
||||
? config.stageCiIds
|
||||
: [];
|
||||
if (
|
||||
!/^[a-zA-Z0-9_-]{1,100}$/.test(g.id) ||
|
||||
!allowed.includes(g.id) ||
|
||||
!/^[a-f0-9]{64}$/.test(g.credentialSha256) ||
|
||||
ids.has(g.id) ||
|
||||
digests.has(g.credentialSha256) ||
|
||||
(config.workerIds.includes(g.id) && config.stageCiIds.includes(g.id))
|
||||
)
|
||||
throw new Error("Invalid or overlapping worker/CI credential grants");
|
||||
ids.add(g.id);
|
||||
digests.add(g.credentialSha256);
|
||||
}
|
||||
const snapshot = JSON.parse(JSON.stringify(config)) as WorkerTransportConfig;
|
||||
snapshot.grants.forEach(Object.freeze);
|
||||
Object.freeze(snapshot.grants);
|
||||
Object.freeze(snapshot.workerIds);
|
||||
Object.freeze(snapshot.stageCiIds);
|
||||
Object.freeze(snapshot);
|
||||
return {
|
||||
module: WorkerTransportModule,
|
||||
imports: [PrismaModule],
|
||||
controllers: [WorkerTransportController],
|
||||
providers: [
|
||||
PrepareWorkflowService,
|
||||
WorkerIdentityGuard,
|
||||
WorkerTransportService,
|
||||
EditorialProposalBridge,
|
||||
EditorialExecutionService,
|
||||
{ provide: CONFIG, useValue: snapshot },
|
||||
{
|
||||
provide: PREPARE_WORKFLOW_CONFIG,
|
||||
useValue: Object.freeze({
|
||||
workerIds: snapshot.workerIds,
|
||||
stageCiIds: snapshot.stageCiIds,
|
||||
}),
|
||||
},
|
||||
],
|
||||
exports: [PrepareWorkflowService],
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,438 +0,0 @@
|
||||
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, readFile, rm, writeFile, symlink, link, rename } from "fs/promises";
|
||||
import { tmpdir } from "os";
|
||||
import { join } from "path";
|
||||
import { execFile } from "child_process";
|
||||
import { promisify } from "util";
|
||||
|
||||
const enabled = process.env.WORKER_TRANSPORT_DB === "1";
|
||||
(enabled ? describe : describe.skip)(
|
||||
"isolated PostgreSQL HTTP executor tracer",
|
||||
() => {
|
||||
const db = new PrismaClient();
|
||||
const token = "test-only-worker-secret-".repeat(3);
|
||||
const ciToken = "test-only-ci-secret-".repeat(3);
|
||||
const digest = (s: string) => createHash("sha256").update(s).digest("hex");
|
||||
let app: INestApplication,
|
||||
workflow: PrepareWorkflowService,
|
||||
base: string,
|
||||
directory: string;
|
||||
let pilot: any;
|
||||
beforeAll(async () => {
|
||||
directory = await mkdtemp(join(tmpdir(), "worker-artifacts-"));
|
||||
const mod = await Test.createTestingModule({
|
||||
imports: [
|
||||
WorkerTransportModule.register({
|
||||
enabled: true,
|
||||
grants: [
|
||||
{
|
||||
id: "pilot",
|
||||
role: "WORKFLOW_WORKER",
|
||||
credentialSha256: digest(token),
|
||||
},
|
||||
{ id: "ci", role: "STAGE_CI", credentialSha256: digest(ciToken) },
|
||||
{ id: "other", role: "WORKFLOW_WORKER", credentialSha256: digest("other-worker-secret-".repeat(3)) },
|
||||
],
|
||||
workerIds: ["pilot", "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();
|
||||
pilot = {
|
||||
runOnce: async (options: any) => {
|
||||
const program =
|
||||
'import {runOnce} from "../infra/hermes-worker/pilot.mjs"; console.log(JSON.stringify(await runOnce(JSON.parse(process.env.TEST_PILOT_OPTIONS))));';
|
||||
const result = await promisify(execFile)(
|
||||
process.execPath,
|
||||
["--input-type=module", "-e", program],
|
||||
{
|
||||
env: {
|
||||
...process.env,
|
||||
TEST_PILOT_OPTIONS: JSON.stringify(options),
|
||||
},
|
||||
timeout: 15000,
|
||||
},
|
||||
);
|
||||
return JSON.parse(result.stdout);
|
||||
},
|
||||
};
|
||||
});
|
||||
afterAll(async () => {
|
||||
await app?.close();
|
||||
await db.$disconnect();
|
||||
if (directory) await rm(directory, { recursive: true, force: true });
|
||||
});
|
||||
const owner = { id: "author", role: "Admin" },
|
||||
reviewer = { id: "reviewer", role: "SUPER_ADMIN" };
|
||||
async function proposal(queue = true, budget = 20) {
|
||||
let t = await workflow.create(
|
||||
{
|
||||
key: randomUUID(),
|
||||
title: "deterministic tracer",
|
||||
department: "TEST",
|
||||
operationMode: "PREPARE",
|
||||
budgetLimitCents: budget,
|
||||
metadata: {
|
||||
commitSha: "a".repeat(40),
|
||||
scope: "Harmless tracer only",
|
||||
acceptanceCriteria: ["writes deterministic JSON; no release"],
|
||||
},
|
||||
},
|
||||
owner,
|
||||
);
|
||||
if (queue) {
|
||||
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;
|
||||
}
|
||||
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() };
|
||||
}
|
||||
it("rejects overlapping worker and CI grants and accepts no credentials when disabled", async () => {
|
||||
expect(() =>
|
||||
WorkerTransportModule.register({
|
||||
enabled: true,
|
||||
grants: [
|
||||
{
|
||||
id: "both",
|
||||
role: "WORKFLOW_WORKER",
|
||||
credentialSha256: digest(token),
|
||||
},
|
||||
],
|
||||
workerIds: ["both"],
|
||||
stageCiIds: ["both"],
|
||||
artifactDirectory: directory,
|
||||
artifactBaseUrl: "https://isolated.invalid/internal/prepare-worker/artifacts",
|
||||
}),
|
||||
).toThrow(/overlapping/);
|
||||
const mod = await Test.createTestingModule({
|
||||
imports: [
|
||||
WorkerTransportModule.register({
|
||||
enabled: false,
|
||||
grants: [],
|
||||
workerIds: [],
|
||||
stageCiIds: [],
|
||||
artifactDirectory: directory,
|
||||
artifactBaseUrl: "https://isolated.invalid/internal/prepare-worker/artifacts",
|
||||
}),
|
||||
],
|
||||
})
|
||||
.overrideProvider(PrismaService)
|
||||
.useValue(db)
|
||||
.compile();
|
||||
const disabled = mod.createNestApplication();
|
||||
await disabled.listen(0, "127.0.0.1");
|
||||
try {
|
||||
const r = await fetch(
|
||||
(await disabled.getUrl()) + "/internal/prepare-worker/claim",
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
authorization: "Bearer " + token,
|
||||
"content-type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ taskId: randomUUID() }),
|
||||
},
|
||||
);
|
||||
expect(r.status).toBe(403);
|
||||
} finally {
|
||||
await disabled.close();
|
||||
}
|
||||
});
|
||||
it("rejects unsafe artifact bases and freezes validated configuration", () => {
|
||||
const config = { enabled: true, grants: [], workerIds: [], stageCiIds: [], artifactDirectory: directory,
|
||||
artifactBaseUrl: "https://isolated.invalid/internal/prepare-worker/artifacts" };
|
||||
for (const artifactBaseUrl of ["https://user:pass@isolated.invalid/internal/prepare-worker/artifacts", "https://127.0.0.1/internal/prepare-worker/artifacts", "https://10.1.2.3/internal/prepare-worker/artifacts", "https://localhost/internal/prepare-worker/artifacts", "https://isolated.invalid/arbitrary", "https://isolated.invalid/x/../internal/prepare-worker/artifacts", "https://[::1]/internal/prepare-worker/artifacts"]) {
|
||||
expect(() => WorkerTransportModule.register({ ...config, artifactBaseUrl })).toThrow();
|
||||
}
|
||||
const registered = WorkerTransportModule.register(config);
|
||||
const snapshot = (registered.providers as any[]).find(p => p.useValue?.artifactBaseUrl)?.useValue;
|
||||
expect(Object.isFrozen(snapshot)).toBe(true);
|
||||
expect(Object.isFrozen(snapshot.workerIds)).toBe(true);
|
||||
expect(Object.isFrozen(snapshot.grants)).toBe(true);
|
||||
const workflowConfig = (registered.providers as any[]).find(p => p.provide === "PREPARE_WORKFLOW_CONFIG").useValue;
|
||||
expect(Object.isFrozen(workflowConfig)).toBe(true);
|
||||
expect(() => WorkerTransportModule.register({ ...config, enabled: "true" as any })).toThrow();
|
||||
});
|
||||
it("denies untrusted identities and body impersonation; dispatches nothing before Gate1", async () => {
|
||||
const t = await proposal(false);
|
||||
expect((await post("claim", {}, "wrong")).status).toBe(401);
|
||||
expect((await post("claim", { taskId: t.id }, ciToken)).status).toBe(403);
|
||||
expect(
|
||||
(await post("claim", { taskId: t.id, actorId: "pilot" })).status,
|
||||
).toBe(400);
|
||||
expect((await post("claim", { taskId: t.id })).body).toEqual({
|
||||
lease: null,
|
||||
});
|
||||
expect(await db.orchestrationRun.count({ where: { taskId: t.id } })).toBe(
|
||||
0,
|
||||
);
|
||||
});
|
||||
it("races claims, fences expired leases and reserves unknown cost against the 20 cent cap", async () => {
|
||||
const t = await proposal();
|
||||
const claims = await Promise.all([
|
||||
post("claim", { taskId: t.id }),
|
||||
post("claim", { taskId: t.id }),
|
||||
]);
|
||||
const leases = claims.map((r) => r.body.lease).filter(Boolean);
|
||||
expect(leases).toHaveLength(1);
|
||||
const lease = leases[0],
|
||||
binding = {
|
||||
taskId: t.id,
|
||||
runId: lease.runId,
|
||||
leaseToken: lease.leaseToken,
|
||||
};
|
||||
expect(
|
||||
(await post("heartbeat", { ...binding, leaseToken: randomUUID() }))
|
||||
.status,
|
||||
).toBe(403);
|
||||
expect(
|
||||
(
|
||||
await post("complete", {
|
||||
...binding,
|
||||
report: "arbitrary shell or prompt",
|
||||
})
|
||||
).status,
|
||||
).toBe(400);
|
||||
expect(
|
||||
(await post("complete", { ...binding, report: "{}", actualCents: 21 }))
|
||||
.status,
|
||||
).toBe(400);
|
||||
await db.prepareWorkflowOutbox.update({
|
||||
where: { taskId: t.id },
|
||||
data: { leaseUntil: new Date(Date.now() - 1000) },
|
||||
});
|
||||
expect((await post("heartbeat", binding)).status).toBe(403);
|
||||
expect(
|
||||
(await post("complete", { ...binding, report: "{}" })).status,
|
||||
).toBe(403);
|
||||
expect((await post("claim", { taskId: t.id })).status).toBe(403);
|
||||
expect(await db.orchestrationRun.count({ where: { taskId: t.id } })).toBe(
|
||||
1,
|
||||
);
|
||||
expect(
|
||||
(
|
||||
await db.orchestrationBudgetLedger.aggregate({
|
||||
where: { taskId: t.id },
|
||||
_sum: { amountCents: true },
|
||||
})
|
||||
)._sum.amountCents,
|
||||
).toBe(20);
|
||||
const larger = await proposal(true, 40);
|
||||
expect((await post("claim", { taskId: larger.id })).status).toBe(403);
|
||||
});
|
||||
it("rechecks kill switch and task interrupt on heartbeat and completion; separates CI authority", async () => {
|
||||
const t = await proposal(),
|
||||
lease = (await post("claim", { taskId: t.id })).body.lease;
|
||||
const binding = {
|
||||
taskId: t.id,
|
||||
runId: lease.runId,
|
||||
leaseToken: lease.leaseToken,
|
||||
};
|
||||
await db.orchestrationKillSwitch.upsert({
|
||||
where: { scope: "GLOBAL" },
|
||||
create: {
|
||||
scope: "GLOBAL",
|
||||
level: "STOP_AGENTS",
|
||||
active: true,
|
||||
reason: "isolated test",
|
||||
actorId: "test",
|
||||
},
|
||||
update: { level: "STOP_AGENTS", active: true },
|
||||
});
|
||||
try {
|
||||
expect((await post("heartbeat", binding)).status).toBe(403);
|
||||
expect(
|
||||
(await post("complete", { ...binding, report: "{}" })).status,
|
||||
).toBe(403);
|
||||
expect((await post("claim", { taskId: t.id })).status).toBe(403);
|
||||
} finally {
|
||||
await db.orchestrationKillSwitch.deleteMany();
|
||||
}
|
||||
await db.orchestrationTask.update({
|
||||
where: { id: t.id },
|
||||
data: { state: "CANCELLED" },
|
||||
});
|
||||
expect((await post("heartbeat", binding)).status).toBe(403);
|
||||
expect(
|
||||
(await post("complete", { ...binding, report: "{}" })).status,
|
||||
).toBe(403);
|
||||
expect(
|
||||
await db.orchestrationArtifact.count({ where: { runId: lease.runId } }),
|
||||
).toBe(0);
|
||||
const evidence = {
|
||||
taskId: t.id,
|
||||
runId: lease.runId,
|
||||
commitSha: "a".repeat(40),
|
||||
sha256: "b".repeat(64),
|
||||
evidenceSha256: "c".repeat(64),
|
||||
uri: "https://isolated.invalid/not-real-ci",
|
||||
};
|
||||
expect((await post("stage-evidence", evidence)).status).toBe(403);
|
||||
expect((await post("stage-evidence", evidence, ciToken)).status).toBe(
|
||||
403,
|
||||
);
|
||||
});
|
||||
it("never follows or overwrites preexisting artifact links on completion", async () => {
|
||||
const t = await proposal();
|
||||
const lease = (await post("claim", { taskId: t.id })).body.lease;
|
||||
const report = tracerReport(lease), hash = digest(report);
|
||||
const target = join(directory, "sentinel");
|
||||
await writeFile(target, "do not overwrite");
|
||||
const file = join(directory, hash + ".json");
|
||||
await symlink(target, file);
|
||||
const result = await post("complete", { taskId: t.id, runId: lease.runId, leaseToken: lease.leaseToken, report });
|
||||
expect(await readFile(target, "utf8")).toBe("do not overwrite");
|
||||
expect(result.status).toBe(409);
|
||||
expect(await db.orchestrationArtifact.count({ where: { runId: lease.runId } })).toBe(0);
|
||||
await rm(file);
|
||||
await link(target, file);
|
||||
expect((await post("complete", { taskId: t.id, runId: lease.runId, leaseToken: lease.leaseToken, report })).status).toBe(409);
|
||||
expect(await readFile(target, "utf8")).toBe("do not overwrite");
|
||||
});
|
||||
it("runs actual HTTP deterministic executor to VERIFYING with retrievable content-addressed artifact", async () => {
|
||||
const t = await proposal();
|
||||
const result = await pilot.runOnce({
|
||||
baseUrl: base,
|
||||
credential: token,
|
||||
taskId: t.id,
|
||||
allowLoopbackHttp: true,
|
||||
});
|
||||
expect(result.state).toBe("VERIFYING");
|
||||
const row = await db.orchestrationTask.findUniqueOrThrow({
|
||||
where: { id: t.id },
|
||||
});
|
||||
expect(row.state).toBe("VERIFYING");
|
||||
const artifact = await db.orchestrationArtifact.findFirstOrThrow({
|
||||
where: { run: { taskId: t.id } },
|
||||
});
|
||||
const bytes = await readFile(join(directory, artifact.sha256 + ".json"));
|
||||
expect(digest(bytes.toString())).toBe(artifact.sha256);
|
||||
expect(JSON.parse(bytes.toString())).toMatchObject({
|
||||
kind: "DETERMINISTIC_PREPARE_TRACER",
|
||||
commitSha: "a".repeat(40),
|
||||
environment: "staging",
|
||||
llmPilot: false,
|
||||
});
|
||||
const get = await fetch(
|
||||
base + "/internal/prepare-worker/artifacts/" + artifact.sha256,
|
||||
{ headers: { authorization: "Bearer " + token } },
|
||||
);
|
||||
expect(get.status).toBe(200);
|
||||
expect(await get.text()).toBe(bytes.toString());
|
||||
const artifactGet = (secret: string) => fetch(base + "/internal/prepare-worker/artifacts/" + artifact.sha256,
|
||||
{ headers: { authorization: "Bearer " + secret } });
|
||||
expect((await artifactGet("other-worker-secret-".repeat(3))).status).toBe(404);
|
||||
expect((await artifactGet(ciToken)).status).toBe(200);
|
||||
const foreignHash = digest("{}");
|
||||
await writeFile(join(directory, foreignHash + ".json"), "{}");
|
||||
await db.orchestrationArtifact.create({ data: { runId: artifact.runId, kind: "QA", sha256: foreignHash, uri: "https://foreign.invalid/" + foreignHash } });
|
||||
expect((await fetch(base + "/internal/prepare-worker/artifacts/" + foreignHash, { headers: { authorization: "Bearer " + token } })).status).toBe(404);
|
||||
const moved = directory + "-moved";
|
||||
await rename(directory, moved);
|
||||
try {
|
||||
await symlink(moved, directory);
|
||||
expect((await artifactGet(token)).status).toBe(404);
|
||||
} finally { await rm(directory, { force: true }); await rename(moved, directory); }
|
||||
const file = join(directory, artifact.sha256 + ".json"), saved = file + ".saved";
|
||||
await rename(file, saved);
|
||||
try {
|
||||
await symlink(saved, file);
|
||||
expect((await artifactGet(token)).status).toBe(404);
|
||||
await rm(file);
|
||||
await link(saved, file);
|
||||
expect((await artifactGet(token)).status).toBe(404);
|
||||
await rm(file);
|
||||
} finally { await rm(file, { force: true }); await rename(saved, file); }
|
||||
const cost = await db.orchestrationBudgetLedger.aggregate({
|
||||
where: { taskId: t.id },
|
||||
_sum: { amountCents: true },
|
||||
});
|
||||
expect(cost._sum.amountCents).toBe(0);
|
||||
expect(
|
||||
await db.orchestrationAttestation.findFirst({
|
||||
where: { run: { taskId: t.id }, kind: "STAGE" },
|
||||
}),
|
||||
).toBeNull();
|
||||
const evidence = {
|
||||
taskId: t.id,
|
||||
state: row.state,
|
||||
sha256: artifact.sha256,
|
||||
artifact: JSON.parse(bytes.toString()),
|
||||
httpGet: get.status,
|
||||
costCents: cost._sum.amountCents,
|
||||
uriExternalReachability: "NOT_TESTED_ISOLATED_FIXTURE_HOST",
|
||||
};
|
||||
console.log("TRACER_EVIDENCE", JSON.stringify(evidence));
|
||||
if (process.env.TRACER_EVIDENCE_PATH)
|
||||
await writeFile(
|
||||
process.env.TRACER_EVIDENCE_PATH,
|
||||
JSON.stringify(evidence, null, 2),
|
||||
);
|
||||
await writeFile(
|
||||
join(directory, artifact.sha256 + ".json"),
|
||||
'{"tampered":true}',
|
||||
);
|
||||
expect(
|
||||
(
|
||||
await fetch(
|
||||
base + "/internal/prepare-worker/artifacts/" + artifact.sha256,
|
||||
{ headers: { authorization: "Bearer " + token } },
|
||||
)
|
||||
).status,
|
||||
).toBe(409);
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -1,43 +0,0 @@
|
||||
# Bounded Stage PREPARE workflow foundation
|
||||
|
||||
This slice is NOT a runtime executor, deployment mechanism, or full v4 completion. It enables authenticated proposal/Gate1/queue APIs and provides internal worker/CI service methods. Default worker/CI identity allowlists are empty: no browser or production execution route exists.
|
||||
|
||||
## Panel HTTP contract
|
||||
All routes require existing JWT authentication with `Admin` or `SUPER_ADMIN`. Actor is exclusively `req.user.id ?? req.user.sub`, role exclusively `req.user.role`. Top-level unknown mutation fields return 400; client actor/profile fields are never trusted.
|
||||
|
||||
Base `/admin/orchestration`:
|
||||
|
||||
- `GET /tasks`: task array.
|
||||
- `GET /tasks/:id`: task object.
|
||||
- `GET /overview`: existing inventory items plus capabilities `{mutations:true, prepare:true, release:false, gate2Decisions:false, chat:false, runtime:false}`, reason `bounded_stage_prepare_only`.
|
||||
- `POST /tasks`: existing CreateOrchestrationTaskDto `{key,title,department,operationMode:"PREPARE",budgetLimitCents,metadata:{commitSha,scope,acceptanceCriteria}}`; commitSha is required lowercase 40hex; scope is a non-blank string of at most 2000 characters; acceptanceCriteria is 1..20 non-blank strings of at most 500 characters each; budget is integer 20..10000 cents. Returns 201 projected task, state DRAFT/version1. Validated scope and acceptance criteria are persisted exactly and remain unchanged by workflow transitions. The internal `workflowScope:"STAGE_PREPARE"`, environment and createdBy are server-owned; input profile/actor/other arbitrary keys are discarded. Key max120, title max255, department max50. Proposal key is unique; sequential or concurrent duplicate keys return 409, without updating the original task or appending another proposal event. Fetch/reconcile the existing task rather than retry blindly.
|
||||
- `PUT /tasks/:id/transition`: `{toState:"GATE1_PENDING"|"READY",expectedVersion,idempotencyKey,reason?}`. Only DRAFT->GATE1_PENDING and GATE1_APPROVED->READY. Owner or SUPER_ADMIN. Returns 200 projected task. READY atomically creates dispatch outbox. Requires unexpired Gate1.
|
||||
- `POST /tasks/:id/gates/1/decide`: existing GateDecisionDto `{decision:"APPROVED"|"REJECTED",expectedVersion,idempotencyKey,expiresAt,notes?}`. SUPER_ADMIN independent from proposer. Future expiry <=24h. Returns 201 **projected task**, not decision object.
|
||||
- Gate2 decision, budget writes/reconcile, command routes (including release), chat and kill-switch HTTP mutations remain 403. Runtime resource reads remain 503.
|
||||
|
||||
Task projection keeps existing scalar fields and safe nested runs/artifacts, decisions, events and ledger. Adds only `metadata:{scope,acceptanceCriteria,environment:"staging",commitSha}` for bounded proposals, otherwise `{}`. Never includes createdBy, workflowScope, payloads, URI, signatures, tokens or lease credentials. Pre-fix rows with the legacy scope sentinel remain readable/operable but have no recovered acceptance criteria: do not represent those as complete immutable scope review evidence.
|
||||
|
||||
`availableActions:string[]` is state/identity-derived: DRAFT owner -> `SUBMIT_GATE1`; GATE1_PENDING independent SUPER_ADMIN -> `APPROVE_GATE1`,`REJECT_GATE1`; GATE1_APPROVED owner/SUPER_ADMIN -> `QUEUE_PREPARE`; all worker/verification/Gate2 and legacy states -> `[]`. Actions indicate workflow eligibility, not a guarantee: current kill switch, approval expiry and concurrent versions are rechecked transactionally on mutation. Panel must not infer actions from state or send actor identity.
|
||||
|
||||
Common errors: 400 invalid DTO/binding; 403 wrong scope/identity/independence/kill/budget/expiry; 409 duplicate proposal key, stale version or idempotency payload mismatch. Transition/decision replay returns current persisted task and does not duplicate side effects.
|
||||
|
||||
## Internal service contracts (NOT browser endpoints)
|
||||
`PrepareWorkflowService` is exported by OrchestrationModule. Trusted backend composition must override `PREPARE_WORKFLOW_CONFIG` with explicit `workerIds` and `stageCiIds`, default both empty. Do not populate from request bodies or user-selected runtime profiles. Worker identities require role WORKFLOW_WORKER AND ID allowlist; Stage identities require STAGE_CI AND separate ID allowlist. Credential/session provisioning and transport remain the integrating runtime owner's responsibility.
|
||||
|
||||
- `claim(identity, taskId?) -> null | {taskId,runId,attempt,leaseToken,leaseUntil,commitSha,environment:"staging",maxCostCents:20}`. Transactional task row lock with SKIP LOCKED, immutable run binding, 30s lease, max3 attempts. Each attempt reserves **a fixed hard upper bound of 20 cents**, not an estimated or measured departmental cost. Unknown cost on expired attempts remains reserved conservatively.
|
||||
- `heartbeat(identity,{taskId,runId,leaseToken}) -> {leaseUntil}`. Same owner/token, unexpired active lease required.
|
||||
- `complete(identity,{taskId,runId,leaseToken,sha256,uri,actualCents}) -> task`. QA success ingestion only; lowercase sha256, HTTPS URI, actual cost integer0..20 from trusted worker. Atomically records immutable QA artifact/attestation, SUCCEEDED run, reservation reconciliation, DONE outbox and VERIFYING task. Expired/stale/foreign token cannot finish. Not replayable after success; caller must read persisted task/run after response loss.
|
||||
- `stageEvidence(identity,{taskId,runId,sha256,commitSha,evidenceSha256,uri}) -> task`. Independently allowlisted Stage CI identity, successful exact staging commit/run, registered QA subject, independent QA signer. Atomically records Stage artifact/attestation and reaches GATE2_PENDING. No release or Gate2 approval. Does not perform or claim deployment itself. Caller supplies actual trusted CI evidence, not browser assertions.
|
||||
|
||||
## Verification and explicit gaps
|
||||
Real isolated PostgreSQL16, no published ports/no live volumes. Added migrations applied with ON_ERROR_STOP. TDD RED logs cover missing workflow behavior, heartbeat/QA behavior and HTTP enablement. Full backend regression excluding existing legacy release-containing orchestration.integration.spec: 106 suites /851 tests including seven real-DB workflow cases, build exit0. No RELEASE_AUTHORIZE service operation executed.
|
||||
|
||||
DB regressions: concurrent claim winner, queue race/replay mismatch, hard budget rollback, stale lease fencing, max3 attempts, conservative expired costs, kill switch, self approval, trusted IDs, QA reconciliation and Stage evidence to Gate2 pending. Test fixture artifact URIs/hashes are not real deployment evidence.
|
||||
|
||||
Remaining: actual authenticated runtime worker transport/executor and pinned CI evidence transport; shared department/company budget model and locking (NOT implemented or represented by fake totals); retry backoff/dead-letter UX/cancellation; hard total execution wall-time; durable request replay of completion/evidence; kill-switch concurrent insertion/version locking; migration rehearsal with full legacy schema; real Stage CI artifact/deployment verification. No push/deploy/live DB/runtime edits. Production release remains denied at HTTP boundary; unsafe legacy core release method is deliberately untouched and must never be called by this slice.
|
||||
|
||||
## Contract repair verification
|
||||
|
||||
Actual frontend inspection confirmed proposal submission sent human scope and acceptanceCriteria but omitted required commitSha. Frontend repair is separately assigned; no frontend file changed here. New proposals now require all three fields. Internal Stage restriction uses a separate server-owned workflowScope marker rather than overwriting user scope. No metadata update route exists in this bounded workflow.
|
||||
|
||||
TDD RED→GREEN against isolated PostgreSQL16 reproduced dropped scope/criteria, nine invalid metadata acceptances (including array-to-string commit coercion), and duplicate-key P2002 leakage. Final database suite: 24 cases, including authenticated HTTP create/list/detail persistence, 400 validation, and 409 duplicate responses. Full allowed backend regression: 106 suites / 868 tests passed; legacy orchestration.integration.spec.ts excluded to avoid executing RELEASE_AUTHORIZE. Build exit0. Scoped lint remains failing on inherited require-await/no-unnecessary-type-assertion errors (13 errors); no lint suppression or config change applied. Disposable DB only; no live edits/push/deploy.
|
||||
@@ -1,59 +0,0 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { PrepareWorkflowService } from './prepare-workflow.service';
|
||||
const enabled=process.env.WORKFLOW_DB==='1';
|
||||
(enabled?describe:describe.skip)('approval queue PostgreSQL atomicity',()=>{
|
||||
const db=new PrismaClient();
|
||||
const service=new PrepareWorkflowService(db,{workerIds:['pmo','worker'],stageCiIds:[]});
|
||||
const owner={id:'pmo',role:'Admin'}, reviewer={id:'reviewer',role:'SUPER_ADMIN'},worker={id:'worker',role:'WORKFLOW_WORKER'};
|
||||
async function pending(){const t=await service.create({key:`queue-${crypto.randomUUID()}`,title:'QA',department:'CONTENT',operationMode:'PREPARE',budgetLimitCents:20,metadata:{scope:'Isolated QA',acceptanceCriteria:['Test queue'],commitSha:'a'.repeat(40)}},owner);return service.transition(t.id,{toState:'GATE1_PENDING',expectedVersion:t.version,idempotencyKey:`submit-${t.id}`},owner);}
|
||||
afterAll(()=>db.$disconnect());
|
||||
it('racing approvals serialize once, replay once, and queue can immediately claim',async()=>{
|
||||
const t=await pending();const dto={decision:'APPROVED',expectedVersion:t.version,idempotencyKey:`approve-${t.id}`};
|
||||
const results=await Promise.all([service.decide(t.id,1,dto,reviewer),service.decide(t.id,1,dto,reviewer)]);
|
||||
expect(results.every(t=>t.state==='READY')).toBe(true);
|
||||
expect(await db.orchestrationApprovalDecision.count({where:{taskId:t.id}})).toBe(1);
|
||||
expect(await db.prepareWorkflowOutbox.count({where:{taskId:t.id}})).toBe(1);
|
||||
const claims=await Promise.all([service.claim(worker,t.id),service.claim(worker,t.id)]);
|
||||
expect(claims.filter(Boolean)).toHaveLength(1);
|
||||
});
|
||||
it('scheduled work cannot claim early via targeted or polling claim; becomes claimable at due time',async()=>{
|
||||
const t=await pending();const notBefore=new Date(Date.now()+3600000).toISOString();
|
||||
await service.decide(t.id,1,{decision:'APPROVED',expectedVersion:t.version,idempotencyKey:`schedule-${t.id}`,notBefore},reviewer);
|
||||
expect(await service.claim(worker,t.id)).toBeNull();
|
||||
expect(await service.claim(worker)).toBeNull();
|
||||
const o=await db.prepareWorkflowOutbox.findUniqueOrThrow({where:{taskId:t.id}});
|
||||
expect(o.notBefore).toEqual(new Date(notBefore));
|
||||
const approval=await db.orchestrationApprovalDecision.findFirstOrThrow({where:{taskId:t.id}});
|
||||
expect(approval.expiresAt.getTime()).toBeGreaterThan(Date.parse(notBefore));
|
||||
await db.prepareWorkflowOutbox.update({where:{taskId:t.id},data:{notBefore:new Date(0)}});
|
||||
expect(await service.claim(worker,t.id)).toMatchObject({taskId:t.id,attempt:1});
|
||||
});
|
||||
it('hold and rejection never queue',async()=>{
|
||||
const t=await pending();const held=await service.transition(t.id,{toState:'GATE1_HELD',expectedVersion:t.version,idempotencyKey:`hold-${t.id}`,reason:'Revise scope'},reviewer);
|
||||
expect(await db.prepareWorkflowOutbox.count({where:{taskId:t.id}})).toBe(0);
|
||||
await service.decide(t.id,1,{decision:'REJECTED',expectedVersion:held.version,idempotencyKey:`reject-${t.id}`},reviewer);
|
||||
expect(await db.prepareWorkflowOutbox.count({where:{taskId:t.id}})).toBe(0);
|
||||
});
|
||||
it('expired approval cannot claim and immutable decision is not edited',async()=>{
|
||||
const t=await pending();await service.decide(t.id,1,{decision:'APPROVED',expectedVersion:t.version,idempotencyKey:`expire-${t.id}`,expiresAt:new Date(Date.now()+300).toISOString()},reviewer);
|
||||
await new Promise(r=>setTimeout(r,350));
|
||||
await expect(service.claim(worker,t.id)).rejects.toThrow(/expired/i);
|
||||
expect(await db.orchestrationRun.count({where:{taskId:t.id}})).toBe(0);
|
||||
});
|
||||
it('PMO recovery queues stored v3 approval without creating a decision, no impersonation',async()=>{
|
||||
const t=await pending();
|
||||
await db.orchestrationTask.update({where:{id:t.id},data:{state:'GATE1_APPROVED',version:3}});
|
||||
await db.orchestrationApprovalDecision.create({data:{taskId:t.id,gate:1,decision:'APPROVED',actorId:'independent',expiresAt:new Date(Date.now()+60000),idempotencyKey:`legacy-${t.id}`}});
|
||||
const dto={taskId:t.id,expectedVersion:3,idempotencyKey:`recover-${t.id}`};
|
||||
const result=await Promise.all([service.enqueueApproved({id:'pmo',role:'WORKFLOW_WORKER'},dto),service.enqueueApproved({id:'pmo',role:'WORKFLOW_WORKER'},dto)]);
|
||||
expect(result.every(t=>t.state==='READY'&&t.version===4)).toBe(true);
|
||||
expect(await db.orchestrationApprovalDecision.count({where:{taskId:t.id}})).toBe(1);
|
||||
expect(await db.prepareWorkflowOutbox.count({where:{taskId:t.id}})).toBe(1);
|
||||
});
|
||||
it('outbox failure rolls back immutable approval and READY together',async()=>{
|
||||
const t=await pending();await db.prepareWorkflowOutbox.create({data:{taskId:t.id}});
|
||||
await expect(service.decide(t.id,1,{decision:'APPROVED',expectedVersion:t.version,idempotencyKey:`fail-${t.id}`},reviewer)).rejects.toThrow();
|
||||
expect((await db.orchestrationTask.findUniqueOrThrow({where:{id:t.id}})).state).toBe('GATE1_PENDING');
|
||||
expect(await db.orchestrationApprovalDecision.count({where:{taskId:t.id}})).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -1,47 +0,0 @@
|
||||
import { PrepareWorkflowService } from './prepare-workflow.service';
|
||||
|
||||
describe('atomic approval queue contract', () => {
|
||||
let t:any, tx:any, service:PrepareWorkflowService;
|
||||
const actor={id:'reviewer',role:'SUPER_ADMIN'};
|
||||
const command={decision:'APPROVED',expectedVersion:2,idempotencyKey:'approve'};
|
||||
beforeEach(()=>{
|
||||
t={id:'t',state:'GATE1_PENDING',version:2,operationMode:'PREPARE',metadata:{workflowScope:'STAGE_PREPARE',createdBy:'pmo'}};
|
||||
tx={$queryRaw:jest.fn().mockResolvedValue([]),orchestrationTask:{findUnique:jest.fn(async()=>t),update:jest.fn(async({data}:any)=>(t={...t,...data,version:t.version+1}))},orchestrationEvent:{findUnique:jest.fn(),create:jest.fn()},orchestrationApprovalDecision:{create:jest.fn()},prepareWorkflowOutbox:{create:jest.fn()}};
|
||||
service=new PrepareWorkflowService({$transaction:async(fn:any)=>fn(tx)},{workerIds:['pmo'],stageCiIds:[]});
|
||||
});
|
||||
it('approval with no date enters READY and creates exactly one outbox atomically',async()=>{
|
||||
await expect(service.decide('t',1,command,actor)).resolves.toMatchObject({state:'READY',version:3});
|
||||
expect(tx.prepareWorkflowOutbox.create).toHaveBeenCalledTimes(1);
|
||||
expect(tx.orchestrationApprovalDecision.create).toHaveBeenCalledWith({data:expect.objectContaining({actorId:'reviewer',expiresAt:expect.any(Date),notes:expect.any(String)})});
|
||||
tx.orchestrationEvent.findUnique.mockResolvedValue(tx.orchestrationEvent.create.mock.calls[0][0].data);
|
||||
await service.decide('t',1,command,actor);
|
||||
expect(tx.prepareWorkflowOutbox.create).toHaveBeenCalledTimes(1);
|
||||
expect(tx.orchestrationApprovalDecision.create).toHaveBeenCalledTimes(1);
|
||||
await expect(service.decide('t',1,{...command,notes:'changed'},actor)).rejects.toMatchObject({status:409});
|
||||
});
|
||||
it('future schedule is durable and server expiry extends beyond it',async()=>{
|
||||
const notBefore=new Date(Date.now()+7*86400000).toISOString();
|
||||
await service.decide('t',1,{...command,notBefore},actor);
|
||||
const data=tx.prepareWorkflowOutbox.create.mock.calls[0][0].data;
|
||||
expect(data.notBefore).toEqual(new Date(notBefore));
|
||||
expect(tx.orchestrationApprovalDecision.create.mock.calls[0][0].data.expiresAt.getTime()).toBeGreaterThan(Date.parse(notBefore));
|
||||
});
|
||||
it('rejected decision never queues',async()=>{
|
||||
await expect(service.decide('t',1,{...command,decision:'REJECTED'},actor)).resolves.toMatchObject({state:'REJECTED'});
|
||||
expect(tx.prepareWorkflowOutbox.create).not.toHaveBeenCalled();
|
||||
});
|
||||
it.each([new Date(0).toISOString(),new Date(Date.now()+31*86400000).toISOString(),'bad'])('denies invalid schedule %s',async notBefore=>{
|
||||
await expect(service.decide('t',1,{...command,notBefore},actor)).rejects.toMatchObject({status:400});
|
||||
expect(tx.orchestrationApprovalDecision.create).not.toHaveBeenCalled();
|
||||
});
|
||||
it('legacy explicit expiry remains an expiry, never a schedule',async()=>{
|
||||
const expiresAt=new Date(Date.now()+60000).toISOString();
|
||||
await service.decide('t',1,{...command,expiresAt},actor);
|
||||
expect(tx.orchestrationApprovalDecision.create.mock.calls[0][0].data.expiresAt).toEqual(new Date(expiresAt));
|
||||
expect(tx.prepareWorkflowOutbox.create.mock.calls[0][0].data.notBefore).toBeNull();
|
||||
});
|
||||
it('expired approval is denied with no writes',async()=>{
|
||||
await expect(service.decide('t',1,{...command,expiresAt:new Date(0).toISOString()},actor)).rejects.toMatchObject({status:400});
|
||||
expect(tx.prepareWorkflowOutbox.create).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -1,100 +0,0 @@
|
||||
import { Type } from 'class-transformer';
|
||||
import {
|
||||
IsEnum,
|
||||
IsHash,
|
||||
IsIn,
|
||||
IsInt,
|
||||
IsISO8601,
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
IsString,
|
||||
IsUUID,
|
||||
MaxLength,
|
||||
Max,
|
||||
Min,
|
||||
} from 'class-validator';
|
||||
import { OperationMode, WorkflowState } from '../orchestration.domain';
|
||||
export class CreateOrchestrationTaskDto {
|
||||
@IsString() @IsNotEmpty() key!: string;
|
||||
@IsString() @IsNotEmpty() title!: string;
|
||||
@IsString() @IsNotEmpty() department!: string;
|
||||
@IsEnum(OperationMode) operationMode!: OperationMode;
|
||||
@Type(() => Number) @IsInt() @Min(0) budgetLimitCents!: number;
|
||||
@IsOptional() metadata?: Record<string, unknown>;
|
||||
}
|
||||
export class TransitionTaskDto {
|
||||
@IsEnum(WorkflowState) toState!: WorkflowState;
|
||||
@Type(() => Number) @IsInt() @Min(1) expectedVersion!: number;
|
||||
@IsString() @IsNotEmpty() idempotencyKey!: string;
|
||||
@IsOptional() @IsString() reason?: string;
|
||||
}
|
||||
export class GateDecisionDto {
|
||||
@IsIn(['APPROVED', 'REJECTED']) decision!: 'APPROVED' | 'REJECTED';
|
||||
@Type(() => Number) @IsInt() @Min(1) expectedVersion!: number;
|
||||
@IsString() @IsNotEmpty() idempotencyKey!: string;
|
||||
// Legacy explicit validity cap; scheduling uses notBefore instead.
|
||||
@IsOptional() @IsISO8601() expiresAt?: string;
|
||||
@IsOptional() @IsISO8601() notBefore?: string;
|
||||
@IsOptional() @IsString() notes?: string;
|
||||
@IsOptional() @IsString() runId?: string;
|
||||
@IsOptional() @IsHash('sha256') artifactHash?: string;
|
||||
@IsOptional() @IsString() commitSha?: string;
|
||||
@IsOptional() @IsString() environment?: string;
|
||||
}
|
||||
export class EditorialFailureDto {
|
||||
@IsUUID() runId!: string;
|
||||
@IsInt() @Min(1) expectedVersion!: number;
|
||||
@IsString() @IsNotEmpty() @MaxLength(120) idempotencyKey!: string;
|
||||
@IsString() @IsNotEmpty() @MaxLength(500) reason!: string;
|
||||
}
|
||||
export class RecordBudgetDto {
|
||||
@Type(() => Number) @IsInt() @Min(1) amountCents!: number;
|
||||
@IsString() @IsNotEmpty() idempotencyKey!: string;
|
||||
@IsString() @IsNotEmpty() kind!: string;
|
||||
@IsOptional() metadata?: Record<string, unknown>;
|
||||
}
|
||||
export class SetKillSwitchDto {
|
||||
@IsIn([
|
||||
'FREEZE_NEW_WORK',
|
||||
'STOP_AGENTS',
|
||||
'DISABLE_MUTATIONS',
|
||||
'EMERGENCY_HALT',
|
||||
])
|
||||
level!: string;
|
||||
@IsString() @IsNotEmpty() reason!: string;
|
||||
}
|
||||
|
||||
export class ReconcileBudgetDto {
|
||||
@IsString() @IsNotEmpty() reservationId!: string;
|
||||
@Type(() => Number) @IsInt() @Min(0) actualCents!: number;
|
||||
@IsString() @IsNotEmpty() idempotencyKey!: string;
|
||||
}
|
||||
export class OrchestrationCommandDto {
|
||||
@Type(() => Number) @IsInt() @Min(1) expectedVersion!: number;
|
||||
@IsString() @IsNotEmpty() idempotencyKey!: string;
|
||||
@IsOptional() @IsString() commitSha?: string;
|
||||
@IsOptional()
|
||||
@IsIn(['development', 'staging', 'production'])
|
||||
environment?: string;
|
||||
@IsOptional() @IsString() runId?: string;
|
||||
@IsOptional() @IsString() approvalId?: string;
|
||||
@IsOptional() @IsString() dependsOnTaskId?: string;
|
||||
@IsOptional() @IsIn(['SUCCEEDED', 'FAILED']) status?: string;
|
||||
@IsOptional()
|
||||
@IsIn([
|
||||
'TRANSIENT',
|
||||
'RATE_LIMITED',
|
||||
'DEPENDENCY',
|
||||
'VALIDATION',
|
||||
'POLICY',
|
||||
'PERMANENT',
|
||||
])
|
||||
retryClass?: string;
|
||||
@IsOptional() @IsHash('sha256') sha256?: string;
|
||||
@IsOptional() @IsHash('sha256') artifactHash?: string;
|
||||
@IsOptional() @IsHash('sha256') subjectHash?: string;
|
||||
@IsOptional() @IsString() kind?: string;
|
||||
@IsOptional() @IsString() uri?: string;
|
||||
@IsOptional() predicate?: Record<string, unknown>;
|
||||
@IsOptional() @IsString() signature?: string;
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
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();});
|
||||
});
|
||||
@@ -1,30 +0,0 @@
|
||||
import { PrepareWorkflowService } from './prepare-workflow.service';
|
||||
import { randomUUID } from 'crypto';
|
||||
|
||||
describe('editorial failure recovery fencing',()=>{
|
||||
const actor={id:'owner',role:'SUPER_ADMIN'},worker={id:'pmo',role:'WORKFLOW_WORKER'};
|
||||
let db:any,core:PrepareWorkflowService,t:any,o:any,r:any,events:any[];
|
||||
beforeEach(()=>{
|
||||
t={id:randomUUID(),key:'editorial-v1:'+'a'.repeat(64),title:'Editorial research: fixture',department:'CONTENT',operationMode:'PREPARE',budgetLimitCents:20,state:'RUNNING',version:6,metadata:{createdBy:'pmo',workflowScope:'STAGE_PREPARE',environment:'staging',commitSha:'a'.repeat(40),scope:'editorial-research-to-brief-v1\nTopic: fixture\nResearch and evidence-backed brief only. No publication, release, production changes or execution authorization.',acceptanceCriteria:['evidence']}};
|
||||
r={id:randomUUID(),taskId:t.id,status:'RUNNING',commitSha:t.metadata.commitSha,environment:'staging'};
|
||||
o={id:randomUUID(),taskId:t.id,status:'PENDING',runId:r.id,leaseOwner:'pmo',leaseToken:randomUUID(),leaseUntil:new Date(Date.now()-1000),reservationId:'reserve'};events=[];
|
||||
const tx:any={$queryRaw:async()=>[],orchestrationTask:{findUnique:async()=>t,update:async({data}:any)=>(Object.assign(t,{state:data.state,version:t.version+1}),{...t})},prepareWorkflowOutbox:{findUnique:async()=>o,update:async({data}:any)=>Object.assign(o,data)},orchestrationRun:{findUnique:async()=>r,update:async({data}:any)=>Object.assign(r,data)},orchestrationEvent:{findUnique:async({where}:any)=>events.find(e=>e.idempotencyKey===where.idempotencyKey),findFirst:async()=>({}),create:async({data}:any)=>events.push(data)},orchestrationBudgetLedger:{findUnique:async()=>({id:'reserve',taskId:t.id,kind:'RESERVE',actorId:'pmo',amountCents:20,idempotencyKey:'prepare-reserve:'+r.id}),aggregate:async()=>({_sum:{amountCents:20}}),findFirst:async()=>null},orchestrationArtifact:{count:async()=>0}};
|
||||
let tail=Promise.resolve();db={$transaction:(fn:any)=>{const p=tail.then(()=>fn(tx));tail=p.catch(()=>{});return p;}};core=new PrepareWorkflowService(db,{workerIds:['pmo'],stageCiIds:[]});
|
||||
});
|
||||
const dto=()=>({runId:r.id,expectedVersion:6,idempotencyKey:'recover:'+r.id,reason:'local process ended; cost unknown'});
|
||||
it('terminalizes expired run without QA/cost writes and replays concurrently',async()=>{
|
||||
const d=dto();const results=await Promise.all([core.editorialFailure(t.id,d,actor),core.editorialFailure(t.id,d,actor)]);
|
||||
expect(results.map(x=>x.state)).toEqual(['FAILED','FAILED']);expect(t.version).toBe(7);expect(r.status).toBe('FAILED');expect(o.status).toBe('DONE');expect(o.leaseToken).toBeNull();expect(o.reservationId).toBe('reserve');expect(events).toHaveLength(1);expect(events[0].payload.retainedReservationCents).toBe(20);
|
||||
});
|
||||
it('rejects wrong run, stale version, unprivileged actor, active recovery and wrong held token',async()=>{
|
||||
for(const [d,a] of [[{...dto(),runId:randomUUID()},actor],[{...dto(),expectedVersion:5},actor],[dto(),{id:'other',role:'Admin'}],[dto(),worker]] as any)await expect(core.editorialFailure(t.id,d,a)).rejects.toThrow();
|
||||
o.leaseUntil=new Date(Date.now()+30000);
|
||||
await expect(core.editorialFailure(t.id,dto(),actor)).rejects.toThrow();
|
||||
await expect(core.editorialFailure(t.id,{...dto(),leaseToken:randomUUID()},worker)).rejects.toThrow();
|
||||
await core.editorialFailure(t.id,{...dto(),leaseToken:o.leaseToken},worker);expect(t.state).toBe('FAILED');
|
||||
});
|
||||
it('rejects cross-task run binding and changed idempotency payload',async()=>{
|
||||
r.taskId=randomUUID();await expect(core.editorialFailure(t.id,dto(),actor)).rejects.toThrow();r.taskId=t.id;
|
||||
const d=dto();await core.editorialFailure(t.id,d,actor);await expect(core.editorialFailure(t.id,{...d,reason:'changed'},actor)).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
@@ -1,44 +0,0 @@
|
||||
import { PrepareWorkflowService } from './prepare-workflow.service';
|
||||
|
||||
describe('queued Gate1 expiry renewal',()=>{
|
||||
const owner={id:'owner',role:'SUPER_ADMIN'};
|
||||
let t:any,tx:any,s:PrepareWorkflowService,approval:any;
|
||||
beforeEach(()=>{
|
||||
jest.useFakeTimers().setSystemTime(new Date('2026-10-24T09:15:00Z'));
|
||||
approval={id:'approval',gate:1,decision:'APPROVED',actorId:'owner',createdAt:new Date('2026-10-24T05:37:54.808Z'),expiresAt:new Date('2026-10-24T06:37:00Z')};
|
||||
t={id:'t',state:'READY',version:4,operationMode:'PREPARE',metadata:{workflowScope:'STAGE_PREPARE',createdBy:'pmo',commitSha:'a'.repeat(40)},decisions:[approval]};
|
||||
tx={$queryRaw:jest.fn().mockResolvedValue([]),orchestrationTask:{findUnique:jest.fn(async()=>t),update:jest.fn(async({data}:any)=>(t={...t,state:data.state,version:t.version+1}))},orchestrationEvent:{findUnique:jest.fn(),create:jest.fn()},orchestrationApprovalDecision:{findFirst:jest.fn(async()=>approval),create:jest.fn()},prepareWorkflowOutbox:{findUnique:jest.fn().mockResolvedValue({status:'PENDING',attempts:0,runId:null,notBefore:null}),create:jest.fn(),update:jest.fn()}};
|
||||
s=new PrepareWorkflowService({$transaction:async(fn:any)=>fn(tx)},{workerIds:['pmo'],stageCiIds:[]});
|
||||
});
|
||||
afterEach(()=>jest.useRealTimers());
|
||||
const dto={decision:'APPROVED',expectedVersion:4,idempotencyKey:'renew'};
|
||||
it('shows blocked renewal rather than actionable READY at actual failure time',()=>{
|
||||
expect(s.project(t,owner)).toMatchObject({displayState:'GATE1_RENEWAL_REQUIRED',availableActions:['APPROVE_GATE1']});
|
||||
});
|
||||
it('renews by ordinary independent approval without recreating outbox or changing immutable task binding',async()=>{
|
||||
await expect(s.decide('t',1,dto,owner)).resolves.toMatchObject({state:'READY',version:5});
|
||||
expect(tx.prepareWorkflowOutbox.create).not.toHaveBeenCalled();
|
||||
expect(tx.orchestrationApprovalDecision.create).toHaveBeenCalledWith({data:expect.objectContaining({actorId:'owner',expiresAt:new Date('2026-10-25T09:15:00Z')})});
|
||||
expect(tx.orchestrationEvent.create).toHaveBeenCalledWith({data:expect.objectContaining({eventType:'PREPARE_GATE1_RENEWED',fromState:'READY',toState:'READY'})});
|
||||
});
|
||||
it('does not renew valid approvals, self review, active runs, or schedule changes',async()=>{
|
||||
await expect(s.decide('t',1,dto,{id:'pmo',role:'SUPER_ADMIN'})).rejects.toMatchObject({status:403});
|
||||
await expect(s.decide('t',1,{...dto,notBefore:'2026-10-25T10:00:00Z'},owner)).rejects.toMatchObject({status:400});
|
||||
tx.prepareWorkflowOutbox.findUnique.mockResolvedValue({status:'PENDING',attempts:1,runId:'r'});
|
||||
await expect(s.decide('t',1,dto,owner)).rejects.toMatchObject({status:409});
|
||||
tx.prepareWorkflowOutbox.findUnique.mockResolvedValue({status:'PENDING',attempts:0,runId:null});
|
||||
approval.expiresAt=new Date('2026-10-25T09:15:00Z');
|
||||
await expect(s.decide('t',1,dto,owner)).rejects.toMatchObject({status:409});
|
||||
});
|
||||
it('replays identical command after expiry but denies changed payload and stale concurrent command',async()=>{
|
||||
await s.decide('t',1,dto,owner);
|
||||
const e=tx.orchestrationEvent.create.mock.calls[0][0].data;
|
||||
tx.orchestrationEvent.findUnique.mockResolvedValue(e);
|
||||
jest.setSystemTime(new Date('2026-10-26T09:15:00Z'));
|
||||
await s.decide('t',1,dto,owner);
|
||||
expect(tx.orchestrationTask.update).toHaveBeenCalledTimes(1);
|
||||
await expect(s.decide('t',1,{...dto,notes:'changed'},owner)).rejects.toMatchObject({status:409});
|
||||
tx.orchestrationEvent.findUnique.mockResolvedValue(null);
|
||||
await expect(s.decide('t',1,{...dto,idempotencyKey:'competing'},owner)).rejects.toMatchObject({status:409});
|
||||
});
|
||||
});
|
||||
@@ -1,45 +0,0 @@
|
||||
import { ForbiddenException } from '@nestjs/common';
|
||||
import { KillSwitchService } from './kill-switch.service';
|
||||
|
||||
describe('KillSwitchService', () => {
|
||||
const prisma = { orchestrationKillSwitch: { findUnique: jest.fn() } };
|
||||
const service = new KillSwitchService(prisma as never);
|
||||
|
||||
beforeEach(() => jest.resetAllMocks());
|
||||
|
||||
it.each([
|
||||
['FREEZE_NEW_WORK', 'CREATE_TASK'],
|
||||
['STOP_AGENTS', 'RUN_AGENT'],
|
||||
['DISABLE_MUTATIONS', 'MUTATE_PRODUCTION'],
|
||||
['EMERGENCY_HALT', 'OBSERVE'],
|
||||
])('blocks %s at its policy boundary', async (level, action) => {
|
||||
prisma.orchestrationKillSwitch.findUnique.mockResolvedValue({
|
||||
scope: 'GLOBAL',
|
||||
level,
|
||||
active: true,
|
||||
});
|
||||
await expect(service.assertAllowed(action as never)).rejects.toThrow(
|
||||
ForbiddenException,
|
||||
);
|
||||
});
|
||||
|
||||
it('freezes new claims while allowing an already running agent to finish', async () => {
|
||||
prisma.orchestrationKillSwitch.findUnique.mockResolvedValue({scope:'GLOBAL',level:'FREEZE_NEW_WORK',active:true});
|
||||
await expect(service.assertAllowed('CLAIM_TASK' as never)).rejects.toThrow(ForbiddenException);
|
||||
await expect(service.assertAllowed('RUN_AGENT')).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('fails closed when an active persisted kill level is unknown', async () => {
|
||||
prisma.orchestrationKillSwitch.findUnique.mockResolvedValue({scope:'GLOBAL',level:'CORRUPT_LEVEL',active:true});
|
||||
await expect(service.assertAllowed('RUN_AGENT')).rejects.toThrow(ForbiddenException);
|
||||
});
|
||||
|
||||
it('allows observation while production mutations are disabled', async () => {
|
||||
prisma.orchestrationKillSwitch.findUnique.mockResolvedValue({
|
||||
scope: 'GLOBAL',
|
||||
level: 'DISABLE_MUTATIONS',
|
||||
active: true,
|
||||
});
|
||||
await expect(service.assertAllowed('OBSERVE')).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -1,36 +0,0 @@
|
||||
import { ForbiddenException, Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { KillSwitchLevel } from './orchestration.domain';
|
||||
export type GuardedAction =
|
||||
| 'CREATE_TASK'
|
||||
| 'CLAIM_TASK'
|
||||
| 'RUN_AGENT'
|
||||
| 'MUTATE_PRODUCTION'
|
||||
| 'OBSERVE';
|
||||
@Injectable()
|
||||
export class KillSwitchService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
async assertAllowed(action: GuardedAction): Promise<void> {
|
||||
const state = await (this.prisma as any).orchestrationKillSwitch.findUnique(
|
||||
{ where: { scope: 'GLOBAL' } },
|
||||
);
|
||||
if (!state?.active) return;
|
||||
const blocked: Record<KillSwitchLevel, GuardedAction[]> = {
|
||||
FREEZE_NEW_WORK: ['CREATE_TASK', 'CLAIM_TASK'],
|
||||
STOP_AGENTS: ['CREATE_TASK', 'CLAIM_TASK', 'RUN_AGENT'],
|
||||
DISABLE_MUTATIONS: ['CREATE_TASK', 'CLAIM_TASK', 'RUN_AGENT', 'MUTATE_PRODUCTION'],
|
||||
EMERGENCY_HALT: [
|
||||
'CREATE_TASK',
|
||||
'CLAIM_TASK',
|
||||
'RUN_AGENT',
|
||||
'MUTATE_PRODUCTION',
|
||||
'OBSERVE',
|
||||
],
|
||||
};
|
||||
if (!Object.values(KillSwitchLevel).includes(state.level) ||
|
||||
blocked[state.level as KillSwitchLevel].includes(action))
|
||||
throw new ForbiddenException(
|
||||
`Orchestration kill switch ${state.level} blocks ${action}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
describe('orchestration migration safety', () => {
|
||||
it('is additive and never mutates legacy governance tables', () => {
|
||||
const sql = readFileSync(
|
||||
join(
|
||||
process.cwd(),
|
||||
'prisma/migrations/20261022000000_add_orchestration_v4_core/migration.sql',
|
||||
),
|
||||
'utf8',
|
||||
);
|
||||
expect(sql).not.toMatch(/^\s*(DROP|TRUNCATE|DELETE|UPDATE)\b/im);
|
||||
expect(sql).not.toMatch(
|
||||
/ALTER TABLE "(approval_requests|department_tasks|audit_logs)"/i,
|
||||
);
|
||||
expect(sql.match(/CREATE TABLE "orchestration_/g)).toHaveLength(9);
|
||||
});
|
||||
});
|
||||
@@ -1,68 +0,0 @@
|
||||
import { BadRequestException } from '@nestjs/common';
|
||||
import { ModelTierPolicyService } from './model-tier-policy.service';
|
||||
|
||||
describe('ModelTierPolicyService', () => {
|
||||
const policy = new ModelTierPolicyService();
|
||||
|
||||
it.each([
|
||||
[
|
||||
{
|
||||
complexity: 0,
|
||||
risk: 0,
|
||||
operationMode: 'OBSERVE',
|
||||
remainingBudgetCents: 1,
|
||||
},
|
||||
'L0',
|
||||
],
|
||||
[
|
||||
{
|
||||
complexity: 2,
|
||||
risk: 1,
|
||||
operationMode: 'PREPARE',
|
||||
remainingBudgetCents: 100,
|
||||
},
|
||||
'L1',
|
||||
],
|
||||
[
|
||||
{
|
||||
complexity: 5,
|
||||
risk: 4,
|
||||
operationMode: 'PREPARE',
|
||||
remainingBudgetCents: 500,
|
||||
},
|
||||
'L2',
|
||||
],
|
||||
[
|
||||
{
|
||||
complexity: 7,
|
||||
risk: 7,
|
||||
operationMode: 'PREPARE',
|
||||
remainingBudgetCents: 1500,
|
||||
},
|
||||
'L3',
|
||||
],
|
||||
[
|
||||
{
|
||||
complexity: 9,
|
||||
risk: 9,
|
||||
operationMode: 'MUTATE_PRODUCTION',
|
||||
remainingBudgetCents: 5000,
|
||||
},
|
||||
'L4',
|
||||
],
|
||||
])('selects a deterministic tier for policy inputs', (input, expected) => {
|
||||
expect(policy.select(input as never)).toBe(expected);
|
||||
expect(policy.select(input as never)).toBe(expected);
|
||||
});
|
||||
|
||||
it('fails closed when the selected tier exceeds the remaining hard budget', () => {
|
||||
expect(() =>
|
||||
policy.select({
|
||||
complexity: 9,
|
||||
risk: 9,
|
||||
operationMode: 'MUTATE_PRODUCTION',
|
||||
remainingBudgetCents: 100,
|
||||
}),
|
||||
).toThrow(BadRequestException);
|
||||
});
|
||||
});
|
||||
@@ -1,40 +0,0 @@
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { OperationMode } from './orchestration.domain';
|
||||
export type ModelTier = 'L0' | 'L1' | 'L2' | 'L3' | 'L4';
|
||||
@Injectable()
|
||||
export class ModelTierPolicyService {
|
||||
select(input: {
|
||||
complexity: number;
|
||||
risk: number;
|
||||
operationMode: OperationMode | string;
|
||||
remainingBudgetCents: number;
|
||||
}): ModelTier {
|
||||
const score =
|
||||
Math.max(input.complexity, input.risk) +
|
||||
(String(input.operationMode) === String(OperationMode.MUTATE_PRODUCTION)
|
||||
? 1
|
||||
: 0);
|
||||
const tier: ModelTier =
|
||||
score <= 1
|
||||
? 'L0'
|
||||
: score <= 3
|
||||
? 'L1'
|
||||
: score <= 5
|
||||
? 'L2'
|
||||
: score <= 8
|
||||
? 'L3'
|
||||
: 'L4';
|
||||
const floor: Record<ModelTier, number> = {
|
||||
L0: 1,
|
||||
L1: 25,
|
||||
L2: 100,
|
||||
L3: 500,
|
||||
L4: 2000,
|
||||
};
|
||||
if (input.remainingBudgetCents < floor[tier])
|
||||
throw new BadRequestException(
|
||||
`Hard budget cannot fund selected model tier ${tier}`,
|
||||
);
|
||||
return tier;
|
||||
}
|
||||
}
|
||||
@@ -1,157 +0,0 @@
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import request from 'supertest';
|
||||
import { OrchestrationController } from './orchestration.controller';
|
||||
import { OrchestrationService } from './orchestration.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { RolesGuard } from '../common/guards/roles.guard';
|
||||
|
||||
describe('integration read boundary', () => {
|
||||
let app: INestApplication;
|
||||
const task = {
|
||||
id: 't1',
|
||||
key: 'k',
|
||||
title: 'Review',
|
||||
department: 'SEO',
|
||||
state: 'GATE2_APPROVED',
|
||||
version: 1,
|
||||
operationMode: 'PREPARE',
|
||||
budgetLimitCents: 0,
|
||||
metadata: { token: 'secret' },
|
||||
runs: [
|
||||
{
|
||||
id: 'r',
|
||||
status: 'VERIFIED',
|
||||
commitSha: 'abc',
|
||||
environment: 'stage',
|
||||
artifacts: [
|
||||
{
|
||||
id: 'a',
|
||||
sha256: 'f'.repeat(64),
|
||||
sizeBytes: 1n,
|
||||
uri: 'secret-uri',
|
||||
metadata: { token: 'secret' },
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
events: [
|
||||
{
|
||||
id: 'e',
|
||||
eventType: 'CREATED',
|
||||
sequence: 1,
|
||||
payload: { token: 'secret' },
|
||||
},
|
||||
],
|
||||
};
|
||||
const service = {
|
||||
findAll: jest.fn(() => Promise.resolve([task])),
|
||||
findOne: jest.fn(() => Promise.resolve(task)),
|
||||
createTask: jest.fn(),
|
||||
transition: jest.fn(),
|
||||
decideGate: jest.fn(),
|
||||
recordBudget: jest.fn(),
|
||||
setKillSwitch: jest.fn(),
|
||||
command: jest.fn(),
|
||||
reconcileBudget: jest.fn(),
|
||||
};
|
||||
beforeAll(async () => {
|
||||
const m = await Test.createTestingModule({
|
||||
controllers: [OrchestrationController],
|
||||
providers: [
|
||||
{ provide: OrchestrationService, useValue: service },
|
||||
RolesGuard,
|
||||
],
|
||||
})
|
||||
.overrideGuard(JwtAuthGuard)
|
||||
.useValue({
|
||||
canActivate(c: any) {
|
||||
const req = c.switchToHttp().getRequest();
|
||||
if (!req.headers.authorization) return false;
|
||||
req.user = { id: 'verified-user', role: req.headers.authorization };
|
||||
return true;
|
||||
},
|
||||
})
|
||||
.compile();
|
||||
app = m.createNestApplication();
|
||||
await app.init();
|
||||
});
|
||||
afterAll(async () => app.close());
|
||||
it('denies every mutation before legacy core is invoked', async () => {
|
||||
for (const [method, path] of [
|
||||
['post', 'tasks'],
|
||||
['put', 'tasks/t1/transition'],
|
||||
['post', 'tasks/t1/gates/2/decide'],
|
||||
['post', 'tasks/t1/budget'],
|
||||
['post', 'tasks/t1/commands/RUN_CREATE'],
|
||||
['post', 'tasks/t1/budget/reconcile'],
|
||||
['put', 'kill-switch'],
|
||||
['post', 'chat/messages'],
|
||||
] as const) {
|
||||
await request(app.getHttpServer())
|
||||
[method]('/admin/orchestration/' + path)
|
||||
.set('Authorization', 'Admin')
|
||||
.send({})
|
||||
.expect(path === 'tasks' || path.endsWith('/transition') ? 400 : 403);
|
||||
}
|
||||
for (const name of [
|
||||
'createTask',
|
||||
'transition',
|
||||
'decideGate',
|
||||
'recordBudget',
|
||||
'setKillSwitch',
|
||||
'command',
|
||||
'reconcileBudget',
|
||||
] as const)
|
||||
expect(service[name]).not.toHaveBeenCalled();
|
||||
});
|
||||
it('projects list and detail with no actions, arbitrary secrets or BigInt transport failures', async () => {
|
||||
for (const path of ['tasks', 'tasks/t1']) {
|
||||
const r = await request(app.getHttpServer())
|
||||
.get('/admin/orchestration/' + path)
|
||||
.set('Authorization', 'Admin')
|
||||
.expect(200);
|
||||
const t = Array.isArray(r.body) ? r.body[0] : r.body;
|
||||
expect(t.availableActions).toEqual([]);
|
||||
expect(t.budgetLimitCents).toBe(0);
|
||||
expect(JSON.stringify(t)).not.toMatch(/secret|sizeBytes|payload|token/);
|
||||
}
|
||||
});
|
||||
it('returns actual inventory overview and explicit unavailable runtime resources', async () => {
|
||||
const r = await request(app.getHttpServer())
|
||||
.get('/admin/orchestration/overview')
|
||||
.set('Authorization', 'Admin')
|
||||
.expect(200);
|
||||
expect(r.body.items).toEqual([
|
||||
expect.objectContaining({ id: 'tasks', count: 1 }),
|
||||
]);
|
||||
expect(r.body.capabilities).toMatchObject({
|
||||
mutations: false,
|
||||
chat: false,
|
||||
runtime: false,
|
||||
});
|
||||
for (const path of ['agents', 'schedules', 'model-policy'])
|
||||
await request(app.getHttpServer())
|
||||
.get('/admin/orchestration/' + path)
|
||||
.set('Authorization', 'Admin')
|
||||
.expect(503);
|
||||
const releases = await request(app.getHttpServer())
|
||||
.get('/admin/orchestration/releases')
|
||||
.set('Authorization', 'Admin')
|
||||
.expect(200);
|
||||
expect(releases.body.items[0]).toMatchObject({
|
||||
id: 'r',
|
||||
commitSha: 'abc',
|
||||
environment: 'stage',
|
||||
});
|
||||
});
|
||||
it('protects read surfaces from anonymous and customer access', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.get('/admin/orchestration/overview')
|
||||
.expect(403);
|
||||
await request(app.getHttpServer())
|
||||
.get('/admin/orchestration/tasks')
|
||||
.set('Authorization', 'CUSTOMER')
|
||||
.expect(403);
|
||||
});
|
||||
});
|
||||
@@ -1,260 +0,0 @@
|
||||
import { EditorialExecutionService, CompletedRecoveryDto } from '../orchestration-worker/editorial-execution';
|
||||
import {
|
||||
Controller,
|
||||
Body,
|
||||
Req,
|
||||
Optional,
|
||||
ValidationPipe,
|
||||
Get,
|
||||
Param,
|
||||
Post,
|
||||
Put,
|
||||
UseGuards,
|
||||
ForbiddenException,
|
||||
ServiceUnavailableException,
|
||||
} from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { RolesGuard } from '../common/guards/roles.guard';
|
||||
import { Roles } from '../common/decorators/roles.decorator';
|
||||
import { OrchestrationService } from './orchestration.service';
|
||||
import {
|
||||
PrepareWorkflowService,
|
||||
WorkflowIdentity,
|
||||
} from './prepare-workflow.service';
|
||||
import {
|
||||
CreateOrchestrationTaskDto,
|
||||
TransitionTaskDto,
|
||||
GateDecisionDto,
|
||||
EditorialFailureDto,
|
||||
} from './dto/orchestration.dto';
|
||||
const workflowValidation = new ValidationPipe({
|
||||
transform: true,
|
||||
whitelist: true,
|
||||
forbidNonWhitelisted: true,
|
||||
});
|
||||
|
||||
// Explicit response allowlists: never serialize arbitrary JSON, signatures, URIs or BigInt.
|
||||
function pick(row: any, keys: string[]) {
|
||||
return Object.fromEntries(
|
||||
keys.filter((k) => row[k] !== undefined).map((k) => [k, row[k]]),
|
||||
);
|
||||
}
|
||||
function project(row: any) {
|
||||
return {
|
||||
...pick(row, [
|
||||
'id',
|
||||
'key',
|
||||
'title',
|
||||
'department',
|
||||
'state',
|
||||
'version',
|
||||
'operationMode',
|
||||
'budgetLimitCents',
|
||||
]),
|
||||
availableActions: [],
|
||||
runs: (row.runs ?? []).map((run: any) => ({
|
||||
...pick(run, ['id', 'commitSha', 'environment', 'status']),
|
||||
artifacts: (run.artifacts ?? []).map((a: any) =>
|
||||
pick(a, ['id', 'kind', 'sha256']),
|
||||
),
|
||||
})),
|
||||
decisions: (row.decisions ?? []).map((d: any) =>
|
||||
pick(d, [
|
||||
'id',
|
||||
'gate',
|
||||
'decision',
|
||||
'expiresAt',
|
||||
'runId',
|
||||
'artifactHash',
|
||||
'commitSha',
|
||||
'environment',
|
||||
'createdAt',
|
||||
]),
|
||||
),
|
||||
events: (row.events ?? []).map((e: any) =>
|
||||
pick(e, [
|
||||
'id',
|
||||
'eventType',
|
||||
'fromState',
|
||||
'toState',
|
||||
'sequence',
|
||||
'createdAt',
|
||||
]),
|
||||
),
|
||||
budgetLedger: (row.budgetLedger ?? []).map((b: any) =>
|
||||
pick(b, ['id', 'amountCents', 'kind', 'createdAt']),
|
||||
),
|
||||
};
|
||||
}
|
||||
@ApiTags('Admin - Orchestration v4')
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin', 'SUPER_ADMIN')
|
||||
@Controller('admin/orchestration')
|
||||
export class OrchestrationController {
|
||||
constructor(
|
||||
private readonly service: OrchestrationService,
|
||||
@Optional() private readonly workflow?: PrepareWorkflowService,
|
||||
@Optional() private readonly editorialExecution?: EditorialExecutionService,
|
||||
) {}
|
||||
private actor(req: any): WorkflowIdentity {
|
||||
const id = req?.user?.id ?? req?.user?.sub;
|
||||
// Match RolesGuard aliases without promoting an admin to a reviewer.
|
||||
const normalized = typeof req?.user?.role === 'string'
|
||||
? req.user.role.toLowerCase().replace(/[_-]/g, '')
|
||||
: '';
|
||||
const role = normalized === 'admin' ? 'Admin'
|
||||
: ['superadmin', 'adminsuper'].includes(normalized) ? 'SUPER_ADMIN'
|
||||
: undefined;
|
||||
if (
|
||||
typeof id !== 'string' ||
|
||||
!id ||
|
||||
!role
|
||||
)
|
||||
throw new ForbiddenException('Authenticated workflow identity required');
|
||||
return { id, role };
|
||||
}
|
||||
private safe(row: any, req: any) {
|
||||
const view = project(row);
|
||||
if (!this.workflow) return view;
|
||||
const w = this.workflow.project(row, this.actor(req));
|
||||
return {
|
||||
...view,
|
||||
metadata: w.metadata,
|
||||
displayState: w.displayState,
|
||||
availableActions: w.availableActions,
|
||||
};
|
||||
}
|
||||
@Get('tasks') async all(@Req() req: any) {
|
||||
return (await this.service.findAll()).map((row: any) =>
|
||||
this.safe(row, req),
|
||||
);
|
||||
}
|
||||
@Get('tasks/:id') async one(@Param('id') id: string, @Req() req: any) {
|
||||
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();
|
||||
return {
|
||||
items: [
|
||||
{ id: 'tasks', title: 'Persisted task inventory', count: tasks.length },
|
||||
],
|
||||
measuredAt: new Date().toISOString(),
|
||||
capabilities: {
|
||||
mutations: !!this.workflow,
|
||||
prepare: !!this.workflow,
|
||||
release: false,
|
||||
gate2Decisions: false,
|
||||
chat: false,
|
||||
runtime: false,
|
||||
},
|
||||
reason: this.workflow
|
||||
? 'bounded_stage_prepare_only'
|
||||
: 'security_hardening_pending',
|
||||
};
|
||||
}
|
||||
@Get('releases') async releases() {
|
||||
const tasks = await this.service.findAll();
|
||||
const details = await Promise.all(
|
||||
tasks.map((t: any) => this.service.findOne(t.id)),
|
||||
);
|
||||
return {
|
||||
items: details.flatMap((t: any) =>
|
||||
(t.runs ?? []).map((run: any) =>
|
||||
pick(run, ['id', 'status', 'commitSha', 'environment']),
|
||||
),
|
||||
),
|
||||
measuredAt: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
@Get('agents') agents() {
|
||||
throw new ServiceUnavailableException(
|
||||
'Hermes runtime unavailable: trusted identity and runtime authentication not configured',
|
||||
);
|
||||
}
|
||||
@Get('schedules') schedules() {
|
||||
throw new ServiceUnavailableException(
|
||||
'Hermes schedules unavailable; runtime remains disabled',
|
||||
);
|
||||
}
|
||||
@Get('model-policy') models() {
|
||||
throw new ServiceUnavailableException(
|
||||
'No persisted model policy read contract available',
|
||||
);
|
||||
}
|
||||
// Only the separate bounded PREPARE service is exposed; legacy commands remain denied.
|
||||
@Post('tasks') async create(
|
||||
@Body(workflowValidation) dto: CreateOrchestrationTaskDto,
|
||||
@Req() req: any,
|
||||
) {
|
||||
if (!this.workflow) return this.deny();
|
||||
return this.safe(await this.workflow.create(dto, this.actor(req)), req);
|
||||
}
|
||||
@Put('tasks/:id/transition') async transition(
|
||||
@Param('id') id: string,
|
||||
@Body(workflowValidation) dto: TransitionTaskDto,
|
||||
@Req() req: any,
|
||||
) {
|
||||
if (!this.workflow) return this.deny();
|
||||
return this.safe(
|
||||
await this.workflow.transition(id, dto, this.actor(req)),
|
||||
req,
|
||||
);
|
||||
}
|
||||
@Post('tasks/:id/gates/:gate/decide') async decide(
|
||||
@Param('id') id: string,
|
||||
@Param('gate') gate: string,
|
||||
@Body() body: unknown,
|
||||
@Req() req: any,
|
||||
) {
|
||||
if (!this.workflow || gate !== '1') return this.deny();
|
||||
const dto = await workflowValidation.transform(body, {
|
||||
type: 'body',
|
||||
metatype: GateDecisionDto,
|
||||
});
|
||||
return this.safe(
|
||||
await this.workflow.decide(id, 1, dto, this.actor(req)),
|
||||
req,
|
||||
);
|
||||
}
|
||||
@Post('tasks/:id/editorial-completed-recovery') async completedRecovery(
|
||||
@Param('id') id: string,
|
||||
@Body(workflowValidation) dto: CompletedRecoveryDto,
|
||||
@Req() req: any,
|
||||
) {
|
||||
if (!this.workflow || !this.editorialExecution) return this.deny();
|
||||
return this.editorialExecution.recoverCompleted(this.actor(req), id, dto);
|
||||
}
|
||||
@Post('tasks/:id/editorial-failure') async editorialFailure(
|
||||
@Param('id') id: string,
|
||||
@Body(workflowValidation) dto: EditorialFailureDto,
|
||||
@Req() req: any,
|
||||
) {
|
||||
if (!this.workflow) return this.deny();
|
||||
return this.safe(await this.workflow.editorialFailure(id, dto, this.actor(req)), req);
|
||||
}
|
||||
@Post('tasks/:id/budget') budget() {
|
||||
return this.deny();
|
||||
}
|
||||
@Put('kill-switch') kill() {
|
||||
return this.deny();
|
||||
}
|
||||
@Post('chat/messages') chat() {
|
||||
return this.deny();
|
||||
}
|
||||
@Post('tasks/:id/commands/:command') command() {
|
||||
return this.deny();
|
||||
}
|
||||
@Post('tasks/:id/budget/reconcile') reconcile() {
|
||||
return this.deny();
|
||||
}
|
||||
private deny(): never {
|
||||
throw new ForbiddenException(
|
||||
'Orchestration mutations and chat disabled pending hardened core and trusted runtime integration',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
ForbiddenException,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
assertTransition,
|
||||
nextStateForGateDecision,
|
||||
OperationMode,
|
||||
WorkflowState,
|
||||
} from './orchestration.domain';
|
||||
|
||||
describe('orchestration domain', () => {
|
||||
it('allows only declared workflow transitions', () => {
|
||||
expect(() =>
|
||||
assertTransition(WorkflowState.DRAFT, WorkflowState.GATE1_PENDING),
|
||||
).not.toThrow();
|
||||
expect(() =>
|
||||
assertTransition(WorkflowState.DRAFT, WorkflowState.RUNNING),
|
||||
).toThrow(BadRequestException);
|
||||
});
|
||||
|
||||
it('keeps gate 1 as execution authorization rather than release authorization', () => {
|
||||
expect(nextStateForGateDecision(1, 'APPROVED')).toBe(
|
||||
WorkflowState.GATE1_APPROVED,
|
||||
);
|
||||
expect(nextStateForGateDecision(2, 'APPROVED')).toBe(
|
||||
WorkflowState.GATE2_APPROVED,
|
||||
);
|
||||
});
|
||||
|
||||
it('rejects an unsupported gate decision', () => {
|
||||
expect(() => nextStateForGateDecision(1, 'PENDING' as never)).toThrow(
|
||||
BadRequestException,
|
||||
);
|
||||
});
|
||||
|
||||
it.each([WorkflowState.GATE1_PENDING, WorkflowState.GATE2_PENDING])('does not revive an expired proposal into %s', (state) => {
|
||||
expect(() => assertTransition(WorkflowState.EXPIRED,state)).toThrow(BadRequestException);
|
||||
});
|
||||
|
||||
it('classifies production mutation separately from observe and prepare', () => {
|
||||
expect(OperationMode.OBSERVE).toBe('OBSERVE');
|
||||
expect(OperationMode.PREPARE).toBe('PREPARE');
|
||||
expect(OperationMode.MUTATE_PRODUCTION).toBe('MUTATE_PRODUCTION');
|
||||
});
|
||||
});
|
||||
@@ -1,109 +0,0 @@
|
||||
import { BadRequestException } from '@nestjs/common';
|
||||
|
||||
export enum WorkflowState {
|
||||
DRAFT = 'DRAFT',
|
||||
GATE1_PENDING = 'GATE1_PENDING',
|
||||
GATE1_HELD = 'GATE1_HELD',
|
||||
GATE1_APPROVED = 'GATE1_APPROVED',
|
||||
READY = 'READY',
|
||||
CLAIMED = 'CLAIMED',
|
||||
RUNNING = 'RUNNING',
|
||||
VERIFYING = 'VERIFYING',
|
||||
GATE2_PENDING = 'GATE2_PENDING',
|
||||
GATE2_APPROVED = 'GATE2_APPROVED',
|
||||
RELEASING = 'RELEASING',
|
||||
PRODUCTION_VERIFYING = 'PRODUCTION_VERIFYING',
|
||||
LIVE_VERIFIED = 'LIVE_VERIFIED',
|
||||
MEASURING = 'MEASURING',
|
||||
CLOSED = 'CLOSED',
|
||||
REJECTED = 'REJECTED',
|
||||
BLOCKED = 'BLOCKED',
|
||||
CANCELLED = 'CANCELLED',
|
||||
FAILED = 'FAILED',
|
||||
EXPIRED = 'EXPIRED',
|
||||
}
|
||||
export enum OperationMode {
|
||||
OBSERVE = 'OBSERVE',
|
||||
PREPARE = 'PREPARE',
|
||||
MUTATE_PRODUCTION = 'MUTATE_PRODUCTION',
|
||||
}
|
||||
export enum RetryClass {
|
||||
TRANSIENT = 'TRANSIENT',
|
||||
RATE_LIMITED = 'RATE_LIMITED',
|
||||
DEPENDENCY = 'DEPENDENCY',
|
||||
VALIDATION = 'VALIDATION',
|
||||
POLICY = 'POLICY',
|
||||
PERMANENT = 'PERMANENT',
|
||||
}
|
||||
export enum KillSwitchLevel {
|
||||
FREEZE_NEW_WORK = 'FREEZE_NEW_WORK',
|
||||
STOP_AGENTS = 'STOP_AGENTS',
|
||||
DISABLE_MUTATIONS = 'DISABLE_MUTATIONS',
|
||||
EMERGENCY_HALT = 'EMERGENCY_HALT',
|
||||
}
|
||||
|
||||
const transitions: Record<WorkflowState, WorkflowState[]> = {
|
||||
DRAFT: [WorkflowState.GATE1_PENDING, WorkflowState.CANCELLED],
|
||||
GATE1_HELD: [WorkflowState.GATE1_APPROVED, WorkflowState.REJECTED],
|
||||
GATE1_PENDING: [
|
||||
WorkflowState.GATE1_HELD,
|
||||
WorkflowState.GATE1_APPROVED,
|
||||
WorkflowState.REJECTED,
|
||||
WorkflowState.EXPIRED,
|
||||
],
|
||||
GATE1_APPROVED: [WorkflowState.READY, WorkflowState.CANCELLED],
|
||||
READY: [
|
||||
WorkflowState.CLAIMED,
|
||||
WorkflowState.BLOCKED,
|
||||
WorkflowState.CANCELLED,
|
||||
],
|
||||
CLAIMED: [
|
||||
WorkflowState.RUNNING,
|
||||
WorkflowState.READY,
|
||||
WorkflowState.CANCELLED,
|
||||
],
|
||||
RUNNING: [
|
||||
WorkflowState.VERIFYING,
|
||||
WorkflowState.FAILED,
|
||||
WorkflowState.BLOCKED,
|
||||
],
|
||||
VERIFYING: [
|
||||
WorkflowState.GATE2_PENDING,
|
||||
WorkflowState.FAILED,
|
||||
WorkflowState.RUNNING,
|
||||
],
|
||||
GATE2_PENDING: [
|
||||
WorkflowState.GATE2_APPROVED,
|
||||
WorkflowState.REJECTED,
|
||||
WorkflowState.EXPIRED,
|
||||
],
|
||||
GATE2_APPROVED: [WorkflowState.RELEASING, WorkflowState.CANCELLED],
|
||||
RELEASING: [WorkflowState.PRODUCTION_VERIFYING, WorkflowState.FAILED],
|
||||
PRODUCTION_VERIFYING: [WorkflowState.LIVE_VERIFIED, WorkflowState.FAILED],
|
||||
LIVE_VERIFIED: [WorkflowState.MEASURING, WorkflowState.CLOSED],
|
||||
MEASURING: [WorkflowState.CLOSED, WorkflowState.FAILED],
|
||||
CLOSED: [],
|
||||
REJECTED: [],
|
||||
BLOCKED: [WorkflowState.READY, WorkflowState.CANCELLED],
|
||||
CANCELLED: [],
|
||||
FAILED: [WorkflowState.READY, WorkflowState.CANCELLED],
|
||||
EXPIRED: [WorkflowState.CANCELLED],
|
||||
};
|
||||
export function assertTransition(from: WorkflowState, to: WorkflowState): void {
|
||||
if (!transitions[from]?.includes(to))
|
||||
throw new BadRequestException(
|
||||
`Invalid orchestration transition ${from} -> ${to}`,
|
||||
);
|
||||
}
|
||||
export function nextStateForGateDecision(
|
||||
gate: 1 | 2,
|
||||
decision: 'APPROVED' | 'REJECTED',
|
||||
): WorkflowState {
|
||||
if (!(['APPROVED', 'REJECTED'] as string[]).includes(decision))
|
||||
throw new BadRequestException('Decision must be APPROVED or REJECTED');
|
||||
return decision === 'REJECTED'
|
||||
? WorkflowState.REJECTED
|
||||
: gate === 1
|
||||
? WorkflowState.GATE1_APPROVED
|
||||
: WorkflowState.GATE2_APPROVED;
|
||||
}
|
||||
@@ -1,577 +0,0 @@
|
||||
import { OrchestrationController } from './orchestration.controller';
|
||||
import { DepartmentTasksController } from '../admin/department-tasks.controller';
|
||||
import { ApprovalsController } from '../admin/approvals.controller';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { OrchestrationService } from './orchestration.service';
|
||||
import { KillSwitchService } from './kill-switch.service';
|
||||
import { WorkflowState, OperationMode } from './orchestration.domain';
|
||||
const enabled = process.env.ORCHESTRATION_INTEGRATION === '1';
|
||||
(enabled ? describe : describe.skip)(
|
||||
'v4 disposable PostgreSQL invariants',
|
||||
() => {
|
||||
const db = new PrismaClient();
|
||||
const service = new OrchestrationService(
|
||||
db as never,
|
||||
new KillSwitchService(db as never),
|
||||
);
|
||||
let n = 0;
|
||||
async function task(state = 'DRAFT', budget = 100) {
|
||||
return db.orchestrationTask.create({
|
||||
data: {
|
||||
key: `test-${Date.now()}-${n++}`,
|
||||
title: 'test',
|
||||
department: 'TEST',
|
||||
operationMode: 'PREPARE',
|
||||
state,
|
||||
budgetLimitCents: budget,
|
||||
},
|
||||
});
|
||||
}
|
||||
afterAll(async () => db.$disconnect());
|
||||
it('release rejects PREPARE operation class even with gate approval', async()=>{
|
||||
const t=await task('GATE2_APPROVED');
|
||||
await expect(service.command(t.id,'RELEASE_AUTHORIZE',{expectedVersion:1,idempotencyKey:`mode-${t.id}`},'ci')).rejects.toThrow(/operation mode/i);
|
||||
});
|
||||
|
||||
it('append-only ledger rejects direct rewrites in PostgreSQL', async () => {
|
||||
const t = await task();
|
||||
const r = await service.recordBudget(
|
||||
t.id,
|
||||
{
|
||||
amountCents: 50,
|
||||
kind: 'RESERVE',
|
||||
idempotencyKey: `immutable-${t.id}`,
|
||||
},
|
||||
'a',
|
||||
);
|
||||
await expect(
|
||||
db.orchestrationBudgetLedger.update({
|
||||
where: { id: r.id },
|
||||
data: { amountCents: 0 },
|
||||
}),
|
||||
).rejects.toThrow(/append.only/i);
|
||||
await expect(
|
||||
db.orchestrationBudgetLedger.delete({ where: { id: r.id } }),
|
||||
).rejects.toThrow(/append.only/i);
|
||||
});
|
||||
it('generic transitions cannot fake run lifecycle or production verification', async () => {
|
||||
for (const [from, to] of [
|
||||
['CLAIMED', 'RUNNING'],
|
||||
['RUNNING', 'VERIFYING'],
|
||||
['FAILED', 'READY'],
|
||||
['PRODUCTION_VERIFYING', 'LIVE_VERIFIED'],
|
||||
]) {
|
||||
const t = await task(from);
|
||||
await expect(
|
||||
service.transition(
|
||||
t.id,
|
||||
{
|
||||
expectedVersion: 1,
|
||||
idempotencyKey: `fake-${t.id}`,
|
||||
toState: to as WorkflowState,
|
||||
},
|
||||
'agent',
|
||||
),
|
||||
).rejects.toThrow(/dedicated/i);
|
||||
}
|
||||
});
|
||||
it('exposes command and reconcile HTTP handlers', () => {
|
||||
const c = new OrchestrationController(service);
|
||||
expect(typeof c.command).toBe('function');
|
||||
expect(typeof c.reconcile).toBe('function');
|
||||
});
|
||||
it('highest kill level blocks reads and reconciliation', async () => {
|
||||
const t = await task();
|
||||
const r = await service.recordBudget(
|
||||
t.id,
|
||||
{
|
||||
kind: 'RESERVE',
|
||||
amountCents: 10,
|
||||
idempotencyKey: `stop-res-${t.id}`,
|
||||
},
|
||||
'a',
|
||||
);
|
||||
await service.setKillSwitch(
|
||||
['EMERGENCY', 'HALT'].join('_'),
|
||||
'test',
|
||||
'ceo',
|
||||
);
|
||||
try {
|
||||
await expect(service.findOne(t.id)).rejects.toThrow(/kill switch/i);
|
||||
await expect(
|
||||
service.reconcileBudget(
|
||||
t.id,
|
||||
{
|
||||
reservationId: r.id,
|
||||
actualCents: 0,
|
||||
idempotencyKey: `stop-recon-${t.id}`,
|
||||
},
|
||||
'a',
|
||||
),
|
||||
).rejects.toThrow(/kill switch/i);
|
||||
} finally {
|
||||
await db.orchestrationKillSwitch.deleteMany();
|
||||
}
|
||||
});
|
||||
it('legacy arbitrary status and approval endpoints fail closed', async () => {
|
||||
await expect(
|
||||
new DepartmentTasksController(db as never).create({
|
||||
title: 'bypass',
|
||||
status: 'DONE',
|
||||
}),
|
||||
).rejects.toThrow(/v4/i);
|
||||
await expect(
|
||||
new DepartmentTasksController(db as never).updateStatus('none', {
|
||||
status: 'DONE',
|
||||
}),
|
||||
).rejects.toThrow(/v4/i);
|
||||
await expect(
|
||||
new ApprovalsController(db as never).decide('none', {
|
||||
decision: 'APPROVED',
|
||||
} as any),
|
||||
).rejects.toThrow(/v4/i);
|
||||
});
|
||||
it('release consumes only matching fresh production Gate2 via dedicated command', async () => {
|
||||
const t = await task('GATE2_PENDING');
|
||||
const hash = 'f'.repeat(64),
|
||||
commitSha = 'a'.repeat(40);
|
||||
const r = await db.orchestrationRun.create({
|
||||
data: {
|
||||
taskId: t.id,
|
||||
status: 'SUCCEEDED',
|
||||
commitSha,
|
||||
environment: 'production',
|
||||
idempotencyKey: `rel-run-${t.id}`,
|
||||
},
|
||||
});
|
||||
await db.orchestrationArtifact.create({
|
||||
data: {
|
||||
runId: r.id,
|
||||
kind: 'BUILD',
|
||||
uri: 'https://ci.example/build',
|
||||
sha256: hash,
|
||||
},
|
||||
});
|
||||
await db.orchestrationAttestation.create({
|
||||
data: {
|
||||
runId: r.id,
|
||||
kind: 'TEST',
|
||||
subjectHash: hash,
|
||||
signer: 'ci',
|
||||
predicate: { passed: true },
|
||||
},
|
||||
});
|
||||
const decision = await service.decideGate(
|
||||
t.id,
|
||||
2,
|
||||
{
|
||||
decision: 'APPROVED',
|
||||
expectedVersion: 1,
|
||||
idempotencyKey: `rel-approve-${t.id}`,
|
||||
expiresAt: new Date(Date.now() + 60000).toISOString(),
|
||||
runId: r.id,
|
||||
artifactHash: hash,
|
||||
commitSha,
|
||||
environment: 'production',
|
||||
},
|
||||
'ceo',
|
||||
);
|
||||
const dto = {
|
||||
expectedVersion: 2,
|
||||
idempotencyKey: `rel-${t.id}`,
|
||||
approvalId: decision.id,
|
||||
runId: r.id,
|
||||
artifactHash: hash,
|
||||
commitSha,
|
||||
environment: 'production',
|
||||
};
|
||||
await expect(
|
||||
(service as any).command(
|
||||
t.id,
|
||||
'RELEASE_AUTHORIZE',
|
||||
{ ...dto, artifactHash: '0'.repeat(64) },
|
||||
'ci',
|
||||
),
|
||||
).rejects.toThrow(/binding/i);
|
||||
const result = await (service as any).command(
|
||||
t.id,
|
||||
'RELEASE_AUTHORIZE',
|
||||
dto,
|
||||
'ci',
|
||||
);
|
||||
expect(result.authorized).toBe(true);
|
||||
expect(
|
||||
(await db.orchestrationTask.findUniqueOrThrow({ where: { id: t.id } }))
|
||||
.state,
|
||||
).toBe('RELEASING');
|
||||
await expect(
|
||||
(service as any).command(
|
||||
t.id,
|
||||
'RELEASE_AUTHORIZE',
|
||||
{ ...dto, expectedVersion: 3, idempotencyKey: `rel-again-${t.id}` },
|
||||
'ci',
|
||||
),
|
||||
).rejects.toThrow();
|
||||
});
|
||||
it('gate decision replay rejects changed immutable expiry and actor', async () => {
|
||||
const t = await task('GATE1_PENDING');
|
||||
const dto = {
|
||||
decision: 'APPROVED' as const,
|
||||
expectedVersion: 1,
|
||||
idempotencyKey: `bind-${t.id}`,
|
||||
expiresAt: new Date(Date.now() + 60000).toISOString(),
|
||||
};
|
||||
await service.decideGate(t.id, 1, dto, 'ceo');
|
||||
await expect(
|
||||
service.decideGate(
|
||||
t.id,
|
||||
1,
|
||||
{ ...dto, expiresAt: new Date(Date.now() + 120000).toISOString() },
|
||||
'ceo',
|
||||
),
|
||||
).rejects.toThrow(/Idempotency/i);
|
||||
await expect(service.decideGate(t.id, 1, dto, 'other')).rejects.toThrow(
|
||||
/Idempotency/i,
|
||||
);
|
||||
});
|
||||
it('dependencies block admission and reject cycles', async () => {
|
||||
const a = await task();
|
||||
const b = await task();
|
||||
await (service as any).command(
|
||||
a.id,
|
||||
'DEPENDENCY_ADD',
|
||||
{
|
||||
expectedVersion: 1,
|
||||
idempotencyKey: `dep-a-${a.id}`,
|
||||
dependsOnTaskId: b.id,
|
||||
},
|
||||
'actor',
|
||||
);
|
||||
await expect(
|
||||
(service as any).command(
|
||||
b.id,
|
||||
'DEPENDENCY_ADD',
|
||||
{
|
||||
expectedVersion: 1,
|
||||
idempotencyKey: `dep-b-${b.id}`,
|
||||
dependsOnTaskId: a.id,
|
||||
},
|
||||
'actor',
|
||||
),
|
||||
).rejects.toThrow(/cycle/i);
|
||||
await db.orchestrationTask.update({
|
||||
where: { id: a.id },
|
||||
data: { state: 'GATE1_PENDING' },
|
||||
});
|
||||
await service.decideGate(
|
||||
a.id,
|
||||
1,
|
||||
{
|
||||
decision: 'APPROVED',
|
||||
expectedVersion: 2,
|
||||
idempotencyKey: `dep-approval-${a.id}`,
|
||||
expiresAt: new Date(Date.now() + 60000).toISOString(),
|
||||
},
|
||||
'ceo',
|
||||
);
|
||||
await expect(
|
||||
service.transition(
|
||||
a.id,
|
||||
{
|
||||
toState: WorkflowState.READY,
|
||||
expectedVersion: 3,
|
||||
idempotencyKey: `dep-ready-${a.id}`,
|
||||
},
|
||||
'agent',
|
||||
),
|
||||
).rejects.toThrow(/dependency/i);
|
||||
});
|
||||
it('registers artifact and attestation for owned run, blocks unregistered subjects', async () => {
|
||||
const t = await task('CLAIMED');
|
||||
const base = {
|
||||
expectedVersion: 1,
|
||||
idempotencyKey: `life-${t.id}`,
|
||||
commitSha: 'c'.repeat(40),
|
||||
environment: 'staging',
|
||||
};
|
||||
const r = await (service as any).command(
|
||||
t.id,
|
||||
'RUN_CREATE',
|
||||
base,
|
||||
'agent',
|
||||
);
|
||||
const hash = 'd'.repeat(64);
|
||||
const a = await (service as any).command(
|
||||
t.id,
|
||||
'ARTIFACT_ADD',
|
||||
{
|
||||
expectedVersion: 2,
|
||||
idempotencyKey: `art-${t.id}`,
|
||||
runId: r.id,
|
||||
sha256: hash,
|
||||
kind: 'BUILD',
|
||||
uri: 'https://ci.example/artifact',
|
||||
},
|
||||
'agent',
|
||||
);
|
||||
expect(a.sha256).toBe(hash);
|
||||
await expect(
|
||||
(service as any).command(
|
||||
t.id,
|
||||
'ATTESTATION_ADD',
|
||||
{
|
||||
expectedVersion: 3,
|
||||
idempotencyKey: `bad-att-${t.id}`,
|
||||
runId: r.id,
|
||||
subjectHash: 'e'.repeat(64),
|
||||
kind: 'TEST',
|
||||
predicate: { passed: true },
|
||||
},
|
||||
'ci',
|
||||
),
|
||||
).rejects.toThrow(/artifact/i);
|
||||
const att = await (service as any).command(
|
||||
t.id,
|
||||
'ATTESTATION_ADD',
|
||||
{
|
||||
expectedVersion: 3,
|
||||
idempotencyKey: `att-${t.id}`,
|
||||
runId: r.id,
|
||||
subjectHash: hash,
|
||||
kind: 'TEST',
|
||||
predicate: { passed: true },
|
||||
},
|
||||
'ci',
|
||||
);
|
||||
expect(att.signer).toBe('ci');
|
||||
});
|
||||
it('creates immutable versioned runs with bounded transient retries', async () => {
|
||||
const t = await task('CLAIMED');
|
||||
const dto = {
|
||||
expectedVersion: 1,
|
||||
idempotencyKey: `run-${t.id}`,
|
||||
commitSha: 'a'.repeat(40),
|
||||
environment: 'staging',
|
||||
};
|
||||
const r = await (service as any).command(
|
||||
t.id,
|
||||
'RUN_CREATE',
|
||||
dto,
|
||||
'agent',
|
||||
);
|
||||
expect(r.attempt).toBe(1);
|
||||
expect(
|
||||
await (service as any).command(t.id, 'RUN_CREATE', dto, 'agent'),
|
||||
).toEqual(r);
|
||||
await expect(
|
||||
(service as any).command(
|
||||
t.id,
|
||||
'RUN_CREATE',
|
||||
{ ...dto, commitSha: 'b'.repeat(40) },
|
||||
'agent',
|
||||
),
|
||||
).rejects.toThrow(/Idempotency/i);
|
||||
await (service as any).command(
|
||||
t.id,
|
||||
'RUN_FINISH',
|
||||
{
|
||||
expectedVersion: 2,
|
||||
idempotencyKey: `finish-${t.id}`,
|
||||
runId: r.id,
|
||||
status: 'FAILED',
|
||||
retryClass: 'POLICY',
|
||||
},
|
||||
'agent',
|
||||
);
|
||||
await expect(
|
||||
(service as any).command(
|
||||
t.id,
|
||||
'RUN_CREATE',
|
||||
{ ...dto, expectedVersion: 3, idempotencyKey: `retry-${t.id}` },
|
||||
'agent',
|
||||
),
|
||||
).rejects.toThrow(/retry/i);
|
||||
});
|
||||
it('selects tier at admission using backend risk and budget', async () => {
|
||||
const t = await task('GATE1_PENDING', 1000);
|
||||
await service.decideGate(
|
||||
t.id,
|
||||
1,
|
||||
{
|
||||
decision: 'APPROVED',
|
||||
expectedVersion: 1,
|
||||
idempotencyKey: `approve-tier-${t.id}`,
|
||||
expiresAt: new Date(Date.now() + 60000).toISOString(),
|
||||
},
|
||||
'ceo',
|
||||
);
|
||||
await service.transition(
|
||||
t.id,
|
||||
{
|
||||
toState: WorkflowState.READY,
|
||||
expectedVersion: 2,
|
||||
idempotencyKey: `admit-tier-${t.id}`,
|
||||
},
|
||||
'agent',
|
||||
);
|
||||
expect(
|
||||
(await db.orchestrationTask.findUniqueOrThrow({ where: { id: t.id } }))
|
||||
.modelTier,
|
||||
).toBe('L0');
|
||||
});
|
||||
it('STOP_AGENTS prevents claims and running transitions', async () => {
|
||||
await service.setKillSwitch('STOP_AGENTS', 'test', 'ceo');
|
||||
try {
|
||||
const t = await task('READY');
|
||||
await expect(
|
||||
service.transition(
|
||||
t.id,
|
||||
{
|
||||
toState: WorkflowState.CLAIMED,
|
||||
expectedVersion: 1,
|
||||
idempotencyKey: `kill-${t.id}`,
|
||||
},
|
||||
'agent',
|
||||
),
|
||||
).rejects.toThrow(/kill switch/i);
|
||||
} finally {
|
||||
await db.orchestrationKillSwitch.deleteMany();
|
||||
}
|
||||
});
|
||||
it('Gate1 admission consumes only unexpired approval', async () => {
|
||||
const t = await task('GATE1_PENDING');
|
||||
await service.decideGate(
|
||||
t.id,
|
||||
1,
|
||||
{
|
||||
decision: 'APPROVED',
|
||||
expectedVersion: 1,
|
||||
idempotencyKey: `approve-${t.id}`,
|
||||
expiresAt: new Date(Date.now() + 100).toISOString(),
|
||||
},
|
||||
'ceo',
|
||||
);
|
||||
await new Promise((r) => setTimeout(r, 150));
|
||||
await expect(
|
||||
service.transition(
|
||||
t.id,
|
||||
{
|
||||
toState: WorkflowState.READY,
|
||||
expectedVersion: 2,
|
||||
idempotencyKey: `admit-${t.id}`,
|
||||
},
|
||||
'agent',
|
||||
),
|
||||
).rejects.toThrow(/expired/i);
|
||||
});
|
||||
it('versioned transition replay returns original result and rejects changed payload', async () => {
|
||||
const t = await task();
|
||||
const dto = {
|
||||
toState: WorkflowState.GATE1_PENDING,
|
||||
expectedVersion: 1,
|
||||
idempotencyKey: `tr-${t.id}`,
|
||||
};
|
||||
const first = await service.transition(t.id, dto, 'actor');
|
||||
expect(await service.transition(t.id, dto, 'actor')).toEqual(first);
|
||||
await expect(
|
||||
service.transition(t.id, { ...dto, reason: 'different' }, 'actor'),
|
||||
).rejects.toThrow(/Idempotency/i);
|
||||
});
|
||||
it('reconciles a reservation exactly once without erasing spent cost', async () => {
|
||||
const t = await task();
|
||||
const r = await service.recordBudget(
|
||||
t.id,
|
||||
{ amountCents: 80, kind: 'RESERVE', idempotencyKey: `res-${t.id}` },
|
||||
'agent',
|
||||
);
|
||||
const dto = {
|
||||
reservationId: r.id,
|
||||
actualCents: 35,
|
||||
idempotencyKey: `reconcile-${t.id}`,
|
||||
};
|
||||
await (service as any).reconcileBudget(t.id, dto, 'agent');
|
||||
await (service as any).reconcileBudget(t.id, dto, 'agent');
|
||||
await expect(
|
||||
(service as any).reconcileBudget(
|
||||
t.id,
|
||||
{ ...dto, idempotencyKey: `second-${t.id}` },
|
||||
'agent',
|
||||
),
|
||||
).rejects.toThrow(/reconciled/i);
|
||||
expect(
|
||||
(
|
||||
await db.orchestrationBudgetLedger.aggregate({
|
||||
where: { taskId: t.id },
|
||||
_sum: { amountCents: true },
|
||||
})
|
||||
)._sum.amountCents,
|
||||
).toBe(35);
|
||||
await expect(
|
||||
service.recordBudget(
|
||||
t.id,
|
||||
{
|
||||
amountCents: 66,
|
||||
kind: 'RESERVE',
|
||||
idempotencyKey: `too-much-${t.id}`,
|
||||
},
|
||||
'agent',
|
||||
),
|
||||
).rejects.toThrow(/budget/i);
|
||||
});
|
||||
it('concurrent budget reservations never exceed hard cap and replay is harmless', async () => {
|
||||
const t = await task();
|
||||
const result = await Promise.allSettled(
|
||||
Array.from({ length: 8 }, (_, i) =>
|
||||
service.recordBudget(
|
||||
t.id,
|
||||
{
|
||||
amountCents: 30,
|
||||
kind: 'RESERVE',
|
||||
idempotencyKey: `reserve-${t.id}-${i}`,
|
||||
},
|
||||
'agent',
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(result.filter((x) => x.status === 'fulfilled')).toHaveLength(3);
|
||||
const sum = await db.orchestrationBudgetLedger.aggregate({
|
||||
where: { taskId: t.id },
|
||||
_sum: { amountCents: true },
|
||||
});
|
||||
expect(sum._sum.amountCents).toBe(90);
|
||||
const first = await db.orchestrationBudgetLedger.findFirstOrThrow({
|
||||
where: { taskId: t.id },
|
||||
});
|
||||
await service.recordBudget(
|
||||
t.id,
|
||||
{
|
||||
amountCents: 30,
|
||||
kind: 'RESERVE',
|
||||
idempotencyKey: first.idempotencyKey,
|
||||
},
|
||||
'agent',
|
||||
);
|
||||
expect(
|
||||
await db.orchestrationBudgetLedger.count({ where: { taskId: t.id } }),
|
||||
).toBe(3);
|
||||
});
|
||||
it('generic transition cannot approve gate 1', async () => {
|
||||
const t = await task('GATE1_PENDING');
|
||||
await expect(
|
||||
service.transition(
|
||||
t.id,
|
||||
{
|
||||
toState: WorkflowState.GATE1_APPROVED,
|
||||
expectedVersion: 1,
|
||||
idempotencyKey: `gate-bypass-${n}`,
|
||||
},
|
||||
'ceo',
|
||||
),
|
||||
).rejects.toThrow(/gate/i);
|
||||
expect(
|
||||
(await db.orchestrationTask.findUniqueOrThrow({ where: { id: t.id } }))
|
||||
.state,
|
||||
).toBe('GATE1_PENDING');
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -1,34 +0,0 @@
|
||||
import { EditorialExecutionService } from '../orchestration-worker/editorial-execution';
|
||||
import {
|
||||
PrepareWorkflowService,
|
||||
PREPARE_WORKFLOW_CONFIG,
|
||||
} from './prepare-workflow.service';
|
||||
import { workflowConfig } from './workflow-config';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
import { KillSwitchService } from './kill-switch.service';
|
||||
import { ModelTierPolicyService } from './model-tier-policy.service';
|
||||
import { OrchestrationController } from './orchestration.controller';
|
||||
import { OrchestrationService } from './orchestration.service';
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [OrchestrationController],
|
||||
providers: [
|
||||
EditorialExecutionService,
|
||||
OrchestrationService,
|
||||
KillSwitchService,
|
||||
ModelTierPolicyService,
|
||||
PrepareWorkflowService,
|
||||
{
|
||||
provide: PREPARE_WORKFLOW_CONFIG,
|
||||
useFactory: () => workflowConfig(process.env),
|
||||
},
|
||||
],
|
||||
exports: [
|
||||
OrchestrationService,
|
||||
KillSwitchService,
|
||||
ModelTierPolicyService,
|
||||
PrepareWorkflowService,
|
||||
],
|
||||
})
|
||||
export class OrchestrationModule {}
|
||||
@@ -1,260 +0,0 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
ForbiddenException,
|
||||
} from '@nestjs/common';
|
||||
import { OrchestrationService } from './orchestration.service';
|
||||
|
||||
function mockPrisma() {
|
||||
const tx: any = {
|
||||
$queryRaw: jest.fn().mockResolvedValue([]),
|
||||
orchestrationTask: {
|
||||
findUnique: jest.fn(),
|
||||
create: jest.fn(),
|
||||
updateMany: jest.fn(),
|
||||
},
|
||||
orchestrationRun: { findUnique: jest.fn(), create: jest.fn() },
|
||||
orchestrationArtifact: { findFirst: jest.fn(), create: jest.fn() },
|
||||
orchestrationAttestation: { create: jest.fn() },
|
||||
orchestrationApprovalDecision: { findUnique: jest.fn(), create: jest.fn() },
|
||||
orchestrationEvent: { findUnique: jest.fn(), create: jest.fn() },
|
||||
orchestrationBudgetLedger: {
|
||||
findUnique: jest.fn(),
|
||||
aggregate: jest.fn(),
|
||||
create: jest.fn(),
|
||||
},
|
||||
orchestrationKillSwitch: { findUnique: jest.fn(), upsert: jest.fn() },
|
||||
};
|
||||
tx.$transaction = jest.fn((cb: any) => Promise.resolve(cb(tx)));
|
||||
return tx;
|
||||
}
|
||||
|
||||
describe('OrchestrationService', () => {
|
||||
let prisma: any;
|
||||
let service: OrchestrationService;
|
||||
beforeEach(() => {
|
||||
prisma = mockPrisma();
|
||||
service = new OrchestrationService(prisma, {
|
||||
assertAllowed: jest.fn(),
|
||||
} as never);
|
||||
});
|
||||
|
||||
it('rejects an unknown kill level before overwriting persisted protection', async () => {
|
||||
await expect(service.setKillSwitch('INVALID_LEVEL','test','admin')).rejects.toThrow(BadRequestException);
|
||||
expect(prisma.orchestrationKillSwitch.upsert).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('creates a task and append-only event transactionally', async () => {
|
||||
prisma.orchestrationTask.create.mockResolvedValue({
|
||||
id: 'task-1',
|
||||
state: 'DRAFT',
|
||||
version: 1,
|
||||
});
|
||||
const result = await service.createTask(
|
||||
{
|
||||
key: 'seo-1',
|
||||
title: 'SEO',
|
||||
department: 'SEO',
|
||||
operationMode: 'PREPARE' as never,
|
||||
budgetLimitCents: 1000,
|
||||
},
|
||||
'actor-1',
|
||||
);
|
||||
expect(prisma.$transaction).toHaveBeenCalledTimes(1);
|
||||
expect(prisma.orchestrationTask.create).toHaveBeenCalledWith({
|
||||
data: expect.objectContaining({ state: 'DRAFT', version: 1 }),
|
||||
});
|
||||
expect(prisma.orchestrationEvent.create).toHaveBeenCalledWith({
|
||||
data: expect.objectContaining({ eventType: 'TASK_CREATED', sequence: 1 }),
|
||||
});
|
||||
expect(result.id).toBe('task-1');
|
||||
});
|
||||
|
||||
it('uses compare-and-swap versioning for transitions', async () => {
|
||||
prisma.orchestrationTask.findUnique.mockResolvedValue({
|
||||
id: 'task-1',
|
||||
state: 'DRAFT',
|
||||
version: 3,
|
||||
});
|
||||
prisma.orchestrationTask.updateMany.mockResolvedValue({ count: 0 });
|
||||
await expect(
|
||||
service.transition(
|
||||
'task-1',
|
||||
{
|
||||
toState: 'GATE1_PENDING' as never,
|
||||
expectedVersion: 3,
|
||||
idempotencyKey: 'i-1',
|
||||
},
|
||||
'actor',
|
||||
),
|
||||
).rejects.toThrow(ConflictException);
|
||||
});
|
||||
|
||||
it('makes gate decisions immutable and idempotent', async () => {
|
||||
const expiresAt = new Date(Date.now() + 10000).toISOString();
|
||||
prisma.orchestrationApprovalDecision.findUnique.mockResolvedValue({
|
||||
id: 'd1',
|
||||
decision: 'APPROVED',
|
||||
gate: 1,
|
||||
taskId: 'task-1',
|
||||
});
|
||||
prisma.orchestrationEvent.findUnique.mockResolvedValue({
|
||||
payload: {
|
||||
fingerprint: JSON.stringify({
|
||||
taskId: 'task-1',
|
||||
gate: 1,
|
||||
dto: {
|
||||
decision: 'APPROVED',
|
||||
expectedVersion: 2,
|
||||
idempotencyKey: 'gate-1',
|
||||
expiresAt,
|
||||
},
|
||||
actorId: 'ceo',
|
||||
}),
|
||||
},
|
||||
});
|
||||
await expect(
|
||||
service.decideGate(
|
||||
'task-1',
|
||||
1,
|
||||
{
|
||||
decision: 'APPROVED',
|
||||
expectedVersion: 2,
|
||||
idempotencyKey: 'gate-1',
|
||||
expiresAt,
|
||||
},
|
||||
'ceo',
|
||||
),
|
||||
).resolves.toMatchObject({ id: 'd1' });
|
||||
expect(prisma.orchestrationApprovalDecision.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects an expired approval', async () => {
|
||||
await expect(
|
||||
service.decideGate(
|
||||
'task-1',
|
||||
1,
|
||||
{
|
||||
decision: 'APPROVED',
|
||||
expectedVersion: 2,
|
||||
idempotencyKey: 'gate-expired',
|
||||
expiresAt: new Date(0).toISOString(),
|
||||
},
|
||||
'ceo',
|
||||
),
|
||||
).rejects.toThrow(BadRequestException);
|
||||
});
|
||||
|
||||
it('binds gate 2 approval to run, artifact hash, commit and environment', async () => {
|
||||
prisma.orchestrationApprovalDecision.findUnique.mockResolvedValue(null);
|
||||
prisma.orchestrationTask.findUnique.mockResolvedValue({
|
||||
id: 'task-1',
|
||||
state: 'GATE2_PENDING',
|
||||
version: 7,
|
||||
});
|
||||
prisma.orchestrationRun.findUnique.mockResolvedValue({
|
||||
id: 'run-1',
|
||||
taskId: 'task-1',
|
||||
commitSha: 'abc123',
|
||||
environment: 'staging',
|
||||
status: 'SUCCEEDED',
|
||||
});
|
||||
prisma.orchestrationArtifact.findFirst.mockResolvedValue({
|
||||
id: 'artifact-1',
|
||||
runId: 'run-1',
|
||||
sha256: 'f'.repeat(64),
|
||||
});
|
||||
prisma.orchestrationTask.updateMany.mockResolvedValue({ count: 1 });
|
||||
prisma.orchestrationApprovalDecision.create.mockResolvedValue({
|
||||
id: 'decision-2',
|
||||
gate: 2,
|
||||
});
|
||||
await service.decideGate(
|
||||
'task-1',
|
||||
2,
|
||||
{
|
||||
decision: 'APPROVED',
|
||||
expectedVersion: 7,
|
||||
idempotencyKey: 'gate-2',
|
||||
expiresAt: new Date(Date.now() + 10000).toISOString(),
|
||||
runId: 'run-1',
|
||||
artifactHash: 'f'.repeat(64),
|
||||
commitSha: 'abc123',
|
||||
environment: 'staging',
|
||||
},
|
||||
'ceo',
|
||||
);
|
||||
expect(prisma.orchestrationApprovalDecision.create).toHaveBeenCalledWith({
|
||||
data: expect.objectContaining({
|
||||
runId: 'run-1',
|
||||
artifactHash: 'f'.repeat(64),
|
||||
commitSha: 'abc123',
|
||||
environment: 'staging',
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
it.each(['RUNNING', 'FAILED', 'CANCELLED'])('refuses Gate 2 approval for an unfinished or unsuccessful %s run', async (status) => {
|
||||
prisma.orchestrationTask.findUnique.mockResolvedValue({id:'task-1',state:'GATE2_PENDING',version:7});
|
||||
prisma.orchestrationRun.findUnique.mockResolvedValue({id:'run-1',taskId:'task-1',commitSha:'abc123',environment:'staging',status});
|
||||
prisma.orchestrationArtifact.findFirst.mockResolvedValue({id:'artifact-1'});
|
||||
prisma.orchestrationTask.updateMany.mockResolvedValue({count:1});
|
||||
await expect(service.decideGate('task-1',2,{
|
||||
decision:'APPROVED',expectedVersion:7,idempotencyKey:'failed-run',
|
||||
expiresAt:new Date(Date.now()+10000).toISOString(),runId:'run-1',
|
||||
artifactHash:'f'.repeat(64),commitSha:'abc123',environment:'staging',
|
||||
},'ceo')).rejects.toThrow(/successful run/i);
|
||||
expect(prisma.orchestrationApprovalDecision.create).not.toHaveBeenCalled();
|
||||
expect(prisma.orchestrationTask.updateMany).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('refuses gate 2 when binding does not match the immutable run', async () => {
|
||||
prisma.orchestrationApprovalDecision.findUnique.mockResolvedValue(null);
|
||||
prisma.orchestrationTask.findUnique.mockResolvedValue({
|
||||
id: 'task-1',
|
||||
state: 'GATE2_PENDING',
|
||||
version: 7,
|
||||
});
|
||||
prisma.orchestrationRun.findUnique.mockResolvedValue({
|
||||
id: 'run-1',
|
||||
taskId: 'task-1',
|
||||
commitSha: 'real',
|
||||
environment: 'staging',
|
||||
});
|
||||
await expect(
|
||||
service.decideGate(
|
||||
'task-1',
|
||||
2,
|
||||
{
|
||||
decision: 'APPROVED',
|
||||
expectedVersion: 7,
|
||||
idempotencyKey: 'gate-bad',
|
||||
expiresAt: new Date(Date.now() + 10000).toISOString(),
|
||||
runId: 'run-1',
|
||||
artifactHash: 'f'.repeat(64),
|
||||
commitSha: 'wrong',
|
||||
environment: 'staging',
|
||||
},
|
||||
'ceo',
|
||||
),
|
||||
).rejects.toThrow(ForbiddenException);
|
||||
});
|
||||
|
||||
it('enforces hard task budget atomically', async () => {
|
||||
prisma.orchestrationTask.findUnique.mockResolvedValue({
|
||||
id: 'task-1',
|
||||
budgetLimitCents: 1000,
|
||||
});
|
||||
prisma.orchestrationBudgetLedger.aggregate.mockResolvedValue({
|
||||
_sum: { amountCents: 900 },
|
||||
});
|
||||
await expect(
|
||||
service.recordBudget(
|
||||
'task-1',
|
||||
{ amountCents: 101, idempotencyKey: 'cost-1', kind: 'SPEND' },
|
||||
'agent',
|
||||
),
|
||||
).rejects.toThrow(ForbiddenException);
|
||||
expect(prisma.orchestrationBudgetLedger.create).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -1,667 +0,0 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
ForbiddenException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import {
|
||||
CreateOrchestrationTaskDto,
|
||||
GateDecisionDto,
|
||||
RecordBudgetDto,
|
||||
TransitionTaskDto,
|
||||
} from './dto/orchestration.dto';
|
||||
import {
|
||||
KillSwitchLevel,
|
||||
assertTransition,
|
||||
nextStateForGateDecision,
|
||||
OperationMode,
|
||||
WorkflowState,
|
||||
} from './orchestration.domain';
|
||||
import { ModelTierPolicyService } from './model-tier-policy.service';
|
||||
import { KillSwitchService } from './kill-switch.service';
|
||||
@Injectable()
|
||||
export class OrchestrationService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly killSwitch: KillSwitchService,
|
||||
) {}
|
||||
async createTask(dto: CreateOrchestrationTaskDto, actorId: string) {
|
||||
await this.killSwitch.assertAllowed('CREATE_TASK');
|
||||
return await (this.prisma as any).$transaction(async (tx: any) => {
|
||||
const task = await tx.orchestrationTask.create({
|
||||
data: {
|
||||
key: dto.key,
|
||||
title: dto.title,
|
||||
department: dto.department,
|
||||
operationMode: dto.operationMode,
|
||||
budgetLimitCents: dto.budgetLimitCents,
|
||||
metadata: dto.metadata ?? {},
|
||||
state: WorkflowState.DRAFT,
|
||||
version: 1,
|
||||
},
|
||||
});
|
||||
await tx.orchestrationEvent.create({
|
||||
data: {
|
||||
taskId: task.id,
|
||||
eventType: 'TASK_CREATED',
|
||||
fromState: null,
|
||||
toState: WorkflowState.DRAFT,
|
||||
sequence: 1,
|
||||
actorId,
|
||||
payload: { key: dto.key },
|
||||
},
|
||||
});
|
||||
return task;
|
||||
});
|
||||
}
|
||||
async transition(id: string, dto: TransitionTaskDto, actorId: string) {
|
||||
if (
|
||||
[
|
||||
WorkflowState.GATE1_APPROVED,
|
||||
WorkflowState.GATE2_APPROVED,
|
||||
WorkflowState.RELEASING,
|
||||
WorkflowState.RUNNING,
|
||||
WorkflowState.VERIFYING,
|
||||
WorkflowState.PRODUCTION_VERIFYING,
|
||||
WorkflowState.LIVE_VERIFIED,
|
||||
WorkflowState.MEASURING,
|
||||
WorkflowState.CLOSED,
|
||||
].includes(dto.toState)
|
||||
)
|
||||
throw new ForbiddenException(
|
||||
'Gate approvals and release require dedicated commands',
|
||||
);
|
||||
return await (this.prisma as any).$transaction(async (tx: any) => {
|
||||
await tx.$queryRaw`SELECT id FROM orchestration_tasks WHERE id = ${id}::uuid FOR UPDATE`;
|
||||
const fingerprint = JSON.stringify({ id, dto, actorId });
|
||||
const replay = await tx.orchestrationEvent.findUnique({
|
||||
where: { idempotencyKey: dto.idempotencyKey },
|
||||
});
|
||||
if (replay) {
|
||||
if (replay.payload.fingerprint !== fingerprint)
|
||||
throw new ConflictException('Idempotency payload mismatch');
|
||||
return replay.payload.result;
|
||||
}
|
||||
const existing = await tx.orchestrationTask.findUnique({ where: { id } });
|
||||
if (!existing)
|
||||
throw new NotFoundException(`Orchestration task ${id} not found`);
|
||||
if (existing.state === WorkflowState.FAILED)
|
||||
throw new ForbiddenException('Retries require dedicated run command');
|
||||
assertTransition(existing.state, dto.toState);
|
||||
await this.killSwitch.assertAllowed(
|
||||
[
|
||||
WorkflowState.READY,
|
||||
WorkflowState.CLAIMED,
|
||||
WorkflowState.RUNNING,
|
||||
WorkflowState.VERIFYING,
|
||||
WorkflowState.GATE2_PENDING,
|
||||
].includes(dto.toState)
|
||||
? 'RUN_AGENT'
|
||||
: 'OBSERVE',
|
||||
);
|
||||
if ([WorkflowState.READY, WorkflowState.CLAIMED].includes(dto.toState))
|
||||
await this.killSwitch.assertAllowed('CLAIM_TASK');
|
||||
let selectedTier = existing.modelTier;
|
||||
if (dto.toState === WorkflowState.READY) {
|
||||
const dependencies = await tx.orchestrationDependency.findMany({
|
||||
where: { taskId: id },
|
||||
include: { dependsOnTask: true },
|
||||
});
|
||||
if (dependencies.some((d: any) => d.dependsOnTask.state !== 'CLOSED'))
|
||||
throw new ForbiddenException('Unresolved dependency');
|
||||
const costs = await tx.orchestrationBudgetLedger.aggregate({
|
||||
where: { taskId: id },
|
||||
_sum: { amountCents: true },
|
||||
});
|
||||
selectedTier = new ModelTierPolicyService().select({
|
||||
complexity: Number(existing.metadata?.complexity ?? 0),
|
||||
risk: Number(existing.metadata?.risk ?? 0),
|
||||
operationMode: existing.operationMode,
|
||||
remainingBudgetCents:
|
||||
existing.budgetLimitCents - (costs._sum.amountCents ?? 0),
|
||||
});
|
||||
const approval = await tx.orchestrationApprovalDecision.findFirst({
|
||||
where: { taskId: id, gate: 1, decision: 'APPROVED' },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
if (!approval || approval.expiresAt <= new Date())
|
||||
throw new ForbiddenException('Gate 1 approval missing or expired');
|
||||
}
|
||||
if (
|
||||
existing.operationMode === OperationMode.MUTATE_PRODUCTION &&
|
||||
dto.toState === WorkflowState.RELEASING
|
||||
)
|
||||
await this.killSwitch.assertAllowed('MUTATE_PRODUCTION');
|
||||
const changed = await tx.orchestrationTask.updateMany({
|
||||
where: { id, version: dto.expectedVersion, state: existing.state },
|
||||
data: {
|
||||
state: dto.toState,
|
||||
modelTier: selectedTier,
|
||||
version: { increment: 1 },
|
||||
},
|
||||
});
|
||||
if (changed.count !== 1)
|
||||
throw new ConflictException(
|
||||
'Task version or state changed; reload before retrying',
|
||||
);
|
||||
await tx.orchestrationEvent.create({
|
||||
data: {
|
||||
taskId: id,
|
||||
eventType: 'STATE_TRANSITION',
|
||||
fromState: existing.state,
|
||||
toState: dto.toState,
|
||||
sequence: dto.expectedVersion + 1,
|
||||
actorId,
|
||||
idempotencyKey: dto.idempotencyKey,
|
||||
payload: {
|
||||
fingerprint,
|
||||
result: JSON.parse(
|
||||
JSON.stringify({
|
||||
...existing,
|
||||
state: dto.toState,
|
||||
modelTier: selectedTier,
|
||||
version: dto.expectedVersion + 1,
|
||||
}),
|
||||
),
|
||||
},
|
||||
},
|
||||
});
|
||||
return JSON.parse(
|
||||
JSON.stringify({
|
||||
...existing,
|
||||
state: dto.toState,
|
||||
modelTier: selectedTier,
|
||||
version: dto.expectedVersion + 1,
|
||||
}),
|
||||
);
|
||||
});
|
||||
}
|
||||
async decideGate(
|
||||
taskId: string,
|
||||
gate: 1 | 2,
|
||||
dto: GateDecisionDto,
|
||||
actorId: string,
|
||||
) {
|
||||
if (![1, 2].includes(gate)) throw new BadRequestException('Invalid gate');
|
||||
return await (this.prisma as any).$transaction(async (tx: any) => {
|
||||
await tx.$queryRaw`SELECT id FROM orchestration_tasks WHERE id = ${taskId}::uuid FOR UPDATE`;
|
||||
const fingerprint = JSON.stringify({ taskId, gate, dto, actorId });
|
||||
const prior = await tx.orchestrationApprovalDecision.findUnique({
|
||||
where: { idempotencyKey: dto.idempotencyKey },
|
||||
});
|
||||
if (prior) {
|
||||
const event = await tx.orchestrationEvent.findUnique({
|
||||
where: { idempotencyKey: `event:${dto.idempotencyKey}` },
|
||||
});
|
||||
if (event?.payload?.fingerprint !== fingerprint)
|
||||
throw new ConflictException('Idempotency payload mismatch');
|
||||
return prior;
|
||||
}
|
||||
if (
|
||||
typeof dto.expiresAt !== 'string' ||
|
||||
!Number.isFinite(Date.parse(dto.expiresAt)) ||
|
||||
new Date(dto.expiresAt) <= new Date()
|
||||
)
|
||||
throw new BadRequestException('Approval decision has expired');
|
||||
await this.killSwitch.assertAllowed('OBSERVE');
|
||||
const task = await tx.orchestrationTask.findUnique({
|
||||
where: { id: taskId },
|
||||
});
|
||||
if (!task)
|
||||
throw new NotFoundException(`Orchestration task ${taskId} not found`);
|
||||
const expected =
|
||||
gate === 1 ? WorkflowState.GATE1_PENDING : WorkflowState.GATE2_PENDING;
|
||||
if (task.state !== expected)
|
||||
throw new ConflictException(`Gate ${gate} requires ${expected}`);
|
||||
if (gate === 2 && dto.decision === 'APPROVED') {
|
||||
if (
|
||||
!dto.runId ||
|
||||
!dto.artifactHash ||
|
||||
!dto.commitSha ||
|
||||
!dto.environment
|
||||
)
|
||||
throw new BadRequestException(
|
||||
'Gate 2 requires runId, artifactHash, commitSha and environment',
|
||||
);
|
||||
const run = await tx.orchestrationRun.findUnique({
|
||||
where: { id: dto.runId },
|
||||
});
|
||||
if (
|
||||
!run ||
|
||||
run.taskId !== taskId ||
|
||||
run.commitSha !== dto.commitSha ||
|
||||
run.environment !== dto.environment
|
||||
)
|
||||
throw new ForbiddenException(
|
||||
'Gate 2 binding does not match immutable run',
|
||||
);
|
||||
if (run.status !== 'SUCCEEDED')
|
||||
throw new ForbiddenException('Gate 2 requires a successful run');
|
||||
const artifact = await tx.orchestrationArtifact.findFirst({
|
||||
where: { runId: dto.runId, sha256: dto.artifactHash },
|
||||
});
|
||||
if (!artifact)
|
||||
throw new ForbiddenException(
|
||||
'Gate 2 artifact hash is not registered for run',
|
||||
);
|
||||
}
|
||||
const next = nextStateForGateDecision(gate, dto.decision);
|
||||
const changed = await tx.orchestrationTask.updateMany({
|
||||
where: { id: taskId, version: dto.expectedVersion, state: expected },
|
||||
data: { state: next, version: { increment: 1 } },
|
||||
});
|
||||
if (changed.count !== 1)
|
||||
throw new ConflictException(
|
||||
'Task version or state changed; approval not applied',
|
||||
);
|
||||
const decision = await tx.orchestrationApprovalDecision.create({
|
||||
data: {
|
||||
taskId,
|
||||
gate,
|
||||
decision: dto.decision,
|
||||
actorId,
|
||||
notes: dto.notes,
|
||||
expiresAt: new Date(dto.expiresAt),
|
||||
idempotencyKey: dto.idempotencyKey,
|
||||
runId: dto.runId,
|
||||
artifactHash: dto.artifactHash,
|
||||
commitSha: dto.commitSha,
|
||||
environment: dto.environment,
|
||||
},
|
||||
});
|
||||
await tx.orchestrationEvent.create({
|
||||
data: {
|
||||
taskId,
|
||||
eventType: `GATE${gate}_${dto.decision}`,
|
||||
fromState: expected,
|
||||
toState: next,
|
||||
sequence: dto.expectedVersion + 1,
|
||||
actorId,
|
||||
idempotencyKey: `event:${dto.idempotencyKey}`,
|
||||
payload: { decisionId: decision.id, fingerprint },
|
||||
},
|
||||
});
|
||||
return decision;
|
||||
});
|
||||
}
|
||||
async recordBudget(taskId: string, dto: RecordBudgetDto, actorId: string) {
|
||||
if (
|
||||
!Number.isSafeInteger(dto.amountCents) ||
|
||||
dto.amountCents < 1 ||
|
||||
!['RESERVE', 'SPEND'].includes(dto.kind)
|
||||
)
|
||||
throw new BadRequestException(
|
||||
'Use RESERVE or SPEND with positive integer cents',
|
||||
);
|
||||
return await (this.prisma as any).$transaction(async (tx: any) => {
|
||||
await tx.$queryRaw`SELECT id FROM orchestration_tasks WHERE id = ${taskId}::uuid FOR UPDATE`;
|
||||
const prior = await tx.orchestrationBudgetLedger.findUnique({
|
||||
where: { idempotencyKey: dto.idempotencyKey },
|
||||
});
|
||||
if (prior) {
|
||||
if (
|
||||
prior.taskId !== taskId ||
|
||||
prior.kind !== dto.kind ||
|
||||
prior.amountCents !== dto.amountCents ||
|
||||
prior.actorId !== actorId ||
|
||||
JSON.stringify(prior.metadata) !== JSON.stringify(dto.metadata ?? {})
|
||||
)
|
||||
throw new ConflictException('Idempotency payload mismatch');
|
||||
return prior;
|
||||
}
|
||||
await this.killSwitch.assertAllowed('RUN_AGENT');
|
||||
const task = await tx.orchestrationTask.findUnique({
|
||||
where: { id: taskId },
|
||||
});
|
||||
if (!task)
|
||||
throw new NotFoundException(`Orchestration task ${taskId} not found`);
|
||||
const sum = await tx.orchestrationBudgetLedger.aggregate({
|
||||
where: { taskId },
|
||||
_sum: { amountCents: true },
|
||||
});
|
||||
if ((sum._sum.amountCents ?? 0) + dto.amountCents > task.budgetLimitCents)
|
||||
throw new ForbiddenException('Hard task budget exceeded');
|
||||
return tx.orchestrationBudgetLedger.create({
|
||||
data: {
|
||||
taskId,
|
||||
amountCents: dto.amountCents,
|
||||
kind: dto.kind,
|
||||
idempotencyKey: dto.idempotencyKey,
|
||||
actorId,
|
||||
metadata: dto.metadata ?? {},
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
async command(taskId: string, command: string, dto: any, actorId: string) {
|
||||
return await (this.prisma as any).$transaction(async (tx: any) => {
|
||||
await tx.$queryRaw`SELECT id FROM orchestration_tasks WHERE id = ${taskId}::uuid FOR UPDATE`;
|
||||
const fingerprint = JSON.stringify({ taskId, command, dto, actorId });
|
||||
const replay = await tx.orchestrationEvent.findUnique({
|
||||
where: { idempotencyKey: dto.idempotencyKey },
|
||||
});
|
||||
if (replay) {
|
||||
if (replay.payload.fingerprint !== fingerprint)
|
||||
throw new ConflictException('Idempotency payload mismatch');
|
||||
return replay.payload.result;
|
||||
}
|
||||
const task = await tx.orchestrationTask.findUnique({
|
||||
where: { id: taskId },
|
||||
});
|
||||
if (!task) throw new NotFoundException('Task not found');
|
||||
if (task.version !== dto.expectedVersion)
|
||||
throw new ConflictException('Stale task version');
|
||||
await this.killSwitch.assertAllowed('RUN_AGENT');
|
||||
let result: any;
|
||||
let state = task.state;
|
||||
if (command === 'RUN_CREATE') {
|
||||
if (
|
||||
!/^[a-f0-9]{40,64}$/.test(dto.commitSha) ||
|
||||
!['development', 'staging', 'production'].includes(dto.environment)
|
||||
)
|
||||
throw new BadRequestException('Invalid immutable run binding');
|
||||
const last = await tx.orchestrationRun.findFirst({
|
||||
where: { taskId },
|
||||
orderBy: { attempt: 'desc' },
|
||||
});
|
||||
if (
|
||||
last &&
|
||||
(last.status !== 'FAILED' ||
|
||||
!['TRANSIENT', 'RATE_LIMITED', 'DEPENDENCY'].includes(
|
||||
last.retryClass,
|
||||
) ||
|
||||
last.attempt >= 3)
|
||||
)
|
||||
throw new ForbiddenException('Retry denied or exhausted');
|
||||
if (!last && task.state !== 'CLAIMED')
|
||||
throw new ConflictException('Run requires CLAIMED task');
|
||||
result = await tx.orchestrationRun.create({
|
||||
data: {
|
||||
taskId,
|
||||
attempt: (last?.attempt ?? 0) + 1,
|
||||
status: 'RUNNING',
|
||||
commitSha: dto.commitSha,
|
||||
environment: dto.environment,
|
||||
idempotencyKey: dto.idempotencyKey,
|
||||
startedAt: new Date(),
|
||||
},
|
||||
});
|
||||
state = 'RUNNING';
|
||||
} else if (command === 'RUN_FINISH') {
|
||||
const run = await tx.orchestrationRun.findUnique({
|
||||
where: { id: dto.runId },
|
||||
});
|
||||
if (
|
||||
!run ||
|
||||
run.taskId !== taskId ||
|
||||
run.status !== 'RUNNING' ||
|
||||
task.state !== 'RUNNING'
|
||||
)
|
||||
throw new ConflictException('Active run required');
|
||||
if (
|
||||
!['SUCCEEDED', 'FAILED'].includes(dto.status) ||
|
||||
(dto.status === 'FAILED' &&
|
||||
![
|
||||
'TRANSIENT',
|
||||
'RATE_LIMITED',
|
||||
'DEPENDENCY',
|
||||
'VALIDATION',
|
||||
'POLICY',
|
||||
'PERMANENT',
|
||||
].includes(dto.retryClass))
|
||||
)
|
||||
throw new BadRequestException('Invalid outcome taxonomy');
|
||||
result = await tx.orchestrationRun.update({
|
||||
where: { id: run.id },
|
||||
data: {
|
||||
status: dto.status,
|
||||
retryClass: dto.status === 'FAILED' ? dto.retryClass : null,
|
||||
finishedAt: new Date(),
|
||||
},
|
||||
});
|
||||
state = dto.status === 'FAILED' ? 'FAILED' : 'VERIFYING';
|
||||
} else if (command === 'ARTIFACT_ADD' || command === 'ATTESTATION_ADD') {
|
||||
const run = await tx.orchestrationRun.findUnique({
|
||||
where: { id: dto.runId },
|
||||
});
|
||||
if (
|
||||
!run ||
|
||||
run.taskId !== taskId ||
|
||||
!['RUNNING', 'VERIFYING'].includes(task.state)
|
||||
)
|
||||
throw new ConflictException('Owned active run required');
|
||||
const hash = command === 'ARTIFACT_ADD' ? dto.sha256 : dto.subjectHash;
|
||||
if (!/^[a-f0-9]{64}$/.test(hash) || !dto.kind)
|
||||
throw new BadRequestException('Invalid artifact hash or kind');
|
||||
if (command === 'ARTIFACT_ADD') {
|
||||
if (!/^https:\/\//.test(dto.uri))
|
||||
throw new BadRequestException('Artifact URI must be HTTPS');
|
||||
result = await tx.orchestrationArtifact.create({
|
||||
data: { runId: run.id, kind: dto.kind, sha256: hash, uri: dto.uri },
|
||||
});
|
||||
} else {
|
||||
if (
|
||||
!(await tx.orchestrationArtifact.findFirst({
|
||||
where: { runId: run.id, sha256: hash },
|
||||
}))
|
||||
)
|
||||
throw new ForbiddenException(
|
||||
'Attestation subject is not a registered artifact',
|
||||
);
|
||||
result = await tx.orchestrationAttestation.create({
|
||||
data: {
|
||||
runId: run.id,
|
||||
kind: dto.kind,
|
||||
subjectHash: hash,
|
||||
predicate: dto.predicate ?? {},
|
||||
signer: actorId,
|
||||
signature: dto.signature,
|
||||
},
|
||||
});
|
||||
}
|
||||
} else if (command === 'DEPENDENCY_ADD') {
|
||||
if (task.state !== 'DRAFT')
|
||||
throw new ConflictException(
|
||||
'Dependencies are frozen after submission',
|
||||
);
|
||||
await tx.$executeRaw`LOCK TABLE orchestration_dependencies IN EXCLUSIVE MODE`;
|
||||
const cycles =
|
||||
await tx.$queryRaw`WITH RECURSIVE chain(id) AS (SELECT ${dto.dependsOnTaskId}::uuid UNION SELECT d.depends_on_task_id FROM orchestration_dependencies d JOIN chain c ON d.task_id=c.id) SELECT id FROM chain WHERE id=${taskId}::uuid`;
|
||||
if (cycles.length) throw new ForbiddenException('Dependency cycle');
|
||||
result = await tx.orchestrationDependency.create({
|
||||
data: { taskId, dependsOnTaskId: dto.dependsOnTaskId },
|
||||
});
|
||||
} else if (command === 'RELEASE_AUTHORIZE') {
|
||||
await this.killSwitch.assertAllowed('MUTATE_PRODUCTION');
|
||||
if (task.state !== 'GATE2_APPROVED')
|
||||
throw new ConflictException(
|
||||
'Gate 2 approval required; already consumed or invalid state',
|
||||
);
|
||||
const approval = await tx.orchestrationApprovalDecision.findUnique({
|
||||
where: { id: dto.approvalId },
|
||||
});
|
||||
if (
|
||||
!approval ||
|
||||
approval.taskId !== taskId ||
|
||||
approval.gate !== 2 ||
|
||||
approval.decision !== 'APPROVED' ||
|
||||
approval.expiresAt <= new Date()
|
||||
)
|
||||
throw new ForbiddenException('Gate 2 missing or expired');
|
||||
for (const key of ['runId', 'artifactHash', 'commitSha', 'environment'])
|
||||
if (approval[key] !== dto[key])
|
||||
throw new ForbiddenException('Release binding mismatch');
|
||||
const run = await tx.orchestrationRun.findUnique({
|
||||
where: { id: dto.runId },
|
||||
});
|
||||
if (
|
||||
!run ||
|
||||
run.taskId !== taskId ||
|
||||
run.status !== 'SUCCEEDED' ||
|
||||
run.commitSha !== dto.commitSha ||
|
||||
run.environment !== dto.environment
|
||||
)
|
||||
throw new ForbiddenException('Run binding mismatch');
|
||||
if (
|
||||
!(await tx.orchestrationArtifact.findFirst({
|
||||
where: { runId: run.id, sha256: dto.artifactHash },
|
||||
})) ||
|
||||
!(await tx.orchestrationAttestation.findFirst({
|
||||
where: {
|
||||
runId: run.id,
|
||||
subjectHash: dto.artifactHash,
|
||||
kind: 'TEST',
|
||||
},
|
||||
}))
|
||||
)
|
||||
throw new ForbiddenException('Artifact evidence missing');
|
||||
result = {
|
||||
authorized: true,
|
||||
approvalId: approval.id,
|
||||
runId: run.id,
|
||||
artifactHash: dto.artifactHash,
|
||||
commitSha: dto.commitSha,
|
||||
environment: dto.environment,
|
||||
execution: 'CI_CD_ONLY',
|
||||
};
|
||||
state = 'RELEASING';
|
||||
} else throw new BadRequestException('Unknown command');
|
||||
await tx.orchestrationTask.update({
|
||||
where: { id: taskId },
|
||||
data: { state, version: { increment: 1 } },
|
||||
});
|
||||
const serialized = JSON.parse(JSON.stringify(result));
|
||||
await tx.orchestrationEvent.create({
|
||||
data: {
|
||||
taskId,
|
||||
eventType: command,
|
||||
fromState: task.state,
|
||||
toState: state,
|
||||
sequence: task.version + 1,
|
||||
actorId,
|
||||
idempotencyKey: dto.idempotencyKey,
|
||||
payload: { fingerprint, result: serialized },
|
||||
},
|
||||
});
|
||||
return serialized;
|
||||
});
|
||||
}
|
||||
async reconcileBudget(
|
||||
taskId: string,
|
||||
dto: { reservationId: string; actualCents: number; idempotencyKey: string },
|
||||
actorId: string,
|
||||
) {
|
||||
await this.killSwitch.assertAllowed('OBSERVE');
|
||||
if (!Number.isSafeInteger(dto.actualCents) || dto.actualCents < 0)
|
||||
throw new BadRequestException('Invalid actual cost');
|
||||
return await (this.prisma as any).$transaction(async (tx: any) => {
|
||||
await tx.$queryRaw`SELECT id FROM orchestration_tasks WHERE id = ${taskId}::uuid FOR UPDATE`;
|
||||
const prior = await tx.orchestrationBudgetLedger.findUnique({
|
||||
where: { idempotencyKey: dto.idempotencyKey },
|
||||
});
|
||||
if (prior) {
|
||||
if (
|
||||
prior.taskId !== taskId ||
|
||||
prior.actorId !== actorId ||
|
||||
prior.metadata.reservationId !== dto.reservationId ||
|
||||
prior.metadata.actualCents !== dto.actualCents
|
||||
)
|
||||
throw new ConflictException('Idempotency payload mismatch');
|
||||
return prior;
|
||||
}
|
||||
const reservation = await tx.orchestrationBudgetLedger.findUnique({
|
||||
where: { id: dto.reservationId },
|
||||
});
|
||||
if (
|
||||
!reservation ||
|
||||
reservation.taskId !== taskId ||
|
||||
reservation.kind !== 'RESERVE'
|
||||
)
|
||||
throw new BadRequestException('Reservation not found');
|
||||
if (
|
||||
await tx.orchestrationBudgetLedger.findFirst({
|
||||
where: {
|
||||
taskId,
|
||||
kind: 'RECONCILE',
|
||||
metadata: { path: ['reservationId'], equals: dto.reservationId },
|
||||
},
|
||||
})
|
||||
)
|
||||
throw new ConflictException('Reservation already reconciled');
|
||||
const task = await tx.orchestrationTask.findUnique({
|
||||
where: { id: taskId },
|
||||
});
|
||||
const total = await tx.orchestrationBudgetLedger.aggregate({
|
||||
where: { taskId },
|
||||
_sum: { amountCents: true },
|
||||
});
|
||||
const delta = dto.actualCents - reservation.amountCents;
|
||||
if (total._sum.amountCents + delta > task.budgetLimitCents)
|
||||
throw new ForbiddenException('Hard task budget exceeded');
|
||||
return tx.orchestrationBudgetLedger.create({
|
||||
data: {
|
||||
taskId,
|
||||
kind: 'RECONCILE',
|
||||
amountCents: delta,
|
||||
idempotencyKey: dto.idempotencyKey,
|
||||
actorId,
|
||||
metadata: {
|
||||
reservationId: dto.reservationId,
|
||||
actualCents: dto.actualCents,
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
async findAll() {
|
||||
await this.killSwitch.assertAllowed('OBSERVE');
|
||||
return (this.prisma as any).orchestrationTask.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
include: { decisions: true },
|
||||
});
|
||||
}
|
||||
async findOne(id: string) {
|
||||
await this.killSwitch.assertAllowed('OBSERVE');
|
||||
const task = await (this.prisma as any).orchestrationTask.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
runs: { include: { artifacts: true, attestations: true } },
|
||||
dependencies: true,
|
||||
decisions: true,
|
||||
events: { orderBy: { sequence: 'asc' } },
|
||||
budgetLedger: true,
|
||||
},
|
||||
});
|
||||
if (!task)
|
||||
throw new NotFoundException(`Orchestration task ${id} not found`);
|
||||
return task;
|
||||
}
|
||||
async setKillSwitch(level: string, reason: string, actorId: string) {
|
||||
if (!Object.values(KillSwitchLevel).includes(level as KillSwitchLevel))
|
||||
throw new BadRequestException('Invalid kill switch level');
|
||||
return await (this.prisma as any).$transaction(async (tx: any) => {
|
||||
const value = await tx.orchestrationKillSwitch.upsert({
|
||||
where: { scope: 'GLOBAL' },
|
||||
create: { scope: 'GLOBAL', level, active: true, reason, actorId },
|
||||
update: {
|
||||
level,
|
||||
active: true,
|
||||
reason,
|
||||
actorId,
|
||||
version: { increment: 1 },
|
||||
},
|
||||
});
|
||||
await tx.orchestrationEvent.create({
|
||||
data: {
|
||||
eventType: 'KILL_SWITCH_CHANGED',
|
||||
sequence: value.version ?? 1,
|
||||
actorId,
|
||||
payload: { scope: 'GLOBAL', level, reason },
|
||||
},
|
||||
});
|
||||
return value;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
import { PrepareWorkflowService } from './prepare-workflow.service';
|
||||
|
||||
describe('owner Gate1 hold operation', () => {
|
||||
const reviewer={id:'reviewer',role:'SUPER_ADMIN'};
|
||||
let task:any, tx:any, service:PrepareWorkflowService;
|
||||
beforeEach(()=>{
|
||||
task={id:'t',state:'GATE1_PENDING',version:2,operationMode:'PREPARE',metadata:{workflowScope:'STAGE_PREPARE',createdBy:'proposer'}};
|
||||
tx={prepareWorkflowOutbox:{create:jest.fn()},$queryRaw:jest.fn().mockResolvedValue([]),orchestrationTask:{findUnique:jest.fn(async()=>task),update:jest.fn(async({data}:any)=>(task={...task,state:data.state,version:task.version+1}))},orchestrationEvent:{findUnique:jest.fn(),create:jest.fn()},orchestrationApprovalDecision:{create:jest.fn()}};
|
||||
service=new PrepareWorkflowService({$transaction:async(cb:any)=>cb(tx)}, {workerIds:[],stageCiIds:[],ownerReviewerIds:['12345678-1234-1234-1234-123456789012']} as any);
|
||||
});
|
||||
it('advertises and permits the pinned canonical Admin independent reviewer only',async()=>{
|
||||
const owner={id:'12345678-1234-1234-1234-123456789012',role:'Admin'};
|
||||
expect(service.project(task,owner).availableActions).toEqual(['APPROVE_GATE1','REJECT_GATE1','HOLD_GATE1']);
|
||||
await expect(service.transition('t',hold,owner)).resolves.toMatchObject({state:'GATE1_HELD'});
|
||||
await expect(service.decide('t',1,{decision:'APPROVED',expectedVersion:3,idempotencyKey:'owner-approve',expiresAt:new Date(Date.now()+60000).toISOString()},owner)).resolves.toMatchObject({state:'READY'});
|
||||
expect(tx.orchestrationApprovalDecision.create).toHaveBeenCalledWith({data:expect.objectContaining({actorId:owner.id})});
|
||||
});
|
||||
it.each(['Admin','SUPER_ADMIN'])('never permits proposer self-review even if allowlisted: %s',async role=>{
|
||||
const actor={id:'12345678-1234-1234-1234-123456789012',role}; task.metadata.createdBy=actor.id;
|
||||
expect(service.project(task,actor).availableActions).toEqual([]);
|
||||
await expect(service.transition('t',hold,actor)).rejects.toMatchObject({status:403});
|
||||
await expect(service.decide('t',1,{decision:'REJECTED',expectedVersion:2,idempotencyKey:'self',expiresAt:new Date(Date.now()+60000).toISOString()},actor)).rejects.toMatchObject({status:403});
|
||||
expect(tx.orchestrationTask.update).not.toHaveBeenCalled();
|
||||
});
|
||||
it.each([{id:'other',role:'Admin'},{id:'12345678-1234-1234-1234-123456789012',role:'User'},{id:'12345678-1234-1234-1234-123456789012',role:'ADMIN'}])('denies noncanonical or ungranted reviewer %j',async actor=>{
|
||||
expect(service.project(task,actor).availableActions).toEqual([]);
|
||||
await expect(service.decide('t',1,{decision:'REJECTED',expectedVersion:2,idempotencyKey:'deny',expiresAt:new Date(Date.now()+60000).toISOString()},actor)).rejects.toMatchObject({status:403});
|
||||
expect(tx.orchestrationTask.update).not.toHaveBeenCalled();
|
||||
});
|
||||
const hold={toState:'GATE1_HELD',expectedVersion:2,idempotencyKey:'hold-t',reason:'Need revised budget'};
|
||||
it('advertises hold only to independent owner reviewer',()=>{
|
||||
expect(service.project(task,reviewer).availableActions).toContain('HOLD_GATE1');
|
||||
expect(service.project(task,{id:'proposer',role:'SUPER_ADMIN'}).availableActions).not.toContain('HOLD_GATE1');
|
||||
});
|
||||
it('persists hold with actor, immutable reason and version without approval or queueing',async()=>{
|
||||
await expect(service.transition('t',hold,reviewer)).resolves.toMatchObject({state:'GATE1_HELD',version:3});
|
||||
expect(tx.orchestrationEvent.create).toHaveBeenCalledWith({data:expect.objectContaining({actorId:'reviewer',fromState:'GATE1_PENDING',toState:'GATE1_HELD',eventType:'PREPARE_GATE1_HELD',idempotencyKey:'hold-t',payload:expect.objectContaining({fingerprint:expect.stringContaining('Need revised budget')})})});
|
||||
expect(tx.orchestrationApprovalDecision.create).not.toHaveBeenCalled();
|
||||
expect(service.project(task,reviewer).availableActions).toEqual(['APPROVE_GATE1','REJECT_GATE1']);
|
||||
});
|
||||
it.each([{id:'proposer',role:'SUPER_ADMIN'},{id:'other',role:'Admin'}])('denies unauthorized hold %j',async actor=>{
|
||||
await expect(service.transition('t',hold,actor)).rejects.toMatchObject({status:403});
|
||||
expect(tx.orchestrationTask.update).not.toHaveBeenCalled();
|
||||
});
|
||||
it.each(['DRAFT','READY','GATE1_APPROVED','GATE2_PENDING'])('denies hold from %s',async state=>{
|
||||
task.state=state;
|
||||
await expect(service.transition('t',hold,reviewer)).rejects.toMatchObject({status:403});
|
||||
expect(tx.orchestrationTask.update).not.toHaveBeenCalled();
|
||||
});
|
||||
it('rejects missing reason and stale version without writes',async()=>{
|
||||
await expect(service.transition('t',{...hold,reason:' '},reviewer)).rejects.toMatchObject({status:400});
|
||||
await expect(service.transition('t',{...hold,expectedVersion:1},reviewer)).rejects.toMatchObject({status:409});
|
||||
expect(tx.orchestrationTask.update).not.toHaveBeenCalled();
|
||||
});
|
||||
it('replays identical hold, rejects changed payload',async()=>{
|
||||
await service.transition('t',hold,reviewer);
|
||||
const event=tx.orchestrationEvent.create.mock.calls[0][0].data;
|
||||
tx.orchestrationEvent.findUnique.mockResolvedValue(event);
|
||||
await service.transition('t',hold,reviewer);
|
||||
expect(tx.orchestrationTask.update).toHaveBeenCalledTimes(1);
|
||||
await expect(service.transition('t',{...hold,reason:'changed'},reviewer)).rejects.toMatchObject({status:409});
|
||||
});
|
||||
it.each(['APPROVED','REJECTED'])('decides held proposal via existing Gate1 command: %s',async decision=>{
|
||||
task.state='GATE1_HELD';
|
||||
await expect(service.decide('t',1,{decision,expectedVersion:2,idempotencyKey:'decision',expiresAt:new Date(Date.now()+60000).toISOString()},reviewer)).resolves.toMatchObject({state:decision==='APPROVED'?'READY':'REJECTED'});
|
||||
});
|
||||
});
|
||||
@@ -1,48 +0,0 @@
|
||||
import {Test} from '@nestjs/testing';
|
||||
import {INestApplication} from '@nestjs/common';
|
||||
import request from 'supertest';
|
||||
import {OrchestrationController} from './orchestration.controller';
|
||||
import {OrchestrationService} from './orchestration.service';
|
||||
import {PrepareWorkflowService} from './prepare-workflow.service';
|
||||
import {JwtAuthGuard} from '../auth/jwt-auth.guard';
|
||||
import {RolesGuard} from '../common/guards/roles.guard';
|
||||
describe('configured owner real workflow HTTP authorization',()=>{
|
||||
const owner='12345678-1234-1234-1234-123456789012';
|
||||
let app:INestApplication,task:any,tx:any;
|
||||
beforeAll(async()=>{
|
||||
tx={prepareWorkflowOutbox:{create:jest.fn()},$queryRaw:jest.fn(async()=>[]),orchestrationTask:{findUnique:async()=>task,update:jest.fn(async({data}:any)=>(task={...task,state:data.state,version:task.version+1}))},orchestrationEvent:{findUnique:jest.fn(),create:jest.fn()},orchestrationApprovalDecision:{create:jest.fn()}};
|
||||
const workflow=new PrepareWorkflowService({$transaction:async(cb:any)=>cb(tx)},{workerIds:[],stageCiIds:[],ownerReviewerIds:[owner]});
|
||||
const m=await Test.createTestingModule({controllers:[OrchestrationController],providers:[{provide:OrchestrationService,useValue:{findAll:async()=>[task],findOne:async()=>task}},{provide:PrepareWorkflowService,useValue:workflow},RolesGuard]})
|
||||
.overrideGuard(JwtAuthGuard).useValue({canActivate(c:any){const r=c.switchToHttp().getRequest();if(!r.headers.authorization)return false;r.user={sub:r.headers.authorization,role:'ADMIN'};return true;}}).compile();
|
||||
app=m.createNestApplication();await app.init();
|
||||
});
|
||||
beforeEach(()=>{jest.clearAllMocks();task={id:'t',state:'GATE1_PENDING',version:2,operationMode:'PREPARE',metadata:{workflowScope:'STAGE_PREPARE',createdBy:'pmo'}};});
|
||||
afterAll(async()=>{await app.close();});
|
||||
it.each(['APPROVED','REJECTED'])('advertises and executes %s only for verified granted JWT subject',async decision=>{
|
||||
const r=await request(app.getHttpServer()).get('/admin/orchestration/tasks').set('Authorization',owner).expect(200);
|
||||
expect(r.body[0].availableActions).toEqual(['APPROVE_GATE1','REJECT_GATE1','HOLD_GATE1']);
|
||||
await request(app.getHttpServer()).post('/admin/orchestration/tasks/t/gates/1/decide').set('Authorization',owner).send({decision,expectedVersion:2,idempotencyKey:'decision',expiresAt:new Date(Date.now()+60000).toISOString(),notes:'Delegated system testing by independent owner'}).expect(201);
|
||||
expect(tx.orchestrationApprovalDecision.create).toHaveBeenCalledWith({data:expect.objectContaining({actorId:owner,decision})});
|
||||
expect(tx.orchestrationEvent.create).toHaveBeenCalledWith({data:expect.objectContaining({actorId:owner,payload:expect.objectContaining({fingerprint:expect.stringContaining('Delegated system testing')})})});
|
||||
});
|
||||
it('holds via canonicalized ADMIN without granting proposer powers',async()=>{
|
||||
await request(app.getHttpServer()).put('/admin/orchestration/tasks/t/transition').set('Authorization',owner).send({toState:'GATE1_HELD',expectedVersion:2,idempotencyKey:'hold',reason:'Independent owner needs evidence'}).expect(200);
|
||||
expect(tx.orchestrationApprovalDecision.create).not.toHaveBeenCalled();
|
||||
});
|
||||
it.each(['ordinary-admin','pmo'])('denies ungranted subject %s and body impersonation',async subject=>{
|
||||
const r=await request(app.getHttpServer()).get('/admin/orchestration/tasks').set('Authorization',subject).expect(200);
|
||||
expect(r.body[0].availableActions).toEqual([]);
|
||||
const body={decision:'APPROVED',expectedVersion:2,idempotencyKey:'deny',expiresAt:new Date(Date.now()+60000).toISOString()};
|
||||
await request(app.getHttpServer()).post('/admin/orchestration/tasks/t/gates/1/decide').set('Authorization',subject).send(body).expect(403);
|
||||
await request(app.getHttpServer()).post('/admin/orchestration/tasks/t/gates/1/decide').set('Authorization',subject).send({...body,actorId:owner}).expect(400);
|
||||
await request(app.getHttpServer()).put('/admin/orchestration/tasks/t/transition').set('Authorization',subject).send({toState:'GATE1_HELD',expectedVersion:2,idempotencyKey:'hold',reason:'hold'}).expect(403);
|
||||
expect(tx.orchestrationTask.update).not.toHaveBeenCalled();
|
||||
});
|
||||
it('denies grant holder self review, Gate2 and release',async()=>{
|
||||
task.metadata.createdBy=owner;
|
||||
await request(app.getHttpServer()).post('/admin/orchestration/tasks/t/gates/1/decide').set('Authorization',owner).send({decision:'APPROVED',expectedVersion:2,idempotencyKey:'self',expiresAt:new Date(Date.now()+60000).toISOString()}).expect(403);
|
||||
await request(app.getHttpServer()).post('/admin/orchestration/tasks/t/gates/2/decide').set('Authorization',owner).send({}).expect(403);
|
||||
await request(app.getHttpServer()).post('/admin/orchestration/tasks/t/commands/RELEASE_AUTHORIZE').set('Authorization',owner).send({}).expect(403);
|
||||
expect(tx.orchestrationTask.update).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -1,185 +0,0 @@
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import request from 'supertest';
|
||||
import { OrchestrationController } from './orchestration.controller';
|
||||
import { OrchestrationService } from './orchestration.service';
|
||||
import { PrepareWorkflowService } from './prepare-workflow.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { RolesGuard } from '../common/guards/roles.guard';
|
||||
|
||||
describe('bounded workflow HTTP contracts', () => {
|
||||
let app: INestApplication;
|
||||
const t = {
|
||||
id: 't',
|
||||
key: 'k',
|
||||
state: 'DRAFT',
|
||||
version: 1,
|
||||
metadata: {
|
||||
scope: 'STAGE_PREPARE',
|
||||
environment: 'staging',
|
||||
commitSha: 'a'.repeat(40),
|
||||
token: 'secret',
|
||||
},
|
||||
};
|
||||
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),
|
||||
project: jest.fn((row: any) => ({
|
||||
...row,
|
||||
metadata: {
|
||||
scope: 'STAGE_PREPARE',
|
||||
environment: 'staging',
|
||||
commitSha: 'a'.repeat(40),
|
||||
},
|
||||
availableActions: ['SUBMIT_GATE1'],
|
||||
})),
|
||||
};
|
||||
const core = {
|
||||
findAll: async () => [t],
|
||||
findOne: async () => t,
|
||||
command: jest.fn(),
|
||||
};
|
||||
beforeAll(async () => {
|
||||
const m = await Test.createTestingModule({
|
||||
controllers: [OrchestrationController],
|
||||
providers: [
|
||||
{ provide: OrchestrationService, useValue: core },
|
||||
{ provide: PrepareWorkflowService, useValue: workflow },
|
||||
RolesGuard,
|
||||
],
|
||||
})
|
||||
.overrideGuard(JwtAuthGuard)
|
||||
.useValue({
|
||||
canActivate(c: any) {
|
||||
const req = c.switchToHttp().getRequest();
|
||||
if (!req.headers.authorization) return false;
|
||||
req.user = {
|
||||
sub: 'verified-subject',
|
||||
role: req.headers.authorization,
|
||||
};
|
||||
return true;
|
||||
},
|
||||
})
|
||||
.compile();
|
||||
app = m.createNestApplication();
|
||||
await app.init();
|
||||
});
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
it.each([
|
||||
['ADMIN', 'Admin'],
|
||||
['admin', 'Admin'],
|
||||
['Admin', 'Admin'],
|
||||
['SUPER_ADMIN', 'SUPER_ADMIN'],
|
||||
['super-admin', 'SUPER_ADMIN'],
|
||||
['SuperAdmin', 'SUPER_ADMIN'],
|
||||
['ADMIN_SUPER', 'SUPER_ADMIN'],
|
||||
])('canonicalizes guard-supported %s on task reads to %s', async (role, canonical) => {
|
||||
for (const path of ['tasks', 'tasks/t']) {
|
||||
workflow.project.mockClear();
|
||||
const response = await request(app.getHttpServer())
|
||||
.get('/admin/orchestration/' + path)
|
||||
.set('Authorization', role)
|
||||
.expect(200);
|
||||
expect(workflow.project).toHaveBeenCalledWith(t, {
|
||||
id: 'verified-subject', role: canonical,
|
||||
});
|
||||
const task = Array.isArray(response.body) ? response.body[0] : response.body;
|
||||
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']) {
|
||||
await request(app.getHttpServer())
|
||||
.get('/admin/orchestration/' + path)
|
||||
.set('Authorization', 'User_PetOwner')
|
||||
.expect(403);
|
||||
}
|
||||
expect(workflow.project).not.toHaveBeenCalled();
|
||||
});
|
||||
it('accepts existing proposal DTO, derives actor from JWT sub and returns projected task', async () => {
|
||||
const dto = {
|
||||
key: 'k',
|
||||
title: 'T',
|
||||
department: 'ENGINEERING',
|
||||
operationMode: 'PREPARE',
|
||||
budgetLimitCents: 20,
|
||||
metadata: {
|
||||
commitSha: 'a'.repeat(40),
|
||||
scope: 'Staging audit only',
|
||||
acceptanceCriteria: ['QA passes'],
|
||||
},
|
||||
};
|
||||
const r = await request(app.getHttpServer())
|
||||
.post('/admin/orchestration/tasks')
|
||||
.set('Authorization', 'Admin')
|
||||
.send(dto)
|
||||
.expect(201);
|
||||
expect(workflow.create).toHaveBeenCalledWith(dto, {
|
||||
id: 'verified-subject',
|
||||
role: 'Admin',
|
||||
});
|
||||
expect(r.body.availableActions).toEqual(['SUBMIT_GATE1']);
|
||||
expect(JSON.stringify(r.body)).not.toMatch(/token|secret/);
|
||||
await request(app.getHttpServer())
|
||||
.post('/admin/orchestration/tasks')
|
||||
.set('Authorization', 'Admin')
|
||||
.send({ ...dto, actorId: 'browser' })
|
||||
.expect(400);
|
||||
});
|
||||
it('routes fixed transition/gate contracts and rejects invalid gates plus release without invoking core', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.put('/admin/orchestration/tasks/t/transition')
|
||||
.set('Authorization', 'Admin')
|
||||
.send({
|
||||
toState: 'GATE1_PENDING',
|
||||
expectedVersion: 1,
|
||||
idempotencyKey: 'submit',
|
||||
})
|
||||
.expect(200);
|
||||
await request(app.getHttpServer())
|
||||
.put('/admin/orchestration/tasks/t/transition')
|
||||
.set('Authorization', 'SUPER_ADMIN')
|
||||
.send({toState:'GATE1_HELD',expectedVersion:2,idempotencyKey:'hold',reason:'Review sources'})
|
||||
.expect(200);
|
||||
expect(workflow.transition).toHaveBeenCalledWith('t', expect.objectContaining({toState:'GATE1_HELD',expectedVersion:2,idempotencyKey:'hold',reason:'Review sources'}), {id:'verified-subject',role:'SUPER_ADMIN'});
|
||||
await request(app.getHttpServer())
|
||||
.post('/admin/orchestration/tasks/t/gates/1/decide')
|
||||
.set('Authorization', 'SUPER_ADMIN')
|
||||
.send({
|
||||
decision: 'APPROVED',
|
||||
expectedVersion: 2,
|
||||
idempotencyKey: 'approval',
|
||||
expiresAt: new Date(Date.now() + 60000).toISOString(),
|
||||
})
|
||||
.expect(201);
|
||||
expect(workflow.decide).toHaveBeenCalledWith('t', 1, expect.any(Object), {
|
||||
id: 'verified-subject',
|
||||
role: 'SUPER_ADMIN',
|
||||
});
|
||||
await request(app.getHttpServer())
|
||||
.post('/admin/orchestration/tasks/t/gates/2/decide')
|
||||
.set('Authorization', 'SUPER_ADMIN')
|
||||
.send({})
|
||||
.expect(403);
|
||||
await request(app.getHttpServer())
|
||||
.post('/admin/orchestration/tasks/t/commands/RELEASE_AUTHORIZE')
|
||||
.set('Authorization', 'SUPER_ADMIN')
|
||||
.send({})
|
||||
.expect(403);
|
||||
expect(core.command).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -1,689 +0,0 @@
|
||||
import {
|
||||
Injectable,
|
||||
Inject,
|
||||
ForbiddenException,
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
export const PREPARE_WORKFLOW_CONFIG = 'PREPARE_WORKFLOW_CONFIG';
|
||||
export interface WorkflowIdentity {
|
||||
id: string;
|
||||
role: string;
|
||||
}
|
||||
export interface WorkflowConfig {
|
||||
ownerReviewerIds?: string[];
|
||||
workerIds: string[];
|
||||
stageCiIds: string[];
|
||||
}
|
||||
|
||||
/** Bounded Stage preparation only. No release executor, production scope or browser worker identities. */
|
||||
@Injectable()
|
||||
export class PrepareWorkflowService {
|
||||
constructor(
|
||||
@Inject(PrismaService) private readonly db: any,
|
||||
@Inject(PREPARE_WORKFLOW_CONFIG) private readonly config: WorkflowConfig,
|
||||
) {}
|
||||
private identity(a: WorkflowIdentity, roles: string[]) {
|
||||
if (
|
||||
!a?.id ||
|
||||
typeof a.id !== 'string' ||
|
||||
a.id.length > 100 ||
|
||||
!roles.includes(a.role)
|
||||
)
|
||||
throw new ForbiddenException('Authenticated workflow identity required');
|
||||
}
|
||||
private reviewer(a: WorkflowIdentity) {
|
||||
return typeof a?.id === 'string' && !!a.id && a.id.length <= 100 &&
|
||||
(a.role === 'SUPER_ADMIN' ||
|
||||
(a.role === 'Admin' && (this.config.ownerReviewerIds ?? []).includes(a.id)));
|
||||
}
|
||||
private worker(a: WorkflowIdentity) {
|
||||
this.identity(a, ['WORKFLOW_WORKER']);
|
||||
if (!this.config.workerIds.includes(a.id))
|
||||
throw new ForbiddenException('Worker identity not provisioned');
|
||||
}
|
||||
private async allowed(tx: any, claiming = false) {
|
||||
const rows =
|
||||
await tx.$queryRaw`SELECT level, active FROM orchestration_kill_switches WHERE scope = 'GLOBAL' FOR SHARE`;
|
||||
if (rows[0]?.active && !(rows[0].level === 'FREEZE_NEW_WORK' && !claiming))
|
||||
throw new ForbiddenException('Kill switch blocks workflow');
|
||||
}
|
||||
private async task(tx: any, id: string) {
|
||||
await tx.$queryRaw`SELECT id FROM orchestration_tasks WHERE id = ${id}::uuid FOR UPDATE`;
|
||||
const t = await tx.orchestrationTask.findUnique({ where: { id } });
|
||||
if (!t) throw new NotFoundException('Task not found');
|
||||
if (
|
||||
t.operationMode !== 'PREPARE' ||
|
||||
(t.metadata?.workflowScope ?? t.metadata?.scope) !== 'STAGE_PREPARE'
|
||||
)
|
||||
throw new ForbiddenException('Not a bounded Stage proposal');
|
||||
return t;
|
||||
}
|
||||
private async event(
|
||||
tx: any,
|
||||
t: any,
|
||||
state: string,
|
||||
a: WorkflowIdentity,
|
||||
key: string,
|
||||
type: string,
|
||||
fingerprint: string,
|
||||
) {
|
||||
const result = await tx.orchestrationTask.update({
|
||||
where: { id: t.id },
|
||||
data: { state, version: { increment: 1 } },
|
||||
});
|
||||
await tx.orchestrationEvent.create({
|
||||
data: {
|
||||
taskId: t.id,
|
||||
sequence: result.version,
|
||||
actorId: a.id,
|
||||
eventType: type,
|
||||
fromState: t.state,
|
||||
toState: state,
|
||||
idempotencyKey: key,
|
||||
payload: { fingerprint },
|
||||
},
|
||||
});
|
||||
return result;
|
||||
}
|
||||
private async replay(tx: any, key: string, fingerprint: string) {
|
||||
const e = await tx.orchestrationEvent.findUnique({
|
||||
where: { idempotencyKey: key },
|
||||
});
|
||||
if (e && e.payload?.fingerprint !== fingerprint)
|
||||
throw new ConflictException('Idempotency payload mismatch');
|
||||
return e;
|
||||
}
|
||||
private version(t: any, dto: any) {
|
||||
if (
|
||||
!Number.isSafeInteger(dto.expectedVersion) ||
|
||||
dto.expectedVersion !== t.version
|
||||
)
|
||||
throw new ConflictException('Stale task version');
|
||||
if (
|
||||
typeof dto.idempotencyKey !== 'string' ||
|
||||
!dto.idempotencyKey.length ||
|
||||
dto.idempotencyKey.length > 120
|
||||
)
|
||||
throw new BadRequestException('Invalid idempotency key');
|
||||
}
|
||||
async create(dto: any, a: WorkflowIdentity) {
|
||||
this.identity(a, ['Admin', 'SUPER_ADMIN']);
|
||||
const metadata = dto?.metadata;
|
||||
const boundedText = (value: unknown, max: number) =>
|
||||
typeof value === 'string' &&
|
||||
value.trim().length > 0 &&
|
||||
value.length <= max;
|
||||
if (
|
||||
!metadata ||
|
||||
typeof metadata !== 'object' ||
|
||||
Array.isArray(metadata) ||
|
||||
typeof metadata.commitSha !== 'string' ||
|
||||
!boundedText(metadata.scope, 2000) ||
|
||||
!Array.isArray(metadata.acceptanceCriteria) ||
|
||||
metadata.acceptanceCriteria.length < 1 ||
|
||||
metadata.acceptanceCriteria.length > 20 ||
|
||||
!metadata.acceptanceCriteria.every((criterion: unknown) =>
|
||||
boundedText(criterion, 500),
|
||||
)
|
||||
)
|
||||
throw new BadRequestException(
|
||||
'PREPARE requires commitSha, scope 1..2000 characters and 1..20 acceptance criteria of 1..500 characters',
|
||||
);
|
||||
if (
|
||||
dto.operationMode !== 'PREPARE' ||
|
||||
!/^[a-f0-9]{40}$/.test(dto.metadata?.commitSha ?? '') ||
|
||||
!Number.isSafeInteger(dto.budgetLimitCents) ||
|
||||
dto.budgetLimitCents < 20 ||
|
||||
dto.budgetLimitCents > 10000
|
||||
)
|
||||
throw new BadRequestException(
|
||||
'PREPARE requires immutable 40hex commit and budget 20..10000 cents',
|
||||
);
|
||||
if (
|
||||
!dto.key ||
|
||||
dto.key.length > 120 ||
|
||||
!dto.title ||
|
||||
dto.title.length > 255 ||
|
||||
!dto.department ||
|
||||
dto.department.length > 50
|
||||
)
|
||||
throw new BadRequestException('Invalid proposal fields');
|
||||
return this.db
|
||||
.$transaction(async (tx: any) => {
|
||||
await this.allowed(tx, true);
|
||||
const t = await tx.orchestrationTask.create({
|
||||
data: {
|
||||
key: dto.key,
|
||||
title: dto.title,
|
||||
department: dto.department,
|
||||
operationMode: 'PREPARE',
|
||||
budgetLimitCents: dto.budgetLimitCents,
|
||||
metadata: {
|
||||
workflowScope: 'STAGE_PREPARE',
|
||||
scope: dto.metadata.scope,
|
||||
acceptanceCriteria: [...dto.metadata.acceptanceCriteria],
|
||||
environment: 'staging',
|
||||
commitSha: dto.metadata.commitSha,
|
||||
createdBy: a.id,
|
||||
},
|
||||
state: 'DRAFT',
|
||||
},
|
||||
});
|
||||
await tx.orchestrationEvent.create({
|
||||
data: {
|
||||
taskId: t.id,
|
||||
sequence: 1,
|
||||
eventType: 'PREPARE_PROPOSED',
|
||||
actorId: a.id,
|
||||
toState: 'DRAFT',
|
||||
},
|
||||
});
|
||||
return t;
|
||||
})
|
||||
.catch((error: any) => {
|
||||
if (
|
||||
error?.code === 'P2002' &&
|
||||
Array.isArray(error.meta?.target) &&
|
||||
error.meta.target.includes('key')
|
||||
)
|
||||
throw new ConflictException(
|
||||
'Proposal key already exists; fetch and reconcile the existing task',
|
||||
);
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
async transition(id: string, dto: any, a: WorkflowIdentity) {
|
||||
this.identity(a, ['Admin', 'SUPER_ADMIN']);
|
||||
return this.db.$transaction(async (tx: any) => {
|
||||
await this.allowed(tx, true);
|
||||
const t = await this.task(tx, id);
|
||||
const fingerprint = JSON.stringify({ id, dto, a });
|
||||
if (await this.replay(tx, dto.idempotencyKey, fingerprint)) return t;
|
||||
this.version(t, dto);
|
||||
if (dto.toState === 'GATE1_HELD') {
|
||||
if (!this.reviewer(a) || t.metadata.createdBy === a.id || t.state !== 'GATE1_PENDING')
|
||||
throw new ForbiddenException('Independent Gate1 reviewer and pending proposal required');
|
||||
if (typeof dto.reason !== 'string' || !dto.reason.trim() || dto.reason.length > 2000)
|
||||
throw new BadRequestException('Hold requires reason of 1..2000 characters');
|
||||
return this.event(tx, t, 'GATE1_HELD', a, dto.idempotencyKey, 'PREPARE_GATE1_HELD', fingerprint);
|
||||
}
|
||||
if (t.metadata.createdBy !== a.id && a.role !== 'SUPER_ADMIN')
|
||||
throw new ForbiddenException('Proposal owner required');
|
||||
if (
|
||||
!(
|
||||
(t.state === 'DRAFT' && dto.toState === 'GATE1_PENDING') ||
|
||||
(t.state === 'GATE1_APPROVED' && dto.toState === 'READY')
|
||||
)
|
||||
)
|
||||
throw new ForbiddenException(
|
||||
'Only submission and approved queueing are available',
|
||||
);
|
||||
if (dto.toState === 'READY') {
|
||||
const approval = await tx.orchestrationApprovalDecision.findFirst({
|
||||
where: { taskId: id, gate: 1, decision: 'APPROVED' },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
if (!approval || approval.expiresAt <= new Date())
|
||||
throw new ForbiddenException('Gate1 expired');
|
||||
await tx.prepareWorkflowOutbox.create({ data: { taskId: id } });
|
||||
}
|
||||
return this.event(
|
||||
tx,
|
||||
t,
|
||||
dto.toState,
|
||||
a,
|
||||
dto.idempotencyKey,
|
||||
'PREPARE_TRANSITION',
|
||||
fingerprint,
|
||||
);
|
||||
});
|
||||
}
|
||||
async decide(id: string, gate: number, dto: any, a: WorkflowIdentity) {
|
||||
if (!this.reviewer(a))
|
||||
throw new ForbiddenException('Provisioned independent Gate1 reviewer required');
|
||||
if (gate !== 1)
|
||||
throw new ForbiddenException(
|
||||
'Gate2 decisions and release remain disabled',
|
||||
);
|
||||
if (!['APPROVED', 'REJECTED'].includes(dto.decision))
|
||||
throw new BadRequestException('Invalid decision');
|
||||
// Time-sensitive validation happens after replay, so a successful command
|
||||
// remains replayable after its original schedule or validity window passes.
|
||||
return this.db.$transaction(async (tx: any) => {
|
||||
await this.allowed(tx);
|
||||
const t = await this.task(tx, id);
|
||||
const fingerprint = JSON.stringify({ id, gate, dto, a });
|
||||
if (await this.replay(tx, dto.idempotencyKey, fingerprint)) return t;
|
||||
this.version(t, dto);
|
||||
if (t.metadata.createdBy === a.id)
|
||||
throw new ForbiddenException('Independent reviewer required');
|
||||
const renewing = t.state === 'READY';
|
||||
let queue: any;
|
||||
if (renewing) {
|
||||
const previous = await tx.orchestrationApprovalDecision.findFirst({
|
||||
where: { taskId: id, gate: 1 }, orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
queue = await tx.prepareWorkflowOutbox.findUnique({ where: { taskId: id } });
|
||||
if (dto.notBefore !== undefined)
|
||||
throw new BadRequestException('Renewal preserves the existing schedule');
|
||||
if (dto.decision !== 'APPROVED' || !previous || previous.decision !== 'APPROVED' ||
|
||||
previous.actorId === t.metadata.createdBy || previous.expiresAt > new Date() ||
|
||||
!queue || queue.status !== 'PENDING' || queue.attempts !== 0 || queue.runId || queue.leaseToken)
|
||||
throw new ConflictException('Only expired independent approval on an unstarted queue can be renewed');
|
||||
} else if (!['GATE1_PENDING', 'GATE1_HELD'].includes(t.state))
|
||||
throw new ConflictException('Gate1 pending required');
|
||||
const now = Date.now();
|
||||
if (renewing && queue.notBefore && new Date(queue.notBefore).getTime() >= now + 86400000)
|
||||
throw new ConflictException('Existing schedule is outside the renewal validity window');
|
||||
const scheduled = dto.notBefore !== undefined;
|
||||
const notBefore = scheduled ? new Date(dto.notBefore) : null;
|
||||
if (scheduled && (dto.decision !== 'APPROVED' || typeof dto.notBefore !== 'string' || !Number.isFinite(notBefore!.getTime()) || notBefore!.getTime() <= now || notBefore!.getTime() > now + 30 * 86400000))
|
||||
throw new BadRequestException('notBefore must be a future approval schedule within 30 days');
|
||||
const policyExpiry = (notBefore?.getTime() ?? now) + 86400000;
|
||||
// Legacy expiresAt is an explicit validity cap, never a scheduled start.
|
||||
const expiresAt = dto.expiresAt === undefined ? new Date(policyExpiry) : new Date(dto.expiresAt);
|
||||
if (!Number.isFinite(expiresAt.getTime()) || expiresAt.getTime() <= (notBefore?.getTime() ?? now) || expiresAt.getTime() > policyExpiry)
|
||||
throw new BadRequestException('Approval expiry must cover schedule and remain within backend validity policy');
|
||||
if (dto.notes !== undefined && (typeof dto.notes !== 'string' || dto.notes.length > 2000))
|
||||
throw new BadRequestException('Audit notes must be at most 2000 characters');
|
||||
if (dto.decision === 'APPROVED') {
|
||||
await this.allowed(tx, true);
|
||||
if (!renewing) await tx.prepareWorkflowOutbox.create({data:{taskId:id,notBefore}});
|
||||
}
|
||||
await tx.orchestrationApprovalDecision.create({
|
||||
data: {
|
||||
taskId: id,
|
||||
gate: 1,
|
||||
decision: dto.decision,
|
||||
actorId: a.id,
|
||||
expiresAt,
|
||||
notes: dto.notes?.trim() || `Owner ${dto.decision.toLowerCase()} Gate1`,
|
||||
idempotencyKey: `decision:${dto.idempotencyKey}`,
|
||||
},
|
||||
});
|
||||
return this.event(
|
||||
tx,
|
||||
t,
|
||||
dto.decision === 'APPROVED' ? 'READY' : 'REJECTED',
|
||||
a,
|
||||
dto.idempotencyKey,
|
||||
renewing ? 'PREPARE_GATE1_RENEWED' : 'PREPARE_GATE1',
|
||||
fingerprint,
|
||||
);
|
||||
});
|
||||
}
|
||||
/** Recovery of a stored approval by its provisioned proposal worker, not a new decision. */
|
||||
async enqueueApproved(a: WorkflowIdentity, dto: {taskId:string;expectedVersion:number;idempotencyKey:string}) {
|
||||
this.worker(a);
|
||||
return this.db.$transaction(async (tx:any)=>{
|
||||
await this.allowed(tx,true);
|
||||
const t=await this.task(tx,dto.taskId);
|
||||
if(t.metadata.createdBy!==a.id) throw new ForbiddenException('Proposal worker required');
|
||||
const fingerprint=JSON.stringify({dto,a});
|
||||
if(await this.replay(tx,dto.idempotencyKey,fingerprint)) return t;
|
||||
this.version(t,dto);
|
||||
if(t.state!=='GATE1_APPROVED') throw new ConflictException('Stored Gate1 approval required');
|
||||
const approval=await tx.orchestrationApprovalDecision.findFirst({where:{taskId:t.id,gate:1},orderBy:{createdAt:'desc'}});
|
||||
if(!approval || approval.decision!=='APPROVED' || approval.actorId===a.id || approval.expiresAt<=new Date())
|
||||
throw new ForbiddenException('Valid independent Gate1 required');
|
||||
await tx.prepareWorkflowOutbox.create({data:{taskId:t.id}});
|
||||
return this.event(tx,t,'READY',a,dto.idempotencyKey,'PREPARE_APPROVED_QUEUED',fingerprint);
|
||||
});
|
||||
}
|
||||
async claim(a: WorkflowIdentity, id?: string): Promise<any> {
|
||||
this.worker(a);
|
||||
return this.db.$transaction(async (tx: any) => {
|
||||
await this.allowed(tx, true);
|
||||
// Lock order is task then outbox for every worker operation. SKIP LOCKED permits competing workers.
|
||||
const rows: any[] = id
|
||||
? await tx.$queryRaw`SELECT t.id FROM orchestration_tasks t JOIN prepare_workflow_outbox o ON o.task_id=t.id WHERE t.id=${id}::uuid AND t.state IN ('READY','RUNNING') AND o.status='PENDING' AND (o.not_before IS NULL OR o.not_before <= now()) AND (o.lease_until IS NULL OR o.lease_until < now()) ORDER BY o.created_at FOR UPDATE OF t SKIP LOCKED LIMIT 1`
|
||||
: await tx.$queryRaw`SELECT t.id FROM orchestration_tasks t JOIN prepare_workflow_outbox o ON o.task_id=t.id WHERE t.state IN ('READY','RUNNING') AND o.status='PENDING' AND (o.not_before IS NULL OR o.not_before <= now()) AND (o.lease_until IS NULL OR o.lease_until < now()) ORDER BY o.created_at FOR UPDATE OF t SKIP LOCKED LIMIT 1`;
|
||||
if (!rows.length) return null;
|
||||
const t = await this.task(tx, rows[0].id);
|
||||
const o = await tx.prepareWorkflowOutbox.findUnique({
|
||||
where: { taskId: t.id },
|
||||
});
|
||||
const approval = await tx.orchestrationApprovalDecision.findFirst({
|
||||
where: { taskId: t.id, gate: 1, decision: 'APPROVED' },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
if (!approval || approval.expiresAt <= new Date())
|
||||
throw new ForbiddenException('Gate1 expired');
|
||||
if (o.attempts >= 3) throw new ForbiddenException('Attempts exhausted');
|
||||
const costs = await tx.orchestrationBudgetLedger.aggregate({
|
||||
where: { taskId: t.id },
|
||||
_sum: { amountCents: true },
|
||||
});
|
||||
if ((costs._sum.amountCents ?? 0) + 20 > t.budgetLimitCents)
|
||||
throw new ForbiddenException('Hard task budget exceeded');
|
||||
if (o.runId)
|
||||
await tx.orchestrationRun.update({
|
||||
where: { id: o.runId },
|
||||
data: {
|
||||
status: 'FAILED',
|
||||
retryClass: 'TRANSIENT',
|
||||
finishedAt: new Date(),
|
||||
},
|
||||
});
|
||||
const token = randomUUID(),
|
||||
attempt = o.attempts + 1;
|
||||
const run = await tx.orchestrationRun.create({
|
||||
data: {
|
||||
taskId: t.id,
|
||||
attempt,
|
||||
status: 'RUNNING',
|
||||
environment: 'staging',
|
||||
commitSha: t.metadata.commitSha,
|
||||
idempotencyKey: `prepare-run:${o.id}:${attempt}`,
|
||||
startedAt: new Date(),
|
||||
},
|
||||
});
|
||||
const reservation = await tx.orchestrationBudgetLedger.create({
|
||||
data: {
|
||||
taskId: t.id,
|
||||
amountCents: 20,
|
||||
kind: 'RESERVE',
|
||||
actorId: a.id,
|
||||
idempotencyKey: `prepare-reserve:${run.id}`,
|
||||
},
|
||||
});
|
||||
const leaseUntil = new Date(Date.now() + 30000);
|
||||
await tx.prepareWorkflowOutbox.update({
|
||||
where: { id: o.id },
|
||||
data: {
|
||||
attempts: attempt,
|
||||
leaseOwner: a.id,
|
||||
leaseToken: token,
|
||||
leaseUntil,
|
||||
heartbeatAt: new Date(),
|
||||
runId: run.id,
|
||||
reservationId: reservation.id,
|
||||
},
|
||||
});
|
||||
await this.event(
|
||||
tx,
|
||||
t,
|
||||
'RUNNING',
|
||||
a,
|
||||
`prepare-claim:${run.id}`,
|
||||
'PREPARE_CLAIMED',
|
||||
token,
|
||||
);
|
||||
return {
|
||||
taskId: t.id,
|
||||
runId: run.id,
|
||||
attempt,
|
||||
leaseToken: token,
|
||||
leaseUntil,
|
||||
commitSha: run.commitSha,
|
||||
environment: 'staging',
|
||||
maxCostCents: 20,
|
||||
};
|
||||
});
|
||||
}
|
||||
private async lease(tx: any, a: WorkflowIdentity, dto: any) {
|
||||
const t = await this.task(tx, dto.taskId);
|
||||
const o = await tx.prepareWorkflowOutbox.findUnique({
|
||||
where: { taskId: t.id },
|
||||
});
|
||||
if (
|
||||
!o ||
|
||||
o.status !== 'PENDING' ||
|
||||
o.leaseOwner !== a.id ||
|
||||
o.leaseToken !== dto.leaseToken ||
|
||||
o.runId !== dto.runId ||
|
||||
!o.leaseUntil ||
|
||||
o.leaseUntil <= new Date() ||
|
||||
t.state !== 'RUNNING'
|
||||
)
|
||||
throw new ForbiddenException('Active owned lease required');
|
||||
return { t, o };
|
||||
}
|
||||
async heartbeat(a: WorkflowIdentity, dto: any) {
|
||||
this.worker(a);
|
||||
return this.db.$transaction(async (tx: any) => {
|
||||
await this.allowed(tx);
|
||||
const { o } = await this.lease(tx, a, dto);
|
||||
return tx.prepareWorkflowOutbox.update({
|
||||
where: { id: o.id },
|
||||
data: {
|
||||
heartbeatAt: new Date(),
|
||||
leaseUntil: new Date(Date.now() + 30000),
|
||||
},
|
||||
select: { leaseUntil: true },
|
||||
});
|
||||
});
|
||||
}
|
||||
async complete(a: WorkflowIdentity, dto: any) {
|
||||
this.worker(a);
|
||||
if (
|
||||
!/^[a-f0-9]{64}$/.test(dto.sha256 ?? '') ||
|
||||
!/^https:\/\//.test(dto.uri ?? '') ||
|
||||
!Number.isSafeInteger(dto.actualCents) ||
|
||||
dto.actualCents < 0 ||
|
||||
dto.actualCents > 20
|
||||
)
|
||||
throw new BadRequestException(
|
||||
'QA hash, HTTPS URI and cost 0..20 required',
|
||||
);
|
||||
return this.db.$transaction(async (tx: any) => {
|
||||
await this.allowed(tx);
|
||||
const { t, o } = await this.lease(tx, a, dto);
|
||||
await tx.orchestrationArtifact.create({
|
||||
data: { runId: o.runId, kind: 'QA', sha256: dto.sha256, uri: dto.uri },
|
||||
});
|
||||
await tx.orchestrationAttestation.create({
|
||||
data: {
|
||||
runId: o.runId,
|
||||
kind: 'QA',
|
||||
subjectHash: dto.sha256,
|
||||
signer: a.id,
|
||||
predicate: { passed: true, scope: 'STAGE_PREPARE' },
|
||||
},
|
||||
});
|
||||
await tx.orchestrationRun.update({
|
||||
where: { id: o.runId },
|
||||
data: { status: 'SUCCEEDED', finishedAt: new Date() },
|
||||
});
|
||||
await tx.orchestrationBudgetLedger.create({
|
||||
data: {
|
||||
taskId: t.id,
|
||||
kind: 'RECONCILE',
|
||||
amountCents: dto.actualCents - 20,
|
||||
actorId: a.id,
|
||||
idempotencyKey: `prepare-cost:${o.runId}`,
|
||||
metadata: {
|
||||
reservationId: o.reservationId,
|
||||
actualCents: dto.actualCents,
|
||||
},
|
||||
},
|
||||
});
|
||||
await tx.prepareWorkflowOutbox.update({
|
||||
where: { id: o.id },
|
||||
data: { status: 'DONE', leaseToken: null, leaseUntil: null },
|
||||
});
|
||||
return this.event(
|
||||
tx,
|
||||
t,
|
||||
'VERIFYING',
|
||||
a,
|
||||
`prepare-qa:${o.runId}`,
|
||||
'PREPARE_QA_SUCCEEDED',
|
||||
dto.sha256,
|
||||
);
|
||||
});
|
||||
}
|
||||
/** 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;
|
||||
if (held) {
|
||||
this.worker(a);
|
||||
if (a.id !== 'pmo') throw new ForbiddenException('Canonical PMO required');
|
||||
} else if (!this.reviewer(a)) throw new ForbiddenException('Independent recovery reviewer required');
|
||||
if (typeof dto.reason !== 'string' || !dto.reason.trim() || dto.reason.length > 500)
|
||||
throw new BadRequestException('Bounded failure reason required');
|
||||
return this.db.$transaction(async (tx: any) => {
|
||||
await this.allowed(tx);
|
||||
const t = await this.task(tx, id);
|
||||
const { admittedEditorialTask } = require('../orchestration-worker/editorial-execution');
|
||||
if (!admittedEditorialTask(t)) throw new ForbiddenException('Exact editorial proposal required');
|
||||
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 ForbiddenException('Editorial provenance required');
|
||||
if (!held && t.metadata.createdBy === a.id) throw new ForbiddenException('Independent recovery reviewer required');
|
||||
const fingerprint = JSON.stringify({ id, dto, a });
|
||||
if (await this.replay(tx, dto.idempotencyKey, fingerprint)) return t;
|
||||
this.version(t, dto);
|
||||
const o = await tx.prepareWorkflowOutbox.findUnique({ where: { taskId: id } });
|
||||
const r = await tx.orchestrationRun.findUnique({ where: { id: dto.runId } });
|
||||
if (t.state !== 'RUNNING' || !o || o.status !== 'PENDING' || o.runId !== dto.runId ||
|
||||
o.leaseOwner !== 'pmo' || !o.leaseUntil || !r || r.taskId !== id || r.status !== 'RUNNING' ||
|
||||
r.environment !== 'staging' || r.commitSha !== t.metadata.commitSha ||
|
||||
(held ? (o.leaseOwner !== a.id || o.leaseToken !== dto.leaseToken || o.leaseUntil <= new Date()) : o.leaseUntil > new Date()))
|
||||
throw new ForbiddenException('Matching held lease or expired run required');
|
||||
const reserve = await tx.orchestrationBudgetLedger.findUnique({ where: { id: o.reservationId } });
|
||||
const ledger = await tx.orchestrationBudgetLedger.aggregate({ where: { taskId: id }, _sum: { amountCents: true } });
|
||||
if (!reserve || reserve.taskId !== id || 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: id, kind: 'RECONCILE' } }) ||
|
||||
await tx.orchestrationArtifact.count({ where: { runId: r.id } }))
|
||||
throw new ForbiddenException('Unreconciled artifact-free editorial reservation required');
|
||||
await tx.orchestrationRun.update({ where: { id: r.id }, data: { status: 'FAILED', retryClass: 'DEPENDENCY', 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 }, data: { state: 'FAILED', version: { increment: 1 } } });
|
||||
await tx.orchestrationEvent.create({ data: {
|
||||
taskId: id, sequence: updated.version, actorId: a.id, eventType: 'EDITORIAL_RUN_STOPPED',
|
||||
fromState: 'RUNNING', toState: 'FAILED', idempotencyKey: dto.idempotencyKey,
|
||||
payload: { fingerprint, runId: r.id, reason: dto.reason, recoveryMode: held ? 'HELD_LEASE' : 'EXPIRED_RUN', costStatus: 'unknown-upper-bound-reserved', retainedReservationCents: 20 },
|
||||
} });
|
||||
return updated;
|
||||
});
|
||||
}
|
||||
async stageEvidence(a: WorkflowIdentity, dto: any) {
|
||||
this.identity(a, ['STAGE_CI']);
|
||||
if (!this.config.stageCiIds.includes(a.id))
|
||||
throw new ForbiddenException('Stage CI identity not provisioned');
|
||||
if (
|
||||
!/^[a-f0-9]{64}$/.test(dto.evidenceSha256 ?? '') ||
|
||||
!/^https:\/\//.test(dto.uri ?? '')
|
||||
)
|
||||
throw new BadRequestException(
|
||||
'Stage evidence hash and HTTPS URI required',
|
||||
);
|
||||
return this.db.$transaction(async (tx: any) => {
|
||||
await this.allowed(tx);
|
||||
const t = await this.task(tx, dto.taskId);
|
||||
const run = await tx.orchestrationRun.findUnique({
|
||||
where: { id: dto.runId },
|
||||
});
|
||||
if (
|
||||
t.state !== 'VERIFYING' ||
|
||||
!run ||
|
||||
run.taskId !== t.id ||
|
||||
run.environment !== 'staging' ||
|
||||
run.status !== 'SUCCEEDED' ||
|
||||
run.commitSha !== dto.commitSha ||
|
||||
t.metadata.commitSha !== dto.commitSha
|
||||
)
|
||||
throw new ForbiddenException(
|
||||
'Immutable successful Stage run binding required',
|
||||
);
|
||||
const qa = await tx.orchestrationAttestation.findFirst({
|
||||
where: { runId: run.id, kind: 'QA', subjectHash: dto.sha256 },
|
||||
});
|
||||
if (!qa || qa.signer === a.id)
|
||||
throw new ForbiddenException('Independent QA evidence required');
|
||||
await tx.orchestrationArtifact.create({
|
||||
data: {
|
||||
runId: run.id,
|
||||
kind: 'STAGE_EVIDENCE',
|
||||
sha256: dto.evidenceSha256,
|
||||
uri: dto.uri,
|
||||
},
|
||||
});
|
||||
await tx.orchestrationAttestation.create({
|
||||
data: {
|
||||
runId: run.id,
|
||||
kind: 'STAGE',
|
||||
subjectHash: dto.sha256,
|
||||
signer: a.id,
|
||||
predicate: {
|
||||
evidenceSha256: dto.evidenceSha256,
|
||||
commitSha: dto.commitSha,
|
||||
environment: 'staging',
|
||||
},
|
||||
},
|
||||
});
|
||||
return this.event(
|
||||
tx,
|
||||
t,
|
||||
'GATE2_PENDING',
|
||||
a,
|
||||
`prepare-stage:${run.id}`,
|
||||
'STAGE_EVIDENCE_RECORDED',
|
||||
dto.evidenceSha256,
|
||||
);
|
||||
});
|
||||
}
|
||||
project(t: any, a: WorkflowIdentity) {
|
||||
const safe =
|
||||
t.operationMode === 'PREPARE' &&
|
||||
(t.metadata?.workflowScope ?? t.metadata?.scope) === 'STAGE_PREPARE';
|
||||
const owner =
|
||||
a?.id && (t.metadata?.createdBy === a.id || a.role === 'SUPER_ADMIN');
|
||||
const latest = [...(t.decisions ?? [])].filter((d: any) => d.gate === 1)
|
||||
.sort((a: any, b: any) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())[0];
|
||||
const expired = safe && t.state === 'READY' && latest?.decision === 'APPROVED' &&
|
||||
latest.actorId !== t.metadata.createdBy && new Date(latest.expiresAt).getTime() <= Date.now();
|
||||
const blocked = safe && t.state === 'READY' && (!latest || latest.decision !== 'APPROVED' ||
|
||||
latest.actorId === t.metadata.createdBy || new Date(latest.expiresAt).getTime() <= Date.now());
|
||||
const actions: string[] = [];
|
||||
if (expired && this.reviewer(a) && t.metadata.createdBy !== a.id)
|
||||
actions.push('APPROVE_GATE1');
|
||||
if (safe && owner && t.state === 'DRAFT') actions.push('SUBMIT_GATE1');
|
||||
if (safe && owner && t.state === 'GATE1_APPROVED')
|
||||
actions.push('QUEUE_PREPARE');
|
||||
if (
|
||||
safe &&
|
||||
this.reviewer(a) &&
|
||||
t.metadata?.createdBy !== a.id &&
|
||||
['GATE1_PENDING', 'GATE1_HELD'].includes(t.state)
|
||||
) {
|
||||
actions.push('APPROVE_GATE1', 'REJECT_GATE1');
|
||||
if (t.state === 'GATE1_PENDING') actions.push('HOLD_GATE1');
|
||||
}
|
||||
return {
|
||||
...t,
|
||||
displayState: blocked ? (expired ? 'GATE1_RENEWAL_REQUIRED' : 'BLOCKED') : t.state,
|
||||
metadata: safe
|
||||
? {
|
||||
scope: t.metadata.scope,
|
||||
...(t.metadata.workflowScope === 'STAGE_PREPARE'
|
||||
? { acceptanceCriteria: t.metadata.acceptanceCriteria }
|
||||
: {}),
|
||||
environment: 'staging',
|
||||
commitSha: t.metadata.commitSha,
|
||||
}
|
||||
: {},
|
||||
availableActions: actions,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { PrepareWorkflowService } from './prepare-workflow.service';
|
||||
(process.env.WORKFLOW_DB==='1'?describe:describe.skip)('renewal PostgreSQL serialization',()=>{
|
||||
const db=new PrismaClient();
|
||||
const service=new PrepareWorkflowService(db,{workerIds:['pmo'],stageCiIds:[]});
|
||||
const owner={id:'owner',role:'SUPER_ADMIN'};
|
||||
afterAll(()=>db.$disconnect());
|
||||
it('denies expired claim, serializes renewal/replay, retains original decision and enables one claim',async()=>{
|
||||
const t=await db.orchestrationTask.create({data:{key:crypto.randomUUID(),title:'Disposable renewal',department:'CONTENT',operationMode:'PREPARE',state:'READY',version:4,budgetLimitCents:20,metadata:{workflowScope:'STAGE_PREPARE',createdBy:'pmo',commitSha:'a'.repeat(40)}}});
|
||||
const old=await db.orchestrationApprovalDecision.create({data:{taskId:t.id,gate:1,decision:'APPROVED',actorId:'owner',expiresAt:new Date('2026-10-24T06:37:00Z'),idempotencyKey:crypto.randomUUID()}});
|
||||
await db.prepareWorkflowOutbox.create({data:{taskId:t.id}});
|
||||
const worker={id:'pmo',role:'WORKFLOW_WORKER'};
|
||||
await expect(service.claim(worker,t.id)).rejects.toMatchObject({status:403});
|
||||
const dto={expectedVersion:4,decision:'APPROVED',idempotencyKey:crypto.randomUUID()};
|
||||
const renewed=await Promise.all([service.decide(t.id,1,dto,owner),service.decide(t.id,1,dto,owner)]);
|
||||
expect(renewed.map(t=>t.version)).toEqual([5,5]);
|
||||
await expect(service.decide(t.id,1,{...dto,idempotencyKey:crypto.randomUUID()},owner)).rejects.toMatchObject({status:409});
|
||||
expect(await db.orchestrationApprovalDecision.count({where:{taskId:t.id}})).toBe(2);
|
||||
expect((await db.orchestrationApprovalDecision.findUniqueOrThrow({where:{id:old.id}})).expiresAt).toEqual(old.expiresAt);
|
||||
expect(await db.prepareWorkflowOutbox.count({where:{taskId:t.id}})).toBe(1);
|
||||
const claims=await Promise.all([service.claim(worker,t.id),service.claim(worker,t.id)]);
|
||||
expect(claims.filter(Boolean)).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
@@ -1,11 +0,0 @@
|
||||
import { workflowConfig } from './workflow-config';
|
||||
describe('startup owner reviewer configuration',()=>{
|
||||
it('defaults denied, accepts explicit UUID only in staging',()=>{
|
||||
expect(workflowConfig({}).ownerReviewerIds).toEqual([]);
|
||||
expect(workflowConfig({NODE_ENV:'production',CANINA_WORKFLOW_OWNER_REVIEWER_IDS:'12345678-1234-1234-1234-123456789012'})).toMatchObject({ownerReviewerIds:[]});
|
||||
expect(workflowConfig({CANINA_ENV:'staging',CANINA_WORKFLOW_OWNER_REVIEWER_IDS:'12345678-1234-1234-1234-123456789012'}).ownerReviewerIds).toEqual(['12345678-1234-1234-1234-123456789012']);
|
||||
});
|
||||
it.each(['*','admin','12345678-1234-1234-1234-123456789012,'])('fails startup on malformed explicit grant %s',ids=>{
|
||||
expect(()=>workflowConfig({CANINA_ENV:'staging',CANINA_WORKFLOW_OWNER_REVIEWER_IDS:ids})).toThrow('Invalid owner reviewer UUID allowlist');
|
||||
});
|
||||
});
|
||||
@@ -1,9 +0,0 @@
|
||||
import type { WorkflowConfig } from './prepare-workflow.service';
|
||||
/** Startup-only configuration; HTTP payloads can never supply reviewer grants. */
|
||||
export function workflowConfig(env: NodeJS.ProcessEnv): WorkflowConfig {
|
||||
const raw = env.CANINA_ENV === 'staging' ? env.CANINA_WORKFLOW_OWNER_REVIEWER_IDS ?? '' : '';
|
||||
const ids = raw ? raw.split(',') : [];
|
||||
if (ids.length > 20 || ids.some(id => !/^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/.test(id)))
|
||||
throw new Error('Invalid owner reviewer UUID allowlist');
|
||||
return {workerIds:[],stageCiIds:[],ownerReviewerIds:[...new Set(ids)]};
|
||||
}
|
||||
@@ -1,506 +0,0 @@
|
||||
import { Test } from '@nestjs/testing';
|
||||
import request from 'supertest';
|
||||
import { OrchestrationController } from './orchestration.controller';
|
||||
import { OrchestrationService } from './orchestration.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { RolesGuard } from '../common/guards/roles.guard';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { PrepareWorkflowService } from './prepare-workflow.service';
|
||||
import { OperationMode, WorkflowState } from './orchestration.domain';
|
||||
|
||||
const enabled = process.env.WORKFLOW_DB === '1';
|
||||
(enabled ? describe : describe.skip)('isolated prepare workflow', () => {
|
||||
const db = new PrismaClient();
|
||||
const admin = { id: 'proposer', role: 'Admin' };
|
||||
const reviewer = { id: 'reviewer', role: 'SUPER_ADMIN' };
|
||||
const worker = { id: 'worker-1', role: 'WORKFLOW_WORKER' };
|
||||
const ci = { id: 'isolated-ci', role: 'STAGE_CI' };
|
||||
const service = new PrepareWorkflowService(db as never, {
|
||||
workerIds: ['worker-1', 'worker-2'],
|
||||
stageCiIds: ['isolated-ci'],
|
||||
});
|
||||
let seq = 0;
|
||||
const dto = () => ({
|
||||
key: `wf-${Date.now()}-${seq++}`,
|
||||
title: 'Bounded isolated QA tracer',
|
||||
department: 'ENGINEERING',
|
||||
operationMode: OperationMode.PREPARE,
|
||||
budgetLimitCents: 20,
|
||||
metadata: {
|
||||
commitSha: 'a'.repeat(40),
|
||||
token: 'must-not-persist',
|
||||
scope: 'Staging schema audit only',
|
||||
acceptanceCriteria: ['Report all schema failures'],
|
||||
actorId: 'spoof',
|
||||
},
|
||||
});
|
||||
async function ready() {
|
||||
let t = await service.create(dto(), admin);
|
||||
t = await service.transition(
|
||||
t.id,
|
||||
{
|
||||
toState: WorkflowState.GATE1_PENDING,
|
||||
expectedVersion: t.version,
|
||||
idempotencyKey: `submit-${t.id}`,
|
||||
},
|
||||
admin,
|
||||
);
|
||||
t = await service.decide(
|
||||
t.id,
|
||||
1,
|
||||
{
|
||||
decision: 'APPROVED',
|
||||
expectedVersion: t.version,
|
||||
idempotencyKey: `approve-${t.id}`,
|
||||
expiresAt: new Date(Date.now() + 60000).toISOString(),
|
||||
},
|
||||
reviewer,
|
||||
);
|
||||
return t;
|
||||
}
|
||||
afterAll(async () => {
|
||||
await db.$disconnect();
|
||||
});
|
||||
it('serializes competing owner holds and preserves audited reason, then decides held task', async () => {
|
||||
let t = await service.create(dto(), admin);
|
||||
t = await service.transition(t.id, {toState:'GATE1_PENDING',expectedVersion:t.version,idempotencyKey:`submit-hold-${t.id}`}, admin);
|
||||
const command = {toState:'GATE1_HELD',expectedVersion:t.version,idempotencyKey:`hold-${t.id}`,reason:'Need source review'};
|
||||
const results = await Promise.allSettled([
|
||||
service.transition(t.id, command, reviewer),
|
||||
service.transition(t.id, {...command,idempotencyKey:`competing-${t.id}`}, reviewer),
|
||||
]);
|
||||
expect(results.filter(r=>r.status==='fulfilled')).toHaveLength(1);
|
||||
const held = await db.orchestrationTask.findUniqueOrThrow({where:{id:t.id}});
|
||||
expect(held.state).toBe('GATE1_HELD');
|
||||
expect(held.version).toBe(t.version+1);
|
||||
const events = await db.orchestrationEvent.findMany({where:{taskId:t.id,eventType:'PREPARE_GATE1_HELD'}});
|
||||
expect(events).toHaveLength(1);
|
||||
expect(events[0].actorId).toBe(reviewer.id);
|
||||
expect(JSON.stringify(events[0].payload)).toContain('Need source review');
|
||||
expect(await db.orchestrationApprovalDecision.count({where:{taskId:t.id}})).toBe(0);
|
||||
expect(await db.prepareWorkflowOutbox.count({where:{taskId:t.id}})).toBe(0);
|
||||
await expect(service.transition(t.id, {...command,idempotencyKey:events[0].idempotencyKey,expectedVersion:held.version,reason:'changed'},reviewer)).rejects.toMatchObject({status:409});
|
||||
const decision = await service.decide(t.id,1,{decision:'REJECTED',expectedVersion:held.version,idempotencyKey:`decline-held-${t.id}`,expiresAt:new Date(Date.now()+60000).toISOString()},reviewer);
|
||||
expect(decision.state).toBe('REJECTED');
|
||||
});
|
||||
it('persists and projects immutable bounded proposal scope without arbitrary metadata', async () => {
|
||||
const input = dto();
|
||||
const t = await service.create(input, admin);
|
||||
expect(t.metadata).toMatchObject({
|
||||
workflowScope: 'STAGE_PREPARE',
|
||||
scope: input.metadata.scope,
|
||||
acceptanceCriteria: input.metadata.acceptanceCriteria,
|
||||
commitSha: input.metadata.commitSha,
|
||||
});
|
||||
expect(t.metadata).not.toHaveProperty('token');
|
||||
expect(t.metadata).not.toHaveProperty('actorId');
|
||||
const submitted = await service.transition(
|
||||
t.id,
|
||||
{
|
||||
toState: 'GATE1_PENDING',
|
||||
expectedVersion: t.version,
|
||||
idempotencyKey: `immutable-${t.id}`,
|
||||
},
|
||||
admin,
|
||||
);
|
||||
expect(submitted.metadata).toEqual(t.metadata);
|
||||
expect(service.project(submitted, reviewer).metadata).toEqual({
|
||||
scope: input.metadata.scope,
|
||||
acceptanceCriteria: input.metadata.acceptanceCriteria,
|
||||
environment: 'staging',
|
||||
commitSha: input.metadata.commitSha,
|
||||
});
|
||||
});
|
||||
it.each([
|
||||
null,
|
||||
[],
|
||||
{},
|
||||
{ commitSha: 'a'.repeat(40), scope: '', acceptanceCriteria: ['ok'] },
|
||||
{
|
||||
commitSha: 'a'.repeat(40),
|
||||
scope: ' '.repeat(10),
|
||||
acceptanceCriteria: ['ok'],
|
||||
},
|
||||
{
|
||||
commitSha: 'a'.repeat(40),
|
||||
scope: 'x'.repeat(2001),
|
||||
acceptanceCriteria: ['ok'],
|
||||
},
|
||||
{ commitSha: 'a'.repeat(40), scope: 'ok', acceptanceCriteria: [] },
|
||||
{
|
||||
commitSha: 'a'.repeat(40),
|
||||
scope: 'ok',
|
||||
acceptanceCriteria: Array(21).fill('ok'),
|
||||
},
|
||||
{
|
||||
commitSha: 'a'.repeat(40),
|
||||
scope: 'ok',
|
||||
acceptanceCriteria: ['x'.repeat(501)],
|
||||
},
|
||||
{ commitSha: 'a'.repeat(40), scope: 'ok', acceptanceCriteria: [' '] },
|
||||
{ commitSha: 'a'.repeat(40), scope: 'ok', acceptanceCriteria: [123] },
|
||||
{ commitSha: ['a'.repeat(40)], scope: 'ok', acceptanceCriteria: ['ok'] },
|
||||
{ scope: 'ok', acceptanceCriteria: ['ok'] },
|
||||
])(
|
||||
'rejects unsafe or unbounded metadata %# without persisting',
|
||||
async (metadata) => {
|
||||
const input = { ...dto(), metadata };
|
||||
await expect(service.create(input, admin)).rejects.toMatchObject({
|
||||
status: 400,
|
||||
});
|
||||
expect(
|
||||
await db.orchestrationTask.count({ where: { key: input.key } }),
|
||||
).toBe(0);
|
||||
},
|
||||
);
|
||||
it('accepts exact metadata bounds', async () => {
|
||||
const metadata = {
|
||||
commitSha: 'a'.repeat(40),
|
||||
scope: 'x'.repeat(2000),
|
||||
acceptanceCriteria: Array(20).fill('x'.repeat(500)),
|
||||
};
|
||||
expect(
|
||||
(await service.create({ ...dto(), metadata }, admin)).metadata,
|
||||
).toMatchObject(metadata);
|
||||
});
|
||||
it('normalizes racing duplicate proposal keys to conflict without duplicate events', async () => {
|
||||
const input = dto();
|
||||
const results = await Promise.allSettled([
|
||||
service.create(input, admin),
|
||||
service.create({ ...input, title: 'Different intent' }, reviewer),
|
||||
]);
|
||||
expect(results.filter((r) => r.status === 'fulfilled')).toHaveLength(1);
|
||||
const rejected = results.find(
|
||||
(r) => r.status === 'rejected',
|
||||
) as PromiseRejectedResult;
|
||||
expect(rejected.reason).toMatchObject({ status: 409 });
|
||||
const persisted = await db.orchestrationTask.findUniqueOrThrow({
|
||||
where: { key: input.key },
|
||||
});
|
||||
expect(
|
||||
await db.orchestrationEvent.count({ where: { taskId: persisted.id } }),
|
||||
).toBe(1);
|
||||
await expect(service.create(input, admin)).rejects.toMatchObject({
|
||||
status: 409,
|
||||
});
|
||||
});
|
||||
it('enforces create and read contracts over HTTP with real PostgreSQL persistence', async () => {
|
||||
const module = await Test.createTestingModule({
|
||||
controllers: [OrchestrationController],
|
||||
providers: [
|
||||
{ provide: PrepareWorkflowService, useValue: service },
|
||||
{
|
||||
provide: OrchestrationService,
|
||||
useValue: {
|
||||
findAll: () => db.orchestrationTask.findMany(),
|
||||
findOne: (id: string) =>
|
||||
db.orchestrationTask.findUniqueOrThrow({ where: { id } }),
|
||||
},
|
||||
},
|
||||
],
|
||||
})
|
||||
.overrideGuard(JwtAuthGuard)
|
||||
.useValue({
|
||||
canActivate(context: any) {
|
||||
context.switchToHttp().getRequest().user = {
|
||||
sub: admin.id,
|
||||
role: admin.role,
|
||||
};
|
||||
return true;
|
||||
},
|
||||
})
|
||||
.overrideGuard(RolesGuard)
|
||||
.useValue({ canActivate: () => true })
|
||||
.compile();
|
||||
const app = module.createNestApplication();
|
||||
await app.init();
|
||||
try {
|
||||
const input = dto();
|
||||
const response = await request(app.getHttpServer())
|
||||
.post('/admin/orchestration/tasks')
|
||||
.send(input)
|
||||
.expect(201);
|
||||
expect(response.body.metadata).toEqual({
|
||||
scope: input.metadata.scope,
|
||||
acceptanceCriteria: input.metadata.acceptanceCriteria,
|
||||
commitSha: input.metadata.commitSha,
|
||||
environment: 'staging',
|
||||
});
|
||||
expect(response.body.availableActions).toEqual(['SUBMIT_GATE1']);
|
||||
const detail = await request(app.getHttpServer())
|
||||
.get(`/admin/orchestration/tasks/${response.body.id}`)
|
||||
.expect(200);
|
||||
expect(detail.body.metadata).toEqual(response.body.metadata);
|
||||
const list = await request(app.getHttpServer())
|
||||
.get('/admin/orchestration/tasks')
|
||||
.expect(200);
|
||||
expect(
|
||||
list.body.find((row: any) => row.id === response.body.id).metadata,
|
||||
).toEqual(response.body.metadata);
|
||||
expect(JSON.stringify(response.body)).not.toMatch(
|
||||
/createdBy|must-not-persist|actorId|workflowScope/,
|
||||
);
|
||||
await request(app.getHttpServer())
|
||||
.post('/admin/orchestration/tasks')
|
||||
.send(input)
|
||||
.expect(409);
|
||||
await request(app.getHttpServer())
|
||||
.post('/admin/orchestration/tasks')
|
||||
.send({
|
||||
...input,
|
||||
key: `${input.key}-invalid`,
|
||||
metadata: { scope: 'only stage', acceptanceCriteria: ['QA passes'] },
|
||||
})
|
||||
.expect(400);
|
||||
await request(app.getHttpServer())
|
||||
.post('/admin/orchestration/tasks')
|
||||
.send({
|
||||
...input,
|
||||
key: `${input.key}-invalid`,
|
||||
metadata: { ...input.metadata, acceptanceCriteria: [' '] },
|
||||
})
|
||||
.expect(400);
|
||||
} finally {
|
||||
await app.close();
|
||||
}
|
||||
});
|
||||
it('fences heartbeat and completes QA plus independent Stage evidence only to Gate2 pending', async () => {
|
||||
const t = await ready();
|
||||
const c = await service.claim(worker, t.id);
|
||||
await expect(
|
||||
service.heartbeat({ ...worker, id: 'worker-2' }, c),
|
||||
).rejects.toThrow(/lease/i);
|
||||
await service.heartbeat(worker, c);
|
||||
const qa = {
|
||||
...c,
|
||||
sha256: 'b'.repeat(64),
|
||||
uri: 'https://ci.example/qa',
|
||||
actualCents: 8,
|
||||
};
|
||||
await service.complete(worker, qa);
|
||||
await expect(
|
||||
service.stageEvidence(
|
||||
{ id: 'proposer', role: 'Admin' },
|
||||
{
|
||||
taskId: t.id,
|
||||
runId: c.runId,
|
||||
sha256: qa.sha256,
|
||||
commitSha: c.commitSha,
|
||||
evidenceSha256: 'c'.repeat(64),
|
||||
uri: 'https://ci.example/stage',
|
||||
},
|
||||
),
|
||||
).rejects.toThrow(/identity/i);
|
||||
await service.stageEvidence(ci, {
|
||||
taskId: t.id,
|
||||
runId: c.runId,
|
||||
sha256: qa.sha256,
|
||||
commitSha: c.commitSha,
|
||||
evidenceSha256: 'c'.repeat(64),
|
||||
uri: 'https://ci.example/stage',
|
||||
});
|
||||
expect(
|
||||
await db.orchestrationTask.findUnique({ where: { id: t.id } }),
|
||||
).toMatchObject({ state: 'GATE2_PENDING' });
|
||||
expect(
|
||||
await db.orchestrationBudgetLedger.aggregate({
|
||||
where: { taskId: t.id },
|
||||
_sum: { amountCents: true },
|
||||
}),
|
||||
).toMatchObject({ _sum: { amountCents: 8 } });
|
||||
await expect(service.complete(worker, qa)).rejects.toThrow(/lease|active/i);
|
||||
});
|
||||
it('bounds expired-lease retries and rejects stale completion without releasing unknown costs', async () => {
|
||||
const t = await service.create({ ...dto(), budgetLimitCents: 60 }, admin);
|
||||
let current = await service.transition(
|
||||
t.id,
|
||||
{
|
||||
toState: 'GATE1_PENDING',
|
||||
expectedVersion: 1,
|
||||
idempotencyKey: `s-${t.id}`,
|
||||
},
|
||||
admin,
|
||||
);
|
||||
current = await service.decide(
|
||||
t.id,
|
||||
1,
|
||||
{
|
||||
decision: 'APPROVED',
|
||||
expectedVersion: current.version,
|
||||
idempotencyKey: `a-${t.id}`,
|
||||
expiresAt: new Date(Date.now() + 60000).toISOString(),
|
||||
},
|
||||
reviewer,
|
||||
);
|
||||
expect(current.state).toBe('READY');
|
||||
const first = await service.claim(worker, t.id);
|
||||
for (let i = 0; i < 2; i++) {
|
||||
await db.prepareWorkflowOutbox.update({
|
||||
where: { taskId: t.id },
|
||||
data: { leaseUntil: new Date(0) },
|
||||
});
|
||||
const next = await service.claim(worker, t.id);
|
||||
expect(next.attempt).toBe(i + 2);
|
||||
}
|
||||
await expect(
|
||||
service.complete(worker, {
|
||||
...first,
|
||||
sha256: 'b'.repeat(64),
|
||||
uri: 'https://ci.example/qa',
|
||||
actualCents: 0,
|
||||
}),
|
||||
).rejects.toThrow(/lease/i);
|
||||
await db.prepareWorkflowOutbox.update({
|
||||
where: { taskId: t.id },
|
||||
data: { leaseUntil: new Date(0) },
|
||||
});
|
||||
await expect(service.claim(worker, t.id)).rejects.toThrow(/exhausted/i);
|
||||
expect(await db.orchestrationRun.count({ where: { taskId: t.id } })).toBe(
|
||||
3,
|
||||
);
|
||||
expect(
|
||||
await db.orchestrationBudgetLedger.aggregate({
|
||||
where: { taskId: t.id },
|
||||
_sum: { amountCents: true },
|
||||
}),
|
||||
).toMatchObject({ _sum: { amountCents: 60 } });
|
||||
});
|
||||
it('rejects self approval, non-PREPARE and unprovisioned workers', async () => {
|
||||
await expect(
|
||||
service.create(
|
||||
{ ...dto(), operationMode: OperationMode.MUTATE_PRODUCTION },
|
||||
admin,
|
||||
),
|
||||
).rejects.toThrow(/PREPARE/);
|
||||
let t = await service.create(dto(), reviewer);
|
||||
t = await service.transition(
|
||||
t.id,
|
||||
{
|
||||
toState: 'GATE1_PENDING',
|
||||
expectedVersion: 1,
|
||||
idempotencyKey: `s-${t.id}`,
|
||||
},
|
||||
reviewer,
|
||||
);
|
||||
await expect(
|
||||
service.decide(
|
||||
t.id,
|
||||
1,
|
||||
{
|
||||
decision: 'APPROVED',
|
||||
expectedVersion: t.version,
|
||||
idempotencyKey: `a-${t.id}`,
|
||||
expiresAt: new Date(Date.now() + 60000).toISOString(),
|
||||
},
|
||||
reviewer,
|
||||
),
|
||||
).rejects.toThrow(/independent/i);
|
||||
await expect(
|
||||
service.claim({ id: 'spoof', role: 'WORKFLOW_WORKER' }),
|
||||
).rejects.toThrow(/provisioned/i);
|
||||
await expect(service.decide(t.id, 2, {}, reviewer)).rejects.toThrow(
|
||||
/disabled/i,
|
||||
);
|
||||
});
|
||||
it('serializes duplicate queue submission and denies claims beyond remaining task budget', async () => {
|
||||
const t = await ready();
|
||||
await db.orchestrationBudgetLedger.create({
|
||||
data: {
|
||||
taskId: t.id,
|
||||
amountCents: 1,
|
||||
kind: 'SPEND',
|
||||
actorId: 'fixture',
|
||||
idempotencyKey: `spend-${t.id}`,
|
||||
},
|
||||
});
|
||||
await expect(service.claim(worker, t.id)).rejects.toThrow(/budget/i);
|
||||
expect(await db.orchestrationRun.count({ where: { taskId: t.id } })).toBe(
|
||||
0,
|
||||
);
|
||||
expect(
|
||||
await db.prepareWorkflowOutbox.count({ where: { taskId: t.id } }),
|
||||
).toBe(1);
|
||||
});
|
||||
it('replays submission safely and serializes racing queue requests', async () => {
|
||||
let t = await service.create(dto(), admin);
|
||||
const submit = {
|
||||
toState: 'GATE1_PENDING',
|
||||
expectedVersion: t.version,
|
||||
idempotencyKey: `replay-${t.id}`,
|
||||
};
|
||||
t = await service.transition(t.id, submit, admin);
|
||||
expect((await service.transition(t.id, submit, admin)).version).toBe(
|
||||
t.version,
|
||||
);
|
||||
await expect(
|
||||
service.transition(t.id, { ...submit, toState: 'READY' }, admin),
|
||||
).rejects.toThrow(/mismatch/i);
|
||||
t = await service.decide(
|
||||
t.id,
|
||||
1,
|
||||
{
|
||||
decision: 'APPROVED',
|
||||
expectedVersion: t.version,
|
||||
idempotencyKey: `r-approve-${t.id}`,
|
||||
expiresAt: new Date(Date.now() + 60000).toISOString(),
|
||||
},
|
||||
reviewer,
|
||||
);
|
||||
expect(t.state).toBe('READY');
|
||||
expect(
|
||||
await db.prepareWorkflowOutbox.count({ where: { taskId: t.id } }),
|
||||
).toBe(1);
|
||||
});
|
||||
it('kill switch freezes claims without creating run or reservation', async () => {
|
||||
const t = await ready();
|
||||
await db.orchestrationKillSwitch.create({
|
||||
data: {
|
||||
scope: 'GLOBAL',
|
||||
level: 'FREEZE_NEW_WORK',
|
||||
reason: 'isolated test',
|
||||
actorId: 'fixture',
|
||||
},
|
||||
});
|
||||
try {
|
||||
await expect(service.claim(worker, t.id)).rejects.toThrow(/kill switch/i);
|
||||
} finally {
|
||||
await db.orchestrationKillSwitch.delete({ where: { scope: 'GLOBAL' } });
|
||||
}
|
||||
expect(await db.orchestrationRun.count({ where: { taskId: t.id } })).toBe(
|
||||
0,
|
||||
);
|
||||
});
|
||||
it('persists an atomic outbox and exactly one worker wins concurrent claims', async () => {
|
||||
const t = await ready();
|
||||
const claims = await Promise.all([
|
||||
service.claim(worker, t.id),
|
||||
service.claim({ id: 'worker-2', role: 'WORKFLOW_WORKER' }, t.id),
|
||||
]);
|
||||
expect(claims.filter(Boolean)).toHaveLength(1);
|
||||
const claim = claims.find(Boolean)!;
|
||||
expect(claim.taskId).toBe(t.id);
|
||||
expect(claim.attempt).toBe(1);
|
||||
expect(await db.orchestrationRun.count({ where: { taskId: t.id } })).toBe(
|
||||
1,
|
||||
);
|
||||
expect(
|
||||
await db.orchestrationBudgetLedger.aggregate({
|
||||
where: { taskId: t.id },
|
||||
_sum: { amountCents: true },
|
||||
}),
|
||||
).toMatchObject({ _sum: { amountCents: 20 } });
|
||||
expect(
|
||||
await service.project(
|
||||
await db.orchestrationTask.findUniqueOrThrow({ where: { id: t.id } }),
|
||||
admin,
|
||||
),
|
||||
).toMatchObject({
|
||||
metadata: {
|
||||
scope: 'Staging schema audit only',
|
||||
acceptanceCriteria: ['Report all schema failures'],
|
||||
environment: 'staging',
|
||||
commitSha: 'a'.repeat(40),
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -3,6 +3,11 @@ import { PrismaService } from "../prisma/prisma.service";
|
||||
import { GetProductsDto } from "./dto/get-products.dto";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { RevalidationService } from "../common/revalidation/revalidation.service";
|
||||
import {
|
||||
createPaginationMeta,
|
||||
extractSearchTokens,
|
||||
calculateRelevanceScore,
|
||||
} from "../common/utils/search.utils";
|
||||
|
||||
@Injectable()
|
||||
export class ProductsService {
|
||||
@@ -62,37 +67,60 @@ export class ProductsService {
|
||||
andConditions.push({ priceValue: { lte: Number(maxPrice) } });
|
||||
}
|
||||
|
||||
if (search) {
|
||||
andConditions.push({
|
||||
OR: [
|
||||
{ artNo: { contains: search, mode: "insensitive" } },
|
||||
{ barcode: { contains: search, mode: "insensitive" } },
|
||||
{ nameFa: { contains: search, mode: "insensitive" } },
|
||||
{ nameEn: { contains: search, mode: "insensitive" } },
|
||||
{ description: { contains: search, mode: "insensitive" } },
|
||||
{ shortDescription: { contains: search, mode: "insensitive" } },
|
||||
const tokens = extractSearchTokens(search);
|
||||
if (tokens.length > 0) {
|
||||
const mode = "insensitive" as const;
|
||||
const searchConditions: Prisma.ProductWhereInput[] = [
|
||||
{ artNo: { contains: search, mode } },
|
||||
{ barcode: { contains: search, mode } },
|
||||
{ slug: { contains: search, mode } },
|
||||
{ nameFa: { contains: search, mode } },
|
||||
{ nameEn: { contains: search, mode } },
|
||||
{ shortDescription: { contains: search, mode } },
|
||||
];
|
||||
|
||||
for (const token of tokens) {
|
||||
searchConditions.push(
|
||||
{ nameFa: { contains: token, mode } },
|
||||
{ nameEn: { contains: token, mode } },
|
||||
{
|
||||
symptoms: {
|
||||
ingredientList: {
|
||||
some: {
|
||||
symptom: { contains: search, mode: "insensitive" },
|
||||
OR: [
|
||||
{ ingredient: { contains: token, mode } },
|
||||
{ ingredientCardSlug: { contains: token, mode } },
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
{
|
||||
symptoms: {
|
||||
some: {
|
||||
symptom: { contains: token, mode },
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
andConditions.push({
|
||||
OR: searchConditions,
|
||||
});
|
||||
}
|
||||
|
||||
const whereClause: Prisma.ProductWhereInput =
|
||||
andConditions.length > 0 ? { AND: andConditions } : {};
|
||||
|
||||
const skip = (page - 1) * limit;
|
||||
const safePage = Math.max(1, Number(page) || 1);
|
||||
const safeLimit = Math.max(1, Number(limit) || 50);
|
||||
const skip = (safePage - 1) * safeLimit;
|
||||
|
||||
// 1. Fetch products with lean relations (no heavy N+1 orderItems join)
|
||||
// 1. Fetch products with lean relations
|
||||
const [rawProducts, total] = await Promise.all([
|
||||
this.prisma.product.findMany({
|
||||
where: whereClause,
|
||||
skip,
|
||||
take: limit,
|
||||
skip: tokens.length > 0 ? undefined : skip,
|
||||
take: tokens.length > 0 ? 100 : safeLimit,
|
||||
orderBy: { [sortBy]: sortOrder },
|
||||
include: {
|
||||
ingredientList: true,
|
||||
@@ -102,8 +130,34 @@ export class ProductsService {
|
||||
this.prisma.product.count({ where: whereClause }),
|
||||
]);
|
||||
|
||||
// Rank by relevance if searching
|
||||
let candidateProducts = rawProducts;
|
||||
if (tokens.length > 0 && search) {
|
||||
candidateProducts = [...rawProducts]
|
||||
.map((p) => ({
|
||||
product: p,
|
||||
score: calculateRelevanceScore(
|
||||
{
|
||||
artNo: p.artNo,
|
||||
barcode: p.barcode,
|
||||
slug: p.slug,
|
||||
title: p.nameFa || p.nameEn,
|
||||
shortDescription: p.shortDescription,
|
||||
description: p.description,
|
||||
ingredients: p.ingredientList,
|
||||
symptoms: p.symptoms,
|
||||
},
|
||||
search,
|
||||
tokens,
|
||||
),
|
||||
}))
|
||||
.sort((a, b) => b.score - a.score)
|
||||
.map((item) => item.product)
|
||||
.slice(skip, skip + safeLimit);
|
||||
}
|
||||
|
||||
// 2. Efficient single aggregate query for total sold counts for the current page slice
|
||||
const productIds = rawProducts.map((p) => p.id);
|
||||
const productIds = candidateProducts.map((p) => p.id);
|
||||
let salesMap: Record<string, number> = {};
|
||||
|
||||
if (productIds.length > 0) {
|
||||
@@ -131,7 +185,7 @@ export class ProductsService {
|
||||
userRole === "SuperAdmin";
|
||||
const isAdmin = userRole === "ADMIN" || userRole === "SuperAdmin";
|
||||
|
||||
const data = rawProducts.map((p) => {
|
||||
const data = candidateProducts.map((p) => {
|
||||
const totalSold = salesMap[p.id] || 0;
|
||||
const { buyPrice, wholesalePrice, ...publicProduct } = p;
|
||||
void buyPrice;
|
||||
@@ -146,12 +200,7 @@ export class ProductsService {
|
||||
|
||||
return {
|
||||
data,
|
||||
meta: {
|
||||
total,
|
||||
page,
|
||||
lastPage: Math.ceil(total / limit) || 1,
|
||||
limit,
|
||||
},
|
||||
meta: createPaginationMeta(total, safePage, safeLimit),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
import { RouterUsageAdapter } from './router-usage.adapter';
|
||||
const stats = {totalRequests:0,totalPromptTokens:0,totalCompletionTokens:0,totalCachedTokens:0,byModel:{},byProvider:{},byAccount:{}};
|
||||
describe('bounded read-only adapter', () => {
|
||||
afterEach(() => jest.restoreAllMocks());
|
||||
it('uses only authenticated GET stats with a validated period and never follows redirects', async () => {
|
||||
const fetcher = jest.spyOn(global, 'fetch').mockResolvedValue(new Response(JSON.stringify(stats)));
|
||||
const adapter = new RouterUsageAdapter();
|
||||
process.env.ROUTER_USAGE_BASE_URL = 'https://nr.parsaaghayi.ir';
|
||||
process.env.ROUTER_USAGE_CLI_TOKEN = 'server-only-secret';
|
||||
const result = await adapter.summary('24h');
|
||||
expect(result.status).toBe('available');
|
||||
expect(result.attribution).toEqual({scope:'9router_total',hermes:null,additive:false});
|
||||
expect(fetcher).toHaveBeenCalledTimes(1);
|
||||
expect(fetcher).toHaveBeenCalledWith('https://nr.parsaaghayi.ir/api/usage/stats?period=24h', expect.objectContaining({method:'GET',redirect:'error',headers:{'x-9r-cli-token':'server-only-secret',Accept:'application/json'}}));
|
||||
expect(JSON.stringify(result)).not.toContain('server-only-secret');
|
||||
});
|
||||
it('rejects invalid time filters before IO', async () => {
|
||||
const fetcher = jest.spyOn(global,'fetch');
|
||||
await expect(new RouterUsageAdapter().summary('../providers')).rejects.toThrow();
|
||||
expect(fetcher).not.toHaveBeenCalled();
|
||||
});
|
||||
it('fails closed when credential is missing', async () => {
|
||||
delete process.env.ROUTER_USAGE_CLI_TOKEN;
|
||||
const fetcher = jest.spyOn(global,'fetch');
|
||||
const result = await new RouterUsageAdapter().summary('7d');
|
||||
expect(result).toMatchObject({status:'unavailable',data:null,reason:'not_configured'});
|
||||
expect(fetcher).not.toHaveBeenCalled();
|
||||
});
|
||||
it.each([401,403,429,500])('returns unknown data without upstream error text for %s', async status => {
|
||||
process.env.ROUTER_USAGE_CLI_TOKEN='secret';
|
||||
jest.spyOn(global,'fetch').mockResolvedValue(new Response('credential-secret',{status}));
|
||||
const result = await new RouterUsageAdapter().summary('7d');
|
||||
expect(result).toMatchObject({status:'unavailable',data:null,reason:'upstream_unavailable'});
|
||||
expect(JSON.stringify(result)).not.toContain('credential-secret');
|
||||
});
|
||||
it('rejects oversized bodies even without content length', async () => {
|
||||
process.env.ROUTER_USAGE_CLI_TOKEN='secret';
|
||||
jest.spyOn(global,'fetch').mockResolvedValue(new Response('x'.repeat(1048577)));
|
||||
expect(await new RouterUsageAdapter().summary('7d')).toMatchObject({status:'unavailable',data:null});
|
||||
});
|
||||
it('does not return stale last-good totals after a failure', async () => {
|
||||
process.env.ROUTER_USAGE_CLI_TOKEN='secret';
|
||||
jest.spyOn(global,'fetch').mockResolvedValueOnce(new Response(JSON.stringify(stats))).mockRejectedValueOnce(new Error('token-secret'));
|
||||
const adapter=new RouterUsageAdapter();
|
||||
expect((await adapter.summary('7d')).status).toBe('available');
|
||||
expect(await adapter.summary('7d')).toMatchObject({status:'unavailable',data:null});
|
||||
});
|
||||
});
|
||||
|
||||
describe('transport boundaries', () => {
|
||||
afterEach(()=>jest.restoreAllMocks());
|
||||
it('never sends credentials to a configured alternate origin', async()=>{
|
||||
process.env.ROUTER_USAGE_CLI_TOKEN='secret';
|
||||
process.env.ROUTER_USAGE_BASE_URL='https://attacker.invalid';
|
||||
const fetcher=jest.spyOn(global,'fetch');
|
||||
expect(await new RouterUsageAdapter().summary()).toMatchObject({status:'unavailable',reason:'invalid_configuration'});
|
||||
expect(fetcher).not.toHaveBeenCalled();
|
||||
delete process.env.ROUTER_USAGE_BASE_URL;
|
||||
});
|
||||
it('bounds simultaneous upstream calls and exposes no stale totals', async()=>{
|
||||
process.env.ROUTER_USAGE_CLI_TOKEN='secret';
|
||||
let resolve!: (value: Response) => void;
|
||||
const pending=new Promise<Response>(r=>{resolve=r;});
|
||||
jest.spyOn(global,'fetch').mockReturnValue(pending);
|
||||
const adapter=new RouterUsageAdapter();
|
||||
const calls=Array.from({length:4},()=>adapter.summary());
|
||||
expect(await adapter.summary()).toMatchObject({status:'unavailable',reason:'busy',data:null});
|
||||
resolve(new Response('',{status:503}));
|
||||
await Promise.all(calls);
|
||||
});
|
||||
});
|
||||
@@ -1,45 +0,0 @@
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { normalizeStats } from './router-usage.contract';
|
||||
export const ROUTER_PERIODS = ['today','24h','7d','30d','60d','all'] as const;
|
||||
@Injectable()
|
||||
export class RouterUsageAdapter {
|
||||
private active = 0;
|
||||
async summary(period = '7d') {
|
||||
if (!ROUTER_PERIODS.includes(period as typeof ROUTER_PERIODS[number])) throw new BadRequestException('Invalid period');
|
||||
const base = {source:'9router' as const,period,attribution:{scope:'9router_total',hermes:null,additive:false},
|
||||
capabilities:{errors:false,reasoningTokens:false,cacheWriteTokens:false,quota:false,customTimeRange:false},
|
||||
freshness:{retrievedAt:null as string | null,sourceUpdatedAt:null,stale:null as boolean | null}};
|
||||
const unavailable = (reason: string) => ({...base,status:'unavailable' as const,reason,data:null});
|
||||
const token=process.env.ROUTER_USAGE_CLI_TOKEN;
|
||||
if (!token) return unavailable('not_configured');
|
||||
if (this.active >= 4) return unavailable('busy');
|
||||
this.active++;
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(),5000);
|
||||
try {
|
||||
const url=new URL(process.env.ROUTER_USAGE_BASE_URL || 'https://nr.parsaaghayi.ir');
|
||||
if (url.origin !== 'https://nr.parsaaghayi.ir' || url.username || url.password || url.pathname !== '/' || url.search || url.hash) return unavailable('invalid_configuration');
|
||||
const response=await fetch(`${url.origin}/api/usage/stats?period=${period}`,{
|
||||
method:'GET',redirect:'error',signal:controller.signal,
|
||||
headers:{'x-9r-cli-token':token,Accept:'application/json'},
|
||||
});
|
||||
if (!response.ok || !response.body) throw new Error('Upstream unavailable');
|
||||
const reader=response.body.getReader();
|
||||
const chunks:Uint8Array[]=[];
|
||||
let size=0;
|
||||
try {
|
||||
while (true) {
|
||||
const {done,value}=await reader.read();
|
||||
if (done) break;
|
||||
size+=value.byteLength;
|
||||
if (size>1048576) {await reader.cancel(); throw new Error('Response too large');}
|
||||
chunks.push(value);
|
||||
}
|
||||
} finally { reader.releaseLock(); }
|
||||
const data=normalizeStats(JSON.parse(Buffer.concat(chunks).toString('utf8')));
|
||||
return {...base,status:'available' as const,reason:null,data,
|
||||
freshness:{retrievedAt:new Date().toISOString(),sourceUpdatedAt:null,stale:null}};
|
||||
} catch { return unavailable('upstream_unavailable'); }
|
||||
finally { clearTimeout(timeout); this.active--; }
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
type RecordValue = Record<string, unknown>;
|
||||
export function record(value: unknown): RecordValue {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) throw new Error('Invalid upstream schema');
|
||||
return value as RecordValue;
|
||||
}
|
||||
function count(value: unknown): number | null {
|
||||
return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 ? value : null;
|
||||
}
|
||||
function identifier(value: unknown): string | null {
|
||||
return typeof value === 'string' && /^[a-zA-Z0-9_./:@()+ -]{1,180}$/.test(value) && !value.includes('@') && !/^(sk-|Bearer )/i.test(value) ? value : null;
|
||||
}
|
||||
function metrics(row: RecordValue, prefix = '') {
|
||||
const key = (name: string) => prefix ? prefix + name[0].toUpperCase() + name.slice(1) : name;
|
||||
return {
|
||||
requests: count(row[key('requests')]), inputTokens: count(row[key('promptTokens')]),
|
||||
outputTokens: count(row[key('completionTokens')]), cacheReadTokens: count(row[key('cachedTokens')]),
|
||||
cacheWriteTokens: null, reasoningTokens: null, errors: null,
|
||||
};
|
||||
}
|
||||
export function normalizeStats(value: unknown) {
|
||||
const stats = record(value);
|
||||
if (count(stats.totalRequests) === null) throw new Error('Invalid stats');
|
||||
const rows = (key: string) => {
|
||||
const entries = Object.entries(record(stats[key]));
|
||||
if (entries.length > 2000) throw new Error('Too many aggregates');
|
||||
return entries;
|
||||
};
|
||||
return {
|
||||
totals: metrics(stats, 'total'),
|
||||
providers: rows('byProvider').map(([id, row]) => ({provider: identifier(id), ...metrics(record(row))})),
|
||||
models: rows('byModel').map(([, row]) => { const r = record(row); return {model: identifier(r.rawModel), provider: identifier(r.provider), ...metrics(r)}; }),
|
||||
accounts: rows('byAccount').map(([, row]) => { const r = record(row); return {
|
||||
accountId: identifier(r.connectionId), model: identifier(r.rawModel), provider: identifier(r.provider), ...metrics(r),
|
||||
quota: {status: 'unknown' as const, remaining: null, resetAt: null, reason: 'upstream_quota_get_mutates_credentials'},
|
||||
}; }),
|
||||
};
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
import { Controller, Get, Header, Query, UseGuards, ValidationPipe } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { IsIn, IsOptional } from 'class-validator';
|
||||
import { Roles } from '../common/decorators/roles.decorator';
|
||||
import { RolesGuard } from '../common/guards/roles.guard';
|
||||
import { ROUTER_PERIODS, RouterUsageAdapter } from './router-usage.adapter';
|
||||
export class RouterUsageQuery {
|
||||
@IsOptional()
|
||||
@IsIn(ROUTER_PERIODS)
|
||||
period?: string;
|
||||
}
|
||||
@Controller('admin/router-usage')
|
||||
@UseGuards(AuthGuard('jwt'), RolesGuard)
|
||||
@Roles('admin')
|
||||
export class RouterUsageController {
|
||||
constructor(private readonly adapter: RouterUsageAdapter) {}
|
||||
@Get()
|
||||
@Header('Cache-Control','private, no-store')
|
||||
summary(@Query(new ValidationPipe({transform:true,whitelist:true,forbidNonWhitelisted:true})) query: RouterUsageQuery) {
|
||||
return this.adapter.summary(query.period ?? '7d');
|
||||
}
|
||||
}
|
||||
Loaded 100 of 224 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user