@capybara-hq/kernel 0.1.0-rc.84 → 0.1.0-rc.85

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 (89) hide show
  1. package/dist/admin/admin.module.js +5 -0
  2. package/dist/admin/health.service.d.ts +2 -2
  3. package/dist/admin/health.service.js +10 -2
  4. package/dist/catalog/admin-catalog.controller.d.ts +2 -1
  5. package/dist/catalog/admin-catalog.controller.js +2 -1
  6. package/dist/catalog/catalog-content.d.ts +5 -15
  7. package/dist/catalog/catalog-content.js +5 -56
  8. package/dist/catalog/catalog-scope.service.d.ts +3 -3
  9. package/dist/catalog/catalog-scope.service.js +4 -3
  10. package/dist/catalog/catalog.module.d.ts +3 -1
  11. package/dist/catalog/catalog.module.js +14 -1
  12. package/dist/catalog/component-scope.service.d.ts +6 -1
  13. package/dist/catalog/component-scope.service.js +16 -2
  14. package/dist/catalog/job-ids.d.ts +29 -0
  15. package/dist/catalog/job-ids.js +29 -0
  16. package/dist/catalog/jobs.d.ts +12 -3
  17. package/dist/catalog/jobs.js +28 -7
  18. package/dist/catalog/org-catalog.controller.d.ts +2 -30
  19. package/dist/catalog/org-catalog.controller.js +3 -29
  20. package/dist/catalog/org-catalog.schemas.d.ts +9 -46
  21. package/dist/catalog/org-catalog.schemas.js +9 -23
  22. package/dist/catalog/org-catalog.service.js +14 -23
  23. package/dist/catalog/plugin-import.d.ts +21 -0
  24. package/dist/catalog/plugin-import.js +33 -3
  25. package/dist/catalog/plugin-install.d.ts +69 -76
  26. package/dist/catalog/plugin-install.js +156 -234
  27. package/dist/catalog/plugin-upgrade.d.ts +71 -11
  28. package/dist/catalog/plugin-upgrade.js +222 -43
  29. package/dist/catalog/scope-sync.d.ts +22 -20
  30. package/dist/catalog/scope-sync.js +57 -42
  31. package/dist/catalog/template-connector-ensure.d.ts +100 -0
  32. package/dist/catalog/template-connector-ensure.js +285 -0
  33. package/dist/catalog/template-connector-jobs.d.ts +28 -0
  34. package/dist/catalog/template-connector-jobs.js +54 -0
  35. package/dist/catalog/template-connector-reconcile.d.ts +44 -0
  36. package/dist/catalog/template-connector-reconcile.js +114 -0
  37. package/dist/catalog/template-connectors.processor.d.ts +32 -0
  38. package/dist/catalog/template-connectors.processor.js +101 -0
  39. package/dist/db/kernel-schema.d.ts +2 -1
  40. package/dist/extensions/org-config.d.ts +6 -1
  41. package/dist/extensions/org-config.js +15 -2
  42. package/dist/git/gateway.js +3 -1
  43. package/dist/infra/outbox/registry.d.ts +28 -0
  44. package/dist/infra/outbox/registry.js +33 -0
  45. package/dist/infra/queue/queue-names.d.ts +1 -1
  46. package/dist/infra/queue/queue-names.js +2 -0
  47. package/dist/lifecycle/kernel-lifecycle.d.ts +8 -1
  48. package/dist/lifecycle/kernel-lifecycle.js +37 -11
  49. package/dist/orgs/org-owner.d.ts +7 -0
  50. package/dist/orgs/org-owner.js +16 -0
  51. package/dist/platform/operation.d.ts +3 -4
  52. package/dist/platform/ops/connectors.ops.js +8 -1
  53. package/dist/platform/ops/org-plugins.ops.d.ts +0 -9
  54. package/dist/platform/ops/org-plugins.ops.js +18 -235
  55. package/dist/platform/ops/plugins.ops.d.ts +0 -1
  56. package/dist/platform/ops/plugins.ops.js +3 -31
  57. package/dist/roles/worker-role.d.ts +9 -4
  58. package/dist/roles/worker-role.js +18 -5
  59. package/dist/roles/worker.module.d.ts +1 -1
  60. package/dist/roles/worker.module.js +8 -1
  61. package/dist/setup/setup-guides.service.d.ts +9 -1
  62. package/dist/setup/setup-guides.service.js +38 -8
  63. package/dist/tools/admin-connectors.d.ts +3 -2
  64. package/dist/tools/admin-connectors.js +5 -4
  65. package/dist/tools/connector-setup.d.ts +28 -0
  66. package/dist/tools/connector-setup.js +33 -0
  67. package/dist/tools/connector-tools-jobs.d.ts +35 -0
  68. package/dist/tools/connector-tools-jobs.js +40 -0
  69. package/dist/tools/connector-tools.processor.d.ts +32 -0
  70. package/dist/tools/connector-tools.processor.js +97 -0
  71. package/dist/tools/connectors.controller.d.ts +1 -0
  72. package/dist/tools/connectors.schemas.d.ts +1 -0
  73. package/dist/tools/connectors.schemas.js +5 -0
  74. package/dist/tools/connectors.service.d.ts +138 -14
  75. package/dist/tools/connectors.service.js +410 -50
  76. package/dist/tools/credentials.d.ts +35 -0
  77. package/dist/tools/credentials.js +89 -1
  78. package/dist/tools/drift.d.ts +5 -0
  79. package/dist/tools/drift.js +21 -2
  80. package/dist/tools/editable-endpoint.d.ts +23 -0
  81. package/dist/tools/editable-endpoint.js +92 -0
  82. package/dist/tools/oauth/flow.d.ts +1 -1
  83. package/dist/tools/oauth/flow.js +3 -2
  84. package/dist/tools/pipeline.js +14 -5
  85. package/dist/tools/registry.d.ts +14 -2
  86. package/dist/tools/registry.js +27 -6
  87. package/dist/tools/tools.module.js +4 -0
  88. package/package.json +2 -2
  89. package/src/migrations/0183_connectors_created_by_nullable.sql +5 -0
