@egbe-ai/opencode 0.0.0-stage → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +4 -0
- package/README.md +124 -2
- package/index.js +5 -0
- package/package.json +55 -4
- package/plugin.js +230 -0
- package/session-log.js +315 -0
- package/skills/.egbe-catalog/manifest.json +1 -0
- package/skills/egbe-os-analytics/SKILL.md +499 -0
- package/skills/egbe-os-analytics/canonical-events.v1.json +256 -0
- package/skills/egbe-os-analytics/references/session-funnel.md +130 -0
- package/skills/egbe-os-analytics/references/workflows/activation-tracking.md +147 -0
- package/skills/egbe-os-analytics/references/workflows/landing-click-tracking.md +209 -0
- package/skills/egbe-os-analytics/references/workflows/payment-lifecycle-tracking.md +122 -0
- package/skills/egbe-os-analytics/references/workflows/product-funnel-tracking.md +77 -0
- package/skills/egbe-os-analytics/references/workflows/signup-lifecycle-tracking.md +208 -0
- package/skills/egbe-os-analytics/scripts/umami_funnel.py +927 -0
- package/skills/egbe-os-apollo-lead-finder/SKILL.md +135 -0
- package/skills/egbe-os-apollo-lead-finder/references/operations.md +240 -0
- package/skills/egbe-os-apollo-lead-finder/scripts/enrich.js +6 -0
- package/skills/egbe-os-apollo-lead-finder/scripts/enrich.sh +4 -0
- package/skills/egbe-os-apollo-lead-finder/scripts/find-leads.js +6 -0
- package/skills/egbe-os-apollo-lead-finder/scripts/find-leads.sh +4 -0
- package/skills/egbe-os-apollo-lead-finder/scripts/lib/apollo-client.js +343 -0
- package/skills/egbe-os-apollo-lead-finder/scripts/lib/connection-identity.js +17 -0
- package/skills/egbe-os-apollo-lead-finder/scripts/lib/helpers.js +127 -0
- package/skills/egbe-os-apollo-lead-finder/scripts/lib/places-filters.js +217 -0
- package/skills/egbe-os-apollo-lead-finder/scripts/lib/sourcing.js +153 -0
- package/skills/egbe-os-apollo-lead-finder/scripts/places-leads.js +653 -0
- package/skills/egbe-os-apollo-lead-finder/scripts/places-leads.sh +4 -0
- package/skills/egbe-os-apollo-lead-finder/scripts/search.js +6 -0
- package/skills/egbe-os-apollo-lead-finder/scripts/search.sh +4 -0
- package/skills/egbe-os-campaign-landing-page/SKILL.md +95 -0
- package/skills/egbe-os-campaign-landing-page/agents/openai.yaml +4 -0
- package/skills/egbe-os-campaign-landing-page/references/leads-calendar-and-tracking.md +66 -0
- package/skills/egbe-os-campaign-landing-page/references/os-wiring.md +58 -0
- package/skills/egbe-os-campaign-landing-page/references/qa.md +40 -0
- package/skills/egbe-os-campaign-landing-page/references/routing.md +32 -0
- package/skills/egbe-os-db/SKILL.md +174 -0
- package/skills/egbe-os-deploy/SKILL.md +412 -0
- package/skills/egbe-os-design/SKILL.md +182 -0
- package/skills/egbe-os-design-landing-page/SKILL.md +117 -0
- package/skills/egbe-os-design-landing-page/agents/openai.yaml +4 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/.dockerignore +11 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/AGENTS.md +31 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/Dockerfile +21 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/eslint.config.mjs +18 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/next.config.ts +11 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/package.json +36 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/postcss.config.mjs +7 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/public/.gitkeep +1 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/sentry.edge.config.ts +13 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/sentry.server.config.ts +13 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/src/app/global-error.tsx +19 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/src/app/globals.css +22 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/src/app/layout.tsx +21 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/src/app/page.tsx +3 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/src/app/privacy/page.tsx +28 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/src/components/egbe-analytics.tsx +39 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/src/instrumentation-client.ts +15 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/src/instrumentation.ts +13 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/src/lib/egbe-observability.generated.ts +20 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/src/lib/utils.ts +6 -0
- package/skills/egbe-os-design-landing-page/assets/scaffold/tsconfig.json +34 -0
- package/skills/egbe-os-design-landing-page/references/application-preparation.md +44 -0
- package/skills/egbe-os-design-landing-page/references/page-inputs.md +22 -0
- package/skills/egbe-os-destination-readiness/SKILL.md +132 -0
- package/skills/egbe-os-destination-readiness/references/checks.md +130 -0
- package/skills/egbe-os-destination-readiness/references/network-requirements.md +34 -0
- package/skills/egbe-os-destination-readiness/references/report.md +116 -0
- package/skills/egbe-os-domains/SKILL.md +189 -0
- package/skills/egbe-os-errors/SKILL.md +259 -0
- package/skills/egbe-os-git/SKILL.md +275 -0
- package/skills/egbe-os-git/references/issues.md +33 -0
- package/skills/egbe-os-google-ads/SKILL.md +163 -0
- package/skills/egbe-os-google-ads/references/artifacts.md +133 -0
- package/skills/egbe-os-google-ads/references/campaigns.md +157 -0
- package/skills/egbe-os-google-ads/references/conversions.md +153 -0
- package/skills/egbe-os-google-ads/references/delivery.md +128 -0
- package/skills/egbe-os-google-ads/references/keyword-ideas.md +40 -0
- package/skills/egbe-os-google-ads/references/reporting.md +105 -0
- package/skills/egbe-os-google-ads/references/search-assets.md +219 -0
- package/skills/egbe-os-google-ads/references/server-conversions.md +119 -0
- package/skills/egbe-os-google-auth/SKILL.md +285 -0
- package/skills/egbe-os-google-auth/agents/openai.yaml +4 -0
- package/skills/egbe-os-gtm-knowledge/SKILL.md +151 -0
- package/skills/egbe-os-images/SKILL.md +163 -0
- package/skills/egbe-os-images/references/stock-images.md +21 -0
- package/skills/egbe-os-instantly-campaign-launcher/SKILL.md +127 -0
- package/skills/egbe-os-instantly-campaign-launcher/references/instantly-ops.md +260 -0
- package/skills/egbe-os-instantly-campaign-launcher/references/observation-and-scheduling.md +188 -0
- package/skills/egbe-os-instantly-campaign-launcher/references/reply-handling.md +116 -0
- package/skills/egbe-os-instantly-campaign-launcher/references/run-spec-template.json +65 -0
- package/skills/egbe-os-instantly-campaign-launcher/references/run-state.md +145 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/conclude-campaign.js +158 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/install-crons.js +956 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/launch-campaign.js +2008 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/lib/campaign-metrics.js +124 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/lib/confirmed-opt-outs.js +208 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/lib/connection-identity.js +17 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/lib/create-once.js +23 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/lib/gtm-identity.js +125 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/lib/helpers.js +545 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/lib/html-body.js +266 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/lib/instantly-client.js +328 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/lib/outreach-db.js +370 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/lib/recipient-exclusion/index.js +86 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/lib/replenish-test-harness.js +53 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/lib/reply-queue.js +67 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/lib/run-config.js +103 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/lib/sender-selection.js +105 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/monitor-campaign.js +607 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/outreach-report.js +147 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/replenish-leads.js +619 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/reply-worker.js +258 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/run-cron-job.js +32 -0
- package/skills/egbe-os-instantly-campaign-launcher/scripts/send-reply.js +260 -0
- package/skills/egbe-os-linkedin-ads/SKILL.md +114 -0
- package/skills/egbe-os-linkedin-ads/references/destination-requirements.md +30 -0
- package/skills/egbe-os-linkedin-ads/references/runtime/api-reference.md +154 -0
- package/skills/egbe-os-linkedin-ads/references/runtime/artifacts.md +63 -0
- package/skills/egbe-os-linkedin-ads/references/workflows/build-paused.md +44 -0
- package/skills/egbe-os-linkedin-ads/references/workflows/conversions.md +88 -0
- package/skills/egbe-os-linkedin-ads/references/workflows/launch-and-pause.md +49 -0
- package/skills/egbe-os-linkedin-ads/references/workflows/monitoring.md +35 -0
- package/skills/egbe-os-linkedin-ads/references/workflows/readiness.md +63 -0
- package/skills/egbe-os-linkedin-ads/references/workflows/targeting-and-creative.md +52 -0
- package/skills/egbe-os-llm/SKILL.md +160 -0
- package/skills/egbe-os-mail/SKILL.md +180 -0
- package/skills/egbe-os-me/SKILL.md +119 -0
- package/skills/egbe-os-meta-ads/SKILL.md +114 -0
- package/skills/egbe-os-meta-ads/references/creative/creative-specs.md +15 -0
- package/skills/egbe-os-meta-ads/references/creative/talking-head.md +35 -0
- package/skills/egbe-os-meta-ads/references/destination-requirements.md +30 -0
- package/skills/egbe-os-meta-ads/references/runtime/api-reference.md +436 -0
- package/skills/egbe-os-meta-ads/references/runtime/artifacts.md +51 -0
- package/skills/egbe-os-meta-ads/references/runtime/insights-reference.md +187 -0
- package/skills/egbe-os-meta-ads/references/runtime/pixel-install.md +196 -0
- package/skills/egbe-os-meta-ads/references/runtime/shared-analytics-attribution.md +16 -0
- package/skills/egbe-os-meta-ads/references/workflows/build-paused.md +36 -0
- package/skills/egbe-os-meta-ads/references/workflows/launch-ready.md +16 -0
- package/skills/egbe-os-meta-ads/references/workflows/launch.md +23 -0
- package/skills/egbe-os-meta-ads/references/workflows/monitoring.md +72 -0
- package/skills/egbe-os-meta-ads/references/workflows/pause.md +17 -0
- package/skills/egbe-os-meta-ads/references/workflows/recovery.md +23 -0
- package/skills/egbe-os-meta-ads/scripts/check-pixel.py +229 -0
- package/skills/egbe-os-meta-ads/scripts/validate-meta-artifacts.py +187 -0
- package/skills/egbe-os-notifications/SKILL.md +78 -0
- package/skills/egbe-os-openai-ads/SKILL.md +149 -0
- package/skills/egbe-os-openai-ads/references/creative/chat-card.md +51 -0
- package/skills/egbe-os-openai-ads/references/destination-requirements.md +29 -0
- package/skills/egbe-os-openai-ads/references/runtime/api-reference.md +219 -0
- package/skills/egbe-os-openai-ads/references/runtime/artifacts.md +169 -0
- package/skills/egbe-os-openai-ads/references/workflows/build-paused.md +40 -0
- package/skills/egbe-os-openai-ads/references/workflows/conversions.md +40 -0
- package/skills/egbe-os-openai-ads/references/workflows/creative-assembly.md +24 -0
- package/skills/egbe-os-openai-ads/references/workflows/launch.md +27 -0
- package/skills/egbe-os-openai-ads/references/workflows/monitoring.md +54 -0
- package/skills/egbe-os-openai-ads/references/workflows/pause.md +22 -0
- package/skills/egbe-os-openai-ads/references/workflows/readiness.md +37 -0
- package/skills/egbe-os-openai-ads/references/workflows/tracking.md +32 -0
- package/skills/egbe-os-openai-ads/scripts/validate-openai-artifacts.py +271 -0
- package/skills/egbe-os-organic-trend-watch/SKILL.md +139 -0
- package/skills/egbe-os-organic-trend-watch/agents/openai.yaml +4 -0
- package/skills/egbe-os-organic-trend-watch/references/analysis-and-adaptation.md +54 -0
- package/skills/egbe-os-organic-trend-watch/references/collection-and-analysis.md +117 -0
- package/skills/egbe-os-organic-trend-watch/references/output-contract.md +133 -0
- package/skills/egbe-os-organic-trend-watch/references/provider-contract.md +169 -0
- package/skills/egbe-os-organic-trend-watch/references/runtime-contract.md +204 -0
- package/skills/egbe-os-organic-trend-watch/references/visual-analysis-prompt.md +17 -0
- package/skills/egbe-os-organic-trend-watch/scripts/_analysis.py +612 -0
- package/skills/egbe-os-organic-trend-watch/scripts/_contract.py +1133 -0
- package/skills/egbe-os-organic-trend-watch/scripts/_provider_inputs.py +165 -0
- package/skills/egbe-os-organic-trend-watch/scripts/analyze-visual-evidence.py +315 -0
- package/skills/egbe-os-organic-trend-watch/scripts/build-provider-inputs.py +88 -0
- package/skills/egbe-os-organic-trend-watch/scripts/connection_identity.py +22 -0
- package/skills/egbe-os-organic-trend-watch/scripts/egbe_artifacts.py +24 -0
- package/skills/egbe-os-organic-trend-watch/scripts/finalize-receipt.py +106 -0
- package/skills/egbe-os-organic-trend-watch/scripts/normalize-videos.py +647 -0
- package/skills/egbe-os-organic-trend-watch/scripts/prepare-visual-evidence.py +324 -0
- package/skills/egbe-os-organic-trend-watch/scripts/render-result.py +30 -0
- package/skills/egbe-os-organic-trend-watch/scripts/run-apify.py +723 -0
- package/skills/egbe-os-organic-trend-watch/scripts/run-state.py +192 -0
- package/skills/egbe-os-organic-trend-watch/scripts/validate-context.py +27 -0
- package/skills/egbe-os-organic-trend-watch/scripts/validate-runtime.py +137 -0
- package/skills/egbe-os-payments/SKILL.md +215 -0
- package/skills/egbe-os-payments/agents/openai.yaml +4 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/.egbe/scaffolds/billing-nextjs-subscription.v1.json +5 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/AGENTS.append.md +13 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/db/migrations/1000_egbe_billing.sql +66 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/package.fragment.json +12 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/scripts/billing-sync.mjs +162 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/app/account/billing/error.tsx +11 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/app/account/billing/loading.tsx +8 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/app/account/billing/page.tsx +121 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/app/api/billing/checkout/route.ts +102 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/app/api/billing/portal/route.ts +54 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/app/api/billing/status/route.ts +22 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/app/api/payments/webhook/route.ts +65 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/components/egbe-billing/plan-badge.tsx +16 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/lib/egbe-billing/billing-sync.test.mjs +58 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/lib/egbe-billing/catalog.ts +82 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/lib/egbe-billing/checkout-payload.ts +47 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/lib/egbe-billing/foundation-contract.test.mjs +59 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/lib/egbe-billing/lifecycle.d.ts +16 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/lib/egbe-billing/lifecycle.mjs +82 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/lib/egbe-billing/lifecycle.test.mjs +94 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/lib/egbe-billing/os-client.ts +96 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/lib/egbe-billing/plans.json +17 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/lib/egbe-billing/repository.ts +325 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/lib/egbe-billing/signature.d.ts +6 -0
- package/skills/egbe-os-payments/assets/scaffolds/billing-nextjs-subscription/src/lib/egbe-billing/signature.mjs +22 -0
- package/skills/egbe-os-payments/references/contracts/plan-catalog.md +46 -0
- package/skills/egbe-os-payments/references/security/durable-verification.md +64 -0
- package/skills/egbe-os-payments/references/security/server-integration.md +26 -0
- package/skills/egbe-os-payments/references/security/webhook-and-entitlements.md +152 -0
- package/skills/egbe-os-payments/references/ux/billing-experience.md +47 -0
- package/skills/egbe-os-payments/references/workflows/account-billing.md +64 -0
- package/skills/egbe-os-payments/references/workflows/discounts-and-promotion-codes.md +265 -0
- package/skills/egbe-os-payments/references/workflows/one-time-purchase.md +75 -0
- package/skills/egbe-os-payments/references/workflows/product-checkout.md +355 -0
- package/skills/egbe-os-payments/references/workflows/subscription-lifecycle.md +55 -0
- package/skills/egbe-os-playbook/SKILL.md +79 -0
- package/skills/egbe-os-playbook/playbooks/market-and-acquisition/channels/email-outreach.md +91 -0
- package/skills/egbe-os-playbook/playbooks/market-and-acquisition/channels/google-search.md +40 -0
- package/skills/egbe-os-playbook/playbooks/market-and-acquisition/channels/linkedin.md +24 -0
- package/skills/egbe-os-playbook/playbooks/market-and-acquisition/channels/meta.md +23 -0
- package/skills/egbe-os-playbook/playbooks/market-and-acquisition/channels/openai.md +15 -0
- package/skills/egbe-os-playbook/playbooks/market-and-acquisition/channels/reddit.md +56 -0
- package/skills/egbe-os-playbook/playbooks/market-and-acquisition/choose-test.md +73 -0
- package/skills/egbe-os-playbook/playbooks/market-and-acquisition/evaluate.md +91 -0
- package/skills/egbe-os-playbook/playbooks/market-and-acquisition/market.md +96 -0
- package/skills/egbe-os-playbook/playbooks/market-and-acquisition/run-test.md +111 -0
- package/skills/egbe-os-playbook/playbooks/market-and-acquisition.md +56 -0
- package/skills/egbe-os-playbook/playbooks/product/business-loop.md +141 -0
- package/skills/egbe-os-playbook/playbooks/product/collaboration.md +26 -0
- package/skills/egbe-os-playbook/playbooks/product/deliver.md +75 -0
- package/skills/egbe-os-playbook/playbooks/product/diagnose.md +80 -0
- package/skills/egbe-os-playbook/playbooks/product/evaluate.md +67 -0
- package/skills/egbe-os-playbook/playbooks/product/experience.md +132 -0
- package/skills/egbe-os-playbook/playbooks/product/loop-examples.md +67 -0
- package/skills/egbe-os-playbook/playbooks/product/offer.md +65 -0
- package/skills/egbe-os-playbook/playbooks/product/value.md +91 -0
- package/skills/egbe-os-playbook/playbooks/product.md +67 -0
- package/skills/egbe-os-playbook/references/catalog.md +127 -0
- package/skills/egbe-os-playbook/references/changes.md +167 -0
- package/skills/egbe-os-playbook/scripts/playbook-changes.py +6 -0
- package/skills/egbe-os-playbook/scripts/playbook_changes/__init__.py +1 -0
- package/skills/egbe-os-playbook/scripts/playbook_changes/cli.py +57 -0
- package/skills/egbe-os-playbook/scripts/playbook_changes/operations.py +137 -0
- package/skills/egbe-os-playbook/scripts/playbook_changes/protocol.py +132 -0
- package/skills/egbe-os-playbook/scripts/playbook_changes/storage.py +180 -0
- package/skills/egbe-os-playbook/scripts/playbook_changes/transport.py +113 -0
- package/skills/egbe-os-product-auth/SKILL.md +151 -0
- package/skills/egbe-os-product-auth/agents/openai.yaml +4 -0
- package/skills/egbe-os-product-auth/assets/db/README.md +102 -0
- package/skills/egbe-os-product-auth/assets/db/create-validation-session.mjs +223 -0
- package/skills/egbe-os-product-auth/assets/db/migrate.mjs +64 -0
- package/skills/egbe-os-product-auth/assets/db/migrations/0001_auth.sql +33 -0
- package/skills/egbe-os-product-auth/assets/db/migrations/0002_auth_identities.sql +16 -0
- package/skills/egbe-os-product-auth/assets/scaffolds/product-auth-database/AGENTS.append.md +17 -0
- package/skills/egbe-os-product-auth/assets/scaffolds/product-auth-database/package.fragment.json +10 -0
- package/skills/egbe-os-product-auth/assets/scaffolds/product-nextjs-postgres/AGENTS.append.md +9 -0
- package/skills/egbe-os-product-auth/assets/scaffolds/product-nextjs-postgres/package.fragment.json +13 -0
- package/skills/egbe-os-product-auth/assets/scaffolds/product-nextjs-postgres/src/lib/egbe-product/db.ts +31 -0
- package/skills/egbe-os-product-auth/assets/scaffolds/product-nextjs-postgres/src/lib/egbe-product/identity.ts +94 -0
- package/skills/egbe-os-product-auth/assets/scaffolds/product-nextjs-postgres/src/lib/egbe-product/index.ts +3 -0
- package/skills/egbe-os-product-auth/assets/scaffolds/product-nextjs-postgres/src/lib/egbe-product/session.ts +91 -0
- package/skills/egbe-os-product-auth/references/contracts/live-auth-interface.md +108 -0
- package/skills/egbe-os-product-auth/references/frameworks/nextjs.md +34 -0
- package/skills/egbe-os-product-auth/references/security/session-and-credentials.md +73 -0
- package/skills/egbe-os-product-auth/references/ux/auth-experience.md +58 -0
- package/skills/egbe-os-product-auth/references/workflows/account-recovery.md +22 -0
- package/skills/egbe-os-product-auth/references/workflows/build-auth.md +212 -0
- package/skills/egbe-os-product-auth/references/workflows/google.md +34 -0
- package/skills/egbe-os-product-auth/references/workflows/passwordless-email.md +65 -0
- package/skills/egbe-os-product-build/SKILL.md +111 -0
- package/skills/egbe-os-product-build/agents/openai.yaml +4 -0
- package/skills/egbe-os-product-build/references/capabilities.md +25 -0
- package/skills/egbe-os-product-build/references/engineering.md +72 -0
- package/skills/egbe-os-product-build/references/execution.md +55 -0
- package/skills/egbe-os-product-build/references/foundations.md +93 -0
- package/skills/egbe-os-product-build/references/interface-quality.md +32 -0
- package/skills/egbe-os-product-build/references/workflows/private-review-handoff.md +86 -0
- package/skills/egbe-os-product-build/scripts/scaffold.py +296 -0
- package/skills/egbe-os-product-demo-video/SKILL.md +118 -0
- package/skills/egbe-os-product-demo-video/references/recording.md +32 -0
- package/skills/egbe-os-product-market-research/SKILL.md +159 -0
- package/skills/egbe-os-product-market-research/agents/openai.yaml +4 -0
- package/skills/egbe-os-product-market-research/references/catalog.v1.json +177 -0
- package/skills/egbe-os-product-market-research/references/config/channel-footprint-sources.v1.json +99 -0
- package/skills/egbe-os-product-market-research/references/config/creative-taxonomy.v1.json +367 -0
- package/skills/egbe-os-product-market-research/references/contracts/creative-video.md +75 -0
- package/skills/egbe-os-product-market-research/references/contracts/funnel-teardown.md +235 -0
- package/skills/egbe-os-product-market-research/references/contracts/geography-comparison.md +71 -0
- package/skills/egbe-os-product-market-research/references/contracts/lifecycle-capture.md +119 -0
- package/skills/egbe-os-product-market-research/references/contracts/research-artifacts.md +520 -0
- package/skills/egbe-os-product-market-research/references/contracts/walk-mechanics.v2.json +18 -0
- package/skills/egbe-os-product-market-research/references/prompts/acquisition-channels.md +137 -0
- package/skills/egbe-os-product-market-research/references/prompts/acquisition-channels.user.md +4 -0
- package/skills/egbe-os-product-market-research/references/prompts/competitive-landscape.md +118 -0
- package/skills/egbe-os-product-market-research/references/prompts/competitive-landscape.user.md +4 -0
- package/skills/egbe-os-product-market-research/references/prompts/demand-validation.md +154 -0
- package/skills/egbe-os-product-market-research/references/prompts/demand-validation.user.md +4 -0
- package/skills/egbe-os-product-market-research/references/prompts/market-size-and-dynamics.md +100 -0
- package/skills/egbe-os-product-market-research/references/prompts/market-size-and-dynamics.user.md +4 -0
- package/skills/egbe-os-product-market-research/references/prompts/pricing-and-monetization.md +126 -0
- package/skills/egbe-os-product-market-research/references/prompts/pricing-and-monetization.user.md +4 -0
- package/skills/egbe-os-product-market-research/references/prompts/target-audience.md +98 -0
- package/skills/egbe-os-product-market-research/references/prompts/target-audience.user.md +4 -0
- package/skills/egbe-os-product-market-research/references/prompts/unit-economics.md +217 -0
- package/skills/egbe-os-product-market-research/references/prompts/unit-economics.user.md +4 -0
- package/skills/egbe-os-product-market-research/references/prompts/value-proposition.md +113 -0
- package/skills/egbe-os-product-market-research/references/prompts/value-proposition.user.md +4 -0
- package/skills/egbe-os-product-market-research/references/workflows/acquisition-channels.md +27 -0
- package/skills/egbe-os-product-market-research/references/workflows/channel-footprint.md +163 -0
- package/skills/egbe-os-product-market-research/references/workflows/community-demand.md +100 -0
- package/skills/egbe-os-product-market-research/references/workflows/competitive-landscape.md +27 -0
- package/skills/egbe-os-product-market-research/references/workflows/competitor-ads.md +82 -0
- package/skills/egbe-os-product-market-research/references/workflows/competitor-set.md +300 -0
- package/skills/egbe-os-product-market-research/references/workflows/creative-patterns.md +228 -0
- package/skills/egbe-os-product-market-research/references/workflows/demand-validation.md +27 -0
- package/skills/egbe-os-product-market-research/references/workflows/fleet-prior.md +80 -0
- package/skills/egbe-os-product-market-research/references/workflows/funnel-teardown.md +184 -0
- package/skills/egbe-os-product-market-research/references/workflows/geography-comparison.md +84 -0
- package/skills/egbe-os-product-market-research/references/workflows/keyword-economics.md +122 -0
- package/skills/egbe-os-product-market-research/references/workflows/lifecycle-capture.md +218 -0
- package/skills/egbe-os-product-market-research/references/workflows/local-research.md +55 -0
- package/skills/egbe-os-product-market-research/references/workflows/market-size-and-dynamics.md +27 -0
- package/skills/egbe-os-product-market-research/references/workflows/preview.md +175 -0
- package/skills/egbe-os-product-market-research/references/workflows/pricing-and-monetization.md +27 -0
- package/skills/egbe-os-product-market-research/references/workflows/recommendations.md +35 -0
- package/skills/egbe-os-product-market-research/references/workflows/review-mining.md +76 -0
- package/skills/egbe-os-product-market-research/references/workflows/target-audience.md +27 -0
- package/skills/egbe-os-product-market-research/references/workflows/tiktok-reels-market-trends.md +46 -0
- package/skills/egbe-os-product-market-research/references/workflows/unit-economics.md +27 -0
- package/skills/egbe-os-product-market-research/references/workflows/value-proposition.md +27 -0
- package/skills/egbe-os-product-market-research/scripts/build-organic-trend-context.py +167 -0
- package/skills/egbe-os-product-market-research/scripts/build-preview-context.py +91 -0
- package/skills/egbe-os-product-market-research/scripts/build-research-query.py +225 -0
- package/skills/egbe-os-product-market-research/scripts/channel-footprint-model.py +294 -0
- package/skills/egbe-os-product-market-research/scripts/connection_identity.py +22 -0
- package/skills/egbe-os-product-market-research/scripts/creative-model.py +364 -0
- package/skills/egbe-os-product-market-research/scripts/creative-selection.py +113 -0
- package/skills/egbe-os-product-market-research/scripts/creative-semantics.py +365 -0
- package/skills/egbe-os-product-market-research/scripts/creative-video.py +542 -0
- package/skills/egbe-os-product-market-research/scripts/egbe_artifacts.py +24 -0
- package/skills/egbe-os-product-market-research/scripts/finalize-organic-trend-watch.py +304 -0
- package/skills/egbe-os-product-market-research/scripts/funnel-email-review.py +97 -0
- package/skills/egbe-os-product-market-research/scripts/funnel-model.py +966 -0
- package/skills/egbe-os-product-market-research/scripts/funnel_features.py +50 -0
- package/skills/egbe-os-product-market-research/scripts/geography-comparison-contract.py +172 -0
- package/skills/egbe-os-product-market-research/scripts/geography-comparison.py +169 -0
- package/skills/egbe-os-product-market-research/scripts/keyword-forecast.py +61 -0
- package/skills/egbe-os-product-market-research/scripts/keyword-locale-publication.py +136 -0
- package/skills/egbe-os-product-market-research/scripts/lifecycle-start.py +190 -0
- package/skills/egbe-os-product-market-research/scripts/persist-research-report.py +63 -0
- package/skills/egbe-os-product-market-research/scripts/research-artifact-io.py +281 -0
- package/skills/egbe-os-product-market-research/scripts/research-evidence.py +496 -0
- package/skills/egbe-os-product-market-research/scripts/research-policy.py +33 -0
- package/skills/egbe-os-product-market-research/scripts/research-section-runtime.py +343 -0
- package/skills/egbe-os-product-market-research/scripts/research-section-workflow.py +288 -0
- package/skills/egbe-os-product-market-research/scripts/research_integrity.py +122 -0
- package/skills/egbe-os-product-market-research/scripts/review-research-completion.py +306 -0
- package/skills/egbe-os-product-market-research/scripts/run-acquisition-channels.py +72 -0
- package/skills/egbe-os-product-market-research/scripts/run-channel-footprint.py +2258 -0
- package/skills/egbe-os-product-market-research/scripts/run-community-demand.py +1603 -0
- package/skills/egbe-os-product-market-research/scripts/run-competitive-landscape.py +66 -0
- package/skills/egbe-os-product-market-research/scripts/run-competitor-ads.py +1156 -0
- package/skills/egbe-os-product-market-research/scripts/run-competitor-set.py +2514 -0
- package/skills/egbe-os-product-market-research/scripts/run-creative-patterns.py +2042 -0
- package/skills/egbe-os-product-market-research/scripts/run-demand-validation.py +72 -0
- package/skills/egbe-os-product-market-research/scripts/run-fleet-prior.py +652 -0
- package/skills/egbe-os-product-market-research/scripts/run-funnel-teardown.py +493 -0
- package/skills/egbe-os-product-market-research/scripts/run-keyword-economics.py +1443 -0
- package/skills/egbe-os-product-market-research/scripts/run-lifecycle-capture.py +1543 -0
- package/skills/egbe-os-product-market-research/scripts/run-market-size-and-dynamics.py +66 -0
- package/skills/egbe-os-product-market-research/scripts/run-os-research.py +311 -0
- package/skills/egbe-os-product-market-research/scripts/run-pricing-and-monetization.py +71 -0
- package/skills/egbe-os-product-market-research/scripts/run-review-mining.py +1051 -0
- package/skills/egbe-os-product-market-research/scripts/run-target-audience.py +66 -0
- package/skills/egbe-os-product-market-research/scripts/run-unit-economics.py +71 -0
- package/skills/egbe-os-product-market-research/scripts/run-value-proposition.py +67 -0
- package/skills/egbe-os-product-market-research/scripts/upsert-markdown-section.py +156 -0
- package/skills/egbe-os-product-market-research/scripts/walk-funnels.py +249 -0
- package/skills/egbe-os-product-market-research/scripts/write-preview.py +710 -0
- package/skills/egbe-os-product-market-research/scripts/write-recommendations.py +272 -0
- package/skills/egbe-os-product-report/SKILL.md +150 -0
- package/skills/egbe-os-product-report/references/artifacts.md +84 -0
- package/skills/egbe-os-product-report/references/brief.md +34 -0
- package/skills/egbe-os-product-report/references/daily.md +76 -0
- package/skills/egbe-os-product-report/references/sources.md +68 -0
- package/skills/egbe-os-product-routines/SKILL.md +166 -0
- package/skills/egbe-os-product-routines/references/catalog.md +22 -0
- package/skills/egbe-os-product-routines/references/continuation-check.md +41 -0
- package/skills/egbe-os-product-routines/references/product-reporting.md +163 -0
- package/skills/egbe-os-product-routines/references/run-preset.md +32 -0
- package/skills/egbe-os-quickstart/scripts/egbe-curl.py +357 -0
- package/skills/egbe-os-quiz-funnel/SKILL.md +152 -0
- package/skills/egbe-os-quiz-funnel/agents/openai.yaml +4 -0
- package/skills/egbe-os-quiz-funnel/assets/engine/quiz-engine.d.mts +224 -0
- package/skills/egbe-os-quiz-funnel/assets/engine/quiz-engine.mjs +652 -0
- package/skills/egbe-os-quiz-funnel/assets/engine/quiz-engine.test.mjs +764 -0
- package/skills/egbe-os-quiz-funnel/assets/engine/quiz-history.d.mts +31 -0
- package/skills/egbe-os-quiz-funnel/assets/engine/quiz-history.mjs +87 -0
- package/skills/egbe-os-quiz-funnel/assets/engine/quiz-history.test.mjs +265 -0
- package/skills/egbe-os-quiz-funnel/assets/engine/quiz-reference.test.mjs +254 -0
- package/skills/egbe-os-quiz-funnel/assets/examples/egbe-docs/product/quizzes/books-checkup/QUIZ.md +195 -0
- package/skills/egbe-os-quiz-funnel/assets/examples/egbe-docs/product/quizzes/books-checkup/quiz.json +421 -0
- package/skills/egbe-os-quiz-funnel/assets/examples/egbe-docs/product/quizzes/books-checkup/quiz.public.json +369 -0
- package/skills/egbe-os-quiz-funnel/assets/examples/egbe-docs/product/quizzes/books-checkup/validation.json +21 -0
- package/skills/egbe-os-quiz-funnel/assets/reference/index.html +26 -0
- package/skills/egbe-os-quiz-funnel/assets/reference/quiz-bootstrap.js +31 -0
- package/skills/egbe-os-quiz-funnel/assets/reference/quiz-early-actions.mjs +11 -0
- package/skills/egbe-os-quiz-funnel/assets/reference/quiz-first-screen.mjs +32 -0
- package/skills/egbe-os-quiz-funnel/assets/reference/quiz-page.mjs +480 -0
- package/skills/egbe-os-quiz-funnel/assets/reference/quiz.css +264 -0
- package/skills/egbe-os-quiz-funnel/references/competitor-teardown.md +159 -0
- package/skills/egbe-os-quiz-funnel/references/conversion-handoff.md +156 -0
- package/skills/egbe-os-quiz-funnel/references/implementation.md +207 -0
- package/skills/egbe-os-quiz-funnel/references/inputs.md +37 -0
- package/skills/egbe-os-quiz-funnel/references/measurement.md +122 -0
- package/skills/egbe-os-quiz-funnel/references/spec-contract.md +335 -0
- package/skills/egbe-os-quiz-funnel/references/verification.md +101 -0
- package/skills/egbe-os-quiz-funnel/references/walk-mechanics.v2.json +18 -0
- package/skills/egbe-os-quiz-funnel/scripts/compile-quiz-spec.py +1869 -0
- package/skills/egbe-os-quiz-funnel/scripts/prepare-research-identity.py +66 -0
- package/skills/egbe-os-reddit-ads/SKILL.md +121 -0
- package/skills/egbe-os-reddit-ads/assets/static-banners/bold-statement/schema.json +41 -0
- package/skills/egbe-os-reddit-ads/assets/static-banners/bold-statement/template.html +106 -0
- package/skills/egbe-os-reddit-ads/assets/static-banners/data-proof/schema.json +61 -0
- package/skills/egbe-os-reddit-ads/assets/static-banners/data-proof/template.html +151 -0
- package/skills/egbe-os-reddit-ads/assets/static-banners/honest-breakdown/schema.json +91 -0
- package/skills/egbe-os-reddit-ads/assets/static-banners/honest-breakdown/template.html +185 -0
- package/skills/egbe-os-reddit-ads/assets/static-banners/question-hook/schema.json +51 -0
- package/skills/egbe-os-reddit-ads/assets/static-banners/question-hook/template.html +121 -0
- package/skills/egbe-os-reddit-ads/assets/static-banners/reddit-post-style/schema.json +87 -0
- package/skills/egbe-os-reddit-ads/assets/static-banners/reddit-post-style/template.html +212 -0
- package/skills/egbe-os-reddit-ads/references/creative/html-to-png-pipeline.md +123 -0
- package/skills/egbe-os-reddit-ads/references/creative/static-banner-qa.md +12 -0
- package/skills/egbe-os-reddit-ads/references/creative/static-banner-system.md +16 -0
- package/skills/egbe-os-reddit-ads/references/destination-requirements.md +36 -0
- package/skills/egbe-os-reddit-ads/references/runtime/api-reference.md +435 -0
- package/skills/egbe-os-reddit-ads/references/runtime/artifacts.md +57 -0
- package/skills/egbe-os-reddit-ads/references/runtime/monitoring.md +64 -0
- package/skills/egbe-os-reddit-ads/references/runtime/pixel-and-tracking.md +49 -0
- package/skills/egbe-os-reddit-ads/references/runtime/policies.md +19 -0
- package/skills/egbe-os-reddit-ads/references/runtime/shared-analytics-attribution.md +14 -0
- package/skills/egbe-os-reddit-ads/references/workflows/build-paused.md +35 -0
- package/skills/egbe-os-reddit-ads/references/workflows/launch-ready.md +19 -0
- package/skills/egbe-os-reddit-ads/references/workflows/launch.md +32 -0
- package/skills/egbe-os-reddit-ads/references/workflows/readiness.md +9 -0
- package/skills/egbe-os-reddit-ads/scripts/capture-ad.sh +178 -0
- package/skills/egbe-os-reddit-ads/scripts/validate-reddit-artifacts.py +187 -0
- package/skills/egbe-os-report-ui/SKILL.md +133 -0
- package/skills/egbe-os-report-ui/assets/egbe-wordmark.svg +9 -0
- package/skills/egbe-os-report-ui/assets/examples/components.html +417 -0
- package/skills/egbe-os-report-ui/assets/examples/editorial.html +209 -0
- package/skills/egbe-os-report-ui/assets/scaffold.html +179 -0
- package/skills/egbe-os-report-ui/references/components.md +209 -0
- package/skills/egbe-os-research/SKILL.md +125 -0
- package/skills/egbe-os-saas-unit-economics/SKILL.md +188 -0
- package/skills/egbe-os-scrape/SKILL.md +160 -0
- package/skills/egbe-os-search/SKILL.md +182 -0
- package/skills/egbe-os-search/references/google-places.md +81 -0
- package/skills/egbe-os-support/SKILL.md +149 -0
- package/skills/egbe-os-telemetry/SKILL.md +344 -0
- package/skills/egbe-os-telemetry/references/harnesses/claude-code.md +97 -0
- package/skills/egbe-os-telemetry/references/harnesses/codex.md +22 -0
- package/skills/egbe-os-telemetry/references/harnesses/hermes.md +39 -0
- package/skills/egbe-os-telemetry/references/harnesses/openclaw.md +139 -0
- package/skills/egbe-os-telemetry/references/harnesses/opencode.md +28 -0
- package/skills/egbe-os-tools/SKILL.md +180 -0
- package/skills/egbe-os-tools/references/theona.md +26 -0
- package/skills/egbe-os-wallet/SKILL.md +181 -0
- package/skills/egbe-setup/EGBE.md +484 -0
- package/skills/egbe-setup/SKILL.md +116 -0
- package/skills/egbe-setup/references/project.md +80 -0
- package/state.js +37 -0
- package/update.js +361 -0
|
@@ -0,0 +1,2008 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const { assessCampaignOptOuts } = require("./lib/recipient-exclusion");
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const { InstantlyClient } = require("./lib/instantly-client");
|
|
6
|
+
const { leadsNotInCampaign } = require("./lib/create-once");
|
|
7
|
+
const { selectSenders } = require("./lib/sender-selection");
|
|
8
|
+
const { plainToHtmlBody } = require("./lib/html-body");
|
|
9
|
+
const { validateLaunchConfig, initialBatchLimit } = require("./lib/run-config");
|
|
10
|
+
const { readOptOutHistory, filterConfirmedOptOuts, campaignSafetyReadback, stopConfirmedRecipient } = require("./lib/confirmed-opt-outs");
|
|
11
|
+
const {
|
|
12
|
+
initDb,
|
|
13
|
+
upsertRun,
|
|
14
|
+
upsertLead,
|
|
15
|
+
upsertRunLead,
|
|
16
|
+
insertLeadEvent,
|
|
17
|
+
} = require("./lib/outreach-db");
|
|
18
|
+
const {
|
|
19
|
+
ensureDir,
|
|
20
|
+
readJson,
|
|
21
|
+
writeJson,
|
|
22
|
+
sha256,
|
|
23
|
+
stableStringify,
|
|
24
|
+
normalizeEmail,
|
|
25
|
+
parseArgs,
|
|
26
|
+
isoNow,
|
|
27
|
+
ymd,
|
|
28
|
+
slugify,
|
|
29
|
+
deriveSenderName,
|
|
30
|
+
replaceSenderNameToken,
|
|
31
|
+
buildImportLead,
|
|
32
|
+
findLeadNameProblems,
|
|
33
|
+
leadNameChecksForSteps,
|
|
34
|
+
findLeadVariableProblems,
|
|
35
|
+
loadRunEnv,
|
|
36
|
+
ensureRunEnv,
|
|
37
|
+
getOsConfig,
|
|
38
|
+
campaignOwnershipStatus,
|
|
39
|
+
assertCampaignNotForeign,
|
|
40
|
+
} = require("./lib/helpers");
|
|
41
|
+
const {
|
|
42
|
+
reconcileRunIdentity,
|
|
43
|
+
reconcileInstantlyAttribution,
|
|
44
|
+
} = require("./lib/gtm-identity");
|
|
45
|
+
|
|
46
|
+
function fail(message) {
|
|
47
|
+
console.error(`ERROR: ${message}`);
|
|
48
|
+
process.exit(1);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Instantly's per-lead `verification_status` (GET /leads?campaign_id=…):
|
|
52
|
+
// 1 = valid, -1 = invalid, 11 / 12 = pending. Every other code (-2 risky,
|
|
53
|
+
// -3 catch-all, -4 job change, anything unknown) is counted as "risky": it is
|
|
54
|
+
// not known how campaign lead_stats counts those, so for the threshold they
|
|
55
|
+
// count as invalid (fail safe) and are listed separately.
|
|
56
|
+
const LEAD_VERIFICATION_VALID = new Set([1]);
|
|
57
|
+
const LEAD_VERIFICATION_INVALID = new Set([-1]);
|
|
58
|
+
const LEAD_VERIFICATION_PENDING = new Set([11, 12]);
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Every lead in a campaign, paging GET /leads 100 at a time. ok=false: the
|
|
62
|
+
* listing could not be read completely (error status, unexpected body, or a
|
|
63
|
+
* cursor the gateway ignores).
|
|
64
|
+
*/
|
|
65
|
+
async function listCampaignLeads(client, campaignId) {
|
|
66
|
+
const leads = [];
|
|
67
|
+
const cursors = new Set();
|
|
68
|
+
let cursor;
|
|
69
|
+
let status = null;
|
|
70
|
+
for (let page = 0; page < 1000; page++) {
|
|
71
|
+
const res = await client.listLeads(campaignId, {
|
|
72
|
+
limit: 100,
|
|
73
|
+
starting_after: cursor,
|
|
74
|
+
});
|
|
75
|
+
status = res.status;
|
|
76
|
+
if (!(res.status >= 200 && res.status < 300)) return { ok: false, status, leads };
|
|
77
|
+
const items = Array.isArray(res.body) ? res.body : res.body?.items;
|
|
78
|
+
if (!Array.isArray(items)) return { ok: false, status, leads };
|
|
79
|
+
leads.push(...items);
|
|
80
|
+
if (items.length < 100) return { ok: true, status, leads };
|
|
81
|
+
const lastId = items[items.length - 1]?.id;
|
|
82
|
+
// Same page again: the cursor was ignored and the list cannot be trusted.
|
|
83
|
+
if (!lastId || cursors.has(lastId)) return { ok: false, status, leads };
|
|
84
|
+
cursors.add(lastId);
|
|
85
|
+
cursor = lastId;
|
|
86
|
+
}
|
|
87
|
+
return { ok: false, status, leads };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Per-lead verification counts for the WHOLE campaign — the fallback when
|
|
92
|
+
* campaign lead_stats is null/empty (#3790). fileEmails only measures how
|
|
93
|
+
* many of this run's leads are visible yet (file_visible). has_status=false:
|
|
94
|
+
* no lead carried a verification_status field, i.e. this surface exposes no
|
|
95
|
+
* verification data either. bad = invalid + risky, the numerator of the
|
|
96
|
+
* invalid-lead threshold.
|
|
97
|
+
*/
|
|
98
|
+
async function readLeadVerification(client, campaignId, fileEmails = null) {
|
|
99
|
+
const listing = await listCampaignLeads(client, campaignId);
|
|
100
|
+
const out = {
|
|
101
|
+
ok: false,
|
|
102
|
+
status: listing.status,
|
|
103
|
+
has_status: false,
|
|
104
|
+
total: 0,
|
|
105
|
+
file_visible: 0,
|
|
106
|
+
valid: 0,
|
|
107
|
+
invalid: 0,
|
|
108
|
+
risky: 0,
|
|
109
|
+
bad: 0,
|
|
110
|
+
pending: 0,
|
|
111
|
+
invalid_leads: [],
|
|
112
|
+
};
|
|
113
|
+
if (!listing.ok) return out;
|
|
114
|
+
let unassigned = 0;
|
|
115
|
+
for (const lead of listing.leads) {
|
|
116
|
+
const email = String(lead?.email || "").trim().toLowerCase();
|
|
117
|
+
if (!email) continue;
|
|
118
|
+
out.total += 1;
|
|
119
|
+
if (fileEmails && fileEmails.has(email)) out.file_visible += 1;
|
|
120
|
+
if (
|
|
121
|
+
lead.verification_status === undefined ||
|
|
122
|
+
lead.verification_status === null
|
|
123
|
+
) {
|
|
124
|
+
// Next to leads that have a status: not picked up by the job yet.
|
|
125
|
+
unassigned += 1;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
out.has_status = true;
|
|
129
|
+
const code = Number(lead.verification_status);
|
|
130
|
+
if (LEAD_VERIFICATION_VALID.has(code)) out.valid += 1;
|
|
131
|
+
else if (LEAD_VERIFICATION_PENDING.has(code)) out.pending += 1;
|
|
132
|
+
else {
|
|
133
|
+
const kind = LEAD_VERIFICATION_INVALID.has(code) ? "invalid" : "risky";
|
|
134
|
+
out[kind] += 1;
|
|
135
|
+
out.invalid_leads.push({
|
|
136
|
+
email,
|
|
137
|
+
id: lead.id ?? null,
|
|
138
|
+
verification_status: lead.verification_status,
|
|
139
|
+
kind,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
out.pending += unassigned;
|
|
144
|
+
out.bad = out.invalid + out.risky;
|
|
145
|
+
out.ok = true;
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Rerun after a failed gate: every lead listed in state/invalid-leads.json
|
|
151
|
+
* whose email is no longer in the leads file is deleted from the campaign —
|
|
152
|
+
* dropping it from the file alone would leave it in the campaign, where it
|
|
153
|
+
* gets mailed on activation. Ids are re-read from the live campaign (a lead
|
|
154
|
+
* already gone counts as removed). Deletes are not retried on 5xx. Returns
|
|
155
|
+
* { ok, deleted, already_absent, failed, kept_in_file, reason }.
|
|
156
|
+
*/
|
|
157
|
+
async function removeDroppedInvalidLeads(client, campaignId, invalidRecord, fileEmails) {
|
|
158
|
+
const result = {
|
|
159
|
+
ok: true,
|
|
160
|
+
deleted: [],
|
|
161
|
+
already_absent: [],
|
|
162
|
+
failed: [],
|
|
163
|
+
kept_in_file: [],
|
|
164
|
+
reason: null,
|
|
165
|
+
};
|
|
166
|
+
const listed = Array.isArray(invalidRecord?.leads) ? invalidRecord.leads : [];
|
|
167
|
+
const dropped = [];
|
|
168
|
+
for (const entry of listed) {
|
|
169
|
+
const email = String(entry?.email || "").trim().toLowerCase();
|
|
170
|
+
if (!email) continue;
|
|
171
|
+
if (fileEmails.has(email)) result.kept_in_file.push(email);
|
|
172
|
+
else dropped.push({ ...entry, email });
|
|
173
|
+
}
|
|
174
|
+
if (!dropped.length) return result;
|
|
175
|
+
const live = await listCampaignLeads(client, campaignId);
|
|
176
|
+
if (!live.ok) {
|
|
177
|
+
result.ok = false;
|
|
178
|
+
result.reason =
|
|
179
|
+
`the campaign's leads could not be read (status ${live.status}) to remove ` +
|
|
180
|
+
`${dropped.length} lead(s) dropped from the leads file after the last failed verification gate`;
|
|
181
|
+
result.failed = dropped.map((d) => ({ email: d.email, id: d.id ?? null, status: live.status }));
|
|
182
|
+
return result;
|
|
183
|
+
}
|
|
184
|
+
const idsByEmail = new Map();
|
|
185
|
+
for (const lead of live.leads) {
|
|
186
|
+
const email = String(lead?.email || "").trim().toLowerCase();
|
|
187
|
+
if (!email || !lead?.id) continue;
|
|
188
|
+
if (!idsByEmail.has(email)) idsByEmail.set(email, []);
|
|
189
|
+
idsByEmail.get(email).push(lead.id);
|
|
190
|
+
}
|
|
191
|
+
for (const entry of dropped) {
|
|
192
|
+
const ids = idsByEmail.get(entry.email) || [];
|
|
193
|
+
if (!ids.length) {
|
|
194
|
+
result.already_absent.push(entry.email);
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
for (const id of ids) {
|
|
198
|
+
const res = await client.deleteLead(id);
|
|
199
|
+
if (res.status >= 200 && res.status < 300) {
|
|
200
|
+
result.deleted.push({
|
|
201
|
+
email: entry.email,
|
|
202
|
+
id,
|
|
203
|
+
verification_status: entry.verification_status ?? null,
|
|
204
|
+
kind: entry.kind ?? null,
|
|
205
|
+
});
|
|
206
|
+
} else {
|
|
207
|
+
result.ok = false;
|
|
208
|
+
result.failed.push({ email: entry.email, id, status: res.status });
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (!result.ok) {
|
|
213
|
+
result.reason = `${result.failed.length} delete(s) failed`;
|
|
214
|
+
}
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async function waitForVerification(client, campaignId, spec, opts = {}) {
|
|
219
|
+
const importedCount = opts.importedCount ?? null;
|
|
220
|
+
const skippedCount = opts.skippedCount ?? null;
|
|
221
|
+
// This run's leads (the leads file) and a lower bound of how many of them
|
|
222
|
+
// must be visible in the campaign: a lagging leads listing showing 10 of
|
|
223
|
+
// 50 must not pass as "0 pending". The counts themselves cover the whole
|
|
224
|
+
// campaign.
|
|
225
|
+
const fileEmails = opts.fileEmails ?? null;
|
|
226
|
+
const expectedLeads = Math.max(0, Number(opts.expectedLeads) || 0);
|
|
227
|
+
// Defaults raised after observing ~20m verification windows in production
|
|
228
|
+
// for 50–100 lead batches. Stuck-detect breaks early when Instantly's
|
|
229
|
+
// verification queue stops making progress (pending count stable across
|
|
230
|
+
// N polls), so we don't burn the full timeout on a hung backlog.
|
|
231
|
+
const maxWaitMs =
|
|
232
|
+
(spec.campaign.verification_timeout_minutes || 45) * 60 * 1000;
|
|
233
|
+
const pollIntervalMs = 15_000;
|
|
234
|
+
const maxInvalidPct = spec.campaign.max_invalid_lead_pct ?? 5;
|
|
235
|
+
const stuckPollLimit = Math.max(
|
|
236
|
+
3,
|
|
237
|
+
spec.campaign.verification_stuck_polls || 8,
|
|
238
|
+
);
|
|
239
|
+
const started = Date.now();
|
|
240
|
+
|
|
241
|
+
let lastPending = null;
|
|
242
|
+
let stuckCount = 0;
|
|
243
|
+
let lastPerLeadPending = null;
|
|
244
|
+
let perLeadStuckCount = 0;
|
|
245
|
+
let lastSeenTotal = 0;
|
|
246
|
+
let nullStatsPolls = 0;
|
|
247
|
+
// Last readable observation, for the "still pending" result at the end of
|
|
248
|
+
// the wait budget.
|
|
249
|
+
let lastObserved = null;
|
|
250
|
+
|
|
251
|
+
// A 2xx from the bulk add is not proof the batch landed: 0 uploaded and
|
|
252
|
+
// 0 already-present means every lead was rejected, and on a reused
|
|
253
|
+
// campaign the leads API showing OLD leads must not mask that. Counters
|
|
254
|
+
// are null for callers that did not supply them (legacy behavior).
|
|
255
|
+
const batchLanded =
|
|
256
|
+
importedCount == null || importedCount > 0 || (skippedCount ?? 0) > 0;
|
|
257
|
+
|
|
258
|
+
// The invalid-rate gate failed: the caller writes the invalid addresses to
|
|
259
|
+
// the run dir and aborts before activation.
|
|
260
|
+
const gateFailure = async (reason, counts, perLead = null) => {
|
|
261
|
+
let invalidLeads = perLead?.ok ? perLead.invalid_leads : null;
|
|
262
|
+
if (!invalidLeads) {
|
|
263
|
+
// lead_stats has counts only — read the per-lead listing for the
|
|
264
|
+
// addresses (whole campaign, like lead_stats).
|
|
265
|
+
const listing = await readLeadVerification(client, campaignId, fileEmails);
|
|
266
|
+
invalidLeads = listing.ok ? listing.invalid_leads : [];
|
|
267
|
+
}
|
|
268
|
+
return {
|
|
269
|
+
gate_failed: true,
|
|
270
|
+
failure_reason: reason,
|
|
271
|
+
invalid_leads: invalidLeads,
|
|
272
|
+
max_invalid_lead_pct: maxInvalidPct,
|
|
273
|
+
...counts,
|
|
274
|
+
};
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
// Read-surface triage, run when neither lead_stats nor the per-lead
|
|
278
|
+
// verification_status yields numbers for stuckPollLimit polls: cross-check
|
|
279
|
+
// the leads API once, reconcile the add-leads counters, and either report a
|
|
280
|
+
// failed import or a degraded read surface (#3790) — instead of burning the
|
|
281
|
+
// whole timeout on identical broken polls. A degraded result means the
|
|
282
|
+
// invalid-lead gate was NOT enforced; the caller does not activate unless
|
|
283
|
+
// campaign.allow_unverified_activation is true.
|
|
284
|
+
const triageDegradedReads = async (detailsSummary, perLead) => {
|
|
285
|
+
if (!batchLanded) {
|
|
286
|
+
return {
|
|
287
|
+
import_failed: true,
|
|
288
|
+
import_failure_reason:
|
|
289
|
+
`${detailsSummary} after ${nullStatsPolls} polls, and the add-leads call reported ` +
|
|
290
|
+
`0 uploaded and 0 already-present — the new batch never landed (all leads ` +
|
|
291
|
+
`invalid/incomplete?). Campaign was NOT activated.`,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
const perLeadSummary = !perLead
|
|
295
|
+
? "the per-lead verification_status was not read"
|
|
296
|
+
: perLead.ok
|
|
297
|
+
? "the leads list exposes no per-lead verification_status"
|
|
298
|
+
: `the leads list is unreadable for per-lead verification_status (status ${perLead.status})`;
|
|
299
|
+
const counts = await client.countLeads(campaignId);
|
|
300
|
+
if (counts.ok && counts.total > 0) {
|
|
301
|
+
const skippedReason =
|
|
302
|
+
`${detailsSummary} after ${nullStatsPolls} polls and ${perLeadSummary}, but the leads API ` +
|
|
303
|
+
`reports ${counts.total} leads in the campaign` +
|
|
304
|
+
(importedCount != null
|
|
305
|
+
? ` and the add-leads call reported ${importedCount} uploaded`
|
|
306
|
+
: "") +
|
|
307
|
+
`. Both verification read surfaces are unavailable (#3790): the max_invalid_lead_pct ` +
|
|
308
|
+
`(${maxInvalidPct}%) gate was NOT enforced for this launch.`;
|
|
309
|
+
console.error(`WARNING: ${skippedReason}`);
|
|
310
|
+
return {
|
|
311
|
+
verifiedLeads: 0,
|
|
312
|
+
invalidLeads: 0,
|
|
313
|
+
totalLeads: counts.total,
|
|
314
|
+
invalidPct: 0,
|
|
315
|
+
degraded_read_surface: true,
|
|
316
|
+
verification_unread: true,
|
|
317
|
+
stuck_reason: skippedReason,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
if (counts.ok) {
|
|
321
|
+
return {
|
|
322
|
+
import_failed: true,
|
|
323
|
+
import_failure_reason:
|
|
324
|
+
`No leads visible (${detailsSummary}) AND the leads API returns zero after ` +
|
|
325
|
+
`${nullStatsPolls} polls` +
|
|
326
|
+
(importedCount != null
|
|
327
|
+
? ` (the add-leads call reported ${importedCount} uploaded)`
|
|
328
|
+
: "") +
|
|
329
|
+
`. The lead import most likely failed silently — inspect the add-leads response in the ` +
|
|
330
|
+
`launch log and confirm the payload used the {"payload": {...}} gateway wrapper. ` +
|
|
331
|
+
`Campaign was NOT activated.`,
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
const skippedReason =
|
|
335
|
+
`${detailsSummary} and the leads list (status ${counts.status}) is unreadable after ` +
|
|
336
|
+
`${nullStatsPolls} polls` +
|
|
337
|
+
(importedCount != null
|
|
338
|
+
? `; the add-leads call reported ${importedCount} uploaded`
|
|
339
|
+
: "") +
|
|
340
|
+
`. Both verification read surfaces are unavailable (#3790): the max_invalid_lead_pct ` +
|
|
341
|
+
`(${maxInvalidPct}%) gate was NOT enforced for this launch.`;
|
|
342
|
+
console.error(`WARNING: ${skippedReason}`);
|
|
343
|
+
return {
|
|
344
|
+
verifiedLeads: 0,
|
|
345
|
+
invalidLeads: 0,
|
|
346
|
+
totalLeads: importedCount ?? 0,
|
|
347
|
+
invalidPct: 0,
|
|
348
|
+
degraded_read_surface: true,
|
|
349
|
+
verification_unread: true,
|
|
350
|
+
stuck_reason: skippedReason,
|
|
351
|
+
};
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
console.log("Waiting for lead verification to complete...");
|
|
355
|
+
while (Date.now() - started < maxWaitMs) {
|
|
356
|
+
await new Promise((r) => setTimeout(r, pollIntervalMs));
|
|
357
|
+
|
|
358
|
+
// The analytics response is not used below — it existed only as a
|
|
359
|
+
// reachability gate, and a down analytics route (#3790: campaigns/
|
|
360
|
+
// analytics 404s) short-circuited the poll so the degraded-read triage
|
|
361
|
+
// never ran. Log and continue with campaign details instead.
|
|
362
|
+
const res = await client.campaignAnalytics(campaignId);
|
|
363
|
+
if (!(res.status >= 200 && res.status < 300)) {
|
|
364
|
+
console.log(
|
|
365
|
+
`Analytics unreadable (status ${res.status}); continuing with campaign details.`,
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// Instantly exposes verification stats through campaign analytics
|
|
370
|
+
// after verify_leads_on_import completes. We use the campaign details
|
|
371
|
+
// endpoint to check if leads have been processed.
|
|
372
|
+
const details = await client.getCampaign(campaignId);
|
|
373
|
+
const detailsReadable = details.status >= 200 && details.status < 300;
|
|
374
|
+
const body = detailsReadable ? details.body || {} : {};
|
|
375
|
+
const leadStats = body.lead_stats || {};
|
|
376
|
+
const totalLeads = leadStats.total || 0;
|
|
377
|
+
const verifiedLeads = leadStats.verified || 0;
|
|
378
|
+
const invalidLeads = leadStats.invalid || 0;
|
|
379
|
+
const pendingVerification = leadStats.pending_verification || 0;
|
|
380
|
+
|
|
381
|
+
// Diagnostic poll log: live runs surfaced `lead_stats: null` from the API,
|
|
382
|
+
// which left the gate working semi-blind. Logging the raw stats object on
|
|
383
|
+
// every poll lets us see whether Instantly is filling lead_stats at all.
|
|
384
|
+
console.log(
|
|
385
|
+
detailsReadable
|
|
386
|
+
? `Verification poll: lead_stats=${JSON.stringify(body.lead_stats ?? null)} totalLeads=${totalLeads}`
|
|
387
|
+
: `Verification poll: campaign details unreadable (status ${details.status})`,
|
|
388
|
+
);
|
|
389
|
+
|
|
390
|
+
if (totalLeads === 0) {
|
|
391
|
+
// lead_stats null/empty or details unreadable (#3790). A production run
|
|
392
|
+
// once burned a 60-minute timeout on 29 identical null polls, and a
|
|
393
|
+
// later one skipped the gate entirely after 8 — while the per-lead
|
|
394
|
+
// verification_status showed 12.7% invalid. Read the per-lead status
|
|
395
|
+
// instead of giving up on the gate. Skipped when the batch never
|
|
396
|
+
// landed: old leads on a reused campaign must not mask that.
|
|
397
|
+
const perLead = batchLanded
|
|
398
|
+
? await readLeadVerification(client, campaignId, fileEmails)
|
|
399
|
+
: null;
|
|
400
|
+
if (perLead?.ok && perLead.has_status && perLead.total > 0) {
|
|
401
|
+
nullStatsPolls = 0;
|
|
402
|
+
// Leads of this run not visible yet count as pending, and in the
|
|
403
|
+
// denominator.
|
|
404
|
+
const notVisible = Math.max(0, expectedLeads - perLead.file_visible);
|
|
405
|
+
const denominator = perLead.total + notVisible;
|
|
406
|
+
const pendingLeads = perLead.pending + notVisible;
|
|
407
|
+
const counts = {
|
|
408
|
+
verifiedLeads: perLead.valid,
|
|
409
|
+
invalidLeads: perLead.bad,
|
|
410
|
+
riskyLeads: perLead.risky,
|
|
411
|
+
pendingLeads,
|
|
412
|
+
totalLeads: denominator,
|
|
413
|
+
invalidPct: (perLead.bad / denominator) * 100,
|
|
414
|
+
source: "per_lead_verification_status",
|
|
415
|
+
};
|
|
416
|
+
lastObserved = counts;
|
|
417
|
+
console.log(
|
|
418
|
+
`Verification progress (per-lead verification_status; lead_stats unavailable): ` +
|
|
419
|
+
`valid=${perLead.valid} invalid=${perLead.invalid} risky=${perLead.risky} ` +
|
|
420
|
+
`pending=${perLead.pending} total=${perLead.total}` +
|
|
421
|
+
(expectedLeads
|
|
422
|
+
? ` (this run's leads visible: ${perLead.file_visible} of >=${expectedLeads})`
|
|
423
|
+
: ""),
|
|
424
|
+
);
|
|
425
|
+
// Pending (or not yet visible) leads can only add to the invalid
|
|
426
|
+
// count, so a rate already over the threshold is final.
|
|
427
|
+
if (counts.invalidPct > maxInvalidPct) {
|
|
428
|
+
return await gateFailure(
|
|
429
|
+
`Lead verification failed: ${counts.invalidPct.toFixed(1)}% invalid (threshold: ${maxInvalidPct}%) — ` +
|
|
430
|
+
`${perLead.bad} of ${denominator} leads are invalid` +
|
|
431
|
+
(perLead.risky
|
|
432
|
+
? ` (${perLead.invalid} invalid + ${perLead.risky} risky / catch-all, counted as invalid)`
|
|
433
|
+
: "") +
|
|
434
|
+
(pendingLeads
|
|
435
|
+
? ` and ${pendingLeads} are still pending (the rate can only go up)`
|
|
436
|
+
: "") +
|
|
437
|
+
` [source: per-lead verification_status; campaign lead_stats was unavailable]. ` +
|
|
438
|
+
`Campaign was NOT activated.`,
|
|
439
|
+
counts,
|
|
440
|
+
perLead,
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
if (pendingLeads > 0) {
|
|
444
|
+
// Same stuck rule as the lead_stats path: a pending count that does
|
|
445
|
+
// not move for verification_stuck_polls polls is a hung queue.
|
|
446
|
+
if (lastPerLeadPending !== null && pendingLeads === lastPerLeadPending) {
|
|
447
|
+
perLeadStuckCount += 1;
|
|
448
|
+
if (perLeadStuckCount >= stuckPollLimit) {
|
|
449
|
+
const pendingReason =
|
|
450
|
+
`Verification appears stuck: ${pendingLeads} leads still pending after ` +
|
|
451
|
+
`${perLeadStuckCount} consecutive polls (~${Math.round((perLeadStuckCount * pollIntervalMs) / 60000)}m ` +
|
|
452
|
+
`no progress); ${perLead.valid} valid + ${perLead.bad} invalid/risky observed so far ` +
|
|
453
|
+
`(${counts.invalidPct.toFixed(1)}% invalid, under the ${maxInvalidPct}% threshold) ` +
|
|
454
|
+
`[source: per-lead verification_status]. Campaign was NOT activated. Rerun the same launch ` +
|
|
455
|
+
`command later (the gate is re-checked), raise verification_stuck_polls / ` +
|
|
456
|
+
`verification_timeout_minutes, or set campaign.allow_unverified_activation: true to activate ` +
|
|
457
|
+
`on the partial result.`;
|
|
458
|
+
console.error(`WARNING: ${pendingReason}`);
|
|
459
|
+
return {
|
|
460
|
+
...counts,
|
|
461
|
+
stuck: true,
|
|
462
|
+
verification_pending: true,
|
|
463
|
+
pending_reason: pendingReason,
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
} else {
|
|
467
|
+
perLeadStuckCount = 0;
|
|
468
|
+
}
|
|
469
|
+
lastPerLeadPending = pendingLeads;
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
console.log(
|
|
473
|
+
`Verification complete (per-lead verification_status): ${perLead.valid} valid, ` +
|
|
474
|
+
`${perLead.invalid} invalid, ${perLead.risky} risky ` +
|
|
475
|
+
`(${counts.invalidPct.toFixed(1)}% invalid+risky — within ${maxInvalidPct}% threshold)`,
|
|
476
|
+
);
|
|
477
|
+
return counts;
|
|
478
|
+
}
|
|
479
|
+
nullStatsPolls += 1;
|
|
480
|
+
if (nullStatsPolls >= stuckPollLimit) {
|
|
481
|
+
return await triageDegradedReads(
|
|
482
|
+
detailsReadable
|
|
483
|
+
? "campaign details never exposed lead_stats"
|
|
484
|
+
: `campaign details unreadable (status ${details.status})`,
|
|
485
|
+
perLead,
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
nullStatsPolls = 0;
|
|
491
|
+
lastSeenTotal = totalLeads;
|
|
492
|
+
lastObserved = {
|
|
493
|
+
verifiedLeads,
|
|
494
|
+
invalidLeads,
|
|
495
|
+
pendingLeads: pendingVerification,
|
|
496
|
+
totalLeads,
|
|
497
|
+
invalidPct: (invalidLeads / totalLeads) * 100,
|
|
498
|
+
source: "lead_stats",
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
console.log(
|
|
502
|
+
`Verification progress: verified=${verifiedLeads} invalid=${invalidLeads} pending=${pendingVerification} total=${totalLeads}`,
|
|
503
|
+
);
|
|
504
|
+
|
|
505
|
+
if (pendingVerification > 0) {
|
|
506
|
+
// Stuck-detect: if pending count hasn't moved for stuckPollLimit polls,
|
|
507
|
+
// treat the verification queue as hung and surface a degraded result
|
|
508
|
+
// instead of waiting out the full timeout.
|
|
509
|
+
if (lastPending !== null && pendingVerification === lastPending) {
|
|
510
|
+
stuckCount += 1;
|
|
511
|
+
if (stuckCount >= stuckPollLimit) {
|
|
512
|
+
// SKILL.md step 4a: "If invalid leads exceed the threshold, the
|
|
513
|
+
// launcher aborts." Stuck-detect must honour the same contract on
|
|
514
|
+
// partial data — a stuck queue with 30% invalid is still a bad
|
|
515
|
+
// launch and we'd rather pause it than activate quietly.
|
|
516
|
+
const partialInvalidPct =
|
|
517
|
+
totalLeads > 0 ? (invalidLeads / totalLeads) * 100 : 0;
|
|
518
|
+
if (partialInvalidPct > maxInvalidPct) {
|
|
519
|
+
return await gateFailure(
|
|
520
|
+
`Lead verification stuck AND invalid rate already exceeds threshold: ` +
|
|
521
|
+
`${partialInvalidPct.toFixed(1)}% invalid (threshold: ${maxInvalidPct}%). ` +
|
|
522
|
+
`${invalidLeads} of ${totalLeads} processed leads are invalid; ${pendingVerification} ` +
|
|
523
|
+
`still pending after ${stuckCount} polls. Refusing to activate. Review lead source.`,
|
|
524
|
+
lastObserved,
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
// A hung queue is "still pending", not a pass: no activation
|
|
528
|
+
// (unless campaign.allow_unverified_activation), rerun later.
|
|
529
|
+
const pendingReason =
|
|
530
|
+
`Verification appears stuck: ${pendingVerification} leads still pending after ` +
|
|
531
|
+
`${stuckCount} consecutive polls (~${Math.round((stuckCount * pollIntervalMs) / 60000)}m no progress); ` +
|
|
532
|
+
`${verifiedLeads} verified + ${invalidLeads} invalid observed so far (${partialInvalidPct.toFixed(1)}% ` +
|
|
533
|
+
`invalid, under the ${maxInvalidPct}% threshold) [source: campaign lead_stats]. Campaign was NOT ` +
|
|
534
|
+
`activated. Rerun the same launch command later (the gate is re-checked), raise ` +
|
|
535
|
+
`verification_stuck_polls / verification_timeout_minutes, or set ` +
|
|
536
|
+
`campaign.allow_unverified_activation: true to activate on the partial result.`;
|
|
537
|
+
console.error(`WARNING: ${pendingReason}`);
|
|
538
|
+
return {
|
|
539
|
+
...lastObserved,
|
|
540
|
+
invalidPct: partialInvalidPct,
|
|
541
|
+
stuck: true,
|
|
542
|
+
verification_pending: true,
|
|
543
|
+
pending_reason: pendingReason,
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
} else {
|
|
547
|
+
stuckCount = 0;
|
|
548
|
+
}
|
|
549
|
+
lastPending = pendingVerification;
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
// All leads processed — check quality
|
|
554
|
+
const invalidPct = (invalidLeads / totalLeads) * 100;
|
|
555
|
+
if (invalidPct > maxInvalidPct) {
|
|
556
|
+
return await gateFailure(
|
|
557
|
+
`Lead verification failed: ${invalidPct.toFixed(1)}% invalid (threshold: ${maxInvalidPct}%). ` +
|
|
558
|
+
`${invalidLeads} of ${totalLeads} leads are invalid [source: campaign lead_stats]. ` +
|
|
559
|
+
`Campaign was NOT activated.`,
|
|
560
|
+
lastObserved,
|
|
561
|
+
);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
console.log(
|
|
565
|
+
`Verification complete: ${verifiedLeads} verified, ${invalidLeads} invalid (${invalidPct.toFixed(1)}% — within ${maxInvalidPct}% threshold)`,
|
|
566
|
+
);
|
|
567
|
+
return { verifiedLeads, invalidLeads, totalLeads, invalidPct, source: "lead_stats" };
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// Wait budget spent with leads still pending: not a crash and not an
|
|
571
|
+
// activation — the caller records the state and asks for a rerun.
|
|
572
|
+
const minutes = Math.round(maxWaitMs / 60000);
|
|
573
|
+
const observed = lastObserved
|
|
574
|
+
? `last seen: valid=${lastObserved.verifiedLeads} invalid=${lastObserved.invalidLeads} ` +
|
|
575
|
+
`pending=${lastObserved.pendingLeads} total=${lastObserved.totalLeads}, source ${lastObserved.source}`
|
|
576
|
+
: `last seen total=${lastSeenTotal}, pending=${lastPending}`;
|
|
577
|
+
return {
|
|
578
|
+
...(lastObserved || {}),
|
|
579
|
+
verification_pending: true,
|
|
580
|
+
pending_reason:
|
|
581
|
+
`Lead verification still pending after ${minutes} minutes (${observed}). ` +
|
|
582
|
+
`Campaign was NOT activated. Rerun the same launch command later: the verification gate is ` +
|
|
583
|
+
`re-checked before activation and leads already in the campaign are not re-sent. Raise ` +
|
|
584
|
+
`campaign.verification_timeout_minutes to wait longer in one run.`,
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
// Activation needs a verification gate that ran to completion (or a gate
|
|
589
|
+
// switched off in the spec). Returns { blocked, warning }: blocked is the
|
|
590
|
+
// launch_status to report instead of activating (the launcher then exits 2),
|
|
591
|
+
// warning the reason printed in the NOT ENFORCED banner.
|
|
592
|
+
// - pending after the wait budget: always blocked, rerun later
|
|
593
|
+
// - stuck lead_stats queue / both stat sources unreadable: blocked unless
|
|
594
|
+
// campaign.allow_unverified_activation is true
|
|
595
|
+
function verificationActivationBlock(result, allowUnverified) {
|
|
596
|
+
if (!result) return { blocked: null, warning: null };
|
|
597
|
+
if (result.verification_pending && result.stuck) {
|
|
598
|
+
return {
|
|
599
|
+
blocked: allowUnverified ? null : "verification_pending",
|
|
600
|
+
warning: result.pending_reason,
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
if (result.verification_pending) {
|
|
604
|
+
return { blocked: "verification_pending", warning: null };
|
|
605
|
+
}
|
|
606
|
+
if (result.verification_unread) {
|
|
607
|
+
return {
|
|
608
|
+
blocked: allowUnverified ? null : "verification_unread",
|
|
609
|
+
warning: result.stuck_reason,
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
return { blocked: null, warning: null };
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
// Instantly's campaign_schedule.timezone enum rejects several IANA names that
|
|
616
|
+
// look obviously valid (e.g. America/New_York). Documenting the workaround in
|
|
617
|
+
// references/instantly-ops.md was not enough — every fresh agent re-discovers
|
|
618
|
+
// the rejection through a 400 from /api/v2/campaigns. Translate in code and
|
|
619
|
+
// warn so the run-spec author sees what we did to their input.
|
|
620
|
+
const TIMEZONE_ALIASES = {
|
|
621
|
+
"America/New_York": "America/Detroit",
|
|
622
|
+
};
|
|
623
|
+
|
|
624
|
+
function resolveTimezone(tz) {
|
|
625
|
+
if (typeof tz !== "string" || !tz) return tz;
|
|
626
|
+
const aliased = TIMEZONE_ALIASES[tz];
|
|
627
|
+
if (aliased && aliased !== tz) {
|
|
628
|
+
console.error(
|
|
629
|
+
`WARNING: campaign.schedule.timezone "${tz}" is rejected by Instantly's enum; ` +
|
|
630
|
+
`using alias "${aliased}" (same effective offset). See references/instantly-ops.md.`,
|
|
631
|
+
);
|
|
632
|
+
return aliased;
|
|
633
|
+
}
|
|
634
|
+
return tz;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
function mapDays(days) {
|
|
638
|
+
if (!days) return undefined;
|
|
639
|
+
// Numeric keys ("0" = Sunday … "6" = Saturday), in any subset: {"2": true}
|
|
640
|
+
// used to fall through to the name lookup and come out as all false.
|
|
641
|
+
if (Object.keys(days).some((key) => /^[0-6]$/.test(key))) {
|
|
642
|
+
return Object.fromEntries(
|
|
643
|
+
[0, 1, 2, 3, 4, 5, 6].map((d) => [d, !!days[String(d)]]),
|
|
644
|
+
);
|
|
645
|
+
}
|
|
646
|
+
return {
|
|
647
|
+
0: !!days.sunday,
|
|
648
|
+
1: !!days.monday,
|
|
649
|
+
2: !!days.tuesday,
|
|
650
|
+
3: !!days.wednesday,
|
|
651
|
+
4: !!days.thursday,
|
|
652
|
+
5: !!days.friday,
|
|
653
|
+
6: !!days.saturday,
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
function compactLabel(value, fallback) {
|
|
658
|
+
const raw = String(value || "").trim();
|
|
659
|
+
if (!raw) return fallback;
|
|
660
|
+
return raw.replace(/\s+/g, " ").slice(0, 42);
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
function buildCampaignName(spec) {
|
|
664
|
+
const datePart = spec.created_at
|
|
665
|
+
? String(spec.created_at).slice(0, 10)
|
|
666
|
+
: ymd();
|
|
667
|
+
const product = compactLabel(
|
|
668
|
+
spec.product?.name || spec.product_name,
|
|
669
|
+
"Product",
|
|
670
|
+
);
|
|
671
|
+
const segment = compactLabel(
|
|
672
|
+
spec.segment?.name || spec.segment?.description || spec.segment,
|
|
673
|
+
"Segment",
|
|
674
|
+
);
|
|
675
|
+
const offer = compactLabel(
|
|
676
|
+
spec.offer?.headline ||
|
|
677
|
+
spec.offer?.name ||
|
|
678
|
+
spec.offer?.description ||
|
|
679
|
+
spec.offer,
|
|
680
|
+
"Offer",
|
|
681
|
+
);
|
|
682
|
+
return `${product} | ${segment} | ${offer} | ${datePart}`;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
// Map run-spec step semantics to Instantly's `delay` semantics:
|
|
686
|
+
// run-spec step.delay_days[N] = days to wait BEFORE step N is sent
|
|
687
|
+
// Instantly step.delay[N] = days to wait AFTER step N before step N+1
|
|
688
|
+
// Instantly therefore needs the *next* step's delay attached to the
|
|
689
|
+
// current step. Step 1's run-spec delay drops off the front (Instantly
|
|
690
|
+
// activates the first step at lead-import time, not on a schedule); the
|
|
691
|
+
// last step's `delay` is unused by Instantly's runtime. We warn loudly
|
|
692
|
+
// if step 1 carries a non-zero delay, so a "wait 2 days before the
|
|
693
|
+
// first email" intent doesn't silently disappear into the shift.
|
|
694
|
+
function instantlyStepDelays(steps) {
|
|
695
|
+
if (!Array.isArray(steps) || steps.length === 0) {
|
|
696
|
+
// The main() validator catches empty sequences upstream, but defending
|
|
697
|
+
// here too means a future caller (or a refactor that bypasses main)
|
|
698
|
+
// gets a clear error instead of a silent empty payload that Instantly
|
|
699
|
+
// rejects with a generic 400.
|
|
700
|
+
fail(
|
|
701
|
+
"sequence.steps is empty — Instantly will reject the campaign with a 400. " +
|
|
702
|
+
"Generate at least one email step before calling launch-campaign.js.",
|
|
703
|
+
);
|
|
704
|
+
}
|
|
705
|
+
if (steps.length > 0) {
|
|
706
|
+
const firstDelay = steps[0].delay_days ?? steps[0].delay ?? 0;
|
|
707
|
+
if (firstDelay > 0) {
|
|
708
|
+
console.error(
|
|
709
|
+
`WARNING: step 1 delay_days=${firstDelay} will be ignored — Instantly fires the first step on lead import, not on a delay. ` +
|
|
710
|
+
`If you need a wait before the first email, configure it at the campaign-schedule level instead.`,
|
|
711
|
+
);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
return steps.map((_, i) => {
|
|
715
|
+
const next = steps[i + 1];
|
|
716
|
+
if (!next) return 0;
|
|
717
|
+
return next.delay_days ?? next.delay ?? 0;
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
function sequenceFingerprint(spec, emailList, senderName) {
|
|
722
|
+
const delays = instantlyStepDelays(spec.sequence.steps);
|
|
723
|
+
const steps = spec.sequence.steps.map((step, i) => ({
|
|
724
|
+
delay: delays[i],
|
|
725
|
+
delay_unit: step.delay_unit || "days",
|
|
726
|
+
subject: step.subject,
|
|
727
|
+
// Fingerprint over the *delivered* HTML body so a plain-text vs HTML
|
|
728
|
+
// change in the source actually produces a different fingerprint.
|
|
729
|
+
// Includes UTM tags (per run_key) — same run_key produces stable hashes;
|
|
730
|
+
// different run_keys (different campaigns) get distinct fingerprints
|
|
731
|
+
// which is correct.
|
|
732
|
+
body: plainToHtmlBody(
|
|
733
|
+
replaceSenderNameToken(step.body, senderName),
|
|
734
|
+
spec.run_key,
|
|
735
|
+
),
|
|
736
|
+
}));
|
|
737
|
+
return sha256(stableStringify({ steps, emailList }));
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
function buildCampaignPayload(spec, emailList, senderName) {
|
|
741
|
+
const delays = instantlyStepDelays(spec.sequence.steps);
|
|
742
|
+
// Tracking is an explicit run choice; launch defaults do not add trackers.
|
|
743
|
+
const seq = spec.sequence.steps.map((step, i) => ({
|
|
744
|
+
type: "email",
|
|
745
|
+
delay: delays[i],
|
|
746
|
+
delay_unit: step.delay_unit || "days",
|
|
747
|
+
variants: [
|
|
748
|
+
{
|
|
749
|
+
subject: replaceSenderNameToken(step.subject, senderName),
|
|
750
|
+
body: plainToHtmlBody(
|
|
751
|
+
replaceSenderNameToken(step.body, senderName),
|
|
752
|
+
spec.run_key,
|
|
753
|
+
),
|
|
754
|
+
},
|
|
755
|
+
],
|
|
756
|
+
}));
|
|
757
|
+
return {
|
|
758
|
+
name: spec.campaign.name,
|
|
759
|
+
campaign_schedule: {
|
|
760
|
+
schedules: [
|
|
761
|
+
{
|
|
762
|
+
name: spec.campaign.schedule.name,
|
|
763
|
+
timing: spec.campaign.schedule.timing,
|
|
764
|
+
days: mapDays(spec.campaign.schedule.days),
|
|
765
|
+
timezone: resolveTimezone(spec.campaign.schedule.timezone),
|
|
766
|
+
},
|
|
767
|
+
],
|
|
768
|
+
},
|
|
769
|
+
sequences: [{ steps: seq }],
|
|
770
|
+
email_gap: spec.campaign.email_gap_minutes,
|
|
771
|
+
daily_limit: spec.campaign.daily_limit,
|
|
772
|
+
daily_max_leads: spec.campaign.daily_max_new_leads,
|
|
773
|
+
stop_on_reply: spec.campaign.stop_on_reply ?? true,
|
|
774
|
+
stop_on_auto_reply: spec.campaign.stop_on_auto_reply ?? true,
|
|
775
|
+
insert_unsubscribe_header: spec.campaign.insert_unsubscribe_header ?? true,
|
|
776
|
+
open_tracking: spec.campaign.open_tracking ?? false,
|
|
777
|
+
link_tracking: spec.campaign.link_tracking ?? false,
|
|
778
|
+
email_list: emailList,
|
|
779
|
+
email_tag_list: spec.campaign.email_tag_list,
|
|
780
|
+
};
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
async function findTagByLabel(client, label) {
|
|
784
|
+
// Shared fleet workspaces accumulate hundreds of ownership tags; a single
|
|
785
|
+
// limit=100 page misses older tags and the subsequent create 409s. Page
|
|
786
|
+
// through the full list (bounded) before concluding the tag is absent.
|
|
787
|
+
let cursor;
|
|
788
|
+
let prevFirstId = null;
|
|
789
|
+
for (let page = 0; page < 50; page++) {
|
|
790
|
+
const res = await client.listCustomTags(100, cursor);
|
|
791
|
+
if (!(res.status >= 200 && res.status < 300)) return null;
|
|
792
|
+
const items = Array.isArray(res.body?.items) ? res.body.items : [];
|
|
793
|
+
// A gateway that drops the starting_after param re-serves page 1 forever
|
|
794
|
+
// — detect the repeated page and stop instead of scanning 50 copies.
|
|
795
|
+
const firstId = items[0]?.id ?? null;
|
|
796
|
+
if (firstId != null && firstId === prevFirstId) return null;
|
|
797
|
+
prevFirstId = firstId;
|
|
798
|
+
const found = items.find((tag) => tag.label === label);
|
|
799
|
+
if (found) return found;
|
|
800
|
+
if (items.length < 100) return null;
|
|
801
|
+
const nextCursor =
|
|
802
|
+
res.body?.next_starting_after || items[items.length - 1]?.id;
|
|
803
|
+
if (!nextCursor || nextCursor === cursor) return null;
|
|
804
|
+
cursor = nextCursor;
|
|
805
|
+
}
|
|
806
|
+
return null;
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
async function ensureTag(client, label, description) {
|
|
810
|
+
const found = await findTagByLabel(client, label);
|
|
811
|
+
if (found) return found;
|
|
812
|
+
const created = await client.createCustomTag({ label, description });
|
|
813
|
+
if (created.status >= 200 && created.status < 300) return created.body;
|
|
814
|
+
// 409: lost the existence race. 5xx: the tag may have been created before
|
|
815
|
+
// the gateway gave up (creating calls are not repeated).
|
|
816
|
+
if (created.status === 409 || created.status >= 500) {
|
|
817
|
+
// Lost the existence race (or the list read is degraded): the tag exists
|
|
818
|
+
// upstream — fetch it instead of failing the launch.
|
|
819
|
+
const existing = await findTagByLabel(client, label);
|
|
820
|
+
if (existing) return existing;
|
|
821
|
+
}
|
|
822
|
+
// Tags are best-effort labels on a shared workspace with 10k+ of them —
|
|
823
|
+
// manifest.json is the authoritative ownership record, so an unresolvable
|
|
824
|
+
// tag must not kill the launch.
|
|
825
|
+
console.error(
|
|
826
|
+
`WARNING: could not create or resolve tag ${label}: ${created.status} ${JSON.stringify(created.body)}. ` +
|
|
827
|
+
`Continuing without it — manifest.json remains the authoritative ownership record.`,
|
|
828
|
+
);
|
|
829
|
+
return null;
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
async function assignCampaignTags(client, campaignId, tagIds) {
|
|
833
|
+
const ids = (tagIds || []).filter(Boolean);
|
|
834
|
+
if (!ids.length) {
|
|
835
|
+
console.error(
|
|
836
|
+
"WARNING: no ownership tag ids resolved; skipping tag assignment (manifest.json records ownership).",
|
|
837
|
+
);
|
|
838
|
+
return false;
|
|
839
|
+
}
|
|
840
|
+
const res = await client.toggleTagResource({
|
|
841
|
+
tag_ids: ids,
|
|
842
|
+
resource_type: 2,
|
|
843
|
+
resource_ids: [campaignId],
|
|
844
|
+
assign: true,
|
|
845
|
+
});
|
|
846
|
+
if (!(res.status >= 200 && res.status < 300)) {
|
|
847
|
+
console.error(
|
|
848
|
+
`WARNING: failed to assign campaign tags: ${res.status} ${JSON.stringify(res.body)}. ` +
|
|
849
|
+
`Tag assignment is best-effort; manifest.json remains the authoritative ownership record.`,
|
|
850
|
+
);
|
|
851
|
+
return false;
|
|
852
|
+
}
|
|
853
|
+
return true;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
async function resolveReusableCampaign(
|
|
857
|
+
client,
|
|
858
|
+
manifestCampaignId,
|
|
859
|
+
campaignName,
|
|
860
|
+
ownership,
|
|
861
|
+
) {
|
|
862
|
+
if (manifestCampaignId) {
|
|
863
|
+
const current = await client.getCampaign(manifestCampaignId);
|
|
864
|
+
if (current.status >= 200 && current.status < 300) {
|
|
865
|
+
assertCampaignNotForeign(
|
|
866
|
+
current.body,
|
|
867
|
+
ownership,
|
|
868
|
+
`campaign ${manifestCampaignId}`,
|
|
869
|
+
);
|
|
870
|
+
return { campaignId: manifestCampaignId, reused: true };
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
const list = await client.listCampaigns(100);
|
|
875
|
+
if (
|
|
876
|
+
!(list.status >= 200 && list.status < 300) ||
|
|
877
|
+
!Array.isArray(list.body?.items)
|
|
878
|
+
) {
|
|
879
|
+
return { campaignId: null, reused: false };
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
const matches = list.body.items.filter(
|
|
883
|
+
(campaign) => campaign?.name === campaignName && campaign?.id,
|
|
884
|
+
);
|
|
885
|
+
for (const match of matches) {
|
|
886
|
+
const details = await client.getCampaign(match.id);
|
|
887
|
+
if (!(details.status >= 200 && details.status < 300)) continue;
|
|
888
|
+
if (campaignOwnershipStatus(details.body, ownership) === "owned") {
|
|
889
|
+
return { campaignId: match.id, reused: true };
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
return { campaignId: null, reused: false };
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
function computeExpectedDailyCapacity(spec) {
|
|
897
|
+
return spec.campaign.daily_limit;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
// CTA URL placeholders the launcher refuses to ship live. These come from
|
|
901
|
+
// agents copying run-spec-template.json or PROFILE-template.md verbatim
|
|
902
|
+
// without substituting their product's real domain. We've seen all of these
|
|
903
|
+
// in production:
|
|
904
|
+
// - `<instance-id>` / `<slug>` / `<product>` — angle-bracket template tokens
|
|
905
|
+
// - `.instance-id.` / `.<slug>.` subdomain leftovers (no brackets, but the
|
|
906
|
+
// literal placeholder word survives in the URL)
|
|
907
|
+
// - example.com / example.org / *.example — RFC test/example domains
|
|
908
|
+
const URL_PLACEHOLDER_PATTERNS = [
|
|
909
|
+
{
|
|
910
|
+
re: /<[a-z][a-z0-9_-]{0,40}>/i,
|
|
911
|
+
label:
|
|
912
|
+
"angle-bracket placeholder like <instance-id> / <slug> / <your-...> in body",
|
|
913
|
+
},
|
|
914
|
+
{
|
|
915
|
+
re: /\.instance-id\b|\binstance-id\.[pn]\.egbe\.app/i,
|
|
916
|
+
label:
|
|
917
|
+
"literal '.instance-id.' subdomain — replace with the real instance public_id",
|
|
918
|
+
},
|
|
919
|
+
{
|
|
920
|
+
re: /\bexample\.(?:com|org|net)\b|\bhttps?:\/\/[a-z0-9-]+\.example\b/i,
|
|
921
|
+
label: "example.com / example.org / *.example placeholder domain",
|
|
922
|
+
},
|
|
923
|
+
{
|
|
924
|
+
re: /\byour-(?:product|domain|slug|landing|site)\b/i,
|
|
925
|
+
label: "literal 'your-product' / 'your-domain' placeholder",
|
|
926
|
+
},
|
|
927
|
+
];
|
|
928
|
+
|
|
929
|
+
function preflightCtaUrls(spec) {
|
|
930
|
+
const steps = spec.sequence?.steps || [];
|
|
931
|
+
if (!steps.length) return;
|
|
932
|
+
const offenses = [];
|
|
933
|
+
for (const [idx, step] of steps.entries()) {
|
|
934
|
+
const haystack = `${step.subject || ""}\n${step.body || ""}`;
|
|
935
|
+
for (const pat of URL_PLACEHOLDER_PATTERNS) {
|
|
936
|
+
const m = haystack.match(pat.re);
|
|
937
|
+
if (m) {
|
|
938
|
+
offenses.push(`step ${idx + 1}: ${pat.label} (matched "${m[0]}")`);
|
|
939
|
+
break; // one offense per step is enough — operator gets the idea
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
if (offenses.length) {
|
|
944
|
+
fail(
|
|
945
|
+
`Copy preflight failed: CTA URLs contain unfilled template placeholders. ` +
|
|
946
|
+
`These would ship live emails pointing at non-existent landing pages, ` +
|
|
947
|
+
`producing real sends that recipients can't act on. ` +
|
|
948
|
+
`Fix the run-spec body so each link points at the real product URL ` +
|
|
949
|
+
`(e.g. https://rolloo.app/, not https://<slug>.<instance-id>.n.egbe.app ` +
|
|
950
|
+
`and not https://example.com/demo). Detected: ${offenses.join("; ")}.`,
|
|
951
|
+
);
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
// Cosmetic source-data concerns are warnings, not universal copy policy.
|
|
956
|
+
// Missing values actually referenced by the sequence remain a payload error.
|
|
957
|
+
function preflightLeadNames(spec, leads, leadsFileLabel, opts = {}) {
|
|
958
|
+
const checks = leadNameChecksForSteps(spec.sequence?.steps);
|
|
959
|
+
const problems = findLeadNameProblems(leads, {
|
|
960
|
+
...checks, skipEmails: opts.alreadyImportedEmails || null,
|
|
961
|
+
});
|
|
962
|
+
const warnings = problems.filter(p => p.severity === "warning");
|
|
963
|
+
if (warnings.length) console.error("WARNING: Review sourced names: " +
|
|
964
|
+
warnings.map(p => p.email + ": " + p.field + " " + JSON.stringify(p.value) + " (" + p.problem + ")").join("; "));
|
|
965
|
+
const missing = findLeadVariableProblems(spec.sequence?.steps, leads, {
|
|
966
|
+
skipEmails: opts.alreadyImportedEmails || null,
|
|
967
|
+
});
|
|
968
|
+
if (missing.length) fail("Unresolved sequence variables in " + leadsFileLabel + ": " +
|
|
969
|
+
missing.map(p => p.email + ": " + p.field).join("; "));
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
// Cold outreach must never send from a trust-reputation domain. @egbe.app and
|
|
973
|
+
// @egbe.dev carry verification/onboarding links and relationship mail; running
|
|
974
|
+
// cold campaigns or warmup through them poisons the deliverability of that mail
|
|
975
|
+
// (GH #752). Cold sending belongs on a dedicated cold domain (e.g.
|
|
976
|
+
// smartcreateinc.org). See docs/email-reputation-surfaces.md. This guard is the
|
|
977
|
+
// code-level enforcement — it holds even if an agent hand-supplies an egbe.app
|
|
978
|
+
// address in the run spec.
|
|
979
|
+
const BLOCKED_SENDER_DOMAINS = ["egbe.app", "egbe.dev"];
|
|
980
|
+
|
|
981
|
+
function senderEmailOf(entry) {
|
|
982
|
+
return normalizeEmail(typeof entry === "string" ? entry : entry && entry.email);
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
function isBlockedSender(entry) {
|
|
986
|
+
const email = senderEmailOf(entry);
|
|
987
|
+
const at = email.lastIndexOf("@");
|
|
988
|
+
const domain = at >= 0 ? email.slice(at + 1) : "";
|
|
989
|
+
return BLOCKED_SENDER_DOMAINS.includes(domain);
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
// Drop any trust-domain mailbox from a candidate sender list. Logs what it
|
|
993
|
+
// removed and hard-fails (never silently proceeds) if nothing else remains,
|
|
994
|
+
// pointing the operator at the dedicated cold domain. Accepts either bare
|
|
995
|
+
// email strings or account objects with an `email` field.
|
|
996
|
+
function excludeProtectedSenders(entries) {
|
|
997
|
+
const blocked = entries.filter(isBlockedSender);
|
|
998
|
+
const allowed = entries.filter((entry) => !isBlockedSender(entry));
|
|
999
|
+
if (blocked.length) {
|
|
1000
|
+
console.error(
|
|
1001
|
+
`Excluded ${blocked.length} trust-domain sender(s) from this cold campaign: ` +
|
|
1002
|
+
`${blocked.map(senderEmailOf).join(", ")}. @egbe.app / @egbe.dev are reserved for ` +
|
|
1003
|
+
`verification and relationship mail (docs/email-reputation-surfaces.md, GH #752).`,
|
|
1004
|
+
);
|
|
1005
|
+
}
|
|
1006
|
+
if (!allowed.length) {
|
|
1007
|
+
fail(
|
|
1008
|
+
"No eligible cold-outreach sending accounts remain after excluding trust-domain " +
|
|
1009
|
+
"(@egbe.app / @egbe.dev) mailboxes. Cold outreach must send from a dedicated cold " +
|
|
1010
|
+
"domain (e.g. smartcreateinc.org). Add cold-domain mailboxes in Instantly and retry.",
|
|
1011
|
+
);
|
|
1012
|
+
}
|
|
1013
|
+
return allowed;
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
async function listAllAccounts(client) {
|
|
1017
|
+
// Accounts have no status filter here: a mailbox named in the run spec but
|
|
1018
|
+
// paused in Instantly must be reported as inactive, not as missing.
|
|
1019
|
+
const all = [];
|
|
1020
|
+
let cursor;
|
|
1021
|
+
let prevFirst = null;
|
|
1022
|
+
for (let page = 0; page < 20; page++) {
|
|
1023
|
+
const res = await client.listAccounts(100, cursor ? { starting_after: cursor } : {});
|
|
1024
|
+
if (!(res.status >= 200 && res.status < 300))
|
|
1025
|
+
fail(`List accounts failed: ${res.status} ${JSON.stringify(res.body)}`);
|
|
1026
|
+
const items = Array.isArray(res.body?.items) ? res.body.items : [];
|
|
1027
|
+
// A gateway that drops starting_after re-serves page 1: stop there.
|
|
1028
|
+
const first = items[0]?.email ?? null;
|
|
1029
|
+
if (first != null && first === prevFirst) break;
|
|
1030
|
+
prevFirst = first;
|
|
1031
|
+
all.push(...items);
|
|
1032
|
+
const next = res.body?.next_starting_after;
|
|
1033
|
+
if (items.length < 100 || !next || next === cursor) break;
|
|
1034
|
+
cursor = next;
|
|
1035
|
+
}
|
|
1036
|
+
return all;
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
async function pickEmailAccounts(client, spec) {
|
|
1040
|
+
const { senders, error } = selectSenders(spec.campaign, await listAllAccounts(client));
|
|
1041
|
+
if (error) fail(error);
|
|
1042
|
+
// Enforce the trust-domain boundary on the chosen senders (GH #752).
|
|
1043
|
+
return excludeProtectedSenders(senders);
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
async function main() {
|
|
1047
|
+
const args = parseArgs(process.argv);
|
|
1048
|
+
const runSpecPath = args["run-spec"];
|
|
1049
|
+
if (!runSpecPath) fail("Missing --run-spec");
|
|
1050
|
+
const resolvedRunSpecPath = path.resolve(runSpecPath);
|
|
1051
|
+
const runDir = path.dirname(resolvedRunSpecPath);
|
|
1052
|
+
ensureDir(runDir);
|
|
1053
|
+
const spec = readJson(resolvedRunSpecPath);
|
|
1054
|
+
if (!spec) fail("Run spec file not found or invalid JSON");
|
|
1055
|
+
if (!spec.run_key) fail("run_key is required");
|
|
1056
|
+
if (!spec.sequence?.steps?.length) fail("sequence.steps is required");
|
|
1057
|
+
if (!spec.leads_file) fail("leads_file is required");
|
|
1058
|
+
|
|
1059
|
+
const manifestPath = path.join(runDir, "manifest.json");
|
|
1060
|
+
const manifest = readJson(manifestPath, {
|
|
1061
|
+
run_key: spec.run_key,
|
|
1062
|
+
agent_id: spec.agent_id || "unknown-agent",
|
|
1063
|
+
created_at: isoNow(),
|
|
1064
|
+
campaign: {},
|
|
1065
|
+
ownership: {},
|
|
1066
|
+
state_files: {},
|
|
1067
|
+
});
|
|
1068
|
+
reconcileRunIdentity(manifest, spec);
|
|
1069
|
+
reconcileInstantlyAttribution(manifest, {
|
|
1070
|
+
runKey: spec.run_key,
|
|
1071
|
+
campaignId: manifest.campaign?.id,
|
|
1072
|
+
label: "manifest",
|
|
1073
|
+
});
|
|
1074
|
+
validateLaunchConfig(spec);
|
|
1075
|
+
// Read before initDb: an absent history after earlier sends is not a clean project.
|
|
1076
|
+
readOptOutHistory(runDir);
|
|
1077
|
+
|
|
1078
|
+
// Materialize OS creds from process.env into the run-scoped .env so cron
|
|
1079
|
+
// jobs (which run with their own environment) can find them. This is also
|
|
1080
|
+
// what stops the agent from falling back to send-email when it can't see the
|
|
1081
|
+
// creds in the run directory.
|
|
1082
|
+
const envInit = ensureRunEnv(runDir);
|
|
1083
|
+
loadRunEnv(runDir);
|
|
1084
|
+
const os = getOsConfig();
|
|
1085
|
+
if (os.missing.length) {
|
|
1086
|
+
fail(
|
|
1087
|
+
`Cold-outbound preflight failed: ${os.missing.join(" and ")} not found in process.env or ${envInit.envFilePath}. ` +
|
|
1088
|
+
`This skill talks to Instantly through the OS outreach capability; it does NOT use a personal Instantly API key. ` +
|
|
1089
|
+
`Set OS_URL and OS_API_KEY in the project credentials (or run .env). ` +
|
|
1090
|
+
`Do NOT fall back to send-email/Mailu for cold outbound — that is for manual 1:1 only. ` +
|
|
1091
|
+
`Report the missing credential; do not continue with an unrelated sending service.`,
|
|
1092
|
+
);
|
|
1093
|
+
}
|
|
1094
|
+
const stateDir = path.join(runDir, "state");
|
|
1095
|
+
ensureDir(stateDir);
|
|
1096
|
+
const dbPath = initDb(runDir);
|
|
1097
|
+
|
|
1098
|
+
spec.created_at = spec.created_at || isoNow();
|
|
1099
|
+
spec.campaign = spec.campaign || {};
|
|
1100
|
+
// An explicitly authored campaign.name wins; the product|segment|offer|date
|
|
1101
|
+
// formula is only the fallback. (Two same-day runs sharing product+segment+
|
|
1102
|
+
// offer used to collide onto one auto-built name and reuse the wrong
|
|
1103
|
+
// campaign via the name-based lookup.)
|
|
1104
|
+
const explicitCampaignName =
|
|
1105
|
+
typeof spec.campaign.name === "string" && spec.campaign.name.trim();
|
|
1106
|
+
spec.campaign.name = explicitCampaignName || buildCampaignName(spec);
|
|
1107
|
+
spec.campaign.instance_count = 1;
|
|
1108
|
+
|
|
1109
|
+
// campaign.verify_leads used to drive BOTH verification-on-import and the
|
|
1110
|
+
// blocking wait gate; disabling it to bypass a hung wait also silently
|
|
1111
|
+
// skipped verification itself. Two flags now, with the legacy one honored
|
|
1112
|
+
// as a fallback for existing run-specs.
|
|
1113
|
+
const verifyOnImport =
|
|
1114
|
+
spec.campaign.verify_leads_on_import ?? spec.campaign.verify_leads ?? true;
|
|
1115
|
+
const waitForVerify =
|
|
1116
|
+
spec.campaign.wait_for_verification ?? spec.campaign.verify_leads ?? true;
|
|
1117
|
+
|
|
1118
|
+
const summaryPath = path.join(runDir, "launch-summary.json");
|
|
1119
|
+
const importedLeadsPath = path.join(stateDir, "imported-leads.json");
|
|
1120
|
+
const sequenceStatePath = path.join(stateDir, "sequence-state.json");
|
|
1121
|
+
const ownershipPath = path.join(stateDir, "ownership.json");
|
|
1122
|
+
const client = new InstantlyClient({
|
|
1123
|
+
connectionEnvFile: envInit.envFilePath,
|
|
1124
|
+
osApiKey: os.osApiKey,
|
|
1125
|
+
osUrl: os.osUrl,
|
|
1126
|
+
verbose: !!args.verbose,
|
|
1127
|
+
});
|
|
1128
|
+
const optOutStopResults = [];
|
|
1129
|
+
const readMembers = async (campaignId) => {
|
|
1130
|
+
try { return await client.listLeadEmails(campaignId); }
|
|
1131
|
+
catch { return { ok: false, emails: new Set() }; }
|
|
1132
|
+
};
|
|
1133
|
+
const containOptOuts = async (emails) => {
|
|
1134
|
+
for (const email of emails) {
|
|
1135
|
+
const result = await stopConfirmedRecipient(client, runDir, email);
|
|
1136
|
+
optOutStopResults.push({ email, ...result, checked_at: isoNow() });
|
|
1137
|
+
// Keep the outcome even if a later import/read gate aborts this attempt.
|
|
1138
|
+
// Do not turn the earlier launch's counts/timestamp into a new observation.
|
|
1139
|
+
writeJson(summaryPath, {
|
|
1140
|
+
...readJson(summaryPath, {}),
|
|
1141
|
+
run_key: spec.run_key,
|
|
1142
|
+
campaign_id: manifest.campaign.id,
|
|
1143
|
+
opt_out_stop_results: optOutStopResults,
|
|
1144
|
+
});
|
|
1145
|
+
if (!result.ok) console.error(`WARNING: Refusal saved but stopping ${email} is not confirmed; inspect opt_out_stop_results.`);
|
|
1146
|
+
}
|
|
1147
|
+
};
|
|
1148
|
+
const requireMembers = async (campaignId) => {
|
|
1149
|
+
const members = await readMembers(campaignId);
|
|
1150
|
+
if (!members.ok) {
|
|
1151
|
+
// Blocking an import/activation does not stop an already-active run.
|
|
1152
|
+
// Every membership gate must still reach the scoped refusal stop path.
|
|
1153
|
+
await containOptOuts(readOptOutHistory(runDir).emails);
|
|
1154
|
+
fail("Cannot establish campaign membership; no import or activation attempted." +
|
|
1155
|
+
(optOutStopResults.length ? " Known-refusal stop results are recorded in launch-summary.json." : ""));
|
|
1156
|
+
}
|
|
1157
|
+
return members;
|
|
1158
|
+
};
|
|
1159
|
+
const chosenAccounts = await pickEmailAccounts(client, spec);
|
|
1160
|
+
const emailList = chosenAccounts
|
|
1161
|
+
.map((acc) => (typeof acc === "string" ? acc : acc.email))
|
|
1162
|
+
.filter(Boolean);
|
|
1163
|
+
if (!emailList.length)
|
|
1164
|
+
fail(
|
|
1165
|
+
"No sending accounts resolved. Cannot launch a campaign without at least one sending account.",
|
|
1166
|
+
);
|
|
1167
|
+
|
|
1168
|
+
const throughputPreflight = { expected_daily_capacity: computeExpectedDailyCapacity(spec) };
|
|
1169
|
+
preflightCtaUrls(spec);
|
|
1170
|
+
|
|
1171
|
+
// The leads file is read and checked before anything is written to
|
|
1172
|
+
// Instantly (tags, campaign, leads), so a bad lead fails the run clean.
|
|
1173
|
+
let leadsPath = path.isAbsolute(spec.leads_file)
|
|
1174
|
+
? spec.leads_file
|
|
1175
|
+
: path.resolve(path.dirname(resolvedRunSpecPath), spec.leads_file);
|
|
1176
|
+
if (!fs.existsSync(leadsPath) && !path.isAbsolute(spec.leads_file)) {
|
|
1177
|
+
// Relative leads_file resolves against the run-spec's directory (so cron
|
|
1178
|
+
// jobs and reruns agree on the location), but operators invoking the CLI
|
|
1179
|
+
// from elsewhere routinely keep leads.json next to their CWD — accept
|
|
1180
|
+
// that with a warning instead of failing on a doubled path.
|
|
1181
|
+
const cwdLeadsPath = path.resolve(process.cwd(), spec.leads_file);
|
|
1182
|
+
if (cwdLeadsPath !== leadsPath && fs.existsSync(cwdLeadsPath)) {
|
|
1183
|
+
console.error(
|
|
1184
|
+
`WARNING: leads_file not found next to the run-spec (${leadsPath}); ` +
|
|
1185
|
+
`using ${cwdLeadsPath} from the current directory. The canonical ` +
|
|
1186
|
+
`location is relative to the run-spec's directory.`,
|
|
1187
|
+
);
|
|
1188
|
+
leadsPath = cwdLeadsPath;
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
if (!fs.existsSync(leadsPath)) {
|
|
1192
|
+
fail(
|
|
1193
|
+
`Leads file not found: ${spec.leads_file} (tried ${leadsPath}` +
|
|
1194
|
+
(path.isAbsolute(spec.leads_file)
|
|
1195
|
+
? ""
|
|
1196
|
+
: ` and ${path.resolve(process.cwd(), spec.leads_file)}`) +
|
|
1197
|
+
`). Relative leads_file paths resolve against the run-spec's directory.`,
|
|
1198
|
+
);
|
|
1199
|
+
}
|
|
1200
|
+
const leadsRaw = JSON.parse(fs.readFileSync(leadsPath, "utf8"));
|
|
1201
|
+
const inputLeads = Array.isArray(leadsRaw) ? leadsRaw : leadsRaw.leads || [];
|
|
1202
|
+
const optOutFilter = filterConfirmedOptOuts(runDir, inputLeads);
|
|
1203
|
+
const leads = optOutFilter.leads;
|
|
1204
|
+
// Leads already imported into the manifest campaign (rerun) are skipped.
|
|
1205
|
+
const priorCampaignId = manifest.campaign?.id || null;
|
|
1206
|
+
const alreadyImportedEmails = new Set();
|
|
1207
|
+
if (priorCampaignId) {
|
|
1208
|
+
const priorHashes = new Set(
|
|
1209
|
+
readJson(path.join(stateDir, "imported-leads.json"), []),
|
|
1210
|
+
);
|
|
1211
|
+
for (const lead of leads) {
|
|
1212
|
+
const email = normalizeEmail(lead?.email);
|
|
1213
|
+
if (
|
|
1214
|
+
email &&
|
|
1215
|
+
priorHashes.has(
|
|
1216
|
+
sha256(JSON.stringify({ campaignId: priorCampaignId, email })),
|
|
1217
|
+
)
|
|
1218
|
+
) {
|
|
1219
|
+
alreadyImportedEmails.add(email);
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
preflightLeadNames(spec, leads, spec.leads_file, {
|
|
1224
|
+
campaignId: priorCampaignId,
|
|
1225
|
+
alreadyImportedEmails,
|
|
1226
|
+
});
|
|
1227
|
+
|
|
1228
|
+
const primaryAccount =
|
|
1229
|
+
typeof chosenAccounts[0] === "string"
|
|
1230
|
+
? { email: chosenAccounts[0] }
|
|
1231
|
+
: chosenAccounts[0];
|
|
1232
|
+
const inferredSenderName =
|
|
1233
|
+
spec.reply_policy?.sender_name ||
|
|
1234
|
+
spec.sender_name ||
|
|
1235
|
+
deriveSenderName({
|
|
1236
|
+
email: primaryAccount.email,
|
|
1237
|
+
firstName: primaryAccount.first_name,
|
|
1238
|
+
lastName: primaryAccount.last_name,
|
|
1239
|
+
}) ||
|
|
1240
|
+
null;
|
|
1241
|
+
|
|
1242
|
+
// {{senderName}} is resolved at build time to ONE name, but the campaign
|
|
1243
|
+
// rotates sends across every mailbox in email_list — a real run shipped
|
|
1244
|
+
// follow-ups signed with the wrong person's name before this warning.
|
|
1245
|
+
const usesSenderNameToken = spec.sequence.steps.some((step) =>
|
|
1246
|
+
/{{\s*senderName\s*}}/.test(`${step.subject || ""}\n${step.body || ""}`),
|
|
1247
|
+
);
|
|
1248
|
+
if (usesSenderNameToken && !inferredSenderName)
|
|
1249
|
+
fail("{{senderName}} requires an explicit sender name or a verified account name; email addresses are not names.");
|
|
1250
|
+
if (usesSenderNameToken && emailList.length > 1) {
|
|
1251
|
+
console.error(
|
|
1252
|
+
`WARNING: {{senderName}} is a launcher build-time token and ${emailList.length} sending mailboxes ` +
|
|
1253
|
+
`rotate on this campaign — every send will be signed "${inferredSenderName}" (the primary ` +
|
|
1254
|
+
`mailbox's name) regardless of which mailbox sends. Prefer a name-less sign-off ("Thanks,") ` +
|
|
1255
|
+
`or set campaign.max_sending_accounts: 1 when the signature name matters.`,
|
|
1256
|
+
);
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
spec.reply_policy = spec.reply_policy || {};
|
|
1260
|
+
spec.reply_policy.sender_name = inferredSenderName;
|
|
1261
|
+
spec.reply_policy.reply_from_email =
|
|
1262
|
+
spec.reply_policy.reply_from_email || primaryAccount.email;
|
|
1263
|
+
spec.campaign.email_list = emailList;
|
|
1264
|
+
// Derived values (campaign name, sender identity, chosen mailboxes) used to
|
|
1265
|
+
// be written back into the operator's run-spec file, destroying the
|
|
1266
|
+
// authored input. They land in state/resolved-run-spec.json instead;
|
|
1267
|
+
// post-launch readers (reply-worker) overlay it when present.
|
|
1268
|
+
const resolvedSpecPath = path.join(stateDir, "resolved-run-spec.json");
|
|
1269
|
+
writeJson(resolvedSpecPath, spec);
|
|
1270
|
+
|
|
1271
|
+
const ownership = {
|
|
1272
|
+
run_tag_label: `oc-run-${slugify(spec.run_key, 48)}`,
|
|
1273
|
+
agent_tag_label: `oc-agent-${slugify(spec.agent_id || "unknown-agent", 48)}`,
|
|
1274
|
+
run_tag_description: `Owned by OpenClaw run ${spec.run_key}`,
|
|
1275
|
+
agent_tag_description: `Owned by OpenClaw agent ${spec.agent_id || "unknown-agent"}`,
|
|
1276
|
+
};
|
|
1277
|
+
|
|
1278
|
+
// Rerun path: reuse tag ids recorded by a previous successful launch of
|
|
1279
|
+
// this run. Shared fleet workspaces hold thousands of tags, so re-resolving
|
|
1280
|
+
// by label is unreliable (and a create retries into a 409).
|
|
1281
|
+
const storedOwnership = readJson(ownershipPath, null);
|
|
1282
|
+
if (
|
|
1283
|
+
storedOwnership &&
|
|
1284
|
+
storedOwnership.run_tag_label === ownership.run_tag_label &&
|
|
1285
|
+
storedOwnership.agent_tag_label === ownership.agent_tag_label &&
|
|
1286
|
+
storedOwnership.run_tag_id &&
|
|
1287
|
+
storedOwnership.agent_tag_id
|
|
1288
|
+
) {
|
|
1289
|
+
ownership.run_tag_id = storedOwnership.run_tag_id;
|
|
1290
|
+
ownership.agent_tag_id = storedOwnership.agent_tag_id;
|
|
1291
|
+
} else {
|
|
1292
|
+
// Cross-run tag cache: agent tags are shared by every run of this agent,
|
|
1293
|
+
// and huge shared workspaces make label lookups unreliable (create 409s
|
|
1294
|
+
// while the paged list cannot surface the tag). Cache label->id locally.
|
|
1295
|
+
const tagCachePath = path.join(runDir, "..", "tag-cache.json");
|
|
1296
|
+
const tagCache = readJson(tagCachePath, {});
|
|
1297
|
+
const resolveTag = async (label, description) => {
|
|
1298
|
+
if (tagCache[label]) return { id: tagCache[label] };
|
|
1299
|
+
const tag = await ensureTag(client, label, description);
|
|
1300
|
+
if (!tag?.id) return null;
|
|
1301
|
+
tagCache[label] = tag.id;
|
|
1302
|
+
writeJson(tagCachePath, tagCache);
|
|
1303
|
+
return tag;
|
|
1304
|
+
};
|
|
1305
|
+
const runTag = await resolveTag(
|
|
1306
|
+
ownership.run_tag_label,
|
|
1307
|
+
ownership.run_tag_description,
|
|
1308
|
+
);
|
|
1309
|
+
const agentTag = await resolveTag(
|
|
1310
|
+
ownership.agent_tag_label,
|
|
1311
|
+
ownership.agent_tag_description,
|
|
1312
|
+
);
|
|
1313
|
+
ownership.run_tag_id = runTag?.id || null;
|
|
1314
|
+
ownership.agent_tag_id = agentTag?.id || null;
|
|
1315
|
+
}
|
|
1316
|
+
writeJson(ownershipPath, ownership);
|
|
1317
|
+
|
|
1318
|
+
const payload = buildCampaignPayload(spec, emailList, inferredSenderName);
|
|
1319
|
+
const campaignFingerprint = sha256(
|
|
1320
|
+
stableStringify({
|
|
1321
|
+
segment: spec.segment,
|
|
1322
|
+
offer: spec.offer,
|
|
1323
|
+
campaign: spec.campaign,
|
|
1324
|
+
}),
|
|
1325
|
+
);
|
|
1326
|
+
const desiredSequenceFingerprint = sequenceFingerprint(
|
|
1327
|
+
spec,
|
|
1328
|
+
emailList,
|
|
1329
|
+
inferredSenderName,
|
|
1330
|
+
);
|
|
1331
|
+
const existingLeadHashes = new Set(readJson(importedLeadsPath, []));
|
|
1332
|
+
const previousSequenceState = readJson(sequenceStatePath, {});
|
|
1333
|
+
|
|
1334
|
+
const reuseState = await resolveReusableCampaign(
|
|
1335
|
+
client,
|
|
1336
|
+
manifest.campaign.id || null,
|
|
1337
|
+
spec.campaign.name,
|
|
1338
|
+
ownership,
|
|
1339
|
+
);
|
|
1340
|
+
let campaignId = reuseState.campaignId;
|
|
1341
|
+
let reused = reuseState.reused;
|
|
1342
|
+
|
|
1343
|
+
let sequenceAction = "unchanged";
|
|
1344
|
+
if (!campaignId) {
|
|
1345
|
+
const created = await client.createCampaign(payload);
|
|
1346
|
+
if (created.status >= 500) {
|
|
1347
|
+
// The campaign may exist anyway, without ownership tags, and other
|
|
1348
|
+
// campaigns can share its name: list them for the person to choose.
|
|
1349
|
+
const list = await client.listCampaigns(100);
|
|
1350
|
+
const readable = list.status >= 200 && list.status < 300 && Array.isArray(list.body?.items);
|
|
1351
|
+
const sameName = readable ? list.body.items.filter((c) => c?.name === payload.name) : [];
|
|
1352
|
+
fail(
|
|
1353
|
+
`Create campaign returned ${created.status}; it may have been created anyway. ` +
|
|
1354
|
+
(!readable
|
|
1355
|
+
? `The campaign list could not be read (${list.status}): check Instantly by hand for "${payload.name}". `
|
|
1356
|
+
: sameName.length
|
|
1357
|
+
? `Campaigns named "${payload.name}": ` +
|
|
1358
|
+
sameName.map((c) => `${c.id} (created ${c.timestamp_created || "?"})`).join(", ") +
|
|
1359
|
+
". "
|
|
1360
|
+
: `The campaign list shows none named "${payload.name}". `) +
|
|
1361
|
+
`Before re-running, delete only the one created at the time of this run, if any.`,
|
|
1362
|
+
);
|
|
1363
|
+
}
|
|
1364
|
+
if (!(created.status >= 200 && created.status < 300))
|
|
1365
|
+
fail(
|
|
1366
|
+
`Create campaign failed: ${created.status} ${JSON.stringify(created.body)}`,
|
|
1367
|
+
);
|
|
1368
|
+
campaignId = created.body?.id;
|
|
1369
|
+
sequenceAction = "created-with-campaign";
|
|
1370
|
+
} else {
|
|
1371
|
+
if (
|
|
1372
|
+
previousSequenceState.applied_fingerprint &&
|
|
1373
|
+
previousSequenceState.applied_fingerprint !==
|
|
1374
|
+
desiredSequenceFingerprint &&
|
|
1375
|
+
!args["allow-sequence-update"]
|
|
1376
|
+
) {
|
|
1377
|
+
fail(
|
|
1378
|
+
"Desired sequence differs from the last applied sequence. Re-run with --allow-sequence-update to patch the live campaign intentionally.",
|
|
1379
|
+
);
|
|
1380
|
+
}
|
|
1381
|
+
const patched = await client.patchCampaign(campaignId, payload);
|
|
1382
|
+
if (!(patched.status >= 200 && patched.status < 300))
|
|
1383
|
+
fail(
|
|
1384
|
+
`Patch campaign failed: ${patched.status} ${JSON.stringify(patched.body)}`,
|
|
1385
|
+
);
|
|
1386
|
+
sequenceAction =
|
|
1387
|
+
previousSequenceState.applied_fingerprint === desiredSequenceFingerprint
|
|
1388
|
+
? "verified-no-change"
|
|
1389
|
+
: "patched-intentionally";
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
const tagsAssigned = await assignCampaignTags(client, campaignId, [
|
|
1393
|
+
ownership.run_tag_id,
|
|
1394
|
+
ownership.agent_tag_id,
|
|
1395
|
+
]);
|
|
1396
|
+
ownership.tags_assigned = tagsAssigned;
|
|
1397
|
+
writeJson(ownershipPath, ownership);
|
|
1398
|
+
|
|
1399
|
+
manifest.campaign = {
|
|
1400
|
+
first_launched_at: manifest.campaign?.first_launched_at || null,
|
|
1401
|
+
last_activated_at: manifest.campaign?.last_activated_at || null,
|
|
1402
|
+
id: campaignId,
|
|
1403
|
+
name: spec.campaign.name,
|
|
1404
|
+
fingerprint: campaignFingerprint,
|
|
1405
|
+
sequence_fingerprint: desiredSequenceFingerprint,
|
|
1406
|
+
reused,
|
|
1407
|
+
owned_by_run: true,
|
|
1408
|
+
last_prepared_at: isoNow(),
|
|
1409
|
+
email_list: emailList,
|
|
1410
|
+
primary_email: primaryAccount.email,
|
|
1411
|
+
sender_name: inferredSenderName,
|
|
1412
|
+
};
|
|
1413
|
+
const attributionBinding = reconcileInstantlyAttribution(manifest, {
|
|
1414
|
+
runKey: spec.run_key,
|
|
1415
|
+
campaignId,
|
|
1416
|
+
label: "manifest",
|
|
1417
|
+
});
|
|
1418
|
+
manifest.ownership = ownership;
|
|
1419
|
+
manifest.state_files.imported_leads = path.relative(
|
|
1420
|
+
runDir,
|
|
1421
|
+
importedLeadsPath,
|
|
1422
|
+
);
|
|
1423
|
+
manifest.state_files.sequence_state = path.relative(
|
|
1424
|
+
runDir,
|
|
1425
|
+
sequenceStatePath,
|
|
1426
|
+
);
|
|
1427
|
+
manifest.state_files.ownership = path.relative(runDir, ownershipPath);
|
|
1428
|
+
manifest.state_files.resolved_spec = path.relative(runDir, resolvedSpecPath);
|
|
1429
|
+
// Register carry-over path unconditionally — replenish-leads.js looks here
|
|
1430
|
+
// first, falls back to Apollo if the file is missing or empty.
|
|
1431
|
+
manifest.state_files.carry_over_leads = path.relative(
|
|
1432
|
+
runDir,
|
|
1433
|
+
path.join(stateDir, "carry-over-leads.json"),
|
|
1434
|
+
);
|
|
1435
|
+
writeJson(manifestPath, manifest);
|
|
1436
|
+
writeJson(sequenceStatePath, {
|
|
1437
|
+
desired_fingerprint: desiredSequenceFingerprint,
|
|
1438
|
+
applied_fingerprint: desiredSequenceFingerprint,
|
|
1439
|
+
sequence_action: sequenceAction,
|
|
1440
|
+
sender_name: inferredSenderName,
|
|
1441
|
+
email_list: emailList,
|
|
1442
|
+
updated_at: isoNow(),
|
|
1443
|
+
});
|
|
1444
|
+
|
|
1445
|
+
upsertRun(dbPath, {
|
|
1446
|
+
run_key: spec.run_key,
|
|
1447
|
+
product_name: spec.product?.name || spec.product_name,
|
|
1448
|
+
segment_name:
|
|
1449
|
+
spec.segment?.name || spec.segment?.description || spec.segment,
|
|
1450
|
+
offer_headline:
|
|
1451
|
+
spec.offer?.headline ||
|
|
1452
|
+
spec.offer?.name ||
|
|
1453
|
+
spec.offer?.description ||
|
|
1454
|
+
spec.offer,
|
|
1455
|
+
campaign_id: campaignId,
|
|
1456
|
+
campaign_name: spec.campaign.name,
|
|
1457
|
+
status: "draft",
|
|
1458
|
+
created_at: manifest.created_at || spec.created_at,
|
|
1459
|
+
});
|
|
1460
|
+
|
|
1461
|
+
const dedup = new Map();
|
|
1462
|
+
for (const lead of leads) {
|
|
1463
|
+
const email = normalizeEmail(lead.email);
|
|
1464
|
+
if (!email) continue;
|
|
1465
|
+
if (!dedup.has(email)) {
|
|
1466
|
+
const normalizedLead = { ...lead, email };
|
|
1467
|
+
dedup.set(email, normalizedLead);
|
|
1468
|
+
upsertLead(dbPath, normalizedLead);
|
|
1469
|
+
upsertRunLead(dbPath, {
|
|
1470
|
+
run_key: spec.run_key,
|
|
1471
|
+
email,
|
|
1472
|
+
campaign_id: campaignId,
|
|
1473
|
+
verification_status: "pending",
|
|
1474
|
+
last_known_status: "new",
|
|
1475
|
+
ownership_tag: ownership.run_tag_label,
|
|
1476
|
+
});
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
// {{personalization}} resolves from a per-lead field at send time. If the
|
|
1481
|
+
// copy references it but no lead carries a value, Instantly substitutes an
|
|
1482
|
+
// empty string for every recipient — refuse to ship that.
|
|
1483
|
+
const usesPersonalization = spec.sequence.steps.some((step) =>
|
|
1484
|
+
`${step.subject || ""}\n${step.body || ""}`.includes("{{personalization}}"),
|
|
1485
|
+
);
|
|
1486
|
+
if (usesPersonalization && dedup.size) {
|
|
1487
|
+
const withPersonalization = Array.from(dedup.values()).filter(
|
|
1488
|
+
(lead) =>
|
|
1489
|
+
lead.personalization != null && String(lead.personalization).trim(),
|
|
1490
|
+
).length;
|
|
1491
|
+
if (withPersonalization === 0) {
|
|
1492
|
+
fail(
|
|
1493
|
+
`Copy references {{personalization}} but none of the ${dedup.size} leads in ${spec.leads_file} ` +
|
|
1494
|
+
`carry a personalization field — the token would render as an empty string in every email. ` +
|
|
1495
|
+
`Add a per-lead "personalization" value to the leads file, or remove the token from the copy.`,
|
|
1496
|
+
);
|
|
1497
|
+
}
|
|
1498
|
+
if (withPersonalization < dedup.size) {
|
|
1499
|
+
console.error(
|
|
1500
|
+
`WARNING: {{personalization}} is used in the copy but only ${withPersonalization} of ${dedup.size} ` +
|
|
1501
|
+
`leads carry a value; the token renders as an empty string for the rest.`,
|
|
1502
|
+
);
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
|
|
1506
|
+
let imported = 0,
|
|
1507
|
+
skipped = 0,
|
|
1508
|
+
failed = 0;
|
|
1509
|
+
// Leads of this file already in the campaign from earlier launches, and
|
|
1510
|
+
// leads of this batch the add call reported as not added — together they
|
|
1511
|
+
// give a lower bound of the leads the verification gate must see.
|
|
1512
|
+
let alreadyImportedCount = 0;
|
|
1513
|
+
let notAddedCount = 0;
|
|
1514
|
+
const allCandidates = [];
|
|
1515
|
+
const allCandidateHashes = [];
|
|
1516
|
+
for (const lead of dedup.values()) {
|
|
1517
|
+
const leadHash = sha256(JSON.stringify({ campaignId, email: lead.email }));
|
|
1518
|
+
if (existingLeadHashes.has(leadHash)) {
|
|
1519
|
+
skipped++;
|
|
1520
|
+
alreadyImportedCount++;
|
|
1521
|
+
upsertRunLead(dbPath, {
|
|
1522
|
+
run_key: spec.run_key,
|
|
1523
|
+
email: lead.email,
|
|
1524
|
+
campaign_id: campaignId,
|
|
1525
|
+
verification_status: "pending",
|
|
1526
|
+
last_known_status: "imported",
|
|
1527
|
+
ownership_tag: ownership.run_tag_label,
|
|
1528
|
+
notes:
|
|
1529
|
+
"Lead already present in imported-leads state; skipped on rerun.",
|
|
1530
|
+
});
|
|
1531
|
+
continue;
|
|
1532
|
+
}
|
|
1533
|
+
allCandidates.push(buildImportLead(lead));
|
|
1534
|
+
allCandidateHashes.push(leadHash);
|
|
1535
|
+
}
|
|
1536
|
+
|
|
1537
|
+
// Only an explicitly configured replenishment plan limits the initial batch.
|
|
1538
|
+
// Retain excess source data so subsequent batches do not buy it again.
|
|
1539
|
+
const carryOverPath = path.join(stateDir, "carry-over-leads.json");
|
|
1540
|
+
const existingMembers = await requireMembers(campaignId);
|
|
1541
|
+
const initialBatchSize = initialBatchLimit(spec, existingMembers.emails.size, allCandidates.length);
|
|
1542
|
+
let pendingLeads = allCandidates;
|
|
1543
|
+
let pendingLeadHashes = allCandidateHashes;
|
|
1544
|
+
let carryOverWritten = 0;
|
|
1545
|
+
if (allCandidates.length > initialBatchSize) {
|
|
1546
|
+
pendingLeads = allCandidates.slice(0, initialBatchSize);
|
|
1547
|
+
pendingLeadHashes = allCandidateHashes.slice(0, initialBatchSize);
|
|
1548
|
+
const carryOver = allCandidates.slice(initialBatchSize);
|
|
1549
|
+
// Merge with any pre-existing carry-over from prior partial-failure reruns
|
|
1550
|
+
// so reruns don't shrink the carry-over pool.
|
|
1551
|
+
const existingCarryOver = readJson(carryOverPath, []);
|
|
1552
|
+
const existingCarryOverEmails = new Set(
|
|
1553
|
+
existingCarryOver.map((l) => l && l.email).filter(Boolean),
|
|
1554
|
+
);
|
|
1555
|
+
const merged = existingCarryOver.slice();
|
|
1556
|
+
for (const lead of carryOver) {
|
|
1557
|
+
if (!existingCarryOverEmails.has(lead.email)) {
|
|
1558
|
+
merged.push(lead);
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1561
|
+
writeJson(carryOverPath, merged);
|
|
1562
|
+
carryOverWritten = carryOver.length;
|
|
1563
|
+
console.log(
|
|
1564
|
+
`Initial load capped at ${initialBatchSize} of ${allCandidates.length} candidates. ` +
|
|
1565
|
+
`${carryOverWritten} new leads carried over → ${merged.length} total in ${path.relative(runDir, carryOverPath)}. ` +
|
|
1566
|
+
`replenish-leads.js will drain this pool before calling Apollo.`,
|
|
1567
|
+
);
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
// An earlier add answered with a 5xx may have landed: send only leads that
|
|
1571
|
+
// are not in the campaign yet.
|
|
1572
|
+
const preImportFilter = filterConfirmedOptOuts(runDir, pendingLeads);
|
|
1573
|
+
pendingLeads = preImportFilter.leads;
|
|
1574
|
+
pendingLeadHashes = pendingLeads.map(lead => sha256(JSON.stringify({ campaignId, email: lead.email })));
|
|
1575
|
+
const leadsToAdd = preImportFilter.leads.length
|
|
1576
|
+
? leadsNotInCampaign(preImportFilter.leads, await requireMembers(campaignId))
|
|
1577
|
+
: [];
|
|
1578
|
+
skipped += pendingLeads.length - leadsToAdd.length;
|
|
1579
|
+
if (leadsToAdd.length) {
|
|
1580
|
+
const res = await client.addLeadsToCampaign({
|
|
1581
|
+
campaign_id: campaignId,
|
|
1582
|
+
leads: leadsToAdd,
|
|
1583
|
+
verify_leads_on_import: verifyOnImport,
|
|
1584
|
+
});
|
|
1585
|
+
if (!(res.status >= 200 && res.status < 300)) {
|
|
1586
|
+
fail(
|
|
1587
|
+
`Add leads failed: ${res.status} ${JSON.stringify(res.body)}` +
|
|
1588
|
+
(res.status >= 500
|
|
1589
|
+
? ". The batch may have landed; a re-run sends only leads not yet in the campaign."
|
|
1590
|
+
: ""),
|
|
1591
|
+
);
|
|
1592
|
+
}
|
|
1593
|
+
const body = res.body || {};
|
|
1594
|
+
imported += Number(body.leads_uploaded || body.created_leads?.length || 0);
|
|
1595
|
+
skipped +=
|
|
1596
|
+
Number(body.skipped_count || 0) +
|
|
1597
|
+
Number(body.duplicated_leads || 0) +
|
|
1598
|
+
Number(body.in_blocklist || 0) +
|
|
1599
|
+
Number(body.duplicate_email_count || 0);
|
|
1600
|
+
failed +=
|
|
1601
|
+
Number(body.invalid_email_count || 0) +
|
|
1602
|
+
Number(body.incomplete_count || 0);
|
|
1603
|
+
// Every "not uploaded" counter, so the expected count stays a lower bound.
|
|
1604
|
+
notAddedCount +=
|
|
1605
|
+
Number(body.skipped_count || 0) +
|
|
1606
|
+
Number(body.duplicated_leads || 0) +
|
|
1607
|
+
Number(body.in_blocklist || 0) +
|
|
1608
|
+
Number(body.duplicate_email_count || 0) +
|
|
1609
|
+
Number(body.invalid_email_count || 0) +
|
|
1610
|
+
Number(body.incomplete_count || 0);
|
|
1611
|
+
|
|
1612
|
+
// Reconcile the bulk response before treating the batch as landed: a
|
|
1613
|
+
// 2xx with 0 uploaded and 0 already-present means every lead was
|
|
1614
|
+
// rejected (invalid/incomplete). Nothing has been persisted yet, so
|
|
1615
|
+
// failing here leaves the rerun free to retry the import.
|
|
1616
|
+
if (imported === 0 && skipped === 0) {
|
|
1617
|
+
fail(
|
|
1618
|
+
`Add leads returned ${res.status} but reported 0 uploaded and 0 already-present ` +
|
|
1619
|
+
`(${failed} invalid/incomplete of ${leadsToAdd.length} sent): ${JSON.stringify(body)}. ` +
|
|
1620
|
+
`The batch never landed — fix the leads file and re-run. Campaign was NOT activated.`,
|
|
1621
|
+
);
|
|
1622
|
+
}
|
|
1623
|
+
}
|
|
1624
|
+
|
|
1625
|
+
// Post-launch validation: verify the campaign has sending accounts before activating
|
|
1626
|
+
let verification = await client.getCampaign(campaignId);
|
|
1627
|
+
let safeguardReadback = campaignSafetyReadback(verification.body);
|
|
1628
|
+
const safeguardKeys = Object.keys(safeguardReadback);
|
|
1629
|
+
if (verification.status >= 200 && verification.status < 300 &&
|
|
1630
|
+
safeguardKeys.some(key => safeguardReadback[key] !== payload[key])) {
|
|
1631
|
+
const requested = Object.fromEntries(safeguardKeys.map(key => [key, payload[key]]));
|
|
1632
|
+
await client.patchCampaign(campaignId, requested);
|
|
1633
|
+
verification = await client.getCampaign(campaignId);
|
|
1634
|
+
safeguardReadback = campaignSafetyReadback(verification.body);
|
|
1635
|
+
}
|
|
1636
|
+
const safeguardConfirmed = verification.status >= 200 && verification.status < 300 &&
|
|
1637
|
+
safeguardKeys.every(key => safeguardReadback[key] === payload[key]);
|
|
1638
|
+
if (verification.status >= 200 && verification.status < 300) {
|
|
1639
|
+
// A GET right after create has been observed returning link_tracking:
|
|
1640
|
+
// null even when false was sent. PATCH the intended value explicitly so
|
|
1641
|
+
// the tracker-domain rewrite cannot sneak back in.
|
|
1642
|
+
const storedLinkTracking = verification.body?.link_tracking;
|
|
1643
|
+
if (storedLinkTracking !== payload.link_tracking) {
|
|
1644
|
+
const patched = await client.patchCampaign(campaignId, {
|
|
1645
|
+
link_tracking: payload.link_tracking,
|
|
1646
|
+
});
|
|
1647
|
+
if (patched.status >= 200 && patched.status < 300) {
|
|
1648
|
+
console.log(
|
|
1649
|
+
`link_tracking read back as ${JSON.stringify(storedLinkTracking)}; patched to ${payload.link_tracking}.`,
|
|
1650
|
+
);
|
|
1651
|
+
} else {
|
|
1652
|
+
console.error(
|
|
1653
|
+
`WARNING: could not enforce link_tracking=${payload.link_tracking} ` +
|
|
1654
|
+
`(read back ${JSON.stringify(storedLinkTracking)}, patch returned ${patched.status}). ` +
|
|
1655
|
+
`Verify click tracking manually in the Instantly UI.`,
|
|
1656
|
+
);
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
const liveEmailList = verification.body?.email_list || [];
|
|
1660
|
+
if (!liveEmailList.length) {
|
|
1661
|
+
fail(
|
|
1662
|
+
`Campaign ${campaignId} has no sending accounts assigned. The campaign was created but email_list is empty. Check that sending accounts exist and are healthy in the Instantly workspace.`,
|
|
1663
|
+
);
|
|
1664
|
+
}
|
|
1665
|
+
if (liveEmailList.length !== emailList.length) {
|
|
1666
|
+
console.error(
|
|
1667
|
+
`WARNING: Campaign has ${liveEmailList.length} sending accounts but ${emailList.length} were requested. Some accounts may have been rejected by Instantly.`,
|
|
1668
|
+
);
|
|
1669
|
+
}
|
|
1670
|
+
|
|
1671
|
+
// Instantly's CREATE endpoint has been observed rewriting paragraph
|
|
1672
|
+
// markup (a body sent as <p> blocks came back as margin-less <div>s and
|
|
1673
|
+
// the delivered email rendered as a wall of text). Bodies are emitted in
|
|
1674
|
+
// editor-native <div> form now; still verify the stored copy kept the
|
|
1675
|
+
// blank-line separators before activating. Match by regex — Instantly
|
|
1676
|
+
// normalizes <br> to <br />, so never compare exact tag strings.
|
|
1677
|
+
const SEPARATOR_RE = /<div>\s*<br\s*\/?\s*>\s*<\/div>/i;
|
|
1678
|
+
const storedSteps = verification.body?.sequences?.[0]?.steps || [];
|
|
1679
|
+
(payload.sequences?.[0]?.steps || []).forEach((desiredStep, idx) => {
|
|
1680
|
+
const desiredBody = desiredStep.variants?.[0]?.body || "";
|
|
1681
|
+
if (!SEPARATOR_RE.test(desiredBody)) return;
|
|
1682
|
+
const storedBody = storedSteps[idx]?.variants?.[0]?.body;
|
|
1683
|
+
// A missing stored body means the read surface is degraded, not that
|
|
1684
|
+
// Instantly stripped the markup — skip rather than false-positive.
|
|
1685
|
+
if (typeof storedBody !== "string" || !storedBody) return;
|
|
1686
|
+
if (!SEPARATOR_RE.test(storedBody)) {
|
|
1687
|
+
fail(
|
|
1688
|
+
`Campaign ${campaignId} step ${idx + 1}: Instantly stripped the paragraph separators from the stored ` +
|
|
1689
|
+
`body (no <div><br /></div> left) — the delivered email would render as a wall of text. ` +
|
|
1690
|
+
`Refusing to activate. Inspect the stored sequence via a campaign GET and re-launch.`,
|
|
1691
|
+
);
|
|
1692
|
+
}
|
|
1693
|
+
});
|
|
1694
|
+
}
|
|
1695
|
+
|
|
1696
|
+
let activation = null;
|
|
1697
|
+
let verificationResult = null;
|
|
1698
|
+
|
|
1699
|
+
// Rerun after a failed gate: leads the agent dropped from the leads file
|
|
1700
|
+
// are still in the Instantly campaign and would be mailed on activation.
|
|
1701
|
+
// Delete them first, then gate the whole campaign as it now is.
|
|
1702
|
+
const invalidLeadsPath = path.join(stateDir, "invalid-leads.json");
|
|
1703
|
+
const deletedLeadsPath = path.join(stateDir, "deleted-leads.json");
|
|
1704
|
+
const previousInvalid = readJson(invalidLeadsPath, null);
|
|
1705
|
+
let invalidCleanup = null;
|
|
1706
|
+
if (previousInvalid?.campaign_id && previousInvalid.campaign_id !== campaignId) {
|
|
1707
|
+
console.error(
|
|
1708
|
+
`WARNING: ${path.relative(runDir, invalidLeadsPath)} belongs to campaign ${previousInvalid.campaign_id}, ` +
|
|
1709
|
+
`not ${campaignId}; not removing any leads from it.`,
|
|
1710
|
+
);
|
|
1711
|
+
} else if (previousInvalid) {
|
|
1712
|
+
invalidCleanup = await removeDroppedInvalidLeads(
|
|
1713
|
+
client,
|
|
1714
|
+
campaignId,
|
|
1715
|
+
previousInvalid,
|
|
1716
|
+
new Set(dedup.keys()),
|
|
1717
|
+
);
|
|
1718
|
+
const droppedEmails = new Set([
|
|
1719
|
+
...invalidCleanup.deleted.map((d) => d.email),
|
|
1720
|
+
...invalidCleanup.already_absent,
|
|
1721
|
+
...invalidCleanup.failed.map((f) => f.email),
|
|
1722
|
+
]);
|
|
1723
|
+
if (invalidCleanup.deleted.length) {
|
|
1724
|
+
const deletedAt = isoNow();
|
|
1725
|
+
writeJson(deletedLeadsPath, [
|
|
1726
|
+
...readJson(deletedLeadsPath, []),
|
|
1727
|
+
...invalidCleanup.deleted.map((d) => ({
|
|
1728
|
+
...d,
|
|
1729
|
+
campaign_id: campaignId,
|
|
1730
|
+
deleted_at: deletedAt,
|
|
1731
|
+
reason: "failed lead verification; dropped from the leads file",
|
|
1732
|
+
})),
|
|
1733
|
+
]);
|
|
1734
|
+
console.log(
|
|
1735
|
+
`Removed ${invalidCleanup.deleted.length} lead(s) dropped from ${spec.leads_file} after the last failed ` +
|
|
1736
|
+
`verification gate from campaign ${campaignId} (recorded in ${path.relative(runDir, deletedLeadsPath)}).`,
|
|
1737
|
+
);
|
|
1738
|
+
}
|
|
1739
|
+
// Replenishment must not bring them back from the carry-over pool.
|
|
1740
|
+
if (droppedEmails.size && fs.existsSync(carryOverPath)) {
|
|
1741
|
+
const pool = readJson(carryOverPath, []);
|
|
1742
|
+
const kept = pool.filter((l) => !droppedEmails.has(normalizeEmail(l?.email)));
|
|
1743
|
+
if (kept.length !== pool.length) writeJson(carryOverPath, kept);
|
|
1744
|
+
}
|
|
1745
|
+
if (!invalidCleanup.ok) {
|
|
1746
|
+
fail(
|
|
1747
|
+
`Could not remove the leads dropped from ${spec.leads_file} after the last failed verification gate: ` +
|
|
1748
|
+
`${invalidCleanup.reason}. They are still in campaign ${campaignId} and would be mailed on activation, ` +
|
|
1749
|
+
`so the campaign was NOT activated. Not removed: ` +
|
|
1750
|
+
invalidCleanup.failed
|
|
1751
|
+
.map((f) => `${f.email}${f.id ? ` (lead ${f.id})` : ""} — status ${f.status}`)
|
|
1752
|
+
.join(", ") +
|
|
1753
|
+
`. Rerun the same launch command — it re-reads the campaign first, so a delete that answered ` +
|
|
1754
|
+
`5xx but happened counts as removed — or remove them in Instantly by hand.` +
|
|
1755
|
+
(invalidCleanup.deleted.length
|
|
1756
|
+
? ` Removed so far: ${invalidCleanup.deleted.length} (see ${path.relative(runDir, deletedLeadsPath)}).`
|
|
1757
|
+
: ""),
|
|
1758
|
+
);
|
|
1759
|
+
}
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
// A run that ended with verification pending or unread must not activate
|
|
1763
|
+
// on a rerun just because no new leads are left to import: re-check the
|
|
1764
|
+
// gate whenever the last gate did not pass.
|
|
1765
|
+
const verificationStatePath = path.join(stateDir, "verification-state.json");
|
|
1766
|
+
const previousVerification = readJson(verificationStatePath, null);
|
|
1767
|
+
const gateEnabled = verifyOnImport && waitForVerify;
|
|
1768
|
+
const recheckGate =
|
|
1769
|
+
gateEnabled &&
|
|
1770
|
+
!pendingLeads.length &&
|
|
1771
|
+
!!previousVerification?.status &&
|
|
1772
|
+
previousVerification.status !== "passed";
|
|
1773
|
+
if (recheckGate) {
|
|
1774
|
+
console.log(
|
|
1775
|
+
`The last launch of this run ended with lead verification "${previousVerification.status}"; ` +
|
|
1776
|
+
`re-checking the verification gate before activation.`,
|
|
1777
|
+
);
|
|
1778
|
+
}
|
|
1779
|
+
if (gateEnabled && (pendingLeads.length || recheckGate)) {
|
|
1780
|
+
verificationResult = await waitForVerification(client, campaignId, spec, {
|
|
1781
|
+
importedCount: imported,
|
|
1782
|
+
skippedCount: skipped,
|
|
1783
|
+
fileEmails: new Set(dedup.keys()),
|
|
1784
|
+
expectedLeads: Math.max(
|
|
1785
|
+
0,
|
|
1786
|
+
alreadyImportedCount + pendingLeads.length - notAddedCount,
|
|
1787
|
+
),
|
|
1788
|
+
});
|
|
1789
|
+
if (verificationResult?.import_failed) {
|
|
1790
|
+
// Nothing has been persisted for this batch yet — failing here leaves
|
|
1791
|
+
// the rerun free to retry the import.
|
|
1792
|
+
fail(verificationResult.import_failure_reason);
|
|
1793
|
+
}
|
|
1794
|
+
const gateStatus = verificationResult?.gate_failed
|
|
1795
|
+
? "failed"
|
|
1796
|
+
: verificationResult?.verification_pending
|
|
1797
|
+
? "pending"
|
|
1798
|
+
: verificationResult?.verification_unread
|
|
1799
|
+
? "unread"
|
|
1800
|
+
: "passed";
|
|
1801
|
+
writeJson(verificationStatePath, {
|
|
1802
|
+
status: gateStatus,
|
|
1803
|
+
campaign_id: campaignId,
|
|
1804
|
+
source: verificationResult?.source || null,
|
|
1805
|
+
checked_at: isoNow(),
|
|
1806
|
+
});
|
|
1807
|
+
if (verificationResult?.gate_failed) {
|
|
1808
|
+
// Like an import failure, nothing is persisted for this batch: the
|
|
1809
|
+
// rerun with the cleaned leads file deletes the dropped leads from the
|
|
1810
|
+
// campaign, re-imports what is missing (Instantly dedupes) and re-runs
|
|
1811
|
+
// the gate over the whole campaign.
|
|
1812
|
+
const invalidLeads = verificationResult.invalid_leads || [];
|
|
1813
|
+
const shown = invalidLeads.slice(0, 50).map((l) => l.email);
|
|
1814
|
+
if (invalidLeads.length) {
|
|
1815
|
+
writeJson(invalidLeadsPath, {
|
|
1816
|
+
run_key: spec.run_key,
|
|
1817
|
+
campaign_id: campaignId,
|
|
1818
|
+
leads_file: spec.leads_file,
|
|
1819
|
+
max_invalid_lead_pct: verificationResult.max_invalid_lead_pct,
|
|
1820
|
+
invalid_pct: verificationResult.invalidPct,
|
|
1821
|
+
generated_at: isoNow(),
|
|
1822
|
+
emails: invalidLeads.map((l) => l.email),
|
|
1823
|
+
leads: invalidLeads,
|
|
1824
|
+
});
|
|
1825
|
+
}
|
|
1826
|
+
fail(
|
|
1827
|
+
`${verificationResult.failure_reason}\n` +
|
|
1828
|
+
(invalidLeads.length
|
|
1829
|
+
? `Invalid addresses (${invalidLeads.length}, also written to ${path.relative(runDir, invalidLeadsPath)}): ` +
|
|
1830
|
+
`${shown.join(", ")}${invalidLeads.length > shown.length ? `, … (${invalidLeads.length - shown.length} more in the file)` : ""}.\n` +
|
|
1831
|
+
`Remove them from ${spec.leads_file} (add replacement leads if you need the volume) and rerun ` +
|
|
1832
|
+
`the same launch command: the rerun deletes every listed lead that is no longer in the leads ` +
|
|
1833
|
+
`file from the Instantly campaign, then re-checks the gate over the whole campaign. A listed ` +
|
|
1834
|
+
`lead you keep in the file stays in the campaign and keeps counting.`
|
|
1835
|
+
: `The per-lead listing could not name the invalid addresses — check the leads' verification ` +
|
|
1836
|
+
`status in Instantly, remove the invalid ones from ${spec.leads_file}, and rerun.`),
|
|
1837
|
+
);
|
|
1838
|
+
}
|
|
1839
|
+
}
|
|
1840
|
+
|
|
1841
|
+
// Import success state (JSON hashes + run_leads rows + imported events) is
|
|
1842
|
+
// persisted only after the import reconciled and the verification gate did
|
|
1843
|
+
// not diagnose a failed import — persisting earlier made reports claim a
|
|
1844
|
+
// failed batch was imported and let the rerun skip re-importing it. A
|
|
1845
|
+
// crash before this point re-imports the batch on rerun; Instantly dedupes
|
|
1846
|
+
// by email within a campaign, so the retry lands as skipped duplicates.
|
|
1847
|
+
if (pendingLeads.length) {
|
|
1848
|
+
for (const leadHash of pendingLeadHashes) {
|
|
1849
|
+
existingLeadHashes.add(leadHash);
|
|
1850
|
+
}
|
|
1851
|
+
const importedAt = isoNow();
|
|
1852
|
+
for (const lead of pendingLeads) {
|
|
1853
|
+
upsertRunLead(dbPath, {
|
|
1854
|
+
run_key: spec.run_key,
|
|
1855
|
+
email: lead.email,
|
|
1856
|
+
campaign_id: campaignId,
|
|
1857
|
+
imported_at: importedAt,
|
|
1858
|
+
verification_status: verifyOnImport ? "pending" : "skipped",
|
|
1859
|
+
last_known_status: "imported",
|
|
1860
|
+
ownership_tag: ownership.run_tag_label,
|
|
1861
|
+
});
|
|
1862
|
+
insertLeadEvent(dbPath, {
|
|
1863
|
+
run_key: spec.run_key,
|
|
1864
|
+
email: lead.email,
|
|
1865
|
+
event_type: "imported",
|
|
1866
|
+
event_at: importedAt,
|
|
1867
|
+
payload_json: {
|
|
1868
|
+
campaign_id: campaignId,
|
|
1869
|
+
verify_leads_on_import: verifyOnImport,
|
|
1870
|
+
},
|
|
1871
|
+
});
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
writeJson(importedLeadsPath, Array.from(existingLeadHashes));
|
|
1875
|
+
|
|
1876
|
+
const allowUnverified = spec.campaign.allow_unverified_activation === true;
|
|
1877
|
+
const gateDecision = verificationActivationBlock(
|
|
1878
|
+
verificationResult,
|
|
1879
|
+
allowUnverified,
|
|
1880
|
+
);
|
|
1881
|
+
let activationBlocked = gateDecision.blocked;
|
|
1882
|
+
if (!safeguardConfirmed) activationBlocked = "safeguard_readback_unconfirmed";
|
|
1883
|
+
// A fresh complete membership read catches opt-outs already imported in an
|
|
1884
|
+
// earlier pass. Filtering a new input file alone cannot make activation safe.
|
|
1885
|
+
const history = readOptOutHistory(runDir);
|
|
1886
|
+
if (spec.campaign.activate_on_launch === true || history.emails.size) {
|
|
1887
|
+
const assessment = await assessCampaignOptOuts(client, campaignId, history.emails);
|
|
1888
|
+
if (!assessment.ok) {
|
|
1889
|
+
activationBlocked = assessment.reason || "confirmed_opt_out_exclusion_unconfirmed";
|
|
1890
|
+
await containOptOuts(history.emails);
|
|
1891
|
+
// Containment may establish exclusion for a later explicit rerun. This
|
|
1892
|
+
// launch attempt stays blocked rather than activating as part of recovery.
|
|
1893
|
+
}
|
|
1894
|
+
}
|
|
1895
|
+
if (gateDecision.warning) {
|
|
1896
|
+
console.error(
|
|
1897
|
+
[
|
|
1898
|
+
"WARNING: ================ LEAD VERIFICATION NOT ENFORCED ================",
|
|
1899
|
+
`WARNING: ${gateDecision.warning}`,
|
|
1900
|
+
allowUnverified
|
|
1901
|
+
? "WARNING: campaign.allow_unverified_activation is true — activation is allowed WITHOUT a completed invalid-lead gate."
|
|
1902
|
+
: "WARNING: The campaign will NOT be activated. Rerun the same launch command later (the gate is " +
|
|
1903
|
+
"re-checked), or set campaign.allow_unverified_activation: true in the run spec to activate without it.",
|
|
1904
|
+
"WARNING: ======================================================================",
|
|
1905
|
+
].join("\n"),
|
|
1906
|
+
);
|
|
1907
|
+
}
|
|
1908
|
+
if (spec.campaign.activate_on_launch === true && !activationBlocked) {
|
|
1909
|
+
const act = await client.activateCampaign(campaignId);
|
|
1910
|
+
activation = { status: act.status, body: act.body };
|
|
1911
|
+
const live = await client.getCampaign(campaignId);
|
|
1912
|
+
activation.confirmed = act.status >= 200 && act.status < 300 &&
|
|
1913
|
+
live.status >= 200 && live.status < 300 && live.body?.status === 1;
|
|
1914
|
+
activation.readback_status = live.body?.status ?? null;
|
|
1915
|
+
if (!activation.confirmed) activationBlocked = "activation_unconfirmed";
|
|
1916
|
+
if (activation.confirmed) {
|
|
1917
|
+
const activatedAt = isoNow();
|
|
1918
|
+
if (!reused || verification.body?.status === 0)
|
|
1919
|
+
manifest.campaign.first_launched_at ||= activatedAt;
|
|
1920
|
+
manifest.campaign.last_activated_at = activatedAt;
|
|
1921
|
+
writeJson(manifestPath, manifest);
|
|
1922
|
+
}
|
|
1923
|
+
}
|
|
1924
|
+
let finalCampaign;
|
|
1925
|
+
try { finalCampaign = await client.getCampaign(campaignId); }
|
|
1926
|
+
catch { finalCampaign = { status: 0, body: null }; }
|
|
1927
|
+
const campaignStatus = finalCampaign.status >= 200 && finalCampaign.status < 300
|
|
1928
|
+
? finalCampaign.body?.status ?? null : null;
|
|
1929
|
+
const actualState = ({0: "draft", 1: "active", 2: "paused", 3: "completed"})[campaignStatus] || "unknown";
|
|
1930
|
+
const launchStatus =
|
|
1931
|
+
activation?.confirmed
|
|
1932
|
+
? "launched"
|
|
1933
|
+
: activationBlocked || actualState;
|
|
1934
|
+
|
|
1935
|
+
upsertRun(dbPath, {
|
|
1936
|
+
run_key: spec.run_key,
|
|
1937
|
+
product_name: spec.product?.name || spec.product_name,
|
|
1938
|
+
segment_name:
|
|
1939
|
+
spec.segment?.name || spec.segment?.description || spec.segment,
|
|
1940
|
+
offer_headline:
|
|
1941
|
+
spec.offer?.headline ||
|
|
1942
|
+
spec.offer?.name ||
|
|
1943
|
+
spec.offer?.description ||
|
|
1944
|
+
spec.offer,
|
|
1945
|
+
campaign_id: campaignId,
|
|
1946
|
+
campaign_name: spec.campaign.name,
|
|
1947
|
+
status: actualState,
|
|
1948
|
+
created_at: manifest.created_at || spec.created_at,
|
|
1949
|
+
});
|
|
1950
|
+
|
|
1951
|
+
const summary = {
|
|
1952
|
+
business_id: spec.business_id || null,
|
|
1953
|
+
product_id: spec.product_id || null,
|
|
1954
|
+
launch_id: spec.launch_id || null,
|
|
1955
|
+
experiment_id: spec.experiment_id || null,
|
|
1956
|
+
variant_id: spec.variant_id || null,
|
|
1957
|
+
provider: attributionBinding.provider,
|
|
1958
|
+
provider_campaign_id: attributionBinding.provider_campaign_id,
|
|
1959
|
+
utm_source: attributionBinding.utm_source,
|
|
1960
|
+
utm_medium: attributionBinding.utm_medium,
|
|
1961
|
+
utm_campaign: attributionBinding.utm_campaign,
|
|
1962
|
+
run_key: spec.run_key,
|
|
1963
|
+
campaign_id: campaignId,
|
|
1964
|
+
campaign_name: spec.campaign.name,
|
|
1965
|
+
reused,
|
|
1966
|
+
sequence_action: sequenceAction,
|
|
1967
|
+
sender_name: inferredSenderName,
|
|
1968
|
+
primary_email: primaryAccount.email,
|
|
1969
|
+
email_list: emailList,
|
|
1970
|
+
ownership_tags: [ownership.run_tag_label, ownership.agent_tag_label],
|
|
1971
|
+
ownership_tags_assigned: tagsAssigned,
|
|
1972
|
+
imported,
|
|
1973
|
+
skipped,
|
|
1974
|
+
failed,
|
|
1975
|
+
deduped_input_count: dedup.size,
|
|
1976
|
+
raw_input_count: inputLeads.length,
|
|
1977
|
+
confirmed_opt_outs_excluded: optOutFilter.excluded.length + preImportFilter.excluded.length,
|
|
1978
|
+
opt_out_stop_results: optOutStopResults,
|
|
1979
|
+
safeguard_readback: safeguardReadback,
|
|
1980
|
+
initial_batch_size: initialBatchSize,
|
|
1981
|
+
carry_over_count: carryOverWritten,
|
|
1982
|
+
launch_status: launchStatus,
|
|
1983
|
+
campaign_status: campaignStatus,
|
|
1984
|
+
actual_state: actualState,
|
|
1985
|
+
activation_blocked: activationBlocked,
|
|
1986
|
+
launched_at: manifest.campaign.first_launched_at || null,
|
|
1987
|
+
invalid_lead_cleanup: invalidCleanup,
|
|
1988
|
+
activation,
|
|
1989
|
+
verification: verificationResult,
|
|
1990
|
+
throughput_preflight: throughputPreflight,
|
|
1991
|
+
generated_at: isoNow(),
|
|
1992
|
+
};
|
|
1993
|
+
writeJson(summaryPath, summary);
|
|
1994
|
+
console.log(JSON.stringify(summary, null, 2));
|
|
1995
|
+
if (activationBlocked) {
|
|
1996
|
+
// Not a crash and not an activation: a distinct exit code so a chained
|
|
1997
|
+
// command does not treat the run as launched.
|
|
1998
|
+
console.error(
|
|
1999
|
+
activationBlocked === "verification_pending"
|
|
2000
|
+
? `VERIFICATION PENDING: ${verificationResult.pending_reason}`
|
|
2001
|
+
: `NOT CONFIRMED LAUNCHED: ${activationBlocked}. Check the recorded verification/readback before continuing.`,
|
|
2002
|
+
);
|
|
2003
|
+
process.exit(2);
|
|
2004
|
+
}
|
|
2005
|
+
}
|
|
2006
|
+
|
|
2007
|
+
if (require.main === module) main().catch((err) => fail(err.stack || err.message));
|
|
2008
|
+
module.exports = { buildCampaignPayload, sequenceFingerprint, preflightLeadNames };
|