@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
@@ -27,17 +27,41 @@ export async function readIsolatedGrant(db, grant) {
27
27
  const snapshot = await query.executeTakeFirst();
28
28
  return snapshotCovers(snapshot, permissions);
29
29
  }
30
- const MAX_GRANTS_PER_INSTANCE = 128;
31
- const GRANT_TTL_MS = 60_000;
30
+ /** A grant checked at the current epoch is still rechecked after this long (backstop only). */
31
+ export const GRANT_FULL_REVALIDATION_MS = 5 * 60_000;
32
32
  const REVALIDATION_BATCH = 8;
33
- /** Replica-local cache of authority checked against deployment and org state. */
33
+ const IN_FLIGHT_RECHECK_BATCH = 64;
34
+ /**
35
+ * Replica-local cache of authority checked against deployment and org state.
36
+ *
37
+ * A cached grant is valid only while it was checked at the current grant epoch and the epoch was
38
+ * read successfully within its maximum age. When the epoch cannot be confirmed, new admissions
39
+ * wait for a fresh read and are refused if it fails (fail closed). Bus hints still fence affected
40
+ * grants at once; the epoch covers hints that never arrive.
41
+ */
34
42
  export class IsolatedInvocationGrants {
43
+ epochs;
44
+ capacity;
45
+ now;
35
46
  instances = new Map();
36
47
  rechecks = new Map();
48
+ /** Every cached grant across instances, least recently used first. */
49
+ lru = new Map();
37
50
  revalidating;
51
+ constructor(epochs, capacity, now = () => performance.now()) {
52
+ this.epochs = epochs;
53
+ this.capacity = capacity;
54
+ this.now = now;
55
+ }
38
56
  async issue({ plugin, orgId }, check, onRevoked, instance) {
39
57
  if (instance.aborted)
40
58
  return undefined;
59
+ this.epochs.start();
60
+ // No await when the epoch is fresh: the grant entry must exist before a revocation can race it.
61
+ if (this.epochs.fresh() === undefined) {
62
+ if ((await this.epochs.ensureFresh()) === undefined || instance.aborted)
63
+ return undefined;
64
+ }
41
65
  let bucket = this.instances.get(instance);
42
66
  if (!bucket) {
43
67
  bucket = { plugin, orgs: new Map() };
@@ -45,8 +69,11 @@ export class IsolatedInvocationGrants {
45
69
  this.instances.set(instance, bucket);
46
70
  instance.addEventListener("abort", () => {
47
71
  this.instances.delete(instance);
48
- for (const grant of orgs.values())
72
+ for (const grant of orgs.values()) {
73
+ this.lru.delete(grant);
74
+ this.rechecks.delete(grant);
49
75
  grant.abort.abort();
76
+ }
50
77
  orgs.clear();
51
78
  }, { once: true });
52
79
  }
@@ -54,34 +81,38 @@ export class IsolatedInvocationGrants {
54
81
  let entry = orgs.get(orgId);
55
82
  if (entry) {
56
83
  // Move it to the back so an idle least recently used grant is the first eviction candidate.
57
- orgs.delete(orgId);
84
+ this.lru.delete(entry);
58
85
  }
59
86
  else {
60
- if (orgs.size >= MAX_GRANTS_PER_INSTANCE) {
61
- for (const [key, idle] of orgs) {
62
- if (idle.calls !== 0 || idle.pending)
63
- continue;
64
- orgs.delete(key);
65
- break;
66
- }
67
- if (orgs.size >= MAX_GRANTS_PER_INSTANCE)
68
- return undefined;
69
- }
87
+ if (this.lru.size >= this.capacity && !this.evictIdle())
88
+ return undefined;
70
89
  entry = {
71
90
  abort: new AbortController(),
72
91
  check,
73
92
  onRevoked: onRevoked ?? (() => { }),
74
- checkedUntil: 0,
93
+ epoch: undefined,
94
+ checkedAt: Number.NEGATIVE_INFINITY,
75
95
  calls: 0,
96
+ admitting: 0,
76
97
  };
98
+ orgs.set(orgId, entry);
77
99
  }
78
- orgs.set(orgId, entry);
79
- while (Date.now() >= entry.checkedUntil) {
80
- if (!(await this.validate(orgs, orgId, entry)))
81
- return undefined;
100
+ this.lru.set(entry, { orgs, orgId, entry });
101
+ entry.admitting += 1;
102
+ try {
103
+ for (;;) {
104
+ const epoch = this.epochs.fresh() ?? (await this.epochs.ensureFresh());
105
+ if (epoch === undefined || entry.abort.signal.aborted)
106
+ return undefined;
107
+ if (this.valid(entry, epoch))
108
+ break;
109
+ if (!(await this.validate(orgs, orgId, entry)))
110
+ return undefined;
111
+ }
112
+ }
113
+ finally {
114
+ entry.admitting -= 1;
82
115
  }
83
- if (entry.abort.signal.aborted)
84
- return undefined;
85
116
  const call = new AbortController();
86
117
  entry.calls += 1;
87
118
  return {
@@ -101,7 +132,7 @@ export class IsolatedInvocationGrants {
101
132
  for (const [key, entry] of bucket.orgs) {
102
133
  if (orgId !== undefined && orgId !== key)
103
134
  continue;
104
- bucket.orgs.delete(key);
135
+ this.drop(bucket.orgs, key, entry);
105
136
  entry.abort.abort();
106
137
  }
107
138
  }
@@ -114,7 +145,7 @@ export class IsolatedInvocationGrants {
114
145
  for (const grant of this.retained()) {
115
146
  if (grant.orgId === undefined || (orgId !== undefined && orgId !== grant.orgId))
116
147
  continue;
117
- grant.entry.checkedUntil = 0;
148
+ grant.entry.epoch = undefined;
118
149
  delete grant.entry.pending;
119
150
  this.rechecks.set(grant.entry, grant);
120
151
  }
@@ -123,11 +154,71 @@ export class IsolatedInvocationGrants {
123
154
  /** Recheck retained grants, including idle lanes, when periodic reconciliation fires. */
124
155
  revalidate() {
125
156
  for (const grant of this.retained()) {
126
- if (grant.entry.checkedUntil > 0)
157
+ if (grant.entry.epoch !== undefined)
127
158
  this.rechecks.set(grant.entry, grant);
128
159
  }
129
160
  return this.runRechecks();
130
161
  }
162
+ /**
163
+ * The grant epoch advanced: some committed change may have revoked any cached grant. Every
164
+ * entry is stale for admission already ({@link valid} compares epochs), so idle entries are
165
+ * rechecked lazily by their next admission or the periodic reconcile. Entries with calls in
166
+ * flight are rechecked now, all together, so a call whose grant is gone is aborted quickly;
167
+ * {@link fenceStale} aborts the ones still unconfirmed at the settle deadline.
168
+ */
169
+ async epochChanged() {
170
+ const inFlight = [...this.retained()].filter(({ entry }) => entry.calls > 0);
171
+ for (let start = 0; start < inFlight.length; start += IN_FLIGHT_RECHECK_BATCH) {
172
+ await Promise.all(inFlight.slice(start, start + IN_FLIGHT_RECHECK_BATCH).map(({ orgs, orgId, entry }) => {
173
+ // A check started before the change cannot confirm the new epoch: supersede it.
174
+ delete entry.pending;
175
+ return this.validate(orgs, orgId, entry);
176
+ }));
177
+ }
178
+ }
179
+ /**
180
+ * The settle deadline after an epoch change: abort calls whose grant was not confirmed at the
181
+ * current epoch, so no call outlives a revocation by more than the bound. Lanes stay up; the
182
+ * next admission checks PostgreSQL again.
183
+ */
184
+ fenceStale() {
185
+ const epoch = this.epochs.fresh();
186
+ for (const { orgs, orgId, entry } of [...this.retained()]) {
187
+ if (entry.calls === 0 || (epoch !== undefined && this.valid(entry, epoch)))
188
+ continue;
189
+ this.drop(orgs, orgId, entry);
190
+ entry.abort.abort();
191
+ }
192
+ }
193
+ /**
194
+ * The epoch has not been confirmed for the whole revocation bound. Fence every cached grant:
195
+ * in-flight calls are cancelled and the next admission checks PostgreSQL again. Lanes stay up,
196
+ * because nothing is known to be revoked.
197
+ */
198
+ fenceAll() {
199
+ this.revoke({});
200
+ }
201
+ valid(entry, epoch) {
202
+ return (entry.pending === undefined &&
203
+ entry.epoch === epoch &&
204
+ this.now() - entry.checkedAt < GRANT_FULL_REVALIDATION_MS);
205
+ }
206
+ evictIdle() {
207
+ for (const { orgs, orgId, entry } of this.lru.values()) {
208
+ if (entry.calls !== 0 || entry.admitting !== 0 || entry.pending)
209
+ continue;
210
+ this.drop(orgs, orgId, entry);
211
+ entry.abort.abort();
212
+ return true;
213
+ }
214
+ return false;
215
+ }
216
+ drop(orgs, orgId, entry) {
217
+ if (orgs.get(orgId) === entry)
218
+ orgs.delete(orgId);
219
+ this.lru.delete(entry);
220
+ this.rechecks.delete(entry);
221
+ }
131
222
  *retained() {
132
223
  for (const { orgs } of this.instances.values()) {
133
224
  for (const [orgId, entry] of orgs)
@@ -162,29 +253,51 @@ export class IsolatedInvocationGrants {
162
253
  if (entry.pending)
163
254
  return entry.pending;
164
255
  this.rechecks.delete(entry);
165
- const checkedUntil = Date.now() + GRANT_TTL_MS;
166
- const pending = Promise.resolve()
167
- .then(entry.check)
168
- .catch(() => false)
169
- .then((allowed) => {
256
+ const run = async (epoch) => {
257
+ // The epoch is captured before the query starts. A revocation committed after the query's
258
+ // snapshot advances the epoch past this value, so the result is never trusted beyond it.
259
+ const checkedAt = this.now();
260
+ let failed = false;
261
+ const allowed = epoch !== undefined &&
262
+ (await Promise.resolve()
263
+ .then(entry.check)
264
+ .catch(() => {
265
+ failed = true;
266
+ return false;
267
+ }));
170
268
  if (entry.pending !== pending) {
171
269
  // A catalog hint superseded this query. Share the next decision or reuse it if complete.
172
270
  if (entry.abort.signal.aborted || orgs.get(orgId) !== entry)
173
271
  return false;
174
- return entry.checkedUntil > Date.now() || this.validate(orgs, orgId, entry);
272
+ const current = this.epochs.fresh();
273
+ return ((current !== undefined && this.valid(entry, current)) || this.validate(orgs, orgId, entry));
175
274
  }
176
275
  delete entry.pending;
177
276
  if (entry.abort.signal.aborted)
178
277
  return false;
278
+ if (epoch === undefined) {
279
+ // The epoch could not be confirmed: refuse this admission but keep the lane, since
280
+ // nothing is known to be revoked.
281
+ entry.epoch = undefined;
282
+ return false;
283
+ }
179
284
  if (allowed) {
180
- entry.checkedUntil = checkedUntil;
285
+ entry.epoch = epoch;
286
+ entry.checkedAt = checkedAt;
181
287
  return true;
182
288
  }
183
- orgs.delete(orgId);
289
+ // Denied or not checkable: fence the grant (calls abort, the next admission checks again).
290
+ // Only a decision that denies retires the lanes; a failed query (a transient database error
291
+ // during any catalog write's recheck) is not a revocation.
292
+ this.drop(orgs, orgId, entry);
184
293
  entry.abort.abort();
185
- entry.onRevoked();
294
+ if (!failed)
295
+ entry.onRevoked();
186
296
  return false;
187
- });
297
+ };
298
+ // Start the query in this tick when the epoch is fresh, so concurrent callers coalesce on it.
299
+ const known = this.epochs.fresh();
300
+ const pending = known !== undefined ? run(known) : this.epochs.ensureFresh().then(run);
188
301
  entry.pending = pending;
189
302
  return pending;
190
303
  }
@@ -153,7 +153,6 @@ export function createChildSession(root, send) {
153
153
  get secrets() {
154
154
  return noGroup();
155
155
  },
156
- extensionPoints: { get: unsupported, list: unsupported },
157
156
  log: { info: logFor("info"), warn: logFor("warn"), error: logFor("error") },
158
157
  kv: {
159
158
  get: (key) => broker("kv", "get", [key]),
@@ -214,7 +214,7 @@ export async function isolatedRevision(options) {
214
214
  key: manifest.name,
215
215
  dispose: () => instance.close(),
216
216
  });
217
- const descriptors = callbackList(await instance.register(), section.manifest.permissions.tools ?? []);
217
+ const descriptors = callbackList(await instance.register(), manifest.permissions?.tools ?? []);
218
218
  for (const descriptor of descriptors) {
219
219
  await bind(ctx, instance, descriptor, options.effective, options.authorize, options.roles, options.quota);
220
220
  }
@@ -5,6 +5,7 @@ import { KernelDb } from "../infra/database/kernel-db.js";
5
5
  import { TransactionHost } from "../infra/database/transaction-host.js";
6
6
  import { KernelEvents } from "../infra/events/kernel-events.js";
7
7
  import { OutboxService } from "../infra/outbox/outbox.service.js";
8
+ import { type HintPublisher } from "../invalidation/audiences.js";
8
9
  import { OrgLock } from "../orgs/org-lock.js";
9
10
  import { EffectiveConfigs } from "./effective-config.js";
10
11
  import { ExtensionHost } from "./host.js";
@@ -137,6 +138,7 @@ export declare class OrgExtensionConfigs {
137
138
  private readonly availability;
138
139
  /** Owes the org's template connector ensure when an extension is switched on (ruling R27). */
139
140
  private readonly outbox;
141
+ private readonly hints;
140
142
  constructor(installLock: PluginInstallLock, kdb: KernelDb, txHost: TransactionHost, host: ExtensionHost,
141
143
  /** The org row lock (orgs/org-lock.ts): joins the transaction. */
142
144
  orgLock: OrgLock,
@@ -149,7 +151,7 @@ export declare class OrgExtensionConfigs {
149
151
  /** Why an installed extension is not loaded (`status` / `reason` of the unavailable ones). */
150
152
  availability: ExtensionAvailability,
151
153
  /** Owes the org's template connector ensure when an extension is switched on (ruling R27). */
152
- outbox: Pick<OutboxService, "add">);
154
+ outbox: Pick<OutboxService, "add">, hints: HintPublisher);
153
155
  /** The kernel-schema handle of the current transaction. */
154
156
  private get db();
155
157
  /** After a write committed: a disabled extension's channel connections drop at once. */
@@ -25,6 +25,8 @@ import { TransactionHost } from "../infra/database/transaction-host.js";
25
25
  import { Transactional } from "../infra/database/transactional.decorator.js";
26
26
  import { KernelEvents } from "../infra/events/kernel-events.js";
27
27
  import { OutboxService } from "../infra/outbox/outbox.service.js";
28
+ import { publishCapabilities } from "../invalidation/audiences.js";
29
+ import { InvalidationHints } from "../invalidation/invalidation-hints.js";
28
30
  import { OrgLock } from "../orgs/org-lock.js";
29
31
  import { EffectiveConfigs } from "./effective-config.js";
30
32
  import { alwaysOnExtensions, enabledExtensionsQuery, extensionSwitch, isExtensionEnabled, requiredExtensions, } from "./enabled.js";
@@ -78,6 +80,7 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
78
80
  effective;
79
81
  availability;
80
82
  outbox;
83
+ hints;
81
84
  constructor(installLock, kdb, txHost, host,
82
85
  /** The org row lock (orgs/org-lock.ts): joins the transaction. */
83
86
  orgLock,
@@ -90,7 +93,7 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
90
93
  /** Why an installed extension is not loaded (`status` / `reason` of the unavailable ones). */
91
94
  availability,
92
95
  /** Owes the org's template connector ensure when an extension is switched on (ruling R27). */
93
- outbox) {
96
+ outbox, hints) {
94
97
  this.installLock = installLock;
95
98
  this.kdb = kdb;
96
99
  this.txHost = txHost;
@@ -101,6 +104,7 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
101
104
  this.effective = effective;
102
105
  this.availability = availability;
103
106
  this.outbox = outbox;
107
+ this.hints = hints;
104
108
  }
105
109
  /** The kernel-schema handle of the current transaction. */
106
110
  get db() {
@@ -312,7 +316,7 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
312
316
  await revokeSiteGrantsOfExtension(t, { orgId, extensionName: plugin });
313
317
  // a model provider switched off: requests for its models need the extension first now
314
318
  if (before)
315
- await cancelEnableRequestsOfProvider(t, orgId, plugin, actorAccountId);
319
+ await cancelEnableRequestsOfProvider(t, this.hints, orgId, plugin, actorAccountId);
316
320
  // its host services on the org's machines stop (local agents spec, call 5)
317
321
  await oweHostServicesStop(t, this.outbox, { orgId, extension: plugin });
318
322
  }
@@ -322,7 +326,7 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
322
326
  if (enabled && !before) {
323
327
  await oweTemplateConnectors(this.outbox, t, orgId);
324
328
  // the org's pending enable requests for this extension are settled with the write
325
- await settleEnableRequests(t, orgId, { kind: "extension", id: plugin }, actorAccountId, {
329
+ await settleEnableRequests(t, this.hints, orgId, { kind: "extension", id: plugin }, actorAccountId, {
326
330
  ...(actorTokenId !== undefined && { actorTokenId }),
327
331
  });
328
332
  }
@@ -338,6 +342,9 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
338
342
  ...opts.auditDetail,
339
343
  },
340
344
  });
345
+ // the org's extension switch moved: its members' pickers refetch
346
+ if (enabled !== before)
347
+ publishCapabilities(this.hints, [orgId]);
341
348
  const view = this.view(manifest, enabled, required, item, bundleUsable, seen);
342
349
  // Only once the root transaction committed (an outer caller's too): a rolled-back write
343
350
  // disconnects nothing
@@ -434,6 +441,7 @@ let OrgExtensionConfigs = class OrgExtensionConfigs {
434
441
  target: plugin,
435
442
  detail: { enabled: false, reason },
436
443
  });
444
+ publishCapabilities(this.hints, [orgId]);
437
445
  return true;
438
446
  }
439
447
  /** {@link disableIn} 的事务提交之后:通知停用(断开该插件的渠道连接等)。 */
@@ -551,7 +559,8 @@ OrgExtensionConfigs = __decorate([
551
559
  __param(6, Inject(KernelEvents)),
552
560
  __param(7, Inject(EffectiveConfigs)),
553
561
  __param(8, Inject(ExtensionAvailability)),
554
- __param(9, Inject(OutboxService))
562
+ __param(9, Inject(OutboxService)),
563
+ __param(10, Inject(InvalidationHints))
555
564
  ], OrgExtensionConfigs);
556
565
  export { OrgExtensionConfigs };
557
566
  /**
@@ -1,6 +1,7 @@
1
1
  import { type Keyring } from "../ctx/secrets.js";
2
2
  import { KernelDb } from "../infra/database/kernel-db.js";
3
3
  import { TransactionHost } from "../infra/database/transaction-host.js";
4
+ import { type HintPublisher } from "../invalidation/audiences.js";
4
5
  import type { KernelLogger } from "../log.js";
5
6
  import { OrgLock } from "../orgs/org-lock.js";
6
7
  import { type ConfigIssue } from "./config-issues.js";
@@ -80,9 +81,13 @@ export declare class OrgOverrides {
80
81
  private readonly orgLock;
81
82
  private readonly effective;
82
83
  private readonly log;
84
+ /** A changed copy publishes `capabilities` to the org (after the cache eviction). */
85
+ private readonly hints;
83
86
  constructor(kdb: KernelDb, txHost: TransactionHost, keyring: Keyring, host: ExtensionHost,
84
87
  /** The org row lock: re-checks `org.extensions.manage` under the lock. */
85
- orgLock: OrgLock, effective: EffectiveConfigs, log: KernelLogger);
88
+ orgLock: OrgLock, effective: EffectiveConfigs, log: KernelLogger,
89
+ /** A changed copy publishes `capabilities` to the org (after the cache eviction). */
90
+ hints: HintPublisher);
86
91
  private get db();
87
92
  private running;
88
93
  /**
@@ -18,6 +18,8 @@ import { KernelDb } from "../infra/database/kernel-db.js";
18
18
  import { TransactionHost } from "../infra/database/transaction-host.js";
19
19
  import { Transactional } from "../infra/database/transactional.decorator.js";
20
20
  import { KERNEL_LOGGER, KEYRING } from "../infra/tokens.js";
21
+ import { publishCapabilities } from "../invalidation/audiences.js";
22
+ import { InvalidationHints } from "../invalidation/invalidation-hints.js";
21
23
  import { OrgLock } from "../orgs/org-lock.js";
22
24
  import { toIssues } from "./config-issues.js";
23
25
  import { configMessages, safeConfigFormSchema } from "./config-json-schema.js";
@@ -46,9 +48,12 @@ let OrgOverrides = class OrgOverrides {
46
48
  orgLock;
47
49
  effective;
48
50
  log;
51
+ hints;
49
52
  constructor(kdb, txHost, keyring, host,
50
53
  /** The org row lock: re-checks `org.extensions.manage` under the lock. */
51
- orgLock, effective, log) {
54
+ orgLock, effective, log,
55
+ /** A changed copy publishes `capabilities` to the org (after the cache eviction). */
56
+ hints) {
52
57
  this.kdb = kdb;
53
58
  this.txHost = txHost;
54
59
  this.keyring = keyring;
@@ -56,6 +61,7 @@ let OrgOverrides = class OrgOverrides {
56
61
  this.orgLock = orgLock;
57
62
  this.effective = effective;
58
63
  this.log = log;
64
+ this.hints = hints;
59
65
  }
60
66
  get db() {
61
67
  return this.kdb.db;
@@ -190,6 +196,8 @@ let OrgOverrides = class OrgOverrides {
190
196
  this.log.addOrgSecrets(secretValues);
191
197
  await this.effective.invalidate(orgId, plugin);
192
198
  });
199
+ // the org's effective config moved (it may need config, or no longer): its members refetch
200
+ publishCapabilities(this.hints, [orgId]);
193
201
  return this.render(orgId, run);
194
202
  }
195
203
  /**
@@ -249,6 +257,7 @@ let OrgOverrides = class OrgOverrides {
249
257
  detail: { changedPaths: changed.sort(), ...actor.auditDetail },
250
258
  });
251
259
  afterCommit(() => this.effective.invalidate(orgId, plugin));
260
+ publishCapabilities(this.hints, [orgId]);
252
261
  return this.render(orgId, run);
253
262
  }
254
263
  /**
@@ -281,6 +290,7 @@ let OrgOverrides = class OrgOverrides {
281
290
  ...(actor.auditDetail !== undefined && { detail: actor.auditDetail }),
282
291
  });
283
292
  afterCommit(() => this.effective.invalidate(orgId, plugin));
293
+ publishCapabilities(this.hints, [orgId]);
284
294
  return this.render(orgId, run);
285
295
  }
286
296
  /**
@@ -474,7 +484,8 @@ OrgOverrides = __decorate([
474
484
  __param(3, Inject(ExtensionHost)),
475
485
  __param(4, Inject(OrgLock)),
476
486
  __param(5, Inject(EffectiveConfigs)),
477
- __param(6, Inject(KERNEL_LOGGER))
487
+ __param(6, Inject(KERNEL_LOGGER)),
488
+ __param(7, Inject(InvalidationHints))
478
489
  ], OrgOverrides);
479
490
  export { OrgOverrides };
480
491
  /**
@@ -0,0 +1,34 @@
1
+ import { KernelConfigService } from "../infra/config/kernel-config.service.js";
2
+ import { KernelDb } from "../infra/database/kernel-db.js";
3
+ import { LockService } from "../infra/lock/lock.service.js";
4
+ import type { KernelLogger } from "../log.js";
5
+ import { ExtensionBlobs } from "./blobs.js";
6
+ export declare const PACKAGE_SHA512_BACKFILL_JOB = "extension-package-sha512-backfill";
7
+ /** Packages read per page, and pages per tick: each read is a full archive, bounded by `packages.maxBytes`. */
8
+ export declare const PACKAGE_SHA512_BACKFILL_BATCH = 50;
9
+ /**
10
+ * Fills `extension_packages.sha512` for packages stored before migration 0235 (plan task 1.2b), so
11
+ * every install resolves its revision grant by `(version, sha512)`. New packages get it at ingest.
12
+ *
13
+ * Idempotent and once per tick across replicas (`@LockedInterval`, worker role): each row is
14
+ * written only while still NULL, from bytes `ExtensionBlobs.get` verified against the sha256. A
15
+ * package whose bytes cannot be read stays NULL and is retried on the next tick; the shadow
16
+ * decision reports it as an unknown digest meanwhile. Once every row is filled a tick is one empty
17
+ * query.
18
+ */
19
+ export declare class PackageDigestBackfillJob {
20
+ private readonly kdb;
21
+ private readonly blobs;
22
+ readonly config: KernelConfigService;
23
+ readonly lockService: LockService;
24
+ readonly log: KernelLogger;
25
+ constructor(kdb: KernelDb, blobs: Pick<ExtensionBlobs, "get">, config: KernelConfigService, lockService: LockService, log: KernelLogger);
26
+ backfill(stop?: AbortSignal): Promise<void>;
27
+ /** One pass of the backfill (the locked tick runs it); returns what it filled and could not. */
28
+ fill(stop?: AbortSignal,
29
+ /** Tests only: the cursor the tick starts at (random otherwise). */
30
+ start?: string): Promise<{
31
+ filled: number;
32
+ failed: number;
33
+ }>;
34
+ }
@@ -0,0 +1,121 @@
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 { randomBytes } from "node:crypto";
11
+ import { Inject, Injectable } from "@nestjs/common";
12
+ import { KernelConfigService } from "../infra/config/kernel-config.service.js";
13
+ import { KernelDb } from "../infra/database/kernel-db.js";
14
+ import { LockService } from "../infra/lock/lock.service.js";
15
+ import { LockedInterval } from "../infra/schedule/locked-interval.js";
16
+ import { KERNEL_LOGGER } from "../infra/tokens.js";
17
+ import { ExtensionBlobs, packageSha512 } from "./blobs.js";
18
+ export const PACKAGE_SHA512_BACKFILL_JOB = "extension-package-sha512-backfill";
19
+ /** Packages read per page, and pages per tick: each read is a full archive, bounded by `packages.maxBytes`. */
20
+ export const PACKAGE_SHA512_BACKFILL_BATCH = 50;
21
+ const PACKAGE_SHA512_BACKFILL_PAGES = 4;
22
+ const PACKAGE_SHA512_BACKFILL_PERIOD_MS = 10 * 60_000;
23
+ /**
24
+ * Fills `extension_packages.sha512` for packages stored before migration 0235 (plan task 1.2b), so
25
+ * every install resolves its revision grant by `(version, sha512)`. New packages get it at ingest.
26
+ *
27
+ * Idempotent and once per tick across replicas (`@LockedInterval`, worker role): each row is
28
+ * written only while still NULL, from bytes `ExtensionBlobs.get` verified against the sha256. A
29
+ * package whose bytes cannot be read stays NULL and is retried on the next tick; the shadow
30
+ * decision reports it as an unknown digest meanwhile. Once every row is filled a tick is one empty
31
+ * query.
32
+ */
33
+ let PackageDigestBackfillJob = class PackageDigestBackfillJob {
34
+ kdb;
35
+ blobs;
36
+ config;
37
+ lockService;
38
+ log;
39
+ constructor(kdb, blobs, config, lockService, log) {
40
+ this.kdb = kdb;
41
+ this.blobs = blobs;
42
+ this.config = config;
43
+ this.lockService = lockService;
44
+ this.log = log;
45
+ }
46
+ async backfill(stop) {
47
+ await this.fill(stop);
48
+ }
49
+ /** One pass of the backfill (the locked tick runs it); returns what it filled and could not. */
50
+ async fill(stop,
51
+ /** Tests only: the cursor the tick starts at (random otherwise). */
52
+ start = randomBytes(32).toString("hex")) {
53
+ let filled = 0;
54
+ let failed = 0;
55
+ // Each tick starts at a random point of the sha256 order and wraps around, so packages whose
56
+ // bytes cannot be read never use up every tick's budget ahead of the same rows: every row is
57
+ // reached within a few ticks however many are unreadable.
58
+ const budget = PACKAGE_SHA512_BACKFILL_BATCH * PACKAGE_SHA512_BACKFILL_PAGES;
59
+ let read = 0;
60
+ for (const half of ["after", "upto"]) {
61
+ let after = half === "after" ? start : "";
62
+ while (read < budget && !stop?.aborted) {
63
+ const limit = Math.min(PACKAGE_SHA512_BACKFILL_BATCH, budget - read);
64
+ let query = this.kdb.db
65
+ .selectFrom("extension_packages")
66
+ .select("sha256")
67
+ .where("sha512", "is", null)
68
+ .where("sha256", ">", after);
69
+ if (half === "upto")
70
+ query = query.where("sha256", "<=", start);
71
+ const rows = await query.orderBy("sha256").limit(limit).execute();
72
+ for (const { sha256 } of rows) {
73
+ if (stop?.aborted)
74
+ break;
75
+ after = sha256;
76
+ read += 1;
77
+ let sha512;
78
+ try {
79
+ sha512 = packageSha512(await this.blobs.get(sha256));
80
+ }
81
+ catch (err) {
82
+ failed += 1;
83
+ this.log.warn("cannot read a stored extension package to record its sha512", {
84
+ sha256,
85
+ err,
86
+ });
87
+ continue;
88
+ }
89
+ const result = await this.kdb.db
90
+ .updateTable("extension_packages")
91
+ .set({ sha512 })
92
+ .where("sha256", "=", sha256)
93
+ .where("sha512", "is", null)
94
+ .executeTakeFirst();
95
+ if (result.numUpdatedRows > 0n)
96
+ filled += 1;
97
+ }
98
+ if (rows.length < limit)
99
+ break;
100
+ }
101
+ }
102
+ if (filled > 0 || failed > 0) {
103
+ this.log.info("extension package sha512 backfill", { filled, failed });
104
+ }
105
+ return { filled, failed };
106
+ }
107
+ };
108
+ __decorate([
109
+ LockedInterval(PACKAGE_SHA512_BACKFILL_JOB, () => PACKAGE_SHA512_BACKFILL_PERIOD_MS, {
110
+ runOnStart: true,
111
+ })
112
+ ], PackageDigestBackfillJob.prototype, "backfill", null);
113
+ PackageDigestBackfillJob = __decorate([
114
+ Injectable(),
115
+ __param(0, Inject(KernelDb)),
116
+ __param(1, Inject(ExtensionBlobs)),
117
+ __param(2, Inject(KernelConfigService)),
118
+ __param(3, Inject(LockService)),
119
+ __param(4, Inject(KERNEL_LOGGER))
120
+ ], PackageDigestBackfillJob);
121
+ export { PackageDigestBackfillJob };