@@ -5,8 +5,12 @@ import type { WriteActor } from "../identity/write-actor.js";
5
5
  import { KernelConfigService } from "../infra/config/kernel-config.service.js";
6
6
  import { KernelDb } from "../infra/database/kernel-db.js";
7
7
  import { TransactionHost } from "../infra/database/transaction-host.js";
8
+ import { LockService } from "../infra/lock/lock.service.js";
9
+ import { OutboxService } from "../infra/outbox/outbox.service.js";
8
10
  import { OrgLock } from "../orgs/org-lock.js";
9
11
  import type { OrgRole } from "../orgs/roles.js";
12
+ import { type ConnectorSetup } from "./connector-setup.js";
13
+ import { type ConnectorToolsJobData } from "./connector-tools-jobs.js";
10
14
  import { type ConnectorAuth, type CredentialOwner, Credentials, type StaticSecret } from "./credentials.js";
11
15
  import { type DriftChanges, ToolDrift, type ToolIssue } from "./drift.js";
12
16
  import { type OAuthClientRecord, type OAuthTokenRecord } from "./oauth/tokens.js";
@@ -64,6 +68,12 @@ export interface ConnectorSummary {
64
68
  endpoint: unknown;
65
69
  auth: ConnectorAuth;
66
70
  status: "active" | "disabled";
71
+ /**
72
+ * Derived on every read, never stored (plugin pages spec §5.2.1): `needs_config` while the org
73
+ * input its auth needs (an OAuth client or an org credential) is absent; such a connector is
74
+ * never called. Independent of `status`.
75
+ */
76
+ setup: ConnectorSetup;
67
77
  /**
68
78
  * The platform connector template it was made from (`connectors.platform_template_id`), null for
69
79
  * a custom one: the web offers it for a pending connector of that template (ruling P4-7).
@@ -91,7 +101,8 @@ export interface ConnectorSummary {
91
101
  * then show its template's, `templateDescription` on the connector routes).
92
102
  */
93
103
  description: string | null;
94
- createdBy: string;
104
+ /** Null: the kernel created it as no person (a plugin scope sync, ruling R17). */
105
+ createdBy: string | null;
95
106
  createdAt: Date;
96
107
  updatedAt: Date;
97
108
  }
