Compare commits

..
Author SHA1 Message Date
devops 55d11fa597 fix(stage): route blog revalidation to Next and omit unrelated article media
Deploy Canina / deploy (push) Successful in 3m16s
2026-11-15 05:54:49 +00:00
devops 85ef01d337 fix(corporate): restore persisted orchestration APIs with code-only Stage CI
Deploy Canina / deploy (push) Successful in 4m39s
2026-11-14 07:32:10 +00:00
37 changed files with 2931 additions and 4550 deletions

No files matched your search

+6 -1
View File
@@ -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
-29
View File
@@ -1,29 +0,0 @@
name: Production publisher helper only
on:
push:
branches:
- ops/prod-publisher-helper-20261112
jobs:
publisher-helper:
runs-on: canina
steps:
- name: Exact reviewed one-file helper release
env:
GITHUB_ACTIONS: 'true'
GITHUB_REF: ${{ github.ref }}
RELEASE_SHA: ${{ github.sha }}
PUBLISHER_HELPER_APPROVED_SHA: ${{ secrets.PUBLISHER_HELPER_APPROVED_SHA }}
run: |
set -eu
[ "$GITHUB_REF" = refs/heads/ops/prod-publisher-helper-20261112 ]
case "$RELEASE_SHA" in *[!0-9a-f]*|'') exit 2 ;; esac
[ "${#RELEASE_SHA}" -eq 40 ]
[ "$RELEASE_SHA" = "$PUBLISHER_HELPER_APPROVED_SHA" ]
export http_proxy= https_proxy= HTTP_PROXY= HTTPS_PROXY=
mkdir -p publisher-src
wget --no-proxy -qO source.tar.gz "https://git.parsaaghayi.ir/parsa/canina/archive/${RELEASE_SHA}.tar.gz"
tar -xzf source.tar.gz --strip-components=1 -C publisher-src
cd publisher-src
export CANINA_EDITORIAL_TEST_SOURCE="$PWD/scripts/publishers/e4c9b52eb040/fixtures"
python3 -m unittest discover -s scripts/publishers/e4c9b52eb040 -p 'test*.py' -v
python3 scripts/publishers/e4c9b52eb040/release_helper.py --sha "$RELEASE_SHA" --approval f5dfb28159010ae043009296006e91bafadd0f62b72681d22063b43d01e81880
+200
View File
@@ -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")
}
+4
View File
@@ -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,
@@ -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();
});
});
@@ -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<string, any> {
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<string, any>;
}
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<string, any>) {
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<string, string> = {
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),
};
}
@@ -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<string, any>) {
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<string, any>) {
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<string, any>) {
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<string, any>) {
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);
}
}
@@ -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<void>((r) => (ready = r));
const hold = new Promise<void>((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<any[]>`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<void>((r) => (locked = r));
const hold = new Promise<void>((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<void>((r) => (locked = r)),
hold = new Promise<void>((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);
});
});
@@ -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<any>,
) {
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<string, any>) {
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<string, any>,
) {
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 },
});
});
}
}
@@ -0,0 +1,27 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
vi.mock('../../../lib/seo', () => ({ getSeoConfig: async () => ({brandNameFa:'Canina', ogImageUrl:'https://example.org/unrelated.webp'}), formatPageTitle: (s:string) => s }));
vi.mock('../../../components/BlogPostClient', () => ({default: () => null}));
vi.mock('next/navigation', () => ({notFound: () => {throw Error('404')}}));
import Page, {generateMetadata} from './page';
const base = {title:'Existing article',content:'Body',slug:'existing',publishedAt:'2026-11-15T05:41:53Z'};
let blog: any;
beforeEach(() => {blog={...base}; vi.stubGlobal('fetch',vi.fn(async () => ({ok:true,json:async () => blog})));});
describe('article-owned media', () => {
it.each([null,undefined,'',' '])('omits unrelated imagery for %s', async (image) => {
blog.imageUrl=image; blog.ogImage=image;
const metadata:any=await generateMetadata({params:Promise.resolve({slug:'existing'})});
expect(metadata.openGraph.images).toEqual([]);
expect(metadata.twitter.images).toEqual([]);
expect(metadata.twitter.card).toBe('summary');
const page:any=await Page({params:Promise.resolve({slug:'existing'})});
const schema=JSON.parse(page.props.children[0].props.dangerouslySetInnerHTML.__html);
expect(schema).not.toHaveProperty('image');
});
it('retains authored OG precedence and Article image', async () => {
blog.imageUrl='/uploads/article.webp'; blog.ogImage='/uploads/social.webp';
const metadata:any=await generateMetadata({params:Promise.resolve({slug:'existing'})});
expect(metadata.openGraph.images[0].url).toBe('/uploads/social.webp');
const page:any=await Page({params:Promise.resolve({slug:'existing'})});
expect(JSON.parse(page.props.children[0].props.dangerouslySetInnerHTML.__html).image[0]).toContain('/uploads/article.webp');
});
});
@@ -77,7 +77,7 @@ export async function generateMetadata({ params }: { params: Promise<{ slug: str
(typeof blog.content === 'string' ? blog.content.substring(0, 155).replace(/<[^>]+>/g, '') : '') ||
config.defaultMetaDescription;
const canonicalUrl = blog.canonicalUrl ? blog.canonicalUrl.replace(/\/+$/, '') : `${siteUrl}/blog/${slug}`;
const ogImageUrl = blog.ogImage || blog.imageUrl || config.ogImageUrl;
const ogImageUrl = (typeof blog.ogImage === 'string' ? blog.ogImage.trim() : '') || (typeof blog.imageUrl === 'string' ? blog.imageUrl.trim() : '');
const ogTitle = blog.ogTitle || title;
const ogDesc = blog.ogDescription || description;
@@ -133,14 +133,14 @@ export async function generateMetadata({ params }: { params: Promise<{ slug: str
openGraph: {
title: ogTitle,
description: ogDesc,
images: [
images: ogImageUrl ? [
{
url: ogImageUrl,
width: 1200,
height: 630,
alt: blog.imageAlt || blog.title || title,
},
],
] : [],
url: canonicalUrl,
siteName: config.brandNameFa,
type: "article",
@@ -150,10 +150,10 @@ export async function generateMetadata({ params }: { params: Promise<{ slug: str
tags: blog.tags?.map((t: any) => typeof t === 'string' ? t : t?.name).filter(Boolean) || [],
},
twitter: {
card: "summary_large_image",
card: ogImageUrl ? "summary_large_image" : "summary",
title: ogTitle,
description: ogDesc,
images: [ogImageUrl],
images: ogImageUrl ? [ogImageUrl] : [],
},
};
}
@@ -205,9 +205,9 @@ export default async function BlogPostPage({ params }: { params: Promise<{ slug:
},
headline: blog.title,
description: blog.excerpt || blog.metaDescription || blog.title,
image: blog.imageUrl
? [blog.imageUrl.startsWith('http') ? blog.imageUrl : `${siteUrl}${blog.imageUrl.startsWith('/') ? '' : '/'}${blog.imageUrl}`]
: [config.ogImageUrl.startsWith('http') ? config.ogImageUrl : `${siteUrl}${config.ogImageUrl.startsWith('/') ? '' : '/'}${config.ogImageUrl}`],
...(typeof blog.imageUrl === 'string' && blog.imageUrl.trim()
? { image: [new URL(blog.imageUrl.trim(), siteUrl).href] }
: {}),
datePublished: publishedIso,
dateModified: modifiedIso,
author: authorSchema,
+9
View File
@@ -74,6 +74,15 @@
}
}
/* Authored body links remain recognizable without relying on color alone. */
.article-html-content a {
color: var(--color-canina-blue);
font-weight: 700;
text-decoration: underline;
text-underline-offset: 4px;
}
.article-html-content a:hover { color: var(--color-medical-gray-900); }
/* Sonner Toasts Vazirmatn Font & Mobile Offset */
[data-sonner-toaster] {
font-family: "Vazirmatn", sans-serif !important;
+19
View File
@@ -0,0 +1,19 @@
# Stage-only storefront repair; retain the deployed admin and nginx artifacts.
ARG STAGE_BASE_IMAGE
FROM node:20-alpine AS app-builder
WORKDIR /app
ARG NPM_REGISTRY=https://registry.parsaaghayi.ir/repository/npm/
RUN npm config set registry "$NPM_REGISTRY" && npm config set strict-ssl false
COPY frontend/application/package*.json ./
RUN npm ci --include=dev --prefer-offline --no-audit
COPY frontend/application ./
ENV NEXT_TELEMETRY_DISABLED=1
ENV NEXT_PUBLIC_API_URL=https://stageapi.canina.ir/api
ENV NEXT_PUBLIC_SITE_URL=https://stage.canina.ir
ENV NEXT_PUBLIC_APP_ENV=staging
RUN npm run build
FROM ${STAGE_BASE_IMAGE}
WORKDIR /app
COPY --from=app-builder /app ./
ENV NEXT_PUBLIC_SITE_URL=https://stage.canina.ir
ENV NEXT_PUBLIC_APP_ENV=staging
+6
View File
@@ -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
@@ -1,272 +0,0 @@
#!/usr/bin/env python3
"""Offline completeness gate, NOT medical/SEO approval or live verification.
No network, credentials, publisher-state operations or article mutations.
"""
import argparse
import hashlib
import json
from pathlib import Path
import re
from html.parser import HTMLParser
from urllib.parse import urlsplit
SOURCES = {
'admin': 'frontend/admin-panel/src/pages/Blogs.tsx',
'client': 'frontend/admin-panel/src/services/api.ts',
'controller': 'backend/src/admin/blogs.controller.ts',
'service': 'backend/src/admin/blogs.service.ts',
'schema': 'backend/prisma/schema.prisma',
}
SYSTEM = {'id', 'viewCount', 'createdAt', 'updatedAt'}
RELATIONS = {'author', 'category', 'tags', 'comments'}
DISPOSITIONS = {'verified_value', 'system_derived', 'not_applicable', 'evidence_unavailable_empty'}
def sha(path):
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
def manifest(root):
root = Path(root)
sources = {k: {'path': v, 'sha256': sha(root / v)} for k, v in SOURCES.items()}
schema = (root / SOURCES['schema']).read_text()
block = re.search(r'model Blog\s*\{(.*?)\n\}', schema, re.S)
if not block:
raise ValueError('Blog model missing')
fields = {}
for line in block.group(1).splitlines():
m = re.match(r'\s*(\w+)\s+([\w\[\]?]+)', line)
if not m:
continue
name, typ = m.groups()
fields[name] = {'type': typ, 'kind': 'system' if name in SYSTEM else 'relation' if name in RELATIONS else 'scalar'}
# tagNames is a transport alias, not a Prisma field; do not send tags/tagIds.
fields['tagNames'] = {'type': 'String[]', 'kind': 'transport_alias', 'readback': 'tags[].name'}
fields['markContentUpdated'] = {'type': 'Boolean', 'kind': 'transport_control', 'readback': 'contentUpdatedAt'}
# Optional UPDATE concurrency precondition; never a persisted Blog scalar.
fields['expectedUpdatedAt'] = {'type': 'String', 'kind': 'transport_control', 'actions': ['UPDATE'], 'persisted': False}
for role, needles in {
'admin': ["api.post('/admin/blogs', payload)", 'tagNames', 'relatedBlogIds'],
'controller': ['@Controller("admin/blogs")', '@Get("tags")', '@Post()'],
'service': ['tagNames', 'connectOrCreate', 'relatedBlogIds', 'contentUpdatedAt'],
'client': ['axios'],
}.items():
text = (root / SOURCES[role]).read_text()
if any(n not in text for n in needles):
raise ValueError(f'{role} contract changed: review parser and manifest manually')
return {'scope': 'LOCAL_SOURCE_CONTRACT_NOT_DEPLOYMENT_PROOF', 'sources': sources, 'fields': fields}
class Links(HTMLParser):
def __init__(self):
super().__init__()
self.hrefs = set()
def handle_starttag(self, tag, attrs):
if tag == 'a':
href = dict(attrs).get('href', '')
u = urlsplit(href)
if (not u.netloc and href.startswith('/') and not href.startswith('//')) or u.netloc == 'canina.ir':
self.hrefs.add(href)
CITATION = re.compile(r'\[\s*([0-9۰-۹٠-٩]+(?:\s*[,،;؛–-]\s*[0-9۰-۹٠-٩]+)*)\s*\]')
class CitationHTML(HTMLParser):
def __init__(self):
super().__init__(convert_charrefs=True)
self.stack = []
self.anchors = []
self.raw = []
self.targets = {}
def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
self.stack.append((tag, attrs))
if attrs.get('id'):
self.targets.setdefault(attrs['id'], set())
if tag == 'a':
self.anchors.append({'href': attrs.get('href', ''), 'text': ''})
attrs['_anchor'] = self.anchors[-1]
for _, parent in self.stack:
if parent.get('id'):
self.targets.setdefault(parent['id'], set()).add(attrs.get('href', ''))
if tag in {'br', 'img', 'hr', 'input', 'meta', 'link'}:
self.stack.pop()
def handle_endtag(self, tag):
for i in range(len(self.stack)-1, -1, -1):
if self.stack[i][0] == tag:
del self.stack[i:]
break
def handle_data(self, text):
anchors = [attrs['_anchor'] for _, attrs in self.stack if '_anchor' in attrs]
if anchors:
anchors[-1]['text'] += text
else:
self.raw.append(text)
def validate_citations(content, sources):
"""Map numeric inline anchors to retrieved source URLs; no network claims."""
parser = CitationHTML()
parser.feed(content)
errors = []
if CITATION.search(''.join(parser.raw)):
errors.append('citations: raw unlinked numbered bracket citation')
for anchor in parser.anchors:
for match in CITATION.finditer(anchor['text']):
numbers = re.split(r'\s*[,،;؛–-]\s*', match[1])
for number in numbers:
key = str(int(number))
source = sources.get(key, {})
url = source.get('url', '')
href = anchor['href']
if not (urlsplit(url).scheme in {'http', 'https'} and urlsplit(url).netloc and source.get('retrieved') is True):
errors.append('citations: missing retrieved source mapping ' + key)
elif href != url and not (href == '#ref-' + key and url in parser.targets.get('ref-' + key, set())):
errors.append('citations: anchor does not resolve to mapped source ' + key)
return errors
def validate(packet, root, base):
"""Errors are cumulative. Evidence assertions still require independent review."""
errors = []
def require(ok, message):
if not ok:
errors.append(message)
expected = manifest(root)
require(packet.get('manifest') == expected, 'current complete source-derived manifest/hash mismatch')
payload = packet.get('payload', {})
errors.extend(validate_citations(payload.get('content', ''), packet.get('citation_sources', {})))
action = packet.get('action')
require(action in {'NEW', 'UPDATE', 'READ_ONLY_ACCEPTANCE'}, 'explicit action required')
if action in {'UPDATE', 'READ_ONLY_ACCEPTANCE'}:
require(bool(packet.get('record_id')), 'existing-record action requires record_id')
fields = packet.get('field_dispositions', {})
require(set(fields) == set(expected['fields']), 'every manifest field/control needs exactly one disposition')
allowed = set(expected['fields']) - SYSTEM - RELATIONS
require(not (set(payload) - allowed), 'unknown/system/readback-only payload keys: ' + ','.join(sorted(set(payload) - allowed)))
for key, field in fields.items():
require(field.get('disposition') in DISPOSITIONS, f'{key}: invalid disposition')
require(bool(field.get('reason')), f'{key}: reason required')
require(bool(field.get('evidence')), f'{key}: evidence references required')
if field.get('disposition') == 'verified_value':
require('value' in field, f'{key}: verified_value needs explicit value')
if key in allowed:
require(key in payload, f'{key}: verified writable value must be in payload; omission needs explicit disposition')
if key in payload:
require(field.get('value') == payload[key], f'{key}: disposition value differs from payload')
if field.get('disposition') in {'not_applicable', 'evidence_unavailable_empty'} and key in payload:
require(payload[key] in (None, '', []), f'{key}: deliberately empty disposition has nonempty payload')
if expected['fields'].get(key, {}).get('kind') == 'transport_control':
require(field.get('disposition') != 'verified_value', f'{key}: transport control is not a fetched scalar; use an explicit no-mutation/system-derived disposition')
if key in SYSTEM:
require(field.get('disposition') == 'system_derived', f'{key}: must be system-derived, not authored')
evidence = packet.get('evidence', {})
for key, entry in evidence.items():
path = Path(base) / entry.get('path', '')
require(path.is_file(), f'{key}: missing evidence file')
if path.is_file():
require(sha(path) == entry.get('sha256'), f'{key}: evidence hash mismatch')
require(bool(entry.get('readAt')), f'{key}: evidence readAt required')
def refs(refs, context):
require(bool(refs) and all(ref in evidence for ref in refs), context + ': unresolved evidence references')
for key, field in fields.items():
refs(field.get('evidence', []), key)
refs(packet.get('artifact_evidence', []), 'immutable artifact')
# Acceptance must bind dispositions to actual saved authenticated JSON, not
# a reconstructed payload that invents absent relations or transport fields.
if action == 'READ_ONLY_ACCEPTANCE':
binding = packet.get('acceptance_readback', {})
ref = binding.get('evidence')
require(ref in evidence, 'acceptance_readback: authenticated record evidence required')
if ref in evidence:
try:
fetched = json.loads((Path(base) / evidence[ref]['path']).read_text())
require(fetched.get('status') == 200, 'acceptance_readback: authenticated 200 required')
record = fetched['body']['data']
require(record.get('id') == packet.get('record_id'), 'acceptance_readback: record identity mismatch')
for key, field in fields.items():
kind = expected['fields'].get(key, {}).get('kind')
if kind == 'transport_control':
require(key not in record, key + ': unexpected transport readback requires contract review')
continue
if key == 'tagNames':
actual = [tag['name'] for tag in record.get('tags', [])]
require(field.get('value') == actual, key + ': readback alias differs')
elif key not in record:
require(field.get('disposition') in {'not_applicable', 'evidence_unavailable_empty'}, key + ': absent from fetched record; cannot claim fetched/system-derived value')
require(field.get('value') in (None, '', []), key + ': absent fetched field must be explicitly empty')
else:
require(field.get('value') == record[key], key + ': disposition differs from actual authenticated record')
except (ValueError, OSError, KeyError, TypeError):
require(False, 'acceptance_readback: invalid authenticated record evidence')
# Bind reviewed article to exact payload bytes, not merely a nearby draft.
artifact = packet.get('artifact', {})
path = Path(base) / artifact.get('path', '')
require(path.is_file(), 'immutable payload artifact missing')
if path.is_file():
require(sha(path) == artifact.get('sha256'), 'immutable payload artifact hash mismatch')
try:
require(json.loads(path.read_text()) == payload, 'immutable artifact differs from payload')
except (ValueError, OSError):
require(False, 'immutable artifact is not payload JSON')
relations = packet.get('relations', {})
for key, value in [('authorId', payload.get('authorId')), ('categoryId', payload.get('categoryId'))]:
item = relations.get(key, {})
require(item.get('selected') == value, key + ': selection mismatch')
require(bool(item.get('reason')), key + ': attribution/category rationale missing')
refs(item.get('evidence', []), key + ' authenticated read')
require(item.get('authenticated') is True and item.get('status') == 200, key + ': authenticated 200 read required')
for key in ['tagNames', 'relatedBlogIds', 'relatedProductIds']:
item = relations.get(key, {})
selected = payload.get(key, [])
require(item.get('selected') == selected, key + ': selection mismatch')
require(bool(item.get('reason')), key + ': deliberate disposition required even when empty')
refs(item.get('evidence', []), key + ' inventory/destination review')
require(item.get('authenticated') is True and item.get('status') == 200, key + ': authenticated inventory read required')
require(item.get('inventory_complete') is True, key + ': complete inventory required')
require(isinstance(selected, list) and len(selected) == len(set(selected)), key + ': unique array required')
require(set(selected or []) <= set(item.get('available', [])), key + ': selection absent from verified inventory')
if key == 'relatedBlogIds':
require(packet.get('record_id') not in selected, 'self related-article forbidden')
parser = Links()
parser.feed(payload.get('content', ''))
links = packet.get('internal_links', {})
candidates = links.get('reviewed_destinations', [])
require(bool(candidates), 'contextual link destination shortlist not reviewed')
for item in candidates:
refs(item.get('evidence', []), 'destination ' + item.get('href', '?'))
require(bool(item.get('reason')), 'destination suitability/medical-safety rationale required')
require(item.get('identity_verified') is True and item.get('content_reviewed') is True, 'destination content/identity not verified (HTTP 200 insufficient)')
selected_links = links.get('selected', [])
require(set(selected_links) == parser.hrefs, 'contextual body links differ from reviewed selections')
suitable = {item.get('href') for item in candidates if item.get('suitable') is True}
require(set(selected_links) <= suitable, 'selected link has no suitable reviewed destination')
if not selected_links:
require(bool(links.get('zero_link_reason')) and links.get('disclose_in_report') is True, 'zero contextual links need justified exception and report disclosure')
require(not suitable, 'zero-link exception contradicts suitable destinations')
return errors
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('action', choices=['manifest', 'validate'])
parser.add_argument('--source-root', required=True)
parser.add_argument('--packet')
args = parser.parse_args()
if args.action == 'manifest':
print(json.dumps(manifest(args.source_root), ensure_ascii=False, indent=2))
return 0
if not args.packet:
parser.error('validate requires --packet')
path = Path(args.packet)
try:
errors = validate(json.loads(path.read_text()), args.source_root, path.parent)
except (ValueError, OSError, KeyError, TypeError, AttributeError) as exc:
errors = ['invalid/incomplete evidence packet: ' + str(exc)]
print(json.dumps({'result': 'EDITORIAL_EVIDENCE_FAILED' if errors else 'EDITORIAL_EVIDENCE_COMPLETE', 'errors': errors, 'not_approval': 'Independent RD/Vet, SEO/Editorial, final QA, current live identity/relations, backup and public browser verification still mandatory.'}, ensure_ascii=False, indent=2))
return 1 if errors else 0
if __name__ == '__main__':
raise SystemExit(main())
File diff suppressed because it is too large. Load diff
@@ -1,182 +0,0 @@
import {
Controller,
Get,
Post,
Put,
Patch,
Delete,
Body,
Param,
Query,
UseGuards,
Request,
} from "@nestjs/common";
import { Prisma } from "@prisma/client";
import { BlogsService } from "./blogs.service";
import { JwtAuthGuard } from "../auth/jwt-auth.guard";
import {
ApiTags,
ApiBearerAuth,
ApiOperation,
ApiQuery,
} from "@nestjs/swagger";
import { PaginationDto } from "../common/dto/pagination.dto";
import { RolesGuard } from "../common/guards/roles.guard";
import { Roles } from "../common/decorators/roles.decorator";
@ApiTags("Admin - مدیریت مقالات (بلاگ)")
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles("Admin", "SUPER_ADMIN")
@Controller("admin/blogs")
export class BlogsController {
constructor(private readonly blogsService: BlogsService) {}
@Get()
@ApiOperation({ summary: "لیست مقالات" })
@ApiQuery({ name: "page", required: false })
@ApiQuery({ name: "limit", required: false })
@ApiQuery({ name: "search", required: false })
@ApiQuery({ name: "status", required: false })
@ApiQuery({ name: "categoryId", required: false })
async getBlogs(
@Query()
query: PaginationDto & {
status?: string;
categoryId?: string;
tagId?: string;
},
) {
return this.blogsService.getBlogs(query);
}
@Get("authors")
@ApiOperation({ summary: "لیست کاربران برای انتخاب به عنوان نویسنده" })
async getAuthors() {
const data = await this.blogsService.getAuthors();
return { success: true, data };
}
@Get("categories")
@ApiOperation({ summary: "لیست دسته‌بندی‌های وبلاگ" })
async getBlogCategories() {
const data = await this.blogsService.getBlogCategories();
return { success: true, data };
}
@Post("categories")
@ApiOperation({ summary: "ایجاد دسته‌بندی جدید برای وبلاگ" })
async createBlogCategory(@Body() body: Prisma.BlogCategoryCreateInput) {
const data = await this.blogsService.createBlogCategory(body);
return { success: true, data };
}
@Put("categories/:id")
@ApiOperation({ summary: "ویرایش دسته‌بندی وبلاگ" })
async updateBlogCategory(
@Param("id") id: string,
@Body() body: Prisma.BlogCategoryUpdateInput,
) {
const data = await this.blogsService.updateBlogCategory(id, body);
return { success: true, data };
}
@Delete("categories/:id")
@ApiOperation({ summary: "حذف دسته‌بندی وبلاگ" })
async deleteBlogCategory(@Param("id") id: string) {
await this.blogsService.deleteBlogCategory(id);
return { success: true };
}
@Get("tags")
@ApiOperation({ summary: "لیست تگ‌های وبلاگ" })
async getBlogTags() {
const data = await this.blogsService.getBlogTags();
return { success: true, data };
}
@Post("tags")
@ApiOperation({ summary: "ایجاد تگ جدید برای وبلاگ" })
async createBlogTag(@Body() body: { name: string; slug?: string }) {
const data = await this.blogsService.createBlogTag(body);
return { success: true, data };
}
@Delete("tags/:id")
@ApiOperation({ summary: "حذف تگ وبلاگ" })
async deleteBlogTag(@Param("id") id: string) {
await this.blogsService.deleteBlogTag(id);
return { success: true };
}
@Get(":id")
@ApiOperation({ summary: "دریافت مشخصات یک مقاله" })
async getBlogById(@Param("id") id: string) {
const data = await this.blogsService.getBlogById(id);
return { success: true, data };
}
@Post()
@ApiOperation({ summary: "ایجاد مقاله جدید" })
async createBlog(
@Body() body: any,
@Request() req: { user: { id: string } },
) {
const data = await this.blogsService.createBlog(body, req.user.id);
return { success: true, data };
}
@Put(":id")
@ApiOperation({ summary: "ویرایش مقاله" })
async updateBlog(@Param("id") id: string, @Body() body: any) {
const data = await this.blogsService.updateBlog(id, body);
return { success: true, data };
}
@Delete(":id")
@ApiOperation({ summary: "حذف مقاله" })
async deleteBlog(@Param("id") id: string) {
await this.blogsService.deleteBlog(id);
return { success: true };
}
// Blog Comments endpoints
@Get("comments")
@ApiOperation({ summary: "لیست نظرات مقالات" })
@ApiQuery({
name: "status",
required: false,
description: "PENDING | APPROVED | REJECTED",
})
@ApiQuery({ name: "blogId", required: false })
@ApiQuery({ name: "page", required: false })
@ApiQuery({ name: "limit", required: false })
async getComments(
@Query()
query: {
page?: number;
limit?: number;
status?: string;
blogId?: string;
},
) {
return this.blogsService.getComments(query);
}
@Patch("comments/:id/status")
@ApiOperation({ summary: "تغییر وضعیت نظر (تأیید/رد)" })
async updateCommentStatus(
@Param("id") id: string,
@Body() body: { status: "APPROVED" | "REJECTED" },
) {
return this.blogsService.updateCommentStatus(id, body.status);
}
@Delete("comments/:id")
@ApiOperation({ summary: "حذف نظر" })
async deleteComment(@Param("id") id: string) {
await this.blogsService.deleteComment(id);
return { success: true };
}
}
@@ -1,677 +0,0 @@
import {
BadRequestException,
ConflictException,
Injectable,
NotFoundException,
OnModuleInit,
OnModuleDestroy,
Logger,
} from "@nestjs/common";
import { Prisma } from "@prisma/client";
import { PrismaService } from "../prisma/prisma.service";
import { PaginationDto } from "../common/dto/pagination.dto";
import { RevalidationService } from "../common/revalidation/revalidation.service";
@Injectable()
export class BlogsService implements OnModuleInit, OnModuleDestroy {
private readonly logger = new Logger(BlogsService.name);
private schedulerTimer: NodeJS.Timeout | null = null;
constructor(
private prisma: PrismaService,
private revalidationService: RevalidationService,
) {}
async onModuleInit() {
await this.seedDefaultCategories();
// Start background scheduled publishing worker (every 60 seconds)
this.schedulerTimer = setInterval(() => {
this.publishDueScheduledBlogs().catch((err) => {
this.logger.error("Error in publishDueScheduledBlogs worker:", err);
});
}, 60000);
// Run an initial check on startup
this.publishDueScheduledBlogs().catch(() => {});
}
onModuleDestroy() {
if (this.schedulerTimer) {
clearInterval(this.schedulerTimer);
this.schedulerTimer = null;
}
}
private async seedDefaultCategories() {
try {
const defaultCategories = [
{ name: "تغذیه و رژیم غذایی", slug: "nutrition", order: 1 },
{ name: "سلامت گوارش و متابولیسم", slug: "digestion", order: 2 },
{ name: "مفاصل، استخوان و اسکلت", slug: "joints-bones", order: 3 },
{ name: "پوست، مو و آلرژی", slug: "skin-coat", order: 4 },
{
name: "ایمنی، پیشگیری و مراقبت بالینی",
slug: "health-prevention",
order: 5,
},
];
for (const cat of defaultCategories) {
await this.prisma.blogCategory.upsert({
where: { slug: cat.slug },
update: { name: cat.name, order: cat.order },
create: { name: cat.name, slug: cat.slug, order: cat.order },
});
}
} catch (err) {
console.warn(
"[BlogsService] Failed to seed default blog categories:",
err,
);
}
}
async getBlogs(
query: PaginationDto & {
status?: string;
categoryId?: string;
tagId?: string;
},
) {
const page = Number(query.page) || 1;
const limit = Number(query.limit) || 10;
const skip = (page - 1) * limit;
const where: Prisma.BlogWhereInput = {};
if (query.search) {
where.OR = [
{ title: { contains: query.search, mode: "insensitive" } },
{ excerpt: { contains: query.search, mode: "insensitive" } },
{ slug: { contains: query.search, mode: "insensitive" } },
];
}
if (query.status) {
where.status = query.status;
}
if (query.categoryId) {
where.categoryId = query.categoryId;
}
const [data, total] = await Promise.all([
this.prisma.blog.findMany({
where,
skip,
take: limit,
orderBy: { createdAt: "desc" },
include: {
author: {
select: { id: true, firstName: true, lastName: true, email: true },
},
category: { select: { id: true, name: true, slug: true } },
tags: { select: { id: true, name: true, slug: true } },
_count: { select: { comments: true } },
},
}),
this.prisma.blog.count({ where }),
]);
return {
data,
meta: {
total,
page,
limit,
lastPage: Math.ceil(total / limit) || 1,
},
};
}
async getBlogById(id: string) {
const blog = await this.prisma.blog.findUnique({
where: { id },
include: {
author: { select: { id: true, firstName: true, lastName: true } },
category: true,
tags: true,
},
});
if (!blog) throw new NotFoundException("Blog not found");
return blog;
}
async createBlog(data: any, currentUserId: string) {
const {
tagNames,
categoryId,
authorId,
publishedAt,
scheduledAt,
readingTime,
markContentUpdated,
contentUpdatedAt,
} = data;
const parseOptionalDate = (value: unknown, field: string): Date | null => {
if (value === undefined || value === null || value === "") return null;
const parsed = new Date(value as string | number | Date);
if (!Number.isFinite(parsed.getTime())) {
throw new BadRequestException(`Invalid ${field}`);
}
return parsed;
};
const parsedPublishedAt = parseOptionalDate(publishedAt, "publishedAt");
const parsedScheduledAt = parseOptionalDate(scheduledAt, "scheduledAt");
const parsedContentUpdatedAt = markContentUpdated
? new Date()
: parseOptionalDate(contentUpdatedAt, "contentUpdatedAt");
// Connect or create tags
const tagConnectOrCreate = [];
if (Array.isArray(tagNames) && tagNames.length > 0) {
for (const tName of tagNames) {
const trimmed = String(tName).trim();
if (trimmed) {
const slug = trimmed
.toLowerCase()
.replace(/\s+/g, "-")
.replace(/[^\w\u0600-\u06FF-]+/g, "");
tagConnectOrCreate.push({
where: { name: trimmed },
create: { name: trimmed, slug: slug || `tag-${Date.now()}` },
});
}
}
}
const targetAuthorId = authorId || currentUserId;
const now = new Date();
const isFutureSchedule = parsedScheduledAt && parsedScheduledAt > now;
let finalStatus = data.status || "PUBLISHED";
let finalIsPublished =
data.isPublished !== undefined ? data.isPublished : true;
let finalPublishedAt = parsedPublishedAt;
if (isFutureSchedule) {
finalStatus = "SCHEDULED";
finalIsPublished = false;
finalPublishedAt = null;
} else if (parsedScheduledAt && parsedScheduledAt <= now) {
finalStatus = "PUBLISHED";
finalIsPublished = true;
finalPublishedAt = parsedScheduledAt;
} else if (!finalPublishedAt && finalStatus === "PUBLISHED") {
finalPublishedAt = now;
}
const createPayload = {
title: data.title,
slug: data.slug,
content: data.content,
excerpt: data.excerpt,
authorTeam: data.authorTeam,
authorRole: data.authorRole,
imageUrl: data.imageUrl,
imageAlt: data.imageAlt,
imageCaption: data.imageCaption,
metaTitle: data.metaTitle,
metaDescription: data.metaDescription,
keywords: data.keywords,
focusKeywords: data.focusKeywords,
canonicalUrl: data.canonicalUrl,
noIndex: data.noIndex,
noFollow: data.noFollow,
schemaType: data.schemaType,
ogTitle: data.ogTitle,
ogDescription: data.ogDescription,
ogImage: data.ogImage,
status: finalStatus,
isPublished: finalIsPublished,
featured: data.featured,
allowComments: data.allowComments,
showOnHome: data.showOnHome,
homeDisplayMode: data.homeDisplayMode,
relatedProductIds: data.relatedProductIds,
relatedBlogIds: data.relatedBlogIds,
readingTime: readingTime !== undefined ? Number(readingTime) : 3,
publishedAt: finalPublishedAt,
scheduledAt: parsedScheduledAt,
contentUpdatedAt: parsedContentUpdatedAt,
author: { connect: { id: targetAuthorId } },
...(categoryId ? { category: { connect: { id: categoryId } } } : {}),
...(tagConnectOrCreate.length > 0
? { tags: { connectOrCreate: tagConnectOrCreate } }
: {}),
} satisfies Prisma.BlogCreateInput;
const created = await this.prisma.blog.create({
data: createPayload,
include: {
author: { select: { firstName: true, lastName: true } },
category: true,
tags: true,
},
});
let cacheRevalidation: "success" | "failed" = "failed";
try {
const revalidated = await this.revalidationService.revalidateBlog(created.slug);
cacheRevalidation = revalidated ? "success" : "failed";
} catch (err: any) {
this.logger.warn(
`Cache revalidation failed for created blog [${created.slug}]: ${err?.message}`,
);
cacheRevalidation = "failed";
}
return {
...created,
cacheRevalidation,
};
}
async updateBlog(id: string, data: any) {
const blog = await this.prisma.blog.findUnique({ where: { id } });
if (!blog) throw new NotFoundException("Blog not found");
const {
tagNames,
categoryId,
authorId,
publishedAt,
scheduledAt,
readingTime,
markContentUpdated,
contentUpdatedAt,
expectedUpdatedAt,
} = data;
const conflict = () => new ConflictException(
"CONCURRENT_MODIFICATION: Blog has been modified by another process",
);
let expectedTime: number | undefined;
if (expectedUpdatedAt !== undefined) {
// Only accept the millisecond, timezone-qualified ISO token returned by GET
// (or an equivalent offset). Avoid Date's coercion of null, numbers, arrays,
// and its normalization of impossible calendar dates.
const match = typeof expectedUpdatedAt === "string" && expectedUpdatedAt.match(
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,3}))?(Z|[+-]\d{2}:\d{2})$/,
);
if (!match) throw conflict();
expectedTime = Date.parse(expectedUpdatedAt);
const calendar = new Date(`${match[1]}-${match[2]}-${match[3]}T00:00:00.000Z`);
if (!Number.isFinite(expectedTime) ||
calendar.toISOString().slice(0, 10) !== `${match[1]}-${match[2]}-${match[3]}` ||
Number(match[4]) > 23 || Number(match[5]) > 59 || Number(match[6]) > 59) {
throw conflict();
}
}
const mutableScalarFields = [
"title",
"slug",
"content",
"excerpt",
"authorTeam",
"authorRole",
"imageUrl",
"imageAlt",
"imageCaption",
"metaTitle",
"metaDescription",
"keywords",
"focusKeywords",
"canonicalUrl",
"noIndex",
"noFollow",
"schemaType",
"ogTitle",
"ogDescription",
"ogImage",
"status",
"isPublished",
"featured",
"allowComments",
"showOnHome",
"homeDisplayMode",
"relatedProductIds",
"relatedBlogIds",
] as const;
const updatePayload: Prisma.BlogUpdateInput = {};
for (const field of mutableScalarFields) {
if (data[field] !== undefined) {
(updatePayload as Record<string, unknown>)[field] = data[field];
}
}
const now = new Date();
const materialContentFields = [
"title",
"content",
"excerpt",
"imageUrl",
"imageAlt",
"imageCaption",
] as const;
const hasMaterialContentChange = materialContentFields.some(
(field) => data[field] !== undefined && data[field] !== blog[field],
);
if (markContentUpdated || (contentUpdatedAt === undefined && hasMaterialContentChange)) {
(updatePayload as Record<string, unknown>).contentUpdatedAt = now;
} else if (contentUpdatedAt !== undefined) {
if (contentUpdatedAt === null || contentUpdatedAt === "") {
(updatePayload as Record<string, unknown>).contentUpdatedAt = null;
} else {
const parsedContentUpdatedAt = new Date(contentUpdatedAt);
if (!Number.isFinite(parsedContentUpdatedAt.getTime())) {
throw new BadRequestException("Invalid contentUpdatedAt");
}
(updatePayload as Record<string, unknown>).contentUpdatedAt = parsedContentUpdatedAt;
}
}
Object.assign(updatePayload, {
...(readingTime !== undefined
? { readingTime: Number(readingTime) }
: {}),
...(readingTime !== undefined
? { readingTime: Number(readingTime) }
: {}),
...(authorId ? { author: { connect: { id: authorId } } } : {}),
...(categoryId !== undefined
? categoryId
? { category: { connect: { id: categoryId } } }
: { category: { disconnect: true } }
: {}),
});
if (scheduledAt !== undefined) {
const parsedScheduledAt = scheduledAt ? new Date(scheduledAt) : null;
updatePayload.scheduledAt = parsedScheduledAt;
if (parsedScheduledAt && parsedScheduledAt > now) {
updatePayload.status = "SCHEDULED";
updatePayload.isPublished = false;
updatePayload.publishedAt = null;
} else if (parsedScheduledAt && parsedScheduledAt <= now) {
updatePayload.status = "PUBLISHED";
updatePayload.isPublished = true;
updatePayload.publishedAt = parsedScheduledAt;
}
} else if (publishedAt !== undefined) {
updatePayload.publishedAt = publishedAt ? new Date(publishedAt) : null;
}
if (Array.isArray(tagNames)) {
const tagConnectOrCreate = [];
for (const tName of tagNames) {
const trimmed = String(tName).trim();
if (trimmed) {
const slug = trimmed
.toLowerCase()
.replace(/\s+/g, "-")
.replace(/[^\w\u0600-\u06FF-]+/g, "");
tagConnectOrCreate.push({
where: { name: trimmed },
create: { name: trimmed, slug: slug || `tag-${Date.now()}` },
});
}
}
updatePayload.tags = {
set: [], // clear previous relations
connectOrCreate: tagConnectOrCreate,
};
}
const include = {
author: { select: { firstName: true, lastName: true } },
category: true,
tags: true,
};
// Legacy UI requests without a token intentionally retain last-write-wins.
// Guarded requests claim the row using a DB predicate, not the earlier read.
const updated = expectedTime === undefined
? await this.prisma.blog.update({ where: { id }, data: updatePayload, include })
: await this.prisma.$transaction(async (tx) => {
// PostgreSQL timestamptz defaults to microseconds; JSON/JS Dates expose
// milliseconds. Match the entire represented millisecond, then move
// strictly beyond it (including same-ms writes and clock rollback).
const claimedAt = new Date(Math.max(Date.now(), expectedTime + 1));
const claim = await tx.blog.updateMany({
where: {
id,
updatedAt: {
gte: new Date(expectedTime),
lt: new Date(expectedTime + 1),
},
},
data: { updatedAt: claimedAt },
});
if (claim.count !== 1) throw conflict();
// The claim holds the row lock until this nested mutation commits.
// Any relation failure rolls back both the claim and all data writes.
return tx.blog.update({
where: { id },
data: { ...updatePayload, updatedAt: claimedAt },
include,
});
});
let cacheRevalidation: "success" | "failed" = "failed";
try {
const revalidated = await this.revalidationService.revalidateBlog(updated.slug);
cacheRevalidation = revalidated ? "success" : "failed";
} catch (err: any) {
this.logger.warn(
`Cache revalidation failed for updated blog [${updated.slug}]: ${err?.message}`,
);
cacheRevalidation = "failed";
}
return {
...updated,
cacheRevalidation,
};
}
async deleteBlog(id: string) {
const blog = await this.prisma.blog.findUnique({ where: { id } });
if (!blog) throw new NotFoundException("Blog not found");
const res = await this.prisma.blog.delete({ where: { id } });
try {
await this.revalidationService.revalidateBlog(blog.slug);
} catch (err: any) {
this.logger.warn(
`Cache revalidation failed for deleted blog [${blog.slug}]: ${err?.message}`,
);
}
return res;
}
// Authors (Users who can write)
async getAuthors() {
return this.prisma.user.findMany({
select: {
id: true,
firstName: true,
lastName: true,
email: true,
role: true,
},
orderBy: { firstName: "asc" },
});
}
// Blog Categories Management
async getBlogCategories() {
return this.prisma.blogCategory.findMany({
orderBy: { order: "asc" },
include: {
parent: true,
children: true,
_count: { select: { blogs: true } },
},
});
}
async createBlogCategory(data: Prisma.BlogCategoryCreateInput) {
return this.prisma.blogCategory.create({ data });
}
async updateBlogCategory(id: string, data: Prisma.BlogCategoryUpdateInput) {
return this.prisma.blogCategory.update({ where: { id }, data });
}
async deleteBlogCategory(id: string) {
return this.prisma.blogCategory.delete({ where: { id } });
}
// Blog Tags Management
async getBlogTags() {
return this.prisma.blogTag.findMany({
orderBy: { createdAt: "desc" },
include: {
_count: { select: { blogs: true } },
},
});
}
async createBlogTag(data: { name: string; slug?: string }) {
const slug =
data.slug ||
data.name
.toLowerCase()
.replace(/\s+/g, "-")
.replace(/[^\w\u0600-\u06FF-]+/g, "");
return this.prisma.blogTag.create({
data: { name: data.name, slug: slug || `tag-${Date.now()}` },
});
}
async deleteBlogTag(id: string) {
return this.prisma.blogTag.delete({ where: { id } });
}
// Blog Comments Management
async getComments(query: {
page?: number;
limit?: number;
status?: string;
blogId?: string;
}) {
const page = Number(query.page) || 1;
const limit = Number(query.limit) || 20;
const skip = (page - 1) * limit;
const where: Prisma.BlogCommentWhereInput = {};
if (query.status) where.status = query.status;
if (query.blogId) where.blogId = query.blogId;
const [data, total] = await Promise.all([
this.prisma.blogComment.findMany({
where,
skip,
take: limit,
orderBy: { createdAt: "desc" },
include: {
blog: { select: { title: true, slug: true } },
user: { select: { firstName: true, lastName: true, mobile: true } },
},
}),
this.prisma.blogComment.count({ where }),
]);
return {
data,
meta: { total, page, limit, lastPage: Math.ceil(total / limit) || 1 },
};
}
async updateCommentStatus(id: string, status: "APPROVED" | "REJECTED") {
const comment = await this.prisma.blogComment.findUnique({
where: { id },
include: { blog: { select: { slug: true } } },
});
if (!comment) throw new NotFoundException("Comment not found");
const updated = await this.prisma.blogComment.update({
where: { id },
data: { status },
});
if (comment.blog?.slug) {
await this.revalidationService.revalidateBlog(comment.blog.slug);
}
return updated;
}
async deleteComment(id: string) {
const comment = await this.prisma.blogComment.findUnique({
where: { id },
include: { blog: { select: { slug: true } } },
});
if (!comment) throw new NotFoundException("Comment not found");
const res = await this.prisma.blogComment.delete({ where: { id } });
if (comment.blog?.slug) {
await this.revalidationService.revalidateBlog(comment.blog.slug);
}
return res;
}
/**
* Publishes blogs that have reached their scheduled publication date
*/
async publishDueScheduledBlogs(): Promise<number> {
const now = new Date();
const dueBlogs = await this.prisma.blog.findMany({
where: {
status: "SCHEDULED",
scheduledAt: { lte: now },
},
select: {
id: true,
slug: true,
title: true,
scheduledAt: true,
},
});
if (!dueBlogs || dueBlogs.length === 0) {
return 0;
}
this.logger.log(
`Found ${dueBlogs.length} due scheduled blog(s) to publish automatically.`,
);
let publishedCount = 0;
for (const blog of dueBlogs) {
try {
const publishDate = blog.scheduledAt || now;
await this.prisma.blog.update({
where: { id: blog.id },
data: {
status: "PUBLISHED",
isPublished: true,
publishedAt: publishDate,
},
});
publishedCount++;
this.logger.log(
`Successfully published scheduled blog [${blog.title}] (${blog.slug})`,
);
// Immediate cache & sitemap revalidation
await this.revalidationService.revalidateBlog(blog.slug);
} catch (err: any) {
this.logger.error(
`Failed to publish scheduled blog [${blog.id}] (${blog.slug}): ${err?.message}`,
);
}
}
return publishedCount;
}
}
File diff suppressed because it is too large. Load diff
@@ -1,157 +0,0 @@
import type { AxiosError,InternalAxiosRequestConfig } from 'axios';
import axios from 'axios';
import toast from 'react-hot-toast';
import { useAdminAuthStore } from '../store/adminAuthStore';
import type { ApiResponse,AuthResponseData } from '../types/admin';
export const BASE_DOMAIN = import.meta.env.VITE_API_URL
? import.meta.env.VITE_API_URL.replace(/\/api$/, '')
: (import.meta.env.DEV ? 'http://127.0.0.1:4400' : '');
const baseURL = `${BASE_DOMAIN}/api`;
export interface ApiErrorPayload {
success: boolean;
statusCode: number;
message: string;
code: string;
details?: Array<{ field: string; message: string }> | Record<string, unknown>;
timestamp?: string;
path?: string;
}
const api = axios.create({
baseURL,
withCredentials: true,
headers: {
'Content-Type': 'application/json',
},
});
let isRefreshing = false;
let failedQueue: Array<{
resolve: (token: string) => void;
reject: (reason?: unknown) => void;
}> = [];
const processQueue = (error: unknown, token: string | null = null) => {
failedQueue.forEach((prom) => {
if (error) {
prom.reject(error);
} else if (token) {
prom.resolve(token);
}
});
failedQueue = [];
};
api.interceptors.request.use((config: InternalAxiosRequestConfig) => {
const token = useAdminAuthStore.getState().accessToken;
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
api.interceptors.response.use(
(response) => response,
async (error: AxiosError<ApiErrorPayload>) => {
const originalRequest = error.config as InternalAxiosRequestConfig & { _retry?: boolean };
const responseData = error.response?.data;
const status = error.response?.status;
const farsiMessage = responseData?.message || 'خطایی در ارتباط با سرور رخ داده است.';
if (status === 401 && originalRequest && !originalRequest._retry) {
const currentToken = useAdminAuthStore.getState().accessToken;
if (!currentToken || originalRequest.url?.includes('/auth/refresh') || originalRequest.url?.includes('/auth/admin-login') || originalRequest.url?.includes('/auth/verify-otp')) {
useAdminAuthStore.getState().clearAuth();
if (window.location.pathname !== '/login') {
toast.error('نشست کاری شما منقضی شده است. لطفاً مجدداً وارد شوید.');
window.location.href = '/login';
}
return Promise.reject(error);
}
if (isRefreshing) {
return new Promise((resolve, reject) => {
failedQueue.push({ resolve, reject });
})
.then((token) => {
originalRequest.headers.Authorization = `Bearer ${token}`;
return api(originalRequest);
})
.catch((err) => Promise.reject(err));
}
originalRequest._retry = true;
isRefreshing = true;
try {
let refreshResponse;
try {
refreshResponse = await axios.post<ApiResponse<AuthResponseData>>(
`${baseURL}/auth/refresh`,
{},
{
withCredentials: true,
headers: currentToken ? { Authorization: `Bearer ${currentToken}` } : {}
}
);
} catch {
// If bearer-based refresh fails (e.g. access token fully expired), try cookie-based /auth/refresh-token
refreshResponse = await axios.post<ApiResponse<AuthResponseData>>(
`${baseURL}/auth/refresh-token`,
{},
{ withCredentials: true }
);
}
if (refreshResponse.data?.success && refreshResponse.data.data?.accessToken) {
const newToken = refreshResponse.data.data.accessToken;
const user = refreshResponse.data.data.user;
useAdminAuthStore.getState().setAuth(newToken, user);
processQueue(null, newToken);
originalRequest.headers.Authorization = `Bearer ${newToken}`;
return api(originalRequest);
} else {
throw new Error('Refresh failed');
}
} catch (refreshErr) {
processQueue(refreshErr, null);
useAdminAuthStore.getState().clearAuth();
if (window.location.pathname !== '/login') {
toast.error('نشست کاری شما منقضی شده است. لطفاً مجدداً وارد شوید.');
window.location.href = '/login';
}
return Promise.reject(refreshErr);
} finally {
isRefreshing = false;
}
}
if (status === 403) {
toast.error('دسترسی شما به این بخش از پنل ادمین مجاز نمی‌باشد.', { id: 'admin-403' });
(error as unknown as Record<string, boolean>)._toastShown = true;
} else if (status !== 401 && !(originalRequest as unknown as Record<string, boolean>)?.hideErrorToast) {
let fullMessage = farsiMessage;
if (responseData?.details && Array.isArray(responseData.details) && responseData.details.length > 0) {
const detailStrings = responseData.details
.map((d: unknown) => typeof d === 'string' ? d : ((d as Record<string, string>)?.message || (d as Record<string, string>)?.field))
.filter(Boolean);
if (detailStrings.length > 0) {
const joinedDetails = detailStrings.join('، ');
if (!farsiMessage.includes(joinedDetails)) {
fullMessage = `${farsiMessage} (${joinedDetails})`;
}
}
}
toast.error(fullMessage, { id: fullMessage });
(error as unknown as Record<string, boolean>)._toastShown = true;
}
return Promise.reject(error);
}
);
export default api;
@@ -1,72 +0,0 @@
#!/usr/bin/env python3
"""Exact one-file publisher release; called only by narrow CI, no site deployment."""
import argparse
import hashlib
import fcntl
import json
import os
from pathlib import Path
import re
import subprocess
import tempfile
TARGET = Path('/opt/data/publishers/e4c9b52eb040/editorial_evidence.py')
OLD = '99539a31b498d2fc922650a9971802e2caa077b6eb165f864f50e2c6c9073792'
def install(source, commit, approval, target=TARGET):
lock = target.parent / '.helper-release.lock'
fd = os.open(lock, os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600)
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
return _install(source, commit, approval, target)
finally:
os.close(fd)
def _install(source, commit, approval, target):
digest = hashlib.sha256(source).hexdigest()
if not re.fullmatch('[0-9a-f]{40}', commit) or approval != digest:
raise ValueError('Reviewed content digest required')
for p in (target, *target.parents):
if p.is_symlink():
raise ValueError('Symlink refused')
st = target.stat()
if st.st_uid != os.getuid() or st.st_nlink != 1:
raise ValueError('Runtime identity or hardlink mismatch')
current = hashlib.sha256(target.read_bytes()).hexdigest()
if current not in (OLD, digest):
raise ValueError('Unreviewed before-image')
compile(source, str(target), 'exec')
fd, name = tempfile.mkstemp(prefix='.editorial-release-', dir=target.parent)
try:
with os.fdopen(fd, 'wb') as f:
f.write(source)
f.flush()
os.fsync(f.fileno())
os.fchmod(f.fileno(), st.st_mode & 0o777)
if target.stat().st_ino != st.st_ino or hashlib.sha256(target.read_bytes()).hexdigest() != current:
raise ValueError('Concurrent before-image change')
os.replace(name, target)
finally:
if os.path.exists(name):
os.unlink(name)
actual = hashlib.sha256(target.read_bytes()).hexdigest()
if actual != digest:
raise ValueError('Readback mismatch')
return {'commit': commit, 'sha256': actual, 'target': str(target), 'site_deploy': False, 'gateway_restart': False}
def main():
p = argparse.ArgumentParser()
p.add_argument('--sha', required=True)
p.add_argument('--approval', required=True)
args = p.parse_args()
if os.environ.get('GITHUB_ACTIONS') != 'true' or os.environ.get('GITHUB_REF') != 'refs/heads/ops/prod-publisher-helper-20261112':
raise ValueError('Exact CI push route required')
root = Path(__file__).parent
source = (root/'editorial_evidence.py').read_bytes()
package = {'code': Path(__file__).read_text(), 'source': source.decode(), 'commit': args.sha, 'approval': args.approval}
code = "import json,sys; p=json.load(sys.stdin); ns={'__name__':'ci_release'}; exec(compile(p['code'],'<reviewed-ci-release>','exec'),ns); print(json.dumps(ns['install'](p['source'].encode(),p['commit'],p['approval'])))"
result = subprocess.check_output(['docker','exec','--user','10000:10000','-i','canina-hermes','/opt/hermes/.venv/bin/python','-B','-c',code], input=json.dumps(package), text=True, timeout=60)
print(result.strip())
if __name__ == '__main__':
main()
@@ -1,145 +0,0 @@
#!/usr/bin/env python3
"""Offline synthetic unit fixtures only: never publication evidence."""
import copy
import json
from pathlib import Path
import tempfile
import unittest
from editorial_evidence import manifest, sha, validate
# Frozen publisher-owned contract fixture; not deployment/publication evidence.
# Override explicitly when testing a maintained source checkout. Never depend on
# a deleted scratch worktree. All generated evidence remains in self.base.
import os
ROOT = os.environ.get('CANINA_EDITORIAL_TEST_SOURCE', str(
Path(__file__).parent / 'runs/20261104/content/source'))
class EditorialEvidenceTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory(dir='/opt/data/cache/scratch')
self.addCleanup(self.temp.cleanup)
self.base = Path(self.temp.name)
self.m = manifest(ROOT)
self.payload = {'title': 'SYNTHETIC UNIT TEST ONLY', 'content': '<p>test</p>', 'authorId': 'test-author', 'categoryId': 'test-category', 'tagNames': [], 'relatedBlogIds': [], 'relatedProductIds': []}
(self.base / 'payload.json').write_text(json.dumps(self.payload))
self.packet = {
'action': 'NEW', 'manifest': self.m, 'payload': self.payload,
'artifact': {'path': 'payload.json', 'sha256': sha(self.base / 'payload.json')},
'artifact_evidence': ['test'],
'evidence': {'test': {'path': 'payload.json', 'sha256': sha(self.base / 'payload.json'), 'readAt': 'UNIT_TEST_NOT_A_LIVE_READ'}},
'field_dispositions': {k: {'disposition': 'system_derived' if v['kind'] == 'system' else 'verified_value' if k in self.payload else 'not_applicable', 'reason': 'synthetic unit fixture only', 'evidence': ['test'], 'value': self.payload.get(k)} for k, v in self.m['fields'].items()},
'relations': {k: {'selected': self.payload[k], 'reason': 'synthetic unit fixture only', 'evidence': ['test'], 'status': 200, 'authenticated': True, 'inventory_complete': True, 'available': []} for k in ['authorId', 'categoryId', 'tagNames', 'relatedBlogIds', 'relatedProductIds']},
'internal_links': {'reviewed_destinations': [{'href': '/blog/test', 'identity_verified': True, 'content_reviewed': True, 'suitable': False, 'reason': 'synthetic medically unsuitable destination', 'evidence': ['test']}], 'selected': [], 'zero_link_reason': 'reviewed destination unsuitable', 'disclose_in_report': True},
}
def sync_artifact(self):
(self.base / 'payload.json').write_text(json.dumps(self.payload))
digest = sha(self.base / 'payload.json')
self.packet['artifact']['sha256'] = digest
self.packet['evidence']['test']['sha256'] = digest
def errors(self):
return validate(self.packet, ROOT, self.base)
def test_transport_control_cannot_claim_fetched_value(self):
self.payload['markContentUpdated'] = False
self.packet['field_dispositions']['markContentUpdated'].update(disposition='verified_value', value=False)
self.sync_artifact()
self.assertTrue(any('transport control is not a fetched scalar' in e for e in self.errors()))
def test_acceptance_requires_readback_binding(self):
self.packet.update(action='READ_ONLY_ACCEPTANCE', record_id='test-id')
self.assertTrue(any('acceptance_readback' in e for e in self.errors()))
def test_acceptance_absent_comments_rejects_invented_include(self):
self.packet.update(action='READ_ONLY_ACCEPTANCE', record_id='test-id', acceptance_readback={'evidence':'readback'})
record = {'id':'test-id', 'tags':[]}
(self.base/'readback.json').write_text(json.dumps({'status':200,'body':{'data':record}}))
self.packet['evidence']['readback']={'path':'readback.json','sha256':sha(self.base/'readback.json'),'readAt':'SYNTHETIC_ONLY'}
self.packet['field_dispositions']['comments'].update(disposition='system_derived',value=None)
self.assertTrue(any('comments: absent from fetched record' in e for e in self.errors()))
def test_verified_value_requires_value(self):
del self.packet['field_dispositions']['title']['value']
self.assertTrue(any('explicit value' in e for e in self.errors()))
def test_omitted_verified_writable_value_rejected(self):
self.packet['field_dispositions']['ogImage']['disposition'] = 'verified_value'
self.assertTrue(any('must be in payload' in e for e in self.errors()))
def test_update_identity_required(self):
self.packet['action'] = 'UPDATE'
self.assertTrue(any('requires record_id' in e for e in self.errors()))
def test_related_membership_with_coherent_payload(self):
self.payload['relatedBlogIds'] = ['missing']
self.packet['relations']['relatedBlogIds']['selected'] = ['missing']
self.packet['field_dispositions']['relatedBlogIds']['value'] = ['missing']
self.sync_artifact()
self.assertTrue(any('absent from verified inventory' in e for e in self.errors()))
def test_valid_selected_body_link(self):
self.payload['content'] = '<a href="/blog/test">test</a>'
self.packet['field_dispositions']['content']['value'] = self.payload['content']
self.packet['internal_links']['selected'] = ['/blog/test']
self.packet['internal_links']['reviewed_destinations'][0]['suitable'] = True
self.sync_artifact()
self.assertEqual([], self.errors())
def test_missing_action_rejected(self):
del self.packet['action']
self.assertTrue(any('explicit action' in e for e in self.errors()))
def test_complete_structure_is_not_publication_approval(self):
self.assertEqual([], self.errors())
def test_manifest_includes_all_43_scalars_relations_controls(self):
self.assertIn('homeDisplayMode', self.m['fields'])
self.assertIn('comments', self.m['fields'])
self.assertIn('markContentUpdated', self.m['fields'])
self.assertIn('tagNames', self.m['fields'])
def test_acceptance_missing_manifest_is_rejected(self):
self.packet['manifest'] = {}
self.assertTrue(self.errors())
def test_missing_disposition_rejected(self):
del self.packet['field_dispositions']['ogImage']
self.assertTrue(self.errors())
def test_wrong_transport_tagIds_rejected(self):
self.payload['tagIds'] = []
self.assertTrue(any('payload keys' in e for e in self.errors()))
def test_unjustified_empty_taxonomy_rejected(self):
for key in ['tagNames', 'relatedBlogIds', 'relatedProductIds']:
packet = copy.deepcopy(self.packet)
self.packet['relations'][key]['reason'] = ''
self.assertTrue(self.errors())
self.packet = packet
def test_missing_authenticated_tag_inventory_rejected(self):
self.packet['relations']['tagNames']['authenticated'] = False
self.assertTrue(self.errors())
def test_nonexisting_related_id_rejected(self):
self.packet['relations']['relatedBlogIds']['selected'] = ['missing']
self.assertTrue(self.errors())
def test_automatic_cards_do_not_replace_body_links(self):
self.packet['internal_links'] = {}
self.assertTrue(self.errors())
def test_zero_link_disclosure_required(self):
self.packet['internal_links']['disclose_in_report'] = False
self.assertTrue(self.errors())
def test_reject_zero_when_suitable_destination_exists(self):
self.packet['internal_links']['reviewed_destinations'][0]['suitable'] = True
self.assertTrue(self.errors())
def test_http_success_not_identity(self):
self.packet['internal_links']['reviewed_destinations'][0]['identity_verified'] = False
self.assertTrue(self.errors())
def test_tampered_artifact_rejected(self):
(self.base / 'payload.json').write_text('{}')
self.assertTrue(self.errors())
def test_unreviewed_body_link_rejected(self):
self.payload['content'] = '<a href="/blog/unreviewed">test</a>'
self.assertTrue(any('contextual body links' in e for e in self.errors()))
def test_missing_evidence_rejected(self):
self.packet['evidence'] = {}
self.assertTrue(self.errors())
def test_false_and_null_are_values_not_missing(self):
self.payload['noIndex'] = False
self.payload['scheduledAt'] = None
for k in ['noIndex', 'scheduledAt']:
self.packet['field_dispositions'][k]['value'] = self.payload[k]
self.packet['field_dispositions'][k]['disposition'] = 'verified_value'
(self.base / 'payload.json').write_text(json.dumps(self.payload))
digest = sha(self.base / 'payload.json')
self.packet['artifact']['sha256'] = digest
self.packet['evidence']['test']['sha256'] = digest
self.assertEqual([], self.errors())
if __name__ == '__main__':
unittest.main(verbosity=2)
@@ -1,16 +0,0 @@
"""Offline regression only; never publication evidence."""
import os
import unittest
from editorial_evidence import manifest
class ManifestTransportTests(unittest.TestCase):
def test_update_only_expected_updated_at_is_explicit_request_control(self):
fields = manifest(os.environ['CANINA_EDITORIAL_TEST_SOURCE'])['fields']
self.assertIn('expectedUpdatedAt', fields)
self.assertEqual(fields['expectedUpdatedAt'], {
'type': 'String', 'kind': 'transport_control',
'actions': ['UPDATE'], 'persisted': False,
})
if __name__ == '__main__':
unittest.main()
@@ -1,136 +0,0 @@
"""Offline release security regressions; all writes stay in temporary fixtures.
Acceptance: reviewed old/idempotent bytes install; unknown bytes, wrong digest,
lock contention and changes at either side of the final check fail closed.
These are post-implementation independent tests, not a TDD claim.
"""
import fcntl
import hashlib
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
from unittest import mock
import release_helper as helper
class ReleaseHelperTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory(prefix='helper-review-')
self.addCleanup(self.tmp.cleanup)
self.target = Path(self.tmp.name) / 'editorial_evidence.py'
self.old = b'# isolated reviewed beforeimage\n'
self.source = b'# isolated reviewed replacement\nvalue = 1\n'
self.target.write_bytes(self.old)
self.target.chmod(0o640)
self.approval = hashlib.sha256(self.source).hexdigest()
self.sha = 'a' * 40
patcher = mock.patch.object(helper, 'OLD', hashlib.sha256(self.old).hexdigest())
patcher.start()
self.addCleanup(patcher.stop)
def install(self, **kw):
return helper.install(self.source, kw.get('sha', self.sha),
kw.get('approval', self.approval), self.target)
def assert_no_temporary_release(self):
self.assertEqual(list(self.target.parent.glob('.editorial-release-*')), [])
def test_reviewed_install_and_idempotency_preserve_mode(self):
for _ in range(2):
result = self.install()
self.assertEqual(result['sha256'], self.approval)
self.assertFalse(result['site_deploy'])
self.assertFalse(result['gateway_restart'])
self.assertEqual(self.target.read_bytes(), self.source)
self.assertEqual(self.target.stat().st_mode & 0o777, 0o640)
self.assert_no_temporary_release()
def test_unknown_beforeimage_rejected(self):
self.target.write_bytes(b'unreviewed')
with self.assertRaisesRegex(ValueError, 'Unreviewed before-image'):
self.install()
self.assertEqual(self.target.read_bytes(), b'unreviewed')
self.assert_no_temporary_release()
def test_approval_mismatch_rejected(self):
with self.assertRaisesRegex(ValueError, 'Reviewed content digest required'):
self.install(approval='0' * 64)
self.assertEqual(self.target.read_bytes(), self.old)
def test_malformed_commit_rejected(self):
with self.assertRaises(ValueError):
self.install(sha='not-a-sha')
self.assertEqual(self.target.read_bytes(), self.old)
def test_contention_from_separate_process_rejected(self):
lock = self.target.parent / '.helper-release.lock'
code = ('import fcntl,sys; f=open(sys.argv[1],"a"); '
'fcntl.flock(f,fcntl.LOCK_EX); print("locked",flush=True); sys.stdin.read()')
process = subprocess.Popen([sys.executable, '-c', code, str(lock)],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
try:
assert process.stdout is not None
self.assertEqual(process.stdout.readline().strip(), 'locked')
with self.assertRaises(BlockingIOError):
self.install()
self.assertEqual(self.target.read_bytes(), self.old)
finally:
process.communicate(input='', timeout=5)
self.assertEqual(process.returncode, 0)
def test_change_during_staging_rejected_before_replace(self):
real_fsync = os.fsync
def mutate(fd):
real_fsync(fd)
self.target.write_bytes(b'concurrent edit')
with mock.patch.object(helper.os, 'fsync', side_effect=mutate):
with self.assertRaisesRegex(ValueError, 'Concurrent before-image change'):
self.install()
self.assertEqual(self.target.read_bytes(), b'concurrent edit')
self.assert_no_temporary_release()
def test_inode_swap_during_staging_rejected(self):
real_fsync = os.fsync
def mutate(fd):
real_fsync(fd)
alternate = self.target.parent / 'alternate'
alternate.write_bytes(self.old)
os.replace(alternate, self.target)
with mock.patch.object(helper.os, 'fsync', side_effect=mutate):
with self.assertRaisesRegex(ValueError, 'Concurrent before-image change'):
self.install()
self.assertEqual(self.target.read_bytes(), self.old)
self.assert_no_temporary_release()
def test_change_after_final_check_before_replace_must_not_be_erased(self):
real_replace = os.replace
def mutate_then_replace(src, dst):
self.target.write_bytes(b'late concurrent edit')
return real_replace(src, dst)
with mock.patch.object(helper.os, 'replace', side_effect=mutate_then_replace):
with self.assertRaisesRegex(ValueError, 'Concurrent before-image change'):
self.install()
self.assertEqual(self.target.read_bytes(), b'late concurrent edit')
self.assert_no_temporary_release()
def test_symlink_rejected(self):
actual = self.target.parent / 'actual'
self.target.rename(actual)
self.target.symlink_to(actual)
with self.assertRaisesRegex(ValueError, 'Symlink refused'):
self.install()
self.assertEqual(actual.read_bytes(), self.old)
def test_hardlink_rejected(self):
os.link(self.target, self.target.parent / 'hardlink')
with self.assertRaisesRegex(ValueError, 'hardlink mismatch'):
self.install()
self.assertEqual(self.target.read_bytes(), self.old)
if __name__ == '__main__':
unittest.main()
+165
View File
@@ -0,0 +1,165 @@
#!/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</host-canina/stage-backend-deploy.lock
flock -n 9 || exit 1
printf "CANINA_STAGE_DEPLOY_LOCK_ACQUIRED\n"
while :; do sleep 60; done
') || { printf 'Stage deployment locked or helper creation failed\n' >&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
# Pin the existing frontend solely to retain operational nginx/admin artifacts.
STAGE_BASE_IMAGE="canina-stage-blog-base:$DEPLOY_SHA"
docker tag "$(docker inspect -f '{{.Image}}' canino_frontend_stage)" "$STAGE_BASE_IMAGE"
export TARGET_DIR DEPLOY_SHA STAGE_BASE_IMAGE
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))
# Preserve operational runtime/env files; override only the incorrect Stage route.
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']}
service['environment']={'FRONTEND_INTERNAL_URL':'http://frontend_stage:3000'}
frontend={'build':{'context':str(root),'dockerfile':'scripts/Dockerfile.blog-stage','args':{'STAGE_BASE_IMAGE':os.environ['STAGE_BASE_IMAGE']},'labels':{'org.opencontainers.image.revision':sha}},'labels':{'ir.canina.release.sha':sha}}
(root/'release.override.json').write_text(json.dumps({'services':{'backend_stage':service,'frontend_stage':frontend}}))
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 frontend_stage
check_stage_lock
# Fail closed without exposing either service's shared secret.
python3 - <<'PY'
import json, subprocess
containers=json.loads(subprocess.check_output(['docker','inspect','canino_backend_stage','canino_frontend_stage']))
envs=[dict(v.split('=',1) for v in c['Config']['Env']) for c in containers]
assert envs[0].get('REVALIDATION_SECRET') and envs[0]['REVALIDATION_SECRET']==envs[1].get('REVALIDATION_SECRET'), 'Stage revalidation secret absent or mismatched'
PY
# No migrations, seed, DB/Redis/bot recreation or host runtime file edits.
timeout --foreground --kill-after=2s 120 "${compose[@]}" up -d --no-deps --force-recreate backend_stage frontend_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" ]]
# Exercise the real consumer-to-Next route without resaving any article.
for attempt in {1..30}; do
if timeout --foreground --kill-after=2s 15 docker exec canino_backend_stage node -e '
const {RevalidationService}=require("./dist/common/revalidation/revalidation.service");
(async()=>{const s=new RevalidationService(); for(const tag of ["blogs","home-data","blog-senior-dog-l-carnitine-myocardial-energy-evidence"]) if(!await s.revalidateTag(tag)) process.exit(1); for(const path of ["/blog","/blog/senior-dog-l-carnitine-myocardial-energy-evidence"]) if(!await s.revalidatePath(path)) process.exit(1);})().catch(()=>process.exit(1));'; then break; fi
[[ $attempt != 30 ]] || exit 1
sleep 2
done
[[ $(docker inspect -f '{{index .Config.Labels "ir.canina.release.sha"}}' canino_frontend_stage) == "$DEPLOY_SHA" ]]
frontend_image=$(docker inspect -f '{{.Image}}' canino_frontend_stage)
[[ $(docker image inspect -f '{{index .Config.Labels "org.opencontainers.image.revision"}}' "$frontend_image") == "$DEPLOY_SHA" ]]
printf 'Stage blog/corporate release complete: %s backend=%s frontend=%s\n' "$DEPLOY_SHA" "$image" "$frontend_image"
}
supervise_release release_stage
+16
View File
@@ -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');
});
+219
View File
@@ -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()
+6
View File
@@ -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.
+27
View File
@@ -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.
+5
View File
@@ -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.
+9
View File
@@ -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.
+4
View File
@@ -0,0 +1,4 @@
# Plan and diagnosis
Active failure: backend create-time revalidation at 05:41:53Z returned Nest 404 for every tag/path. Runtime FRONTEND_INTERNAL_URL absent; FRONTEND_URL=https://stage.canina.ir routes /api to Nest. Both runtime secrets present. Fix Stage backend environment in the official code-only release override to frontend_stage:3000, not generic public FRONTEND_URL. Preserve operational Compose/env files. Extend existing serialized CI to build/recreate only backend/frontend from immutable source; reuse live admin artifacts in a build stage to avoid unrelated admin build. Compare secrets privately before deployment and invoke authorized cache-only existing-record invalidation through backend RevalidationService after readiness.
Metadata removes config fallback and explicitly empty social arrays; body-only link rule. RED/GREEN focused regressions before release. No schema operations.
Graph SHA256 6a73473cee9e207543fdeeb668bea544f766200a1b16b8a3a9ce18d4e42e51af queried at base 85ef01d3; binding unknown and locations stale, navigation only. Direct source confirms admin BlogsService -> RevalidationService -> Next handler; blog route -> metadata/JSONLD/BlogPostClient. No corporate source changes.
+3
View File
@@ -0,0 +1,3 @@
# Stage publication repair
Preserve existing article 97b81659-4496-4fcc-9bbc-2403150e84ae and all corporate restoration contracts from 85ef01d33782eb422d5eb80fb531bb0b2ae2b08e. No article writes, duplicates, Production, schema, schedules or model changes.
Acceptance: existing slug appears in settled /blog; empty article image fields produce no unrelated OG/Twitter/Article image; authored images retain precedence; body links are blue, bold and underlined. Stage antiindex remains intact. Exact pushed develop SHA must pass official CI and match both running images.
+6
View File
@@ -0,0 +1,6 @@
# Tasks
1. Record sanitized runtime routing failure and inspect dependency scope (done).
2. Run focused RED regression for media absence, body links and tracked Stage routing (pending).
3. Apply smallest route metadata/CSS and official Stage-only CI override (pending).
4. Run focused GREEN, typecheck and release-script tests (pending).
5. Commit/push develop; verify official terminal CI, immutable image parity and public existing-record acceptance (pending).
@@ -1 +0,0 @@
Base: 4d41a5bbf70e9bf9f9ead2412822668998b2c2cf; clean detached isolated worktree. Publisher validator currently untracked in application repository; preserve exact live baseline before minimal fix. Graph hash 6a73473cee9e207543fdeeb668bea544f766200a1b16b8a3a9ce18d4e42e51af; graph is unbound navigation only. Direct source confirms transport control; no backend/frontend edits. Dependencies: manifest -> field dispositions -> validate -> independent QA. Existing push workflow deploys site and restarts bot, forbidden in this scope. Do not push develop through that workflow; report publisher-only release boundary to parent.
@@ -1,3 +0,0 @@
# Production publisher manifest repair
The complete Blog contract must include optional UPDATE-only expectedUpdatedAt. NEW must explicitly disposition it not applicable and omit it from its payload. Preserve request-only semantics and independent approvals. No site deployment, gateway restart, Stage publisher or shared skill modifications.
Acceptance: regression fails before fix and passes after; existing editorial tests pass. Activate only through an authorized publisher-only CI/CD route; current site deploy workflow is not acceptable.
@@ -1,5 +0,0 @@
1. Preserve live validator/test source unchanged in Git.
2. RED: exercise expectedUpdatedAt manifest completeness on exact archived/current source.
3. GREEN: minimal update-only request-control manifest fix; full isolated editorial suite.
4. Record source hash and tests; commit isolated change without triggering prohibited site deploy.
5. Publisher-only CI/CD deployment, source/runtime mapping and exact-hash SEO/final QA remain prerequisites to cron create.