.github/9
ISSUE_TEMPLATE/3bug_report.md config.yml feature_request.md
workflows/3ci.yml clean-clone.yml release.yml
CODEOWNERS dependabot.yml pull_request_template.md
apps/630
api/250
src/243
common/30
__tests__/3authorize.test.ts bigint-serialization.interceptor.test.ts files-limits.test.ts
filters/1all-exceptions.filter.ts
guards/3auth.guard.ts internal-service.guard.ts rate-limit.guard.ts
interceptors/2actor-context.interceptor.ts bigint-serialization.interceptor.ts
middleware/2idempotency.test.ts idempotency.ts
pipes/1zod-validation.pipe.ts
actor-context.ts audit.service.ts cache.test.ts cache.ts cookies.ts core.module.ts deprecation.ts email.service.ts errors.ts event-bus.ts log-scrubber.test.ts log-scrubber.ts logger.ts permission-cache-bridge.ts prisma.service.ts redis.service.ts trace-metadata.ts types.ts
config/1env.ts
lib/11
__tests__/5crypto.test.ts filter-builder.test.ts jwt.test.ts pagination.test.ts positions.test.ts
authorize.ts crypto.ts filter-builder.ts jwt.ts pagination.ts positions.ts
modules/197
ai/28
__tests__/4ai-writing.test.ts database-ai.test.ts generate-executor.test.ts prompts.test.ts
generator/4executor.ts generate.controller.ts generate.dto.ts generate.service.ts
ai-chat.service.ts ai-rate-limit.ts ai-writing.controller.ts ai-writing.dto.ts ai-writing.service.ts ai.controller.ts ai.module.ts ai.service.ts budget-guard.service.ts database-ai.controller.ts database-ai.dto.ts database-ai.service.ts db-prompts.ts dto.ts gateway-provider.ts ledger.service.ts prompts.ts rag.controller.ts rag.dto.ts rag.service.ts
api-keys/3api-keys.controller.ts api-keys.module.ts api-keys.service.ts
audit/2audit-log.controller.ts audit.module.ts
auth/4auth.controller.ts auth.module.ts auth.service.ts dto.ts
automations/5automations.controller.ts automations.module.ts automations.service.ts dto.ts webhook-incoming.controller.ts
blocks/5
__tests__/1batch-version-cas.test.ts
block.service.ts blocks.controller.ts blocks.module.ts dto.ts
comments/4comments.controller.ts comments.module.ts comments.service.ts dto.ts
databases/9
__tests__/3normalize-select-options.test.ts option-id-canonicalization.test.ts record-open-query-shape.test.ts
database.service.ts databases.controller.ts databases.module.ts dto.ts property-cache-schema.ts query.service.ts
docs/2docs.controller.ts docs.module.ts
files/10
__tests__/1quota.test.ts
dto.ts files.controller.ts files.module.ts files.service.ts quota.ts record.ts s3.ts uploads.controller.ts uploads.service.ts
forms/18debug.controller.ts form.dto.ts forms.controller.ts forms.module.ts forms.service.ts markdown.test.ts markdown.ts optional-auth.guard.ts public-forms.controller.ts public-forms.service.ts question.dto.ts questions.service.ts rate-limit.ts recaptcha.ts slug.ts submissions.controller.ts submissions.service.ts submit-schema.ts
health/3health.controller.ts health.module.ts health.service.ts
integrations/3integrations.controller.ts integrations.module.ts integrations.service.ts
internal/12
__tests__/2flush-query-shape.test.ts internal.endpoints.test.ts
ai-test-seam.controller.ts ai-test-seam.dto.ts ai-test-seam.service.ts internal.controller.ts internal.dto.ts internal.module.ts internal.service.ts test-seam.controller.ts test-seam.dto.ts test-seam.service.ts
notifications/3notifications.controller.ts notifications.module.ts notifications.service.ts
oauth/6
__tests__/1oauth.scopes.test.ts
oauth.controller.ts oauth.module.ts oauth.service.ts scopes.ts token-keys.ts
org/15audit.service.ts embed-preview.controller.ts guards.ts members.service.ts org-admin.controller.ts org-policy.service.ts org.module.ts overview.service.ts policy-effective.controller.ts policy.schema.ts policy.test.ts retention.service.ts totp.test.ts totp.ts two-factor.service.ts
orgs/5dto.ts orgs.controller.ts orgs.module.ts orgs.service.ts util.ts
pages/4dto.ts page.service.ts pages.controller.ts pages.module.ts
scim/15
__tests__/3email-mapping.test.ts etag.test.ts filters.test.ts
etag.ts filters.ts groups.service.ts schemas.ts scim-admin.controller.ts scim-admin.service.ts scim-auth.guard.ts scim-auth.service.ts scim-errors.ts scim.controller.ts scim.module.ts users.service.ts
search/4
__tests__/1search.service.test.ts
search.controller.ts search.module.ts search.service.ts
sessions/3sessions.controller.ts sessions.module.ts sessions.service.ts
share/3share.controller.ts share.module.ts share.service.ts
sso/16
__tests__/4enforcement.test.ts oidc.verify.test.ts provisioning.test.ts saml.validate.test.ts
enforcement.ts oidc.controller.ts oidc.service.ts oidc.ts provisioning.service.ts saml.controller.ts saml.ts schemas.ts sso-admin.controller.ts sso-admin.service.ts sso-secrets.ts sso.module.ts
teams/4dto.ts teams.controller.ts teams.module.ts teams.service.ts
users/4dto.ts users.controller.ts users.module.ts users.service.ts
webhooks/3webhooks.controller.ts webhooks.module.ts webhooks.service.ts
workspaces/4dto.ts workspaces.controller.ts workspaces.module.ts workspaces.service.ts
types/1xmldom.d.ts
app.module.ts constants.ts main.ts
_probe_rawbody.cjs .env.example Dockerfile package.json README.md tsconfig.json vitest.config.ts
realtime/33
src/17auth.ts comments.ts config.ts event-source.ts http-store.ts index.ts metrics.ts notifications.ts page-updated-source.ts presence.ts prisma-store.ts protocol.ts rate-limit.ts room-key.ts rooms.ts server.ts update-log.ts
test/11auth.test.ts config.test.ts event-source.test.ts http-store.test.ts metrics-merge.test.ts page-updated-source.test.ts presence.test.ts protocol.test.ts rate-limit.test.ts rooms.test.ts task-0094-builders.test.ts
.env.example Dockerfile package.json README.md tsconfig.json
web/255
src/244
app/45
__tests__/1theme-tokens.test.ts
(app)/26
w/25
[workspace]/25
ask/1page.tsx
databases/1
[id]/1page.tsx
favorites/1page.tsx
files/1page.tsx
notifications/1page.tsx
page/4
[id]/4
[block]/1page.tsx
share/1page.tsx
layout.tsx page.tsx
search/1page.tsx
settings/10
appearance/1page.tsx
automations/3
[id]/1page.tsx
new/1page.tsx
page.tsx
billing/1page.tsx
developers/1page.tsx
members/1page.tsx
permissions/1page.tsx
teams/1page.tsx
page.tsx
shared/1page.tsx
templates/1page.tsx
trash/1page.tsx
layout.tsx page.tsx
layout.tsx
(auth)/4
login/1page.tsx
register/1page.tsx
reset-password/1page.tsx
layout.tsx
api/2
metrics/1route.ts
v1/1
health/1route.ts
f/1
[slug]/1page.tsx
health/1
ready/1route.ts
org/2
[orgId]/2
admin/2
integrations/1page.tsx
page.tsx
share/1
[id]/1page.tsx
error.tsx global-error.tsx globals.css layout.tsx not-found.tsx page.tsx providers.tsx
components/118
admin/10
org-admin/8ApiKeysTab.tsx AuditTab.tsx MembersTab.tsx OverviewTab.tsx PoliciesTab.tsx RetentionTab.tsx WebhooksTab.tsx WorkspacePicker.tsx
ScimCard.tsx SsoTabs.tsx
ai/6ai-panel.tsx ask-ai-submenu.tsx db-ai-dialog.tsx generate-page-dialog.tsx writing-actions-menu.tsx writing-diff.tsx
automations/5action-editor.tsx condition-editor.tsx lib.ts run-history.tsx trigger-editor.tsx
collaboration/6CursorLayer.tsx OfflineBanner.tsx PresenceBar.tsx README.md ReviewFailedDialog.tsx StatusChip.tsx
comments/3comment-panel.tsx comment-thread.tsx mention-input.tsx
database/27
__tests__/2cell-editor.test.tsx cell-renderer.test.tsx
AiExtractDialog.tsx AiFormulaDialog.tsx AiGenerateDialog.tsx board-view.tsx buttons-panel.tsx calendar-view.tsx cell-editor.tsx cell-renderer.tsx chart-view.tsx database-page-view.tsx database-toolbar.tsx database-view-renderer.tsx db-embedded.tsx filter-builder.tsx form-builder-view.tsx formula-editor.tsx gallery-view.tsx group-builder.tsx list-view.tsx property-editor.tsx record-header.tsx record-panel.tsx sort-builder.tsx table-view.tsx timeline-view.tsx
dialogs/7
__tests__/1move-to-dialog.test.tsx
create-dialog.tsx dialog.tsx file-permissions-dialog.tsx invite-dialog.tsx move-to-dialog.tsx share-dialog.tsx
editor/19
__tests__/3code-block-text.test.ts code-block.test.tsx expandable.test.tsx
block-card.tsx block-renderer.tsx block-save-queue.ts block-toolbar.tsx context-menu.tsx expandable.tsx history-popover.tsx inline-editor.tsx insert-dialog.tsx page-canvas.tsx page-spinner.tsx page-view.tsx selection-toolbar.tsx slash-menu.tsx template-dialog.tsx type-menu.tsx
files/4FileBlock.tsx FileBrowser.tsx FilePicker.tsx PreviewModal.tsx
form/4form-field.tsx form-renderer.tsx form-responses-view.tsx form-view.tsx
layout/8
__tests__/1page-tree-icon.test.tsx
command-palette.tsx factory-logo.tsx page-tree-node.tsx sidebar.tsx theme-provider.tsx topbar.tsx workspace-shell.tsx
mentions/3
__tests__/1mention-model.test.ts
mention-model.ts MentionPopup.tsx
search/3ask-ai-search.tsx results-list.tsx search-dialog.tsx
settings/10
api-keys/4ApiKeyCreateDialog.tsx ApiKeyRevealDialog.tsx ApiKeyTable.tsx ScopesBadge.tsx
webhooks/5CurlExamples.tsx DeliveryInspector.tsx EventExplorer.tsx OverviewTab.tsx WebhooksTab.tsx
settings-shell.tsx
ui/3confirm-dialog.tsx form.tsx toaster.tsx
hooks/21
__tests__/4realtime-frame-handler.test.ts use-blocks.test.ts use-database-options.test.ts use-workspace.test.tsx
realtime-frame-handler.ts use-ai-stream.ts use-auth.ts use-blocks.ts use-database.ts use-db-ai-stream.ts use-forms.ts use-members.ts use-mention-sources.ts use-notifications.ts use-offline-status.ts use-pages.ts use-presence-announcer.ts use-presence-toasts.ts use-presence.ts use-realtime.ts use-workspace.ts
lib/50
__tests__/21ai-database.test.ts ai-generate.test.ts ai-sse.test.ts ai-writing.test.ts api-keys.test.ts api-members.test.ts auth-refresh.test.ts auth-store.test.ts batch-queue-drop.test.ts block-content.test.ts clipboard-import.test.ts code-highlight.test.ts offline-status.test.ts presence-announcer.test.ts presence-color.test.ts presence-cursor.test.ts presence-emitter.test.ts realtime-url.test.ts search.test.ts utils.test.ts writing-diff.test.ts
files/7
__tests__/2picker.test.ts upload.test.ts
api.ts picker.ts upload-store.ts upload.ts use-file-uploads.ts
ai-database.ts ai.ts api-keys.ts api.ts auth-store.ts batch-queue.ts block-content.ts clipboard-import.ts code-highlight.ts constants.ts offline-status.ts org-admin.ts presence-announcer.ts presence-color.ts presence-cursor.ts presence-editor-views.ts presence-emitter.ts query-client.ts realtime.ts search.ts types.ts utils.ts
stores/8
__tests__/1block-patch-store.test.ts
block-patch-store.ts comment-pill-store.ts database-store.ts editor-store.ts offlineQueue.ts presence-store.ts ui-store.ts
instrumentation.ts middleware.ts
.env.example Dockerfile next-env.d.ts next.config.mjs package.json postcss.config.mjs README.md tailwind.config.ts tsconfig.json tsconfig.tsbuildinfo vitest.config.ts
worker/92
src/87
__tests__/13ai-embedding.test.ts ai-request.test.ts ai-usage-budget.test.ts audit-export.test.ts email-decision.test.ts encrypt.test.ts health-search-index-lag.test.ts mime.test.ts retention.test.ts s3-sign.test.ts search-index.test.ts smtp.test.ts webhook-sign.test.ts
health/1server.ts
lib/12
__tests__/1clamav.test.ts
clamav.ts encrypt.ts job-otel.ts mime.ts notify.ts prisma.ts redis-stream.ts redis.ts s3.ts sharp.ts smtp.ts
processors/56
automations/28
__tests__/3condition-evaluator.spec.ts executors.spec.ts run-pipeline.spec.ts
executors/17assign-user.ts call-integration.ts change-status.ts classify.ts create-notification.ts create-page.ts email-cap.ts helpers.ts integration-registry.ts permission.ts record-write.ts registry.ts run-ai-action.ts send-email.ts send-webhook.ts types.ts update-record.ts
condition-evaluator.ts dead-letter.ts log.ts matcher.ts pipeline.ts run-state.ts schedule.ts watchdog.ts
files/15
__tests__/4errors.test.ts scan-gate.test.ts status.test.ts temp.test.ts
steps/4finalize.ts metadata.ts scan.ts thumbnails.ts
cleanup.ts errors.ts generateThumbs.ts processor.ts status.ts temp.ts types.ts
search-index/4builders.ts grants.ts index.ts processor.ts
ai-embedding.ts ai.processor.ts audit-export.ts automations.processor.ts email-decision.processor.ts email.processor.ts maintenance.processor.ts retention.ts webhooks.processor.ts
config.ts events.ts index.ts logger.ts queues.ts
.env.example Dockerfile package.json README.md tsconfig.json
docs/73
api/17ai.md authentication.md automations.md blocks.md comments.md databases.md files.md forms.md openapi.yaml org-admin.md pages.md scim.md search.md sso.md versioning.md webhooks.md workspaces.md
architecture/10ai-architecture.md automation.md backend-architecture.md database-architecture.md frontend-architecture.md realtime-architecture.md search-architecture.md security-architecture.md storage.md system-architecture.md
database/2database-architecture.md redis-keys.md
development/8admin-guide.md coding-standards.md deployment.md development-guide.md files-pipeline.md perf-report.md performance.md testing.md
product/4functional-requirements.md non-functional-requirements.md product-requirements.md ux-requirements.md
requirements/1requirements-matrix.md
security/5audit-report.md files.md forms.md license-allowlist.md security-architecture.md
tasks/16phase-00-foundation.md phase-01-authentication.md phase-02-workspace.md phase-03-pages.md phase-04-editor.md phase-05-databases.md phase-06-relations.md phase-07-collaboration.md phase-08-search.md phase-09-files.md phase-10-forms.md phase-11-automation.md phase-12-ai.md phase-13-api.md phase-14-enterprise.md phase-15-production.md
ui/9ai.md automations.md design-system.md developers.md files.md forms.md org-admin.md scim.md sso.md
support-matrix.md
infrastructure/71
caddy/1Caddyfile
docker/6
init/101-extensions.sql
opensearch/1init-indices.json
.env.docker docker-compose.yml opensearch-init.sh README.md
kubernetes/40
observability/20
alertmanager/1alertmanager.yaml
grafana/8
dashboards/6ai.json api-overview.json infra.json realtime.json search.json workers-queues.json
provisioning/2
dashboards/1dashboards.yaml
datasources/1datasource.yaml
prometheus/3alerts.yaml recording-rules.yaml scrape.yaml
scripts/1sync-sources.mjs
alertmanager.yaml grafana.yaml kustomization.yaml namespace.yaml node-exporter.yaml otel-collector.yaml prometheus.yaml
postgres/7configmap.yaml cronjob-backup.yaml cronjob-opensearch-snapshot.yaml pgbouncer.yaml replica.yaml statefulset.yaml walg-iam-policy.yaml
api.yaml configmap.yaml ingress.yaml kustomization.yaml namespace.yaml networkpolicy.yaml pdb.yaml README.md realtime.yaml redis.yaml secret.example.yaml web.yaml worker.yaml
nginx/2nginx.conf README.md
observability/14
alertmanager/1alertmanager.yaml
grafana/8
dashboards/6ai.json api-overview.json infra.json realtime.json search.json workers-queues.json
provisioning/2
dashboards/1dashboards.yaml
datasources/1datasource.yaml
prometheus/3alerts.yaml recording-rules.yaml scrape.yaml
scripts/1dashboard-completeness.mjs
README.md
terraform/8compute.tf data.tf main.tf network.tf outputs.tf README.md security.tf variables.tf
packages/289
ai/24
src/20
providers/7anthropic.ts google.ts http.ts index.ts openai-compatible.ts rag.test.ts writing.test.ts
schemas/2database.ts generate.ts
adapters.test.ts config.test.ts config.ts gateway.test.ts gateway.ts index.ts tools.test.ts tools.ts types.ts usage.test.ts usage.ts
package.json README.md tsconfig.json vitest.config.ts
auth/11
src/7index.ts passwords.test.ts passwords.ts sessions.test.ts sessions.ts tokens.test.ts tokens.ts
package.json README.md tsconfig.json vitest.config.ts
collaboration/17
src/13awareness.ts codec.test.ts codec.ts index.ts lww.test.ts lww.ts mapping.ts offline.ts outbox.test.ts outbox.ts provider.test.ts provider.ts types.ts
package.json README.md tsconfig.json vitest.config.ts
config/7
src/4eslint.base.js index.ts prettier.base.json tsconfig.base.json
package.json README.md tsconfig.json
database-client/5
src/2index.test.ts index.ts
package.json README.md tsconfig.json
database-engine/33
src/29
filter/2filter.test.ts index.ts
formula/8evaluator.ts formula.test.ts functions.ts index.ts lexer.ts parser.ts types.ts validate.ts
query/6group.ts index.ts paginate.ts pivot.ts query.test.ts sort.ts
relations/2index.ts relations.test.ts
rollups/2index.ts rollups.test.ts
values/5dates.ts index.ts registry.ts types.ts values.test.ts
views/3buttons.ts index.ts views.test.ts
index.ts
package.json README.md tsconfig.json vitest.config.ts
editor/25
src/21
marks/1highlight.ts
nodes/2equation.ts mention.ts
autosave.test.ts autosave.ts clipboard.test.ts clipboard.ts codec.test.ts codec.ts context-menu.test.ts context-menu.ts diff.test.ts diff.ts factory.ts history.test.ts history.ts index.ts positions.ts slash.test.ts slash.ts types.ts
package.json README.md tsconfig.json vitest.config.ts
observability/20
src/16
__tests__/8db-redis-metrics.test.ts metrics.test.ts otel.test.ts pino-otel.test.ts prometheus.test.ts resource.test.ts scrubber.test.ts traceparent.test.ts
index.ts metrics.ts otel.ts pino-otel.ts prometheus.ts resource.ts scrubber.ts traceparent.ts
package.json README.md tsconfig.json vitest.config.ts
permissions/11
src/7index.ts levels.test.ts levels.ts resolve.test.ts resolve.ts rules.test.ts rules.ts
package.json README.md tsconfig.json vitest.config.ts
search/22
src/18chunking.test.ts chunking.ts client.test.ts client.ts embeddings.test.ts embeddings.ts hybrid.test.ts hybrid.ts index.ts indexer.test.ts indexer.ts mapping.test.ts mapping.ts queries.test.ts queries.ts rrf.test.ts rrf.ts types.ts
package.json README.md tsconfig.json vitest.config.ts
shared/61
src/57
dto/17ai.dto.ts auth.dto.test.ts auth.dto.ts block.dto.ts browser-safety.test.ts comment.dto.ts database.dto.test.ts database.dto.ts file.dto.ts formula-ast.ts index.ts notification.dto.ts page.dto.ts search.dto.ts share.dto.ts webhook.dto.ts workspace.dto.ts
events/8audit-catalog.test.ts audit-catalog.ts audit-csv.test.ts audit-csv.ts audit-scope.test.ts audit-scope.ts catalog.test.ts catalog.ts
realtime/6comment-events.ts frames.test.ts frames.ts notification-policy.ts notification-prefs.ts task-0094.test.ts
automation.test.ts automation.ts constants.ts crypto.test.ts crypto.ts env.ts errors.ts events.test.ts events.ts forms.test.ts forms.ts index.ts markdown.test.ts markdown.ts mentions.test.ts mentions.ts net.test.ts net.ts positions.test.ts positions.ts presence.test.ts presence.ts security-headers.ts webhook-delivery.test.ts webhook-delivery.ts webhook-events.ts
package.json README.md tsconfig.json vitest.config.ts
storage/16
src/12client.test.ts client.ts index.ts keys.test.ts keys.ts mime.test.ts mime.ts presign.test.ts presign.ts thumbnails.test.ts thumbnails.ts types.ts
package.json README.md tsconfig.json vitest.config.ts
ui/37
src/33
components/19avatar.tsx badge.tsx button.tsx checkbox.tsx command.tsx context-menu.tsx dialog.tsx dropdown-menu.tsx empty-state.tsx index.ts popover.tsx separator.tsx skeleton.tsx slider.tsx spinner.tsx switch.tsx tabs.tsx toast.tsx tooltip.tsx
color.ts command-filter.test.ts command-filter.ts focus.test.ts focus.ts formatters.test.ts formatters.ts index.ts theme.test.ts theme.ts toast-store.test.ts toast-store.ts tokens.test.ts tokens.ts
package.json README.md tsconfig.json vitest.config.ts
prisma/16
migrations/13
20260927000000_init/1migration.sql
20260928000000_search_documents_content_hash/1migration.sql
20260929000000_workspace_usage_storage_bytes/1migration.sql
20260930000000_form_submitted_notification_type/1migration.sql
20261001000000_ai_phase12/1migration.sql
20261002000000_webhooks_secret_encrypted_dlq/1migration.sql
20261002111436_webhook_delivery_latency/1migration.sql
20261002120000_org_admin_2fa_retention_unique/1migration.sql
20261002215124_audit_export/1migration.sql
20261003210000_perf_indexes_hot_paths/1migration.sql
20261004004500_perf_indexes_hot_paths_2/1migration.sql
migration_lock.toml README.md
schema.prisma seed.ts verify.ts
scripts/7backup-health.ts docs-completeness.mjs generate-openapi.mjs minio-init.ts opensearch-reindex.ts storage-orphans.ts verify-clean-clone.sh
tests/169
e2e/63
.auth/1user.json
fixtures/7
files/6clip.mp4 clip.wav clip.webm hero-1400.png note.docx note.txt
realtime.ts
mocks/2oidc-issuer.ts saml-idp.ts
playwright-report/3
data/2a6557bae9c2714a658968d4eb8534195c08ebda8.png e3092d07d00a16063e46385c02a559a9054fa847.webm
index.html
reports/1files-security.json
specs/34ai-database.spec.ts ai-generate.spec.ts ai-rag.spec.ts ai-security.spec.ts ai-writing.spec.ts ai.spec.ts api-keys.spec.ts appearance-theme.spec.ts auth.spec.ts automation.spec.ts collab-two-tab.spec.ts comments.spec.ts databases.spec.ts default-workspace-alias.spec.ts developers.spec.ts editor.spec.ts files.spec.ts final-flow.spec.ts forms.spec.ts oauth.spec.ts org-admin.spec.ts pages.spec.ts performance.spec.ts presence-cursors.spec.ts qa-paste-toggle.spec.ts realtime.spec.ts scim.spec.ts search-index-lag.spec.ts search.spec.ts sharing.spec.ts sso.spec.ts task-0094-comments-notifications.spec.ts webhooks.spec.ts workspace.spec.ts
test-results-final-flow/3
final-flow-Release-v1-0-fi-6c06b-w-state-carries-sequential--chromium-desktop/2test-failed-1.png trace.zip
.last-run.json
utils/4api.ts dnd.ts search-lag.ts wait.ts
.gitignore fixtures.ts global-setup.ts package.json playwright.config.ts playwright.final-flow.config.ts README.md tsconfig.json
fixtures/1
files/1make-sparse.ts
integration/60
ai-capability/2ai-app.capability.test.ts gateway.capability.test.ts
drill/2seed-drill.mjs verify-restore.mjs
fixtures/5
files/5clip.mp4 clip.wav doc.pdf hero-1400.png note.txt
helpers/3api.ts containers.ts seed.ts
realtime/11control-channel.test.ts convergence.test.ts flush-rebase-http.test.ts handshake.test.ts heartbeat-limits.test.ts lifecycle.test.ts presence.test.ts rebase.test.ts resume.test.ts rt-helpers.ts task-0094-fanout.test.ts
ai-chat.integration.test.ts ai-database.integration.test.ts ai-generate.integration.test.ts ai-rag.integration.test.ts ai-writing.integration.test.ts api-keys.integration.test.ts audit-completeness.integration.test.ts audit-export-s3.integration.test.ts auth.integration.test.ts automation-pipeline.integration.test.ts automations.integration.test.ts blocks.integration.test.ts cache-invalidation.integration.test.ts comments.integration.test.ts databases.integration.test.ts files-lifecycle.integration.test.ts files-pipeline.integration.test.ts files-security.integration.test.ts files.integration.test.ts forms-responses.integration.test.ts forms.integration.test.ts oauth.integration.test.ts openapi.contract.test.ts org-admin.integration.test.ts package.json pages.integration.test.ts permissions.integration.test.ts README.md replica-routing.integration.test.ts retention-engine.integration.test.ts scim.integration.test.ts search.integration.test.ts sso.integration.test.ts tsconfig.json vitest.config.ts webhooks.integration.test.ts workspace.integration.test.ts
performance/28
fixtures/1search-100k.ts
helpers/2seed-100k.mjs seed-1k-load.mjs
reports/7api-load-smoke.json database-load-smoke.json editor-local.json realtime-load.json search-full.json search-last.json search-reduced.json
api-load.js api.js archive.tar database-load.js database.js editor-local.mjs extract-100k-env.mjs package.json README.md realtime-1k.js realtime-load.js realtime.js run-realtime-load.sh search-search.k6.js search-summary.json seed-100k.mjs threshold-breach-demo.js verify-zero-op-loss.mjs
security/16
helpers/1app.ts
audit-report.test.ts auth-paths.test.ts deps.test.ts formula-fuzz.test.ts gitleaks-config.test.ts headers.test.ts images.test.ts oracle.ts package.json permission-fuzz.test.ts rate-limits.test.ts README.md tsconfig.json upload-abuse.test.ts vitest.config.ts
README.md
.dockerignore .env .env.bak-task0125 .env.example .eslintrc.json .gitignore .npmrc .prettierignore .prettierrc.json AGENTS.md ARCHITECTURE.md CHANGELOG.md CONTRIBUTING.md gitleaks.toml LICENSE package.json pnpm-lock.yaml pnpm-workspace.yaml PRODUCT-SPEC.md README.md ROADMAP.md SECURITY.md tsconfig.json turbo.json
automations.service.tsapps/api/src/modules/automations/automations.service.tsraw← automations33,044 B← Back to the app
1/**
2 * TASK-0110 — automation service: CRUD, sub-resource upserts, status machine,
3 * manual runs, runs listing/detail/retry, and the incoming-webhook secret
4 * lifecycle (generate-once / KMS-wrapped / regenerate).
5 *
6 * Permission model (spec §1): automations are an EDIT capability —
7 * Owner/Admin always (FULL_ACCESS); Member with EDIT on the scoped database
8 * when `databaseId` is set. Enforcement via `assertCan` (server-side only).
9 *
10 * The BullMQ enqueue degrades best-effort (same pattern as the files
11 * pipeline): a queue outage logs a WARN and the run row stays PENDING —
12 * the TASK-0113 watchdog reclaims stale PENDING/RUNNING rows, so nothing is
13 * lost (documented in docs/architecture/automation.md).
14 */
15import { Prisma } from "@prisma/client";
16import { Inject, Injectable, Logger } from "@nestjs/common";
17import type { Request } from "express";
18import {
19  MANUAL_RUN_INPUT_MAX_BYTES,
20  RUN_INPUT_MAX_BYTES,
21  TERMINAL_RUN_STATUSES,
22  generateWebhookSecret,
23  isValidAutomationTransition,
24  truncateJson,
25  validateActionDto,
26  webhookSecretHash,
27  type AutomationStatus
28} from "@factory/shared";
29import { v4 as uuid } from "uuid";
30import { jobTraceMetadata } from "../../common/trace-metadata.js";
31import { EventBus } from "../../common/event-bus.js";
32import { ApiError } from "../../common/errors.js";
33import { RedisService } from "../../common/redis.service.js";
34import { AuditService } from "../../common/audit.service.js";
35import { assertCan } from "../../lib/authorize.js";
36import { PrismaService } from "../../common/prisma.service.js";
37import {
38  assertValidActions,
39  assertValidCondition,
40  assertValidTriggers,
41  type CreateAutomationInput,
42  type ListRunsQueryInput,
43  type PutActionsInput,
44  type PutConditionsInput,
45  type PutTriggersInput,
46  type RunInputInput,
47  type SetStatusInput,
48  type UpdateAutomationInput
49} from "./dto.js";
50import { aes256gcmDecrypt, aes256gcmEncrypt, keyFromSecret } from "../../lib/crypto.js";
51import { loadEnv } from "../../config/env.js";
52 
53type AutomationRow = {
54  id: string;
55  workspaceId: string;
56  name: string;
57  description: string | null;
58  databaseId: string | null;
59  status: string;
60  createdById: string;
61  createdAt: Date;
62  updatedAt: Date;
63  triggers: { id: string; type: string; config: unknown; position: number }[];
64  conditions: { id: string; expression: unknown; position: number }[];
65  actions: {
66    id: string;
67    type: string;
68    config: unknown;
69    position: number;
70    retries: number;
71    timeoutMs: number;
72  }[];
73};
74 
75/** Coerce an unknown value to a Prisma Json input (the API's DTOs hand us
76 * plain JS from Zod; Prisma's `InputJsonValue` type is narrower). */
77function toJson(v: unknown): Prisma.InputJsonValue {
78  return v as Prisma.InputJsonValue;
79}
80 
81@Injectable()
82export class AutomationsService {
83  private readonly log = new Logger(AutomationsService.name);
84 
85  constructor(
86    @Inject(PrismaService) private readonly prisma: PrismaService,
87    @Inject(RedisService) private readonly redis: RedisService,
88    @Inject(EventBus) private readonly events: EventBus,
89    @Inject(AuditService) private readonly audit: AuditService
90  ) {}
91 
92  // ---------------------------------------------------------------------
93  // Permission helpers
94  // ---------------------------------------------------------------------
95 
96  private async requireAutomation(id: string, userId: string, level: "VIEW" | "EDIT"): Promise<AutomationRow> {
97    const a = await this.prisma.automation.findUnique({
98      where: { id },
99      include: {
100        triggers: { orderBy: { position: "asc" } },
101        conditions: { orderBy: { position: "asc" } },
102        actions: { orderBy: { position: "asc" } }
103      }
104    });
105    if (!a) throw ApiError.notFound("Automation not found.");
106    const resource = a.databaseId ? { databaseId: a.databaseId } : undefined;
107    await assertCan(this.prisma, { workspaceId: a.workspaceId, userId, resource }, level);
108    return a;
109  }
110 
111  private async requireWorkspaceView(workspaceId: string, userId: string): Promise<void> {
112    await assertCan(this.prisma, { workspaceId, userId }, "VIEW");
113  }
114 
115  // ---------------------------------------------------------------------
116  // CRUD
117  // ---------------------------------------------------------------------
118 
119  async list(workspaceId: string, userId: string, query: { status?: string; cursor?: string; limit?: number }): Promise<{ data: unknown[]; nextCursor: string | null }> {
120    await this.requireWorkspaceView(workspaceId, userId);
121    const limit = Math.min(Math.max(query.limit ?? 50, 1), 200);
122    const cursor = query.cursor
123      ? (() => {
124          // Invalid cursor (bad base64url / non-JSON) → 400, not 500.
125          let c: { createdAt?: string; id?: string };
126          try {
127            c = JSON.parse(Buffer.from(query.cursor as string, "base64url").toString("utf8")) as { createdAt?: string; id?: string };
128          } catch {
129            throw ApiError.validation("invalid cursor");
130          }
131          if (!c || typeof c.createdAt !== "string" || typeof c.id !== "string") throw ApiError.validation("invalid cursor");
132          return { createdAt: c.createdAt, id: c.id };
133        })()
134      : undefined;
135 
136    const where: Record<string, unknown> = { workspaceId };
137    if (query.status) where.status = query.status;
138    if (cursor) {
139      where.OR = [
140        { createdAt: { lt: new Date(cursor.createdAt) } },
141        { createdAt: { equals: new Date(cursor.createdAt) }, id: { lt: cursor.id } }
142      ];
143    }
144 
145    const rows = await this.prisma.automation.findMany({
146      where: where as Prisma.AutomationWhereInput,
147      include: {
148        triggers: true,
149        conditions: true,
150        actions: true
151      },
152      orderBy: { createdAt: "desc" },
153      take: limit + 1
154    });
155    const more = rows.length > limit;
156    const page = rows.slice(0, limit);
157 
158    // Latest run per automation in this page (one query, grouped in JS).
159    const ids = page.map((p) => p.id);
160    const runs = ids.length
161      ? await this.prisma.automationRun.findMany({
162          where: { automationId: { in: ids } },
163          orderBy: { createdAt: "desc" },
164          select: { id: true, automationId: true, status: true, createdAt: true, finishedAt: true }
165        })
166      : [];
167    const latest = new Map<string, (typeof runs)[number]>();
168    for (const r of runs) if (!latest.has(r.automationId)) latest.set(r.automationId, r);
169 
170    return {
171      data: page.map((a) => ({
172        ...this.toSummary(a),
173        latestRun: latest.get(a.id) ?? null
174      })),
175      nextCursor: more && page.length
176        ? Buffer.from(JSON.stringify({ createdAt: page[page.length - 1]!.createdAt.toISOString(), id: page[page.length - 1]!.id })).toString("base64url")
177        : null
178    };
179  }
180 
181  async get(id: string, userId: string): Promise<unknown> {
182    const a = await this.requireAutomation(id, userId, "VIEW");
183    const [run] = await this.prisma.automationRun.findMany({
184      where: { automationId: id },
185      orderBy: { createdAt: "desc" },
186      take: 1,
187      select: { id: true, status: true, createdAt: true, finishedAt: true, retryCount: true, error: true }
188    });
189    return {
190      ...this.toDetail(a),
191      latestRun: run ?? null
192    };
193  }
194 
195  async create(workspaceId: string, userId: string, input: CreateAutomationInput, req: Request): Promise<unknown> {
196    // databaseId (optional) must exist in this workspace and not be archived.
197    if (input.databaseId) {
198      const db = await this.prisma.database.findUnique({ where: { id: input.databaseId } });
199      if (!db || db.workspaceId !== workspaceId) throw ApiError.unprocessable("databaseId must reference a database in this workspace", [{ field: "databaseId", issue: "unknown database" }]);
200      if (db.isArchived) throw ApiError.unprocessable("databaseId must reference an active database", [{ field: "databaseId", issue: "archived" }]);
201    }
202    await assertCan(this.prisma, { workspaceId, userId, resource: input.databaseId ? { databaseId: input.databaseId } : undefined }, "EDIT");
203 
204    // Validate optional inline sub-resources (same rules as the PUTs).
205    if (input.triggers) assertValidTriggers(input.triggers as PutTriggersInput["triggers"]);
206    if (input.condition !== undefined) assertValidCondition(input.condition);
207    if (input.actions) assertValidActions(input.actions as PutActionsInput["actions"]);
208 
209    const created = await this.prisma.$transaction(async (tx) => {
210      const a = await tx.automation.create({
211        data: { workspaceId, name: input.name, description: input.description ?? null, databaseId: input.databaseId ?? null, createdById: userId }
212      });
213      await this.writeSubResources(tx, a.id, input.triggers, input.condition, input.actions);
214      return a;
215    });
216    await this.audit.audit(req, req.auth, {
217      action: "automation.created",
218      resourceType: "automation",
219      resourceId: created.id,
220      workspaceId,
221      metadata: { name: created.name }
222    });
223    return this.get(created.id, userId);
224  }
225 
226  async update(id: string, userId: string, input: UpdateAutomationInput, req: Request): Promise<unknown> {
227    const a = await this.requireAutomation(id, userId, "EDIT");
228    if (input.databaseId !== undefined) {
229      if (input.databaseId === null) {
230        // un-scope — allowed.
231      } else {
232        const db = await this.prisma.database.findUnique({ where: { id: input.databaseId } });
233        if (!db || db.workspaceId !== a.workspaceId) throw ApiError.unprocessable("databaseId must reference a database in this workspace", [{ field: "databaseId", issue: "unknown database" }]);
234        if (db.isArchived) throw ApiError.unprocessable("databaseId must reference an active database", [{ field: "databaseId", issue: "archived" }]);
235        await assertCan(this.prisma, { workspaceId: a.workspaceId, userId, resource: { databaseId: input.databaseId } }, "EDIT");
236      }
237    }
238    await this.prisma.automation.update({
239      where: { id },
240      data: {
241        ...(input.name !== undefined ? { name: input.name } : {}),
242        ...(input.description !== undefined ? { description: input.description } : {}),
243        ...(input.databaseId !== undefined ? { databaseId: input.databaseId } : {})
244      }
245    });
246    await this.audit.audit(req, req.auth, {
247      action: "automation.updated",
248      resourceType: "automation",
249      resourceId: id,
250      workspaceId: a.workspaceId,
251      metadata: { changed: Object.keys(input) }
252    });
253    return this.get(id, userId);
254  }
255 
256  async setStatus(id: string, userId: string, to: SetStatusInput["status"], req: Request): Promise<unknown> {
257    const a = await this.requireAutomation(id, userId, "EDIT");
258    const from = a.status as AutomationStatus;
259    if (from === to) throw ApiError.conflict(`Automation is already ${from}.`, [{ reason: "no_op" }]);
260    if (!isValidAutomationTransition(from, to)) {
261      throw ApiError.conflict(`Invalid status transition ${from} → ${to}.`, [{ reason: "invalid_transition", from, to }]);
262    }
263    // Spec §2: the transition itself is the ONLY gate (6 valid pairs, 409
264    // otherwise). A DRAFT/ACTIVE automation without triggers simply matches
265    // nothing — the matcher skips it; no completeness gate here (recorded).
266    await this.prisma.automation.update({ where: { id }, data: { status: to } });
267    await this.audit.audit(req, req.auth, {
268      action: "automation.status_changed",
269      resourceType: "automation",
270      resourceId: id,
271      workspaceId: a.workspaceId,
272      metadata: { from, to }
273    });
274    this.log.log(`automation ${id} status ${from} → ${to}`);
275    return this.get(id, userId);
276  }
277 
278  /** DELETE → ARCHIVED (soft; runs retained for audit). */
279  async remove(id: string, userId: string, req: Request): Promise<void> {
280    const a = await this.requireAutomation(id, userId, "EDIT");
281    if (a.status === "ARCHIVED") return;
282    await this.prisma.automation.update({ where: { id }, data: { status: "ARCHIVED" } });
283    await this.audit.audit(req, req.auth, {
284      action: "automation.status_changed",
285      resourceType: "automation",
286      resourceId: id,
287      workspaceId: a.workspaceId,
288      metadata: { from: a.status, to: "ARCHIVED" }
289    });
290  }
291 
292  // ---------------------------------------------------------------------
293  // Sub-resource PUTs (editor contract — replace-set semantics)
294  // ---------------------------------------------------------------------
295 
296  async putTriggers(id: string, userId: string, body: PutTriggersInput, req: Request): Promise<unknown> {
297    const a = await this.requireAutomation(id, userId, "EDIT");
298    assertValidTriggers(body.triggers);
299    // WEBHOOK triggers: mint the one-time secret on FIRST create (the
300    // plaintext is returned exactly once, in this response). On RE-SAVE the
301    // client echoes back only the public `webhookId` (the KMS-wrapped secret
302    // is stripped on read and can never be sent back), so the stored
303    // `secretHash`/`secretEncrypted` must be preserved from the previous
304    // row — otherwise the editor's core operation (PUT triggers) would
305    // silently clobber the secret and break the live endpoint.
306    const storedByWebhookId = new Map<string, Record<string, unknown>>();
307    for (const t of a.triggers) if (t.type === "WEBHOOK") {
308      const wid = this.webhookIdOf(t.config);
309      if (wid) storedByWebhookId.set(String(wid), t.config as Record<string, unknown>);
310    }
311    const generated: { webhookId: string; secret: string }[] = [];
312    const triggers = body.triggers.map((t) => {
313      const config = { ...(t.config as Record<string, unknown>) } as Record<string, unknown>;
314      if (t.type === "WEBHOOK" && !config.webhookId) {
315        const secret = generateWebhookSecret();
316        config.webhookId = uuid().replace(/-/g, "").slice(0, 24);
317        config.secretHash = webhookSecretHash(secret);
318        config.secretEncrypted = this.wrapSecret(secret);
319        generated.push({ webhookId: String(config.webhookId), secret });
320      } else if (t.type === "WEBHOOK" && config.webhookId) {
321        const wid = String(config.webhookId);
322        const stored = storedByWebhookId.get(wid);
323        if (!stored) {
324          throw ApiError.validation("WEBHOOK trigger references an unknown webhookId — create a new webhook trigger instead.", [
325            { field: "triggers", issue: "unknown_webhook_id", received: wid }
326          ]);
327        }
328        // Preserve the server-managed secret material (never client-supplied).
329        config.secretHash = stored.secretHash;
330        config.secretEncrypted = stored.secretEncrypted;
331      }
332      return { type: t.type, config };
333    });
334 
335    await this.prisma.$transaction(async (tx) => {
336      await tx.automationTrigger.deleteMany({ where: { automationId: id } });
337      await tx.automationTrigger.createMany({
338        data: triggers.map((t, i) => ({ automationId: id, type: t.type, config: toJson(t.config), position: i }))
339      });
340    });
341    await this.audit.audit(req, req.auth, {
342      action: "automation.updated",
343      resourceType: "automation",
344      resourceId: id,
345      workspaceId: a.workspaceId,
346      metadata: { subresource: "triggers", count: triggers.length }
347    });
348    return {
349      automationId: id,
350      triggers: this.sanitizeTriggers(a.id, triggers),
351      webhooks: generated.map((g) => ({
352        webhookId: g.webhookId,
353        /** Plaintext — shown ONCE. It is KMS-wrapped at rest and never returned again. */
354        secret: g.secret,
355        url: `${loadEnv().WEBHOOK_BASE_URL}/webhooks/incoming/${g.webhookId}`
356      }))
357    };
358  }
359 
360  async putConditions(id: string, userId: string, body: PutConditionsInput, req: Request): Promise<unknown> {
361    const a = await this.requireAutomation(id, userId, "EDIT");
362    assertValidCondition(body.condition ?? null);
363    const tree = body.condition ?? null;
364    await this.prisma.$transaction(async (tx) => {
365      await tx.automationCondition.deleteMany({ where: { automationId: id } });
366      if (tree) await tx.automationCondition.create({ data: { automationId: id, expression: toJson(tree), position: 0 } });
367    });
368    await this.audit.audit(req, req.auth, {
369      action: "automation.updated",
370      resourceType: "automation",
371      resourceId: id,
372      workspaceId: a.workspaceId,
373      metadata: { subresource: "conditions", present: Boolean(tree) }
374    });
375    return { automationId: id, condition: tree };
376  }
377 
378  /**
379   * Shared `validateActionDto` + normalization: applies the schema defaults
380   * (e.g. UPDATE_RECORD.recordId → "from_trigger") so the stored row is
381   * self-contained for the worker (which re-validates but trusts the stored
382   * shape). The DTO layer has already 422'd invalid entries — this never
383   * throws for reachable input.
384   */
385  private normalizeActions(actions: PutActionsInput["actions"]) {
386    return actions.map((act) => {
387      const r = validateActionDto({ type: act.type as never, config: act.config, retries: act.retries, timeoutMs: act.timeoutMs });
388      if (!r.ok) throw ApiError.unprocessable(`Invalid action config.`, r.errors.map((e) => ({ field: e.path, issue: e.message })));
389      return r.normalized;
390    });
391  }
392 
393  async putActions(id: string, userId: string, body: PutActionsInput, req: Request): Promise<unknown> {
394    const a = await this.requireAutomation(id, userId, "EDIT");
395    assertValidActions(body.actions);
396    const normalized = this.normalizeActions(body.actions);
397    await this.prisma.$transaction(async (tx) => {
398      await tx.automationAction.deleteMany({ where: { automationId: id } });
399      await tx.automationAction.createMany({
400        data: normalized.map((act, i) => ({
401          automationId: id,
402          type: act.type,
403          config: toJson(act.config),
404          position: i,
405          retries: act.retries,
406          timeoutMs: act.timeoutMs
407        }))
408      });
409    });
410    await this.audit.audit(req, req.auth, {
411      action: "automation.updated",
412      resourceType: "automation",
413      resourceId: id,
414      workspaceId: a.workspaceId,
415      metadata: { subresource: "actions", count: body.actions.length }
416    });
417    return this.get(id, userId);
418  }
419 
420  // ---------------------------------------------------------------------
421  // Webhook secret lifecycle
422  // ---------------------------------------------------------------------
423 
424  /** Regenerate the webhook trigger's secret (invalidates the old one). */
425  async regenerateWebhookSecret(id: string, userId: string, req: Request): Promise<unknown> {
426    const a = await this.requireAutomation(id, userId, "EDIT");
427    const wt = a.triggers.find((t) => t.type === "WEBHOOK");
428    if (!wt) throw ApiError.notFound("Automation has no WEBHOOK trigger.");
429    const cfg = (wt.config ?? {}) as Record<string, unknown>;
430    const secret = generateWebhookSecret();
431    const next = {
432      ...cfg,
433      secretHash: webhookSecretHash(secret),
434      secretEncrypted: this.wrapSecret(secret)
435    };
436    await this.prisma.automationTrigger.update({ where: { id: wt.id }, data: { config: toJson(next) } });
437    await this.audit.audit(req, req.auth, {
438      action: "automation.webhook_secret_regenerated",
439      resourceType: "automation",
440      resourceId: id,
441      workspaceId: a.workspaceId,
442      metadata: { webhookId: cfg.webhookId }
443    });
444    return {
445      webhookId: String(cfg.webhookId),
446      secret,
447      url: `${loadEnv().WEBHOOK_BASE_URL}/webhooks/incoming/${cfg.webhookId}`
448    };
449  }
450 
451  /**
452   * KMS key for webhook secret wrapping (D5): `AUTOMATION_WEBHOOK_KMS_KEY`
453   * when set, else `MASTER_ENCRYPTION_KEY` (single-key deployments).
454   */
455  private kmsKey(): Buffer {
456    const env = loadEnv();
457    const key = env.AUTOMATION_WEBHOOK_KMS_KEY || env.MASTER_ENCRYPTION_KEY;
458    if (!key) throw new Error("AUTOMATION_WEBHOOK_KMS_KEY (or MASTER_ENCRYPTION_KEY) is required for WEBHOOK triggers");
459    return keyFromSecret(key);
460  }
461 
462  private wrapSecret(secret: string): string {
463    return aes256gcmEncrypt(secret, this.kmsKey());
464  }
465 
466  /** KMS-unwrap the stored secret (used by the incoming-webhook endpoint
467   * for signature verification). */
468  unwrapSecret(encrypted: string): string {
469    return aes256gcmDecrypt(encrypted, this.kmsKey());
470  }
471 
472  private webhookIdOf(config: unknown): string | undefined {
473    return config && typeof config === "object" ? (config as Record<string, unknown>).webhookId as string | undefined : undefined;
474  }
475 
476  /** Strip server-managed fields from trigger configs before returning them. */
477  private sanitizeTriggers(_automationId: string, triggers: { type: string; config: Record<string, unknown> }[]) {
478    return triggers.map((t) => {
479      const config = { ...t.config };
480      if (t.type === "WEBHOOK") {
481        delete config.secretEncrypted;
482        delete config.secretHash;
483        const wh = config.webhookId as string | undefined;
484        config.url = wh ? `${loadEnv().WEBHOOK_BASE_URL}/webhooks/incoming/${wh}` : undefined;
485      }
486      return { type: t.type, config };
487    });
488  }
489 
490  // ---------------------------------------------------------------------
491  // Manual run (202 { runId }) — DRAFT dry-run path included
492  // ---------------------------------------------------------------------
493 
494  async manualRun(id: string, userId: string, body: RunInputInput, req: Request): Promise<{ runId: string }> {
495    const a = await this.requireAutomation(id, userId, "EDIT");
496    if (a.actions.length === 0) throw ApiError.unprocessable("Cannot run: automation has no actions.", [{ reason: "no_actions" }]);
497    const triggerEvent: Record<string, unknown> = { type: "manual", actorId: userId };
498    if (body.input !== undefined) {
499      const { value, truncated } = truncateJson(body.input, MANUAL_RUN_INPUT_MAX_BYTES);
500      triggerEvent.input = value;
501      if (truncated) triggerEvent.inputTruncated = true;
502    }
503    const run = await this.prisma.automationRun.create({
504      data: {
505        workspaceId: a.workspaceId,
506        automationId: id,
507        status: "PENDING",
508        triggerEvent: toJson(triggerEvent),
509        input: toJson(truncateJson(triggerEvent, RUN_INPUT_MAX_BYTES).value),
510        retryCount: 0
511      }
512    });
513    await this.enqueueRun(run.id, id, a.workspaceId, triggerEvent);
514    await this.audit.audit(req, req.auth, {
515      action: "automation.manual_triggered",
516      resourceType: "automation",
517      resourceId: id,
518      workspaceId: a.workspaceId,
519      metadata: { runId: run.id }
520    });
521    return { runId: run.id };
522  }
523 
524  /**
525   * Enqueue `run-automation` on the `automations` BullMQ queue (TASK-0113
526   * job shape: `{ runId, automationId, workspaceId, triggerEvent, eventId }`).
527   * Best-effort: on queue failure the run stays PENDING and the watchdog
528   * reclaims it (documented degrade, same as the files pipeline).
529   */
530  private async enqueueRun(runId: string, automationId: string, workspaceId: string, triggerEvent: Record<string, unknown>): Promise<void> {
531    await this.enqueueRunPublic(runId, automationId, workspaceId, triggerEvent, uuid());
532  }
533 
534  /** Public variant (the incoming-webhook controller supplies its own eventId). */
535  async enqueueRunPublic(runId: string, automationId: string, workspaceId: string, triggerEvent: Record<string, unknown>, eventId: string): Promise<void> {
536    try {
537      const { Queue } = await import("bullmq");
538      const q = new Queue("automations", { connection: this.redis.getRedis().options as never });
539      await q.add("run-automation", { runId, automationId, workspaceId, triggerEvent, eventId }, {
540        removeOnComplete: 1000,
541        removeOnFail: 500,
542        ...jobTraceMetadata()
543      });
544      await q.close();
545    } catch (err) {
546      this.log.warn(`automation run ${runId} enqueue failed (run stays PENDING; watchdog reclaims): ${(err as Error).message}`);
547    }
548  }
549 
550  /**
551   * Create a PENDING run for a webhook trigger (the incoming controller
552   * already verified signature + rate limit). `input` is the truncated
553   * triggerEvent for run storage.
554   */
555  async createIncomingRun(opts: {
556    workspaceId: string;
557    automationId: string;
558    triggerEvent: Record<string, unknown>;
559    input: unknown;
560  }): Promise<{ id: string }> {
561    const run = await this.prisma.automationRun.create({
562      data: {
563        workspaceId: opts.workspaceId,
564        automationId: opts.automationId,
565        status: "PENDING",
566        triggerEvent: toJson(opts.triggerEvent),
567        input: toJson(opts.input),
568        retryCount: 0
569      }
570    });
571    return { id: run.id };
572  }
573 
574  /** Minimal webhook hit log (no cookie/auth/signature values ever). */
575  logWebhookHit(webhookId: string, automationId: string, runId: string, safeHeaders: Record<string, string>): void {
576    this.log.log(`webhook ${webhookId} → automation ${automationId} → run ${runId} headers=${JSON.stringify(safeHeaders)}`);
577  }
578 
579  // ---------------------------------------------------------------------
580  // Runs (list / detail / retry) — the TASK-0114 history UI contract
581  // ---------------------------------------------------------------------
582 
583  async listRuns(
584    id: string,
585    userId: string,
586    query: ListRunsQueryInput
587  ): Promise<{ data: unknown[]; nextCursor: string | null }> {
588    const a = await this.requireAutomation(id, userId, "EDIT");
589    const limit = query.limit ?? 50;
590    const where: Record<string, unknown> = { automationId: id, ...(query.status ? { status: query.status } : {}) };
591    if (query.cursor) {
592      // Invalid cursor (bad base64url / non-JSON) → 400, not 500.
593      let c: { createdAt?: string; id?: string };
594      try {
595        c = JSON.parse(Buffer.from(query.cursor, "base64url").toString("utf8")) as { createdAt?: string; id?: string };
596      } catch {
597        throw ApiError.validation("invalid cursor");
598      }
599      if (!c || typeof c.createdAt !== "string" || typeof c.id !== "string") throw ApiError.validation("invalid cursor");
600      where.OR = [
601        { createdAt: { lt: new Date(c.createdAt) } },
602        { createdAt: { equals: new Date(c.createdAt) }, id: { lt: c.id } }
603      ];
604    }
605    const rows = await this.prisma.automationRun.findMany({
606      where: where as Prisma.AutomationRunWhereInput,
607      orderBy: { createdAt: "desc" },
608      take: limit + 1,
609      select: { id: true, status: true, triggerEvent: true, startedAt: true, finishedAt: true, retryCount: true, error: true, createdAt: true }
610    });
611    const more = rows.length > limit;
612    const page = rows.slice(0, limit);
613    return {
614      data: page.map((r) => this.toRunSummary(r, a.workspaceId)),
615      nextCursor:
616        more && page.length
617          ? Buffer.from(JSON.stringify({ createdAt: page[page.length - 1]!.createdAt.toISOString(), id: page[page.length - 1]!.id })).toString("base64url")
618          : null
619    };
620  }
621 
622  async getRun(id: string, runId: string, userId: string): Promise<unknown> {
623    const a = await this.requireAutomation(id, userId, "EDIT");
624    const run = await this.prisma.automationRun.findFirst({
625      where: { id: runId, automationId: id },
626      select: {
627        id: true,
628        status: true,
629        triggerEvent: true,
630        input: true,
631        output: true,
632        error: true,
633        startedAt: true,
634        finishedAt: true,
635        retryCount: true,
636        createdAt: true,
637        logs: { orderBy: { createdAt: "asc" }, select: { level: true, message: true, data: true, createdAt: true } }
638      }
639    });
640    if (!run) throw ApiError.notFound("Run not found.");
641    return {
642      ...this.toRunSummary(run as never, a.workspaceId),
643      input: run.input,
644      output: run.output,
645      logs: run.logs
646    };
647  }
648 
649  /** Retry a FAILED/DEAD_LETTER run: reset retryCount, re-enqueue (spec §4e). */
650  async retryRun(id: string, runId: string, userId: string, req: Request): Promise<{ runId: string; status: string }> {
651    const a = await this.requireAutomation(id, userId, "EDIT");
652    const run = await this.prisma.automationRun.findFirst({ where: { id: runId, automationId: id } });
653    if (!run) throw ApiError.notFound("Run not found.");
654    if (!(TERMINAL_RUN_STATUSES as readonly string[]).includes(run.status) || run.status === "SUCCEEDED") {
655      throw ApiError.conflict(`Only FAILED/DEAD_LETTER runs can be retried (run is ${run.status}).`, [{ reason: "not_retryable", status: run.status }]);
656    }
657    await this.prisma.automationRun.update({
658      where: { id: run.id },
659      data: { status: "PENDING", retryCount: 0, startedAt: null, finishedAt: null, error: Prisma.JsonNull }
660    });
661    const te = run.triggerEvent as Record<string, unknown> | null;
662    await this.enqueueRun(run.id, id, a.workspaceId, te ?? { type: "manual" });
663    await this.audit.audit(req, req.auth, {
664      action: "automation.run_retried",
665      resourceType: "automation",
666      resourceId: id,
667      workspaceId: a.workspaceId,
668      metadata: { runId: run.id }
669    });
670    return { runId: run.id, status: "PENDING" };
671  }
672 
673  // ---------------------------------------------------------------------
674  // DTO shaping
675  // ---------------------------------------------------------------------
676 
677  private toSummary(a: {
678    id: string;
679    name: string;
680    description: string | null;
681    databaseId: string | null;
682    status: string;
683    createdAt: Date;
684    updatedAt: Date;
685    createdById: string;
686    triggers: { type: string }[];
687    conditions: unknown[];
688    actions: { type: string }[];
689  }) {
690    return {
691      id: a.id,
692      name: a.name,
693      description: a.description,
694      databaseId: a.databaseId,
695      status: a.status,
696      triggers: a.triggers.map((t) => t.type),
697      hasCondition: a.conditions.length > 0,
698      actions: a.actions.map((t) => t.type),
699      createdAt: a.createdAt,
700      updatedAt: a.updatedAt,
701      createdById: a.createdById
702    };
703  }
704 
705  private toDetail(a: AutomationRow) {
706    return {
707      ...this.toSummary(a),
708      triggers: this.sanitizeTriggers(a.id, a.triggers.map((t) => ({ type: t.type, config: (t.config ?? {}) as Record<string, unknown> }))),
709      condition: a.conditions[0]?.expression ?? null,
710      actions: a.actions.map((act) => ({
711        type: act.type,
712        config: act.config,
713        retries: act.retries,
714        timeoutMs: act.timeoutMs
715      }))
716    };
717  }
718 
719  private toRunSummary(
720    r: {
721      id: string;
722      status: string;
723      triggerEvent: unknown;
724      startedAt: Date | null;
725      finishedAt: Date | null;
726      retryCount: number;
727      error: unknown;
728      createdAt: Date;
729    },
730    _workspaceId: string
731  ) {
732    const te = (r.triggerEvent ?? {}) as Record<string, unknown>;
733    return {
734      id: r.id,
735      status: r.status,
736      triggerType: typeof te.type === "string" ? te.type : null,
737      startedAt: r.startedAt,
738      finishedAt: r.finishedAt,
739      retryCount: r.retryCount,
740      error: r.error ? truncateJson(r.error, 8 * 1024).value : null,
741      createdAt: r.createdAt
742    };
743  }
744 
745  /** Write optional inline sub-resources (create flow). */
746  private async writeSubResources(
747    tx: Prisma.TransactionClient,
748    automationId: string,
749    triggers?: PutTriggersInput["triggers"],
750    condition?: unknown,
751    actions?: PutActionsInput["actions"]
752  ): Promise<void> {
753    if (triggers && triggers.length) {
754      const normalized = triggers.map((t) => {
755        const config = { ...(t.config ?? {}) } as Record<string, unknown>;
756        if (t.type === "WEBHOOK" && !config.webhookId) {
757          const secret = generateWebhookSecret();
758          config.webhookId = uuid().replace(/-/g, "").slice(0, 24);
759          config.secretHash = webhookSecretHash(secret);
760          config.secretEncrypted = this.wrapSecret(secret);
761        }
762        return { type: t.type, config };
763      });
764      await tx.automationTrigger.createMany({
765        data: normalized.map((t, i) => ({ automationId, type: t.type, config: toJson(t.config), position: i }))
766      });
767    }
768    if (condition !== undefined && condition !== null) {
769      await tx.automationCondition.create({ data: { automationId, expression: toJson(condition), position: 0 } });
770    }
771    if (actions && actions.length) {
772      // Same normalization as the PUT (schema defaults persisted, not
773      // re-derived at read time).
774      const normalized = this.normalizeActions(actions);
775      await tx.automationAction.createMany({
776        data: normalized.map((act, i) => ({
777          automationId,
778          type: act.type,
779          config: toJson(act.config),
780          position: i,
781          retries: act.retries,
782          timeoutMs: act.timeoutMs
783        }))
784      });
785    }
786  }
787 
788  /** Exposed for the incoming-webhook controller (validation only — the
789   * controller owns the HTTP concerns). */
790  findActiveWebhook(webhookId: string) {
791    return this.prisma.automationTrigger.findFirst({
792      where: {
793        type: "WEBHOOK",
794        config: { path: ["webhookId"], equals: webhookId },
795        automation: { status: "ACTIVE", workspace: { deletedAt: null } }
796      },
797      include: {
798        automation: { select: { id: true, name: true, workspaceId: true, status: true, createdById: true } }
799      }
800    });
801  }
802}
803