@zackbart/connecta 0.4.1 → 0.6.0

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 (85) hide show
  1. package/CHANGELOG.md +527 -0
  2. package/README.md +83 -7
  3. package/SECURITY.md +10 -6
  4. package/dist/activity.d.ts +8 -0
  5. package/dist/activity.d.ts.map +1 -1
  6. package/dist/activity.js +1 -0
  7. package/dist/activity.js.map +1 -1
  8. package/dist/auth/bearer.d.ts +10 -3
  9. package/dist/auth/bearer.d.ts.map +1 -1
  10. package/dist/auth/bearer.js +21 -0
  11. package/dist/auth/bearer.js.map +1 -1
  12. package/dist/auth/clerk.d.ts +26 -1
  13. package/dist/auth/clerk.d.ts.map +1 -1
  14. package/dist/auth/clerk.js +161 -4
  15. package/dist/auth/clerk.js.map +1 -1
  16. package/dist/connectors/api.d.ts +13 -0
  17. package/dist/connectors/api.d.ts.map +1 -1
  18. package/dist/connectors/api.js +2 -0
  19. package/dist/connectors/api.js.map +1 -1
  20. package/dist/connectors/remote-mcp.d.ts +13 -0
  21. package/dist/connectors/remote-mcp.d.ts.map +1 -1
  22. package/dist/connectors/remote-mcp.js +10 -0
  23. package/dist/connectors/remote-mcp.js.map +1 -1
  24. package/dist/credential-health.d.ts +212 -0
  25. package/dist/credential-health.d.ts.map +1 -0
  26. package/dist/credential-health.js +535 -0
  27. package/dist/credential-health.js.map +1 -0
  28. package/dist/execute.d.ts +4 -4
  29. package/dist/execute.d.ts.map +1 -1
  30. package/dist/execute.js +16 -4
  31. package/dist/execute.js.map +1 -1
  32. package/dist/index.d.ts +77 -2
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +112 -2
  35. package/dist/index.js.map +1 -1
  36. package/dist/meta-tools.d.ts +76 -7
  37. package/dist/meta-tools.d.ts.map +1 -1
  38. package/dist/meta-tools.js +328 -98
  39. package/dist/meta-tools.js.map +1 -1
  40. package/dist/registry.d.ts +245 -2
  41. package/dist/registry.d.ts.map +1 -1
  42. package/dist/registry.js +377 -27
  43. package/dist/registry.js.map +1 -1
  44. package/dist/server.d.ts +7 -1
  45. package/dist/server.d.ts.map +1 -1
  46. package/dist/server.js +342 -27
  47. package/dist/server.js.map +1 -1
  48. package/dist/skills.d.ts +53 -2
  49. package/dist/skills.d.ts.map +1 -1
  50. package/dist/skills.js +162 -2
  51. package/dist/skills.js.map +1 -1
  52. package/dist/timeout.d.ts +16 -0
  53. package/dist/timeout.d.ts.map +1 -0
  54. package/dist/timeout.js +38 -0
  55. package/dist/timeout.js.map +1 -0
  56. package/dist/toolkits.d.ts +138 -0
  57. package/dist/toolkits.d.ts.map +1 -0
  58. package/dist/toolkits.js +319 -0
  59. package/dist/toolkits.js.map +1 -0
  60. package/dist/types.d.ts +90 -1
  61. package/dist/types.d.ts.map +1 -1
  62. package/dist/ui.d.ts +63 -0
  63. package/dist/ui.d.ts.map +1 -1
  64. package/dist/ui.js +176 -11
  65. package/dist/ui.js.map +1 -1
  66. package/dist/version.d.ts +1 -1
  67. package/dist/version.js +1 -1
  68. package/package.json +5 -2
  69. package/src/activity.ts +9 -0
  70. package/src/auth/bearer.ts +35 -1
  71. package/src/auth/clerk.ts +202 -5
  72. package/src/connectors/api.ts +15 -0
  73. package/src/connectors/remote-mcp.ts +24 -0
  74. package/src/credential-health.ts +736 -0
  75. package/src/execute.ts +32 -8
  76. package/src/index.ts +226 -2
  77. package/src/meta-tools.ts +397 -119
  78. package/src/registry.ts +540 -29
  79. package/src/server.ts +431 -25
  80. package/src/skills.ts +185 -2
  81. package/src/timeout.ts +49 -0
  82. package/src/toolkits.ts +450 -0
  83. package/src/types.ts +96 -2
  84. package/src/ui.ts +190 -11
  85. package/src/version.ts +1 -1
