@ultimat3/action 31.22.0 → 31.22.3

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 CHANGED
@@ -912,8 +912,12 @@ to `maxDelayMs` (or until `maxRecords` are queued) and writes the batch as ONE
912
912
  `insert … select … from jsonb_to_recordset($1)`; each `write()` resolves or rejects with its own
913
913
  batch, so a failed statement is `X_AUDIT_SINK_FAILED` (or `X_QUERY_AUDIT_SINK_FAILED`) on every
914
914
  call it carried — none succeeds silently. The queue flushes on drain, in the `inflight` phase,
915
- before the pool closes. A sink of your own with a `writeMany(records)` gets the same through
916
- `batchedAuditSink(sink, { maxRecords, maxDelayMs })` from `@ultimat3/action/adapter`.
915
+ before the pool closes. A batch carries other requests' records, so it is sent **outside any
916
+ transaction**: one caller's rollback never takes the rest of its batch with it, and a record is
917
+ kept whatever its own transaction does — an audit row records the attempt. Unbatched, a write
918
+ joins the caller's open transaction, as it always has. A sink of your own with a
919
+ `writeMany(records)` gets the batching through `batchedAuditSink(sink, { maxRecords, maxDelayMs })`
920
+ from `@ultimat3/action/adapter` — and sends outside the ambient transaction itself.
917
921
 
918
922
  `x_audit` carries the framework's own facts as columns — the action, the surface, the outcome,
919
923
  the actor, the correlation ids, the idempotency key — and the parsed `input` as `jsonb`, redacted
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/action",
3
- "version": "31.22.0",
3
+ "version": "31.22.3",
4
4
  "description": "The action primitive: one declaration projected to route, OpenAPI, client, MCP tool, job handle, tests",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -48,13 +48,13 @@
48
48
  "test": "bun test"
49
49
  },
50
50
  "dependencies": {
51
- "@ultimat3/cache": "31.22.0",
52
- "@ultimat3/core": "31.22.0",
53
- "@ultimat3/db": "31.22.0",
54
- "@ultimat3/entity": "31.22.0",
55
- "@ultimat3/http": "31.22.0",
56
- "@ultimat3/policy": "31.22.0",
57
- "@ultimat3/schema": "31.22.0",
58
- "@ultimat3/storage": "31.22.0"
51
+ "@ultimat3/cache": "31.22.3",
52
+ "@ultimat3/core": "31.22.3",
53
+ "@ultimat3/db": "31.22.3",
54
+ "@ultimat3/entity": "31.22.3",
55
+ "@ultimat3/http": "31.22.3",
56
+ "@ultimat3/policy": "31.22.3",
57
+ "@ultimat3/schema": "31.22.3",
58
+ "@ultimat3/storage": "31.22.3"
59
59
  }
60
60
  }
@@ -86,6 +86,10 @@ const single =
86
86
  * **Flushes on drain**: an `inflight`-phase shutdown hook, which runs after in-flight requests end
87
87
  * and before the `close` phase shuts the pool the sink writes through. `postgresAuditSink({
88
88
  * groupCommit: true })` is this over the Postgres sink; a custom sink with a `writeMany` wraps here.
89
+ *
90
+ * A batch is sent in the async context of the write that armed it — its timer's, or its own when
91
+ * the batch fills — so a sink that reads ambient state (a transaction) must send outside it, as
92
+ * `postgresAuditSink` does: otherwise every record rides the arming caller's transaction.
89
93
  */
