@capybara-hq/kernel 0.1.0-rc.142 → 0.1.0-rc.144

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 (238) hide show
  1. package/dist/admin/admin-agents.controller.d.ts +3 -5
  2. package/dist/admin/admin-agents.controller.js +1 -1
  3. package/dist/admin/admin-connectors.controller.d.ts +3 -5
  4. package/dist/admin/admin-connectors.controller.js +1 -1
  5. package/dist/admin/admin-items.schemas.d.ts +6 -6
  6. package/dist/admin/admin-items.schemas.js +5 -9
  7. package/dist/admin/admin.controller.d.ts +18 -12
  8. package/dist/admin/admin.controller.js +29 -15
  9. package/dist/admin/admin.schemas.d.ts +12 -3
  10. package/dist/admin/admin.schemas.js +17 -6
  11. package/dist/agents/admin-agents.d.ts +10 -8
  12. package/dist/agents/admin-agents.js +13 -9
  13. package/dist/agents/agents.module.js +3 -0
  14. package/dist/agents/agents.service.js +1 -1
  15. package/dist/agents/org-templates.d.ts +3 -1
  16. package/dist/agents/org-templates.js +7 -3
  17. package/dist/agents/pending-connector-requests.d.ts +10 -4
  18. package/dist/agents/pending-connector-requests.js +29 -7
  19. package/dist/automations/automations.controller.d.ts +9 -5
  20. package/dist/automations/automations.controller.js +21 -4
  21. package/dist/automations/automations.schemas.d.ts +19 -0
  22. package/dist/automations/automations.schemas.js +9 -0
  23. package/dist/automations/automations.service.d.ts +25 -8
  24. package/dist/automations/automations.service.js +40 -34
  25. package/dist/automations/events/acceptance.d.ts +6 -1
  26. package/dist/automations/events/acceptance.js +30 -11
  27. package/dist/automations/store.d.ts +17 -0
  28. package/dist/automations/store.js +35 -0
  29. package/dist/catalog/admin-catalog.controller.d.ts +64 -2
  30. package/dist/catalog/admin-catalog.controller.js +17 -0
  31. package/dist/catalog/catalog-import.module.js +3 -0
  32. package/dist/catalog/catalog-scope.service.d.ts +3 -1
  33. package/dist/catalog/catalog-scope.service.js +11 -7
  34. package/dist/catalog/catalog.module.js +3 -0
  35. package/dist/catalog/catalog.service.d.ts +3 -1
  36. package/dist/catalog/catalog.service.js +8 -4
  37. package/dist/catalog/cli.js +2 -2
  38. package/dist/catalog/component-promotion.d.ts +3 -1
  39. package/dist/catalog/component-promotion.js +7 -3
  40. package/dist/catalog/component-scope.service.d.ts +3 -1
  41. package/dist/catalog/component-scope.service.js +7 -3
  42. package/dist/catalog/enable-requests.d.ts +10 -8
  43. package/dist/catalog/enable-requests.js +40 -21
  44. package/dist/catalog/marketplaces.service.d.ts +1 -1
  45. package/dist/catalog/owned-items.d.ts +3 -1
  46. package/dist/catalog/owned-items.js +13 -9
  47. package/dist/catalog/platform-components.d.ts +6 -1
  48. package/dist/catalog/platform-components.js +9 -3
  49. package/dist/catalog/plugin-connectors.d.ts +8 -4
  50. package/dist/catalog/plugin-connectors.js +12 -6
  51. package/dist/catalog/plugin-import.d.ts +3 -1
  52. package/dist/catalog/plugin-import.js +14 -5
  53. package/dist/catalog/plugin-install.d.ts +3 -1
  54. package/dist/catalog/plugin-install.js +8 -4
  55. package/dist/catalog/plugin-package.js +2 -1
  56. package/dist/catalog/plugin-upgrade.d.ts +3 -1
  57. package/dist/catalog/plugin-upgrade.js +8 -4
  58. package/dist/catalog/request-hints.d.ts +19 -0
  59. package/dist/catalog/request-hints.js +34 -0
  60. package/dist/catalog/request-rules.d.ts +9 -5
  61. package/dist/catalog/request-rules.js +16 -9
  62. package/dist/catalog/requests.service.d.ts +3 -1
  63. package/dist/catalog/requests.service.js +28 -9
  64. package/dist/catalog/scope-orgs.d.ts +2 -1
  65. package/dist/catalog/scope-orgs.js +2 -2
  66. package/dist/catalog/scope-sync.d.ts +3 -1
  67. package/dist/catalog/scope-sync.js +12 -2
  68. package/dist/catalog/scope-writer.d.ts +6 -3
  69. package/dist/catalog/scope-writer.js +28 -10
  70. package/dist/catalog/unshare.d.ts +3 -2
  71. package/dist/catalog/unshare.js +4 -4
  72. package/dist/client/client-access.service.js +29 -1
  73. package/dist/config/schema.d.ts +12 -0
  74. package/dist/config/schema.js +56 -0
  75. package/dist/ctx/create-ctx.d.ts +2 -19
  76. package/dist/ctx/create-ctx.js +3 -93
  77. package/dist/ctx/durable-relay.d.ts +2 -5
  78. package/dist/ctx/durable-relay.js +56 -29
  79. package/dist/db/extension-handoff-schema.js +5 -0
  80. package/dist/db/kernel-schema.d.ts +74 -0
  81. package/dist/db/migrator.js +36 -0
  82. package/dist/erasure/account-batches.js +2 -0
  83. package/dist/erasure/account-residual.js +1 -0
  84. package/dist/erasure/org-catalog.js +8 -3
  85. package/dist/erasure/org-rules.js +1 -0
  86. package/dist/evals/evals.schemas.d.ts +15 -5
  87. package/dist/evals/evals.schemas.js +17 -10
  88. package/dist/evals/runs.controller.d.ts +4 -12
  89. package/dist/evals/runs.controller.js +14 -11
  90. package/dist/evals/runs.service.d.ts +29 -25
  91. package/dist/evals/runs.service.js +73 -61
  92. package/dist/evals/trajectory.controller.d.ts +10 -6
  93. package/dist/evals/trajectory.controller.js +11 -4
  94. package/dist/evals/trajectory.service.d.ts +17 -9
  95. package/dist/evals/trajectory.service.js +52 -35
  96. package/dist/extensions/admin-ops.d.ts +6 -1
  97. package/dist/extensions/admin-ops.js +21 -7
  98. package/dist/extensions/blobs.d.ts +2 -0
  99. package/dist/extensions/blobs.js +11 -0
  100. package/dist/extensions/deployment-config.d.ts +6 -1
  101. package/dist/extensions/deployment-config.js +13 -2
  102. package/dist/extensions/enabled.d.ts +5 -0
  103. package/dist/extensions/extensions.module.js +3 -0
  104. package/dist/extensions/grant-epoch.d.ts +108 -0
  105. package/dist/extensions/grant-epoch.js +214 -0
  106. package/dist/extensions/host.d.ts +29 -9
  107. package/dist/extensions/host.js +148 -42
  108. package/dist/extensions/instance.d.ts +3 -3
  109. package/dist/extensions/instance.js +1 -1
  110. package/dist/extensions/isolated/authorization.d.ts +54 -2
  111. package/dist/extensions/isolated/authorization.js +148 -35
  112. package/dist/extensions/isolated/child-session.js +0 -1
  113. package/dist/extensions/isolated/revision.js +1 -1
  114. package/dist/extensions/org-config.d.ts +3 -1
  115. package/dist/extensions/org-config.js +13 -4
  116. package/dist/extensions/org-overrides.d.ts +6 -1
  117. package/dist/extensions/org-overrides.js +13 -2
  118. package/dist/extensions/package-digest.job.d.ts +34 -0
  119. package/dist/extensions/package-digest.job.js +121 -0
  120. package/dist/extensions/package.js +10 -6
  121. package/dist/extensions/permission-set.d.ts +14 -78
  122. package/dist/extensions/permission-set.js +17 -143
  123. package/dist/extensions/permission-view.d.ts +13 -1
  124. package/dist/extensions/permission-view.js +20 -0
  125. package/dist/extensions/registry.js +4 -1
  126. package/dist/extensions/resolve.d.ts +7 -8
  127. package/dist/extensions/resolve.js +7 -8
  128. package/dist/extensions/revision-approvals.d.ts +167 -0
  129. package/dist/extensions/revision-approvals.js +353 -0
  130. package/dist/extensions/revision-grants.d.ts +197 -0
  131. package/dist/extensions/revision-grants.js +584 -0
  132. package/dist/extensions/state.module.js +12 -1
  133. package/dist/identity/accounts.d.ts +10 -4
  134. package/dist/identity/accounts.js +18 -5
  135. package/dist/inbox/inbox-items.service.d.ts +6 -1
  136. package/dist/inbox/inbox-items.service.js +29 -10
  137. package/dist/inbox/inbox.module.js +3 -0
  138. package/dist/inbox/inbox.service.d.ts +3 -1
  139. package/dist/inbox/inbox.service.js +9 -2
  140. package/dist/inbox/org-notices.d.ts +4 -3
  141. package/dist/inbox/org-notices.js +9 -6
  142. package/dist/inbox/platform-inbox.service.d.ts +3 -1
  143. package/dist/inbox/platform-inbox.service.js +11 -3
  144. package/dist/inbox/platform-notices.d.ts +3 -2
  145. package/dist/inbox/platform-notices.js +10 -4
  146. package/dist/infra/config/kernel-config.service.js +4 -1
  147. package/dist/infra/database/extension-effect-key-lock.js +7 -0
  148. package/dist/infra/paging/paged-read.d.ts +34 -4
  149. package/dist/infra/paging/paged-read.js +38 -1
  150. package/dist/invalidation/audiences.d.ts +32 -0
  151. package/dist/invalidation/audiences.js +43 -0
  152. package/dist/invalidation/hints.d.ts +6 -0
  153. package/dist/invalidation/hints.js +7 -1
  154. package/dist/invalidation/invalidation-hints.d.ts +12 -1
  155. package/dist/invalidation/invalidation-hints.js +46 -5
  156. package/dist/invalidation/invalidation-hints.module.d.ts +2 -0
  157. package/dist/invalidation/invalidation-hints.module.js +4 -1
  158. package/dist/invalidation/me-stream.d.ts +1 -11
  159. package/dist/invalidation/me-stream.js +12 -41
  160. package/dist/invalidation/org-intent-bridge.d.ts +18 -0
  161. package/dist/invalidation/org-intent-bridge.js +46 -0
  162. package/dist/models/enablement.d.ts +3 -1
  163. package/dist/models/enablement.js +25 -5
  164. package/dist/orgs/invitations.controller.d.ts +4 -7
  165. package/dist/orgs/invitations.controller.js +8 -5
  166. package/dist/orgs/invitations.service.d.ts +17 -3
  167. package/dist/orgs/invitations.service.js +35 -13
  168. package/dist/orgs/members.service.d.ts +35 -0
  169. package/dist/orgs/members.service.js +52 -3
  170. package/dist/orgs/offboarding.d.ts +3 -1
  171. package/dist/orgs/offboarding.js +7 -3
  172. package/dist/orgs/orgs.controller.d.ts +11 -4
  173. package/dist/orgs/orgs.controller.js +25 -5
  174. package/dist/orgs/orgs.module.js +3 -0
  175. package/dist/orgs/orgs.schemas.d.ts +14 -0
  176. package/dist/orgs/orgs.schemas.js +19 -2
  177. package/dist/orgs/orgs.service.d.ts +24 -2
  178. package/dist/orgs/orgs.service.js +47 -4
  179. package/dist/orgs/security-settings.service.d.ts +14 -6
  180. package/dist/orgs/security-settings.service.js +18 -13
  181. package/dist/orgs/settings.controller.d.ts +7 -4
  182. package/dist/platform/exclusions.js +15 -0
  183. package/dist/platform/ops/accounts.ops.js +7 -16
  184. package/dist/platform/ops/admin-items.ops.js +7 -7
  185. package/dist/platform/ops/automations.ops.js +6 -4
  186. package/dist/platform/ops/evals.ops.js +30 -15
  187. package/dist/platform/ops/org-settings.ops.js +2 -2
  188. package/dist/platform/ops/orgs.ops.js +44 -27
  189. package/dist/push/delivery.d.ts +3 -1
  190. package/dist/push/delivery.js +7 -3
  191. package/dist/push/push-processor.js +8 -1
  192. package/dist/roles/worker.module.js +3 -0
  193. package/dist/runtime/step-log.d.ts +1 -1
  194. package/dist/runtime/step-log.js +5 -4
  195. package/dist/runtime/stream-limit.d.ts +13 -0
  196. package/dist/runtime/stream-limit.js +40 -0
  197. package/dist/runtime/stream.controller.d.ts +2 -0
  198. package/dist/runtime/stream.controller.js +2 -0
  199. package/dist/runtime/stream.d.ts +1 -0
  200. package/dist/runtime/stream.js +15 -6
  201. package/dist/sandbox/permission-authority.module.js +6 -1
  202. package/dist/sandbox/permission-transitions.d.ts +3 -1
  203. package/dist/sandbox/permission-transitions.js +17 -2
  204. package/dist/setup/kernel-checks.d.ts +3 -3
  205. package/dist/setup/kernel-checks.js +4 -4
  206. package/dist/setup/setup-check-registry.d.ts +16 -5
  207. package/dist/setup/setup-check-registry.js +11 -3
  208. package/dist/setup/setup-checks.service.d.ts +1 -2
  209. package/dist/setup/setup-checks.service.js +7 -6
  210. package/dist/skills/agent-skills.service.d.ts +6 -1
  211. package/dist/skills/agent-skills.service.js +12 -3
  212. package/dist/skills/changes.service.d.ts +6 -1
  213. package/dist/skills/changes.service.js +11 -3
  214. package/dist/skills/conversation-catalog.d.ts +3 -0
  215. package/dist/skills/conversation-catalog.js +2 -0
  216. package/dist/skills/skills.module.js +3 -0
  217. package/dist/skills/skills.service.d.ts +3 -1
  218. package/dist/skills/skills.service.js +8 -4
  219. package/dist/tools/admin-connectors.d.ts +10 -8
  220. package/dist/tools/admin-connectors.js +12 -10
  221. package/dist/tools/approvals.service.d.ts +12 -1
  222. package/dist/tools/approvals.service.js +36 -9
  223. package/dist/tools/builtins/automations.d.ts +1 -1
  224. package/dist/tools/builtins/automations.js +1 -1
  225. package/dist/tools/connectors.service.d.ts +3 -1
  226. package/dist/tools/connectors.service.js +29 -7
  227. package/dist/tools/tools.module.js +3 -0
  228. package/dist/ui/admin-ui.controller.d.ts +3 -1
  229. package/dist/ui/admin-ui.controller.js +3 -1
  230. package/dist/ui/ui-stream.d.ts +2 -0
  231. package/dist/ui/ui-stream.js +20 -6
  232. package/dist/ui/views.controller.d.ts +4 -2
  233. package/dist/ui/views.controller.js +4 -2
  234. package/package.json +3 -3
  235. package/src/migrations/0234_extension_grant_epoch.sql +141 -0
  236. package/src/migrations/0235_extension_revision_grants.sql +166 -0
  237. package/src/migrations/0236_paged_table_idx.sql +33 -0
  238. package/src/migrations/0237_extension_handoff_health.sql +18 -0
