@akai-workflow-builder/cli-sdk 0.16.0 → 0.18.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 (39) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/dist/ctx/build-ctx.d.ts.map +1 -1
  3. package/dist/ctx/build-ctx.js +41 -4
  4. package/dist/ctx/dynamic-egress.d.ts +11 -0
  5. package/dist/ctx/dynamic-egress.d.ts.map +1 -1
  6. package/dist/ctx/dynamic-egress.js +7 -2
  7. package/dist/ctx/safe-fetch.d.ts +1 -0
  8. package/dist/ctx/safe-fetch.d.ts.map +1 -1
  9. package/dist/ctx/safe-fetch.js +1 -1
  10. package/dist/ctx/sftp-egress.d.ts +47 -0
  11. package/dist/ctx/sftp-egress.d.ts.map +1 -0
  12. package/dist/ctx/sftp-egress.js +99 -0
  13. package/dist/ctx/sftp.d.ts +101 -0
  14. package/dist/ctx/sftp.d.ts.map +1 -0
  15. package/dist/ctx/sftp.js +426 -0
  16. package/dist/index.d.ts +4 -1
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +1 -0
  19. package/dist/manifest/index.d.ts +1 -1
  20. package/dist/manifest/index.d.ts.map +1 -1
  21. package/dist/manifest/json-schema.d.ts +15 -0
  22. package/dist/manifest/json-schema.d.ts.map +1 -1
  23. package/dist/manifest/json-schema.js +11 -2
  24. package/dist/manifest/to-schema.d.ts.map +1 -1
  25. package/dist/manifest/to-schema.js +10 -0
  26. package/dist/manifest/types.d.ts +10 -1
  27. package/dist/manifest/types.d.ts.map +1 -1
  28. package/dist/runtime/executor/index.d.ts.map +1 -1
  29. package/dist/runtime/executor/index.js +22 -6
  30. package/dist/runtime/registry/loader.d.ts +18 -10
  31. package/dist/runtime/registry/loader.d.ts.map +1 -1
  32. package/dist/runtime/registry/loader.js +282 -143
  33. package/dist/runtime/registry/types.d.ts +11 -3
  34. package/dist/runtime/registry/types.d.ts.map +1 -1
  35. package/dist/tool.d.ts.map +1 -1
  36. package/dist/tool.js +30 -1
  37. package/dist/types.d.ts +13 -3
  38. package/dist/types.d.ts.map +1 -1
  39. package/package.json +3 -1
@@ -34,10 +34,11 @@ export class Registry {
34
34
  // Map keyed by `connection.slug`. CLIs join to a connection by
35
35
  // `connectionSlug` at the wire layer; the connection itself is identity-only.
36
36
  byConnectionSlug;
37
- // Packages skipped at load (recorded, not thrown): a customer package that
38
- // failed the brand/shape check, OR either side of a duplicate id/slug whose
39
- // later definition was dropped (see `resolveCollision`). A *malformed* baseline
40
- // package still fails the boot hard; a *duplicate* never does (non-strict).
37
+ // Packages skipped at load (recorded, not thrown): a package that failed to
38
+ // load (customer always; baseline unless strict), OR either side of a
39
+ // duplicate id/slug whose later definition was dropped (see
40
+ // `resolveCollision`). Only strict mode fails the load on a malformed baseline
41
+ // package or an accidental duplicate.
41
42
  loadFailures;
42
43
  // Lazy mode only: CLIs catalogued from manifest.json, keyed by cli id. Their
43
44
  // handler modules are imported on first `resolveTool` (see `resolvedCache`).
@@ -126,8 +127,34 @@ export class Registry {
126
127
  });
127
128
  return replace;
128
129
  };