package/src/registry.ts CHANGED
@@ -7,11 +7,60 @@ import type {
7
7
  ToolDef,
8
8
  } from "./types.js";
9
9
  import type { CredentialVault } from "./credentials.js";
10
+ import {
11
+ CredentialHealthChecker,
12
+ type CredentialCheckOptions,
13
+ type CredentialCheckResult,
14
+ type CredentialHealthConfig,
15
+ type CredentialHealthRecord,
16
+ } from "./credential-health.js";
17
+ import { splitAddress, type Toolkit } from "./toolkits.js";
10
18
 
11
19
  const ID_RE = /^[a-z0-9_-]+$/;
12
20
  const DEFAULT_TTL_SECONDS = 300;
13
21
  const DEFAULT_STALE_SECONDS = 3600;
14
- const DEFAULT_MAX_RESULT_BYTES = 50_000;
22
+ export const DEFAULT_MAX_RESULT_BYTES = 50_000;
23
+
24
+ /**
25
+ * Smallest accepted inline-result cap. One byte is pathological but harmless:
26
+ * `alignEndToCharBoundary` widens a window narrower than the codepoint at the
27
+ * offset, so even a 1-byte cap still truncates sanely and still pages. Caps
28
+ * that small already ship in the test suite (4 and 5), so the floor is placed
29
+ * where it excludes only values that are *broken* rather than merely tiny.
30
+ */
31
+ export const MIN_MAX_RESULT_BYTES = 1;
32
+
33
+ /**
34
+ * The one definition of a usable `maxResultBytes`: a finite whole number of at
35
+ * least {@link MIN_MAX_RESULT_BYTES} bytes. Shared by all three intake points
36
+ * — deployment config, the per-connector override, and `get_result`'s
37
+ * `maxBytes` argument — so a value that is valid at one is valid at all.
38
+ *
39
+ * Everything else is rejected rather than coerced, because each rejected shape
40
+ * silently does something *worse* than the default: `0`/`NaN` serve an empty
41
+ * head (`slice(0, 0)`) and make paging fail to advance, negatives serve a
42
+ * LARGER head than asked for (`slice(0, -1)` counts from the end) while still
43
+ * claiming truncation, and `Infinity` disables the guard with no notice.
44
+ */
45
+ export function isValidMaxResultBytes(value: number): boolean {
46
+ return Number.isInteger(value) && value >= MIN_MAX_RESULT_BYTES;
47
+ }
48
+
49
+ /**
50
+ * Resolve a configured cap against the value it inherits, dropping anything
51
+ * `isValidMaxResultBytes` rejects. Operator-facing surfaces pair this with a
52
+ * startup warning (see `Registry.checkResultCaps`) so the fallback is never
53
+ * silent; the resolution itself stays total so no call site has to cope with
54
+ * a broken cap.
55
+ */
56
+ export function resolveMaxResultBytes(
57
+ value: number | undefined,
58
+ inherited: number,
59
+ ): number {
60
+ return value !== undefined && isValidMaxResultBytes(value)
61
+ ? value
62
+ : inherited;
63
+ }
15
64
 
