@capybara-hq/kernel 0.1.0-rc.135 → 0.1.0-rc.137

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 (242) hide show
  1. package/dist/admin/admin.module.js +3 -0
  2. package/dist/admin/audit-log.service.d.ts +96 -12
  3. package/dist/admin/audit-log.service.js +508 -44
  4. package/dist/admin/audit-targets.d.ts +17 -0
  5. package/dist/admin/audit-targets.js +84 -0
  6. package/dist/admin/audit.controller.d.ts +12 -9
  7. package/dist/admin/audit.controller.js +29 -8
  8. package/dist/admin/audit.schemas.d.ts +20 -4
  9. package/dist/admin/audit.schemas.js +42 -17
  10. package/dist/admin/health.service.d.ts +2 -2
  11. package/dist/admin/health.service.js +6 -2
  12. package/dist/agents/templates.js +3 -10
  13. package/dist/app.d.ts +3 -1
  14. package/dist/app.js +1 -0
  15. package/dist/automations/automations.service.js +4 -5
  16. package/dist/catalog/catalog-content.d.ts +1 -1
  17. package/dist/catalog/catalog-content.js +17 -22
  18. package/dist/catalog/catalog-items.d.ts +5 -10
  19. package/dist/catalog/catalog-items.js +27 -51
  20. package/dist/catalog/catalog-scope.service.d.ts +4 -2
  21. package/dist/catalog/catalog-scope.service.js +105 -141
  22. package/dist/catalog/catalog.module.js +2 -0
  23. package/dist/catalog/catalog.service.d.ts +3 -3
  24. package/dist/catalog/catalog.service.js +33 -8
  25. package/dist/catalog/cli.js +17 -0
  26. package/dist/catalog/component-promotion.js +8 -4
  27. package/dist/catalog/component-scope.service.d.ts +3 -1
  28. package/dist/catalog/component-scope.service.js +101 -140
  29. package/dist/catalog/component-state.d.ts +10 -8
  30. package/dist/catalog/component-state.js +42 -30
  31. package/dist/catalog/dependency-edges.d.ts +25 -0
  32. package/dist/catalog/dependency-edges.js +125 -0
  33. package/dist/catalog/install-status.d.ts +2 -3
  34. package/dist/catalog/install-status.js +2 -3
  35. package/dist/catalog/marketplaces.service.d.ts +1 -1
  36. package/dist/catalog/org-catalog.service.d.ts +3 -1
  37. package/dist/catalog/org-catalog.service.js +33 -94
  38. package/dist/catalog/org-intent.d.ts +43 -0
  39. package/dist/catalog/org-intent.js +184 -0
  40. package/dist/catalog/owned-items.d.ts +1 -1
  41. package/dist/catalog/owned-items.js +9 -29
  42. package/dist/catalog/ownership.d.ts +2 -3
  43. package/dist/catalog/ownership.js +10 -28
  44. package/dist/catalog/platform-components.d.ts +3 -1
  45. package/dist/catalog/platform-components.js +9 -1
  46. package/dist/catalog/plugin-import.js +12 -16
  47. package/dist/catalog/plugin-install.d.ts +3 -4
  48. package/dist/catalog/plugin-install.js +25 -49
  49. package/dist/catalog/plugin-upgrade.d.ts +2 -2
  50. package/dist/catalog/plugin-upgrade.js +18 -34
  51. package/dist/catalog/request-rules.js +20 -8
  52. package/dist/catalog/scope-orgs.js +2 -7
  53. package/dist/catalog/scope-state.d.ts +6 -6
  54. package/dist/catalog/scope-state.js +24 -43
  55. package/dist/catalog/scope-sync.d.ts +0 -2
  56. package/dist/catalog/scope-sync.js +11 -39
  57. package/dist/catalog/scope-writer.d.ts +78 -0
  58. package/dist/catalog/scope-writer.js +181 -0
  59. package/dist/catalog/unshare.js +8 -41
  60. package/dist/channels/connections.service.js +6 -6
  61. package/dist/channels/supervisor.js +3 -7
  62. package/dist/client/client-access.service.js +2 -12
  63. package/dist/config/schema.d.ts +6 -0
  64. package/dist/config/schema.js +29 -1
  65. package/dist/conversations/access.d.ts +15 -7
  66. package/dist/conversations/access.js +59 -10
  67. package/dist/conversations/conversation-agent.d.ts +18 -0
  68. package/dist/conversations/conversation-agent.js +23 -0
  69. package/dist/conversations/conversation-ref.d.ts +3 -2
  70. package/dist/conversations/conversation-ref.js +6 -1
  71. package/dist/conversations/conversations.controller.d.ts +28 -5
  72. package/dist/conversations/conversations.controller.js +99 -10
  73. package/dist/conversations/conversations.module.js +10 -0
  74. package/dist/conversations/conversations.schemas.d.ts +6 -0
  75. package/dist/conversations/conversations.schemas.js +15 -0
  76. package/dist/conversations/conversations.service.d.ts +125 -21
  77. package/dist/conversations/conversations.service.js +260 -90
  78. package/dist/conversations/event-log.d.ts +9 -1
  79. package/dist/conversations/event-log.js +2 -0
  80. package/dist/conversations/events.d.ts +57 -0
  81. package/dist/conversations/events.js +73 -1
  82. package/dist/conversations/external/external-calls.d.ts +75 -0
  83. package/dist/conversations/external/external-calls.js +91 -0
  84. package/dist/conversations/external/external-calls.processor.d.ts +45 -0
  85. package/dist/conversations/external/external-calls.processor.js +144 -0
  86. package/dist/conversations/external/external-conversations.service.d.ts +266 -0
  87. package/dist/conversations/external/external-conversations.service.js +1138 -0
  88. package/dist/conversations/external/external.schemas.d.ts +63 -0
  89. package/dist/conversations/external/external.schemas.js +52 -0
  90. package/dist/conversations/external/token.d.ts +6 -0
  91. package/dist/conversations/external/token.js +6 -0
  92. package/dist/conversations/permission-profile-store.d.ts +4 -1
  93. package/dist/conversations/permission-profiles.controller.d.ts +33 -9
  94. package/dist/conversations/permission-profiles.controller.js +86 -6
  95. package/dist/conversations/prompt-suggestions.js +2 -1
  96. package/dist/conversations/queue.js +2 -1
  97. package/dist/conversations/titles.js +4 -3
  98. package/dist/ctx/create-ctx.d.ts +9 -2
  99. package/dist/ctx/create-ctx.js +47 -62
  100. package/dist/ctx/guard-db.d.ts +6 -0
  101. package/dist/ctx/guard-db.js +55 -0
  102. package/dist/db/extension-handoff-schema.d.ts +1 -1
  103. package/dist/db/extension-handoff-schema.js +48 -10
  104. package/dist/db/extension-schema-transfer.js +5 -2
  105. package/dist/db/kernel-schema.d.ts +42 -5
  106. package/dist/device-access/host-services.d.ts +2 -2
  107. package/dist/device-access/host-services.js +8 -8
  108. package/dist/device-access/person-handles.d.ts +16 -7
  109. package/dist/device-access/person-handles.js +14 -9
  110. package/dist/devices/conversation-devices.d.ts +5 -0
  111. package/dist/devices/conversation-devices.js +16 -0
  112. package/dist/erasure/org-catalog.js +2 -2
  113. package/dist/erasure/org-rules.js +5 -0
  114. package/dist/errors.d.ts +34 -0
  115. package/dist/errors.js +34 -0
  116. package/dist/evals/snapshot.js +3 -1
  117. package/dist/evals/trajectory.service.d.ts +2 -2
  118. package/dist/evals/trajectory.service.js +11 -7
  119. package/dist/extensions/admin-ops.d.ts +9 -5
  120. package/dist/extensions/admin-ops.js +52 -8
  121. package/dist/extensions/admin-view.js +1 -4
  122. package/dist/extensions/bootstrap.js +4 -1
  123. package/dist/extensions/cli.js +1 -0
  124. package/dist/extensions/effective-config.js +1 -4
  125. package/dist/extensions/enabled.d.ts +23 -29
  126. package/dist/extensions/enabled.js +141 -62
  127. package/dist/extensions/extensions.module.js +3 -0
  128. package/dist/extensions/host.d.ts +71 -45
  129. package/dist/extensions/host.js +454 -128
  130. package/dist/extensions/installs.d.ts +9 -0
  131. package/dist/extensions/installs.js +23 -0
  132. package/dist/extensions/isolated/admission.d.ts +10 -0
  133. package/dist/extensions/isolated/admission.js +206 -0
  134. package/dist/extensions/isolated/authorization.d.ts +37 -0
  135. package/dist/extensions/isolated/authorization.js +146 -0
  136. package/dist/extensions/isolated/broker.d.ts +15 -0
  137. package/dist/extensions/isolated/broker.js +452 -0
  138. package/dist/extensions/isolated/child-protocol.d.ts +84 -0
  139. package/dist/extensions/isolated/child-protocol.js +21 -0
  140. package/dist/extensions/isolated/child-registration.d.ts +22 -0
  141. package/dist/extensions/isolated/child-registration.js +152 -0
  142. package/dist/extensions/isolated/child-session.d.ts +6 -0
  143. package/dist/extensions/isolated/child-session.js +343 -0
  144. package/dist/extensions/isolated/child-streams.d.ts +4 -0
  145. package/dist/extensions/isolated/child-streams.js +142 -0
  146. package/dist/extensions/isolated/child.d.ts +1 -0
  147. package/dist/extensions/isolated/child.js +10 -0
  148. package/dist/extensions/isolated/configured-package.d.ts +15 -0
  149. package/dist/extensions/isolated/configured-package.js +73 -0
  150. package/dist/extensions/isolated/lanes.d.ts +100 -0
  151. package/dist/extensions/isolated/lanes.js +699 -0
  152. package/dist/extensions/isolated/postgres-gate.d.ts +14 -0
  153. package/dist/extensions/isolated/postgres-gate.js +255 -0
  154. package/dist/extensions/isolated/revision.d.ts +32 -0
  155. package/dist/extensions/isolated/revision.js +358 -0
  156. package/dist/extensions/isolated/rpc-frame.d.ts +50 -0
  157. package/dist/extensions/isolated/rpc-frame.js +193 -0
  158. package/dist/extensions/isolated/rpc.d.ts +66 -0
  159. package/dist/extensions/isolated/rpc.js +314 -0
  160. package/dist/extensions/isolated/sandbox.d.ts +35 -0
  161. package/dist/extensions/isolated/sandbox.js +372 -0
  162. package/dist/extensions/isolated/tier.d.ts +6 -0
  163. package/dist/extensions/isolated/tier.js +45 -0
  164. package/dist/extensions/ledger.d.ts +1 -1
  165. package/dist/extensions/lifecycle-topics.d.ts +1 -0
  166. package/dist/extensions/lifecycle-topics.js +1 -0
  167. package/dist/extensions/lifecycle.d.ts +3 -1
  168. package/dist/extensions/lifecycle.js +32 -5
  169. package/dist/extensions/locks.d.ts +2 -0
  170. package/dist/extensions/locks.js +7 -0
  171. package/dist/extensions/org-config.d.ts +17 -27
  172. package/dist/extensions/org-config.js +114 -129
  173. package/dist/extensions/permission-view.d.ts +3 -1
  174. package/dist/extensions/permission-view.js +4 -1
  175. package/dist/extensions/plugin-guides.d.ts +5 -10
  176. package/dist/extensions/plugin-guides.js +18 -37
  177. package/dist/extensions/plugin-icons.d.ts +3 -21
  178. package/dist/extensions/plugin-icons.js +9 -44
  179. package/dist/extensions/plugin-items.d.ts +10 -30
  180. package/dist/extensions/plugin-items.js +32 -73
  181. package/dist/extensions/point-defaults.d.ts +13 -21
  182. package/dist/extensions/point-defaults.js +32 -72
  183. package/dist/extensions/point-serving.d.ts +56 -0
  184. package/dist/extensions/point-serving.js +120 -0
  185. package/dist/extensions/source-intent.d.ts +14 -0
  186. package/dist/extensions/source-intent.js +77 -0
  187. package/dist/inbox/inbox-items.service.d.ts +11 -0
  188. package/dist/inbox/inbox-items.service.js +39 -0
  189. package/dist/index.d.ts +2 -1
  190. package/dist/index.js +2 -1
  191. package/dist/infra/database/database.module.js +6 -1
  192. package/dist/infra/database/kernel-db.d.ts +3 -1
  193. package/dist/infra/database/kernel-db.js +4 -0
  194. package/dist/infra/outbox/registry.d.ts +70 -0
  195. package/dist/infra/outbox/registry.js +44 -0
  196. package/dist/infra/paging/paged-read.d.ts +56 -0
  197. package/dist/infra/paging/paged-read.js +78 -0
  198. package/dist/infra/queue/queue-names.d.ts +1 -1
  199. package/dist/infra/queue/queue-names.js +1 -0
  200. package/dist/kernel.d.ts +3 -2
  201. package/dist/kernel.js +21 -2
  202. package/dist/kernel.module.js +4 -0
  203. package/dist/lifecycle/kernel-lifecycle.d.ts +3 -1
  204. package/dist/lifecycle/kernel-lifecycle.js +22 -12
  205. package/dist/lifecycle/tokens.d.ts +8 -0
  206. package/dist/lifecycle/tokens.js +2 -0
  207. package/dist/orgs/directory-sync.js +1 -4
  208. package/dist/platform/exclusions.js +10 -0
  209. package/dist/platform/ops/admin.ops.js +14 -3
  210. package/dist/platform/ops/conversations.ops.d.ts +8 -1
  211. package/dist/platform/ops/conversations.ops.js +52 -14
  212. package/dist/platform/ops/orgs.ops.js +13 -3
  213. package/dist/plugin-reads/plugin-reads.service.js +4 -1
  214. package/dist/push/recipients.js +5 -2
  215. package/dist/retention/worker.js +2 -1
  216. package/dist/roles/worker-role.d.ts +3 -1
  217. package/dist/roles/worker-role.js +8 -2
  218. package/dist/roles/worker.module.js +3 -0
  219. package/dist/runtime/context/build.js +8 -0
  220. package/dist/runtime/context/compaction.js +7 -0
  221. package/dist/runtime/idle-arm.js +3 -1
  222. package/dist/runtime/idle-compaction.js +2 -1
  223. package/dist/runtime/run-finish.js +2 -1
  224. package/dist/runtime/run-jobs.js +2 -1
  225. package/dist/runtime/run-queue.js +4 -2
  226. package/dist/runtime/run-worker.js +5 -2
  227. package/dist/runtime/stream.js +10 -0
  228. package/dist/runtime/usage-report.js +2 -1
  229. package/dist/runtime/waits.js +2 -1
  230. package/dist/setup/setup-checks.service.js +3 -2
  231. package/dist/skills/usable.js +41 -30
  232. package/dist/themes/theme-state.js +3 -3
  233. package/dist/tools/approvals.service.js +6 -2
  234. package/dist/tools/builtins/skill-load.js +3 -2
  235. package/dist/tools/registry.d.ts +1 -1
  236. package/dist/tools/registry.js +1 -1
  237. package/dist/tools/tool-watch.service.js +3 -1
  238. package/package.json +3 -3
  239. package/src/migrations/0227_external_conversations.sql +33 -0
  240. package/src/migrations/0228_extension_org_switches.sql +56 -0
  241. package/src/migrations/0229_catalog_dependency_edges.sql +17 -0
  242. package/src/migrations/0230_audit_filter_idx.sql +18 -0
