@capybara-hq/kernel 0.1.0-rc.77 → 0.1.0-rc.78

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 (228) hide show
  1. package/bootstrap.json +6 -6
  2. package/dist/admin/outbox-dead.service.js +4 -1
  3. package/dist/agents/admin-agents.js +2 -0
  4. package/dist/agents/agent-lookup.js +4 -0
  5. package/dist/agents/agent-profile.service.js +3 -0
  6. package/dist/agents/agents.service.js +7 -0
  7. package/dist/agents/org-templates.js +1 -0
  8. package/dist/agents/pending-connector-requests.js +6 -0
  9. package/dist/agents/pending-connectors.service.js +2 -0
  10. package/dist/agents/readiness.service.d.ts +9 -3
  11. package/dist/agents/readiness.service.js +3 -3
  12. package/dist/agents/templates.js +1 -0
  13. package/dist/background/finish.js +3 -0
  14. package/dist/background/stop.js +2 -0
  15. package/dist/background/store.js +44 -0
  16. package/dist/background/tasks.service.js +3 -2
  17. package/dist/background/wake-rules.js +1 -0
  18. package/dist/catalog/admin-catalog.controller.d.ts +6 -0
  19. package/dist/catalog/admin-catalog.controller.js +13 -0
  20. package/dist/catalog/catalog-content.d.ts +6 -0
  21. package/dist/catalog/catalog-content.js +1 -0
  22. package/dist/catalog/catalog-items.js +4 -0
  23. package/dist/catalog/catalog-scope.service.js +4 -1
  24. package/dist/catalog/component-promotion.js +2 -0
  25. package/dist/catalog/component-scope.controller.d.ts +9 -15
  26. package/dist/catalog/component-scope.controller.js +14 -9
  27. package/dist/catalog/component-scope.service.js +5 -0
  28. package/dist/catalog/component-state.js +2 -0
  29. package/dist/catalog/marketplaces.service.d.ts +6 -1
  30. package/dist/catalog/marketplaces.service.js +8 -3
  31. package/dist/catalog/org-catalog.controller.d.ts +9 -0
  32. package/dist/catalog/org-catalog.controller.js +18 -0
  33. package/dist/catalog/org-catalog.schemas.d.ts +2 -4
  34. package/dist/catalog/org-catalog.schemas.js +2 -3
  35. package/dist/catalog/org-catalog.service.d.ts +10 -1
  36. package/dist/catalog/org-catalog.service.js +34 -23
  37. package/dist/catalog/org-extension-switch.d.ts +3 -10
  38. package/dist/catalog/org-extension-switch.js +14 -60
  39. package/dist/catalog/org-extension.controller.d.ts +6 -7
  40. package/dist/catalog/org-extension.controller.js +4 -4
  41. package/dist/catalog/org-extension.schemas.d.ts +1 -2
  42. package/dist/catalog/org-extension.schemas.js +1 -3
  43. package/dist/catalog/owned-items.js +10 -1
  44. package/dist/catalog/ownership.js +2 -0
  45. package/dist/catalog/plugin-import.js +8 -0
  46. package/dist/catalog/plugin-install.d.ts +1 -4
  47. package/dist/catalog/plugin-install.js +11 -12
  48. package/dist/catalog/plugin-upgrade.js +4 -1
  49. package/dist/catalog/request-rules.js +6 -0
  50. package/dist/catalog/requests.service.js +4 -0
  51. package/dist/catalog/scope-orgs.js +1 -0
  52. package/dist/catalog/scope-sync.d.ts +8 -5
  53. package/dist/catalog/scope-sync.js +21 -23
  54. package/dist/catalog/unshare.js +3 -0
  55. package/dist/channels/connections.service.js +1 -0
  56. package/dist/channels/inbound.js +1 -0
  57. package/dist/channels/jobs.js +1 -0
  58. package/dist/channels/outbound.js +6 -0
  59. package/dist/channels/supervisor.js +1 -0
  60. package/dist/channels/threads.js +1 -0
  61. package/dist/client/client-reports.service.js +1 -0
  62. package/dist/config/fields.js +20 -4
  63. package/dist/conversations/access.js +1 -0
  64. package/dist/conversations/attachments.service.js +16 -4
  65. package/dist/conversations/conversations.service.js +17 -3
  66. package/dist/conversations/event-log.js +2 -0
  67. package/dist/conversations/events.d.ts +1 -0
  68. package/dist/conversations/events.js +3 -1
  69. package/dist/conversations/files.js +3 -2
  70. package/dist/conversations/permission-profiles.service.js +1 -0
  71. package/dist/conversations/prompt-suggestions.js +2 -0
  72. package/dist/conversations/queue.js +8 -0
  73. package/dist/conversations/queue.service.js +6 -2
  74. package/dist/conversations/source.js +2 -0
  75. package/dist/conversations/titles.js +10 -2
  76. package/dist/conversations/tree.js +6 -0
  77. package/dist/db/kernel-schema.d.ts +3 -1
  78. package/dist/devices/conversation-devices.js +3 -0
  79. package/dist/devices/device-projects.service.js +2 -0
  80. package/dist/devices/devices.service.js +5 -0
  81. package/dist/devices/legacy-cleanup.job.js +5 -2
  82. package/dist/dial-in/handlers.js +2 -0
  83. package/dist/dial-in/machines.js +11 -2
  84. package/dist/dial-in/targets.d.ts +0 -2
  85. package/dist/dial-in/targets.js +0 -10
  86. package/dist/errors.d.ts +0 -1
  87. package/dist/errors.js +0 -1
  88. package/dist/evals/budget.js +14 -1
  89. package/dist/evals/run-replay.js +4 -0
  90. package/dist/evals/runs.service.js +13 -7
  91. package/dist/evals/sets.service.js +23 -8
  92. package/dist/evals/trajectory.service.d.ts +1 -1
  93. package/dist/evals/trajectory.service.js +1 -0
  94. package/dist/evals/worker.js +5 -1
  95. package/dist/extensions/admin-ops.js +1 -0
  96. package/dist/extensions/admin-view.js +3 -1
  97. package/dist/extensions/bootstrap-packages.d.ts +12 -1
  98. package/dist/extensions/bootstrap-packages.js +49 -9
  99. package/dist/extensions/bootstrap.d.ts +17 -2
  100. package/dist/extensions/bootstrap.js +41 -9
  101. package/dist/extensions/config-json-schema.d.ts +2 -2
  102. package/dist/extensions/config-json-schema.js +1 -3
  103. package/dist/extensions/deployment-config.d.ts +5 -3
  104. package/dist/extensions/deployment-config.js +3 -3
  105. package/dist/extensions/effective-config.d.ts +8 -7
  106. package/dist/extensions/effective-config.js +8 -7
  107. package/dist/extensions/enabled.js +1 -0
  108. package/dist/extensions/host.d.ts +4 -3
  109. package/dist/extensions/host.js +11 -12
  110. package/dist/extensions/instance.d.ts +1 -1
  111. package/dist/extensions/instance.js +17 -2
  112. package/dist/extensions/org-config.d.ts +35 -97
  113. package/dist/extensions/org-config.js +48 -315
  114. package/dist/extensions/org-levels.d.ts +22 -9
  115. package/dist/extensions/org-levels.js +100 -20
  116. package/dist/extensions/org-overrides.d.ts +14 -15
  117. package/dist/extensions/org-overrides.js +20 -20
  118. package/dist/extensions/package.js +1 -1
  119. package/dist/extensions/permission-set.d.ts +1 -3
  120. package/dist/extensions/permission-set.js +3 -7
  121. package/dist/extensions/permission-view.d.ts +3 -1
  122. package/dist/extensions/permission-view.js +5 -4
  123. package/dist/extensions/plugin-icons.d.ts +72 -1
  124. package/dist/extensions/plugin-icons.js +194 -2
  125. package/dist/extensions/point-defaults.d.ts +17 -18
  126. package/dist/extensions/point-defaults.js +25 -28
  127. package/dist/identity/accounts.js +6 -0
  128. package/dist/identity/api-tokens.js +8 -0
  129. package/dist/infra/outbox/outbox.dispatcher.js +4 -1
  130. package/dist/infra/outbox/outbox.service.js +2 -0
  131. package/dist/orgs/directory-sync.d.ts +9 -6
  132. package/dist/orgs/directory-sync.js +15 -12
  133. package/dist/orgs/invitations.service.js +2 -0
  134. package/dist/orgs/members.service.js +2 -0
  135. package/dist/orgs/offboarding.js +2 -0
  136. package/dist/packages/npm.d.ts +9 -1
  137. package/dist/packages/npm.js +37 -5
  138. package/dist/platform/exclusions.js +20 -0
  139. package/dist/platform/ops/admin-items.ops.js +1 -0
  140. package/dist/platform/ops/agents.ops.js +1 -0
  141. package/dist/platform/ops/extensions.ops.d.ts +6 -8
  142. package/dist/platform/ops/extensions.ops.js +18 -97
  143. package/dist/platform/ops/org-plugins.ops.d.ts +3 -6
  144. package/dist/platform/ops/org-plugins.ops.js +14 -65
  145. package/dist/platform/ops/outbox.ops.js +1 -0
  146. package/dist/platform/ops/person-inputs.d.ts +0 -4
  147. package/dist/platform/ops/person-inputs.js +0 -8
  148. package/dist/platform/ops/plugins.ops.d.ts +4 -1
  149. package/dist/platform/ops/plugins.ops.js +27 -34
  150. package/dist/platform/person-fields.d.ts +1 -1
  151. package/dist/platform/person-fields.js +1 -1
  152. package/dist/platform/sealed-reveals.js +2 -0
  153. package/dist/push/recipients.js +9 -2
  154. package/dist/runtime/permission-context.service.js +1 -0
  155. package/dist/runtime/plugin-context.service.js +1 -0
  156. package/dist/runtime/project/knowledge.service.js +1 -0
  157. package/dist/runtime/run-context.service.js +1 -0
  158. package/dist/runtime/run-deletion.js +4 -0
  159. package/dist/runtime/run-execution.js +2 -0
  160. package/dist/runtime/run-finish.js +3 -0
  161. package/dist/runtime/run-jobs.js +3 -0
  162. package/dist/runtime/run-orphan-check.job.js +7 -0
  163. package/dist/runtime/run-queue.js +21 -2
  164. package/dist/runtime/run-worker.js +5 -0
  165. package/dist/runtime/step-log.js +1 -0
  166. package/dist/runtime/subagents/child.js +2 -0
  167. package/dist/runtime/subagents/deliveries.js +3 -0
  168. package/dist/runtime/subagents/dispatch.js +5 -1
  169. package/dist/runtime/subagents/messages.js +2 -0
  170. package/dist/runtime/subagents/org-agents.d.ts +1 -1
  171. package/dist/runtime/subagents/org-agents.js +2 -1
  172. package/dist/runtime/usage-report-jobs.js +1 -0
  173. package/dist/runtime/usage-report.js +3 -0
  174. package/dist/runtime/waits.js +5 -0
  175. package/dist/sandbox/cloud-permission-snapshot.js +9 -3
  176. package/dist/sandbox/device-reports.js +3 -0
  177. package/dist/sandbox/legacy-retirement.js +5 -1
  178. package/dist/sandbox/managed-device-context.js +1 -0
  179. package/dist/sandbox/modes.d.ts +5 -2
  180. package/dist/sandbox/modes.js +8 -4
  181. package/dist/sandbox/network-grants.js +2 -0
  182. package/dist/sandbox/operation-intents.js +58 -0
  183. package/dist/sandbox/operation-reconcile.job.js +2 -0
  184. package/dist/sandbox/permission-authority-lock.js +12 -6
  185. package/dist/sandbox/permission-call-grants.js +6 -0
  186. package/dist/sandbox/permission-ceiling-writes.js +5 -0
  187. package/dist/sandbox/permission-config-publication.js +5 -0
  188. package/dist/sandbox/permission-instance-writes.js +4 -0
  189. package/dist/sandbox/permission-runtime-reports.js +10 -1
  190. package/dist/sandbox/permission-transitions.js +3 -0
  191. package/dist/sandbox/review/review.service.js +8 -5
  192. package/dist/sandbox/sandbox.service.d.ts +14 -0
  193. package/dist/sandbox/sandbox.service.js +195 -29
  194. package/dist/sandbox/unreachable.js +4 -1
  195. package/dist/schedules/fire.js +9 -3
  196. package/dist/schedules/pause.js +2 -0
  197. package/dist/schedules/run-tracker.js +16 -1
  198. package/dist/schedules/scheduled-tasks.service.js +4 -2
  199. package/dist/schedules/store.js +8 -0
  200. package/dist/schedules/unattended.js +4 -0
  201. package/dist/setup/setup-checks.service.d.ts +1 -10
  202. package/dist/setup/setup-checks.service.js +19 -29
  203. package/dist/shares/share-open.service.js +1 -0
  204. package/dist/shares/shares.service.js +2 -0
  205. package/dist/skills/agent-skills.service.d.ts +1 -1
  206. package/dist/skills/agent-skills.service.js +13 -2
  207. package/dist/skills/catalog.js +3 -0
  208. package/dist/skills/changes.service.js +10 -2
  209. package/dist/skills/conversation-catalog.js +1 -0
  210. package/dist/skills/skills.service.js +4 -0
  211. package/dist/tools/approvals.service.js +40 -0
  212. package/dist/tools/builtins/schedules.js +11 -6
  213. package/dist/tools/builtins/web/fetch.js +1 -1
  214. package/dist/tools/builtins/web/search.js +1 -1
  215. package/dist/tools/connector-account-policies.js +3 -0
  216. package/dist/tools/connectors.controller.d.ts +20 -8
  217. package/dist/tools/connectors.controller.js +30 -12
  218. package/dist/tools/connectors.service.d.ts +8 -0
  219. package/dist/tools/connectors.service.js +21 -1
  220. package/dist/tools/drift.js +5 -0
  221. package/dist/tools/git-push-policy.js +3 -0
  222. package/dist/tools/oauth/flow.js +1 -0
  223. package/dist/tools/outputs.js +2 -0
  224. package/dist/tools/permission-approval-jobs.js +3 -0
  225. package/dist/tools/pipeline.js +2 -0
  226. package/dist/tools/policy.js +3 -0
  227. package/dist/tools/questions.service.js +13 -0
  228. package/package.json +3 -3
