@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.
- package/lib/index.cjs +547 -72
- package/lib/index.js +547 -72
- package/lib/src/apiClient.d.ts +7 -2
- package/lib/src/apiClient.js +3 -2
- package/lib/src/generated/api/AISessionMessage.d.ts +4 -3
- package/lib/src/generated/api/AiEndpoint.d.ts +3 -2
- package/lib/src/generated/api/Cardinality.d.ts +7 -0
- package/lib/src/generated/api/Cardinality.js +2 -0
- package/lib/src/generated/api/CascadeStep.d.ts +12 -0
- package/lib/src/generated/api/CascadeStep.js +2 -0
- package/lib/src/generated/api/CollectionItemAdded.d.ts +14 -0
- package/lib/src/generated/api/CollectionItemAdded.js +2 -0
- package/lib/src/generated/api/CollectionItemRemoved.d.ts +12 -0
- package/lib/src/generated/api/CollectionItemRemoved.js +2 -0
- package/lib/src/generated/api/CollectionItems.d.ts +7 -0
- package/lib/src/generated/api/CollectionItems.js +2 -0
- package/lib/src/generated/api/ConfigChanged.d.ts +8 -0
- package/lib/src/generated/api/ConfigChanged.js +2 -0
- package/lib/src/generated/api/ConfigKeyInfo.d.ts +10 -0
- package/lib/src/generated/api/ConfigKeyInfo.js +1 -0
- package/lib/src/generated/api/ConfigRefusal.d.ts +6 -0
- package/lib/src/generated/api/ConfigRefusal.js +1 -0
- package/lib/src/generated/api/DeviceKeys.d.ts +9 -0
- package/lib/src/generated/api/DeviceKeys.js +2 -0
- package/lib/src/generated/api/Events.d.ts +2 -0
- package/lib/src/generated/api/InstanceAddToCollection.d.ts +16 -0
- package/lib/src/generated/api/InstanceAddToCollection.js +2 -0
- package/lib/src/generated/api/InstanceCreate.d.ts +31 -0
- package/lib/src/generated/api/InstanceCreate.js +2 -0
- package/lib/src/generated/api/InstanceCreated.d.ts +17 -0
- package/lib/src/generated/api/InstanceCreated.js +1 -0
- package/lib/src/generated/api/InstanceError.d.ts +55 -0
- package/lib/src/generated/api/InstanceError.js +1 -0
- package/lib/src/generated/api/InstanceRemove.d.ts +12 -0
- package/lib/src/generated/api/InstanceRemove.js +2 -0
- package/lib/src/generated/api/InstanceRemoveFromCollection.d.ts +16 -0
- package/lib/src/generated/api/InstanceRemoveFromCollection.js +2 -0
- package/lib/src/generated/api/InstanceRemoved.d.ts +10 -0
- package/lib/src/generated/api/InstanceRemoved.js +2 -0
- package/lib/src/generated/api/InstanceUpdate.d.ts +15 -0
- package/lib/src/generated/api/InstanceUpdate.js +2 -0
- package/lib/src/generated/api/InstanceUpdated.d.ts +5 -0
- package/lib/src/generated/api/InstanceUpdated.js +2 -0
- package/lib/src/generated/api/KeyClass.d.ts +4 -0
- package/lib/src/generated/api/KeyClass.js +2 -0
- package/lib/src/generated/api/LogTail.d.ts +6 -0
- package/lib/src/generated/api/LogTail.js +2 -0
- package/lib/src/generated/api/LogTailParams.d.ts +9 -0
- package/lib/src/generated/api/LogTailParams.js +2 -0
- package/lib/src/generated/api/PerspectiveInstanceAddToCollectionParams.d.ts +14 -0
- package/lib/src/generated/api/PerspectiveInstanceAddToCollectionParams.js +2 -0
- package/lib/src/generated/api/PerspectiveInstanceCreateParams.d.ts +29 -0
- package/lib/src/generated/api/PerspectiveInstanceCreateParams.js +2 -0
- package/lib/src/generated/api/PerspectiveInstanceRemoveFromCollectionParams.d.ts +14 -0
- package/lib/src/generated/api/PerspectiveInstanceRemoveFromCollectionParams.js +2 -0
- package/lib/src/generated/api/PerspectiveInstanceRemoveParams.d.ts +9 -0
- package/lib/src/generated/api/PerspectiveInstanceRemoveParams.js +2 -0
- package/lib/src/generated/api/PerspectiveInstanceUpdateParams.d.ts +13 -0
- package/lib/src/generated/api/PerspectiveInstanceUpdateParams.js +2 -0
- package/lib/src/generated/api/Pinned.d.ts +5 -0
- package/lib/src/generated/api/Pinned.js +2 -0
- package/lib/src/generated/api/ReadOnlyReason.d.ts +4 -0
- package/lib/src/generated/api/ReadOnlyReason.js +2 -0
- package/lib/src/generated/api/RefusalReason.d.ts +4 -0
- package/lib/src/generated/api/RefusalReason.js +2 -0
- package/lib/src/generated/api/RpcMethods.d.ts +60 -0
- package/lib/src/generated/api/RpcMethods.js +3 -0
- package/lib/src/generated/api/RuntimeConfig.d.ts +23 -0
- package/lib/src/generated/api/RuntimeConfig.js +1 -0
- package/lib/src/generated/api/SetConfigParams.d.ts +7 -0
- package/lib/src/generated/api/SetConfigParams.js +2 -0
- package/lib/src/generated/api/SetConfigResult.d.ts +8 -0
- package/lib/src/generated/api/SetConfigResult.js +1 -0
- package/lib/src/generated/api/ValidationError.d.ts +34 -0
- package/lib/src/generated/api/ValidationError.js +1 -0
- package/lib/src/generated/api/ValidationProblem.d.ts +34 -0
- package/lib/src/generated/api/ValidationProblem.js +1 -0
- package/lib/src/generated/api/WriteStep.d.ts +37 -0
- package/lib/src/generated/api/WriteStep.js +2 -0
- package/lib/src/generated/api/index.d.ts +30 -0
- package/lib/src/identity/IdentityClient.d.ts +14 -0
- package/lib/src/identity/IdentityClient.js +17 -0
- package/lib/src/model/shacl-gen.js +4 -2
- package/lib/src/neighbourhood/MeshManager.d.ts +17 -1
- package/lib/src/neighbourhood/MeshManager.js +17 -0
- package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +7 -2
- package/lib/src/neighbourhood/NeighbourhoodProxy.js +7 -2
- package/lib/src/neighbourhood/Session.d.ts +5 -3
- package/lib/src/neighbourhood/Session.js +171 -26
- package/lib/src/perspectives/PerspectiveClient.d.ts +34 -0
- package/lib/src/perspectives/PerspectiveClient.js +45 -0
- package/lib/src/perspectives/PerspectiveProxy.d.ts +33 -0
- package/lib/src/perspectives/PerspectiveProxy.js +67 -2
- package/lib/src/perspectives/WakerSubscriptionManager.d.ts +23 -1
- package/lib/src/perspectives/WakerSubscriptionManager.js +187 -36
- package/lib/src/runtime/RuntimeClient.d.ts +13 -1
- package/lib/src/runtime/RuntimeClient.js +20 -0
- package/lib/src/shacl/SHACLShape.js +6 -2
- package/package.json +2 -2
- package/src/Ad4mClient.test.ts +118 -2
- package/src/apiClient.ts +10 -4
- package/src/generated/api/AISessionMessage.ts +4 -3
- package/src/generated/api/AiEndpoint.ts +3 -2
- package/src/generated/api/Cardinality.ts +6 -0
- package/src/generated/api/CascadeStep.ts +6 -0
- package/src/generated/api/CollectionItemAdded.ts +11 -0
- package/src/generated/api/CollectionItemRemoved.ts +9 -0
- package/src/generated/api/CollectionItems.ts +6 -0
- package/src/generated/api/ConfigChanged.ts +7 -0
- package/src/generated/api/ConfigKeyInfo.ts +8 -0
- package/src/generated/api/ConfigRefusal.ts +4 -0
- package/src/generated/api/DeviceKeys.ts +8 -0
- package/src/generated/api/Events.ts +2 -0
- package/src/generated/api/InstanceAddToCollection.ts +14 -0
- package/src/generated/api/InstanceCreate.ts +32 -0
- package/src/generated/api/InstanceCreated.ts +16 -0
- package/src/generated/api/InstanceError.ts +10 -0
- package/src/generated/api/InstanceRemove.ts +11 -0
- package/src/generated/api/InstanceRemoveFromCollection.ts +14 -0
- package/src/generated/api/InstanceRemoved.ts +9 -0
- package/src/generated/api/InstanceUpdate.ts +14 -0
- package/src/generated/api/InstanceUpdated.ts +3 -0
- package/src/generated/api/KeyClass.ts +6 -0
- package/src/generated/api/LogTail.ts +6 -0
- package/src/generated/api/LogTailParams.ts +10 -0
- package/src/generated/api/PerspectiveInstanceAddToCollectionParams.ts +11 -0
- package/src/generated/api/PerspectiveInstanceCreateParams.ts +29 -0
- package/src/generated/api/PerspectiveInstanceRemoveFromCollectionParams.ts +11 -0
- package/src/generated/api/PerspectiveInstanceRemoveParams.ts +7 -0
- package/src/generated/api/PerspectiveInstanceUpdateParams.ts +11 -0
- package/src/generated/api/Pinned.ts +7 -0
- package/src/generated/api/ReadOnlyReason.ts +6 -0
- package/src/generated/api/RefusalReason.ts +6 -0
- package/src/generated/api/RpcMethods.ts +30 -0
- package/src/generated/api/RuntimeConfig.ts +24 -0
- package/src/generated/api/SetConfigParams.ts +6 -0
- package/src/generated/api/SetConfigResult.ts +7 -0
- package/src/generated/api/ValidationError.ts +34 -0
- package/src/generated/api/ValidationProblem.ts +7 -0
- package/src/generated/api/WriteStep.ts +6 -0
- package/src/generated/api/index.ts +34 -0
- package/src/identity/IdentityClient.ts +20 -0
- package/src/model/shacl-gen.ts +4 -2
- package/src/neighbourhood/MeshManager.test.ts +57 -0
- package/src/neighbourhood/MeshManager.ts +26 -0
- package/src/neighbourhood/NeighbourhoodProxy.ts +7 -2
- package/src/neighbourhood/Session.test.ts +355 -3
- package/src/neighbourhood/Session.ts +170 -28
- package/src/perspectives/PerspectiveClient.ts +56 -0
- package/src/perspectives/PerspectiveProxy.test.ts +34 -0
- package/src/perspectives/PerspectiveProxy.ts +76 -2
- package/src/perspectives/WakerSubscriptionManager.test.ts +379 -1
- package/src/perspectives/WakerSubscriptionManager.ts +189 -41
- package/src/runtime/RuntimeClient.ts +25 -1
- package/src/shacl/SHACLFlow.test.ts +4 -2
- package/src/shacl/SHACLShape.test.ts +10 -3
- package/src/shacl/SHACLShape.ts +6 -2
|
@@ -496,6 +496,337 @@ describe('WakerSubscriptionManager', () => {
|
|
|
496
496
|
}
|
|
497
497
|
});
|
|
498
498
|
|
|
499
|
+
/**
|
|
500
|
+
* A QuerySubscriptionProxy stand-in for #1735: the mention query (over Mentioning) answers
|
|
501
|
+
* as `mention` says, each text query (over TextContent) as the next of `text` says, the last
|
|
502
|
+
* one for every attempt after. `hang` never answers the handshake, `open` answers at once with
|
|
503
|
+
* `result`, a number answers that many ms after the handshake starts. Every proxy is kept in
|
|
504
|
+
* `made` with when it was made (`at`), its dispose calls (and when, `disposedAt`) and its
|
|
505
|
+
* `onResult` callback.
|
|
506
|
+
*/
|
|
507
|
+
function slowTextProxyClass(
|
|
508
|
+
text: ('hang' | 'open' | number)[],
|
|
509
|
+
mention: 'hang' | 'open' = 'open',
|
|
510
|
+
textResult?: any,
|
|
511
|
+
) {
|
|
512
|
+
const made: {
|
|
513
|
+
path: string;
|
|
514
|
+
at: number;
|
|
515
|
+
disposed: any[];
|
|
516
|
+
disposedAt?: number;
|
|
517
|
+
deliver?: (result: any) => Promise<void>;
|
|
518
|
+
}[] = [];
|
|
519
|
+
let textAttempts = 0;
|
|
520
|
+
const ProxyClass = function (...args: any[]) {
|
|
521
|
+
const path = args[3] === 'ad4m://Mentioning' ? 'mentions' : 'text';
|
|
522
|
+
const behaviour = path === 'mentions' ? mention : text[Math.min(textAttempts++, text.length - 1)];
|
|
523
|
+
const entry: (typeof made)[number] = { path, at: Date.now(), disposed: [] };
|
|
524
|
+
made.push(entry);
|
|
525
|
+
let answered: Promise<void> | undefined;
|
|
526
|
+
const answer = () =>
|
|
527
|
+
(answered ??=
|
|
528
|
+
behaviour === 'open'
|
|
529
|
+
? Promise.resolve()
|
|
530
|
+
: behaviour === 'hang'
|
|
531
|
+
? new Promise<void>(() => {})
|
|
532
|
+
: new Promise<void>((resolve) => setTimeout(resolve, behaviour)));
|
|
533
|
+
return {
|
|
534
|
+
get initialized() { return answer().then(() => true); },
|
|
535
|
+
subscribe: answer,
|
|
536
|
+
dispose: (options?: any) => {
|
|
537
|
+
entry.disposed.push(options);
|
|
538
|
+
entry.disposedAt ??= Date.now();
|
|
539
|
+
},
|
|
540
|
+
result: path === 'text' && behaviour !== 'hang' ? textResult : undefined,
|
|
541
|
+
onResult: (cb: (result: any) => Promise<void>) => { entry.deliver = cb; },
|
|
542
|
+
};
|
|
543
|
+
};
|
|
544
|
+
const texts = () => made.filter((p) => p.path === 'text');
|
|
545
|
+
const mentions = () => made.filter((p) => p.path === 'mentions');
|
|
546
|
+
return { ProxyClass, made, texts, mentions };
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* A manager over {@link slowTextProxyClass} with a 1 s handshake budget and 5 s re-attempts,
|
|
551
|
+
* restored from `previousSeenMessages` when given. `persisted` holds what it last persisted.
|
|
552
|
+
*/
|
|
553
|
+
function slowTextManager(ProxyClass: any, previousSeenMessages?: Record<string, string[]>) {
|
|
554
|
+
const wakes: string[][] = [];
|
|
555
|
+
const warnings: string[] = [];
|
|
556
|
+
const infos: string[] = [];
|
|
557
|
+
const debugs: string[] = [];
|
|
558
|
+
const persisted: { subs: any[]; seen: Record<string, string[]> } = { subs: [], seen: {} };
|
|
559
|
+
const manager = new WakerSubscriptionManager({
|
|
560
|
+
previousSeenMessages,
|
|
561
|
+
onPersist: (subs, seen) => {
|
|
562
|
+
persisted.subs = subs.map((s) => ({ ...s }));
|
|
563
|
+
persisted.seen = seen;
|
|
564
|
+
},
|
|
565
|
+
perspectiveClient: {
|
|
566
|
+
modelQuery: () => Promise.resolve({ instances: [], totalCount: 0 }),
|
|
567
|
+
},
|
|
568
|
+
logger: {
|
|
569
|
+
...noopLogger(),
|
|
570
|
+
info: (msg: string) => { infos.push(msg); },
|
|
571
|
+
warn: (msg: string) => { warnings.push(msg); },
|
|
572
|
+
debug: (msg: string) => { debugs.push(msg); },
|
|
573
|
+
},
|
|
574
|
+
QuerySubscriptionProxy: ProxyClass,
|
|
575
|
+
debounceMs: 10,
|
|
576
|
+
subscribeTimeoutMs: 1000,
|
|
577
|
+
retryPendingMs: 5000,
|
|
578
|
+
maxRetryPendingMs: 20_000,
|
|
579
|
+
onWake: (_sub, _result, mentions) => { wakes.push(mentions!.map((m) => m.address)); },
|
|
580
|
+
});
|
|
581
|
+
return { manager, wakes, warnings, infos, debugs, persisted };
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
describe('a text query slower than the handshake budget (#1735)', () => {
|
|
585
|
+
beforeEach(() => { jest.useFakeTimers(); });
|
|
586
|
+
afterEach(() => { jest.useRealTimers(); });
|
|
587
|
+
|
|
588
|
+
it('listens through the mention query, and disposes the text query it gave up on', async () => {
|
|
589
|
+
const proxies = slowTextProxyClass(['hang']);
|
|
590
|
+
const { manager, wakes, warnings } = slowTextManager(proxies.ProxyClass);
|
|
591
|
+
|
|
592
|
+
const subscribed = manager.subscribe(sub);
|
|
593
|
+
await jest.advanceTimersByTimeAsync(1000);
|
|
594
|
+
await subscribed;
|
|
595
|
+
|
|
596
|
+
expect(manager.getActive().map((s) => s.id)).toEqual([sub.id]);
|
|
597
|
+
expect(manager.getPending()).toHaveLength(0);
|
|
598
|
+
expect(proxies.texts()[0].disposed).toEqual([{ cancelOpening: true }]);
|
|
599
|
+
expect(proxies.mentions()[0].disposed).toEqual([]);
|
|
600
|
+
expect(warnings.filter((w) => w.includes('listening through the mention query only'))).toEqual([
|
|
601
|
+
expect.stringMatching(
|
|
602
|
+
new RegExp(`^\\[waker\\] ${sub.id}: text query failed \\(subscribe handshake timed out after 1000ms\\); listening through the mention query only, re-attempting the text query in \\d+s$`),
|
|
603
|
+
),
|
|
604
|
+
]);
|
|
605
|
+
|
|
606
|
+
await proxies.mentions()[0].deliver!(result(mentionRow('test://m1')));
|
|
607
|
+
await jest.advanceTimersByTimeAsync(10);
|
|
608
|
+
expect(wakes).toEqual([['test://m1']]);
|
|
609
|
+
|
|
610
|
+
manager.disposeAll();
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
it('re-attempts the text query alone, which joins the subscription once it opens, and wakes once per record', async () => {
|
|
614
|
+
// A record the mention query woke on is in the text query's opening result too.
|
|
615
|
+
const proxies = slowTextProxyClass(['hang', 'hang', 'open'], 'open', result(named('test://m1')));
|
|
616
|
+
const { manager, wakes, warnings, infos } = slowTextManager(proxies.ProxyClass);
|
|
617
|
+
|
|
618
|
+
const subscribed = manager.subscribe(sub);
|
|
619
|
+
await jest.advanceTimersByTimeAsync(1000);
|
|
620
|
+
await subscribed;
|
|
621
|
+
await proxies.mentions()[0].deliver!(result(mentionRow('test://m1')));
|
|
622
|
+
await jest.advanceTimersByTimeAsync(10);
|
|
623
|
+
expect(wakes).toEqual([['test://m1']]);
|
|
624
|
+
|
|
625
|
+
// The second attempt starts within 5 s and times out a second later.
|
|
626
|
+
await jest.advanceTimersByTimeAsync(5000 + 1000);
|
|
627
|
+
expect(proxies.texts()).toHaveLength(2);
|
|
628
|
+
expect(proxies.texts()[1].disposed).toEqual([{ cancelOpening: true }]);
|
|
629
|
+
// The third, within 10 s (the wait doubled), opens.
|
|
630
|
+
await jest.advanceTimersByTimeAsync(10_000);
|
|
631
|
+
expect(proxies.texts()).toHaveLength(3);
|
|
632
|
+
expect(proxies.texts()[2].disposed).toEqual([]);
|
|
633
|
+
expect(infos.some((i) => i.includes('text query opened'))).toBe(true);
|
|
634
|
+
// Only the mention query was ever opened again: the subscription was not.
|
|
635
|
+
expect(proxies.mentions()).toHaveLength(1);
|
|
636
|
+
// Warned once, when the subscription opened without it.
|
|
637
|
+
expect(warnings.filter((w) => w.includes('text query failed'))).toHaveLength(1);
|
|
638
|
+
expect(manager.getActive().map((s) => s.id)).toEqual([sub.id]);
|
|
639
|
+
|
|
640
|
+
// Its results wake; a record the mention query woke on does not wake again.
|
|
641
|
+
await proxies.texts()[2].deliver!(result(named('test://m1'), named('test://m2')));
|
|
642
|
+
await jest.advanceTimersByTimeAsync(10);
|
|
643
|
+
expect(wakes).toEqual([['test://m1'], ['test://m2']]);
|
|
644
|
+
|
|
645
|
+
// No more attempts once it joined.
|
|
646
|
+
await jest.advanceTimersByTimeAsync(60_000);
|
|
647
|
+
expect(proxies.texts()).toHaveLength(3);
|
|
648
|
+
|
|
649
|
+
// Disposing the subscription disposes the text query that joined.
|
|
650
|
+
manager.dispose(sub.id);
|
|
651
|
+
expect(proxies.texts()[2].disposed).toEqual([{ cancelOpening: true }]);
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
it('stops re-attempting the text query once disposed, while waiting and while an attempt is in flight', async () => {
|
|
655
|
+
// Disposed while waiting for the next attempt.
|
|
656
|
+
const waiting = slowTextProxyClass(['hang', 'open']);
|
|
657
|
+
const first = slowTextManager(waiting.ProxyClass);
|
|
658
|
+
const subscribed = first.manager.subscribe(sub);
|
|
659
|
+
await jest.advanceTimersByTimeAsync(1000);
|
|
660
|
+
await subscribed;
|
|
661
|
+
first.manager.dispose(sub.id);
|
|
662
|
+
await jest.advanceTimersByTimeAsync(60_000);
|
|
663
|
+
expect(waiting.texts()).toHaveLength(1);
|
|
664
|
+
expect(jest.getTimerCount()).toBe(0);
|
|
665
|
+
|
|
666
|
+
// Disposed during an attempt: the attempt's query is cancelled at once, and never joins.
|
|
667
|
+
const inFlight = slowTextProxyClass(['hang']);
|
|
668
|
+
const second = slowTextManager(inFlight.ProxyClass);
|
|
669
|
+
const again = second.manager.subscribe(sub);
|
|
670
|
+
await jest.advanceTimersByTimeAsync(1000);
|
|
671
|
+
await again;
|
|
672
|
+
// Up to the moment the second attempt starts (within 5 s), which then hangs.
|
|
673
|
+
while (inFlight.texts().length < 2) await jest.advanceTimersByTimeAsync(100);
|
|
674
|
+
expect(inFlight.texts()[1].disposed).toEqual([]);
|
|
675
|
+
second.manager.disposeAll();
|
|
676
|
+
expect(inFlight.texts()[1].disposed).toEqual([{ cancelOpening: true }]);
|
|
677
|
+
await jest.advanceTimersByTimeAsync(60_000);
|
|
678
|
+
expect(inFlight.texts()).toHaveLength(2);
|
|
679
|
+
// Each proxy disposed once, with its opening cancelled: none left behind.
|
|
680
|
+
for (const p of inFlight.made) expect(p.disposed).toEqual([{ cancelOpening: true }]);
|
|
681
|
+
expect(jest.getTimerCount()).toBe(0);
|
|
682
|
+
});
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* Review of #1737 (Lal): restored, with a seen set saved while only the mention query
|
|
686
|
+
* listened, the text query's opening result was read as news, and every old mention found
|
|
687
|
+
* only through its text woke the agent at once.
|
|
688
|
+
*/
|
|
689
|
+
it('reads the text query\'s opening result as history after a restart until it has read it once', async () => {
|
|
690
|
+
// Saved by a version without `textHistoryRead`: the seen set holds a mention link only.
|
|
691
|
+
const saved = { ...sub };
|
|
692
|
+
const proxies = slowTextProxyClass(
|
|
693
|
+
['hang', 'open'],
|
|
694
|
+
'open',
|
|
695
|
+
result(named('test://m1'), named('test://old1'), named('test://old2')),
|
|
696
|
+
);
|
|
697
|
+
const { manager, wakes, persisted } = slowTextManager(proxies.ProxyClass, { [sub.id]: ['test://m1'] });
|
|
698
|
+
|
|
699
|
+
const subscribed = manager.subscribe(saved);
|
|
700
|
+
await jest.advanceTimersByTimeAsync(1000);
|
|
701
|
+
await subscribed;
|
|
702
|
+
expect(persisted.subs.map((s) => [s.id, s.textHistoryRead])).toEqual([[sub.id, undefined]]);
|
|
703
|
+
|
|
704
|
+
// The re-attempt within 5 s opens; its old matches are history.
|
|
705
|
+
await jest.advanceTimersByTimeAsync(5000);
|
|
706
|
+
expect(proxies.texts()).toHaveLength(2);
|
|
707
|
+
expect(proxies.texts()[1].disposed).toEqual([]);
|
|
708
|
+
await jest.advanceTimersByTimeAsync(1000);
|
|
709
|
+
expect(wakes).toEqual([]);
|
|
710
|
+
expect(persisted.subs.map((s) => [s.id, s.textHistoryRead])).toEqual([[sub.id, true]]);
|
|
711
|
+
expect([...persisted.seen[sub.id]].sort()).toEqual(['test://m1', 'test://old1', 'test://old2']);
|
|
712
|
+
// A later match wakes.
|
|
713
|
+
await proxies.texts()[1].deliver!(result(named('test://old1'), named('test://new')));
|
|
714
|
+
await jest.advanceTimersByTimeAsync(10);
|
|
715
|
+
expect(wakes).toEqual([['test://new']]);
|
|
716
|
+
manager.disposeAll();
|
|
717
|
+
|
|
718
|
+
// The next start, from what it persisted: the text query has read its history, so a match
|
|
719
|
+
// written while the waker was down wakes when the text query opens, here on a re-attempt.
|
|
720
|
+
const next = slowTextProxyClass(
|
|
721
|
+
['hang', 'open'],
|
|
722
|
+
'open',
|
|
723
|
+
result(named('test://old1'), named('test://new'), named('test://downtime')),
|
|
724
|
+
);
|
|
725
|
+
const restarted = slowTextManager(next.ProxyClass, persisted.seen);
|
|
726
|
+
const again = restarted.manager.subscribe(persisted.subs[0]);
|
|
727
|
+
await jest.advanceTimersByTimeAsync(1000);
|
|
728
|
+
await again;
|
|
729
|
+
await jest.advanceTimersByTimeAsync(5000 + 10);
|
|
730
|
+
expect(next.texts()).toHaveLength(2);
|
|
731
|
+
expect(restarted.wakes).toEqual([['test://downtime']]);
|
|
732
|
+
restarted.manager.disposeAll();
|
|
733
|
+
});
|
|
734
|
+
|
|
735
|
+
it('gives each text re-attempt twice the handshake budget of the one before, up to maxRetryPendingMs', async () => {
|
|
736
|
+
// Answers after 1.5 budgets: the first re-attempt (1 s) times out, the second (2 s) opens.
|
|
737
|
+
const slow = slowTextProxyClass(['hang', 1500]);
|
|
738
|
+
const first = slowTextManager(slow.ProxyClass);
|
|
739
|
+
const subscribed = first.manager.subscribe(sub);
|
|
740
|
+
await jest.advanceTimersByTimeAsync(1000);
|
|
741
|
+
await subscribed;
|
|
742
|
+
await jest.advanceTimersByTimeAsync(5000 + 1000 + 10_000 + 2000);
|
|
743
|
+
expect(slow.texts()).toHaveLength(3);
|
|
744
|
+
expect(slow.texts()[1].disposed).toEqual([{ cancelOpening: true }]);
|
|
745
|
+
expect(slow.texts()[2].disposed).toEqual([]);
|
|
746
|
+
expect(first.infos.some((i) => i.includes('text query opened'))).toBe(true);
|
|
747
|
+
first.manager.disposeAll();
|
|
748
|
+
|
|
749
|
+
// Never answers: the budget doubles up to maxRetryPendingMs (20 s) and stays there.
|
|
750
|
+
const never = slowTextProxyClass(['hang']);
|
|
751
|
+
const second = slowTextManager(never.ProxyClass);
|
|
752
|
+
const again = second.manager.subscribe(sub);
|
|
753
|
+
await jest.advanceTimersByTimeAsync(1000);
|
|
754
|
+
await again;
|
|
755
|
+
await jest.advanceTimersByTimeAsync(400_000);
|
|
756
|
+
const budgets = second.debugs
|
|
757
|
+
.map((d) => /text query failed again \(subscribe handshake timed out after (\d+)ms\)/.exec(d)?.[1])
|
|
758
|
+
.filter((b) => b !== undefined)
|
|
759
|
+
.map(Number);
|
|
760
|
+
expect(budgets.slice(0, 8)).toEqual([1000, 2000, 4000, 8000, 16_000, 20_000, 20_000, 20_000]);
|
|
761
|
+
// One attempt in flight at a time, each given up with its opening cancelled.
|
|
762
|
+
for (const t of never.texts().slice(0, -1)) expect(t.disposed).toEqual([{ cancelOpening: true }]);
|
|
763
|
+
second.manager.disposeAll();
|
|
764
|
+
});
|
|
765
|
+
|
|
766
|
+
it('waits twice as long before each text re-attempt, up to maxRetryPendingMs', async () => {
|
|
767
|
+
// The longest wait each time, so the waits are exact.
|
|
768
|
+
const random = jest.spyOn(Math, 'random').mockReturnValue(1);
|
|
769
|
+
try {
|
|
770
|
+
const never = slowTextProxyClass(['hang']);
|
|
771
|
+
const { manager } = slowTextManager(never.ProxyClass);
|
|
772
|
+
const subscribed = manager.subscribe(sub);
|
|
773
|
+
await jest.advanceTimersByTimeAsync(1000);
|
|
774
|
+
await subscribed;
|
|
775
|
+
await jest.advanceTimersByTimeAsync(300_000);
|
|
776
|
+
const texts = never.texts();
|
|
777
|
+
// From the moment an attempt is given up on to the start of the next.
|
|
778
|
+
const waits = texts.slice(1).map((t, i) => t.at - texts[i].disposedAt!);
|
|
779
|
+
expect(waits.slice(0, 6)).toEqual([5000, 10_000, 20_000, 20_000, 20_000, 20_000]);
|
|
780
|
+
manager.disposeAll();
|
|
781
|
+
} finally {
|
|
782
|
+
random.mockRestore();
|
|
783
|
+
}
|
|
784
|
+
});
|
|
785
|
+
|
|
786
|
+
it('drops a text re-attempt in flight when the subscription is opened again under its id', async () => {
|
|
787
|
+
// The first re-attempt answers 0.5 s into its handshake, after the subscription is replaced.
|
|
788
|
+
const proxies = slowTextProxyClass(['hang', 500, 'hang']);
|
|
789
|
+
const { manager, infos } = slowTextManager(proxies.ProxyClass);
|
|
790
|
+
const subscribed = manager.subscribe(sub);
|
|
791
|
+
await jest.advanceTimersByTimeAsync(1000);
|
|
792
|
+
await subscribed;
|
|
793
|
+
while (proxies.texts().length < 2) await jest.advanceTimersByTimeAsync(100);
|
|
794
|
+
const inFlight = proxies.texts()[1];
|
|
795
|
+
expect(inFlight.disposed).toEqual([]);
|
|
796
|
+
|
|
797
|
+
const resubscribed = manager.subscribe(sub);
|
|
798
|
+
// Cancelled at once.
|
|
799
|
+
expect(inFlight.disposed).toEqual([{ cancelOpening: true }]);
|
|
800
|
+
await jest.advanceTimersByTimeAsync(1000);
|
|
801
|
+
await resubscribed;
|
|
802
|
+
// It answered meanwhile, and never joined: nothing listens to it.
|
|
803
|
+
expect(inFlight.deliver).toBeUndefined();
|
|
804
|
+
expect(inFlight.disposed).toEqual([{ cancelOpening: true }]);
|
|
805
|
+
expect(infos.some((i) => i.includes('text query opened'))).toBe(false);
|
|
806
|
+
expect(manager.getActive().map((s) => s.id)).toEqual([sub.id]);
|
|
807
|
+
|
|
808
|
+
manager.disposeAll();
|
|
809
|
+
await jest.advanceTimersByTimeAsync(60_000);
|
|
810
|
+
expect(jest.getTimerCount()).toBe(0);
|
|
811
|
+
for (const p of proxies.made) expect(p.disposed).toEqual([{ cancelOpening: true }]);
|
|
812
|
+
});
|
|
813
|
+
|
|
814
|
+
it('fails the subscription, as before, when the mention query does not open', async () => {
|
|
815
|
+
const proxies = slowTextProxyClass(['open'], 'hang');
|
|
816
|
+
const { manager } = slowTextManager(proxies.ProxyClass);
|
|
817
|
+
|
|
818
|
+
const subscribed = manager.subscribe(sub);
|
|
819
|
+
subscribed.catch(() => {});
|
|
820
|
+
await jest.advanceTimersByTimeAsync(1000);
|
|
821
|
+
await expect(subscribed).rejects.toThrow(/subscribe handshake timed out after 1000ms/);
|
|
822
|
+
expect(manager.getActive()).toHaveLength(0);
|
|
823
|
+
expect(manager.getPending().map((s) => s.id)).toEqual([sub.id]);
|
|
824
|
+
for (const p of proxies.made) expect(p.disposed).toEqual([{ cancelOpening: true }]);
|
|
825
|
+
|
|
826
|
+
manager.disposeAll();
|
|
827
|
+
});
|
|
828
|
+
});
|
|
829
|
+
|
|
499
830
|
/**
|
|
500
831
|
* Delivers one new mention (or each of the `deliveries` in turn, {@link toMentions}'s to the mention query) and returns
|
|
501
832
|
* what it woke with, every model query the manager made (`[className, parsed query]`), any warnings and the
|
|
@@ -745,6 +1076,8 @@ describe('WakerSubscriptionManager', () => {
|
|
|
745
1076
|
initial?: any;
|
|
746
1077
|
initialMentions?: any[];
|
|
747
1078
|
previousSeen?: Record<string, string[]>;
|
|
1079
|
+
/** Whether the restored subscription was saved after its text query read its history. */
|
|
1080
|
+
textHistoryRead?: boolean;
|
|
748
1081
|
modelQuery?: (uuid: string, className: string, queryJson: string) => Promise<any>;
|
|
749
1082
|
},
|
|
750
1083
|
) {
|
|
@@ -772,7 +1105,11 @@ describe('WakerSubscriptionManager', () => {
|
|
|
772
1105
|
onPersist: (_subs, seen) => { persistedSeen.push(seen['mention-harness'] ?? []); },
|
|
773
1106
|
previousSeenMessages: opening?.previousSeen,
|
|
774
1107
|
} as any);
|
|
775
|
-
await manager.subscribe({
|
|
1108
|
+
await manager.subscribe({
|
|
1109
|
+
...sub,
|
|
1110
|
+
id: 'mention-harness',
|
|
1111
|
+
...(opening?.textHistoryRead !== undefined ? { textHistoryRead: opening.textHistoryRead } : {}),
|
|
1112
|
+
});
|
|
776
1113
|
const msg = (...ids: string[]) => result(...ids.map(named));
|
|
777
1114
|
return {
|
|
778
1115
|
manager,
|
|
@@ -796,6 +1133,7 @@ describe('WakerSubscriptionManager', () => {
|
|
|
796
1133
|
// The mention query opens first: a record both hold wakes through its mention link.
|
|
797
1134
|
initialMentions: [mentionRow('test://posted'), mentionRow('test://old')],
|
|
798
1135
|
previousSeen: { 'mention-harness': ['test://old'] },
|
|
1136
|
+
textHistoryRead: true,
|
|
799
1137
|
});
|
|
800
1138
|
await waitUntil(() => h.wakes.length > 0, 1000);
|
|
801
1139
|
h.manager.disposeAll();
|
|
@@ -818,6 +1156,46 @@ describe('WakerSubscriptionManager', () => {
|
|
|
818
1156
|
expect(h.wakes.map((w: any) => w.map((m: any) => m.address))).toEqual([['test://new']]);
|
|
819
1157
|
});
|
|
820
1158
|
|
|
1159
|
+
/**
|
|
1160
|
+
* Review of #1737 (CodeRabbit): a result whose parents were still resolving when the
|
|
1161
|
+
* subscription was opened again under its id wrote its seen set over the new one's, and woke.
|
|
1162
|
+
*/
|
|
1163
|
+
it('writes nothing and wakes for nothing when the subscription is replaced during a parent lookup', async () => {
|
|
1164
|
+
let lookups = 0;
|
|
1165
|
+
let resolveFirst: (rows: any) => void = () => {};
|
|
1166
|
+
const delivers: Record<string, (result: any) => Promise<void>> = {};
|
|
1167
|
+
const wakes: string[][] = [];
|
|
1168
|
+
const manager = new WakerSubscriptionManager({
|
|
1169
|
+
perspectiveClient: {
|
|
1170
|
+
modelQuery: () =>
|
|
1171
|
+
++lookups === 1
|
|
1172
|
+
? new Promise((r) => { resolveFirst = r; })
|
|
1173
|
+
: Promise.resolve({ instances: [], totalCount: 0 }),
|
|
1174
|
+
},
|
|
1175
|
+
logger: noopLogger(),
|
|
1176
|
+
QuerySubscriptionProxy: answeringProxyClass(delivers),
|
|
1177
|
+
debounceMs: 10,
|
|
1178
|
+
retryPendingMs: 60_000,
|
|
1179
|
+
onWake: (_sub, _result, mentions) => { wakes.push(mentions!.map((m) => m.address)); },
|
|
1180
|
+
});
|
|
1181
|
+
const replaced = { ...sub, id: 'mention-replaced' };
|
|
1182
|
+
await manager.subscribe(replaced);
|
|
1183
|
+
const stale = delivers.text(result(named('test://a')));
|
|
1184
|
+
await waitUntil(() => lookups === 1, 1000);
|
|
1185
|
+
|
|
1186
|
+
await manager.subscribe(replaced);
|
|
1187
|
+
resolveFirst({ instances: [], totalCount: 0 });
|
|
1188
|
+
await stale;
|
|
1189
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
1190
|
+
expect(wakes).toEqual([]);
|
|
1191
|
+
|
|
1192
|
+
// The new subscription has not seen it: its own result wakes for it.
|
|
1193
|
+
await delivers.text(result(named('test://a')));
|
|
1194
|
+
await waitUntil(() => wakes.length > 0, 1000);
|
|
1195
|
+
expect(wakes).toEqual([['test://a']]);
|
|
1196
|
+
manager.disposeAll();
|
|
1197
|
+
});
|
|
1198
|
+
|
|
821
1199
|
/** Review of #1512 (Lal): B was persisted as seen while its parents were still resolving. */
|
|
822
1200
|
it('does not persist a mention still resolving its parents when an earlier wake lands', async () => {
|
|
823
1201
|
let landA: (delivered: boolean) => void = () => {};
|