@coasys/ad4m 0.13.0-test-model-layer → 0.13.0-test-inverse-relations
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 +432 -46
- package/lib/index.js +430 -47
- package/lib/src/ai/AIClient.d.ts +13 -0
- package/lib/src/ai/AIClient.js +15 -0
- package/lib/src/model/Ad4mModel.js +14 -6
- package/lib/src/model/ModelQueryBuilder.js +13 -3
- package/lib/src/model/query-common.d.ts +10 -1
- package/lib/src/model/query-common.js +16 -0
- package/lib/src/model/query-sparql.js +25 -1
- package/lib/src/model/shacl-gen.js +12 -0
- package/lib/src/model/types.d.ts +91 -1
- package/lib/src/model/types.js +9 -0
- package/lib/src/perspectives/AutoProcessor.d.ts +8 -0
- package/lib/src/perspectives/FlowInstance.d.ts +30 -12
- package/lib/src/perspectives/FlowInstance.js +36 -13
- package/lib/src/perspectives/FlowModels.d.ts +7 -1
- package/lib/src/perspectives/FlowModels.js +8 -2
- package/lib/src/perspectives/PerspectiveClient.d.ts +16 -0
- package/lib/src/perspectives/PerspectiveClient.js +12 -0
- package/lib/src/perspectives/PerspectiveProxy.d.ts +31 -3
- package/lib/src/perspectives/PerspectiveProxy.js +78 -2
- package/lib/src/perspectives/WakerSubscriptionManager.d.ts +55 -5
- package/lib/src/perspectives/WakerSubscriptionManager.js +183 -8
- package/package.json +1 -1
- package/src/ai/AIClient.ts +16 -0
- package/src/model/Ad4mModel.test.ts +79 -0
- package/src/model/Ad4mModel.ts +14 -6
- package/src/model/ModelQueryBuilder.ts +13 -3
- package/src/model/query-common.ts +20 -1
- package/src/model/query-sparql.ts +28 -1
- package/src/model/shacl-gen.ts +12 -0
- package/src/model/shacl-roundtrip.test.ts +71 -0
- package/src/model/types.ts +96 -1
- package/src/perspectives/AutoProcessor.ts +8 -0
- package/src/perspectives/FlowInstance.ts +39 -13
- package/src/perspectives/FlowModels.ts +8 -2
- package/src/perspectives/PerspectiveClient.ts +28 -0
- package/src/perspectives/PerspectiveProxy.test.ts +154 -0
- package/src/perspectives/PerspectiveProxy.ts +84 -5
- package/src/perspectives/WakerSubscriptionManager.test.ts +199 -0
- package/src/perspectives/WakerSubscriptionManager.ts +197 -9
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { PerspectiveProxy, QuerySubscriptionProxy } from './PerspectiveProxy';
|
|
2
|
+
import { Link, LinkExpression } from '../links/Links';
|
|
2
3
|
|
|
3
4
|
function createMockPerspectiveClient(): any {
|
|
4
5
|
return {
|
|
@@ -529,3 +530,156 @@ describe('QuerySubscriptionProxy', () => {
|
|
|
529
530
|
expect(liveCallbacks).toBe(0);
|
|
530
531
|
});
|
|
531
532
|
});
|
|
533
|
+
|
|
534
|
+
describe('PerspectiveProxy.subjectClassTargetClasses', () => {
|
|
535
|
+
function mockLink(source: string) {
|
|
536
|
+
return { data: { source, predicate: 'rdf://type', target: 'ad4m://SubjectClass' } };
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function proxyWithLinks(links: any[]): PerspectiveProxy {
|
|
540
|
+
const mockClient: any = {
|
|
541
|
+
...createMockPerspectiveClient(),
|
|
542
|
+
queryLinks: jest.fn().mockResolvedValue(links),
|
|
543
|
+
};
|
|
544
|
+
return createProxy(mockClient);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
it('returns full URIs, not stripped names', async () => {
|
|
548
|
+
const proxy = proxyWithLinks([
|
|
549
|
+
mockLink('we://Space'),
|
|
550
|
+
mockLink('flux://Channel'),
|
|
551
|
+
mockLink('recipe://Recipe'),
|
|
552
|
+
]);
|
|
553
|
+
|
|
554
|
+
const result = await proxy.subjectClassTargetClasses();
|
|
555
|
+
|
|
556
|
+
expect(result).toContain('we://Space');
|
|
557
|
+
expect(result).toContain('flux://Channel');
|
|
558
|
+
expect(result).toContain('recipe://Recipe');
|
|
559
|
+
expect(result).toHaveLength(3);
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
it('deduplicates URIs', async () => {
|
|
563
|
+
const proxy = proxyWithLinks([
|
|
564
|
+
mockLink('we://Space'),
|
|
565
|
+
mockLink('we://Space'),
|
|
566
|
+
]);
|
|
567
|
+
|
|
568
|
+
expect(await proxy.subjectClassTargetClasses()).toEqual(['we://Space']);
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
it('filters out empty sources', async () => {
|
|
572
|
+
const proxy = proxyWithLinks([
|
|
573
|
+
mockLink('we://Space'),
|
|
574
|
+
mockLink(''),
|
|
575
|
+
]);
|
|
576
|
+
|
|
577
|
+
expect(await proxy.subjectClassTargetClasses()).toEqual(['we://Space']);
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
it('returns an empty array when no classes are registered', async () => {
|
|
581
|
+
const proxy = proxyWithLinks([]);
|
|
582
|
+
expect(await proxy.subjectClassTargetClasses()).toEqual([]);
|
|
583
|
+
});
|
|
584
|
+
|
|
585
|
+
it('returns an empty array on error', async () => {
|
|
586
|
+
const mockClient: any = {
|
|
587
|
+
...createMockPerspectiveClient(),
|
|
588
|
+
queryLinks: jest.fn().mockRejectedValue(new Error('network error')),
|
|
589
|
+
};
|
|
590
|
+
const proxy = createProxy(mockClient);
|
|
591
|
+
|
|
592
|
+
expect(await proxy.subjectClassTargetClasses()).toEqual([]);
|
|
593
|
+
});
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
// ── fix #1008: PerspectiveProxy.remove accepts bare Link ────────────────────
|
|
597
|
+
//
|
|
598
|
+
// PerspectiveClient.removeLink does `delete link.data.__typename` which throws
|
|
599
|
+
// when a bare Link (no .data) is passed. The fix resolves the Link to its stored
|
|
600
|
+
// LinkExpression first, so remove(new Link({...})) must work without error.
|
|
601
|
+
describe('PerspectiveProxy.remove with bare Link', () => {
|
|
602
|
+
function makeStoredExpression(source: string, predicate: string, target: string): LinkExpression {
|
|
603
|
+
const expr = new LinkExpression();
|
|
604
|
+
expr.author = 'did:test:agent';
|
|
605
|
+
expr.timestamp = '2026-01-01T00:00:00Z';
|
|
606
|
+
expr.data = new Link({ source, predicate, target });
|
|
607
|
+
expr.proof = { valid: true, invalid: false, signature: 'sig', key: 'key' } as any;
|
|
608
|
+
return expr;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
it('resolves a bare Link to the stored expression and removes it', async () => {
|
|
612
|
+
const storedExpr = makeStoredExpression('s://a', 'p://b', 't://c');
|
|
613
|
+
const removeLink = jest.fn().mockResolvedValue(true);
|
|
614
|
+
const mockClient: any = {
|
|
615
|
+
...createMockPerspectiveClient(),
|
|
616
|
+
// queryLinks is what PerspectiveProxy.get calls
|
|
617
|
+
queryLinks: jest.fn().mockResolvedValue([storedExpr]),
|
|
618
|
+
removeLink,
|
|
619
|
+
};
|
|
620
|
+
const proxy = createProxy(mockClient);
|
|
621
|
+
|
|
622
|
+
const result = await proxy.remove(new Link({ source: 's://a', predicate: 'p://b', target: 't://c' }));
|
|
623
|
+
|
|
624
|
+
expect(result).toBe(true);
|
|
625
|
+
// removeLink must have been called with the resolved expression, not the bare Link
|
|
626
|
+
expect(removeLink).toHaveBeenCalledWith('test-uuid', storedExpr, undefined);
|
|
627
|
+
});
|
|
628
|
+
|
|
629
|
+
it('throws a descriptive error when no stored expression matches the bare Link', async () => {
|
|
630
|
+
const mockClient: any = {
|
|
631
|
+
...createMockPerspectiveClient(),
|
|
632
|
+
queryLinks: jest.fn().mockResolvedValue([]),
|
|
633
|
+
};
|
|
634
|
+
const proxy = createProxy(mockClient);
|
|
635
|
+
|
|
636
|
+
await expect(
|
|
637
|
+
proxy.remove(new Link({ source: 'missing://src', predicate: 'p://pred', target: 'missing://tgt' }))
|
|
638
|
+
).rejects.toThrow('PerspectiveProxy.remove: no stored LinkExpression matches');
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
// Lal's #1011 review: the bare-Link resolution used `bare.predicate ||
|
|
642
|
+
// undefined`, and the Link constructor coerces a missing predicate to "" —
|
|
643
|
+
// so the predicate filter was silently dropped and matches[0] removed an
|
|
644
|
+
// arbitrary source→target link under a different predicate.
|
|
645
|
+
it('matches only predicate-less stored links when the bare Link has no predicate', async () => {
|
|
646
|
+
const withPredicate = makeStoredExpression('s://a', 'p://b', 't://c');
|
|
647
|
+
const withoutPredicate = makeStoredExpression('s://a', '', 't://c');
|
|
648
|
+
// the store reports a missing predicate as null, not ''
|
|
649
|
+
(withoutPredicate.data as any).predicate = null;
|
|
650
|
+
const removeLink = jest.fn().mockResolvedValue(true);
|
|
651
|
+
// the wrong candidate first: matches[0] of the unfiltered result
|
|
652
|
+
const queryLinks = jest.fn().mockResolvedValue([withPredicate, withoutPredicate]);
|
|
653
|
+
const mockClient: any = { ...createMockPerspectiveClient(), queryLinks, removeLink };
|
|
654
|
+
const proxy = createProxy(mockClient);
|
|
655
|
+
|
|
656
|
+
await proxy.remove(new Link({ source: 's://a', target: 't://c' }));
|
|
657
|
+
|
|
658
|
+
expect(removeLink).toHaveBeenCalledWith('test-uuid', withoutPredicate, undefined);
|
|
659
|
+
});
|
|
660
|
+
|
|
661
|
+
it('throws instead of removing a predicated link when the bare Link has no predicate', async () => {
|
|
662
|
+
const withPredicate = makeStoredExpression('s://a', 'p://b', 't://c');
|
|
663
|
+
const queryLinks = jest.fn().mockResolvedValue([withPredicate]);
|
|
664
|
+
const mockClient: any = { ...createMockPerspectiveClient(), queryLinks };
|
|
665
|
+
const proxy = createProxy(mockClient);
|
|
666
|
+
|
|
667
|
+
await expect(
|
|
668
|
+
proxy.remove(new Link({ source: 's://a', target: 't://c' }))
|
|
669
|
+
).rejects.toThrow('no stored LinkExpression matches');
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
it('passes a full LinkExpressionInput through unchanged', async () => {
|
|
673
|
+
const storedExpr = makeStoredExpression('s://x', 'p://y', 't://z');
|
|
674
|
+
const removeLink = jest.fn().mockResolvedValue(true);
|
|
675
|
+
const mockClient: any = {
|
|
676
|
+
...createMockPerspectiveClient(),
|
|
677
|
+
removeLink,
|
|
678
|
+
};
|
|
679
|
+
const proxy = createProxy(mockClient);
|
|
680
|
+
|
|
681
|
+
await proxy.remove(storedExpr as any);
|
|
682
|
+
// Should NOT have called queryLinks — no bare Link resolution needed
|
|
683
|
+
expect(removeLink).toHaveBeenCalledWith('test-uuid', storedExpr, undefined);
|
|
684
|
+
});
|
|
685
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LinkCallback, PerspectiveClient, SyncStateChangeCallback } from "./PerspectiveClient";
|
|
1
|
+
import { LinkCallback, PerspectiveClient, SyncStateChangeCallback, FlowFireOutcome } from "./PerspectiveClient";
|
|
2
2
|
import { CallOptions } from "../apiClient";
|
|
3
3
|
import { Link, LinkExpression, LinkExpressionInput, LinkExpressionMutations, LinkMutations } from "../links/Links";
|
|
4
4
|
import { LinkQuery } from "./LinkQuery";
|
|
@@ -868,6 +868,15 @@ export class PerspectiveProxy {
|
|
|
868
868
|
return await this.#client.rejectInterpretation(this.#handle.uuid, base, property)
|
|
869
869
|
}
|
|
870
870
|
|
|
871
|
+
async acceptFlowProposal(proposalUri: string): Promise<FlowFireOutcome[]> {
|
|
872
|
+
return await this.#client.acceptFlowProposal(this.#handle.uuid, proposalUri)
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
/** Withdraw our own links from a proposal; resolves to how many went. */
|
|
876
|
+
async rejectFlowProposal(proposalUri: string): Promise<number> {
|
|
877
|
+
return await this.#client.rejectFlowProposal(this.#handle.uuid, proposalUri)
|
|
878
|
+
}
|
|
879
|
+
|
|
871
880
|
/** Subscribe to this perspective's auto-processor step signals. */
|
|
872
881
|
async addAutoProcessorEventListener(cb: (event: AutoProcessorEvent) => void): Promise<void> {
|
|
873
882
|
return await this.#client.addAutoProcessorEventListener(this.#handle.uuid, cb)
|
|
@@ -1131,12 +1140,50 @@ export class PerspectiveProxy {
|
|
|
1131
1140
|
|
|
1132
1141
|
/**
|
|
1133
1142
|
* Removes a link from the perspective.
|
|
1134
|
-
*
|
|
1135
|
-
*
|
|
1143
|
+
*
|
|
1144
|
+
* Accepts either a full `LinkExpressionInput` (as returned by `add`) or a bare
|
|
1145
|
+
* `Link` (source/predicate/target only). When a bare Link is passed the method
|
|
1146
|
+
* resolves it to the first stored `LinkExpression` whose source, predicate, and
|
|
1147
|
+
* target match, then removes that expression. A bare Link without a predicate
|
|
1148
|
+
* (or with the constructor's `""` stand-in) matches only stored links that
|
|
1149
|
+
* themselves have no predicate. If no match is found an error is thrown
|
|
1150
|
+
* naming the link so the caller knows what was expected.
|
|
1151
|
+
*
|
|
1152
|
+
* @param link - The link to remove (LinkExpressionInput or bare Link)
|
|
1136
1153
|
* @param batchId - Optional batch ID to group this operation with others
|
|
1137
1154
|
*/
|
|
1138
|
-
async remove(link: LinkExpressionInput, batchId?: string): Promise<boolean> {
|
|
1139
|
-
|
|
1155
|
+
async remove(link: LinkExpressionInput | Link, batchId?: string): Promise<boolean> {
|
|
1156
|
+
let resolvedLink: LinkExpressionInput;
|
|
1157
|
+
if (!('data' in link) || (link as any).data === undefined) {
|
|
1158
|
+
// bare Link — resolve to stored expression
|
|
1159
|
+
const bare = link as Link;
|
|
1160
|
+
// The Link constructor coerces an absent predicate to "" and the
|
|
1161
|
+
// store reports predicate-less links as predicate null — so ""
|
|
1162
|
+
// and undefined both mean "no predicate" here, and neither may
|
|
1163
|
+
// widen the query: dropping the filter would resolve (and
|
|
1164
|
+
// remove!) an arbitrary source→target link under a *different*
|
|
1165
|
+
// predicate. LinkQuery cannot express "predicate is absent", so
|
|
1166
|
+
// in that case we query on source/target only and require the
|
|
1167
|
+
// absence ourselves.
|
|
1168
|
+
const predicateGiven = bare.predicate !== undefined && bare.predicate !== '';
|
|
1169
|
+
const candidates = await this.get(new LinkQuery({
|
|
1170
|
+
source: bare.source || undefined,
|
|
1171
|
+
predicate: predicateGiven ? bare.predicate : undefined,
|
|
1172
|
+
target: bare.target || undefined,
|
|
1173
|
+
}));
|
|
1174
|
+
const matches = predicateGiven
|
|
1175
|
+
? candidates
|
|
1176
|
+
: candidates.filter(m => !m.data.predicate);
|
|
1177
|
+
if (matches.length === 0) {
|
|
1178
|
+
throw new Error(
|
|
1179
|
+
`PerspectiveProxy.remove: no stored LinkExpression matches Link { source: "${bare.source}", predicate: "${bare.predicate}", target: "${bare.target}" }`
|
|
1180
|
+
);
|
|
1181
|
+
}
|
|
1182
|
+
resolvedLink = matches[0] as unknown as LinkExpressionInput;
|
|
1183
|
+
} else {
|
|
1184
|
+
resolvedLink = link as LinkExpressionInput;
|
|
1185
|
+
}
|
|
1186
|
+
const result = await this.#client.removeLink(this.#handle.uuid, resolvedLink, batchId)
|
|
1140
1187
|
invalidatePerspectiveCache(this.#handle.uuid);
|
|
1141
1188
|
return result;
|
|
1142
1189
|
}
|
|
@@ -1767,6 +1814,38 @@ export class PerspectiveProxy {
|
|
|
1767
1814
|
}
|
|
1768
1815
|
}
|
|
1769
1816
|
|
|
1817
|
+
/**
|
|
1818
|
+
* Return the full target-class URIs of every registered SubjectClass.
|
|
1819
|
+
*
|
|
1820
|
+
* Unlike {@link subjectClasses}, which strips the namespace and returns bare
|
|
1821
|
+
* names (`"Space"`, `"Channel"`), this method returns the complete URIs
|
|
1822
|
+
* (`"we://Space"`, `"flux://Channel"`). The full URI is the key that
|
|
1823
|
+
* `load_shape` resolves against, so it is the only collision-safe identifier
|
|
1824
|
+
* — two apps can both declare a class called `"Template"` under different
|
|
1825
|
+
* namespaces, and the bare name cannot distinguish them.
|
|
1826
|
+
*
|
|
1827
|
+
* Callers that need to check whether a *set* of models are already
|
|
1828
|
+
* registered should call this once and test membership against the returned
|
|
1829
|
+
* set, rather than issuing one `queryLinks` per model.
|
|
1830
|
+
*
|
|
1831
|
+
* One `queryLinks` round trip, no executor-side changes.
|
|
1832
|
+
*/
|
|
1833
|
+
async subjectClassTargetClasses(): Promise<string[]> {
|
|
1834
|
+
try {
|
|
1835
|
+
const classLinks = await this.get(new LinkQuery({
|
|
1836
|
+
predicate: "rdf://type",
|
|
1837
|
+
target: "ad4m://SubjectClass"
|
|
1838
|
+
}));
|
|
1839
|
+
const uris = classLinks
|
|
1840
|
+
.map(l => l.data.source)
|
|
1841
|
+
.filter(source => source.length > 0);
|
|
1842
|
+
return [...new Set(uris)];
|
|
1843
|
+
} catch (e) {
|
|
1844
|
+
console.warn('subjectClassTargetClasses: lookup failed:', e);
|
|
1845
|
+
return [];
|
|
1846
|
+
}
|
|
1847
|
+
}
|
|
1848
|
+
|
|
1770
1849
|
async stringOrTemplateObjectToSubjectClassName<T>(subjectClass: T): Promise<string> {
|
|
1771
1850
|
if(typeof subjectClass === "string")
|
|
1772
1851
|
return subjectClass
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { WakerSubscriptionManager, hintFor } from './WakerSubscriptionManager';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Regression guard for the fork that used to exist between this file and
|
|
5
|
+
* plugins/ad4m/wakerSubscriptionManager.ts: the plugin copy learned to report
|
|
6
|
+
* a rejected subscription and keep re-attempting it, while this copy — the one
|
|
7
|
+
* `@coasys/ad4m` actually exports — still swallowed the failure and returned as
|
|
8
|
+
* if the subscription were live. The plugin now re-exports this module, so
|
|
9
|
+
* these tests cover both.
|
|
10
|
+
*/
|
|
11
|
+
describe('WakerSubscriptionManager', () => {
|
|
12
|
+
const noopLogger = () => ({
|
|
13
|
+
info: () => {},
|
|
14
|
+
warn: () => {},
|
|
15
|
+
error: () => {},
|
|
16
|
+
debug: () => {},
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const perspectiveClient = {
|
|
20
|
+
querySparql: () => Promise.resolve({ results: { bindings: [] } }),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const sub = {
|
|
24
|
+
id: 'mention-locked',
|
|
25
|
+
type: 'mention' as const,
|
|
26
|
+
perspective: 'fake-uuid',
|
|
27
|
+
channel: '',
|
|
28
|
+
query: 'SELECT * WHERE { ?s ?p ?o }',
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** A QuerySubscriptionProxy stand-in whose registration the executor rejects. */
|
|
32
|
+
function rejectingProxyClass(error: Error, disposed: { count: number }) {
|
|
33
|
+
return function () {
|
|
34
|
+
return {
|
|
35
|
+
initialized: Promise.reject(error),
|
|
36
|
+
subscribe: () => Promise.reject(error),
|
|
37
|
+
dispose: () => {
|
|
38
|
+
disposed.count += 1;
|
|
39
|
+
},
|
|
40
|
+
onResult: () => {},
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
it('rejects when the executor refuses the subscription, and keeps it pending', async () => {
|
|
46
|
+
const disposed = { count: 0 };
|
|
47
|
+
const persisted: { subs: any[] } = { subs: [{ placeholder: true }] };
|
|
48
|
+
const manager = new WakerSubscriptionManager({
|
|
49
|
+
perspectiveClient,
|
|
50
|
+
logger: noopLogger(),
|
|
51
|
+
QuerySubscriptionProxy: rejectingProxyClass(
|
|
52
|
+
new Error('RPC error 403: main key not found'),
|
|
53
|
+
disposed,
|
|
54
|
+
),
|
|
55
|
+
debounceMs: 10,
|
|
56
|
+
// Stay pending for the assertion instead of racing a re-attempt.
|
|
57
|
+
retryPendingMs: 60_000,
|
|
58
|
+
onWake: () => {},
|
|
59
|
+
onPersist: (subs) => {
|
|
60
|
+
persisted.subs = subs;
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Must not resolve: a silent resolve is what let the subscribe tools reply
|
|
65
|
+
// "Subscribed..." to a subscription that was never registered.
|
|
66
|
+
await expect(manager.subscribe(sub)).rejects.toThrow(/main key not found/);
|
|
67
|
+
|
|
68
|
+
expect(manager.has(sub.id)).toBe(false);
|
|
69
|
+
expect(manager.getActive()).toHaveLength(0);
|
|
70
|
+
expect(persisted.subs).toHaveLength(0);
|
|
71
|
+
expect(disposed.count).toBeGreaterThan(0);
|
|
72
|
+
// ...but it is enrolled for re-attempt, because the caller asked for it.
|
|
73
|
+
expect(manager.getPending().map((s) => s.id)).toEqual([sub.id]);
|
|
74
|
+
|
|
75
|
+
manager.disposeAll();
|
|
76
|
+
expect(manager.getPending()).toHaveLength(0);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A QuerySubscriptionProxy stand-in whose handshake never answers: the
|
|
81
|
+
* executor accepted the socket but nothing ever comes back (#1016).
|
|
82
|
+
*/
|
|
83
|
+
function hangingProxyClass(
|
|
84
|
+
disposed: { count: number },
|
|
85
|
+
hangOn: 'subscribe' | 'initialized' = 'subscribe',
|
|
86
|
+
) {
|
|
87
|
+
return function () {
|
|
88
|
+
return {
|
|
89
|
+
initialized:
|
|
90
|
+
hangOn === 'initialized' ? new Promise<boolean>(() => {}) : Promise.resolve(true),
|
|
91
|
+
subscribe: () =>
|
|
92
|
+
hangOn === 'subscribe' ? new Promise<void>(() => {}) : Promise.resolve(),
|
|
93
|
+
dispose: () => {
|
|
94
|
+
disposed.count += 1;
|
|
95
|
+
},
|
|
96
|
+
onResult: () => {},
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function waitUntil(cond: () => boolean, ms = 2000): Promise<void> {
|
|
102
|
+
const deadline = Date.now() + ms;
|
|
103
|
+
while (!cond() && Date.now() < deadline) {
|
|
104
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
it('times out a subscribe handshake the executor never answers, and keeps it pending', async () => {
|
|
109
|
+
const disposed = { count: 0 };
|
|
110
|
+
const manager = new WakerSubscriptionManager({
|
|
111
|
+
perspectiveClient,
|
|
112
|
+
logger: noopLogger(),
|
|
113
|
+
QuerySubscriptionProxy: hangingProxyClass(disposed),
|
|
114
|
+
debounceMs: 10,
|
|
115
|
+
subscribeTimeoutMs: 50,
|
|
116
|
+
retryPendingMs: 60_000,
|
|
117
|
+
onWake: () => {},
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// Before the deadline existed this await never settled: the subscription
|
|
121
|
+
// was recorded neither as active nor as pending, so the 30s re-attempt
|
|
122
|
+
// never saw it and the caller's tool invocation died at the harness
|
|
123
|
+
// timeout instead (#1016).
|
|
124
|
+
await expect(manager.subscribe(sub)).rejects.toThrow(/timed out after 50ms/);
|
|
125
|
+
|
|
126
|
+
expect(manager.has(sub.id)).toBe(false);
|
|
127
|
+
expect(manager.getActive()).toHaveLength(0);
|
|
128
|
+
expect(manager.getPending().map((s) => s.id)).toEqual([sub.id]);
|
|
129
|
+
// The proxy is dropped so a late answer backs out instead of registering.
|
|
130
|
+
expect(disposed.count).toBeGreaterThan(0);
|
|
131
|
+
|
|
132
|
+
manager.disposeAll();
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('times out when subscribe() answers but initialized never settles', async () => {
|
|
136
|
+
const disposed = { count: 0 };
|
|
137
|
+
const manager = new WakerSubscriptionManager({
|
|
138
|
+
perspectiveClient,
|
|
139
|
+
logger: noopLogger(),
|
|
140
|
+
QuerySubscriptionProxy: hangingProxyClass(disposed, 'initialized'),
|
|
141
|
+
debounceMs: 10,
|
|
142
|
+
subscribeTimeoutMs: 50,
|
|
143
|
+
retryPendingMs: 60_000,
|
|
144
|
+
onWake: () => {},
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
await expect(manager.subscribe(sub)).rejects.toThrow(/timed out after 50ms/);
|
|
148
|
+
expect(manager.getActive()).toHaveLength(0);
|
|
149
|
+
expect(manager.getPending().map((s) => s.id)).toEqual([sub.id]);
|
|
150
|
+
|
|
151
|
+
manager.disposeAll();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('re-attempts a timed-out subscription and enrolls it once the executor answers', async () => {
|
|
155
|
+
let attempt = 0;
|
|
156
|
+
const ProxyClass = function () {
|
|
157
|
+
attempt += 1;
|
|
158
|
+
const answers = attempt >= 2;
|
|
159
|
+
return {
|
|
160
|
+
initialized: Promise.resolve(true),
|
|
161
|
+
subscribe: () => (answers ? Promise.resolve() : new Promise<void>(() => {})),
|
|
162
|
+
dispose: () => {},
|
|
163
|
+
onResult: () => {},
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
const manager = new WakerSubscriptionManager({
|
|
167
|
+
perspectiveClient,
|
|
168
|
+
logger: noopLogger(),
|
|
169
|
+
QuerySubscriptionProxy: ProxyClass,
|
|
170
|
+
debounceMs: 10,
|
|
171
|
+
subscribeTimeoutMs: 30,
|
|
172
|
+
retryPendingMs: 30,
|
|
173
|
+
onWake: () => {},
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
await expect(manager.subscribe(sub)).rejects.toThrow(/re-attempting every/);
|
|
177
|
+
expect(manager.getPending()).toHaveLength(1);
|
|
178
|
+
|
|
179
|
+
await waitUntil(() => manager.has(sub.id));
|
|
180
|
+
expect(manager.has(sub.id)).toBe(true);
|
|
181
|
+
expect(manager.getPending()).toHaveLength(0);
|
|
182
|
+
expect(attempt).toBe(2);
|
|
183
|
+
|
|
184
|
+
manager.disposeAll();
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('names the operator as the fix for a locked executor, on both message shapes', () => {
|
|
188
|
+
// Current executor message (post-#973) and the older internal error must
|
|
189
|
+
// both map to the same hint, and the hint must point at the node operator —
|
|
190
|
+
// a remote agent cannot unlock someone else's node.
|
|
191
|
+
const current = hintFor('RPC error 403: Executor is locked: the wallet has not been unlocked since the last restart.');
|
|
192
|
+
const legacy = hintFor('RPC error 403: main key not found');
|
|
193
|
+
for (const hint of [current, legacy]) {
|
|
194
|
+
expect(hint).toContain('executor is locked');
|
|
195
|
+
expect(hint).toContain('operator');
|
|
196
|
+
}
|
|
197
|
+
expect(hintFor('connection refused')).toBe('');
|
|
198
|
+
});
|
|
199
|
+
});
|