@coasys/ad4m 0.13.1-staging.11 → 0.13.1-staging.13

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.
Files changed (157) hide show
  1. package/lib/index.cjs +547 -72
  2. package/lib/index.js +547 -72
  3. package/lib/src/apiClient.d.ts +7 -2
  4. package/lib/src/apiClient.js +3 -2
  5. package/lib/src/generated/api/AISessionMessage.d.ts +4 -3
  6. package/lib/src/generated/api/AiEndpoint.d.ts +3 -2
  7. package/lib/src/generated/api/Cardinality.d.ts +7 -0
  8. package/lib/src/generated/api/Cardinality.js +2 -0
  9. package/lib/src/generated/api/CascadeStep.d.ts +12 -0
  10. package/lib/src/generated/api/CascadeStep.js +2 -0
  11. package/lib/src/generated/api/CollectionItemAdded.d.ts +14 -0
  12. package/lib/src/generated/api/CollectionItemAdded.js +2 -0
  13. package/lib/src/generated/api/CollectionItemRemoved.d.ts +12 -0
  14. package/lib/src/generated/api/CollectionItemRemoved.js +2 -0
  15. package/lib/src/generated/api/CollectionItems.d.ts +7 -0
  16. package/lib/src/generated/api/CollectionItems.js +2 -0
  17. package/lib/src/generated/api/ConfigChanged.d.ts +8 -0
  18. package/lib/src/generated/api/ConfigChanged.js +2 -0
  19. package/lib/src/generated/api/ConfigKeyInfo.d.ts +10 -0
  20. package/lib/src/generated/api/ConfigKeyInfo.js +1 -0
  21. package/lib/src/generated/api/ConfigRefusal.d.ts +6 -0
  22. package/lib/src/generated/api/ConfigRefusal.js +1 -0
  23. package/lib/src/generated/api/DeviceKeys.d.ts +9 -0
  24. package/lib/src/generated/api/DeviceKeys.js +2 -0
  25. package/lib/src/generated/api/Events.d.ts +2 -0
  26. package/lib/src/generated/api/InstanceAddToCollection.d.ts +16 -0
  27. package/lib/src/generated/api/InstanceAddToCollection.js +2 -0
  28. package/lib/src/generated/api/InstanceCreate.d.ts +31 -0
  29. package/lib/src/generated/api/InstanceCreate.js +2 -0
  30. package/lib/src/generated/api/InstanceCreated.d.ts +17 -0
  31. package/lib/src/generated/api/InstanceCreated.js +1 -0
  32. package/lib/src/generated/api/InstanceError.d.ts +55 -0
  33. package/lib/src/generated/api/InstanceError.js +1 -0
  34. package/lib/src/generated/api/InstanceRemove.d.ts +12 -0
  35. package/lib/src/generated/api/InstanceRemove.js +2 -0
  36. package/lib/src/generated/api/InstanceRemoveFromCollection.d.ts +16 -0
  37. package/lib/src/generated/api/InstanceRemoveFromCollection.js +2 -0
  38. package/lib/src/generated/api/InstanceRemoved.d.ts +10 -0
  39. package/lib/src/generated/api/InstanceRemoved.js +2 -0
  40. package/lib/src/generated/api/InstanceUpdate.d.ts +15 -0
  41. package/lib/src/generated/api/InstanceUpdate.js +2 -0
  42. package/lib/src/generated/api/InstanceUpdated.d.ts +5 -0
  43. package/lib/src/generated/api/InstanceUpdated.js +2 -0
  44. package/lib/src/generated/api/KeyClass.d.ts +4 -0
  45. package/lib/src/generated/api/KeyClass.js +2 -0
  46. package/lib/src/generated/api/LogTail.d.ts +6 -0
  47. package/lib/src/generated/api/LogTail.js +2 -0
  48. package/lib/src/generated/api/LogTailParams.d.ts +9 -0
  49. package/lib/src/generated/api/LogTailParams.js +2 -0
  50. package/lib/src/generated/api/PerspectiveInstanceAddToCollectionParams.d.ts +14 -0
  51. package/lib/src/generated/api/PerspectiveInstanceAddToCollectionParams.js +2 -0
  52. package/lib/src/generated/api/PerspectiveInstanceCreateParams.d.ts +29 -0
  53. package/lib/src/generated/api/PerspectiveInstanceCreateParams.js +2 -0
  54. package/lib/src/generated/api/PerspectiveInstanceRemoveFromCollectionParams.d.ts +14 -0
  55. package/lib/src/generated/api/PerspectiveInstanceRemoveFromCollectionParams.js +2 -0
  56. package/lib/src/generated/api/PerspectiveInstanceRemoveParams.d.ts +9 -0
  57. package/lib/src/generated/api/PerspectiveInstanceRemoveParams.js +2 -0
  58. package/lib/src/generated/api/PerspectiveInstanceUpdateParams.d.ts +13 -0
  59. package/lib/src/generated/api/PerspectiveInstanceUpdateParams.js +2 -0
  60. package/lib/src/generated/api/Pinned.d.ts +5 -0
  61. package/lib/src/generated/api/Pinned.js +2 -0
  62. package/lib/src/generated/api/ReadOnlyReason.d.ts +4 -0
  63. package/lib/src/generated/api/ReadOnlyReason.js +2 -0
  64. package/lib/src/generated/api/RefusalReason.d.ts +4 -0
  65. package/lib/src/generated/api/RefusalReason.js +2 -0
  66. package/lib/src/generated/api/RpcMethods.d.ts +60 -0
  67. package/lib/src/generated/api/RpcMethods.js +3 -0
  68. package/lib/src/generated/api/RuntimeConfig.d.ts +23 -0
  69. package/lib/src/generated/api/RuntimeConfig.js +1 -0
  70. package/lib/src/generated/api/SetConfigParams.d.ts +7 -0
  71. package/lib/src/generated/api/SetConfigParams.js +2 -0
  72. package/lib/src/generated/api/SetConfigResult.d.ts +8 -0
  73. package/lib/src/generated/api/SetConfigResult.js +1 -0
  74. package/lib/src/generated/api/ValidationError.d.ts +34 -0
  75. package/lib/src/generated/api/ValidationError.js +1 -0
  76. package/lib/src/generated/api/ValidationProblem.d.ts +34 -0
  77. package/lib/src/generated/api/ValidationProblem.js +1 -0
  78. package/lib/src/generated/api/WriteStep.d.ts +37 -0
  79. package/lib/src/generated/api/WriteStep.js +2 -0
  80. package/lib/src/generated/api/index.d.ts +30 -0
  81. package/lib/src/identity/IdentityClient.d.ts +14 -0
  82. package/lib/src/identity/IdentityClient.js +17 -0
  83. package/lib/src/model/shacl-gen.js +4 -2
  84. package/lib/src/neighbourhood/MeshManager.d.ts +17 -1
  85. package/lib/src/neighbourhood/MeshManager.js +17 -0
  86. package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +7 -2
  87. package/lib/src/neighbourhood/NeighbourhoodProxy.js +7 -2
  88. package/lib/src/neighbourhood/Session.d.ts +5 -3
  89. package/lib/src/neighbourhood/Session.js +171 -26
  90. package/lib/src/perspectives/PerspectiveClient.d.ts +34 -0
  91. package/lib/src/perspectives/PerspectiveClient.js +45 -0
  92. package/lib/src/perspectives/PerspectiveProxy.d.ts +33 -0
  93. package/lib/src/perspectives/PerspectiveProxy.js +67 -2
  94. package/lib/src/perspectives/WakerSubscriptionManager.d.ts +23 -1
  95. package/lib/src/perspectives/WakerSubscriptionManager.js +187 -36
  96. package/lib/src/runtime/RuntimeClient.d.ts +13 -1
  97. package/lib/src/runtime/RuntimeClient.js +20 -0
  98. package/lib/src/shacl/SHACLShape.js +6 -2
  99. package/package.json +2 -2
  100. package/src/Ad4mClient.test.ts +118 -2
  101. package/src/apiClient.ts +10 -4
  102. package/src/generated/api/AISessionMessage.ts +4 -3
  103. package/src/generated/api/AiEndpoint.ts +3 -2
  104. package/src/generated/api/Cardinality.ts +6 -0
  105. package/src/generated/api/CascadeStep.ts +6 -0
  106. package/src/generated/api/CollectionItemAdded.ts +11 -0
  107. package/src/generated/api/CollectionItemRemoved.ts +9 -0
  108. package/src/generated/api/CollectionItems.ts +6 -0
  109. package/src/generated/api/ConfigChanged.ts +7 -0
  110. package/src/generated/api/ConfigKeyInfo.ts +8 -0
  111. package/src/generated/api/ConfigRefusal.ts +4 -0
  112. package/src/generated/api/DeviceKeys.ts +8 -0
  113. package/src/generated/api/Events.ts +2 -0
  114. package/src/generated/api/InstanceAddToCollection.ts +14 -0
  115. package/src/generated/api/InstanceCreate.ts +32 -0
  116. package/src/generated/api/InstanceCreated.ts +16 -0
  117. package/src/generated/api/InstanceError.ts +10 -0
  118. package/src/generated/api/InstanceRemove.ts +11 -0
  119. package/src/generated/api/InstanceRemoveFromCollection.ts +14 -0
  120. package/src/generated/api/InstanceRemoved.ts +9 -0
  121. package/src/generated/api/InstanceUpdate.ts +14 -0
  122. package/src/generated/api/InstanceUpdated.ts +3 -0
  123. package/src/generated/api/KeyClass.ts +6 -0
  124. package/src/generated/api/LogTail.ts +6 -0
  125. package/src/generated/api/LogTailParams.ts +10 -0
  126. package/src/generated/api/PerspectiveInstanceAddToCollectionParams.ts +11 -0
  127. package/src/generated/api/PerspectiveInstanceCreateParams.ts +29 -0
  128. package/src/generated/api/PerspectiveInstanceRemoveFromCollectionParams.ts +11 -0
  129. package/src/generated/api/PerspectiveInstanceRemoveParams.ts +7 -0
  130. package/src/generated/api/PerspectiveInstanceUpdateParams.ts +11 -0
  131. package/src/generated/api/Pinned.ts +7 -0
  132. package/src/generated/api/ReadOnlyReason.ts +6 -0
  133. package/src/generated/api/RefusalReason.ts +6 -0
  134. package/src/generated/api/RpcMethods.ts +30 -0
  135. package/src/generated/api/RuntimeConfig.ts +24 -0
  136. package/src/generated/api/SetConfigParams.ts +6 -0
  137. package/src/generated/api/SetConfigResult.ts +7 -0
  138. package/src/generated/api/ValidationError.ts +34 -0
  139. package/src/generated/api/ValidationProblem.ts +7 -0
  140. package/src/generated/api/WriteStep.ts +6 -0
  141. package/src/generated/api/index.ts +34 -0
  142. package/src/identity/IdentityClient.ts +20 -0
  143. package/src/model/shacl-gen.ts +4 -2
  144. package/src/neighbourhood/MeshManager.test.ts +57 -0
  145. package/src/neighbourhood/MeshManager.ts +26 -0
  146. package/src/neighbourhood/NeighbourhoodProxy.ts +7 -2
  147. package/src/neighbourhood/Session.test.ts +355 -3
  148. package/src/neighbourhood/Session.ts +170 -28
  149. package/src/perspectives/PerspectiveClient.ts +56 -0
  150. package/src/perspectives/PerspectiveProxy.test.ts +34 -0
  151. package/src/perspectives/PerspectiveProxy.ts +76 -2
  152. package/src/perspectives/WakerSubscriptionManager.test.ts +379 -1
  153. package/src/perspectives/WakerSubscriptionManager.ts +189 -41
  154. package/src/runtime/RuntimeClient.ts +25 -1
  155. package/src/shacl/SHACLFlow.test.ts +4 -2
  156. package/src/shacl/SHACLShape.test.ts +10 -3
  157. package/src/shacl/SHACLShape.ts +6 -2
