@openlup/outbox 0.0.0 → 0.13.1

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/package.json CHANGED
@@ -1,9 +1,62 @@
1
1
  {
2
2
  "name": "@openlup/outbox",
3
- "version": "0.0.0",
4
- "description": "Inert package-name registration placeholder; no outbox implementation.",
3
+ "version": "0.13.1",
4
+ "private": false,
5
+ "description": "Transactional PostgreSQL outbox rail; development preview.",
5
6
  "license": "Apache-2.0",
6
7
  "type": "module",
7
- "files": ["README.md"],
8
- "publishConfig": {"access": "public", "tag": "preview"}
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/openlup/openlup.git",
11
+ "directory": "packages/outbox"
12
+ },
13
+ "engines": {
14
+ "node": "24.x"
15
+ },
16
+ "packageManager": "npm@11.19.0",
17
+ "sideEffects": false,
18
+ "files": [
19
+ "dist/**",
20
+ "src/**",
21
+ "sql/**",
22
+ "AGENTS.md",
23
+ "README.md",
24
+ "CHANGELOG.md",
25
+ "LICENSE",
26
+ "release-gates.json"
27
+ ],
28
+ "scripts": {
29
+ "build": "tsc -p tsconfig.build.json",
30
+ "prepack": "npm run build",
31
+ "test": "vitest run test",
32
+ "prepublishOnly": "node -e \"throw new Error('OUTBOX_DIRECTORY_PUBLISH_REFUSED')\"",
33
+ "api:check": "node --experimental-strip-types ./scripts/api-contract.ts",
34
+ "api:update": "npm run build && node --experimental-strip-types ./scripts/api-contract.ts --update",
35
+ "ci": "npm run build && npm run test && npm run api:check"
36
+ },
37
+ "peerDependencies": {
38
+ "@openlup/core": "0.13.1"
39
+ },
40
+ "exports": {
41
+ ".": {
42
+ "types": "./dist/index.d.ts",
43
+ "import": "./dist/index.js",
44
+ "default": "./dist/index.js"
45
+ },
46
+ "./postgres": {
47
+ "types": "./dist/postgres.d.ts",
48
+ "import": "./dist/postgres.js",
49
+ "default": "./dist/postgres.js"
50
+ },
51
+ "./testing": {
52
+ "types": "./dist/testing.d.ts",
53
+ "import": "./dist/testing.js",
54
+ "default": "./dist/testing.js"
55
+ }
56
+ },
57
+ "publishConfig": {
58
+ "access": "public",
59
+ "provenance": true,
60
+ "tag": "preview"
61
+ }
9
62
  }