@@ -0,0 +1,55 @@
1
+ import { Command, ConnectionBuilder, ControlledTransactionBuilder, Kysely, TransactionBuilder, } from "kysely";
2
+ import { isExtensionTransaction } from "../infra/database/extension-handoff.js";
3
+ /**
4
+ * Protects the owned pool and records the underlying transactions used for durable effects.
5
+ * Kysely methods use private fields, so they are always called on the underlying object.
6
+ */
7
+ export function guardDb(db, plugin, transactions) {
8
+ const guarded = new Proxy(db, {
9
+ get(target, prop) {
10
+ // Async disposal must never close an instance-owned pool.
11
+ if (prop === "destroy" || prop === Symbol.asyncDispose) {
12
+ return () => {
13
+ throw new Error(`plugin ${plugin}: ctx.db.destroy() is not allowed; the kernel owns the connection pool`);
14
+ };
15
+ }
16
+ const value = Reflect.get(target, prop, target);
17
+ if (typeof value !== "function")
18
+ return value;
19
+ return (...args) => guardValue(value.apply(target, args), plugin, transactions);
20
+ },
21
+ });
22
+ if (isExtensionTransaction(db))
23
+ transactions.set(guarded, db);
24
+ return guarded;
25
+ }
26
+ /** Builders can return another database handle or pass one into a callback. */
27
+ const BUILDERS = [ConnectionBuilder, TransactionBuilder, ControlledTransactionBuilder, Command];
28
+ const isBuilder = (v) => BUILDERS.some((B) => v instanceof B);
29
+ /** Wrap only values that can expose another database handle. */
30
+ function guardValue(v, plugin, transactions) {
31
+ if (v instanceof Kysely)
32
+ return guardDb(v, plugin, transactions);
33
+ if (isBuilder(v))
34
+ return guardBuilder(v, plugin, transactions);
35
+ return v;
36
+ }
37
+ /** Protect callback arguments, builder results and handles returned by promises. */
38
+ function guardBuilder(builder, plugin, transactions) {
39
+ return new Proxy(builder, {
40
+ get(target, prop) {
41
+ const value = Reflect.get(target, prop, target);
42
+ if (typeof value !== "function")
43
+ return value;
44
+ return (...args) => {
45
+ const wrapped = args.map((a) => typeof a === "function"
46
+ ? (...cbArgs) => a(...cbArgs.map((c) => guardValue(c, plugin, transactions)))
47
+ : a);
48
+ const out = guardValue(value.apply(target, wrapped), plugin, transactions);
49
+ return out instanceof Promise
50
+ ? out.then((r) => guardValue(r, plugin, transactions))
51
+ : out;
52
+ };
53
+ },
54
+ });
55
+ }
@@ -1,6 +1,6 @@
1
1
  import { type Kysely } from "kysely";