130
+ /**
131
+ * Record a connection this cli-sdk cannot load (see `checkConnection`). Strict
132
+ * mode (CI) throws, so a connection the loading cli-sdk does not support fails
133
+ * the PR instead of shipping unavailable. Otherwise it is logged + recorded and
134
+ * the rest of its package loads. Returns `true` when the caller should hold
135
+ * its slug — the first claim on a slug — so a later definition still collides
136
+ * with it and a customer package cannot quietly take a baseline slug that is
137
+ * only unavailable because this cli-sdk is older than the one it was written for.
138
+ */
139
+ const markUnsupported = (failure, slugTaken) => {
140
+ if (strict)
141
+ throw new Error(failure.error);
142
+ logger?.error({ slug: failure.slug, packagePath: failure.packagePath, source: failure.source }, `${failure.error} — the rest of the package still loads`);
143
+ failures.push(failure);
144
+ return !slugTaken;
145
+ };
146
+ // Slugs held by an unsupported connection (eager path): a later connection with
147
+ // the same slug collides with it exactly as with a loaded one.
148
+ const unsupportedConnBySlug = new Map();
129
149
  const ingest = (loaded) => {
130
150
  for (const item of loaded) {
151
+ if (item.kind === 'unsupported-connection') {
152
+ const { slug, packagePath, source } = item.failure;
153
+ const taken = connBySlug.has(slug) || unsupportedConnBySlug.has(slug);
154
+ if (markUnsupported(item.failure, taken))
155
+ unsupportedConnBySlug.set(slug, { packagePath, source, override: false });
156
+ continue;
157
+ }
131
158
  if (item.kind === 'cli') {
132
159
  const e = item.entry;
133
160
  const existing = cliById.get(e.cli.id);
@@ -142,8 +169,11 @@ export class Registry {
142
169
  else {
143
170
  const c = item.entry;
144
171
  const existing = connBySlug.get(c.connection.slug);
145
- if (existing) {
146
- const replace = resolveCollision('connection slug', c.connection.slug, { packagePath: existing.packagePath, source: existing.source, override: existing.connection.override === true }, { packagePath: c.packagePath, source: c.source, override: c.connection.override === true });
172
+ const held = existing
173
+ ? { packagePath: existing.packagePath, source: existing.source, override: existing.connection.override === true }
174
+ : unsupportedConnBySlug.get(c.connection.slug);
175
+ if (held) {
176
+ const replace = resolveCollision('connection slug', c.connection.slug, held, { packagePath: c.packagePath, source: c.source, override: c.connection.override === true });
147
177
  if (replace)
148
178
  connBySlug.set(c.connection.slug, c);
149
179
  continue;
@@ -161,12 +191,12 @@ export class Registry {
161
191
  // first-party tool, the exact outage class this loader defends against.
162
192
  if (!opts.lazy) {
163
193
  if (opts.baselinePath) {
164
- const scan = await scanPath(opts.baselinePath, 'baseline', opts.logger);
194
+ const scan = await scanPath(opts.baselinePath, 'baseline', strict, opts.logger);
165
195
  ingest(scan.loaded);
166
196
  failures.push(...scan.failures);
167
197
  }
168
198
  if (opts.customerPath) {
169
- const scan = await scanPath(opts.customerPath, 'customer', opts.logger);
199
+ const scan = await scanPath(opts.customerPath, 'customer', strict, opts.logger);
170
200
  ingest(scan.loaded);
171
201
  failures.push(...scan.failures);
172
202
  }
@@ -199,6 +229,13 @@ export class Registry {
199
229
  // Manifest-less CLIs → unified CLI chooser; eager connections → unified
200
230
  // connection chooser (slug space is independent of the cli-id space).
201
231
  for (const item of scan.eager) {
232
+ if (item.kind === 'unsupported-connection') {
233
+ const { slug, packagePath, source } = item.failure;
234
+ if (markUnsupported(item.failure, connChoice.has(slug))) {
235
+ connChoice.set(slug, { kind: 'unsupported', slug, source, packagePath, override: false });
236
+ }
237
+ continue;
238
+ }
202
239
  if (item.kind === 'connection') {
203
240
  chooseConn({ kind: 'eager', slug: item.entry.connection.slug, source: item.entry.source, packagePath: item.entry.packagePath, override: item.entry.connection.override === true, entry: item.entry });
204
241
  }
@@ -215,9 +252,9 @@ export class Registry {
215
252
  };
216
253
  // Same baseline-before-customer invariant as the eager path.
217
254
  if (opts.baselinePath)
218
- ingestLazyScan(await scanPathLazy(opts.baselinePath, 'baseline', opts.logger));
255
+ ingestLazyScan(await scanPathLazy(opts.baselinePath, 'baseline', strict, opts.logger));
219
256
  if (opts.customerPath)
220
- ingestLazyScan(await scanPathLazy(opts.customerPath, 'customer', opts.logger));
257
+ ingestLazyScan(await scanPathLazy(opts.customerPath, 'customer', strict, opts.logger));
221
258
  const eagerEntries = [];
222
259
  const lazyEntries = [];
223
260
  for (const cand of cliChoice.values()) {
@@ -231,7 +268,7 @@ export class Registry {
231
268
  for (const cand of connChoice.values()) {
232
269
  if (cand.kind === 'eager')
233
270
  eagerConns.push(cand.entry);
234
- else
271
+ else if (cand.kind === 'manifest')
235
272
  manifestConns.push(cand.manifest);
236
273
  }
237
274
  return new Registry(eagerEntries, eagerConns, failures, lazyEntries, manifestConns);
@@ -322,8 +359,9 @@ export class Registry {
322
359
  return undefined;
323
360
  return (await this.resolveLazyEntry(lazy)).cli;
324
361
  }
325
- /** Packages skipped at load: failed brand/shape check, or the dropped side of
326
- * a duplicate id/slug collision. */
362
+ /** Packages skipped at load: failed to load (import / brand / shape check —
363
+ * baseline too, unless strict), or the dropped side of a duplicate id/slug
364
+ * collision. */
327
365
  failures() {
328
366
  return this.loadFailures;
329
367
  }
@@ -419,17 +457,15 @@ export class Registry {
419
457
  * and skipped (`resolveEntryPoint` returns undefined) — a directory that
420
458
  * isn't a package shouldn't block startup.
421
459
  *
422
- * A package that IS a loadable module but fails the brand/shape check is
423
- * handled by `source`:
424
- * - `baseline` (first-party, gated by our own CI): throws immediately. A
425
- * broken baseline is a bad deploy we want to halt.
426
- * - `customer` (third-party): logged loudly, recorded as a `LoadFailure`, and
427
- * skipped — one customer's broken package must not take every tenant's
428
- * tools down. This is NOT a silent half-load: each skip is recorded
429
- * explicitly, so the catalog view stays deterministic ("these loaded,
430
- * these are unavailable: <reason>").
460
+ * A package that fails to load — unreadable package.json, import error, or a
461
+ * failed brand/shape check — is handled by {@link recordPackageFailure}: in
462
+ * strict mode a BASELINE failure throws (the CI gate); otherwise the package is
463
+ * logged loudly, recorded as a `LoadFailure`, and skipped, so one broken package
464
+ * can't take every tenant's tools down. This is NOT a silent half-load: each
465
+ * skip is recorded explicitly, so the catalog view stays deterministic ("these
466
+ * loaded, these are unavailable: <reason>").
431
467
  */
432
- async function scanPath(root, source, logger) {
468
+ async function scanPath(root, source, strict, logger) {
433
469
  let dirents;
434
470
  try {
435
471
  dirents = await fs.readdir(root, { withFileTypes: true });
@@ -454,39 +490,52 @@ async function scanPath(root, source, logger) {
454
490
  .sort();
455
491
  for (const name of subdirs) {
456
492
  const packagePath = path.join(root, name);
457
- const entryPoint = await resolveEntryPoint(packagePath);
458
- if (!entryPoint) {
459
- logger?.warn({ packagePath }, 'registry: skipping — no package.json or resolvable entry point');
460
- continue;
461
- }
462
493
  try {
494
+ const entryPoint = await resolveEntryPoint(packagePath);
495
+ if (!entryPoint) {
496
+ logger?.warn({ packagePath }, 'registry: skipping — no package.json or resolvable entry point');
497
+ continue;
498
+ }
499
+ // `loadPackageEntry` resolves all of a package's items or throws, so a
500
+ // failed package never half-loads.
463
501
  for (const item of await loadPackageEntry(entryPoint, packagePath, source)) {
464
502
  loaded.push(item);
465
503
  }
466
504
  }
467
505
  catch (err) {
468
- // Baseline failures fail the boot hard (first-party, gated by our CI).
469
- // A broken customer package is skipped + recorded so it can't take every
470
- // tenant's tools down with it.
471
- if (source === 'baseline')
472
- throw err;
473
- const message = err instanceof Error ? err.message : String(err);
474
- logger?.error({ packagePath, source, error: message }, 'registry: customer package failed to load — skipping');
475
- failures.push({ packagePath, source, error: message });
506
+ recordPackageFailure(err, packagePath, source, strict, failures, logger);
476
507
  }
477
508
  }
478
509
  return { loaded, failures };
479
510
  }
511
+ /**
512
+ * One package failed to load. Strict mode rethrows a BASELINE failure, so the
513
+ * CI gates that load the baseline strictly stay hard. Otherwise — every customer
514
+ * failure, and a baseline failure outside strict mode — the package is logged,
515
+ * recorded as a `load-error` `LoadFailure`, and skipped: the rest of the
516
+ * catalog still loads. A baseline failure at runtime usually means the host's
517
+ * cli-sdk is older than the one the bundle was built with and can't read one
518
+ * package; failing the whole load for that would drop every tool on every
519
+ * tenant.
520
+ */
521
+ function recordPackageFailure(err, packagePath, source, strict, failures, logger) {
522
+ if (strict && source === 'baseline')
523
+ throw err;
524
+ const message = err instanceof Error ? err.message : String(err);
525
+ logger?.error({ packagePath, source, error: message }, `registry: ${source} package failed to load — skipping it, the rest of the catalog still loads`);
526
+ failures.push({ packagePath, source, error: message, kind: 'load-error' });
527
+ }
480
528
  /**
481
529
  * Lazy variant of {@link scanPath}. Per package:
482
530
  * - a valid `manifest.json` ⇒ catalog it as a lazy CLI (no module import); the
483
531
  * entry point is resolved now so `resolveTool` can import it later.
484
532
  * - no/invalid manifest ⇒ import eagerly, exactly as `scanPath` does (this is
485
533
  * how connection packages and any manifest-less CLI keep working).
486
- * Failure handling mirrors `scanPath`: a broken BASELINE package throws (bad
487
- * deploy); a broken CUSTOMER package is recorded and skipped.
534
+ * Failure handling mirrors `scanPath` ({@link recordPackageFailure}): a broken
535
+ * package is recorded and skipped, except a BASELINE one in strict mode, which
536
+ * throws.
488
537
  */
489
- async function scanPathLazy(root, source, logger) {
538
+ async function scanPathLazy(root, source, strict, logger) {
490
539
  let dirents;
491
540
  try {
492
541
  dirents = await fs.readdir(root, { withFileTypes: true });
@@ -508,58 +557,54 @@ async function scanPathLazy(root, source, logger) {
508
557
  .sort();
509
558
  for (const name of subdirs) {
510
559
  const packagePath = path.join(root, name);
511
- const manifest = await readCliManifest(packagePath, logger);
512
- if (manifest) {
513
- const entryPoint = await resolveEntryPoint(packagePath);
514
- if (!entryPoint) {
515
- // A manifest we could serve on /tools/list but never execute is worse
516
- // than absent. Match the eager loader, which SKIPS a package with no
517
- // resolvable entry point (see scanPath) — so the lazy catalog advertises
518
- // exactly the packages the eager one would load, never a phantom tool.
519
- logger?.warn({ packagePath, source }, 'registry: manifest present but no resolvable entry point — skipping (cannot execute)');
520
- continue;
521
- }
522
- lazyClis.push({
523
- cliId: manifest.id,
524
- manifest,
525
- source,
526
- packagePath,
527
- entryPoint,
528
- override: manifest.override === true,
529
- });
530
- // MIXED package (AKAI-2408): the CLI manifest embeds its sibling connection
531
- // so a mixed package loads LAZILY instead of falling back to eager import.
532
- // Surface it as a manifest-sourced connection so it participates in the
533
- // catalog + slug-collision detection exactly like an eager ConnectionEntry.
534
- // Its handler-less identity needs no module import — only the CLI's does.
535
- if (manifest.connection) {
536
- manifestConns.push({
537
- slug: manifest.connection.slug,
538
- connection: manifest.connection,
560
+ try {
561
+ const manifest = await readCliManifest(packagePath, logger);
562
+ if (manifest) {
563
+ const entryPoint = await resolveEntryPoint(packagePath);
564
+ if (!entryPoint) {
565
+ // A manifest we could serve on /tools/list but never execute is worse
566
+ // than absent. Match the eager loader, which SKIPS a package with no
567
+ // resolvable entry point (see scanPath) — so the lazy catalog advertises
568
+ // exactly the packages the eager one would load, never a phantom tool.
569
+ logger?.warn({ packagePath, source }, 'registry: manifest present but no resolvable entry point — skipping (cannot execute)');
570
+ continue;
571
+ }
572
+ lazyClis.push({
573
+ cliId: manifest.id,
574
+ manifest,
539
575
  source,
540
576
  packagePath,
541
- override: manifest.connection.override === true,
577
+ entryPoint,
578
+ override: manifest.override === true,
542
579
  });
580
+ // MIXED package (AKAI-2408): the CLI manifest embeds its sibling connection
581
+ // so a mixed package loads LAZILY instead of falling back to eager import.
582
+ // Surface it as a manifest-sourced connection so it participates in the
583
+ // catalog + slug-collision detection exactly like an eager ConnectionEntry.
584
+ // Its handler-less identity needs no module import — only the CLI's does.
585
+ if (manifest.connection) {
586
+ manifestConns.push({
587
+ slug: manifest.connection.slug,
588
+ connection: manifest.connection,
589
+ source,
590
+ packagePath,
591
+ override: manifest.connection.override === true,
592
+ });
593
+ }
594
+ continue;
595
+ }
596
+ // No usable manifest → eager import (connection packages + manifest-less
597
+ // CLIs), byte-for-byte the same per-package handling as `scanPath`.
598
+ const entryPoint = await resolveEntryPoint(packagePath);
599
+ if (!entryPoint) {
600
+ logger?.warn({ packagePath }, 'registry: skipping — no package.json or resolvable entry point');
601
+ continue;
543
602
  }
544
- continue;
545
- }
546
- // No usable manifest → eager import (connection packages + manifest-less
547
- // CLIs), byte-for-byte the same per-package handling as `scanPath`.
548
- const entryPoint = await resolveEntryPoint(packagePath);
549
- if (!entryPoint) {
550
- logger?.warn({ packagePath }, 'registry: skipping — no package.json or resolvable entry point');
551
- continue;
552
- }
553
- try {
554
603
  for (const item of await loadPackageEntry(entryPoint, packagePath, source))
555
604
  eager.push(item);
556
605
  }
557
606
  catch (err) {
558
- if (source === 'baseline')
559
- throw err;
560
- const message = err instanceof Error ? err.message : String(err);
561
- logger?.error({ packagePath, source, error: message }, 'registry: customer package failed to load — skipping');
562
- failures.push({ packagePath, source, error: message });
607
+ recordPackageFailure(err, packagePath, source, strict, failures, logger);
563
608
  }
564
609
  }
565
610
  return { lazyClis, manifestConns, eager, failures };
@@ -639,7 +684,10 @@ async function resolveEntryPoint(packagePath) {
639
684
  * an array of them (the `connections/` package default-exports a Connection[],
640
685
  * a customer package may export a mixed `[cli, connection]`). Each item is
641
686
  * brand-checked as a CLI xor a Connection; anything else fails fast so a
642
- * malformed package can't half-load the registry.
687
+ * malformed package can't half-load the registry. The one exception is a
688
+ * connection this cli-sdk cannot load because it was most likely written for a
689
+ * newer one (see `checkConnection`): it comes back as `unsupported-connection`
690
+ * so the caller leaves out only that connection.
643
691
  */
644
692
  async function loadPackageEntry(entryPoint, packagePath, source) {
645
693
  // Dynamic import via file:// URL so absolute paths outside node_modules
@@ -663,13 +711,31 @@ async function loadPackageEntry(entryPoint, packagePath, source) {
663
711
  loaded.push({ kind: 'cli', entry: { cli: candidate, source, packagePath } });
664
712
  continue;
665
713
  }
666
- if (isBrandedConnection(candidate)) {
714
+ const check = checkConnection(candidate);
715
+ if (check.verdict === 'valid') {
667
716
  loaded.push({
668
717
  kind: 'connection',
669
718
  entry: { connection: candidate, source, packagePath },
670
719
  });
671
720
  continue;
672
721
  }
722
+ if (check.verdict === 'unsupported') {
723
+ // Most likely written for a newer cli-sdk. Leave out THIS connection and keep
724
+ // the rest of the package: rejecting the package would take every connection
725
+ // (and, for a baseline package, every tool) down with it.
726
+ loaded.push({
727
+ kind: 'unsupported-connection',
728
+ failure: {
729
+ packagePath,
730
+ source,
731
+ kind: 'unsupported-connection',
732
+ slug: check.slug,
733
+ error: `registry: connection '${check.slug}' (${packagePath}) is unavailable — ${check.reason}. ` +
734
+ 'It was probably defined with a newer @akai-workflow-builder/cli-sdk; upgrade the loading service to load it',
735
+ },
736
+ });
737
+ continue;
738
+ }
673
739
  throw new Error(`registry: ${packagePath} default export is neither a defineCLI() nor a defineConnection() result (missing __akaiCLI/__akaiConnection brand or required fields)`);
674
740
  }
675
741
  return loaded;
@@ -719,6 +785,30 @@ function isBrandedCLI(x) {
719
785
  return false;
720
786
  return true;
721
787
  }
788
+ const INVALID_CONNECTION = { verdict: 'invalid' };
789
+ const VALID_CONNECTION = { verdict: 'valid' };
790
+ const CONNECTION_AUTH_KINDS = new Set(['apiKey', 'oauth2', 'none']);
791
+ /** Every top-level field a `defineConnection()` result can carry in this cli-sdk.
792
+ * Anything else was added by a newer cli-sdk; it is named in the reason when the
793
+ * connection is left out, so the log says what the old loader could not read. */
794
+ const KNOWN_CONNECTION_FIELDS = new Set([
795
+ '__akaiConnection',
796
+ 'slug',
797
+ 'name',
798
+ 'vendor',
799
+ 'blurb',
800
+ 'longDesc',
801
+ 'instructions',
802
+ 'secrets',
803
+ 'apiKeyHeader',
804
+ 'hosts',
805
+ 'authKind',
806
+ 'oauth',
807
+ 'jwtRefresh',
808
+ 'nativeBuiltin',
809
+ 'mcpServer',
810
+ 'override',
811
+ ]);
722
812
  /**
723
813
  * Brand + shape check for a `defineConnection()` result. Same rigor as
724
814
  * `isBrandedCLI`: the wire serializer reads every field unconditionally, so a
@@ -726,85 +816,117 @@ function isBrandedCLI(x) {
726
816
  * precomputed. A customer package exporting a mixed `[cli, connection]` default
727
817
  * relies on this to disambiguate from `isBrandedCLI` — the `__akaiConnection`
728
818
  * brand and `__akaiCLI` brand are mutually exclusive on a real def.
819
+ *
820
+ * Wrong types on known fields are `invalid`. Unknown values and combinations of
821
+ * otherwise well-typed fields are `unsupported` (see {@link ConnectionCheck}).
822
+ * Unknown extra fields alone do not reject a connection — it loads as before, with
823
+ * the fields this cli-sdk does not read ignored.
729
824
  */
730
- function isBrandedConnection(x) {
825
+ function checkConnection(x) {
731
826
  if (x === null || typeof x !== 'object')
732
- return false;
827
+ return INVALID_CONNECTION;
733
828
  const o = x;
734
829
  if (o.__akaiConnection !== true)
735
- return false;
830
+ return INVALID_CONNECTION;
736
831
  if (o.__akaiCLI === true)
737
- return false; // brands are mutually exclusive
832
+ return INVALID_CONNECTION; // brands are mutually exclusive
738
833
  if (typeof o.slug !== 'string' || o.slug.length === 0)
739
- return false;
834
+ return INVALID_CONNECTION;
740
835
  if (typeof o.name !== 'string' || o.name.length === 0)
741
- return false;
836
+ return INVALID_CONNECTION;
742
837
  if (typeof o.vendor !== 'string' || o.vendor.length === 0)
743
- return false;
838
+ return INVALID_CONNECTION;
744
839
  if (typeof o.blurb !== 'string')
745
- return false;
840
+ return INVALID_CONNECTION;
746
841
  if (typeof o.longDesc !== 'string')
747
- return false;
842
+ return INVALID_CONNECTION;
748
843
  if (!Array.isArray(o.instructions) || !o.instructions.every((i) => typeof i === 'string')) {
749
- return false;
750
- }
751
- if (!isConnectionAuthKind(o.authKind))
752
- return false;
753
- // A hosted MCP server connection (no CLIs, auth via the server's own discovered OAuth or a token
754
- // each person pastes) has its own rules, mirroring defineConnection — and is exempt from the
755
- // oauth2-requires-oauth rule.
756
- if (o.mcpServer !== undefined) {
757
- if (!isHostedMcpConnection(o))
758
- return false;
759
- }
760
- else {
761
- // oauth is required for oauth2 and forbidden for all other authKinds.
762
- if (o.authKind === 'oauth2' && o.oauth === undefined)
763
- return false;
764
- if (o.authKind !== 'oauth2' && o.oauth !== undefined)
765
- return false;
844
+ return INVALID_CONNECTION;
766
845
  }
846
+ if (typeof o.authKind !== 'string' || o.authKind.length === 0)
847
+ return INVALID_CONNECTION;
767
848
  if (o.oauth !== undefined && !isBrandedConnectionOAuth(o.oauth))
768
- return false;
849
+ return INVALID_CONNECTION;
769
850
  // Optional wire-relevant fields — the /tools/list serializer reads each
770
851
  // unconditionally, so a present-but-malformed value must fail at load, not
771
852
  // when the list payload is precomputed.
772
853
  if (o.secrets !== undefined && (!Array.isArray(o.secrets) || !o.secrets.every(isBrandedSecretDecl))) {
773
- return false;
854
+ return INVALID_CONNECTION;
774
855
  }
775
856
  if (o.apiKeyHeader !== undefined && typeof o.apiKeyHeader !== 'string')
776
- return false;
857
+ return INVALID_CONNECTION;
777
858
  if (o.hosts !== undefined && !isConnectionHosts(o.hosts))
778
- return false;
859
+ return INVALID_CONNECTION;
779
860
  if (o.nativeBuiltin !== undefined && typeof o.nativeBuiltin !== 'boolean')
780
- return false;
861
+ return INVALID_CONNECTION;
781
862
  // Optional + additive: the cross-repo override opt-in (see BrandedCLI).
782
863
  if (o.override !== undefined && typeof o.override !== 'boolean')
783
- return false;
784
- return true;
864
+ return INVALID_CONNECTION;
865
+ if (o.mcpServer !== undefined) {
866
+ const server = o.mcpServer;
867
+ if (server === null || typeof server !== 'object' || Array.isArray(server))
868
+ return INVALID_CONNECTION;
869
+ const url = server.url;
870
+ if (url !== undefined && typeof url !== 'string')
871
+ return INVALID_CONNECTION;
872
+ }
873
+ // Every known field is well typed. What is left is what a newer cli-sdk can change.
874
+ const problem = unsupportedConnectionProblem(o);
875
+ if (problem === undefined)
876
+ return VALID_CONNECTION;
877
+ const unknown = Object.keys(o).filter((k) => !KNOWN_CONNECTION_FIELDS.has(k));
878
+ const reason = unknown.length > 0 ? `${problem}; fields this cli-sdk does not know: ${unknown.join(', ')}` : problem;
879
+ return { verdict: 'unsupported', slug: o.slug, reason };
880
+ }
881
+ /** The first value or combination on a well-typed connection this cli-sdk does not
882
+ * support, or `undefined` when it supports all of them. */
883
+ function unsupportedConnectionProblem(o) {
884
+ if (!CONNECTION_AUTH_KINDS.has(o.authKind))
885
+ return `unknown authKind '${String(o.authKind)}'`;
886
+ if (o.oauth !== undefined) {
887
+ const oauthProblem = unsupportedOAuthValue(o.oauth);
888
+ if (oauthProblem !== undefined)
889
+ return oauthProblem;
890
+ }
891
+ // A hosted MCP server connection (no CLIs, auth via the server's own discovered OAuth or a token
892
+ // each person pastes) has its own rules, mirroring defineConnection — and is exempt from the
893
+ // oauth2-requires-oauth rule.
894
+ if (o.mcpServer !== undefined) {
895
+ const mcpProblem = hostedMcpProblem(o);
896
+ return mcpProblem === undefined ? undefined : `mcpServer with ${mcpProblem}`;
897
+ }
898
+ // oauth is required for oauth2 and forbidden for all other authKinds.
899
+ if (o.authKind === 'oauth2' && o.oauth === undefined)
900
+ return "authKind 'oauth2' without an oauth block";
901
+ if (o.authKind !== 'oauth2' && o.oauth !== undefined) {
902
+ return `an oauth block with authKind '${String(o.authKind)}'`;
903
+ }
904
+ return undefined;
785
905
  }
786
906
  /**
787
907
  * Rules for a connection declaring `mcpServer` (mirrors defineConnection): an https `url`, any
788
908
  * `authKind`, `apiKeyHeader` only with `apiKey`, and no `oauth` block, secrets, jwtRefresh or
789
- * `nativeBuiltin: true`. Standalone by design — no CLI has to link to it for it to load.
909
+ * `nativeBuiltin: true`. Standalone by design — no CLI has to link to it for it to load. Returns
910
+ * the first rule broken, or `undefined`.
790
911
  */
791
- function isHostedMcpConnection(o) {
792
- const server = o.mcpServer;
793
- if (server === null || typeof server !== 'object')
794
- return false;
795
- const url = server.url;
796
- if (typeof url !== 'string' || !isHttpsUrl(url))
797
- return false;
798
- if (o.apiKeyHeader !== undefined && o.authKind !== 'apiKey')
799
- return false;
800
- if (o.oauth !== undefined || o.jwtRefresh !== undefined)
801
- return false;
912
+ function hostedMcpProblem(o) {
913
+ const url = o.mcpServer.url;
914
+ if (typeof url !== 'string')
915
+ return 'no url';
916
+ if (!isHttpsUrl(url))
917
+ return 'a url that is not https';
918
+ if (o.apiKeyHeader !== undefined && o.authKind !== 'apiKey') {
919
+ return `apiKeyHeader and authKind '${String(o.authKind)}'`;
920
+ }
921
+ if (o.oauth !== undefined)
922
+ return 'an oauth block';
923
+ if (o.jwtRefresh !== undefined)
924
+ return 'jwtRefresh';
802
925
  if (Array.isArray(o.secrets) && o.secrets.length > 0)
803
- return false;
804
- return o.nativeBuiltin !== true;
805
- }
806
- function isConnectionAuthKind(x) {
807
- return x === 'apiKey' || x === 'oauth2' || x === 'none';
926
+ return 'secrets';
927
+ if (o.nativeBuiltin === true)
928
+ return 'nativeBuiltin';
929
+ return undefined;
808
930
  }
809
931
  /** Validate the optional per-env host map — known keys must be strings when present. */
810
932
  function isConnectionHosts(x) {
@@ -832,14 +954,12 @@ function isBrandedConnectionOAuth(x) {
832
954
  }
833
955
  if (o.pkce !== undefined && typeof o.pkce !== 'boolean')
834
956
  return false;
835
- if (o.tokenEndpointAuthMethod !== undefined &&
836
- o.tokenEndpointAuthMethod !== 'client_secret_post' &&
837
- o.tokenEndpointAuthMethod !== 'client_secret_basic') {
957
+ // Enum-valued fields only need to be strings here: an unknown VALUE is a newer
958
+ // cli-sdk's, checked by `unsupportedOAuthValue`, not a malformed package.
959
+ if (o.tokenEndpointAuthMethod !== undefined && typeof o.tokenEndpointAuthMethod !== 'string')
838
960
  return false;
839
- }
840
- if (o.refreshStrategy !== undefined && o.refreshStrategy !== 'cron' && o.refreshStrategy !== 'jit') {
961
+ if (o.refreshStrategy !== undefined && typeof o.refreshStrategy !== 'string')
841
962
  return false;
842
- }
843
963
  if (o.extraAuthorizeParams !== undefined && !isStringRecord(o.extraAuthorizeParams))
844
964
  return false;
845
965
  if (!Array.isArray(o.adminFields) || !o.adminFields.every(isBrandedOAuthAdminField))
@@ -861,9 +981,8 @@ function isBrandedAppToken(x) {
861
981
  const o = x;
862
982
  if (typeof o.tokenUrl !== 'string' || o.tokenUrl.length === 0)
863
983
  return false;
864
- if (o.requestStyle !== undefined && o.requestStyle !== 'oauth2_client_credentials' && o.requestStyle !== 'json') {
984
+ if (o.requestStyle !== undefined && typeof o.requestStyle !== 'string')
865
985
  return false;
866
- }
867
986
  if (o.credentialParams !== undefined) {
868
987
  const c = o.credentialParams;
869
988
  if (c === null || typeof c !== 'object')
@@ -878,6 +997,22 @@ function isBrandedAppToken(x) {
878
997
  return false;
879
998
  return true;
880
999
  }
1000
+ /** The first OAuth enum value this cli-sdk does not know, or `undefined`. Runs on an
1001
+ * oauth block {@link isBrandedConnectionOAuth} already accepted, so every field is typed. */
1002
+ function unsupportedOAuthValue(o) {
1003
+ const method = o.tokenEndpointAuthMethod;
1004
+ if (method !== undefined && method !== 'client_secret_post' && method !== 'client_secret_basic') {
1005
+ return `unknown oauth.tokenEndpointAuthMethod '${String(method)}'`;
1006
+ }
1007
+ if (o.refreshStrategy !== undefined && o.refreshStrategy !== 'cron' && o.refreshStrategy !== 'jit') {
1008
+ return `unknown oauth.refreshStrategy '${String(o.refreshStrategy)}'`;
1009
+ }
1010
+ const style = o.appToken?.requestStyle;
1011
+ if (style !== undefined && style !== 'oauth2_client_credentials' && style !== 'json') {
1012
+ return `unknown oauth.appToken.requestStyle '${String(style)}'`;
1013
+ }
1014
+ return undefined;
1015
+ }
881
1016
  function isStringRecord(x) {
882
1017
  if (x === null || typeof x !== 'object' || Array.isArray(x))
883
1018
  return false;
@@ -989,6 +1124,8 @@ function isBrandedTool(x) {
989
1124
  // 'secret:<KEY>' }` resolved per-invocation from a
990
1125
  // tenant property/secret (SDK DynamicEgress). `http`
991
1126
  // is the implicit default when `transport` is unset.
1127
+ // transport: 'sftp' → `options.network.egress` MUST be a dynamic-egress
1128
+ // ref (the host:port allowlist is per connection).
992
1129
  if (o.transport === 'native') {
993
1130
  if (opts.network !== undefined)
994
1131
  return false;
@@ -1006,6 +1143,8 @@ function isBrandedTool(x) {
1006
1143
  DYNAMIC_EGRESS_PREFIXES.some((p) => from.startsWith(p) && from.length > p.length);
1007
1144
  if (!isHostArray && !isDynamic)
1008
1145
  return false;
1146
+ if (o.transport === 'sftp' && !isDynamic)
1147
+ return false;
1009
1148
  }
1010
1149
  return true;
1011
1150
  }