90
94
  export function batchedAuditSink(
91
95
  sink: BatchAuditSink,
@@ -9,6 +9,7 @@ import type { AuditRecord } from './audit';
9
9
  import type { BatchAuditSink, BatchedAuditSink, BatchedAuditSinkOptions } from './audit-batch';
10
10
  import { batchedAuditSink } from './audit-batch';
11
11
  import { rowOf } from './audit-postgres-row';
12
+ import { outsideTransactions } from './tx-scope';
12
13
 
13
14
  /**
14
15
  * Applied by the boot, never by an app migration — the rule `SQL_IDEMPOTENCY_TABLE` follows, and
@@ -125,6 +126,12 @@ export interface PostgresAuditSinkOptions {
125
126
  * Group commit: concurrent writes share one `SQL_AUDIT_INSERT_MANY`, and each `write()` still
126
127
  * settles with its own batch (`batchedAuditSink`). `true` takes the defaults; an object sets
127
128
  * `maxRecords` / `maxDelayMs`. Absent: one insert per record, as before.
129
+ *
130
+ * **A batch is sent outside any transaction** (`outsideTransactions`, in `tx-scope.ts`), so
131
+ * every record in it is stored even when the transaction of the write that armed it rolls back.
132
+ * Without that, the timer inherited the arming write's async context, and `dbExecutor()` sent
133
+ * OTHER callers' records on that transaction's connection, to be rolled back with it.
134
+ * Unbatched, a write joins the caller's transaction, as it always has.
128
135
  */
129
136
  readonly groupCommit?: boolean | BatchedAuditSinkOptions | undefined;
130
137
  }
@@ -162,7 +169,9 @@ export function postgresAuditSink(options: PostgresAuditSinkOptions): PostgresAu
162
169
  export function postgresAuditSink(
163
170
  options: PostgresAuditSinkOptions,
164
171
  ): PostgresAuditSink | BatchedAuditSink {
165
- const exec = options.executor;
172
+ const group = options.groupCommit;
173
+ const grouped = group !== undefined && group !== false;
174
+ const exec = grouped ? outsideTransactions(options.executor) : options.executor;
166
175
  const sink: PostgresAuditSink = {
167
176
  async write(record: AuditRecord): Promise<void> {
168
177
  await exec.query(SQL_AUDIT_INSERT, Object.values(rowOf(record)));
@@ -172,7 +181,6 @@ export function postgresAuditSink(
172
181
  await exec.query(SQL_AUDIT_INSERT_MANY, [JSON.stringify(records.map(rowOf))]);
173
182
  },
174
183
  };
175
- const group = options.groupCommit;
176
- if (group === undefined || group === false) return sink;
184
+ if (!grouped) return sink;
177
185
  return batchedAuditSink(sink, group === true ? {} : group);
178
186
  }
package/src/tx-scope.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  /**
2
2
  * The open database transaction, as this package needs it — **the only** file here that imports
3
- * `@ultimat3/db` (`tx-scope.test.ts` holds that). When is a write durable (`openCommitScope`, the
4
- * cache bust), and is a transaction really OPEN, on which client (`liveTransaction`, idempotency).
5
- * And a scope whose writes are never kept (`inRolledBackScope`, the contract step's exercise).
3
+ * `@ultimat3/db` (`tx-scope.test.ts`): when a write is durable, whether a transaction is OPEN and on
4
+ * which client, a scope whose writes are never kept, and an executor that never joins one.
6
5
  */
7
6
 
8
7
  // Type-only: `idempotency-postgres.ts` imports this module, and a runtime edge would be a cycle.
@@ -13,6 +12,7 @@ import {
13
12
  currentTx,
14
13
  dbClientInstalled,
15
14
  liveTxConnection,
15
+ outsideTransaction,
16
16
  withTransaction,
17
17
  } from '@ultimat3/db';
18
18
  import { memoryJournal, memoryTransactor } from '@ultimat3/entity';
@@ -20,6 +20,15 @@ import { memoryJournal, memoryTransactor } from '@ultimat3/entity';
20
20
  /** The mark a keyed run puts on the open root: its COMMIT carries the settle, abort or no abort. */
21
21
  export const carrySettleOnCommit = (): void => completeTransaction();
22
22
 
23
+ /**
24
+ * `executor` with every statement sent outside any transaction on the caller's async context, so
25
+ * `dbExecutor()` reads the pool. For a statement whose rows belong to MORE than its caller — a
26
+ * group-committed audit batch carries other requests' records — and must not roll back with it.
27
+ */
28
+ export const outsideTransactions = (executor: PgExecutor): PgExecutor => ({
29
+ query: (text, values) => outsideTransaction(() => executor.query(text, values)),
30
+ });
31
+
23
32
  /** The half of a transaction a deferred effect needs. `@ultimat3/db`'s `DbTx` satisfies it. */
24
33
  export interface CommitScope {
25
34
  /** Fired once the ROOT transaction has committed; dropped on rollback or a lost COMMIT. */