@@ -0,0 +1,167 @@
1
+ import { KernelConfigService } from "../infra/config/kernel-config.service.js";
2
+ import { KernelDb } from "../infra/database/kernel-db.js";
3
+ import { TransactionHost } from "../infra/database/transaction-host.js";
4
+ import { ExtensionApprovals } from "./approvals.js";
5
+ import { ExtensionBlobs } from "./blobs.js";
6
+ import { type PermissionSet } from "./permission-set.js";
7
+ import { ExtensionRevisionGrants, type NewRevisionGrant, type PurposeRow, type RevisionGrant, type RevisionIdentity } from "./revision-grants.js";
8
+ /**
9
+ * Revision approvals in shadow mode (extension permission model plan task 1.2b; the rollout fence
10
+ * "shadow mode until 1.5a"). Every installed revision is resolved against its own revision grant,
11
+ * never against the grant of an older or newer version (no `isOlder`): a rollback rechecks the
12
+ * selected revision's own grant. The result sits next to the legacy snapshot decision, which keeps
13
+ * governing admission: nothing here refuses a start, load or call.
14
+ *
15
+ * No grant is minted from a legacy snapshot approval: that consent never showed purposes, realms or
16
+ * the full-access warning. First grants come from the review (plan task 1.3a) and the bootstrap
17
+ * approval (1.3c); until then every revision resolves to `missing_grant`.
18
+ */
19
+ /** How the enforcing path (plan task 1.5a) would treat one installed revision. */
20
+ export type ShadowGrantStatus =
21
+ /** Its own live grant covers every current key. */
22
+ "granted"
23
+ /** Its own live grant does not serve some current keys; the rest would serve. */
24
+ | "partially_revoked"
25
+ /** No live grant of its own; it waits for review. */
26
+ | "missing_grant"
27
+ /** No live grant, and the manifest lacks bilingual purposes: Republish with purposes. */
28
+ | "missing_purposes"
29
+ /** The package sha512 is not recorded yet (the backfill job fills it); no grant can match. */
30
+ | "unknown_digest"
31
+ /** The stored package could not be read (storage unavailable, too large, refused). */
32
+ | "unreadable";
33
+ /** What the legacy snapshot path, which governs admission until 1.5a, decides. */
34
+ export type LegacyDecision = "approved" | "pending";
35
+ export interface ShadowRevisionDecision {
36
+ revision: {
37
+ plugin: string;
38
+ version: string;
39
+ sha512: string | null;
40
+ };
41
+ status: ShadowGrantStatus;
42
+ /** The generation of the live grant it resolved to. */
43
+ generation?: number;
44
+ /** Current keys its grant revoked or retired. */
45
+ refusedKeys: string[];
46
+ /** Current keys its grant never had (a key set that changed since the review). */
47
+ ungrantedKeys: string[];
48
+ /** Keys without a bilingual purpose. */
49
+ missingPurposes: string[];
50
+ /** Why the approved revision's grant does not carry over as continuity, when there is one. */
51
+ continuity?: string;
52
+ /** A required bootstrap install (the start gate). */
53
+ required: boolean;
54
+ /** Whether the enforcing path would register and serve it ("register only after the grant"). */
55
+ wouldServe: boolean;
56
+ /** The legacy snapshot decision, which governs today. */
57
+ legacy: LegacyDecision;
58
+ /** The legacy and the enforcing decision differ: what the cutover would change. */
59
+ diverges: boolean;
60
+ }
61
+ export interface ShadowStartGate {
62
+ /** Required installs the enforcing start gate would refuse. */
63
+ missingRequired: ShadowRevisionDecision[];
64
+ /** Every other install that would not serve (they would wait as pending). */
65
+ pending: ShadowRevisionDecision[];
66
+ /** Installs whose legacy and enforcing decisions differ: what the cutover would change. */
67
+ diverging: ShadowRevisionDecision[];
68
+ }
69
+ /**
70
+ * The kernel-written purpose of a migration warnings digest key. The key carries the digest, so a
71
+ * revision whose risky migration statements changed has a key its predecessor's grant lacks, and
72
+ * continuity refuses it: the change goes to review.
73
+ */
74
+ export declare function warningsPurpose(key: string): PurposeRow;
75
+ /**
76
+ * The reviewed purpose rows of a permission set, and the keys that lack one. A migration warnings
77
+ * digest key gets the kernel-written row of {@link warningsPurpose}: authors cannot write one, and
78
+ * the grant must still bind the digest.
79
+ */
80
+ export declare function purposeRowsOf(set: PermissionSet): {
81
+ rows: PurposeRow[];
82
+ missing: string[];
83
+ };
84
+ /**
85
+ * The legacy decision for a revision's set against its snapshot, by `planApprovals`' approval rule:
86
+ * no snapshot or a new blocking key waits, unless `extensionPermissions.autoApprove` is on. Its
87
+ * dependency rule (an install whose required providers are all pending waits too) is not mirrored:
88
+ * the shadow report compares approval, not load order.
89
+ */
90
+ export declare function legacyDecision(set: PermissionSet, snapshot: {
91
+ permissions: PermissionSet;
92
+ } | undefined, autoApprove?: boolean): LegacyDecision;
93
+ /**
94
+ * The shadow decision for one installed revision: resolved against its own live grant. `approved`
95
+ * is the live grant of the install's approved revision when that is another revision; it only
96
+ * explains whether continuity could carry over (the publisher evidence is not known here, so it
97
+ * never grants by itself).
98
+ */
99
+ export declare function resolveRevisionGrant(input: {
100
+ revision: {
101
+ plugin: string;
102
+ version: string;
103
+ sha512: string | null;
104
+ };
105
+ set: PermissionSet;
106
+ required: boolean;
107
+ own: RevisionGrant | undefined;
108
+ approved: RevisionGrant | undefined;
109
+ legacy: LegacyDecision;
110
+ }): ShadowRevisionDecision;
111
+ /** What a reviewed promotion carries besides the revision: its verified publisher evidence. */
112
+ export interface PromotionEvidence {
113
+ publisher: NewRevisionGrant["publisher"];
114
+ }
115
+ /**
116
+ * An upgrade may continue the approved revision's grant; a rollback never does: it is served
117
+ * only under the selected revision's own grant.
118
+ */
119
+ export type PromotionKind = "upgrade" | "rollback";
120
+ export type PromotionOutcome = {
121
+ status: "promoted";
122
+ grant: RevisionGrant;
123
+ continued: boolean;
124
+ } | {
125
+ status: "pending";
126
+ reason: string;
127
+ };
128
+ /** The installed revisions' shadow decisions, and promotion with continuity. */
129
+ export declare class ExtensionRevisionApprovals {
130
+ private readonly kdb;
131
+ readonly txHost: TransactionHost;
132
+ private readonly grants;
133
+ private readonly approvals;
134
+ private readonly blobs;
135
+ private readonly config;
136
+ /** Permission sets by package sha256: content-addressed, so a cached set never goes stale. */
137
+ private readonly sets;
138
+ constructor(kdb: KernelDb, txHost: TransactionHost, grants: ExtensionRevisionGrants, approvals: Pick<ExtensionApprovals, "all">, blobs: Pick<ExtensionBlobs, "get">, config: KernelConfigService);
139
+ /**
140
+ * The shadow decision of every install record. A package that cannot be read is reported as
141
+ * `unreadable` and never hides the others.
142
+ */
143
+ decisions(): Promise<ShadowRevisionDecision[]>;
144
+ /** The facts the enforcing start gate would act on; computed and reported, never refused. */
145
+ startGate(): Promise<ShadowStartGate>;
146
+ /**
147
+ * Makes a revision the approved one of its install. A revision with its own live grant is
148
+ * promoted under it (an upgrade or a rollback). An upgrade without one continues the install's
149
+ * approved revision when that has a live grant and the candidate is a continuation of it (the
150
+ * store's rules): the continuity grant is recorded in the same transaction before the promotion.
151
+ * Anything else leaves the revision as the install's candidate, pending review, with the reason;
152
+ * it never throws for a refusal. A continuation carries no full-access realm (narrower, so safe):
153
+ * realms and the `extension.full_access_continued` audit come with plan tasks 1.2e and 1.3a.
154
+ *
155
+ * The package is read before the transaction: no object storage call holds a connection.
156
+ */
157
+ promote(candidate: RevisionIdentity, evidence: PromotionEvidence, kind?: PromotionKind): Promise<PromotionOutcome>;
158
+ private promoteIn;
159
+ /** The stored package of a revision, or undefined while its sha512 is not recorded. */
160
+ private sha256Of;
161
+ private setOf;
162
+ }
163
+ /**
164
+ * The review hash of a continuity grant: the kernel reviewed it against its source, so the hash
165
+ * binds the candidate, its purpose rows and the exact source generation.
166
+ */
167
+ export declare function continuityReviewHash(source: RevisionGrant, candidate: RevisionIdentity, rows: readonly PurposeRow[]): string;
@@ -0,0 +1,353 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
8
+ return function (target, key) { decorator(target, key, paramIndex); }
9
+ };
10
+ import { createHash } from "node:crypto";
11
+ import { SDK_VERSION } from "@capybara-hq/plugin-sdk";
12
+ import { Inject, Injectable } from "@nestjs/common";
13
+ import { KernelConfigService } from "../infra/config/kernel-config.service.js";
14
+ import { KernelDb } from "../infra/database/kernel-db.js";
15
+ import { TransactionHost } from "../infra/database/transaction-host.js";
16
+ import { Transactional } from "../infra/database/transactional.decorator.js";
17
+ import { canonicalJson } from "../tools/canonical-json.js";
18
+ import { ExtensionApprovals } from "./approvals.js";
19
+ import { ExtensionBlobs } from "./blobs.js";
20
+ import { permissionSetOfPackage, readExtensionPackage } from "./package.js";
21
+ import { blockingKeys, diffPermissions, permissionKeys, WARNINGS_KEY_PREFIX, } from "./permission-set.js";
22
+ import { reviewPermissionRows } from "./permission-view.js";
23
+ import { continuityProblem, ExtensionRevisionGrants, effectiveKeys, RevisionGrantError, } from "./revision-grants.js";
24
+ /**
25
+ * The kernel-written purpose of a migration warnings digest key. The key carries the digest, so a
26
+ * revision whose risky migration statements changed has a key its predecessor's grant lacks, and
27
+ * continuity refuses it: the change goes to review.
28
+ */
29
+ export function warningsPurpose(key) {
30
+ const digest = key.slice(WARNINGS_KEY_PREFIX.length);
31
+ return {
32
+ key,
33
+ zh: `迁移脚本含有需要审阅的语句(摘要 ${digest})`,
34
+ en: `The migration SQL contains statements that need review (digest ${digest})`,
35
+ };
36
+ }
37
+ /**
38
+ * The reviewed purpose rows of a permission set, and the keys that lack one. A migration warnings
39
+ * digest key gets the kernel-written row of {@link warningsPurpose}: authors cannot write one, and
40
+ * the grant must still bind the digest.
41
+ */
42
+ export function purposeRowsOf(set) {
43
+ const { rows } = reviewPermissionRows(set);
44
+ const purposes = new Map(rows.map((row) => [row.key, row.purpose]));
45
+ const out = [];
46
+ const missing = [];
47
+ for (const key of permissionKeys(set)) {
48
+ const purpose = purposes.get(key);
49
+ if (key.startsWith(WARNINGS_KEY_PREFIX))
50
+ out.push(warningsPurpose(key));
51
+ else if (purpose)
52
+ out.push({ key, zh: purpose.zh, en: purpose.en });
53
+ else
54
+ missing.push(key);
55
+ }
56
+ return { rows: out, missing };
57
+ }
58
+ /**
59
+ * The legacy decision for a revision's set against its snapshot, by `planApprovals`' approval rule:
60
+ * no snapshot or a new blocking key waits, unless `extensionPermissions.autoApprove` is on. Its
61
+ * dependency rule (an install whose required providers are all pending waits too) is not mirrored:
62
+ * the shadow report compares approval, not load order.
63
+ */
64
+ export function legacyDecision(set, snapshot, autoApprove = false) {
65
+ if (autoApprove)
66
+ return "approved";
67
+ if (!snapshot)
68
+ return "pending";
69
+ const { added } = diffPermissions(snapshot.permissions, set);
70
+ return blockingKeys(added).length > 0 ? "pending" : "approved";
71
+ }
72
+ /**
73
+ * The shadow decision for one installed revision: resolved against its own live grant. `approved`
74
+ * is the live grant of the install's approved revision when that is another revision; it only
75
+ * explains whether continuity could carry over (the publisher evidence is not known here, so it
76
+ * never grants by itself).
77
+ */
78
+ export function resolveRevisionGrant(input) {
79
+ const { revision, set, required, own, approved, legacy } = input;
80
+ const { rows, missing } = purposeRowsOf(set);
81
+ const decide = (status, wouldServe, extra = {}) => ({
82
+ revision,
83
+ status,
84
+ refusedKeys: [],
85
+ ungrantedKeys: [],
86
+ missingPurposes: missing,
87
+ required,
88
+ wouldServe,
89
+ legacy,
90
+ diverges: (legacy === "approved") !== wouldServe,
91
+ ...extra,
92
+ });
93
+ if (revision.sha512 === null)
94
+ return decide("unknown_digest", false);
95
+ if (own) {
96
+ const serving = new Set(effectiveKeys(own));
97
+ const granted = new Set(own.keys);
98
+ const keys = permissionKeys(set).filter((key) => !serving.has(key));
99
+ const refusedKeys = keys.filter((key) => granted.has(key));
100
+ const ungrantedKeys = keys.filter((key) => !granted.has(key));
101
+ return decide(keys.length > 0 ? "partially_revoked" : "granted", true, {
102
+ generation: own.generation,
103
+ refusedKeys,
104
+ ungrantedKeys,
105
+ });
106
+ }
107
+ const continuity = approved
108
+ ? (continuityProblem(approved, {
109
+ purposes: rows,
110
+ realms: {},
111
+ approvalScope: approved.approvalScope,
112
+ publisher: { evidence: "none" },
113
+ }) ?? "continuity is recorded when the revision is promoted")
114
+ : undefined;
115
+ return decide(missing.length > 0 ? "missing_purposes" : "missing_grant", false, {
116
+ ...(continuity !== undefined && { continuity }),
117
+ });
118
+ }
119
+ const SET_CACHE_ENTRIES = 512;
120
+ /** The installed revisions' shadow decisions, and promotion with continuity. */
121
+ let ExtensionRevisionApprovals = class ExtensionRevisionApprovals {
122
+ kdb;
123
+ txHost;
124
+ grants;
125
+ approvals;
126
+ blobs;
127
+ config;
128
+ /** Permission sets by package sha256: content-addressed, so a cached set never goes stale. */
129
+ sets = new Map();
130
+ constructor(kdb, txHost, grants, approvals, blobs, config) {
131
+ this.kdb = kdb;
132
+ this.txHost = txHost;
133
+ this.grants = grants;
134
+ this.approvals = approvals;
135
+ this.blobs = blobs;
136
+ this.config = config;
137
+ }
138
+ /**
139
+ * The shadow decision of every install record. A package that cannot be read is reported as
140
+ * `unreadable` and never hides the others.
141
+ */
142
+ async decisions() {
143
+ const installs = await this.kdb.db
144
+ .selectFrom("extension_installs as i")
145
+ .innerJoin("extension_packages as p", "p.sha256", "i.sha256")
146
+ .select([
147
+ "i.name",
148
+ "i.version",
149
+ "i.sha256",
150
+ "i.required",
151
+ "i.approved_version",
152
+ "i.approved_sha512",
153
+ "p.sha512",
154
+ ])
155
+ .orderBy("i.name")
156
+ .execute();
157
+ const snapshots = await this.approvals.all();
158
+ const autoApprove = this.config.get().extensionPermissions.autoApprove === true;
159
+ const out = [];
160
+ for (const row of installs) {
161
+ const revision = { plugin: row.name, version: row.version, sha512: row.sha512 };
162
+ let set;
163
+ try {
164
+ set = await this.setOf(row.sha256);
165
+ }
166
+ catch {
167
+ out.push({
168
+ revision,
169
+ status: "unreadable",
170
+ refusedKeys: [],
171
+ ungrantedKeys: [],
172
+ missingPurposes: [],
173
+ required: row.required,
174
+ wouldServe: false,
175
+ legacy: autoApprove || snapshots.has(row.name) ? "approved" : "pending",
176
+ diverges: autoApprove || snapshots.has(row.name),
177
+ });
178
+ continue;
179
+ }
180
+ const own = row.sha512 === null
181
+ ? undefined
182
+ : await this.grants.live({ ...revision, sha512: row.sha512 });
183
+ const approvedIsOther = row.approved_version !== null &&
184
+ row.approved_sha512 !== null &&
185
+ row.approved_sha512 !== row.sha512;
186
+ const approved = !own && approvedIsOther
187
+ ? await this.grants.live({
188
+ plugin: row.name,
189
+ version: row.approved_version,
190
+ sha512: row.approved_sha512,
191
+ })
192
+ : undefined;
193
+ out.push(resolveRevisionGrant({
194
+ revision,
195
+ set,
196
+ required: row.required,
197
+ own,
198
+ approved,
199
+ legacy: legacyDecision(set, snapshots.get(row.name), autoApprove),
200
+ }));
201
+ }
202
+ return out;
203
+ }
204
+ /** The facts the enforcing start gate would act on; computed and reported, never refused. */
205
+ async startGate() {
206
+ const all = await this.decisions();
207
+ return {
208
+ missingRequired: all.filter((d) => d.required && !d.wouldServe),
209
+ pending: all.filter((d) => !d.required && !d.wouldServe),
210
+ diverging: all.filter((d) => d.diverges),
211
+ };
212
+ }
213
+ /**
214
+ * Makes a revision the approved one of its install. A revision with its own live grant is
215
+ * promoted under it (an upgrade or a rollback). An upgrade without one continues the install's
216
+ * approved revision when that has a live grant and the candidate is a continuation of it (the
217
+ * store's rules): the continuity grant is recorded in the same transaction before the promotion.
218
+ * Anything else leaves the revision as the install's candidate, pending review, with the reason;
219
+ * it never throws for a refusal. A continuation carries no full-access realm (narrower, so safe):
220
+ * realms and the `extension.full_access_continued` audit come with plan tasks 1.2e and 1.3a.
221
+ *
222
+ * The package is read before the transaction: no object storage call holds a connection.
223
+ */
224
+ async promote(candidate, evidence, kind = "upgrade") {
225
+ const sha256 = await this.sha256Of(candidate);
226
+ let purposes;
227
+ if (sha256 === undefined)
228
+ purposes = "unknown_digest";
229
+ else {
230
+ try {
231
+ purposes = purposeRowsOf(await this.setOf(sha256));
232
+ }
233
+ catch {
234
+ // storage unavailable, the package too large or refused: a revision with its own grant
235
+ // still promotes, anything else waits
236
+ purposes = "unreadable";
237
+ }
238
+ }
239
+ return this.promoteIn(candidate, evidence, kind, purposes);
240
+ }
241
+ async promoteIn(candidate, evidence, kind, purposes) {
242
+ const own = await this.grants.live(candidate);
243
+ if (own) {
244
+ await this.grants.promote(candidate);
245
+ return { status: "promoted", grant: own, continued: false };
246
+ }
247
+ const pending = async (reason) => {
248
+ await this.grants.setCandidate(candidate.plugin, candidate);
249
+ return { status: "pending", reason };
250
+ };
251
+ const newest = await this.grants.get(candidate);
252
+ if (newest)
253
+ return pending(`its grant is ${newest.state}; it needs a new review`);
254
+ if (kind === "rollback")
255
+ return pending("a rollback is served only under its own grant");
256
+ if (purposes === "unknown_digest")
257
+ return pending("the package digest is not recorded");
258
+ if (purposes === "unreadable")
259
+ return pending("the stored package cannot be read");
260
+ if (purposes.missing.length > 0) {
261
+ return pending(`missing purposes: ${purposes.missing.join(", ")}`);
262
+ }
263
+ const { approved } = await this.grants.installRevisions(candidate.plugin);
264
+ const source = approved ? await this.grants.live(approved) : undefined;
265
+ if (!approved || !source)
266
+ return pending("no live grant to continue");
267
+ const problem = continuityProblem(source, {
268
+ purposes: purposes.rows,
269
+ realms: {},
270
+ approvalScope: source.approvalScope,
271
+ publisher: evidence.publisher,
272
+ });
273
+ if (problem)
274
+ return pending(problem);
275
+ let grant;
276
+ try {
277
+ // The store checks continuity again under the source's share lock.
278
+ grant = await this.grants.record({
279
+ revision: candidate,
280
+ purposes: purposes.rows,
281
+ reviewHash: continuityReviewHash(source, candidate, purposes.rows),
282
+ approvalScope: source.approvalScope,
283
+ source: "continuity",
284
+ publisher: evidence.publisher,
285
+ continuedFrom: source.revision,
286
+ approvedBy: null,
287
+ });
288
+ }
289
+ catch (err) {
290
+ // The source was revoked meanwhile, or no longer continues: the candidate waits.
291
+ if (err instanceof RevisionGrantError &&
292
+ (err.code === "not_live" || err.code === "not_continuous" || err.code === "terminal")) {
293
+ return pending(err.message);
294
+ }
295
+ throw err;
296
+ }
297
+ await this.grants.promote(candidate);
298
+ return { status: "promoted", grant, continued: true };
299
+ }
300
+ /** The stored package of a revision, or undefined while its sha512 is not recorded. */
301
+ async sha256Of(revision) {
302
+ const row = await this.kdb.db
303
+ .selectFrom("extension_packages")
304
+ .select("sha256")
305
+ .where("name", "=", revision.plugin)
306
+ .where("version", "=", revision.version)
307
+ .where("sha512", "=", revision.sha512)
308
+ .executeTakeFirst();
309
+ return row?.sha256;
310
+ }
311
+ async setOf(sha256) {
312
+ const cached = this.sets.get(sha256);
313
+ if (cached)
314
+ return cached;
315
+ const pkg = await readExtensionPackage(await this.blobs.get(sha256), {
316
+ maxBytes: this.config.get().packages.maxBytes,
317
+ hostSdkVersion: SDK_VERSION,
318
+ });
319
+ const set = permissionSetOfPackage(pkg);
320
+ // a bound on memory only: a dropped entry is read again from the package store
321
+ if (this.sets.size >= SET_CACHE_ENTRIES)
322
+ this.sets.clear();
323
+ this.sets.set(sha256, set);
324
+ return set;
325
+ }
326
+ };
327
+ __decorate([
328
+ Transactional()
329
+ ], ExtensionRevisionApprovals.prototype, "promoteIn", null);
330
+ ExtensionRevisionApprovals = __decorate([
331
+ Injectable(),
332
+ __param(0, Inject(KernelDb)),
333
+ __param(1, Inject(TransactionHost)),
334
+ __param(2, Inject(ExtensionRevisionGrants)),
335
+ __param(3, Inject(ExtensionApprovals)),
336
+ __param(4, Inject(ExtensionBlobs)),
337
+ __param(5, Inject(KernelConfigService))
338
+ ], ExtensionRevisionApprovals);
339
+ export { ExtensionRevisionApprovals };
340
+ /**
341
+ * The review hash of a continuity grant: the kernel reviewed it against its source, so the hash
342
+ * binds the candidate, its purpose rows and the exact source generation.
343
+ */
344
+ export function continuityReviewHash(source, candidate, rows) {
345
+ const digest = createHash("sha256")
346
+ .update(canonicalJson({
347
+ candidate,
348
+ purposes: rows,
349
+ source: { ...source.revision, generation: source.generation },
350
+ }))
351
+ .digest("hex");
352
+ return `continuity:${digest}`;
353
+ }