@@ -44,6 +44,17 @@ export interface WakerSubscription {
44
44
  mentionQuery?: string;
45
45
  /** The class `mentionQuery` reads: the built-in `ad4m://Mentioning`. */
46
46
  mentionClass?: string;
47
+ /**
48
+ * Set by the manager, not the caller: whether a mention subscription's text
49
+ * query has taken its opening result, which the manager records as history.
50
+ * It persists with the subscription. Until it is set, the text query's
51
+ * opening result is history even after a restart: the persisted seen set
52
+ * may hold the mention query's records only (the subscription listened
53
+ * without its text query, #1735), so the text query's old matches would all
54
+ * wake at once. A subscription saved without it (an older version) starts
55
+ * that way too.
56
+ */
57
+ textHistoryRead?: boolean;
47
58
  neighbourhood?: string;
48
59
  }
49
60
 
@@ -346,7 +357,10 @@ export interface WakerSubscriptionManagerOptions {
346
357
  * the bound. Covers both `proxy.subscribe()` and `proxy.initialized`: when
347
358
  * the executor accepts the socket but never answers, neither settles, and
348
359
  * without a deadline the subscription ended up neither active nor pending,
349
- * so the re-attempt cycle never saw it (#1016).
360
+ * so the re-attempt cycle never saw it (#1016). Each query of a mention
361
+ * subscription gets its own: one whose mention query opens listens through
362
+ * it, and re-attempts a text query that did not open on its own (#1735),
363
+ * with this budget doubled per failed re-attempt, up to `maxRetryPendingMs`.
350
364
  */
351
365
  subscribeTimeoutMs?: number;
352
366
  /** First delay before offering an undelivered mention wake again (default 5000); doubles per
@@ -428,6 +442,14 @@ export class WakerSubscriptionManager {
428
442
  * keeps persisting them so a restart restores them (#1595).
429
443
  */
430
444
  private reopening = new Map<string, { sub: WakerSubscription; proxies: Set<any> }>();
445
+ /**
446
+ * Mention subscriptions listening through their mention query only, because
447
+ * their text query failed to open (#1735: a TextContent `contains` scan over a
448
+ * large neighbourhood can take longer than the handshake budget). The text
449
+ * query alone is re-attempted: `timer` is the wait before the next attempt,
450
+ * `proxy` the attempt in flight. `release()` cancels both.
451
+ */
452
+ private textRetries = new Map<string, { timer: ReturnType<typeof setTimeout> | null; proxy: any }>();
431
453
  /**
432
454
  * Cancellation tokens for in-flight `subscribe()` calls.
433
455
  *
@@ -491,6 +513,15 @@ export class WakerSubscriptionManager {
491
513
  * If a subscription with the same id already exists, it is disposed first.
492
514
  */
493
515
  async subscribe(sub: WakerSubscription): Promise<void> {
516
+ // The manager's own copy: it records `textHistoryRead` on it. A re-derived
517
+ // subscription under the same id keeps what the manager knows of its text
518
+ // query.
519
+ const prior =
520
+ this.activeSubscriptions.get(sub.id) ??
521
+ this.pendingSubscriptions.get(sub.id) ??
522
+ this.reopening.get(sub.id)?.sub;
523
+ sub = { ...sub };
524
+ if (prior?.textHistoryRead && sub.textHistoryRead === undefined) sub.textHistoryRead = true;
494
525
  // Dispose existing subscription with same id if any. Not `dispose()`: a
495
526
  // re-attempt must not start the backoff over.
496
527
  this.release(sub.id, false);
@@ -521,12 +552,16 @@ export class WakerSubscriptionManager {
521
552
  this.logger.info(`[waker] ${sub.id}: model query over ${sub.mentionClass}:\n${sub.mentionQuery}`);
522
553
  }
523
554
 
524
- const proxy = sub.className
525
- ? new QuerySubscriptionProxy(sub.perspective, sub.query, this.perspectiveClient, sub.className)
526
- : new QuerySubscriptionProxy(sub.perspective, sub.query, this.perspectiveClient);
555
+ const textProxy = () =>
556
+ sub.className
557
+ ? new QuerySubscriptionProxy(sub.perspective, sub.query, this.perspectiveClient, sub.className)
558
+ : new QuerySubscriptionProxy(sub.perspective, sub.query, this.perspectiveClient);
559
+ const proxy = textProxy();
527
560
  const mentionProxy = sub.mentionQuery
528
561
  ? new QuerySubscriptionProxy(sub.perspective, sub.mentionQuery, this.perspectiveClient, sub.mentionClass)
529
562
  : undefined;
563
+ // The live proxies. A text query that failed while the mention query opened
564
+ // leaves it, and joins it again once a re-attempt opens (#1735).
530
565
  const proxies = mentionProxy ? [proxy, mentionProxy] : [proxy];
531
566
  // `cancelOpening`: a handshake given up on must not keep the executor
532
567
  // computing a first result nobody reads; abandoned ones piled up into
@@ -536,32 +571,48 @@ export class WakerSubscriptionManager {
536
571
  try { p.dispose({ cancelOpening: true }); } catch {}
537
572
  }
538
573
  };
539
- // Suppress unhandled rejection from proxy.initialized — QuerySubscriptionProxy
540
- // rejects this promise internally when subscribe() fails, and if nobody catches
541
- // it before the next microtask it crashes the process.
542
- for (const p of proxies) {
543
- if (p.initialized && typeof p.initialized.catch === "function") p.initialized.catch(() => {});
544
- }
545
- // One deadline for the whole handshake: `proxy.subscribe()` and the
574
+ // One deadline per query's handshake: `proxy.subscribe()` and the
546
575
  // `initialized` promise it resolves share the budget, so a hang in either
547
576
  // surfaces as the same loud-but-recoverable failure a rejection does.
548
- const budgetMs = this.subscribeTimeoutMs;
549
- const startedAt = Date.now();
550
- const bounded = <T>(step: Promise<T>, what: string): Promise<T> =>
551
- budgetMs > 0
552
- ? withTimeout(
553
- step,
554
- Math.max(0, budgetMs - (Date.now() - startedAt)),
555
- `${what} timed out after ${budgetMs}ms`,
556
- )
557
- : step;
577
+ const open = async (p: any, budgetMs: number): Promise<void> => {
578
+ // Suppress unhandled rejection from proxy.initialized — QuerySubscriptionProxy
579
+ // rejects this promise internally when subscribe() fails, and if nobody catches
580
+ // it before the next microtask it crashes the process.
581
+ if (p.initialized && typeof p.initialized.catch === "function") p.initialized.catch(() => {});
582
+ const startedAt = Date.now();
583
+ const bounded = <T>(step: Promise<T>, what: string): Promise<T> =>
584
+ budgetMs > 0
585
+ ? withTimeout(
586
+ step,
587
+ Math.max(0, budgetMs - (Date.now() - startedAt)),
588
+ `${what} timed out after ${budgetMs}ms`,
589
+ )
590
+ : step;
591
+ await bounded(p.subscribe(), "subscribe handshake");
592
+ // Stale: the caller disposes the proxies.
593
+ if (this.isStale(sub.id, token)) return;
594
+ await bounded(p.initialized, "subscription initialization");
595
+ };
596
+ // A mention subscription's two queries open independently: on a large
597
+ // neighbourhood the text query (a `contains` scan over every TextContent)
598
+ // can take longer than the budget while the mention query answers in under
599
+ // a second (#1735). Waiting on both made the whole subscription fail, and
600
+ // the agent got no wake at all, not even through a mention link. The
601
+ // mention query decides; a text query that fails is re-attempted on its own
602
+ // once the subscription listens.
603
+ const openings = proxies.map((p) => open(p, this.subscribeTimeoutMs));
604
+ for (const o of openings) o.catch(() => {});
605
+ let textFailure: string | undefined;
558
606
  try {
559
- await bounded(Promise.all(proxies.map((p) => p.subscribe())), "subscribe handshake");
560
- if (this.isStale(sub.id, token)) {
561
- disposeProxies();
562
- return;
607
+ await openings[openings.length - 1];
608
+ if (mentionProxy) {
609
+ const [text] = await Promise.allSettled([openings[0]]);
610
+ if (text.status === "rejected") {
611
+ textFailure = text.reason?.message ?? String(text.reason);
612
+ try { proxy.dispose({ cancelOpening: true }); } catch {}
613
+ proxies.splice(proxies.indexOf(proxy), 1);
614
+ }
563
615
  }
564
- await bounded(Promise.all(proxies.map((p) => p.initialized)), "subscription initialization");
565
616
  } catch (err: any) {
566
617
  const msg = err?.message ?? String(err);
567
618
  this.logger.warn(
@@ -609,10 +660,13 @@ export class WakerSubscriptionManager {
609
660
  this.logger.info(`[waker] ${sub.id}: seeded ${seen.size} seen message(s) from persisted state`);
610
661
  }
611
662
 
612
- // `opening` is the result the subscription holds when it opens (`proxy.result`): `onResult`
613
- // only reports later changes, so this is the one chance to see what was written meanwhile.
614
663
  // `of`: the query the result is from, for the log (a mention subscription has two).
615
- const handleResult = async (result: any, opening = false, of = "") => {
664
+ // `opening` marks the result the query holds when it opens (`proxy.result`): `onResult`
665
+ // only reports later changes, so this is the one chance to see what was written meanwhile.
666
+ // A mention subscription reads it as `"history"` (recorded as seen, no wake) or `"news"`
667
+ // (wakes on what is not seen yet: written while the waker was down).
668
+ const handleResult = async (result: any, of: string, opening?: "history" | "news") => {
669
+ if (this.isStale(sub.id, token)) return;
616
670
  this.logger.info(
617
671
  `[waker] ${sub.id}: ${opening ? "opening result" : "onResult fired"}${of} — type=${typeof result}, isArray=${Array.isArray(result)}, value=${String(JSON.stringify(result)).substring(0, 500)}`,
618
672
  );
@@ -657,8 +711,8 @@ export class WakerSubscriptionManager {
657
711
  return;
658
712
  }
659
713
 
660
- if (opening && !restored) {
661
- // A new subscription: what it opens on is history, readable on demand, not news.
714
+ if (opening === "history") {
715
+ // What it opens on is history, readable on demand, not news.
662
716
  this.seenMessages.set(sub.id, currentSeen);
663
717
  const done = this.delivered.get(sub.id) ?? new Set<string>();
664
718
  for (const address of newMessages) done.add(address);
@@ -685,6 +739,9 @@ export class WakerSubscriptionManager {
685
739
  );
686
740
  mentions.push({ address: msgAddr, parents, ancestors, ...provenance.get(msgAddr)! });
687
741
  }
742
+ // Released, or replaced under the same id, during the lookups: the state under `sub.id`
743
+ // is no longer this subscription's, so write nothing and wake for nothing.
744
+ if (this.isStale(sub.id, token)) return;
688
745
 
689
746
  this.seenMessages.set(sub.id, currentSeen);
690
747
  const pending = this.undelivered.get(sub.id) ?? new Map<string, MentionMessage>();
@@ -713,11 +770,30 @@ export class WakerSubscriptionManager {
713
770
  );
714
771
  }
715
772
  };
716
- proxy.onResult((result: any) => handleResult(result));
717
- mentionProxy?.onResult((result: any) => handleResult(result, false, " (mention query)"));
718
- for (const p of proxies) {
773
+ // Read `p`'s opening result. The mention query's is news only after a restart. The text
774
+ // query's is news only after a restart and once a text query of this subscription has read
775
+ // its history (`textHistoryRead`): it may have listened without it, so the seen set may not
776
+ // hold what the text query finds (#1735).
777
+ const readOpening = async (p: any, of: string) => {
778
+ if (p.result === undefined) return;
779
+ const isText = p !== mentionProxy;
780
+ const news = restored && (!isText || sub.type !== "mention" || sub.textHistoryRead === true);
781
+ try {
782
+ await handleResult(p.result, of, news ? "news" : "history");
783
+ } catch (e: any) {
784
+ this.logger.warn(`[waker] ${sub.id}: reading the opening result failed: ${e?.message ?? e}`);
785
+ }
786
+ const rows: unknown = sub.className ? p.result?.instances : p.result;
787
+ if (isText && sub.type === "mention" && Array.isArray(rows) && !this.isStale(sub.id, token)) {
788
+ sub.textHistoryRead = true;
789
+ }
790
+ };
791
+ const listen = (p: any, of: string) => {
792
+ p.onResult((result: any) => handleResult(result, of));
719
793
  p.onStatus?.((status: any) => this.onReopenStatus(sub, proxies, p, status));
720
- }
794
+ };
795
+ if (proxies.includes(proxy)) listen(proxy, "");
796
+ if (mentionProxy) listen(mentionProxy, " (mention query)");
721
797
 
722
798
  if (this.isStale(sub.id, token)) {
723
799
  disposeProxies();
@@ -725,16 +801,78 @@ export class WakerSubscriptionManager {
725
801
  }
726
802
  this.proxies.set(sub.id, proxies);
727
803
  this.activeSubscriptions.set(sub.id, sub);
804
+
805
+ if (textFailure !== undefined) {
806
+ // Re-attempt the text query alone, not the subscription: subscribing it
807
+ // again would re-seed its seen set from the persisted one, and wake again
808
+ // on mentions delivered since. Each failed attempt doubles the wait, up to
809
+ // `maxRetryPendingMs`, between half and all of it, as `scheduleRetry` does.
810
+ // The handshake budget doubles per failed attempt too: a scan that takes
811
+ // longer than `subscribeTimeoutMs` (#1735 measured over 30 s) would
812
+ // otherwise time out on every attempt. It is capped at `maxRetryPendingMs`
813
+ // (or the first budget, if longer): with one attempt in flight at a time,
814
+ // the executor then spends at most about as long on an attempt as the
815
+ // waker waits between two.
816
+ let ceiling = this.retryPendingMs;
817
+ let budgetMs = this.subscribeTimeoutMs;
818
+ const retryText = (): number => {
819
+ const delayMs = ceiling / 2 + Math.random() * (ceiling / 2);
820
+ ceiling = Math.min(ceiling * 2, this.maxRetryPendingMs);
821
+ const attemptBudgetMs = budgetMs;
822
+ budgetMs = Math.min(budgetMs * 2, Math.max(this.subscribeTimeoutMs, this.maxRetryPendingMs));
823
+ const timer = setTimeout(async () => {
824
+ if (this.textRetries.get(sub.id)?.timer !== timer || this.isStale(sub.id, token)) return;
825
+ const p = textProxy();
826
+ this.textRetries.set(sub.id, { timer: null, proxy: p });
827
+ // `release()` disposed it already when the entry no longer holds it.
828
+ const ours = () => this.textRetries.get(sub.id)?.proxy === p;
829
+ try {
830
+ await open(p, attemptBudgetMs);
831
+ } catch (err: any) {
832
+ if (!ours()) return;
833
+ try { p.dispose({ cancelOpening: true }); } catch {}
834
+ if (this.isStale(sub.id, token)) {
835
+ this.textRetries.delete(sub.id);
836
+ return;
837
+ }
838
+ const nextMs = retryText();
839
+ this.logger.debug(
840
+ `[waker] ${sub.id}: text query failed again (${err?.message ?? err}); re-attempting in ${Math.round(nextMs / 1000)}s`,
841
+ );
842
+ return;
843
+ }
844
+ if (!ours()) return;
845
+ this.textRetries.delete(sub.id);
846
+ if (this.isStale(sub.id, token) || this.proxies.get(sub.id) !== proxies) {
847
+ try { p.dispose({ cancelOpening: true }); } catch {}
848
+ return;
849
+ }
850
+ proxies.push(p);
851
+ listen(p, "");
852
+ this.logger.info(`[waker] ${sub.id}: text query opened; listening through both queries`);
853
+ // The seen set is the mention query's, so a record it already woke on does not wake
854
+ // again.
855
+ await readOpening(p, "");
856
+ if (!this.isStale(sub.id, token)) this.persist();
857
+ }, delayMs);
858
+ if (typeof timer.unref === "function") timer.unref();
859
+ this.textRetries.set(sub.id, { timer, proxy: null });
860
+ return delayMs;
861
+ };
862
+ const firstMs = this.retryPendingMs > 0 ? retryText() : undefined;
863
+ this.logger.warn(
864
+ `[waker] ${sub.id}: text query failed (${textFailure}); listening through the mention query only, ` +
865
+ (firstMs === undefined
866
+ ? "not re-attempting the text query (retryPendingMs is 0)"
867
+ : `re-attempting the text query in ${Math.round(firstMs / 1000)}s`),
868
+ );
869
+ }
870
+
728
871
  // After a restart this wakes on mentions written while the waker was down; on a new
729
872
  // subscription it records the existing ones as history. The mention query's first, so a
730
873
  // record both hold wakes through its mention link.
731
874
  for (const [p, of] of [[mentionProxy, " (mention query)"], [proxy, ""]] as const) {
732
- if (p?.result === undefined) continue;
733
- try {
734
- await handleResult(p.result, true, of);
735
- } catch (e: any) {
736
- this.logger.warn(`[waker] ${sub.id}: reading the opening result failed: ${e?.message ?? e}`);
737
- }
875
+ if (p && proxies.includes(p)) await readOpening(p, of);
738
876
  }
739
877
  this.persist();
740
878
 
@@ -863,6 +1001,16 @@ export class WakerSubscriptionManager {
863
1001
  }
864
1002
  }
865
1003
  this.proxies.delete(id);
1004
+ const textRetry = this.textRetries.get(id);
1005
+ if (textRetry) {
1006
+ if (textRetry.timer) clearTimeout(textRetry.timer);
1007
+ try {
1008
+ textRetry.proxy?.dispose({ cancelOpening: true });
1009
+ } catch {
1010
+ /* ignore */
1011
+ }
1012
+ this.textRetries.delete(id);
1013
+ }
866
1014
  this.reopening.delete(id);
867
1015
  const timer = this.debounceTimers.get(id);
868
1016
  if (timer) {
@@ -1,7 +1,7 @@
1
1
  import {ApiClient, CallOptions } from '../apiClient'
2
2
  import { Perspective, PerspectiveExpression } from "../perspectives/Perspective"
3
3
  import { RuntimeInfo, SentMessage, NotificationInput, NotificationUpdate, Notification, ImportResult, UserStatistics } from "./RuntimeTypes"
4
- import type { HostRate, UnytVersionInfo } from "../generated/api"
4
+ import type { HostRate, LogTail, RuntimeConfig, SetConfigResult, UnytVersionInfo } from "../generated/api"
5
5
  import { LanguageMeta } from "../language/LanguageMeta"
6
6
 
7
7
  export class RuntimeClient {
@@ -19,10 +19,34 @@ export class RuntimeClient {
19
19
  return this.#apiClient.call('runtime.tlsDomain', {})
20
20
  }
21
21
 
22
+ /** Stops the executor (exit 0). Only the operator on the executor's machine. */
22
23
  async quit(): Promise<Boolean> {
23
24
  return this.#apiClient.call('runtime.quit', {})
24
25
  }
25
26
 
27
+ /** Answers, then the executor exits 75 and its supervisor starts it again. */
28
+ async restart(): Promise<boolean> {
29
+ return this.#apiClient.call('runtime.restart', {})
30
+ }
31
+
32
+ /** The newest lines (at most 2000) of the executor's log file (`--log-dir`). */
33
+ async logTail(lines: number): Promise<LogTail> {
34
+ return this.#apiClient.call('runtime.logTail', { lines })
35
+ }
36
+
37
+ /** The executor's config file: its values, every key with its class, and whether a write waits for a restart. */
38
+ async config(): Promise<RuntimeConfig> {
39
+ return this.#apiClient.call('runtime.config', {})
40
+ }
41
+
42
+ /**
43
+ * Writes keys to the executor's config file (`--config`); they take effect at the next
44
+ * start. A `machine` key from a caller off the executor's machine is refused `local-only`.
45
+ */
46
+ async setConfig(set?: Record<string, unknown>, unset?: string[]): Promise<SetConfigResult> {
47
+ return this.#apiClient.call('runtime.setConfig', { set, unset })
48
+ }
49
+
26
50
  async openLink(url: string): Promise<Boolean> {
27
51
  return this.#apiClient.call('runtime.openLink', { url })
28
52
  }
@@ -304,14 +304,16 @@ describe('SHACLFlow', () => {
304
304
  // V8 backtracks through all of them when the match fails at the end
305
305
  // (100k digits then `x`: about 10 s).
306
306
  it('state value decoder rejects a long non-number in linear time', () => {
307
- const digits = '1'.repeat(50_000);
307
+ // A quadratic decoder takes seconds at 50k digits, and 16 times that at 200k; a linear one
308
+ // takes milliseconds. The 2 s bound sits far from both, so a busy CI runner cannot fail it.
309
+ const digits = '1'.repeat(200_000);
308
310
  for (const tail of ['x', 'e', 'e+', '.x']) {
309
311
  const target = `literal:number:${digits}${tail}`;
310
312
  const started = performance.now();
311
313
  const flow = SHACLFlow.fromLinks(flowLinksWithStates('xrt://XFlow', [['s', target]], false), 'xrt://XFlow');
312
314
  const elapsed = performance.now() - started;
313
315
  expect([tail, Number.isNaN(flow.states[0].value)]).toEqual([tail, true]);
314
- expect([tail, elapsed < 200]).toEqual([tail, true]);
316
+ expect([tail, elapsed < 2000]).toEqual([tail, true]);
315
317
  }
316
318
  });
317
319
 
@@ -306,7 +306,8 @@ describe('SHACLShape', () => {
306
306
  return [...names, ...links.flatMap(({ source, predicate, target }) => {
307
307
  const string = target.startsWith('literal:string:') ? target.slice('literal:string:'.length) : undefined;
308
308
  if (predicate === 'sh://in') {
309
- const values = JSON.parse(string!).map((v: { value: string }) => v.value);
309
+ const values = JSON.parse(string!).map((v: any) =>
310
+ v !== null && typeof v === 'object' && 'value' in v ? v.value : v);
310
311
  return [`${source} sh://in ${JSON.stringify(values)}`, `${source} ad4m://in ${string}`];
311
312
  }
312
313
  const value = string === undefined
@@ -614,7 +615,9 @@ describe('SHACLShape', () => {
614
615
  expect(reconstructed.properties[0].in).toEqual(original.properties[0].in);
615
616
  });
616
617
 
617
- it('handles sh:in with empty array (no link created)', () => {
618
+ it('handles sh:in with empty array (deny-all constraint is stored)', () => {
619
+ // `in: []` allows nothing — a constraint the executor must see, so the
620
+ // link is written. Only an absent `in` means unconstrained.
618
621
  const original = new SHACLShape('test://Model');
619
622
  original.addProperty({
620
623
  name: 'field',
@@ -624,7 +627,11 @@ describe('SHACLShape', () => {
624
627
 
625
628
  const links = original.toLinks();
626
629
  const inLink = links.find(l => l.predicate === 'sh://in');
627
- expect(inLink).toBeUndefined();
630
+ expect(inLink).toBeDefined();
631
+ expect(inLink!.target).toBe('literal:string:[]');
632
+
633
+ const reconstructed = SHACLShape.fromLinks(links, 'test://ModelShape');
634
+ expect(reconstructed.properties[0].in).toEqual([]);
628
635
  });
629
636
 
630
637
  it('preserves sh:in through toJSON() → fromJSON()', () => {
@@ -393,7 +393,10 @@ export class SHACLShape {
393
393
  const statements = (source: string) => links
394
394
  .filter(l => l.source === source && l.predicate !== 'sh://property')
395
395
  .flatMap(l => l.predicate === 'sh://in'
396
- ? [`sh:in ( ${JSON.parse(l.target.slice(15)).map((v: { value: string }) => str(v.value)).join(' ')} )`, `ad4m:in ${obj(l)}`]
396
+ ? [`sh:in ( ${JSON.parse(l.target.slice(15)).map((v: any) => {
397
+ const value = v !== null && typeof v === 'object' && 'value' in v ? v.value : v;
398
+ return typeof value === 'string' ? str(value) : JSON.stringify(value);
399
+ }).join(' ')} )`, `ad4m:in ${obj(l)}`]
397
400
  : [`${pred(l.predicate!)} ${obj(l)}`]);
398
401
 
399
402
  const properties = links
@@ -662,7 +665,8 @@ export class SHACLShape {
662
665
  });
663
666
  }
664
667
 
665
- if (prop.in && prop.in.length > 0) {
668
+ // `in: []` is a deny-all constraint, not the absence of one — store it.
669
+ if (prop.in !== undefined) {
666
670
  links.push({
667
671
  source: propShapeId,
668
672
  predicate: "sh://in",