@entifix/transactions 0.13.0 → 0.15.0
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/README.md +6 -6
- package/dist/contracts/flow-messages.d.ts +16 -0
- package/dist/contracts/flow-messages.d.ts.map +1 -1
- package/dist/contracts/flow-messages.js +20 -0
- package/dist/contracts/flow-messages.js.map +1 -1
- package/dist/engine/index.d.ts +2 -9
- package/dist/engine/index.d.ts.map +1 -1
- package/dist/engine/index.js +3 -10
- package/dist/engine/index.js.map +1 -1
- package/dist/index.d.ts +0 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -14
- package/dist/index.js.map +1 -1
- package/dist/ports/event-bus.d.ts +2 -2
- package/dist/ports/event-bus.d.ts.map +1 -1
- package/dist/ports/event-bus.js.map +1 -1
- package/dist/ports/participant-client.d.ts +7 -2
- package/dist/ports/participant-client.d.ts.map +1 -1
- package/dist/ports/participant-client.js.map +1 -1
- package/package.json +6 -6
- package/dist/contracts/command.d.ts +0 -59
- package/dist/contracts/command.d.ts.map +0 -1
- package/dist/contracts/command.js +0 -46
- package/dist/contracts/command.js.map +0 -1
- package/dist/contracts/event.d.ts +0 -85
- package/dist/contracts/event.d.ts.map +0 -1
- package/dist/contracts/event.js +0 -80
- package/dist/contracts/event.js.map +0 -1
- package/dist/contracts/messages.d.ts +0 -37
- package/dist/contracts/messages.d.ts.map +0 -1
- package/dist/contracts/messages.js +0 -68
- package/dist/contracts/messages.js.map +0 -1
- package/dist/contracts/record.d.ts +0 -106
- package/dist/contracts/record.d.ts.map +0 -1
- package/dist/contracts/record.js +0 -27
- package/dist/contracts/record.js.map +0 -1
- package/dist/contracts/saga-definition.d.ts +0 -181
- package/dist/contracts/saga-definition.d.ts.map +0 -1
- package/dist/contracts/saga-definition.js +0 -95
- package/dist/contracts/saga-definition.js.map +0 -1
- package/dist/engine/local-saga-dispatcher.d.ts +0 -35
- package/dist/engine/local-saga-dispatcher.d.ts.map +0 -1
- package/dist/engine/local-saga-dispatcher.js +0 -32
- package/dist/engine/local-saga-dispatcher.js.map +0 -1
- package/dist/engine/resolve-template.d.ts +0 -48
- package/dist/engine/resolve-template.d.ts.map +0 -1
- package/dist/engine/resolve-template.js +0 -105
- package/dist/engine/resolve-template.js.map +0 -1
- package/dist/engine/resume-sweep.d.ts +0 -46
- package/dist/engine/resume-sweep.d.ts.map +0 -1
- package/dist/engine/resume-sweep.js +0 -74
- package/dist/engine/resume-sweep.js.map +0 -1
- package/dist/engine/run-saga.d.ts +0 -54
- package/dist/engine/run-saga.d.ts.map +0 -1
- package/dist/engine/run-saga.js +0 -463
- package/dist/engine/run-saga.js.map +0 -1
- package/dist/engine/run-transaction.d.ts +0 -55
- package/dist/engine/run-transaction.d.ts.map +0 -1
- package/dist/engine/run-transaction.js +0 -101
- package/dist/engine/run-transaction.js.map +0 -1
- package/dist/mixins/transaction-mixins.d.ts +0 -20
- package/dist/mixins/transaction-mixins.d.ts.map +0 -1
- package/dist/mixins/transaction-mixins.js +0 -13
- package/dist/mixins/transaction-mixins.js.map +0 -1
- package/dist/ports/lock-service.d.ts +0 -24
- package/dist/ports/lock-service.d.ts.map +0 -1
- package/dist/ports/lock-service.js +0 -5
- package/dist/ports/lock-service.js.map +0 -1
- package/dist/ports/saga-dispatcher.d.ts +0 -46
- package/dist/ports/saga-dispatcher.d.ts.map +0 -1
- package/dist/ports/saga-dispatcher.js +0 -5
- package/dist/ports/saga-dispatcher.js.map +0 -1
- package/dist/ports/saga-store.d.ts +0 -96
- package/dist/ports/saga-store.d.ts.map +0 -1
- package/dist/ports/saga-store.js +0 -19
- package/dist/ports/saga-store.js.map +0 -1
- package/dist/ports/sequence-service.d.ts +0 -17
- package/dist/ports/sequence-service.d.ts.map +0 -1
- package/dist/ports/sequence-service.js +0 -5
- package/dist/ports/sequence-service.js.map +0 -1
- package/dist/ports/transaction-handler.d.ts +0 -66
- package/dist/ports/transaction-handler.d.ts.map +0 -1
- package/dist/ports/transaction-handler.js +0 -5
- package/dist/ports/transaction-handler.js.map +0 -1
- package/dist/ports/transaction-sink.d.ts +0 -120
- package/dist/ports/transaction-sink.d.ts.map +0 -1
- package/dist/ports/transaction-sink.js +0 -28
- package/dist/ports/transaction-sink.js.map +0 -1
- package/dist/ports/transaction-stream-hub.d.ts +0 -28
- package/dist/ports/transaction-stream-hub.d.ts.map +0 -1
- package/dist/ports/transaction-stream-hub.js +0 -5
- package/dist/ports/transaction-stream-hub.js.map +0 -1
- package/dist/stream/transaction-stream-hub.d.ts +0 -18
- package/dist/stream/transaction-stream-hub.d.ts.map +0 -1
- package/dist/stream/transaction-stream-hub.js +0 -50
- package/dist/stream/transaction-stream-hub.js.map +0 -1
- package/dist/use-case/facade.uc.d.ts +0 -22
- package/dist/use-case/facade.uc.d.ts.map +0 -1
- package/dist/use-case/facade.uc.js +0 -55
- package/dist/use-case/facade.uc.js.map +0 -1
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import { Effect } from 'effect';
|
|
2
|
-
import { acceptedEvent, failedEvent } from '../contracts/event.js';
|
|
3
|
-
import { OutboxTag } from '../contracts/outbox.js';
|
|
4
|
-
import { CommandTag, LockHandlesTag, OutcomeTag } from '../mixins/transaction-mixins.js';
|
|
5
|
-
import { EventSourceTag } from '../ports/event-source.js';
|
|
6
|
-
import { executeUCFactory, freeUCFactory, lockUCFactory, rollbackUCFactory, validateUCFactory } from '../use-case/facade.uc.js';
|
|
7
|
-
/**
|
|
8
|
-
* The saga engine, split at the 202 boundary so validation/lock failures are
|
|
9
|
-
* synchronous (mapped to `400`/`409`) while the write runs asynchronously:
|
|
10
|
-
*
|
|
11
|
-
* - {@link acceptTransaction} — validate -> claim -> lock. Runs before the
|
|
12
|
-
* service answers `202`; its failures are the client's answer.
|
|
13
|
-
* - {@link completeTransaction} — execute -> free, or rollback -> record
|
|
14
|
-
* `failed` -> free. Forked after `202`; `free` always runs via `ensuring`, so
|
|
15
|
-
* locks never leak on a rollback path.
|
|
16
|
-
*
|
|
17
|
-
* **The engine never publishes.** Every event it produces goes to the
|
|
18
|
-
* {@link Outbox}, and a relay carries the outbox to the broker. That
|
|
19
|
-
* is the whole fix for the dual write: an event that only ever existed in a
|
|
20
|
-
* `bus.publish` call between two Mongo writes is lost when the broker is down,
|
|
21
|
-
* and the transaction is then mislabelled `STALE` despite having succeeded.
|
|
22
|
-
*/ /**
|
|
23
|
-
* Accept phase (synchronous).
|
|
24
|
-
*
|
|
25
|
-
* The order is deliberate: validate, then **claim, then lock**. The claim is the
|
|
26
|
-
* outbox insert for the `accepted` event, and its uniqueness on
|
|
27
|
-
* the message id (`<transactionId>:<step>`) is what makes the client-generated
|
|
28
|
-
* id an idempotency key — a replayed command is recognised here, before a lock is taken and
|
|
29
|
-
* before any work is forked. Locking first would make a retry queue behind its
|
|
30
|
-
* own original.
|
|
31
|
-
*/ export function acceptTransaction() {
|
|
32
|
-
return Effect.gen(function*() {
|
|
33
|
-
const command = yield* CommandTag;
|
|
34
|
-
const outbox = yield* OutboxTag;
|
|
35
|
-
const source = yield* EventSourceTag;
|
|
36
|
-
yield* validateUCFactory();
|
|
37
|
-
const claim = yield* outbox.enqueue(acceptedEvent(command, source));
|
|
38
|
-
if (claim === 'duplicate') {
|
|
39
|
-
return {
|
|
40
|
-
status: 'duplicate'
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
const handles = yield* lockUCFactory();
|
|
44
|
-
return {
|
|
45
|
-
status: 'accepted',
|
|
46
|
-
handles
|
|
47
|
-
};
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Execute phase (asynchronous continuation). Never leaks locks.
|
|
52
|
-
*
|
|
53
|
-
* The success arm records **nothing**: `execute` is contractually required to
|
|
54
|
-
* have written the `completed` entry in the same storage transaction as its
|
|
55
|
-
* state change, because only the handler holds the session that can make those
|
|
56
|
-
* two writes one. Recording it here as well would reintroduce the dual write in
|
|
57
|
-
* the opposite direction — an event announcing a write that may have rolled
|
|
58
|
-
* back.
|
|
59
|
-
*
|
|
60
|
-
* The failure arm does record, because there is no state change left to be
|
|
61
|
-
* atomic with: the rollback has already undone it.
|
|
62
|
-
*/ export function completeTransaction(handles) {
|
|
63
|
-
return Effect.gen(function*() {
|
|
64
|
-
const command = yield* CommandTag;
|
|
65
|
-
const outbox = yield* OutboxTag;
|
|
66
|
-
const source = yield* EventSourceTag;
|
|
67
|
-
const free = freeUCFactory().pipe(Effect.provideService(LockHandlesTag, handles), Effect.ignore);
|
|
68
|
-
yield* executeUCFactory().pipe(Effect.matchEffect({
|
|
69
|
-
onSuccess: ()=>Effect.void,
|
|
70
|
-
onFailure: (error)=>rollbackUCFactory().pipe(Effect.provideService(OutcomeTag, undefined), // The compensation's own failure is reported before it is
|
|
71
|
-
// discarded. Discarding it is right — the client is polling for a
|
|
72
|
-
// terminal state and must still get one, so the `failed` record
|
|
73
|
-
// below is written either way — but until this line the error
|
|
74
|
-
// reached nothing: not the record, whose `error` is `execute`'s,
|
|
75
|
-
// and not the log. An operator reading `FAILED` then reasonably
|
|
76
|
-
// concluded the write had been undone when it had not.
|
|
77
|
-
//
|
|
78
|
-
// Annotations rather than message arguments, matching the outbox
|
|
79
|
-
// relay: they reach a sink as structured fields, so a stranded
|
|
80
|
-
// transaction is queryable by its id instead of being parsed out of
|
|
81
|
-
// a rendered string.
|
|
82
|
-
//
|
|
83
|
-
// That is true as of this change and was not before it. A service's
|
|
84
|
-
// Effect→tooling logger bridge (`observability.ts`) dropped
|
|
85
|
-
// `annotations` outright and matched Effect's level labels with the
|
|
86
|
-
// wrong casing, so every annotated field was discarded and every
|
|
87
|
-
// level arrived as INFO — measured in Loki while verifying this log.
|
|
88
|
-
Effect.tapError((rollbackError)=>Effect.logError('transaction rollback failed').pipe(Effect.annotateLogs({
|
|
89
|
-
transactionId: command.transactionId,
|
|
90
|
-
entity: command.entity,
|
|
91
|
-
executeError: String(error),
|
|
92
|
-
rollbackError: String(rollbackError)
|
|
93
|
-
}))), Effect.ignore, Effect.andThen(// Best-effort: the transaction already failed, and a store that
|
|
94
|
-
// cannot take the failure record must not turn into an unhandled
|
|
95
|
-
// defect in a forked daemon. The recovery sweep is what notices.
|
|
96
|
-
Effect.ignore(outbox.enqueue(failedEvent(command, error, source)))))
|
|
97
|
-
}), Effect.ensuring(free));
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
//# sourceMappingURL=run-transaction.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/engine/run-transaction.ts"],"sourcesContent":["import { Effect } from 'effect';\n\nimport { acceptedEvent, failedEvent } from '../contracts/event.js';\nimport { OutboxTag } from '../contracts/outbox.js';\nimport {\n CommandTag,\n LockHandlesTag,\n OutcomeTag,\n} from '../mixins/transaction-mixins.js';\nimport { EventSourceTag } from '../ports/event-source.js';\nimport type { LockHandle } from '../ports/lock-service.js';\nimport {\n executeUCFactory,\n freeUCFactory,\n lockUCFactory,\n rollbackUCFactory,\n validateUCFactory,\n} from '../use-case/facade.uc.js';\n\n/**\n * The saga engine, split at the 202 boundary so validation/lock failures are\n * synchronous (mapped to `400`/`409`) while the write runs asynchronously:\n *\n * - {@link acceptTransaction} — validate -> claim -> lock. Runs before the\n * service answers `202`; its failures are the client's answer.\n * - {@link completeTransaction} — execute -> free, or rollback -> record\n * `failed` -> free. Forked after `202`; `free` always runs via `ensuring`, so\n * locks never leak on a rollback path.\n *\n * **The engine never publishes.** Every event it produces goes to the\n * {@link Outbox}, and a relay carries the outbox to the broker. That\n * is the whole fix for the dual write: an event that only ever existed in a\n * `bus.publish` call between two Mongo writes is lost when the broker is down,\n * and the transaction is then mislabelled `STALE` despite having succeeded.\n */\n\n/**\n * Accept phase (synchronous).\n *\n * The order is deliberate: validate, then **claim, then lock**. The claim is the\n * outbox insert for the `accepted` event, and its uniqueness on\n * the message id (`<transactionId>:<step>`) is what makes the client-generated\n * id an idempotency key — a replayed command is recognised here, before a lock is taken and\n * before any work is forked. Locking first would make a retry queue behind its\n * own original.\n */\nexport function acceptTransaction() {\n return Effect.gen(function* () {\n const command = yield* CommandTag;\n const outbox = yield* OutboxTag;\n const source = yield* EventSourceTag;\n\n yield* validateUCFactory();\n\n const claim = yield* outbox.enqueue(acceptedEvent(command, source));\n if (claim === 'duplicate') {\n return { status: 'duplicate' } as const;\n }\n\n const handles = yield* lockUCFactory();\n return { status: 'accepted', handles } as const;\n });\n}\n\n/** What {@link acceptTransaction} decided — a fresh command, or a replay. */\nexport type AcceptOutcome = Effect.Success<\n ReturnType<typeof acceptTransaction>\n>;\n\n/**\n * Execute phase (asynchronous continuation). Never leaks locks.\n *\n * The success arm records **nothing**: `execute` is contractually required to\n * have written the `completed` entry in the same storage transaction as its\n * state change, because only the handler holds the session that can make those\n * two writes one. Recording it here as well would reintroduce the dual write in\n * the opposite direction — an event announcing a write that may have rolled\n * back.\n *\n * The failure arm does record, because there is no state change left to be\n * atomic with: the rollback has already undone it.\n */\nexport function completeTransaction(handles: readonly LockHandle[]) {\n return Effect.gen(function* () {\n const command = yield* CommandTag;\n const outbox = yield* OutboxTag;\n const source = yield* EventSourceTag;\n\n const free = freeUCFactory().pipe(\n Effect.provideService(LockHandlesTag, handles),\n Effect.ignore,\n );\n\n yield* executeUCFactory().pipe(\n Effect.matchEffect({\n onSuccess: () => Effect.void,\n onFailure: error =>\n rollbackUCFactory().pipe(\n Effect.provideService(OutcomeTag, undefined),\n // The compensation's own failure is reported before it is\n // discarded. Discarding it is right — the client is polling for a\n // terminal state and must still get one, so the `failed` record\n // below is written either way — but until this line the error\n // reached nothing: not the record, whose `error` is `execute`'s,\n // and not the log. An operator reading `FAILED` then reasonably\n // concluded the write had been undone when it had not.\n //\n // Annotations rather than message arguments, matching the outbox\n // relay: they reach a sink as structured fields, so a stranded\n // transaction is queryable by its id instead of being parsed out of\n // a rendered string.\n //\n // That is true as of this change and was not before it. A service's\n // Effect→tooling logger bridge (`observability.ts`) dropped\n // `annotations` outright and matched Effect's level labels with the\n // wrong casing, so every annotated field was discarded and every\n // level arrived as INFO — measured in Loki while verifying this log.\n Effect.tapError(rollbackError =>\n Effect.logError('transaction rollback failed').pipe(\n Effect.annotateLogs({\n transactionId: command.transactionId,\n entity: command.entity,\n executeError: String(error),\n rollbackError: String(rollbackError),\n }),\n ),\n ),\n Effect.ignore,\n Effect.andThen(\n // Best-effort: the transaction already failed, and a store that\n // cannot take the failure record must not turn into an unhandled\n // defect in a forked daemon. The recovery sweep is what notices.\n Effect.ignore(\n outbox.enqueue(failedEvent(command, error, source)),\n ),\n ),\n ),\n }),\n Effect.ensuring(free),\n );\n });\n}\n"],"names":["Effect","acceptedEvent","failedEvent","OutboxTag","CommandTag","LockHandlesTag","OutcomeTag","EventSourceTag","executeUCFactory","freeUCFactory","lockUCFactory","rollbackUCFactory","validateUCFactory","acceptTransaction","gen","command","outbox","source","claim","enqueue","status","handles","completeTransaction","free","pipe","provideService","ignore","matchEffect","onSuccess","void","onFailure","error","undefined","tapError","rollbackError","logError","annotateLogs","transactionId","entity","executeError","String","andThen","ensuring"],"mappings":"AAAA,SAASA,MAAM,QAAQ,SAAS;AAEhC,SAASC,aAAa,EAAEC,WAAW,QAAQ,wBAAwB;AACnE,SAASC,SAAS,QAAQ,yBAAyB;AACnD,SACEC,UAAU,EACVC,cAAc,EACdC,UAAU,QACL,kCAAkC;AACzC,SAASC,cAAc,QAAQ,2BAA2B;AAE1D,SACEC,gBAAgB,EAChBC,aAAa,EACbC,aAAa,EACbC,iBAAiB,EACjBC,iBAAiB,QACZ,2BAA2B;AAElC;;;;;;;;;;;;;;;CAeC,GAED;;;;;;;;;CASC,GACD,OAAO,SAASC;IACd,OAAOb,OAAOc,GAAG,CAAC;QAChB,MAAMC,UAAU,OAAOX;QACvB,MAAMY,SAAS,OAAOb;QACtB,MAAMc,SAAS,OAAOV;QAEtB,OAAOK;QAEP,MAAMM,QAAQ,OAAOF,OAAOG,OAAO,CAAClB,cAAcc,SAASE;QAC3D,IAAIC,UAAU,aAAa;YACzB,OAAO;gBAAEE,QAAQ;YAAY;QAC/B;QAEA,MAAMC,UAAU,OAAOX;QACvB,OAAO;YAAEU,QAAQ;YAAYC;QAAQ;IACvC;AACF;AAOA;;;;;;;;;;;;CAYC,GACD,OAAO,SAASC,oBAAoBD,OAA8B;IAChE,OAAOrB,OAAOc,GAAG,CAAC;QAChB,MAAMC,UAAU,OAAOX;QACvB,MAAMY,SAAS,OAAOb;QACtB,MAAMc,SAAS,OAAOV;QAEtB,MAAMgB,OAAOd,gBAAgBe,IAAI,CAC/BxB,OAAOyB,cAAc,CAACpB,gBAAgBgB,UACtCrB,OAAO0B,MAAM;QAGf,OAAOlB,mBAAmBgB,IAAI,CAC5BxB,OAAO2B,WAAW,CAAC;YACjBC,WAAW,IAAM5B,OAAO6B,IAAI;YAC5BC,WAAWC,CAAAA,QACTpB,oBAAoBa,IAAI,CACtBxB,OAAOyB,cAAc,CAACnB,YAAY0B,YAClC,0DAA0D;gBAC1D,kEAAkE;gBAClE,gEAAgE;gBAChE,8DAA8D;gBAC9D,iEAAiE;gBACjE,gEAAgE;gBAChE,uDAAuD;gBACvD,EAAE;gBACF,iEAAiE;gBACjE,+DAA+D;gBAC/D,oEAAoE;gBACpE,qBAAqB;gBACrB,EAAE;gBACF,oEAAoE;gBACpE,4DAA4D;gBAC5D,oEAAoE;gBACpE,iEAAiE;gBACjE,qEAAqE;gBACrEhC,OAAOiC,QAAQ,CAACC,CAAAA,gBACdlC,OAAOmC,QAAQ,CAAC,+BAA+BX,IAAI,CACjDxB,OAAOoC,YAAY,CAAC;wBAClBC,eAAetB,QAAQsB,aAAa;wBACpCC,QAAQvB,QAAQuB,MAAM;wBACtBC,cAAcC,OAAOT;wBACrBG,eAAeM,OAAON;oBACxB,MAGJlC,OAAO0B,MAAM,EACb1B,OAAOyC,OAAO,CACZ,gEAAgE;gBAChE,iEAAiE;gBACjE,iEAAiE;gBACjEzC,OAAO0B,MAAM,CACXV,OAAOG,OAAO,CAACjB,YAAYa,SAASgB,OAAOd;QAIrD,IACAjB,OAAO0C,QAAQ,CAACnB;IAEpB;AACF"}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Context } from 'effect';
|
|
2
|
-
import type { TransactionCommand } from '../contracts/command.js';
|
|
3
|
-
import type { LockHandle } from '../ports/lock-service.js';
|
|
4
|
-
import type { TransactionOutcome } from '../ports/transaction-handler.js';
|
|
5
|
-
declare const CommandTag_base: Context.ServiceClass<CommandTag, "TransactionCommandTag", TransactionCommand<import("@entifix/core").SerializedEntity>>;
|
|
6
|
-
/**
|
|
7
|
-
* The runtime inputs the facade use-cases yield — the transaction analogue of
|
|
8
|
-
* `EntityTag`/`EntityIdTag`. The engine provides them at the right moment
|
|
9
|
-
* (`LockHandlesTag` after `lock`, `OutcomeTag` before `rollback`).
|
|
10
|
-
*/
|
|
11
|
-
export declare class CommandTag extends CommandTag_base {
|
|
12
|
-
}
|
|
13
|
-
declare const LockHandlesTag_base: Context.ServiceClass<LockHandlesTag, "LockHandlesTag", readonly LockHandle[]>;
|
|
14
|
-
export declare class LockHandlesTag extends LockHandlesTag_base {
|
|
15
|
-
}
|
|
16
|
-
declare const OutcomeTag_base: Context.ServiceClass<OutcomeTag, "TransactionOutcomeTag", TransactionOutcome | undefined>;
|
|
17
|
-
export declare class OutcomeTag extends OutcomeTag_base {
|
|
18
|
-
}
|
|
19
|
-
export {};
|
|
20
|
-
//# sourceMappingURL=transaction-mixins.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"transaction-mixins.d.ts","sourceRoot":"","sources":["../../src/mixins/transaction-mixins.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAEjC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;;AAE1E;;;;GAIG;AACH,qBAAa,UAAW,SAAQ,eAGJ;CAAG;;AAE/B,qBAAa,cAAe,SAAQ,mBAGf;CAAG;;AAExB,qBAAa,UAAW,SAAQ,eAGJ;CAAG"}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Context } from 'effect';
|
|
2
|
-
/**
|
|
3
|
-
* The runtime inputs the facade use-cases yield — the transaction analogue of
|
|
4
|
-
* `EntityTag`/`EntityIdTag`. The engine provides them at the right moment
|
|
5
|
-
* (`LockHandlesTag` after `lock`, `OutcomeTag` before `rollback`).
|
|
6
|
-
*/ export class CommandTag extends Context.Service()('TransactionCommandTag') {
|
|
7
|
-
}
|
|
8
|
-
export class LockHandlesTag extends Context.Service()('LockHandlesTag') {
|
|
9
|
-
}
|
|
10
|
-
export class OutcomeTag extends Context.Service()('TransactionOutcomeTag') {
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
//# sourceMappingURL=transaction-mixins.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/mixins/transaction-mixins.ts"],"sourcesContent":["import { Context } from 'effect';\n\nimport type { TransactionCommand } from '../contracts/command.js';\nimport type { LockHandle } from '../ports/lock-service.js';\nimport type { TransactionOutcome } from '../ports/transaction-handler.js';\n\n/**\n * The runtime inputs the facade use-cases yield — the transaction analogue of\n * `EntityTag`/`EntityIdTag`. The engine provides them at the right moment\n * (`LockHandlesTag` after `lock`, `OutcomeTag` before `rollback`).\n */\nexport class CommandTag extends Context.Service<\n CommandTag,\n TransactionCommand\n>()('TransactionCommandTag') {}\n\nexport class LockHandlesTag extends Context.Service<\n LockHandlesTag,\n readonly LockHandle[]\n>()('LockHandlesTag') {}\n\nexport class OutcomeTag extends Context.Service<\n OutcomeTag,\n TransactionOutcome | undefined\n>()('TransactionOutcomeTag') {}\n"],"names":["Context","CommandTag","Service","LockHandlesTag","OutcomeTag"],"mappings":"AAAA,SAASA,OAAO,QAAQ,SAAS;AAMjC;;;;CAIC,GACD,OAAO,MAAMC,mBAAmBD,QAAQE,OAAO,GAG3C;AAA0B;AAE9B,OAAO,MAAMC,uBAAuBH,QAAQE,OAAO,GAG/C;AAAmB;AAEvB,OAAO,MAAME,mBAAmBJ,QAAQE,OAAO,GAG3C;AAA0B"}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { StoreFailure } from '@entifix/core';
|
|
2
|
-
import { Context, type Effect } from 'effect';
|
|
3
|
-
/** A held lock; `token` proves ownership so release only frees our own lock. */
|
|
4
|
-
export interface LockHandle {
|
|
5
|
-
key: string;
|
|
6
|
-
token: string;
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Distributed mutual exclusion — the facade's `lock`/`free` steps. Implemented
|
|
10
|
-
* over Redis (`SET NX PX` + bounded retry) in `@entifix/redis`.
|
|
11
|
-
*/
|
|
12
|
-
export interface LockService {
|
|
13
|
-
/**
|
|
14
|
-
* Acquire `key`, retrying up to the adapter's bounded budget. Fails with
|
|
15
|
-
* {@link Contention} on contention (surfaced as `409`), not a `500`.
|
|
16
|
-
*/
|
|
17
|
-
acquire(key: string): Effect.Effect<LockHandle, StoreFailure>;
|
|
18
|
-
release(handle: LockHandle): Effect.Effect<void, StoreFailure>;
|
|
19
|
-
}
|
|
20
|
-
declare const LockServiceTag_base: Context.ServiceClass<LockServiceTag, "LockServiceTag", LockService>;
|
|
21
|
-
export declare class LockServiceTag extends LockServiceTag_base {
|
|
22
|
-
}
|
|
23
|
-
export {};
|
|
24
|
-
//# sourceMappingURL=lock-service.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"lock-service.d.ts","sourceRoot":"","sources":["../../src/ports/lock-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,YAAY,EAAE,MAAM,eAAe,CAAC;AAC9D,OAAO,EAAE,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAE9C,gFAAgF;AAChF,MAAM,WAAW,UAAU;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC;IAC9D,OAAO,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;CAChE;;AAED,qBAAa,cAAe,SAAQ,mBAGf;CAAG"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ports/lock-service.ts"],"sourcesContent":["import type { Contention, StoreFailure } from '@entifix/core';\nimport { Context, type Effect } from 'effect';\n\n/** A held lock; `token` proves ownership so release only frees our own lock. */\nexport interface LockHandle {\n key: string;\n token: string;\n}\n\n/**\n * Distributed mutual exclusion — the facade's `lock`/`free` steps. Implemented\n * over Redis (`SET NX PX` + bounded retry) in `@entifix/redis`.\n */\nexport interface LockService {\n /**\n * Acquire `key`, retrying up to the adapter's bounded budget. Fails with\n * {@link Contention} on contention (surfaced as `409`), not a `500`.\n */\n acquire(key: string): Effect.Effect<LockHandle, StoreFailure>;\n release(handle: LockHandle): Effect.Effect<void, StoreFailure>;\n}\n\nexport class LockServiceTag extends Context.Service<\n LockServiceTag,\n LockService\n>()('LockServiceTag') {}\n"],"names":["Context","LockServiceTag","Service"],"mappings":"AACA,SAASA,OAAO,QAAqB,SAAS;AAqB9C,OAAO,MAAMC,uBAAuBD,QAAQE,OAAO,GAG/C;AAAmB"}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import type { EntifixConnError } from '@entifix/core';
|
|
2
|
-
import { Context, type Effect } from 'effect';
|
|
3
|
-
import type { SagaCall } from '../contracts/saga-definition.js';
|
|
4
|
-
/** One call the engine wants made, fully resolved. */
|
|
5
|
-
export interface SagaDispatch {
|
|
6
|
-
/** The participant's configuration key — the dispatcher resolves the address. */
|
|
7
|
-
readonly participant: string;
|
|
8
|
-
readonly call: SagaCall;
|
|
9
|
-
/** `<sagaId>:<stepId>[:<index>]`, which the participant claims. */
|
|
10
|
-
readonly commandId: string;
|
|
11
|
-
/**
|
|
12
|
-
* The organization this call acts for, when the participant is tenant-plane.
|
|
13
|
-
*
|
|
14
|
-
* Absent for a platform-plane participant. Present, it becomes the
|
|
15
|
-
* `x-organization-id` header — and it is safe only because the crossing token is
|
|
16
|
-
* checked first, so the input the caller controls is never the input that
|
|
17
|
-
* authorizes.
|
|
18
|
-
*/
|
|
19
|
-
readonly organizationId?: string;
|
|
20
|
-
readonly body?: unknown;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* What a dispatched call answered.
|
|
24
|
-
*
|
|
25
|
-
* ⚠️ **A refusal is not a failure, and the split is the dispatcher's job.** A
|
|
26
|
-
* `409` from reserve is the business answer "not enough stock" and must fail the
|
|
27
|
-
* saga *forward* into compensation; a `503` is a participant that is down and
|
|
28
|
-
* the entry is retried. Collapsing the two either retries an out-of-stock line
|
|
29
|
-
* forever or permanently fails a checkout because a pod was restarting.
|
|
30
|
-
*
|
|
31
|
-
* So a `refused` response is a **success** of this port carrying `ok: false`,
|
|
32
|
-
* and only a transport or `5xx` failure is an `EntifixConnError`.
|
|
33
|
-
*/
|
|
34
|
-
export interface SagaResponse {
|
|
35
|
-
readonly ok: boolean;
|
|
36
|
-
readonly status: number;
|
|
37
|
-
readonly body: unknown;
|
|
38
|
-
}
|
|
39
|
-
export interface SagaDispatcher {
|
|
40
|
-
dispatch(dispatch: SagaDispatch): Effect.Effect<SagaResponse, EntifixConnError>;
|
|
41
|
-
}
|
|
42
|
-
declare const SagaDispatcherTag_base: Context.ServiceClass<SagaDispatcherTag, "SagaDispatcherTag", SagaDispatcher>;
|
|
43
|
-
export declare class SagaDispatcherTag extends SagaDispatcherTag_base {
|
|
44
|
-
}
|
|
45
|
-
export {};
|
|
46
|
-
//# sourceMappingURL=saga-dispatcher.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"saga-dispatcher.d.ts","sourceRoot":"","sources":["../../src/ports/saga-dispatcher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAE9C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAC;AAEhE,sDAAsD;AACtD,MAAM,WAAW,YAAY;IAC3B,iFAAiF;IACjF,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACxB,mEAAmE;IACnE,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B;;;;;;;OAOG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;CACzB;AAED;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,EAAE,EAAE,OAAO,CAAC;IACrB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,cAAc;IAC7B,QAAQ,CACN,QAAQ,EAAE,YAAY,GACrB,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE,gBAAgB,CAAC,CAAC;CAClD;;AAED,qBAAa,iBAAkB,SAAQ,sBAGf;CAAG"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ports/saga-dispatcher.ts"],"sourcesContent":["import type { EntifixConnError } from '@entifix/core';\nimport { Context, type Effect } from 'effect';\n\nimport type { SagaCall } from '../contracts/saga-definition.js';\n\n/** One call the engine wants made, fully resolved. */\nexport interface SagaDispatch {\n /** The participant's configuration key — the dispatcher resolves the address. */\n readonly participant: string;\n readonly call: SagaCall;\n /** `<sagaId>:<stepId>[:<index>]`, which the participant claims. */\n readonly commandId: string;\n /**\n * The organization this call acts for, when the participant is tenant-plane.\n *\n * Absent for a platform-plane participant. Present, it becomes the\n * `x-organization-id` header — and it is safe only because the crossing token is\n * checked first, so the input the caller controls is never the input that\n * authorizes.\n */\n readonly organizationId?: string;\n readonly body?: unknown;\n}\n\n/**\n * What a dispatched call answered.\n *\n * ⚠️ **A refusal is not a failure, and the split is the dispatcher's job.** A\n * `409` from reserve is the business answer \"not enough stock\" and must fail the\n * saga *forward* into compensation; a `503` is a participant that is down and\n * the entry is retried. Collapsing the two either retries an out-of-stock line\n * forever or permanently fails a checkout because a pod was restarting.\n *\n * So a `refused` response is a **success** of this port carrying `ok: false`,\n * and only a transport or `5xx` failure is an `EntifixConnError`.\n */\nexport interface SagaResponse {\n readonly ok: boolean;\n readonly status: number;\n readonly body: unknown;\n}\n\nexport interface SagaDispatcher {\n dispatch(\n dispatch: SagaDispatch,\n ): Effect.Effect<SagaResponse, EntifixConnError>;\n}\n\nexport class SagaDispatcherTag extends Context.Service<\n SagaDispatcherTag,\n SagaDispatcher\n>()('SagaDispatcherTag') {}\n"],"names":["Context","SagaDispatcherTag","Service"],"mappings":"AACA,SAASA,OAAO,QAAqB,SAAS;AA+C9C,OAAO,MAAMC,0BAA0BD,QAAQE,OAAO,GAGlD;AAAsB"}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import type { StoreFailure } from '@entifix/core';
|
|
2
|
-
import { Context, type Effect } from 'effect';
|
|
3
|
-
import type { SagaInputs, SagaStepOutcome } from '../contracts/saga-definition.js';
|
|
4
|
-
/**
|
|
5
|
-
* Where a saga instance is.
|
|
6
|
-
*
|
|
7
|
-
* `COMPENSATED` and `STRANDED` are deliberately different terminal states:
|
|
8
|
-
* "failed and reversed" and "failed with something still held" are the two
|
|
9
|
-
* outcomes an operator has to be able to tell apart, and a single `FAILED`
|
|
10
|
-
* would make the second look like the first.
|
|
11
|
-
*/
|
|
12
|
-
export declare const SAGA_STATES: readonly ["RUNNING", "COMPLETED", "COMPENSATING", "COMPENSATED", "STRANDED"];
|
|
13
|
-
export type SagaState = (typeof SAGA_STATES)[number];
|
|
14
|
-
/** One flow in progress, and what each of its steps returned. */
|
|
15
|
-
export interface SagaInstance {
|
|
16
|
-
readonly sagaId: string;
|
|
17
|
-
/** The {@link SagaDefinition}'s `name`. */
|
|
18
|
-
readonly definition: string;
|
|
19
|
-
readonly state: SagaState;
|
|
20
|
-
/** The step being dispatched, or the last one dispatched. */
|
|
21
|
-
readonly stepIndex: number;
|
|
22
|
-
readonly outcomes: readonly SagaStepOutcome[];
|
|
23
|
-
/**
|
|
24
|
-
* What the caller supplied, kept so a **different process** can finish this
|
|
25
|
-
* flow.
|
|
26
|
-
*
|
|
27
|
-
* ⚠️ This is the member that makes the instance a resumable record rather
|
|
28
|
-
* than a report on one. Without it a resumed walk knows which step is next
|
|
29
|
-
* and has nothing to dispatch it with: a fan-out step's cardinality is its
|
|
30
|
-
* input's length, and a body template resolves against `inputs` before it
|
|
31
|
-
* resolves against earlier outcomes.
|
|
32
|
-
*/
|
|
33
|
-
readonly inputs: SagaInputs;
|
|
34
|
-
/**
|
|
35
|
-
* How many times a sweep has picked this instance up, which is the ceiling
|
|
36
|
-
* that stops a resume loop.
|
|
37
|
-
*
|
|
38
|
-
* It plays the part `attempts` plays on an outbox entry, on the
|
|
39
|
-
* record that already exists rather than on a second one beside it. Past the
|
|
40
|
-
* ceiling the instance is settled `STRANDED` and logged — surfaced rather
|
|
41
|
-
* than retried forever, because a coordinator spinning on a permanent failure
|
|
42
|
-
* is how a customer's money stays captured with nobody told.
|
|
43
|
-
*/
|
|
44
|
-
readonly resumeAttempts: number;
|
|
45
|
-
readonly error?: string;
|
|
46
|
-
readonly createdAt: string;
|
|
47
|
-
readonly updatedAt: string;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* The saga's own state, in the `saga` store.
|
|
51
|
-
*
|
|
52
|
-
* A saga instance is a transaction's state with steps in it, so it belongs in
|
|
53
|
-
* the store that already holds transaction state — no new store, and the
|
|
54
|
-
* `transaction` slice keeps `domains: []`, because orchestration is a mechanism
|
|
55
|
-
* rather than a business domain.
|
|
56
|
-
*/
|
|
57
|
-
export interface SagaStore {
|
|
58
|
-
start(instance: Omit<SagaInstance, 'updatedAt'>): Effect.Effect<void, StoreFailure>;
|
|
59
|
-
/**
|
|
60
|
-
* Persist that a step is about to be dispatched.
|
|
61
|
-
*
|
|
62
|
-
* ⚠️ **Before the dispatch, never after.** The outbox's rule extended from
|
|
63
|
-
* events to commands: a coordinator that marks a step in flight only after
|
|
64
|
-
* calling has the dual write that record exists to close, with the same
|
|
65
|
-
* consequence — a crash between the two leaves a step that either ran twice
|
|
66
|
-
* or never ran, and the record says the opposite of the truth.
|
|
67
|
-
*/
|
|
68
|
-
beginStep(sagaId: string, stepIndex: number): Effect.Effect<void, StoreFailure>;
|
|
69
|
-
recordOutcome(sagaId: string, outcome: SagaStepOutcome): Effect.Effect<void, StoreFailure>;
|
|
70
|
-
settle(sagaId: string, state: SagaState, error?: string): Effect.Effect<void, StoreFailure>;
|
|
71
|
-
get(sagaId: string): Effect.Effect<SagaInstance | undefined, StoreFailure>;
|
|
72
|
-
/** Instances left `RUNNING` or `COMPENSATING` past a deadline, for the sweep. */
|
|
73
|
-
findStale(olderThanMs: number): Effect.Effect<readonly SagaInstance[], StoreFailure>;
|
|
74
|
-
/**
|
|
75
|
-
* Take ownership of a stale instance, or answer `undefined`.
|
|
76
|
-
*
|
|
77
|
-
* ⚠️ **A claim, not a read**, and it is what makes the sweep safe to run in
|
|
78
|
-
* more than one process: the same conditional write that increments
|
|
79
|
-
* `resumeAttempts` also re-stamps `updatedAt`, so an instance already picked
|
|
80
|
-
* up is no longer stale and the second sweeper gets nothing. Reading and then
|
|
81
|
-
* resuming would have two coordinators dispatching one flow — survivable,
|
|
82
|
-
* because every participant claims the command id, but it would double every
|
|
83
|
-
* call and make the attempt ceiling meaningless.
|
|
84
|
-
*
|
|
85
|
-
* Re-stamping is also what stops *this* sweep's next tick finding the
|
|
86
|
-
* instance it is still working on.
|
|
87
|
-
*/
|
|
88
|
-
claimForResume(sagaId: string, olderThanMs: number): Effect.Effect<SagaInstance | undefined, StoreFailure>;
|
|
89
|
-
/** Mark one step's calls as given back, so a resumed unwind skips them. */
|
|
90
|
-
markCompensated(sagaId: string, stepId: string): Effect.Effect<void, StoreFailure>;
|
|
91
|
-
}
|
|
92
|
-
declare const SagaStoreTag_base: Context.ServiceClass<SagaStoreTag, "SagaStoreTag", SagaStore>;
|
|
93
|
-
export declare class SagaStoreTag extends SagaStoreTag_base {
|
|
94
|
-
}
|
|
95
|
-
export {};
|
|
96
|
-
//# sourceMappingURL=saga-store.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"saga-store.d.ts","sourceRoot":"","sources":["../../src/ports/saga-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAE9C,OAAO,KAAK,EACV,UAAU,EACV,eAAe,EAChB,MAAM,iCAAiC,CAAC;AAEzC;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW,8EAMd,CAAC;AAEX,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,CAAC,CAAC;AAErD,iEAAiE;AACjE,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,2CAA2C;IAC3C,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;IAC1B,6DAA6D;IAC7D,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,SAAS,eAAe,EAAE,CAAC;IAC9C;;;;;;;;;OASG;IACH,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;IAC5B;;;;;;;;;OASG;IACH,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,SAAS;IACxB,KAAK,CACH,QAAQ,EAAE,IAAI,CAAC,YAAY,EAAE,WAAW,CAAC,GACxC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;IACrC;;;;;;;;OAQG;IACH,SAAS,CACP,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,GAChB,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;IACrC,aAAa,CACX,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,eAAe,GACvB,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;IACrC,MAAM,CACJ,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,SAAS,EAChB,KAAK,CAAC,EAAE,MAAM,GACb,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;IACrC,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,YAAY,GAAG,SAAS,EAAE,YAAY,CAAC,CAAC;IAC3E,iFAAiF;IACjF,SAAS,CACP,WAAW,EAAE,MAAM,GAClB,MAAM,CAAC,MAAM,CAAC,SAAS,YAAY,EAAE,EAAE,YAAY,CAAC,CAAC;IACxD;;;;;;;;;;;;;OAaG;IACH,cAAc,CACZ,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,GAClB,MAAM,CAAC,MAAM,CAAC,YAAY,GAAG,SAAS,EAAE,YAAY,CAAC,CAAC;IACzD,2EAA2E;IAC3E,eAAe,CACb,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,GACb,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;CACtC;;AAED,qBAAa,YAAa,SAAQ,iBAEjC;CAAG"}
|
package/dist/ports/saga-store.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { Context } from 'effect';
|
|
2
|
-
/**
|
|
3
|
-
* Where a saga instance is.
|
|
4
|
-
*
|
|
5
|
-
* `COMPENSATED` and `STRANDED` are deliberately different terminal states:
|
|
6
|
-
* "failed and reversed" and "failed with something still held" are the two
|
|
7
|
-
* outcomes an operator has to be able to tell apart, and a single `FAILED`
|
|
8
|
-
* would make the second look like the first.
|
|
9
|
-
*/ export const SAGA_STATES = [
|
|
10
|
-
'RUNNING',
|
|
11
|
-
'COMPLETED',
|
|
12
|
-
'COMPENSATING',
|
|
13
|
-
'COMPENSATED',
|
|
14
|
-
'STRANDED'
|
|
15
|
-
];
|
|
16
|
-
export class SagaStoreTag extends Context.Service()('SagaStoreTag') {
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
//# sourceMappingURL=saga-store.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ports/saga-store.ts"],"sourcesContent":["import type { StoreFailure } from '@entifix/core';\nimport { Context, type Effect } from 'effect';\n\nimport type {\n SagaInputs,\n SagaStepOutcome,\n} from '../contracts/saga-definition.js';\n\n/**\n * Where a saga instance is.\n *\n * `COMPENSATED` and `STRANDED` are deliberately different terminal states:\n * \"failed and reversed\" and \"failed with something still held\" are the two\n * outcomes an operator has to be able to tell apart, and a single `FAILED`\n * would make the second look like the first.\n */\nexport const SAGA_STATES = [\n 'RUNNING',\n 'COMPLETED',\n 'COMPENSATING',\n 'COMPENSATED',\n 'STRANDED',\n] as const;\n\nexport type SagaState = (typeof SAGA_STATES)[number];\n\n/** One flow in progress, and what each of its steps returned. */\nexport interface SagaInstance {\n readonly sagaId: string;\n /** The {@link SagaDefinition}'s `name`. */\n readonly definition: string;\n readonly state: SagaState;\n /** The step being dispatched, or the last one dispatched. */\n readonly stepIndex: number;\n readonly outcomes: readonly SagaStepOutcome[];\n /**\n * What the caller supplied, kept so a **different process** can finish this\n * flow.\n *\n * ⚠️ This is the member that makes the instance a resumable record rather\n * than a report on one. Without it a resumed walk knows which step is next\n * and has nothing to dispatch it with: a fan-out step's cardinality is its\n * input's length, and a body template resolves against `inputs` before it\n * resolves against earlier outcomes.\n */\n readonly inputs: SagaInputs;\n /**\n * How many times a sweep has picked this instance up, which is the ceiling\n * that stops a resume loop.\n *\n * It plays the part `attempts` plays on an outbox entry, on the\n * record that already exists rather than on a second one beside it. Past the\n * ceiling the instance is settled `STRANDED` and logged — surfaced rather\n * than retried forever, because a coordinator spinning on a permanent failure\n * is how a customer's money stays captured with nobody told.\n */\n readonly resumeAttempts: number;\n readonly error?: string;\n readonly createdAt: string;\n readonly updatedAt: string;\n}\n\n/**\n * The saga's own state, in the `saga` store.\n *\n * A saga instance is a transaction's state with steps in it, so it belongs in\n * the store that already holds transaction state — no new store, and the\n * `transaction` slice keeps `domains: []`, because orchestration is a mechanism\n * rather than a business domain.\n */\nexport interface SagaStore {\n start(\n instance: Omit<SagaInstance, 'updatedAt'>,\n ): Effect.Effect<void, StoreFailure>;\n /**\n * Persist that a step is about to be dispatched.\n *\n * ⚠️ **Before the dispatch, never after.** The outbox's rule extended from\n * events to commands: a coordinator that marks a step in flight only after\n * calling has the dual write that record exists to close, with the same\n * consequence — a crash between the two leaves a step that either ran twice\n * or never ran, and the record says the opposite of the truth.\n */\n beginStep(\n sagaId: string,\n stepIndex: number,\n ): Effect.Effect<void, StoreFailure>;\n recordOutcome(\n sagaId: string,\n outcome: SagaStepOutcome,\n ): Effect.Effect<void, StoreFailure>;\n settle(\n sagaId: string,\n state: SagaState,\n error?: string,\n ): Effect.Effect<void, StoreFailure>;\n get(sagaId: string): Effect.Effect<SagaInstance | undefined, StoreFailure>;\n /** Instances left `RUNNING` or `COMPENSATING` past a deadline, for the sweep. */\n findStale(\n olderThanMs: number,\n ): Effect.Effect<readonly SagaInstance[], StoreFailure>;\n /**\n * Take ownership of a stale instance, or answer `undefined`.\n *\n * ⚠️ **A claim, not a read**, and it is what makes the sweep safe to run in\n * more than one process: the same conditional write that increments\n * `resumeAttempts` also re-stamps `updatedAt`, so an instance already picked\n * up is no longer stale and the second sweeper gets nothing. Reading and then\n * resuming would have two coordinators dispatching one flow — survivable,\n * because every participant claims the command id, but it would double every\n * call and make the attempt ceiling meaningless.\n *\n * Re-stamping is also what stops *this* sweep's next tick finding the\n * instance it is still working on.\n */\n claimForResume(\n sagaId: string,\n olderThanMs: number,\n ): Effect.Effect<SagaInstance | undefined, StoreFailure>;\n /** Mark one step's calls as given back, so a resumed unwind skips them. */\n markCompensated(\n sagaId: string,\n stepId: string,\n ): Effect.Effect<void, StoreFailure>;\n}\n\nexport class SagaStoreTag extends Context.Service<SagaStoreTag, SagaStore>()(\n 'SagaStoreTag',\n) {}\n"],"names":["Context","SAGA_STATES","SagaStoreTag","Service"],"mappings":"AACA,SAASA,OAAO,QAAqB,SAAS;AAO9C;;;;;;;CAOC,GACD,OAAO,MAAMC,cAAc;IACzB;IACA;IACA;IACA;IACA;CACD,CAAU;AAwGX,OAAO,MAAMC,qBAAqBF,QAAQG,OAAO,GAC/C;AACC"}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { StoreFailure } from '@entifix/core';
|
|
2
|
-
import { Context, type Effect } from 'effect';
|
|
3
|
-
/**
|
|
4
|
-
* A monotonic, gap-free-under-contention counter — the mechanism that makes
|
|
5
|
-
* codes unique across service instances. Implemented as Redis `INCR` in
|
|
6
|
-
* `@entifix/redis`; the atomicity of `INCR` is what guarantees no two
|
|
7
|
-
* concurrent commands ever draw the same number.
|
|
8
|
-
*/
|
|
9
|
-
export interface SequenceService {
|
|
10
|
-
/** Atomically increments the named sequence and returns the new value. */
|
|
11
|
-
next(name: string): Effect.Effect<number, StoreFailure>;
|
|
12
|
-
}
|
|
13
|
-
declare const SequenceServiceTag_base: Context.ServiceClass<SequenceServiceTag, "SequenceServiceTag", SequenceService>;
|
|
14
|
-
export declare class SequenceServiceTag extends SequenceServiceTag_base {
|
|
15
|
-
}
|
|
16
|
-
export {};
|
|
17
|
-
//# sourceMappingURL=sequence-service.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sequence-service.d.ts","sourceRoot":"","sources":["../../src/ports/sequence-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAE9C;;;;;GAKG;AACH,MAAM,WAAW,eAAe;IAC9B,0EAA0E;IAC1E,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;CACzD;;AAED,qBAAa,kBAAmB,SAAQ,uBAGf;CAAG"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ports/sequence-service.ts"],"sourcesContent":["import type { StoreFailure } from '@entifix/core';\nimport { Context, type Effect } from 'effect';\n\n/**\n * A monotonic, gap-free-under-contention counter — the mechanism that makes\n * codes unique across service instances. Implemented as Redis `INCR` in\n * `@entifix/redis`; the atomicity of `INCR` is what guarantees no two\n * concurrent commands ever draw the same number.\n */\nexport interface SequenceService {\n /** Atomically increments the named sequence and returns the new value. */\n next(name: string): Effect.Effect<number, StoreFailure>;\n}\n\nexport class SequenceServiceTag extends Context.Service<\n SequenceServiceTag,\n SequenceService\n>()('SequenceServiceTag') {}\n"],"names":["Context","SequenceServiceTag","Service"],"mappings":"AACA,SAASA,OAAO,QAAqB,SAAS;AAa9C,OAAO,MAAMC,2BAA2BD,QAAQE,OAAO,GAGnD;AAAuB"}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import type { EntifixError, EntityId } from '@entifix/core';
|
|
2
|
-
import { Context, type Effect } from 'effect';
|
|
3
|
-
import type { TransactionCommand } from '../contracts/command.js';
|
|
4
|
-
/** What a successful `execute` produced — echoed into the completed event. */
|
|
5
|
-
export interface TransactionOutcome {
|
|
6
|
-
code: string;
|
|
7
|
-
entityId: EntityId;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* The domain-specific half of the facade. `validate`/`lock`/`execute`/`free`/
|
|
11
|
-
* `rollback` are standardised by the engine, but *what* a command validates to,
|
|
12
|
-
* *which* resources it locks, and *how* it executes/rolls back are the service's
|
|
13
|
-
* concern — injected as this handler.
|
|
14
|
-
*
|
|
15
|
-
* Methods return `R = never`: the service closes over its own dependencies (db,
|
|
16
|
-
* sequence, repository) when constructing the handler, exactly as
|
|
17
|
-
* `makeMongoRepository` closes over its `Db`.
|
|
18
|
-
*/
|
|
19
|
-
export interface TransactionHandler {
|
|
20
|
-
/** Reject a malformed/illegal command (surfaced as `400`). */
|
|
21
|
-
validate(command: TransactionCommand): Effect.Effect<void, EntifixError>;
|
|
22
|
-
/** The resource keys to lock for this command (e.g. the code sequence). */
|
|
23
|
-
lockKeys(command: TransactionCommand): readonly string[];
|
|
24
|
-
/**
|
|
25
|
-
* Assign the code and persist — the actual write.
|
|
26
|
-
*
|
|
27
|
-
* **The implementation must write the `completed` outbox entry in the same
|
|
28
|
-
* storage transaction as its state change**, as a full `OutboxEntry` whose
|
|
29
|
-
* `event` is the `DomainEvent` `completedEvent` builds and whose `eventId` is
|
|
30
|
-
* that event's `id`. The engine deliberately records
|
|
31
|
-
* nothing on the success path, because only an implementation holds the
|
|
32
|
-
* driver session that can make those two writes one fact; an engine-level
|
|
33
|
-
* record would be a second dual write, announcing a state change that may
|
|
34
|
-
* still roll back.
|
|
35
|
-
*
|
|
36
|
-
* Two rules that only a replica set will teach you, and only in production:
|
|
37
|
-
* drive the transaction with the driver's retrying helper (Mongo's
|
|
38
|
-
* `session.withTransaction`), because an election aborts in-flight
|
|
39
|
-
* transactions with a `TransientTransactionError` the *application* is
|
|
40
|
-
* expected to retry; and keep any non-transactional side effect — drawing a
|
|
41
|
-
* code from the Redis sequence, say — **outside** that retried callback, or a
|
|
42
|
-
* retry consumes a second value and skips a code.
|
|
43
|
-
*/
|
|
44
|
-
execute(command: TransactionCommand): Effect.Effect<TransactionOutcome, EntifixError>;
|
|
45
|
-
/**
|
|
46
|
-
* Undo a failed execution. Must be idempotent (e.g. delete-if-exists), since
|
|
47
|
-
* the recovery sweep may reach a transaction whose rollback already ran.
|
|
48
|
-
*
|
|
49
|
-
* **`outcome` is always `undefined` on the single-step path, and cannot be
|
|
50
|
-
* anything else there.** `rollback` runs on exactly one arm — the one where
|
|
51
|
-
* `execute` failed — so there is no outcome to hand it.
|
|
52
|
-
*
|
|
53
|
-
* That is a property of this path rather than an unfinished signature, and it
|
|
54
|
-
* stays true now that the multi-step engine exists beside it. A saga's
|
|
55
|
-
* compensation is dispatched by `runSaga` with **its own call's** recorded
|
|
56
|
-
* outcome (`SagaStepOutcome`), because reversing a reservation needs the
|
|
57
|
-
* reservation's id and a delete-if-exists does not. A single-step handler is
|
|
58
|
-
* untouched by that: the contract grows a second shape and keeps the first.
|
|
59
|
-
*/
|
|
60
|
-
rollback(command: TransactionCommand, outcome: TransactionOutcome | undefined): Effect.Effect<void, EntifixError>;
|
|
61
|
-
}
|
|
62
|
-
declare const TransactionHandlerTag_base: Context.ServiceClass<TransactionHandlerTag, "TransactionHandlerTag", TransactionHandler>;
|
|
63
|
-
export declare class TransactionHandlerTag extends TransactionHandlerTag_base {
|
|
64
|
-
}
|
|
65
|
-
export {};
|
|
66
|
-
//# sourceMappingURL=transaction-handler.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"transaction-handler.d.ts","sourceRoot":"","sources":["../../src/ports/transaction-handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC5D,OAAO,EAAE,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAE9C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAElE,8EAA8E;AAC9E,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,QAAQ,CAAC;CACpB;AAED;;;;;;;;;GASG;AACH,MAAM,WAAW,kBAAkB;IACjC,8DAA8D;IAC9D,QAAQ,CAAC,OAAO,EAAE,kBAAkB,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;IACzE,2EAA2E;IAC3E,QAAQ,CAAC,OAAO,EAAE,kBAAkB,GAAG,SAAS,MAAM,EAAE,CAAC;IACzD;;;;;;;;;;;;;;;;;;;OAmBG;IACH,OAAO,CACL,OAAO,EAAE,kBAAkB,GAC1B,MAAM,CAAC,MAAM,CAAC,kBAAkB,EAAE,YAAY,CAAC,CAAC;IACnD;;;;;;;;;;;;;;OAcG;IACH,QAAQ,CACN,OAAO,EAAE,kBAAkB,EAC3B,OAAO,EAAE,kBAAkB,GAAG,SAAS,GACtC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;CACtC;;AAED,qBAAa,qBAAsB,SAAQ,0BAGf;CAAG"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ports/transaction-handler.ts"],"sourcesContent":["import type { EntifixError, EntityId } from '@entifix/core';\nimport { Context, type Effect } from 'effect';\n\nimport type { TransactionCommand } from '../contracts/command.js';\n\n/** What a successful `execute` produced — echoed into the completed event. */\nexport interface TransactionOutcome {\n code: string;\n entityId: EntityId;\n}\n\n/**\n * The domain-specific half of the facade. `validate`/`lock`/`execute`/`free`/\n * `rollback` are standardised by the engine, but *what* a command validates to,\n * *which* resources it locks, and *how* it executes/rolls back are the service's\n * concern — injected as this handler.\n *\n * Methods return `R = never`: the service closes over its own dependencies (db,\n * sequence, repository) when constructing the handler, exactly as\n * `makeMongoRepository` closes over its `Db`.\n */\nexport interface TransactionHandler {\n /** Reject a malformed/illegal command (surfaced as `400`). */\n validate(command: TransactionCommand): Effect.Effect<void, EntifixError>;\n /** The resource keys to lock for this command (e.g. the code sequence). */\n lockKeys(command: TransactionCommand): readonly string[];\n /**\n * Assign the code and persist — the actual write.\n *\n * **The implementation must write the `completed` outbox entry in the same\n * storage transaction as its state change**, as a full `OutboxEntry` whose\n * `event` is the `DomainEvent` `completedEvent` builds and whose `eventId` is\n * that event's `id`. The engine deliberately records\n * nothing on the success path, because only an implementation holds the\n * driver session that can make those two writes one fact; an engine-level\n * record would be a second dual write, announcing a state change that may\n * still roll back.\n *\n * Two rules that only a replica set will teach you, and only in production:\n * drive the transaction with the driver's retrying helper (Mongo's\n * `session.withTransaction`), because an election aborts in-flight\n * transactions with a `TransientTransactionError` the *application* is\n * expected to retry; and keep any non-transactional side effect — drawing a\n * code from the Redis sequence, say — **outside** that retried callback, or a\n * retry consumes a second value and skips a code.\n */\n execute(\n command: TransactionCommand,\n ): Effect.Effect<TransactionOutcome, EntifixError>;\n /**\n * Undo a failed execution. Must be idempotent (e.g. delete-if-exists), since\n * the recovery sweep may reach a transaction whose rollback already ran.\n *\n * **`outcome` is always `undefined` on the single-step path, and cannot be\n * anything else there.** `rollback` runs on exactly one arm — the one where\n * `execute` failed — so there is no outcome to hand it.\n *\n * That is a property of this path rather than an unfinished signature, and it\n * stays true now that the multi-step engine exists beside it. A saga's\n * compensation is dispatched by `runSaga` with **its own call's** recorded\n * outcome (`SagaStepOutcome`), because reversing a reservation needs the\n * reservation's id and a delete-if-exists does not. A single-step handler is\n * untouched by that: the contract grows a second shape and keeps the first.\n */\n rollback(\n command: TransactionCommand,\n outcome: TransactionOutcome | undefined,\n ): Effect.Effect<void, EntifixError>;\n}\n\nexport class TransactionHandlerTag extends Context.Service<\n TransactionHandlerTag,\n TransactionHandler\n>()('TransactionHandlerTag') {}\n"],"names":["Context","TransactionHandlerTag","Service"],"mappings":"AACA,SAASA,OAAO,QAAqB,SAAS;AAqE9C,OAAO,MAAMC,8BAA8BD,QAAQE,OAAO,GAGtD;AAA0B"}
|