@hotmeshio/hotmesh 0.26.1 → 0.26.2

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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hotmeshio/hotmesh",
3
- "version": "0.26.1",
3
+ "version": "0.26.2",
4
4
  "description": "Durable Workflow",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -171,6 +171,12 @@ export declare class EscalationClientService {
171
171
  * making it `@>`-queryable alongside the creation metadata ("what was
172
172
  * intended"). This is distinct from `resolverPayload`, which is delivered to
173
173
  * the waiting workflow as `condition()`'s return value and is not GIN-indexed.
174
+ *
175
+ * Pass `params.assertClaim` to additionally require — inside the same guarded
176
+ * UPDATE — that no active claim lock stands against that assignee. Blocks
177
+ * with `claim-expired` (own claim window lapsed) or `claimed-by-other` (a
178
+ * live window held by someone else); unclaimed rows and durable
179
+ * pre-assignments (no window) resolve normally.
174
180
  */
175
181
  resolve(params: ResolveEscalationParams, namespace?: string): Promise<ResolveEscalationResult>;
176
182
  /**
@@ -349,6 +349,12 @@ class EscalationClientService {
349
349
  * making it `@>`-queryable alongside the creation metadata ("what was
350
350
  * intended"). This is distinct from `resolverPayload`, which is delivered to
351
351
  * the waiting workflow as `condition()`'s return value and is not GIN-indexed.
352
+ *
353
+ * Pass `params.assertClaim` to additionally require — inside the same guarded
354
+ * UPDATE — that no active claim lock stands against that assignee. Blocks
355
+ * with `claim-expired` (own claim window lapsed) or `claimed-by-other` (a
356
+ * live window held by someone else); unclaimed rows and durable
357
+ * pre-assignments (no window) resolve normally.
352
358
  */
353
359
  async resolve(params, namespace) {
354
360
  const ns = (params.namespace ?? namespace) ?? factory_1.APP_ID;
@@ -361,7 +367,7 @@ class EscalationClientService {
361
367
  if (preview?.signal_key) {
362
368
  wakeCommand = await this._buildWakeCommand(ns, preview.topic, preview.signal_key, params.resolverPayload ?? {});
363
369
  }
364
- const dbResult = await store.resolveEscalation({ id: params.id, resolverPayload: params.resolverPayload, metadata: params.metadata }, wakeCommand ?? undefined);
370
+ const dbResult = await store.resolveEscalation({ id: params.id, resolverPayload: params.resolverPayload, metadata: params.metadata, assertClaim: params.assertClaim }, wakeCommand ?? undefined);
365
371
  if (!dbResult.ok)
366
372
  return dbResult;
367
373
  if (dbResult.signalKey && !dbResult.wakeEnqueued) {
@@ -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 FROM public.hmsh_escalations
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 {
@@ -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
@@ -307,6 +307,18 @@ export interface ResolveEscalationParams {
307
307
  * `condition()`'s return value and is not GIN-indexed.
308
308
  */
309
309
  metadata?: Record<string, unknown>;
310
+ /**
311
+ * When provided, the resolve additionally asserts — inside the same guarded
312
+ * UPDATE — that no claim LOCK stands against this assignee. A claim is a
313
+ * lock only while its TTL window (`assigned_until`) is active; the assert
314
+ * blocks exactly two states: a live window held by a different assignee
315
+ * (`claimed-by-other`), and this assignee's own lapsed window
316
+ * (`claim-expired` — stale work; re-claim to resolve). Unclaimed rows,
317
+ * durable pre-assignments (`assigned_to` with no window), and rows whose
318
+ * window lapsed under a different assignee resolve normally. Closes the
319
+ * claim-race window for interactive claim-then-resolve flows.
320
+ */
321
+ assertClaim?: string;
310
322
  }
311
323
  export interface ResolveByMetadataParams {
312
324
  key: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hotmeshio/hotmesh",
3
- "version": "0.26.1",
3
+ "version": "0.26.2",
4
4
  "description": "Durable Workflow",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",