16
65
  interface CacheEntry {
17
66
  tools: ToolDef[];
@@ -34,6 +83,48 @@ export interface HealthObservation {
34
83
  lastError?: string;
35
84
  }
36
85
 
86
+ /**
87
+ * Recent real-call outcomes per connector, as observed by ONE view.
88
+ *
89
+ * The deployment keeps one log, and each toolkit keeps its own. That split is a
90
+ * scope boundary, not bookkeeping: `lastError` is a downstream error string
91
+ * that routinely names the tool that failed, and `list_connectors` returns the
92
+ * observation verbatim. Sharing one log would let a toolkit read back the tools
93
+ * and failures of a sibling toolkit through a connector they happen to share —
94
+ * and let a sibling's failures flip this view's reported status. A scoped view
95
+ * reads back only the calls it made itself.
96
+ */
97
+ export class HealthLog {
98
+ private readonly observations = new Map<string, HealthObservation>();
99
+
100
+ recordSuccess(id: string, latencyMs: number): void {
101
+ const previous = this.observations.get(id);
102
+ this.observations.set(id, {
103
+ ...previous,
104
+ lastSuccessAt: new Date().toISOString(),
105
+ lastLatencyMs: latencyMs,
106
+ consecutiveFailures: 0,
107
+ lastError: undefined,
108
+ });
109
+ }
110
+
111
+ recordFailure(id: string, latencyMs: number, error: unknown): void {
112
+ const previous = this.observations.get(id);
113
+ this.observations.set(id, {
114
+ ...previous,
115
+ lastFailureAt: new Date().toISOString(),
116
+ lastLatencyMs: latencyMs,
117
+ consecutiveFailures: (previous?.consecutiveFailures ?? 0) + 1,
118
+ lastError: msg(error),
119
+ });
120
+ }
121
+
122
+ get(id: string): HealthObservation | undefined {
123
+ const observation = this.observations.get(id);
124
+ return observation ? { ...observation } : undefined;
125
+ }
126
+ }
127
+
37
128
  export interface RegistryOptions {
38
129
  storage: KVStorage;
39
130
  logger: Logger;
@@ -41,8 +132,14 @@ export interface RegistryOptions {
41
132
  toolCacheTtlSeconds?: number;
42
133
  persistToolCatalog?: boolean;
43
134
  toolCatalogStaleSeconds?: number;
44
- /** Cap on inline result size before truncation + get_result paging. Default 50_000. */
135
+ /**
136
+ * Cap on inline result size before truncation + get_result paging. Must be a
137
+ * whole number of bytes >= 1; anything else warns at startup and falls back
138
+ * to the default 50_000.
139
+ */
45
140
  maxResultBytes?: number;
141
+ /** Tuning for the credential liveness checks (issue #24). */
142
+ credentialHealth?: CredentialHealthConfig;
46
143
  }
47
144
 
48
145
  function namespaced(storage: KVStorage, prefix: string): KVStorage {
@@ -57,21 +154,83 @@ function msg(err: unknown): string {
57
154
  return err instanceof Error ? err.message : String(err);
58
155
  }
59
156
 
157
+ /**
158
+ * The registry surface a per-connection MCP server consumes: every meta-tool
159
+ * (`src/meta-tools.ts`) and the `execute_code` sandbox bridge (`src/execute.ts`)
160
+ * is typed against THIS, never against the concrete `Registry`.
161
+ *
162
+ * That indirection is the toolkit boundary (issue #22): a scoped connection is
163
+ * handed a `ScopedRegistry` instead of the registry, and every meta-tool
164
+ * inherits the scope from one place. Reaching for a registry method that isn't
165
+ * here is a compile error, and adding one here is a compile error until
166
+ * `ScopedRegistry` filters it — so a new meta-tool cannot quietly step around
167
+ * the boundary.
168
+ */
169
+ export interface RegistryView {
170
+ /** Deployment-wide result-size cap threaded to the meta-tools. */
171
+ readonly maxResultBytes: number;
172
+ listConnectors(): Connector[];
173
+ getConnector(id: string): Connector | undefined;
174
+ resolveAddress(
175
+ address: string,
176
+ ): { connector: Connector; toolName: string } | null;
177
+ getTools(
178
+ id: string,
179
+ baseUrl: string,
180
+ requestScope?: object,
181
+ ): Promise<ToolDef[]>;
182
+ refreshTools(
183
+ id: string,
184
+ baseUrl: string,
185
+ requestScope?: object,
186
+ ): Promise<ToolDef[]>;
187
+ peekTools(id: string): ToolDef[] | undefined;
188
+ contextFor(
189
+ id: string,
190
+ baseUrl: string,
191
+ requestScope?: object,
192
+ callOptions?: { signal?: AbortSignal; timeoutMs?: number },
193
+ ): ConnectorContext;
194
+ resultsStorage(): KVStorage;
195
+ recordSuccess(id: string, latencyMs: number): void;
196
+ recordFailure(id: string, latencyMs: number, error: unknown): void;
197
+ healthFor(id: string): HealthObservation | undefined;
198
+ hasObservedSuccess(id: string): boolean;
199
+ /** When ANY view last saw a successful call to `id`, deployment-wide. */
200
+ observedSuccessAt(id: string): string | undefined;
201
+ /** Last credential-liveness verdict for `id`. Cached; no downstream I/O. */
202
+ credentialHealthFor(id: string): Promise<CredentialHealthRecord | undefined>;
203
+ /** Store a liveness verdict a live status check just produced. */
204
+ recordCredentialHealth(
205
+ id: string,
206
+ record: CredentialHealthRecord,
207
+ ): Promise<void>;
208
+ statusFor(
209
+ id: string,
210
+ baseUrl: string,
211
+ requestScope?: object,
212
+ ): Promise<ConnectorStatus>;
213
+ invalidateStored(id: string): Promise<void>;
214
+ }
215
+
60
216
  /**
61
217
  * Holds the connector set, resolves addresses, and caches per-connector tool
62
218
  * lists in memory with a TTL. Connector failures are isolated: a broken
63
219
  * connector surfaces status "error"; the rest keep working.
64
220
  */
65
- export class Registry {
221
+ export class Registry implements RegistryView {
66
222
  private readonly connectors = new Map<string, Connector>();
67
223
  private readonly cache = new Map<string, CacheEntry>();
68
224
  private readonly invalidated = new Set<string>();
69
- private readonly health = new Map<string, HealthObservation>();
225
+ /** Deployment-wide observations every call, whatever view made it. */
226
+ private readonly health = new HealthLog();
70
227
  private readonly ttlMs: number;
71
228
  private readonly staleMs: number;
72
229
  private readonly persistToolCatalog: boolean;
73
230
  /** Result-size guard cap threaded to the meta-tools. */
74
231
  readonly maxResultBytes: number;
232
+ /** Proactive liveness checks over stored downstream credentials (issue #24). */
233
+ private readonly credentialHealth: CredentialHealthChecker;
75
234
 
76
235
  constructor(
77
236
  connectors: Connector[],
@@ -82,7 +241,10 @@ export class Registry {
82
241
  this.staleMs =
83
242
  (opts.toolCatalogStaleSeconds ?? DEFAULT_STALE_SECONDS) * 1000;
84
243
  this.persistToolCatalog = opts.persistToolCatalog ?? true;
85
- this.maxResultBytes = opts.maxResultBytes ?? DEFAULT_MAX_RESULT_BYTES;
244
+ this.maxResultBytes = resolveMaxResultBytes(
245
+ opts.maxResultBytes,
246
+ DEFAULT_MAX_RESULT_BYTES,
247
+ );
86
248
  for (const c of connectors) {
87
249
  if (!ID_RE.test(c.id)) {
88
250
  throw new Error(
@@ -95,6 +257,59 @@ export class Registry {
95
257
  this.connectors.set(c.id, c);
96
258
  }
97
259
  this.checkConventions(opts.logger);
260
+ this.checkResultCaps(opts.logger, opts.maxResultBytes);
261
+ this.credentialHealth = new CredentialHealthChecker(
262
+ {
263
+ listConnectors: () => this.listConnectors(),
264
+ getConnector: (id) => this.getConnector(id),
265
+ contextFor: (id, baseUrl, requestScope) =>
266
+ this.contextFor(id, baseUrl, requestScope),
267
+ storage: opts.storage,
268
+ logger: opts.logger,
269
+ credentialVault: opts.credentialVault,
270
+ },
271
+ opts.credentialHealth,
272
+ );
273
+ }
274
+
275
+ /**
276
+ * Warn once per unusable result cap, at construction time — the same
277
+ * "runs fine but is surely unintended" channel as the insecure-config
278
+ * warnings in `createConnecta`. A rejected cap can't be honoured, and
279
+ * honouring it *approximately* is exactly the inversion issue #32 is about,
280
+ * so the value is dropped in favour of what it inherits and the operator is
281
+ * told which one is actually in force.
282
+ */
283
+ private checkResultCaps(
284
+ logger: Logger,
285
+ configured: number | undefined,
286
+ ): void {
287
+ if (configured !== undefined && !isValidMaxResultBytes(configured)) {
288
+ logger.warn(
289
+ `[connecta] maxResultBytes ${configured} is not a whole number of ` +
290
+ `bytes >= ${MIN_MAX_RESULT_BYTES}: it would serve an empty, ` +
291
+ "oversized, or unguarded result instead of truncating. Using the " +
292
+ `default ${DEFAULT_MAX_RESULT_BYTES} instead.`,
293
+ );
294
+ }
295
+ for (const c of this.connectors.values()) {
296
+ if (
297
+ c.maxResultBytes !== undefined &&
298
+ !isValidMaxResultBytes(c.maxResultBytes)
299
+ ) {
300
+ // The number quoted here is `this.maxResultBytes` because that is
301
+ // literally what a call falls back to: meta-tools reads the inherited
302
+ // cap off `RegistryView.maxResultBytes`, so the warned value and the
303
+ // runtime value are the same field rather than two copies of it.
304
+ logger.warn(
305
+ `[connecta] connector "${c.id}" sets maxResultBytes ` +
306
+ `${c.maxResultBytes}, which is not a whole number of bytes >= ` +
307
+ `${MIN_MAX_RESULT_BYTES}. Ignoring the override — the connector ` +
308
+ `inherits the deployment-wide cap calls fall back to ` +
309
+ `(${this.maxResultBytes}).`,
310
+ );
311
+ }
312
+ }
98
313
  }
99
314
 
100
315
  /**
@@ -169,13 +384,11 @@ export class Registry {
169
384
  resolveAddress(
170
385
  address: string,
171
386
  ): { connector: Connector; toolName: string } | null {
172
- const dot = address.indexOf(".");
173
- if (dot <= 0 || dot === address.length - 1) return null;
174
- const id = address.slice(0, dot);
175
- const toolName = address.slice(dot + 1);
176
- const connector = this.connectors.get(id);
387
+ const parts = splitAddress(address);
388
+ if (!parts) return null;
389
+ const connector = this.connectors.get(parts.connectorId);
177
390
  if (!connector) return null;
178
- return { connector, toolName };
391
+ return { connector, toolName: parts.toolName };
179
392
  }
180
393
 
181
394
  private catalogKey(id: string): string {
@@ -320,30 +533,91 @@ export class Registry {
320
533
  }
321
534
 
322
535
  recordSuccess(id: string, latencyMs: number): void {
323
- const previous = this.health.get(id);
324
- this.health.set(id, {
325
- ...previous,
326
- lastSuccessAt: new Date().toISOString(),
327
- lastLatencyMs: latencyMs,
328
- consecutiveFailures: 0,
329
- lastError: undefined,
330
- });
536
+ this.health.recordSuccess(id, latencyMs);
331
537
  }
332
538
 
333
539
  recordFailure(id: string, latencyMs: number, error: unknown): void {
334
- const previous = this.health.get(id);
335
- this.health.set(id, {
336
- ...previous,
337
- lastFailureAt: new Date().toISOString(),
338
- lastLatencyMs: latencyMs,
339
- consecutiveFailures: (previous?.consecutiveFailures ?? 0) + 1,
340
- lastError: msg(error),
341
- });
540
+ this.health.recordFailure(id, latencyMs, error);
342
541
  }
343
542
 
344
543
  healthFor(id: string): HealthObservation | undefined {
345
- const observation = this.health.get(id);
346
- return observation ? { ...observation } : undefined;
544
+ return this.health.get(id);
545
+ }
546
+
547
+ /**
548
+ * Whether ANY view of this deployment has seen a successful call to `id` —
549
+ * a bare boolean, never the observation. Connector liveness (reachable,
550
+ * credentials still valid) is a deployment-level fact, not a per-view one, so
551
+ * `list_connectors` may classify a connector as ok/unknown from it. The
552
+ * observation itself — `lastError` above all, which names the tool that
553
+ * failed — stays strictly per view.
554
+ */
555
+ hasObservedSuccess(id: string): boolean {
556
+ return this.observedSuccessAt(id) !== undefined;
557
+ }
558
+
559
+ /**
560
+ * The timestamp behind `hasObservedSuccess`, on the same deployment-wide
561
+ * terms and for the same reason: it says only *when* the connector last
562
+ * answered, never what was called or what failed. Credential health reads it
563
+ * to decide whether a failed verdict has been overtaken by real traffic.
564
+ */
565
+ observedSuccessAt(id: string): string | undefined {
566
+ return this.health.get(id)?.lastSuccessAt;
567
+ }
568
+
569
+ /**
570
+ * The last credential-liveness verdict for `id` — the layer that lets a cached
571
+ * status read report `auth_required` before a real call discovers it. Read
572
+ * from storage (mirrored in memory for a few seconds) rather than held in
573
+ * memory alone, because on Workers the isolate that ran the check is usually
574
+ * not the isolate answering this read.
575
+ */
576
+ credentialHealthFor(id: string): Promise<CredentialHealthRecord | undefined> {
577
+ return this.credentialHealth.healthFor(id);
578
+ }
579
+
580
+ recordCredentialHealth(
581
+ id: string,
582
+ record: CredentialHealthRecord,
583
+ ): Promise<void> {
584
+ return this.credentialHealth.record(id, record);
585
+ }
586
+
587
+ /**
588
+ * Check stored downstream credentials now and return one outcome per
589
+ * connector considered. THE operator-facing entry point behind
590
+ * `Connecta.checkCredentials()`: wire it to a Worker cron trigger or a Node
591
+ * interval. Never rejects; connectors checked recently are reported as
592
+ * `fresh` unless `force` is set.
593
+ */
594
+ checkCredentialHealth(
595
+ baseUrl: string,
596
+ opts?: CredentialCheckOptions,
597
+ ): Promise<CredentialCheckResult[]> {
598
+ return this.credentialHealth.check(baseUrl, opts);
599
+ }
600
+
601
+ /**
602
+ * The traffic-triggered sweep: a promise for the caller to defer (Workers:
603
+ * `ctx.waitUntil`), or `undefined` when nothing is due — which is the common
604
+ * case and costs no I/O. Called by the server after an authenticated request;
605
+ * the checker owns the rate limiting.
606
+ */
607
+ sweepCredentialHealthIfDue(
608
+ baseUrl: string,
609
+ ): Promise<CredentialCheckResult[]> | undefined {
610
+ return this.credentialHealth.sweepIfDue(baseUrl);
611
+ }
612
+
613
+ /**
614
+ * Drop a connector's liveness verdict, because its credential just changed
615
+ * under us (OAuth callback completed, credential stored or removed in /ui). A
616
+ * stale `auth_required` must not outlive the re-authorization that fixed it —
617
+ * that is the difference between recovery working and needing a restart.
618
+ */
619
+ clearCredentialHealth(id: string): Promise<void> {
620
+ return this.credentialHealth.clear(id);
347
621
  }
348
622
 
349
623
  /** Best-effort connector status for list_connectors. */
@@ -398,3 +672,240 @@ export class Registry {
398
672
  }
399
673
  }
400
674
  }
675
+
676
+ /**
677
+ * Per-toolkit HealthLogs, keyed by registry and then by toolkit name.
678
+ *
679
+ * Module-scoped rather than a `Registry` member on purpose: `Registry`'s type
680
+ * is part of the public API surface (`Connecta.registry`), and this is internal
681
+ * factoring only `ScopedRegistry` may touch. The WeakMap keeps the logs alive
682
+ * exactly as long as their registry, and the inner map is bounded by the
683
+ * number of configured toolkits.
684
+ */
685
+ const toolkitHealthLogs = new WeakMap<Registry, Map<string, HealthLog>>();
686
+
687
+ /** The long-lived log one toolkit records into; created on first use. */
688
+ function toolkitHealthLog(base: Registry, toolkitName: string): HealthLog {
689
+ let logs = toolkitHealthLogs.get(base);
690
+ if (!logs) {
691
+ logs = new Map<string, HealthLog>();
692
+ toolkitHealthLogs.set(base, logs);
693
+ }
694
+ let log = logs.get(toolkitName);
695
+ if (!log) {
696
+ log = new HealthLog();
697
+ logs.set(toolkitName, log);
698
+ }
699
+ return log;
700
+ }
701
+
702
+ /**
703
+ * THE toolkit enforcement point (issue #22).
704
+ *
705
+ * A filtered VIEW of one long-lived `Registry`: same connectors, same tool
706
+ * caches, same health — narrowed to the connectors and tool addresses one
707
+ * toolkit selects. `serveMcp` builds it once per scoped connection and hands it
708
+ * to `registerMetaTools`/`registerExecuteTool`, so `list_connectors`,
709
+ * `search_tools`, `describe_tools`, `call_tool`, `call_destructive_tool`,
710
+ * `batch_call`, `authorize_connector`, `get_result`, `skills`, and the
711
+ * `execute_code` host bridge all inherit the boundary from here instead of
712
+ * re-implementing nine checks.
713
+ *
714
+ * Two invariants make it reviewable:
715
+ *
716
+ * 1. **Indistinguishability.** Out-of-scope input fails through the SAME code
717
+ * path, with the same error class and message, as input naming something
718
+ * that does not exist at all. An out-of-scope CONNECTOR disappears from
719
+ * `resolveAddress`/`getConnector` (→ `Unknown address` / `Unknown
720
+ * connector`); an out-of-scope TOOL disappears from the catalog this view
721
+ * returns (→ `Unknown tool "<t>" on connector "<c>"`), which is exactly the
722
+ * error a misspelled tool name already produced. Scoping deliberately does
723
+ * NOT reject at `resolveAddress` for tools: that would answer with the
724
+ * connector-level message and make "hidden here" distinguishable from
725
+ * "never existed".
726
+ * 2. **It filters views, never state.** Every read delegates to the shared
727
+ * registry and filters the returned array, so the tool cache, the persisted
728
+ * catalog, and the health map stay whole and shared across scopes.
729
+ */
730
+ export class ScopedRegistry implements RegistryView {
731
+ /** This toolkit's own health observations — see HealthLog. */
732
+ private readonly health: HealthLog;
733
+
734
+ constructor(
735
+ private readonly base: Registry,
736
+ private readonly toolkit: Toolkit,
737
+ ) {
738
+ this.health = toolkitHealthLog(base, toolkit.name);
739
+ }
740
+
741
+ get maxResultBytes(): number {
742
+ return this.base.maxResultBytes;
743
+ }
744
+
745
+ /** In scope AND actually registered. */
746
+ private visible(id: string): boolean {
747
+ return (
748
+ this.toolkit.hasConnector(id) && this.base.getConnector(id) !== undefined
749
+ );
750
+ }
751
+
752
+ /** Byte-identical to what the unscoped registry throws for an unknown id. */
753
+ private unknownConnector(id: string): Error {
754
+ return new Error(`Unknown connector "${id}"`);
755
+ }
756
+
757
+ private inScopeTools(id: string, tools: ToolDef[]): ToolDef[] {
758
+ return tools.filter((tool) => this.toolkit.hasTool(id, tool.name));
759
+ }
760
+
761
+ listConnectors(): Connector[] {
762
+ return this.base
763
+ .listConnectors()
764
+ .filter((connector) => this.toolkit.hasConnector(connector.id));
765
+ }
766
+
767
+ getConnector(id: string): Connector | undefined {
768
+ return this.visible(id) ? this.base.getConnector(id) : undefined;
769
+ }
770
+
771
+ resolveAddress(
772
+ address: string,
773
+ ): { connector: Connector; toolName: string } | null {
774
+ const resolved = this.base.resolveAddress(address);
775
+ if (!resolved) return null;
776
+ // Connector-level only — see invariant 1 above.
777
+ return this.toolkit.hasConnector(resolved.connector.id) ? resolved : null;
778
+ }
779
+
780
+ async getTools(
781
+ id: string,
782
+ baseUrl: string,
783
+ requestScope?: object,
784
+ ): Promise<ToolDef[]> {
785
+ if (!this.visible(id)) throw this.unknownConnector(id);
786
+ return this.inScopeTools(
787
+ id,
788
+ await this.base.getTools(id, baseUrl, requestScope),
789
+ );
790
+ }
791
+
792
+ async refreshTools(
793
+ id: string,
794
+ baseUrl: string,
795
+ requestScope?: object,
796
+ ): Promise<ToolDef[]> {
797
+ if (!this.visible(id)) throw this.unknownConnector(id);
798
+ return this.inScopeTools(
799
+ id,
800
+ await this.base.refreshTools(id, baseUrl, requestScope),
801
+ );
802
+ }
803
+
804
+ peekTools(id: string): ToolDef[] | undefined {
805
+ if (!this.visible(id)) return undefined;
806
+ const tools = this.base.peekTools(id);
807
+ return tools ? this.inScopeTools(id, tools) : undefined;
808
+ }
809
+
810
+ contextFor(
811
+ id: string,
812
+ baseUrl: string,
813
+ requestScope: object = {},
814
+ callOptions: { signal?: AbortSignal; timeoutMs?: number } = {},
815
+ ): ConnectorContext {
816
+ // Unreachable through the meta-tools (they resolve first), so a throw here
817
+ // is a loud backstop rather than a silent grant of connector storage and
818
+ // credentials to a scope that may not see the connector.
819
+ if (!this.visible(id)) throw this.unknownConnector(id);
820
+ return this.base.contextFor(id, baseUrl, requestScope, callOptions);
821
+ }
822
+
823
+ /**
824
+ * Stashed oversized results are bound to the scope that produced them: a
825
+ * scoped session cannot page a result it could not have produced, and an id
826
+ * from another scope reads back as the ordinary "Unknown or expired result
827
+ * id". Unscoped sessions keep the historical `results:` prefix untouched.
828
+ */
829
+ resultsStorage(): KVStorage {
830
+ return namespaced(
831
+ this.base.resultsStorage(),
832
+ `toolkit:${this.toolkit.name}:`,
833
+ );
834
+ }
835
+
836
+ // Outcomes are recorded twice on purpose: the deployment-wide log keeps the
837
+ // operator surfaces complete, while the toolkit's own log is the ONLY one
838
+ // this view reads back — so `list_connectors` here never reports a sibling
839
+ // toolkit's failures, or the tool names their error strings carry.
840
+ recordSuccess(id: string, latencyMs: number): void {
841
+ if (!this.visible(id)) return;
842
+ this.base.recordSuccess(id, latencyMs);
843
+ this.health.recordSuccess(id, latencyMs);
844
+ }
845
+
846
+ recordFailure(id: string, latencyMs: number, error: unknown): void {
847
+ if (!this.visible(id)) return;
848
+ this.base.recordFailure(id, latencyMs, error);
849
+ this.health.recordFailure(id, latencyMs, error);
850
+ }
851
+
852
+ healthFor(id: string): HealthObservation | undefined {
853
+ return this.visible(id) ? this.health.get(id) : undefined;
854
+ }
855
+
856
+ /**
857
+ * Deliberately NOT per view: whether the connector has ever answered is a
858
+ * fact about the connector, not about a team's traffic, and withholding it
859
+ * would report every remote connector as "unknown" to a scoped session that
860
+ * has not called it yet. It carries no tool name, error text, or count — the
861
+ * per-view isolation of those, above, is unchanged.
862
+ */
863
+ hasObservedSuccess(id: string): boolean {
864
+ return this.visible(id) ? this.base.hasObservedSuccess(id) : false;
865
+ }
866
+
867
+ /** Deployment-wide for the same reason as `hasObservedSuccess` above. */
868
+ observedSuccessAt(id: string): string | undefined {
869
+ return this.visible(id) ? this.base.observedSuccessAt(id) : undefined;
870
+ }
871
+
872
+ /**
873
+ * Also deliberately NOT per view, for the same reason as
874
+ * `hasObservedSuccess`: whether the credential connecta stores for a connector
875
+ * still works is a fact about the deployment's credential, not about a team's
876
+ * traffic. Withholding it would leave a scoped session unable to see that the
877
+ * connector it shares needs re-authorization. The verdict carries the
878
+ * connector's own connector-level reason — never a tool name — so the per-view
879
+ * isolation of `lastError` above is unchanged.
880
+ */
881
+ credentialHealthFor(id: string): Promise<CredentialHealthRecord | undefined> {
882
+ return this.visible(id)
883
+ ? this.base.credentialHealthFor(id)
884
+ : Promise.resolve(undefined);
885
+ }
886
+
887
+ async recordCredentialHealth(
888
+ id: string,
889
+ record: CredentialHealthRecord,
890
+ ): Promise<void> {
891
+ if (!this.visible(id)) return;
892
+ await this.base.recordCredentialHealth(id, record);
893
+ }
894
+
895
+ async statusFor(
896
+ id: string,
897
+ baseUrl: string,
898
+ requestScope: object = {},
899
+ ): Promise<ConnectorStatus> {
900
+ // Same shape the unscoped registry returns for an unregistered id.
901
+ if (!this.visible(id)) {
902
+ return { state: "error", message: "Unknown connector" };
903
+ }
904
+ return this.base.statusFor(id, baseUrl, requestScope);
905
+ }
906
+
907
+ async invalidateStored(id: string): Promise<void> {
908
+ if (!this.visible(id)) return;
909
+ await this.base.invalidateStored(id);
910
+ }
911
+ }