diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 1c8c6e15..19e2cc6d 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -14,6 +14,8 @@ jobs: env: BRANCH: ${{ github.ref_name }} SUPPORT_CHAT_KEY: ${{ secrets.SUPPORT_CHAT_KEY }} + DEPLOY_SHA: ${{ github.sha }} + GITHUB_ACTIONS: 'true' run: | # 1. رفع مشکل DNS و غیرفعال کردن پروکسی برای دامنه محلی echo "87.248.133.138 git.parsaaghayi.ir" >> /etc/hosts @@ -25,7 +27,10 @@ jobs: # 2. دانلود و اجرای اسکریپت deploy.sh بر اساس برنچ جاری mkdir -p scripts - wget --no-proxy -qO scripts/deploy.sh https://git.parsaaghayi.ir/parsa/canina/raw/branch/${BRANCH}/scripts/deploy.sh + wget --no-proxy -qO scripts/deploy.sh https://git.parsaaghayi.ir/parsa/canina/raw/commit/${DEPLOY_SHA}/scripts/deploy.sh + if [ "$BRANCH" = develop ]; then + wget --no-proxy -qO scripts/release-corporate-stage.sh https://git.parsaaghayi.ir/parsa/canina/raw/commit/${DEPLOY_SHA}/scripts/release-corporate-stage.sh + fi chmod +x scripts/deploy.sh cd scripts diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prisma index a1529d03..5ee314ff 100644 --- a/backend/prisma/schema.prisma +++ b/backend/prisma/schema.prisma @@ -1207,3 +1207,203 @@ model NotificationTemplate { @@index([isActive]) @@map("notification_templates") } + + +enum CorporateTaskStatus { + TODO + IN_PROGRESS + TEST + DONE + BLOCKED + REJECTED + CANCELLED +} + +enum CorporatePriority { + CEO_EXPEDITE + CRITICAL + HIGH + MEDIUM + LOW +} + +model CorporateTask { + id String @id @default(uuid()) @db.Uuid + taskCode String @unique + title String + normalizedTitle String + normalizedCode String + description String + department String + priority CorporatePriority @default(CEO_EXPEDITE) + status CorporateTaskStatus @default(TODO) + actionType String @default("CEO_DIRECTIVE") + dueCycle String? + parentId String? @db.Uuid + parent CorporateTask? @relation("TaskChildren", fields: [parentId], references: [id]) + children CorporateTask[] @relation("TaskChildren") + goalId String? @db.Uuid + goal CorporateGoal? @relation(fields: [goalId], references: [id]) + assignedAgent String? + claimToken String? @db.Uuid + leasedUntil DateTime? @db.Timestamptz() + executionCount Int @default(0) + qaCount Int @default(0) + rejectionCount Int @default(0) + createdBy String + createdAt DateTime @default(now()) @db.Timestamptz() + updatedAt DateTime @updatedAt @db.Timestamptz() + logs CorporateEvent[] + evidence CorporateEvidence[] + reviews CorporateReview[] + effects CorporateEffect[] + @@index([status, priority, updatedAt]) + @@index([parentId]) + @@index([goalId]) + @@map("corporate_tasks") +} + +model CorporateEvent { + id String @id @default(uuid()) @db.Uuid + taskId String @db.Uuid + task CorporateTask @relation(fields: [taskId], references: [id]) + speaker String + speakerName String + step String + content String + payload Json? + timestamp DateTime @default(now()) @db.Timestamptz() + @@index([taskId, timestamp]) + @@map("corporate_events") +} + +model CorporateEvidence { + id String @id @default(uuid()) @db.Uuid + taskId String @db.Uuid + task CorporateTask @relation(fields: [taskId], references: [id]) + claimToken String @db.Uuid + executor String + artifactUri String + sha256 String + summary String + tool String + result String + createdAt DateTime @default(now()) @db.Timestamptz() + reviews CorporateReview[] + @@index([taskId]) + @@map("corporate_evidence") +} + +model CorporateReview { + id String @id @default(uuid()) @db.Uuid + taskId String @db.Uuid + task CorporateTask @relation(fields: [taskId], references: [id]) + evidenceId String @db.Uuid + evidence CorporateEvidence @relation(fields: [evidenceId], references: [id]) + reviewer String + passed Boolean + findings String + verification Json + createdAt DateTime @default(now()) @db.Timestamptz() + @@index([taskId]) + @@map("corporate_reviews") +} + +model CorporateResearch { + id String @id @default(uuid()) @db.Uuid + title String + category String + source String + opportunity String + impact String + recommendedAction String + status String @default("DISCOVERED") + cadence String + provenance Json + createdBy String + createdAt DateTime @default(now()) @db.Timestamptz() + sources CorporateSource[] + roadmaps CorporateRoadmap[] + @@map("corporate_research") +} + +model CorporateSource { + id String @id @default(uuid()) @db.Uuid + researchId String @db.Uuid + research CorporateResearch @relation(fields: [researchId], references: [id]) + url String + title String + excerpt String + retrievedAt DateTime @db.Timestamptz() + sha256 String + @@index([researchId]) + @@map("corporate_sources") +} + +model CorporateRoadmap { + id String @id @default(uuid()) @db.Uuid + researchId String @db.Uuid + research CorporateResearch @relation(fields: [researchId], references: [id]) + cycleName String + month String + seasonalTheme String + strategicGoals String[] + plannedArticlesCount Int + plannedRefreshesCount Int + plannedCatalogEnrichments Int + isActive Boolean @default(false) + createdBy String + createdAt DateTime @default(now()) @db.Timestamptz() + goals CorporateGoal[] + @@index([month]) + @@map("corporate_roadmaps") +} + +model CorporateGoal { + id String @id @default(uuid()) @db.Uuid + roadmapId String @db.Uuid + roadmap CorporateRoadmap @relation(fields: [roadmapId], references: [id]) + title String + department String + acceptanceCriteria String + metric String + target Float? + createdAt DateTime @default(now()) @db.Timestamptz() + tasks CorporateTask[] + @@index([roadmapId]) + @@map("corporate_goals") +} + +model CorporateEffect { + id String @id @default(uuid()) @db.Uuid + taskId String @db.Uuid + task CorporateTask @relation(fields: [taskId], references: [id]) + metric String + availability String + baseline Float? + observed Float? + source String + observedAt DateTime @db.Timestamptz() + recordedBy String + createdAt DateTime @default(now()) @db.Timestamptz() + @@index([taskId]) + @@map("corporate_effects") +} + +model CorporateReport { + id String @id @default(uuid()) @db.Uuid + day String @unique + summary String + snapshot Json + createdBy String + createdAt DateTime @default(now()) @db.Timestamptz() + @@map("corporate_reports") +} + +model CorporateOperation { + key String @id + payloadHash String + response Json + createdAt DateTime @default(now()) @db.Timestamptz() + @@map("corporate_operations") +} diff --git a/backend/src/admin/admin.module.ts b/backend/src/admin/admin.module.ts index 66f749b0..6d67d12b 100644 --- a/backend/src/admin/admin.module.ts +++ b/backend/src/admin/admin.module.ts @@ -1,4 +1,6 @@ import { Module } from "@nestjs/common"; +import { CorporateController } from "./hermes/corporate.controller"; +import { CorporateService } from "./hermes/corporate.service"; import { AdminController } from "./admin.controller"; import { AdminService } from "./admin.service"; import { ReportsController } from "./reports.controller"; @@ -33,6 +35,7 @@ import { SupportChatAnalyticsService } from "./support-chat-analytics.service"; @Module({ imports: [PrismaModule, RedisModule, SettingsModule], controllers: [ + CorporateController, AdminController, ReportsController, MediaController, @@ -52,6 +55,7 @@ import { SupportChatAnalyticsService } from "./support-chat-analytics.service"; SupportChatAnalyticsController, ], providers: [ + CorporateService, AdminService, ReportsService, MediaService, diff --git a/backend/src/admin/hermes/corporate-policy.spec.ts b/backend/src/admin/hermes/corporate-policy.spec.ts new file mode 100644 index 00000000..f3fbbd2a --- /dev/null +++ b/backend/src/admin/hermes/corporate-policy.spec.ts @@ -0,0 +1,69 @@ +import { + normalizeSearch, + pagination, + taskInput, + monthlyPlanInput, +} from './corporate-policy'; +describe('corporate contract policy', () => { + it.each([ + ['2027-02', 28], + ['2028-02', 29], + ['2028-04', 30], + ['2028-01', 31], + ])('enforces real calendar coverage for %s', (month, count) => { + const id = '11111111-1111-4111-8111-111111111111'; + const body = { + roadmapId: id, + month, + needs: [], + dailyArticles: Array.from({ length: Number(count) }, (_, i) => ({ + title: 'new article', + description: 'instruction', + actionType: 'BLOG_NEW', + department: 'CONTENT_SEO', + goalId: id, + sourceIds: [id], + dueCycle: `${month}-${String(i + 1).padStart(2, '0')}`, + })), + }; + expect(monthlyPlanInput(body).dailyArticles).toHaveLength(Number(count)); + expect(() => + monthlyPlanInput({ ...body, dailyArticles: body.dailyArticles.slice(1) }), + ).toThrow(); + expect(() => + monthlyPlanInput({ + ...body, + dailyArticles: [...body.dailyArticles, body.dailyArticles[0]], + }), + ).toThrow(); + }); + it('normalizes Persian letters, digits and separators', () => { + expect(normalizeSearch('كيك ۱۲۳ (ة)‌')).toBe('کیک 123 ه'); + }); + it('bounds pagination and sort keys', () => { + expect(pagination({ limit: '100' }).limit).toBe(100); + expect(() => pagination({ limit: '101' })).toThrow(); + expect(() => pagination({ page: '-1' })).toThrow(); + expect(() => pagination({ sortBy: 'password' })).toThrow(); + }); + it('defaults CEO priority and rejects status/command injection', () => { + expect( + taskInput({ + title: 'عنوان', + description: 'کار', + department: 'CONTENT_SEO', + }).priority, + ).toBe('CEO_EXPEDITE'); + expect(() => + taskInput({ + title: 'x', + description: 'x', + department: 'CONTENT_SEO', + status: 'DONE', + }), + ).toThrow(); + expect(() => + taskInput({ title: '', description: 'x', department: 'CONTENT_SEO' }), + ).toThrow(); + }); +}); diff --git a/backend/src/admin/hermes/corporate-policy.ts b/backend/src/admin/hermes/corporate-policy.ts new file mode 100644 index 00000000..c9216c87 --- /dev/null +++ b/backend/src/admin/hermes/corporate-policy.ts @@ -0,0 +1,317 @@ +import { BadRequestException } from '@nestjs/common'; +export const departments = [ + 'CONTENT_SEO', + 'CATALOG_COMMERCE', + 'WIKI_SCIENTIFIC', + 'TECH_DEVOPS', + 'MARKET_INTELLIGENCE', +]; +export const priorities = ['CEO_EXPEDITE', 'CRITICAL', 'HIGH', 'MEDIUM', 'LOW']; +export const statuses = [ + 'TODO', + 'IN_PROGRESS', + 'TEST', + 'DONE', + 'BLOCKED', + 'REJECTED', + 'CANCELLED', +]; +export function object(value: unknown, keys: string[]): Record { + if ( + !value || + typeof value !== 'object' || + Array.isArray(value) || + Object.keys(value).some((k) => !keys.includes(k)) + ) + throw new BadRequestException('Invalid fields'); + return value as Record; +} +export function text(value: unknown, max = 10000): string { + if (typeof value !== 'string' || !value.trim() || value.length > max) + throw new BadRequestException('Invalid text'); + return value.trim(); +} +export function uuid(value: unknown): string { + const id = text(value, 36); + if ( + !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test( + id, + ) + ) + throw new BadRequestException('Invalid UUID'); + return id.toLowerCase(); +} +export function choice(value: unknown, values: string[]): string { + if (typeof value !== 'string' || !values.includes(value)) + throw new BadRequestException('Invalid enum'); + return value; +} +export function normalizeSearch(value: string): string { + return value + .normalize('NFKC') + .toLowerCase() + .replace(/[يى]/g, 'ی') + .replace(/ك/g, 'ک') + .replace(/ة/g, 'ه') + .replace(/[۰-۹٠-٩]/g, (c) => + String( + '۰۱۲۳۴۵۶۷۸۹'.includes(c) + ? '۰۱۲۳۴۵۶۷۸۹'.indexOf(c) + : '٠١٢٣٤٥٦٧٨٩'.indexOf(c), + ), + ) + .replace(/[\u200c\u200d()()]/g, ' ') + .replace(/[\u064b-\u065f]/g, '') + .replace(/\s+/g, ' ') + .trim(); +} +export function pagination(q: Record) { + const page = Number(q.page ?? 1), + limit = Number(q.limit ?? 20); + if ( + !Number.isSafeInteger(page) || + page < 1 || + page > 1000000 || + !Number.isInteger(limit) || + limit < 1 || + limit > 100 + ) + throw new BadRequestException('Invalid pagination'); + const sortBy = choice(q.sortBy ?? 'createdAt', [ + 'createdAt', + 'updatedAt', + 'title', + 'taskCode', + 'priority', + 'status', + ]); + const sortOrder = choice(q.sortOrder ?? 'desc', ['asc', 'desc']) as + | 'asc' + | 'desc'; + return { page, limit, skip: (page - 1) * limit, sortBy, sortOrder }; +} +export function meta(total: number, page: number, limit: number) { + const totalPages = Math.ceil(total / limit); + return { + total, + page, + limit, + totalPages, + hasNextPage: page < totalPages, + hasPrevPage: page > 1, + }; +} +export function taskInput(input: unknown) { + const b = object(input, [ + 'title', + 'department', + 'description', + 'priority', + 'parentId', + 'goalId', + 'actionType', + 'dueCycle', + ]); + return { + title: text(b.title, 300), + description: text(b.description), + department: choice(b.department, departments), + priority: choice(b.priority ?? 'CEO_EXPEDITE', priorities), + parentId: b.parentId === undefined ? undefined : uuid(b.parentId), + goalId: b.goalId === undefined ? undefined : uuid(b.goalId), + actionType: choice(b.actionType ?? 'CEO_DIRECTIVE', [ + 'CEO_DIRECTIVE', + 'BLOG_NEW', + 'BLOG_UPDATE', + 'CATALOG_ENRICH', + 'WIKI_ENRICH', + 'RESEARCH', + 'ENGINEERING', + ]), + dueCycle: b.dueCycle === undefined ? undefined : text(b.dueCycle, 40), + }; +} +export function planMonth(value: unknown): string { + const month = text(value, 7); + if (!/^(?:[2-9]\d{3})-(?:0[1-9]|1[0-2])$/.test(month)) + throw new BadRequestException('Invalid YYYY-MM month'); + return month; +} +export function monthlyPlanInput(input: unknown) { + const b = object(input, ['roadmapId', 'month', 'dailyArticles', 'needs']); + const month = planMonth(b.month); + const days = new Date( + Date.UTC(Number(month.slice(0, 4)), Number(month.slice(5)), 0), + ).getUTCDate(); + if ( + !Array.isArray(b.dailyArticles) || + b.dailyArticles.length !== days || + !Array.isArray(b.needs) || + b.needs.length > 200 + ) + throw new BadRequestException( + 'Full daily article coverage and bounded needs required', + ); + const mapping: Record = { + BLOG_NEW: 'CONTENT_SEO', + BLOG_UPDATE: 'CONTENT_SEO', + CATALOG_ENRICH: 'CATALOG_COMMERCE', + WIKI_ENRICH: 'WIKI_SCIENTIFIC', + ENGINEERING: 'TECH_DEVOPS', + }; + const parse = (raw: unknown, daily: boolean) => { + const t = object(raw, [ + 'title', + 'description', + 'department', + 'priority', + 'goalId', + 'actionType', + 'dueCycle', + 'sourceIds', + 'target', + 'reason', + ]); + const actionType = choice( + t.actionType, + daily + ? ['BLOG_NEW'] + : ['BLOG_UPDATE', 'CATALOG_ENRICH', 'WIKI_ENRICH', 'ENGINEERING'], + ); + const department = choice(t.department, [mapping[actionType]]); + const dueCycle = text(t.dueCycle, 10); + const date = new Date(dueCycle + 'T00:00:00Z'); + if ( + !/^\d{4}-\d{2}-\d{2}$/.test(dueCycle) || + !dueCycle.startsWith(month + '-') || + !Number.isFinite(date.getTime()) || + date.toISOString().slice(0, 10) !== dueCycle + ) + throw new BadRequestException('Invalid plan day'); + if ( + !Array.isArray(t.sourceIds) || + !t.sourceIds.length || + t.sourceIds.length > 30 + ) + throw new BadRequestException('Stored sources required'); + const sourceIds = t.sourceIds.map(uuid); + if (new Set(sourceIds).size !== sourceIds.length) + throw new BadRequestException('Duplicate source'); + if (daily && (t.target !== undefined || t.reason !== undefined)) + throw new BadRequestException('Daily new articles cannot be needs'); + return { + title: text(t.title, 300), + description: text(t.description), + department, + actionType, + dueCycle, + priority: choice(t.priority ?? 'MEDIUM', priorities), + goalId: uuid(t.goalId), + sourceIds, + ...(daily + ? {} + : { target: text(t.target, 2000), reason: text(t.reason) }), + }; + }; + const dailyArticles = b.dailyArticles.map((t) => parse(t, true)); + if (new Set(dailyArticles.map((t) => t.dueCycle)).size !== days) + throw new BadRequestException( + 'Exactly one new article per calendar day required', + ); + const needs = b.needs.map((t) => parse(t, false)); + if ( + new Set(needs.map((t) => canonical([t.actionType, t.target, t.dueCycle]))) + .size !== needs.length + ) + throw new BadRequestException('Duplicate need'); + return { roadmapId: uuid(b.roadmapId), month, dailyArticles, needs }; +} +export function canonical(value: any): string { + if (Array.isArray(value)) return '[' + value.map(canonical).join(',') + ']'; + if (value && typeof value === 'object') + return ( + '{' + + Object.keys(value) + .sort() + .map((k) => JSON.stringify(k) + ':' + canonical(value[k])) + .join(',') + + '}' + ); + return JSON.stringify(value); +} +/** Public projection must also cover nested evidence and child records. */ +export function publicData(value: any): any { + if (value instanceof Date) return value; + if (Array.isArray(value)) return value.map(publicData); + if (value && typeof value === 'object') + return Object.fromEntries( + Object.entries(value) + .filter(([key]) => key !== 'claimToken') + .map(([key, item]) => [key, publicData(item)]), + ); + return value; +} +export function httpsUrl(value: unknown): string { + const raw = text(value, 2000); + try { + if (!/^https:\/\/[a-z0-9]/i.test(raw) || /[\s\\]/.test(raw)) + throw new Error(); + const u = new URL(raw); + const host = u.hostname.toLowerCase(); + if ( + u.protocol !== 'https:' || + u.username || + u.password || + !/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$/.test(host) || + host === 'localhost' || + host.endsWith('.localhost') || + /(^|\.)example\.(com|org|net)$/.test(host) + ) + throw new Error(); + return raw; + } catch { + throw new BadRequestException('Valid HTTPS host URL required'); + } +} +export function timestamp(value: unknown, message: string): Date { + if ( + typeof value !== 'string' || + !/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?Z$/.test(value) + ) + throw new BadRequestException(message); + const date = new Date(value); + if ( + !Number.isFinite(date.getTime()) || + date.getTime() > Date.now() || + date.toISOString().slice(0, 19) !== value.slice(0, 19) + ) + throw new BadRequestException(message); + return date; +} +export function evidenceInput(input: unknown) { + const b = object(input, [ + 'claimToken', + 'artifactUri', + 'sha256', + 'summary', + 'tool', + 'result', + ]); + const uri = text(b.artifactUri, 2000); + + const sha = text(b.sha256, 64); + if (!/^[a-f0-9]{64}$/.test(sha)) + throw new BadRequestException('Invalid digest'); + if (uri.startsWith('urn:')) { + if (uri !== 'urn:sha256:' + sha) + throw new BadRequestException('Digest locator mismatch'); + } else httpsUrl(uri); + return { + claimToken: uuid(b.claimToken), + artifactUri: uri, + sha256: sha, + summary: text(b.summary), + tool: text(b.tool, 100), + result: text(b.result), + }; +} diff --git a/backend/src/admin/hermes/corporate.controller.ts b/backend/src/admin/hermes/corporate.controller.ts new file mode 100644 index 00000000..3a0c6ed6 --- /dev/null +++ b/backend/src/admin/hermes/corporate.controller.ts @@ -0,0 +1,138 @@ +import { + Body, + Controller, + Get, + Headers, + Param, + Post, + Query, + Req, + UnauthorizedException, + UseGuards, +} from '@nestjs/common'; +import { JwtAuthGuard } from '../../auth/jwt-auth.guard'; +import { RolesGuard } from '../../common/guards/roles.guard'; +import { Roles } from '../../common/decorators/roles.decorator'; +import { CorporateService } from './corporate.service'; +import { randomUUID } from 'crypto'; +import { text } from './corporate-policy'; + +@UseGuards(JwtAuthGuard, RolesGuard) +@Roles('Admin', 'SUPER_ADMIN') +@Controller('admin/hermes') +export class CorporateController { + constructor(private readonly service: CorporateService) {} + private actor(req: any) { + // API keys must use their authenticated key identity, not an editable worker label. + const user = req.user; + const id = user?.id ?? user?.sub; + if (!id) throw new UnauthorizedException('Authenticated subject required'); + return (user.isApiKey ? 'apikey:' : 'user:') + text(String(id), 200); + } + private key(key?: string) { + return text(key, 200); + } + @Get('runtime') runtime() { + return { + mode: 'MANUAL_DORMANT', + schedulesEnabled: false, + externalExecutionEnabled: false, + }; + } + @Get('tasks') tasks(@Query() query: Record) { + return this.service.tasks(query); + } + @Get('tasks/:id') detail(@Param('id') id: string) { + return this.service.detail(id); + } + @Post('tasks') create( + @Req() req: any, + @Headers('idempotency-key') key: string, + @Body() body: unknown, + ) { + return this.service.create(this.actor(req), key ?? randomUUID(), body); + } + @Post('claims') claim( + @Req() req: any, + @Headers('idempotency-key') key: string, + @Body() body: unknown, + ) { + return this.service.claim(this.actor(req), this.key(key), body); + } + @Post('tasks/:id/evidence') evidence( + @Req() req: any, + @Headers('idempotency-key') key: string, + @Param('id') id: string, + @Body() body: unknown, + ) { + return this.service.submitEvidence( + this.actor(req), + this.key(key), + id, + body, + ); + } + @Post('tasks/:id/reviews') review( + @Req() req: any, + @Headers('idempotency-key') key: string, + @Param('id') id: string, + @Body() body: unknown, + ) { + return this.service.review(this.actor(req), this.key(key), id, body); + } + @Post('tasks/:id/effects') effect( + @Req() req: any, + @Headers('idempotency-key') key: string, + @Param('id') id: string, + @Body() body: unknown, + ) { + return this.service.effect(this.actor(req), this.key(key), id, body); + } + @Get('market-insights') insights(@Query() q: Record) { + return this.service.collection('market-insights', q); + } + @Post('market-insights') research( + @Req() req: any, + @Headers('idempotency-key') key: string, + @Body() body: unknown, + ) { + return this.service.research(this.actor(req), this.key(key), body); + } + @Get('monthly-roadmap') roadmaps(@Query() q: Record) { + return this.service.collection('monthly-roadmap', q); + } + @Post('monthly-roadmap') roadmap( + @Req() req: any, + @Headers('idempotency-key') key: string, + @Body() body: unknown, + ) { + return this.service.roadmap(this.actor(req), this.key(key), body); + } + @Post('monthly-plans') persistMonthlyPlan( + @Req() req: any, + @Headers('idempotency-key') key: string, + @Body() body: unknown, + ) { + return this.service.persistMonthlyPlan( + this.actor(req), + this.key(key), + body, + ); + } + @Get('monthly-plans/:roadmapId') monthlyPlan( + @Param('roadmapId') roadmapId: string, + @Query('month') month: string, + ) { + return this.service.monthlyPlan(roadmapId, month); + } + @Get('reports') reports(@Query() q: Record) { + return this.service.collection('reports', q); + } + @Post('reports') report( + @Req() req: any, + @Headers('idempotency-key') key: string, + @Body() body: unknown, + ) { + return this.service.report(this.actor(req), this.key(key), body); + } +} diff --git a/backend/src/admin/hermes/corporate.integration.spec.ts b/backend/src/admin/hermes/corporate.integration.spec.ts new file mode 100644 index 00000000..bc91d022 --- /dev/null +++ b/backend/src/admin/hermes/corporate.integration.spec.ts @@ -0,0 +1,840 @@ +import passport from 'passport'; +import { Strategy, ExtractJwt } from 'passport-jwt'; +import { PrismaClient } from '@prisma/client'; +import { Test } from '@nestjs/testing'; +import { INestApplication, ValidationPipe } from '@nestjs/common'; +import { randomUUID } from 'crypto'; +import request from 'supertest'; +import { PrismaService } from '../../prisma/prisma.service'; +import { CorporateController } from './corporate.controller'; +import { CorporateService } from './corporate.service'; +import { ApiKeysService } from '../api-keys.service'; + +const integration = process.env.CORPORATE_TEST_DATABASE_URL + ? describe + : describe.skip; +integration('corporate API / disposable PostgreSQL', () => { + let app: INestApplication, db: PrismaService; + const key = () => randomUUID(); + const post = (path: string, body: any, actor = 'executor', op = key()) => + request(app.getHttpServer()) + .post('/admin/hermes/' + path) + .set('x-api-key', actor) + .set('Idempotency-Key', op) + .send(body); + const input = { + title: 'كيك ۱۲۳', + description: 'integration fixture', + department: 'CONTENT_SEO', + }; + beforeAll(async () => { + // Explicit disposable loopback opt-in only; never consumes runtime DATABASE_URL. + const url = new URL(process.env.CORPORATE_TEST_DATABASE_URL!); + if (url.hostname !== '127.0.0.1' || url.pathname !== '/corporate_test') + throw new Error('Disposable loopback corporate_test DB required'); + passport.use( + 'jwt', + new Strategy( + { + jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), + secretOrKey: 'test-only-not-runtime', + }, + (_payload, done) => done(null, false), + ), + ); + db = new PrismaClient({ + datasources: { db: { url: process.env.CORPORATE_TEST_DATABASE_URL } }, + }) as PrismaService; + const module = await Test.createTestingModule({ + controllers: [CorporateController], + providers: [ + CorporateService, + { provide: PrismaService, useValue: db }, + { + provide: ApiKeysService, + useValue: { + validateKey: async (id: string) => ({ + id, + isApiKey: true, + role: 'Admin', + scopes: id === 'unscoped' ? [] : ['admin'], + }), + }, + }, + ], + }).compile(); + app = module.createNestApplication(); + app.useGlobalPipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true })); + await app.listen(0, '127.0.0.1'); + }); + afterAll(async () => { + if (db) + await db.$executeRawUnsafe( + 'TRUNCATE corporate_operations, corporate_reports, corporate_effects, corporate_reviews, corporate_evidence, corporate_events, corporate_tasks, corporate_goals, corporate_roadmaps, corporate_sources, corporate_research CASCADE', + ); + await app?.close(); + await db?.$disconnect(); + passport.unuse('jwt'); + }); + beforeEach(async () => { + await db.$executeRawUnsafe( + 'TRUNCATE corporate_operations, corporate_reports, corporate_effects, corporate_reviews, corporate_evidence, corporate_events, corporate_tasks, corporate_goals, corporate_roadmaps, corporate_sources, corporate_research CASCADE', + ); + }); + async function planFixture(month = '2996-02') { + const research = ( + await post('market-insights', { + title: 'monthly fixture', + category: 'TREND', + source: 'disposable receipt', + opportunity: 'test planning', + impact: 'LOW', + recommendedAction: 'plan', + cadence: 'MONTHLY', + provenance: { + method: 'fixture', + collector: 'jest', + context: 'disposable', + }, + sources: [ + { + url: 'https://pubmed.ncbi.nlm.nih.gov/', + title: 'fixture', + excerpt: 'test only', + retrievedAt: '2026-01-01T00:00:00Z', + sha256: 'a'.repeat(64), + }, + ], + }).expect(201) + ).body; + const roadmap = ( + await post('monthly-roadmap', { + researchId: research.id, + month, + cycleName: 'monthly fixture', + seasonalTheme: 'test', + strategicGoals: ['coverage'], + plannedArticlesCount: 29, + plannedRefreshesCount: 0, + plannedCatalogEnrichments: 0, + goals: [ + 'CONTENT_SEO', + 'CATALOG_COMMERCE', + 'WIKI_SCIENTIFIC', + 'TECH_DEVOPS', + ].map((department) => ({ + title: department, + department, + acceptanceCriteria: 'independent QA', + metric: 'coverage', + })), + }).expect(201) + ).body; + const task = ( + actionType: string, + department: string, + dueCycle: string, + ) => ({ + title: actionType + ' ' + dueCycle, + description: 'Evidence-backed instruction requiring QA', + actionType, + department, + dueCycle, + goalId: roadmap.goals.find((g: any) => g.department === department).id, + sourceIds: [research.sources[0].id], + ...(actionType === 'BLOG_NEW' + ? {} + : { + target: 'https://canina.ir/identified-gap', + reason: 'Documented coverage gap', + }), + }); + return { + research, + roadmap, + body: { + roadmapId: roadmap.id, + month, + dailyArticles: Array.from({ length: 29 }, (_, i) => + task( + 'BLOG_NEW', + 'CONTENT_SEO', + `${month}-${String(i + 1).padStart(2, '0')}`, + ), + ), + needs: [ + task('BLOG_UPDATE', 'CONTENT_SEO', month + '-01'), + task('CATALOG_ENRICH', 'CATALOG_COMMERCE', month + '-02'), + task('WIKI_ENRICH', 'WIKI_SCIENTIFIC', month + '-03'), + task('ENGINEERING', 'TECH_DEVOPS', month + '-04'), + ], + }, + }; + } + it('persists a complete leap-month daily-new plan, separate needs and evidence lineage with readback', async () => { + const { body, research, roadmap } = await planFixture(); + body.roadmapId = body.roadmapId.toUpperCase(); + for (const entry of [...body.dailyArticles, ...body.needs]) { + entry.goalId = entry.goalId.toUpperCase(); + entry.sourceIds = entry.sourceIds.map((id: string) => id.toUpperCase()); + } + const op = key(); + const created = ( + await post('monthly-plans', body, 'planner', op).expect(201) + ).body; + expect(created.tasks).toHaveLength(33); + const need = created.tasks.find((t: any) => t.actionType === 'WIKI_ENRICH'); + expect(need.logs[0].payload).toMatchObject({ + target: body.needs[2].target, + reason: body.needs[2].reason, + }); + expect( + created.tasks.filter((t: any) => t.actionType === 'BLOG_NEW'), + ).toHaveLength(29); + expect(created.tasks.every((t: any) => t.status === 'TODO')).toBe(true); + expect( + (await post('monthly-plans', body, 'planner', op).expect(201)).body, + ).toEqual(created); + const read = await request(app.getHttpServer()) + .get(`/admin/hermes/monthly-plans/${roadmap.id.toUpperCase()}?month=${body.month}`) + .set('x-api-key', 'executor') + .expect(200); + expect(read.body).toEqual(created); + const detail = await request(app.getHttpServer()) + .get('/admin/hermes/tasks/' + created.tasks[0].id) + .set('x-api-key', 'executor') + .expect(200); + expect(detail.body.logs[0].step).toBe('PLANNED'); + expect(detail.body.logs[0].payload).toMatchObject({ + researchId: research.id, + roadmapId: roadmap.id, + month: body.month, + dueCycle: body.month + '-01', + sourceIds: [research.sources[0].id], + }); + expect(await db.corporateOperation.count()).toBe(3); + expect((await post('claims', {}).expect(201)).body.task).toBeNull(); + }); + it('validates calendar, action separation, needs and stored lineage without partial writes', async () => { + const { body } = await planFixture(); + const mutations: Array<[number, (b: any) => void]> = [ + [400, (b) => b.dailyArticles.pop()], + [400, (b) => (b.dailyArticles[1].dueCycle = b.dailyArticles[0].dueCycle)], + [400, (b) => (b.dailyArticles[0].dueCycle = '2028-02-30')], + [400, (b) => (b.month = '2028-13')], + [400, (b) => (b.dailyArticles[0].actionType = 'BLOG_UPDATE')], + [400, (b) => (b.needs[0].actionType = 'BLOG_NEW')], + [400, (b) => (b.needs[0].reason = ' ')], + [400, (b) => delete b.needs[0].target], + [400, (b) => (b.needs[0].department = 'WIKI_SCIENTIFIC')], + [400, (b) => (b.dailyArticles[0].sourceIds = [])], + [409, (b) => (b.dailyArticles[0].sourceIds = [key()])], + [409, (b) => (b.dailyArticles[0].goalId = key())], + [409, (b) => (b.needs[0].goalId = b.needs[1].goalId)], + [409, (b) => (b.roadmapId = key())], + ]; + for (const [status, mutate] of mutations) { + const invalid = JSON.parse(JSON.stringify(body)); + mutate(invalid); + await post('monthly-plans', invalid).expect(status); + expect(await db.corporateTask.count()).toBe(0); + expect(await db.corporateEvent.count()).toBe(0); + expect(await db.corporateOperation.count()).toBe(2); + } + }); + it('serializes competing actors and keys on roadmap/month, rejects changed replay and keeps auth dormant', async () => { + const { body } = await planFixture(); + await request(app.getHttpServer()) + .post('/admin/hermes/monthly-plans') + .send(body) + .expect(401); + await post('monthly-plans', body, 'unscoped').expect(403); + const op = key(); + const otherOp = key(); + let release!: () => void; + let ready!: () => void; + const locked = new Promise((r) => (ready = r)); + const hold = new Promise((r) => (release = r)); + const scope = JSON.stringify(['monthly-plan', body.roadmapId, body.month]); + const blocker = db.$transaction(async (tx) => { + await tx.$executeRaw`SELECT pg_advisory_xact_lock(hashtextextended(${scope}, 0))`; + ready(); + await hold; + }); + await locked; + const pending = [ + post('monthly-plans', body, 'planner', op).then((r) => r), + post('monthly-plans', body, 'other', otherOp).then((r) => r), + ]; + try { + let waiters = 0; + for (let i = 0; i < 100; i++) { + const rows = await db.$queryRaw`SELECT pid FROM pg_stat_activity + WHERE datname=current_database() AND wait_event_type='Lock' AND query LIKE '%pg_advisory_xact_lock%'`; + waiters = rows.length; + if (waiters >= 2) break; + await new Promise((r) => setTimeout(r, 10)); + } + expect(waiters).toBeGreaterThanOrEqual(2); + } finally { + release(); + await blocker; + } + const results = await Promise.all(pending); + expect(results.map((r) => r.status).sort()).toEqual([201, 409]); + expect(await db.corporateTask.count()).toBe(33); + expect(await db.corporateEvent.count({ where: { step: 'PLANNED' } })).toBe( + 33, + ); + const winner = results[0].status === 201 ? 'planner' : 'other'; + const winningOp = winner === 'planner' ? op : otherOp; + await post( + 'monthly-plans', + { ...body, needs: [] }, + winner, + winningOp, + ).expect(409); + expect( + (await post('monthly-plans', body, winner, winningOp).expect(201)).body, + ).toEqual(results[winner === 'planner' ? 0 : 1].body); + const runtime = await request(app.getHttpServer()) + .get('/admin/hermes/runtime') + .set('x-api-key', 'executor') + .expect(200); + expect(runtime.body).toEqual({ + mode: 'MANUAL_DORMANT', + schedulesEnabled: false, + externalExecutionEnabled: false, + }); + }); + it('rolls back a mid-plan database failure including tasks, events and idempotency receipt', async () => { + const { body } = await planFixture(); + // Disposable-only trigger fails after the first task/event have been written. + await db.$executeRawUnsafe( + `CREATE FUNCTION reject_plan_event() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.step='PLANNED' AND NEW.payload->>'dueCycle'='2996-02-02' THEN RAISE EXCEPTION 'injected plan failure'; END IF; RETURN NEW; END $$`, + ); + await db.$executeRawUnsafe( + 'CREATE TRIGGER reject_plan_event BEFORE INSERT ON corporate_events FOR EACH ROW EXECUTE FUNCTION reject_plan_event()', + ); + const op = key(); + try { + await post('monthly-plans', body, 'planner', op).expect(500); + expect(await db.corporateTask.count()).toBe(0); + expect(await db.corporateEvent.count()).toBe(0); + expect(await db.corporateOperation.count()).toBe(2); + } finally { + await db.$executeRawUnsafe( + 'DROP TRIGGER reject_plan_event ON corporate_events', + ); + await db.$executeRawUnsafe('DROP FUNCTION reject_plan_event()'); + } + await post('monthly-plans', body, 'planner', op).expect(201); + }); + it('gates only PLANNED future tasks while legacy future dueCycle remains claimable and UTC today is eligible', async () => { + const { body } = await planFixture(); + await post('monthly-plans', body).expect(201); + const legacy = ( + await post('tasks', { ...input, dueCycle: '2999-12-31' }).expect(201) + ).body; + expect((await post('claims', {}).expect(201)).body.task.id).toBe(legacy.id); + expect((await post('claims', {}).expect(201)).body.task).toBeNull(); + // Model the persisted event's due-day boundary in the disposable DB without changing system time. + const planned = await db.corporateTask.findFirstOrThrow({ + where: { actionType: 'BLOG_NEW' }, + }); + const day = new Date().toISOString().slice(0, 10); + await db.corporateTask.update({ + where: { id: planned.id }, + data: { dueCycle: day }, + }); + expect((await post('claims', {}).expect(201)).body.task).toBeNull(); // mismatch fails closed + const event = await db.corporateEvent.findFirstOrThrow({ + where: { taskId: planned.id, step: 'PLANNED' }, + }); + await db.corporateEvent.update({ + where: { id: event.id }, + data: { payload: { ...(event.payload as object), dueCycle: day } }, + }); + expect((await post('claims', {}).expect(201)).body.task.id).toBe( + planned.id, + ); + }); + it('rejects children after a parent claim commits while child creation waits on its row lock', async () => { + const parent = (await post('tasks', input).expect(201)).body; + let release!: () => void, locked!: () => void; + const ready = new Promise((r) => (locked = r)); + const hold = new Promise((r) => (release = r)); + const claiming = db.$transaction(async (tx) => { + await tx.$queryRaw`SELECT id FROM corporate_tasks WHERE id=${parent.id}::uuid FOR UPDATE`; + await tx.corporateTask.update({ + where: { id: parent.id }, + data: { + status: 'IN_PROGRESS', + claimToken: key(), + assignedAgent: 'executor', + leasedUntil: new Date(Date.now() + 60000), + }, + }); + locked(); + await hold; + }); + await ready; + const pending = post('tasks', { ...input, parentId: parent.id }).then( + (r) => r, + ); + try { + // Observe actual competing PostgreSQL lock wait, not a timing-only race. + let waiting = false; + for (let i = 0; i < 100; i++) { + const rows = await db.$queryRaw< + any[] + >`SELECT pid FROM pg_stat_activity WHERE datname=current_database() AND wait_event_type='Lock' AND query LIKE '%corporate_tasks%'`; + if (rows.length) { + waiting = true; + break; + } + await new Promise((r) => setTimeout(r, 10)); + } + expect(waiting).toBe(true); + } finally { + release(); + await claiming; + } + expect((await pending).status).toBe(409); + expect( + await db.corporateTask.count({ where: { parentId: parent.id } }), + ).toBe(0); + }); + it('accepts children on TODO and BLOCKED, which prevents parent claiming', async () => { + for (const status of ['TODO', 'BLOCKED'] as const) { + const parent = (await post('tasks', input).expect(201)).body; + await db.corporateTask.update({ + where: { id: parent.id }, + data: { status }, + }); + const child = ( + await post('tasks', { ...input, parentId: parent.id }).expect(201) + ).body; + expect((await post('claims', {}).expect(201)).body.task.id).toBe( + child.id, + ); + } + }); + it('rejects malformed artifact locators and mismatched digest URNs before persistence', async () => { + const task = (await post('tasks', input).expect(201)).body; + const claim = (await post('claims', {}).expect(201)).body.task; + const body = { + claimToken: claim.claimToken, + artifactUri: '', + sha256: 'a'.repeat(64), + summary: 'receipt', + tool: 'jest', + result: 'passed', + }; + for (const artifactUri of [ + 'https://', + 'https://?x', + 'https:///pubmed.ncbi.nlm.nih.gov/x', + 'https://pubmed.ncbi.nlm.nih.gov./x', + 'https://bad host/x', + 'https://user:pass@host.org/a', + 'https://localhost/a', + 'https://127.1/a', + 'https://[::1]/a', + 'urn:sha256:', + 'urn:sha256:' + 'b'.repeat(64), + ]) { + await post(`tasks/${task.id}/evidence`, { ...body, artifactUri }).expect( + 400, + ); + } + expect(await db.corporateEvidence.count()).toBe(0); + await post(`tasks/${task.id}/evidence`, { + ...body, + artifactUri: 'https://pubmed.ncbi.nlm.nih.gov/123/', + }).expect(201); + }); + it('rejects implicit timestamp coercions in source and effect inputs', async () => { + const service = app.get(CorporateService); + const research = { + title: 'research', + category: 'TREND', + source: 'receipt', + opportunity: 'investigate', + impact: 'LOW', + recommendedAction: 'measure', + cadence: 'MONTHLY', + provenance: { method: 'test', collector: 'fixture', context: 'isolated' }, + sources: [ + { + url: 'https://pubmed.ncbi.nlm.nih.gov/', + title: 'source', + excerpt: 'receipt', + retrievedAt: '', + sha256: 'a'.repeat(64), + }, + ], + }; + for (const time of [ + null, + 0, + false, + [], + '2026-01-01', + '2026-02-30T00:00:00Z', + ]) { + await expect( + Promise.resolve().then(() => + service.research('fixture', key(), { + ...research, + sources: [{ ...research.sources[0], retrievedAt: time }], + }), + ), + ).rejects.toThrow(); + await expect( + Promise.resolve().then(() => + service.effect('fixture', key(), key(), { + metric: 'views', + availability: 'UNAVAILABLE', + source: 'none', + observedAt: time, + }), + ), + ).rejects.toThrow('Invalid observation time'); + } + for (const url of ['https://', 'https://?bad', 'https://bad host']) + await expect( + Promise.resolve().then(() => + service.research('fixture', key(), { + ...research, + sources: [ + { + ...research.sources[0], + retrievedAt: '2026-01-01T00:00:00Z', + url, + }, + ], + }), + ), + ).rejects.toThrow(); + expect(await db.corporateResearch.count()).toBe(0); + }); + it('public reads strip claim tokens recursively while claim and replay retain ownership token', async () => { + const parent = (await post('tasks', input).expect(201)).body; + const child = ( + await post('tasks', { ...input, parentId: parent.id }).expect(201) + ).body; + const op = key(); + const claim = (await post('claims', {}, 'executor', op).expect(201)).body + .task; + expect(claim.claimToken).toBeTruthy(); + expect( + (await post('claims', {}, 'executor', op).expect(201)).body.task + .claimToken, + ).toBe(claim.claimToken); + await post(`tasks/${child.id}/evidence`, { + claimToken: claim.claimToken, + artifactUri: 'urn:sha256:' + 'a'.repeat(64), + sha256: 'a'.repeat(64), + summary: 'receipt', + tool: 'jest', + result: 'passed', + }).expect(201); + for (const path of ['tasks', 'tasks/' + child.id, 'tasks/' + parent.id]) { + const r = await request(app.getHttpServer()) + .get('/admin/hermes/' + path) + .set('x-api-key', 'competitor') + .expect(200); + expect(JSON.stringify(r.body)).not.toContain('claimToken'); + expect(JSON.stringify(r.body)).not.toContain(claim.claimToken); + } + }); + it('bounds detail relations and report snapshots with explicit truncation and immutable replay', async () => { + const task = (await post('tasks', input).expect(201)).body; + await db.corporateEvent.createMany({ + data: Array.from({ length: 105 }, () => ({ + taskId: task.id, + speaker: 'SYSTEM', + speakerName: 'fixture', + step: 'FIXTURE', + content: 'x'.repeat(10000), + payload: { nested: ['x'.repeat(10000)] }, + })), + }); + const detail = await request(app.getHttpServer()) + .get('/admin/hermes/tasks/' + task.id) + .set('x-api-key', 'executor') + .expect(200); + expect(detail.body.logs).toHaveLength(100); + expect(detail.body.relationsMeta.logs).toEqual({ + total: 106, + returned: 100, + limit: 100, + truncated: true, + }); + const op = key(), + body = { day: new Date().toISOString().slice(0, 10), summary: 'bounded' }; + const report = (await post('reports', body, 'executor', op).expect(201)) + .body; + expect(report.snapshot.events).toHaveLength(100); + expect(report.snapshot.meta.events).toEqual({ + total: 106, + returned: 100, + limit: 100, + truncated: true, + }); + expect(report.snapshot.events.every((e: any) => !('payload' in e))).toBe( + true, + ); + expect(report.snapshot.meta.fieldsTruncated).toBe(true); + expect( + Buffer.byteLength(JSON.stringify(report.body ?? report)), + ).toBeLessThan(1000000); + await db.corporateEvent.deleteMany(); + expect( + (await post('reports', body, 'executor', op).expect(201)).body, + ).toEqual(report); + }); + it('rolls back oversized mutation responses instead of persisting huge idempotency values', async () => { + const service = app.get(CorporateService) as any; + const before = await db.corporateTask.count(); + await expect( + service.operation('fixture', 'oversize', key(), {}, async (tx: any) => { + await tx.corporateTask.create({ + data: { + title: 'rollback', + description: 'fixture', + taskCode: key(), + normalizedTitle: 'rollback', + normalizedCode: key(), + department: 'CONTENT_SEO', + createdBy: 'fixture', + }, + }); + return { value: 'x'.repeat(1100000) }; + }), + ).rejects.toThrow('Operation response exceeds limit'); + expect(await db.corporateTask.count()).toBe(before); + expect(await db.corporateOperation.count()).toBe(0); + }); + it('auth, UI list contract, normalized search, immutable idempotent replay', async () => { + await request(app.getHttpServer()).get('/admin/hermes/tasks').expect(401); + await post('tasks', input, 'unscoped').expect(403); + const op = key(), + a = await post('tasks', input, 'executor', op).expect(201); + const b = await post('tasks', input, 'executor', op).expect(201); + expect(b.body.id).toBe(a.body.id); + await post('tasks', { ...input, title: 'changed' }, 'executor', op).expect( + 409, + ); + const list = await request(app.getHttpServer()) + .get('/admin/hermes/tasks?search=کیک%20123&limit=1') + .set('x-api-key', 'executor') + .expect(200); + expect(list.body.meta).toEqual({ + total: 1, + page: 1, + limit: 1, + totalPages: 1, + hasNextPage: false, + hasPrevPage: false, + }); + expect(list.body.data[0].priority).toBe('CEO_EXPEDITE'); + await request(app.getHttpServer()) + .get('/admin/hermes/tasks?limit=101') + .set('x-api-key', 'executor') + .expect(400); + await request(app.getHttpServer()) + .get('/admin/hermes/tasks?sortBy=password') + .set('x-api-key', 'executor') + .expect(400); + }); + it('claims exclusively, expires/reclaims, fences stale executor, requires independent QA', async () => { + const task = (await post('tasks', input).expect(201)).body; + const claims = await Promise.all([ + post('claims', {}, 'executor'), + post('claims', {}, 'competitor'), + ]); + expect(claims.filter((x) => x.body.task !== null)).toHaveLength(1); + const claimed = claims.find((x) => x.body.task !== null)!.body.task; + const oldActor = claimed.assignedAgent.replace('apikey:', ''); + await db.$executeRaw`UPDATE corporate_tasks SET "leasedUntil"=clock_timestamp()-interval '1 second' WHERE id=${task.id}::uuid`; + const renewed = (await post('claims', {}, 'new-executor').expect(201)).body + .task; + expect(renewed.claimToken).not.toBe(claimed.claimToken); + const evidence = { + claimToken: claimed.claimToken, + artifactUri: 'urn:sha256:' + 'a'.repeat(64), + sha256: 'a'.repeat(64), + summary: 'receipt', + tool: 'jest', + result: 'passed', + }; + await post(`tasks/${task.id}/evidence`, evidence, oldActor).expect(409); + const e = ( + await post( + `tasks/${task.id}/evidence`, + { ...evidence, claimToken: renewed.claimToken }, + 'new-executor', + ).expect(201) + ).body; + const review = { + evidenceId: e.id, + passed: true, + findings: 'verified', + verification: { + artifactUri: e.artifactUri, + sha256: e.sha256, + method: 'digest readback', + result: 'match', + }, + }; + await post(`tasks/${task.id}/reviews`, review, 'new-executor').expect(409); + await post(`tasks/${task.id}/reviews`, review, 'reviewer').expect(201); + const detail = await request(app.getHttpServer()) + .get('/admin/hermes/tasks/' + task.id) + .set('x-api-key', 'reviewer') + .expect(200); + expect(detail.body.status).toBe('DONE'); + expect(detail.body.logs.map((x: any) => x.step)).toContain('QA_PASSED'); + await post(`tasks/${task.id}/effects`, { + metric: 'engagement', + availability: 'UNAVAILABLE', + observed: 0, + source: 'provider unavailable', + observedAt: new Date().toISOString(), + }).expect(400); + }); + it('skips a held row lock and gives CEO priority before lower priorities', async () => { + const urgent = (await post('tasks', input).expect(201)).body; + const low = (await post('tasks', { ...input, priority: 'LOW' }).expect(201)) + .body; + let release!: () => void, locked!: () => void; + const ready = new Promise((r) => (locked = r)), + hold = new Promise((r) => (release = r)); + const locking = db.$transaction(async (tx) => { + await tx.$queryRaw`SELECT id FROM corporate_tasks WHERE id=${urgent.id}::uuid FOR UPDATE`; + locked(); + await hold; + }); + await ready; + try { + const claim = (await post('claims', {}).expect(201)).body.task; + expect(claim.id).toBe(low.id); + } finally { + release(); + await locking; + } + const next = (await post('claims', {}, 'other').expect(201)).body.task; + expect(next.id).toBe(urgent.id); + }); + it('serializes simultaneous duplicate mutations into one durable record', async () => { + const op = key(); + const responses = await Promise.all([ + post('tasks', input, 'executor', op), + post('tasks', input, 'executor', op), + ]); + expect(responses.map((r) => r.status)).toEqual([201, 201]); + expect(responses[0].body.id).toBe(responses[1].body.id); + expect(await db.corporateTask.count()).toBe(1); + }); + it('gates parents on children, persists provenance, roadmap/goals and reports', async () => { + const parent = (await post('tasks', input).expect(201)).body; + const child = ( + await post('tasks', { ...input, parentId: parent.id }).expect(201) + ).body; + const claim = (await post('claims', {}).expect(201)).body.task; + expect(claim.id).toBe(child.id); + const e = ( + await post(`tasks/${child.id}/evidence`, { + claimToken: claim.claimToken, + artifactUri: 'urn:sha256:' + 'b'.repeat(64), + sha256: 'b'.repeat(64), + summary: 'child artifact', + tool: 'jest', + result: 'receipt', + }).expect(201) + ).body; + await post( + `tasks/${child.id}/reviews`, + { + evidenceId: e.id, + passed: false, + findings: 'needs correction', + verification: { + artifactUri: e.artifactUri, + sha256: e.sha256, + method: 'readback', + result: 'failed', + }, + }, + 'reviewer', + ).expect(201); + expect( + (await db.corporateTask.findUniqueOrThrow({ where: { id: parent.id } })) + .status, + ).toBe('BLOCKED'); + expect((await post('claims', {}).expect(201)).body.task.id).toBe(child.id); + const research = ( + await post('market-insights', { + title: 'fixture research', + category: 'TREND', + source: 'study receipt', + opportunity: 'investigate', + impact: 'LOW', + recommendedAction: 'measure', + cadence: 'MONTHLY', + provenance: { + method: 'test fixture', + collector: 'integration', + context: 'disposable', + }, + sources: [ + { + url: 'https://pubmed.ncbi.nlm.nih.gov/', + title: 'fixture', + excerpt: 'test-only', + retrievedAt: '2026-01-01T00:00:00Z', + sha256: 'a'.repeat(64), + }, + ], + }).expect(201) + ).body; + const roadmap = ( + await post('monthly-roadmap', { + researchId: research.id, + cycleName: 'test', + month: '2026-10', + seasonalTheme: 'evidence pending seasonal analysis', + strategicGoals: ['measure'], + plannedArticlesCount: 31, + plannedRefreshesCount: 0, + plannedCatalogEnrichments: 0, + goals: [ + { + title: 'goal', + department: 'CONTENT_SEO', + acceptanceCriteria: 'QA', + metric: 'engagement', + }, + ], + }).expect(201) + ).body; + expect(roadmap.goals).toHaveLength(1); + await post('tasks', { ...input, goalId: roadmap.goals[0].id }).expect(201); + for (const path of ['market-insights', 'monthly-roadmap']) { + const r = await request(app.getHttpServer()) + .get('/admin/hermes/' + path) + .set('x-api-key', 'executor') + .expect(200); + expect(r.body.meta.total).toBe(1); + } + const report = ( + await post('reports', { + day: new Date().toISOString().slice(0, 10), + summary: 'test fixture daily report', + }).expect(201) + ).body; + expect(report.snapshot.tasks).toHaveLength(3); + }); +}); diff --git a/backend/src/admin/hermes/corporate.service.ts b/backend/src/admin/hermes/corporate.service.ts new file mode 100644 index 00000000..7c9a238f --- /dev/null +++ b/backend/src/admin/hermes/corporate.service.ts @@ -0,0 +1,828 @@ +import { + Injectable, + BadRequestException, + ConflictException, + NotFoundException, +} from '@nestjs/common'; +import { Prisma, CorporatePriority } from '@prisma/client'; +import { createHash, randomUUID } from 'crypto'; +import { PrismaService } from '../../prisma/prisma.service'; +import { + canonical, + monthlyPlanInput, + planMonth, + publicData, + choice, + departments, + evidenceInput, + httpsUrl, + timestamp, + meta, + normalizeSearch, + object, + pagination, + priorities, + statuses, + taskInput, + text, + uuid, +} from './corporate-policy'; +type Tx = Prisma.TransactionClient; +@Injectable() +export class CorporateService { + constructor(private readonly db: PrismaService) {} + private async operation( + actor: string, + op: string, + key: string, + body: unknown, + fn: (tx: Tx) => Promise, + ) { + text(key, 200); + const scoped = createHash('sha256') + .update(canonical([actor, op, key])) + .digest('hex'); + const hash = createHash('sha256').update(canonical(body)).digest('hex'); + return this.db.$transaction( + async (tx) => { + await tx.$executeRaw`SELECT pg_advisory_xact_lock(hashtextextended(${scoped}, 0))`; + const prior = await tx.corporateOperation.findUnique({ + where: { key: scoped }, + }); + if (prior) { + if (prior.payloadHash !== hash) + throw new ConflictException('Idempotency key payload mismatch'); + return prior.response; + } + const result = await fn(tx); + const serialized = JSON.stringify(result); + if (Buffer.byteLength(serialized, 'utf8') > 1000000) + throw new BadRequestException('Operation response exceeds limit'); + const response = JSON.parse(serialized); + await tx.corporateOperation.create({ + data: { key: scoped, payloadHash: hash, response }, + }); + return response; + }, + { maxWait: 10000, timeout: 20000 }, + ); + } + private event( + tx: Tx, + taskId: string, + actor: string, + step: string, + content: string, + speaker = 'SYSTEM', + payload?: any, + ) { + return tx.corporateEvent.create({ + data: { taskId, speaker, speakerName: actor, step, content, payload }, + }); + } + private async lock(tx: Tx, id: string) { + uuid(id); + const rows = await tx.$queryRaw< + { id: string }[] + >`SELECT id FROM corporate_tasks WHERE id=${id}::uuid FOR UPDATE`; + if (!rows.length) throw new NotFoundException('Task not found'); + return tx.corporateTask.findUniqueOrThrow({ where: { id } }); + } + private async fenced(tx: Tx, id: string, token: string, actor: string) { + const t = await this.lock(tx, id); + const rows = await tx.$queryRaw< + { id: string }[] + >`SELECT id FROM corporate_tasks WHERE id=${id}::uuid AND "claimToken"=${token}::uuid AND "assignedAgent"=${actor} AND "leasedUntil">clock_timestamp() AND status='IN_PROGRESS'`; + if (!rows.length) throw new ConflictException('Stale lease or owner'); + return t; + } + async tasks(q: Record) { + const p = pagination(q), + where: Prisma.CorporateTaskWhereInput = {}; + if (q.department) where.department = choice(q.department, departments); + if (q.priority) + where.priority = choice(q.priority, priorities) as CorporatePriority; + if (q.status) where.status = choice(q.status, statuses) as any; + if (q.search) { + const search = normalizeSearch(text(q.search, 300)); + where.OR = [ + { normalizedTitle: { contains: search } }, + { normalizedCode: { contains: search } }, + ]; + } + if (q.goalId) where.goalId = uuid(q.goalId); + return this.db.$transaction( + async (tx) => { + const total = await tx.corporateTask.count({ where }); + const data = await tx.corporateTask.findMany({ + where, + skip: p.skip, + take: p.limit, + orderBy: [{ [p.sortBy]: p.sortOrder }, { id: 'asc' }], + }); + return publicData({ data, meta: meta(total, p.page, p.limit) }); + }, + { isolationLevel: Prisma.TransactionIsolationLevel.RepeatableRead }, + ); + } + async detail(id: string) { + uuid(id); + return this.db.$transaction( + async (tx) => { + const order = { id: 'asc' as const }; + const task = await tx.corporateTask.findUnique({ + where: { id }, + include: { + logs: { take: 100, orderBy: [{ timestamp: 'asc' }, order] }, + evidence: { take: 100, orderBy: order }, + reviews: { take: 100, orderBy: order }, + effects: { take: 100, orderBy: order }, + children: { take: 100, orderBy: order }, + _count: { + select: { + logs: true, + evidence: true, + reviews: true, + effects: true, + children: true, + }, + }, + }, + }); + if (!task) throw new NotFoundException('Task not found'); + const { _count, ...data } = task; + const relationsMeta = Object.fromEntries( + Object.entries(_count).map(([name, total]) => [ + name, + { + total, + returned: Math.min(total, 100), + limit: 100, + truncated: total > 100, + }, + ]), + ); + return publicData({ ...data, relationsMeta }); + }, + { isolationLevel: Prisma.TransactionIsolationLevel.RepeatableRead }, + ); + } + create(actor: string, key: string, input: unknown) { + const b = taskInput(input); + return this.operation(actor, 'create-task', key, b, async (tx) => { + if (b.parentId) { + const parent = await this.lock(tx, b.parentId); + if (!['TODO', 'BLOCKED'].includes(parent.status)) + throw new ConflictException('Parent no longer accepts children'); + } + const code = 'TSK-' + randomUUID(); + const task = await tx.corporateTask.create({ + data: { + ...b, + priority: b.priority as CorporatePriority, + taskCode: code, + normalizedCode: normalizeSearch(code), + normalizedTitle: normalizeSearch(b.title), + createdBy: actor, + }, + }); + await this.event(tx, task.id, actor, 'CREATED', b.description, 'CEO'); + return task; + }); + } + private plannedWhere( + roadmapId: string, + month: string, + ): Prisma.CorporateEventWhereInput { + return { + step: 'PLANNED', + AND: [ + { payload: { path: ['roadmapId'], equals: roadmapId } }, + { payload: { path: ['month'], equals: month } }, + ], + }; + } + private async readPlan(tx: Tx, roadmapId: string, month: string) { + const roadmap = await tx.corporateRoadmap.findUnique({ + where: { id: roadmapId }, + }); + const tasks = await tx.corporateTask.findMany({ + where: { logs: { some: this.plannedWhere(roadmapId, month) } }, + include: { + logs: { + where: this.plannedWhere(roadmapId, month), + orderBy: { id: 'asc' }, + }, + }, + orderBy: [{ dueCycle: 'asc' }, { actionType: 'asc' }, { id: 'asc' }], + }); + if (!roadmap || !tasks.length) + throw new NotFoundException('Monthly plan not found'); + return publicData({ + roadmapId, + month, + researchId: roadmap.researchId, + tasks, + }); + } + monthlyPlan(roadmapId: string, month: unknown) { + roadmapId = uuid(roadmapId); + const cycle = planMonth(month); + return this.db.$transaction((tx) => this.readPlan(tx, roadmapId, cycle), { + isolationLevel: Prisma.TransactionIsolationLevel.RepeatableRead, + }); + } + persistMonthlyPlan(actor: string, key: string, input: unknown) { + const b = monthlyPlanInput(input); + return this.operation(actor, 'monthly-plan', key, b, async (tx) => { + // Separate actor/key-independent fence: one complete plan per roadmap/month. + const scope = canonical(['monthly-plan', b.roadmapId, b.month]); + await tx.$executeRaw`SELECT pg_advisory_xact_lock(hashtextextended(${scope}, 0))`; + if ( + await tx.corporateEvent.count({ + where: this.plannedWhere(b.roadmapId, b.month), + }) + ) + throw new ConflictException('Monthly plan already persisted'); + const roadmap = await tx.corporateRoadmap.findUnique({ + where: { id: b.roadmapId }, + include: { goals: true, research: { include: { sources: true } } }, + }); + if ( + !roadmap || + roadmap.month !== b.month || + !roadmap.research.sources.length + ) + throw new ConflictException( + 'Matching evidence-backed roadmap required', + ); + const entries = [...b.dailyArticles, ...b.needs]; + for (const entry of entries) { + if ( + !roadmap.goals.some( + (g) => g.id === entry.goalId && g.department === entry.department, + ) || + !entry.sourceIds.every((id) => + roadmap.research.sources.some((s) => s.id === id), + ) + ) + throw new ConflictException( + 'Goal/department and source lineage must match roadmap research', + ); + } + for (const entry of entries) { + const { sourceIds, target, reason, ...data } = entry; + const taskCode = 'TSK-' + randomUUID(); + const task = await tx.corporateTask.create({ + data: { + ...data, + priority: data.priority as CorporatePriority, + status: 'TODO', + taskCode, + normalizedCode: normalizeSearch(taskCode), + normalizedTitle: normalizeSearch(data.title), + createdBy: actor, + }, + }); + await this.event( + tx, + task.id, + actor, + 'PLANNED', + entry.description, + 'SYSTEM', + { + version: 1, + roadmapId: roadmap.id, + researchId: roadmap.researchId, + month: b.month, + goalId: entry.goalId, + department: entry.department, + actionType: entry.actionType, + dueCycle: entry.dueCycle, + sourceIds, + ...(target ? { target, reason } : {}), + }, + ); + } + return this.readPlan(tx, b.roadmapId, b.month); + }); + } + claim(actor: string, key: string, input: unknown) { + const b = object(input, ['department', 'leaseSeconds']); + const department = + b.department === undefined ? null : choice(b.department, departments); + const seconds = Number(b.leaseSeconds ?? 300); + if (!Number.isInteger(seconds) || seconds < 30 || seconds > 900) + throw new BadRequestException('Lease must be 30..900 seconds'); + return this.operation( + actor, + 'claim', + key, + { department, seconds }, + async (tx) => { + const candidates = await tx.$queryRaw< + { id: string }[] + >`SELECT t.id FROM corporate_tasks t + WHERE (t.status='TODO' OR (t.status='BLOCKED' AND EXISTS (SELECT 1 FROM corporate_tasks c WHERE c."parentId"=t.id)) OR (t.status='IN_PROGRESS' AND t."leasedUntil"<=clock_timestamp())) + AND NOT EXISTS (SELECT 1 FROM corporate_events e WHERE e."taskId"=t.id AND e.step='PLANNED' + AND (t."dueCycle" IS NULL OR t."dueCycle" !~ '^[0-9]{4}-[0-9]{2}-[0-9]{2}$' + OR e.payload->>'dueCycle' IS DISTINCT FROM t."dueCycle" + OR t."dueCycle">to_char(clock_timestamp() AT TIME ZONE 'UTC','YYYY-MM-DD'))) + AND (${department}::text IS NULL OR t.department=${department}) + AND NOT EXISTS (SELECT 1 FROM corporate_tasks c WHERE c."parentId"=t.id AND c.status<>'DONE') + ORDER BY t.priority ASC,t."updatedAt" ASC,t.id ASC FOR UPDATE OF t SKIP LOCKED LIMIT 1`; + if (!candidates.length) return { task: null }; + const id = candidates[0].id, + token = randomUUID(); + await tx.$executeRaw`UPDATE corporate_tasks SET status='IN_PROGRESS', "assignedAgent"=${actor}, "claimToken"=${token}::uuid, "leasedUntil"=clock_timestamp()+make_interval(secs=>${seconds}::int), "executionCount"="executionCount"+1,"updatedAt"=clock_timestamp() WHERE id=${id}::uuid`; + await this.event(tx, id, actor, 'CLAIMED', 'Manual lease acquired'); + return { + task: await tx.corporateTask.findUniqueOrThrow({ where: { id } }), + }; + }, + ); + } + submitEvidence(actor: string, key: string, id: string, input: unknown) { + const b = evidenceInput(input); + return this.operation(actor, 'evidence:' + id, key, b, async (tx) => { + await this.fenced(tx, id, b.claimToken, actor); + const e = await tx.corporateEvidence.create({ + data: { ...b, taskId: id, executor: actor }, + }); + await tx.corporateTask.update({ + where: { id }, + data: { status: 'TEST', leasedUntil: null }, + }); + await this.event( + tx, + id, + actor, + 'EVIDENCE_SUBMITTED', + b.summary, + 'AGENT', + { evidenceId: e.id }, + ); + return e; + }); + } + review(actor: string, key: string, id: string, input: unknown) { + const b = object(input, [ + 'evidenceId', + 'passed', + 'findings', + 'verification', + ]); + const evidenceId = uuid(b.evidenceId), + findings = text(b.findings); + if (typeof b.passed !== 'boolean') + throw new BadRequestException('passed must be boolean'); + const v = object(b.verification, [ + 'artifactUri', + 'sha256', + 'method', + 'result', + ]); + const verification = { + artifactUri: text(v.artifactUri, 2000), + sha256: text(v.sha256, 64), + method: text(v.method, 1000), + result: text(v.result), + }; + return this.operation(actor, 'review:' + id, key, b, async (tx) => { + uuid(id); + const relation = await tx.corporateTask.findUnique({ + where: { id }, + select: { parentId: true }, + }); + if (relation?.parentId) await this.lock(tx, relation.parentId); + const task = await this.lock(tx, id); + const evidence = await tx.corporateEvidence.findUnique({ + where: { id: evidenceId }, + }); + if ( + task.status !== 'TEST' || + !evidence || + evidence.taskId !== id || + evidence.claimToken !== task.claimToken + ) + throw new ConflictException('Current execution evidence required'); + if (evidence.executor === actor) + throw new ConflictException('Independent reviewer required'); + if ( + verification.sha256 !== evidence.sha256 || + verification.artifactUri !== evidence.artifactUri + ) + throw new ConflictException('QA must verify exact artifact'); + if ( + b.passed && + (await tx.corporateTask.count({ + where: { parentId: id, status: { not: 'DONE' } }, + })) + ) + throw new ConflictException('Unfinished children'); + const r = await tx.corporateReview.create({ + data: { + taskId: id, + evidenceId, + reviewer: actor, + passed: b.passed, + findings, + verification, + }, + }); + await tx.corporateTask.update({ + where: { id }, + data: { + status: b.passed ? 'DONE' : 'TODO', + claimToken: null, + leasedUntil: null, + qaCount: { increment: 1 }, + rejectionCount: { increment: b.passed ? 0 : 1 }, + }, + }); + if (!b.passed && task.parentId) { + await tx.corporateTask.update({ + where: { id: task.parentId }, + data: { status: 'BLOCKED' }, + }); + await this.event( + tx, + task.parentId, + actor, + 'CHILD_QA_REJECTED', + 'Parent blocked pending child correction', + 'QA', + { childId: id }, + ); + } + await this.event( + tx, + id, + actor, + b.passed ? 'QA_PASSED' : 'QA_REJECTED', + findings, + 'QA', + { reviewId: r.id }, + ); + return r; + }); + } + async collection( + kind: 'market-insights' | 'monthly-roadmap' | 'reports', + q: Record, + ) { + const p = pagination(q); + return this.db.$transaction( + async (tx) => { + const model = + kind === 'market-insights' + ? tx.corporateResearch + : kind === 'monthly-roadmap' + ? tx.corporateRoadmap + : tx.corporateReport; + const where: any = {}; + if (kind === 'market-insights' && q.category) + where.category = choice(q.category, [ + 'TREND', + 'COMPETITOR', + 'NEW_CHANNEL', + 'CATALOG_GAP', + ]); + if (kind === 'monthly-roadmap' && q.month) + where.month = text(q.month, 40); + const delegate = model as any; + const total = await delegate.count({ where }); + const data = await delegate.findMany({ + where, + skip: p.skip, + take: p.limit, + orderBy: [{ createdAt: 'desc' }, { id: 'asc' }], + ...(kind === 'market-insights' + ? { include: { sources: true } } + : kind === 'monthly-roadmap' + ? { include: { goals: true } } + : {}), + }); + return publicData({ data, meta: meta(total, p.page, p.limit) }); + }, + { isolationLevel: Prisma.TransactionIsolationLevel.RepeatableRead }, + ); + } + research(actor: string, key: string, input: unknown) { + const b = object(input, [ + 'title', + 'category', + 'source', + 'opportunity', + 'impact', + 'recommendedAction', + 'cadence', + 'provenance', + 'sources', + ]); + if (!Array.isArray(b.sources) || !b.sources.length || b.sources.length > 30) + throw new BadRequestException('1..30 sources required'); + const sources = b.sources.map((raw) => { + const s = object(raw, [ + 'url', + 'title', + 'excerpt', + 'retrievedAt', + 'sha256', + ]); + const url = httpsUrl(s.url), + sha256 = text(s.sha256, 64), + retrievedAt = timestamp(s.retrievedAt, 'Invalid source time'); + if ( + !url.startsWith('https://') || + !/^[a-f0-9]{64}$/.test(sha256) || + !Number.isFinite(retrievedAt.getTime()) || + retrievedAt.getTime() > Date.now() + ) + throw new BadRequestException('Invalid source'); + return { + url, + sha256, + retrievedAt, + title: text(s.title, 300), + excerpt: text(s.excerpt), + }; + }); + const provenance = object(b.provenance, ['method', 'collector', 'context']); + const data = { + title: text(b.title, 300), + category: choice(b.category, [ + 'TREND', + 'COMPETITOR', + 'NEW_CHANNEL', + 'CATALOG_GAP', + ]), + source: text(b.source, 2000), + opportunity: text(b.opportunity), + impact: choice(b.impact, ['HIGH', 'MEDIUM', 'LOW']), + recommendedAction: text(b.recommendedAction), + cadence: choice(b.cadence, ['MONTHLY', 'EVENT']), + provenance: { + method: text(provenance.method), + collector: text(provenance.collector, 300), + context: text(provenance.context), + }, + createdBy: actor, + }; + return this.operation(actor, 'research', key, b, (tx) => + tx.corporateResearch.create({ + data: { ...data, sources: { create: sources } }, + include: { sources: true }, + }), + ); + } + roadmap(actor: string, key: string, input: unknown) { + const b = object(input, [ + 'researchId', + 'cycleName', + 'month', + 'seasonalTheme', + 'strategicGoals', + 'plannedArticlesCount', + 'plannedRefreshesCount', + 'plannedCatalogEnrichments', + 'isActive', + 'goals', + ]); + if ( + !Array.isArray(b.strategicGoals) || + !b.strategicGoals.length || + b.strategicGoals.length > 30 || + !Array.isArray(b.goals) || + !b.goals.length || + b.goals.length > 100 + ) + throw new BadRequestException('Goals required'); + for (const k of [ + 'plannedArticlesCount', + 'plannedRefreshesCount', + 'plannedCatalogEnrichments', + ]) + if (!Number.isInteger(b[k]) || b[k] < 0 || b[k] > 10000) + throw new BadRequestException('Invalid counts'); + if (b.isActive !== undefined && typeof b.isActive !== 'boolean') + throw new BadRequestException('Invalid active flag'); + const goals = b.goals.map((raw) => { + const g = object(raw, [ + 'title', + 'department', + 'acceptanceCriteria', + 'metric', + 'target', + ]); + if ( + g.target !== undefined && + (typeof g.target !== 'number' || !Number.isFinite(g.target)) + ) + throw new BadRequestException('Invalid target'); + return { + title: text(g.title, 300), + department: choice(g.department, departments), + acceptanceCriteria: text(g.acceptanceCriteria), + metric: text(g.metric, 300), + target: g.target, + }; + }); + const data = { + researchId: uuid(b.researchId), + cycleName: text(b.cycleName, 300), + month: text(b.month, 40), + seasonalTheme: text(b.seasonalTheme), + strategicGoals: b.strategicGoals.map((g) => text(g, 1000)), + plannedArticlesCount: b.plannedArticlesCount, + plannedRefreshesCount: b.plannedRefreshesCount, + plannedCatalogEnrichments: b.plannedCatalogEnrichments, + isActive: b.isActive ?? false, + createdBy: actor, + }; + return this.operation(actor, 'roadmap', key, b, async (tx) => { + const research = await tx.corporateResearch.findUnique({ + where: { id: data.researchId }, + include: { sources: true }, + }); + if (!research || !research.sources.length) + throw new ConflictException('Evidence-backed research required'); + const result = await tx.corporateRoadmap.create({ + data: { ...data, goals: { create: goals } }, + include: { goals: true }, + }); + await tx.corporateResearch.update({ + where: { id: research.id }, + data: { status: 'IN_BACKLOG' }, + }); + return result; + }); + } + effect(actor: string, key: string, id: string, input: unknown) { + const b = object(input, [ + 'metric', + 'availability', + 'baseline', + 'observed', + 'source', + 'observedAt', + ]); + const availability = choice(b.availability, [ + 'MEASURED', + 'UNAVAILABLE', + 'UNINSTRUMENTED', + 'BUSINESS_DISABLED', + ]); + for (const k of ['baseline', 'observed']) + if ( + b[k] !== undefined && + b[k] !== null && + (typeof b[k] !== 'number' || !Number.isFinite(b[k])) + ) + throw new BadRequestException('Invalid metric'); + if (availability === 'MEASURED' && typeof b.observed !== 'number') + throw new BadRequestException('Measured value required'); + if ( + availability !== 'MEASURED' && + (b.observed != null || b.baseline != null) + ) + throw new BadRequestException('Unavailable is not zero'); + const observedAt = timestamp(b.observedAt, 'Invalid observation time'); + if ( + !Number.isFinite(observedAt.getTime()) || + observedAt.getTime() > Date.now() + ) + throw new BadRequestException('Invalid observation time'); + return this.operation(actor, 'effect:' + id, key, b, async (tx) => { + const task = await this.lock(tx, id); + if (task.status !== 'DONE') + throw new ConflictException('Effects require completed QA'); + const e = await tx.corporateEffect.create({ + data: { + taskId: id, + metric: text(b.metric, 300), + availability, + baseline: b.baseline, + observed: b.observed, + source: text(b.source, 2000), + observedAt, + recordedBy: actor, + }, + }); + await this.event(tx, id, actor, 'EFFECT_RECORDED', e.metric, 'SYSTEM', { + effectId: e.id, + }); + return e; + }); + } + report(actor: string, key: string, input: unknown) { + const b = object(input, ['day', 'summary']); + const day = text(b.day, 10), + summary = text(b.summary); + if ( + !/^\d{4}-\d{2}-\d{2}$/.test(day) || + !Number.isFinite(Date.parse(day + 'T00:00:00Z')) + ) + throw new BadRequestException('ISO day required'); + return this.operation(actor, 'report', key, b, async (tx) => { + const start = new Date(day + 'T00:00:00Z'), + end = new Date(start.getTime() + 86400000); + const taskWhere = { updatedAt: { gte: start, lt: end } }; + const eventWhere = { timestamp: { gte: start, lt: end } }; + const effectWhere = { createdAt: { gte: start, lt: end } }; + const totals = { + tasks: await tx.corporateTask.count({ where: taskWhere }), + events: await tx.corporateEvent.count({ where: eventWhere }), + effects: await tx.corporateEffect.count({ where: effectWhere }), + }; + const tasks = await tx.corporateTask.findMany({ + where: taskWhere, + take: 100, + orderBy: [{ updatedAt: 'asc' }, { id: 'asc' }], + select: { + id: true, + taskCode: true, + title: true, + status: true, + department: true, + updatedAt: true, + }, + }); + const events = await tx.corporateEvent.findMany({ + where: eventWhere, + take: 100, + orderBy: [{ timestamp: 'asc' }, { id: 'asc' }], + select: { + id: true, + taskId: true, + timestamp: true, + speaker: true, + speakerName: true, + step: true, + content: true, + }, + }); + const effects = await tx.corporateEffect.findMany({ + where: effectWhere, + take: 100, + orderBy: [{ createdAt: 'asc' }, { id: 'asc' }], + select: { + id: true, + taskId: true, + metric: true, + availability: true, + baseline: true, + observed: true, + source: true, + observedAt: true, + createdAt: true, + }, + }); + let fieldsTruncated = false; + const project = (rows: any[]) => + rows.map((row) => + Object.fromEntries( + Object.entries(row).map(([name, value]) => { + if (typeof value === 'string' && value.length > 1000) { + fieldsTruncated = true; + return [name, value.slice(0, 1000)]; + } + return [name, value]; + }), + ), + ); + const bounded = { + tasks: project(tasks), + events: project(events), + effects: project(effects), + }; + const counts = Object.fromEntries( + Object.entries(totals).map(([name, total]) => [ + name, + { + total, + returned: Math.min(total, 100), + limit: 100, + truncated: total > 100, + }, + ]), + ); + const snapshot = JSON.parse( + JSON.stringify({ + timezone: 'UTC', + ...bounded, + meta: { + ...counts, + fieldsTruncated, + stringLimit: 1000, + eventPayloadsOmitted: true, + }, + }), + ); + return tx.corporateReport.create({ + data: { day, summary, snapshot, createdBy: actor }, + }); + }); + } +} diff --git a/scripts/deploy.sh b/scripts/deploy.sh index fc81e0af..93ee2851 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -1,6 +1,12 @@ #!/bin/bash set -e +# CI-only code restoration: isolate Stage before all legacy shared side effects. +if [ "${1:-}" = develop ]; then + SCRIPT_DIR=$(cd -- "$(dirname -- "$0")" && pwd) + exec bash "$SCRIPT_DIR/release-corporate-stage.sh" +fi + # Explicit no-data-write stage rollout. Run only after testing and pushing HEAD. if [ "${2:-}" = "stage-backend-only" ]; then [ "${1:-}" = "develop" ] || exit 2 diff --git a/scripts/release-corporate-stage.sh b/scripts/release-corporate-stage.sh new file mode 100644 index 00000000..1dc06d0a --- /dev/null +++ b/scripts/release-corporate-stage.sh @@ -0,0 +1,142 @@ +#!/usr/bin/env bash +# Narrow code-only Stage release in the existing CI runner/daemon boundary. +set -euo pipefail +set +x +umask 077 +[[ ${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}} == true && ${DEPLOY_SHA:-} =~ ^[0-9a-f]{40}$ ]] || exit 2 +STAGE_DIR=/opt/services/canina/stage +# Alpine runner has BusyBox timeout; supervision needs GNU --foreground. +if ! timeout --help 2>&1 | grep -q -- --foreground; then + apk add --no-cache coreutils +fi +timeout --help 2>&1 | grep -q -- --foreground || exit 2 +# The runner and host share a daemon, not a filesystem inode. The atomic +# container name serializes runners; the bind-mounted flock also fences host jobs. +# Never reclaim an existing name automatically (including stopped/stale helpers). +lock_container_id= +release_pid= +check_stage_lock() { + [[ $(timeout --foreground --kill-after=2s 10 docker inspect -f '{{.State.Running}}' "$lock_container_id") == true ]] || { + printf 'Stage host lock lost; aborting release\n' >&2 + return 1 + } +} +supervise_release() { + # A separate process group lets lock loss/signals stop the active command too. + set -m + (set +m; "$@") & + release_pid=$! + while kill -0 "$release_pid" 2>/dev/null; do + check_stage_lock || return 1 + sleep 1 + done + local status=0 + wait "$release_pid" || status=$? + release_pid= + check_stage_lock || return 1 + return "$status" +} +cleanup_stage_lock() { + local status=$? + trap - EXIT + if [[ -n $release_pid ]]; then + kill -TERM -- "-$release_pid" 2>/dev/null || : + sleep 1 + kill -KILL -- "-$release_pid" 2>/dev/null || : + wait "$release_pid" 2>/dev/null || : + fi + if [[ -n $lock_container_id ]]; then + if [[ $status != 0 ]]; then + # Never emit raw helper/daemon output (image entrypoints may contain + # secrets). Classify only a bounded tail before deleting the owned ID. + local helper_log diagnostic=unavailable + helper_log=$(timeout --foreground --kill-after=2s 10 docker logs --tail 10 "$lock_container_id" 2>&1 | head -c 4096) || : + case "$helper_log" in + *'Permission denied'*) diagnostic=permission-denied ;; + *'No such file or directory'*) diagnostic=missing-host-lock ;; + *'Read-only file system'*) diagnostic=read-only-filesystem ;; + *'flock: not found'*) diagnostic=flock-unavailable ;; + *CANINA_STAGE_DEPLOY_LOCK_ACQUIRED*) diagnostic=previously-acquired ;; + ?*) diagnostic=unclassified-output ;; + esac + printf 'Stage lock helper diagnostic: %s\n' "$diagnostic" >&2 + fi + timeout --kill-after=2s 15 docker rm -f "$lock_container_id" >/dev/null || { + printf 'Stage lock helper cleanup failed; manual inspection required\n' >&2 + [[ $status != 0 ]] || status=1 + } + fi + exit "$status" +} +trap cleanup_stage_lock EXIT +trap 'exit 130' INT +trap 'exit 143' TERM +runner_image=$(timeout --foreground --kill-after=2s 10 docker inspect -f '{{.Config.Image}}' gitea_runner) +[[ -n $runner_image ]] || exit 1 +lock_container_id=$(timeout --kill-after=2s 15 docker create --name canina-stage-deploy-lock \ + --pull=never --network none --cap-drop=ALL --security-opt=no-new-privileges \ + --mount type=bind,src=/opt/services/canina,dst=/host-canina,readonly \ + --entrypoint /bin/sh "$runner_image" -c ' + # Linux flock permits an exclusive lock on a read-only descriptor. Preserve + # daemon userns remapping; require the existing host inode, never create it. + exec 9&2; exit 1; } +[[ -n $lock_container_id ]] || exit 1 +timeout --foreground --kill-after=2s 10 docker start "$lock_container_id" >/dev/null +lock_acquired=false +for lock_attempt in {1..10}; do + [[ $(timeout --foreground --kill-after=2s 10 docker inspect -f '{{.State.Running}}' "$lock_container_id") == true ]] || break + if [[ $(timeout --foreground --kill-after=2s 10 docker logs --tail 1 "$lock_container_id" 2>/dev/null) == CANINA_STAGE_DEPLOY_LOCK_ACQUIRED ]]; then + lock_acquired=true + break + fi + [[ $lock_attempt == 10 ]] || sleep 1 +done +[[ $lock_acquired == true ]] || { printf 'Stage host lock not confirmed\n' >&2; exit 1; } +release_stage() { +cd "$STAGE_DIR" +timeout --foreground --kill-after=2s 60 git fetch origin develop +[[ $(git rev-parse origin/develop) == "$DEPLOY_SHA" ]] || exit 2 +RELEASE_ROOT=/opt/services/canina/cache-releases +mkdir -p "$RELEASE_ROOT" +TARGET_DIR=$(mktemp -d "$RELEASE_ROOT/$DEPLOY_SHA.corporate.XXXXXXXX") +git archive "$DEPLOY_SHA" | tar -x -C "$TARGET_DIR" +command -v python3 >/dev/null || apk add --no-cache python3 +export TARGET_DIR DEPLOY_SHA +python3 - <<'PY' +import json, os, hashlib +from pathlib import Path +root=Path(os.environ['TARGET_DIR']); sha=os.environ['DEPLOY_SHA'] +files={str(p.relative_to(root)):hashlib.sha256(p.read_bytes()).hexdigest() for p in sorted(root.rglob('*')) if p.is_file() and not p.is_symlink()} +(root/'.release-manifest.json').write_text(json.dumps({'sha':sha,'files':files},sort_keys=True)) +# Only backend code is changed. Preserve every operational runtime/env file. +service={'build':{'context':str(root/'backend'),'labels':{'org.opencontainers.image.revision':sha},'args':{'NPM_REGISTRY':'https://registry.parsaaghayi.ir/repository/npm/'}},'labels':{'ir.canina.release.sha':sha},'command':['node','dist/main.js']} +(root/'release.override.json').write_text(json.dumps({'services':{'backend_stage':service}})) +PY +compose=(docker compose -p stage --project-directory "$STAGE_DIR" -f "$STAGE_DIR/docker-compose.yml" -f "$TARGET_DIR/release.override.json") +timeout --foreground --kill-after=2s 60 "${compose[@]}" config --quiet +check_stage_lock +timeout --foreground --kill-after=2s 1800 "${compose[@]}" --progress plain build backend_stage +check_stage_lock +# No migrations, seed, DB/Redis/frontend/bot recreation or runtime config edits. +timeout --foreground --kill-after=2s 120 "${compose[@]}" up -d --no-deps --force-recreate backend_stage +ready=false +for attempt in {1..30}; do + check_stage_lock + [[ $(timeout --foreground --kill-after=2s 10 docker inspect -f '{{.State.Running}} {{.RestartCount}}' canino_backend_stage) == 'true 0' ]] || exit 1 + if timeout --foreground --kill-after=2s 10 docker exec canino_backend_stage node -e ' +const http=require("http"); +const req=http.get("http://127.0.0.1:3000/api/admin/hermes/monthly-roadmap",res=>{res.resume();process.exit(res.statusCode===401?0:1)}); +req.setTimeout(5000,()=>req.destroy());req.on("error",()=>process.exit(1));'; then ready=true; break; fi + sleep 2 +done +[[ $ready == true ]] || exit 1 +[[ $(timeout --foreground --kill-after=2s 10 docker inspect -f '{{index .Config.Labels "ir.canina.release.sha"}}' canino_backend_stage) == "$DEPLOY_SHA" ]] +image=$(timeout --foreground --kill-after=2s 10 docker inspect -f '{{.Image}}' canino_backend_stage) +[[ $(timeout --foreground --kill-after=2s 10 docker image inspect -f '{{index .Config.Labels "org.opencontainers.image.revision"}}' "$image") == "$DEPLOY_SHA" ]] +printf 'Corporate Stage backend release complete: %s image=%s (route guarded; authenticated acceptance separate)\n' "$DEPLOY_SHA" "$image" +} +supervise_release release_stage diff --git a/scripts/test-corporate-restoration.cjs b/scripts/test-corporate-restoration.cjs new file mode 100644 index 00000000..12cbab4d --- /dev/null +++ b/scripts/test-corporate-restoration.cjs @@ -0,0 +1,16 @@ +const {test} = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const root = path.join(__dirname, '..'); +test('AdminModule registers persisted corporate controller and service', () => { + const module = fs.readFileSync(path.join(root,'backend/src/admin/admin.module.ts'),'utf8'); + assert.match(module, /controllers:\s*\[[\s\S]*?CorporateController/); + assert.match(module, /providers:\s*\[[\s\S]*?CorporateService/); +}); +test('develop CI dispatches code-only isolated Stage release before legacy side effects', () => { + const deploy = fs.readFileSync(path.join(root,'scripts/deploy.sh'),'utf8'); + const dispatch = deploy.indexOf('exec bash "$SCRIPT_DIR/release-corporate-stage.sh"'); + assert.ok(dispatch > 0, 'Stage code-only dispatch required'); + assert.ok(dispatch < deploy.indexOf('docker network connect'), 'dispatch precedes shared bot and legacy DB changes'); +}); diff --git a/scripts/tests/test_corporate_stage_lock.py b/scripts/tests/test_corporate_stage_lock.py new file mode 100644 index 00000000..abddcc1e --- /dev/null +++ b/scripts/tests/test_corporate_stage_lock.py @@ -0,0 +1,219 @@ +"""Exercise the real release lock shell with a strictly local Docker double.""" +import json +import os +from pathlib import Path +import subprocess +import tempfile +import unittest + +SCRIPT = Path(__file__).resolve().parents[1] / 'release-corporate-stage.sh' + + +class StageDeployLockTests(unittest.TestCase): + def exercise(self, mode='ok', body='docker compose build; docker compose up', accelerated=False): + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + docker = root / 'docker' + docker.write_text('''#!/usr/bin/env python3 +import json, os, sys, time, signal +from pathlib import Path +args = sys.argv[1:] +root = Path(os.environ['FAKE_ROOT']) +with (root / 'calls').open('a') as out: out.write(json.dumps(args) + '\\n') +mode = os.environ['MODE'] +if (mode == 'stubborn-operation' and args[-1] == 'gitea_runner' + or mode == 'stubborn-cleanup' and args[0] == 'rm'): + signal.signal(signal.SIGTERM, signal.SIG_IGN) + time.sleep(6) # Self-bounded even before the production fix. + (root / 'stubborn-completed').touch() +if args[0] == 'inspect': + denied = False + if mode == 'remapped-host' and (root / 'create.json').exists(): + create = json.loads((root / 'create.json').read_text()) + # uid0 maps to host100000: directory1001:755/file0:644. + # DAC allows read/traversal, not truncation/creation. Model transport + # access only; the separate real-shell test proves actual host flock. + denied = '9>/host-canina/' in create[-1] + print('runner:local' if args[-1] == 'gitea_runner' else ('false' if denied or (root / 'lost').exists() else 'true')) +elif args[0] == 'create': + if mode == 'contended': sys.exit(1) + (root / 'held').touch() + (root / 'create.json').write_text(json.dumps(args)) + print('abc123owned') +elif args[0] == 'start': + if mode == 'start-failure': sys.exit(1) +elif args[0] == 'logs': + if mode == 'diagnostic-failure': + print('sh: cannot open lock: Permission denied; SECRET_MUST_NOT_LEAK', file=sys.stderr) + elif mode != 'host-contended': print('CANINA_STAGE_DEPLOY_LOCK_ACQUIRED') +elif args[0] == 'rm': + assert args[-1] == 'abc123owned', 'must only remove owned ID' + (root / 'held').unlink() +elif args[0] == 'compose': + assert (root / 'held').exists(), 'deployment ran without lock' + if mode == 'loss-during-build' and 'build' in args: + (root / 'lost').touch() + def stopped(*_): + with (root / 'calls').open('a') as out: out.write(json.dumps(['child-stopped']) + '\\n') + sys.exit(143) + signal.signal(signal.SIGTERM, stopped) + time.sleep(5) + (root / 'child-completed').touch() +else: sys.exit(99) +''') + docker.chmod(0o755) + (root / 'sleep').write_text('#!/bin/sh\nexec /bin/sleep 0.1\n') + (root / 'sleep').chmod(0o755) + source = SCRIPT.read_text().split('release_stage() {')[0] + if accelerated: + # Keep GNU timeout real; shorten only test-local deadlines. + source = source.replace('--kill-after=2s', '--kill-after=0.1s') + source = source.replace('--foreground --kill-after=0.1s 10 ', '--foreground --kill-after=0.1s 2 ') + source = source.replace('timeout 15 ', 'timeout 2 ') + source = source.replace('--kill-after=0.1s 15 ', '--kill-after=0.1s 2 ') + # Only the isolated lock prefix runs; no operational paths are touched. + result = subprocess.run(['bash', '-c', source + '\nrelease_stage() {\n' + body + '\n}\nsupervise_release release_stage'], + env={**os.environ, 'PATH': str(root) + ':' + os.environ['PATH'], + 'GITHUB_ACTIONS': 'true', 'DEPLOY_SHA': 'a' * 40, + 'FAKE_ROOT': str(root), 'MODE': mode}, + capture_output=True, text=True, timeout=20) + calls = [json.loads(line) for line in (root / 'calls').read_text().splitlines()] if (root / 'calls').exists() else [] + if accelerated: + self.assertFalse((root / 'stubborn-completed').exists(), + 'TERM-ignoring transport outlived its kill deadline') + return result, calls, (root / 'held').exists() + + def test_success_holds_host_lock_through_build_and_deploy(self): + result, calls, held = self.exercise() + self.assertEqual(result.returncode, 0, result.stderr) + create = next(c for c in calls if c[0] == 'create') + self.assertIn('--pull=never', create) + self.assertIn('type=bind,src=/opt/services/canina,dst=/host-canina,readonly', create) + self.assertIn('runner:local', create) + self.assertIn('flock', ' '.join(create)) + self.assertIn('/host-canina/stage-backend-deploy.lock', ' '.join(create)) + self.assertEqual(calls[-1], ['rm', '-f', 'abc123owned']) + self.assertEqual(len([c for c in calls if c[0] == 'compose']), 2) + self.assertFalse(held) + + def test_remapped_uid_can_lock_existing_root_owned_readable_host_file(self): + result, calls, held = self.exercise('remapped-host') + self.assertEqual(result.returncode, 0, result.stderr) + create = next(c for c in calls if c[0] == 'create') + self.assertIn('type=bind,src=/opt/services/canina,dst=/host-canina,readonly', create) + self.assertIn('--cap-drop=ALL', create) + self.assertIn('--security-opt=no-new-privileges', create) + self.assertFalse(any(c.startswith('--userns') or c == '--privileged' for c in create)) + self.assertNotIn('9>/host-canina/', create[-1]) + self.assertFalse(held) + + def test_existing_named_helper_fails_closed_without_removal(self): + result, calls, _ = self.exercise('contended') + self.assertNotEqual(result.returncode, 0) + self.assertFalse(any(c[0] in ('rm', 'compose', 'start') for c in calls)) + + def test_unconfirmed_host_lock_is_bounded_and_removes_only_owned_id(self): + result, calls, held = self.exercise('host-contended') + self.assertNotEqual(result.returncode, 0) + self.assertFalse(any(c[0] == 'compose' for c in calls)) + self.assertEqual(len([c for c in calls if c[:3] == ['logs', '--tail', '1']]), 10) + self.assertEqual(calls[-1], ['rm', '-f', 'abc123owned']) + self.assertFalse(held) + + def test_failure_diagnostics_are_sanitized_and_captured_before_owned_cleanup(self): + result, calls, held = self.exercise('diagnostic-failure') + self.assertNotEqual(result.returncode, 0) + self.assertIn('Stage lock helper diagnostic: permission-denied', result.stderr) + self.assertNotIn('SECRET_MUST_NOT_LEAK', result.stderr) + diagnostic = next(i for i, c in enumerate(calls) if c[:3] == ['logs', '--tail', '10']) + self.assertLess(diagnostic, len(calls) - 1) + self.assertFalse(any(c[0] == 'compose' for c in calls)) + self.assertFalse(held) + + def test_start_failure_cleans_owned_helper(self): + result, calls, held = self.exercise('start-failure') + self.assertNotEqual(result.returncode, 0) + self.assertEqual(calls[-1], ['rm', '-f', 'abc123owned']) + self.assertFalse(held) + self.assertFalse(any(c[0] == 'compose' for c in calls)) + + def test_deploy_failure_preserves_status_and_releases_owned_helper(self): + result, calls, held = self.exercise(body='docker compose build; exit 42') + self.assertEqual(result.returncode, 42) + self.assertEqual(calls[-1], ['rm', '-f', 'abc123owned']) + self.assertFalse(held) + + def test_termination_releases_owned_helper(self): + result, calls, held = self.exercise(body='kill -TERM $$') + self.assertEqual(result.returncode, 143) + self.assertEqual(calls[-1], ['rm', '-f', 'abc123owned']) + self.assertFalse(held) + + def test_loss_after_acquisition_before_rollout_prevents_up(self): + result, calls, held = self.exercise(body='touch "$FAKE_ROOT/lost"; /bin/sleep 2; docker compose up') + self.assertNotEqual(result.returncode, 0, result.stderr) + self.assertFalse(any(c[0] == 'compose' for c in calls)) + self.assertFalse(held) + + def test_loss_during_build_interrupts_child_and_prevents_up(self): + result, calls, held = self.exercise('loss-during-build') + self.assertNotEqual(result.returncode, 0, result.stderr) + self.assertFalse(any(c[0] == 'compose' and 'up' in c for c in calls)) + self.assertIn('Stage host lock lost', result.stderr) + self.assertIn(['child-stopped'], calls) + self.assertFalse(held) + + def test_loss_interrupts_timeout_wrapped_docker_child(self): + result, calls, held = self.exercise('loss-during-build', body='timeout --foreground 20 docker compose build; docker compose up') + self.assertNotEqual(result.returncode, 0, result.stderr) + self.assertIn(['child-stopped'], calls) + self.assertFalse(any(c[0] == 'compose' and 'up' in c for c in calls)) + self.assertFalse(held) + + def test_term_ignoring_operational_transport_is_killed(self): + result, calls, held = self.exercise('stubborn-operation', accelerated=True) + self.assertNotEqual(result.returncode, 0) + self.assertFalse(any(c[0] in ('create', 'compose') for c in calls)) + self.assertFalse(held) + + def test_term_ignoring_cleanup_transport_is_killed_and_fails_closed(self): + result, calls, held = self.exercise('stubborn-cleanup', accelerated=True) + self.assertNotEqual(result.returncode, 0) + self.assertIn('manual inspection required', result.stderr) + self.assertTrue(held, 'failed cleanup must not claim removal') + self.assertEqual(calls[-1], ['rm', '-f', 'abc123owned']) + + def test_helper_shell_contends_on_exact_host_inode(self): + import fcntl + import selectors + source = SCRIPT.read_text() + helper = source.split('--entrypoint /bin/sh "$runner_image" -c \'')[1].split("\n ')")[0] + with tempfile.TemporaryDirectory() as tmp: + helper = helper.replace('/host-canina', tmp).replace('sleep 60', 'sleep 0.1') + lock_path = Path(tmp) / 'stage-backend-deploy.lock' + with lock_path.open('w') as host: + fcntl.flock(host, fcntl.LOCK_EX | fcntl.LOCK_NB) + blocked = subprocess.run(['sh', '-c', helper], capture_output=True, text=True, timeout=2) + self.assertNotEqual(blocked.returncode, 0) + self.assertNotIn('ACQUIRED', blocked.stdout) + lock_path.write_text('existing-host-inode\n') + lock_path.chmod(0o444) + with subprocess.Popen(['sh', '-c', helper], stdout=subprocess.PIPE, text=True) as proc: + try: + assert proc.stdout is not None + with selectors.DefaultSelector() as selector: + selector.register(proc.stdout, selectors.EVENT_READ) + self.assertTrue(selector.select(timeout=2), 'helper did not confirm flock') + self.assertEqual(proc.stdout.readline().strip(), 'CANINA_STAGE_DEPLOY_LOCK_ACQUIRED') + self.assertEqual(lock_path.read_text(), 'existing-host-inode\n') + self.assertEqual(lock_path.stat().st_mode & 0o777, 0o444) + with lock_path.open('r') as host: + with self.assertRaises(BlockingIOError): + fcntl.flock(host, fcntl.LOCK_EX | fcntl.LOCK_NB) + finally: + proc.terminate() + proc.wait(timeout=2) + + +if __name__ == '__main__': + unittest.main() diff --git a/specs/corporate-restoration/plan.md b/specs/corporate-restoration/plan.md new file mode 100644 index 00000000..7ea2b902 --- /dev/null +++ b/specs/corporate-restoration/plan.md @@ -0,0 +1,6 @@ +# Plan +Baseline develop 4d41a5bbf70e9bf9f9ead2412822668998b2c2cf; historical implementation 216e05ba258cf7882350f00da9d9b659885d9816. Restore only corporate policy/service/controller and matching standalone Prisma models, register in AdminModule. Keep existing shared JWT and role guards (admin API-key scope enforced). Restore original regression tests and add module registration gate. No historical migrations applied: existing tables must match generated types read-only. + +Graphify baseline graph SHA256 6a73473cee9e207543fdeeb668bea544f766200a1b16b8a3a9ce18d4e42e51af (4304 nodes, source binding unknown); isolated query returned unrelated review symbols and is navigation only. Direct source establishes AdminModule → controller → service → Prisma, guards → ApiKeysService. No frontend changes. Durable isolated repository /opt/data/engineering/canina-corporate-restoration-repo (metadata .git), official SSH origin. + +Release blocker discovered: current CI deploy.sh resets operational Stage, recreates shared bot and runs migrations. Add a narrow Stage-only early dispatcher using tracked isolated archive and operational Compose override, existing host lock, backend-only build/recreate, no database operations. Main path untouched. Exercise dispatcher regression before modification. Confirm exact pushed SHA/run/image then authenticated existing-record reads; no live lifecycle mutation of publisher tasks. diff --git a/specs/corporate-restoration/review.md b/specs/corporate-restoration/review.md new file mode 100644 index 00000000..d84f43dc --- /dev/null +++ b/specs/corporate-restoration/review.md @@ -0,0 +1,27 @@ +# Restoration review and execution evidence + +## Scope + +Restore historical `216e05ba258cf7882350f00da9d9b659885d9816` corporate policy/controller/service onto develop `4d41a5bbf70e9bf9f9ead2412822668998b2c2cf`. Physical corporate tables already exist: no migration file, SQL deployment, seed, runtime credential change, publication or schedule enablement is included. All lifecycle mutations remain explicit authenticated API operations. Existing PLANNED rows are read, never regenerated. + +## Review + +Self-review only; an independent reviewer is unavailable within this nondelegating leaf task. Reviewed AdminModule registration, authenticated stable actor identity, API-key admin scope guard, strict object input allowlists (Body unknown intentionally delegates to policy), mandatory mutation idempotency, transaction/advisory locks, skip-locked claims, lease fencing, independent exact-artifact review, and completed-QA effect gate. Added production-style ValidationPipe to integration harness to ensure strict global validation does not reject the unknown-body contract. + +Stage rollout uses official develop push CI, commit-pinned script download, and exact origin/develop SHA check. A historical tested shared-daemon lock prefix is retained. Build and recreate target backend_stage only; command is node dist/main.js. Operational Compose/env, frontend, bot, DB, Redis and Production remain untouched. No remove-orphans, migration or seeding commands. Image/container revision labels and immutable source hash manifest establish provenance. Guarded-route readiness is separate from authenticated acceptance. + +## Tests and evidence + +Durable raw logs: `/opt/data/engineering/corporate-restoration-evidence/`. + +- Backend initial full suite: 3 failed, 1 skipped, 85 passed; 4 failed, 17 skipped, 708 passed. Failing files are existing support-chat HTTP/settings/service tests. +- Equivalent base full suite with identical resolved dependencies: 3 failed, 84 passed; 4 failed, 701 passed. Same unrelated failures; no claim of full-suite green. +- Disposable PostgreSQL 16.14 actual API policy/integration suite: 2 suites, 24 tests passed, including concurrent claims/idempotency/monthly-plan contention and rollback fault injection. Database is loopback corporate_test only, historical corporate DDL fixture only, never runtime DATABASE_URL. +- Lock transport suite: 14 tests passed including exact inode contention, lock loss, fail-closed contention, bounded stubborn transport and owned-ID cleanup. +- Restoration wiring node tests: 2 passed. Bash syntax and non-CI denial (exit 2) checked. +- Backend TypeScript build successful. No frontend changes/build required for backend-only rollout. +- Read-only Stage inventory: 107 physical corporate columns with UUID, enum, JSONB, timestamptz, float8 mappings recorded in stage-columns.txt; 42 PLANNED events. No live DDL. + +Graphify existing graph was stale and returned unrelated review nodes; a scoped structural extraction was generated outside Git: graphify-restoration.json, 93 nodes/399 raw edges. Integrity diagnostics report 72 dangling endpoint edges (partial corpus), 3 self-loops, 2 directed pair collapses; graph is navigation only, not proof of completeness. Direct source and executable tests are authoritative. Semantic token use: zero (AST-only extraction). + +Live exact SHA, official CI completion and authenticated reads must be recorded in the external release receipt after push; tests/commit alone are not Stage acceptance. diff --git a/specs/corporate-restoration/spec.md b/specs/corporate-restoration/spec.md new file mode 100644 index 00000000..378ccedb --- /dev/null +++ b/specs/corporate-restoration/spec.md @@ -0,0 +1,5 @@ +# Existing corporate API restoration — Stage only + +Restore the existing authenticated monthly-roadmap listing, monthly-plan readback, task list/detail and claim → evidence → independent review → effect lifecycle. The existing November roadmap 1c0c4ffb-17d0-48ae-b53e-f0f51bffd1e0 and its 30 article tasks must remain unchanged; never regenerate the plan. + +Acceptance: authenticated reads return existing records; uncredentialed requests fail 401; non-admin scopes fail 403; leases, artifact matching, independent review and idempotency survive restoration. No arbitrary status bypass. No publication, scheduler, credential or Production changes. Code-only Stage CI must not migrate/seed or overwrite operational configuration. PostgreSQL compatibility is required before release; table presence alone is insufficient. diff --git a/specs/corporate-restoration/tasks.md b/specs/corporate-restoration/tasks.md new file mode 100644 index 00000000..e7ae46bc --- /dev/null +++ b/specs/corporate-restoration/tasks.md @@ -0,0 +1,9 @@ +# Tasks and consistency check +- [x] Read discovery and verify official develop/SSH access; inspect original and deployed contracts. +- [x] Scope/spec/plan and graph navigation recorded; no conflicting Production authority. +- [ ] RED: existing corporate runtime registration and policy/API regression. +- [ ] GREEN: minimal original corporate runtime/models, module registration. +- [ ] Typed Stage catalog compatibility and disposable PostgreSQL lifecycle regressions. +- [ ] RED/GREEN safe Stage-only CI dispatcher; full backend/build gates. +- [ ] Commit/push develop; wait for exact CI completion, immutable image and authenticated reads. +- [ ] Receipt with distinct test/CI/live acceptance limitations, no regenerated plan.