2
2
  import pg from "pg";
3
3
  /** Creates a handoff owned by the one-shot provisioner. The extension can append only. */
4
- export declare function provisionExtensionHandoff(owner: pg.ClientBase, schema: string, extensionLogin: string, runtimeLogin: string): Promise<void>;
4
+ export declare function provisionExtensionHandoff(owner: pg.ClientBase, schema: string, extensionLogin: string, runtimeLogin: string, legacyOwner?: string): Promise<void>;
5
5
  /** Single-login deployments use the kernel's own connection for this table. */
6
6
  export declare function ensureLocalExtensionHandoff(db: Kysely<unknown>, dbUrl: string, schema: string, connectTimeoutMs: number | undefined): Promise<void>;
@@ -3,9 +3,9 @@ import pg from "pg";
3
3
  import { EXTENSION_EFFECT_HANDOFF } from "../infra/database/extension-handoff.js";
4
4
  import { lockMigrationSchema } from "./migrator.js";
5
5
  import { pgConnectOptions } from "./pool.js";
6
- function createHandoffTable(table) {
6
+ function createHandoffTable(table, ifNotExists = true) {
7
7
  return `
8
- create table if not exists ${table} (
8
+ create table ${ifNotExists ? "if not exists " : ""}${table} (
9
9
  id uuid primary key,
10
10
  created_at timestamptz not null default pg_catalog.now(),
11
11
  attempts integer not null default 0,
@@ -22,20 +22,58 @@ function createHandoffTable(table) {
22
22
  `;
23
23
  }
24
24
  /** Creates a handoff owned by the one-shot provisioner. The extension can append only. */
25
- export async function provisionExtensionHandoff(owner, schema, extensionLogin, runtimeLogin) {
25
+ export async function provisionExtensionHandoff(owner, schema, extensionLogin, runtimeLogin, legacyOwner) {
26
26
  await lockMigrationSchema(owner, schema, "transaction");
27
27
  const ns = pg.escapeIdentifier(schema);
28
28
  const writer = pg.escapeIdentifier(extensionLogin);
29
29
  const reader = pg.escapeIdentifier(runtimeLogin);
30
30
  const table = `${ns}.${pg.escapeIdentifier(EXTENSION_EFFECT_HANDOFF)}`;
31
31
  await owner.query(`create schema if not exists ${ns} authorization ${writer}`);
32
- await owner.query(createHandoffTable(table));
33
- const { rows: [relation], } = await owner.query(`
34
- select c.relowner = (select oid from pg_roles where rolname = current_user) and c.relkind = 'r' as valid
35
- from pg_class c where c.oid = to_regclass($1)
36
- `, [table]);
37
- if (!relation?.valid)
38
- throw new Error(`Invalid handoff owner or type for ${schema}`);
32
+ const lookup = async () => {
33
+ const { rows } = await owner.query(`select c.oid, pg_get_userbyid(c.relowner) as login, current_user as provisioner,
34
+ c.relkind = 'r' and c.relpersistence = 'p' and not c.relrowsecurity and not exists (
35
+ select 1 from pg_inherits where inhrelid = c.oid or inhparent = c.oid
36
+ ) as plain
37
+ from pg_class c where c.oid = to_regclass($1)`, [table]);
38
+ return rows[0];
39
+ };
40
+ const invalid = () => new Error(`Invalid handoff owner or type for ${schema}`);
41
+ const valid = (relation) => relation.plain && (relation.login === relation.provisioner || relation.login === legacyOwner);
42
+ const existing = await lookup();
43
+ if (existing) {
44
+ if (!valid(existing))
45
+ throw invalid();
46
+ // The migration advisory lock covers Capybara sessions. The relation lock also excludes SQL
47
+ // DDL from other clients while the provisioner changes ownership and grants.
48
+ await owner.query(`lock table only ${table} in access exclusive mode`);
49
+ const locked = await lookup();
50
+ if (!locked || locked.oid !== existing.oid || !valid(locked))
51
+ throw invalid();
52
+ if (locked.login !== locked.provisioner) {
53
+ await owner.query(`alter table ${table} owner to current_user`);
54
+ // Replace legacy grants, including column ACLs that a table-wide REVOKE would leave behind.
55
+ const { rows: grants } = await owner.query(`with grants as (
56
+ select acl.grantee, null::name as column_name
57
+ from pg_class c cross join lateral aclexplode(c.relacl) acl where c.oid = $1
58
+ union
59
+ select acl.grantee, a.attname as column_name
60
+ from pg_attribute a cross join lateral aclexplode(a.attacl) acl
61
+ where a.attrelid = $1 and not a.attisdropped
62
+ )
63
+ select case when grantee = 0 then 'PUBLIC'
64
+ else quote_ident(pg_get_userbyid(grantee)) end as recipient, column_name
65
+ from grants where grantee <> current_user::regrole
66
+ order by column_name nulls first, recipient`, [locked.oid]);
67
+ for (const grant of grants) {
68
+ const privilege = grant.column_name
69
+ ? `all (${pg.escapeIdentifier(grant.column_name)})`
70
+ : "all";
71
+ await owner.query(`revoke ${privilege} on table ${table} from ${grant.recipient} cascade`);
72
+ }
73
+ }
74
+ }
75
+ // An object that appears after an absent lookup must not be implicitly adopted.
76
+ await owner.query(createHandoffTable(table, existing !== undefined));
39
77
  await owner.query(`revoke all on table ${table} from public, ${writer}`);
40
78
  await owner.query(`grant insert (id, envelope) on table ${table} to ${writer}`);
41
79
  await owner.query(`grant usage on schema ${ns} to ${reader}`);
@@ -133,7 +133,8 @@ export async function transferKnownExtensionSchemas(bootstrapUrl, identity, decl
133
133
  await owner.query("set local search_path = pg_catalog");
134
134
  await owner.query("set local lock_timeout = '30s'");
135
135
  const { rows: [legacy], } = await owner.query(`
136
- select current_user as login, pg_get_userbyid(nspowner) as kernel_owner
136
+ select current_user as login, pg_get_userbyid(nspowner) as kernel_owner,
137
+ coalesce(nullif(current_setting('capybara.bootstrap_legacy_role', true), ''), 'capybara') as single_login
137
138
  from pg_catalog.pg_namespace where nspname = 'capybara'
138
139
  `);
139
140
  if (!legacy)
@@ -151,6 +152,8 @@ export async function transferKnownExtensionSchemas(bootstrapUrl, identity, decl
151
152
  return { name, schema: pluginSchemaName(name), target: identity.resolve(name) };
152
153
  });
153
154
  const legacySources = new Set([legacy.login, legacy.kernel_owner]);
155
+ if (provisioning)
156
+ legacySources.add(legacy.single_login);
154
157
  if (identity.sharedLogin !== undefined)
155
158
  legacySources.add(identity.sharedLogin);
156
159
  const sources = new Set([...legacySources, ...resolved.map(({ target }) => target.login)]);
@@ -201,7 +204,7 @@ export async function transferKnownExtensionSchemas(bootstrapUrl, identity, decl
201
204
  await transferExtensionSchema(owner, schema, target.login, target.dedicated ? legacySources : sources, target.dedicated);
202
205
  if (provisioning && target.dedicated) {
203
206
  const relayLogin = provisioning.extensionEffectRelayLogin ?? "capybara_runtime";
204
- await provisionExtensionHandoff(owner, schema, target.login, relayLogin);
207
+ await provisionExtensionHandoff(owner, schema, target.login, relayLogin, legacy.single_login);
205
208
  revoked.push(...(await revokeForeignSchemaGrants(owner, schema, relayLogin)));
206
209
  }
207
210
  if (target.dedicated && identity.sharedLogin) {
@@ -1,4 +1,4 @@
1
- import type { CacheWriteByTtl, ConversationSource, FileSystemHandle, GitHostEntry, ParsedTheme, PermissionProfile, SandboxBrowseScope, SandboxExecutionRef, SandboxOperationScope, SandboxPermissionSupport, SandboxRoots, SiteAccessAction, SkillInvocation, TriggerFactValue, TriggerSubject } from "@capybara-hq/plugin-sdk";
1
+ import type { CacheWriteByTtl, ConversationSource, ExternalConversationState, FileSystemHandle, GitHostEntry, ParsedTheme, PermissionProfile, SandboxBrowseScope, SandboxExecutionRef, SandboxOperationScope, SandboxPermissionSupport, SandboxRoots, SiteAccessAction, SkillInvocation, TriggerFactValue, TriggerSubject } from "@capybara-hq/plugin-sdk";
2
2
  import type { ColumnType, Generated, Kysely } from "kysely";
3
3
  import type { ModelRef } from "../agents/definition.js";
4
4
  import type { AutomationChat, AutomationEventMarker, AutomationMode, AutomationOrigin, AutomationPauseReason, AutomationPrincipal, AutomationStatus, AutomationTrigger, AutomationUnattendedAsk, ScheduleMarker, TaskRunSkipReason, TaskRunStatus } from "../automations/types.js";
@@ -399,7 +399,8 @@ export interface ConversationsTable {
399
399
  create_request_hash: ColumnType<string | null, string | null | undefined, string | null>;
400
400
  id: string;
401
401
  org_id: string;
402
- agent_id: string;
402
+ /** 0227: null exactly when `kind` is 'external' (`conversations_external_shape`). */
403
+ agent_id: string | null;
403
404
  account_id: string;
404
405
  title: string | null;
405
406
  last_seq: Int8;
@@ -455,6 +456,29 @@ export interface ConversationsTable {
455
456
  project_name: ColumnType<string | null, string | null | undefined, never>;
456
457
  /** 0133: the absolute path the bound project resolved to at creation; null: free mode. */
457
458
  project_path: ColumnType<string | null, string | null | undefined, never>;
459
+ /** 0227: the target an external conversation is held with (set once); null for other kinds. */
460
+ external: ColumnType<ExternalRef | null, string | null | undefined, never>;
461
+ /** 0227: what the target last reported (external conversations; 'idle' for the others). */
462
+ external_state: ColumnType<ExternalConversationState, ExternalConversationState | undefined, ExternalConversationState>;
463
+ /**
464
+ * 0227: the highest `external_user_message.deliverySeq` handed to the target; -1 (written only
465
+ * by the kernel) once the conversation's `start` was dropped after its retries.
466
+ */
467
+ external_delivered_seq: Int8;
468
+ }
469
+ /** `conversations.external` (0227): the `conversation-target@1` target of an external conversation. */
470
+ export interface ExternalRef {
471
+ /** The extension that provides the target. */
472
+ extension: string;
473
+ targetKey: string;
474
+ /** The target's label as written at creation. */
475
+ label: string;
476
+ /** The options chosen for the start on the new conversation page (absent: none recorded). */
477
+ options?: {
478
+ model?: string;
479
+ effort?: string;
480
+ mode?: string;
481
+ };
458
482
  }
459
483
  /** 0060: an org's order of file-preview@1 previewers (UI refresh B Task 11). */
460
484
  export interface FilePreviewersTable {
@@ -514,8 +538,11 @@ export interface ConversationSubject {
514
538
  id: string;
515
539
  title: string;
516
540
  }
517
- /** chat; eval (a hidden full-run replay conversation); subagent (0053: a sub-agent's conversation). */
518
- export type ConversationKind = "chat" | "eval" | "subagent";
541
+ /**
542
+ * chat; eval (a hidden full-run replay conversation); subagent (0053: a sub-agent's conversation);
543
+ * external (0227: mirrored from a `conversation-target@1` target; no agent, never a run).
544
+ */
545
+ export type ConversationKind = "chat" | "eval" | "subagent" | "external";
519
546
  /** 0006_conversations.sql: append-only conversation event log. */
520
547
  export interface ConversationEventsTable {
521
548
  conversation_id: string;
@@ -2163,6 +2190,7 @@ export interface KernelDatabase {
2163
2190
  plugin_import_staging: PluginImportStagingTable;
2164
2191
  catalog_requests: CatalogRequestsTable;
2165
2192
  catalog_org_grants: CatalogOrgGrantsTable;
2193
+ catalog_dependency_edges: CatalogDependencyEdgesTable;
2166
2194
  catalog_item_scope_orgs: CatalogItemScopeOrgsTable;
2167
2195
  catalog_item_org_switches: CatalogItemOrgSwitchesTable;
2168
2196
  platform_skills: PlatformSkillsTable;
@@ -2343,6 +2371,15 @@ export interface CatalogItemsTable {
2343
2371
  /** 0084: the owner shares it with other orgs (the gate is `owner_org_id IS NULL OR shareable`). */
2344
2372
  shareable: Dflt<boolean>;
2345
2373
  }
2374
+ /** 0229: selected deployment versions and their resolved dependency targets. */
2375
+ export interface CatalogDependencyEdgesTable {
2376
+ item_id: string;
2377
+ item_version: string;
2378
+ dependency_name: string;
2379
+ template_id: string;
2380
+ dependency_item_id: string | null;
2381
+ dependency_version: string | null;
2382
+ }
2346
2383
  /** 0075: the orgs a `named` item is offered to. */
2347
2384
  export interface CatalogItemScopeOrgsTable {
2348
2385
  item_id: string;
@@ -2490,7 +2527,7 @@ export interface ComponentScopeOrgsTable {
2490
2527
  }
2491
2528
  /** 0081: an org's on / off switch of a component. */
2492
2529
  export interface ComponentOrgSwitchesTable {
2493
- kind: ComponentKind;
2530
+ kind: ComponentKind | "extension";
2494
2531
  component_id: string;
2495
2532
  org_id: string;
2496
2533
  enabled: boolean;
@@ -158,8 +158,8 @@ export declare class HostServiceAccess {
158
158
  private clearOwed;
159
159
  /**
160
160
  * Whether the extension is on in the org and on the deployment now, read in a short transaction
161
- * of its own that takes `FOR SHARE` on the org's `extension_org_configs` row and the
162
- * `extension_installs` row: every switch-off writes one of them before it marks placements owed,
161
+ * of its own that takes `FOR SHARE` on the org row and the
162
+ * `extension_installs` row: an org switch-off locks the org and a deployment switch-off updates the install before it marks placements owed,
163
163
  * so a switch-off still open waits this read out and is then seen (never a stale "enabled" while
164
164
  * its placement UPDATE missed a row inserted meanwhile). Called with the placement lock held,
165
165
  * never the other way round.
@@ -74,9 +74,10 @@ function resolveAt(at) {
74
74
  return { orgId: call.orgId, accountId: call.accountId, deviceId, call };
75
75
  }
76
76
  const person = personOf(at.person);
77
- if (person === null)
77
+ // a handle bound to one device (a conversation target's call) reaches that device only
78
+ if (person === null || (person.deviceId !== undefined && person.deviceId !== deviceId))
78
79
  throw new DeviceAccessError("invalid_request", HOST_SCOPE_TEXT);
79
- return { ...person, deviceId, call: null };
80
+ return { orgId: person.orgId, accountId: person.accountId, deviceId, call: null };
80
81
  }
81
82
  /**
82
83
  * Host device services (local agents spec, Part 1): a service of a platform-trusted extension that
@@ -485,18 +486,17 @@ let HostServiceAccess = class HostServiceAccess {
485
486
  }
486
487
  /**
487
488
  * Whether the extension is on in the org and on the deployment now, read in a short transaction
488
- * of its own that takes `FOR SHARE` on the org's `extension_org_configs` row and the
489
- * `extension_installs` row: every switch-off writes one of them before it marks placements owed,
489
+ * of its own that takes `FOR SHARE` on the org row and the
490
+ * `extension_installs` row: an org switch-off locks the org and a deployment switch-off updates the install before it marks placements owed,
490
491
  * so a switch-off still open waits this read out and is then seen (never a stale "enabled" while
491
492
  * its placement UPDATE missed a row inserted meanwhile). Called with the placement lock held,
492
493
  * never the other way round.
493
494
  */
494
495
  async enabledNow(orgId, extension) {
495
496
  await this.db
496
- .selectFrom("extension_org_configs")
497
- .select("enabled")
498
- .where("org_id", "=", orgId)
499
- .where("plugin", "=", extension)
497
+ .selectFrom("orgs")
498
+ .select("id")
499
+ .where("id", "=", orgId)
500
500
  .forShare()
501
501
  .executeTakeFirst();
502
502
  // deployment-wide: the extension's install record (none for a dev-loaded extension)
@@ -1,10 +1,19 @@
1
1
  import type { DevicePersonHandle } from "@capybara-hq/plugin-sdk";
2
- /** A new handle for `accountId` in `orgId`, live until {@link revokePersonHandle}. */
3
- export declare function issuePersonHandle(orgId: string, accountId: string): DevicePersonHandle;
4
- /** Ends the handle: later host service calls with it are refused. */
5
- export declare function revokePersonHandle(handle: DevicePersonHandle): void;
6
- /** The org and account of a live handle the kernel issued; null for anything else. */
7
- export declare function personOf(handle: unknown): {
2
+ /**
3
+ * What a live handle stands for; `deviceId` only for a handle bound to one device (null: bound to
4
+ * none, so no device is reached with it).
5
+ */
6
+ export interface PersonHandleScope {
8
7
  orgId: string;
9
8
  accountId: string;
10
- } | null;
9
+ deviceId?: string | null;
10
+ }
11
+ /**
12
+ * A new handle for `accountId` in `orgId`, live until {@link revokePersonHandle}; with `deviceId`,
13
+ * valid for that device only (null: for no device).
14
+ */
15
+ export declare function issuePersonHandle(orgId: string, accountId: string, deviceId?: string | null): DevicePersonHandle;
16
+ /** Ends the handle: later host service calls with it are refused. */
17
+ export declare function revokePersonHandle(handle: DevicePersonHandle): void;
18
+ /** The org, account (and bound device) of a live handle the kernel issued; null for anything else. */
19
+ export declare function personOf(handle: unknown): PersonHandleScope | null;
@@ -1,23 +1,28 @@
1
1
  /**
2
2
  * Kernel-issued person handles (`DevicePersonHandle`, local agents spec, call 6): the kernel puts
3
- * one on the args of each org `ctx.ui.action` / `ctx.ui.query` handler call, for the person the
4
- * call runs for, and revokes it when the handler settles. A host service call with a person scope
5
- * is admitted only for a live handle, and the org and account come from it, never from the
6
- * extension. Per call and per process like a tool call's scope (`tools/call-scope.ts`): the handle
7
- * never leaves the process that issued it, and nothing is shared across replicas.
3
+ * one on the args of each org `ctx.ui.action` / `ctx.ui.query` handler call and of each
4
+ * `conversation-target@1` call, for the person the call runs for, and revokes it when the call
5
+ * settles. A host service call with a person scope is admitted only for a live handle, and the org
6
+ * and account come from it, never from the extension; a handle bound to a device (a call about an
7
+ * external conversation: its device) is admitted for that device only. Per call and per process
8
+ * like a tool call's scope (`tools/call-scope.ts`): the handle never leaves the process that issued
9
+ * it, and nothing is shared across replicas.
8
10
  */
9
11
  const persons = new WeakMap();
10
- /** A new handle for `accountId` in `orgId`, live until {@link revokePersonHandle}. */
11
- export function issuePersonHandle(orgId, accountId) {
12
+ /**
13
+ * A new handle for `accountId` in `orgId`, live until {@link revokePersonHandle}; with `deviceId`,
14
+ * valid for that device only (null: for no device).
15
+ */
16
+ export function issuePersonHandle(orgId, accountId, deviceId) {
12
17
  const handle = Object.freeze({ kind: "device-person" });
13
- persons.set(handle, { orgId, accountId });
18
+ persons.set(handle, { orgId, accountId, ...(deviceId !== undefined && { deviceId }) });
14
19
  return handle;
15
20
  }
16
21
  /** Ends the handle: later host service calls with it are refused. */
17
22
  export function revokePersonHandle(handle) {
18
23
  persons.delete(handle);
19
24
  }
20
- /** The org and account of a live handle the kernel issued; null for anything else. */
25
+ /** The org, account (and bound device) of a live handle the kernel issued; null for anything else. */
21
26
  export function personOf(handle) {
22
27
  if (typeof handle !== "object" || handle === null)
23
28
  return null;
@@ -113,6 +113,11 @@ export declare class ConversationDevices {
113
113
  * has no default device and none can be made (no default provider on in the org).
114
114
  */
115
115
  forNew(orgId: string, accountId: string, pick?: NewConversationDevice): Promise<string | null>;
116
+ /**
117
+ * The person's default device in the org when they already have one, else null; unlike
118
+ * {@link forNew}, never makes one (no access check: a caller binding it checks it as a choice).
119
+ */
120
+ existingDefault(orgId: string, accountId: string): Promise<string | null>;
116
121
  /**
117
122
  * The device a sandbox tool call of the conversation tree rooted at `rootConversationId`
118
123
  * enters, checked for its owner now. A conversation still unbound (created before
@@ -149,6 +149,22 @@ let ConversationDevices = class ConversationDevices {
149
149
  return null;
150
150
  return this.defaultFor(orgId, accountId, pick.im === true);
151
151
  }
152
+ /**
153
+ * The person's default device in the org when they already have one, else null; unlike
154
+ * {@link forNew}, never makes one (no access check: a caller binding it checks it as a choice).
155
+ */
156
+ async existingDefault(orgId, accountId) {
157
+ const row = await this.db
158
+ .selectFrom("devices")
159
+ .select("id")
160
+ .where("org_id", "=", orgId)
161
+ .where("owner_kind", "=", "personal")
162
+ .where("owner_account_id", "=", accountId)
163
+ .where("is_default", "=", true)
164
+ .where("deleted_at", "is", null)
165
+ .executeTakeFirst();
166
+ return row?.id ?? null;
167
+ }
152
168
  /**
153
169
  * The device a sandbox tool call of the conversation tree rooted at `rootConversationId`
154
170
  * enters, checked for its owner now. A conversation still unbound (created before
@@ -1,5 +1,5 @@
1
1
  import { sql } from "kysely";
2
- // PostgreSQL 17 after 0225. Re-review the ownership map before updating this fingerprint.
2
+ // PostgreSQL 17 after 0229. Re-review the ownership map before updating this fingerprint.
3
3
  // It includes relation/column identities and ordered PK, UNIQUE and inbound/outbound FK
4
4
  // columns and actions, including FKs from outside the kernel schema.
5
5
  export async function orgCatalogDrift(db) {
@@ -30,5 +30,5 @@ WITH catalog AS (
30
30
  )
31
31
  SELECT md5(string_agg(entry, chr(10) ORDER BY entry COLLATE "C")) AS fingerprint FROM catalog
32
32
  `.execute(db);
33
- return rows[0]?.fingerprint === "3693e0b7004dae3c5721abfadc80e1e8" ? undefined : "schema-drift";
33
+ return rows[0]?.fingerprint === "d312e85ccc7cc10086f6fa60f6e9e851" ? undefined : "schema-drift";
34
34
  }
@@ -95,6 +95,7 @@ export const rowRules = [
95
95
  rule("platform_connector_templates", "id", ownedRetired),
96
96
  rule("platform_skills", "id", ownedRetired),
97
97
  rule("platform_themes", "id", ownedRetired),
98
+ rule("catalog_dependency_edges", "item_id, dependency_name, template_id", ownsCatalogVersion("catalog_items", "item_id")),
98
99
  rule("catalog_item_versions", "item_id, version", ownsCatalogVersion("catalog_items", "item_id")),
99
100
  rule("catalog_items", "id", ownedRetired),
100
101
  ];
@@ -150,6 +151,10 @@ export const childReferences = {
150
151
  ],
151
152
  platform_themes: [["platform_theme_versions", "child.theme_id = parent.id"]],
152
153
  catalog_items: [
154
+ [
155
+ "catalog_dependency_edges",
156
+ "child.item_id = parent.id OR child.dependency_item_id = parent.id",
157
+ ],
153
158
  ["catalog_item_org_switches", "child.item_id = parent.id"],
154
159
  ["catalog_item_scope_orgs", "child.item_id = parent.id"],
155
160
  ["catalog_item_versions", "child.item_id = parent.id"],
package/dist/errors.d.ts CHANGED
@@ -402,6 +402,12 @@ export declare const ERROR_CODES: {
402
402
  readonly "conversation.queue_item_not_found": 404;
403
403
  /** A sub-agent's conversation takes no messages from people (sub-agents spec §6.1). */
404
404
  readonly "conversation.read_only": 409;
405
+ /**
406
+ * The conversation is with a local agent (an external conversation, local agents spec Part 2): it
407
+ * does not run here, so Capybara's run controls (queue, compaction, model switch, extension UI
408
+ * actions) do not apply to it.
409
+ */
410
+ readonly "conversation.external_read_only": 409;
405
411
  /**
406
412
  * A referenced project file is not there to reference (composer menus §5): the conversation has
407
413
  * no device project, the path leaves it, or the device reports no file there;
@@ -422,6 +428,34 @@ export declare const ERROR_CODES: {
422
428
  * "not_offered" }]`.
423
429
  */
424
430
  readonly "conversation.connector_unavailable": 422;
431
+ /**
432
+ * The conversation target cannot be used now (local agents spec Part 2): its extension does not
433
+ * serve `conversation-target@1` in the org, it does not list the target (or the chosen device),
434
+ * or it did not answer.
435
+ */
436
+ readonly "conversation.target_unavailable": 409;
437
+ /**
438
+ * A first message mixes the fields of the two kinds: `permission` (or a message model, effort,
439
+ * skill, tokens or files) with a `target`, or a target's `model` / `effort` / `mode` with an
440
+ * `agentId`.
441
+ */
442
+ readonly "conversation.target_fields": 400;
443
+ /**
444
+ * No device for an external conversation: the chosen one is not among the target's devices, or
445
+ * without a choice neither the default device nor any ready device the target lists fits.
446
+ */
447
+ readonly "conversation.target_device": 409;
448
+ /** The local agent never started this external conversation (its `start` was dropped). */
449
+ readonly "conversation.external_ended": 409;
450
+ /**
451
+ * A message to an external conversation carries more than its text (tokens, files, a model or
452
+ * effort switch, a skill, a retry); `interrupt` is for external conversations only.
453
+ */
454
+ readonly "conversation.external_fields": 400;
455
+ /** The external conversation has no request of that id (`external_request`). */
456
+ readonly "conversation.request_not_found": 404;
457
+ /** The request was already answered; the first answer stands. */
458
+ readonly "conversation.request_answered": 409;
425
459
  readonly "run.not_active": 409;
426
460
  /** A run that is not one of the conversation's (the live draft route, spec 2026-11-07 §3.2). */
427
461
  readonly "run.not_found": 404;
package/dist/errors.js CHANGED
@@ -402,6 +402,12 @@ export const ERROR_CODES = {
402
402
  "conversation.queue_item_not_found": 404,
403
403
  /** A sub-agent's conversation takes no messages from people (sub-agents spec §6.1). */
404
404
  "conversation.read_only": 409,
405
+ /**
406
+ * The conversation is with a local agent (an external conversation, local agents spec Part 2): it
407
+ * does not run here, so Capybara's run controls (queue, compaction, model switch, extension UI
408
+ * actions) do not apply to it.
409
+ */
410
+ "conversation.external_read_only": 409,
405
411
  /**
406
412
  * A referenced project file is not there to reference (composer menus §5): the conversation has
407
413
  * no device project, the path leaves it, or the device reports no file there;
@@ -422,6 +428,34 @@ export const ERROR_CODES = {
422
428
  * "not_offered" }]`.
423
429
  */
424
430
  "conversation.connector_unavailable": 422,
431
+ /**
432
+ * The conversation target cannot be used now (local agents spec Part 2): its extension does not
433
+ * serve `conversation-target@1` in the org, it does not list the target (or the chosen device),
434
+ * or it did not answer.
435
+ */
436
+ "conversation.target_unavailable": 409,
437
+ /**
438
+ * A first message mixes the fields of the two kinds: `permission` (or a message model, effort,
439
+ * skill, tokens or files) with a `target`, or a target's `model` / `effort` / `mode` with an
440
+ * `agentId`.
441
+ */
442
+ "conversation.target_fields": 400,
443
+ /**
444
+ * No device for an external conversation: the chosen one is not among the target's devices, or
445
+ * without a choice neither the default device nor any ready device the target lists fits.
446
+ */
447
+ "conversation.target_device": 409,
448
+ /** The local agent never started this external conversation (its `start` was dropped). */
449
+ "conversation.external_ended": 409,
450
+ /**
451
+ * A message to an external conversation carries more than its text (tokens, files, a model or
452
+ * effort switch, a skill, a retry); `interrupt` is for external conversations only.
453
+ */
454
+ "conversation.external_fields": 400,
455
+ /** The external conversation has no request of that id (`external_request`). */
456
+ "conversation.request_not_found": 404,
457
+ /** The request was already answered; the first answer stands. */
458
+ "conversation.request_answered": 409,
425
459
  "run.not_active": 409,
426
460
  /** A run that is not one of the conversation's (the live draft route, spec 2026-11-07 §3.2). */
427
461
  "run.not_found": 404,
@@ -1,4 +1,5 @@
1
1
  import { normalizeStoredDefinition } from "../agents/definition.js";
2
+ import { agentIdOf } from "../conversations/conversation-agent.js";
2
3
  import { ApiError } from "../errors.js";
3
4
  import { childRunDefinition } from "../runtime/subagents/definition.js";
4
5
  import { storedToolSpec } from "../tools/specs.js";
@@ -40,7 +41,8 @@ async function conversationRun(db, orgId, conversationId, runId) {
40
41
  if (!conv || !run) {
41
42
  throw new ApiError("conversation.not_found", "Conversation or run not found");
42
43
  }
43
- return { conv, agentVersion: run.agent_version };
44
+ // the run makes it an agent conversation (an external one has no runs)
45
+ return { conv: { ...conv, agent_id: agentIdOf(conv) }, agentVersion: run.agent_version };
44
46
  }
45
47
  /**
46
48
  * The snapshot of `events`: the agent versions they reference (each `run_started` and the run's
@@ -1,5 +1,5 @@
1
+ import { type AgentConversationKind } from "../conversations/conversation-agent.js";
1
2
  import { EventLog } from "../conversations/event-log.js";
2
- import type { ConversationKind } from "../db/kernel-schema.js";
3
3
  import type { PersonRef } from "../identity/account-avatar.js";
4
4
  import { KernelDb } from "../infra/database/kernel-db.js";
5
5
  import { type TrajectoryRun } from "./trajectory.js";
@@ -23,7 +23,7 @@ export interface TrajectoryDetail {
23
23
  agentId: string;
24
24
  accountId: string;
25
25
  title: string | null;
26
- kind: ConversationKind;
26
+ kind: AgentConversationKind;
27
27
  /**
28
28
  * A sub-agent's conversation (phase C, spec §15.4): the parent conversation and the `agent`
29
29
  * call that first dispatched it, for the link back. Visible exactly when the child is (the