@hotmeshio/hotmesh 0.26.1 → 0.26.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/build/package.json +1 -1
- package/build/services/escalations/client.d.ts +36 -1
- package/build/services/escalations/client.js +59 -8
- package/build/services/escalations/index.d.ts +1 -0
- package/build/services/escalations/index.js +3 -1
- package/build/services/store/providers/postgres/postgres.d.ts +7 -0
- package/build/services/store/providers/postgres/postgres.js +65 -3
- package/build/types/hmsh_escalations.d.ts +91 -1
- package/build/types/index.d.ts +1 -1
- package/package.json +1 -1
package/build/package.json
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { HotMesh } from '../hotmesh';
|
|
2
2
|
import { EventsConfig } from '../../types/system_events';
|
|
3
3
|
import { Connection } from '../../types/durable';
|
|
4
|
-
import { EscalationEntry, ClaimEscalationResult, ClaimByMetadataResult, ReleaseEscalationResult, ResolveEscalationResult, CancelEscalationResult, ListEscalationsParams, StatsEscalationsParams, EscalationStats, CreateEscalationParams, UpdateEscalationParams, AppendMilestonesParams, ClaimEscalationParams, ClaimByMetadataParams, ReleaseEscalationParams, ResolveEscalationParams, ResolveByMetadataParams, EscalateToRoleParams, MigrateEscalationParams, ClaimManyParams, EscalateManyToRoleParams, UpdateManyPriorityParams, ResolveManyParams, ResolveAllOrNoneParams, ResolveAllOrNoneResult, PruneEscalationsParams, PruneEscalationsResult } from '../../types/hmsh_escalations';
|
|
4
|
+
import { EscalationEntry, ClaimEscalationResult, ClaimByMetadataResult, ReleaseEscalationResult, ResolveEscalationResult, CancelEscalationResult, ListEscalationsParams, StatsEscalationsParams, EscalationStats, CreateEscalationParams, UpdateEscalationParams, AppendMilestonesParams, ClaimEscalationParams, ClaimByMetadataParams, ReleaseEscalationParams, ResolveEscalationParams, ResolveByMetadataParams, EscalateToRoleParams, MigrateEscalationParams, ClaimManyParams, EscalateManyToRoleParams, UpdateManyPriorityParams, ResolveManyParams, ResolveAllOrNoneParams, ResolveAllOrNoneResult, PruneEscalationsParams, PruneEscalationsResult, ClaimManyByQueryParams } from '../../types/hmsh_escalations';
|
|
5
|
+
/**
|
|
6
|
+
* Reserved signal-payload key carrying resolution provenance
|
|
7
|
+
* (`EscalationResolution`) to the waiting workflow. The `$` prefix marks the
|
|
8
|
+
* control-key namespace — consumer payload fields never collide with it.
|
|
9
|
+
*/
|
|
10
|
+
export declare const ESCALATION_RESOLUTION_KEY = "$resolution";
|
|
5
11
|
export type GetHotMeshFn = (topic: string | null, namespace?: string) => Promise<HotMesh>;
|
|
6
12
|
export interface EscalationClientConfig {
|
|
7
13
|
/** Postgres connection options — used when creating a standalone EscalationClient. */
|
|
@@ -71,6 +77,17 @@ export declare class EscalationClientService {
|
|
|
71
77
|
private _emitMany;
|
|
72
78
|
private _makeEngineFactory;
|
|
73
79
|
private _hashConnection;
|
|
80
|
+
/**
|
|
81
|
+
* Composes the signal `data` delivered to the waiting workflow. When the
|
|
82
|
+
* resolving caller supplies `resolvedBy`, resolution provenance (escalation
|
|
83
|
+
* id + resolver identity) rides under the reserved `$resolution` key —
|
|
84
|
+
* making the waiter's payload contract deterministic per call site. Without
|
|
85
|
+
* `resolvedBy` the payload passes through byte-identical, so callers that
|
|
86
|
+
* exact-match on payloads are unaffected. The enrichment rides the SIGNAL
|
|
87
|
+
* only — the stored `resolver_payload` column receives the caller's payload
|
|
88
|
+
* untouched.
|
|
89
|
+
*/
|
|
90
|
+
private _signalData;
|
|
74
91
|
private _deliverEscalationSignal;
|
|
75
92
|
/**
|
|
76
93
|
* Builds the wake as a webhook message so the store can commit it
|
|
@@ -171,6 +188,12 @@ export declare class EscalationClientService {
|
|
|
171
188
|
* making it `@>`-queryable alongside the creation metadata ("what was
|
|
172
189
|
* intended"). This is distinct from `resolverPayload`, which is delivered to
|
|
173
190
|
* the waiting workflow as `condition()`'s return value and is not GIN-indexed.
|
|
191
|
+
*
|
|
192
|
+
* Pass `params.assertClaim` to additionally require — inside the same guarded
|
|
193
|
+
* UPDATE — that no active claim lock stands against that assignee. Blocks
|
|
194
|
+
* with `claim-expired` (own claim window lapsed) or `claimed-by-other` (a
|
|
195
|
+
* live window held by someone else); unclaimed rows and durable
|
|
196
|
+
* pre-assignments (no window) resolve normally.
|
|
174
197
|
*/
|
|
175
198
|
resolve(params: ResolveEscalationParams, namespace?: string): Promise<ResolveEscalationResult>;
|
|
176
199
|
/**
|
|
@@ -201,6 +224,18 @@ export declare class EscalationClientService {
|
|
|
201
224
|
claimed: number;
|
|
202
225
|
skipped: number;
|
|
203
226
|
}>;
|
|
227
|
+
/**
|
|
228
|
+
* Atomic query-form bulk claim: one UPDATE selects and claims every pending,
|
|
229
|
+
* claimable row matching the selector (role/type/priority equality, metadata
|
|
230
|
+
* `@>` containment). Prefer this over `list()` + `claimMany({ids})` when the
|
|
231
|
+
* population is describable by filter — a row that re-parks between a search
|
|
232
|
+
* and an ids-claim is invisible to the ids form but claimed by this one.
|
|
233
|
+
* Returns the claimed rows.
|
|
234
|
+
*/
|
|
235
|
+
claimManyByQuery(params: ClaimManyByQueryParams): Promise<{
|
|
236
|
+
claimed: number;
|
|
237
|
+
entries: EscalationEntry[];
|
|
238
|
+
}>;
|
|
204
239
|
/**
|
|
205
240
|
* Bulk-reassigns pending escalations to a new role, clearing any current claim.
|
|
206
241
|
* Returns the count of rows updated.
|
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EscalationClientService = void 0;
|
|
3
|
+
exports.EscalationClientService = exports.ESCALATION_RESOLUTION_KEY = void 0;
|
|
4
4
|
const enums_1 = require("../../modules/enums");
|
|
5
5
|
const utils_1 = require("../../modules/utils");
|
|
6
6
|
const hotmesh_1 = require("../hotmesh");
|
|
7
7
|
const types_1 = require("../../types");
|
|
8
8
|
const stream_1 = require("../../types/stream");
|
|
9
9
|
const factory_1 = require("../durable/schemas/factory");
|
|
10
|
+
/**
|
|
11
|
+
* Reserved signal-payload key carrying resolution provenance
|
|
12
|
+
* (`EscalationResolution`) to the waiting workflow. The `$` prefix marks the
|
|
13
|
+
* control-key namespace — consumer payload fields never collide with it.
|
|
14
|
+
*/
|
|
15
|
+
exports.ESCALATION_RESOLUTION_KEY = '$resolution';
|
|
10
16
|
/**
|
|
11
17
|
* Standalone client for the `public.hmsh_escalations` signal-pause surface.
|
|
12
18
|
*
|
|
@@ -122,6 +128,28 @@ class EscalationClientService {
|
|
|
122
128
|
return parts.join('');
|
|
123
129
|
}
|
|
124
130
|
// ─── Signal delivery ───────────────────────────────────────────────────────
|
|
131
|
+
/**
|
|
132
|
+
* Composes the signal `data` delivered to the waiting workflow. When the
|
|
133
|
+
* resolving caller supplies `resolvedBy`, resolution provenance (escalation
|
|
134
|
+
* id + resolver identity) rides under the reserved `$resolution` key —
|
|
135
|
+
* making the waiter's payload contract deterministic per call site. Without
|
|
136
|
+
* `resolvedBy` the payload passes through byte-identical, so callers that
|
|
137
|
+
* exact-match on payloads are unaffected. The enrichment rides the SIGNAL
|
|
138
|
+
* only — the stored `resolver_payload` column receives the caller's payload
|
|
139
|
+
* untouched.
|
|
140
|
+
*/
|
|
141
|
+
_signalData(payload, escalationId, resolvedBy) {
|
|
142
|
+
if (!resolvedBy)
|
|
143
|
+
return payload ?? {};
|
|
144
|
+
return {
|
|
145
|
+
...(payload ?? {}),
|
|
146
|
+
[exports.ESCALATION_RESOLUTION_KEY]: {
|
|
147
|
+
escalationId,
|
|
148
|
+
resolvedBy: resolvedBy.id,
|
|
149
|
+
...(resolvedBy.email ? { resolvedByEmail: resolvedBy.email } : {}),
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
125
153
|
async _deliverEscalationSignal(ns, topic, signalPayload) {
|
|
126
154
|
if (topic) {
|
|
127
155
|
try {
|
|
@@ -349,6 +377,12 @@ class EscalationClientService {
|
|
|
349
377
|
* making it `@>`-queryable alongside the creation metadata ("what was
|
|
350
378
|
* intended"). This is distinct from `resolverPayload`, which is delivered to
|
|
351
379
|
* the waiting workflow as `condition()`'s return value and is not GIN-indexed.
|
|
380
|
+
*
|
|
381
|
+
* Pass `params.assertClaim` to additionally require — inside the same guarded
|
|
382
|
+
* UPDATE — that no active claim lock stands against that assignee. Blocks
|
|
383
|
+
* with `claim-expired` (own claim window lapsed) or `claimed-by-other` (a
|
|
384
|
+
* live window held by someone else); unclaimed rows and durable
|
|
385
|
+
* pre-assignments (no window) resolve normally.
|
|
352
386
|
*/
|
|
353
387
|
async resolve(params, namespace) {
|
|
354
388
|
const ns = (params.namespace ?? namespace) ?? factory_1.APP_ID;
|
|
@@ -359,9 +393,9 @@ class EscalationClientService {
|
|
|
359
393
|
let wakeCommand = null;
|
|
360
394
|
const preview = await store.getEscalation(params.id, params.namespace);
|
|
361
395
|
if (preview?.signal_key) {
|
|
362
|
-
wakeCommand = await this._buildWakeCommand(ns, preview.topic, preview.signal_key, params.resolverPayload
|
|
396
|
+
wakeCommand = await this._buildWakeCommand(ns, preview.topic, preview.signal_key, this._signalData(params.resolverPayload, params.id, params.resolvedBy));
|
|
363
397
|
}
|
|
364
|
-
const dbResult = await store.resolveEscalation({ id: params.id, resolverPayload: params.resolverPayload, metadata: params.metadata }, wakeCommand ?? undefined);
|
|
398
|
+
const dbResult = await store.resolveEscalation({ id: params.id, resolverPayload: params.resolverPayload, metadata: params.metadata, assertClaim: params.assertClaim }, wakeCommand ?? undefined);
|
|
365
399
|
if (!dbResult.ok)
|
|
366
400
|
return dbResult;
|
|
367
401
|
if (dbResult.signalKey && !dbResult.wakeEnqueued) {
|
|
@@ -369,7 +403,7 @@ class EscalationClientService {
|
|
|
369
403
|
//enqueue was rolled back to its savepoint) — deliver post-commit
|
|
370
404
|
await this._deliverEscalationSignal(ns, dbResult.topic, {
|
|
371
405
|
id: dbResult.signalKey,
|
|
372
|
-
data: params.resolverPayload
|
|
406
|
+
data: this._signalData(params.resolverPayload, params.id, params.resolvedBy),
|
|
373
407
|
});
|
|
374
408
|
}
|
|
375
409
|
this._emit('resolved', dbResult.entry);
|
|
@@ -397,7 +431,10 @@ class EscalationClientService {
|
|
|
397
431
|
namespace: params.namespace,
|
|
398
432
|
});
|
|
399
433
|
if (preview?.signalKey) {
|
|
400
|
-
|
|
434
|
+
//the wake embeds the previewed row's id; forSignalKey pins it to that
|
|
435
|
+
//row, so a different row winning the race falls through to post-commit
|
|
436
|
+
//delivery where the winner's own id is used
|
|
437
|
+
wakeCommand = await this._buildWakeCommand(ns, preview.topic, preview.signalKey, this._signalData(params.resolverPayload, preview.id, params.resolvedBy));
|
|
401
438
|
}
|
|
402
439
|
const dbResult = await store.resolveEscalationByMetadata({ key: params.key, value: params.value, resolverPayload: params.resolverPayload, roles: params.roles, metadata: params.metadata }, wakeCommand ?? undefined);
|
|
403
440
|
if (!dbResult.ok)
|
|
@@ -405,7 +442,7 @@ class EscalationClientService {
|
|
|
405
442
|
if (dbResult.signalKey && !dbResult.wakeEnqueued) {
|
|
406
443
|
await this._deliverEscalationSignal(ns, dbResult.topic, {
|
|
407
444
|
id: dbResult.signalKey,
|
|
408
|
-
data: params.resolverPayload
|
|
445
|
+
data: this._signalData(params.resolverPayload, dbResult.entry.id, params.resolvedBy),
|
|
409
446
|
});
|
|
410
447
|
}
|
|
411
448
|
this._emit('resolved', dbResult.entry);
|
|
@@ -440,6 +477,20 @@ class EscalationClientService {
|
|
|
440
477
|
this._emitMany('claimed', entries);
|
|
441
478
|
return { claimed: entries.length, skipped };
|
|
442
479
|
}
|
|
480
|
+
/**
|
|
481
|
+
* Atomic query-form bulk claim: one UPDATE selects and claims every pending,
|
|
482
|
+
* claimable row matching the selector (role/type/priority equality, metadata
|
|
483
|
+
* `@>` containment). Prefer this over `list()` + `claimMany({ids})` when the
|
|
484
|
+
* population is describable by filter — a row that re-parks between a search
|
|
485
|
+
* and an ids-claim is invisible to the ids form but claimed by this one.
|
|
486
|
+
* Returns the claimed rows.
|
|
487
|
+
*/
|
|
488
|
+
async claimManyByQuery(params) {
|
|
489
|
+
const hm = await this._engine(null, params.namespace);
|
|
490
|
+
const entries = await hm.engine.store.claimManyEscalationsByQuery(params);
|
|
491
|
+
this._emitMany('claimed', entries);
|
|
492
|
+
return { claimed: entries.length, entries };
|
|
493
|
+
}
|
|
443
494
|
/**
|
|
444
495
|
* Bulk-reassigns pending escalations to a new role, clearing any current claim.
|
|
445
496
|
* Returns the count of rows updated.
|
|
@@ -518,7 +569,7 @@ class EscalationClientService {
|
|
|
518
569
|
for (const row of previews) {
|
|
519
570
|
if (!row.signal_key)
|
|
520
571
|
continue;
|
|
521
|
-
const cmd = await this._buildWakeCommand(ns, row.topic, row.signal_key, payloadById.get(row.id)
|
|
572
|
+
const cmd = await this._buildWakeCommand(ns, row.topic, row.signal_key, this._signalData(payloadById.get(row.id), row.id, params.resolvedBy));
|
|
522
573
|
if (cmd)
|
|
523
574
|
wakeCommands.push(cmd);
|
|
524
575
|
}
|
|
@@ -537,7 +588,7 @@ class EscalationClientService {
|
|
|
537
588
|
if (entry.signal_key && !enqueuedKeys.has(entry.signal_key)) {
|
|
538
589
|
await this._deliverEscalationSignal(ns, entry.topic, {
|
|
539
590
|
id: entry.signal_key,
|
|
540
|
-
data: payloadById.get(entry.id)
|
|
591
|
+
data: this._signalData(payloadById.get(entry.id), entry.id, params.resolvedBy),
|
|
541
592
|
});
|
|
542
593
|
}
|
|
543
594
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EscalationClientService = exports.Escalations = void 0;
|
|
3
|
+
exports.ESCALATION_RESOLUTION_KEY = exports.EscalationClientService = exports.Escalations = void 0;
|
|
4
4
|
const utils_1 = require("../../modules/utils");
|
|
5
5
|
const client_1 = require("./client");
|
|
6
6
|
Object.defineProperty(exports, "EscalationClientService", { enumerable: true, get: function () { return client_1.EscalationClientService; } });
|
|
@@ -69,3 +69,5 @@ EscalationsClass.guid = utils_1.guid;
|
|
|
69
69
|
* when calling `client.migrate()` or any direct UUID column insert.
|
|
70
70
|
*/
|
|
71
71
|
EscalationsClass.uuid = utils_1.uuid;
|
|
72
|
+
var client_2 = require("./client");
|
|
73
|
+
Object.defineProperty(exports, "ESCALATION_RESOLUTION_KEY", { enumerable: true, get: function () { return client_2.ESCALATION_RESOLUTION_KEY; } });
|
|
@@ -325,6 +325,13 @@ declare class PostgresStoreService extends StoreService<ProviderClient, Provider
|
|
|
325
325
|
entries: import('../../../../types/hmsh_escalations').EscalationEntry[];
|
|
326
326
|
skipped: number;
|
|
327
327
|
}>;
|
|
328
|
+
/**
|
|
329
|
+
* Atomic query-form bulk claim: the selector composes into the UPDATE's own
|
|
330
|
+
* WHERE via `_escalationFilterConditions`, so selection and claim are one
|
|
331
|
+
* statement — no SELECT-then-claim window. `status='pending'` and the
|
|
332
|
+
* claimability guard are forced regardless of the selector.
|
|
333
|
+
*/
|
|
334
|
+
claimManyEscalationsByQuery(params: import('../../../../types/hmsh_escalations').ClaimManyByQueryParams): Promise<import('../../../../types/hmsh_escalations').EscalationEntry[]>;
|
|
328
335
|
escalateManyEscalationsToRole(params: import('../../../../types/hmsh_escalations').EscalateManyToRoleParams): Promise<number>;
|
|
329
336
|
updateManyEscalationsPriority(params: import('../../../../types/hmsh_escalations').UpdateManyPriorityParams): Promise<number>;
|
|
330
337
|
resolveManyEscalations(params: import('../../../../types/hmsh_escalations').ResolveManyParams): Promise<import('../../../../types/hmsh_escalations').EscalationEntry[]>;
|
|
@@ -1889,7 +1889,7 @@ class PostgresStoreService extends __1.StoreService {
|
|
|
1889
1889
|
};
|
|
1890
1890
|
}
|
|
1891
1891
|
async resolveEscalation(params, wakeCommand) {
|
|
1892
|
-
const { id, namespace, resolverPayload, metadata } = params;
|
|
1892
|
+
const { id, namespace, resolverPayload, metadata, assertClaim } = params;
|
|
1893
1893
|
const payloadJson = resolverPayload ? JSON.stringify(resolverPayload) : null;
|
|
1894
1894
|
const metaJson = metadata ? JSON.stringify(metadata) : null;
|
|
1895
1895
|
//single statement: FOR UPDATE lock, TOCTOU-guarded UPDATE, and the
|
|
@@ -1901,10 +1901,20 @@ class PostgresStoreService extends __1.StoreService {
|
|
|
1901
1901
|
const sqlParams = namespace
|
|
1902
1902
|
? [id, payloadJson, metaJson, namespace]
|
|
1903
1903
|
: [id, payloadJson, metaJson];
|
|
1904
|
+
//claim assertion rides the same guarded UPDATE: when $claimIdx is NULL
|
|
1905
|
+
//the predicate collapses to TRUE (today's semantics). A claim is a work
|
|
1906
|
+
//LOCK that exists only while a TTL window (assigned_until) is active —
|
|
1907
|
+
//blocked states are exactly: a live window held by a different assignee,
|
|
1908
|
+
//and the asserting assignee's own lapsed window (stale work). Unclaimed
|
|
1909
|
+
//rows, durable pre-assignments (assigned_to with no window), and rows
|
|
1910
|
+
//whose window lapsed under a different assignee all resolve normally.
|
|
1911
|
+
sqlParams.push(assertClaim ?? null);
|
|
1912
|
+
const claimIdx = sqlParams.length;
|
|
1904
1913
|
const { wakeCTE, wakeCount } = this.composeEscalationWakeCTE(wakeCommand, sqlParams, 'resolved');
|
|
1905
1914
|
const result = await this.pgClient.query(`
|
|
1906
1915
|
WITH target AS MATERIALIZED (
|
|
1907
|
-
SELECT id, signal_key, topic, status
|
|
1916
|
+
SELECT id, signal_key, topic, status, assigned_to, assigned_until
|
|
1917
|
+
FROM public.hmsh_escalations
|
|
1908
1918
|
WHERE id = $1 ${namespace ? 'AND namespace = $4' : ''}
|
|
1909
1919
|
LIMIT 1 FOR UPDATE
|
|
1910
1920
|
),
|
|
@@ -1917,9 +1927,14 @@ class PostgresStoreService extends __1.StoreService {
|
|
|
1917
1927
|
updated_at = NOW()
|
|
1918
1928
|
FROM target
|
|
1919
1929
|
WHERE e.id = target.id AND target.status = 'pending'
|
|
1930
|
+
AND ($${claimIdx}::text IS NULL
|
|
1931
|
+
OR target.assigned_to IS NULL
|
|
1932
|
+
OR target.assigned_until IS NULL
|
|
1933
|
+
OR (target.assigned_to = $${claimIdx}::text AND target.assigned_until > NOW())
|
|
1934
|
+
OR (target.assigned_to <> $${claimIdx}::text AND target.assigned_until <= NOW()))
|
|
1920
1935
|
RETURNING e.*
|
|
1921
1936
|
)${wakeCTE}
|
|
1922
|
-
SELECT t.id, t.status AS prior_status, t.signal_key, t.topic,
|
|
1937
|
+
SELECT t.id, t.status AS prior_status, t.signal_key, t.topic, t.assigned_to,
|
|
1923
1938
|
CASE
|
|
1924
1939
|
WHEN r.id IS NOT NULL THEN 'resolved'
|
|
1925
1940
|
WHEN t.id IS NULL THEN 'not-found'
|
|
@@ -1943,6 +1958,15 @@ class PostgresStoreService extends __1.StoreService {
|
|
|
1943
1958
|
// resolution landed.
|
|
1944
1959
|
return { ok: false, reason: 'already-expired' };
|
|
1945
1960
|
}
|
|
1961
|
+
if (row.prior_status === 'pending') {
|
|
1962
|
+
// Still pending, yet blocked — only the claim assertion can do that.
|
|
1963
|
+
// Same-assignee means their claim lapsed; anything else means the
|
|
1964
|
+
// row is (or was last) held by a different principal.
|
|
1965
|
+
return {
|
|
1966
|
+
ok: false,
|
|
1967
|
+
reason: row.assigned_to === assertClaim ? 'claim-expired' : 'claimed-by-other',
|
|
1968
|
+
};
|
|
1969
|
+
}
|
|
1946
1970
|
return { ok: false, reason: 'already-resolved' };
|
|
1947
1971
|
}
|
|
1948
1972
|
return {
|
|
@@ -2179,6 +2203,44 @@ class PostgresStoreService extends __1.StoreService {
|
|
|
2179
2203
|
const entries = result.rows;
|
|
2180
2204
|
return { entries, skipped: ids.length - entries.length };
|
|
2181
2205
|
}
|
|
2206
|
+
/**
|
|
2207
|
+
* Atomic query-form bulk claim: the selector composes into the UPDATE's own
|
|
2208
|
+
* WHERE via `_escalationFilterConditions`, so selection and claim are one
|
|
2209
|
+
* statement — no SELECT-then-claim window. `status='pending'` and the
|
|
2210
|
+
* claimability guard are forced regardless of the selector.
|
|
2211
|
+
*/
|
|
2212
|
+
async claimManyEscalationsByQuery(params) {
|
|
2213
|
+
const { query, namespace, assignee, durationMinutes = 1 } = params;
|
|
2214
|
+
const selectorIsEmpty = !query.role && !query.roles?.length && !query.type && !query.subtype &&
|
|
2215
|
+
!query.entity && query.priority === undefined &&
|
|
2216
|
+
!(query.metadata && Object.keys(query.metadata).length);
|
|
2217
|
+
if (selectorIsEmpty) {
|
|
2218
|
+
throw new Error('claimManyEscalationsByQuery: empty selector — an unbounded bulk claim is not allowed');
|
|
2219
|
+
}
|
|
2220
|
+
const { conditions, values, idx } = this._escalationFilterConditions({
|
|
2221
|
+
namespace,
|
|
2222
|
+
role: query.role,
|
|
2223
|
+
roles: query.roles,
|
|
2224
|
+
type: query.type,
|
|
2225
|
+
subtype: query.subtype,
|
|
2226
|
+
entity: query.entity,
|
|
2227
|
+
priority: query.priority,
|
|
2228
|
+
metadata: query.metadata,
|
|
2229
|
+
}, 1);
|
|
2230
|
+
const assigneeIdx = idx;
|
|
2231
|
+
const durationIdx = idx + 1;
|
|
2232
|
+
const result = await this.pgClient.query(`UPDATE public.hmsh_escalations
|
|
2233
|
+
SET assigned_to = $${assigneeIdx},
|
|
2234
|
+
claimed_at = NOW(),
|
|
2235
|
+
claim_expires_at = NOW() + ($${durationIdx} * INTERVAL '1 minute'),
|
|
2236
|
+
assigned_until = NOW() + ($${durationIdx} * INTERVAL '1 minute'),
|
|
2237
|
+
updated_at = NOW()
|
|
2238
|
+
WHERE ${conditions.join(' AND ')}
|
|
2239
|
+
AND status = 'pending'
|
|
2240
|
+
AND (assigned_to IS NULL OR assigned_until IS NULL OR assigned_until <= NOW() OR assigned_to = $${assigneeIdx})
|
|
2241
|
+
RETURNING *`, [...values, assignee, durationMinutes]);
|
|
2242
|
+
return result.rows;
|
|
2243
|
+
}
|
|
2182
2244
|
async escalateManyEscalationsToRole(params) {
|
|
2183
2245
|
const { ids, namespace, targetRole } = params;
|
|
2184
2246
|
const result = await this.pgClient.query(`UPDATE public.hmsh_escalations
|
|
@@ -103,7 +103,7 @@ export type ResolveEscalationResult = {
|
|
|
103
103
|
entry: EscalationEntry;
|
|
104
104
|
} | {
|
|
105
105
|
ok: false;
|
|
106
|
-
reason: 'not-found' | 'already-resolved' | 'already-cancelled' | 'already-expired';
|
|
106
|
+
reason: 'not-found' | 'already-resolved' | 'already-cancelled' | 'already-expired' | 'claim-expired' | 'claimed-by-other';
|
|
107
107
|
};
|
|
108
108
|
/**
|
|
109
109
|
* A pre-built wake message, committed INSIDE the resolve/cancel transaction
|
|
@@ -117,6 +117,43 @@ export interface EscalationWakeCommand {
|
|
|
117
117
|
forSignalKey: string;
|
|
118
118
|
message: string;
|
|
119
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Resolution provenance delivered to the waiting workflow alongside the
|
|
122
|
+
* resolver payload, under the reserved `$resolution` key. Present exactly when
|
|
123
|
+
* the resolving caller supplied `resolvedBy` — without it the signal payload
|
|
124
|
+
* passes through byte-identical, so existing waiters are unaffected. The
|
|
125
|
+
* `$`-prefixed key namespace is reserved for control data riding the signal
|
|
126
|
+
* (like `$escalation_id` on the legacy routing path); consumer payload fields
|
|
127
|
+
* never collide with it. The stored `resolver_payload` row column stays clean —
|
|
128
|
+
* provenance rides the signal only.
|
|
129
|
+
*
|
|
130
|
+
* A waiting workflow declares the key in its `condition()` payload generic:
|
|
131
|
+
*
|
|
132
|
+
* ```typescript
|
|
133
|
+
* const decision = await Durable.workflow.condition<{
|
|
134
|
+
* approved: boolean;
|
|
135
|
+
* $resolution?: EscalationResolution;
|
|
136
|
+
* }>(signalId, config);
|
|
137
|
+
* decision.$resolution?.resolvedBy; // who resolved it
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
export interface EscalationResolution {
|
|
141
|
+
/** The id of the escalation row whose resolve delivered this signal. */
|
|
142
|
+
escalationId: string;
|
|
143
|
+
/** Resolver's user id. */
|
|
144
|
+
resolvedBy: string;
|
|
145
|
+
/** Resolver's email — present when supplied alongside the id. */
|
|
146
|
+
resolvedByEmail?: string;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Resolver identity, supplied by the resolving caller (the API layer that
|
|
150
|
+
* authenticated the human or agent). Delivered to the waiting workflow inside
|
|
151
|
+
* `$resolution`; never written to the stored `resolver_payload`.
|
|
152
|
+
*/
|
|
153
|
+
export interface ResolvedByIdentity {
|
|
154
|
+
id: string;
|
|
155
|
+
email?: string;
|
|
156
|
+
}
|
|
120
157
|
export type ReleaseEscalationResult = {
|
|
121
158
|
ok: true;
|
|
122
159
|
entry: EscalationEntry;
|
|
@@ -307,6 +344,24 @@ export interface ResolveEscalationParams {
|
|
|
307
344
|
* `condition()`'s return value and is not GIN-indexed.
|
|
308
345
|
*/
|
|
309
346
|
metadata?: Record<string, unknown>;
|
|
347
|
+
/**
|
|
348
|
+
* When provided, the resolve additionally asserts — inside the same guarded
|
|
349
|
+
* UPDATE — that no claim LOCK stands against this assignee. A claim is a
|
|
350
|
+
* lock only while its TTL window (`assigned_until`) is active; the assert
|
|
351
|
+
* blocks exactly two states: a live window held by a different assignee
|
|
352
|
+
* (`claimed-by-other`), and this assignee's own lapsed window
|
|
353
|
+
* (`claim-expired` — stale work; re-claim to resolve). Unclaimed rows,
|
|
354
|
+
* durable pre-assignments (`assigned_to` with no window), and rows whose
|
|
355
|
+
* window lapsed under a different assignee resolve normally. Closes the
|
|
356
|
+
* claim-race window for interactive claim-then-resolve flows.
|
|
357
|
+
*/
|
|
358
|
+
assertClaim?: string;
|
|
359
|
+
/**
|
|
360
|
+
* Resolver identity, delivered to the waiting workflow under the reserved
|
|
361
|
+
* `$resolution` signal key (see {@link EscalationResolution}). Never written
|
|
362
|
+
* to the stored `resolver_payload`.
|
|
363
|
+
*/
|
|
364
|
+
resolvedBy?: ResolvedByIdentity;
|
|
310
365
|
}
|
|
311
366
|
export interface ResolveByMetadataParams {
|
|
312
367
|
key: string;
|
|
@@ -320,12 +375,42 @@ export interface ResolveByMetadataParams {
|
|
|
320
375
|
* `key`/`value` selector used to find the row. See {@link ResolveEscalationParams.metadata}.
|
|
321
376
|
*/
|
|
322
377
|
metadata?: Record<string, unknown>;
|
|
378
|
+
/** Resolver identity delivered under `$resolution`. See {@link ResolveEscalationParams.resolvedBy}. */
|
|
379
|
+
resolvedBy?: ResolvedByIdentity;
|
|
323
380
|
}
|
|
324
381
|
export interface EscalateToRoleParams {
|
|
325
382
|
id: string;
|
|
326
383
|
targetRole: string;
|
|
327
384
|
namespace?: string;
|
|
328
385
|
}
|
|
386
|
+
/**
|
|
387
|
+
* Query selector for `claimManyByQuery()` — the filterable subset of
|
|
388
|
+
* `ListEscalationsParams` that describes a claimable population. `status` is
|
|
389
|
+
* not accepted: the claim targets pending rows by definition, enforced in SQL.
|
|
390
|
+
*/
|
|
391
|
+
export interface ClaimManyQuerySelector {
|
|
392
|
+
role?: string;
|
|
393
|
+
roles?: string[];
|
|
394
|
+
type?: string;
|
|
395
|
+
subtype?: string;
|
|
396
|
+
entity?: string;
|
|
397
|
+
priority?: number;
|
|
398
|
+
/** GIN-indexed `@>` containment against row metadata — the facet filter. */
|
|
399
|
+
metadata?: Record<string, unknown>;
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Atomic query-form bulk claim: one UPDATE selects and claims every matching
|
|
403
|
+
* pending, claimable row — no SELECT-then-claim window. Prefer this over
|
|
404
|
+
* `list()` + `claimMany({ids})` whenever the population is describable by
|
|
405
|
+
* filter: a row that re-parks between a search and an ids-claim is invisible
|
|
406
|
+
* to the ids form but claimed by this one.
|
|
407
|
+
*/
|
|
408
|
+
export interface ClaimManyByQueryParams {
|
|
409
|
+
query: ClaimManyQuerySelector;
|
|
410
|
+
namespace?: string;
|
|
411
|
+
assignee: string;
|
|
412
|
+
durationMinutes?: number;
|
|
413
|
+
}
|
|
329
414
|
export interface ClaimManyParams {
|
|
330
415
|
ids: string[];
|
|
331
416
|
namespace?: string;
|
|
@@ -375,6 +460,11 @@ export interface ResolveAllOrNoneParams {
|
|
|
375
460
|
* principal between the caller's claim and this resolve blocks the batch.
|
|
376
461
|
*/
|
|
377
462
|
assertAssignee?: string;
|
|
463
|
+
/**
|
|
464
|
+
* Resolver identity delivered to EVERY item's waiting workflow under
|
|
465
|
+
* `$resolution` (one resolver per batch). See {@link ResolveEscalationParams.resolvedBy}.
|
|
466
|
+
*/
|
|
467
|
+
resolvedBy?: ResolvedByIdentity;
|
|
378
468
|
}
|
|
379
469
|
/** Why a specific row blocked a `resolveAllOrNone()` batch. */
|
|
380
470
|
export type ResolveAllOrNoneBlockReason = 'not-found' | 'already-resolved' | 'already-cancelled' | 'already-expired' | 'assignee-mismatch';
|
package/build/types/index.d.ts
CHANGED
|
@@ -25,5 +25,5 @@ export { ReclaimedMessageType, RetryPolicy, RouterConfig, StreamCode, StreamConf
|
|
|
25
25
|
export { context, Context, Counter, Meter, metrics, propagation, SpanContext, Span, SpanStatus, SpanStatusCode, SpanKind, trace, Tracer, ValueType, } from './telemetry';
|
|
26
26
|
export { WorkListTaskType } from './task';
|
|
27
27
|
export { TransitionMatch, TransitionRule, Transitions } from './transition';
|
|
28
|
-
export { ConditionQueueConfig, EscalationEntry, ClaimEscalationResult, ClaimByMetadataResult, ReleaseEscalationResult, ResolveEscalationResult, CancelEscalationResult, ListEscalationsParams, CreateEscalationParams, UpdateEscalationParams, AppendMilestonesParams, ClaimEscalationParams, ClaimByMetadataParams, ReleaseEscalationParams, ResolveEscalationParams, ResolveByMetadataParams, ResolveAllOrNoneItem, ResolveAllOrNoneParams, ResolveAllOrNoneBlockReason, ResolveAllOrNoneResult, EscalateToRoleParams, MigrateEscalationParams, } from './hmsh_escalations';
|
|
28
|
+
export { ConditionQueueConfig, EscalationEntry, ClaimEscalationResult, ClaimByMetadataResult, ReleaseEscalationResult, ResolveEscalationResult, CancelEscalationResult, ListEscalationsParams, CreateEscalationParams, UpdateEscalationParams, AppendMilestonesParams, ClaimEscalationParams, ClaimByMetadataParams, ReleaseEscalationParams, ResolveEscalationParams, ResolveByMetadataParams, ResolveAllOrNoneItem, ResolveAllOrNoneParams, ResolveAllOrNoneBlockReason, ResolveAllOrNoneResult, EscalateToRoleParams, MigrateEscalationParams, EscalationResolution, ResolvedByIdentity, ClaimManyQuerySelector, ClaimManyByQueryParams, } from './hmsh_escalations';
|
|
29
29
|
export { EscalationVerb, EngineVerb, WorkerVerb, SystemEvent, EventsConfig } from './system_events';
|