@@ -133,6 +133,8 @@ let RunQueue = class RunQueue {
133
133
  return this.startCompactionIn(conversationId, accountId);
134
134
  }
135
135
  /** {@link startCompaction}'s transaction: the active-run check, the run row and its owed job. */
136
+ // org-filter: the only caller (ConversationsService.compact) checked writable(orgId, ...) first;
137
+ // the conversation's own org is read under its row lock
136
138
  async startCompactionIn(conversationId, accountId) {
137
139
  const t = this.db;
138
140
  const conv = await t
@@ -166,6 +168,7 @@ let RunQueue = class RunQueue {
166
168
  .selectFrom("agents")
167
169
  .select(["current_version", "deleted_at"])
168
170
  .where("id", "=", conv.agent_id)
171
+ .where("org_id", "=", conv.org_id)
169
172
  .executeTakeFirst();
170
173
  if (!agent || agent.deleted_at !== null) {
171
174
  throw new ApiError("conversation.not_found", "Conversation not found");
@@ -193,6 +196,8 @@ let RunQueue = class RunQueue {
193
196
  return runId;
194
197
  }
195
198
  /** {@link ensureRun}'s transaction: what it decided goes back through `st`. */
199
+ // org-filter: ensure-run is keyed by a kernel-resolved conversation id (after a write that checked
200
+ // the org, or an owed outbox job); the conversation's own org is read under its row lock
196
201
  async ensureRunIn(conversationId, st) {
197
202
  const t = this.db;
198
203
  const conv = await t
@@ -248,6 +253,7 @@ let RunQueue = class RunQueue {
248
253
  .selectFrom("agents")
249
254
  .select("deleted_at")
250
255
  .where("id", "=", conv.agent_id)
256
+ .where("org_id", "=", conv.org_id)
251
257
  .executeTakeFirstOrThrow();
252
258
  if (owner.deleted_at !== null)
253
259
  return null;
@@ -322,6 +328,7 @@ let RunQueue = class RunQueue {
322
328
  .selectFrom("agents")
323
329
  .select(["current_version", "deleted_at"])
324
330
  .where("id", "=", conv.agent_id)
331
+ .where("org_id", "=", conv.org_id)
325
332
  .executeTakeFirstOrThrow();
326
333
  // agent 已软删除:会话对任何人都不可见,不再为它起运行。
327
334
  if (agent.deleted_at !== null)
@@ -358,6 +365,7 @@ let RunQueue = class RunQueue {
358
365
  .updateTable("conversations")
359
366
  .set({ tree_stopped_at: null })
360
367
  .where("id", "=", conversationId)
368
+ .where("org_id", "=", conv.org_id)
361
369
  .execute();
362
370
  }
363
371
  return runId;
@@ -437,7 +445,7 @@ let RunQueue = class RunQueue {
437
445
  // with nothing running (or one whose report raced the stop) is not woken by a late finish
438
446
  if (await treeStopped(t, rootId))
439
447
  return null;
440
- const wake = (await this.pendingMessageIn(conversationId, Number(conv.consumed_seq), rootId)) ??
448
+ const wake = (await this.pendingMessageIn(conversationId, Number(conv.consumed_seq), rootId, conv.org_id)) ??
441
449
  (await wakeDecisionIn(t, { id: conversationId, orgId: conv.org_id, rootId }, Number(conv.consumed_seq), this.config.get().runtime.wakeOnBackgroundTask, (accountId) => authorCanSend(t, rootId, accountId)));
442
450
  if (wake?.kind === "later") {
443
451
  await this.jobs.oweBgWake(conversationId, wake, st.wakeRetry, conv.org_id);
@@ -449,6 +457,7 @@ let RunQueue = class RunQueue {
449
457
  .selectFrom("agents")
450
458
  .select(["current_version", "deleted_at"])
451
459
  .where("id", "=", conv.agent_id)
460
+ .where("org_id", "=", conv.org_id)
452
461
  .executeTakeFirstOrThrow();
453
462
  if (agent.deleted_at !== null)
454
463
  return null;
@@ -489,7 +498,7 @@ let RunQueue = class RunQueue {
489
498
  * `aborted`): a message that reached it while running and then stopped is parked, and the next
490
499
  * run its parent starts absorbs it. Called under the sub-agent's row lock.
491
500
  */
492
- async pendingMessageIn(conversationId, consumed, rootId) {
501
+ async pendingMessageIn(conversationId, consumed, rootId, orgId) {
493
502
  const t = this.db;
494
503
  const message = await t
495
504
  .selectFrom("conversation_events")
@@ -506,6 +515,7 @@ let RunQueue = class RunQueue {
506
515
  .selectFrom("conversations")
507
516
  .select(["queue_paused", "deleted_at"])
508
517
  .where("id", "=", rootId)
518
+ .where("org_id", "=", orgId)
509
519
  .executeTakeFirst();
510
520
  if (!root || root.queue_paused || !isLive(root.deleted_at))
511
521
  return null;
@@ -538,6 +548,8 @@ let RunQueue = class RunQueue {
538
548
  * before that call leaves no held wake behind. A root whose queue is still paused (or that is
539
549
  * deleted) releases nothing and owes nothing.
540
550
  */
551
+ // org-filter: every caller (QueueService resume / remove / sendNow) checked
552
+ // writable(orgId, rootId, ...) first
541
553
  async oweHeldTreeIn(rootId) {
542
554
  const root = await this.db
543
555
  .selectFrom("conversations")
@@ -555,6 +567,7 @@ let RunQueue = class RunQueue {
555
567
  * unconsumed wake event (a finished background task, a delivery) or message, and no active run;
556
568
  * with their org and newest seq. Reads only (no row locks).
557
569
  */
570
+ // org-filter: the sub-agents below a root the caller checked; each row's own org is returned
558
571
  async heldTree(rootId) {
559
572
  const ids = (await conversationTree(this.db, rootId))
560
573
  .filter((c) => c.level > 0)
@@ -606,6 +619,8 @@ let RunQueue = class RunQueue {
606
619
  await this.dropCheckpoints(runId);
607
620
  return true;
608
621
  }
622
+ // org-filter: callers checked the conversation in the org (ConversationsService.abort via get),
623
+ // stop a tree's kernel-resolved descendants, or pass an org offboarding job's kernel-written id
609
624
  async abortOrphanIn(conversationId, runId, byAccountId, detail) {
610
625
  const t = this.db;
611
626
  const conv = await t
@@ -653,6 +668,8 @@ let RunQueue = class RunQueue {
653
668
  await this.dropCheckpoints(runId);
654
669
  return true;
655
670
  }
671
+ // org-filter: callers checked the conversation in the org (ConversationsService.abort via get) or
672
+ // pass the kernel-written id of an org offboarding job
656
673
  async abortQueuedIn(conversationId, runId, byAccountId) {
657
674
  const t = this.db;
658
675
  const conv = await t
@@ -766,6 +783,7 @@ async function authorCanSend(t, conversationId, accountId) {
766
783
  .selectFrom("agents")
767
784
  .select(sql `1`.as("one"))
768
785
  .whereRef("agents.id", "=", "conversations.agent_id")
786
+ .whereRef("agents.org_id", "=", "conversations.org_id")
769
787
  .where("agents.deleted_at", "is", null)
770
788
  .where(visibleTo(accountId))),
771
789
  ]))
@@ -776,6 +794,7 @@ async function authorCanSend(t, conversationId, accountId) {
776
794
  * Whether a person's stop of the root conversation holds its tree (`tree_stopped_at`, agent map
777
795
  * spec §6): set by the stop, cleared when the root next starts a run.
778
796
  */
797
+ // org-filter: rootId is rootConversationOf a conversation the caller holds the row lock of
779
798
  async function treeStopped(t, rootId) {
780
799
  const root = await t
781
800
  .selectFrom("conversations")
@@ -427,6 +427,7 @@ let RunWorker = class RunWorker {
427
427
  if (run.status === "waiting")
428
428
  return;
429
429
  await this.hooks?.afterRunRead?.(runId, conversationId);
430
+ // org-filter: the run job's kernel-written conversation id; reads the conversation's own org
430
431
  const conv = await this.db
431
432
  .selectFrom("conversations")
432
433
  .select(["org_id", "agent_id", "kind", "subagent_depth", "source", "account_id"])
@@ -799,6 +800,7 @@ let RunWorker = class RunWorker {
799
800
  .selectFrom("conversations")
800
801
  .select("consumed_seq")
801
802
  .where("id", "=", conversationId)
803
+ .where("org_id", "=", conv.org_id)
802
804
  .executeTakeFirstOrThrow();
803
805
  const startConsumed = Number(consumed_seq);
804
806
  let absorbed = startConsumed;
@@ -974,6 +976,7 @@ let RunWorker = class RunWorker {
974
976
  .selectFrom("conversations")
975
977
  .select("consumed_seq")
976
978
  .where("id", "=", conversationId)
979
+ .where("org_id", "=", conv.org_id)
977
980
  .executeTakeFirstOrThrow();
978
981
  const ac = new AbortController();
979
982
  let abortedBy;
@@ -1110,6 +1113,7 @@ let RunWorker = class RunWorker {
1110
1113
  * (conversation, then run, as `RunFinish.finishIn`): the job read its status before taking the
1111
1114
  * conversation lock, and a stop may have ended it since.
1112
1115
  */
1116
+ // org-filter: locks the conversation of the run this worker holds (kernel-written job id)
1113
1117
  async endedUnderLocks(conversationId, runId) {
1114
1118
  await this.db
1115
1119
  .selectFrom("conversations")
@@ -1180,6 +1184,7 @@ let RunWorker = class RunWorker {
1180
1184
  return { toSeq: ev?.seq, agent };
1181
1185
  }
1182
1186
  /** {@link fallBackIfUnavailable}'s write: null when the person switched again meanwhile. */
1187
+ // org-filter: the conversation of the run this worker holds (kernel-written job id)
1183
1188
  async fallBackIn(conversationId, runId, sel) {
1184
1189
  const t = this.db;
1185
1190
  // re-check under the row lock: a person who switched again since the read above wins
@@ -64,6 +64,7 @@ let StepLog = class StepLog {
64
64
  async appendCounted(conversationId, counter, events) {
65
65
  const t = this.kdb.db;
66
66
  // the conversation row lock the append takes anyway, taken first to read the seq before it
67
+ // org-filter: a sub-agent run's own conversation (kernel-resolved by the run worker)
67
68
  const before = await t
68
69
  .selectFrom("conversations")
69
70
  .select("last_seq")
@@ -79,6 +79,8 @@ export async function dispatchOfCall(db, runId, callId) {
79
79
  * conversation without a parent); just `[conversationId]` for a root or an unknown id. Walks at
80
80
  * most 16 levels, far above `subagents.maxDepth`'s bound of 5.
81
81
  */
82
+ // org-filter: walks parent links of a kernel-resolved conversation (a run's); a sub-agent
83
+ // conversation is always created in its parent's org
82
84
  async function ancestryOf(db, conversationId) {
83
85
  const chain = [conversationId];
84
86
  for (let i = 0; i < 16; i++) {
@@ -66,6 +66,7 @@ let SubagentDeliveries = class SubagentDeliveries {
66
66
  * the commit: the log's notify and `subagent.delivered`; the parent's ensure-run backstop is
67
67
  * owed in the transaction. Returns the new event's seq.
68
68
  */
69
+ // org-filter: the dispatch row is kernel-written; its parent conversation's own org is read here
69
70
  async deliverIn(row, d, opts = {}) {
70
71
  const idemKey = opts.idemKey ?? (d.kind === "finished" ? finishedDeliveryIdemKey(row.childRunId) : undefined);
71
72
  if (idemKey === undefined)
@@ -176,6 +177,7 @@ let SubagentDeliveries = class SubagentDeliveries {
176
177
  * notify after the commit. No `subagent.delivered`: nothing model-visible arrived, so no wake.
177
178
  * True when this call wrote it.
178
179
  */
180
+ // org-filter: locks the parent conversation of a kernel-written dispatch row
179
181
  async finishSilentlyIn(row, finished) {
180
182
  const parent = row.parentConversationId;
181
183
  const event = finishedEvent(row, finished);
@@ -211,6 +213,7 @@ let SubagentDeliveries = class SubagentDeliveries {
211
213
  const dispatched = await this.eventLog.findByIdem(row.parentConversationId, dispatchedIdemKey(row.parentRunId, row.parentCallId));
212
214
  if (dispatched?.type === "subagent_dispatched")
213
215
  return dispatched.payload.description;
216
+ // org-filter: the child conversation of a kernel-written dispatch row
214
217
  const conv = await this.db
215
218
  .selectFrom("conversations")
216
219
  .select("title")
@@ -121,6 +121,7 @@ export async function dispatchSubagent(d, a) {
121
121
  .selectFrom("conversations")
122
122
  .select("account_id")
123
123
  .where("id", "=", ctx.conversationId)
124
+ .where("org_id", "=", ctx.orgId)
124
125
  .executeTakeFirstOrThrow();
125
126
  await t
126
127
  .insertInto("conversations")
@@ -185,7 +186,7 @@ export async function dispatchSubagent(d, a) {
185
186
  */
186
187
  export async function orgAgentGates(db, a, agentId, agentType, resumed) {
187
188
  const { ctx } = a;
188
- const parentDefinition = (await currentAgentDefinition(db, ctx.agentId)) ?? {};
189
+ const parentDefinition = (await currentAgentDefinition(db, ctx.orgId, ctx.agentId)) ?? {};
189
190
  const check = await checkOrgAgent(db, {
190
191
  orgId: ctx.orgId,
191
192
  parentDefinition,
@@ -276,6 +277,7 @@ export async function commitDispatchIn(d, a, plan, child) {
276
277
  .selectFrom("conversations")
277
278
  .select("id")
278
279
  .where("id", "=", ctx.conversationId)
280
+ .where("org_id", "=", ctx.orgId)
279
281
  .forUpdate()
280
282
  .executeTakeFirst();
281
283
  const parent = await t
@@ -353,6 +355,7 @@ export async function commitDispatchIn(d, a, plan, child) {
353
355
  .selectFrom("agents")
354
356
  .select("id")
355
357
  .where("id", "in", [ctx.agentId, plan.agentId])
358
+ .where("org_id", "=", ctx.orgId)
356
359
  .orderBy("id")
357
360
  .forShare()
358
361
  .execute();
@@ -545,6 +548,7 @@ async function limitRefusal(t, d, ctx, budgetRunId, tokensUsed, texts) {
545
548
  .selectFrom("conversations as c")
546
549
  .select(sql `count(*)`.as("n"))
547
550
  .where("c.parent_conversation_id", "=", ctx.conversationId)
551
+ .where("c.org_id", "=", ctx.orgId)
548
552
  .where("c.kind", "=", "subagent")
549
553
  .where("c.deleted_at", "is", null)
550
554
  .where(({ exists, selectFrom }) => exists(selectFrom("runs as cr")
@@ -202,6 +202,7 @@ let SubagentMessages = class SubagentMessages {
202
202
  .selectFrom("conversations")
203
203
  .select("id")
204
204
  .where("id", "=", childId)
205
+ .where("org_id", "=", ctx.orgId)
205
206
  .forUpdate()
206
207
  .executeTakeFirst();
207
208
  if ((await this.activeRunOf(childId)) === undefined)
@@ -254,6 +255,7 @@ let SubagentMessages = class SubagentMessages {
254
255
  .selectFrom("conversations")
255
256
  .select("id")
256
257
  .where("id", "=", plan.childConversationId)
258
+ .where("org_id", "=", ctx.orgId)
257
259
  .forUpdate()
258
260
  .executeTakeFirst();
259
261
  if (await activeRun())
@@ -32,7 +32,7 @@ export declare function checkOrgAgent(db: KernelSchemaDb, a: {
32
32
  * The parent agent's current definition (the allowlist gate reads it, not the run's version), or
33
33
  * null when the agent no longer exists or is deleted.
34
34
  */
35
- export declare function currentAgentDefinition(db: KernelSchemaDb, agentId: string): Promise<AgentDefinition | null>;
35
+ export declare function currentAgentDefinition(db: KernelSchemaDb, orgId: string, agentId: string): Promise<AgentDefinition | null>;
36
36
  /**
37
37
  * Audits a refused org agent dispatch (`subagent.refused`, spec §5.5): actor the person who
38
38
  * started the parent run, target the refused agent, so its owner can see attempts to use it.
@@ -35,7 +35,7 @@ export async function checkOrgAgent(db, a) {
35
35
  * The parent agent's current definition (the allowlist gate reads it, not the run's version), or
36
36
  * null when the agent no longer exists or is deleted.
37
37
  */
38
- export async function currentAgentDefinition(db, agentId) {
38
+ export async function currentAgentDefinition(db, orgId, agentId) {
39
39
  const row = await db
40
40
  .selectFrom("agents")
41
41
  .innerJoin("agent_versions", (j) => j
@@ -43,6 +43,7 @@ export async function currentAgentDefinition(db, agentId) {
43
43
  .onRef("agent_versions.version", "=", "agents.current_version"))
44
44
  .select("agent_versions.definition")
45
45
  .where("agents.id", "=", agentId)
46
+ .where("agents.org_id", "=", orgId)
46
47
  // a deleted parent agent's running run allowlists nothing (batch 9 review M2)
47
48
  .where("agents.deleted_at", "is", null)
48
49
  .executeTakeFirst();
@@ -51,6 +51,7 @@ let UsageReportJobs = class UsageReportJobs {
51
51
  const usage = events.filter((e) => e.type === "usage");
52
52
  if (usage.length === 0 || !(await this.subscribers.any()))
53
53
  return;
54
+ // org-filter: reads the org of the conversation whose log this transaction just wrote
54
55
  const conv = await this.kdb.db
55
56
  .selectFrom("conversations")
56
57
  .select("org_id")
@@ -238,6 +238,7 @@ let UsageReporter = class UsageReporter {
238
238
  * and `usage.outputImages` when it produced any), else `embedding`; the calling extension, no
239
239
  * conversation fields; `eventId` is the row id. Null when the row no longer exists.
240
240
  */
241
+ // org-filter: the usage job's kernel-written model_usage id; the row's own org is reported
241
242
  async payloadOfUsage(id) {
242
243
  const row = await this.db
243
244
  .selectFrom("model_usage")
@@ -278,6 +279,7 @@ let UsageReporter = class UsageReporter {
278
279
  };
279
280
  }
280
281
  /** A `model_usage` job is done (delivered, or nothing left to deliver to): marks the row. */
282
+ // org-filter: marks the model_usage row of a kernel-written usage job id
281
283
  async markReported(data) {
282
284
  if (data.usageId === undefined)
283
285
  return;
@@ -289,6 +291,7 @@ let UsageReporter = class UsageReporter {
289
291
  .execute();
290
292
  }
291
293
  /** A `model_usage` job gave up after its last attempt: the row records it. */
294
+ // org-filter: marks the model_usage row of a kernel-written usage job id
292
295
  async markFailed(data) {
293
296
  if (data.usageId === undefined)
294
297
  return;
@@ -185,6 +185,8 @@ let Waits = class Waits {
185
185
  * Not a run of a sub-agent, or a `subagents` wait: nothing. Display only: a failure is logged,
186
186
  * the wait goes on.
187
187
  */
188
+ // org-filter: the wait's approval / question id was written by this run's own suspend (kernel
189
+ // state, not caller input)
188
190
  async surfaceInRoot(runId, conversationId, waitingFor) {
189
191
  if (waitingFor.kind === "subagents")
190
192
  return;
@@ -308,6 +310,8 @@ let Waits = class Waits {
308
310
  return true;
309
311
  }
310
312
  /** {@link abortWaiting}'s transaction; the open `Agent` calls it wrote go back through `st`. */
313
+ // org-filter: stops a run by kernel-resolved ids (ConversationsService.abort checked the org, or
314
+ // a tree stop / timeout / offboarding job); wait rows are read by that run's id
311
315
  async abortWaitingIn(conversationId, runId, byAccountId, detail, st) {
312
316
  const t = this.db;
313
317
  await t
@@ -428,6 +432,7 @@ let Waits = class Waits {
428
432
  * Whether the wait is over: an approval or input request is no longer pending (decided, expired
429
433
  * or cancelled); `subagents`: the run's fork invocation child is no longer active.
430
434
  */
435
+ // org-filter: the wait's approval / question id was written by this run's own suspend
431
436
  async isSettled(w, runId) {
432
437
  if (w.kind === "subagents") {
433
438
  const active = await this.db
@@ -175,6 +175,8 @@ let CloudPermissionSnapshots = class CloudPermissionSnapshots {
175
175
  }
176
176
  /** {@link reconcile} by device id alone (after a file reset); nothing for an unknown device. */
177
177
  async reconcileDevice(deviceId) {
178
+ // org-filter: reads the device's own org to scope the reconcile; the id comes from the
179
+ // kernel-enqueued reset-files job, not caller input
178
180
  const row = await this.db
179
181
  .selectFrom("devices")
180
182
  .select(["org_id", "permission_reconcile_required"])
@@ -210,7 +212,7 @@ let CloudPermissionSnapshots = class CloudPermissionSnapshots {
210
212
  ]);
211
213
  // the OLD algorithm on the old inputs: the snapshot columns are still empty here
212
214
  const old = effectiveDevicePolicy(deployment, settings, cloudChosenMode(device));
213
- const instance = await this.lockedInstance(deviceId);
215
+ const instance = await this.lockedInstance(orgId, deviceId);
214
216
  const rec = instance === null ? null : readInstanceLimits(instance.instance_limits);
215
217
  // what the instance really runs narrows the snapshot even when a newer value is pending
216
218
  const known = rec?.applied ?? null;
@@ -237,6 +239,7 @@ let CloudPermissionSnapshots = class CloudPermissionSnapshots {
237
239
  permission_reconcile_required: state === "reconcile-required",
238
240
  })
239
241
  .where("id", "=", deviceId)
242
+ .where("org_id", "=", orgId)
240
243
  .execute();
241
244
  // A snapshot narrowed by what the instance really runs changes the device's authority: it
242
245
  // goes through the device ceiling writer (epoch, Full consent, exact revocation) in this
@@ -284,7 +287,7 @@ let CloudPermissionSnapshots = class CloudPermissionSnapshots {
284
287
  return { state: "skipped" };
285
288
  if (!device.permission_reconcile_required)
286
289
  return { state: "existing" };
287
- const instance = await this.lockedInstance(deviceId);
290
+ const instance = await this.lockedInstance(orgId, deviceId);
288
291
  let state = "unprobed";
289
292
  let target;
290
293
  if (instance !== null) {
@@ -310,6 +313,7 @@ let CloudPermissionSnapshots = class CloudPermissionSnapshots {
310
313
  ...ownNetworkOf(narrowed.outerNetwork),
311
314
  })
312
315
  .where("id", "=", deviceId)
316
+ .where("org_id", "=", orgId)
313
317
  .execute();
314
318
  await this.publishNarrowing(orgId, deviceId, narrowed, fullNarrowed);
315
319
  }
@@ -326,6 +330,7 @@ let CloudPermissionSnapshots = class CloudPermissionSnapshots {
326
330
  .updateTable("devices")
327
331
  .set({ permission_reconcile_required: false })
328
332
  .where("id", "=", deviceId)
333
+ .where("org_id", "=", orgId)
329
334
  .execute();
330
335
  return state === "reconcile-required"
331
336
  ? { state, action: "reset-files", ...(target !== undefined && { target }) }
@@ -374,11 +379,12 @@ let CloudPermissionSnapshots = class CloudPermissionSnapshots {
374
379
  * The device's instance row when an instance or a file system exists (their limits are in
375
380
  * force); null when neither does (the next create applies the desired limits exactly).
376
381
  */
377
- async lockedInstance(deviceId) {
382
+ async lockedInstance(orgId, deviceId) {
378
383
  const row = await this.db
379
384
  .selectFrom("device_instances")
380
385
  .select(["handle", "status", "fs_status", "instance_limits", "generation"])
381
386
  .where("device_id", "=", deviceId)
387
+ .where("org_id", "=", orgId)
382
388
  .forUpdate()
383
389
  .executeTakeFirst();
384
390
  if (row === undefined)
@@ -158,6 +158,7 @@ let DeviceReports = class DeviceReports {
158
158
  .selectFrom("device_instances")
159
159
  .select("handle")
160
160
  .where("device_id", "=", deviceId)
161
+ .where("org_id", "=", device.org_id)
161
162
  .where("provider", "=", plugin)
162
163
  .executeTakeFirst();
163
164
  const ref = {
@@ -189,6 +190,7 @@ let DeviceReports = class DeviceReports {
189
190
  * none was read); null when no report is stored. Reads PG only.
190
191
  */
191
192
  async current(deviceId) {
193
+ // org-filter: read seam for tests only (no production caller); the id is never caller input
192
194
  const row = await this.db
193
195
  .selectFrom("devices")
194
196
  .select("report")
@@ -261,6 +263,7 @@ let DeviceReports = class DeviceReports {
261
263
  .updateTable("devices")
262
264
  .set({ report: JSON.stringify({ ...report, support }), report_at: sql `now()` })
263
265
  .where("id", "=", ref.deviceId)
266
+ .where("org_id", "=", ref.orgId)
264
267
  .execute();
265
268
  const before = storedRevision(row.report);
266
269
  const changed = before !== report.revision;
@@ -872,7 +872,11 @@ __decorate([
872
872
  Transactional({ propagation: "requires_new" })
873
873
  ], LegacyRetirementStore.prototype, "resolveCancelled", null);
874
874
  LegacyRetirementStore = __decorate([
875
- Injectable(),
875
+ Injectable()
876
+ // org-filter: deployment-wide retirement of legacy launches: the worker sweeps (retire-legacy-tasks
877
+ // job), the release-gate report and the cutover CLI span every org; the per-device and per-obligation
878
+ // calls come from SandboxService's retirement gate with kernel-held device and obligation ids
879
+ ,
876
880
  __param(0, Inject(KernelDb)),
877
881
  __param(1, Inject(TransactionHost)),
878
882
  __param(2, Inject(PermissionAuthorityLock))
@@ -227,6 +227,7 @@ let ManagedDeviceContextRecorder = class ManagedDeviceContextRecorder {
227
227
  .selectFrom("conversations")
228
228
  .select(["org_id", "kind", "eval_run_id"])
229
229
  .where("id", "=", input.conversationId)
230
+ .where("org_id", "=", input.orgId)
230
231
  .executeTakeFirst();
231
232
  if (!conv || conv.org_id !== input.orgId)
232
233
  throw new ApiError("conversation.not_found", "Conversation not found");
@@ -187,11 +187,14 @@ export declare function appliedLacksHost(a: AppliedLimits | null | undefined, b:
187
187
  * `device_instances.instance_limits` read leniently: `applied` is what the instance runs with
188
188
  * (null: unknown, e.g. an instance made before auto mode recorded tiers, which differs from every
189
189
  * effective value); `unapplied` is a newer value the provider could not apply without a file
190
- * reset.
190
+ * reset; `pending` is the target of an `update` the kernel sent and never heard back from (device
191
+ * update fencing design §7.2): the instance enforces `applied`, or `pending` if that update landed,
192
+ * so the next exclusive use applies the desired limits again.
191
193
  */
192
194
  export declare function readInstanceLimits(v: unknown): {
193
195
  applied: AppliedLimits | null;
194
196
  unapplied: AppliedLimits | null;
197
+ pending: AppliedLimits | null;
195
198
  };
196
199
  /** The JSON written to `device_instances.instance_limits`. */
197
- export declare function instanceLimitsJson(applied: AppliedLimits | null, unapplied?: AppliedLimits): string | null;
200
+ export declare function instanceLimitsJson(applied: AppliedLimits | null, unapplied?: AppliedLimits, pending?: AppliedLimits): string | null;
@@ -205,22 +205,26 @@ function appliedFrom(v) {
205
205
  * `device_instances.instance_limits` read leniently: `applied` is what the instance runs with
206
206
  * (null: unknown, e.g. an instance made before auto mode recorded tiers, which differs from every
207
207
  * effective value); `unapplied` is a newer value the provider could not apply without a file
208
- * reset.
208
+ * reset; `pending` is the target of an `update` the kernel sent and never heard back from (device
209
+ * update fencing design §7.2): the instance enforces `applied`, or `pending` if that update landed,
210
+ * so the next exclusive use applies the desired limits again.
209
211
  */
210
212
  export function readInstanceLimits(v) {
211
213
  if (typeof v !== "object" || v === null)
212
- return { applied: null, unapplied: null };
214
+ return { applied: null, unapplied: null, pending: null };
213
215
  return {
214
216
  applied: appliedFrom(v),
215
217
  unapplied: appliedFrom(v.unapplied),
218
+ pending: appliedFrom(v.pending),
216
219
  };
217
220
  }
218
221
  /** The JSON written to `device_instances.instance_limits`. */
219
- export function instanceLimitsJson(applied, unapplied) {
220
- if (applied === null && unapplied === undefined)
222
+ export function instanceLimitsJson(applied, unapplied, pending) {
223
+ if (applied === null && unapplied === undefined && pending === undefined)
221
224
  return null;
222
225
  return JSON.stringify({
223
226
  ...(applied !== null && appliedOf(applied)),
224
227
  ...(unapplied !== undefined && { unapplied: appliedOf(unapplied) }),
228
+ ...(pending !== undefined && { pending: appliedOf(pending) }),
225
229
  });
226
230
  }
@@ -89,6 +89,8 @@ let NetworkGrants = class NetworkGrants {
89
89
  async runState(runId, deviceId) {
90
90
  const task = deviceId === undefined ? undefined : await this.taskGrants({ runId, deviceId });
91
91
  const root = await budgetRunOf(this.db.db, runId);
92
+ // org-filter: runId is the executing tool call's own run (the pipeline's ctx.runId), so its
93
+ // run tree lies in that run's org; no caller-supplied id reaches this query
92
94
  const rows = await this.db
93
95
  .selectFrom("approvals")
94
96
  .select(["run_id", "status", "scope", "detail"])