@@ -230,6 +241,11 @@ export interface UpdateConnectorInput {
230
241
  useForGit?: boolean | undefined;
231
242
  /** The connector's own description; null or blank removes it. */
232
243
  description?: string | null | undefined;
244
+ /**
245
+ * Endpoint fields to change on a connector made from a connector template, only at the paths
246
+ * its current template lists in `editableEndpoint` (merged onto the stored endpoint).
247
+ */
248
+ endpoint?: Record<string, unknown> | undefined;
233
249
  }
234
250
  /** OAuth 安装准备的结果:要存的客户端,以及(客户端凭据流)安装时取到的组织 token。 */
235
251
  export interface OAuthSetup {
@@ -253,6 +269,10 @@ export declare class ConnectorsService {
253
269
  private readonly drift;
254
270
  /** The org row lock (orgs/org-lock.ts): joins the caller's transaction. */
255
271
  private readonly orgLock;
272
+ /** Owed work: the tool listings that follow a deferred create or a setup (§5.2.1). */
273
+ private readonly outbox;
274
+ /** The cross-replica lock of a deferred connector's lazy client registration (R16). */
275
+ private readonly locks;
256
276
  readonly txHost: TransactionHost;
257
277
  /** Setup guides of connector templates (spec 2026-11-09-setup-guides-v2-design.md §11). */
258
278
  private readonly guides;
@@ -263,7 +283,11 @@ export declare class ConnectorsService {
263
283
  */
264
284
  extensions: Pick<ExtensionRegistry, "manifests">, credentials: Credentials, config: Pick<KernelConfigService, "get">, drift: ToolDrift,
265
285
  /** The org row lock (orgs/org-lock.ts): joins the caller's transaction. */
266
- orgLock: Pick<OrgLock, "lock" | "lockAndAuthorize">, txHost: TransactionHost,
286
+ orgLock: Pick<OrgLock, "lock" | "tryLock" | "lockAndAuthorize">,
287
+ /** Owed work: the tool listings that follow a deferred create or a setup (§5.2.1). */
288
+ outbox: Pick<OutboxService, "add">,
289
+ /** The cross-replica lock of a deferred connector's lazy client registration (R16). */
290
+ locks: Pick<LockService, "run">, txHost: TransactionHost,
267
291
  /** Setup guides of connector templates (spec 2026-11-09-setup-guides-v2-design.md §11). */
268
292
  guides: Pick<PluginGuides, "templateAck" | "connectorStates">);
269
293
  /** The kernel-schema handle of the current context (its transaction, else the pool). */
@@ -291,8 +315,12 @@ export declare class ConnectorsService {
291
315
  * endpoint against the connector's schema, the credential shape, the OAuth client and the OAuth
292
316
  * URL policy. `create` runs them first; a plugin install runs them for every template before it
293
317
  * creates anything, so bad input is a 400 rather than a failed install. Returns what they parsed.
318
+ * `deferred` ({@link createDeferredIn}): an OAuth connector takes no client (an org admin saves
319
+ * it on the connector later) and needs no `http.publicUrl` yet (the connect flow requires it).
294
320
  */
295
- checkCreateInput(actorAccountId: string, input: CreateConnectorInput): {
321
+ checkCreateInput(actorAccountId: string | null, input: CreateConnectorInput, opts?: {
322
+ deferred?: boolean;
323
+ }): {
296
324
  slug: string;
297
325
  endpoint: unknown;
298
326
  owner?: CredentialOwner;
@@ -310,6 +338,35 @@ export declare class ConnectorsService {
310
338
  }>;
311
339
  /** The org-locked, re-checked insert of {@link create} (the outbound checks ran before). */
312
340
  private createIn;
341
+ /**
342
+ * Creates a connector without listing its tools (plugin pages spec §5.2.1 "Tool lists without
343
+ * a network call in a transaction"): for plugin installs, scope syncs and upgrades, which create
344
+ * connectors inside their own transaction. It makes no outbound call: the row is written with an
345
+ * empty tool list (`tools_synced_at` null, so the first listing counts as the install's and
346
+ * applies the template's default policies), a static credential in `input` is stored, and an
347
+ * OAuth connector takes no client (an org admin saves it, which turns the connector ready).
348
+ * When the connector is `ready` and its credential is not personal, the listing is owed in the
349
+ * same transaction (`connector-tools-list`, trigger `created`); a personal one is listed on its
350
+ * first member credential. `actorAccountId` null: the kernel itself (a scope sync); the
351
+ * connector then has no creator (`created_by` null, ruling R17) and the audit no actor. With an
352
+ * actor, `connectors.manage` is checked under the org lock. A `client: "dcr"` connector
353
+ * registers its client on the first connect ({@link clientForConnect}). Returns the id.
354
+ */
355
+ createDeferredIn(orgId: string, actorAccountId: string | null, input: CreateConnectorInput, opts?: {
356
+ actorTokenId?: string;
357
+ auditDetail?: Record<string, unknown>;
358
+ }): Promise<string>;
359
+ /**
360
+ * The org-locked, re-checked insert shared by {@link createIn} and {@link createDeferredIn}
361
+ * (joins their transaction); returns the snapshot's issues.
362
+ */
363
+ private writeConnectorIn;
364
+ /**
365
+ * Owes a listing of the connector's tools in the caller's transaction (`connector-tools-list`,
366
+ * processed by `ConnectorToolsProcessor` after the commit). `accountId`: the member whose
367
+ * credential lists a personal connector.
368
+ */
369
+ private oweToolList;
313
370
  /**
314
371
  * Checks a template update before anything is written (the platform plugin update): the endpoint
315
372
  * against the connector's schema, and with `resetCredentials` the new credential's shape, or for
@@ -323,12 +380,20 @@ export declare class ConnectorsService {
323
380
  * template's default policies (`template_tool_policies`) and the policies the org did not set
324
381
  * (`policy_source` `template` or `derived`): a tool the new template names takes its policy
325
382
  * (`template`), an unnamed `template` tool returns to the policy derived from its annotations,
326
- * unnamed `derived` tools and the org's own (`admin`) stay (spec §10). Tool definitions are untouched (the next refresh takes the new endpoint through drift review). With `resetCredentials` the credentials are replaced.
383
+ * unnamed `derived` tools and the org's own (`admin`) stay (spec §10). Tool definitions are untouched (the next refresh takes the new endpoint through drift review). With `resetCredentials` the credentials are replaced, and a connector that ends `ready` owes its tool list through the outbox (`template_updated`, ruling R19).
327
384
  * Audits `connector.template_applied`. Deleted / missing -> `connector.not_found`.
328
385
  */
329
386
  applyTemplateIn(orgId: string, connectorId: string,
330
387
  /** null: the kernel itself (a platform update the bootstrap step runs). */
331
388
  actorAccountId: string | null, p: PreparedTemplateUpdate, actorTokenId?: string): Promise<void>;
389
+ /**
390
+ * Whether a connector of `kind` can be created in the org now: the kind is loaded on this
391
+ * replica and its providing extension (the first loaded one, as {@link kinds}) is enabled in
392
+ * the org. The create's own check ({@link assertKindAvailable}); the platform update decides its
393
+ * deferred template connectors with it, so a connector it previews as created is never refused
394
+ * at the confirm for its kind.
395
+ */
396
+ kindAvailable(orgId: string, kind: string): Promise<boolean>;
332
397
  /** Refuses a loaded kind whose extension is not enabled in the org (the same notion as {@link kinds}). */
333
398
  private assertKindAvailable;
334
399
  /**
@@ -354,6 +419,8 @@ export declare class ConnectorsService {
354
419
  private insertTools;
355
420
  /** 读本组织未删除的连接器行;不存在 → `connector.not_found`。`lock` 时 FOR UPDATE。 */
356
421
  private row;
422
+ /** The connector's derived setup state (spec §5.2.1), from its org input's key presence. */
423
+ setupOf(orgId: string, r: Pick<Awaited<ReturnType<ConnectorsService["row"]>>, "id" | "auth">): Promise<ConnectorSetup>;
357
424
  private summary;
358
425
  private detail;
359
426
  /**
@@ -399,8 +466,21 @@ export declare class ConnectorsService {
399
466
  itemId: string | null;
400
467
  }>;
401
468
  get(orgId: string, connectorId: string, viewerAccountId: string): Promise<ConnectorDetail>;
402
- /** 改名、启停连接器、工具启停与默认策略、批量把无注解工具改为 allow(`connectors.manage`)。 */
469
+ /**
470
+ * Renames, switches on or off, sets tool status and default policies, allows unannotated tools
471
+ * in bulk (`connectors.manage`), and changes the editable endpoint fields of a template connector
472
+ * ({@link endpointChange}).
473
+ */
403
474
  update(orgId: string, connectorId: string, actorAccountId: string, patch: UpdateConnectorInput, actorTokenId?: string, auditDetail?: Record<string, unknown>): Promise<ConnectorDetail>;
475
+ /**
476
+ * The endpoint a PATCH makes (under the org lock, the row locked): only a connector made from a
477
+ * live platform connector template (else 400 at `endpoint`), only the paths that template's
478
+ * current `editableEndpoint` lists (`mergeEditableEndpoint`, onto the stored endpoint), checked
479
+ * again as at create (the connector's `endpointSchema`, no credential in the endpoint, a URL for
480
+ * MCP discovery). Its URL policy is the connection's (`net`), applied when the tools are listed.
481
+ * Undefined when nothing changes.
482
+ */
483
+ private endpointChange;
404
484
  /**
405
485
  * The read-only checks of {@link setGitPushPolicy} (the Platform operation's precheck): the
406
486
  * connector exists (`connector.not_found`) and declares git hosts (400 on `policy`); with
@@ -480,11 +560,32 @@ export declare class ConnectorsService {
480
560
  /** The org-locked compare-and-write of {@link refresh} (the tool list was fetched before). */
481
561
  private refreshIn;
482
562
  /**
483
- * 复核一个待复核的工具(`connectors.manage`,spec 7.3)。`approve`:采用待复核的新定义(若有)并启用,
484
- * `reviewed_at` 记为现在;默认策略按新定义的注解重新推导,除非管理员设过(`policy_source = admin`)且来源没变。
485
- * `reject`:停用,丢弃待复核的新定义、保留旧定义;从未复核过的工具被拒后 `reviewed_at` 仍为空(仍可再批准)。
486
- * 工具不存在 → 404 `connector.tool_not_found`;不可复核(已复核且无待复核定义,或已消失)→ 409 `connector.tool_not_pending`;新定义不合规(如 key 不合法)不能通过 → `validation_failed`。
487
- * 采用新定义即换了指纹:旧的「以后都允许」/「本会话都允许」授权按指纹比对自然失效(D8)。审计 `connector.tool_reviewed`。
563
+ * The consumer of a `connector-tools-list` job (`ConnectorToolsProcessor`, after the commit
564
+ * that owed it): lists the connector's tools as {@link refresh} does and stores them with the
565
+ * same compare-and-write, as the kernel itself (audit `connector.tools_refreshed` without an
566
+ * actor). Idempotent: listing again compares against the stored snapshot and changes nothing.
567
+ * Skipped (nothing written) when the connector is gone, disabled or `needs_config`, or the
568
+ * credential to list with is missing (a later save owes a new job); an outbound failure throws,
569
+ * so the queue retries it, and leaves the connector `ready` without tools (the drift scan
570
+ * lists a connector without a snapshot too).
571
+ */
572
+ listToolsForJob(data: ConnectorToolsJobData): Promise<"listed" | "skipped">;
573
+ /**
574
+ * The org-locked write of {@link listToolsForJob}; false (nothing written) when the org or the
575
+ * connector is gone, disabled or `needs_config` by now.
576
+ */
577
+ private storeListedIn;
578
+ /**
579
+ * Reviews a tool pending review (`connectors.manage`, spec 7.3). `approve`: adopts the pending
580
+ * definition (if any) and enables it, `reviewed_at` set to now; the default policy is derived
581
+ * again from the new definition's annotations unless an admin set it (`policy_source = admin`)
582
+ * and the source did not change. `reject`: disables it, drops the pending definition and keeps
583
+ * the old one; a never-reviewed tool keeps `reviewed_at` empty after a rejection (it can still
584
+ * be approved). A missing tool -> 404 `connector.tool_not_found`; not reviewable (reviewed with
585
+ * nothing pending, or gone) -> 409 `connector.tool_not_pending`; a non-compliant new definition
586
+ * (such as an invalid key) cannot pass -> `validation_failed`. Adopting a new definition changes
587
+ * the fingerprint: earlier "always allow" / "allow for this conversation" grants stop matching
588
+ * (D8). Audits `connector.tool_reviewed`.
488
589
  */
489
590
  reviewTool(orgId: string, connectorId: string, name: string, action: "approve" | "reject", actorAccountId: string, actorTokenId?: string): Promise<ConnectorDetail>;
490
591
  /**
@@ -495,14 +596,37 @@ export declare class ConnectorsService {
495
596
  /** The org-locked static credential write of {@link setCredential}. */
496
597
  private setCredentialIn;
497
598
  /**
498
- * 轮换 OAuth 客户端(Ruling A18,组织凭据接口,`connectors.manage`):值为 `{ clientId, clientSecret }`。
499
- * 客户端凭据连接器先用新客户端取一次 token 验证(失败 → 502 `connector.connect_failed`,什么都不改),再替换客户端并以
500
- * 新 token 替换缓存的组织 token;授权码连接器只限手填 client,只替换客户端(成员的 token 保留,之后刷新若得到
501
- * invalid_client 会作废)。审计只记类型与归属。
599
+ * Rotates the OAuth client (Ruling A18, the org credential route, `connectors.manage`): the
600
+ * value is `{ clientId, clientSecret }`. A client credentials connector first fetches a token
601
+ * with the new client to check it (failure -> 502 `connector.connect_failed`, nothing changed),
602
+ * then replaces the client and the cached org token with the new one; an authorization-code
603
+ * connector (manual client only) replaces only the client (members' tokens stay, and a later
604
+ * refresh answered `invalid_client` drops them). The audit records the type and ownership only.
605
+ * A connector with no client yet (`needs_config`, plugin pages spec §5.2.1) gets its first one
606
+ * set up as a create does (the client credentials token, or the authorization-code endpoints
607
+ * from discovery or the auth's URLs), outside any transaction.
502
608
  */
503
609
  private rotateOAuthClient;
504
610
  /** The org-locked client (and token) write of {@link rotateOAuthClient}. */
505
611
  private storeOAuthClient;
612
+ /**
613
+ * The OAuth client a member's connect starts with (`OAuthFlow.start`): the stored one, else, for
614
+ * an authorization-code connector with `client: "dcr"` created without one
615
+ * ({@link createDeferredIn}, plugin pages spec §5.2.1, ruling R16), one registered now: discovery
616
+ * and dynamic registration run once across replicas under the lock
617
+ * `connector-dcr:<connectorId>` (re-read inside it, never inside a transaction), and the client
618
+ * is stored unless another registration stored one first. Null: no client and none to register
619
+ * (a manual client an org admin has not saved). An outbound failure is 502
620
+ * `connector.connect_failed`.
621
+ */
622
+ clientForConnect(orgId: string, connectorId: string, accountId: string): Promise<OAuthClientRecord | null>;
623
+ /**
624
+ * The org-locked write of {@link clientForConnect}: the first stored client wins. Re-checked
625
+ * under the row lock: a connector whose auth stopped being `oauth_code` with `client: "dcr"`
626
+ * meanwhile (a template update), or whose endpoint URL is no longer the one `discoveredAt`
627
+ * registered with (an admin's edit), stores nothing and gets null.
628
+ */
629
+ private storeRegisteredClientIn;
506
630
  /** 删除发起人自己的个人凭据(`connectors.use`);本来就没有也成功。 */
507
631
  clearMyCredential(orgId: string, connectorId: string, actorAccountId: string, actorTokenId?: string, auditDetail?: Record<string, unknown>): Promise<void>;
508
632
  }