@@ -0,0 +1,53 @@
1
+ {
2
+ "schemaVersion": 5,
3
+ "kind": "rail",
4
+ "compatibilityPolicies": {
5
+ "internal-candidate": "Development-preview API; no stable compatibility claim.",
6
+ "testing-internal": "Synthetic protocol test support."
7
+ },
8
+ "packageSurface": {
9
+ ".": {
10
+ "role": "rail",
11
+ "maturity": "experimental",
12
+ "compatibilityPolicy": "internal-candidate",
13
+ "packageSmokeEvidence": [
14
+ "test/packageContract.test.ts"
15
+ ],
16
+ "entrypoint": "dist/index.d.ts",
17
+ "snapshot": "api/index.api.md"
18
+ },
19
+ "./postgres": {
20
+ "role": "rail",
21
+ "maturity": "experimental",
22
+ "compatibilityPolicy": "internal-candidate",
23
+ "packageSmokeEvidence": [
24
+ "test/packageContract.test.ts"
25
+ ],
26
+ "entrypoint": "dist/postgres.d.ts",
27
+ "snapshot": "api/postgres.api.md"
28
+ },
29
+ "./testing": {
30
+ "role": "testing",
31
+ "maturity": "testing",
32
+ "compatibilityPolicy": "testing-internal",
33
+ "packageSmokeEvidence": [
34
+ "test/packageContract.test.ts"
35
+ ],
36
+ "entrypoint": "dist/testing.d.ts",
37
+ "snapshot": "api/testing.api.md"
38
+ }
39
+ },
40
+ "pack": {
41
+ "code": {
42
+ "maxPackedBytes": 32768,
43
+ "maxFiles": 48
44
+ },
45
+ "docs": {
46
+ "maxPackedBytes": 16384,
47
+ "maxFiles": 12
48
+ },
49
+ "maxPackedBytes": 65536,
50
+ "maxUnpackedBytes": 262144,
51
+ "maxFiles": 64
52
+ }
53
+ }
@@ -0,0 +1,329 @@
1
+ -- Public platform communications outbox rail: the queue table, the dormant-event-type
2
+ -- allowlist, and the claim/ack protocol that drains them.
3
+ --
4
+ -- WHAT THIS FORWARD SHIPS, AND WHY IT IS THE FIRST ONE CARRYING FUNCTIONS.
5
+ -- The baseline and the read-parity forward both hold `grep -c 'CREATE FUNCTION'` = 0 on
6
+ -- purpose. This migration breaks that property deliberately, because the dispatch rail
7
+ -- has no non-RPC surface: claim is an atomic `UPDATE ... FOR UPDATE SKIP LOCKED` that
8
+ -- hands back a claim token and evaluates a per-aggregate ordering barrier inside the same
9
+ -- statement. Expressing it through the gateway's query builder would mean reimplementing
10
+ -- pessimistic locking in the application, i.e. a second and divergent rail. Four functions
11
+ -- is the whole crossing: no privilege model, no `private` schema, no queue statistics, no
12
+ -- pruning, no preview-matrix claim scope, and above all no enqueue -- rows are written by
13
+ -- capabilities that own their own business logic, and none of them is authored here.
14
+ --
15
+ -- AUTHORED, NOT COPIED. The protocol below was written for this migration against the
16
+ -- observable contract the platform outbox store port issues (five named RPC arguments sets,
17
+ -- a boolean/text/integer/SETOF return shape). It is not a cut of any historical migration:
18
+ -- the provider-specific idempotency window and the hardcoded product event-type vocabulary
19
+ -- that the managed implementation calls into are absent, the preview-matrix claim scope is
20
+ -- absent, row-level security and role grants are absent because this kernel creates no
21
+ -- roles to grant to, `SECURITY DEFINER` is absent for the same reason, every constraint is
22
+ -- named rather than left to a generated identifier, and `CREATE` is unconditional because a
23
+ -- manifest-ordered forward runs exactly once against a known prefix -- `IF NOT EXISTS` there
24
+ -- would hide the drift the migration ledger exists to catch. `SET search_path` is kept: it
25
+ -- is a real injection-hardening property that does not depend on a role model.
26
+
27
+ CREATE TABLE public.outbox_events (
28
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
29
+ created_at timestamptz NOT NULL DEFAULT now(),
30
+ available_at timestamptz NOT NULL DEFAULT now(),
31
+ processed_at timestamptz,
32
+ aggregate_type text NOT NULL,
33
+ aggregate_id uuid NOT NULL,
34
+ event_type text NOT NULL,
35
+ idempotency_key text NOT NULL,
36
+ status text NOT NULL DEFAULT 'pending',
37
+ attempts integer NOT NULL DEFAULT 0,
38
+ payload jsonb NOT NULL,
39
+ error text,
40
+ metadata jsonb NOT NULL DEFAULT '{}'::jsonb,
41
+ CONSTRAINT outbox_events_aggregate_type_nonempty_check CHECK (btrim(aggregate_type) <> ''),
42
+ CONSTRAINT outbox_events_event_type_nonempty_check CHECK (btrim(event_type) <> ''),
43
+ CONSTRAINT outbox_events_idempotency_key_nonempty_check CHECK (btrim(idempotency_key) <> ''),
44
+ CONSTRAINT outbox_events_attempts_check CHECK (attempts >= 0),
45
+ CONSTRAINT outbox_events_status_check
46
+ CHECK (status IN ('pending', 'processing', 'processed', 'failed', 'discarded')),
47
+ CONSTRAINT outbox_events_event_type_idempotency_key_key UNIQUE (event_type, idempotency_key)
48
+ );
49
+
50
+ CREATE INDEX idx_outbox_events_status_available
51
+ ON public.outbox_events (status, available_at);
52
+
53
+ CREATE INDEX idx_outbox_events_aggregate
54
+ ON public.outbox_events (aggregate_type, aggregate_id);
55
+
56
+ -- The claim barrier below refuses to skip an older unprocessed row for the same aggregate,
57
+ -- INCLUDING one whose event type the caller does not know about. Without a way to declare a
58
+ -- type intentionally unconsumed, a single retired producer would stall its aggregate
59
+ -- forever. This table is that declaration, and it ships empty: which types are dormant is
60
+ -- operator data, not platform content.
61
+ CREATE TABLE public.outbox_dormant_event_types (
62
+ event_type text PRIMARY KEY,
63
+ owner text NOT NULL,
64
+ reason text NOT NULL,
65
+ created_at timestamptz NOT NULL DEFAULT now(),
66
+ updated_at timestamptz NOT NULL DEFAULT now(),
67
+ CONSTRAINT outbox_dormant_event_types_event_type_nonempty_check CHECK (btrim(event_type) <> ''),
68
+ CONSTRAINT outbox_dormant_event_types_owner_nonempty_check CHECK (btrim(owner) <> ''),
69
+ CONSTRAINT outbox_dormant_event_types_reason_nonempty_check CHECK (btrim(reason) <> '')
70
+ );
71
+
72
+ -- Claim a batch for one allowlist of event types.
73
+ --
74
+ -- Three properties make this more than a SELECT, and each is load-bearing:
75
+ -- 1. an empty allowlist is refused, never widened -- a caller that forgot its filter must
76
+ -- not drain every event type on the instance;
77
+ -- 2. rows whose attempts already reached the ceiling are discarded before the scan, so an
78
+ -- exhausted row cannot be re-selected on every tick;
79
+ -- 3. per aggregate, an older unprocessed row blocks the newer one. A prior of an event
80
+ -- type the caller neither claims nor knows also blocks, unless it has been declared
81
+ -- dormant -- ordering within an aggregate is the guarantee producers rely on.
82
+ -- The claim token written into metadata is the authorization for every later acknowledgement:
83
+ -- knowing the row id is not enough, which is what makes a stale ack detectable rather than
84
+ -- destructive.
85
+ CREATE FUNCTION public.outbox_claim_batch(
86
+ p_event_types text[],
87
+ p_known_event_types text[] DEFAULT ARRAY[]::text[],
88
+ p_batch_size integer DEFAULT 25,
89
+ p_visibility_seconds integer DEFAULT 300,
90
+ p_max_attempts integer DEFAULT 8
91
+ )
92
+ RETURNS SETOF public.outbox_events
93
+ LANGUAGE plpgsql
94
+ SET search_path = public, pg_catalog
95
+ AS $$
96
+ DECLARE
97
+ v_batch integer := LEAST(GREATEST(COALESCE(p_batch_size, 25), 1), 100);
98
+ v_visibility integer := LEAST(GREATEST(COALESCE(p_visibility_seconds, 300), 300), 3600);
99
+ v_max integer := LEAST(GREATEST(COALESCE(p_max_attempts, 8), 1), 20);
100
+ v_known text[];
101
+ BEGIN
102
+ IF p_event_types IS NULL OR cardinality(p_event_types) = 0 THEN
103
+ RAISE EXCEPTION 'outbox_claim_allowlist_required' USING ERRCODE = '22023';
104
+ END IF;
105
+
106
+ -- Everything the caller can recognise: what it claims now, plus what it declares it knows
107
+ -- about. A prior outside this set is treated as foreign, and only the dormant table can
108
+ -- release it.
109
+ SELECT array_agg(DISTINCT event_type)
110
+ INTO v_known
111
+ FROM unnest(COALESCE(p_known_event_types, ARRAY[]::text[]) || p_event_types) AS known(event_type)
112
+ WHERE event_type IS NOT NULL AND btrim(event_type) <> '';
113
+
114
+ WITH exhausted AS (
115
+ SELECT event.id
116
+ FROM public.outbox_events event
117
+ WHERE event.event_type = ANY (p_event_types)
118
+ AND event.status IN ('pending', 'failed', 'processing')
119
+ AND event.available_at <= now()
120
+ AND event.attempts >= v_max
121
+ ORDER BY event.created_at, event.id
122
+ LIMIT 100
123
+ FOR UPDATE SKIP LOCKED
124
+ )
125
+ UPDATE public.outbox_events event
126
+ SET status = 'discarded',
127
+ error = left(COALESCE(event.error, '') || ' | max_attempts_exhausted_at_claim', 2000),
128
+ metadata = event.metadata || jsonb_build_object(
129
+ 'discardReason', 'max_attempts',
130
+ 'discardedAt', now(),
131
+ 'finalAttempts', event.attempts)
132
+ FROM exhausted
133
+ WHERE event.id = exhausted.id;
134
+
135
+ RETURN QUERY
136
+ WITH candidates AS (
137
+ SELECT event.id
138
+ FROM public.outbox_events event
139
+ WHERE event.event_type = ANY (p_event_types)
140
+ AND event.available_at <= now()
141
+ AND event.status IN ('pending', 'failed', 'processing')
142
+ AND event.attempts < v_max
143
+ AND NOT EXISTS (
144
+ SELECT 1
145
+ FROM public.outbox_events prior
146
+ WHERE prior.aggregate_type = event.aggregate_type
147
+ AND prior.aggregate_id = event.aggregate_id
148
+ AND prior.status IN ('pending', 'failed', 'processing')
149
+ AND (prior.created_at, prior.id) < (event.created_at, event.id)
150
+ AND (
151
+ prior.event_type = ANY (p_event_types)
152
+ OR (
153
+ prior.event_type <> ALL (COALESCE(v_known, ARRAY[]::text[]))
154
+ AND NOT EXISTS (
155
+ SELECT 1
156
+ FROM public.outbox_dormant_event_types dormant
157
+ WHERE dormant.event_type = prior.event_type
158
+ )
159
+ )
160
+ ))
161
+ ORDER BY event.created_at, event.id
162
+ LIMIT v_batch
163
+ FOR UPDATE SKIP LOCKED
164
+ )
165
+ UPDATE public.outbox_events event
166
+ SET status = 'processing',
167
+ attempts = event.attempts + 1,
168
+ available_at = now() + make_interval(secs => v_visibility),
169
+ metadata = event.metadata || jsonb_build_object(
170
+ 'lastClaimedAt', now(),
171
+ 'claimToken', gen_random_uuid()::text)
172
+ FROM candidates
173
+ WHERE event.id = candidates.id
174
+ RETURNING event.*;
175
+ END;
176
+ $$;
177
+
178
+ -- Acknowledge success. Returns false rather than raising when the row is no longer the
179
+ -- caller's: a lease that expired and was re-claimed elsewhere is an ordinary race, and the
180
+ -- caller logs it instead of aborting a batch.
181
+ CREATE FUNCTION public.outbox_mark_processed(
182
+ p_event_id uuid,
183
+ p_claim_token text,
184
+ p_metadata jsonb DEFAULT '{}'::jsonb
185
+ )
186
+ RETURNS boolean
187
+ LANGUAGE sql
188
+ SET search_path = public, pg_catalog
189
+ AS $$
190
+ WITH updated AS (
191
+ UPDATE public.outbox_events
192
+ SET status = 'processed',
193
+ processed_at = now(),
194
+ error = NULL,
195
+ metadata = metadata || COALESCE(p_metadata, '{}'::jsonb)
196
+ WHERE id = p_event_id
197
+ AND status = 'processing'
198
+ AND metadata->>'claimToken' = p_claim_token
199
+ RETURNING id
200
+ )
201
+ SELECT EXISTS (SELECT 1 FROM updated);
202
+ $$;
203
+
204
+ -- Acknowledge failure. The outcome the handler asks for is a request, not a verdict: a
205
+ -- 'retry' on a row that has already spent its attempts becomes a discard, so the ceiling is
206
+ -- enforced in one place instead of in every caller.
207
+ --
208
+ -- 'snooze' means the downstream is unavailable rather than the row being bad, so the attempt
209
+ -- taken at claim is refunded -- an outage must not consume the DLQ budget of the events it
210
+ -- happened to touch. Retry delay is exponential with equal jitter in [d/2, d]: without the
211
+ -- jitter every row that failed in the same tick returns in the same later tick, and the
212
+ -- convoy reforms on each retry instead of dispersing.
213
+ CREATE FUNCTION public.outbox_mark_failed(
214
+ p_event_id uuid,
215
+ p_claim_token text,
216
+ p_error text,
217
+ p_outcome text DEFAULT 'retry',
218
+ p_base_delay_seconds integer DEFAULT 60,
219
+ p_max_delay_seconds integer DEFAULT 3600,
220
+ p_max_attempts integer DEFAULT 8,
221
+ p_snooze_seconds integer DEFAULT 300
222
+ )
223
+ RETURNS text
224
+ LANGUAGE plpgsql
225
+ SET search_path = public, pg_catalog
226
+ AS $$
227
+ DECLARE
228
+ v_row public.outbox_events%ROWTYPE;
229
+ v_base integer := LEAST(GREATEST(COALESCE(p_base_delay_seconds, 60), 5), 3600);
230
+ v_cap integer;
231
+ v_max integer := LEAST(GREATEST(COALESCE(p_max_attempts, 8), 1), 20);
232
+ v_snooze integer := LEAST(GREATEST(COALESCE(p_snooze_seconds, 300), 60), 3600);
233
+ v_delay double precision;
234
+ BEGIN
235
+ IF p_outcome IS NULL OR p_outcome NOT IN ('retry', 'discard', 'snooze') THEN
236
+ RAISE EXCEPTION 'outbox_mark_failed_invalid_outcome' USING ERRCODE = '22023';
237
+ END IF;
238
+
239
+ v_cap := LEAST(GREATEST(v_base, COALESCE(p_max_delay_seconds, 3600)), 86400);
240
+
241
+ SELECT *
242
+ INTO v_row
243
+ FROM public.outbox_events
244
+ WHERE id = p_event_id
245
+ AND status = 'processing'
246
+ AND metadata->>'claimToken' = p_claim_token
247
+ FOR UPDATE;
248
+
249
+ IF NOT FOUND THEN
250
+ RETURN 'missed';
251
+ END IF;
252
+
253
+ IF p_outcome = 'discard' OR (p_outcome = 'retry' AND v_row.attempts >= v_max) THEN
254
+ UPDATE public.outbox_events
255
+ SET status = 'discarded',
256
+ error = left(p_error, 2000),
257
+ metadata = metadata || jsonb_build_object(
258
+ 'discardReason',
259
+ CASE WHEN p_outcome = 'discard' THEN 'permanent_error' ELSE 'max_attempts' END,
260
+ 'discardedAt', now(),
261
+ 'finalAttempts', v_row.attempts)
262
+ WHERE id = v_row.id;
263
+ RETURN 'discarded';
264
+ END IF;
265
+
266
+ IF p_outcome = 'snooze' THEN
267
+ UPDATE public.outbox_events
268
+ SET status = 'failed',
269
+ attempts = GREATEST(v_row.attempts - 1, 0),
270
+ available_at = now() + make_interval(secs => v_snooze),
271
+ error = left(p_error, 2000),
272
+ metadata = metadata || jsonb_build_object(
273
+ 'snoozeCount', COALESCE((metadata->>'snoozeCount')::integer, 0) + 1,
274
+ 'lastSnoozedAt', now())
275
+ WHERE id = v_row.id;
276
+ RETURN 'snoozed';
277
+ END IF;
278
+
279
+ v_delay := LEAST(
280
+ v_cap::double precision,
281
+ v_base::double precision * power(2, LEAST(GREATEST(v_row.attempts - 1, 0), 16))
282
+ ) * (0.5 + random() * 0.5);
283
+
284
+ UPDATE public.outbox_events
285
+ SET status = 'failed',
286
+ available_at = now() + make_interval(secs => v_delay),
287
+ error = left(p_error, 2000)
288
+ WHERE id = v_row.id;
289
+ RETURN 'failed';
290
+ END;
291
+ $$;
292
+
293
+ -- Hand back rows the caller claimed but never ran -- a drain loop that aborts mid-batch, or
294
+ -- one handler reporting the downstream is out. The attempt taken at claim is refunded for
295
+ -- exactly the same reason as in 'snooze': nothing was tried, so nothing was spent. Ids and
296
+ -- tokens are supplied as parallel arrays and must agree in length, because a mismatch means
297
+ -- the caller lost track of which token belongs to which row and releasing the wrong pairing
298
+ -- would silently release nothing.
299
+ CREATE FUNCTION public.outbox_release_unprocessed(
300
+ p_event_ids uuid[],
301
+ p_claim_tokens text[],
302
+ p_delay_seconds integer DEFAULT 0
303
+ )
304
+ RETURNS integer
305
+ LANGUAGE plpgsql
306
+ SET search_path = public, pg_catalog
307
+ AS $$
308
+ DECLARE
309
+ v_delay integer := LEAST(GREATEST(COALESCE(p_delay_seconds, 0), 0), 3600);
310
+ v_count integer;
311
+ BEGIN
312
+ IF p_event_ids IS NULL OR p_claim_tokens IS NULL
313
+ OR cardinality(p_event_ids) <> cardinality(p_claim_tokens) THEN
314
+ RAISE EXCEPTION 'outbox_release_invalid_input' USING ERRCODE = '22023';
315
+ END IF;
316
+
317
+ UPDATE public.outbox_events event
318
+ SET status = 'pending',
319
+ available_at = now() + make_interval(secs => v_delay),
320
+ attempts = GREATEST(event.attempts - 1, 0)
321
+ FROM unnest(p_event_ids, p_claim_tokens) AS released(id, token)
322
+ WHERE event.id = released.id
323
+ AND event.status = 'processing'
324
+ AND event.metadata->>'claimToken' = released.token;
325
+
326
+ GET DIAGNOSTICS v_count = ROW_COUNT;
327
+ RETURN v_count;
328
+ END;
329
+ $$;
@@ -0,0 +1,11 @@
1
+ {
2
+ "schemaVersion": 1,
3
+ "version": "outbox-1",
4
+ "migrations": [
5
+ {
6
+ "file": "0001_outbox.sql",
7
+ "source": "db/platform/migrations/20260807190000_comms_outbox_rail.sql",
8
+ "sha256": "dfb3e9808714846738222bb74a1bfdb38ceafdde2ca8a0c71ab835739fb333bd"
9
+ }
10
+ ]
11
+ }
package/src/config.ts ADDED
@@ -0,0 +1,79 @@
1
+ import type { OutboxDispatchConfig } from "./contracts.js";
2
+ const CONFIG_BOUNDS = {
3
+ batchSize: { env: "COMMERCE_OUTBOX_DISPATCH_BATCH_SIZE", def: 25, min: 1, max: 100 },
4
+ maxAttempts: { env: "COMMERCE_OUTBOX_DISPATCH_MAX_ATTEMPTS", def: 8, min: 1, max: 20 },
5
+ visibilitySeconds: {
6
+ env: "COMMERCE_OUTBOX_DISPATCH_VISIBILITY_SECONDS",
7
+ def: 300,
8
+ min: 300,
9
+ max: 3600,
10
+ },
11
+ backoffBaseSeconds: { env: "COMMERCE_OUTBOX_DISPATCH_BACKOFF_BASE_SECONDS", def: 60, min: 5, max: 3600 },
12
+ backoffCapSeconds: { env: "COMMERCE_OUTBOX_DISPATCH_BACKOFF_CAP_SECONDS", def: 3600, min: 5, max: 86_400 },
13
+ snoozeSeconds: { env: "COMMERCE_OUTBOX_DISPATCH_SNOOZE_SECONDS", def: 300, min: 60, max: 3600 },
14
+ maxSnoozes: { env: "COMMERCE_OUTBOX_DISPATCH_MAX_SNOOZES", def: 48, min: 1, max: 1000 },
15
+ // Max keeps a >= 15s tail under the cron's hard kill: drain stop + in-flight
16
+ // handler + marks + queue stats + finishJobRun must all fit after the budget.
17
+ softBudgetMs: {
18
+ env: "COMMERCE_OUTBOX_DISPATCH_SOFT_BUDGET_MS",
19
+ def: 40_000,
20
+ min: 5000,
21
+ max: 60 * 1000 - 15_000,
22
+ },
23
+ } as const;
24
+
25
+ function readBoundedInt(
26
+ env: Record<string, string | undefined>,
27
+ bound: { env: string; def: number; min: number; max: number },
28
+ ): number {
29
+ const raw = env[bound.env];
30
+ if (raw === undefined || raw.trim() === "") return bound.def;
31
+ const parsed = Number.parseInt(raw, 10);
32
+ if (!Number.isFinite(parsed)) {
33
+ console.warn(`[outbox-dispatch] invalid ${bound.env}=${raw}; using default ${bound.def}`);
34
+ return bound.def;
35
+ }
36
+ const clamped = Math.min(Math.max(parsed, bound.min), bound.max);
37
+ if (clamped !== parsed) {
38
+ console.warn(`[outbox-dispatch] ${bound.env}=${parsed} clamped to ${clamped}`);
39
+ }
40
+ return clamped;
41
+ }
42
+
43
+ export function readOutboxDispatchConfig(
44
+ env: Record<string, string | undefined>,
45
+ prefix = "COMMERCE_OUTBOX_DISPATCH_",
46
+ ): OutboxDispatchConfig {
47
+ const bounds = Object.fromEntries(Object.entries(CONFIG_BOUNDS).map(([key, bound]) => [key, { ...bound, env: prefix + bound.env.slice("COMMERCE_OUTBOX_DISPATCH_".length) }])) as typeof CONFIG_BOUNDS;
48
+ const config: OutboxDispatchConfig = {
49
+ batchSize: readBoundedInt(env, bounds.batchSize),
50
+ maxAttempts: readBoundedInt(env, bounds.maxAttempts),
51
+ visibilitySeconds: readBoundedInt(env, bounds.visibilitySeconds),
52
+ backoffBaseSeconds: readBoundedInt(env, bounds.backoffBaseSeconds),
53
+ backoffCapSeconds: readBoundedInt(env, bounds.backoffCapSeconds),
54
+ snoozeSeconds: readBoundedInt(env, bounds.snoozeSeconds),
55
+ maxSnoozes: readBoundedInt(env, bounds.maxSnoozes),
56
+ softBudgetMs: readBoundedInt(env, bounds.softBudgetMs),
57
+ };
58
+ if (config.backoffCapSeconds < config.backoffBaseSeconds) {
59
+ console.warn(
60
+ `[outbox-dispatch] backoff cap ${config.backoffCapSeconds}s below base ${config.backoffBaseSeconds}s; raising cap to base`,
61
+ );
62
+ config.backoffCapSeconds = config.backoffBaseSeconds;
63
+ }
64
+ return config;
65
+ }
66
+
67
+ /** @beta Scheduled configuration is stricter than the bare immediate worker. */
68
+ export function validateScheduledConfig(config: OutboxDispatchConfig, hostLimitSeconds: number, timeouts: readonly number[]): void {
69
+ const fail = (key: string, fix: string): never => { throw new Error(`@openlup/outbox: ${key}: ${fix}`); };
70
+ if (!Number.isFinite(hostLimitSeconds) || hostLimitSeconds < 20 || hostLimitSeconds > 720) fail("hostLimitSeconds", "use 20–720 seconds");
71
+ for (const [key, bound] of Object.entries(CONFIG_BOUNDS)) {
72
+ const value = config[key as keyof OutboxDispatchConfig];
73
+ const min = key === "visibilitySeconds" ? Math.min(5 * hostLimitSeconds, 3600) : bound.min;
74
+ const max = key === "softBudgetMs" ? hostLimitSeconds * 1000 - 15000 : bound.max;
75
+ if (!Number.isInteger(value) || value < min || value > max) fail(key, `use an integer in ${min}–${max}`);
76
+ }
77
+ if (config.backoffCapSeconds < config.backoffBaseSeconds) fail("backoffCapSeconds", "keep cap at least the base delay");
78
+ for (const timeout of timeouts) if (!Number.isFinite(timeout) || timeout <= 0 || timeout + 2000 > config.softBudgetMs) fail("handler.timeoutMs", "fit the effective timeout plus 2000 ms mark margin in the budget");
79
+ }
@@ -0,0 +1,86 @@
1
+ import type { OutboxHandler as KernelHandler, OutboxEventRow } from "@openlup/core/outbox";
2
+ export type { OutboxEventRow, OutboxHandlerOutcome, OutboxHandlerExecutionContext, OutboxHandlerDescriptor } from "@openlup/core/outbox";
3
+ /** @beta The worker creates the host's standard AbortController. */
4
+ export type OutboxHandler = KernelHandler<AbortSignal>;
5
+ export type OutboxHandlerRegistry = ReadonlyMap<string, OutboxHandler>;
6
+
7
+ export type OutboxMarkFailedStatus = "failed" | "discarded" | "snoozed" | "missed";
8
+
9
+ export interface OutboxStore {
10
+ /** Read-only preflight used to admit a terminal handler when active providers are unavailable. */
11
+ hasPendingEventType?(eventType: string): Promise<boolean>;
12
+ claimBatch(input: {
13
+ eventTypes: readonly string[];
14
+ knownEventTypes?: readonly string[];
15
+ batchSize: number;
16
+ visibilitySeconds: number;
17
+ maxAttempts: number;
18
+ }): Promise<OutboxEventRow[]>;
19
+ markProcessed(input: {
20
+ eventId: string;
21
+ claimToken: string;
22
+ metadata?: Record<string, unknown>;
23
+ }): Promise<{ applied: boolean }>;
24
+ markFailed(input: {
25
+ eventId: string;
26
+ claimToken: string;
27
+ error: string;
28
+ outcome: "retry" | "discard" | "snooze";
29
+ baseDelaySeconds: number;
30
+ maxDelaySeconds: number;
31
+ maxAttempts: number;
32
+ snoozeSeconds: number;
33
+ }): Promise<{ status: OutboxMarkFailedStatus }>;
34
+ releaseUnprocessed(
35
+ items: ReadonlyArray<{ eventId: string; claimToken: string }>,
36
+ delaySeconds: number,
37
+ ): Promise<number>;
38
+ }
39
+
40
+ /** Managed-only best-effort queue observation, separate from core claim/ack policy. */
41
+ export interface OutboxQueueDiagnostics {
42
+ queueStats(): Promise<Record<string, unknown>>;
43
+ }
44
+
45
+ export interface Clock {
46
+ now(): number;
47
+ }
48
+
49
+ export interface OutboxDispatchRunResult {
50
+ ok: boolean;
51
+ checked: number;
52
+ updated: number;
53
+ failures: number;
54
+ skipped: boolean;
55
+ reason?: string;
56
+ claimed: number;
57
+ processed: number;
58
+ retried: number;
59
+ snoozed: number;
60
+ discarded: number;
61
+ released: number;
62
+ duplicateInRun: number;
63
+ leaseLost: number;
64
+ batches: number;
65
+ runMs: number;
66
+ byEventType: Record<string, Record<string, number>>;
67
+ discardedEventIds: string[];
68
+ queue?: Record<string, unknown>;
69
+ }
70
+
71
+ export interface OutboxDispatchConfig {
72
+ batchSize: number;
73
+ maxAttempts: number;
74
+ visibilitySeconds: number;
75
+ backoffBaseSeconds: number;
76
+ backoffCapSeconds: number;
77
+ snoozeSeconds: number;
78
+ maxSnoozes: number;
79
+ softBudgetMs: number;
80
+ }
81
+
82
+
83
+ /** @beta Logging defaults retain the dispatch console transcript. */
84
+ export interface OutboxLogger { warn(...args: unknown[]): void; error(...args: unknown[]): void }
85
+ /** @beta Synchronous bounded observations; no payloads or credentials. */
86
+ export interface OutboxObservation { event: "claimed" | "finished"; count: number }
package/src/enqueue.ts ADDED
@@ -0,0 +1,15 @@
1
+ /** @beta Caller executes this inside the same transaction as its domain write. */
2
+ export interface EnqueueEvent {
3
+ aggregateType: string; aggregateId: string; eventType: string; idempotencyKey: string;
4
+ payload: Record<string, unknown>; metadata?: Record<string, unknown>;
5
+ availableAt?: string;
6
+ }
7
+ /** @beta Duplicate enqueue keeps the durable event identity; no independent commit. */
8
+ export function buildOutboxEnqueue(event: EnqueueEvent): { text: string; values: readonly unknown[] } {
9
+ return {
10
+ text: `insert into public.outbox_events (aggregate_type, aggregate_id, event_type, idempotency_key, payload, metadata, available_at)
11
+ values ($1, $2::uuid, $3, $4, $5::jsonb, $6::jsonb, coalesce($7::timestamptz, now()))
12
+ on conflict (event_type, idempotency_key) do nothing returning id::text`,
13
+ values: [event.aggregateType, event.aggregateId, event.eventType, event.idempotencyKey, JSON.stringify(event.payload), JSON.stringify(event.metadata ?? {}), event.availableAt ?? null],
14
+ };
15
+ }