Extending TxLoom
TxLoom has three extension surfaces: sinks, fraud typologies and imperfection types. Sinks are a real plugin interface. Typologies and imperfections are extended by adding a branch to one central function, which is a known gap and not hidden. Locale name packs are data plus a one-line allowlist entry.
Sinks
Every delivery target implements Sink and is built by a matching SinkFactory, both defined in packages/sinks/src/interface.ts (SINK_PLUGIN_API_VERSION = 2). The full contract is specs/003-generation-delivery-overhaul/contracts/sink-plugin.md. File output does not use this interface, because the generation pipeline writes it.
export interface Sink {
readonly type: SinkType;
readonly name: string;
readonly maxInFlight: number; // most unacknowledged publishes the pump may keep outstanding
readonly maxAttempts?: number; // attempts before a delivery is dropped (default 5)
readonly partitionKeyField?: string; // wire field used as `SinkTarget.key` (default consumer_id)
// Resolves ONLY once the destination has acknowledged the message, or ok:false on failure.
// Never retries internally; honours `signal` (abort -> {ok:false, retryable:true}).
publish(
target: SinkTarget, // { topic?, routing_key?, key? }
body: Uint8Array, // the wire-encoded event (packages/sinks/src/wire.ts)
envelope: SinkDeliveryEnvelope, // { delivery_id, event_id }
signal: AbortSignal,
): Promise<{ ok: boolean; retryable?: boolean; error?: string }>;
readonly saturated?: boolean; // transport backpressure hint
waitForCapacity?(): Promise<void>; // resolves when `saturated` clears
setRunContext?(context: { runId; sessionSeq; engineVersion }): void;
probe(target: SinkTarget): Promise<ProbeResult>;
close(): Promise<void>;
}
export interface SinkFactory<TConfig = unknown, TSecrets = unknown> {
readonly type: SinkType;
readonly configSchema: object; // JSON Schema the API validates connection bodies against
readonly secretsSchema: object | null;
create(name: string, config: TConfig, secrets: TSecrets | null): Sink;
}
Retries, backoff, the in-flight window, backpressure and the dropped answer-key labels belong to the shared DeliveryPump (packages/sinks/src/pump.ts). A sink only reports what happened to one message.
Adding a Sink Type
- Add the new literal to
SinkTypeinpackages/spec/src/types.ts. This is a schema change, so update the JSON Schema inpackages/spec/src/schema.jsontoo, and add an invariant if the type has its own constraints. - Implement
Sinkand aSinkFactoryunderpackages/sinks/src/<name>/, followingkafka/producer.ts,rabbitmq/publisher.tsorwebhook/publisher.ts.publishmust resolve on the destination's acknowledgement: a delivery report, a publisher confirm or an HTTP 2xx.- Mark a failure
retryable: falseonly when retrying cannot fix it, such as an HTTP 400. close()should release any held connection.
- Export the factory from
packages/sinks/src/index.ts. - Register the factory in the three
FACTORIESmaps:apps/api/src/services/connection-validation.ts,apps/worker/src/jobs/stream-drive.tsandapps/worker/src/jobs/connection-test.ts. - Widen the connection-type enums with a new migration, not an edit to an existing one:
connections.typeandrun_destinations.connection_type. - Widen the connection-type unions the API, worker and spec package hard-code, and give the new type its own branches where they switch on it:
apps/api/src/db/repositories/connections.tsandrun-destinations.ts(ConnectionType)apps/api/src/services/connection-probe.tsandconnection-validation.tsapps/api/src/routes/connections.ts(the defaultSinkTargetfor a connection)apps/worker/src/jobs/stream-drive.ts(how a destination's target maps onto the sink)packages/spec/src/connection-warnings.ts
- Wire the clients so every surface can reach the new type:
- Web:
apps/web/src/api/client.ts(ConnectionTypeand the typed config),surfaces/connections/connection-form.tsx(type picker and fields) andcomponents/data/destination-picker.tsx - CLI:
apps/cli/src/commands/connections.ts
- Web:
- Add integration tests under
packages/sinks/tests/integration/. Use the Testcontainers pattern, or a local-server pattern likewebhook-retry.test.tsif the target needs no real broker.
Migrating a v1 Sink to v2
| v1 | v2 |
|---|---|
publish(payload, envelope); the sink serialized the payload | publish(target, body, envelope, signal); the body is already encoded |
envelope.ts | Removed; delivered_at is inside the body |
Result { ok, backpressure?, error? } | { ok, retryable?, error? }; backpressure is saturated / waitForCapacity |
ok could mean "handed to the client" | ok means the destination acknowledged it |
| The sink retried internally (webhook backoff loop) | The sink makes one attempt; the pump retries |
| No concurrency limit | Declare maxInFlight |
testConnection() | probe(target) |
Fraud Typologies
Three typologies ship: card_testing, account_takeover and refund_abuse. Fraud is planned and emitted in two steps, both in packages/engine/src/fraud/planner.ts:
planDayruns once per simulated day. There is no global budget. The target rate is met by feedback: each typology is owedrate / (1 − rate) × share × legitimate volume so far, and the day plans campaigns for whatever is still short. It dispatches per typology through anifchain and pushes each campaign's steps ontoworld.pendingFraudasScheduledFraudEvents keyed by simulated time.drainDaypops the scheduled events that fall inside the day and turns each into aTruthEventand itsLabelRecord. Events that cannot happen (for example a refund with no purchase to refund) are dropped and un-planned, so the nextplanDaymakes up the shortfall.
card_testing and account_takeover keep their step generation in their own files (card-testing.ts, account-takeover.ts), each returning FraudEventDraft[] (fraud/types.ts). refund_abuse is written inline in planDay, and its refunds are matched against the consumer's recent purchases in drainDay.
Adding a Typology
- Add the variant to
FraudTypologyConfiginpackages/spec/src/types.ts, with its JSON Schema and invariants (for example a bounds check on its params, likedormancy-satisfiable.tsforaccount_takeover). - Write
packages/engine/src/fraud/<name>.tsexporting a function that takes the typology's params and the RNG and returnsFraudEventDraft[]. SeegenerateCardTestingBurstorgenerateAccountTakeoverScript. TheFraudEventDraft[]return type is the contract; function names vary. - Add a branch to
planDay's dispatch that calls it and schedules each draft ontoworld.pendingFraud, adding toworld.planner.fraudPlannedfor the feedback loop. Use thecard_testingoraccount_takeoverbranch as a template. If the new typology's events depend on world state at emission time, resolve them indrainDayasrefund_abusedoes. - Add golden-master and property-test coverage.
packages/engine/tests/property/fraud-rate.test.tsshows the achieved-versus-target convergence pattern every typology must satisfy.
Known gap
Typologies are not behind a formal interface or registry. planDay is one function with one branch per typology, and drainDay special-cases refunds. A real plugin registry, mirroring Sink and SinkFactory, would be a larger refactor.
Imperfection Types
ImperfectionTransformer (packages/engine/src/imperfections/transformer.ts) runs once per output sink. For each event it checks late_arrival, clock_skew, out_of_order and duplicate_delivery in turn and calls a small pure function per type (late-arrival.ts, clock-skew.ts, out-of-order.ts, duplicate.ts). That function decides, using the sink's own RNG, whether and how the delivered copy is corrupted. The truth event is never mutated. push queues a delivery, drainUntil(t) emits what is due by simulated time t, and flush() emits the rest.
Adding an Imperfection Type
- Add the key to
ImperfectionsConfiginpackages/spec/src/types.ts(extendImperfectionRateConfigif it needs extra params, asLateArrivalConfigandClockSkewConfigdo), plus a bounds invariant followingimperfection-bounds.ts(rate in [0, 0.2]). - Add a
CorruptionTypevariant inpackages/engine/src/types.ts. - Write
packages/engine/src/imperfections/<name>.tsexporting a pure decision function taking(config, rng)and any per-event context, and returning what to corrupt. - Add a branch to
ImperfectionTransformer.push(oremit, if the effect is on delivery order). It must add aLabelRecordwith the newcorruption_typeto the delivery'scorruptions, so the answer key lists every corruption. - Any new random draw changes generated output. Bump
ENGINE_VERSIONand re-baseline the golden master. - Add a property test in
packages/engine/tests/property/imperfections.test.tsasserting the corruption only touches delivered copies, never the truth store.
Known gap
Like typologies, this is a branch in one class and not a formal plugin interface.
Name-Dictionary Packs
A locale pack is mostly data. Two packs ship, en-IN and en-US, under packages/engine/data/name-packs/. packages/engine/src/naming/pack-loader.ts defines the pack shape: given-names.json and family-names.json (weighted entries) and merchant-grammars.json (naming patterns and word lists), each with a source field recording where the data came from.
- Add
packages/engine/data/name-packs/<locale>/with the same three files. - Add
<locale>toSHIPPED_LOCALE_PACKSinpackages/spec/src/invariants/reference-integrity.ts. The validator rejects any spec whoselocaleis not in that list. - Set
<locale>as the spec's top-levellocale.
