Compare commits

..
Author SHA1 Message Date
devops 09dbe4de48 feat(orchestration): bind PMO editorial proposals to owner Gate1 2026-10-24 03:26:30 +00:00
devops 8cedc2cd67 feat(orchestration): expose owner Gate1 queue and audited hold decisions
Deploy Canina / deploy (push) Successful in 3m9s
2026-10-24 02:28:58 +00:00
devops 65a6fe1f0c Repair Persian RTL orchestration panel and honest unavailable states
Deploy Canina / deploy (push) Successful in 2m1s
Preserve bounded command safety and snapshot provenance; render structured Persian records without translating human evidence. Validated app/node TypeScript, Vite build and 394 tests across 64 files in private Node 22 runner.
2026-10-23 20:48:52 +00:00
devops afbee9cf70 Fix nginx access to admin fonts and correct Shabnam asset path
Deploy Canina / deploy (push) Successful in 2m23s
2026-10-23 20:16:25 +00:00
devops 5984242ce4 Add authenticated fixed Tech one-shot adapter with bounded transport and admission
Deploy Canina / deploy (push) Successful in 3m10s
2026-10-23 19:36:56 +00:00
devops c523acc9e8 Add separately admitted Tech source ingestion with retained unknown cost 2026-10-23 19:36:56 +00:00
devops c78bdbcdb5 Accept quoted opaque session cookie emitted by installed Hermes
Deploy Canina / deploy (push) Successful in 3m9s
2026-10-23 19:15:41 +00:00
devops 7a081e28df Avoid inherited virtual address cap breaking Docker Go client
Deploy Canina / deploy (push) Successful in 3m13s
2026-10-23 19:09:54 +00:00
devops 93d43fb261 Match reviewed Stage API prefix and readable public runtime entrypoint
Deploy Canina / deploy (push) Failing after 1m18s
2026-10-23 19:07:18 +00:00
devops 8c435ab09e Restore Stage availability with explicit runtime provisioning rollback
Deploy Canina / deploy (push) Successful in 1m32s
2026-10-23 18:31:23 +00:00
devops 4388a5c2e6 Recover prior accepted backend within CI provisioning after pre-start failure
Deploy Canina / deploy (push) Failing after 1m44s
2026-10-23 18:28:44 +00:00
devops 62a7a87378 Quote comma-containing tmpfs options as one Compose mount
Deploy Canina / deploy (push) Failing after 1m17s
2026-10-23 18:25:50 +00:00
devops 19583c5ad8 Bound Docker client runtime within provisioning resource ceiling
Deploy Canina / deploy (push) Failing after 1m38s
2026-10-23 18:23:28 +00:00
devops 1858eba3e6 Prevent provisioning import cache from dirtying approved CI checkout
Deploy Canina / deploy (push) Failing after 1m17s
2026-10-23 18:20:58 +00:00
devops cb20c2acb5 Enable reviewed Stage runtime provisioning across authenticated CI boundary
Deploy Canina / deploy (push) Failing after 1m18s
2026-10-23 18:17:43 +00:00
devops 105ab07fbd Wire opt-in Stage runtime provisioning before isolated render; verify snapshot and Compose integration 2026-10-23 18:15:47 +00:00
devops bf84e5da43 Bound pilot inference with ongoing heartbeat and process-group cancellation 2026-10-23 16:58:12 +00:00
devops 02badd0e25 Add fail-closed real Hermes Tech pilot and independent artifact QA proof 2026-10-23 16:49:09 +00:00
devops aa23c7ef91 Add opt-in CI Stage credential provisioning and restricted SQLite inventory export 2026-10-23 16:49:09 +00:00
devops 4b147b0091 Route CI Stage deployment through authenticated pinned SSH host boundary
Deploy Canina / deploy (push) Successful in 4m35s
2026-10-23 15:06:24 +00:00
devops 6950ff0d79 Isolate commit-pinned Stage releases from dirty operational checkout 2026-10-23 14:50:32 +00:00
devops cc9ec25388 Wire explicitly enabled worker startup and exact legacy rehearsal comparison 2026-10-23 14:41:48 +00:00
devops be2ff0719a feat(worker): add fail-closed reviewed startup config registration 2026-10-23 14:38:54 +00:00
devops 9d93707848 fix(worker): scope artifact access and harden immutable transport storage 2026-10-23 14:07:01 +00:00
devops aa99216c49 feat(worker): add authenticated bounded PREPARE HTTP executor tracer 2026-10-23 13:49:38 +00:00
devops 58d3794050 fix(admin): distinguish snapshot inventory from live runtime telemetry 2026-10-23 13:34:53 +00:00
devops 16f9610027 Wire runtime read routes with explicit file-granted startup opt-in 2026-10-23 13:26:29 +00:00
devops 2023dd4e36 Make snapshot inventory provenance explicit and deny false live readiness 2026-10-23 13:22:21 +00:00
devops 0bed6e0313 fix(admin): restore immutable PREPARE scope contract
Match d43aa02 metadata validation and exact DTO; distinguish missing safe-projection workflow scope from human review evidence. Preserve action normalization and explicit kill-switch capability. TDD regression RED verified; disposable admin 61 files / 381 tests, app+node TypeScript and build pass.
2026-10-23 13:13:41 +00:00
devops f29ba4ab8b fix(admin): reconcile bounded PREPARE backend contracts 2026-10-23 13:04:06 +00:00
devops 56b6696ca3 Preserve bounded PREPARE scope and normalize proposal conflicts 2026-10-23 13:04:06 +00:00
devops 0d0c67704c Complete safe admin workflow and authenticated runtime read UI 2026-10-23 12:53:06 +00:00
devops 09e8a890e0 Add bounded Stage PREPARE workflow with transactional leased outbox 2026-10-23 12:53:06 +00:00
devops 4b17a6fcc3 Add admin-scoped Hermes runtime reads and isolated pinned HTTP inventory 2026-10-23 12:53:06 +00:00
devops 5507fbcb29 Add server-only Hermes login broker with deny-first module registration 2026-10-23 11:05:27 +00:00
devops 176cc08a6b Restore guarded legacy approvals controller removed by background run 2026-10-23 09:33:42 +00:00
devops 5953c3cf75 Remove outdated approvals controller 2026-10-23 09:13:22 +00:00
devops 0952a482fb Checkpoint recovered orchestration core with tested fail-closed boundaries 2026-10-23 08:01:08 +00:00
devops c7f1d25e98 Integrate schema-first Stage rollout with immutable CI commit pinning 2026-10-23 07:52:49 +00:00
devops 841eab0556 Harden integration read-only boundaries and admin contracts 2026-10-23 06:38:39 +00:00
devops ac065fa911 feat(orchestration): integrate read-only v4 surfaces with mutation lockdown 2026-10-22 19:04:00 +00:00
devops 70d8128bfb feat(router-usage): add protected read-only consumption adapter with honest quota unknowns 2026-10-22 18:54:24 +00:00
devops 8c8ae66c7d feat(runtime): add fail-closed Hermes management adapter and contract gates 2026-10-22 18:54:24 +00:00
devops c8a3f35b0a feat(admin): add backend-authoritative orchestration command center and usage view 2026-10-22 18:54:24 +00:00
devops e5c83e87fe feat(orchestration): add v4 additive backend core and policy tests 2026-10-22 18:15:32 +00:00
devops eb17c8e284 feat(orchestration): enforce corporate guardrails, domain invariant and daily publishing rate limits
Deploy Canina / deploy (push) Successful in 28s
2026-10-20 20:43:39 +00:00
devops 459f8b59fe Merge develop into main: complete autonomous corporate orchestration, manager actions separation, and truthful QA gates
Deploy Canina / deploy (push) Successful in 50s
2026-10-20 20:01:23 +00:00
devops e6c705cef7 feat(orchestration): implement live HTTP artifact verification with soft-404 detection
Deploy Canina / deploy (push) Successful in 24s
2026-10-20 19:55:28 +00:00
devops 19c4bb451d merge(worker): integrate orchestration worker contract
Deploy Canina / deploy (push) Successful in 2m56s
2026-10-20 19:43:59 +00:00
devops f71b79a17f merge(market): integrate feat/safe-market-proposals-20261020 2026-10-20 19:43:31 +00:00
devops e3eb7ca205 merge(admin_ui): integrate feat/admin-manager-actions-separation-20261020 2026-10-20 19:43:30 +00:00
devops 2e54163d9e merge(backend): integrate feat/backend-approval-separation-20261020 2026-10-20 19:43:30 +00:00
devops 344f79039f feat(orchestration): implement atomic claims, root task selection and truthful completion gates 2026-10-20 19:42:53 +00:00
devops 02fe634944 feat(admin): implement approval separation and verifiable completion policy
- Separate B2B_SUPPLIER_STRATEGY strategic manager actions from digital tasks
- Ensure strategic proposals do not create/requeue digital tasks or set executedAt
- Ensure SEO and digital approvals transition actionable tasks to TODO
- Strengthen task completion policy: require independently verifiable HTTP(S) deliverables, reject canina://, git://stage, and post-tsk missing article placeholders
- Reject completion when QA verification evidence reports 404 or missing article errors
- Add unit test coverage for ApprovalsController, DepartmentTasksController and completion policy
2026-10-20 19:41:39 +00:00
devops 7b1f64d727 feat(admin): separate manager actions from operational authorization and eliminate synthetic 3D room 2026-10-20 19:30:47 +00:00
devops d53e38a48f test: gate market proposal evidence and dedup before approval 2026-10-20 19:13:48 +00:00
devops 740e738fca Merge develop into main: modernize ApprovalsPage with clean light theme and smart payload mapper
Deploy Canina / deploy (push) Successful in 38s
2026-10-20 16:01:17 +00:00
devops ee90284129 feat(admin): modernize ApprovalsPage UI with clean light theme, structured payload visualizer and CEO directives
Deploy Canina / deploy (push) Successful in 38s
2026-10-20 16:00:59 +00:00
devops de42b3126b Merge feature/task-status-history-and-ceo-priority into main
Deploy Canina / deploy (push) Successful in 39s
2026-10-20 13:37:01 +00:00
devops 71dccbc590 feat(admin): add status transition metrics and CEO_EXPEDITE priority to task manager
Deploy Canina / deploy (push) Successful in 37s
2026-10-20 13:36:59 +00:00
devops 4c6127786f Merge develop into main: complete department and status filters in task management
Deploy Canina / deploy (push) Successful in 39s
2026-10-20 12:25:11 +00:00
devops bf082c59fe feat(admin): complete all department and status filters in task management
Deploy Canina / deploy (push) Successful in 37s
2026-10-20 12:00:59 +00:00
devops ee35c10454 merge: fix GA4 gtag arguments and route tracking
Deploy Canina / deploy (push) Successful in 2m32s
2026-10-20 10:54:19 +00:00
devops 1651fd00a2 fix(analytics): pass native arguments to dataLayer and track route changes
Deploy Canina / deploy (push) Successful in 2m20s
2026-10-20 10:49:56 +00:00
devops d9e4447903 merge: suppress Prisma CLI update banner during migrations
Deploy Canina / deploy (push) Successful in 40s
2026-10-20 09:54:23 +00:00
devops f59dd6ebec fix(prisma): suppress CLI update banner during migrations and builds
Deploy Canina / deploy (push) Successful in 2m34s
2026-10-20 09:46:32 +00:00
devops 10413547c1 merge: publish blog attribution and freshness fixes
Deploy Canina / deploy (push) Successful in 2m37s
2026-10-20 09:08:50 +00:00
devops 2f178db2c6 fix(blog): keep attribution rows on one line
Deploy Canina / deploy (push) Successful in 2m20s
2026-10-20 09:01:32 +00:00
devops 28485d51f7 fix(blog): improve attribution layout and freshness
Deploy Canina / deploy (push) Successful in 4m4s
2026-10-20 08:41:40 +00:00
devops 24362b89d7 fix(blog): refine editorial team label
Deploy Canina / deploy (push) Successful in 2m40s
2026-10-20 07:59:08 +00:00
devops 159e4a4cbf fix(blog): separate Canina author and editorial team
Deploy Canina / deploy (push) Successful in 2m47s
2026-10-20 07:52:07 +00:00
devops 13a7919c91 merge: reconcile main before blog freshness release
Deploy Canina / deploy (push) Successful in 2m34s
2026-10-20 07:26:54 +00:00
devops d670690816 fix(blog): unify structured author identity
Deploy Canina / deploy (push) Successful in 5m16s
2026-10-20 07:15:20 +00:00
devops 62c58c7c89 fix(blog): harden creation and unify author fallback 2026-10-20 06:59:43 +00:00
devops 0b4d9a8463 fix(blog): harden freshness and authorship 2026-10-20 06:31:22 +00:00
devops 51ca8c7e34 feat(blog): add material freshness and authorship 2026-10-20 05:36:03 +00:00
devops 64047054b8 merge: integrate live HQ telemetry and child task execution pipeline
Deploy Canina / deploy (push) Successful in 38s
2026-10-19 17:49:00 +00:00
devops 3f5258a76c feat(admin-hq): bind 3D orchestration room to live telemetry and dynamic task state
Deploy Canina / deploy (push) Successful in 36s
2026-10-19 17:48:44 +00:00
devops 3b93308b71 merge: resilient orchestration JSON parsing
Deploy Canina / deploy (push) Successful in 39s
2026-10-19 14:13:03 +00:00
devops bf53f2e17c fix(orchestration): tolerate raw control characters in model JSON
Deploy Canina / deploy (push) Successful in 36s
2026-10-19 14:13:00 +00:00
devops c2eca11a3b merge: verified completion gates and cross-department orchestration
Deploy Canina / deploy (push) Successful in 51s
2026-10-19 14:07:01 +00:00
devops 4c3f8de1fb feat(orchestration): enforce verified completion and cross-department fan-out
Deploy Canina / deploy (push) Successful in 2m8s
2026-10-19 13:59:22 +00:00
devops b3e1869724 merge: add CompanyOrchestrationHQ into main
Deploy Canina / deploy (push) Successful in 37s
2026-10-19 13:03:56 +00:00
devops 624f58c723 feat(pages): add CompanyOrchestrationHQ interactive 3D component
Deploy Canina / deploy (push) Successful in 37s
2026-10-19 13:03:54 +00:00
devops aedc0f6979 merge: register /governance/hq route into main
Deploy Canina / deploy (push) Failing after 7s
2026-10-19 13:03:31 +00:00
devops 1e53ceeda6 feat(governance): register /governance/hq route
Deploy Canina / deploy (push) Failing after 8s
2026-10-19 13:03:29 +00:00
devops f08ed0c22c merge: integrate Tasks state fix and Sidebar 3D link into main
Deploy Canina / deploy (push) Successful in 39s
2026-10-19 12:51:18 +00:00
devops ee0f877d97 fix(tasks): define sortOrder state and add 3D HQ to Sidebar navigation
Deploy Canina / deploy (push) Successful in 37s
2026-10-19 12:50:12 +00:00
devops b3d4e08914 merge: integrate develop fixes (Dashboard null-safety, Tasks Tehran datetime, and sorting) into main
Deploy Canina / deploy (push) Successful in 2m47s
2026-10-19 12:43:06 +00:00
devops ec9dd83e77 fix: syntax escape in TasksManagementPage
Deploy Canina / deploy (push) Successful in 3m21s
2026-10-19 12:33:00 +00:00
devops 4930e2a870 feat(tasks): interactive date-sorting toggle and Tehran datetime column
Deploy Canina / deploy (push) Failing after 8s
2026-10-19 12:32:25 +00:00
devops 5e21cc76fc fix(admin): resolve Dashboard crash with null safety and add Tehran time/sorting to Tasks
Deploy Canina / deploy (push) Failing after 2m16s
2026-10-19 12:29:33 +00:00
devops 529110a693 feat(support-chat): delegate blog consultation CTA clicks to open support chat modal
Deploy Canina / deploy (push) Successful in 2m38s
2026-10-19 06:32:11 +00:00
devops 0b5f2a88cc fix(blog): highlight in-article links and trigger support chat from smart advisor button
Deploy Canina / deploy (push) Successful in 2m44s
2026-10-19 06:24:22 +00:00
devops ff6f04fe46 fix(cache): revalidate storefront after admin mutations
Deploy Canina / deploy (push) Successful in 2m35s
2026-10-18 19:31:46 +00:00
devops 2e9a0e30c1 chore: sync feature branch with latest origin/develop
Deploy Canina / deploy (push) Successful in 2m40s
2026-10-18 18:53:04 +00:00
devops ad0acaccc2 refactor(admin): extract ProductFaqTab component from monolithic Products.tsx [FE-020] 2026-10-18 18:34:52 +00:00
devops 30019a02d8 fix(deps): upgrade next to 16.4.2 eliminating critical vulnerability [FE-023] 2026-10-18 18:30:31 +00:00
devops 87999be3d2 fix(frontend): comprehensive SEO, A11y, media upload validation, and logging enhancements [FE-013 to FE-022] 2026-10-18 17:55:37 +00:00
devops 4ce9f6000c fix(ux): enforce safe responsive containment on mobile CartDrawer [FE-012] 2026-10-18 16:42:57 +00:00
devops 0529962391 fix(admin): resolve array response unwrapping in SupportChatAnalyticsPage [FE-011] 2026-10-18 16:37:45 +00:00
devops 14c45e4f6c fix(api): preserve error semantics in getProductBySlug distinguishing 404 from 5xx [FE-010] 2026-10-18 16:35:14 +00:00
devops 1c38b95fb0 fix(auth): align admin refresh token contract with backend cookie and bearer endpoints [FE-009] 2026-10-18 16:32:28 +00:00
devops dab66a2dce fix(checkout): guard against empty cart order submission and disable checkout CTA [FE-008] 2026-10-18 16:16:15 +00:00
devops 86eb0cfcae fix(monitoring): eliminate Math.random mock values from admin monitoring stats [FE-007] 2026-10-18 16:12:32 +00:00
devops d3aa10c769 fix(security): sanitize dynamic HTML rendering in Hero and ProductPage [FE-006] 2026-10-18 16:09:36 +00:00
devops 15524609fe fix(security): enforce withCredentials true on storefront api for httpOnly cookies [FE-005] 2026-10-18 16:04:34 +00:00
devops aacb91ac70 fix(security): sanitize and block payment tokens and secrets in public ui-texts endpoint [FE-004] 2026-10-18 15:47:12 +00:00
devops f91502026b feat(admin): configure vitest test runner and fix dashboard audit-logs array guards [FE-003] 2026-10-18 15:43:19 +00:00
devops 7f2f638f38 test(backend): fix unit tests for media magic validation, slow query logger, and support chat specs
Deploy Canina / deploy (push) Successful in 41s
2026-10-18 15:40:03 +00:00
devops 7879ead486 fix(frontend): harmonize selectors and assertions across failing application unit tests [FE-002] 2026-10-18 15:35:43 +00:00
devops dda1caf3e0 feat(backend & dba): implement inventory concurrency guard, redis distributed locks, magic byte file validation, slow query logger, and db check constraints 2026-10-18 15:29:37 +00:00
devops bb483d3e68 fix(frontend): resolve react-hooks declaration order and lint errors in SupportChat [FE-001] 2026-10-18 15:23:12 +00:00
devops 73bc79b4bb fix(backend): encapsulate TasksService in TasksModule and export OrdersService
Deploy Canina / deploy (push) Successful in 30s
2026-10-18 14:43:17 +00:00
devops 9520426a66 perf(backend & dba): resolve unindexed foreign keys, eliminate N+1 in product catalog, add background tasks for inventory release & payment reconciliation, and harden SQL queries
Deploy Canina / deploy (push) Failing after 36s
2026-10-18 14:35:41 +00:00
devops b077423ab2 fix: move BackToTop to right side, wire global prescription modal trigger in header & mobile drawer
Deploy Canina / deploy (push) Successful in 2m15s
2026-10-18 14:09:01 +00:00
devops 94867fbd5a fix(frontend): ensure use client is first line in ArchivePage
Deploy Canina / deploy (push) Successful in 2m16s
2026-10-18 13:54:31 +00:00
devops 6a37f2f4c7 fix: support chat elevated only on PDP, rx button opens modal instantly, and hide pet badge when not logged in
Deploy Canina / deploy (push) Failing after 21s
2026-10-18 13:50:54 +00:00
devops b54338f91f fix(backend): fix duplicate success in deleteSymptom
Deploy Canina / deploy (push) Successful in 2m33s
2026-10-18 13:28:39 +00:00
devops c7b95b4d2b feat: dock PDP buy bar above mobile nav, redesign B2B modal to mobile-card layout, and implement full symptoms CRUD in backend & admin panel
Deploy Canina / deploy (push) Failing after 1m23s
2026-10-18 13:26:01 +00:00
devops 8e4388c325 fix(backend): fix duplicate success property in renameSymptom response
Deploy Canina / deploy (push) Successful in 2m30s
2026-10-18 12:58:12 +00:00
devops 305995cbe2 fix(backend): correct syntax for prisma transaction in renameSymptom
Deploy Canina / deploy (push) Failing after 1m15s
2026-10-18 12:55:11 +00:00
devops 468170349d feat(ux-fix): resolve wiki fallback 404, responsive B2B modal, clean badges, aligned bullets, mobile bottom nav on PDP and admin Rx/symptoms management
Deploy Canina / deploy (push) Failing after 1m14s
2026-10-18 12:51:04 +00:00
devops 52446592e7 fix(header): clean up prescription button navigation onClick handler
Deploy Canina / deploy (push) Successful in 2m16s
2026-10-18 11:35:43 +00:00
devops 7bca0ef335 feat(prescriptions): launch online veterinary prescription assessment portal with regression tests (B2B-03)
Deploy Canina / deploy (push) Failing after 1m6s
2026-10-18 11:31:49 +00:00
devops 80717680a1 feat(b2b): connect B2B landing inquiry form to public inquiry endpoint with regression test (B2B-01)
Deploy Canina / deploy (push) Successful in 2m14s
2026-10-18 11:19:51 +00:00
devops bb81f2c944 feat(blog): implement interactive in-article product cards (CAT-P2-10)
Deploy Canina / deploy (push) Successful in 2m15s
2026-10-18 06:37:05 +00:00
devops 69094a8646 fix(admin): resolve ReferenceError SmartChatAnalyticsPage in admin routes
Deploy Canina / deploy (push) Successful in 38s
2026-10-18 05:33:18 +00:00
devops bed0b480d7 Merge develop into main for sub-sitemaps, FAQ schema and local font release (SEO-P1-07, SEO-P1-10, SEO-P2-16)
Deploy Canina / deploy (push) Successful in 26s
2026-10-17 20:38:55 +00:00
devops f8f9329c50 feat(seo): implement FAQPage schema, sub-sitemaps and local Lalezar WOFF2 font (SEO-P1-07, SEO-P1-10, SEO-P2-16)
Deploy Canina / deploy (push) Successful in 2m10s
- SEO-P1-07: extract FAQs and accordion items from blog content and inject JSON-LD FAQPage schema
- SEO-P1-10: add dynamic sub-sitemaps /sitemap-blog.xml and /sitemap-wiki.xml and register in robots.ts
- SEO-P2-16: host Lalezar font locally as optimized WOFF2 with preload enabled in lib/fonts.ts
- Add full regression test suites with 100% pass rate
2026-10-17 20:38:09 +00:00
devops 189e1f1202 Merge develop into main for technical SEO fixes release (SEO-P0-01, SEO-P0-02, SEO-P1-06, SEO-P2-15)
Deploy Canina / deploy (push) Successful in 2m14s
2026-10-17 20:11:48 +00:00
devops 5cf2863987 fix(seo): resolve technical SEO issues (SEO-P0-01, SEO-P0-02, SEO-P1-06, SEO-P2-15)
Deploy Canina / deploy (push) Successful in 2m11s
- SEO-P0-01: prioritize options.canonicalPath in getPageMetadata over shop_canonical text
- SEO-P0-02: invoke notFound() in /shop/[slug] and /wiki/[slug] when entity is missing
- SEO-P1-06: package root JSON-LD Organization and WebSite into @graph structure
- SEO-P2-15: add hreflang fa-IR and x-default alternates in root layout metadata
- Add regression test suite lib/__tests__/seo-technical.test.ts
2026-10-17 20:02:56 +00:00
devops 83cf70ac74 Merge develop into main for complete UX overhaul release (UX-01 to UX-37)
Deploy Canina / deploy (push) Successful in 2m25s
2026-10-17 19:23:20 +00:00
devops 87bf867513 feat(ux-37): add /api/version deployment metadata endpoint and CI verification gate
Deploy Canina / deploy (push) Successful in 2m9s
2026-10-17 19:06:01 +00:00
devops 961bb383db test(ux-36): establish multi-device responsive matrix specification and test suite 2026-10-17 19:04:02 +00:00
devops 5d5cc14f9e test(ux-35): enforce 100% home section retention and zero mobile content exclusion regression gate 2026-10-17 19:02:19 +00:00
devops bfe228adee test(ux-34): add automated WCAG 2.1 AA/AAA contrast and readability gate 2026-10-17 18:58:03 +00:00
devops b5b4e64453 fix(ux-33): prevent support button overlap on contact form and elevate with safe-area spacing 2026-10-17 18:56:13 +00:00
devops f23efbf90d fix(ux-32): verify responsive menu sheet trigger and add mobile safe padding on search route 2026-10-17 18:53:20 +00:00
devops 81d1fa816b fix(ux-31): eliminate 13px horizontal viewport overflow and align MobileBottomNav with safe-area 2026-10-17 18:47:20 +00:00
devops f5c970f82e feat(ux-30): ensure touch targets exceed 44px across CartDrawer, Wiki, Contact, Footer and MobileBottomNav with regression tests 2026-10-17 18:43:34 +00:00
devops 6d0dea07f8 test(ux-29): add responsive and semantic tests for login, cart, profile, videos and smart-advisor routes 2026-10-17 18:37:26 +00:00
devops c4d95762f8 test(ux-28): add integration test verifying singleton authmodal and cartdrawer in shell layout 2026-10-17 18:33:29 +00:00
devops 23724df4e4 feat(ux-27): normalize persian text, add persistent error/retry state and accessible aria empty-state to search 2026-10-17 18:27:21 +00:00
devops 21f7768cce feat(ux-26): implement real shop pagination with deep-link page support and regression test 2026-10-17 18:15:42 +00:00
devops 92065cc7f2 fix(ux-25): add persistent error state with retry and recovery to shop archive 2026-10-17 18:05:35 +00:00
devops 956ef3b4d3 fix(ux-24): enforce semantic single H1 in catalog spread and add heading hierarchy regression tests 2026-10-17 17:28:27 +00:00
devops 0d8125bd37 fix(shop): unify responsive H1 and strip nested H1 tag in catalog title
Deploy Canina / deploy (push) Successful in 2m10s
2026-10-17 16:10:38 +00:00
devops ea4d33b6bb fix(mobile): ensure touch targets exceed 44x44px across bottom navigation
Deploy Canina / deploy (push) Successful in 2m10s
2026-10-17 15:44:19 +00:00
devops b4fdd7abb8 fix(contact): add standard autocomplete, inputmode and attributes for contact inputs
Deploy Canina / deploy (push) Successful in 2m10s
2026-10-17 15:27:07 +00:00
devops f9d70bc196 fix(shop): prevent duplicate product card rendering across breakpoints in list viewMode
Deploy Canina / deploy (push) Successful in 2m5s
2026-10-17 14:42:44 +00:00
devops 5b20bab381 feat(wiki): add accessible accordion disclosures for ingredients
Deploy Canina / deploy (push) Successful in 2m6s
2026-10-17 14:01:23 +00:00
devops 2c3e4bb752 fix(search): connect tabs to results panel
Deploy Canina / deploy (push) Successful in 2m10s
2026-10-17 12:52:00 +00:00
devops 570f0acc66 feat(search): add segmented universal results
Deploy Canina / deploy (push) Successful in 2m7s
2026-10-17 12:33:00 +00:00
devops 297aaadbe6 fix(blog): preserve sanitized content through hydration
Deploy Canina / deploy (push) Successful in 2m12s
2026-10-17 11:45:34 +00:00
devops 5573bb3568 fix(wiki): contain long ingredient codes on mobile
Deploy Canina / deploy (push) Successful in 2m10s
2026-10-17 11:24:36 +00:00
devops 80d9dc2f45 fix(wiki): compact mobile cards and add pagination
Deploy Canina / deploy (push) Successful in 2m9s
2026-10-17 11:18:52 +00:00
devops 092933860a feat(ux): complete 5-task suite (category filter, real total count, mobile wiki, blog mobile UX, chatbot typewriter)
Deploy Canina / deploy (push) Successful in 2m12s
2026-10-17 10:59:21 +00:00
devops 3d45b618ab feat(search): redesign search page for mobile-first layout with multi-content tabs (products, blogs, wiki, videos)
Deploy Canina / deploy (push) Successful in 2m9s
2026-10-17 10:42:55 +00:00
devops 2ab2318732 fix(ux): render mobile solution and symptom modals via React Portal to prevent CSS mask clipping
Deploy Canina / deploy (push) Successful in 2m10s
2026-10-17 10:29:51 +00:00
devops f3806cd6de fix(ux): remove search autoFocus in mobile shop sheet and add dedicated search entry in mobile menu
Deploy Canina / deploy (push) Successful in 2m8s
2026-10-17 10:20:49 +00:00
devops 8326e10ce8 feat(catalog): add progressive product pagination and load more action
Deploy Canina / deploy (push) Successful in 2m8s
2026-10-17 10:04:10 +00:00
devops d77ddd4bb2 feat(ux): optimize mobile spacing, padding and density across landing sections without removing content
Deploy Canina / deploy (push) Successful in 2m6s
2026-10-17 09:52:06 +00:00
devops 9149e2b5ab fix(seo): ensure proper H1 heading on shop, wiki, checkout, track, success and dashboard pages
Deploy Canina / deploy (push) Successful in 2m6s
2026-10-17 09:42:22 +00:00
devops 5534d050af fix(ux): replace overlay button with clean gradient mask for mobile catalog filters
Deploy Canina / deploy (push) Successful in 2m7s
2026-10-17 09:31:34 +00:00
devops 994086e580 feat(ux): add visible interactive scroll cue for mobile catalog filters
Deploy Canina / deploy (push) Successful in 2m11s
2026-10-17 09:12:34 +00:00
devops dbd7d8b37d fix(ux): expand mobile catalog filter fade height and width for visible affordance
Deploy Canina / deploy (push) Successful in 3m7s
2026-10-17 09:02:44 +00:00
devops f440c60891 feat(ux): add mobile catalog filter edge affordance
Deploy Canina / deploy (push) Successful in 2m8s
2026-10-17 07:57:05 +00:00
devops 8672d4bcac feat(ux): add mobile catalog filter edge affordance
Deploy Canina / deploy (push) Successful in 2m15s
2026-10-17 07:53:46 +00:00
devops 87ad359e60 fix(frontend): separate mobile back-to-top control
Deploy Canina / deploy (push) Successful in 2m7s
2026-10-17 07:44:07 +00:00
devops 5afa2a37be fix(ux): remove duplicate utility route shells
Deploy Canina / deploy (push) Successful in 2m9s
2026-10-17 06:53:10 +00:00
devops d73b2e51c6 fix(blogs): tolerate missing stage author relations
Deploy Canina / deploy (push) Successful in 1m59s
2026-10-17 06:29:15 +00:00
devops 26f5f68708 fix(ux): harden dialogs and remove unsupported contact fallbacks
Deploy Canina / deploy (push) Successful in 2m10s
2026-10-17 05:52:02 +00:00
devops 86cfa4b3c8 fix(pdp): keep initial server product visible
Deploy Canina / deploy (push) Successful in 2m7s
2026-10-16 21:03:13 +00:00
devops e2b6d2acae fix(pdp): render product from server data
Deploy Canina / deploy (push) Successful in 2m9s
2026-10-16 20:57:54 +00:00
devops 8ef98cb415 test(shop): cover mobile catalog search refresh
Deploy Canina / deploy (push) Successful in 2m13s
2026-10-16 20:51:18 +00:00
devops 6dbee69601 fix(a11y): label catalog sort control
Deploy Canina / deploy (push) Successful in 2m15s
2026-10-16 20:47:00 +00:00
devops 1aad481930 fix(a11y): label catalog search controls
Deploy Canina / deploy (push) Successful in 2m10s
2026-10-16 20:41:53 +00:00
devops 4393de5d8f fix(a11y): label global search controls
Deploy Canina / deploy (push) Successful in 2m13s
2026-10-16 20:35:53 +00:00
devops 5380bb66bb test(ux): add frontend quality gates
Deploy Canina / deploy (push) Successful in 2m46s
2026-10-16 20:31:32 +00:00
devops 9a9ab149ed feat(ux): restore login and cart routes
Deploy Canina / deploy (push) Successful in 2m11s
2026-10-16 20:22:41 +00:00
devops bec76393bb fix(shop): render products from server data
Deploy Canina / deploy (push) Successful in 2m8s
2026-10-16 19:48:37 +00:00
devops 427b0d8591 Merge support chat pet context and analytics
Deploy Canina / deploy (push) Successful in 49s
2026-10-16 11:56:13 +00:00
devops 1de8fd9a60 feat(support-chat): add pet context and admin analytics
Deploy Canina / deploy (push) Successful in 2m10s
2026-10-16 11:52:17 +00:00
devops 5c90d1629d Merge develop into main
Deploy Canina / deploy (push) Successful in 2m22s
2026-10-15 21:45:06 +00:00
devops c1a5223798 fix(support-chat): ground identity and improve widget UX
Deploy Canina / deploy (push) Successful in 3m43s
2026-10-15 21:39:29 +00:00
devops 139aaff86c Merge develop into main
Deploy Canina / deploy (push) Successful in 2m22s
2026-10-15 20:17:11 +00:00
devops d9ff4aef58 fix(contact): use database settings everywhere
Deploy Canina / deploy (push) Successful in 3m36s
2026-10-15 20:13:57 +00:00
devops a68e9b2c1e Merge develop into main
Deploy Canina / deploy (push) Successful in 28s
2026-10-15 19:02:33 +00:00
devops 66cbf4c68d fix(support-chat): recognize Finglish contact requests
Deploy Canina / deploy (push) Successful in 26s
2026-10-15 19:02:24 +00:00
devops 30ca17712c Merge develop into main
Deploy Canina / deploy (push) Successful in 28s
2026-10-15 18:56:37 +00:00
devops 781b437c11 fix(support-chat): ground contact answers in live source
Deploy Canina / deploy (push) Successful in 25s
2026-10-15 18:56:29 +00:00
devops cf8f920aa6 Merge develop into main
Deploy Canina / deploy (push) Successful in 39s
2026-10-15 18:51:18 +00:00
devops c16e34df02 fix(support-chat): send classifier-compatible payload
Deploy Canina / deploy (push) Successful in 1m54s
2026-10-15 18:51:10 +00:00
devops e67a4bd51c Merge develop into main
Deploy Canina / deploy (push) Successful in 39s
2026-10-15 18:39:45 +00:00
devops 18e59efdcc fix(support-chat): register config routes before history
Deploy Canina / deploy (push) Successful in 1m55s
2026-10-15 18:39:38 +00:00
devops c596c3a8b2 Merge develop into main
Deploy Canina / deploy (push) Successful in 38s
2026-10-15 18:35:44 +00:00
devops 1f68210d08 fix(support-chat): import UUID route validator
Deploy Canina / deploy (push) Successful in 1m50s
2026-10-15 18:35:37 +00:00
devops fb4669e5a5 Merge develop into main
Deploy Canina / deploy (push) Failing after 1m6s
2026-10-15 18:33:28 +00:00
devops 7b376f1126 fix(support-chat): baseline legacy migration in CI
Deploy Canina / deploy (push) Failing after 1m10s
2026-10-15 18:33:20 +00:00
devops e3426449e8 Merge develop into main
Deploy Canina / deploy (push) Failing after 25s
2026-10-15 18:28:35 +00:00
devops 8795617d7f fix(deploy): apply committed support chat migrations
Deploy Canina / deploy (push) Successful in 27s
2026-10-15 18:28:28 +00:00
devops 0dbf9cabb5 Merge develop into main
Deploy Canina / deploy (push) Successful in 2m18s
2026-10-15 15:50:32 +00:00
devops b6ac30d0e3 fix(prisma): quote support chat schema mappings
Deploy Canina / deploy (push) Successful in 4m2s
2026-10-15 15:50:25 +00:00
devops d79612b7e8 Merge develop into main
Deploy Canina / deploy (push) Failing after 29s
2026-10-15 15:39:57 +00:00
devops 33c605132b feat(support-chat): resolve trusted page context
Deploy Canina / deploy (push) Failing after 24s
2026-10-15 15:39:55 +00:00
devops 686de48a95 Merge develop into main
Deploy Canina / deploy (push) Has been cancelled
2026-10-15 15:39:13 +00:00
devops 043488fe55 fix(support-chat): allow safe Canina research and contact links
Deploy Canina / deploy (push) Failing after 31s
2026-10-15 15:39:06 +00:00
devops f292195edf fix(support-chat): restore saved conversation history
Deploy Canina / deploy (push) Failing after 30s
2026-10-15 07:41:21 +00:00
devops b61ba761bc fix(support-chat): default conversation updated timestamp
Deploy Canina / deploy (push) Failing after 23s
2026-10-15 07:34:44 +00:00
devops 4d64ad96b2 fix(support-chat): complete conversation analytics persistence
Deploy Canina / deploy (push) Has been cancelled
2026-10-15 07:34:07 +00:00
devops def903734f feat(support-chat): persist conversations and analytics
Deploy Canina / deploy (push) Failing after 28s
2026-10-15 07:28:02 +00:00
devops 59ab154d0f fix(support-chat): capture bot key before recreate
Deploy Canina / deploy (push) Successful in 32s
2026-10-15 07:02:27 +00:00
devops 31c55525c3 fix(support-chat): sync backend key from running bot
Deploy Canina / deploy (push) Successful in 21s
2026-10-15 07:02:00 +00:00
devops 332d400a37 fix(ci): recreate bot through runner docker daemon
Deploy Canina / deploy (push) Successful in 21s
2026-10-15 06:55:16 +00:00
devops b5dcdec18c fix(ci): correct support key env newline
Deploy Canina / deploy (push) Failing after 4s
2026-10-15 06:40:35 +00:00
devops c6cf2e31aa fix(ci): preserve newline in support key env
Deploy Canina / deploy (push) Failing after 5s
2026-10-15 06:40:06 +00:00
devops b7268d0c47 fix(support-chat): do not overwrite synced key with empty CI secret
Deploy Canina / deploy (push) Failing after 5s
2026-10-15 06:39:55 +00:00
devops a7b7164fc0 fix(support-chat): sync backend key with bot at deploy
Deploy Canina / deploy (push) Failing after 16s
2026-10-15 06:32:21 +00:00
devops 73f5d6d278 fix(deploy): prevent runtime prisma schema mutation
Deploy Canina / deploy (push) Failing after 5s
2026-10-15 06:18:47 +00:00
devops c04f1675f6 fix(ci): create bot override directory at runtime
Deploy Canina / deploy (push) Failing after 15s
2026-10-15 06:10:55 +00:00
devops 50cbd5b8ae fix(ci): create bot override directory
Deploy Canina / deploy (push) Has been cancelled
2026-10-15 06:10:49 +00:00
devops 19a029291b fix(ci): build support bot without unavailable buildx
Deploy Canina / deploy (push) Failing after 5s
2026-10-15 06:07:28 +00:00
devops bd7345078a fix(ci): preserve compose build continuation
Deploy Canina / deploy (push) Failing after 2s
2026-10-15 06:04:45 +00:00
devops 4969ec3fcd chore(ci): normalize deploy script whitespace
Deploy Canina / deploy (push) Failing after 1m21s
2026-10-15 06:00:48 +00:00
devops ec420e6053 fix(ci): expand bot image variables at deploy time
Deploy Canina / deploy (push) Has been cancelled
2026-10-15 06:00:41 +00:00
devops 4749ec91e4 fix(ci): preserve deploy variables in bot override
Deploy Canina / deploy (push) Failing after 2s
2026-10-15 06:00:00 +00:00
devops 679a551243 ci: deploy support chat bot from committed source
Deploy Canina / deploy (push) Failing after 2s
2026-10-15 05:59:48 +00:00
devops 240a176151 fix(support-chat): preserve provider catalog in isolated runtime
Deploy Canina / deploy (push) Successful in 30s
2026-10-14 21:55:58 +00:00
devops d2c227c733 fix(revalidation): preserve production runtime secret
Deploy Canina / deploy (push) Successful in 17s
2026-10-14 21:33:34 +00:00
devops a532e2c73f merge: synchronize main into develop before release
Deploy Canina / deploy (push) Has been cancelled
2026-10-14 21:33:02 +00:00
devops 3ff77d585d fix(deploy): make rollout code-only
Deploy Canina / deploy (push) Failing after 13s
2026-10-14 21:28:27 +00:00
devops 2a58e0c731 fix(revalidation): load runtime secret from env file
Deploy Canina / deploy (push) Successful in 37s
2026-10-14 21:20:38 +00:00
devops 200ddda352 fix(ci): preserve support chat runtime configuration
Deploy Canina / deploy (push) Successful in 33s
2026-10-14 20:57:08 +00:00
devops 1cbd210be1 fix(ci): preserve support chat runtime configuration
Deploy Canina / deploy (push) Successful in 49s
2026-10-14 20:57:07 +00:00
devops 419e478489 merge: remove redundant e2e and torob workflows
Deploy Canina / deploy (push) Successful in 2m4s
2026-10-14 20:08:43 +00:00
devops 8f8f0c36d5 ci: keep deploy workflow only
Deploy Canina / deploy (push) Successful in 38s
2026-10-14 20:08:41 +00:00
devops 2271291c87 merge: promote available products ordering
Deploy Canina / deploy (push) Waiting to run
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 6s
Torob regression / torob-regression (push) Failing after 42s
2026-10-14 20:06:43 +00:00
devops ed20f6d089 merge: prioritize available products
Deploy Canina / deploy (push) Has been cancelled
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s
Torob regression / torob-regression (push) Failing after 38s
2026-10-14 20:06:40 +00:00
devops 12861288f2 feat(shop): show available products first 2026-10-14 20:04:50 +00:00
devops 73bfe0d893 merge: promote develop to main
Deploy Canina / deploy (push) Successful in 2m18s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s
Torob regression / torob-regression (push) Failing after 34s
2026-10-14 19:58:50 +00:00
devops a289c40503 merge: admin smart-chat settings
Deploy Canina / deploy (push) Successful in 4m32s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 6s
Torob regression / torob-regression (push) Failing after 40s
2026-10-14 19:44:40 +00:00
devops b6735b5a9c feat(support-chat): add admin smart chat settings 2026-10-14 19:44:24 +00:00
devops f6736cd43f fix(stage): deploy Torob backend without schema writes [skip ci] 2026-10-14 19:38:57 +00:00
devops fa4f1124e6 fix(stage): deploy Torob backend without schema writes [skip ci] 2026-10-14 19:36:18 +00:00
devops 30cebb92fa test(torob): verify PostgreSQL feed regression and guard invalid pagination 2026-10-14 19:35:21 +00:00
353 changed files with 22707 additions and 2511 deletions

No files matched your search

+18 -15
View File
@@ -10,22 +10,25 @@ jobs:
deploy:
runs-on: canina
steps:
- name: Run deploy.sh from within the scripts folder
- name: Authenticated host Stage deployment (Production blocked)
shell: bash
env:
BRANCH: ${{ github.ref_name }}
DEPLOY_SHA: ${{ github.sha }}
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
run: |
# 1. رفع مشکل DNS و غیرفعال کردن پروکسی برای دامنه محلی
echo "87.248.133.138 git.parsaaghayi.ir" >> /etc/hosts
export http_proxy=""
export https_proxy=""
export HTTP_PROXY=""
export HTTPS_PROXY=""
set -euo pipefail
set +x
[[ "${BRANCH}" == develop ]] || { echo 'Production blocked' >&2; exit 2; }
[[ "${DEPLOY_SHA}" =~ ^[0-9a-f]{40}$ ]] || exit 2
# Provision DEPLOY_KNOWN_HOSTS from an independently verified host key.
# Never use baked-in /opt/services, runner SSH keys or docker.sock.
[[ -n "${DEPLOY_KEY}" && -n "${DEPLOY_KNOWN_HOSTS}" ]] || { echo 'Host boundary requires DEPLOY_KEY and verified DEPLOY_KNOWN_HOSTS' >&2; exit 2; }
export http_proxy="" https_proxy="" HTTP_PROXY="" HTTPS_PROXY=""
export no_proxy="git.parsaaghayi.ir,localhost,127.0.0.1,87.248.133.138"
# 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
chmod +x scripts/deploy.sh
cd scripts
./deploy.sh ${BRANCH}
umask 077
wrapper=$(mktemp)
trap 'rm -f "$wrapper"' EXIT
wget --no-proxy -qO "$wrapper" "https://git.parsaaghayi.ir/parsa/canina/raw/commit/${DEPLOY_SHA}/scripts/ci-host-deploy.sh"
bash "$wrapper"
-45
View File
@@ -1,45 +0,0 @@
name: E2E Playwright Tests
on:
push:
branches:
- main
- develop
pull_request:
branches:
- main
- develop
jobs:
test-e2e:
name: Run Full E2E & Security Suites
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- name: Install Monorepo Dependencies
run: npm ci
- name: Install Playwright Browsers
run: npx playwright install --with-deps chromium
- name: Run E2E Test Suite
run: npx playwright test --project=chromium-desktop --project=admin-chromium
env:
CI: true
NODE_ENV: test
- name: Upload Test Report
if: always()
uses: actions/upload-artifact@v4
with:
name: playwright-report
path: playwright-report/
retention-days: 14
@@ -0,0 +1,24 @@
name: Hermes runtime contract review (NO DEPLOY)
on:
workflow_dispatch:
permissions:
contents: read
jobs:
contract-review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '22'
cache: npm
cache-dependency-path: backend/package-lock.json
- run: npm ci --ignore-scripts --no-audit
working-directory: backend
- run: npm test -- --runInBand hermes-management
working-directory: backend
- run: npx tsc -p src/hermes-management/tsconfig.contract.json --noEmit
working-directory: backend
- run: npx tsc -p src/hermes-management/tsconfig.contract.json
working-directory: backend
# No SSH, Docker socket, secrets, deployments, dispatch or runtime mutation.
+2
View File
@@ -49,6 +49,8 @@ RUN chown -R nginx:nginx /var/log/nginx /var/cache/nginx /run/nginx 2>/dev/null
WORKDIR /app
COPY --from=app-builder /app .
COPY --from=admin-builder /app/dist ./admin_static
# Build-context umask must not make public assets unreadable by nginx workers.
RUN chmod -R a+rX /app/admin_static
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/http.d/default.conf
+3 -1
View File
@@ -2,6 +2,7 @@ FROM node:20-alpine AS builder
USER root
RUN http_proxy=http://172.17.0.1:8888 https_proxy=http://172.17.0.1:8888 apk add --no-cache openssl || apk add --no-cache openssl || true
WORKDIR /app
ENV PRISMA_HIDE_UPDATE_MESSAGE=true
ENV PRISMA_SKIP_POSTINSTALL_GENERATE=true
ENV PRISMA_ENGINES_MIRROR=https://registry.npmmirror.com/-/binary/prisma
ENV DISABLE_OPENCOLLECTIVE=1
@@ -33,6 +34,7 @@ USER root
RUN http_proxy=http://172.17.0.1:8888 https_proxy=http://172.17.0.1:8888 apk add --no-cache openssl || apk add --no-cache openssl || true
RUN mkdir -p /app/uploads && chown node:node /app/uploads
WORKDIR /app
ENV PRISMA_HIDE_UPDATE_MESSAGE=true
COPY --chown=node:node --from=builder /app/package*.json ./
COPY --chown=node:node --from=builder /app/tsconfig.json ./tsconfig.json
COPY --chown=node:node --from=builder /app/eslint.config.mjs ./eslint.config.mjs
@@ -45,4 +47,4 @@ COPY --chown=node:node --from=builder /app/src ./src
COPY --chown=node:node --from=builder /app/test ./test
EXPOSE 3000
USER node
CMD ["sh", "-c", "npx prisma db push --skip-generate && if [ -f dist/src/main.js ]; then node dist/src/main.js; else node dist/main.js; fi"]
CMD ["node", "dist/main.js"]
+28
View File
@@ -0,0 +1,28 @@
# Prisma migration history and Orchestration v4 rollout boundary
The checked-in history is **not a complete baseline of the current Prisma schema**. Do not assume that replaying it into an empty database can recreate the application, or that existing production tables imply corresponding migration records. Never use `prisma db push`, reset, or edit applied SQL to reconcile this.
## Actual tracked history
| Migration | What its SQL actually does |
| --- | --- |
| `20260526145407_init` | Creates users, addresses, wallets, products, product ingredients/symptoms, pets and medical conditions, reminders/completions, health logs, coupons, orders and order items, with their indexes/foreign keys. |
| `20260526160916_add_ui_texts_and_scientific_terms` | Creates `ui_texts` and `scientific_terms`. |
| `20261013210253_add_admin_modules` | Creates only `competitor_price_logs`, `b2b_leads`, `price_suggestions`, and `sms_campaigns`, with indexes and product foreign keys. It does **not** create legacy approvals, department tasks or audit logs. |
| `20261015000000_add_support_conversations` | Creates support conversations/messages and their foreign keys. |
| `20261020000000_add_blog_freshness_authorship` | Adds three columns to an assumed-existing `blogs` table. No earlier tracked migration creates `blogs`; fresh replay is therefore incomplete before v4. |
| `20261022000000_add_orchestration_v4_core` | Additively creates nine `orchestration_*` tables, indexes and internal foreign keys; it neither creates nor copies legacy governance tables. |
There is no checked-in CREATE TABLE migration for `approval_requests`, `department_tasks`, or `audit_logs`. Their models/controllers are not evidence that either live environment has these tables, nor proof of how any historical database acquired them. The broader missing legacy schema must be reconciled separately. This document does not certify live table existence or migration status.
## Required rollout safeguards (not authorization to deploy)
1. Back up each environment and verify restore capability. Capture its actual schema and `_prisma_migrations` history read-only; compare those independently against all tracked SQL, including prerequisites such as `blogs`.
2. Rehearse the exact planned chain on a disposable database restored from a reviewed baseline. A failed empty replay is a legacy-history gap, not evidence that the additive v4 SQL alone is faulty. Resolve the missing baseline through a separately reviewed migration/baseline plan, never by skipping errors.
3. `prisma migrate resolve --applied <name>` only records history; it creates no objects. Use it only for a specifically verified, fully matching existing migration and reviewed environment-specific plan. Never mark the whole chain applied or mark missing tables as applied.
4. After baseline reconciliation and rehearsal pass, apply reviewed pending migrations through CI/CD only using `prisma migrate deploy`. Verify all nine v4 tables, constraints, migration records and application health before considering the read surface ready.
5. Keep v4 mutations, chat and runtime capabilities denied. Retiring legacy mutation routes does not authorize a replacement execution path.
No deployment, live DDL, baseline resolution or migration SQL change is part of this hardening milestone. Schema rollout remains a separate gate, not full-system completion.
Rollback is application-first: stop use of the new module while retaining append-only records. Do not drop tables in automated rollback; removal requires separately reviewed archival migration.
@@ -0,0 +1,8 @@
CREATE TABLE support_conversations (id UUID NOT NULL, user_id UUID, anonymous_browser_id VARCHAR(64), entry_path VARCHAR(512), product_slugs JSONB, topics JSONB, unanswered BOOLEAN NOT NULL DEFAULT false, needs_followup BOOLEAN NOT NULL DEFAULT false, escalation_signal BOOLEAN NOT NULL DEFAULT false, summary TEXT, summary_status VARCHAR(20) NOT NULL DEFAULT 'pending', created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, expires_at TIMESTAMPTZ NOT NULL, CONSTRAINT support_conversations_pkey PRIMARY KEY (id));
CREATE TABLE support_conversation_messages (id UUID NOT NULL, conversation_id UUID NOT NULL, role VARCHAR(20) NOT NULL, content TEXT NOT NULL, sequence INTEGER NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT support_conversation_messages_pkey PRIMARY KEY (id));
CREATE UNIQUE INDEX support_conversation_messages_conversation_id_sequence_key ON support_conversation_messages(conversation_id, sequence);
CREATE INDEX support_conversations_user_id_updated_at_idx ON support_conversations(user_id, updated_at);
CREATE INDEX support_conversations_anonymous_browser_id_updated_at_idx ON support_conversations(anonymous_browser_id, updated_at);
CREATE INDEX support_conversation_messages_conversation_id_created_at_idx ON support_conversation_messages(conversation_id, created_at);
ALTER TABLE support_conversations ADD CONSTRAINT support_conversations_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE support_conversation_messages ADD CONSTRAINT support_conversation_messages_conversation_id_fkey FOREIGN KEY (conversation_id) REFERENCES support_conversations(id) ON DELETE CASCADE ON UPDATE CASCADE;
@@ -0,0 +1,3 @@
ALTER TABLE blogs ADD COLUMN IF NOT EXISTS author_team VARCHAR(200);
ALTER TABLE blogs ADD COLUMN IF NOT EXISTS author_role VARCHAR(200);
ALTER TABLE blogs ADD COLUMN IF NOT EXISTS content_updated_at TIMESTAMPTZ;
@@ -0,0 +1,211 @@
-- CreateTable
CREATE TABLE "orchestration_tasks" (
"id" UUID NOT NULL,
"key" VARCHAR(120) NOT NULL,
"title" VARCHAR(255) NOT NULL,
"department" VARCHAR(50) NOT NULL,
"state" VARCHAR(40) NOT NULL DEFAULT 'DRAFT',
"operation_mode" VARCHAR(30) NOT NULL,
"model_tier" VARCHAR(2),
"budget_limit_cents" INTEGER NOT NULL,
"version" INTEGER NOT NULL DEFAULT 1,
"metadata" JSONB NOT NULL DEFAULT '{}',
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMPTZ NOT NULL,
CONSTRAINT "orchestration_tasks_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "orchestration_runs" (
"id" UUID NOT NULL,
"task_id" UUID NOT NULL,
"attempt" INTEGER NOT NULL DEFAULT 1,
"status" VARCHAR(30) NOT NULL DEFAULT 'PENDING',
"commit_sha" VARCHAR(64) NOT NULL,
"environment" VARCHAR(50) NOT NULL,
"retry_class" VARCHAR(30),
"idempotency_key" VARCHAR(150) NOT NULL,
"started_at" TIMESTAMPTZ,
"finished_at" TIMESTAMPTZ,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "orchestration_runs_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "orchestration_artifacts" (
"id" UUID NOT NULL,
"run_id" UUID NOT NULL,
"kind" VARCHAR(50) NOT NULL,
"uri" TEXT NOT NULL,
"sha256" CHAR(64) NOT NULL,
"size_bytes" BIGINT,
"metadata" JSONB NOT NULL DEFAULT '{}',
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "orchestration_artifacts_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "orchestration_attestations" (
"id" UUID NOT NULL,
"run_id" UUID NOT NULL,
"kind" VARCHAR(50) NOT NULL,
"subject_hash" CHAR(64) NOT NULL,
"predicate" JSONB NOT NULL,
"signer" VARCHAR(150) NOT NULL,
"signature" TEXT,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "orchestration_attestations_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "orchestration_dependencies" (
"id" UUID NOT NULL,
"task_id" UUID NOT NULL,
"depends_on_task_id" UUID NOT NULL,
"kind" VARCHAR(30) NOT NULL DEFAULT 'BLOCKS',
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "orchestration_dependencies_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "orchestration_approval_decisions" (
"id" UUID NOT NULL,
"task_id" UUID NOT NULL,
"gate" INTEGER NOT NULL,
"decision" VARCHAR(20) NOT NULL,
"actor_id" VARCHAR(100) NOT NULL,
"notes" TEXT,
"expires_at" TIMESTAMPTZ NOT NULL,
"idempotency_key" VARCHAR(150) NOT NULL,
"run_id" UUID,
"artifact_hash" CHAR(64),
"commit_sha" VARCHAR(64),
"environment" VARCHAR(50),
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "orchestration_approval_decisions_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "orchestration_events" (
"id" UUID NOT NULL,
"task_id" UUID,
"event_type" VARCHAR(60) NOT NULL,
"from_state" VARCHAR(40),
"to_state" VARCHAR(40),
"sequence" INTEGER NOT NULL,
"actor_id" VARCHAR(100) NOT NULL,
"idempotency_key" VARCHAR(150),
"payload" JSONB NOT NULL DEFAULT '{}',
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "orchestration_events_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "orchestration_budget_ledger" (
"id" UUID NOT NULL,
"task_id" UUID NOT NULL,
"amount_cents" INTEGER NOT NULL,
"kind" VARCHAR(40) NOT NULL,
"idempotency_key" VARCHAR(150) NOT NULL,
"actor_id" VARCHAR(100) NOT NULL,
"metadata" JSONB NOT NULL DEFAULT '{}',
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "orchestration_budget_ledger_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "orchestration_kill_switches" (
"scope" VARCHAR(80) NOT NULL,
"level" VARCHAR(30) NOT NULL,
"active" BOOLEAN NOT NULL DEFAULT true,
"reason" TEXT NOT NULL,
"actor_id" VARCHAR(100) NOT NULL,
"version" INTEGER NOT NULL DEFAULT 1,
"updated_at" TIMESTAMPTZ NOT NULL,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "orchestration_kill_switches_pkey" PRIMARY KEY ("scope")
);
-- CreateIndex
CREATE UNIQUE INDEX "orchestration_tasks_key_key" ON "orchestration_tasks"("key");
-- CreateIndex
CREATE INDEX "orchestration_tasks_state_created_at_idx" ON "orchestration_tasks"("state", "created_at");
-- CreateIndex
CREATE INDEX "orchestration_tasks_department_state_idx" ON "orchestration_tasks"("department", "state");
-- CreateIndex
CREATE UNIQUE INDEX "orchestration_runs_idempotency_key_key" ON "orchestration_runs"("idempotency_key");
-- CreateIndex
CREATE INDEX "orchestration_runs_task_id_status_idx" ON "orchestration_runs"("task_id", "status");
-- CreateIndex
CREATE UNIQUE INDEX "orchestration_runs_task_id_attempt_key" ON "orchestration_runs"("task_id", "attempt");
-- CreateIndex
CREATE UNIQUE INDEX "orchestration_artifacts_run_id_sha256_key" ON "orchestration_artifacts"("run_id", "sha256");
-- CreateIndex
CREATE UNIQUE INDEX "orchestration_attestations_run_id_kind_subject_hash_signer_key" ON "orchestration_attestations"("run_id", "kind", "subject_hash", "signer");
-- CreateIndex
CREATE UNIQUE INDEX "orchestration_dependencies_task_id_depends_on_task_id_key" ON "orchestration_dependencies"("task_id", "depends_on_task_id");
-- CreateIndex
CREATE UNIQUE INDEX "orchestration_approval_decisions_idempotency_key_key" ON "orchestration_approval_decisions"("idempotency_key");
-- CreateIndex
CREATE INDEX "orchestration_approval_decisions_task_id_gate_created_at_idx" ON "orchestration_approval_decisions"("task_id", "gate", "created_at");
-- CreateIndex
CREATE UNIQUE INDEX "orchestration_events_idempotency_key_key" ON "orchestration_events"("idempotency_key");
-- CreateIndex
CREATE INDEX "orchestration_events_event_type_created_at_idx" ON "orchestration_events"("event_type", "created_at");
-- CreateIndex
CREATE UNIQUE INDEX "orchestration_events_task_id_sequence_key" ON "orchestration_events"("task_id", "sequence");
-- CreateIndex
CREATE UNIQUE INDEX "orchestration_budget_ledger_idempotency_key_key" ON "orchestration_budget_ledger"("idempotency_key");
-- CreateIndex
CREATE INDEX "orchestration_budget_ledger_task_id_created_at_idx" ON "orchestration_budget_ledger"("task_id", "created_at");
-- AddForeignKey
ALTER TABLE "orchestration_runs" ADD CONSTRAINT "orchestration_runs_task_id_fkey" FOREIGN KEY ("task_id") REFERENCES "orchestration_tasks"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "orchestration_artifacts" ADD CONSTRAINT "orchestration_artifacts_run_id_fkey" FOREIGN KEY ("run_id") REFERENCES "orchestration_runs"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "orchestration_attestations" ADD CONSTRAINT "orchestration_attestations_run_id_fkey" FOREIGN KEY ("run_id") REFERENCES "orchestration_runs"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "orchestration_dependencies" ADD CONSTRAINT "orchestration_dependencies_task_id_fkey" FOREIGN KEY ("task_id") REFERENCES "orchestration_tasks"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "orchestration_dependencies" ADD CONSTRAINT "orchestration_dependencies_depends_on_task_id_fkey" FOREIGN KEY ("depends_on_task_id") REFERENCES "orchestration_tasks"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "orchestration_approval_decisions" ADD CONSTRAINT "orchestration_approval_decisions_task_id_fkey" FOREIGN KEY ("task_id") REFERENCES "orchestration_tasks"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "orchestration_approval_decisions" ADD CONSTRAINT "orchestration_approval_decisions_run_id_fkey" FOREIGN KEY ("run_id") REFERENCES "orchestration_runs"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "orchestration_events" ADD CONSTRAINT "orchestration_events_task_id_fkey" FOREIGN KEY ("task_id") REFERENCES "orchestration_tasks"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "orchestration_budget_ledger" ADD CONSTRAINT "orchestration_budget_ledger_task_id_fkey" FOREIGN KEY ("task_id") REFERENCES "orchestration_tasks"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
@@ -0,0 +1,10 @@
-- Immutable evidence and accounting; corrections are new entries.
CREATE FUNCTION orchestration_append_only() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN RAISE EXCEPTION 'Orchestration append-only record cannot be changed'; END;
$$;
CREATE TRIGGER orchestration_budget_immutable BEFORE UPDATE OR DELETE ON orchestration_budget_ledger FOR EACH ROW EXECUTE FUNCTION orchestration_append_only();
CREATE TRIGGER orchestration_events_immutable BEFORE UPDATE OR DELETE ON orchestration_events FOR EACH ROW EXECUTE FUNCTION orchestration_append_only();
CREATE TRIGGER orchestration_approvals_immutable BEFORE UPDATE OR DELETE ON orchestration_approval_decisions FOR EACH ROW EXECUTE FUNCTION orchestration_append_only();
CREATE TRIGGER orchestration_artifacts_immutable BEFORE UPDATE OR DELETE ON orchestration_artifacts FOR EACH ROW EXECUTE FUNCTION orchestration_append_only();
CREATE TRIGGER orchestration_attestations_immutable BEFORE UPDATE OR DELETE ON orchestration_attestations FOR EACH ROW EXECUTE FUNCTION orchestration_append_only();
ALTER TABLE orchestration_tasks ADD CONSTRAINT orchestration_budget_nonnegative CHECK (budget_limit_cents >= 0);
@@ -0,0 +1,10 @@
CREATE TABLE prepare_workflow_outbox (
id uuid PRIMARY KEY, task_id uuid NOT NULL UNIQUE REFERENCES orchestration_tasks(id) ON DELETE RESTRICT,
status varchar(20) NOT NULL DEFAULT 'PENDING' CHECK (status IN ('PENDING','DONE')),
attempts integer NOT NULL DEFAULT 0 CHECK (attempts BETWEEN 0 AND 3),
lease_owner varchar(100), lease_token uuid, lease_until timestamptz, heartbeat_at timestamptz,
run_id uuid REFERENCES orchestration_runs(id) ON DELETE RESTRICT,
reservation_id uuid REFERENCES orchestration_budget_ledger(id) ON DELETE RESTRICT,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX prepare_workflow_outbox_status_lease_until_idx ON prepare_workflow_outbox(status,lease_until);
+200 -2
View File
@@ -32,12 +32,49 @@ model User {
tickets Ticket[]
reviews ProductReview[]
blogComments BlogComment[]
supportConversations SupportConversation[]
@@index([role])
@@index([createdAt])
@@map("users")
}
model SupportConversation {
id String @id @default(uuid()) @db.Uuid
userId String? @map("user_id") @db.Uuid
anonymousBrowserId String? @map("anonymous_browser_id") @db.VarChar(64)
entryPath String? @map("entry_path") @db.VarChar(512)
productSlugs Json? @map("product_slugs")
topics Json?
unanswered Boolean @default(false)
needsFollowup Boolean @default(false) @map("needs_followup")
escalationSignal Boolean @default(false) @map("escalation_signal")
summary String? @db.Text
summaryStatus String @default("pending") @map("summary_status") @db.VarChar(20)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz()
expiresAt DateTime @map("expires_at") @db.Timestamptz()
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
messages SupportConversationMessage[]
@@index([userId, updatedAt])
@@index([anonymousBrowserId, updatedAt])
@@map("support_conversations")
}
model SupportConversationMessage {
id String @id @default(uuid()) @db.Uuid
conversationId String @map("conversation_id") @db.Uuid
role String @db.VarChar(20)
content String @db.Text
sequence Int
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
conversation SupportConversation @relation(fields: [conversationId], references: [id], onDelete: Cascade)
@@unique([conversationId, sequence])
@@index([conversationId, createdAt])
@@map("support_conversation_messages")
}
model UserAddress {
id String @id @default(uuid()) @db.Uuid
userId String @map("user_id") @db.Uuid
@@ -141,7 +178,9 @@ model Blog {
slug String @unique @db.VarChar(200)
content String @db.Text
excerpt String? @db.Text
authorId String @map("author_id") @db.Uuid
authorId String? @map("author_id") @db.Uuid
authorTeam String? @map("author_team") @db.VarChar(200)
authorRole String? @map("author_role") @db.VarChar(200)
categoryId String? @map("category_id") @db.Uuid
imageUrl String? @map("image_url") @db.Text
imageAlt String? @map("image_alt") @db.VarChar(255)
@@ -161,6 +200,7 @@ model Blog {
isPublished Boolean @default(true) @map("is_published")
publishedAt DateTime? @map("published_at") @db.Timestamptz()
scheduledAt DateTime? @map("scheduled_at") @db.Timestamptz()
contentUpdatedAt DateTime? @map("content_updated_at") @db.Timestamptz()
readingTime Int @default(3) @map("reading_time")
featured Boolean @default(false)
allowComments Boolean @default(true) @map("allow_comments")
@@ -172,7 +212,7 @@ model Blog {
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
updatedAt DateTime @default(now()) @updatedAt @map("updated_at") @db.Timestamptz()
author User @relation(fields: [authorId], references: [id])
author User? @relation(fields: [authorId], references: [id])
category BlogCategory? @relation(fields: [categoryId], references: [id], onDelete: SetNull)
tags BlogTag[]
comments BlogComment[]
@@ -1051,3 +1091,161 @@ model DepartmentTask {
@@index([createdAt])
@@map("department_tasks")
}
// Orchestration v4 additive models. Legacy governance tables remain untouched.
model OrchestrationTask {
id String @id @default(uuid()) @db.Uuid
key String @unique @db.VarChar(120)
title String @db.VarChar(255)
department String @db.VarChar(50)
state String @default("DRAFT") @db.VarChar(40)
operationMode String @map("operation_mode") @db.VarChar(30)
modelTier String? @map("model_tier") @db.VarChar(2)
budgetLimitCents Int @map("budget_limit_cents")
version Int @default(1)
metadata Json @default("{}") @db.JsonB
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz()
runs OrchestrationRun[]
dependencies OrchestrationDependency[] @relation("task_dependencies")
dependedOnBy OrchestrationDependency[] @relation("task_dependents")
decisions OrchestrationApprovalDecision[]
events OrchestrationEvent[]
budgetLedger OrchestrationBudgetLedger[]
@@index([state, createdAt])
@@index([department, state])
@@map("orchestration_tasks")
}
model OrchestrationRun {
id String @id @default(uuid()) @db.Uuid
taskId String @map("task_id") @db.Uuid
attempt Int @default(1)
status String @default("PENDING") @db.VarChar(30)
commitSha String @map("commit_sha") @db.VarChar(64)
environment String @db.VarChar(50)
retryClass String? @map("retry_class") @db.VarChar(30)
idempotencyKey String @unique @map("idempotency_key") @db.VarChar(150)
startedAt DateTime? @map("started_at") @db.Timestamptz()
finishedAt DateTime? @map("finished_at") @db.Timestamptz()
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
task OrchestrationTask @relation(fields:[taskId],references:[id],onDelete:Restrict)
artifacts OrchestrationArtifact[]
attestations OrchestrationAttestation[]
approvals OrchestrationApprovalDecision[]
@@unique([taskId, attempt])
@@index([taskId, status])
@@map("orchestration_runs")
}
model OrchestrationArtifact {
id String @id @default(uuid()) @db.Uuid
runId String @map("run_id") @db.Uuid
kind String @db.VarChar(50)
uri String @db.Text
sha256 String @db.Char(64)
sizeBytes BigInt? @map("size_bytes")
metadata Json @default("{}") @db.JsonB
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
run OrchestrationRun @relation(fields:[runId],references:[id],onDelete:Restrict)
@@unique([runId, sha256])
@@map("orchestration_artifacts")
}
model OrchestrationAttestation {
id String @id @default(uuid()) @db.Uuid
runId String @map("run_id") @db.Uuid
kind String @db.VarChar(50)
subjectHash String @map("subject_hash") @db.Char(64)
predicate Json @db.JsonB
signer String @db.VarChar(150)
signature String? @db.Text
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
run OrchestrationRun @relation(fields:[runId],references:[id],onDelete:Restrict)
@@unique([runId, kind, subjectHash, signer])
@@map("orchestration_attestations")
}
model OrchestrationDependency {
id String @id @default(uuid()) @db.Uuid
taskId String @map("task_id") @db.Uuid
dependsOnTaskId String @map("depends_on_task_id") @db.Uuid
kind String @default("BLOCKS") @db.VarChar(30)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
task OrchestrationTask @relation("task_dependencies",fields:[taskId],references:[id],onDelete:Restrict)
dependsOnTask OrchestrationTask @relation("task_dependents",fields:[dependsOnTaskId],references:[id],onDelete:Restrict)
@@unique([taskId, dependsOnTaskId])
@@map("orchestration_dependencies")
}
model OrchestrationApprovalDecision {
id String @id @default(uuid()) @db.Uuid
taskId String @map("task_id") @db.Uuid
gate Int
decision String @db.VarChar(20)
actorId String @map("actor_id") @db.VarChar(100)
notes String? @db.Text
expiresAt DateTime @map("expires_at") @db.Timestamptz()
idempotencyKey String @unique @map("idempotency_key") @db.VarChar(150)
runId String? @map("run_id") @db.Uuid
artifactHash String? @map("artifact_hash") @db.Char(64)
commitSha String? @map("commit_sha") @db.VarChar(64)
environment String? @db.VarChar(50)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
task OrchestrationTask @relation(fields:[taskId],references:[id],onDelete:Restrict)
run OrchestrationRun? @relation(fields:[runId],references:[id],onDelete:Restrict)
@@index([taskId, gate, createdAt])
@@map("orchestration_approval_decisions")
}
model OrchestrationEvent {
id String @id @default(uuid()) @db.Uuid
taskId String? @map("task_id") @db.Uuid
eventType String @map("event_type") @db.VarChar(60)
fromState String? @map("from_state") @db.VarChar(40)
toState String? @map("to_state") @db.VarChar(40)
sequence Int
actorId String @map("actor_id") @db.VarChar(100)
idempotencyKey String? @unique @map("idempotency_key") @db.VarChar(150)
payload Json @default("{}") @db.JsonB
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
task OrchestrationTask? @relation(fields:[taskId],references:[id],onDelete:Restrict)
@@unique([taskId, sequence])
@@index([eventType, createdAt])
@@map("orchestration_events")
}
model OrchestrationBudgetLedger {
id String @id @default(uuid()) @db.Uuid
taskId String @map("task_id") @db.Uuid
amountCents Int @map("amount_cents")
kind String @db.VarChar(40)
idempotencyKey String @unique @map("idempotency_key") @db.VarChar(150)
actorId String @map("actor_id") @db.VarChar(100)
metadata Json @default("{}") @db.JsonB
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
task OrchestrationTask @relation(fields:[taskId],references:[id],onDelete:Restrict)
@@index([taskId, createdAt])
@@map("orchestration_budget_ledger")
}
model OrchestrationKillSwitch {
scope String @id @db.VarChar(80)
level String @db.VarChar(30)
active Boolean @default(true)
reason String @db.Text
actorId String @map("actor_id") @db.VarChar(100)
version Int @default(1)
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz()
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
@@map("orchestration_kill_switches")
}
// Transactional dispatch intent. Lease tokens are server-only fencing credentials.
model PrepareWorkflowOutbox {
id String @id @default(uuid()) @db.Uuid
taskId String @unique @map("task_id") @db.Uuid
status String @default("PENDING") @db.VarChar(20)
attempts Int @default(0)
leaseOwner String? @map("lease_owner") @db.VarChar(100)
leaseToken String? @map("lease_token") @db.Uuid
leaseUntil DateTime? @map("lease_until") @db.Timestamptz()
heartbeatAt DateTime? @map("heartbeat_at") @db.Timestamptz()
runId String? @map("run_id") @db.Uuid
reservationId String? @map("reservation_id") @db.Uuid
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz()
@@index([status, leaseUntil])
@@map("prepare_workflow_outbox")
}
+33 -3
View File
@@ -57,7 +57,7 @@ export class AdminController {
@ApiQuery({ name: "search", required: false, description: "جستجو" })
async getUsers(@Query() query: AdminQueryDto) {
const result = await this.adminService.getUsers(query);
return { success: true, ...result };
return result;
}
@Post("users")
@@ -145,7 +145,7 @@ export class AdminController {
@ApiQuery({ name: "search", required: false, description: "جستجو" })
async getProducts(@Query() query: AdminQueryDto) {
const result = await this.adminService.getProducts(query);
return { success: true, ...result };
return result;
}
@Post("products")
@@ -206,7 +206,7 @@ export class AdminController {
@ApiQuery({ name: "status", required: false, description: "فیلتر وضعیت" })
async getOrders(@Query() query: AdminQueryDto) {
const result = await this.adminService.getOrders(query);
return { success: true, ...result };
return result;
}
@Get("orders/:id")
@@ -354,4 +354,34 @@ export class AdminController {
await this.settingsService.updateBulkUiTexts(body);
return { success: true, data: body };
}
@Get("symptoms")
@ApiOperation({ summary: "لیست کامل علائم درمانی با صفحه‌بندی، جستجو و متادیتا" })
async getSymptoms(@Query() query: { page?: number; limit?: number; search?: string }) {
const res = await this.adminService.getSymptoms(query);
return res;
}
@Post("symptoms")
@ApiOperation({ summary: "افزودن علامت درمانی جدید" })
async createSymptom(@Body() body: { name: string; slug?: string; description?: string; metaTitle?: string; metaDescription?: string; keywords?: string }) {
const res = await this.adminService.createSymptom(body);
return { success: true, data: res };
}
@Put("symptoms/:id")
@ApiOperation({ summary: "ویرایش کامل علامت درمانی و متادیتاهای سئو" })
async updateSymptom(@Param("id") id: string, @Body() body: any) {
const res = await this.adminService.updateSymptom(id, body);
return { success: true, data: res };
}
@Delete("symptoms/:id")
@ApiOperation({ summary: "حذف علامت درمانی" })
async deleteSymptom(@Param("id") id: string) {
const res = await this.adminService.deleteSymptom(id);
return res;
}
}
+4
View File
@@ -27,6 +27,8 @@ import { DepartmentTasksController } from "./department-tasks.controller";
import { PrismaModule } from "../prisma/prisma.module";
import { RedisModule } from "../redis/redis.module";
import { SettingsModule } from "../settings/settings.module";
import { SupportChatAnalyticsController } from "./support-chat-analytics.controller";
import { SupportChatAnalyticsService } from "./support-chat-analytics.service";
@Module({
imports: [PrismaModule, RedisModule, SettingsModule],
@@ -47,6 +49,7 @@ import { SettingsModule } from "../settings/settings.module";
ApprovalsController,
AuditLogsController,
DepartmentTasksController,
SupportChatAnalyticsController,
],
providers: [
AdminService,
@@ -58,6 +61,7 @@ import { SettingsModule } from "../settings/settings.module";
PetsService,
SslService,
ApiKeysService,
SupportChatAnalyticsService,
],
exports: [ApiKeysService, MediaService],
})
+160
View File
@@ -1869,4 +1869,164 @@ export class AdminService {
};
});
}
async getSymptoms(params?: { page?: number; limit?: number; search?: string }) {
const page = Math.max(1, Number(params?.page) || 1);
const limit = Math.max(1, Number(params?.limit) || 20);
const offset = (page - 1) * limit;
const search = params?.search?.trim() || '';
let items: any[] = [];
let total = 0;
if (search) {
const searchPattern = `%${search}%`;
const [listResult, countResult] = await Promise.all([
this.prisma.$queryRaw<any[]>`
SELECT
s.id,
s.name,
s.slug,
s.description,
s.meta_title as "metaTitle",
s.meta_description as "metaDescription",
s.keywords,
s.icon,
s.created_at as "createdAt",
COALESCE(COUNT(ps.product_id), 0)::int as "productsCount"
FROM symptoms s
LEFT JOIN product_symptoms ps ON ps.symptom = s.name
WHERE s.name ILIKE ${searchPattern} OR s.slug ILIKE ${searchPattern}
GROUP BY s.id, s.name, s.slug, s.description, s.meta_title, s.meta_description, s.keywords, s.icon, s.created_at
ORDER BY s.name ASC
LIMIT ${limit} OFFSET ${offset};
`,
this.prisma.$queryRaw<any[]>`
SELECT COUNT(*)::int as total
FROM symptoms s
WHERE s.name ILIKE ${searchPattern} OR s.slug ILIKE ${searchPattern};
`,
]);
items = listResult;
total = countResult[0]?.total || 0;
} else {
const [listResult, countResult] = await Promise.all([
this.prisma.$queryRaw<any[]>`
SELECT
s.id,
s.name,
s.slug,
s.description,
s.meta_title as "metaTitle",
s.meta_description as "metaDescription",
s.keywords,
s.icon,
s.created_at as "createdAt",
COALESCE(COUNT(ps.product_id), 0)::int as "productsCount"
FROM symptoms s
LEFT JOIN product_symptoms ps ON ps.symptom = s.name
GROUP BY s.id, s.name, s.slug, s.description, s.meta_title, s.meta_description, s.keywords, s.icon, s.created_at
ORDER BY s.name ASC
LIMIT ${limit} OFFSET ${offset};
`,
this.prisma.$queryRaw<any[]>`
SELECT COUNT(*)::int as total
FROM symptoms s;
`,
]);
items = listResult;
total = countResult[0]?.total || 0;
}
const lastPage = Math.ceil(total / limit) || 1;
return {
data: items,
meta: {
page,
limit,
total,
lastPage,
},
};
}
async createSymptom(dto: { name: string; slug?: string; description?: string; metaTitle?: string; metaDescription?: string; keywords?: string }) {
if (!dto.name?.trim()) throw new BadRequestException('نام علامت الزامی است');
const name = dto.name.trim();
const slug = dto.slug?.trim() || name.replace(/\s+/g, '-').toLowerCase();
return await this.prisma.$queryRawUnsafe(`
INSERT INTO symptoms (name, slug, description, meta_title, meta_description, keywords)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id, name, slug, description, meta_title as "metaTitle", meta_description as "metaDescription", keywords;
`, name, slug, dto.description || '', dto.metaTitle || '', dto.metaDescription || '', dto.keywords || '');
}
async updateSymptom(id: string, dto: { name?: string; slug?: string; description?: string; metaTitle?: string; metaDescription?: string; keywords?: string }) {
// 1. Get existing symptom
const existing = await this.prisma.$queryRawUnsafe<any[]>(`SELECT * FROM symptoms WHERE id = $1::uuid LIMIT 1`, id);
if (!existing || existing.length === 0) throw new NotFoundException('علامت یافت نشد');
const oldName = existing[0].name;
const newName = dto.name?.trim() || oldName;
const newSlug = dto.slug?.trim() || existing[0].slug;
return await this.prisma.$transaction(async (tx) => {
// Update symptoms table
const updated = await tx.$queryRawUnsafe(`
UPDATE symptoms
SET
name = $2,
slug = $3,
description = $4,
meta_title = $5,
meta_description = $6,
keywords = $7,
updated_at = CURRENT_TIMESTAMP
WHERE id = $1::uuid
RETURNING id, name, slug, description, meta_title as "metaTitle", meta_description as "metaDescription", keywords;
`, id, newName, newSlug, dto.description ?? existing[0].description, dto.metaTitle ?? existing[0].meta_title, dto.metaDescription ?? existing[0].meta_description, dto.keywords ?? existing[0].keywords);
// If name changed, cascade update across product_symptoms & products JSON
if (oldName !== newName) {
await tx.$executeRaw`UPDATE product_symptoms SET symptom = ${newName} WHERE symptom = ${oldName};`;
await tx.$executeRaw`
UPDATE products
SET dosage_config = jsonb_set(
dosage_config,
'{symptoms}',
(
SELECT jsonb_agg(
CASE
WHEN elem::text = to_jsonb(${oldName}::text)::text THEN to_jsonb(${newName}::text)
ELSE elem
END
)
FROM jsonb_array_elements(dosage_config->'symptoms') AS elem
)
)
WHERE dosage_config ? 'symptoms'
AND dosage_config->'symptoms' @> to_jsonb(${oldName}::text);
`;
}
return updated;
});
}
async deleteSymptom(id: string) {
const existing = await this.prisma.$queryRawUnsafe<any[]>(`SELECT * FROM symptoms WHERE id = $1::uuid LIMIT 1`, id);
if (!existing || existing.length === 0) throw new NotFoundException('علامت یافت نشد');
const name = existing[0].name;
return await this.prisma.$transaction(async (tx) => {
// Delete join links
await tx.$executeRaw`DELETE FROM product_symptoms WHERE symptom = ${name};`;
// Delete symptom row
await tx.$queryRawUnsafe(`DELETE FROM symptoms WHERE id = $1::uuid;`, id);
return { success: true, message: `علامت «${name}» با موفقیت حذف شد` };
});
}
}
@@ -0,0 +1,25 @@
import { GoneException } from '@nestjs/common';
import { ApprovalsController } from './approvals.controller';
import { PrismaService } from '../prisma/prisma.service';
describe('ApprovalsController retired mutation boundary',()=>{
const access=jest.fn();
const prisma=new Proxy({},{get:(_target,key)=>{access(key);throw new Error('Database access forbidden');}});
const controller=new ApprovalsController(prisma as PrismaService);
beforeEach(()=>access.mockClear());
it('create {department:SEO,category:SEO,title:Legacy,payload:{}} returns 410 without reading or writing persistence',async()=>{
await expect(controller.create({department:'SEO',category:'SEO',title:'Legacy',payload:{}})).rejects.toBeInstanceOf(GoneException);
await expect(controller.create({department:'SEO',category:'SEO',title:'Legacy',payload:{}})).rejects.toMatchObject({status:410});
expect(access).not.toHaveBeenCalled();
});
it('decide missing, {decision:APPROVED} returns 410 without reading or writing persistence',async()=>{
await expect(controller.decide('missing', {decision:'APPROVED'})).rejects.toBeInstanceOf(GoneException);
await expect(controller.decide('missing', {decision:'APPROVED'})).rejects.toMatchObject({status:410});
expect(access).not.toHaveBeenCalled();
});
it('decide missing, {decision:REJECTED} returns 410 without reading or writing persistence',async()=>{
await expect(controller.decide('missing', {decision:'REJECTED'})).rejects.toBeInstanceOf(GoneException);
await expect(controller.decide('missing', {decision:'REJECTED'})).rejects.toMatchObject({status:410});
expect(access).not.toHaveBeenCalled();
});
});
+3 -88
View File
@@ -3,13 +3,12 @@ import {
Get,
Post,
Put,
Delete,
Body,
Param,
Query,
UseGuards,
NotFoundException,
BadRequestException,
GoneException,
} from "@nestjs/common";
import { ApiTags, ApiBearerAuth, ApiOperation } from "@nestjs/swagger";
import { JwtAuthGuard } from "../auth/jwt-auth.guard";
@@ -83,17 +82,7 @@ export class ApprovalsController {
@Post()
@ApiOperation({ summary: "ثبت آیتم جدید جهت تایید مدیریت (از طرف سیستم یا دپارتمان‌ها)" })
async create(@Body() dto: CreateApprovalRequestDto) {
const item = await (this.prisma as any).approvalRequest.create({
data: {
department: dto.department,
category: dto.category,
title: dto.title,
payload: dto.payload,
testContext: dto.testContext || {},
status: "PENDING",
},
});
return { success: true, data: item };
throw new GoneException("Legacy orchestration mutations are retired. Orchestration v4 remains read-only; no replacement mutation is enabled.");
}
@Post(":id/test")
@@ -106,7 +95,6 @@ export class ApprovalsController {
throw new NotFoundException(`Approval request ${id} not found`);
}
// شبیه‌سازی بسته به دسته‌بندی
let testResult: any = { status: "SIMULATION_SUCCESS" };
if (item.category === "PRICING") {
const payload = item.payload as any;
@@ -140,80 +128,7 @@ export class ApprovalsController {
@Put(":id/decide")
@ApiOperation({ summary: "تصمیم‌گیری مدیرعامل (تایید یا رد همراه با یادداشت)" })
async decide(@Param("id") id: string, @Body() dto: DecideApprovalDto) {
if (!["APPROVED", "REJECTED"].includes(dto.decision)) {
throw new BadRequestException("Decision must be either APPROVED or REJECTED");
}
const item = await (this.prisma as any).approvalRequest.findUnique({
where: { id },
});
if (!item) {
throw new NotFoundException(`Approval request ${id} not found`);
}
if (dto.decision === "REJECTED") {
// 5. رد توسط مدیرعامل: تسک و درخواست کلاً از سیستم پاکسازی کامل می‌شوند
await (this.prisma as any).departmentTask.deleteMany({
where: { approvalId: id },
});
await (this.prisma as any).approvalRequest.delete({
where: { id },
});
return {
success: true,
message: "درخواست با موفقیت رد و از سیستم پاکسازی شد.",
};
}
// 1. تایید مدیرعامل: وضعیت درخواست APPROVED و وضعیت تسک در صف اقدام به TODO تغییر می‌یابد
const updated = await (this.prisma as any).approvalRequest.update({
where: { id },
data: {
status: "APPROVED",
ceoNotes: dto.ceoNotes || null,
executedAt: new Date(),
},
});
// جستجو یا ایجاد تسک متناظر با استاتوس TODO
const existingTask = await (this.prisma as any).departmentTask.findFirst({
where: { approvalId: id },
});
if (existingTask) {
await (this.prisma as any).departmentTask.update({
where: { id: existingTask.id },
data: {
status: "TODO",
description: dto.ceoNotes ? (existingTask.description || "") + "\n\n[یادداشت مدیرعامل]: " + dto.ceoNotes : existingTask.description,
},
});
} else {
await (this.prisma as any).departmentTask.create({
data: {
code: "TSK-" + Date.now().toString().slice(-6),
department: item.department,
title: item.title,
description: dto.ceoNotes ? (item.title + "\n\n[یادداشت مدیرعامل]: " + dto.ceoNotes) : item.title,
status: "TODO",
priority: "HIGH",
assignee: "AI_AGENT",
approvalId: item.id,
},
});
}
return {
success: true,
message: "درخواست تایید شد و مأموریت به صف اقدام (TODO) دپارتمان منتقل گردید.",
data: updated,
};
throw new GoneException("Legacy orchestration mutations are retired. Orchestration v4 remains read-only; no replacement mutation is enabled.");
}
@Delete(":id")
@ApiOperation({ summary: "حذف آیتم از کارتابل" })
async remove(@Param("id") id: string) {
await (this.prisma as any).approvalRequest.delete({ where: { id } });
return { success: true, message: "Deleted successfully" };
}
}
@@ -229,6 +229,15 @@ describe("BlogsController (Admin)", () => {
expect(result).toEqual({ success: true, data: createdBlog });
});
it("propagates create validation failures", async () => {
const req = { user: { id: "admin-user-id" } };
const body = { title: "Bad date", contentUpdatedAt: "invalid", createdAt: "2000-01-01" };
mockBlogsService.createBlog.mockRejectedValueOnce(new Error("Invalid contentUpdatedAt"));
await expect(controller.createBlog(body, req)).rejects.toThrow("Invalid contentUpdatedAt");
expect(service.createBlog).toHaveBeenCalledWith(body, req.user.id);
});
it("updateBlog should update blog details and return success: true", async () => {
const blogId = "blog-10";
const body = {
@@ -244,6 +253,14 @@ describe("BlogsController (Admin)", () => {
expect(result).toEqual({ success: true, data: updatedBlog });
});
it("propagates invalid content timestamp rejection without accepting createdAt", async () => {
const body = { contentUpdatedAt: "invalid", createdAt: "2000-01-01" };
mockBlogsService.updateBlog.mockRejectedValueOnce(new Error("Invalid contentUpdatedAt"));
await expect(controller.updateBlog("blog-10", body)).rejects.toThrow("Invalid contentUpdatedAt");
expect(service.updateBlog).toHaveBeenCalledWith("blog-10", body);
});
it("deleteBlog should delete blog and return success: true", async () => {
const blogId = "blog-10";
mockBlogsService.deleteBlog.mockResolvedValue(undefined);
+119 -1
View File
@@ -203,6 +203,50 @@ describe("Admin BlogsService", () => {
expect(result).toEqual(createdBlog);
});
it("allowlists create fields and blocks server-owned and unknown fields", async () => {
mockPrisma.blog.create.mockResolvedValue({ id: "created", slug: "safe-post" });
await service.createBlog(
{
title: "Safe post",
slug: "safe-post",
content: "Safe content",
id: "client-id",
createdAt: "2000-01-01T00:00:00.000Z",
updatedAt: "2000-01-01T00:00:00.000Z",
viewCount: 999999,
serverOwned: "must-not-pass",
},
"user-admin-1",
);
const payload = mockPrisma.blog.create.mock.calls[0][0].data;
expect(payload).toEqual(expect.objectContaining({
title: "Safe post",
slug: "safe-post",
content: "Safe content",
}));
expect(payload).not.toHaveProperty("id");
expect(payload).not.toHaveProperty("createdAt");
expect(payload).not.toHaveProperty("updatedAt");
expect(payload).not.toHaveProperty("viewCount");
expect(payload).not.toHaveProperty("serverOwned");
});
it.each([
["contentUpdatedAt", { contentUpdatedAt: "not-a-date" }, "Invalid contentUpdatedAt"],
["publishedAt", { publishedAt: "not-a-date" }, "Invalid publishedAt"],
["scheduledAt", { scheduledAt: "not-a-date" }, "Invalid scheduledAt"],
])("rejects invalid explicit %s", async (_field, timestamps, message) => {
await expect(
service.createBlog(
{ title: "Bad date", slug: "bad-date", content: "Content", ...timestamps },
"user-admin-1",
),
).rejects.toThrow(message);
expect(mockPrisma.blog.create).not.toHaveBeenCalled();
});
it("should create a SCHEDULED blog if scheduledAt is in the future", async () => {
const futureDate = new Date(Date.now() + 86400000).toISOString();
const blogData = {
@@ -274,6 +318,81 @@ describe("Admin BlogsService", () => {
});
});
describe("material freshness and scheduled publishing", () => {
it("sets contentUpdatedAt intentionally and never rewrites createdAt", async () => {
mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post", createdAt: new Date("2025-01-01") });
mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" });
await service.updateBlog("b1", { title: "Material edit", markContentUpdated: true });
const payload = mockPrisma.blog.update.mock.calls[0][0].data;
expect(payload.contentUpdatedAt).toEqual(expect.any(Date));
expect(payload).not.toHaveProperty("markContentUpdated");
expect(payload).not.toHaveProperty("createdAt");
});
it("automatically marks freshness when material article content changes", async () => {
mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post", content: "Old content" });
mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" });
await service.updateBlog("b1", { content: "Meaningfully revised content" });
expect(mockPrisma.blog.update.mock.calls[0][0].data.contentUpdatedAt).toEqual(expect.any(Date));
});
it("does not mark freshness for attribution-only changes", async () => {
mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post", content: "Same content" });
mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" });
await service.updateBlog("b1", { authorTeam: "Clinical team", authorRole: "Review" });
expect(mockPrisma.blog.update.mock.calls[0][0].data).not.toHaveProperty("contentUpdatedAt");
});
it("allows explicitly clearing contentUpdatedAt", async () => {
mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post" });
mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" });
await service.updateBlog("b1", { contentUpdatedAt: null });
expect(mockPrisma.blog.update.mock.calls[0][0].data.contentUpdatedAt).toBeNull();
});
it("publishes due posts without rewriting createdAt", async () => {
const scheduledAt = new Date("2025-02-01T10:00:00Z");
mockPrisma.blog.findMany.mockResolvedValue([{ id: "b1", slug: "post", title: "Post", scheduledAt }]);
mockPrisma.blog.update.mockResolvedValue({});
await service.publishDueScheduledBlogs();
expect(mockPrisma.blog.update).toHaveBeenCalledWith({ where: { id: "b1" }, data: { status: "PUBLISHED", isPublished: true, publishedAt: scheduledAt } });
});
});
describe("updateBlog security and freshness validation", () => {
beforeEach(() => {
mockPrisma.blog.findUnique.mockResolvedValue({ id: "blog-1", slug: "safe" });
mockPrisma.blog.update.mockResolvedValue({ id: "blog-1", slug: "safe" });
});
it("allowlists mutable fields and ignores createdAt and unrecognized fields", async () => {
await service.updateBlog("blog-1", {
title: "Allowed title",
createdAt: "2000-01-01T00:00:00.000Z",
serverOwned: "must-not-pass",
});
const payload = mockPrisma.blog.update.mock.calls[0][0].data;
expect(payload.title).toBe("Allowed title");
expect(payload).not.toHaveProperty("createdAt");
expect(payload).not.toHaveProperty("serverOwned");
});
it("rejects an invalid explicit contentUpdatedAt", async () => {
await expect(
service.updateBlog("blog-1", { contentUpdatedAt: "not-a-date" }),
).rejects.toThrow("Invalid contentUpdatedAt");
expect(mockPrisma.blog.update).not.toHaveBeenCalled();
});
it("preserves explicit null clearing and markContentUpdated precedence", async () => {
await service.updateBlog("blog-1", { contentUpdatedAt: null });
expect(mockPrisma.blog.update.mock.calls[0][0].data.contentUpdatedAt).toBeNull();
await service.updateBlog("blog-1", {
contentUpdatedAt: "not-a-date",
markContentUpdated: true,
});
expect(mockPrisma.blog.update.mock.calls[1][0].data.contentUpdatedAt).toEqual(expect.any(Date));
});
});
describe("deleteBlog", () => {
it("should throw NotFoundException if blog not found", async () => {
mockPrisma.blog.findUnique.mockResolvedValue(null);
@@ -468,7 +587,6 @@ describe("Admin BlogsService", () => {
status: "PUBLISHED",
isPublished: true,
publishedAt: scheduledDate,
createdAt: scheduledDate,
},
});
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith(
+120 -17
View File
@@ -1,4 +1,5 @@
import {
BadRequestException,
Injectable,
NotFoundException,
OnModuleInit,
@@ -141,16 +142,31 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
async createBlog(data: any, currentUserId: string) {
const {
tags,
tagNames,
categoryId,
authorId,
publishedAt,
scheduledAt,
readingTime,
...rest
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) {
@@ -171,13 +187,12 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
const targetAuthorId = authorId || currentUserId;
const now = new Date();
const parsedScheduledAt = scheduledAt ? new Date(scheduledAt) : null;
const isFutureSchedule = parsedScheduledAt && parsedScheduledAt > now;
let finalStatus = rest.status || "PUBLISHED";
let finalStatus = data.status || "PUBLISHED";
let finalIsPublished =
rest.isPublished !== undefined ? rest.isPublished : true;
let finalPublishedAt = publishedAt ? new Date(publishedAt) : null;
data.isPublished !== undefined ? data.isPublished : true;
let finalPublishedAt = parsedPublishedAt;
if (isFutureSchedule) {
finalStatus = "SCHEDULED";
@@ -191,19 +206,45 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
finalPublishedAt = now;
}
const createPayload: Prisma.BlogCreateInput = {
...rest,
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,
readingTime: readingTime ? Number(readingTime) : 3,
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,
@@ -223,19 +264,83 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
if (!blog) throw new NotFoundException("Blog not found");
const {
tags,
tagNames,
categoryId,
authorId,
publishedAt,
scheduledAt,
readingTime,
...rest
markContentUpdated,
contentUpdatedAt,
} = data;
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 updatePayload: Prisma.BlogUpdateInput = {
...rest,
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) }
: {}),
@@ -245,7 +350,7 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
? { category: { connect: { id: categoryId } } }
: { category: { disconnect: true } }
: {}),
};
});
if (scheduledAt !== undefined) {
const parsedScheduledAt = scheduledAt ? new Date(scheduledAt) : null;
@@ -259,7 +364,6 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
updatePayload.status = "PUBLISHED";
updatePayload.isPublished = true;
updatePayload.publishedAt = parsedScheduledAt;
updatePayload.createdAt = parsedScheduledAt;
}
} else if (publishedAt !== undefined) {
updatePayload.publishedAt = publishedAt ? new Date(publishedAt) : null;
@@ -472,7 +576,6 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
status: "PUBLISHED",
isPublished: true,
publishedAt: publishDate,
createdAt: publishDate,
},
});
@@ -0,0 +1,84 @@
import { BadRequestException } from "@nestjs/common";
import { assertTaskCanComplete } from "./department-task-completion.policy";
describe("department task completion policy", () => {
it("rejects model-only output without a live deliverable", () => {
expect(() =>
assertTaskCanComplete({
deliverableUrl: null,
acceptanceCriteria: { criteria: ["live URL responds 200"] },
executionLogs: [{ status: "COMPLETED_BY_AGENT" }],
}),
).toThrow(BadRequestException);
});
it("rejects completion without acceptance criteria", () => {
expect(() =>
assertTaskCanComplete({
deliverableUrl: "https://canina.ir/blog/verified",
acceptanceCriteria: null,
executionLogs: [{ status: "QA_PASSED" }],
}),
).toThrow(BadRequestException);
});
it("rejects completion without QA evidence", () => {
expect(() =>
assertTaskCanComplete({
deliverableUrl: "https://canina.ir/blog/verified",
acceptanceCriteria: { criteria: ["HTTP 200"] },
executionLogs: [{ status: "COMPLETED_BY_AGENT" }],
}),
).toThrow(BadRequestException);
});
it("allows completion with URL, criteria and QA evidence", () => {
expect(() =>
assertTaskCanComplete({
deliverableUrl: "https://canina.ir/blog/verified",
acceptanceCriteria: { criteria: ["HTTP 200"] },
executionLogs: [{ status: "QA_PASSED" }],
}),
).not.toThrow();
});
it("rejects pseudo-protocol canina:// deliverable URLs", () => {
expect(() =>
assertTaskCanComplete({
deliverableUrl: "canina://blog/sample-article",
acceptanceCriteria: { criteria: ["HTTP 200"] },
executionLogs: [{ status: "QA_PASSED" }],
}),
).toThrow(BadRequestException);
});
it("rejects git://stage staging or repo URLs as verifiable deliverable", () => {
expect(() =>
assertTaskCanComplete({
deliverableUrl: "git://stage/canina-repo#commit123",
acceptanceCriteria: { criteria: ["HTTP 200"] },
executionLogs: [{ status: "QA_PASSED" }],
}),
).toThrow(BadRequestException);
});
it("rejects missing or placeholder post-tsk article deliverables", () => {
expect(() =>
assertTaskCanComplete({
deliverableUrl: "https://canina.ir/blog/post-tsk-49102",
acceptanceCriteria: { criteria: ["HTTP 200"] },
executionLogs: [{ status: "QA_PASSED" }],
}),
).toThrow(BadRequestException);
});
it("rejects completion when QA evidence reports missing article or 404 error", () => {
expect(() =>
assertTaskCanComplete({
deliverableUrl: "https://canina.ir/blog/verified-article",
acceptanceCriteria: { criteria: ["HTTP 200"] },
executionLogs: [{ status: "QA_PASSED", note: "HTTP 404 - مقاله یافت نشد" }],
}),
).toThrow(BadRequestException);
});
});
@@ -0,0 +1,93 @@
import { BadRequestException } from "@nestjs/common";
export interface CompletableTask {
code?: string | null;
title?: string | null;
deliverableUrl?: string | null;
acceptanceCriteria?: unknown;
executionLogs?: unknown;
}
function hasAcceptanceCriteria(criteria: unknown): boolean {
if (!criteria) return false;
if (typeof criteria === "string") return criteria.trim().length > 0;
if (Array.isArray(criteria)) return criteria.length > 0;
if (typeof criteria === "object") {
const value = criteria as Record<string, unknown>;
const items = value.criteria;
return Array.isArray(items) ? items.length > 0 : Object.keys(value).length > 0;
}
return false;
}
function hasValidQaEvidence(logs: unknown): boolean {
if (!Array.isArray(logs)) return false;
const validPassStatuses = ["QA_PASSED", "VERIFIED", "PRODUCTION_VERIFIED"];
let foundValidPass = false;
for (const log of logs) {
if (!log || typeof log !== "object") continue;
const entry = log as Record<string, unknown>;
const status = String(entry.status || "").toUpperCase();
const note = String(entry.note || "").toLowerCase();
// Rejection of invalid QA claims: 404, missing article, not found, post-tsk
if (
note.includes("404") ||
note.includes("مقاله یافت نشد") ||
note.includes("not found") ||
note.includes("missing article") ||
note.includes("post-tsk")
) {
return false;
}
if (validPassStatuses.includes(status)) {
foundValidPass = true;
}
}
return foundValidPass;
}
export function assertTaskCanComplete(task: CompletableTask): void {
const rawUrl = task.deliverableUrl?.trim();
if (!rawUrl) {
throw new BadRequestException("DONE requires a verifiable deliverable URL");
}
const lowerUrl = rawUrl.toLowerCase();
// Reject internal pseudo-protocols
if (lowerUrl.startsWith("canina://") || lowerUrl.includes("canina://")) {
throw new BadRequestException("Deliverable URL cannot use pseudo-protocol canina://; an independently verifiable HTTP(S) URL is required");
}
// Reject git repository pointers or staging branches as production deliverables
if (lowerUrl.startsWith("git://") || lowerUrl.includes("git://stage")) {
throw new BadRequestException("Deliverable URL cannot be a git staging reference; an independently verifiable HTTP(S) URL is required");
}
// Must be an HTTP or HTTPS URL
if (!lowerUrl.startsWith("http://") && !lowerUrl.startsWith("https://")) {
throw new BadRequestException("DONE requires an independently verifiable HTTP or HTTPS deliverable URL");
}
// Reject deleted or placeholder post-tsk articles
if (/post[-_]?tsk/i.test(rawUrl)) {
throw new BadRequestException("Deliverable URL references an unverified or deleted post-tsk placeholder article");
}
if (task.title && /post[-_]?tsk/i.test(task.title)) {
throw new BadRequestException("Task references an unverified or deleted post-tsk article");
}
if (task.code && /post[-_]?tsk/i.test(task.code)) {
throw new BadRequestException("Task references an unverified or deleted post-tsk code");
}
if (!hasAcceptanceCriteria(task.acceptanceCriteria)) {
throw new BadRequestException("DONE requires explicit acceptance criteria");
}
if (!hasValidQaEvidence(task.executionLogs)) {
throw new BadRequestException("DONE requires QA/production verification evidence");
}
}
@@ -0,0 +1,25 @@
import { GoneException } from '@nestjs/common';
import { DepartmentTasksController } from './department-tasks.controller';
import { PrismaService } from '../prisma/prisma.service';
describe('DepartmentTasksController retired mutation boundary',()=>{
const access=jest.fn();
const prisma=new Proxy({},{get:(_target,key)=>{access(key);throw new Error('Database access forbidden');}});
const controller=new DepartmentTasksController(prisma as PrismaService);
beforeEach(()=>access.mockClear());
it('create {department:SEO,title:Legacy} returns 410 without reading or writing persistence',async()=>{
await expect(controller.create({department:'SEO',title:'Legacy'})).rejects.toBeInstanceOf(GoneException);
await expect(controller.create({department:'SEO',title:'Legacy'})).rejects.toMatchObject({status:410});
expect(access).not.toHaveBeenCalled();
});
it('updateStatus missing, {status:DONE} returns 410 without reading or writing persistence',async()=>{
await expect(controller.updateStatus('missing', {status:'DONE'})).rejects.toBeInstanceOf(GoneException);
await expect(controller.updateStatus('missing', {status:'DONE'})).rejects.toMatchObject({status:410});
expect(access).not.toHaveBeenCalled();
});
it('updateStatus missing, {status:TODO} returns 410 without reading or writing persistence',async()=>{
await expect(controller.updateStatus('missing', {status:'TODO'})).rejects.toBeInstanceOf(GoneException);
await expect(controller.updateStatus('missing', {status:'TODO'})).rejects.toMatchObject({status:410});
expect(access).not.toHaveBeenCalled();
});
});
@@ -3,12 +3,12 @@ import {
Get,
Post,
Put,
Delete,
Body,
Param,
Query,
UseGuards,
NotFoundException,
GoneException,
} from "@nestjs/common";
import { ApiTags, ApiBearerAuth, ApiOperation } from "@nestjs/swagger";
import { JwtAuthGuard } from "../auth/jwt-auth.guard";
@@ -112,37 +112,7 @@ export class DepartmentTasksController {
@Post()
@ApiOperation({ summary: "ایجاد تسک جدید سازمانی" })
async create(@Body() body: any) {
const code = body.code || "TSK-" + Date.now().toString().slice(-6);
const task = await (this.prisma as any).departmentTask.create({
data: {
code,
department: body.department,
title: body.title,
description: body.description || null,
status: body.status || "TODO",
priority: body.priority || "MEDIUM",
assignee: body.assignee || "AI_AGENT",
acceptanceCriteria: body.acceptanceCriteria || null,
executionLogs: body.executionLogs || [],
deliverableUrl: body.deliverableUrl || null,
startedAt: body.status === "IN_PROGRESS" ? new Date() : null,
},
});
// ثبت در تاریخچه لاگ‌ها
await (this.prisma as any).auditLog.create({
data: {
entity: "task",
entityId: task.id,
action: "CREATE",
department: task.department,
actor: task.assignee || "SYSTEM",
summary: "تسک جدید " + task.code + " (" + task.title + ") ایجاد شد.",
newValues: task,
},
});
return { success: true, data: task };
throw new GoneException("Legacy orchestration mutations are retired. Orchestration v4 remains read-only; no replacement mutation is enabled.");
}
@Put(":id/status")
@@ -151,58 +121,7 @@ export class DepartmentTasksController {
@Param("id") id: string,
@Body() body: { status: string; logNote?: string; deliverableUrl?: string }
) {
const existing = await (this.prisma as any).departmentTask.findUnique({
where: { id },
});
if (!existing) throw new NotFoundException(`Task ${id} not found`);
const logs = Array.isArray(existing.executionLogs) ? [...existing.executionLogs] : [];
if (body.logNote) {
logs.push({
timestamp: new Date().toISOString(),
status: body.status,
note: body.logNote,
});
}
const updateData: any = {
status: body.status,
executionLogs: logs,
};
if (body.deliverableUrl) updateData.deliverableUrl = body.deliverableUrl;
if (body.status === "IN_PROGRESS" && !existing.startedAt) updateData.startedAt = new Date();
if (body.status === "DONE") updateData.completedAt = new Date();
// اگر تست QA ناموفق بود و به TODO برگشت، لاگ اصلاح ضمیمه می‌شود
if (body.status === "TODO" && body.logNote) {
updateData.description = (existing.description || "") + "\n\n[اصلاحات و تضمین کیفیت QA]: " + body.logNote;
}
const updated = await (this.prisma as any).departmentTask.update({
where: { id },
data: updateData,
});
// ثبت در تاریخچه
await (this.prisma as any).auditLog.create({
data: {
entity: "task",
entityId: updated.id,
action: "UPDATE",
department: updated.department,
actor: "SYSTEM",
summary: "وضعیت تسک " + updated.code + " به " + body.status + " تغییر یافت.",
oldValues: { status: existing.status },
newValues: { status: updated.status, note: body.logNote },
},
});
return { success: true, data: updated };
throw new GoneException("Legacy orchestration mutations are retired. Orchestration v4 remains read-only; no replacement mutation is enabled.");
}
@Delete(":id")
@ApiOperation({ summary: "حذف تسک" })
async remove(@Param("id") id: string) {
await (this.prisma as any).departmentTask.delete({ where: { id } });
return { success: true, message: "Deleted successfully" };
}
}
@@ -33,6 +33,16 @@ export class DecideApprovalDto {
@IsOptional()
@IsString()
ceoNotes?: string;
@ApiPropertyOptional({ description: "نتیجه اقدام مدیریتی یا استراتژیک" })
@IsOptional()
@IsString()
outcome?: string;
@ApiPropertyOptional({ description: "مستندات و مدرک اقدام مدیریتی" })
@IsOptional()
@IsString()
evidence?: string;
}
export class QueryApprovalDto {
@@ -0,0 +1,52 @@
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { ApprovalsController } from './approvals.controller';
import { DepartmentTasksController } from './department-tasks.controller';
import { PrismaService } from '../prisma/prisma.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
describe('retired legacy orchestration HTTP boundary', () => {
let app: INestApplication;
const dbAccess = jest.fn();
const prisma = new Proxy({}, {get: (_target, key) => {
// Nest inspects provider thenables and lifecycle hooks during setup.
if (["then", "onModuleInit", "onApplicationBootstrap", "onModuleDestroy", "beforeApplicationShutdown", "onApplicationShutdown"].includes(String(key))) return undefined;
dbAccess(key);
throw new Error('Persistence must not be accessed');
}});
const routes = [
['post', '/admin/approvals'],
['put', '/admin/approvals/missing/decide'],
['post', '/admin/department-tasks'],
['put', '/admin/department-tasks/missing/status'],
] as const;
beforeAll(async () => {
const module = await Test.createTestingModule({
controllers: [ApprovalsController, DepartmentTasksController],
providers: [{provide: PrismaService, useValue: prisma}, RolesGuard],
}).overrideGuard(JwtAuthGuard).useValue({canActivate(context: any) {
const req = context.switchToHttp().getRequest();
if (!req.headers.authorization) return false;
req.user = {id: 'test-admin', role: req.headers.authorization};
return true;
}}).compile();
app = module.createNestApplication();
await app.init();
dbAccess.mockClear();
});
beforeEach(() => dbAccess.mockClear());
afterAll(async () => { if (app) await app.close(); });
it.each(routes)('%s %s returns explicit 410 for authorized callers before database access', async (method, path) => {
const response = await request(app.getHttpServer())[method](path)
.set('Authorization', 'Admin').send({decision:'APPROVED',status:'DONE'}).expect(410);
expect(response.body.message).toContain('retired');
expect(dbAccess).not.toHaveBeenCalled();
});
it.each(routes)('%s %s retains anonymous and customer authorization guards', async (method, path) => {
await request(app.getHttpServer())[method](path).send({}).expect(403);
await request(app.getHttpServer())[method](path).set('Authorization','CUSTOMER').send({}).expect(403);
expect(dbAccess).not.toHaveBeenCalled();
});
});
+1 -1
View File
@@ -76,7 +76,7 @@ describe("MediaService", () => {
const validFile = {
originalname: "product-cover.webp",
buffer: Buffer.from("dummy-image-bytes"),
buffer: Buffer.from([0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50, 0x56, 0x50, 0x38]),
mimetype: "image/webp",
size: 5000,
} as any;
+22
View File
@@ -79,6 +79,28 @@ export class MediaService {
"پسوند فایل ارسالی مجاز نیست (فرمت SVG و فایل‌های اجرایی مسدود هستند).",
);
}
// SEC-01: Magic number verification for uploaded binary content
const buf = file.buffer;
if (!buf || buf.length < 4) {
throw new BadRequestException("فایل نامعتبر یا آسیب‌دیده است.");
}
const isJpeg = buf[0] === 0xff && buf[1] === 0xd8 && buf[2] === 0xff;
const isPng = buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4e && buf[3] === 0x47;
const isGif = buf[0] === 0x47 && buf[1] === 0x49 && buf[2] === 0x46; // GIF87a or GIF89a
const isPdf = buf[0] === 0x25 && buf[1] === 0x50 && buf[2] === 0x44 && buf[3] === 0x46; // %PDF
const isWebp =
buf.length >= 12 &&
buf[0] === 0x52 && buf[1] === 0x49 && buf[2] === 0x46 && buf[3] === 0x46 && // RIFF
buf[8] === 0x57 && buf[9] === 0x45 && buf[10] === 0x42 && buf[11] === 0x50; // WEBP
if (!isJpeg && !isPng && !isGif && !isPdf && !isWebp) {
throw new BadRequestException(
"محتوای باینری فایل با پسوند یا هدر ادعایی همخوانی ندارد (اعتبارسنجی امضای فایل رد شد).",
);
}
const filename = `${uniqueSuffix}${ext}`;
const filePath = path.join(uploadDir, filename);
@@ -0,0 +1,14 @@
import { Controller, Get, Query, 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 { SupportChatAnalyticsService } from "./support-chat-analytics.service";
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles("Admin", "SUPER_ADMIN")
@Controller("admin/support-chat")
export class SupportChatAnalyticsController {
constructor(private readonly analytics: SupportChatAnalyticsService) {}
@Get("stats") stats(@Query("startDate") startDate?: string, @Query("endDate") endDate?: string) { return this.analytics.stats(startDate, endDate).then(data => ({ success: true, data })); }
@Get("conversations") conversations(@Query("page") page?: string, @Query("limit") limit?: string, @Query("startDate") startDate?: string, @Query("endDate") endDate?: string, @Query("onlyUnanswered") onlyUnanswered?: string) { return this.analytics.conversations(Number(page), Number(limit), startDate, endDate, onlyUnanswered === "true").then(data => ({ success: true, ...data })); }
}
@@ -0,0 +1,41 @@
import { Injectable } from "@nestjs/common";
import { PrismaService } from "../prisma/prisma.service";
@Injectable()
export class SupportChatAnalyticsService {
constructor(private readonly prisma: PrismaService) {}
async stats(startDate?: string, endDate?: string) {
const where = this.range(startDate, endDate);
const [conversations, messages, authenticated, unanswered, followup, escalated, topics] = await Promise.all([
this.prisma.supportConversation.count({ where }),
this.prisma.supportConversationMessage.count({ where: { conversation: where } }),
this.prisma.supportConversation.count({ where: { ...where, userId: { not: null } } }),
this.prisma.supportConversation.count({ where: { ...where, unanswered: true } }),
this.prisma.supportConversation.count({ where: { ...where, needsFollowup: true } }),
this.prisma.supportConversation.count({ where: { ...where, escalationSignal: true } }),
this.prisma.supportConversation.findMany({ where, select: { topics: true }, take: 5000 }),
]);
const topicCounts: Record<string, number> = {};
for (const row of topics) for (const topic of Array.isArray(row.topics) ? row.topics : []) if (typeof topic === "string") topicCounts[topic] = (topicCounts[topic] || 0) + 1;
return { conversations, messages, authenticated, anonymous: conversations - authenticated, unanswered, followup, escalated, topics: topicCounts };
}
async conversations(page = 1, limit = 25, startDate?: string, endDate?: string, onlyUnanswered = false) {
const safePage = Math.max(1, Math.min(10000, Number(page) || 1));
const safeLimit = Math.max(1, Math.min(100, Number(limit) || 25));
const where = { ...this.range(startDate, endDate), ...(onlyUnanswered ? { unanswered: true } : {}) };
const [data, total] = await Promise.all([
this.prisma.supportConversation.findMany({ where, orderBy: { updatedAt: "desc" }, skip: (safePage - 1) * safeLimit, take: safeLimit, include: { user: { select: { id: true, firstName: true, lastName: true, mobile: true, email: true } }, messages: { orderBy: { sequence: "asc" }, take: 40 } } }),
this.prisma.supportConversation.count({ where }),
]);
return { data, meta: { total, page: safePage, limit: safeLimit, lastPage: Math.ceil(total / safeLimit) } };
}
private range(startDate?: string, endDate?: string) {
const createdAt: any = {};
if (startDate && /^\d{4}-\d{2}-\d{2}$/.test(startDate)) createdAt.gte = new Date(`${startDate}T00:00:00.000Z`);
if (endDate && /^\d{4}-\d{2}-\d{2}$/.test(endDate)) { const end = new Date(`${endDate}T00:00:00.000Z`); end.setUTCDate(end.getUTCDate() + 1); createdAt.lt = end; }
return Object.keys(createdAt).length ? { createdAt } : {};
}
}
+11
View File
@@ -1,3 +1,7 @@
import { registerPrepareWorker } from './orchestration-worker/worker-registration';
import { RouterUsageModule } from './router-usage/router-usage.module';
import { runtimeReadRegistration } from './hermes-management/runtime-read.registration';
import { TasksModule } from './common/services/tasks.module';
import { Module, NestModule, MiddlewareConsumer } from "@nestjs/common";
import { PrismaModule } from "./prisma/prisma.module";
import { SupportChatModule } from './support-chat/support-chat.module';
@@ -36,11 +40,17 @@ import { DoctorsModule } from "./doctors/doctors.module";
import { FaqModule } from "./faq/faq.module";
import { MenuModule } from "./menu/menu.module";
import { RevalidationModule } from "./common/revalidation/revalidation.module";
import { OrchestrationModule } from "./orchestration/orchestration.module";
import { Request, Response, NextFunction } from "express";
@Module({
imports: [
...registerPrepareWorker(),
RouterUsageModule,
// Deny-all registration: no request identity or runtime credentials are accepted yet.
runtimeReadRegistration(),
OrchestrationModule,
SupportChatModule,
RevalidationModule,
SmsModule,
@@ -79,6 +89,7 @@ import { Request, Response, NextFunction } from "express";
B2BModule,
FaqModule,
MenuModule,
TasksModule,
],
controllers: [MetricsController],
providers: [
+22 -1
View File
@@ -40,7 +40,7 @@ describe("BlogsService", () => {
describe("findAll", () => {
it("should query published blogs with pagination", async () => {
const blogs = [
{ id: "1", title: "Blog 1", isPublished: true, status: "PUBLISHED" },
{ id: "1", title: "Blog 1", isPublished: true, status: "PUBLISHED", author: null },
];
mockPrisma.blog.findMany.mockResolvedValue(blogs);
mockPrisma.blog.count.mockResolvedValue(1);
@@ -49,6 +49,9 @@ describe("BlogsService", () => {
expect(mockPrisma.blog.findMany).toHaveBeenCalled();
expect(result.data).toEqual(blogs);
expect(result.meta.total).toBe(1);
const query = mockPrisma.blog.findMany.mock.calls[0][0];
expect(query.include.author).toEqual({ select: { id: true, firstName: true, lastName: true } });
expect(query.orderBy).toEqual([{ publishedAt: "desc" }, { id: "asc" }]);
});
it("should filter by categorySlug and search term", async () => {
@@ -67,7 +70,19 @@ describe("BlogsService", () => {
});
});
it("selects the minimal public author identity for list results", async () => {
mockPrisma.blog.findMany.mockResolvedValue([]); mockPrisma.blog.count.mockResolvedValue(0);
await service.findAll({});
expect(mockPrisma.blog.findMany.mock.calls[0][0].include.author).toEqual({ select: { id: true, firstName: true, lastName: true } });
});
describe("findHomepage", () => {
it("selects the minimal public author identity for homepage results", async () => {
mockPrisma.blog.findMany.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
await service.findHomepage();
for (const call of mockPrisma.blog.findMany.mock.calls.slice(0, 2)) expect(call[0].include.author).toEqual({ select: { id: true, firstName: true, lastName: true } });
});
it("should return selected blogs if showOnHome posts exist", async () => {
const selected = [{ id: "b1", title: "Hero Blog", showOnHome: true }];
mockPrisma.blog.findMany.mockResolvedValueOnce(selected);
@@ -90,6 +105,12 @@ describe("BlogsService", () => {
});
describe("findOneBySlug", () => {
it("selects the minimal public author identity for detail results", async () => {
mockPrisma.blog.findFirst.mockResolvedValue(null);
await expect(service.findOneBySlug("missing")).rejects.toThrow(NotFoundException);
expect(mockPrisma.blog.findFirst.mock.calls[0][0].include.author).toEqual({ select: { id: true, firstName: true, lastName: true } });
});
it("should throw NotFoundException if slug is empty or blog not found", async () => {
mockPrisma.blog.findFirst.mockResolvedValue(null);
await expect(service.findOneBySlug("unknown-slug")).rejects.toThrow(
+16 -11
View File
@@ -64,12 +64,15 @@ export class BlogsService {
where: whereClause,
skip,
take: Number(limit),
orderBy: {
[sortBy === "createdAt" ? "createdAt" : "publishedAt"]: sortOrder,
},
orderBy: [
{
[sortBy === "createdAt" ? "createdAt" : "publishedAt"]: sortOrder,
},
{ id: "asc" },
],
include: {
author: {
select: { firstName: true, lastName: true },
select: { id: true, firstName: true, lastName: true },
},
category: {
select: { id: true, name: true, slug: true },
@@ -108,9 +111,11 @@ export class BlogsService {
const selectedBlogs = await this.prisma.blog.findMany({
where: { ...baseWhere, showOnHome: true },
take: 4,
orderBy: { publishedAt: "desc" },
orderBy: [{ publishedAt: "desc" }, { id: "asc" }],
include: {
author: { select: { firstName: true, lastName: true } },
author: {
select: { id: true, firstName: true, lastName: true },
},
category: { select: { id: true, name: true, slug: true } },
tags: { select: { id: true, name: true, slug: true } },
_count: { select: { comments: { where: { status: "APPROVED" } } } },
@@ -125,9 +130,11 @@ export class BlogsService {
const newestBlogs = await this.prisma.blog.findMany({
where: baseWhere,
take: 4,
orderBy: { publishedAt: "desc" },
orderBy: [{ publishedAt: "desc" }, { id: "asc" }],
include: {
author: { select: { firstName: true, lastName: true } },
author: {
select: { id: true, firstName: true, lastName: true },
},
category: { select: { id: true, name: true, slug: true } },
tags: { select: { id: true, name: true, slug: true } },
_count: { select: { comments: { where: { status: "APPROVED" } } } },
@@ -163,9 +170,7 @@ export class BlogsService {
],
},
include: {
author: {
select: { firstName: true, lastName: true },
},
author: { select: { id: true, firstName: true, lastName: true } },
category: {
select: { id: true, name: true, slug: true },
},
@@ -0,0 +1,61 @@
import { CallHandler, ExecutionContext } from "@nestjs/common";
import { of, lastValueFrom } from "rxjs";
import { AdminRevalidationInterceptor } from "./admin-revalidation.interceptor";
import { RevalidationService } from "./revalidation.service";
describe("AdminRevalidationInterceptor", () => {
const revalidation = { revalidate: jest.fn().mockResolvedValue(true) } as unknown as RevalidationService;
const interceptor = new AdminRevalidationInterceptor(revalidation);
const next = { handle: () => of({ ok: true }) } as CallHandler;
beforeEach(() => jest.clearAllMocks());
function context(method: string, url: string): ExecutionContext {
return { switchToHttp: () => ({ getRequest: () => ({ method, originalUrl: url }) }) } as unknown as ExecutionContext;
}
it("invalidates blog list and detail after an admin blog mutation", async () => {
await lastValueFrom(interceptor.intercept(context("PUT", "/api/admin/blogs/blog-1"), next));
expect(revalidation.revalidate).toHaveBeenCalledWith(
expect.arrayContaining(["blogs", "blog-categories", "blog-tags", "home-data"]),
expect.arrayContaining(["/", "/blog"]),
);
});
it.each([
["/api/admin/products/p1", "products"],
["/api/admin/categories/c1", "categories"],
["/api/admin/wiki/glucosamine", "wiki"],
["/api/admin/settings", "settings"],
["/api/admin/cms/hero-banners/b1", "banners"],
["/api/admin/cms/vet-testimonials/t1", "testimonials"],
["/api/admin/reviews/r1", "products"],
["/api/admin/media/m1", "products"],
])("maps %s mutations to %s cache invalidation", async (url, expectedTag) => {
await lastValueFrom(interceptor.intercept(context("DELETE", url), next));
expect(revalidation.revalidate).toHaveBeenCalledWith(expect.arrayContaining([expectedTag]), expect.any(Array));
});
it("does not invalidate cache for reads or internal-only admin CRUD", async () => {
await lastValueFrom(interceptor.intercept(context("GET", "/api/admin/blogs"), next));
await lastValueFrom(interceptor.intercept(context("PUT", "/api/admin/users/u1"), next));
expect(revalidation.revalidate).not.toHaveBeenCalled();
});
it.each([
["/api/banners/banner-1", "banners"],
["/api/menu/menu-1", "menu"],
["/api/videos/video-1", "videos"],
["/api/testimonials/testimonial-1", "testimonials"],
["/api/faq/faq-1", "faq"],
["/api/ingredients/ingredient-1", "ingredients"],
["/api/doctors/doctor-1", "doctors"],
["/api/settings/seo", "seo-config"],
["/api/settings/ui-texts/HERO_TITLE", "ui-texts"],
["/api/contact/info", "settings"],
["/api/smart-advisor/rules/rule-1", "smart-advisor"],
])("maps storefront mutation %s to %s cache invalidation", async (url, expectedTag) => {
await lastValueFrom(interceptor.intercept(context("PATCH", url), next));
expect(revalidation.revalidate).toHaveBeenCalledWith(expect.arrayContaining([expectedTag]), expect.any(Array));
});
});
@@ -0,0 +1,48 @@
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from "@nestjs/common";
import { Observable, tap } from "rxjs";
import { RevalidationService } from "./revalidation.service";
type Target = { tags: string[]; paths: string[] };
const TARGETS: Array<{ pattern: RegExp; target: Target }> = [
{ pattern: /^\/api\/banners(?:\/|$)/, target: { tags: ["banners", "home-data"], paths: ["/"] } },
{ pattern: /^\/api\/menu(?:\/|$)/, target: { tags: ["menu", "settings"], paths: ["/"] } },
{ pattern: /^\/api\/videos(?:\/|$)/, target: { tags: ["videos", "home-data"], paths: ["/", "/videos"] } },
{ pattern: /^\/api\/testimonials(?:\/|$)/, target: { tags: ["testimonials", "home-data"], paths: ["/"] } },
{ pattern: /^\/api\/faq(?:\/|$)/, target: { tags: ["faq"], paths: ["/faq"] } },
{ pattern: /^\/api\/ingredients(?:\/|$)/, target: { tags: ["ingredients", "products", "wiki"], paths: ["/shop", "/wiki"] } },
{ pattern: /^\/api\/doctors(?:\/|$)/, target: { tags: ["doctors", "settings"], paths: ["/contact"] } },
{ pattern: /^\/api\/settings\/seo(?:\/|$)/, target: { tags: ["seo-config", "settings"], paths: ["/", "/sitemap.xml"] } },
{ pattern: /^\/api\/settings\/(?:ui-texts|system|financial)(?:\/|$)/, target: { tags: ["ui-texts", "settings", "home-data"], paths: ["/"] } },
{ pattern: /^\/api\/contact\/info(?:\/|$)/, target: { tags: ["settings"], paths: ["/contact", "/"] } },
{ pattern: /^\/api\/smart-advisor\/rules(?:\/|$)/, target: { tags: ["smart-advisor", "products"], paths: ["/smart-advisor"] } },
{ pattern: /^\/api\/admin\/blogs(?:\/|$)/, target: { tags: ["blogs", "blog-categories", "blog-tags", "home-data"], paths: ["/", "/blog", "/sitemap.xml"] } },
{ pattern: /^\/api\/admin\/categories(?:\/|$)/, target: { tags: ["products", "categories", "home-data"], paths: ["/", "/shop", "/sitemap.xml"] } },
{ pattern: /^\/api\/admin\/wiki(?:\/|$)/, target: { tags: ["wiki", "scientific-terms"], paths: ["/wiki", "/sitemap.xml"] } },
{ pattern: /^\/api\/admin\/(?:settings|pricing)(?:\/|$)/, target: { tags: ["settings", "ui-texts", "seo-config", "products", "home-data"], paths: ["/", "/shop"] } },
{ pattern: /^\/api\/admin\/cms\/hero-banners(?:\/|$)/, target: { tags: ["banners", "home-data"], paths: ["/"] } },
{ pattern: /^\/api\/admin\/cms\/vet-testimonials(?:\/|$)/, target: { tags: ["testimonials", "home-data"], paths: ["/"] } },
{ pattern: /^\/api\/admin\/reviews(?:\/|$)/, target: { tags: ["products", "home-data"], paths: ["/", "/shop"] } },
{ pattern: /^\/api\/admin\/media(?:\/|$)/, target: { tags: ["products", "blogs", "wiki", "home-data"], paths: ["/", "/shop", "/blog", "/wiki"] } },
{ pattern: /^\/api\/admin\/products(?:\/|$)/, target: { tags: ["products", "home-data"], paths: ["/", "/shop", "/sitemap.xml"] } },
];
@Injectable()
export class AdminRevalidationInterceptor implements NestInterceptor {
constructor(private readonly revalidation: RevalidationService) {}
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
const request = context.switchToHttp().getRequest<{ method?: string; originalUrl?: string; url?: string }>();
const method = (request.method || "GET").toUpperCase();
const url = (request.originalUrl || request.url || "").split("?")[0];
const target = method === "GET" || method === "HEAD" || method === "OPTIONS"
? undefined
: TARGETS.find((entry) => entry.pattern.test(url))?.target;
return next.handle().pipe(
tap(() => {
if (target) void this.revalidation.revalidate(target.tags, target.paths);
}),
);
}
}
@@ -1,9 +1,11 @@
import { Global, Module } from "@nestjs/common";
import { APP_INTERCEPTOR } from "@nestjs/core";
import { AdminRevalidationInterceptor } from "./admin-revalidation.interceptor";
import { RevalidationService } from "./revalidation.service";
@Global()
@Module({
providers: [RevalidationService],
providers: [RevalidationService, { provide: APP_INTERCEPTOR, useClass: AdminRevalidationInterceptor }],
exports: [RevalidationService],
})
export class RevalidationModule {}
@@ -110,4 +110,22 @@ describe("RevalidationService", () => {
expect(pathSpy).toHaveBeenCalledWith("/shop/ART-100");
});
});
describe("revalidate batch", () => {
it("should send deduplicated tags and paths in one request", async () => {
const mockFetch = jest.spyOn(global, "fetch" as any).mockResolvedValue({ ok: true, status: 200 } as any);
const result = await service.revalidate(["blogs", "blogs"], ["/blog", "/blog"]);
expect(result).toBe(true);
expect(mockFetch).toHaveBeenCalledTimes(1);
expect(mockFetch).toHaveBeenCalledWith(
"http://frontend_mock:8080/api/revalidate",
expect.objectContaining({
method: "POST",
body: JSON.stringify({ tags: ["blogs"], paths: ["/blog"] }),
}),
);
});
});
});
@@ -31,6 +31,32 @@ export class RevalidationService {
return secret;
}
async revalidate(tags: string[] = [], paths: string[] = []): Promise<boolean> {
const cleanTags = [...new Set(tags.filter(Boolean))];
const cleanPaths = [...new Set(paths.filter((path) => path.startsWith("/")))];
if (!cleanTags.length && !cleanPaths.length) return true;
try {
const url = `${this.frontendUrl}/api/revalidate`;
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-revalidate-secret": this.secret,
},
body: JSON.stringify({ tags: cleanTags, paths: cleanPaths }),
});
if (!res.ok) {
this.logger.warn(`Failed to revalidate cache batch: ${res.status} ${res.statusText}`);
return false;
}
return true;
} catch (err: any) {
this.logger.debug(`Batch revalidation skipped or frontend offline: ${err?.message}`);
return false;
}
}
async revalidateTag(tag: string): Promise<boolean> {
try {
const url = `${this.frontendUrl}/api/revalidate`;
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { TasksService } from './tasks.service';
import { OrdersModule } from '../../orders/orders.module';
import { PaymentModule } from '../../payment/payment.module';
import { RedisModule } from '../../redis/redis.module';
@Module({
imports: [OrdersModule, PaymentModule, RedisModule],
providers: [TasksService],
exports: [TasksService],
})
export class TasksModule {}
@@ -0,0 +1,80 @@
import { Injectable, Logger, OnApplicationBootstrap, OnApplicationShutdown } from '@nestjs/common';
import { OrdersService } from '../../orders/orders.service';
import { RedisService } from '../../redis/redis.service';
import { PaymentService } from '../../payment/payment.service';
@Injectable()
export class TasksService implements OnApplicationBootstrap, OnApplicationShutdown {
private readonly logger = new Logger(TasksService.name);
private reservationTimer: NodeJS.Timeout | null = null;
private reconciliationTimer: NodeJS.Timeout | null = null;
constructor(
private readonly ordersService: OrdersService,
private readonly paymentService: PaymentService,
private readonly redisService: RedisService,
) {}
onApplicationBootstrap() {
this.logger.log('Starting background maintenance tasks...');
// 1. Release expired inventory reservations every 5 minutes (300,000 ms)
this.reservationTimer = setInterval(async () => {
const lockAcquired = await this.redisService.acquireLock('task:inventory_release', 240);
if (!lockAcquired) {
this.logger.debug('[Task:Inventory] Another instance holds the lock, skipping execution.');
return;
}
try {
const result = await this.ordersService.releaseExpiredInventoryReservations();
if (result.releasedCount > 0) {
this.logger.log(`[Task:Inventory] Automatically released ${result.releasedCount} expired inventory reservations.`);
}
} catch (err) {
this.logger.error('[Task:Inventory] Failed to release expired inventory reservations:', err);
} finally {
await this.redisService.releaseLock('task:inventory_release');
}
}, 5 * 60 * 1000);
// 2. Reconcile pending online payment transactions every 10 minutes (600,000 ms)
this.reconciliationTimer = setInterval(async () => {
const lockAcquired = await this.redisService.acquireLock('task:payment_reconciliation', 540);
if (!lockAcquired) {
this.logger.debug('[Task:PaymentReconciliation] Another instance holds the lock, skipping execution.');
return;
}
try {
const result = await this.paymentService.reconcilePendingTransactions();
if (result.total > 0) {
this.logger.log(`[Task:PaymentReconciliation] Checked ${result.total} transactions: ${result.verified} verified, ${result.failed} failed.`);
}
} catch (err) {
this.logger.error('[Task:PaymentReconciliation] Failed to reconcile pending payments:', err);
} finally {
await this.redisService.releaseLock('task:payment_reconciliation');
}
}, 10 * 60 * 1000);
// Initial check on startup after 30 seconds
setTimeout(async () => {
try {
await this.ordersService.releaseExpiredInventoryReservations();
} catch (err) {
this.logger.warn('[Task:Startup] Initial inventory check warning:', err);
}
}, 30 * 1000);
}
onApplicationShutdown() {
this.logger.log('Stopping background maintenance tasks...');
if (this.reservationTimer) {
clearInterval(this.reservationTimer);
this.reservationTimer = null;
}
if (this.reconciliationTimer) {
clearInterval(this.reconciliationTimer);
this.reconciliationTimer = null;
}
}
}
+17 -17
View File
@@ -13,9 +13,8 @@ describe("ContactService", () => {
count: jest.fn(),
update: jest.fn(),
},
contactInfo: {
setting: {
findMany: jest.fn(),
upsert: jest.fn(),
},
};
@@ -82,26 +81,27 @@ describe("ContactService", () => {
});
describe("getContactInfo", () => {
it("should return contact items if they already exist", async () => {
const items = [
{ id: "1", key: "branch_info", title: "تلفن", value: "021" },
];
mockPrisma.contactInfo.findMany.mockResolvedValue(items);
it("should return contact items from canonical settings", async () => {
mockPrisma.setting.findMany.mockResolvedValue([
{ key: "CONTACT_PHONE", value: "021-12345678" },
{ key: "CONTACT_EMAIL", value: "support@example.test" },
{ key: "CONTACT_ADDRESS", value: "آدرس تنظیم‌شده" },
]);
const result = await service.getContactInfo();
expect(result).toEqual(items);
expect(mockPrisma.contactInfo.upsert).not.toHaveBeenCalled();
expect(result).toEqual([
{ key: "phone", title: "تلفن‌های تماس", value: "021-12345678", icon: "phone", order: 1 },
{ key: "email", title: "پست الکترونیک", value: "support@example.test", icon: "mail", order: 2 },
{ key: "address", title: "نشانی دفتر مرکزی", value: "آدرس تنظیم‌شده", icon: "map-pin", order: 3 },
]);
expect(mockPrisma.setting.findMany).toHaveBeenCalled();
});
it("should seed default items if empty", async () => {
mockPrisma.contactInfo.findMany
.mockResolvedValueOnce([])
.mockResolvedValueOnce([{ key: "branch_info" }]);
const result = await service.getContactInfo();
expect(mockPrisma.contactInfo.upsert).toHaveBeenCalledTimes(3);
expect(result).toHaveLength(1);
it("does not return hardcoded contact values when settings are empty", async () => {
mockPrisma.setting.findMany.mockResolvedValue([]);
expect(await service.getContactInfo()).toEqual([]);
});
});
describe("getAllSubmissions", () => {
+13 -44
View File
@@ -65,51 +65,20 @@ export class ContactService {
}
async getContactInfo() {
let items = await this.prisma.contactInfo.findMany({
orderBy: { order: "asc" },
const settings = await this.prisma.setting.findMany({
where: { key: { in: ["CONTACT_PHONE", "contact_phone", "CONTACT_EMAIL", "contact_email", "CONTACT_ADDRESS", "contact_address"] } },
select: { key: true, value: true },
});
if (items.length === 0) {
// Seed default items if empty
const defaultItems = [
{
key: "branch_info",
title: "اطلاعات نمایندگی",
value:
"تلفن‌های تماس\n۰۲۱-۸۸۸۸ ۴۴۴۴\n\nشنبه تا چهارشنبه ۹:۰۰ الی ۱۸:۰۰",
icon: "phone",
order: 1,
},
{
key: "email_info",
title: "پست الکترونیک",
value: "info@canina.ir\n\nپاسخگویی در کمتر از ۲۴ ساعت کاری",
icon: "mail",
order: 2,
},
{
key: "address_info",
title: "نشانی دفتر مرکزی",
value: "تهران، جردن، خیابان سعیدی، ساختمان کنینا، طبقه ۵، واحد ۱۹",
icon: "map-pin",
order: 3,
},
];
for (const item of defaultItems) {
await this.prisma.contactInfo.upsert({
where: { key: item.key },
update: {},
create: item,
});
}
items = await this.prisma.contactInfo.findMany({
orderBy: { order: "asc" },
});
}
return items;
const values = new Map(settings.map((item) => [item.key, String(item.value ?? "")]));
const read = (...keys: string[]) => keys.map((key) => values.get(key)).find((value) => value?.trim()) || "";
const phone = read("CONTACT_PHONE", "contact_phone");
const email = read("CONTACT_EMAIL", "contact_email");
const address = read("CONTACT_ADDRESS", "contact_address");
return [
{ key: "phone", title: "تلفن‌های تماس", value: phone, icon: "phone", order: 1 },
{ key: "email", title: "پست الکترونیک", value: email, icon: "mail", order: 2 },
{ key: "address", title: "نشانی دفتر مرکزی", value: address, icon: "map-pin", order: 3 },
].filter((item) => item.value.trim());
}
async getAllSubmissions(page = 1, limit = 20, status?: string) {
@@ -0,0 +1,149 @@
import { HermesIntegrationService } from './hermes-integration.service';
import { HermesAdapter } from './hermes.adapter';
describe('backend-owned runtime authorization', () => {
const ctx = { subject: 'admin', profiles: ['seo'] };
const make = (allow = true, enabled = false) => {
const fetcher = jest
.fn()
.mockResolvedValue(
new Response(
JSON.stringify({ id: 'j', profile: 'seo', enabled: false }),
),
);
const authorize = jest.fn().mockResolvedValue(allow);
return {
fetcher,
authorize,
s: new HermesIntegrationService(
new HermesAdapter(
{
origin: 'http://canina-hermes:9119',
token: () => Promise.resolve('secret'),
},
fetcher,
),
{ authorize },
enabled,
),
};
};
it('denies missing principal, missing profile, denied policy before upstream IO', async () => {
const { s, fetcher } = make(false);
await expect(s.pauseCron(ctx, 'seo', 'j')).rejects.toMatchObject({
code: 'FORBIDDEN',
});
await expect(
s.pauseCron({ ...ctx, subject: '' }, 'seo', 'j'),
).rejects.toMatchObject({ code: 'FORBIDDEN' });
await expect(s.pauseCron(ctx, 'dev', 'j')).rejects.toMatchObject({
code: 'FORBIDDEN',
});
expect(fetcher).not.toHaveBeenCalled();
});
it('allows authorized pause but leaves resume disabled by default', async () => {
const { s, fetcher, authorize } = make();
await s.pauseCron(ctx, 'seo', 'j');
expect(authorize).toHaveBeenCalledWith(ctx, 'cron.pause', 'seo');
await expect(s.resumeCron(ctx, 'seo', 'j')).rejects.toMatchObject({
code: 'FORBIDDEN',
});
expect(fetcher).toHaveBeenCalledTimes(1);
});
it('fails closed on all execution and never forwards arbitrary events or RPC', async () => {
const { s, fetcher } = make();
expect(s.capabilities()).toMatchObject({
chat: false,
control: false,
eventDispatch: false,
cronResume: false,
});
await expect(s.chat(ctx, 'seo', 'hello')).rejects.toMatchObject({
code: 'UNSUPPORTED',
});
await expect(s.control(ctx, 'seo', 'interrupt')).rejects.toMatchObject({
code: 'UNSUPPORTED',
});
await expect(
s.dispatchEvent(ctx, 'seo', { type: 'order.created' }),
).rejects.toMatchObject({ code: 'FORBIDDEN' });
expect(fetcher).not.toHaveBeenCalled();
});
it('fails closed when authorization provider throws', async () => {
const a = new HermesAdapter({
origin: 'http://canina-hermes:9119',
token: () => Promise.resolve('secret'),
});
const s = new HermesIntegrationService(a, {
authorize: () => Promise.reject(new Error('private')),
});
await expect(s.health(ctx)).rejects.toMatchObject({ code: 'FORBIDDEN' });
});
});
it('checks every read capability and filters the global profile roster', async () => {
const fetcher = jest.fn().mockImplementation((input: URL) => {
const payloads: Record<string, unknown> = {
'/api/health': {
ok: true,
version: 'x',
commit: 'x',
auth_required: true,
},
'/api/status': {
version: 'x',
gateway_running: false,
gateway_state: null,
overall: 'degraded',
active_agents: 0,
},
'/api/profiles': {
profiles: [
{ name: 'seo', is_default: false, gateway_running: false },
{ name: 'dev', is_default: false, gateway_running: false },
],
},
'/api/model/info': {
model: 'm',
provider: 'p',
effective_context_length: 1,
},
'/api/model/options': { providers: [] },
'/api/analytics/usage': {
period_days: 30,
totals: {
total_input: 0,
total_output: 0,
total_sessions: 0,
total_estimated_cost: 0,
},
},
'/api/sessions': { sessions: [], total: 0 },
'/api/cron/jobs': [],
};
return Promise.resolve(
new Response(JSON.stringify(payloads[input.pathname])),
);
});
const authorize = jest.fn().mockResolvedValue(true);
const s = new HermesIntegrationService(
new HermesAdapter(
{
origin: 'http://canina-hermes:9119',
token: () => Promise.resolve('secret'),
},
fetcher,
),
{ authorize },
);
const p = { subject: 'verified', profiles: ['seo'] };
await s.health(p);
await s.status(p, 'seo');
expect(await s.profiles(p)).toHaveLength(1);
await s.modelInfo(p, 'seo');
await s.modelOptions(p, 'seo');
await s.usage(p, 'seo');
await s.sessions(p, 'seo');
await s.crons(p, 'seo');
expect(authorize).toHaveBeenCalledTimes(8);
expect(authorize.mock.calls.every((c) => c[1] === 'runtime.read')).toBe(true);
});
@@ -0,0 +1,126 @@
import { HermesAdapter, HermesError, Json } from './hermes.adapter';
/** Context MUST be constructed from verified backend identity, never request bodies. */
export interface RuntimePrincipal {
subject: string;
profiles: readonly string[];
}
export type RuntimePermission =
| 'runtime.read'
| 'cron.pause'
| 'cron.resume'
| 'chat'
| 'control'
| 'event.dispatch';
export interface RuntimeAuthorizer {
authorize(
principal: RuntimePrincipal,
permission: RuntimePermission,
profile?: string,
): Promise<boolean>;
}
/** No controller or socket is registered: only the backend gatekeeper can call this seam. */
export class HermesIntegrationService {
constructor(
private readonly adapter: HermesAdapter,
private readonly policy: RuntimeAuthorizer,
private readonly resumeEnabled = false,
) {}
capabilities() {
return {
inventory: true,
cronPause: true,
cronResume: this.resumeEnabled,
chat: false,
control: false,
eventDispatch: false,
} as const;
}
private async check(
principal: RuntimePrincipal,
permission: RuntimePermission,
profile?: string,
): Promise<void> {
if (
!principal?.subject ||
!Array.isArray(principal.profiles) ||
(profile && !principal.profiles.includes(profile))
)
throw new HermesError('FORBIDDEN');
try {
if (
(await this.policy.authorize(principal, permission, profile)) !== true
)
throw new HermesError('FORBIDDEN');
} catch {
throw new HermesError('FORBIDDEN');
}
}
async health(p: RuntimePrincipal) {
await this.check(p, 'runtime.read');
return this.adapter.health();
}
async status(p: RuntimePrincipal, profile: string) {
await this.check(p, 'runtime.read', profile);
return this.adapter.status(profile);
}
async profiles(p: RuntimePrincipal) {
await this.check(p, 'runtime.read');
return (await this.adapter.profiles()).filter(
(r) => typeof r.name === 'string' && p.profiles.includes(r.name),
);
}
async modelInfo(p: RuntimePrincipal, profile: string) {
await this.check(p, 'runtime.read', profile);
return this.adapter.modelInfo(profile);
}
async modelOptions(p: RuntimePrincipal, profile: string) {
await this.check(p, 'runtime.read', profile);
return this.adapter.modelOptions(profile);
}
async usage(p: RuntimePrincipal, profile: string, days = 30) {
await this.check(p, 'runtime.read', profile);
return this.adapter.usage(profile, days);
}
async sessions(p: RuntimePrincipal, profile: string, limit = 20, offset = 0) {
await this.check(p, 'runtime.read', profile);
return this.adapter.sessions(profile, limit, offset);
}
async crons(p: RuntimePrincipal, profile: string) {
await this.check(p, 'runtime.read', profile);
return this.adapter.crons(profile);
}
async pauseCron(p: RuntimePrincipal, profile: string, id: string) {
await this.check(p, 'cron.pause', profile);
return this.adapter.pauseCron(profile, id);
}
async resumeCron(p: RuntimePrincipal, profile: string, id: string) {
await this.check(p, 'cron.resume', profile);
if (!this.resumeEnabled) throw new HermesError('FORBIDDEN');
return this.adapter.resumeCron(profile, id);
}
async chat(
p: RuntimePrincipal,
profile: string,
_text: string,
): Promise<never> {
await this.check(p, 'chat', profile);
throw new HermesError('UNSUPPORTED');
}
async control(
p: RuntimePrincipal,
profile: string,
_command: 'interrupt',
): Promise<never> {
await this.check(p, 'control', profile);
throw new HermesError('UNSUPPORTED');
}
/** Disabled independently of policy: no unreviewed event may become an agent prompt. */
async dispatchEvent(
p: RuntimePrincipal,
profile: string,
_event: Json,
): Promise<never> {
await this.check(p, 'event.dispatch', profile);
throw new HermesError('FORBIDDEN');
}
}
@@ -0,0 +1,26 @@
import { Test } from '@nestjs/testing';
import { HermesManagementModule } from './hermes-management.module';
import { HermesIntegrationService } from './hermes-integration.service';
it('requires explicit registration and denies by default without publishing controllers', async () => {
const module = await Test.createTestingModule({
imports: [
HermesManagementModule.register({
origin: 'http://canina-hermes:9119',
token: () => Promise.resolve('secret'),
}),
],
}).compile();
const service = module.get(HermesIntegrationService);
await expect(
service.health({ subject: 'admin', profiles: [] }),
).rejects.toMatchObject({ code: 'FORBIDDEN' });
await module.close();
});
it('registers backend credential broker without acquiring credentials before policy authorization', async () => {
const credentials=jest.fn().mockResolvedValue({username:'backend',password:'test-only'});
const module=await Test.createTestingModule({imports:[HermesManagementModule.registerAuthenticated(credentials)]}).compile();
await expect(module.get(HermesIntegrationService).health({subject:'admin',profiles:[]})).rejects.toMatchObject({code:'FORBIDDEN'});
expect(credentials).not.toHaveBeenCalled();
await module.close();
});
@@ -0,0 +1,36 @@
import { DynamicModule, Module } from '@nestjs/common';
import { HermesSessionBroker } from './hermes-session-broker';
import { HermesAdapter, HermesConfig } from './hermes.adapter';
import {
HermesIntegrationService,
RuntimeAuthorizer,
} from './hermes-integration.service';
@Module({})
export class HermesManagementModule {
static registerAuthenticated(
credentials: () => Promise<{username:string;password:string}>,
policy: RuntimeAuthorizer = {authorize: async () => false},
): DynamicModule {
const broker = new HermesSessionBroker(credentials);
return this.register({origin:'http://canina-hermes-inventory:9119',token:()=>broker.token()},policy);
}
static register(
config: HermesConfig,
policy: RuntimeAuthorizer = { authorize: () => Promise.resolve(false) },
): DynamicModule {
return {
module: HermesManagementModule,
providers: [
{ provide: HermesAdapter, useFactory: () => new HermesAdapter(config) },
{
provide: HermesIntegrationService,
useFactory: (adapter: HermesAdapter) =>
new HermesIntegrationService(adapter, policy, false),
inject: [HermesAdapter],
},
],
exports: [HermesIntegrationService],
};
}
}
@@ -0,0 +1,53 @@
import { afterEach } from '@jest/globals';
import { HermesSessionBroker } from './hermes-session-broker';
const credentials = async () => ({username:'backend',password:'test-only'});
const response = () => new Response('{"ok":true}', {headers:{'set-cookie':'hermes_session_at=verified-test-token; Max-Age=60; Path=/; HttpOnly'}});
describe('server-only Hermes session broker',()=>{
afterEach(()=>{jest.useRealTimers();});
it('renews expired cached access without retaining the refresh cookie',async()=>{
jest.useFakeTimers();
const fetcher=jest.fn().mockImplementation(async()=>response());
const broker=new HermesSessionBroker(credentials,fetcher);
await broker.token();
await jest.advanceTimersByTimeAsync(51000);
await broker.token();
expect(fetcher).toHaveBeenCalledTimes(2);
});
it('bounds credential waits and releases the pending login after timeout',async()=>{
jest.useFakeTimers();
const load=jest.fn().mockImplementationOnce(()=>new Promise(()=>{})).mockImplementation(credentials);
const broker=new HermesSessionBroker(load,jest.fn().mockImplementation(async()=>response()));
const result=expect(broker.token()).rejects.toMatchObject({message:'Hermes unavailable'});
await jest.advanceTimersByTimeAsync(5001);await result;
expect(await broker.token()).toBe('verified-test-token');
});
it('rejects oversized response bodies and short cookie lifetimes',async()=>{
for(const res of [new Response('x'.repeat(4097)),new Response('{"ok":true}',{headers:{'set-cookie':'hermes_session_at=token; Max-Age=1; Path=/'}})]){
await expect(new HermesSessionBroker(credentials,jest.fn().mockResolvedValue(res)).token()).rejects.toMatchObject({message:'Hermes auth_required'});
}
});
it('coalesces login, caches only access token and uses installed password-login contract',async()=>{
const fetcher=jest.fn().mockImplementation(async()=>response());
const broker=new HermesSessionBroker(credentials,fetcher);
expect(await Promise.all([broker.token(),broker.token()])).toEqual(['verified-test-token','verified-test-token']);
expect(await broker.token()).toBe('verified-test-token');
expect(fetcher).toHaveBeenCalledTimes(1);
const [url,options]=fetcher.mock.calls[0];
expect(url).toBe('http://canina-hermes-inventory:9119/auth/password-login');
expect(JSON.parse(options.body)).toEqual({provider:'basic',username:'backend',password:'test-only'});
expect(options.redirect).toBe('error');
});
it('fails closed without echoing credentials, response bodies or provider errors',async()=>{
for(const result of [new Response('private secret',{status:401}),new Response('{"ok":true}'),new Response('{"ok":false}',{headers:{'set-cookie':'hermes_session_at=bad; Max-Age=60'}})]){
const broker=new HermesSessionBroker(credentials,jest.fn().mockResolvedValue(result));
await expect(broker.token()).rejects.toMatchObject({message:'Hermes auth_required'});
}
});
it('does not cache failures and permits a later successful login',async()=>{
const fetcher=jest.fn().mockRejectedValueOnce(new Error('sensitive upstream')).mockImplementationOnce(async()=>response());
const broker=new HermesSessionBroker(credentials,fetcher);
await expect(broker.token()).rejects.toMatchObject({message:'Hermes unavailable'});
expect(await broker.token()).toBe('verified-test-token');
});
});
@@ -0,0 +1,36 @@
import { HermesError } from './hermes.adapter';
export class HermesSessionBroker {
private cached: {token:string;until:number}|undefined;
private pending: Promise<string>|undefined;
constructor(private readonly credentials:()=>Promise<{username:string;password:string}>,private readonly fetcher:typeof fetch=fetch) {}
token():Promise<string>{
if(this.cached && this.cached.until>Date.now()) return Promise.resolve(this.cached.token);
if(this.pending) return this.pending;
this.pending=this.login().finally(()=>{this.pending=undefined;});
return this.pending;
}
private async login():Promise<string>{
const controller=new AbortController();
const timer=setTimeout(()=>controller.abort(),5000);
try{
const creds=await Promise.race([this.credentials(),new Promise<never>((_,reject)=>controller.signal.addEventListener('abort',()=>reject(new HermesError('UNAVAILABLE')),{once:true}))]);
if(!creds.username || !creds.password) throw new HermesError('AUTH_REQUIRED');
const response=await this.fetcher('http://canina-hermes-inventory:9119/auth/password-login',{method:'POST',redirect:'error',signal:controller.signal,headers:{'Content-Type':'application/json'},body:JSON.stringify({provider:'basic',...creds})});
if(!response.ok) throw new HermesError('AUTH_REQUIRED');
const reader=response.body?.getReader();
if(!reader) throw new HermesError('AUTH_REQUIRED');
const chunks:Uint8Array[]=[];let size=0;
try{for(;;){const {done,value}=await reader.read();if(done)break;size+=value.byteLength;if(size>4096)throw new HermesError('AUTH_REQUIRED');chunks.push(value);}}finally{await reader.cancel().catch(()=>undefined);}
const body=JSON.parse(Buffer.concat(chunks).toString('utf8')) as {ok?:unknown};
const cookies=response.headers.getSetCookie();
const cookie=cookies.find(c=>c.startsWith('hermes_session_at='));
const tokenMatch=cookie?.match(/^hermes_session_at=(?:"([A-Za-z0-9_.=-]+)"|([A-Za-z0-9_.=-]+));/);
const token=tokenMatch?.[1]??tokenMatch?.[2];
const age=Number(cookie?.match(/(?:^|;)\s*Max-Age=(\d+)/i)?.[1]);
if(body.ok!==true || !token || !Number.isSafeInteger(age) || age<30)throw new HermesError('AUTH_REQUIRED');
this.cached={token,until:Date.now()+Math.min(age-10,300)*1000};
return token;
}catch(error){if(error instanceof HermesError)throw error;throw new HermesError('UNAVAILABLE');}
finally{clearTimeout(timer);}
}
}
@@ -0,0 +1,246 @@
import { HermesAdapter, HermesError } from './hermes.adapter';
const health = {
ok: true,
version: '0.13',
commit: 'abc',
auth_required: true,
};
describe('installed Hermes health contract', () => {
it('uses pinned internal origin, bearer only server side and strips extra fields', async () => {
const fetcher = jest
.fn()
.mockResolvedValue(
new Response(
JSON.stringify({ ...health, token: 'never', home: '/secret' }),
),
);
const adapter = new HermesAdapter(
{
origin: 'http://canina-hermes:9119',
token: () => Promise.resolve('private'),
},
fetcher,
);
expect(await adapter.health()).toEqual(health);
expect(fetcher.mock.calls[0][0].toString()).toBe(
'http://canina-hermes:9119/api/health',
);
expect(fetcher.mock.calls[0][1].headers.Authorization).toBe(
'Bearer private',
);
expect(fetcher.mock.calls[0][1].redirect).toBe('error');
});
it('refuses a runtime reporting that authentication is disabled', async () => {
const adapter = new HermesAdapter({origin:'http://canina-hermes:9119',token:async()=> 'private'},
jest.fn().mockResolvedValue(new Response(JSON.stringify({...health,auth_required:false}))));
await expect(adapter.health()).rejects.toMatchObject({code:'AUTH_REQUIRED'});
});
it('fails closed for bad origin, auth errors, malformed contract and oversized bodies', async () => {
expect(
() =>
new HermesAdapter({
origin: 'https://evil.test',
token: () => Promise.resolve(''),
}),
).toThrow(HermesError);
for (const response of [
new Response('private', { status: 401 }),
new Response('{}'),
new Response('x'.repeat(262145)),
]) {
const a = new HermesAdapter(
{
origin: 'http://canina-hermes:9119',
token: () => Promise.resolve('secret'),
},
jest.fn().mockResolvedValue(response),
);
await expect(a.health()).rejects.toBeInstanceOf(HermesError);
}
});
});
describe('bounded profile-scoped REST inventory', () => {
const create = (payload: unknown) => {
const fetcher = jest
.fn()
.mockResolvedValue(new Response(JSON.stringify(payload)));
return {
a: new HermesAdapter(
{
origin: 'http://canina-hermes:9119',
token: () => Promise.resolve('secret'),
},
fetcher,
),
fetcher,
};
};
it('projects status, profiles, models and usage without raw paths, prompts or credentials', async () => {
expect(
await create({
version: 'x',
gateway_running: false,
gateway_state: null,
overall: 'degraded',
active_agents: 0,
}).a.status('seo'),
).toEqual({
version: 'x',
gateway_running: false,
gateway_state: null,
overall: 'degraded',
active_agents: 0,
});
expect(
await create({
profiles: [
{
name: 'seo',
is_default: false,
gateway_running: false,
path: '/private',
description: 'token=secret',
},
],
}).a.profiles(),
).toEqual([{ name: 'seo', is_default: false, gateway_running: false }]);
expect(
await create({
model: 'm',
provider: 'p',
effective_context_length: 0,
api_key: 'secret',
}).a.modelInfo('seo'),
).toEqual({ model: 'm', provider: 'p', effective_context_length: 0 });
expect(
await create({
providers: [
{ slug: 'p', name: 'Provider', models: ['m'], api_key: 'secret' },
],
current: { api_key: 'secret' },
}).a.modelOptions('seo'),
).toEqual([{ slug: 'p', name: 'Provider', models: ['m'] }]);
expect(
await create({
period_days: 7,
totals: {
total_input: 0,
total_output: 2,
total_sessions: 1,
total_estimated_cost: 0,
},
tools: { secret: 1 },
}).a.usage('seo', 7),
).toEqual({
period_days: 7,
totals: {
total_input: 0,
total_output: 2,
total_sessions: 1,
total_estimated_cost: 0,
},
});
});
it('uses scoped list contract and rejects unbounded queries or profile escape', async () => {
const { a, fetcher } = create({
sessions: [
{
id: 's',
profile: 'seo',
is_active: false,
source: 'cron',
title: 'secret',
},
],
total: 1,
storage: {},
});
expect(await a.sessions('seo', 20, 0)).toEqual({
sessions: [{ id: 's', profile: 'seo', is_active: false, source: 'cron' }],
total: 1,
});
expect(fetcher.mock.calls[0][0].pathname).toBe('/api/sessions');
expect(fetcher.mock.calls[0][0].searchParams.get('full')).toBe('false');
await expect(a.sessions('../escape', 20, 0)).rejects.toThrow();
await expect(a.sessions('seo', 101, 0)).rejects.toThrow();
await expect(a.usage('seo', 366)).rejects.toThrow();
await expect(
create({
sessions: [],
total: 0,
storage: { seo: 'corrupt' },
}).a.sessions('seo'),
).rejects.toThrow();
await expect(
create({
sessions: [
{ id: 's', profile: 'dev', source: 'cli', is_active: false },
],
total: 1,
}).a.sessions('seo'),
).rejects.toThrow();
});
it('lists scoped cron array; pause/resume use exact POST routes without triggers', async () => {
expect(
await create([
{ id: 'j', profile: 'seo', enabled: false, prompt: 'secret' },
]).a.crons('seo'),
).toEqual([{ id: 'j', profile: 'seo', enabled: false }]);
const { a, fetcher } = create({ id: 'j', profile: 'seo', enabled: false });
expect(await a.pauseCron('seo', 'j')).toEqual({
id: 'j',
profile: 'seo',
enabled: false,
});
expect(fetcher.mock.calls[0][0].pathname).toBe('/api/cron/jobs/j/pause');
expect(fetcher.mock.calls[0][1].method).toBe('POST');
const resume = create({ id: 'j', profile: 'seo', enabled: true });
await resume.a.resumeCron('seo', 'j');
expect(resume.fetcher.mock.calls[0][0].pathname).toBe(
'/api/cron/jobs/j/resume',
);
await expect(a.pauseCron('seo', '../trigger')).rejects.toThrow();
});
});
describe('resource budgets and safe errors', () => {
it('does not leak upstream errors or tokens and limits credential waits', async () => {
jest.useFakeTimers();
const a = new HermesAdapter({
origin: 'http://canina-hermes:9119',
token: () => new Promise(() => {}),
});
const result = expect(a.health()).rejects.toMatchObject({
code: 'UNAVAILABLE',
message: 'Hermes unavailable',
});
await jest.advanceTimersByTimeAsync(5001);
await result;
jest.useRealTimers();
});
it('caps concurrency before upstream IO', async () => {
const resolvers: ((r: Response) => void)[] = [];
const fetcher = jest.fn(
() =>
new Promise<Response>((r) => {
resolvers.push(r);
}),
);
const a = new HermesAdapter(
{
origin: 'http://canina-hermes:9119',
token: () => Promise.resolve('secret'),
},
fetcher,
);
const calls = Array.from({ length: 4 }, () => a.health());
const denied = expect(a.health()).rejects.toMatchObject({ code: 'LIMIT' });
await Promise.resolve();
await Promise.resolve();
resolvers.forEach((r) => r(new Response(JSON.stringify(health))));
await denied;
await Promise.all(calls);
});
});
@@ -0,0 +1,342 @@
export type Json =
| null
| boolean
| number
| string
| Json[]
| { [key: string]: Json };
export type Row = Record<string, Json>;
export type HermesErrorCode =
| 'CONFIGURATION'
| 'AUTH_REQUIRED'
| 'UNAVAILABLE'
| 'CONTRACT'
| 'LIMIT'
| 'FORBIDDEN'
| 'UNSUPPORTED';
export class HermesError extends Error {
constructor(readonly code: HermesErrorCode) {
super(`Hermes ${code.toLowerCase()}`);
}
}
export interface HermesConfig {
origin: string;
token: () => Promise<string>;
}
export interface Health {
ok: boolean;
version: string;
commit: string;
auth_required: boolean;
provenance?: Row;
}
function object(value: unknown): Record<string, unknown> {
if (!value || typeof value !== 'object' || Array.isArray(value))
throw new HermesError('CONTRACT');
return value as Record<string, unknown>;
}
function fields(
value: unknown,
schema: Record<string, 'string' | 'boolean' | 'number'>,
): Row {
const source = object(value);
const result: Row = {};
for (const [key, type] of Object.entries(schema)) {
if (
typeof source[key] !== type ||
(type === 'number' && !Number.isFinite(source[key]))
)
throw new HermesError('CONTRACT');
result[key] = source[key] as Json;
}
return result;
}
export class HermesAdapter {
private readonly origin: string;
private inflight = 0;
constructor(
private readonly config: HermesConfig,
private readonly fetcher: typeof fetch = fetch,
) {
if (
![
'http://canina-hermes:9119',
'http://canina-hermes-inventory:9119',
].includes(config.origin)
)
throw new HermesError('CONFIGURATION');
this.origin = config.origin;
}
private async request(
path: string,
profile?: string,
method = 'GET',
params: Record<string, string> = {},
): Promise<unknown> {
if (
profile !== undefined &&
!/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$/.test(profile)
)
throw new HermesError('FORBIDDEN');
const url = new URL(path, this.origin);
if (profile) url.searchParams.set('profile', profile);
for (const [k, v] of Object.entries(params)) url.searchParams.set(k, v);
if (this.inflight >= 4) throw new HermesError('LIMIT');
this.inflight++;
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 5000);
try {
const token = await Promise.race([
this.config.token(),
new Promise<never>((_, reject) =>
controller.signal.addEventListener(
'abort',
() => reject(new HermesError('UNAVAILABLE')),
{ once: true },
),
),
]);
if (!token || /[\r\n]/.test(token))
throw new HermesError('AUTH_REQUIRED');
const response = await this.fetcher(url, {
method,
headers: {
Authorization: `Bearer ${token}`,
Accept: 'application/json',
},
signal: controller.signal,
redirect: 'error',
});
if ([401, 403].includes(response.status))
throw new HermesError('AUTH_REQUIRED');
if (response.status === 404) throw new HermesError('UNSUPPORTED');
if (!response.ok) throw new HermesError('UNAVAILABLE');
if (Number(response.headers.get('content-length')) > 262144)
throw new HermesError('LIMIT');
const reader = response.body?.getReader();
if (!reader) throw new HermesError('CONTRACT');
let bytes = 0;
const chunks: Uint8Array[] = [];
try {
for (;;) {
const { done, value } = await reader.read();
if (done) break;
bytes += value.byteLength;
if (bytes > 262144) throw new HermesError('LIMIT');
chunks.push(value);
}
} finally {
await reader.cancel().catch(() => undefined);
}
const data: unknown = JSON.parse(Buffer.concat(chunks).toString('utf8'));
if (this.origin === 'http://canina-hermes-inventory:9119') {
const rows = Array.isArray(data) ? data : [data];
for (const row of rows) provenance(row, true);
}
return data;
} catch (error) {
if (error instanceof HermesError) throw error;
throw new HermesError('UNAVAILABLE');
} finally {
clearTimeout(timer);
this.inflight--;
}
}
async health(): Promise<Health> {
const raw = await this.request('/api/health');
const health = {
...fields(raw, {
ok: 'boolean',
version: 'string',
commit: 'string',
auth_required: 'boolean',
}),
...provenance(raw),
} as unknown as Health;
if (!health.auth_required) throw new HermesError('AUTH_REQUIRED');
return health;
}
async status(profile: string): Promise<Row> {
const raw = object(await this.request('/api/status', profile));
const row = fields(raw, {
version: 'string',
gateway_running: 'boolean',
overall: 'string',
active_agents: 'number',
});
if (raw.gateway_state !== null && typeof raw.gateway_state !== 'string')
throw new HermesError('CONTRACT');
return {
...row,
gateway_state: raw.gateway_state as Json,
...provenance(raw),
...(raw.provenance
? {
gateway_running: false,
active_agents: 0,
gateway_state: null,
overall: 'unknown',
liveGatewayState: 'unknown',
liveActiveAgents: null,
}
: {}),
};
}
async profiles(): Promise<Row[]> {
return list(object(await this.request('/api/profiles')).profiles).map(
(r) => ({
...fields(r, {
name: 'string',
is_default: 'boolean',
gateway_running: 'boolean',
}),
...provenance(r),
...(object(r).provenance
? { gateway_running: false, liveGatewayState: 'unknown' }
: {}),
}),
);
}
async modelInfo(profile: string): Promise<Row> {
const raw = await this.request('/api/model/info', profile);
return {
...fields(raw, {
model: 'string',
provider: 'string',
effective_context_length: 'number',
}),
...provenance(raw),
};
}
async modelOptions(profile: string): Promise<Row[]> {
return list(
object(await this.request('/api/model/options', profile)).providers,
).map((r) => {
const row = fields(r, { slug: 'string', name: 'string' });
const models = list(object(r).models);
if (models.some((m) => typeof m !== 'string'))
throw new HermesError('CONTRACT');
return { ...row, models: models as string[] };
});
}
async usage(profile: string, days = 30): Promise<Row> {
bound(days, 1, 365);
const raw = object(
await this.request('/api/analytics/usage', profile, 'GET', {
days: String(days),
}),
);
return {
...fields(raw, { period_days: 'number' }),
...provenance(raw),
totals: fields(raw.totals, {
total_input: 'number',
total_output: 'number',
total_sessions: 'number',
total_estimated_cost: 'number',
}),
};
}
async sessions(profile: string, limit = 20, offset = 0): Promise<Row> {
bound(limit, 1, 100);
bound(offset, 0, 10000);
const raw = object(
await this.request('/api/sessions', profile, 'GET', {
limit: String(limit),
offset: String(offset),
full: 'false',
order: 'recent',
}),
);
if (raw.storage && Object.keys(object(raw.storage)).length)
throw new HermesError('UNAVAILABLE');
const rows = list(raw.sessions).map((r) =>
scoped(r, profile, {
id: 'string',
profile: 'string',
source: 'string',
is_active: 'boolean',
}),
);
return {
...fields(raw, { total: 'number' }),
sessions: rows,
...provenance(raw),
};
}
async crons(profile: string): Promise<Row[]> {
return list(await this.request('/api/cron/jobs', profile)).map((r) =>
scoped(r, profile, {
id: 'string',
profile: 'string',
enabled: 'boolean',
}),
);
}
private async cron(
profile: string,
id: string,
action: 'pause' | 'resume',
): Promise<Row> {
if (!/^[a-zA-Z0-9_-]{1,128}$/.test(id)) throw new HermesError('FORBIDDEN');
return scoped(
await this.request(`/api/cron/jobs/${id}/${action}`, profile, 'POST'),
profile,
{ id: 'string', profile: 'string', enabled: 'boolean' },
);
}
pauseCron(profile: string, id: string): Promise<Row> {
return this.cron(profile, id, 'pause');
}
resumeCron(profile: string, id: string): Promise<Row> {
return this.cron(profile, id, 'resume');
}
}
function list(value: unknown): unknown[] {
if (!Array.isArray(value) || value.length > 1000)
throw new HermesError('CONTRACT');
return value;
}
function bound(value: number, min: number, max: number): void {
if (!Number.isInteger(value) || value < min || value > max)
throw new HermesError('LIMIT');
}
function scoped(
value: unknown,
profile: string,
schema: Record<string, 'string' | 'boolean' | 'number'>,
): Row {
const row = fields(value, schema);
if (row.profile !== profile) throw new HermesError('FORBIDDEN');
return { ...row, ...provenance(value) };
}
/** Snapshot provenance is mandatory on inventory origin; never synthesize a capture time. */
function provenance(value: unknown, required = false): Row {
const raw = object(value);
if (!raw.provenance && !required) return {};
const p = object(raw.provenance);
if (
p.source !== 'snapshot' ||
p.liveProcess !== 'unknown' ||
typeof p.capturedAt !== 'string' ||
!p.capturedAt.endsWith('Z') ||
!Number.isFinite(Date.parse(p.capturedAt)) ||
Date.parse(p.capturedAt) > Date.now() ||
typeof p.ageSeconds !== 'number' ||
!Number.isInteger(p.ageSeconds) ||
p.ageSeconds < 0 ||
!['fresh', 'stale'].includes(String(p.freshness))
)
throw new HermesError('CONTRACT');
return {
provenance: {
source: 'snapshot',
capturedAt: p.capturedAt,
ageSeconds: p.ageSeconds,
freshness: p.freshness as string,
liveProcess: 'unknown',
},
};
}
@@ -0,0 +1,50 @@
import { createServer } from 'node:http';
import type { AddressInfo } from 'node:net';
import { HermesAdapter } from './hermes.adapter';
it('exercises bounded HTTP transport against an isolated server', async () => {
const seen: string[] = [];
const server = createServer((req, res) => {
seen.push(`${req.method} ${req.url}`);
if (req.headers.authorization !== 'Bearer local-test') {
res.writeHead(401).end();
return;
}
res.setHeader('Content-Type', 'application/json');
res.end(
JSON.stringify({
ok: true,
version: 'test',
commit: 'test',
auth_required: true,
api_key: 'redacted',
}),
);
});
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
try {
const port = (server.address() as AddressInfo).port;
const transport: typeof fetch = (input, init) =>
fetch(
`http://127.0.0.1:${port}${new URL(input instanceof URL ? input.href : typeof input === 'string' ? input : input.url).pathname}`,
init,
);
const adapter = new HermesAdapter(
{
origin: 'http://canina-hermes:9119',
token: () => Promise.resolve('local-test'),
},
transport,
);
expect(await adapter.health()).toEqual({
ok: true,
version: 'test',
commit: 'test',
auth_required: true,
});
expect(seen).toEqual(['GET /api/health']);
} finally {
await new Promise<void>((resolve, reject) =>
server.close((e) => (e ? reject(e) : resolve())),
);
}
});
@@ -0,0 +1,31 @@
import { loadRuntimeGrants } from './runtime-read.module';
import { mkdtemp, writeFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
describe('reviewed runtime grants file', () => {
it('loads an exact reviewed file and fails closed for malformed grants', async () => {
const dir = await mkdtemp(join(tmpdir(), 'runtime-grants-'));
const file = join(dir, 'grants.json');
try {
await writeFile(file, JSON.stringify({ 'subject-1': ['default'] }));
expect(await loadRuntimeGrants(file, dir)).toEqual({
'subject-1': ['default'],
});
for (const invalid of [
null,
[],
{ 'subject-1': [] },
{ 'subject-1': ['default', 'default'] },
{ 'subject-1': ['../x'] },
{ 'subject-1': [3] },
{ 'subject-1': ['default'], extra: true },
]) {
await writeFile(file, JSON.stringify(invalid));
await expect(loadRuntimeGrants(file, dir)).rejects.toThrow();
}
await expect(loadRuntimeGrants('/etc/passwd')).rejects.toThrow();
} finally {
await rm(dir, { recursive: true });
}
});
});
@@ -0,0 +1,83 @@
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { Strategy, ExtractJwt } from 'passport-jwt';
import passport from 'passport';
import { sign } from 'jsonwebtoken';
import request from 'supertest';
import { RuntimeReadModule } from './runtime-read.module';
import { HermesIntegrationService } from './hermes-integration.service';
describe('runtime HTTP authentication and read contract', () => {
let app: INestApplication;
const health = jest.fn().mockResolvedValue({ ok: true, auth_required: true });
beforeAll(async () => {
passport.use(
'jwt',
new Strategy(
{
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: 'test-only-http-key',
},
(payload, done) => done(null, { id: payload.sub, role: payload.role }),
),
);
const module = await Test.createTestingModule({
imports: [
PassportModule,
RuntimeReadModule.register({ 'admin-1': ['default'] }),
],
})
.overrideProvider(HermesIntegrationService)
.useValue({ health })
.compile();
app = module.createNestApplication();
await app.init();
});
afterAll(async () => {
await app.close();
passport.unuse('jwt');
});
it('rejects missing and forged credentials before upstream', async () => {
health.mockClear();
await request(app.getHttpServer())
.get('/admin/orchestration/runtime/health')
.expect(401);
await request(app.getHttpServer())
.get('/admin/orchestration/runtime/health')
.set(
'Authorization',
'Bearer ' + sign({ sub: 'admin-1', role: 'Admin' }, 'forged'),
)
.expect(401);
expect(health).not.toHaveBeenCalled();
});
it('accepts verified allowlisted admin but rejects signed ungranted subject', async () => {
await request(app.getHttpServer())
.get('/admin/orchestration/runtime/health')
.set(
'Authorization',
'Bearer ' + sign({ sub: 'other', role: 'Admin' }, 'test-only-http-key'),
)
.expect(403);
const r = await request(app.getHttpServer())
.get('/admin/orchestration/runtime/health')
.set(
'Authorization',
'Bearer ' +
sign({ sub: 'admin-1', role: 'Admin' }, 'test-only-http-key'),
)
.expect(200);
expect(r.body).toEqual({ ok: true, auth_required: true });
expect(r.headers['set-cookie']).toBeUndefined();
});
it('does not register mutation handlers', async () => {
await request(app.getHttpServer())
.post('/admin/orchestration/runtime/schedules')
.set(
'Authorization',
'Bearer ' +
sign({ sub: 'admin-1', role: 'Admin' }, 'test-only-http-key'),
)
.expect(404);
});
});
@@ -0,0 +1,183 @@
import {
Controller,
Get,
Req,
Query,
UseGuards,
Injectable,
ForbiddenException,
ServiceUnavailableException,
HttpException,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import {
HermesIntegrationService,
RuntimePrincipal,
} from './hermes-integration.service';
import { HermesError } from './hermes.adapter';
export interface VerifiedRuntimeRequest {
user?: {
id?: string;
role?: string;
isApiKey?: boolean;
profiles?: string[];
};
}
@Injectable()
export class RuntimeReadAccess {
private readonly grants: Readonly<Record<string, readonly string[]>>;
constructor(grants: Readonly<Record<string, readonly string[]>>) {
if (
!grants ||
typeof grants !== 'object' ||
Array.isArray(grants) ||
Object.keys(grants).length > 1000
)
throw new Error('Invalid runtime access configuration');
const copy: Record<string, readonly string[]> = Object.create(null);
for (const [subject, profiles] of Object.entries(grants)) {
if (
!/^[a-zA-Z0-9][a-zA-Z0-9@._:-]{0,127}$/.test(subject) ||
['__proto__', 'constructor', 'prototype'].includes(subject) ||
!Array.isArray(profiles) ||
profiles.length === 0 ||
profiles.length > 100 ||
new Set(profiles).size !== profiles.length ||
profiles.some(
(p) =>
typeof p !== 'string' ||
!/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$/.test(p),
)
)
throw new Error('Invalid runtime access configuration');
copy[subject] = Object.freeze([...profiles]);
}
this.grants = Object.freeze(copy);
}
principal(req: VerifiedRuntimeRequest): RuntimePrincipal {
const u = req.user;
if (
!u?.id ||
u.isApiKey ||
!['admin', 'superadmin'].includes(
(u.role ?? '').toLowerCase().replace(/[_-]/g, ''),
) ||
!Object.hasOwn(this.grants, u.id)
)
throw new ForbiddenException('Runtime access denied');
return { subject: u.id, profiles: this.grants[u.id] };
}
authorize(
p: RuntimePrincipal,
permission: string,
profile?: string,
): Promise<boolean> {
return Promise.resolve(
permission === 'runtime.read' &&
Object.hasOwn(this.grants, p.subject) &&
(!profile || this.grants[p.subject].includes(profile)),
);
}
}
@Controller('admin/orchestration/runtime')
@UseGuards(AuthGuard('jwt'))
export class RuntimeReadController {
constructor(
private readonly runtime: HermesIntegrationService,
private readonly access: RuntimeReadAccess,
) {}
private async read(
req: VerifiedRuntimeRequest,
call: (p: RuntimePrincipal) => Promise<unknown>,
) {
const p = this.access.principal(req);
try {
return await call(p);
} catch (e) {
if (e instanceof HermesError) {
if (e.code === 'FORBIDDEN')
throw new ForbiddenException('Runtime access denied');
if (e.code === 'LIMIT')
throw new HttpException('Runtime request limit', 429);
}
throw new ServiceUnavailableException('Runtime unavailable');
}
}
private profile(
q: Record<string, unknown>,
p: RuntimePrincipal,
extras: string[] = [],
): string {
if (
Object.keys(q).some((k) => !['profile', ...extras].includes(k)) ||
typeof q.profile !== 'string' ||
!p.profiles.includes(q.profile)
)
throw new HermesError('FORBIDDEN');
return q.profile;
}
private number(
q: Record<string, unknown>,
key: string,
fallback: number,
min: number,
max: number,
): number {
if (q[key] === undefined) return fallback;
if (typeof q[key] !== 'string' || !/^\d+$/.test(q[key]))
throw new HermesError('LIMIT');
const n = Number(q[key]);
if (!Number.isSafeInteger(n) || n < min || n > max)
throw new HermesError('LIMIT');
return n;
}
@Get('health') health(@Req() req: VerifiedRuntimeRequest) {
return this.read(req, (p) => this.runtime.health(p));
}
@Get('profiles') profiles(@Req() req: VerifiedRuntimeRequest) {
return this.read(req, (p) => this.runtime.profiles(p));
}
@Get('status') status(
@Req() req: VerifiedRuntimeRequest,
@Query() q: Record<string, unknown>,
) {
return this.read(req, (p) => this.runtime.status(p, this.profile(q, p)));
}
@Get('usage') usage(
@Req() req: VerifiedRuntimeRequest,
@Query() q: Record<string, unknown>,
) {
return this.read(req, (p) =>
this.runtime.usage(
p,
this.profile(q, p, ['days']),
this.number(q, 'days', 30, 1, 365),
),
);
}
@Get('sessions') sessions(
@Req() req: VerifiedRuntimeRequest,
@Query() q: Record<string, unknown>,
) {
return this.read(req, (p) =>
this.runtime.sessions(
p,
this.profile(q, p, ['limit', 'offset']),
this.number(q, 'limit', 20, 1, 100),
this.number(q, 'offset', 0, 0, 10000),
),
);
}
@Get('schedules') schedules(
@Req() req: VerifiedRuntimeRequest,
@Query() q: Record<string, unknown>,
) {
return this.read(req, (p) => this.runtime.crons(p, this.profile(q, p)));
}
@Get('model-info') modelInfo(
@Req() req: VerifiedRuntimeRequest,
@Query() q: Record<string, unknown>,
) {
return this.read(req, (p) => this.runtime.modelInfo(p, this.profile(q, p)));
}
}
@@ -0,0 +1,88 @@
import { DynamicModule, Module } from '@nestjs/common';
import { open, lstat } from 'node:fs/promises';
import { constants } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { HermesManagementModule } from './hermes-management.module';
import {
RuntimeReadAccess,
RuntimeReadController,
} from './runtime-read.controller';
import { HermesError } from './hermes.adapter';
export async function runtimeSecret(path: string): Promise<string> {
if (!/^\/run\/secrets\/[a-zA-Z0-9_-]+$/.test(path))
throw new HermesError('CONFIGURATION');
try {
const file = await open(path, 'r');
try {
const stat = await file.stat();
if (!stat.isFile() || stat.size > 4096) throw new Error();
const value = (await file.readFile('utf8')).trim();
if (!value || /[\r\n]/.test(value)) throw new Error();
return value;
} finally {
await file.close();
}
} catch {
throw new HermesError('AUTH_REQUIRED');
}
}
/** Exact fixed mount roots only; startup fails closed, never browser/JWT grants. */
export async function loadRuntimeGrants(
path = '/run/secrets/hermes_runtime_grants',
reviewedRoot?: string,
): Promise<Readonly<Record<string, readonly string[]>>> {
const roots = reviewedRoot
? [resolve(reviewedRoot)]
: ['/run/secrets', '/etc/canina'];
if (!roots.includes(dirname(path)) || resolve(path) !== path)
throw new HermesError('CONFIGURATION');
try {
const stat = await lstat(path);
if (
!stat.isFile() ||
stat.isSymbolicLink() ||
stat.size > 65536 ||
stat.mode & 0o022
)
throw new Error();
const file = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
try {
const data: unknown = JSON.parse(await file.readFile('utf8'));
if (!data || typeof data !== 'object' || Array.isArray(data))
throw new Error();
new RuntimeReadAccess(data as Record<string, readonly string[]>);
return data as Record<string, readonly string[]>;
} finally {
await file.close();
}
} catch {
throw new HermesError('CONFIGURATION');
}
}
@Module({})
export class RuntimeReadModule {
static async registerFromFile(
path = '/run/secrets/hermes_runtime_grants',
): Promise<DynamicModule> {
return this.register(await loadRuntimeGrants(path));
}
static register(
grants: Readonly<Record<string, readonly string[]>>,
): DynamicModule {
const access = new RuntimeReadAccess(grants);
return {
module: RuntimeReadModule,
imports: [
HermesManagementModule.registerAuthenticated(
async () => ({
username: await runtimeSecret('/run/secrets/hermes_username'),
password: await runtimeSecret('/run/secrets/hermes_password'),
}),
access,
),
],
controllers: [RuntimeReadController],
providers: [{ provide: RuntimeReadAccess, useValue: access }],
};
}
}
@@ -0,0 +1,16 @@
import { runtimeReadRegistration } from './runtime-read.registration';
import { RuntimeReadModule } from './runtime-read.module';
describe('application runtime registration',()=>{
afterEach(()=>jest.restoreAllMocks());
it('registers denied routes without reading files when not explicitly enabled',async()=>{
const file=jest.spyOn(RuntimeReadModule,'registerFromFile');
const register=jest.spyOn(RuntimeReadModule,'register');
await runtimeReadRegistration({});
expect(file).not.toHaveBeenCalled();expect(register).toHaveBeenCalledWith({});
});
it('uses only the fixed reviewed grants mount when enabled',async()=>{
const file=jest.spyOn(RuntimeReadModule,'registerFromFile').mockResolvedValue({module:RuntimeReadModule});
await runtimeReadRegistration({CANINA_RUNTIME_READ_ENABLED:'true'});
expect(file).toHaveBeenCalledWith('/run/secrets/hermes_runtime_grants');
});
});
@@ -0,0 +1,6 @@
import { RuntimeReadModule } from './runtime-read.module';
export function runtimeReadRegistration(env:Record<string,string|undefined>=process.env) {
return env.CANINA_RUNTIME_READ_ENABLED === 'true'
? RuntimeReadModule.registerFromFile('/run/secrets/hermes_runtime_grants')
: RuntimeReadModule.register({});
}
@@ -0,0 +1,75 @@
import { Test } from '@nestjs/testing';
import {
RuntimeReadController,
RuntimeReadAccess,
} from './runtime-read.controller';
import { HermesIntegrationService } from './hermes-integration.service';
import { HermesError } from './hermes.adapter';
import {
ForbiddenException,
ServiceUnavailableException,
} from '@nestjs/common';
describe('runtime read boundary', () => {
const health = jest.fn();
const profiles = jest.fn();
const status = jest.fn();
const service = {
health,
profiles,
status,
} as unknown as HermesIntegrationService;
const access = new RuntimeReadAccess({ 'admin-1': ['default'] });
const controller = new RuntimeReadController(service, access);
beforeEach(() => jest.clearAllMocks());
it('denies unconfigured identity without IO', async () => {
await expect(
controller.health({ user: { id: 'other', role: 'Admin' } }),
).rejects.toBeInstanceOf(ForbiddenException);
expect(health).not.toHaveBeenCalled();
});
it('uses server scope not body or user supplied profiles', async () => {
status.mockResolvedValue({ overall: 'ok' });
await expect(
controller.status(
{ user: { id: 'admin-1', role: 'Admin', profiles: ['other'] } },
{ profile: 'other' },
),
).rejects.toBeInstanceOf(ForbiddenException);
expect(status).not.toHaveBeenCalled();
await controller.status(
{ user: { id: 'admin-1', role: 'Admin' } },
{ profile: 'default' },
);
expect(status).toHaveBeenCalledWith(
{ subject: 'admin-1', profiles: ['default'] },
'default',
);
});
it('rejects duplicate/missing/unknown query parameters before IO', async () => {
for (const q of [
{},
{ profile: ['default', 'other'] },
{ profile: 'default', token: 'leak' },
])
await expect(
controller.status({ user: { id: 'admin-1', role: 'Admin' } }, q),
).rejects.toBeInstanceOf(ForbiddenException);
expect(status).not.toHaveBeenCalled();
});
it('maps credential failure without leaking upstream errors', async () => {
health.mockRejectedValue(new HermesError('AUTH_REQUIRED'));
await expect(
controller.health({ user: { id: 'admin-1', role: 'Admin' } }),
).rejects.toThrow('Runtime unavailable');
});
it('rejects API keys and non-admins even when subject is configured', async () => {
for (const user of [
{ id: 'admin-1', role: 'User' },
{ id: 'admin-1', role: 'Admin', isApiKey: true },
])
await expect(controller.health({ user })).rejects.toBeInstanceOf(
ForbiddenException,
);
expect(health).not.toHaveBeenCalled();
});
});
@@ -0,0 +1,68 @@
import { HermesAdapter } from './hermes.adapter';
const provenance = {
source: 'snapshot',
capturedAt: '2020-01-01T00:00:00Z',
ageSeconds: 100,
freshness: 'stale',
liveProcess: 'unknown',
};
function adapter(data: unknown) {
return new HermesAdapter(
{
origin: 'http://canina-hermes-inventory:9119',
token: () => Promise.resolve('test'),
},
jest.fn(() =>
Promise.resolve(new Response(JSON.stringify(data), { status: 200 })),
) as typeof fetch,
);
}
describe('snapshot provenance boundary', () => {
it('rejects missing provenance from dedicated inventory origin', async () => {
await expect(
adapter({
ok: true,
version: '1',
commit: 'x',
auth_required: true,
}).health(),
).rejects.toMatchObject({ code: 'CONTRACT' });
});
it('cannot present copied gateway scalars as live telemetry', async () => {
const row = await adapter({
version: '1',
gateway_running: true,
active_agents: 12,
gateway_state: 'running',
overall: 'ok',
provenance,
}).status('default');
expect(row).toMatchObject({
gateway_running: false,
active_agents: 0,
gateway_state: null,
overall: 'unknown',
liveGatewayState: 'unknown',
liveActiveAgents: null,
provenance,
});
});
it('profile readiness cannot be inferred from copied PID state', async () => {
const rows = await adapter({
profiles: [
{
name: 'default',
is_default: true,
gateway_running: true,
provenance,
},
],
provenance,
}).profiles();
expect(rows[0]).toMatchObject({
gateway_running: false,
liveGatewayState: 'unknown',
provenance,
});
});
});
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "../../../.hermes-contract-build",
"incremental": false,
"strict": true
},
"include": ["./*.ts"],
"exclude": []
}
@@ -0,0 +1,19 @@
# Opt-in fragment ONLY: merge into the reviewed Stage backend service definition.
# Not a standalone compose file; does not enable the worker automatically.
# Host files/dirs must already exist, with reviewed ownership and modes.
x-prepare-worker-backend:
environment:
PREPARE_WORKER_ENABLED: "${PREPARE_WORKER_ENABLED:-false}"
secrets:
- source: prepare_worker_grants
target: prepare_worker_grants
mode: 0400
volumes:
- type: bind
source: ${PREPARE_WORKER_ARTIFACT_HOST_DIRECTORY:?review private artifact directory}
target: /var/lib/canina/prepare-artifacts
bind:
create_host_path: false
secrets:
prepare_worker_grants:
file: ${PREPARE_WORKER_GRANTS_FILE:?review mounted grants JSON file}
@@ -0,0 +1,44 @@
# Bounded PMO editorial proposal bridge
No live configuration, deployment, publication or execution is introduced. Opt-in transport/startup flag remains unchanged. Core services/controllers are untouched.
## Parent contract
`POST /internal/prepare-worker/editorial-proposals` (plus the application's existing `/api` global prefix when present), Bearer service credential. Strict JSON body, all fields required, no extra fields:
```json
{
"requestId": "a client-generated UUID retained across retries",
"pilotKind": "editorial-research-to-brief-v1",
"operationMode": "PREPARE",
"environment": "staging",
"scope": "EDITORIAL_RESEARCH_TO_BRIEF",
"researchTopic": "Evidence-based senior dog nutrition research",
"acceptanceCriteria": ["Cite primary sources with dates", "Return a research brief; do not publish"],
"commitSha": "40 lowercase hexadecimal characters",
"budgetLimitCents": 20
}
```
Topic 1..180 characters, criteria 1..10 items each 1..500 characters; whitespace-only rejected. commitSha must be a string. requestId must be a UUID. Fixed pilot/scope/environment/mode/budget are checked, not caller-controlled options. Department is server-owned CONTENT. Human research topic and criteria are owner-reviewable proposal data, not instructions executed by this endpoint.
201 returns `{taskId,key,pilotKind,state,version,department,operationMode,budgetLimitCents,metadata:{scope,acceptanceCriteria,environment,commitSha}}`. First success is GATE1_PENDING; replay returns current state, does not undo owner decisions or requeue work. Scope includes pilot kind, topic and explicit no-publication/release/production restriction. `key` is `editorial-v1:` plus SHA256 of JSON `[authenticatedWorkerId,requestId]`. Same request UUID + differing immutable proposal is 409. Concurrent identical requests produce one task, one PREPARE_PROPOSED event, one PREPARE_TRANSITION event. Crash after create can resume DRAFT submission. No arbitrary reads or mutations are exposed.
## Explicit grant and author authority
Only canonical server identity `pmo`, authenticated by a configured unique digest grant with role WORKFLOW_WORKER and included in workerIds, can submit. There was no canonical PMO grant mapping in repository transport configuration: the operator must explicitly provision this grant through the existing fixed `/run/secrets/prepare_worker_grants` configuration, using a new random dedicated credential, **without runtime defaults**. Never copy an arbitrary agent/request actor ID or reuse CI identity. Missing pmo grant denies; other valid workers and Stage CI get 403. Transport disabled remains denied/unregistered according to existing registration boundary. No secrets were read or provisioned by this change.
Existing core create/transition APIs accept only Admin/SUPER_ADMIN. This bounded adapter delegates **author-only Admin capability**, binding id to the authenticated pmo grant, for exactly create PREPARE and DRAFT→GATE1_PENDING. It never uses SUPER_ADMIN, calls decide, approves, queues READY, writes through Prisma, creates outbox/runs, reserves/reconciles costs or submits evidence. Prisma reads only support idempotency/payload reconciliation. Core remains authoritative for kill switch, immutable metadata/events, versioning and independent Gate1 reviewer. No approval bypass or arbitrary Admin endpoint is exposed.
Owner uses existing Backend owner Gate1 queue and independent authenticated SUPER_ADMIN review. This bridge does not replace that UI/API. Current deterministic/Tech transports are not editorial executors: editorial keys are explicitly refused by generic claim/complete even after independent approval. Actual research execution/completion needs a separately reviewed executor/ingestion contract; no toy QA, fake zero cost, release or publication is claimed.
## Verification
Disposable PostgreSQL16 (no published ports/live volumes), Node22 private copied dependencies from readonly orchestration-v4-runtime donor, regenerated private Prisma client and real listening Nest TCP HTTP boundary. Evidence directory `/opt/services/canina/validation/v4-editorial-bridge/`:
- red.log: missing endpoint 404 vs required 201.
- tracer-red.log: approved editorial mistakenly admitted by generic tracer (201 vs required 403), fixed with transport fence.
- green.log: five bridge tests pass, including race/replay/crash recovery, immutable payload conflict, grant/body/scope/production denial, kill switch and independent approval with tracer refusal.
- regression-final.log / build.log: all 4 worker suites, 46 tests pass (exit 0); full backend tsc -b exits 0. New bridge files scoped ESLint exits 0 (lint.log). Initial combined regression hit a shared fixture kill-switch uniqueness collision; tests now preserve and restore the fixture row.
Tests use disposable fixture commit hashes, not deployed commit evidence. No AppModule, production, live DB, release authorization, push or deploy changes. Parent owns ingress service-network/TLS/body/rate limits and reviewed grant provisioning. This commit is the proposal slice only.
@@ -0,0 +1,271 @@
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
import { createHash, randomUUID } from 'crypto';
import { PrismaService } from '../prisma/prisma.service';
import { PrepareWorkflowService } from '../orchestration/prepare-workflow.service';
import { WorkerTransportModule } from './worker-transport.module';
(process.env.EDITORIAL_BRIDGE_DB === '1' ? describe : describe.skip)(
'editorial proposal real TCP boundary',
() => {
const db = new PrismaClient();
const token = 'disposable-pmo-token-'.repeat(3),
other = 'disposable-other-token-'.repeat(3),
ci = 'disposable-ci-token-'.repeat(3);
const digest = (s: string) => createHash('sha256').update(s).digest('hex');
let app: INestApplication, base: string, workflow: PrepareWorkflowService;
const config = (enabled = true, withPmo = true) => ({
enabled,
grants: [
...(withPmo
? [
{
id: 'pmo',
role: 'WORKFLOW_WORKER' as const,
credentialSha256: digest(token),
},
]
: []),
{
id: 'other',
role: 'WORKFLOW_WORKER' as const,
credentialSha256: digest(other),
},
{ id: 'ci', role: 'STAGE_CI' as const, credentialSha256: digest(ci) },
],
workerIds: withPmo ? ['pmo', 'other'] : ['other'],
stageCiIds: ['ci'],
artifactDirectory: '/work/tmp/editorial-artifacts',
artifactBaseUrl:
'https://isolated.invalid/internal/prepare-worker/artifacts',
});
async function start(c = config()) {
const m = await Test.createTestingModule({
imports: [WorkerTransportModule.register(c)],
})
.overrideProvider(PrismaService)
.useValue(db)
.compile();
const a = m.createNestApplication();
await a.listen(0, '127.0.0.1');
return a;
}
beforeAll(async () => {
app = await start();
base = await app.getUrl();
workflow = app.get(PrepareWorkflowService);
});
afterAll(async () => {
await app?.close();
await db.$disconnect();
});
const dto = () => ({
requestId: randomUUID(),
pilotKind: 'editorial-research-to-brief-v1',
operationMode: 'PREPARE',
environment: 'staging',
scope: 'EDITORIAL_RESEARCH_TO_BRIEF',
researchTopic: 'Evidence-based senior dog nutrition research',
acceptanceCriteria: [
'Cite primary sources with dates',
'Return a research brief, not a published article',
],
commitSha: 'a'.repeat(40),
budgetLimitCents: 20,
});
async function post(body: any, secret = token, url = base) {
const r = await fetch(
url + '/internal/prepare-worker/editorial-proposals',
{
method: 'POST',
headers: {
authorization: 'Bearer ' + secret,
'content-type': 'application/json',
},
body: JSON.stringify(body),
},
);
return { status: r.status, body: await r.json() };
}
it('submits immutable owner-reviewable proposal, never approves or dispatches, retries idempotently', async () => {
const body = dto();
const r = await post(body);
expect(r.status).toBe(201);
expect(r.body).toMatchObject({
pilotKind: body.pilotKind,
state: 'GATE1_PENDING',
operationMode: 'PREPARE',
department: 'CONTENT',
budgetLimitCents: 20,
metadata: {
environment: 'staging',
commitSha: body.commitSha,
acceptanceCriteria: body.acceptanceCriteria,
},
});
const task = await db.orchestrationTask.findUniqueOrThrow({
where: { id: r.body.taskId },
});
expect((task.metadata as any).createdBy).toBe('pmo');
expect(task.key).toMatch(/^editorial-v1:/);
expect((await post(body)).body.taskId).toBe(task.id);
expect(
await db.orchestrationEvent.count({ where: { taskId: task.id } }),
).toBe(2);
for (const model of [
db.orchestrationApprovalDecision,
db.orchestrationRun,
db.prepareWorkflowOutbox,
db.orchestrationBudgetLedger,
] as any[])
expect(await model.count({ where: { taskId: task.id } })).toBe(0);
expect(
(await post({ ...body, researchTopic: 'Changed topic' })).status,
).toBe(409);
expect((await post({ ...body, commitSha: 'b'.repeat(40) })).status).toBe(
409,
);
await expect(
workflow.decide(task.id, 1, {}, { id: 'pmo', role: 'Admin' }),
).rejects.toThrow('Authenticated workflow identity required');
});
it('rejects untrusted workers, CI, impersonation, unbounded scope and disabled or absent PMO grants', async () => {
for (const secret of [other, ci])
expect((await post(dto(), secret)).status).toBe(403);
expect((await post(dto(), 'incorrect')).status).toBe(401);
for (const patch of [
{ actorId: 'owner' },
{ role: 'SUPER_ADMIN' },
{ department: 'ENGINEERING' },
{ scope: 'arbitrary' },
{ pilotKind: 'other' },
{ environment: 'production' },
{ operationMode: 'RELEASE' },
{ budgetLimitCents: 21 },
{ researchTopic: ' ' },
{ acceptanceCriteria: [' '] },
{ commitSha: 123 },
{ acceptanceCriteria: [] },
{ taskId: randomUUID() },
])
expect((await post({ ...dto(), ...patch })).status).toBe(400);
for (const c of [config(false), config(true, false)]) {
const isolated = await start(c);
try {
const r = await post(dto(), token, await isolated.getUrl());
expect([401, 403]).toContain(r.status);
} finally {
await isolated.close();
}
}
});
it('races identical requests with one proposal and submission event, and resumes a draft', async () => {
const body = dto();
const responses = await Promise.all(
Array.from({ length: 5 }, () => post(body)),
);
expect(responses.map((r) => r.status)).toEqual([201, 201, 201, 201, 201]);
expect(new Set(responses.map((r) => r.body.taskId)).size).toBe(1);
expect(
await db.orchestrationEvent.count({
where: { taskId: responses[0].body.taskId },
}),
).toBe(2);
const pending = await post(dto());
const task = await db.orchestrationTask.findUniqueOrThrow({
where: { id: pending.body.taskId },
});
// Interrupted transaction fixture: core create exists but no submission.
const retry = dto();
const key =
'editorial-v1:' + digest(JSON.stringify(['pmo', retry.requestId]));
const metadata = task.metadata as any;
await workflow.create(
{
key,
title: task.title,
department: 'CONTENT',
operationMode: 'PREPARE',
budgetLimitCents: 20,
metadata,
},
{ id: 'pmo', role: 'Admin' },
);
expect((await post(retry)).body.state).toBe('GATE1_PENDING');
});
it('honors kill switches without creating work', async () => {
const previous = await db.orchestrationKillSwitch.findUnique({
where: { scope: 'GLOBAL' },
});
await db.orchestrationKillSwitch.upsert({
where: { scope: 'GLOBAL' },
create: {
scope: 'GLOBAL',
level: 'FREEZE_NEW_WORK',
active: true,
reason: 'disposable test',
actorId: 'fixture',
},
update: { level: 'FREEZE_NEW_WORK', active: true },
});
try {
expect((await post(dto())).status).toBe(403);
} finally {
if (previous)
await db.orchestrationKillSwitch.update({
where: { scope: 'GLOBAL' },
data: { level: previous.level, active: previous.active },
});
else
await db.orchestrationKillSwitch.delete({
where: { scope: 'GLOBAL' },
});
}
});
it('never admits an editorial proposal into the deterministic tracer even after real independent Gate1 approval', async () => {
const created = await post(dto());
let t = await db.orchestrationTask.findUniqueOrThrow({
where: { id: created.body.taskId },
});
t = await workflow.decide(
t.id,
1,
{
decision: 'APPROVED',
expiresAt: new Date(Date.now() + 60000).toISOString(),
expectedVersion: t.version,
idempotencyKey: randomUUID(),
},
{ id: 'independent-fixture-owner', role: 'SUPER_ADMIN' },
);
t = await workflow.transition(
t.id,
{
toState: 'READY',
expectedVersion: t.version,
idempotencyKey: randomUUID(),
},
{ id: 'pmo', role: 'Admin' },
);
const r = await fetch(base + '/internal/prepare-worker/claim', {
method: 'POST',
headers: {
authorization: 'Bearer ' + token,
'content-type': 'application/json',
},
body: JSON.stringify({ taskId: t.id }),
});
expect(r.status).toBe(403);
expect(await db.orchestrationRun.count({ where: { taskId: t.id } })).toBe(
0,
);
expect(
await db.orchestrationBudgetLedger.count({ where: { taskId: t.id } }),
).toBe(0);
expect((await post({ ...dto(), requestId: randomUUID() })).status).toBe(
201,
);
});
},
);
@@ -0,0 +1,145 @@
import {
BadRequestException,
ConflictException,
ForbiddenException,
Injectable,
} from '@nestjs/common';
import {
ArrayMaxSize,
ArrayMinSize,
IsArray,
IsIn,
IsInt,
IsString,
IsUUID,
Matches,
MaxLength,
MinLength,
} from 'class-validator';
import { createHash } from 'crypto';
import {
PrepareWorkflowService,
WorkflowIdentity,
} from '../orchestration/prepare-workflow.service';
import { PrismaService } from '../prisma/prisma.service';
export const EDITORIAL_PILOT_KIND = 'editorial-research-to-brief-v1';
// No new implicit grant. Only an explicitly provisioned existing worker grant with
// canonical id pmo is admitted; absence of that grant means denial, never fallback.
export const EDITORIAL_PMO_WORKER_ID = 'pmo';
export class EditorialProposalDto {
@IsUUID() requestId!: string;
@IsIn([EDITORIAL_PILOT_KIND]) pilotKind!: string;
@IsIn(['PREPARE']) operationMode!: string;
@IsIn(['staging']) environment!: string;
@IsIn(['EDITORIAL_RESEARCH_TO_BRIEF']) scope!: string;
@IsString() @MinLength(1) @MaxLength(180) researchTopic!: string;
@IsArray()
@ArrayMinSize(1)
@ArrayMaxSize(10)
@IsString({ each: true })
@MinLength(1, { each: true })
@MaxLength(500, { each: true })
acceptanceCriteria!: string[];
@IsString() @Matches(/^[a-f0-9]{40}$/) commitSha!: string;
@IsInt() @IsIn([20]) budgetLimitCents!: number;
}
export function isEditorialProposal(task: any): boolean {
return typeof task?.key === 'string' && task.key.startsWith('editorial-v1:');
}
@Injectable()
export class EditorialProposalBridge {
constructor(
private readonly workflow: PrepareWorkflowService,
private readonly db: PrismaService,
) {}
async submit(identity: WorkflowIdentity, dto: EditorialProposalDto) {
if (
identity.role !== 'WORKFLOW_WORKER' ||
identity.id !== EDITORIAL_PMO_WORKER_ID
)
throw new ForbiddenException(
'Explicit canonical PMO worker grant required',
);
if (
!dto.researchTopic.trim() ||
dto.acceptanceCriteria.some((s) => !s.trim())
)
throw new BadRequestException('Nonblank topic and criteria required');
// Narrow server-owned author capability, NEVER a reviewer/SUPER_ADMIN identity.
// The network credential still authenticates WORKFLOW_WORKER; only these two
// fixed create/submit operations are delegated to core's existing Admin API.
const author: WorkflowIdentity = { id: identity.id, role: 'Admin' };
const key =
'editorial-v1:' +
createHash('sha256')
.update(JSON.stringify([identity.id, dto.requestId]))
.digest('hex');
const scope = `${EDITORIAL_PILOT_KIND}\nTopic: ${dto.researchTopic}\nResearch and evidence-backed brief only. No publication, release, production changes or execution authorization.`;
const proposal = {
key,
title: `Editorial research: ${dto.researchTopic}`,
department: 'CONTENT',
operationMode: 'PREPARE',
budgetLimitCents: 20,
metadata: {
scope,
acceptanceCriteria: dto.acceptanceCriteria,
commitSha: dto.commitSha,
},
};
let task = await this.db.orchestrationTask.findUnique({ where: { key } });
if (!task) {
try {
task = await this.workflow.create(proposal, author);
} catch (e) {
if (!(e instanceof ConflictException)) throw e;
task = await this.db.orchestrationTask.findUnique({ where: { key } });
if (!task) throw e;
}
}
const m = task!.metadata as any;
if (
task!.operationMode !== 'PREPARE' ||
task!.department !== 'CONTENT' ||
task!.budgetLimitCents !== 20 ||
task!.title !== proposal.title ||
m.createdBy !== identity.id ||
m.workflowScope !== 'STAGE_PREPARE' ||
m.environment !== 'staging' ||
m.scope !== scope ||
m.commitSha !== dto.commitSha ||
JSON.stringify(m.acceptanceCriteria) !==
JSON.stringify(dto.acceptanceCriteria)
)
throw new ConflictException('Editorial request payload mismatch');
// Fixed idempotency token/version permits concurrent submits and recovery from
// a crash between existing core create and transition transactions.
if (task!.state === 'DRAFT')
task = await this.workflow.transition(
task!.id,
{
toState: 'GATE1_PENDING',
expectedVersion: 1,
idempotencyKey: `${key}:submit`,
},
author,
);
return {
taskId: task!.id,
key,
pilotKind: EDITORIAL_PILOT_KIND,
state: task!.state,
version: task!.version,
department: 'CONTENT',
operationMode: 'PREPARE',
budgetLimitCents: 20,
metadata: {
scope,
acceptanceCriteria: m.acceptanceCriteria,
environment: 'staging',
commitSha: m.commitSha,
},
};
}
}
@@ -0,0 +1,26 @@
# Tech fixture ingestion adapter contract
Separate fixed pilot, never application QA, deployment evidence, Gate2 or release authorization.
## Frozen HTTP contract
Authenticated worker Bearer credential (same guard/config as existing transport):
- `POST /internal/prepare-worker/tech-claim` body `{taskId: UUID}` -> `{lease:null | {...existingLease,pilotKind:"tech-dev-clamp-quantity-v1"}}`.
- `POST /internal/prepare-worker/heartbeat` body `{taskId,runId,leaseToken}` unchanged.
- `POST /internal/prepare-worker/tech-complete` body **only** `{taskId:UUID,runId:UUID,leaseToken:UUID,source:string}`. Source max2048 characters AND UTF-8 bytes. Returns `{taskId,runId,state:"VERIFYING",artifactSha256,costStatus:"upper-bound-reserved"}`.
- `GET /internal/prepare-worker/artifacts/<artifactSha256>` authenticated own-worker or independent Stage CI. Returns canonical JSON containing actual source, run/commit binding, server QA9, sourceSha256, `applicationQa:false`, `retainedReservationCents:20`, and costStatus. Hash is over canonical stored JSON, not source bytes; source digest is in `qa.sourceSha256`.
Stored task admission requires exact key AND metadata.scope `tech-dev-clamp-quantity-v1`, department TECH, PREPARE, budget20, workflowScope STAGE_PREPARE, environment staging and immutable40hex commit. Existing Gate1/READY workflow must be used. No body pilotKind/scope/pass/cost/URI/identity accepted. Task IDs never authorize themselves. Generic tracer completion refuses Tech key or scope so it cannot reconcile unknown provider cost as zero.
## Parent adapter wiring
After independent Gate1/READY and provider cap approval, call tech-claim, require pilotKind and existing lease bounds, and pass only the original executor lease fields to `infra/hermes-pilot/executor.py` (strip pilotKind because executor uses strict fields). Reuse authenticated heartbeat with <=5s network deadline/no retry and ongoing executor fencing. On successful local executor result, read actual quantity.py (not a report/QA claim) bounded2048 and submit exact source with original binding to tech-complete. Backend independently parses/interprets a fixed expression grammar without executing Python or any subprocess. It accepts only one clamp_quantity(quantity) function with a single nested min/max/0..99/quantity return. Depth32/nodes128/source2048 bound computation; nine boundary cases must pass. This intentionally narrower grammar can reject Python that local AST QA accepts; fail closed, do not rewrite model source.
Retain the existing20-cent RESERVE unchanged: no RECONCILE row, actualCents or measured-cost claim. Server creates TECH_PILOT_SOURCE artifact and TECH_PILOT_FIXTURE_QA attestation signed server-bounded-minmax-v1. No generic QA/TEST attestation. State VERIFYING is fixture completion, not application acceptance. Independent CI remains separate and must not infer application QA from this artifact.
Descriptor-relative O_EXCL/O_NOFOLLOW immutable content storage and bounded authenticated digest/binding checks are reused. Source max2048, artifact JSON read max8192 for escaping/envelope. Same-UID/root storage attackers remain outside boundary. File-before-transaction can leave an unregistered unreadable orphan on cancellation/races; no automatic overwrite/retry. Exact stored task mutation is rechecked at commit, but operational task metadata/key mutation must remain forbidden after review.
## Verification
Run existing `WORKER_EVIDENCE_DIR=/opt/services/canina/validation/v4-tech-ingestion bash infra/hermes-worker/test-isolated.sh green-final` from this worktree. Disposable PostgreSQL16/no published ports/no live volumes, real Nest TCP HTTP, existing child executor regression and backend build. Evidence includes red.log (missing route404), tracer-bypass-red.log (old endpoint201 instead of403), green-final.log and build.log. No model/provider call, live database/runtime edit, push, deployment or RELEASE_AUTHORIZE was performed.
@@ -0,0 +1,74 @@
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
import { PrepareWorkflowService } from '../orchestration/prepare-workflow.service';
import { WorkerTransportModule, tracerReport } from './worker-transport.module';
import { createHash, randomUUID } from 'crypto';
import { mkdtemp, rm, readFile } from 'fs/promises';
import { join } from 'path';
import { tmpdir } from 'os';
const enabled = process.env.WORKER_TRANSPORT_DB === '1';
(enabled ? describe : describe.skip)('real TCP tech artifact ingestion with PostgreSQL', () => {
const db = new PrismaClient();
const token = 'test-tech-worker-secret-'.repeat(3), other = 'test-other-worker-secret-'.repeat(3), ci = 'test-ci-secret-'.repeat(3);
const sha = (s: string) => createHash('sha256').update(s).digest('hex');
let app: INestApplication, workflow: PrepareWorkflowService, base: string, directory: string;
const kind = 'tech-dev-clamp-quantity-v1';
const source = 'def clamp_quantity(quantity):\n return max(0, min(quantity, 99))\n';
const owner = {id:'tech-author',role:'Admin'}, reviewer = {id:'tech-reviewer',role:'SUPER_ADMIN'};
beforeAll(async () => {
directory = await mkdtemp(join(tmpdir(), 'tech-artifacts-'));
const mod = await Test.createTestingModule({imports:[WorkerTransportModule.register({enabled:true, grants:[{id:'tech',role:'WORKFLOW_WORKER',credentialSha256:sha(token)},{id:'other',role:'WORKFLOW_WORKER',credentialSha256:sha(other)},{id:'ci',role:'STAGE_CI',credentialSha256:sha(ci)}],workerIds:['tech','other'],stageCiIds:['ci'],artifactDirectory:directory,artifactBaseUrl:'https://isolated.invalid/internal/prepare-worker/artifacts'})]}).overrideProvider(PrismaService).useValue(db).compile();
workflow = mod.get(PrepareWorkflowService); app = mod.createNestApplication(); await app.listen(0,'127.0.0.1'); base = await app.getUrl();
});
afterAll(async () => {await app?.close(); await db.$disconnect(); if(directory) await rm(directory,{recursive:true,force:true});});
async function post(route: string, body: any, secret=token) {const r=await fetch(base+'/internal/prepare-worker/'+route,{method:'POST',headers:{authorization:'Bearer '+secret,'content-type':'application/json'},body:JSON.stringify(body)});return {status:r.status,body:await r.json()};}
async function get(hash: string, secret=token) {const r=await fetch(base+'/internal/prepare-worker/artifacts/'+hash,{headers:{authorization:'Bearer '+secret}});return {status:r.status,body:await r.json()};}
async function task(key=kind, scope=kind, ready=true) {
// Exact reviewed key is unique; old fixture rows are never reused as admission.
if(key===kind) { const existing=await db.orchestrationTask.findUnique({where:{key}}); if(existing) await db.orchestrationTask.update({where:{id:existing.id},data:{key:'used-'+randomUUID()}}); }
let t=await workflow.create({key,title:'quantity fixture only',department:'TECH',operationMode:'PREPARE',budgetLimitCents:20,metadata:{commitSha:'a'.repeat(40),scope,acceptanceCriteria:['quantity fixture only; not application QA']}},owner);
if(ready) {t=await workflow.transition(t.id,{toState:'GATE1_PENDING',expectedVersion:t.version,idempotencyKey:randomUUID()},owner);t=await workflow.decide(t.id,1,{decision:'APPROVED',expiresAt:new Date(Date.now()+60000).toISOString(),expectedVersion:t.version,idempotencyKey:randomUUID()},reviewer);t=await workflow.transition(t.id,{toState:'READY',expectedVersion:t.version,idempotencyKey:randomUUID()},owner);}
return t;
}
it('rejects unreviewed stored key/scope and cannot dispatch before Gate1',async()=>{
for(const [key,scope] of [[randomUUID(),kind],[kind,'other scope']]) {const t=await task(key,scope);expect((await post('tech-claim',{taskId:t.id})).status).toBe(403);expect(await db.orchestrationRun.count({where:{taskId:t.id}})).toBe(0);}
const t=await task(kind,kind,false);const r=await post('tech-claim',{taskId:t.id});expect(r.status).toBe(201);expect(r.body.lease).toBeNull();expect(await db.orchestrationRun.count({where:{taskId:t.id}})).toBe(0);
});
it('fences callers, rejects arbitrary source/fields and retains budget on denial',async()=>{
const t=await task();const claims=await Promise.all([post('tech-claim',{taskId:t.id}),post('tech-claim',{taskId:t.id})]);const leases=claims.filter(x=>x.body.lease);expect(leases).toHaveLength(1);const l=leases[0].body.lease;const b={taskId:t.id,runId:l.runId,leaseToken:l.leaseToken};
expect((await post('tech-complete',{...b,source},ci)).status).toBe(403);expect((await post('tech-complete',{...b,source},other)).status).toBe(403);expect((await post('tech-complete',{...b,leaseToken:randomUUID(),source})).status).toBe(403);
for(const bad of ['import os\n'+source, 'def clamp_quantity(quantity):\n return 0\n', 'def clamp_quantity(quantity):\n return __import__("os")\n', 'def clamp_quantity(quantity):\n return min(quantity,99)\n','x'.repeat(2049), 12]) expect((await post('tech-complete',{...b,source:bad})).status).toBe(400);
for(const extra of [{actualCents:0},{qa:{passed:true}},{uri:'https://fake.invalid'},{role:'STAGE_CI'}]) expect((await post('tech-complete',{...b,source,...extra})).status).toBe(400);
expect(await db.orchestrationArtifact.count({where:{runId:l.runId}})).toBe(0);
await db.prepareWorkflowOutbox.update({where:{taskId:t.id},data:{leaseUntil:new Date(Date.now()-1000)}});expect((await post('tech-complete',{...b,source})).status).toBe(403);
expect((await db.orchestrationBudgetLedger.findMany({where:{taskId:t.id}})).map(x=>[x.kind,x.amountCents])).toEqual([['RESERVE',20]]);
});
it('honors cancellation and STOP_AGENTS before storing accepted evidence',async()=>{
for(const stop of ['cancel','kill']) {
const t=await task();const {body}=await post('tech-claim',{taskId:t.id});const l=body.lease;const b={taskId:t.id,runId:l.runId,leaseToken:l.leaseToken};
if(stop==='cancel') await db.orchestrationTask.update({where:{id:t.id},data:{state:'CANCELLED'}});
else await db.orchestrationKillSwitch.upsert({where:{scope:'GLOBAL'},create:{scope:'GLOBAL',level:'STOP_AGENTS',active:true,reason:'disposable tech test',actorId:'fixture'},update:{level:'STOP_AGENTS',active:true}});
try {expect((await post('tech-complete',{...b,source})).status).toBe(403);expect(await db.orchestrationArtifact.count({where:{runId:l.runId}})).toBe(0);}
finally {if(stop==='kill') await db.orchestrationKillSwitch.update({where:{scope:'GLOBAL'},data:{active:false}});}
}
});
it('does not let tracer completion release a Tech reservation',async()=>{
const t=await task();const {body}=await post('tech-claim',{taskId:t.id});const l=body.lease;
const result=await post('complete',{taskId:t.id,runId:l.runId,leaseToken:l.leaseToken,report:tracerReport(l)});
expect(result.status).toBe(403);
expect((await db.orchestrationBudgetLedger.findMany({where:{taskId:t.id}})).map(x=>[x.kind,x.amountCents])).toEqual([['RESERVE',20]]);
});
it('ingests actual source with independent fixture QA and retains unknown-cost reservation',async()=>{
const t=await task(); const claim=await post('tech-claim',{taskId:t.id}); expect(claim.status).toBe(201); expect(claim.body.lease.pilotKind).toBe(kind);
const l=claim.body.lease, binding={taskId:t.id,runId:l.runId,leaseToken:l.leaseToken};
const completed=await post('tech-complete',{...binding,source}); expect(completed.status).toBe(201); expect(completed.body).toEqual({taskId:t.id,runId:l.runId,state:'VERIFYING',artifactSha256:expect.stringMatching(/^[a-f0-9]{64}$/),costStatus:'upper-bound-reserved'});
const hash=completed.body.artifactSha256; const artifact=await get(hash); expect(artifact.status).toBe(200); expect(artifact.body).toMatchObject({source,taskId:t.id,runId:l.runId,pilotKind:kind,applicationQa:false,costStatus:'upper-bound-reserved',qa:{passed:true,cases:9,validator:'bounded-minmax-v1'}});
expect(sha(await readFile(join(directory,hash+'.json'),'utf8'))).toBe(hash);
expect((await get(hash,other)).status).toBe(404); expect((await get(hash,ci)).status).toBe(200);
const ledger=await db.orchestrationBudgetLedger.findMany({where:{taskId:t.id}}); expect(ledger.map(x=>[x.kind,x.amountCents])).toEqual([['RESERVE',20]]);
const attestations=await db.orchestrationAttestation.findMany({where:{runId:l.runId}}); expect(attestations).toHaveLength(1);expect(attestations[0].kind).toBe('TECH_PILOT_FIXTURE_QA');expect(attestations[0].predicate).toMatchObject({applicationQa:false,sourceSha256:sha(source),cases:9});
expect((await post('tech-complete',{...binding,source})).status).toBe(403);
});
});
@@ -0,0 +1,35 @@
import { BadRequestException } from '@nestjs/common';
import { createHash } from 'crypto';
export const TECH_PILOT_KIND = 'tech-dev-clamp-quantity-v1';
export function admittedTechTask(t: any): boolean {
return !!t && t.key === TECH_PILOT_KIND && t.department === 'TECH' &&
t.operationMode === 'PREPARE' && t.budgetLimitCents === 20 &&
t.metadata?.scope === TECH_PILOT_KIND && t.metadata?.workflowScope === 'STAGE_PREPARE' &&
t.metadata?.environment === 'staging' && /^[a-f0-9]{40}$/.test(t.metadata?.commitSha ?? '');
}
/** Separate bounded recognizer/interpreter. No Python, eval, subprocess or model QA claims. */
export function validateTechSource(source: string) {
const deny = (): never => { throw new BadRequestException('Only bounded clamp_quantity min/max fixture source passing QA9 accepted'); };
if(typeof source !== 'string' || Buffer.byteLength(source, 'utf8') > 2048) deny();
const body = /^def clamp_quantity\(quantity\):[ \t]*\r?\n[ \t]+return ([^\r\n]+)[ \t]*\r?\n?$/.exec(source);
if(!body) deny();
const text = body![1]; let pos=0, nodes=0;
type Expr = {kind:'q'} | {kind:'n';value:number} | {kind:'min'|'max';left:Expr;right:Expr};
const ws=()=>{while(text[pos]===' ' || text[pos]==='\t') pos++;};
function parse(depth:number): Expr {
if(depth>32 || ++nodes>128) return deny(); ws();
if(text.slice(pos,pos+8)==='quantity') {pos+=8;return {kind:'q'};}
const number=/^(0|[1-9][0-9]?)(?![0-9])/.exec(text.slice(pos));
if(number) {pos+=number[0].length;return {kind:'n',value:Number(number[0])};}
const name=text.slice(pos,pos+3); if(name!=='min' && name!=='max') return deny(); pos+=3;ws();
if(text[pos++]!=='(') return deny(); const left=parse(depth+1);ws();if(text[pos++]!==',') return deny();
const right=parse(depth+1);ws();if(text[pos++]!==')') return deny();return {kind:name,left,right};
}
const tree=parse(0);ws();if(pos!==text.length) deny();
function evaluate(n:Expr,q:number): number {if(n.kind==='q') return q;if(n.kind==='n') return n.value;const a=evaluate(n.left,q),b=evaluate(n.right,q);return n.kind==='min'?Math.min(a,b):Math.max(a,b);}
for(const q of [-1000,-1,0,1,50,98,99,100,1e30]) if(evaluate(tree,q)!==Math.max(0,Math.min(q,99))) deny();
return {passed:true,cases:9,validator:'bounded-minmax-v1',scope:'quantity clamp fixture only',sourceSha256:createHash('sha256').update(source).digest('hex')};
}
export function techArtifact(binding:{taskId:string;runId:string;commitSha:string},source:string) {
return JSON.stringify({pilotKind:TECH_PILOT_KIND,schemaVersion:1,...binding,environment:'staging',source,applicationQa:false,costStatus:'upper-bound-reserved',retainedReservationCents:20,qa:validateTechSource(source)});
}
@@ -0,0 +1,48 @@
import { constants, openSync, closeSync, fstatSync, readSync } from 'fs';
import { WorkerTransportConfig, WorkerTransportModule } from './worker-transport.module';
export const PREPARE_WORKER_CONFIG_PATH = '/run/secrets/prepare_worker_grants';
export const PREPARE_WORKER_ARTIFACT_DIRECTORY = '/var/lib/canina/prepare-artifacts';
const MAX_BYTES = 16384;
const fail = () => { throw new Error('Prepare worker startup configuration rejected'); };
function exact(value: any, keys: string[]): boolean {
return value !== null && typeof value === 'object' && !Array.isArray(value) &&
Object.keys(value).length === keys.length && keys.every(k => Object.hasOwn(value, k));
}
/** Fixed-path, descriptor-pinned, bounded startup read; never logs file contents. */
export function loadPrepareWorkerConfig(): WorkerTransportConfig {
let dir: number | undefined, file: number | undefined;
try {
dir = openSync('/', constants.O_RDONLY | constants.O_DIRECTORY);
for (const part of ['run', 'secrets']) {
const next = openSync(`/proc/self/fd/${dir}/${part}`, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
closeSync(dir); dir = next;
if ((fstatSync(dir).mode & 0o022) !== 0) fail();
}
file = openSync(`/proc/self/fd/${dir}/prepare_worker_grants`, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
const stat = fstatSync(file);
if (!stat.isFile() || stat.nlink !== 1 || (stat.mode & 0o022) !== 0 || stat.size > MAX_BYTES ||
(stat.uid !== 0 && stat.uid !== process.getuid!())) fail();
const bytes = Buffer.alloc(MAX_BYTES + 1);
let length = 0, n: number;
do { n = readSync(file, bytes, length, bytes.length - length, length); length += n; }
while (n > 0 && length < bytes.length);
if (length > MAX_BYTES) fail();
const input = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes.subarray(0, length)));
if (!exact(input, ['grants', 'workerIds', 'stageCiIds', 'artifactBaseUrl']) ||
!Array.isArray(input.grants) || input.grants.length < 1 || input.grants.length > 32 ||
!Array.isArray(input.workerIds) || !Array.isArray(input.stageCiIds) ||
input.workerIds.length < 1 || input.stageCiIds.length < 1 ||
input.workerIds.length + input.stageCiIds.length !== input.grants.length ||
typeof input.artifactBaseUrl !== 'string' || input.artifactBaseUrl.length > 2048 ||
input.grants.some((g: any) => !exact(g, ['id', 'role', 'credentialSha256']))) fail();
// Transport registration performs shared role, SHA256, ID, overlap and HTTPS validation.
const config: WorkerTransportConfig = { ...input, enabled: true, artifactDirectory: PREPARE_WORKER_ARTIFACT_DIRECTORY };
WorkerTransportModule.register(config); // Reuse the real transport's reviewed validation, no IO.
config.grants.forEach(Object.freeze);
Object.freeze(config.grants); Object.freeze(config.workerIds); Object.freeze(config.stageCiIds);
return Object.freeze(config);
} catch { return fail(); }
finally { if (file !== undefined) closeSync(file); if (dir !== undefined) closeSync(dir); }
}
@@ -0,0 +1,21 @@
# Prepare worker startup boundary
Parent integration API (AppModule remains untouched here):
```ts
import { registerPrepareWorker } from './orchestration-worker/worker-registration';
// In AppModule imports:
...registerPrepareWorker(),
```
Only literal `PREPARE_WORKER_ENABLED=true` registers the transport. Missing or other values register nothing (404 routes), do not read configuration, and do not instantiate Prisma. Enabled malformed/missing configuration throws synchronously before Nest starts, with one generic error and no secret logging.
The fixed mount `/run/secrets/prepare_worker_grants` contains JSON with exactly `grants`, `workerIds`, `stageCiIds`, `artifactBaseUrl`. Each grant contains exactly `id`, `role` (`WORKFLOW_WORKER` or `STAGE_CI`), `credentialSha256` (64 lowercase hexadecimal characters; never the bearer credential). Require at least one worker and CI, distinct role-bound IDs/digests, exact allowlist coverage, and at most 32 grants. No enabled flag, directory, token, user, or request-derived fields accepted in JSON.
`artifactBaseUrl` must be explicitly reviewed by the operator: canonical public-DNS HTTPS origin plus `/internal/prepare-worker/artifacts`, no credentials, IP, alternate port, query or fragment. Shared transport validation is authoritative. This loader does not fetch the URL or perform DNS resolution. Directory is always `/var/lib/canina/prepare-artifacts`.
Descriptor-relative Linux `/proc/self/fd` traversal refuses symlink directories/files, nonregular files, hardlinks, files over 16 KiB, group/world writable files/directories, and foreign-owned files (root/runtime UID only). Reads are bounded even on concurrent growth. Configuration is frozen recursively and transport takes its own frozen snapshot. Trusted operator owns mounted configuration; edits require restart and review.
`compose.prepare-worker.fragment.yml` is an extension fragment, not a deployable compose file. Parent must merge its `x-prepare-worker-backend` into the correct reviewed Stage backend service and merge `secrets`. Compose bind-backed secrets may ignore mode/UID declarations: provision actual host grants file as root/runtime-owned mode 0400 and artifact directory runtime-owned mode 0700 first. No mounts or live configuration are created by this change.
Tests deliberately use the fixed `/run/secrets` path. Run ONLY in a fresh disposable container with source and donor dependencies read-only, private dependency copy, and private `prisma generate`; never on a live service. Tests need a disposable Linux root filesystem and do not require DB credentials. Registration test uses a real Nest HTTP listener to prove disabled routes are absent. Existing DB-backed transport tracer remains separately gated by `WORKER_TRANSPORT_DB=1`.
@@ -0,0 +1,84 @@
import { Test } from '@nestjs/testing';
import { writeFileSync, chmodSync, symlinkSync, mkdirSync, rmSync } from 'fs';
import { execFileSync } from 'child_process';
import { registerPrepareWorker } from './worker-registration';
import { loadPrepareWorkerConfig } from './worker-config';
import { linkSync } from 'fs';
describe('reviewed prepare-worker startup registration', () => {
const path = '/run/secrets/prepare_worker_grants';
const valid = () => ({ grants: [{ id: 'worker', role: 'WORKFLOW_WORKER', credentialSha256: 'a'.repeat(64) }, { id: 'ci', role: 'STAGE_CI', credentialSha256: 'b'.repeat(64) }], workerIds: ['worker'], stageCiIds: ['ci'], artifactBaseUrl: 'https://stage.example.com/internal/prepare-worker/artifacts' });
it('loader itself rejects malformed SHA grants', () => {
mkdirSync('/run/secrets', { recursive: true });
const c = valid(); c.grants[0].credentialSha256 = 'secret-not-a-sha';
writeFileSync(path, JSON.stringify(c), { mode: 0o400 });
try { expect(() => loadPrepareWorkerConfig()).toThrow('Prepare worker startup configuration rejected'); }
finally { rmSync(path, { force: true }); }
});
it.each(['1', 'TRUE', 'yes', ' true', 'false', ''])('does not opt in for %s', value => {
expect(registerPrepareWorker({ PREPARE_WORKER_ENABLED: value })).toEqual([]);
});
it.each(['missing', 'symlink', 'directory', 'fifo', 'oversize', 'group-write', 'world-write', 'hardlink'])('fails startup on unsafe file: %s', kind => {
mkdirSync('/run/secrets', { recursive: true });
const other = '/run/secrets/test-other';
try {
if (kind === 'fifo') execFileSync('mkfifo', [path]);
else if (kind === 'directory') mkdirSync(path);
else if (kind === 'symlink') symlinkSync(other, path);
else if (kind !== 'missing') {
writeFileSync(path, kind === 'oversize' ? ' '.repeat(16385) : JSON.stringify(valid()), { mode: 0o400 });
if (kind === 'group-write') chmodSync(path, 0o420);
if (kind === 'world-write') chmodSync(path, 0o402);
if (kind === 'hardlink') linkSync(path, other);
}
expect(() => registerPrepareWorker({ PREPARE_WORKER_ENABLED: 'true' })).toThrow('Prepare worker startup configuration rejected');
} finally { rmSync(path, { recursive: true, force: true }); rmSync(other, { force: true }); }
});
it.each(['extra', 'raw-token', 'wrong-role', 'overlap', 'duplicate-sha', 'missing-grant', 'http', 'credentials', 'localhost', 'query', 'bad-json', 'null'])('rejects unreviewed JSON: %s without leaking contents or logging', kind => {
mkdirSync('/run/secrets', { recursive: true });
const c: any = valid();
if (kind === 'extra') c.artifactDirectory = '/user-selected';
if (kind === 'raw-token') c.grants[0].token = 'DO_NOT_LOG_THIS_SECRET';
if (kind === 'wrong-role') c.grants[0].role = 'Admin';
if (kind === 'overlap') c.stageCiIds = ['worker'];
if (kind === 'duplicate-sha') c.grants[1].credentialSha256 = c.grants[0].credentialSha256;
if (kind === 'missing-grant') c.workerIds.push('unbound');
if (kind === 'http') c.artifactBaseUrl = 'http://stage.example.com/internal/prepare-worker/artifacts';
if (kind === 'credentials') c.artifactBaseUrl = 'https://DO_NOT_LOG_THIS_SECRET@stage.example.com/internal/prepare-worker/artifacts';
if (kind === 'localhost') c.artifactBaseUrl = 'https://127.0.0.1/internal/prepare-worker/artifacts';
if (kind === 'query') c.artifactBaseUrl += '?secret=DO_NOT_LOG_THIS_SECRET';
writeFileSync(path, kind === 'bad-json' ? 'DO_NOT_LOG_THIS_SECRET' : kind === 'null' ? 'null' : JSON.stringify(c), { mode: 0o400 });
const logs = [jest.spyOn(console, 'log'), jest.spyOn(console, 'warn'), jest.spyOn(console, 'error')];
try {
expect(() => registerPrepareWorker({ PREPARE_WORKER_ENABLED: 'true' })).toThrow(/^Prepare worker startup configuration rejected$/);
logs.forEach(log => expect(log).not.toHaveBeenCalled());
} finally { logs.forEach(log => log.mockRestore()); rmSync(path, { force: true }); }
});
it('defaults to no module/no config IO and exposes no worker routes', async () => {
expect(registerPrepareWorker({})).toEqual([]);
expect(registerPrepareWorker({ PREPARE_WORKER_ENABLED: 'false' })).toEqual([]);
const module = await Test.createTestingModule({ imports: registerPrepareWorker({}) }).compile();
const app = module.createNestApplication();
await app.listen(0, '127.0.0.1');
try {
const response = await fetch((await app.getUrl()) + '/internal/prepare-worker/claim', { method: 'POST' });
expect(response.status).toBe(404);
} finally { await app.close(); }
});
it('loads immutable reviewed JSON into the real transport registration', async () => {
mkdirSync('/run/secrets', { recursive: true });
const file = '/run/secrets/prepare_worker_grants';
const config = { grants: [{ id: 'worker', role: 'WORKFLOW_WORKER', credentialSha256: 'a'.repeat(64) }, { id: 'ci', role: 'STAGE_CI', credentialSha256: 'b'.repeat(64) }], workerIds: ['worker'], stageCiIds: ['ci'], artifactBaseUrl: 'https://stage.example.com/internal/prepare-worker/artifacts' };
writeFileSync(file, JSON.stringify(config), { mode: 0o400 });
try {
const [registered] = registerPrepareWorker({ PREPARE_WORKER_ENABLED: 'true' });
expect(registered.module.name).toBe('WorkerTransportModule');
expect(registered.controllers?.length).toBe(1);
const snapshot = (registered.providers as any[]).find(p => p.useValue?.artifactDirectory)?.useValue;
expect(snapshot).toEqual({ ...config, enabled: true, artifactDirectory: '/var/lib/canina/prepare-artifacts' });
expect(Object.isFrozen(snapshot)).toBe(true);
expect(Object.isFrozen(snapshot.grants[0])).toBe(true);
} finally { rmSync(file, { force: true }); }
});
});
@@ -0,0 +1,10 @@
import { DynamicModule } from '@nestjs/common';
import { loadPrepareWorkerConfig } from './worker-config';
import { WorkerTransportModule } from './worker-transport.module';
/** AppModule imports: ...registerPrepareWorker(); disabled means no module or IO. */
export function registerPrepareWorker(env: NodeJS.ProcessEnv = process.env): DynamicModule[] {
if (env.PREPARE_WORKER_ENABLED !== 'true') return [];
try { return [WorkerTransportModule.register(loadPrepareWorkerConfig())]; }
catch { throw new Error('Prepare worker startup configuration rejected'); }
}
@@ -0,0 +1,417 @@
import {
BadRequestException,
Body,
CanActivate,
ConflictException,
Controller,
DynamicModule,
ForbiddenException,
Get,
Inject,
Injectable,
Module,
NotFoundException,
Param,
Post,
Req,
UnauthorizedException,
UseGuards,
UsePipes,
ValidationPipe,
} from "@nestjs/common";
import { IsString, IsUUID, Matches, MaxLength } from "class-validator";
import { createHash, timingSafeEqual } from "crypto";
import { mkdir, open, FileHandle } from "fs/promises";
import { constants } from "fs";
import { isAbsolute } from "path";
import { PrismaModule } from "../prisma/prisma.module";
import { PrismaService } from "../prisma/prisma.service";
import {
PREPARE_WORKFLOW_CONFIG,
PrepareWorkflowService,
WorkflowIdentity,
} from "../orchestration/prepare-workflow.service";
import { EditorialProposalBridge, EditorialProposalDto, isEditorialProposal } from "./editorial-bridge";
import { admittedTechTask, TECH_PILOT_KIND, techArtifact, validateTechSource } from "./tech-pilot-validator";
// This is reviewed static configuration, not a URL fetched by the transport.
function validArtifactBase(value: string): boolean {
try {
const u = new URL(value);
// DNS name only: reject all IP literals, localhost/private single-label names,
// noncanonical paths/escapes, credentials, query, fragment and alternate ports.
return u.protocol === "https:" && !u.username && !u.password && !u.port &&
/^[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?$/.test(u.hostname) &&
u.hostname.includes(".") && !/^[0-9.]+$/.test(u.hostname) &&
!/(^|\.)(localhost|local|internal|lan|home)$/.test(u.hostname) &&
["/internal/prepare-worker/artifacts", "/api/internal/prepare-worker/artifacts"].includes(u.pathname) &&
value === u.origin + u.pathname;
} catch { return false; }
}
const CONFIG = Symbol("WORKER_TRANSPORT_CONFIG");
export interface WorkerTransportConfig {
enabled: boolean;
grants: {
id: string;
role: "WORKFLOW_WORKER" | "STAGE_CI";
credentialSha256: string;
}[];
workerIds: string[];
stageCiIds: string[];
artifactDirectory: string;
artifactBaseUrl: string;
}
class ClaimDto {
@IsUUID() taskId!: string;
}
class LeaseDto extends ClaimDto {
@IsUUID() runId!: string;
@IsUUID() leaseToken!: string;
}
class CompleteDto extends LeaseDto {
@IsString() @MaxLength(2048) report!: string;
}
class TechCompleteDto extends LeaseDto {
@IsString() @MaxLength(2048) source!: string;
}
class StageEvidenceDto extends ClaimDto {
@IsUUID() runId!: string;
@Matches(/^[a-f0-9]{40}$/) commitSha!: string;
@Matches(/^[a-f0-9]{64}$/) sha256!: string;
@Matches(/^[a-f0-9]{64}$/) evidenceSha256!: string;
@Matches(/^https:\/\/[^\s]+$/) @MaxLength(2000) uri!: string;
}
export function tracerReport(lease: {
taskId: string;
runId: string;
commitSha: string;
}) {
return JSON.stringify({
kind: "DETERMINISTIC_PREPARE_TRACER",
schemaVersion: 1,
taskId: lease.taskId,
runId: lease.runId,
commitSha: lease.commitSha,
environment: "staging",
llmPilot: false,
browserExecuted: false,
shellExecuted: false,
deploymentExecuted: false,
checks: ["lease-bound identity", "deterministic JSON serialization"],
actualCents: 0,
});
}
@Injectable()
class WorkerIdentityGuard implements CanActivate {
constructor(@Inject(CONFIG) private readonly config: WorkerTransportConfig) {}
canActivate(context: any) {
if (!this.config.enabled)
throw new ForbiddenException("Worker transport disabled");
const req = context.switchToHttp().getRequest();
const auth = req.headers.authorization;
if (typeof auth !== "string" || !/^Bearer [\x21-\x7e]{32,512}$/.test(auth))
throw new UnauthorizedException();
const digest = createHash("sha256").update(auth.slice(7)).digest();
const grant = this.config.grants.find((g) =>
timingSafeEqual(Buffer.from(g.credentialSha256, "hex"), digest),
);
if (!grant) throw new UnauthorizedException();
req.workerIdentity = { id: grant.id, role: grant.role };
return true;
}
}
@Injectable()
class WorkerTransportService {
constructor(
private readonly workflow: PrepareWorkflowService,
private readonly db: PrismaService,
@Inject(CONFIG) private readonly config: WorkerTransportConfig,
) {}
// Linux descriptor-relative traversal pins each directory across renames. Never
// realpath-check then reopen by name: that reintroduces a symlink race.
private async artifactDir(): Promise<FileHandle> {
let dir = await open("/", constants.O_RDONLY | constants.O_DIRECTORY);
try {
for (const part of this.config.artifactDirectory.split("/").filter(Boolean)) {
const path = `/proc/self/fd/${dir.fd}/${part}`;
try { await mkdir(path, { mode: 0o700 }); }
catch (e: any) { if (e.code !== "EEXIST") throw e; }
const next = await open(path, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
await dir.close();
dir = next;
}
const stat = await dir.stat();
if (stat.uid !== process.getuid!() || (stat.mode & 0o077) !== 0)
throw new Error("Private owned artifact directory required");
return dir;
} catch (e) { await dir.close(); throw e; }
}
private worker(a: WorkflowIdentity) {
if (a.role !== "WORKFLOW_WORKER" || !this.config.workerIds.includes(a.id))
throw new ForbiddenException("Worker allowlist required");
}
async claim(a: WorkflowIdentity, dto: ClaimDto) {
this.worker(a);
const t = await this.db.orchestrationTask.findUnique({
where: { id: dto.taskId },
});
if (isEditorialProposal(t)) throw new ForbiddenException("Editorial execution requires a separately reviewed executor");
// Explicit per-task pilot scope; never opportunistically consume arbitrary queued work.
if (!t || !["READY", "RUNNING"].includes(t.state)) return { lease: null };
if (t.budgetLimitCents !== 20)
throw new ForbiddenException("Pilot hard task cap is 20 cents");
return { lease: await this.workflow.claim(a, dto.taskId) };
}
heartbeat(a: WorkflowIdentity, dto: LeaseDto) {
this.worker(a);
return this.workflow.heartbeat(a, dto);
}
async complete(a: WorkflowIdentity, dto: CompleteDto) {
this.worker(a);
const task = await this.db.orchestrationTask.findUnique({ where: { id: dto.taskId } });
if (isEditorialProposal(task)) throw new ForbiddenException("Editorial execution requires a separately reviewed executor");
if (task?.key === TECH_PILOT_KIND || (task?.metadata as any)?.scope === TECH_PILOT_KIND)
throw new ForbiddenException("Tech pilot requires separate source completion");
// Recheck stop/interrupt and fencing before artifact creation; core rechecks atomically at commit.
await this.workflow.heartbeat(a, dto);
const run = await this.db.orchestrationRun.findUniqueOrThrow({
where: { id: dto.runId },
});
const expected = tracerReport({ ...dto, commitSha: run.commitSha });
if (dto.report !== expected)
throw new BadRequestException(
"Only deterministic harmless tracer report accepted",
);
const sha256 = createHash("sha256").update(expected).digest("hex");
let dir: FileHandle | undefined, file: FileHandle | undefined;
try {
dir = await this.artifactDir();
file = await open(`/proc/self/fd/${dir.fd}/${sha256}.json`,
constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
await file.writeFile(expected, "utf8");
await file.sync();
} catch { throw new ConflictException("Artifact creation refused"); }
finally { await file?.close(); await dir?.close(); }
const result = await this.workflow.complete(a, {
...dto,
sha256,
uri: this.config.artifactBaseUrl + "/" + sha256,
actualCents: 0,
});
return {
state: result.state,
taskId: result.id,
runId: dto.runId,
sha256,
actualCents: 0,
};
}
async techClaim(a: WorkflowIdentity, dto: ClaimDto) {
this.worker(a);
const t = await this.db.orchestrationTask.findUnique({ where: { id: dto.taskId } });
if (!admittedTechTask(t)) throw new ForbiddenException("Reviewed Tech pilot task required");
const { lease } = await this.claim(a, dto);
return { lease: lease ? { ...lease, pilotKind: TECH_PILOT_KIND } : null };
}
async techComplete(a: WorkflowIdentity, dto: TechCompleteDto) {
this.worker(a);
await this.workflow.heartbeat(a, dto);
const task = await this.db.orchestrationTask.findUnique({ where: { id: dto.taskId } });
const run = await this.db.orchestrationRun.findUnique({ where: { id: dto.runId } });
if (!admittedTechTask(task) || !run || run.taskId !== dto.taskId || run.commitSha !== (task!.metadata as any).commitSha || run.environment !== 'staging')
throw new ForbiddenException('Reviewed Tech pilot task required');
const qa = validateTechSource(dto.source);
const bytes = techArtifact({ taskId: dto.taskId, runId: dto.runId, commitSha: run.commitSha }, dto.source);
const hash = createHash('sha256').update(bytes).digest('hex');
let dir: FileHandle | undefined, file: FileHandle | undefined;
try {
dir = await this.artifactDir();
file = await open(`/proc/self/fd/${dir.fd}/${hash}.json`, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
await file.writeFile(bytes, 'utf8'); await file.sync();
} catch { throw new ConflictException('Artifact creation refused'); }
finally { await file?.close(); await dir?.close(); }
// This fixture path deliberately does not call core complete: its QA attestation and
// measured-cost reconciliation would misrepresent generated toy-source evidence.
return this.db.$transaction(async (tx: any) => {
const switches = await tx.$queryRaw`SELECT level, active FROM orchestration_kill_switches WHERE scope = 'GLOBAL' FOR SHARE`;
if (switches[0]?.active && switches[0].level !== 'FREEZE_NEW_WORK') throw new ForbiddenException('Kill switch blocks workflow');
await tx.$queryRaw`SELECT id FROM orchestration_tasks WHERE id = ${dto.taskId}::uuid FOR UPDATE`;
const t = await tx.orchestrationTask.findUnique({ where: { id: dto.taskId } });
const o = await tx.prepareWorkflowOutbox.findUnique({ where: { taskId: dto.taskId } });
const r = await tx.orchestrationRun.findUnique({ where: { id: dto.runId } });
if (!admittedTechTask(t) || t.state !== 'RUNNING' || !o || o.status !== 'PENDING' || o.leaseOwner !== a.id || o.leaseToken !== dto.leaseToken || o.runId !== dto.runId || !o.leaseUntil || o.leaseUntil <= new Date() || !r || r.taskId !== t.id || r.status !== 'RUNNING' || r.commitSha !== t.metadata.commitSha || r.commitSha !== run.commitSha || r.environment !== 'staging')
throw new ForbiddenException('Active owned Tech lease required');
const reservation = await tx.orchestrationBudgetLedger.findUnique({ where: { id: o.reservationId } });
if (!reservation || reservation.taskId !== t.id || reservation.kind !== 'RESERVE' || reservation.amountCents !== 20 || await tx.orchestrationBudgetLedger.findFirst({ where: { taskId: t.id, kind: 'RECONCILE', metadata: { path: ['reservationId'], equals: reservation.id } } }))
throw new ForbiddenException('Unreconciled 20 cent reservation required');
await tx.orchestrationArtifact.create({ data: { runId: r.id, kind: 'TECH_PILOT_SOURCE', sha256: hash, uri: this.config.artifactBaseUrl + '/' + hash } });
await tx.orchestrationAttestation.create({ data: { runId: r.id, kind: 'TECH_PILOT_FIXTURE_QA', subjectHash: hash, signer: 'server-bounded-minmax-v1', predicate: { ...qa, applicationQa: false, costStatus: 'upper-bound-reserved' } } });
await tx.orchestrationRun.update({ where: { id: r.id }, data: { status: 'SUCCEEDED', finishedAt: new Date() } });
await tx.prepareWorkflowOutbox.update({ where: { id: o.id }, data: { status: 'DONE', leaseToken: null, leaseUntil: null } });
const updated = await tx.orchestrationTask.update({ where: { id: t.id }, data: { state: 'VERIFYING', version: { increment: 1 } } });
await tx.orchestrationEvent.create({ data: { taskId: t.id, sequence: updated.version, actorId: a.id, eventType: 'TECH_PILOT_FIXTURE_VALIDATED', fromState: t.state, toState: 'VERIFYING', idempotencyKey: `tech-pilot:${r.id}`, payload: { artifactSha256: hash, ...qa, applicationQa: false, costStatus: 'upper-bound-reserved', retainedReservationCents: 20 } } });
return { taskId: t.id, runId: r.id, state: updated.state, artifactSha256: hash, costStatus: 'upper-bound-reserved' };
});
}
async artifact(a: WorkflowIdentity, hash: string) {
if (!/^[a-f0-9]{64}$/.test(hash))
throw new BadRequestException("Invalid artifact hash");
const registered = await this.db.orchestrationArtifact.findFirst({
where: {
sha256: hash, kind: { in: ["QA", "TECH_PILOT_SOURCE"] }, uri: this.config.artifactBaseUrl + "/" + hash,
run: { environment: "staging", status: "SUCCEEDED", task: {
operationMode: "PREPARE", budgetLimitCents: 20,
} },
},
});
if (!registered) throw new NotFoundException("Artifact not registered");
const run = await this.db.orchestrationRun.findUniqueOrThrow({ where: { id: registered.runId } });
const task = await this.db.orchestrationTask.findUniqueOrThrow({ where: { id: run.taskId } });
const outbox = await this.db.prepareWorkflowOutbox.findUnique({ where: { taskId: run.taskId } });
if (!outbox || outbox.status !== "DONE" || outbox.runId !== run.id ||
(a.role === "WORKFLOW_WORKER" && outbox.leaseOwner !== a.id))
throw new NotFoundException("Artifact not registered");
const metadata = task.metadata as any;
if (metadata?.workflowScope !== "STAGE_PREPARE" || metadata?.commitSha !== run.commitSha)
throw new NotFoundException("Artifact not registered");
const tech = registered.kind === "TECH_PILOT_SOURCE";
if (tech && !admittedTechTask(task)) throw new NotFoundException("Artifact not registered");
const limit = tech ? 8192 : 2048;
let bytes: string;
let dir: FileHandle | undefined, file: FileHandle | undefined;
try {
dir = await this.artifactDir();
file = await open(`/proc/self/fd/${dir.fd}/${hash}.json`,
constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
const stat = await file.stat();
if (!stat.isFile() || stat.nlink !== 1 || stat.uid !== process.getuid!() ||
(stat.mode & 0o077) !== 0 || stat.size > limit)
throw new Error("Unsafe artifact file");
// Fixed-size descriptor read: concurrent growth cannot allocate unbounded memory.
const buffer = Buffer.alloc(limit + 1);
const { bytesRead } = await file.read(buffer, 0, buffer.length, 0);
if (bytesRead > limit) throw new Error("Oversized artifact");
bytes = buffer.subarray(0, bytesRead).toString("utf8");
} catch {
throw new NotFoundException("Artifact unavailable");
} finally { await file?.close(); await dir?.close(); }
if (createHash("sha256").update(bytes).digest("hex") !== hash)
throw new ConflictException("Artifact integrity mismatch");
let expected: string;
try { expected = tech ? techArtifact({ taskId: run.taskId, runId: run.id, commitSha: run.commitSha }, JSON.parse(bytes).source) : tracerReport({ taskId: run.taskId, runId: run.id, commitSha: run.commitSha }); }
catch { throw new ConflictException("Artifact binding mismatch"); }
if (bytes !== expected)
throw new ConflictException("Artifact binding mismatch");
return JSON.parse(bytes);
}
stage(a: WorkflowIdentity, dto: StageEvidenceDto) {
return this.workflow.stageEvidence(a, dto);
}
}
@Controller("internal/prepare-worker")
@UseGuards(WorkerIdentityGuard)
@UsePipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
}),
)
class WorkerTransportController {
constructor(private readonly service: WorkerTransportService, private readonly editorial: EditorialProposalBridge) {}
@Post("editorial-proposals") editorialProposal(@Req() req: any, @Body() dto: EditorialProposalDto) {
return this.editorial.submit(req.workerIdentity, dto);
}
@Post("claim") claim(@Req() req: any, @Body() dto: ClaimDto) {
return this.service.claim(req.workerIdentity, dto);
}
@Post("tech-claim") techClaim(@Req() req: any, @Body() dto: ClaimDto) {
return this.service.techClaim(req.workerIdentity, dto);
}
@Post("tech-complete") techComplete(@Req() req: any, @Body() dto: TechCompleteDto) {
return this.service.techComplete(req.workerIdentity, dto);
}
@Post("heartbeat") heartbeat(@Req() req: any, @Body() dto: LeaseDto) {
return this.service.heartbeat(req.workerIdentity, dto);
}
@Post("complete") complete(@Req() req: any, @Body() dto: CompleteDto) {
return this.service.complete(req.workerIdentity, dto);
}
@Post("stage-evidence") stage(
@Req() req: any,
@Body() dto: StageEvidenceDto,
) {
return this.service.stage(req.workerIdentity, dto);
}
@Get("artifacts/:hash") artifact(@Req() req: any, @Param("hash") hash: string) {
return this.service.artifact(req.workerIdentity, hash);
}
}
@Module({})
export class WorkerTransportModule {
static register(config: WorkerTransportConfig): DynamicModule {
if (typeof config.enabled !== "boolean" || !Array.isArray(config.grants) ||
!Array.isArray(config.workerIds) || !Array.isArray(config.stageCiIds))
throw new Error("Explicit boolean enable and credential allowlists required");
const allIds = [...config.workerIds, ...config.stageCiIds];
if (allIds.some(id => typeof id !== "string" || !/^[a-zA-Z0-9_-]{1,100}$/.test(id)) ||
new Set(allIds).size !== allIds.length)
throw new Error("Invalid or overlapping worker/CI allowlists");
const ids = new Set<string>(),
digests = new Set<string>();
if (
!isAbsolute(config.artifactDirectory) || config.artifactDirectory === "/" ||
config.artifactDirectory.split("/").some(p => p === "." || p === "..") ||
!validArtifactBase(config.artifactBaseUrl)
)
throw new Error(
"Absolute artifact directory and fixed HTTPS artifact base required",
);
for (const g of config.grants) {
const allowed =
g.role === "WORKFLOW_WORKER"
? config.workerIds
: g.role === "STAGE_CI"
? config.stageCiIds
: [];
if (
!/^[a-zA-Z0-9_-]{1,100}$/.test(g.id) ||
!allowed.includes(g.id) ||
!/^[a-f0-9]{64}$/.test(g.credentialSha256) ||
ids.has(g.id) ||
digests.has(g.credentialSha256) ||
(config.workerIds.includes(g.id) && config.stageCiIds.includes(g.id))
)
throw new Error("Invalid or overlapping worker/CI credential grants");
ids.add(g.id);
digests.add(g.credentialSha256);
}
const snapshot = JSON.parse(JSON.stringify(config)) as WorkerTransportConfig;
snapshot.grants.forEach(Object.freeze);
Object.freeze(snapshot.grants);
Object.freeze(snapshot.workerIds);
Object.freeze(snapshot.stageCiIds);
Object.freeze(snapshot);
return {
module: WorkerTransportModule,
imports: [PrismaModule],
controllers: [WorkerTransportController],
providers: [
PrepareWorkflowService,
WorkerIdentityGuard,
WorkerTransportService,
EditorialProposalBridge,
{ provide: CONFIG, useValue: snapshot },
{
provide: PREPARE_WORKFLOW_CONFIG,
useValue: Object.freeze({
workerIds: snapshot.workerIds,
stageCiIds: snapshot.stageCiIds,
}),
},
],
exports: [PrepareWorkflowService],
};
}
}
@@ -0,0 +1,438 @@
import { Test } from "@nestjs/testing";
import { INestApplication } from "@nestjs/common";
import { PrismaClient } from "@prisma/client";
import { PrismaService } from "../prisma/prisma.service";
import { PrepareWorkflowService } from "../orchestration/prepare-workflow.service";
import { WorkerTransportModule, tracerReport } from "./worker-transport.module";
import { createHash, randomUUID } from "crypto";
import { mkdtemp, readFile, rm, writeFile, symlink, link, rename } from "fs/promises";
import { tmpdir } from "os";
import { join } from "path";
import { execFile } from "child_process";
import { promisify } from "util";
const enabled = process.env.WORKER_TRANSPORT_DB === "1";
(enabled ? describe : describe.skip)(
"isolated PostgreSQL HTTP executor tracer",
() => {
const db = new PrismaClient();
const token = "test-only-worker-secret-".repeat(3);
const ciToken = "test-only-ci-secret-".repeat(3);
const digest = (s: string) => createHash("sha256").update(s).digest("hex");
let app: INestApplication,
workflow: PrepareWorkflowService,
base: string,
directory: string;
let pilot: any;
beforeAll(async () => {
directory = await mkdtemp(join(tmpdir(), "worker-artifacts-"));
const mod = await Test.createTestingModule({
imports: [
WorkerTransportModule.register({
enabled: true,
grants: [
{
id: "pilot",
role: "WORKFLOW_WORKER",
credentialSha256: digest(token),
},
{ id: "ci", role: "STAGE_CI", credentialSha256: digest(ciToken) },
{ id: "other", role: "WORKFLOW_WORKER", credentialSha256: digest("other-worker-secret-".repeat(3)) },
],
workerIds: ["pilot", "other"],
stageCiIds: ["ci"],
artifactDirectory: directory,
artifactBaseUrl:
"https://isolated.invalid/internal/prepare-worker/artifacts",
}),
],
})
.overrideProvider(PrismaService)
.useValue(db)
.compile();
workflow = mod.get(PrepareWorkflowService);
app = mod.createNestApplication();
await app.listen(0, "127.0.0.1");
base = await app.getUrl();
pilot = {
runOnce: async (options: any) => {
const program =
'import {runOnce} from "../infra/hermes-worker/pilot.mjs"; console.log(JSON.stringify(await runOnce(JSON.parse(process.env.TEST_PILOT_OPTIONS))));';
const result = await promisify(execFile)(
process.execPath,
["--input-type=module", "-e", program],
{
env: {
...process.env,
TEST_PILOT_OPTIONS: JSON.stringify(options),
},
timeout: 15000,
},
);
return JSON.parse(result.stdout);
},
};
});
afterAll(async () => {
await app?.close();
await db.$disconnect();
if (directory) await rm(directory, { recursive: true, force: true });
});
const owner = { id: "author", role: "Admin" },
reviewer = { id: "reviewer", role: "SUPER_ADMIN" };
async function proposal(queue = true, budget = 20) {
let t = await workflow.create(
{
key: randomUUID(),
title: "deterministic tracer",
department: "TEST",
operationMode: "PREPARE",
budgetLimitCents: budget,
metadata: {
commitSha: "a".repeat(40),
scope: "Harmless tracer only",
acceptanceCriteria: ["writes deterministic JSON; no release"],
},
},
owner,
);
if (queue) {
t = await workflow.transition(
t.id,
{
toState: "GATE1_PENDING",
expectedVersion: t.version,
idempotencyKey: randomUUID(),
},
owner,
);
t = await workflow.decide(
t.id,
1,
{
decision: "APPROVED",
expiresAt: new Date(Date.now() + 60000).toISOString(),
expectedVersion: t.version,
idempotencyKey: randomUUID(),
},
reviewer,
);
t = await workflow.transition(
t.id,
{
toState: "READY",
expectedVersion: t.version,
idempotencyKey: randomUUID(),
},
owner,
);
}
return t;
}
async function post(route: string, body: any = {}, secret = token) {
const r = await fetch(base + "/internal/prepare-worker/" + route, {
method: "POST",
headers: {
authorization: "Bearer " + secret,
"content-type": "application/json",
},
body: JSON.stringify(body),
});
return { status: r.status, body: await r.json() };
}
it("rejects overlapping worker and CI grants and accepts no credentials when disabled", async () => {
expect(() =>
WorkerTransportModule.register({
enabled: true,
grants: [
{
id: "both",
role: "WORKFLOW_WORKER",
credentialSha256: digest(token),
},
],
workerIds: ["both"],
stageCiIds: ["both"],
artifactDirectory: directory,
artifactBaseUrl: "https://isolated.invalid/internal/prepare-worker/artifacts",
}),
).toThrow(/overlapping/);
const mod = await Test.createTestingModule({
imports: [
WorkerTransportModule.register({
enabled: false,
grants: [],
workerIds: [],
stageCiIds: [],
artifactDirectory: directory,
artifactBaseUrl: "https://isolated.invalid/internal/prepare-worker/artifacts",
}),
],
})
.overrideProvider(PrismaService)
.useValue(db)
.compile();
const disabled = mod.createNestApplication();
await disabled.listen(0, "127.0.0.1");
try {
const r = await fetch(
(await disabled.getUrl()) + "/internal/prepare-worker/claim",
{
method: "POST",
headers: {
authorization: "Bearer " + token,
"content-type": "application/json",
},
body: JSON.stringify({ taskId: randomUUID() }),
},
);
expect(r.status).toBe(403);
} finally {
await disabled.close();
}
});
it("rejects unsafe artifact bases and freezes validated configuration", () => {
const config = { enabled: true, grants: [], workerIds: [], stageCiIds: [], artifactDirectory: directory,
artifactBaseUrl: "https://isolated.invalid/internal/prepare-worker/artifacts" };
for (const artifactBaseUrl of ["https://user:pass@isolated.invalid/internal/prepare-worker/artifacts", "https://127.0.0.1/internal/prepare-worker/artifacts", "https://10.1.2.3/internal/prepare-worker/artifacts", "https://localhost/internal/prepare-worker/artifacts", "https://isolated.invalid/arbitrary", "https://isolated.invalid/x/../internal/prepare-worker/artifacts", "https://[::1]/internal/prepare-worker/artifacts"]) {
expect(() => WorkerTransportModule.register({ ...config, artifactBaseUrl })).toThrow();
}
const registered = WorkerTransportModule.register(config);
const snapshot = (registered.providers as any[]).find(p => p.useValue?.artifactBaseUrl)?.useValue;
expect(Object.isFrozen(snapshot)).toBe(true);
expect(Object.isFrozen(snapshot.workerIds)).toBe(true);
expect(Object.isFrozen(snapshot.grants)).toBe(true);
const workflowConfig = (registered.providers as any[]).find(p => p.provide === "PREPARE_WORKFLOW_CONFIG").useValue;
expect(Object.isFrozen(workflowConfig)).toBe(true);
expect(() => WorkerTransportModule.register({ ...config, enabled: "true" as any })).toThrow();
});
it("denies untrusted identities and body impersonation; dispatches nothing before Gate1", async () => {
const t = await proposal(false);
expect((await post("claim", {}, "wrong")).status).toBe(401);
expect((await post("claim", { taskId: t.id }, ciToken)).status).toBe(403);
expect(
(await post("claim", { taskId: t.id, actorId: "pilot" })).status,
).toBe(400);
expect((await post("claim", { taskId: t.id })).body).toEqual({
lease: null,
});
expect(await db.orchestrationRun.count({ where: { taskId: t.id } })).toBe(
0,
);
});
it("races claims, fences expired leases and reserves unknown cost against the 20 cent cap", async () => {
const t = await proposal();
const claims = await Promise.all([
post("claim", { taskId: t.id }),
post("claim", { taskId: t.id }),
]);
const leases = claims.map((r) => r.body.lease).filter(Boolean);
expect(leases).toHaveLength(1);
const lease = leases[0],
binding = {
taskId: t.id,
runId: lease.runId,
leaseToken: lease.leaseToken,
};
expect(
(await post("heartbeat", { ...binding, leaseToken: randomUUID() }))
.status,
).toBe(403);
expect(
(
await post("complete", {
...binding,
report: "arbitrary shell or prompt",
})
).status,
).toBe(400);
expect(
(await post("complete", { ...binding, report: "{}", actualCents: 21 }))
.status,
).toBe(400);
await db.prepareWorkflowOutbox.update({
where: { taskId: t.id },
data: { leaseUntil: new Date(Date.now() - 1000) },
});
expect((await post("heartbeat", binding)).status).toBe(403);
expect(
(await post("complete", { ...binding, report: "{}" })).status,
).toBe(403);
expect((await post("claim", { taskId: t.id })).status).toBe(403);
expect(await db.orchestrationRun.count({ where: { taskId: t.id } })).toBe(
1,
);
expect(
(
await db.orchestrationBudgetLedger.aggregate({
where: { taskId: t.id },
_sum: { amountCents: true },
})
)._sum.amountCents,
).toBe(20);
const larger = await proposal(true, 40);
expect((await post("claim", { taskId: larger.id })).status).toBe(403);
});
it("rechecks kill switch and task interrupt on heartbeat and completion; separates CI authority", async () => {
const t = await proposal(),
lease = (await post("claim", { taskId: t.id })).body.lease;
const binding = {
taskId: t.id,
runId: lease.runId,
leaseToken: lease.leaseToken,
};
await db.orchestrationKillSwitch.upsert({
where: { scope: "GLOBAL" },
create: {
scope: "GLOBAL",
level: "STOP_AGENTS",
active: true,
reason: "isolated test",
actorId: "test",
},
update: { level: "STOP_AGENTS", active: true },
});
try {
expect((await post("heartbeat", binding)).status).toBe(403);
expect(
(await post("complete", { ...binding, report: "{}" })).status,
).toBe(403);
expect((await post("claim", { taskId: t.id })).status).toBe(403);
} finally {
await db.orchestrationKillSwitch.deleteMany();
}
await db.orchestrationTask.update({
where: { id: t.id },
data: { state: "CANCELLED" },
});
expect((await post("heartbeat", binding)).status).toBe(403);
expect(
(await post("complete", { ...binding, report: "{}" })).status,
).toBe(403);
expect(
await db.orchestrationArtifact.count({ where: { runId: lease.runId } }),
).toBe(0);
const evidence = {
taskId: t.id,
runId: lease.runId,
commitSha: "a".repeat(40),
sha256: "b".repeat(64),
evidenceSha256: "c".repeat(64),
uri: "https://isolated.invalid/not-real-ci",
};
expect((await post("stage-evidence", evidence)).status).toBe(403);
expect((await post("stage-evidence", evidence, ciToken)).status).toBe(
403,
);
});
it("never follows or overwrites preexisting artifact links on completion", async () => {
const t = await proposal();
const lease = (await post("claim", { taskId: t.id })).body.lease;
const report = tracerReport(lease), hash = digest(report);
const target = join(directory, "sentinel");
await writeFile(target, "do not overwrite");
const file = join(directory, hash + ".json");
await symlink(target, file);
const result = await post("complete", { taskId: t.id, runId: lease.runId, leaseToken: lease.leaseToken, report });
expect(await readFile(target, "utf8")).toBe("do not overwrite");
expect(result.status).toBe(409);
expect(await db.orchestrationArtifact.count({ where: { runId: lease.runId } })).toBe(0);
await rm(file);
await link(target, file);
expect((await post("complete", { taskId: t.id, runId: lease.runId, leaseToken: lease.leaseToken, report })).status).toBe(409);
expect(await readFile(target, "utf8")).toBe("do not overwrite");
});
it("runs actual HTTP deterministic executor to VERIFYING with retrievable content-addressed artifact", async () => {
const t = await proposal();
const result = await pilot.runOnce({
baseUrl: base,
credential: token,
taskId: t.id,
allowLoopbackHttp: true,
});
expect(result.state).toBe("VERIFYING");
const row = await db.orchestrationTask.findUniqueOrThrow({
where: { id: t.id },
});
expect(row.state).toBe("VERIFYING");
const artifact = await db.orchestrationArtifact.findFirstOrThrow({
where: { run: { taskId: t.id } },
});
const bytes = await readFile(join(directory, artifact.sha256 + ".json"));
expect(digest(bytes.toString())).toBe(artifact.sha256);
expect(JSON.parse(bytes.toString())).toMatchObject({
kind: "DETERMINISTIC_PREPARE_TRACER",
commitSha: "a".repeat(40),
environment: "staging",
llmPilot: false,
});
const get = await fetch(
base + "/internal/prepare-worker/artifacts/" + artifact.sha256,
{ headers: { authorization: "Bearer " + token } },
);
expect(get.status).toBe(200);
expect(await get.text()).toBe(bytes.toString());
const artifactGet = (secret: string) => fetch(base + "/internal/prepare-worker/artifacts/" + artifact.sha256,
{ headers: { authorization: "Bearer " + secret } });
expect((await artifactGet("other-worker-secret-".repeat(3))).status).toBe(404);
expect((await artifactGet(ciToken)).status).toBe(200);
const foreignHash = digest("{}");
await writeFile(join(directory, foreignHash + ".json"), "{}");
await db.orchestrationArtifact.create({ data: { runId: artifact.runId, kind: "QA", sha256: foreignHash, uri: "https://foreign.invalid/" + foreignHash } });
expect((await fetch(base + "/internal/prepare-worker/artifacts/" + foreignHash, { headers: { authorization: "Bearer " + token } })).status).toBe(404);
const moved = directory + "-moved";
await rename(directory, moved);
try {
await symlink(moved, directory);
expect((await artifactGet(token)).status).toBe(404);
} finally { await rm(directory, { force: true }); await rename(moved, directory); }
const file = join(directory, artifact.sha256 + ".json"), saved = file + ".saved";
await rename(file, saved);
try {
await symlink(saved, file);
expect((await artifactGet(token)).status).toBe(404);
await rm(file);
await link(saved, file);
expect((await artifactGet(token)).status).toBe(404);
await rm(file);
} finally { await rm(file, { force: true }); await rename(saved, file); }
const cost = await db.orchestrationBudgetLedger.aggregate({
where: { taskId: t.id },
_sum: { amountCents: true },
});
expect(cost._sum.amountCents).toBe(0);
expect(
await db.orchestrationAttestation.findFirst({
where: { run: { taskId: t.id }, kind: "STAGE" },
}),
).toBeNull();
const evidence = {
taskId: t.id,
state: row.state,
sha256: artifact.sha256,
artifact: JSON.parse(bytes.toString()),
httpGet: get.status,
costCents: cost._sum.amountCents,
uriExternalReachability: "NOT_TESTED_ISOLATED_FIXTURE_HOST",
};
console.log("TRACER_EVIDENCE", JSON.stringify(evidence));
if (process.env.TRACER_EVIDENCE_PATH)
await writeFile(
process.env.TRACER_EVIDENCE_PATH,
JSON.stringify(evidence, null, 2),
);
await writeFile(
join(directory, artifact.sha256 + ".json"),
'{"tampered":true}',
);
expect(
(
await fetch(
base + "/internal/prepare-worker/artifacts/" + artifact.sha256,
{ headers: { authorization: "Bearer " + token } },
)
).status,
).toBe(409);
});
},
);
@@ -0,0 +1,43 @@
# Bounded Stage PREPARE workflow foundation
This slice is NOT a runtime executor, deployment mechanism, or full v4 completion. It enables authenticated proposal/Gate1/queue APIs and provides internal worker/CI service methods. Default worker/CI identity allowlists are empty: no browser or production execution route exists.
## Panel HTTP contract
All routes require existing JWT authentication with `Admin` or `SUPER_ADMIN`. Actor is exclusively `req.user.id ?? req.user.sub`, role exclusively `req.user.role`. Top-level unknown mutation fields return 400; client actor/profile fields are never trusted.
Base `/admin/orchestration`:
- `GET /tasks`: task array.
- `GET /tasks/:id`: task object.
- `GET /overview`: existing inventory items plus capabilities `{mutations:true, prepare:true, release:false, gate2Decisions:false, chat:false, runtime:false}`, reason `bounded_stage_prepare_only`.
- `POST /tasks`: existing CreateOrchestrationTaskDto `{key,title,department,operationMode:"PREPARE",budgetLimitCents,metadata:{commitSha,scope,acceptanceCriteria}}`; commitSha is required lowercase 40hex; scope is a non-blank string of at most 2000 characters; acceptanceCriteria is 1..20 non-blank strings of at most 500 characters each; budget is integer 20..10000 cents. Returns 201 projected task, state DRAFT/version1. Validated scope and acceptance criteria are persisted exactly and remain unchanged by workflow transitions. The internal `workflowScope:"STAGE_PREPARE"`, environment and createdBy are server-owned; input profile/actor/other arbitrary keys are discarded. Key max120, title max255, department max50. Proposal key is unique; sequential or concurrent duplicate keys return 409, without updating the original task or appending another proposal event. Fetch/reconcile the existing task rather than retry blindly.
- `PUT /tasks/:id/transition`: `{toState:"GATE1_PENDING"|"READY",expectedVersion,idempotencyKey,reason?}`. Only DRAFT->GATE1_PENDING and GATE1_APPROVED->READY. Owner or SUPER_ADMIN. Returns 200 projected task. READY atomically creates dispatch outbox. Requires unexpired Gate1.
- `POST /tasks/:id/gates/1/decide`: existing GateDecisionDto `{decision:"APPROVED"|"REJECTED",expectedVersion,idempotencyKey,expiresAt,notes?}`. SUPER_ADMIN independent from proposer. Future expiry <=24h. Returns 201 **projected task**, not decision object.
- Gate2 decision, budget writes/reconcile, command routes (including release), chat and kill-switch HTTP mutations remain 403. Runtime resource reads remain 503.
Task projection keeps existing scalar fields and safe nested runs/artifacts, decisions, events and ledger. Adds only `metadata:{scope,acceptanceCriteria,environment:"staging",commitSha}` for bounded proposals, otherwise `{}`. Never includes createdBy, workflowScope, payloads, URI, signatures, tokens or lease credentials. Pre-fix rows with the legacy scope sentinel remain readable/operable but have no recovered acceptance criteria: do not represent those as complete immutable scope review evidence.
`availableActions:string[]` is state/identity-derived: DRAFT owner -> `SUBMIT_GATE1`; GATE1_PENDING independent SUPER_ADMIN -> `APPROVE_GATE1`,`REJECT_GATE1`; GATE1_APPROVED owner/SUPER_ADMIN -> `QUEUE_PREPARE`; all worker/verification/Gate2 and legacy states -> `[]`. Actions indicate workflow eligibility, not a guarantee: current kill switch, approval expiry and concurrent versions are rechecked transactionally on mutation. Panel must not infer actions from state or send actor identity.
Common errors: 400 invalid DTO/binding; 403 wrong scope/identity/independence/kill/budget/expiry; 409 duplicate proposal key, stale version or idempotency payload mismatch. Transition/decision replay returns current persisted task and does not duplicate side effects.
## Internal service contracts (NOT browser endpoints)
`PrepareWorkflowService` is exported by OrchestrationModule. Trusted backend composition must override `PREPARE_WORKFLOW_CONFIG` with explicit `workerIds` and `stageCiIds`, default both empty. Do not populate from request bodies or user-selected runtime profiles. Worker identities require role WORKFLOW_WORKER AND ID allowlist; Stage identities require STAGE_CI AND separate ID allowlist. Credential/session provisioning and transport remain the integrating runtime owner's responsibility.
- `claim(identity, taskId?) -> null | {taskId,runId,attempt,leaseToken,leaseUntil,commitSha,environment:"staging",maxCostCents:20}`. Transactional task row lock with SKIP LOCKED, immutable run binding, 30s lease, max3 attempts. Each attempt reserves **a fixed hard upper bound of 20 cents**, not an estimated or measured departmental cost. Unknown cost on expired attempts remains reserved conservatively.
- `heartbeat(identity,{taskId,runId,leaseToken}) -> {leaseUntil}`. Same owner/token, unexpired active lease required.
- `complete(identity,{taskId,runId,leaseToken,sha256,uri,actualCents}) -> task`. QA success ingestion only; lowercase sha256, HTTPS URI, actual cost integer0..20 from trusted worker. Atomically records immutable QA artifact/attestation, SUCCEEDED run, reservation reconciliation, DONE outbox and VERIFYING task. Expired/stale/foreign token cannot finish. Not replayable after success; caller must read persisted task/run after response loss.
- `stageEvidence(identity,{taskId,runId,sha256,commitSha,evidenceSha256,uri}) -> task`. Independently allowlisted Stage CI identity, successful exact staging commit/run, registered QA subject, independent QA signer. Atomically records Stage artifact/attestation and reaches GATE2_PENDING. No release or Gate2 approval. Does not perform or claim deployment itself. Caller supplies actual trusted CI evidence, not browser assertions.
## Verification and explicit gaps
Real isolated PostgreSQL16, no published ports/no live volumes. Added migrations applied with ON_ERROR_STOP. TDD RED logs cover missing workflow behavior, heartbeat/QA behavior and HTTP enablement. Full backend regression excluding existing legacy release-containing orchestration.integration.spec: 106 suites /851 tests including seven real-DB workflow cases, build exit0. No RELEASE_AUTHORIZE service operation executed.
DB regressions: concurrent claim winner, queue race/replay mismatch, hard budget rollback, stale lease fencing, max3 attempts, conservative expired costs, kill switch, self approval, trusted IDs, QA reconciliation and Stage evidence to Gate2 pending. Test fixture artifact URIs/hashes are not real deployment evidence.
Remaining: actual authenticated runtime worker transport/executor and pinned CI evidence transport; shared department/company budget model and locking (NOT implemented or represented by fake totals); retry backoff/dead-letter UX/cancellation; hard total execution wall-time; durable request replay of completion/evidence; kill-switch concurrent insertion/version locking; migration rehearsal with full legacy schema; real Stage CI artifact/deployment verification. No push/deploy/live DB/runtime edits. Production release remains denied at HTTP boundary; unsafe legacy core release method is deliberately untouched and must never be called by this slice.
## Contract repair verification
Actual frontend inspection confirmed proposal submission sent human scope and acceptanceCriteria but omitted required commitSha. Frontend repair is separately assigned; no frontend file changed here. New proposals now require all three fields. Internal Stage restriction uses a separate server-owned workflowScope marker rather than overwriting user scope. No metadata update route exists in this bounded workflow.
TDD RED→GREEN against isolated PostgreSQL16 reproduced dropped scope/criteria, nine invalid metadata acceptances (including array-to-string commit coercion), and duplicate-key P2002 leakage. Final database suite: 24 cases, including authenticated HTTP create/list/detail persistence, 400 validation, and 409 duplicate responses. Full allowed backend regression: 106 suites / 868 tests passed; legacy orchestration.integration.spec.ts excluded to avoid executing RELEASE_AUTHORIZE. Build exit0. Scoped lint remains failing on inherited require-await/no-unnecessary-type-assertion errors (13 errors); no lint suppression or config change applied. Disposable DB only; no live edits/push/deploy.
@@ -0,0 +1,90 @@
import { Type } from 'class-transformer';
import {
IsEnum,
IsHash,
IsIn,
IsInt,
IsISO8601,
IsNotEmpty,
IsOptional,
IsString,
Max,
Min,
} from 'class-validator';
import { OperationMode, WorkflowState } from '../orchestration.domain';
export class CreateOrchestrationTaskDto {
@IsString() @IsNotEmpty() key!: string;
@IsString() @IsNotEmpty() title!: string;
@IsString() @IsNotEmpty() department!: string;
@IsEnum(OperationMode) operationMode!: OperationMode;
@Type(() => Number) @IsInt() @Min(0) budgetLimitCents!: number;
@IsOptional() metadata?: Record<string, unknown>;
}
export class TransitionTaskDto {
@IsEnum(WorkflowState) toState!: WorkflowState;
@Type(() => Number) @IsInt() @Min(1) expectedVersion!: number;
@IsString() @IsNotEmpty() idempotencyKey!: string;
@IsOptional() @IsString() reason?: string;
}
export class GateDecisionDto {
@IsIn(['APPROVED', 'REJECTED']) decision!: 'APPROVED' | 'REJECTED';
@Type(() => Number) @IsInt() @Min(1) expectedVersion!: number;
@IsString() @IsNotEmpty() idempotencyKey!: string;
@IsISO8601() expiresAt!: string;
@IsOptional() @IsString() notes?: string;
@IsOptional() @IsString() runId?: string;
@IsOptional() @IsHash('sha256') artifactHash?: string;
@IsOptional() @IsString() commitSha?: string;
@IsOptional() @IsString() environment?: string;
}
export class RecordBudgetDto {
@Type(() => Number) @IsInt() @Min(1) amountCents!: number;
@IsString() @IsNotEmpty() idempotencyKey!: string;
@IsString() @IsNotEmpty() kind!: string;
@IsOptional() metadata?: Record<string, unknown>;
}
export class SetKillSwitchDto {
@IsIn([
'FREEZE_NEW_WORK',
'STOP_AGENTS',
'DISABLE_MUTATIONS',
'EMERGENCY_HALT',
])
level!: string;
@IsString() @IsNotEmpty() reason!: string;
}
export class ReconcileBudgetDto {
@IsString() @IsNotEmpty() reservationId!: string;
@Type(() => Number) @IsInt() @Min(0) actualCents!: number;
@IsString() @IsNotEmpty() idempotencyKey!: string;
}
export class OrchestrationCommandDto {
@Type(() => Number) @IsInt() @Min(1) expectedVersion!: number;
@IsString() @IsNotEmpty() idempotencyKey!: string;
@IsOptional() @IsString() commitSha?: string;
@IsOptional()
@IsIn(['development', 'staging', 'production'])
environment?: string;
@IsOptional() @IsString() runId?: string;
@IsOptional() @IsString() approvalId?: string;
@IsOptional() @IsString() dependsOnTaskId?: string;
@IsOptional() @IsIn(['SUCCEEDED', 'FAILED']) status?: string;
@IsOptional()
@IsIn([
'TRANSIENT',
'RATE_LIMITED',
'DEPENDENCY',
'VALIDATION',
'POLICY',
'PERMANENT',
])
retryClass?: string;
@IsOptional() @IsHash('sha256') sha256?: string;
@IsOptional() @IsHash('sha256') artifactHash?: string;
@IsOptional() @IsHash('sha256') subjectHash?: string;
@IsOptional() @IsString() kind?: string;
@IsOptional() @IsString() uri?: string;
@IsOptional() predicate?: Record<string, unknown>;
@IsOptional() @IsString() signature?: string;
}
@@ -0,0 +1,45 @@
import { ForbiddenException } from '@nestjs/common';
import { KillSwitchService } from './kill-switch.service';
describe('KillSwitchService', () => {
const prisma = { orchestrationKillSwitch: { findUnique: jest.fn() } };
const service = new KillSwitchService(prisma as never);
beforeEach(() => jest.resetAllMocks());
it.each([
['FREEZE_NEW_WORK', 'CREATE_TASK'],
['STOP_AGENTS', 'RUN_AGENT'],
['DISABLE_MUTATIONS', 'MUTATE_PRODUCTION'],
['EMERGENCY_HALT', 'OBSERVE'],
])('blocks %s at its policy boundary', async (level, action) => {
prisma.orchestrationKillSwitch.findUnique.mockResolvedValue({
scope: 'GLOBAL',
level,
active: true,
});
await expect(service.assertAllowed(action as never)).rejects.toThrow(
ForbiddenException,
);
});
it('freezes new claims while allowing an already running agent to finish', async () => {
prisma.orchestrationKillSwitch.findUnique.mockResolvedValue({scope:'GLOBAL',level:'FREEZE_NEW_WORK',active:true});
await expect(service.assertAllowed('CLAIM_TASK' as never)).rejects.toThrow(ForbiddenException);
await expect(service.assertAllowed('RUN_AGENT')).resolves.toBeUndefined();
});
it('fails closed when an active persisted kill level is unknown', async () => {
prisma.orchestrationKillSwitch.findUnique.mockResolvedValue({scope:'GLOBAL',level:'CORRUPT_LEVEL',active:true});
await expect(service.assertAllowed('RUN_AGENT')).rejects.toThrow(ForbiddenException);
});
it('allows observation while production mutations are disabled', async () => {
prisma.orchestrationKillSwitch.findUnique.mockResolvedValue({
scope: 'GLOBAL',
level: 'DISABLE_MUTATIONS',
active: true,
});
await expect(service.assertAllowed('OBSERVE')).resolves.toBeUndefined();
});
});
@@ -0,0 +1,36 @@
import { ForbiddenException, Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { KillSwitchLevel } from './orchestration.domain';
export type GuardedAction =
| 'CREATE_TASK'
| 'CLAIM_TASK'
| 'RUN_AGENT'
| 'MUTATE_PRODUCTION'
| 'OBSERVE';
@Injectable()
export class KillSwitchService {
constructor(private readonly prisma: PrismaService) {}
async assertAllowed(action: GuardedAction): Promise<void> {
const state = await (this.prisma as any).orchestrationKillSwitch.findUnique(
{ where: { scope: 'GLOBAL' } },
);
if (!state?.active) return;
const blocked: Record<KillSwitchLevel, GuardedAction[]> = {
FREEZE_NEW_WORK: ['CREATE_TASK', 'CLAIM_TASK'],
STOP_AGENTS: ['CREATE_TASK', 'CLAIM_TASK', 'RUN_AGENT'],
DISABLE_MUTATIONS: ['CREATE_TASK', 'CLAIM_TASK', 'RUN_AGENT', 'MUTATE_PRODUCTION'],
EMERGENCY_HALT: [
'CREATE_TASK',
'CLAIM_TASK',
'RUN_AGENT',
'MUTATE_PRODUCTION',
'OBSERVE',
],
};
if (!Object.values(KillSwitchLevel).includes(state.level) ||
blocked[state.level as KillSwitchLevel].includes(action))
throw new ForbiddenException(
`Orchestration kill switch ${state.level} blocks ${action}`,
);
}
}
@@ -0,0 +1,18 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
describe('orchestration migration safety', () => {
it('is additive and never mutates legacy governance tables', () => {
const sql = readFileSync(
join(
process.cwd(),
'prisma/migrations/20261022000000_add_orchestration_v4_core/migration.sql',
),
'utf8',
);
expect(sql).not.toMatch(/^\s*(DROP|TRUNCATE|DELETE|UPDATE)\b/im);
expect(sql).not.toMatch(
/ALTER TABLE "(approval_requests|department_tasks|audit_logs)"/i,
);
expect(sql.match(/CREATE TABLE "orchestration_/g)).toHaveLength(9);
});
});
@@ -0,0 +1,68 @@
import { BadRequestException } from '@nestjs/common';
import { ModelTierPolicyService } from './model-tier-policy.service';
describe('ModelTierPolicyService', () => {
const policy = new ModelTierPolicyService();
it.each([
[
{
complexity: 0,
risk: 0,
operationMode: 'OBSERVE',
remainingBudgetCents: 1,
},
'L0',
],
[
{
complexity: 2,
risk: 1,
operationMode: 'PREPARE',
remainingBudgetCents: 100,
},
'L1',
],
[
{
complexity: 5,
risk: 4,
operationMode: 'PREPARE',
remainingBudgetCents: 500,
},
'L2',
],
[
{
complexity: 7,
risk: 7,
operationMode: 'PREPARE',
remainingBudgetCents: 1500,
},
'L3',
],
[
{
complexity: 9,
risk: 9,
operationMode: 'MUTATE_PRODUCTION',
remainingBudgetCents: 5000,
},
'L4',
],
])('selects a deterministic tier for policy inputs', (input, expected) => {
expect(policy.select(input as never)).toBe(expected);
expect(policy.select(input as never)).toBe(expected);
});
it('fails closed when the selected tier exceeds the remaining hard budget', () => {
expect(() =>
policy.select({
complexity: 9,
risk: 9,
operationMode: 'MUTATE_PRODUCTION',
remainingBudgetCents: 100,
}),
).toThrow(BadRequestException);
});
});
@@ -0,0 +1,40 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { OperationMode } from './orchestration.domain';
export type ModelTier = 'L0' | 'L1' | 'L2' | 'L3' | 'L4';
@Injectable()
export class ModelTierPolicyService {
select(input: {
complexity: number;
risk: number;
operationMode: OperationMode | string;
remainingBudgetCents: number;
}): ModelTier {
const score =
Math.max(input.complexity, input.risk) +
(String(input.operationMode) === String(OperationMode.MUTATE_PRODUCTION)
? 1
: 0);
const tier: ModelTier =
score <= 1
? 'L0'
: score <= 3
? 'L1'
: score <= 5
? 'L2'
: score <= 8
? 'L3'
: 'L4';
const floor: Record<ModelTier, number> = {
L0: 1,
L1: 25,
L2: 100,
L3: 500,
L4: 2000,
};
if (input.remainingBudgetCents < floor[tier])
throw new BadRequestException(
`Hard budget cannot fund selected model tier ${tier}`,
);
return tier;
}
}
@@ -0,0 +1,157 @@
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { OrchestrationController } from './orchestration.controller';
import { OrchestrationService } from './orchestration.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
describe('integration read boundary', () => {
let app: INestApplication;
const task = {
id: 't1',
key: 'k',
title: 'Review',
department: 'SEO',
state: 'GATE2_APPROVED',
version: 1,
operationMode: 'PREPARE',
budgetLimitCents: 0,
metadata: { token: 'secret' },
runs: [
{
id: 'r',
status: 'VERIFIED',
commitSha: 'abc',
environment: 'stage',
artifacts: [
{
id: 'a',
sha256: 'f'.repeat(64),
sizeBytes: 1n,
uri: 'secret-uri',
metadata: { token: 'secret' },
},
],
},
],
events: [
{
id: 'e',
eventType: 'CREATED',
sequence: 1,
payload: { token: 'secret' },
},
],
};
const service = {
findAll: jest.fn(() => Promise.resolve([task])),
findOne: jest.fn(() => Promise.resolve(task)),
createTask: jest.fn(),
transition: jest.fn(),
decideGate: jest.fn(),
recordBudget: jest.fn(),
setKillSwitch: jest.fn(),
command: jest.fn(),
reconcileBudget: jest.fn(),
};
beforeAll(async () => {
const m = await Test.createTestingModule({
controllers: [OrchestrationController],
providers: [
{ provide: OrchestrationService, useValue: service },
RolesGuard,
],
})
.overrideGuard(JwtAuthGuard)
.useValue({
canActivate(c: any) {
const req = c.switchToHttp().getRequest();
if (!req.headers.authorization) return false;
req.user = { id: 'verified-user', role: req.headers.authorization };
return true;
},
})
.compile();
app = m.createNestApplication();
await app.init();
});
afterAll(async () => app.close());
it('denies every mutation before legacy core is invoked', async () => {
for (const [method, path] of [
['post', 'tasks'],
['put', 'tasks/t1/transition'],
['post', 'tasks/t1/gates/2/decide'],
['post', 'tasks/t1/budget'],
['post', 'tasks/t1/commands/RUN_CREATE'],
['post', 'tasks/t1/budget/reconcile'],
['put', 'kill-switch'],
['post', 'chat/messages'],
] as const) {
await request(app.getHttpServer())
[method]('/admin/orchestration/' + path)
.set('Authorization', 'Admin')
.send({})
.expect(path === 'tasks' || path.endsWith('/transition') ? 400 : 403);
}
for (const name of [
'createTask',
'transition',
'decideGate',
'recordBudget',
'setKillSwitch',
'command',
'reconcileBudget',
] as const)
expect(service[name]).not.toHaveBeenCalled();
});
it('projects list and detail with no actions, arbitrary secrets or BigInt transport failures', async () => {
for (const path of ['tasks', 'tasks/t1']) {
const r = await request(app.getHttpServer())
.get('/admin/orchestration/' + path)
.set('Authorization', 'Admin')
.expect(200);
const t = Array.isArray(r.body) ? r.body[0] : r.body;
expect(t.availableActions).toEqual([]);
expect(t.budgetLimitCents).toBe(0);
expect(JSON.stringify(t)).not.toMatch(/secret|sizeBytes|payload|token/);
}
});
it('returns actual inventory overview and explicit unavailable runtime resources', async () => {
const r = await request(app.getHttpServer())
.get('/admin/orchestration/overview')
.set('Authorization', 'Admin')
.expect(200);
expect(r.body.items).toEqual([
expect.objectContaining({ id: 'tasks', count: 1 }),
]);
expect(r.body.capabilities).toMatchObject({
mutations: false,
chat: false,
runtime: false,
});
for (const path of ['agents', 'schedules', 'model-policy'])
await request(app.getHttpServer())
.get('/admin/orchestration/' + path)
.set('Authorization', 'Admin')
.expect(503);
const releases = await request(app.getHttpServer())
.get('/admin/orchestration/releases')
.set('Authorization', 'Admin')
.expect(200);
expect(releases.body.items[0]).toMatchObject({
id: 'r',
commitSha: 'abc',
environment: 'stage',
});
});
it('protects read surfaces from anonymous and customer access', async () => {
await request(app.getHttpServer())
.get('/admin/orchestration/overview')
.expect(403);
await request(app.getHttpServer())
.get('/admin/orchestration/tasks')
.set('Authorization', 'CUSTOMER')
.expect(403);
});
});
@@ -0,0 +1,230 @@
import {
Controller,
Body,
Req,
Optional,
ValidationPipe,
Get,
Param,
Post,
Put,
UseGuards,
ForbiddenException,
ServiceUnavailableException,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles } from '../common/decorators/roles.decorator';
import { OrchestrationService } from './orchestration.service';
import {
PrepareWorkflowService,
WorkflowIdentity,
} from './prepare-workflow.service';
import {
CreateOrchestrationTaskDto,
TransitionTaskDto,
GateDecisionDto,
} from './dto/orchestration.dto';
const workflowValidation = new ValidationPipe({
transform: true,
whitelist: true,
forbidNonWhitelisted: true,
});
// Explicit response allowlists: never serialize arbitrary JSON, signatures, URIs or BigInt.
function pick(row: any, keys: string[]) {
return Object.fromEntries(
keys.filter((k) => row[k] !== undefined).map((k) => [k, row[k]]),
);
}
function project(row: any) {
return {
...pick(row, [
'id',
'key',
'title',
'department',
'state',
'version',
'operationMode',
'budgetLimitCents',
]),
availableActions: [],
runs: (row.runs ?? []).map((run: any) => ({
...pick(run, ['id', 'commitSha', 'environment', 'status']),
artifacts: (run.artifacts ?? []).map((a: any) =>
pick(a, ['id', 'kind', 'sha256']),
),
})),
decisions: (row.decisions ?? []).map((d: any) =>
pick(d, [
'id',
'gate',
'decision',
'expiresAt',
'runId',
'artifactHash',
'commitSha',
'environment',
'createdAt',
]),
),
events: (row.events ?? []).map((e: any) =>
pick(e, [
'id',
'eventType',
'fromState',
'toState',
'sequence',
'createdAt',
]),
),
budgetLedger: (row.budgetLedger ?? []).map((b: any) =>
pick(b, ['id', 'amountCents', 'kind', 'createdAt']),
),
};
}
@ApiTags('Admin - Orchestration v4')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('Admin', 'SUPER_ADMIN')
@Controller('admin/orchestration')
export class OrchestrationController {
constructor(
private readonly service: OrchestrationService,
@Optional() private readonly workflow?: PrepareWorkflowService,
) {}
private actor(req: any): WorkflowIdentity {
const id = req?.user?.id ?? req?.user?.sub;
if (
typeof id !== 'string' ||
!id ||
!['Admin', 'SUPER_ADMIN'].includes(req?.user?.role)
)
throw new ForbiddenException('Authenticated workflow identity required');
return { id, role: req.user.role };
}
private safe(row: any, req: any) {
const view = project(row);
if (!this.workflow) return view;
const w = this.workflow.project(row, this.actor(req));
return {
...view,
metadata: w.metadata,
availableActions: w.availableActions,
};
}
@Get('tasks') async all(@Req() req: any) {
return (await this.service.findAll()).map((row: any) =>
this.safe(row, req),
);
}
@Get('tasks/:id') async one(@Param('id') id: string, @Req() req: any) {
return this.safe(await this.service.findOne(id), req);
}
@Get('overview') async overview() {
const tasks = await this.service.findAll();
return {
items: [
{ id: 'tasks', title: 'Persisted task inventory', count: tasks.length },
],
measuredAt: new Date().toISOString(),
capabilities: {
mutations: !!this.workflow,
prepare: !!this.workflow,
release: false,
gate2Decisions: false,
chat: false,
runtime: false,
},
reason: this.workflow
? 'bounded_stage_prepare_only'
: 'security_hardening_pending',
};
}
@Get('releases') async releases() {
const tasks = await this.service.findAll();
const details = await Promise.all(
tasks.map((t: any) => this.service.findOne(t.id)),
);
return {
items: details.flatMap((t: any) =>
(t.runs ?? []).map((run: any) =>
pick(run, ['id', 'status', 'commitSha', 'environment']),
),
),
measuredAt: new Date().toISOString(),
};
}
@Get('agents') agents() {
throw new ServiceUnavailableException(
'Hermes runtime unavailable: trusted identity and runtime authentication not configured',
);
}
@Get('schedules') schedules() {
throw new ServiceUnavailableException(
'Hermes schedules unavailable; runtime remains disabled',
);
}
@Get('model-policy') models() {
throw new ServiceUnavailableException(
'No persisted model policy read contract available',
);
}
// Only the separate bounded PREPARE service is exposed; legacy commands remain denied.
@Post('tasks') async create(
@Body(workflowValidation) dto: CreateOrchestrationTaskDto,
@Req() req: any,
) {
if (!this.workflow) return this.deny();
return this.safe(await this.workflow.create(dto, this.actor(req)), req);
}
@Put('tasks/:id/transition') async transition(
@Param('id') id: string,
@Body(workflowValidation) dto: TransitionTaskDto,
@Req() req: any,
) {
if (!this.workflow) return this.deny();
return this.safe(
await this.workflow.transition(id, dto, this.actor(req)),
req,
);
}
@Post('tasks/:id/gates/:gate/decide') async decide(
@Param('id') id: string,
@Param('gate') gate: string,
@Body() body: unknown,
@Req() req: any,
) {
if (!this.workflow || gate !== '1') return this.deny();
const dto = await workflowValidation.transform(body, {
type: 'body',
metatype: GateDecisionDto,
});
return this.safe(
await this.workflow.decide(id, 1, dto, this.actor(req)),
req,
);
}
@Post('tasks/:id/budget') budget() {
return this.deny();
}
@Put('kill-switch') kill() {
return this.deny();
}
@Post('chat/messages') chat() {
return this.deny();
}
@Post('tasks/:id/commands/:command') command() {
return this.deny();
}
@Post('tasks/:id/budget/reconcile') reconcile() {
return this.deny();
}
private deny(): never {
throw new ForbiddenException(
'Orchestration mutations and chat disabled pending hardened core and trusted runtime integration',
);
}
}
@@ -0,0 +1,47 @@
import {
BadRequestException,
ConflictException,
ForbiddenException,
} from '@nestjs/common';
import {
assertTransition,
nextStateForGateDecision,
OperationMode,
WorkflowState,
} from './orchestration.domain';
describe('orchestration domain', () => {
it('allows only declared workflow transitions', () => {
expect(() =>
assertTransition(WorkflowState.DRAFT, WorkflowState.GATE1_PENDING),
).not.toThrow();
expect(() =>
assertTransition(WorkflowState.DRAFT, WorkflowState.RUNNING),
).toThrow(BadRequestException);
});
it('keeps gate 1 as execution authorization rather than release authorization', () => {
expect(nextStateForGateDecision(1, 'APPROVED')).toBe(
WorkflowState.GATE1_APPROVED,
);
expect(nextStateForGateDecision(2, 'APPROVED')).toBe(
WorkflowState.GATE2_APPROVED,
);
});
it('rejects an unsupported gate decision', () => {
expect(() => nextStateForGateDecision(1, 'PENDING' as never)).toThrow(
BadRequestException,
);
});
it.each([WorkflowState.GATE1_PENDING, WorkflowState.GATE2_PENDING])('does not revive an expired proposal into %s', (state) => {
expect(() => assertTransition(WorkflowState.EXPIRED,state)).toThrow(BadRequestException);
});
it('classifies production mutation separately from observe and prepare', () => {
expect(OperationMode.OBSERVE).toBe('OBSERVE');
expect(OperationMode.PREPARE).toBe('PREPARE');
expect(OperationMode.MUTATE_PRODUCTION).toBe('MUTATE_PRODUCTION');
});
});
@@ -0,0 +1,109 @@
import { BadRequestException } from '@nestjs/common';
export enum WorkflowState {
DRAFT = 'DRAFT',
GATE1_PENDING = 'GATE1_PENDING',
GATE1_HELD = 'GATE1_HELD',
GATE1_APPROVED = 'GATE1_APPROVED',
READY = 'READY',
CLAIMED = 'CLAIMED',
RUNNING = 'RUNNING',
VERIFYING = 'VERIFYING',
GATE2_PENDING = 'GATE2_PENDING',
GATE2_APPROVED = 'GATE2_APPROVED',
RELEASING = 'RELEASING',
PRODUCTION_VERIFYING = 'PRODUCTION_VERIFYING',
LIVE_VERIFIED = 'LIVE_VERIFIED',
MEASURING = 'MEASURING',
CLOSED = 'CLOSED',
REJECTED = 'REJECTED',
BLOCKED = 'BLOCKED',
CANCELLED = 'CANCELLED',
FAILED = 'FAILED',
EXPIRED = 'EXPIRED',
}
export enum OperationMode {
OBSERVE = 'OBSERVE',
PREPARE = 'PREPARE',
MUTATE_PRODUCTION = 'MUTATE_PRODUCTION',
}
export enum RetryClass {
TRANSIENT = 'TRANSIENT',
RATE_LIMITED = 'RATE_LIMITED',
DEPENDENCY = 'DEPENDENCY',
VALIDATION = 'VALIDATION',
POLICY = 'POLICY',
PERMANENT = 'PERMANENT',
}
export enum KillSwitchLevel {
FREEZE_NEW_WORK = 'FREEZE_NEW_WORK',
STOP_AGENTS = 'STOP_AGENTS',
DISABLE_MUTATIONS = 'DISABLE_MUTATIONS',
EMERGENCY_HALT = 'EMERGENCY_HALT',
}
const transitions: Record<WorkflowState, WorkflowState[]> = {
DRAFT: [WorkflowState.GATE1_PENDING, WorkflowState.CANCELLED],
GATE1_HELD: [WorkflowState.GATE1_APPROVED, WorkflowState.REJECTED],
GATE1_PENDING: [
WorkflowState.GATE1_HELD,
WorkflowState.GATE1_APPROVED,
WorkflowState.REJECTED,
WorkflowState.EXPIRED,
],
GATE1_APPROVED: [WorkflowState.READY, WorkflowState.CANCELLED],
READY: [
WorkflowState.CLAIMED,
WorkflowState.BLOCKED,
WorkflowState.CANCELLED,
],
CLAIMED: [
WorkflowState.RUNNING,
WorkflowState.READY,
WorkflowState.CANCELLED,
],
RUNNING: [
WorkflowState.VERIFYING,
WorkflowState.FAILED,
WorkflowState.BLOCKED,
],
VERIFYING: [
WorkflowState.GATE2_PENDING,
WorkflowState.FAILED,
WorkflowState.RUNNING,
],
GATE2_PENDING: [
WorkflowState.GATE2_APPROVED,
WorkflowState.REJECTED,
WorkflowState.EXPIRED,
],
GATE2_APPROVED: [WorkflowState.RELEASING, WorkflowState.CANCELLED],
RELEASING: [WorkflowState.PRODUCTION_VERIFYING, WorkflowState.FAILED],
PRODUCTION_VERIFYING: [WorkflowState.LIVE_VERIFIED, WorkflowState.FAILED],
LIVE_VERIFIED: [WorkflowState.MEASURING, WorkflowState.CLOSED],
MEASURING: [WorkflowState.CLOSED, WorkflowState.FAILED],
CLOSED: [],
REJECTED: [],
BLOCKED: [WorkflowState.READY, WorkflowState.CANCELLED],
CANCELLED: [],
FAILED: [WorkflowState.READY, WorkflowState.CANCELLED],
EXPIRED: [WorkflowState.CANCELLED],
};
export function assertTransition(from: WorkflowState, to: WorkflowState): void {
if (!transitions[from]?.includes(to))
throw new BadRequestException(
`Invalid orchestration transition ${from} -> ${to}`,
);
}
export function nextStateForGateDecision(
gate: 1 | 2,
decision: 'APPROVED' | 'REJECTED',
): WorkflowState {
if (!(['APPROVED', 'REJECTED'] as string[]).includes(decision))
throw new BadRequestException('Decision must be APPROVED or REJECTED');
return decision === 'REJECTED'
? WorkflowState.REJECTED
: gate === 1
? WorkflowState.GATE1_APPROVED
: WorkflowState.GATE2_APPROVED;
}
@@ -0,0 +1,577 @@
import { OrchestrationController } from './orchestration.controller';
import { DepartmentTasksController } from '../admin/department-tasks.controller';
import { ApprovalsController } from '../admin/approvals.controller';
import { PrismaClient } from '@prisma/client';
import { OrchestrationService } from './orchestration.service';
import { KillSwitchService } from './kill-switch.service';
import { WorkflowState, OperationMode } from './orchestration.domain';
const enabled = process.env.ORCHESTRATION_INTEGRATION === '1';
(enabled ? describe : describe.skip)(
'v4 disposable PostgreSQL invariants',
() => {
const db = new PrismaClient();
const service = new OrchestrationService(
db as never,
new KillSwitchService(db as never),
);
let n = 0;
async function task(state = 'DRAFT', budget = 100) {
return db.orchestrationTask.create({
data: {
key: `test-${Date.now()}-${n++}`,
title: 'test',
department: 'TEST',
operationMode: 'PREPARE',
state,
budgetLimitCents: budget,
},
});
}
afterAll(async () => db.$disconnect());
it('release rejects PREPARE operation class even with gate approval', async()=>{
const t=await task('GATE2_APPROVED');
await expect(service.command(t.id,'RELEASE_AUTHORIZE',{expectedVersion:1,idempotencyKey:`mode-${t.id}`},'ci')).rejects.toThrow(/operation mode/i);
});
it('append-only ledger rejects direct rewrites in PostgreSQL', async () => {
const t = await task();
const r = await service.recordBudget(
t.id,
{
amountCents: 50,
kind: 'RESERVE',
idempotencyKey: `immutable-${t.id}`,
},
'a',
);
await expect(
db.orchestrationBudgetLedger.update({
where: { id: r.id },
data: { amountCents: 0 },
}),
).rejects.toThrow(/append.only/i);
await expect(
db.orchestrationBudgetLedger.delete({ where: { id: r.id } }),
).rejects.toThrow(/append.only/i);
});
it('generic transitions cannot fake run lifecycle or production verification', async () => {
for (const [from, to] of [
['CLAIMED', 'RUNNING'],
['RUNNING', 'VERIFYING'],
['FAILED', 'READY'],
['PRODUCTION_VERIFYING', 'LIVE_VERIFIED'],
]) {
const t = await task(from);
await expect(
service.transition(
t.id,
{
expectedVersion: 1,
idempotencyKey: `fake-${t.id}`,
toState: to as WorkflowState,
},
'agent',
),
).rejects.toThrow(/dedicated/i);
}
});
it('exposes command and reconcile HTTP handlers', () => {
const c = new OrchestrationController(service);
expect(typeof c.command).toBe('function');
expect(typeof c.reconcile).toBe('function');
});
it('highest kill level blocks reads and reconciliation', async () => {
const t = await task();
const r = await service.recordBudget(
t.id,
{
kind: 'RESERVE',
amountCents: 10,
idempotencyKey: `stop-res-${t.id}`,
},
'a',
);
await service.setKillSwitch(
['EMERGENCY', 'HALT'].join('_'),
'test',
'ceo',
);
try {
await expect(service.findOne(t.id)).rejects.toThrow(/kill switch/i);
await expect(
service.reconcileBudget(
t.id,
{
reservationId: r.id,
actualCents: 0,
idempotencyKey: `stop-recon-${t.id}`,
},
'a',
),
).rejects.toThrow(/kill switch/i);
} finally {
await db.orchestrationKillSwitch.deleteMany();
}
});
it('legacy arbitrary status and approval endpoints fail closed', async () => {
await expect(
new DepartmentTasksController(db as never).create({
title: 'bypass',
status: 'DONE',
}),
).rejects.toThrow(/v4/i);
await expect(
new DepartmentTasksController(db as never).updateStatus('none', {
status: 'DONE',
}),
).rejects.toThrow(/v4/i);
await expect(
new ApprovalsController(db as never).decide('none', {
decision: 'APPROVED',
} as any),
).rejects.toThrow(/v4/i);
});
it('release consumes only matching fresh production Gate2 via dedicated command', async () => {
const t = await task('GATE2_PENDING');
const hash = 'f'.repeat(64),
commitSha = 'a'.repeat(40);
const r = await db.orchestrationRun.create({
data: {
taskId: t.id,
status: 'SUCCEEDED',
commitSha,
environment: 'production',
idempotencyKey: `rel-run-${t.id}`,
},
});
await db.orchestrationArtifact.create({
data: {
runId: r.id,
kind: 'BUILD',
uri: 'https://ci.example/build',
sha256: hash,
},
});
await db.orchestrationAttestation.create({
data: {
runId: r.id,
kind: 'TEST',
subjectHash: hash,
signer: 'ci',
predicate: { passed: true },
},
});
const decision = await service.decideGate(
t.id,
2,
{
decision: 'APPROVED',
expectedVersion: 1,
idempotencyKey: `rel-approve-${t.id}`,
expiresAt: new Date(Date.now() + 60000).toISOString(),
runId: r.id,
artifactHash: hash,
commitSha,
environment: 'production',
},
'ceo',
);
const dto = {
expectedVersion: 2,
idempotencyKey: `rel-${t.id}`,
approvalId: decision.id,
runId: r.id,
artifactHash: hash,
commitSha,
environment: 'production',
};
await expect(
(service as any).command(
t.id,
'RELEASE_AUTHORIZE',
{ ...dto, artifactHash: '0'.repeat(64) },
'ci',
),
).rejects.toThrow(/binding/i);
const result = await (service as any).command(
t.id,
'RELEASE_AUTHORIZE',
dto,
'ci',
);
expect(result.authorized).toBe(true);
expect(
(await db.orchestrationTask.findUniqueOrThrow({ where: { id: t.id } }))
.state,
).toBe('RELEASING');
await expect(
(service as any).command(
t.id,
'RELEASE_AUTHORIZE',
{ ...dto, expectedVersion: 3, idempotencyKey: `rel-again-${t.id}` },
'ci',
),
).rejects.toThrow();
});
it('gate decision replay rejects changed immutable expiry and actor', async () => {
const t = await task('GATE1_PENDING');
const dto = {
decision: 'APPROVED' as const,
expectedVersion: 1,
idempotencyKey: `bind-${t.id}`,
expiresAt: new Date(Date.now() + 60000).toISOString(),
};
await service.decideGate(t.id, 1, dto, 'ceo');
await expect(
service.decideGate(
t.id,
1,
{ ...dto, expiresAt: new Date(Date.now() + 120000).toISOString() },
'ceo',
),
).rejects.toThrow(/Idempotency/i);
await expect(service.decideGate(t.id, 1, dto, 'other')).rejects.toThrow(
/Idempotency/i,
);
});
it('dependencies block admission and reject cycles', async () => {
const a = await task();
const b = await task();
await (service as any).command(
a.id,
'DEPENDENCY_ADD',
{
expectedVersion: 1,
idempotencyKey: `dep-a-${a.id}`,
dependsOnTaskId: b.id,
},
'actor',
);
await expect(
(service as any).command(
b.id,
'DEPENDENCY_ADD',
{
expectedVersion: 1,
idempotencyKey: `dep-b-${b.id}`,
dependsOnTaskId: a.id,
},
'actor',
),
).rejects.toThrow(/cycle/i);
await db.orchestrationTask.update({
where: { id: a.id },
data: { state: 'GATE1_PENDING' },
});
await service.decideGate(
a.id,
1,
{
decision: 'APPROVED',
expectedVersion: 2,
idempotencyKey: `dep-approval-${a.id}`,
expiresAt: new Date(Date.now() + 60000).toISOString(),
},
'ceo',
);
await expect(
service.transition(
a.id,
{
toState: WorkflowState.READY,
expectedVersion: 3,
idempotencyKey: `dep-ready-${a.id}`,
},
'agent',
),
).rejects.toThrow(/dependency/i);
});
it('registers artifact and attestation for owned run, blocks unregistered subjects', async () => {
const t = await task('CLAIMED');
const base = {
expectedVersion: 1,
idempotencyKey: `life-${t.id}`,
commitSha: 'c'.repeat(40),
environment: 'staging',
};
const r = await (service as any).command(
t.id,
'RUN_CREATE',
base,
'agent',
);
const hash = 'd'.repeat(64);
const a = await (service as any).command(
t.id,
'ARTIFACT_ADD',
{
expectedVersion: 2,
idempotencyKey: `art-${t.id}`,
runId: r.id,
sha256: hash,
kind: 'BUILD',
uri: 'https://ci.example/artifact',
},
'agent',
);
expect(a.sha256).toBe(hash);
await expect(
(service as any).command(
t.id,
'ATTESTATION_ADD',
{
expectedVersion: 3,
idempotencyKey: `bad-att-${t.id}`,
runId: r.id,
subjectHash: 'e'.repeat(64),
kind: 'TEST',
predicate: { passed: true },
},
'ci',
),
).rejects.toThrow(/artifact/i);
const att = await (service as any).command(
t.id,
'ATTESTATION_ADD',
{
expectedVersion: 3,
idempotencyKey: `att-${t.id}`,
runId: r.id,
subjectHash: hash,
kind: 'TEST',
predicate: { passed: true },
},
'ci',
);
expect(att.signer).toBe('ci');
});
it('creates immutable versioned runs with bounded transient retries', async () => {
const t = await task('CLAIMED');
const dto = {
expectedVersion: 1,
idempotencyKey: `run-${t.id}`,
commitSha: 'a'.repeat(40),
environment: 'staging',
};
const r = await (service as any).command(
t.id,
'RUN_CREATE',
dto,
'agent',
);
expect(r.attempt).toBe(1);
expect(
await (service as any).command(t.id, 'RUN_CREATE', dto, 'agent'),
).toEqual(r);
await expect(
(service as any).command(
t.id,
'RUN_CREATE',
{ ...dto, commitSha: 'b'.repeat(40) },
'agent',
),
).rejects.toThrow(/Idempotency/i);
await (service as any).command(
t.id,
'RUN_FINISH',
{
expectedVersion: 2,
idempotencyKey: `finish-${t.id}`,
runId: r.id,
status: 'FAILED',
retryClass: 'POLICY',
},
'agent',
);
await expect(
(service as any).command(
t.id,
'RUN_CREATE',
{ ...dto, expectedVersion: 3, idempotencyKey: `retry-${t.id}` },
'agent',
),
).rejects.toThrow(/retry/i);
});
it('selects tier at admission using backend risk and budget', async () => {
const t = await task('GATE1_PENDING', 1000);
await service.decideGate(
t.id,
1,
{
decision: 'APPROVED',
expectedVersion: 1,
idempotencyKey: `approve-tier-${t.id}`,
expiresAt: new Date(Date.now() + 60000).toISOString(),
},
'ceo',
);
await service.transition(
t.id,
{
toState: WorkflowState.READY,
expectedVersion: 2,
idempotencyKey: `admit-tier-${t.id}`,
},
'agent',
);
expect(
(await db.orchestrationTask.findUniqueOrThrow({ where: { id: t.id } }))
.modelTier,
).toBe('L0');
});
it('STOP_AGENTS prevents claims and running transitions', async () => {
await service.setKillSwitch('STOP_AGENTS', 'test', 'ceo');
try {
const t = await task('READY');
await expect(
service.transition(
t.id,
{
toState: WorkflowState.CLAIMED,
expectedVersion: 1,
idempotencyKey: `kill-${t.id}`,
},
'agent',
),
).rejects.toThrow(/kill switch/i);
} finally {
await db.orchestrationKillSwitch.deleteMany();
}
});
it('Gate1 admission consumes only unexpired approval', async () => {
const t = await task('GATE1_PENDING');
await service.decideGate(
t.id,
1,
{
decision: 'APPROVED',
expectedVersion: 1,
idempotencyKey: `approve-${t.id}`,
expiresAt: new Date(Date.now() + 100).toISOString(),
},
'ceo',
);
await new Promise((r) => setTimeout(r, 150));
await expect(
service.transition(
t.id,
{
toState: WorkflowState.READY,
expectedVersion: 2,
idempotencyKey: `admit-${t.id}`,
},
'agent',
),
).rejects.toThrow(/expired/i);
});
it('versioned transition replay returns original result and rejects changed payload', async () => {
const t = await task();
const dto = {
toState: WorkflowState.GATE1_PENDING,
expectedVersion: 1,
idempotencyKey: `tr-${t.id}`,
};
const first = await service.transition(t.id, dto, 'actor');
expect(await service.transition(t.id, dto, 'actor')).toEqual(first);
await expect(
service.transition(t.id, { ...dto, reason: 'different' }, 'actor'),
).rejects.toThrow(/Idempotency/i);
});
it('reconciles a reservation exactly once without erasing spent cost', async () => {
const t = await task();
const r = await service.recordBudget(
t.id,
{ amountCents: 80, kind: 'RESERVE', idempotencyKey: `res-${t.id}` },
'agent',
);
const dto = {
reservationId: r.id,
actualCents: 35,
idempotencyKey: `reconcile-${t.id}`,
};
await (service as any).reconcileBudget(t.id, dto, 'agent');
await (service as any).reconcileBudget(t.id, dto, 'agent');
await expect(
(service as any).reconcileBudget(
t.id,
{ ...dto, idempotencyKey: `second-${t.id}` },
'agent',
),
).rejects.toThrow(/reconciled/i);
expect(
(
await db.orchestrationBudgetLedger.aggregate({
where: { taskId: t.id },
_sum: { amountCents: true },
})
)._sum.amountCents,
).toBe(35);
await expect(
service.recordBudget(
t.id,
{
amountCents: 66,
kind: 'RESERVE',
idempotencyKey: `too-much-${t.id}`,
},
'agent',
),
).rejects.toThrow(/budget/i);
});
it('concurrent budget reservations never exceed hard cap and replay is harmless', async () => {
const t = await task();
const result = await Promise.allSettled(
Array.from({ length: 8 }, (_, i) =>
service.recordBudget(
t.id,
{
amountCents: 30,
kind: 'RESERVE',
idempotencyKey: `reserve-${t.id}-${i}`,
},
'agent',
),
),
);
expect(result.filter((x) => x.status === 'fulfilled')).toHaveLength(3);
const sum = await db.orchestrationBudgetLedger.aggregate({
where: { taskId: t.id },
_sum: { amountCents: true },
});
expect(sum._sum.amountCents).toBe(90);
const first = await db.orchestrationBudgetLedger.findFirstOrThrow({
where: { taskId: t.id },
});
await service.recordBudget(
t.id,
{
amountCents: 30,
kind: 'RESERVE',
idempotencyKey: first.idempotencyKey,
},
'agent',
);
expect(
await db.orchestrationBudgetLedger.count({ where: { taskId: t.id } }),
).toBe(3);
});
it('generic transition cannot approve gate 1', async () => {
const t = await task('GATE1_PENDING');
await expect(
service.transition(
t.id,
{
toState: WorkflowState.GATE1_APPROVED,
expectedVersion: 1,
idempotencyKey: `gate-bypass-${n}`,
},
'ceo',
),
).rejects.toThrow(/gate/i);
expect(
(await db.orchestrationTask.findUniqueOrThrow({ where: { id: t.id } }))
.state,
).toBe('GATE1_PENDING');
});
},
);
@@ -0,0 +1,31 @@
import {
PrepareWorkflowService,
PREPARE_WORKFLOW_CONFIG,
} from './prepare-workflow.service';
import { Module } from '@nestjs/common';
import { PrismaModule } from '../prisma/prisma.module';
import { KillSwitchService } from './kill-switch.service';
import { ModelTierPolicyService } from './model-tier-policy.service';
import { OrchestrationController } from './orchestration.controller';
import { OrchestrationService } from './orchestration.service';
@Module({
imports: [PrismaModule],
controllers: [OrchestrationController],
providers: [
OrchestrationService,
KillSwitchService,
ModelTierPolicyService,
PrepareWorkflowService,
{
provide: PREPARE_WORKFLOW_CONFIG,
useValue: { workerIds: [], stageCiIds: [] },
},
],
exports: [
OrchestrationService,
KillSwitchService,
ModelTierPolicyService,
PrepareWorkflowService,
],
})
export class OrchestrationModule {}
@@ -0,0 +1,260 @@
import {
BadRequestException,
ConflictException,
ForbiddenException,
} from '@nestjs/common';
import { OrchestrationService } from './orchestration.service';
function mockPrisma() {
const tx: any = {
$queryRaw: jest.fn().mockResolvedValue([]),
orchestrationTask: {
findUnique: jest.fn(),
create: jest.fn(),
updateMany: jest.fn(),
},
orchestrationRun: { findUnique: jest.fn(), create: jest.fn() },
orchestrationArtifact: { findFirst: jest.fn(), create: jest.fn() },
orchestrationAttestation: { create: jest.fn() },
orchestrationApprovalDecision: { findUnique: jest.fn(), create: jest.fn() },
orchestrationEvent: { findUnique: jest.fn(), create: jest.fn() },
orchestrationBudgetLedger: {
findUnique: jest.fn(),
aggregate: jest.fn(),
create: jest.fn(),
},
orchestrationKillSwitch: { findUnique: jest.fn(), upsert: jest.fn() },
};
tx.$transaction = jest.fn((cb: any) => Promise.resolve(cb(tx)));
return tx;
}
describe('OrchestrationService', () => {
let prisma: any;
let service: OrchestrationService;
beforeEach(() => {
prisma = mockPrisma();
service = new OrchestrationService(prisma, {
assertAllowed: jest.fn(),
} as never);
});
it('rejects an unknown kill level before overwriting persisted protection', async () => {
await expect(service.setKillSwitch('INVALID_LEVEL','test','admin')).rejects.toThrow(BadRequestException);
expect(prisma.orchestrationKillSwitch.upsert).not.toHaveBeenCalled();
});
it('creates a task and append-only event transactionally', async () => {
prisma.orchestrationTask.create.mockResolvedValue({
id: 'task-1',
state: 'DRAFT',
version: 1,
});
const result = await service.createTask(
{
key: 'seo-1',
title: 'SEO',
department: 'SEO',
operationMode: 'PREPARE' as never,
budgetLimitCents: 1000,
},
'actor-1',
);
expect(prisma.$transaction).toHaveBeenCalledTimes(1);
expect(prisma.orchestrationTask.create).toHaveBeenCalledWith({
data: expect.objectContaining({ state: 'DRAFT', version: 1 }),
});
expect(prisma.orchestrationEvent.create).toHaveBeenCalledWith({
data: expect.objectContaining({ eventType: 'TASK_CREATED', sequence: 1 }),
});
expect(result.id).toBe('task-1');
});
it('uses compare-and-swap versioning for transitions', async () => {
prisma.orchestrationTask.findUnique.mockResolvedValue({
id: 'task-1',
state: 'DRAFT',
version: 3,
});
prisma.orchestrationTask.updateMany.mockResolvedValue({ count: 0 });
await expect(
service.transition(
'task-1',
{
toState: 'GATE1_PENDING' as never,
expectedVersion: 3,
idempotencyKey: 'i-1',
},
'actor',
),
).rejects.toThrow(ConflictException);
});
it('makes gate decisions immutable and idempotent', async () => {
const expiresAt = new Date(Date.now() + 10000).toISOString();
prisma.orchestrationApprovalDecision.findUnique.mockResolvedValue({
id: 'd1',
decision: 'APPROVED',
gate: 1,
taskId: 'task-1',
});
prisma.orchestrationEvent.findUnique.mockResolvedValue({
payload: {
fingerprint: JSON.stringify({
taskId: 'task-1',
gate: 1,
dto: {
decision: 'APPROVED',
expectedVersion: 2,
idempotencyKey: 'gate-1',
expiresAt,
},
actorId: 'ceo',
}),
},
});
await expect(
service.decideGate(
'task-1',
1,
{
decision: 'APPROVED',
expectedVersion: 2,
idempotencyKey: 'gate-1',
expiresAt,
},
'ceo',
),
).resolves.toMatchObject({ id: 'd1' });
expect(prisma.orchestrationApprovalDecision.create).not.toHaveBeenCalled();
});
it('rejects an expired approval', async () => {
await expect(
service.decideGate(
'task-1',
1,
{
decision: 'APPROVED',
expectedVersion: 2,
idempotencyKey: 'gate-expired',
expiresAt: new Date(0).toISOString(),
},
'ceo',
),
).rejects.toThrow(BadRequestException);
});
it('binds gate 2 approval to run, artifact hash, commit and environment', async () => {
prisma.orchestrationApprovalDecision.findUnique.mockResolvedValue(null);
prisma.orchestrationTask.findUnique.mockResolvedValue({
id: 'task-1',
state: 'GATE2_PENDING',
version: 7,
});
prisma.orchestrationRun.findUnique.mockResolvedValue({
id: 'run-1',
taskId: 'task-1',
commitSha: 'abc123',
environment: 'staging',
status: 'SUCCEEDED',
});
prisma.orchestrationArtifact.findFirst.mockResolvedValue({
id: 'artifact-1',
runId: 'run-1',
sha256: 'f'.repeat(64),
});
prisma.orchestrationTask.updateMany.mockResolvedValue({ count: 1 });
prisma.orchestrationApprovalDecision.create.mockResolvedValue({
id: 'decision-2',
gate: 2,
});
await service.decideGate(
'task-1',
2,
{
decision: 'APPROVED',
expectedVersion: 7,
idempotencyKey: 'gate-2',
expiresAt: new Date(Date.now() + 10000).toISOString(),
runId: 'run-1',
artifactHash: 'f'.repeat(64),
commitSha: 'abc123',
environment: 'staging',
},
'ceo',
);
expect(prisma.orchestrationApprovalDecision.create).toHaveBeenCalledWith({
data: expect.objectContaining({
runId: 'run-1',
artifactHash: 'f'.repeat(64),
commitSha: 'abc123',
environment: 'staging',
}),
});
});
it.each(['RUNNING', 'FAILED', 'CANCELLED'])('refuses Gate 2 approval for an unfinished or unsuccessful %s run', async (status) => {
prisma.orchestrationTask.findUnique.mockResolvedValue({id:'task-1',state:'GATE2_PENDING',version:7});
prisma.orchestrationRun.findUnique.mockResolvedValue({id:'run-1',taskId:'task-1',commitSha:'abc123',environment:'staging',status});
prisma.orchestrationArtifact.findFirst.mockResolvedValue({id:'artifact-1'});
prisma.orchestrationTask.updateMany.mockResolvedValue({count:1});
await expect(service.decideGate('task-1',2,{
decision:'APPROVED',expectedVersion:7,idempotencyKey:'failed-run',
expiresAt:new Date(Date.now()+10000).toISOString(),runId:'run-1',
artifactHash:'f'.repeat(64),commitSha:'abc123',environment:'staging',
},'ceo')).rejects.toThrow(/successful run/i);
expect(prisma.orchestrationApprovalDecision.create).not.toHaveBeenCalled();
expect(prisma.orchestrationTask.updateMany).not.toHaveBeenCalled();
});
it('refuses gate 2 when binding does not match the immutable run', async () => {
prisma.orchestrationApprovalDecision.findUnique.mockResolvedValue(null);
prisma.orchestrationTask.findUnique.mockResolvedValue({
id: 'task-1',
state: 'GATE2_PENDING',
version: 7,
});
prisma.orchestrationRun.findUnique.mockResolvedValue({
id: 'run-1',
taskId: 'task-1',
commitSha: 'real',
environment: 'staging',
});
await expect(
service.decideGate(
'task-1',
2,
{
decision: 'APPROVED',
expectedVersion: 7,
idempotencyKey: 'gate-bad',
expiresAt: new Date(Date.now() + 10000).toISOString(),
runId: 'run-1',
artifactHash: 'f'.repeat(64),
commitSha: 'wrong',
environment: 'staging',
},
'ceo',
),
).rejects.toThrow(ForbiddenException);
});
it('enforces hard task budget atomically', async () => {
prisma.orchestrationTask.findUnique.mockResolvedValue({
id: 'task-1',
budgetLimitCents: 1000,
});
prisma.orchestrationBudgetLedger.aggregate.mockResolvedValue({
_sum: { amountCents: 900 },
});
await expect(
service.recordBudget(
'task-1',
{ amountCents: 101, idempotencyKey: 'cost-1', kind: 'SPEND' },
'agent',
),
).rejects.toThrow(ForbiddenException);
expect(prisma.orchestrationBudgetLedger.create).not.toHaveBeenCalled();
});
});
@@ -0,0 +1,665 @@
import {
BadRequestException,
ConflictException,
ForbiddenException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import {
CreateOrchestrationTaskDto,
GateDecisionDto,
RecordBudgetDto,
TransitionTaskDto,
} from './dto/orchestration.dto';
import {
KillSwitchLevel,
assertTransition,
nextStateForGateDecision,
OperationMode,
WorkflowState,
} from './orchestration.domain';
import { ModelTierPolicyService } from './model-tier-policy.service';
import { KillSwitchService } from './kill-switch.service';
@Injectable()
export class OrchestrationService {
constructor(
private readonly prisma: PrismaService,
private readonly killSwitch: KillSwitchService,
) {}
async createTask(dto: CreateOrchestrationTaskDto, actorId: string) {
await this.killSwitch.assertAllowed('CREATE_TASK');
return await (this.prisma as any).$transaction(async (tx: any) => {
const task = await tx.orchestrationTask.create({
data: {
key: dto.key,
title: dto.title,
department: dto.department,
operationMode: dto.operationMode,
budgetLimitCents: dto.budgetLimitCents,
metadata: dto.metadata ?? {},
state: WorkflowState.DRAFT,
version: 1,
},
});
await tx.orchestrationEvent.create({
data: {
taskId: task.id,
eventType: 'TASK_CREATED',
fromState: null,
toState: WorkflowState.DRAFT,
sequence: 1,
actorId,
payload: { key: dto.key },
},
});
return task;
});
}
async transition(id: string, dto: TransitionTaskDto, actorId: string) {
if (
[
WorkflowState.GATE1_APPROVED,
WorkflowState.GATE2_APPROVED,
WorkflowState.RELEASING,
WorkflowState.RUNNING,
WorkflowState.VERIFYING,
WorkflowState.PRODUCTION_VERIFYING,
WorkflowState.LIVE_VERIFIED,
WorkflowState.MEASURING,
WorkflowState.CLOSED,
].includes(dto.toState)
)
throw new ForbiddenException(
'Gate approvals and release require dedicated commands',
);
return await (this.prisma as any).$transaction(async (tx: any) => {
await tx.$queryRaw`SELECT id FROM orchestration_tasks WHERE id = ${id}::uuid FOR UPDATE`;
const fingerprint = JSON.stringify({ id, dto, actorId });
const replay = await tx.orchestrationEvent.findUnique({
where: { idempotencyKey: dto.idempotencyKey },
});
if (replay) {
if (replay.payload.fingerprint !== fingerprint)
throw new ConflictException('Idempotency payload mismatch');
return replay.payload.result;
}
const existing = await tx.orchestrationTask.findUnique({ where: { id } });
if (!existing)
throw new NotFoundException(`Orchestration task ${id} not found`);
if (existing.state === WorkflowState.FAILED)
throw new ForbiddenException('Retries require dedicated run command');
assertTransition(existing.state, dto.toState);
await this.killSwitch.assertAllowed(
[
WorkflowState.READY,
WorkflowState.CLAIMED,
WorkflowState.RUNNING,
WorkflowState.VERIFYING,
WorkflowState.GATE2_PENDING,
].includes(dto.toState)
? 'RUN_AGENT'
: 'OBSERVE',
);
if ([WorkflowState.READY, WorkflowState.CLAIMED].includes(dto.toState))
await this.killSwitch.assertAllowed('CLAIM_TASK');
let selectedTier = existing.modelTier;
if (dto.toState === WorkflowState.READY) {
const dependencies = await tx.orchestrationDependency.findMany({
where: { taskId: id },
include: { dependsOnTask: true },
});
if (dependencies.some((d: any) => d.dependsOnTask.state !== 'CLOSED'))
throw new ForbiddenException('Unresolved dependency');
const costs = await tx.orchestrationBudgetLedger.aggregate({
where: { taskId: id },
_sum: { amountCents: true },
});
selectedTier = new ModelTierPolicyService().select({
complexity: Number(existing.metadata?.complexity ?? 0),
risk: Number(existing.metadata?.risk ?? 0),
operationMode: existing.operationMode,
remainingBudgetCents:
existing.budgetLimitCents - (costs._sum.amountCents ?? 0),
});
const approval = await tx.orchestrationApprovalDecision.findFirst({
where: { taskId: id, gate: 1, decision: 'APPROVED' },
orderBy: { createdAt: 'desc' },
});
if (!approval || approval.expiresAt <= new Date())
throw new ForbiddenException('Gate 1 approval missing or expired');
}
if (
existing.operationMode === OperationMode.MUTATE_PRODUCTION &&
dto.toState === WorkflowState.RELEASING
)
await this.killSwitch.assertAllowed('MUTATE_PRODUCTION');
const changed = await tx.orchestrationTask.updateMany({
where: { id, version: dto.expectedVersion, state: existing.state },
data: {
state: dto.toState,
modelTier: selectedTier,
version: { increment: 1 },
},
});
if (changed.count !== 1)
throw new ConflictException(
'Task version or state changed; reload before retrying',
);
await tx.orchestrationEvent.create({
data: {
taskId: id,
eventType: 'STATE_TRANSITION',
fromState: existing.state,
toState: dto.toState,
sequence: dto.expectedVersion + 1,
actorId,
idempotencyKey: dto.idempotencyKey,
payload: {
fingerprint,
result: JSON.parse(
JSON.stringify({
...existing,
state: dto.toState,
modelTier: selectedTier,
version: dto.expectedVersion + 1,
}),
),
},
},
});
return JSON.parse(
JSON.stringify({
...existing,
state: dto.toState,
modelTier: selectedTier,
version: dto.expectedVersion + 1,
}),
);
});
}
async decideGate(
taskId: string,
gate: 1 | 2,
dto: GateDecisionDto,
actorId: string,
) {
if (![1, 2].includes(gate)) throw new BadRequestException('Invalid gate');
return await (this.prisma as any).$transaction(async (tx: any) => {
await tx.$queryRaw`SELECT id FROM orchestration_tasks WHERE id = ${taskId}::uuid FOR UPDATE`;
const fingerprint = JSON.stringify({ taskId, gate, dto, actorId });
const prior = await tx.orchestrationApprovalDecision.findUnique({
where: { idempotencyKey: dto.idempotencyKey },
});
if (prior) {
const event = await tx.orchestrationEvent.findUnique({
where: { idempotencyKey: `event:${dto.idempotencyKey}` },
});
if (event?.payload?.fingerprint !== fingerprint)
throw new ConflictException('Idempotency payload mismatch');
return prior;
}
if (
!Number.isFinite(Date.parse(dto.expiresAt)) ||
new Date(dto.expiresAt) <= new Date()
)
throw new BadRequestException('Approval decision has expired');
await this.killSwitch.assertAllowed('OBSERVE');
const task = await tx.orchestrationTask.findUnique({
where: { id: taskId },
});
if (!task)
throw new NotFoundException(`Orchestration task ${taskId} not found`);
const expected =
gate === 1 ? WorkflowState.GATE1_PENDING : WorkflowState.GATE2_PENDING;
if (task.state !== expected)
throw new ConflictException(`Gate ${gate} requires ${expected}`);
if (gate === 2 && dto.decision === 'APPROVED') {
if (
!dto.runId ||
!dto.artifactHash ||
!dto.commitSha ||
!dto.environment
)
throw new BadRequestException(
'Gate 2 requires runId, artifactHash, commitSha and environment',
);
const run = await tx.orchestrationRun.findUnique({
where: { id: dto.runId },
});
if (
!run ||
run.taskId !== taskId ||
run.commitSha !== dto.commitSha ||
run.environment !== dto.environment
)
throw new ForbiddenException(
'Gate 2 binding does not match immutable run',
);
if (run.status !== 'SUCCEEDED')
throw new ForbiddenException('Gate 2 requires a successful run');
const artifact = await tx.orchestrationArtifact.findFirst({
where: { runId: dto.runId, sha256: dto.artifactHash },
});
if (!artifact)
throw new ForbiddenException(
'Gate 2 artifact hash is not registered for run',
);
}
const next = nextStateForGateDecision(gate, dto.decision);
const changed = await tx.orchestrationTask.updateMany({
where: { id: taskId, version: dto.expectedVersion, state: expected },
data: { state: next, version: { increment: 1 } },
});
if (changed.count !== 1)
throw new ConflictException(
'Task version or state changed; approval not applied',
);
const decision = await tx.orchestrationApprovalDecision.create({
data: {
taskId,
gate,
decision: dto.decision,
actorId,
notes: dto.notes,
expiresAt: new Date(dto.expiresAt),
idempotencyKey: dto.idempotencyKey,
runId: dto.runId,
artifactHash: dto.artifactHash,
commitSha: dto.commitSha,
environment: dto.environment,
},
});
await tx.orchestrationEvent.create({
data: {
taskId,
eventType: `GATE${gate}_${dto.decision}`,
fromState: expected,
toState: next,
sequence: dto.expectedVersion + 1,
actorId,
idempotencyKey: `event:${dto.idempotencyKey}`,
payload: { decisionId: decision.id, fingerprint },
},
});
return decision;
});
}
async recordBudget(taskId: string, dto: RecordBudgetDto, actorId: string) {
if (
!Number.isSafeInteger(dto.amountCents) ||
dto.amountCents < 1 ||
!['RESERVE', 'SPEND'].includes(dto.kind)
)
throw new BadRequestException(
'Use RESERVE or SPEND with positive integer cents',
);
return await (this.prisma as any).$transaction(async (tx: any) => {
await tx.$queryRaw`SELECT id FROM orchestration_tasks WHERE id = ${taskId}::uuid FOR UPDATE`;
const prior = await tx.orchestrationBudgetLedger.findUnique({
where: { idempotencyKey: dto.idempotencyKey },
});
if (prior) {
if (
prior.taskId !== taskId ||
prior.kind !== dto.kind ||
prior.amountCents !== dto.amountCents ||
prior.actorId !== actorId ||
JSON.stringify(prior.metadata) !== JSON.stringify(dto.metadata ?? {})
)
throw new ConflictException('Idempotency payload mismatch');
return prior;
}
await this.killSwitch.assertAllowed('RUN_AGENT');
const task = await tx.orchestrationTask.findUnique({
where: { id: taskId },
});
if (!task)
throw new NotFoundException(`Orchestration task ${taskId} not found`);
const sum = await tx.orchestrationBudgetLedger.aggregate({
where: { taskId },
_sum: { amountCents: true },
});
if ((sum._sum.amountCents ?? 0) + dto.amountCents > task.budgetLimitCents)
throw new ForbiddenException('Hard task budget exceeded');
return tx.orchestrationBudgetLedger.create({
data: {
taskId,
amountCents: dto.amountCents,
kind: dto.kind,
idempotencyKey: dto.idempotencyKey,
actorId,
metadata: dto.metadata ?? {},
},
});
});
}
async command(taskId: string, command: string, dto: any, actorId: string) {
return await (this.prisma as any).$transaction(async (tx: any) => {
await tx.$queryRaw`SELECT id FROM orchestration_tasks WHERE id = ${taskId}::uuid FOR UPDATE`;
const fingerprint = JSON.stringify({ taskId, command, dto, actorId });
const replay = await tx.orchestrationEvent.findUnique({
where: { idempotencyKey: dto.idempotencyKey },
});
if (replay) {
if (replay.payload.fingerprint !== fingerprint)
throw new ConflictException('Idempotency payload mismatch');
return replay.payload.result;
}
const task = await tx.orchestrationTask.findUnique({
where: { id: taskId },
});
if (!task) throw new NotFoundException('Task not found');
if (task.version !== dto.expectedVersion)
throw new ConflictException('Stale task version');
await this.killSwitch.assertAllowed('RUN_AGENT');
let result: any;
let state = task.state;
if (command === 'RUN_CREATE') {
if (
!/^[a-f0-9]{40,64}$/.test(dto.commitSha) ||
!['development', 'staging', 'production'].includes(dto.environment)
)
throw new BadRequestException('Invalid immutable run binding');
const last = await tx.orchestrationRun.findFirst({
where: { taskId },
orderBy: { attempt: 'desc' },
});
if (
last &&
(last.status !== 'FAILED' ||
!['TRANSIENT', 'RATE_LIMITED', 'DEPENDENCY'].includes(
last.retryClass,
) ||
last.attempt >= 3)
)
throw new ForbiddenException('Retry denied or exhausted');
if (!last && task.state !== 'CLAIMED')
throw new ConflictException('Run requires CLAIMED task');
result = await tx.orchestrationRun.create({
data: {
taskId,
attempt: (last?.attempt ?? 0) + 1,
status: 'RUNNING',
commitSha: dto.commitSha,
environment: dto.environment,
idempotencyKey: dto.idempotencyKey,
startedAt: new Date(),
},
});
state = 'RUNNING';
} else if (command === 'RUN_FINISH') {
const run = await tx.orchestrationRun.findUnique({
where: { id: dto.runId },
});
if (
!run ||
run.taskId !== taskId ||
run.status !== 'RUNNING' ||
task.state !== 'RUNNING'
)
throw new ConflictException('Active run required');
if (
!['SUCCEEDED', 'FAILED'].includes(dto.status) ||
(dto.status === 'FAILED' &&
![
'TRANSIENT',
'RATE_LIMITED',
'DEPENDENCY',
'VALIDATION',
'POLICY',
'PERMANENT',
].includes(dto.retryClass))
)
throw new BadRequestException('Invalid outcome taxonomy');
result = await tx.orchestrationRun.update({
where: { id: run.id },
data: {
status: dto.status,
retryClass: dto.status === 'FAILED' ? dto.retryClass : null,
finishedAt: new Date(),
},
});
state = dto.status === 'FAILED' ? 'FAILED' : 'VERIFYING';
} else if (command === 'ARTIFACT_ADD' || command === 'ATTESTATION_ADD') {
const run = await tx.orchestrationRun.findUnique({
where: { id: dto.runId },
});
if (
!run ||
run.taskId !== taskId ||
!['RUNNING', 'VERIFYING'].includes(task.state)
)
throw new ConflictException('Owned active run required');
const hash = command === 'ARTIFACT_ADD' ? dto.sha256 : dto.subjectHash;
if (!/^[a-f0-9]{64}$/.test(hash) || !dto.kind)
throw new BadRequestException('Invalid artifact hash or kind');
if (command === 'ARTIFACT_ADD') {
if (!/^https:\/\//.test(dto.uri))
throw new BadRequestException('Artifact URI must be HTTPS');
result = await tx.orchestrationArtifact.create({
data: { runId: run.id, kind: dto.kind, sha256: hash, uri: dto.uri },
});
} else {
if (
!(await tx.orchestrationArtifact.findFirst({
where: { runId: run.id, sha256: hash },
}))
)
throw new ForbiddenException(
'Attestation subject is not a registered artifact',
);
result = await tx.orchestrationAttestation.create({
data: {
runId: run.id,
kind: dto.kind,
subjectHash: hash,
predicate: dto.predicate ?? {},
signer: actorId,
signature: dto.signature,
},
});
}
} else if (command === 'DEPENDENCY_ADD') {
if (task.state !== 'DRAFT')
throw new ConflictException(
'Dependencies are frozen after submission',
);
await tx.$executeRaw`LOCK TABLE orchestration_dependencies IN EXCLUSIVE MODE`;
const cycles =
await tx.$queryRaw`WITH RECURSIVE chain(id) AS (SELECT ${dto.dependsOnTaskId}::uuid UNION SELECT d.depends_on_task_id FROM orchestration_dependencies d JOIN chain c ON d.task_id=c.id) SELECT id FROM chain WHERE id=${taskId}::uuid`;
if (cycles.length) throw new ForbiddenException('Dependency cycle');
result = await tx.orchestrationDependency.create({
data: { taskId, dependsOnTaskId: dto.dependsOnTaskId },
});
} else if (command === 'RELEASE_AUTHORIZE') {
await this.killSwitch.assertAllowed('MUTATE_PRODUCTION');
if (task.state !== 'GATE2_APPROVED')
throw new ConflictException(
'Gate 2 approval required; already consumed or invalid state',
);
const approval = await tx.orchestrationApprovalDecision.findUnique({
where: { id: dto.approvalId },
});
if (
!approval ||
approval.taskId !== taskId ||
approval.gate !== 2 ||
approval.decision !== 'APPROVED' ||
approval.expiresAt <= new Date()
)
throw new ForbiddenException('Gate 2 missing or expired');
for (const key of ['runId', 'artifactHash', 'commitSha', 'environment'])
if (approval[key] !== dto[key])
throw new ForbiddenException('Release binding mismatch');
const run = await tx.orchestrationRun.findUnique({
where: { id: dto.runId },
});
if (
!run ||
run.taskId !== taskId ||
run.status !== 'SUCCEEDED' ||
run.commitSha !== dto.commitSha ||
run.environment !== dto.environment
)
throw new ForbiddenException('Run binding mismatch');
if (
!(await tx.orchestrationArtifact.findFirst({
where: { runId: run.id, sha256: dto.artifactHash },
})) ||
!(await tx.orchestrationAttestation.findFirst({
where: {
runId: run.id,
subjectHash: dto.artifactHash,
kind: 'TEST',
},
}))
)
throw new ForbiddenException('Artifact evidence missing');
result = {
authorized: true,
approvalId: approval.id,
runId: run.id,
artifactHash: dto.artifactHash,
commitSha: dto.commitSha,
environment: dto.environment,
execution: 'CI_CD_ONLY',
};
state = 'RELEASING';
} else throw new BadRequestException('Unknown command');
await tx.orchestrationTask.update({
where: { id: taskId },
data: { state, version: { increment: 1 } },
});
const serialized = JSON.parse(JSON.stringify(result));
await tx.orchestrationEvent.create({
data: {
taskId,
eventType: command,
fromState: task.state,
toState: state,
sequence: task.version + 1,
actorId,
idempotencyKey: dto.idempotencyKey,
payload: { fingerprint, result: serialized },
},
});
return serialized;
});
}
async reconcileBudget(
taskId: string,
dto: { reservationId: string; actualCents: number; idempotencyKey: string },
actorId: string,
) {
await this.killSwitch.assertAllowed('OBSERVE');
if (!Number.isSafeInteger(dto.actualCents) || dto.actualCents < 0)
throw new BadRequestException('Invalid actual cost');
return await (this.prisma as any).$transaction(async (tx: any) => {
await tx.$queryRaw`SELECT id FROM orchestration_tasks WHERE id = ${taskId}::uuid FOR UPDATE`;
const prior = await tx.orchestrationBudgetLedger.findUnique({
where: { idempotencyKey: dto.idempotencyKey },
});
if (prior) {
if (
prior.taskId !== taskId ||
prior.actorId !== actorId ||
prior.metadata.reservationId !== dto.reservationId ||
prior.metadata.actualCents !== dto.actualCents
)
throw new ConflictException('Idempotency payload mismatch');
return prior;
}
const reservation = await tx.orchestrationBudgetLedger.findUnique({
where: { id: dto.reservationId },
});
if (
!reservation ||
reservation.taskId !== taskId ||
reservation.kind !== 'RESERVE'
)
throw new BadRequestException('Reservation not found');
if (
await tx.orchestrationBudgetLedger.findFirst({
where: {
taskId,
kind: 'RECONCILE',
metadata: { path: ['reservationId'], equals: dto.reservationId },
},
})
)
throw new ConflictException('Reservation already reconciled');
const task = await tx.orchestrationTask.findUnique({
where: { id: taskId },
});
const total = await tx.orchestrationBudgetLedger.aggregate({
where: { taskId },
_sum: { amountCents: true },
});
const delta = dto.actualCents - reservation.amountCents;
if (total._sum.amountCents + delta > task.budgetLimitCents)
throw new ForbiddenException('Hard task budget exceeded');
return tx.orchestrationBudgetLedger.create({
data: {
taskId,
kind: 'RECONCILE',
amountCents: delta,
idempotencyKey: dto.idempotencyKey,
actorId,
metadata: {
reservationId: dto.reservationId,
actualCents: dto.actualCents,
},
},
});
});
}
async findAll() {
await this.killSwitch.assertAllowed('OBSERVE');
return (this.prisma as any).orchestrationTask.findMany({
orderBy: { createdAt: 'desc' },
});
}
async findOne(id: string) {
await this.killSwitch.assertAllowed('OBSERVE');
const task = await (this.prisma as any).orchestrationTask.findUnique({
where: { id },
include: {
runs: { include: { artifacts: true, attestations: true } },
dependencies: true,
decisions: true,
events: { orderBy: { sequence: 'asc' } },
budgetLedger: true,
},
});
if (!task)
throw new NotFoundException(`Orchestration task ${id} not found`);
return task;
}
async setKillSwitch(level: string, reason: string, actorId: string) {
if (!Object.values(KillSwitchLevel).includes(level as KillSwitchLevel))
throw new BadRequestException('Invalid kill switch level');
return await (this.prisma as any).$transaction(async (tx: any) => {
const value = await tx.orchestrationKillSwitch.upsert({
where: { scope: 'GLOBAL' },
create: { scope: 'GLOBAL', level, active: true, reason, actorId },
update: {
level,
active: true,
reason,
actorId,
version: { increment: 1 },
},
});
await tx.orchestrationEvent.create({
data: {
eventType: 'KILL_SWITCH_CHANGED',
sequence: value.version ?? 1,
actorId,
payload: { scope: 'GLOBAL', level, reason },
},
});
return value;
});
}
}
@@ -0,0 +1,48 @@
import { PrepareWorkflowService } from './prepare-workflow.service';
describe('owner Gate1 hold operation', () => {
const reviewer={id:'reviewer',role:'SUPER_ADMIN'};
let task:any, tx:any, service:PrepareWorkflowService;
beforeEach(()=>{
task={id:'t',state:'GATE1_PENDING',version:2,operationMode:'PREPARE',metadata:{workflowScope:'STAGE_PREPARE',createdBy:'proposer'}};
tx={$queryRaw:jest.fn().mockResolvedValue([]),orchestrationTask:{findUnique:jest.fn(async()=>task),update:jest.fn(async({data}:any)=>(task={...task,state:data.state,version:task.version+1}))},orchestrationEvent:{findUnique:jest.fn(),create:jest.fn()},orchestrationApprovalDecision:{create:jest.fn()}};
service=new PrepareWorkflowService({$transaction:async(cb:any)=>cb(tx)}, {workerIds:[],stageCiIds:[]});
});
const hold={toState:'GATE1_HELD',expectedVersion:2,idempotencyKey:'hold-t',reason:'Need revised budget'};
it('advertises hold only to independent owner reviewer',()=>{
expect(service.project(task,reviewer).availableActions).toContain('HOLD_GATE1');
expect(service.project(task,{id:'proposer',role:'SUPER_ADMIN'}).availableActions).not.toContain('HOLD_GATE1');
});
it('persists hold with actor, immutable reason and version without approval or queueing',async()=>{
await expect(service.transition('t',hold,reviewer)).resolves.toMatchObject({state:'GATE1_HELD',version:3});
expect(tx.orchestrationEvent.create).toHaveBeenCalledWith({data:expect.objectContaining({actorId:'reviewer',fromState:'GATE1_PENDING',toState:'GATE1_HELD',eventType:'PREPARE_GATE1_HELD',idempotencyKey:'hold-t',payload:expect.objectContaining({fingerprint:expect.stringContaining('Need revised budget')})})});
expect(tx.orchestrationApprovalDecision.create).not.toHaveBeenCalled();
expect(service.project(task,reviewer).availableActions).toEqual(['APPROVE_GATE1','REJECT_GATE1']);
});
it.each([{id:'proposer',role:'SUPER_ADMIN'},{id:'other',role:'Admin'}])('denies unauthorized hold %j',async actor=>{
await expect(service.transition('t',hold,actor)).rejects.toMatchObject({status:403});
expect(tx.orchestrationTask.update).not.toHaveBeenCalled();
});
it.each(['DRAFT','READY','GATE1_APPROVED','GATE2_PENDING'])('denies hold from %s',async state=>{
task.state=state;
await expect(service.transition('t',hold,reviewer)).rejects.toMatchObject({status:403});
expect(tx.orchestrationTask.update).not.toHaveBeenCalled();
});
it('rejects missing reason and stale version without writes',async()=>{
await expect(service.transition('t',{...hold,reason:' '},reviewer)).rejects.toMatchObject({status:400});
await expect(service.transition('t',{...hold,expectedVersion:1},reviewer)).rejects.toMatchObject({status:409});
expect(tx.orchestrationTask.update).not.toHaveBeenCalled();
});
it('replays identical hold, rejects changed payload',async()=>{
await service.transition('t',hold,reviewer);
const event=tx.orchestrationEvent.create.mock.calls[0][0].data;
tx.orchestrationEvent.findUnique.mockResolvedValue(event);
await service.transition('t',hold,reviewer);
expect(tx.orchestrationTask.update).toHaveBeenCalledTimes(1);
await expect(service.transition('t',{...hold,reason:'changed'},reviewer)).rejects.toMatchObject({status:409});
});
it.each(['APPROVED','REJECTED'])('decides held proposal via existing Gate1 command: %s',async decision=>{
task.state='GATE1_HELD';
await expect(service.decide('t',1,{decision,expectedVersion:2,idempotencyKey:'decision',expiresAt:new Date(Date.now()+60000).toISOString()},reviewer)).resolves.toMatchObject({state:decision==='APPROVED'?'GATE1_APPROVED':'REJECTED'});
});
});
@@ -0,0 +1,143 @@
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { OrchestrationController } from './orchestration.controller';
import { OrchestrationService } from './orchestration.service';
import { PrepareWorkflowService } from './prepare-workflow.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
describe('bounded workflow HTTP contracts', () => {
let app: INestApplication;
const t = {
id: 't',
key: 'k',
state: 'DRAFT',
version: 1,
metadata: {
scope: 'STAGE_PREPARE',
environment: 'staging',
commitSha: 'a'.repeat(40),
token: 'secret',
},
};
const workflow = {
create: jest.fn(async () => t),
transition: jest.fn(async () => t),
decide: jest.fn(async () => t),
project: jest.fn((row: any) => ({
...row,
metadata: {
scope: 'STAGE_PREPARE',
environment: 'staging',
commitSha: 'a'.repeat(40),
},
availableActions: ['SUBMIT_GATE1'],
})),
};
const core = {
findAll: async () => [t],
findOne: async () => t,
command: jest.fn(),
};
beforeAll(async () => {
const m = await Test.createTestingModule({
controllers: [OrchestrationController],
providers: [
{ provide: OrchestrationService, useValue: core },
{ provide: PrepareWorkflowService, useValue: workflow },
RolesGuard,
],
})
.overrideGuard(JwtAuthGuard)
.useValue({
canActivate(c: any) {
const req = c.switchToHttp().getRequest();
if (!req.headers.authorization) return false;
req.user = {
sub: 'verified-subject',
role: req.headers.authorization,
};
return true;
},
})
.compile();
app = m.createNestApplication();
await app.init();
});
afterAll(async () => {
await app.close();
});
it('accepts existing proposal DTO, derives actor from JWT sub and returns projected task', async () => {
const dto = {
key: 'k',
title: 'T',
department: 'ENGINEERING',
operationMode: 'PREPARE',
budgetLimitCents: 20,
metadata: {
commitSha: 'a'.repeat(40),
scope: 'Staging audit only',
acceptanceCriteria: ['QA passes'],
},
};
const r = await request(app.getHttpServer())
.post('/admin/orchestration/tasks')
.set('Authorization', 'Admin')
.send(dto)
.expect(201);
expect(workflow.create).toHaveBeenCalledWith(dto, {
id: 'verified-subject',
role: 'Admin',
});
expect(r.body.availableActions).toEqual(['SUBMIT_GATE1']);
expect(JSON.stringify(r.body)).not.toMatch(/token|secret/);
await request(app.getHttpServer())
.post('/admin/orchestration/tasks')
.set('Authorization', 'Admin')
.send({ ...dto, actorId: 'browser' })
.expect(400);
});
it('routes fixed transition/gate contracts and rejects invalid gates plus release without invoking core', async () => {
await request(app.getHttpServer())
.put('/admin/orchestration/tasks/t/transition')
.set('Authorization', 'Admin')
.send({
toState: 'GATE1_PENDING',
expectedVersion: 1,
idempotencyKey: 'submit',
})
.expect(200);
await request(app.getHttpServer())
.put('/admin/orchestration/tasks/t/transition')
.set('Authorization', 'SUPER_ADMIN')
.send({toState:'GATE1_HELD',expectedVersion:2,idempotencyKey:'hold',reason:'Review sources'})
.expect(200);
expect(workflow.transition).toHaveBeenCalledWith('t', expect.objectContaining({toState:'GATE1_HELD',expectedVersion:2,idempotencyKey:'hold',reason:'Review sources'}), {id:'verified-subject',role:'SUPER_ADMIN'});
await request(app.getHttpServer())
.post('/admin/orchestration/tasks/t/gates/1/decide')
.set('Authorization', 'SUPER_ADMIN')
.send({
decision: 'APPROVED',
expectedVersion: 2,
idempotencyKey: 'approval',
expiresAt: new Date(Date.now() + 60000).toISOString(),
})
.expect(201);
expect(workflow.decide).toHaveBeenCalledWith('t', 1, expect.any(Object), {
id: 'verified-subject',
role: 'SUPER_ADMIN',
});
await request(app.getHttpServer())
.post('/admin/orchestration/tasks/t/gates/2/decide')
.set('Authorization', 'SUPER_ADMIN')
.send({})
.expect(403);
await request(app.getHttpServer())
.post('/admin/orchestration/tasks/t/commands/RELEASE_AUTHORIZE')
.set('Authorization', 'SUPER_ADMIN')
.send({})
.expect(403);
expect(core.command).not.toHaveBeenCalled();
});
});
@@ -0,0 +1,568 @@
import {
Injectable,
Inject,
ForbiddenException,
BadRequestException,
ConflictException,
NotFoundException,
} from '@nestjs/common';
import { randomUUID } from 'crypto';
import { PrismaService } from '../prisma/prisma.service';
export const PREPARE_WORKFLOW_CONFIG = 'PREPARE_WORKFLOW_CONFIG';
export interface WorkflowIdentity {
id: string;
role: string;
}
export interface WorkflowConfig {
workerIds: string[];
stageCiIds: string[];
}
/** Bounded Stage preparation only. No release executor, production scope or browser worker identities. */
@Injectable()
export class PrepareWorkflowService {
constructor(
@Inject(PrismaService) private readonly db: any,
@Inject(PREPARE_WORKFLOW_CONFIG) private readonly config: WorkflowConfig,
) {}
private identity(a: WorkflowIdentity, roles: string[]) {
if (
!a?.id ||
typeof a.id !== 'string' ||
a.id.length > 100 ||
!roles.includes(a.role)
)
throw new ForbiddenException('Authenticated workflow identity required');
}
private worker(a: WorkflowIdentity) {
this.identity(a, ['WORKFLOW_WORKER']);
if (!this.config.workerIds.includes(a.id))
throw new ForbiddenException('Worker identity not provisioned');
}
private async allowed(tx: any, claiming = false) {
const rows =
await tx.$queryRaw`SELECT level, active FROM orchestration_kill_switches WHERE scope = 'GLOBAL' FOR SHARE`;
if (rows[0]?.active && !(rows[0].level === 'FREEZE_NEW_WORK' && !claiming))
throw new ForbiddenException('Kill switch blocks workflow');
}
private async task(tx: any, id: string) {
await tx.$queryRaw`SELECT id FROM orchestration_tasks WHERE id = ${id}::uuid FOR UPDATE`;
const t = await tx.orchestrationTask.findUnique({ where: { id } });
if (!t) throw new NotFoundException('Task not found');
if (
t.operationMode !== 'PREPARE' ||
(t.metadata?.workflowScope ?? t.metadata?.scope) !== 'STAGE_PREPARE'
)
throw new ForbiddenException('Not a bounded Stage proposal');
return t;
}
private async event(
tx: any,
t: any,
state: string,
a: WorkflowIdentity,
key: string,
type: string,
fingerprint: string,
) {
const result = await tx.orchestrationTask.update({
where: { id: t.id },
data: { state, version: { increment: 1 } },
});
await tx.orchestrationEvent.create({
data: {
taskId: t.id,
sequence: result.version,
actorId: a.id,
eventType: type,
fromState: t.state,
toState: state,
idempotencyKey: key,
payload: { fingerprint },
},
});
return result;
}
private async replay(tx: any, key: string, fingerprint: string) {
const e = await tx.orchestrationEvent.findUnique({
where: { idempotencyKey: key },
});
if (e && e.payload?.fingerprint !== fingerprint)
throw new ConflictException('Idempotency payload mismatch');
return e;
}
private version(t: any, dto: any) {
if (
!Number.isSafeInteger(dto.expectedVersion) ||
dto.expectedVersion !== t.version
)
throw new ConflictException('Stale task version');
if (
typeof dto.idempotencyKey !== 'string' ||
!dto.idempotencyKey.length ||
dto.idempotencyKey.length > 120
)
throw new BadRequestException('Invalid idempotency key');
}
async create(dto: any, a: WorkflowIdentity) {
this.identity(a, ['Admin', 'SUPER_ADMIN']);
const metadata = dto?.metadata;
const boundedText = (value: unknown, max: number) =>
typeof value === 'string' &&
value.trim().length > 0 &&
value.length <= max;
if (
!metadata ||
typeof metadata !== 'object' ||
Array.isArray(metadata) ||
typeof metadata.commitSha !== 'string' ||
!boundedText(metadata.scope, 2000) ||
!Array.isArray(metadata.acceptanceCriteria) ||
metadata.acceptanceCriteria.length < 1 ||
metadata.acceptanceCriteria.length > 20 ||
!metadata.acceptanceCriteria.every((criterion: unknown) =>
boundedText(criterion, 500),
)
)
throw new BadRequestException(
'PREPARE requires commitSha, scope 1..2000 characters and 1..20 acceptance criteria of 1..500 characters',
);
if (
dto.operationMode !== 'PREPARE' ||
!/^[a-f0-9]{40}$/.test(dto.metadata?.commitSha ?? '') ||
!Number.isSafeInteger(dto.budgetLimitCents) ||
dto.budgetLimitCents < 20 ||
dto.budgetLimitCents > 10000
)
throw new BadRequestException(
'PREPARE requires immutable 40hex commit and budget 20..10000 cents',
);
if (
!dto.key ||
dto.key.length > 120 ||
!dto.title ||
dto.title.length > 255 ||
!dto.department ||
dto.department.length > 50
)
throw new BadRequestException('Invalid proposal fields');
return this.db
.$transaction(async (tx: any) => {
await this.allowed(tx, true);
const t = await tx.orchestrationTask.create({
data: {
key: dto.key,
title: dto.title,
department: dto.department,
operationMode: 'PREPARE',
budgetLimitCents: dto.budgetLimitCents,
metadata: {
workflowScope: 'STAGE_PREPARE',
scope: dto.metadata.scope,
acceptanceCriteria: [...dto.metadata.acceptanceCriteria],
environment: 'staging',
commitSha: dto.metadata.commitSha,
createdBy: a.id,
},
state: 'DRAFT',
},
});
await tx.orchestrationEvent.create({
data: {
taskId: t.id,
sequence: 1,
eventType: 'PREPARE_PROPOSED',
actorId: a.id,
toState: 'DRAFT',
},
});
return t;
})
.catch((error: any) => {
if (
error?.code === 'P2002' &&
Array.isArray(error.meta?.target) &&
error.meta.target.includes('key')
)
throw new ConflictException(
'Proposal key already exists; fetch and reconcile the existing task',
);
throw error;
});
}
async transition(id: string, dto: any, a: WorkflowIdentity) {
this.identity(a, ['Admin', 'SUPER_ADMIN']);
return this.db.$transaction(async (tx: any) => {
await this.allowed(tx, true);
const t = await this.task(tx, id);
const fingerprint = JSON.stringify({ id, dto, a });
if (await this.replay(tx, dto.idempotencyKey, fingerprint)) return t;
this.version(t, dto);
if (dto.toState === 'GATE1_HELD') {
if (a.role !== 'SUPER_ADMIN' || t.metadata.createdBy === a.id || t.state !== 'GATE1_PENDING')
throw new ForbiddenException('Independent Gate1 reviewer and pending proposal required');
if (typeof dto.reason !== 'string' || !dto.reason.trim() || dto.reason.length > 2000)
throw new BadRequestException('Hold requires reason of 1..2000 characters');
return this.event(tx, t, 'GATE1_HELD', a, dto.idempotencyKey, 'PREPARE_GATE1_HELD', fingerprint);
}
if (t.metadata.createdBy !== a.id && a.role !== 'SUPER_ADMIN')
throw new ForbiddenException('Proposal owner required');
if (
!(
(t.state === 'DRAFT' && dto.toState === 'GATE1_PENDING') ||
(t.state === 'GATE1_APPROVED' && dto.toState === 'READY')
)
)
throw new ForbiddenException(
'Only submission and approved queueing are available',
);
if (dto.toState === 'READY') {
const approval = await tx.orchestrationApprovalDecision.findFirst({
where: { taskId: id, gate: 1, decision: 'APPROVED' },
orderBy: { createdAt: 'desc' },
});
if (!approval || approval.expiresAt <= new Date())
throw new ForbiddenException('Gate1 expired');
await tx.prepareWorkflowOutbox.create({ data: { taskId: id } });
}
return this.event(
tx,
t,
dto.toState,
a,
dto.idempotencyKey,
'PREPARE_TRANSITION',
fingerprint,
);
});
}
async decide(id: string, gate: number, dto: any, a: WorkflowIdentity) {
this.identity(a, ['SUPER_ADMIN']);
if (gate !== 1)
throw new ForbiddenException(
'Gate2 decisions and release remain disabled',
);
if (
!['APPROVED', 'REJECTED'].includes(dto.decision) ||
!Number.isFinite(Date.parse(dto.expiresAt)) ||
Date.parse(dto.expiresAt) <= Date.now() ||
Date.parse(dto.expiresAt) > Date.now() + 86400000
)
throw new BadRequestException(
'Decision requires future expiry within 24 hours',
);
return this.db.$transaction(async (tx: any) => {
await this.allowed(tx);
const t = await this.task(tx, id);
const fingerprint = JSON.stringify({ id, gate, dto, a });
if (await this.replay(tx, dto.idempotencyKey, fingerprint)) return t;
this.version(t, dto);
if (t.metadata.createdBy === a.id)
throw new ForbiddenException('Independent reviewer required');
if (!['GATE1_PENDING', 'GATE1_HELD'].includes(t.state))
throw new ConflictException('Gate1 pending required');
await tx.orchestrationApprovalDecision.create({
data: {
taskId: id,
gate: 1,
decision: dto.decision,
actorId: a.id,
expiresAt: new Date(dto.expiresAt),
idempotencyKey: `decision:${dto.idempotencyKey}`,
},
});
return this.event(
tx,
t,
dto.decision === 'APPROVED' ? 'GATE1_APPROVED' : 'REJECTED',
a,
dto.idempotencyKey,
'PREPARE_GATE1',
fingerprint,
);
});
}
async claim(a: WorkflowIdentity, id?: string): Promise<any> {
this.worker(a);
return this.db.$transaction(async (tx: any) => {
await this.allowed(tx, true);
// Lock order is task then outbox for every worker operation. SKIP LOCKED permits competing workers.
const rows: any[] = id
? await tx.$queryRaw`SELECT t.id FROM orchestration_tasks t JOIN prepare_workflow_outbox o ON o.task_id=t.id WHERE t.id=${id}::uuid AND o.status='PENDING' AND (o.lease_until IS NULL OR o.lease_until < now()) ORDER BY o.created_at FOR UPDATE OF t SKIP LOCKED LIMIT 1`
: await tx.$queryRaw`SELECT t.id FROM orchestration_tasks t JOIN prepare_workflow_outbox o ON o.task_id=t.id WHERE o.status='PENDING' AND (o.lease_until IS NULL OR o.lease_until < now()) ORDER BY o.created_at FOR UPDATE OF t SKIP LOCKED LIMIT 1`;
if (!rows.length) return null;
const t = await this.task(tx, rows[0].id);
const o = await tx.prepareWorkflowOutbox.findUnique({
where: { taskId: t.id },
});
const approval = await tx.orchestrationApprovalDecision.findFirst({
where: { taskId: t.id, gate: 1, decision: 'APPROVED' },
orderBy: { createdAt: 'desc' },
});
if (!approval || approval.expiresAt <= new Date())
throw new ForbiddenException('Gate1 expired');
if (o.attempts >= 3) throw new ForbiddenException('Attempts exhausted');
const costs = await tx.orchestrationBudgetLedger.aggregate({
where: { taskId: t.id },
_sum: { amountCents: true },
});
if ((costs._sum.amountCents ?? 0) + 20 > t.budgetLimitCents)
throw new ForbiddenException('Hard task budget exceeded');
if (o.runId)
await tx.orchestrationRun.update({
where: { id: o.runId },
data: {
status: 'FAILED',
retryClass: 'TRANSIENT',
finishedAt: new Date(),
},
});
const token = randomUUID(),
attempt = o.attempts + 1;
const run = await tx.orchestrationRun.create({
data: {
taskId: t.id,
attempt,
status: 'RUNNING',
environment: 'staging',
commitSha: t.metadata.commitSha,
idempotencyKey: `prepare-run:${o.id}:${attempt}`,
startedAt: new Date(),
},
});
const reservation = await tx.orchestrationBudgetLedger.create({
data: {
taskId: t.id,
amountCents: 20,
kind: 'RESERVE',
actorId: a.id,
idempotencyKey: `prepare-reserve:${run.id}`,
},
});
const leaseUntil = new Date(Date.now() + 30000);
await tx.prepareWorkflowOutbox.update({
where: { id: o.id },
data: {
attempts: attempt,
leaseOwner: a.id,
leaseToken: token,
leaseUntil,
heartbeatAt: new Date(),
runId: run.id,
reservationId: reservation.id,
},
});
await this.event(
tx,
t,
'RUNNING',
a,
`prepare-claim:${run.id}`,
'PREPARE_CLAIMED',
token,
);
return {
taskId: t.id,
runId: run.id,
attempt,
leaseToken: token,
leaseUntil,
commitSha: run.commitSha,
environment: 'staging',
maxCostCents: 20,
};
});
}
private async lease(tx: any, a: WorkflowIdentity, dto: any) {
const t = await this.task(tx, dto.taskId);
const o = await tx.prepareWorkflowOutbox.findUnique({
where: { taskId: t.id },
});
if (
!o ||
o.status !== 'PENDING' ||
o.leaseOwner !== a.id ||
o.leaseToken !== dto.leaseToken ||
o.runId !== dto.runId ||
!o.leaseUntil ||
o.leaseUntil <= new Date() ||
t.state !== 'RUNNING'
)
throw new ForbiddenException('Active owned lease required');
return { t, o };
}
async heartbeat(a: WorkflowIdentity, dto: any) {
this.worker(a);
return this.db.$transaction(async (tx: any) => {
await this.allowed(tx);
const { o } = await this.lease(tx, a, dto);
return tx.prepareWorkflowOutbox.update({
where: { id: o.id },
data: {
heartbeatAt: new Date(),
leaseUntil: new Date(Date.now() + 30000),
},
select: { leaseUntil: true },
});
});
}
async complete(a: WorkflowIdentity, dto: any) {
this.worker(a);
if (
!/^[a-f0-9]{64}$/.test(dto.sha256 ?? '') ||
!/^https:\/\//.test(dto.uri ?? '') ||
!Number.isSafeInteger(dto.actualCents) ||
dto.actualCents < 0 ||
dto.actualCents > 20
)
throw new BadRequestException(
'QA hash, HTTPS URI and cost 0..20 required',
);
return this.db.$transaction(async (tx: any) => {
await this.allowed(tx);
const { t, o } = await this.lease(tx, a, dto);
await tx.orchestrationArtifact.create({
data: { runId: o.runId, kind: 'QA', sha256: dto.sha256, uri: dto.uri },
});
await tx.orchestrationAttestation.create({
data: {
runId: o.runId,
kind: 'QA',
subjectHash: dto.sha256,
signer: a.id,
predicate: { passed: true, scope: 'STAGE_PREPARE' },
},
});
await tx.orchestrationRun.update({
where: { id: o.runId },
data: { status: 'SUCCEEDED', finishedAt: new Date() },
});
await tx.orchestrationBudgetLedger.create({
data: {
taskId: t.id,
kind: 'RECONCILE',
amountCents: dto.actualCents - 20,
actorId: a.id,
idempotencyKey: `prepare-cost:${o.runId}`,
metadata: {
reservationId: o.reservationId,
actualCents: dto.actualCents,
},
},
});
await tx.prepareWorkflowOutbox.update({
where: { id: o.id },
data: { status: 'DONE', leaseToken: null, leaseUntil: null },
});
return this.event(
tx,
t,
'VERIFYING',
a,
`prepare-qa:${o.runId}`,
'PREPARE_QA_SUCCEEDED',
dto.sha256,
);
});
}
async stageEvidence(a: WorkflowIdentity, dto: any) {
this.identity(a, ['STAGE_CI']);
if (!this.config.stageCiIds.includes(a.id))
throw new ForbiddenException('Stage CI identity not provisioned');
if (
!/^[a-f0-9]{64}$/.test(dto.evidenceSha256 ?? '') ||
!/^https:\/\//.test(dto.uri ?? '')
)
throw new BadRequestException(
'Stage evidence hash and HTTPS URI required',
);
return this.db.$transaction(async (tx: any) => {
await this.allowed(tx);
const t = await this.task(tx, dto.taskId);
const run = await tx.orchestrationRun.findUnique({
where: { id: dto.runId },
});
if (
t.state !== 'VERIFYING' ||
!run ||
run.taskId !== t.id ||
run.environment !== 'staging' ||
run.status !== 'SUCCEEDED' ||
run.commitSha !== dto.commitSha ||
t.metadata.commitSha !== dto.commitSha
)
throw new ForbiddenException(
'Immutable successful Stage run binding required',
);
const qa = await tx.orchestrationAttestation.findFirst({
where: { runId: run.id, kind: 'QA', subjectHash: dto.sha256 },
});
if (!qa || qa.signer === a.id)
throw new ForbiddenException('Independent QA evidence required');
await tx.orchestrationArtifact.create({
data: {
runId: run.id,
kind: 'STAGE_EVIDENCE',
sha256: dto.evidenceSha256,
uri: dto.uri,
},
});
await tx.orchestrationAttestation.create({
data: {
runId: run.id,
kind: 'STAGE',
subjectHash: dto.sha256,
signer: a.id,
predicate: {
evidenceSha256: dto.evidenceSha256,
commitSha: dto.commitSha,
environment: 'staging',
},
},
});
return this.event(
tx,
t,
'GATE2_PENDING',
a,
`prepare-stage:${run.id}`,
'STAGE_EVIDENCE_RECORDED',
dto.evidenceSha256,
);
});
}
project(t: any, a: WorkflowIdentity) {
const safe =
t.operationMode === 'PREPARE' &&
(t.metadata?.workflowScope ?? t.metadata?.scope) === 'STAGE_PREPARE';
const owner =
a?.id && (t.metadata?.createdBy === a.id || a.role === 'SUPER_ADMIN');
const actions: string[] = [];
if (safe && owner && t.state === 'DRAFT') actions.push('SUBMIT_GATE1');
if (safe && owner && t.state === 'GATE1_APPROVED')
actions.push('QUEUE_PREPARE');
if (
safe &&
a?.role === 'SUPER_ADMIN' &&
t.metadata?.createdBy !== a.id &&
['GATE1_PENDING', 'GATE1_HELD'].includes(t.state)
) {
actions.push('APPROVE_GATE1', 'REJECT_GATE1');
if (t.state === 'GATE1_PENDING') actions.push('HOLD_GATE1');
}
return {
...t,
metadata: safe
? {
scope: t.metadata.scope,
...(t.metadata.workflowScope === 'STAGE_PREPARE'
? { acceptanceCriteria: t.metadata.acceptanceCriteria }
: {}),
environment: 'staging',
commitSha: t.metadata.commitSha,
}
: {},
availableActions: actions,
};
}
}
@@ -0,0 +1,535 @@
import { Test } from '@nestjs/testing';
import request from 'supertest';
import { OrchestrationController } from './orchestration.controller';
import { OrchestrationService } from './orchestration.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { PrismaClient } from '@prisma/client';
import { PrepareWorkflowService } from './prepare-workflow.service';
import { OperationMode, WorkflowState } from './orchestration.domain';
const enabled = process.env.WORKFLOW_DB === '1';
(enabled ? describe : describe.skip)('isolated prepare workflow', () => {
const db = new PrismaClient();
const admin = { id: 'proposer', role: 'Admin' };
const reviewer = { id: 'reviewer', role: 'SUPER_ADMIN' };
const worker = { id: 'worker-1', role: 'WORKFLOW_WORKER' };
const ci = { id: 'isolated-ci', role: 'STAGE_CI' };
const service = new PrepareWorkflowService(db as never, {
workerIds: ['worker-1', 'worker-2'],
stageCiIds: ['isolated-ci'],
});
let seq = 0;
const dto = () => ({
key: `wf-${Date.now()}-${seq++}`,
title: 'Bounded isolated QA tracer',
department: 'ENGINEERING',
operationMode: OperationMode.PREPARE,
budgetLimitCents: 20,
metadata: {
commitSha: 'a'.repeat(40),
token: 'must-not-persist',
scope: 'Staging schema audit only',
acceptanceCriteria: ['Report all schema failures'],
actorId: 'spoof',
},
});
async function ready() {
let t = await service.create(dto(), admin);
t = await service.transition(
t.id,
{
toState: WorkflowState.GATE1_PENDING,
expectedVersion: t.version,
idempotencyKey: `submit-${t.id}`,
},
admin,
);
t = await service.decide(
t.id,
1,
{
decision: 'APPROVED',
expectedVersion: t.version,
idempotencyKey: `approve-${t.id}`,
expiresAt: new Date(Date.now() + 60000).toISOString(),
},
reviewer,
);
return service.transition(
t.id,
{
toState: WorkflowState.READY,
expectedVersion: t.version,
idempotencyKey: `ready-${t.id}`,
},
admin,
);
}
afterAll(async () => {
await db.$disconnect();
});
it('serializes competing owner holds and preserves audited reason, then decides held task', async () => {
let t = await service.create(dto(), admin);
t = await service.transition(t.id, {toState:'GATE1_PENDING',expectedVersion:t.version,idempotencyKey:`submit-hold-${t.id}`}, admin);
const command = {toState:'GATE1_HELD',expectedVersion:t.version,idempotencyKey:`hold-${t.id}`,reason:'Need source review'};
const results = await Promise.allSettled([
service.transition(t.id, command, reviewer),
service.transition(t.id, {...command,idempotencyKey:`competing-${t.id}`}, reviewer),
]);
expect(results.filter(r=>r.status==='fulfilled')).toHaveLength(1);
const held = await db.orchestrationTask.findUniqueOrThrow({where:{id:t.id}});
expect(held.state).toBe('GATE1_HELD');
expect(held.version).toBe(t.version+1);
const events = await db.orchestrationEvent.findMany({where:{taskId:t.id,eventType:'PREPARE_GATE1_HELD'}});
expect(events).toHaveLength(1);
expect(events[0].actorId).toBe(reviewer.id);
expect(JSON.stringify(events[0].payload)).toContain('Need source review');
expect(await db.orchestrationApprovalDecision.count({where:{taskId:t.id}})).toBe(0);
expect(await db.prepareWorkflowOutbox.count({where:{taskId:t.id}})).toBe(0);
await expect(service.transition(t.id, {...command,expectedVersion:held.version,reason:'changed'},reviewer)).rejects.toMatchObject({status:409});
const decision = await service.decide(t.id,1,{decision:'REJECTED',expectedVersion:held.version,idempotencyKey:`decline-held-${t.id}`,expiresAt:new Date(Date.now()+60000).toISOString()},reviewer);
expect(decision.state).toBe('REJECTED');
});
it('persists and projects immutable bounded proposal scope without arbitrary metadata', async () => {
const input = dto();
const t = await service.create(input, admin);
expect(t.metadata).toMatchObject({
workflowScope: 'STAGE_PREPARE',
scope: input.metadata.scope,
acceptanceCriteria: input.metadata.acceptanceCriteria,
commitSha: input.metadata.commitSha,
});
expect(t.metadata).not.toHaveProperty('token');
expect(t.metadata).not.toHaveProperty('actorId');
const submitted = await service.transition(
t.id,
{
toState: 'GATE1_PENDING',
expectedVersion: t.version,
idempotencyKey: `immutable-${t.id}`,
},
admin,
);
expect(submitted.metadata).toEqual(t.metadata);
expect(service.project(submitted, reviewer).metadata).toEqual({
scope: input.metadata.scope,
acceptanceCriteria: input.metadata.acceptanceCriteria,
environment: 'staging',
commitSha: input.metadata.commitSha,
});
});
it.each([
null,
[],
{},
{ commitSha: 'a'.repeat(40), scope: '', acceptanceCriteria: ['ok'] },
{
commitSha: 'a'.repeat(40),
scope: ' '.repeat(10),
acceptanceCriteria: ['ok'],
},
{
commitSha: 'a'.repeat(40),
scope: 'x'.repeat(2001),
acceptanceCriteria: ['ok'],
},
{ commitSha: 'a'.repeat(40), scope: 'ok', acceptanceCriteria: [] },
{
commitSha: 'a'.repeat(40),
scope: 'ok',
acceptanceCriteria: Array(21).fill('ok'),
},
{
commitSha: 'a'.repeat(40),
scope: 'ok',
acceptanceCriteria: ['x'.repeat(501)],
},
{ commitSha: 'a'.repeat(40), scope: 'ok', acceptanceCriteria: [' '] },
{ commitSha: 'a'.repeat(40), scope: 'ok', acceptanceCriteria: [123] },
{ commitSha: ['a'.repeat(40)], scope: 'ok', acceptanceCriteria: ['ok'] },
{ scope: 'ok', acceptanceCriteria: ['ok'] },
])(
'rejects unsafe or unbounded metadata %# without persisting',
async (metadata) => {
const input = { ...dto(), metadata };
await expect(service.create(input, admin)).rejects.toMatchObject({
status: 400,
});
expect(
await db.orchestrationTask.count({ where: { key: input.key } }),
).toBe(0);
},
);
it('accepts exact metadata bounds', async () => {
const metadata = {
commitSha: 'a'.repeat(40),
scope: 'x'.repeat(2000),
acceptanceCriteria: Array(20).fill('x'.repeat(500)),
};
expect(
(await service.create({ ...dto(), metadata }, admin)).metadata,
).toMatchObject(metadata);
});
it('normalizes racing duplicate proposal keys to conflict without duplicate events', async () => {
const input = dto();
const results = await Promise.allSettled([
service.create(input, admin),
service.create({ ...input, title: 'Different intent' }, reviewer),
]);
expect(results.filter((r) => r.status === 'fulfilled')).toHaveLength(1);
const rejected = results.find(
(r) => r.status === 'rejected',
) as PromiseRejectedResult;
expect(rejected.reason).toMatchObject({ status: 409 });
const persisted = await db.orchestrationTask.findUniqueOrThrow({
where: { key: input.key },
});
expect(
await db.orchestrationEvent.count({ where: { taskId: persisted.id } }),
).toBe(1);
await expect(service.create(input, admin)).rejects.toMatchObject({
status: 409,
});
});
it('enforces create and read contracts over HTTP with real PostgreSQL persistence', async () => {
const module = await Test.createTestingModule({
controllers: [OrchestrationController],
providers: [
{ provide: PrepareWorkflowService, useValue: service },
{
provide: OrchestrationService,
useValue: {
findAll: () => db.orchestrationTask.findMany(),
findOne: (id: string) =>
db.orchestrationTask.findUniqueOrThrow({ where: { id } }),
},
},
],
})
.overrideGuard(JwtAuthGuard)
.useValue({
canActivate(context: any) {
context.switchToHttp().getRequest().user = {
sub: admin.id,
role: admin.role,
};
return true;
},
})
.overrideGuard(RolesGuard)
.useValue({ canActivate: () => true })
.compile();
const app = module.createNestApplication();
await app.init();
try {
const input = dto();
const response = await request(app.getHttpServer())
.post('/admin/orchestration/tasks')
.send(input)
.expect(201);
expect(response.body.metadata).toEqual({
scope: input.metadata.scope,
acceptanceCriteria: input.metadata.acceptanceCriteria,
commitSha: input.metadata.commitSha,
environment: 'staging',
});
expect(response.body.availableActions).toEqual(['SUBMIT_GATE1']);
const detail = await request(app.getHttpServer())
.get(`/admin/orchestration/tasks/${response.body.id}`)
.expect(200);
expect(detail.body.metadata).toEqual(response.body.metadata);
const list = await request(app.getHttpServer())
.get('/admin/orchestration/tasks')
.expect(200);
expect(
list.body.find((row: any) => row.id === response.body.id).metadata,
).toEqual(response.body.metadata);
expect(JSON.stringify(response.body)).not.toMatch(
/createdBy|must-not-persist|actorId|workflowScope/,
);
await request(app.getHttpServer())
.post('/admin/orchestration/tasks')
.send(input)
.expect(409);
await request(app.getHttpServer())
.post('/admin/orchestration/tasks')
.send({
...input,
key: `${input.key}-invalid`,
metadata: { scope: 'only stage', acceptanceCriteria: ['QA passes'] },
})
.expect(400);
await request(app.getHttpServer())
.post('/admin/orchestration/tasks')
.send({
...input,
key: `${input.key}-invalid`,
metadata: { ...input.metadata, acceptanceCriteria: [' '] },
})
.expect(400);
} finally {
await app.close();
}
});
it('fences heartbeat and completes QA plus independent Stage evidence only to Gate2 pending', async () => {
const t = await ready();
const c = await service.claim(worker, t.id);
await expect(
service.heartbeat({ ...worker, id: 'worker-2' }, c),
).rejects.toThrow(/lease/i);
await service.heartbeat(worker, c);
const qa = {
...c,
sha256: 'b'.repeat(64),
uri: 'https://ci.example/qa',
actualCents: 8,
};
await service.complete(worker, qa);
await expect(
service.stageEvidence(
{ id: 'proposer', role: 'Admin' },
{
taskId: t.id,
runId: c.runId,
sha256: qa.sha256,
commitSha: c.commitSha,
evidenceSha256: 'c'.repeat(64),
uri: 'https://ci.example/stage',
},
),
).rejects.toThrow(/identity/i);
await service.stageEvidence(ci, {
taskId: t.id,
runId: c.runId,
sha256: qa.sha256,
commitSha: c.commitSha,
evidenceSha256: 'c'.repeat(64),
uri: 'https://ci.example/stage',
});
expect(
await db.orchestrationTask.findUnique({ where: { id: t.id } }),
).toMatchObject({ state: 'GATE2_PENDING' });
expect(
await db.orchestrationBudgetLedger.aggregate({
where: { taskId: t.id },
_sum: { amountCents: true },
}),
).toMatchObject({ _sum: { amountCents: 8 } });
await expect(service.complete(worker, qa)).rejects.toThrow(/lease|active/i);
});
it('bounds expired-lease retries and rejects stale completion without releasing unknown costs', async () => {
const t = await service.create({ ...dto(), budgetLimitCents: 60 }, admin);
let current = await service.transition(
t.id,
{
toState: 'GATE1_PENDING',
expectedVersion: 1,
idempotencyKey: `s-${t.id}`,
},
admin,
);
current = await service.decide(
t.id,
1,
{
decision: 'APPROVED',
expectedVersion: current.version,
idempotencyKey: `a-${t.id}`,
expiresAt: new Date(Date.now() + 60000).toISOString(),
},
reviewer,
);
await service.transition(
t.id,
{
toState: 'READY',
expectedVersion: current.version,
idempotencyKey: `q-${t.id}`,
},
admin,
);
const first = await service.claim(worker, t.id);
for (let i = 0; i < 2; i++) {
await db.prepareWorkflowOutbox.update({
where: { taskId: t.id },
data: { leaseUntil: new Date(0) },
});
const next = await service.claim(worker, t.id);
expect(next.attempt).toBe(i + 2);
}
await expect(
service.complete(worker, {
...first,
sha256: 'b'.repeat(64),
uri: 'https://ci.example/qa',
actualCents: 0,
}),
).rejects.toThrow(/lease/i);
await db.prepareWorkflowOutbox.update({
where: { taskId: t.id },
data: { leaseUntil: new Date(0) },
});
await expect(service.claim(worker, t.id)).rejects.toThrow(/exhausted/i);
expect(await db.orchestrationRun.count({ where: { taskId: t.id } })).toBe(
3,
);
expect(
await db.orchestrationBudgetLedger.aggregate({
where: { taskId: t.id },
_sum: { amountCents: true },
}),
).toMatchObject({ _sum: { amountCents: 60 } });
});
it('rejects self approval, non-PREPARE and unprovisioned workers', async () => {
await expect(
service.create(
{ ...dto(), operationMode: OperationMode.MUTATE_PRODUCTION },
admin,
),
).rejects.toThrow(/PREPARE/);
let t = await service.create(dto(), reviewer);
t = await service.transition(
t.id,
{
toState: 'GATE1_PENDING',
expectedVersion: 1,
idempotencyKey: `s-${t.id}`,
},
reviewer,
);
await expect(
service.decide(
t.id,
1,
{
decision: 'APPROVED',
expectedVersion: t.version,
idempotencyKey: `a-${t.id}`,
expiresAt: new Date(Date.now() + 60000).toISOString(),
},
reviewer,
),
).rejects.toThrow(/independent/i);
await expect(
service.claim({ id: 'spoof', role: 'WORKFLOW_WORKER' }),
).rejects.toThrow(/provisioned/i);
await expect(service.decide(t.id, 2, {}, reviewer)).rejects.toThrow(
/disabled/i,
);
});
it('serializes duplicate queue submission and denies claims beyond remaining task budget', async () => {
const t = await ready();
await db.orchestrationBudgetLedger.create({
data: {
taskId: t.id,
amountCents: 1,
kind: 'SPEND',
actorId: 'fixture',
idempotencyKey: `spend-${t.id}`,
},
});
await expect(service.claim(worker, t.id)).rejects.toThrow(/budget/i);
expect(await db.orchestrationRun.count({ where: { taskId: t.id } })).toBe(
0,
);
expect(
await db.prepareWorkflowOutbox.count({ where: { taskId: t.id } }),
).toBe(1);
});
it('replays submission safely and serializes racing queue requests', async () => {
let t = await service.create(dto(), admin);
const submit = {
toState: 'GATE1_PENDING',
expectedVersion: t.version,
idempotencyKey: `replay-${t.id}`,
};
t = await service.transition(t.id, submit, admin);
expect((await service.transition(t.id, submit, admin)).version).toBe(
t.version,
);
await expect(
service.transition(t.id, { ...submit, toState: 'READY' }, admin),
).rejects.toThrow(/mismatch/i);
t = await service.decide(
t.id,
1,
{
decision: 'APPROVED',
expectedVersion: t.version,
idempotencyKey: `r-approve-${t.id}`,
expiresAt: new Date(Date.now() + 60000).toISOString(),
},
reviewer,
);
const results = await Promise.allSettled(
['one', 'two'].map((k) =>
service.transition(
t.id,
{
toState: 'READY',
expectedVersion: t.version,
idempotencyKey: `${k}-${t.id}`,
},
admin,
),
),
);
expect(results.filter((r) => r.status === 'fulfilled')).toHaveLength(1);
expect(
await db.prepareWorkflowOutbox.count({ where: { taskId: t.id } }),
).toBe(1);
});
it('kill switch freezes claims without creating run or reservation', async () => {
const t = await ready();
await db.orchestrationKillSwitch.create({
data: {
scope: 'GLOBAL',
level: 'FREEZE_NEW_WORK',
reason: 'isolated test',
actorId: 'fixture',
},
});
try {
await expect(service.claim(worker, t.id)).rejects.toThrow(/kill switch/i);
} finally {
await db.orchestrationKillSwitch.delete({ where: { scope: 'GLOBAL' } });
}
expect(await db.orchestrationRun.count({ where: { taskId: t.id } })).toBe(
0,
);
});
it('persists an atomic outbox and exactly one worker wins concurrent claims', async () => {
const t = await ready();
const claims = await Promise.all([
service.claim(worker, t.id),
service.claim({ id: 'worker-2', role: 'WORKFLOW_WORKER' }, t.id),
]);
expect(claims.filter(Boolean)).toHaveLength(1);
const claim = claims.find(Boolean)!;
expect(claim.taskId).toBe(t.id);
expect(claim.attempt).toBe(1);
expect(await db.orchestrationRun.count({ where: { taskId: t.id } })).toBe(
1,
);
expect(
await db.orchestrationBudgetLedger.aggregate({
where: { taskId: t.id },
_sum: { amountCents: true },
}),
).toMatchObject({ _sum: { amountCents: 20 } });
expect(
await service.project(
await db.orchestrationTask.findUniqueOrThrow({ where: { id: t.id } }),
admin,
),
).toMatchObject({
metadata: {
scope: 'Staging schema audit only',
acceptanceCriteria: ['Report all schema failures'],
environment: 'staging',
commitSha: 'a'.repeat(40),
},
});
});
});
+1
View File
@@ -5,5 +5,6 @@ import { OrdersController } from "./orders.controller";
@Module({
controllers: [OrdersController],
providers: [OrdersService],
exports: [OrdersService],
})
export class OrdersModule {}
+1 -1
View File
@@ -39,7 +39,7 @@ describe("OrdersService", () => {
uiText: {
findFirst: jest.fn().mockResolvedValue(null),
},
$executeRaw: jest.fn(),
$executeRaw: jest.fn().mockResolvedValue(1),
$transaction: jest.fn(),
};
+2 -1
View File
@@ -346,13 +346,14 @@ export class OrdersService {
// Deduct inventory immediately for non-online orders (card to card)
if (!isOnline) {
for (const item of createOrderDto.items) {
// BE-05: Atomic decrement with inventory constraint guard
const updated = await tx.product.update({
where: { id: item.productId },
data: {
stock: { decrement: item.quantity },
},
});
if (updated.stock <= 0) {
if (updated && updated.stock <= 0) {
await tx.product.update({
where: { id: item.productId },
data: { stockStatus: "OUT_OF_STOCK" },
+24 -9
View File
@@ -447,17 +447,32 @@ export class PaymentService {
if (orderWithItems?.orderItems) {
for (const item of orderWithItems.orderItems) {
if (item.productId) {
const updated = await tx.product.update({
where: { id: item.productId },
data: {
stock: { decrement: item.quantity },
},
});
if (updated.stock <= 0) {
await tx.product.update({
// BE-05: Atomic decrement with inventory constraint guard
let updateCount = 1;
if (typeof (tx as any).$executeRaw === 'function') {
updateCount = await tx.$executeRaw`
UPDATE products
SET stock = stock - ${item.quantity},
stock_status = CASE WHEN stock - ${item.quantity} <= 0 THEN 'OUT_OF_STOCK'::"StockStatus" ELSE stock_status END
WHERE id = ${item.productId}::uuid AND stock >= ${item.quantity}
`;
} else {
const updated = await tx.product.update({
where: { id: item.productId },
data: { stockStatus: "OUT_OF_STOCK" },
data: { stock: { decrement: item.quantity } },
});
if (updated.stock <= 0) {
await tx.product.update({
where: { id: item.productId },
data: { stockStatus: "OUT_OF_STOCK" },
});
}
}
if (updateCount === 0) {
this.logger.error(
`[CriticalInventoryAlert] Product ${item.productId} lacked sufficient stock for paid order ${transaction.orderId} (requested ${item.quantity}). Manual inventory check required.`
);
}
}
}
+26
View File
@@ -1,13 +1,39 @@
import { Injectable, OnModuleInit, OnModuleDestroy } from "@nestjs/common";
import { PrismaClient } from "@prisma/client";
import { Logger } from "@nestjs/common";
@Injectable()
export class PrismaService
extends PrismaClient
implements OnModuleInit, OnModuleDestroy
{
private readonly logger = new Logger(PrismaService.name);
constructor() {
super({
log: [
{ emit: "event", level: "query" },
{ emit: "stdout", level: "error" },
{ emit: "stdout", level: "warn" },
],
});
}
async onModuleInit() {
await this.$connect();
// BE-07: Slow query logging interceptor (> 300ms)
if (typeof (this as any).$on === "function") {
(this as any).$on("query", (e: any) => {
if (e.duration >= 300) {
this.logger.warn(
`[SlowQueryAlert] Duration: ${e.duration}ms | Target: ${e.target} | Query: ${e.query.substring(0, 300)}...`,
);
}
});
}
await this.ensureTablesExist();
}
@@ -14,6 +14,9 @@ describe("ProductsService", () => {
findFirst: jest.fn(),
count: jest.fn(),
},
orderItem: {
groupBy: jest.fn().mockResolvedValue([]),
},
};
const mockRevalidationService = {
+25 -10
View File
@@ -87,6 +87,7 @@ export class ProductsService {
const skip = (page - 1) * limit;
// 1. Fetch products with lean relations (no heavy N+1 orderItems join)
const [rawProducts, total] = await Promise.all([
this.prisma.product.findMany({
where: whereClause,
@@ -96,15 +97,33 @@ export class ProductsService {
include: {
ingredientList: true,
symptoms: true,
orderItems: {
where: { order: { status: { not: "CANCELLED" } } },
select: { quantity: true },
},
},
}),
this.prisma.product.count({ where: whereClause }),
]);
// 2. Efficient single aggregate query for total sold counts for the current page slice
const productIds = rawProducts.map((p) => p.id);
let salesMap: Record<string, number> = {};
if (productIds.length > 0) {
const salesAgg = await this.prisma.orderItem.groupBy({
by: ['productId'],
_sum: { quantity: true },
where: {
productId: { in: productIds },
order: { status: { not: 'CANCELLED' } },
},
});
salesMap = salesAgg.reduce((acc, row) => {
if (row.productId) {
acc[row.productId] = Number(row._sum.quantity) || 0;
}
return acc;
}, {} as Record<string, number>);
}
const isWholesaleOrAdmin =
userRole === "User_Wholesale" ||
userRole === "User_Partner" ||
@@ -113,12 +132,8 @@ export class ProductsService {
const isAdmin = userRole === "ADMIN" || userRole === "SuperAdmin";
const data = rawProducts.map((p) => {
const totalSold =
p.orderItems?.reduce(
(acc, item) => acc + (Number(item.quantity) || 0),
0,
) || 0;
const { buyPrice, wholesalePrice, orderItems, ...publicProduct } = p;
const totalSold = salesMap[p.id] || 0;
const { buyPrice, wholesalePrice, ...publicProduct } = p;
void buyPrice;
void wholesalePrice;
if (!isWholesaleOrAdmin) {
@@ -140,6 +140,18 @@ describe("TorobController", () => {
expect(result.max_pages).toBe(1);
});
it.each([['garbage', 'garbage', 100], ['2', 'garbage', 100], ['garbage', '20', 20]])(
"defaults nonnumeric pagination safely (%s, %s)", async (page, size, take) => {
mockPrismaService.product.count.mockResolvedValue(0);
mockPrismaService.product.findMany.mockResolvedValue([]);
const result = await controller.getTorobProducts(page, size);
expect(mockPrismaService.product.findMany).toHaveBeenCalledWith(
expect.objectContaining({ skip: page === '2' ? 100 : 0, take }),
);
expect(result.max_pages).toBe(1);
},
);
it("should correctly mark out-of-stock products", async () => {
const mockOutOfStock = [
{
Loaded 100 of 353 files, more files were not shown because too many files have changed in this diff. Show more