@@ -77,11 +77,7 @@ interface UsageEntry {
7777 metadata ?: UsageLogMetadata
7878}
7979
80- /**
81- * Parameters for the central recordUsage function.
82- * This is the single entry point for all billing mutations.
83- */
84- export interface RecordUsageParams {
80+ interface RecordUsageBaseParams {
8581 /** The user being charged */
8682 userId : string
8783 /** One or more usage_log entries to record. Total cost is derived from these. */
@@ -92,19 +88,37 @@ export interface RecordUsageParams {
9288 workflowId ?: string
9389 /** Execution context */
9490 executionId ?: string
95- /** Billing entity scope, resolved by caller when already known. */
96- billingEntity ?: BillingEntity
97- /** Billing period bounds, resolved by caller when already known. */
98- billingPeriod ?: { start : Date ; end : Date }
99- /**
100- * Optional transaction to run the ledger INSERT in. Callers that reconcile a
101- * read-then-insert under a lock (e.g. the per-execution advisory lock in the
102- * workflow completion path) pass their tx so the insert participates in the
103- * same locked transaction. Defaults to the pooled db.
104- */
105- tx ?: DbOrTx
10691}
10792
93+ /**
94+ * Parameters for the central recordUsage function.
95+ * This is the single entry point for all billing mutations.
96+ *
97+ * Callers that pass `tx` (e.g. the per-execution advisory-lock reconciliation
98+ * in the workflow completion path) must pre-resolve the billing context before
99+ * opening the transaction: resolving it inside would run the subscription
100+ * lookups on the global pool while the tx already holds a pooled connection,
101+ * starving the pool under load (see recordCumulativeUsage for the history).
102+ */
103+ export type RecordUsageParams = RecordUsageBaseParams &
104+ (
105+ | {
106+ /** Transaction the ledger INSERT participates in. */
107+ tx : DbOrTx
108+ /** Billing entity scope, resolved before the transaction opened. */
109+ billingEntity : BillingEntity
110+ /** Billing period bounds, resolved before the transaction opened. */
111+ billingPeriod : { start : Date ; end : Date }
112+ }
113+ | {
114+ tx ?: undefined
115+ /** Billing entity scope, resolved by caller when already known. */
116+ billingEntity ?: BillingEntity
117+ /** Billing period bounds, resolved by caller when already known. */
118+ billingPeriod ?: { start : Date ; end : Date }
119+ }
120+ )
121+
108122export function stableEventKey ( parts : Record < string , unknown > ) : string {
109123 const payload = Object . keys ( parts )
110124 . sort ( )
0 commit comments