Security teams forgive a missed ticket once. They never forgive a hundred duplicates.
Megatron scans an organization's repositories for vulnerable dependencies. Security teams don't live in Megatron, though. They live in ServiceNow, Cortex XSOAR, or Cortex XSIAM. So every new finding must become one incident there, and a fixed finding must close it.
The naive version is a loop: for each open finding, create a ticket. It works exactly once. The second run duplicates everything, a crash halfway leaves half a backlog, and two workers running at once double it. Here's the design that makes it safe to run after every scan and every 15 minutes, indefinitely.
1. Reconcile, don't replay
The forwarding job doesn't process events ("a finding appeared"). It compares two states:
- the organization's open findings right now, filtered by each integration's minimum severity
sink_deliveries: what has already been delivered to that integration
A finding that's open but not delivered gets a ticket. A delivered finding that's no longer open (fixed, ignored, or below the threshold) gets its ticket resolved. If a run misses something because the network failed or a worker restarted, the next run sees the same gap and fixes it. Missed events can't exist, because there are no events.
2. A stable key for every delivery
Each delivery is keyed by a stable finding key, derived from what the finding is (repository, package, vulnerability) rather than from a database row ID that changes when a scan re-creates rows. The same vulnerability in the same package on the next scan maps to the same key and the same ticket.
In the code it's literally dep:<repository>:<vulnerability>:<package>, and the ledger has a unique constraint on (integration, finding_key). Recording a delivery is an INSERT … ON CONFLICT DO UPDATE, so even a race between two writers ends in one row.
That's the whole idempotency guarantee: one key, one row in the ledger, one ticket. Duplicates aren't unlikely; they're impossible.
3. One run per tenant at a time
Scans finishing, imports finishing, and the 15-minute schedule can all queue a forwarding job at once, and the jobs run on a pool of workers. A per-tenant Postgres advisory lock wraps each run: a second run for the same organization waits or skips instead of racing the first. Different organizations still forward in parallel.
4. Budgets, not heroics
External systems fail in boring ways, like rate limits, timeouts, and expired credentials. The job has explicit budgets:
- Capped runs: at most 100 opens and 100 resolves per integration per run, so a first sync of a large backlog spreads over several runs instead of flooding the ticketing system.
- Per-finding retries: a failing finding is retried up to 5 times.
- A circuit breaker: 3 failures in a row stop that integration's run and record the error where an admin can see it, rather than hammering a system that's clearly down.
5. Off by default
Every integration starts with forwarding off, with a minimum severity and a choice of whether to include findings imported from other scanners. Turning it on is a deliberate admin action, and there's a "run now" button and a list of what was delivered, so the first run isn't a leap of faith.
Testing against something real
demo/tools-mock imitates ServiceNow, XSOAR (versions 6 and 8), and XSIAM, and lists everything it received. For this kind of code, the useful test isn't "did the request succeed" but "what exactly arrived on the other side, and how many times", and a mock you can inspect answers that.
The pattern, generalized
Anything that pushes state into another system (tickets, emails, invoices, webhooks) benefits from the same four pieces:
- Reconcile states, don't replay events.
- Key deliveries by identity, not by row IDs.
- Lock per tenant, so concurrent runs can't race.
- Budget failures, so one bad integration can't take the job down.

Henry Iddirisu
AI product engineer · Accra, Ghana · Remote