copilotz 1.0.0-rc.21 → 1.0.0-rc.23

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 (211) hide show
  1. package/CHANGELOG.md +320 -1
  2. package/README.md +217 -87
  3. package/hosts/orgs/mod.d.ts +33 -13
  4. package/hosts/orgs/mod.js +183 -84
  5. package/hosts/web/address.d.ts +11 -0
  6. package/hosts/web/address.js +46 -0
  7. package/hosts/web/assets.js +11 -5
  8. package/hosts/web/entrances.d.ts +250 -0
  9. package/hosts/web/entrances.js +362 -0
  10. package/hosts/web/host.d.ts +15 -3
  11. package/hosts/web/host.js +31 -18
  12. package/hosts/web/mod.d.ts +4 -2
  13. package/hosts/web/mod.js +6 -0
  14. package/hosts/web/node/bundle.d.ts +5 -0
  15. package/hosts/web/node/bundle.js +1 -1
  16. package/hosts/web/node/mod.d.ts +6 -4
  17. package/hosts/web/node/mod.js +6 -4
  18. package/hosts/web/node/serve.d.ts +20 -23
  19. package/hosts/web/node/serve.js +10 -21
  20. package/hosts/web/routes.d.ts +23 -17
  21. package/hosts/web/routes.js +56 -32
  22. package/hosts/web/server.d.ts +89 -44
  23. package/hosts/web/server.js +208 -108
  24. package/hosts/web/signin.d.ts +7 -10
  25. package/hosts/web/signin.js +6 -3
  26. package/hosts/web/surface.d.ts +20 -0
  27. package/hosts/web/surface.js +42 -0
  28. package/hosts/web/theme/css.d.ts +2 -1
  29. package/hosts/web/theme/css.js +9 -2
  30. package/hosts/web/tickets.d.ts +14 -1
  31. package/hosts/web/tickets.js +30 -7
  32. package/hosts/web/widget.d.ts +31 -0
  33. package/hosts/web/widget.js +429 -0
  34. package/hosts/web/widgets.d.ts +43 -0
  35. package/hosts/web/widgets.js +117 -0
  36. package/package.json +5 -1
  37. package/plugins/access/contacts/actions/forget.js +3 -1
  38. package/plugins/access/contacts/actions/start.js +18 -8
  39. package/plugins/access/contacts/collections/contact.d.ts +2 -2
  40. package/plugins/access/contacts/collections/contact.js +1 -1
  41. package/plugins/access/contacts/contract.d.ts +5 -5
  42. package/plugins/access/contacts/contract.js +5 -5
  43. package/plugins/access/contacts/mod.d.ts +4 -0
  44. package/plugins/access/contacts/mod.js +4 -0
  45. package/plugins/access/contacts/views/contact.js +1 -0
  46. package/plugins/access/dev/actions/sign-in.d.ts +1 -0
  47. package/plugins/access/dev/actions/sign-in.js +38 -0
  48. package/plugins/access/dev/mod.d.ts +17 -0
  49. package/plugins/access/dev/mod.js +31 -0
  50. package/plugins/access/dev/shared/nearby.d.ts +2 -0
  51. package/plugins/access/dev/shared/nearby.js +27 -0
  52. package/plugins/access/directory/actions/invite.js +2 -2
  53. package/plugins/access/directory/actions/org.js +11 -8
  54. package/plugins/access/directory/collections/credit-code.d.ts +1 -1
  55. package/plugins/access/google/actions/sign-in.js +2 -0
  56. package/plugins/access/google/mod.d.ts +4 -2
  57. package/plugins/access/google/mod.js +15 -2
  58. package/plugins/access/identity/actions/session.js +5 -3
  59. package/plugins/access/identity/mod.d.ts +4 -2
  60. package/plugins/access/identity/mod.js +17 -2
  61. package/plugins/access/mod.d.ts +7 -0
  62. package/plugins/access/mod.js +1 -0
  63. package/plugins/access/spaces/shared/declared.d.ts +1 -1
  64. package/plugins/access/spaces/shared/rules.d.ts +16 -5
  65. package/plugins/access/spaces/shared/rules.js +74 -6
  66. package/plugins/agents/chat/actions/send.d.ts +21 -0
  67. package/plugins/agents/chat/actions/send.js +72 -13
  68. package/plugins/agents/chat/collections/file.d.ts +15 -0
  69. package/plugins/agents/chat/collections/file.js +12 -3
  70. package/plugins/agents/chat/collections/live.d.ts +11 -0
  71. package/plugins/agents/chat/collections/live.js +11 -1
  72. package/plugins/agents/chat/collections/message.d.ts +1 -1
  73. package/plugins/agents/chat/collections/wait.d.ts +1 -1
  74. package/plugins/agents/chat/contract.d.ts +3 -2
  75. package/plugins/agents/chat/contract.js +1 -0
  76. package/plugins/agents/chat/mod.d.ts +9 -3
  77. package/plugins/agents/chat/mod.js +28 -7
  78. package/plugins/agents/chat/processors/failed.js +8 -3
  79. package/plugins/agents/chat/processors/turn.js +6 -3
  80. package/plugins/agents/chat/shared/agents.js +11 -2
  81. package/plugins/agents/chat/shared/live.d.ts +4 -2
  82. package/plugins/agents/chat/shared/live.js +2 -1
  83. package/plugins/agents/chat/shared/types.d.ts +8 -0
  84. package/plugins/agents/history/collections/outline.d.ts +2 -2
  85. package/plugins/agents/openapi/shared/files.js +2 -0
  86. package/plugins/agents/routines/collections/routine.d.ts +1 -1
  87. package/plugins/billing/credits/collections/plan.d.ts +2 -2
  88. package/plugins/billing/credits/collections/room.d.ts +1 -1
  89. package/plugins/billing/usage/collections/rollup.d.ts +2 -2
  90. package/plugins/billing/usage/collections/usage.d.ts +2 -2
  91. package/plugins/compute/cloud/actions/pool.js +2 -0
  92. package/plugins/compute/cloud/actions/remote.js +4 -2
  93. package/plugins/compute/cloud/actions/vms.js +2 -0
  94. package/plugins/compute/cloud/collections/pool-vm.d.ts +1 -1
  95. package/plugins/compute/computers/actions/admit.js +2 -0
  96. package/plugins/compute/computers/actions/code.js +2 -0
  97. package/plugins/compute/computers/actions/enrollment.js +2 -0
  98. package/plugins/compute/computers/actions/work.js +3 -0
  99. package/plugins/compute/computers/program/version.d.ts +1 -1
  100. package/plugins/compute/computers/program/version.js +1 -1
  101. package/plugins/compute/computers/resources/downloads.js +1 -1
  102. package/plugins/compute/computers/resources/previews.js +5 -2
  103. package/plugins/interfaces/maintenance/mod.d.ts +3 -1
  104. package/plugins/interfaces/maintenance/mod.js +3 -1
  105. package/plugins/interfaces/mcp/actions/call.d.ts +4 -0
  106. package/plugins/interfaces/mcp/actions/call.js +120 -18
  107. package/plugins/interfaces/mcp/actions/conversation.d.ts +1 -0
  108. package/plugins/interfaces/mcp/actions/conversation.js +49 -0
  109. package/plugins/interfaces/mcp/actions/prune.js +5 -3
  110. package/plugins/interfaces/mcp/collections/opening.d.ts +24 -0
  111. package/plugins/interfaces/mcp/collections/opening.js +28 -0
  112. package/plugins/interfaces/mcp/mod.d.ts +27 -3
  113. package/plugins/interfaces/mcp/mod.js +42 -7
  114. package/plugins/interfaces/mcp/resources/server.d.ts +13 -4
  115. package/plugins/interfaces/mcp/resources/server.js +163 -21
  116. package/plugins/interfaces/mcp/resources/tools.d.ts +88 -12
  117. package/plugins/interfaces/mcp/resources/tools.js +113 -19
  118. package/plugins/interfaces/mcp/resources/widgets.d.ts +161 -0
  119. package/plugins/interfaces/mcp/resources/widgets.js +246 -0
  120. package/plugins/interfaces/mcp/shared/keys.d.ts +25 -19
  121. package/plugins/interfaces/mcp/shared/keys.js +55 -9
  122. package/plugins/interfaces/mcp/shared/settings.d.ts +70 -37
  123. package/plugins/interfaces/mcp/shared/settings.js +64 -39
  124. package/plugins/interfaces/mcp/views/offered.d.ts +1 -1
  125. package/plugins/interfaces/mod.d.ts +2 -1
  126. package/plugins/interfaces/mod.js +2 -1
  127. package/plugins/interfaces/voice/collections/blocked.d.ts +2 -2
  128. package/plugins/interfaces/voice/collections/call.d.ts +3 -3
  129. package/plugins/interfaces/voice/contract.d.ts +1 -1
  130. package/plugins/interfaces/voice/mod.d.ts +3 -3
  131. package/plugins/interfaces/voice/mod.js +3 -3
  132. package/plugins/interfaces/voice/resources/relay.d.ts +5 -3
  133. package/plugins/interfaces/voice/resources/relay.js +17 -6
  134. package/plugins/interfaces/voice/resources/tools.js +2 -0
  135. package/plugins/interfaces/voice/shared/settings.d.ts +5 -0
  136. package/plugins/interfaces/voice/views/call.d.ts +18 -11
  137. package/plugins/interfaces/voice/views/call.js +15 -3
  138. package/plugins/interfaces/voice/views/message.d.ts +1 -1
  139. package/plugins/interfaces/web/actions/arrive.d.ts +1 -0
  140. package/plugins/interfaces/web/actions/arrive.js +120 -0
  141. package/plugins/interfaces/web/actions/prune.d.ts +1 -0
  142. package/plugins/interfaces/web/actions/prune.js +51 -0
  143. package/plugins/interfaces/web/actions/sending.d.ts +1 -0
  144. package/plugins/interfaces/web/actions/sending.js +222 -0
  145. package/plugins/interfaces/web/actions/spent.d.ts +2 -0
  146. package/plugins/interfaces/web/actions/spent.js +59 -0
  147. package/plugins/interfaces/web/collections/rate.d.ts +122 -0
  148. package/plugins/interfaces/web/collections/rate.js +37 -0
  149. package/plugins/interfaces/web/collections/visitor.d.ts +110 -0
  150. package/plugins/interfaces/web/collections/visitor.js +64 -0
  151. package/plugins/interfaces/web/contract.d.ts +4 -0
  152. package/plugins/interfaces/web/contract.js +1 -0
  153. package/plugins/interfaces/web/lines.d.ts +36 -0
  154. package/plugins/interfaces/web/lines.js +78 -0
  155. package/plugins/interfaces/web/processors/forget.d.ts +10 -0
  156. package/plugins/interfaces/web/processors/forget.js +14 -0
  157. package/plugins/interfaces/web/resources/entrance.d.ts +47 -0
  158. package/plugins/interfaces/web/resources/entrance.js +83 -0
  159. package/plugins/interfaces/web/resources/spend.d.ts +2 -0
  160. package/plugins/interfaces/web/resources/spend.js +62 -0
  161. package/plugins/interfaces/web/shared/budget.d.ts +65 -0
  162. package/plugins/interfaces/web/shared/budget.js +134 -0
  163. package/plugins/interfaces/web/shared/lines.d.ts +158 -0
  164. package/plugins/interfaces/web/shared/lines.js +156 -0
  165. package/plugins/interfaces/web/shared/secret.d.ts +33 -0
  166. package/plugins/interfaces/web/shared/secret.js +86 -0
  167. package/plugins/interfaces/whatsapp/collections/phone.d.ts +1 -1
  168. package/plugins/interfaces/whatsapp/mod.d.ts +1 -1
  169. package/plugins/interfaces/whatsapp/mod.js +1 -1
  170. package/plugins/interfaces/whatsapp/views/message.d.ts +1 -1
  171. package/plugins/models/contract.d.ts +1 -1
  172. package/plugins/models/shared/generate.js +13 -4
  173. package/plugins/models/shared/transcribe.js +7 -1
  174. package/plugins/models/shared/use.d.ts +16 -2
  175. package/plugins/models/shared/use.js +20 -0
  176. package/runtime/browsers.d.ts +153 -17
  177. package/runtime/browsers.js +336 -30
  178. package/runtime/cache.js +25 -5
  179. package/runtime/caller.d.ts +117 -0
  180. package/runtime/caller.js +215 -0
  181. package/runtime/client.d.ts +47 -3
  182. package/runtime/client.js +132 -42
  183. package/runtime/define.d.ts +30 -9
  184. package/runtime/executor.js +7 -6
  185. package/runtime/expiring.d.ts +23 -0
  186. package/runtime/expiring.js +64 -0
  187. package/runtime/links.d.ts +27 -4
  188. package/runtime/links.js +49 -11
  189. package/runtime/memory.js +102 -15
  190. package/runtime/mod.d.ts +3 -1
  191. package/runtime/mod.js +3 -1
  192. package/runtime/mongo.d.ts +12 -0
  193. package/runtime/mongo.js +44 -0
  194. package/runtime/observe.js +82 -9
  195. package/runtime/ops.d.ts +9 -1
  196. package/runtime/owner.d.ts +14 -1
  197. package/runtime/owner.js +78 -23
  198. package/runtime/reads.js +2 -2
  199. package/runtime/registry.js +9 -3
  200. package/runtime/runtime.d.ts +7 -0
  201. package/runtime/runtime.js +1 -0
  202. package/runtime/timers.js +2 -0
  203. package/runtime/views.d.ts +1 -1
  204. package/runtime/views.js +2 -2
  205. package/runtime/wire.d.ts +1 -1
  206. package/stores/sql/postgres/maintenance.d.ts +8 -1
  207. package/stores/sql/postgres/maintenance.js +76 -9
  208. package/stores/sql/postgres/mod.d.ts +12 -4
  209. package/stores/sql/postgres/mod.js +21 -8
  210. package/plugins/interfaces/mcp/shared/caller.d.ts +0 -45
  211. package/plugins/interfaces/mcp/shared/caller.js +0 -99
@@ -5,6 +5,7 @@ import type { Plugins, RenderOptions, Resources, WorkerJob } from "./define.js";
5
5
  import { type Blobs } from "./blobs.js";
6
6
  import { type Maintenance } from "./maintain.js";
7
7
  import { type Observation, type ObserveOptions } from "./observe.js";
8
+ import { type BrowserLimits } from "./browsers.js";
8
9
  import type { Actor, Event, Store } from "./store.js";
9
10
  export type Transport = HypervisorOptions["transports"][number];
10
11
  export type Feed = {
@@ -40,9 +41,11 @@ export type Description = {
40
41
  collections: Record<string, {
41
42
  schema: unknown;
42
43
  }>;
44
+ /** `record: false`: unrecorded, so nothing of its calls is in the log (a sign-in's). */
43
45
  actions: Record<string, {
44
46
  description?: string;
45
47
  input?: unknown;
48
+ record?: false;
46
49
  }>;
47
50
  /** The media each kind has a View for. */
48
51
  views: Record<string, string[]>;
@@ -70,7 +73,7 @@ export type Owner = {
70
73
  * Renders `item` through the View for `kind` in a medium (`"cli"`, `"llm"`…),
71
74
  * or the medium's default View (kind `"*"`); undefined when there's neither.
72
75
  */
73
- render<R = unknown>(renderer: string, kind: string, item: unknown, options?: RenderOptions): Promise<R | undefined>;
76
+ render<R = unknown>(medium: string, kind: string, item: unknown, options?: RenderOptions): Promise<R | undefined>;
74
77
  /** What the plugins declare: collections' schemas, Actions, Views by kind, resources. */
75
78
  describe(): Description;
76
79
  /**
@@ -98,6 +101,14 @@ export type Owner = {
98
101
  processor: string;
99
102
  key: string;
100
103
  }): Promise<boolean>;
104
+ /**
105
+ * Ends an actor's browsers, when who they were ends (signed out, erased):
106
+ * their resume credentials go and each connection closes (one let in but
107
+ * not yet ready is never ready); one comes back only with a new ticket,
108
+ * which only a cookie that's still someone gets. How many connections it
109
+ * ended.
110
+ */
111
+ disconnect(actor: Actor): number;
101
112
  /** Resolves when no Processor delivery or timer occurrence is pending, and observed Views caught up. */
102
113
  idle(): Promise<void>;
103
114
  /**
@@ -159,4 +170,6 @@ export declare function createOwner(options: {
159
170
  * ticket the host issued): undefined refuses it. Without it, no browsers.
160
171
  */
161
172
  admit?(secret: string): Actor | undefined | Promise<Actor | undefined>;
173
+ /** How much browsers may hold (see browsers.ts): contacts' caps, a pool shared with others. */
174
+ browsers?: BrowserLimits;
162
175
  }): Promise<Owner>;
package/runtime/owner.js CHANGED
@@ -15,7 +15,8 @@ import { createCache } from "./cache.js";
15
15
  import { createMaintenance } from "./maintain.js";
16
16
  import { createObservers } from "./observe.js";
17
17
  import { reading, startup } from "./reads.js";
18
- import { BROWSER, createBrowsers, READ_VIEW, readView, SESSION, WIRE } from "./browsers.js";
18
+ import { BROWSER, createBrowsers, limitsProblem, READ_VIEW, readView, SESSION, WIRE, } from "./browsers.js";
19
+ import { expiring } from "./expiring.js";
19
20
  import { memoryStore } from "./memory.js";
20
21
  import { isRead, perform, read, } from "./ops.js";
21
22
  import { check, checkPath, matches } from "./mongo.js";
@@ -35,6 +36,8 @@ const DAY = 86_400_000;
35
36
  const DELIVERY_ATTEMPTS = 3;
36
37
  /** How long a closing owner waits for running deliveries to finish, by default (ms). */
37
38
  const DRAIN_MS = 5_000;
39
+ /** How long a worker welcomed has to say it's ready (ms) before its connection closes. */
40
+ const READY_MS = 10_000;
38
41
  const aborted = Object.freeze({ aborted: true });
39
42
  const forbidden = (message) => Object.assign(new Error(message), { code: "forbidden" });
40
43
  /** Strings compared in time that doesn't depend on where they differ (keys). */
@@ -47,6 +50,9 @@ function same(a, b) {
47
50
  }
48
51
  export async function createOwner(options) {
49
52
  const { store } = options;
53
+ const limited = limitsProblem(options.browsers);
54
+ if (limited)
55
+ throw invalid(limited);
50
56
  const registry = merge(options.plugins);
51
57
  // The View a browser's watched read is: one read, made as its person.
52
58
  registry.views[READ_VIEW] = { [WIRE]: readView };
@@ -510,13 +516,19 @@ export async function createOwner(options) {
510
516
  }
511
517
  return rules.length > 1 ? { $and: rules } : rules[0];
512
518
  };
513
- /** Whether any rule speaks for a collection's reads by this actor: a browser reads nothing else directly. */
514
- const ruled = async (actor, collection) => {
519
+ /**
520
+ * Whether any rule speaks for a collection's reads by this actor: a browser
521
+ * reads nothing else directly. Asked as its own read (`direct`), with its
522
+ * filter: a rule may refuse one it won't watch (thrown).
523
+ */
524
+ const ruled = async (actor, collection, filter) => {
525
+ let found = false;
515
526
  for (const guard of registry.guards) {
516
- if (await guard(actor, { collection, access: "read" }, ruleReads) !== undefined)
517
- return true;
527
+ const said = await guard(actor, { collection, access: "read", ...(filter ? { filter } : {}), direct: true }, ruleReads);
528
+ if (said !== undefined)
529
+ found = true;
518
530
  }
519
- return false;
531
+ return found;
520
532
  };
521
533
  /** Whether an actor may run a query from outside: one that lets them, as Actions do. */
522
534
  const askable = (actor, collection, query, params) => {
@@ -549,7 +561,19 @@ export async function createOwner(options) {
549
561
  }
550
562
  case "aggregate": {
551
563
  const found = await rule(actor, "read", op.collection, undefined, reads);
552
- return found ? { ...op, pipeline: [{ $match: found }, ...op.pipeline] } : op;
564
+ if (!found)
565
+ return op;
566
+ // One `$match`, first: a pipeline's own joins the rule's.
567
+ const [first, ...rest] = op.pipeline;
568
+ const own = first && Object.keys(first).length === 1 && "$match" in first
569
+ ? first.$match
570
+ : undefined;
571
+ return {
572
+ ...op,
573
+ pipeline: own
574
+ ? [{ $match: both(own, found) }, ...rest]
575
+ : [{ $match: found }, ...op.pipeline],
576
+ };
553
577
  }
554
578
  case "nearest": {
555
579
  const found = await rule(actor, "read", op.collection, op.options.filter, reads);
@@ -724,6 +748,12 @@ export async function createOwner(options) {
724
748
  throw invalid("A cancel goes on its own.");
725
749
  return cancel(first);
726
750
  }
751
+ if (first?.op === "disconnect") {
752
+ if ("tx" in request || request.ops.length !== 1) {
753
+ throw invalid("A disconnect goes on its own.");
754
+ }
755
+ return disconnect(first.actor);
756
+ }
727
757
  const lowered = request.ops.map((op) => timers.lower(op, request.actor));
728
758
  lowered.forEach(known);
729
759
  const guarded = await guard(lowered, request.actor);
@@ -757,7 +787,8 @@ export async function createOwner(options) {
757
787
  };
758
788
  // Workers of plugins' kinds (computers…): admitted by their kind's Action, then found by id.
759
789
  const connected = workerPresence();
760
- const resumed = new Map();
790
+ /** What a worker shows to reconnect: a secret of its own, for a day. */
791
+ const resumed = expiring(DAY);
761
792
  /** Who a worker of a plugin's kind is, from `<kind>:<secret>`; undefined: not one. */
762
793
  const workerOf = async (credential) => {
763
794
  const capability = String(credential.capability);
@@ -767,7 +798,7 @@ export async function createOwner(options) {
767
798
  return undefined;
768
799
  if (credential.kind === "resume") {
769
800
  const kept = resumed.get(capability);
770
- if (!kept || kept.until < Date.now())
801
+ if (!kept)
771
802
  throw invalid(`Unknown ${kind}.`);
772
803
  return kept;
773
804
  }
@@ -786,11 +817,8 @@ export async function createOwner(options) {
786
817
  const worker = await workerOf(credential);
787
818
  if (worker) {
788
819
  const now = Date.now();
789
- for (const [kept, { until }] of resumed)
790
- if (until < now)
791
- resumed.delete(kept);
792
820
  const again = `${worker.kind}:${crypto.randomUUID()}${crypto.randomUUID()}`;
793
- resumed.set(again, { ...worker, until: now + DAY });
821
+ resumed.set(again, { kind: worker.kind, id: worker.id });
794
822
  return {
795
823
  definition: {
796
824
  workerId: identity.workerId,
@@ -809,26 +837,30 @@ export async function createOwner(options) {
809
837
  };
810
838
  }
811
839
  if (!options.key || !same(secret, options.key)) {
840
+ // Who's ended from here on (signed out, erased) isn't let in by what it showed before.
841
+ const since = browsers.ends();
812
842
  const actor = credential.kind === "resume"
813
- ? browsers.resumed(secret)
843
+ ? browsers.resumed(secret, identity.workerId)
814
844
  : options.admit
815
845
  ? await options.admit(secret)
816
846
  : undefined;
817
847
  if (!actor)
818
848
  throw invalid("Unknown executor key.");
819
- // A browser: its person rides on the connection, and it takes no work but its session.
849
+ browsers.room(actor, identity.workerId, since);
850
+ // A browser: its person rides on the connection, and it takes no work but its session. Its
851
+ // place is taken as it's let in (`onHandshake`).
820
852
  return {
821
853
  definition: {
822
854
  workerId: identity.workerId,
823
855
  providerId: BROWSER,
824
856
  workloads: [SESSION],
825
857
  capacity: 1,
826
- providerConfig: browsers.config(actor),
858
+ providerConfig: browsers.config(actor, since),
827
859
  labels: {},
828
860
  },
829
861
  sessionGeneration: ++generation,
830
862
  authenticatedWith: credential.kind,
831
- resume: browsers.resume(actor),
863
+ resume: browsers.resume(actor, identity.workerId),
832
864
  };
833
865
  }
834
866
  return {
@@ -895,7 +927,13 @@ export async function createOwner(options) {
895
927
  options.transports.some((t) => t.type !== "in-process")
896
928
  ? { admit }
897
929
  : {}),
898
- config: { maxQueuedDispatches: 100_000, dispatchQueueTimeoutMs: 30_000 },
930
+ // A worker says it's ready as soon as it's welcomed (none starts anything first): one that
931
+ // doesn't soon is closed, so a connection let in can't hold its place without being used.
932
+ config: {
933
+ maxQueuedDispatches: 100_000,
934
+ dispatchQueueTimeoutMs: 30_000,
935
+ readyTimeoutMs: READY_MS,
936
+ },
899
937
  workerCalls: {
900
938
  maxConcurrent: 65_536,
901
939
  maxPerConnection: 1_024,
@@ -911,11 +949,14 @@ export async function createOwner(options) {
911
949
  if (context.workload !== OP)
912
950
  return undefined;
913
951
  // Executors holding the owner's key run the app's own code. One admitted as bound
914
- // (`bound:…`, an org's own) runs only as its jobs.
952
+ // (`bound:…`, an org's own) runs only as its jobs: it ends nobody's browsers.
915
953
  const trusted = !provider.startsWith("bound:");
916
954
  return async ({ input }) => {
917
955
  try {
918
956
  const request = await decode(input);
957
+ if (!trusted && request.ops.some((op) => op.op === "disconnect")) {
958
+ throw forbidden("This job can't end browsers");
959
+ }
919
960
  await authorize(request, job, trusted);
920
961
  return encode({ value: await serve(request, job) });
921
962
  }
@@ -928,9 +969,15 @@ export async function createOwner(options) {
928
969
  admitUnassigned: (context) => browsers.admit(context),
929
970
  },
930
971
  }, {
972
+ // A browser takes its place as it's let in: connections let in together can't pass a limit.
973
+ onHandshake: ({ fence, definition }) => {
974
+ if (definition.providerId !== BROWSER)
975
+ return;
976
+ browsers.take(fence.identity.workerId, fence.connectionId, definition.providerConfig);
977
+ },
931
978
  onReady: ({ fence, definition }) => {
932
979
  if (definition.providerId === BROWSER) {
933
- return browsers.ready(fence.identity.workerId, definition.providerConfig, fence.connectionId);
980
+ return browsers.ready(fence.identity.workerId, fence.connectionId);
934
981
  }
935
982
  if (definition.providerId.startsWith("worker:")) {
936
983
  const { kind, id } = definition.providerConfig;
@@ -996,6 +1043,10 @@ export async function createOwner(options) {
996
1043
  });
997
1044
  return { metadata: {}, body: handle.output };
998
1045
  };
1046
+ /** Ends an actor's browsers: see `Owner.disconnect`. */
1047
+ const disconnect = (actor) => browsers.end(actor, (workerId) => {
1048
+ void hypervisor.drain(workerId, "ended").catch(() => { });
1049
+ });
999
1050
  const browsers = createBrowsers({
1000
1051
  observers,
1001
1052
  ruled,
@@ -1003,6 +1054,7 @@ export async function createOwner(options) {
1003
1054
  call: (action, input, actor) => api.call(action, input, { actor }),
1004
1055
  emit: (type, data, actor) => emit(type, data, { actor }),
1005
1056
  dispatch: (input) => hypervisor.dispatch(input),
1057
+ ...(options.browsers ? { limits: options.browsers } : {}),
1006
1058
  });
1007
1059
  // Sends a job to an executor; its live frames are published as they arrive.
1008
1060
  const run = async (job, signal) => {
@@ -1012,7 +1064,7 @@ export async function createOwner(options) {
1012
1064
  : job.kind === "process"
1013
1065
  ? job.processor
1014
1066
  : job.kind === "render"
1015
- ? `${job.renderer}:${job.view}`
1067
+ ? `${job.medium}:${job.view}`
1016
1068
  : `${job.collection}.${job.query}`;
1017
1069
  try {
1018
1070
  const result = await dispatch(job, signal);
@@ -1405,7 +1457,7 @@ export async function createOwner(options) {
1405
1457
  async render(medium, kind, item, options = {}) {
1406
1458
  return await run({
1407
1459
  kind: "render",
1408
- renderer: medium,
1460
+ medium,
1409
1461
  view: kind,
1410
1462
  item: json(item),
1411
1463
  ...(options.viewer ? { viewer: options.viewer } : {}),
@@ -1419,6 +1471,7 @@ export async function createOwner(options) {
1419
1471
  actions: Object.fromEntries(Object.entries(registry.actions).map(([name, def]) => [name, {
1420
1472
  ...(def.description ? { description: def.description } : {}),
1421
1473
  ...(def.input ? { input: def.input } : {}),
1474
+ ...(def.record === false ? { record: false } : {}),
1422
1475
  }])),
1423
1476
  views: Object.fromEntries(Object.entries(registry.views).map(([kind, media]) => [kind, Object.keys(media)])),
1424
1477
  resources: registry.resources,
@@ -1447,6 +1500,7 @@ export async function createOwner(options) {
1447
1500
  return () => listeners.delete(listener);
1448
1501
  },
1449
1502
  cancel: (target) => Promise.resolve(cancel(target)),
1503
+ disconnect: (actor) => disconnect(actor),
1450
1504
  // Nothing pending, and every observed View showing what that left.
1451
1505
  async idle() {
1452
1506
  do {
@@ -1492,7 +1546,8 @@ export async function createOwner(options) {
1492
1546
  clearTimeout(flushing);
1493
1547
  // A browser's session is a stream that never ends on its own: end them, so the
1494
1548
  // shutdown's drain doesn't wait them out.
1495
- browsers.closeAll();
1549
+ browsers.close();
1550
+ resumed.close();
1496
1551
  await hypervisor.shutdown();
1497
1552
  // Processors' last positions are saved if they can be (a fenced owner can't): the store
1498
1553
  // closes either way, and a failure still reaches the caller.
package/runtime/reads.js CHANGED
@@ -6,14 +6,14 @@
6
6
  import { collection } from "./collection.js";
7
7
  import { isRead } from "./ops.js";
8
8
  import { invalid, json, readCall } from "./registry.js";
9
- import { renderer } from "./views.js";
9
+ import { presenter } from "./views.js";
10
10
  export function reading(registry, run, record, metrics, actor) {
11
11
  const reads = (op) => isRead(op) ? run(op) : Promise.reject(invalid("Queries are read-only"));
12
12
  const query = querying(registry, reads, record, metrics, actor);
13
13
  const ctx = {
14
14
  db: { collection: (name) => collection(name, reads, query) },
15
15
  graph: { edges: (query) => reads({ op: "edges", query }) },
16
- render: renderer(registry.views, () => reading(registry, reads, record, metrics, actor)),
16
+ render: presenter(registry.views, () => reading(registry, reads, record, metrics, actor)),
17
17
  record,
18
18
  metrics,
19
19
  resources: registry.resources,
@@ -121,8 +121,8 @@ export function merge(list, options = {}) {
121
121
  into(registry.actions, plugin.actions, "action");
122
122
  into(registry.events, plugin.events, "event");
123
123
  into(registry.processors, plugin.processors, "processor");
124
- for (const [kind, renderers] of Object.entries(plugin.views ?? {})) {
125
- replace(registry.views[kind] ??= {}, renderers);
124
+ for (const [kind, media] of Object.entries(plugin.views ?? {})) {
125
+ replace(registry.views[kind] ??= {}, media);
126
126
  }
127
127
  for (const [kind, entries] of Object.entries(plugin.resources ?? {})) {
128
128
  registry.resources[kind] = patch(registry.resources[kind], entries);
@@ -178,7 +178,12 @@ export function merge(list, options = {}) {
178
178
  * the first that throws stops the runtime from starting.
179
179
  */
180
180
  export async function checked(registry, call) {
181
- const merged = { resources: registry.resources, adapters: registry.adapters, call };
181
+ const merged = {
182
+ resources: registry.resources,
183
+ adapters: registry.adapters,
184
+ actions: registry.actions,
185
+ call,
186
+ };
182
187
  for (const { name, check } of registry.checks) {
183
188
  try {
184
189
  await check(merged);
@@ -222,6 +227,7 @@ export async function readCall(registry, reads, call, name, input = {}) {
222
227
  schedule: refuse,
223
228
  unschedule: refuse,
224
229
  cancel: refuse,
230
+ disconnect: refuse,
225
231
  stream: refuse,
226
232
  graph: { ...reads.graph, link: refuse, unlink: refuse },
227
233
  };
@@ -1,5 +1,6 @@
1
1
  import { type Owner, type Transport } from "./owner.js";
2
2
  import type { Blobs } from "./blobs.js";
3
+ import type { BrowserLimits } from "./browsers.js";
3
4
  import type { Plugins } from "./define.js";
4
5
  import type { Actor, Store } from "./store.js";
5
6
  import type { Metrics } from "./metrics.js";
@@ -32,4 +33,10 @@ export declare function createRuntime(options: {
32
33
  * ticket the host issued); undefined refuses it. Without it, no browsers.
33
34
  */
34
35
  admit?(secret: string): Actor | undefined | Promise<Actor | undefined>;
36
+ /**
37
+ * How much browsers may hold, past the defaults (see browsers.ts): each
38
+ * actor's, contacts' together, and a `contactsPool()` shared with other
39
+ * runtimes on the same listener (the orgs host gives one to each org).
40
+ */
41
+ browsers?: BrowserLimits;
35
42
  }): Promise<Runtime>;
@@ -20,6 +20,7 @@ export async function createRuntime(options) {
20
20
  transports: [transport, ...options.transports ?? []],
21
21
  key,
22
22
  ...(options.admit ? { admit: options.admit } : {}),
23
+ ...(options.browsers ? { browsers: options.browsers } : {}),
23
24
  blobs: options.blobs,
24
25
  blobThreshold: options.blobThreshold,
25
26
  cache: options.cache,
package/runtime/timers.js CHANGED
@@ -203,6 +203,8 @@ export function createTimers(options) {
203
203
  lower(op, actor) {
204
204
  if (op.op === "cancel")
205
205
  throw invalid("A cancel goes on its own.");
206
+ if (op.op === "disconnect")
207
+ throw invalid("A disconnect goes on its own.");
206
208
  if (op.op !== "schedule" && op.op !== "unschedule")
207
209
  return op;
208
210
  if (op.timer in schedules) {
@@ -30,5 +30,5 @@ export declare const failure: (pass: Pass) => {
30
30
  */
31
31
  export declare function viewCollections(source: ReadContext, pass: Pass): ViewContext["db"];
32
32
  /** A Render over `views`, loading data through `reads` (a read-only context). */
33
- export declare function renderer(views: Views, reads: () => ReadContext): Render;
33
+ export declare function presenter(views: Views, reads: () => ReadContext): Render;
34
34
  export {};
package/runtime/views.js CHANGED
@@ -46,7 +46,7 @@ export function viewCollections(source, pass) {
46
46
  /** Rendered once, a View has no handlers that could run: it calls nothing. */
47
47
  const once = () => Promise.reject(invalid("Only a View being observed calls Actions, from its handlers."));
48
48
  /** A Render over `views`, loading data through `reads` (a read-only context). */
49
- export function renderer(views, reads) {
49
+ export function presenter(views, reads) {
50
50
  return async (medium, kind, item, options = {}) => {
51
51
  const view = viewOf(views, kind, medium);
52
52
  if (!view)
@@ -57,7 +57,7 @@ export function renderer(views, reads) {
57
57
  const context = (kind, hints) => ({
58
58
  db,
59
59
  resources: source.resources,
60
- renderer: medium,
60
+ medium,
61
61
  kind,
62
62
  ...(options.viewer ? { viewer: options.viewer } : {}),
63
63
  ...(source.actor ? { actor: source.actor } : {}),
package/runtime/wire.d.ts CHANGED
@@ -45,7 +45,7 @@ export type Job =
45
45
  /** A View, rendered for a medium (see `Owner.render`). */
46
46
  | {
47
47
  kind: "render";
48
- renderer: string;
48
+ medium: string;
49
49
  view: string;
50
50
  item: unknown;
51
51
  viewer?: {
@@ -3,7 +3,7 @@ import { type Session } from "./layout.js";
3
3
  export declare function connect(url: string, trace?: (sql: string) => void): Promise<Session & {
4
4
  end(): Promise<void>;
5
5
  }>;
6
- /** Refuses raw SQL on a server whose store names no maintenance role. */
6
+ /** Refuses raw SQL on a server whose store names no role for it. */
7
7
  export declare const noRole: () => Error & {
8
8
  code: string;
9
9
  public?: true;
@@ -14,6 +14,13 @@ export declare const noRole: () => Error & {
14
14
  * the allowlist.
15
15
  */
16
16
  export declare function allowed(db: Session, schema: string): Promise<void>;
17
+ /**
18
+ * Refuses maintenance SQL as the store's own role (`storeRole`) when that
19
+ * role, on `db`, could turn SQL into server commands or file access
20
+ * (`UNCONFINED`). Whatever else the role may do, the key may too: that's
21
+ * what the store's options chose.
22
+ */
23
+ export declare function confined(db: Session): Promise<void>;
17
24
  /**
18
25
  * PGlite's check, its one session being the store's: no other schema's
19
26
  * tables within reach (another store's, in this process), and no remote
@@ -10,12 +10,18 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
10
10
  * Maintenance SQL's connection to a PostgreSQL server, and the role it runs
11
11
  * as (runtime/maintain.ts; the store's `inspect` and `execute`).
12
12
  *
13
- * - **A role of its own, required on a server**
13
+ * - **A role of its own, by default required on a server**
14
14
  * (`postgresStore({ maintenance: { url } })`): the store's own role owns
15
15
  * the tables, may stop the app's connections, and is whatever the
16
16
  * deployment made it (a superuser, even). Without one, raw SQL is
17
17
  * refused; the collections' operations, named queries and blobs don't
18
18
  * need it. PGlite, in this process, runs it as its one session.
19
+ * - **Or, deliberately, the store's own role**
20
+ * (`postgresStore({ maintenance: { storeRole: true } })`): the key's
21
+ * holder may then do whatever that role may (drop its tables, read every
22
+ * schema it reaches). Only the allowlist below is skipped, which that role
23
+ * couldn't pass; `confined` still refuses one that would turn SQL into
24
+ * server commands or file access.
19
25
  * - **Exactly the grants maintenance needs, checked before every
20
26
  * statement** (`allowed`), on the connection that runs it: an allowlist,
21
27
  * not a list of what's refused. The role has no attribute beyond LOGIN,
@@ -28,10 +34,11 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
28
34
  * function (one runs as its owner: revoke EXECUTE from PUBLIC), and the
29
35
  * database has no `dblink` or `postgres_fdw`. The store makes no
30
36
  * triggers: one on its tables means someone else did, and nothing runs.
31
- * - **A connection of its own for each statement, closed after:** whatever
32
- * the statement leaves on its session goes with it. It speaks `pg`
33
- * itself, the driver Ominipg loads, so every query goes over the
34
- * extended protocol, where the server refuses more than one statement.
37
+ * - **A connection of its own for each statement, closed after,** as
38
+ * either role: whatever the statement leaves on its session goes with
39
+ * it. It speaks `pg` itself, the driver Ominipg loads, so every query
40
+ * goes over the extended protocol, where the server refuses more than
41
+ * one statement.
35
42
  * - **At most `connections` at once** (2 by default; ../shared/capped.ts).
36
43
  */
37
44
  import { failure } from "../../../runtime/errors.js";
@@ -65,10 +72,12 @@ export async function connect(url, trace) {
65
72
  };
66
73
  }
67
74
  const unsafe = (message) => failure("unsafe_role", message);
68
- /** Refuses raw SQL on a server whose store names no maintenance role. */
69
- export const noRole = () => unsafe("Raw SQL on a PostgreSQL server runs only as a maintenance role of its own: " +
70
- "postgresStore({ maintenance: { url } }), set up as the README's Maintenance says. " +
71
- "The collections' operations, named queries and blobs work without it.");
75
+ /** Refuses raw SQL on a server whose store names no role for it. */
76
+ export const noRole = () => unsafe("Raw SQL on a PostgreSQL server runs as a maintenance role of its own, " +
77
+ "postgresStore({ maintenance: { url } }), set up as the README's Maintenance says; or, " +
78
+ "deliberately, as the store's own role, postgresStore({ maintenance: { storeRole: true } }), " +
79
+ "which lets the key do whatever that role may (the README says what that costs). " +
80
+ "The collections' operations, named queries and blobs work without either.");
72
81
  /**
73
82
  * Cloud SQL's markers of an IAM database user (a role made by `gcloud sql
74
83
  * users create … --type=cloud_iam_*`): such a role is a member of one. Every
@@ -235,6 +244,64 @@ export async function allowed(db, schema) {
235
244
  throw unsafe(`The maintenance role holds more than maintenance needs: it ${shown}. It must hold the ` +
236
245
  `README's grants on "${schema}" and nothing else.`);
237
246
  }
247
+ /** The predefined roles whose members PostgreSQL lets run server programs or reach its files. */
248
+ const SERVER = ["pg_execute_server_program", "pg_read_server_files", "pg_write_server_files"];
249
+ /**
250
+ * What the session's role (`session_user`) may do that turns SQL into
251
+ * server commands or file access, one line for each, none when nothing:
252
+ * being a superuser, or able to become one (a member of a superuser role
253
+ * could `set_config('role', …)` to it within a statement); being a member,
254
+ * by any path, of a role in `$1`; before PostgreSQL 16, `CREATEROLE`
255
+ * (enough to grant itself any of them, and a function it made could use
256
+ * one within the same statement, before the check after it); and running
257
+ * a function that reads or writes the server's files (`pg_read_file`,
258
+ * `lo_import`, `lo_export`…, refused to everyone unless granted). Cloud
259
+ * SQL's `postgres` user isn't a superuser (a member of
260
+ * `cloudsqlsuperuser`, which isn't one either), and from PostgreSQL 16 its
261
+ * `CREATEROLE` grants nothing it doesn't administer. Every catalog name is
262
+ * `pg_catalog`'s.
263
+ */
264
+ const UNCONFINED = `
265
+ WITH me AS (
266
+ SELECT oid, rolsuper, rolcreaterole FROM pg_catalog.pg_roles WHERE rolname = session_user
267
+ )
268
+ SELECT problem FROM (
269
+ SELECT 1 AS n, 'is a superuser' AS problem FROM me WHERE rolsuper
270
+ UNION ALL SELECT 2, 'may become superuser ' || r.rolname::pg_catalog.text ||
271
+ ' (it is a member of it)'
272
+ FROM pg_catalog.pg_roles r, me
273
+ WHERE NOT me.rolsuper AND r.rolsuper AND r.oid <> me.oid
274
+ AND pg_catalog.pg_has_role(me.oid, r.oid, 'MEMBER')
275
+ UNION ALL SELECT 3, 'is a member of ' || r.rolname::pg_catalog.text
276
+ FROM pg_catalog.pg_roles r, me
277
+ WHERE NOT me.rolsuper AND r.rolname::pg_catalog.text = ANY ($1::pg_catalog.text[])
278
+ AND pg_catalog.pg_has_role(me.oid, r.oid, 'MEMBER')
279
+ UNION ALL SELECT 4, 'may create roles (CREATEROLE), and before PostgreSQL 16 so grant itself ' ||
280
+ 'any of them'
281
+ FROM me WHERE NOT me.rolsuper AND me.rolcreaterole
282
+ AND pg_catalog.current_setting('server_version_num')::pg_catalog.int4 < 160000
283
+ UNION ALL SELECT 5, 'may run ' || p.oid::pg_catalog.regprocedure::pg_catalog.text
284
+ FROM pg_catalog.pg_proc p, me
285
+ WHERE NOT me.rolsuper AND p.pronamespace = 'pg_catalog'::pg_catalog.regnamespace
286
+ AND p.proname IN ('pg_read_file', 'pg_read_binary_file', 'lo_import', 'lo_export',
287
+ 'pg_file_write', 'pg_file_rename', 'pg_file_unlink')
288
+ AND pg_catalog.has_function_privilege(me.oid, p.oid, 'EXECUTE')
289
+ ) found ORDER BY n, problem`;
290
+ /**
291
+ * Refuses maintenance SQL as the store's own role (`storeRole`) when that
292
+ * role, on `db`, could turn SQL into server commands or file access
293
+ * (`UNCONFINED`). Whatever else the role may do, the key may too: that's
294
+ * what the store's options chose.
295
+ */
296
+ export async function confined(db) {
297
+ const problems = (await db.query(UNCONFINED, [SERVER])).rows.map((row) => row.problem);
298
+ if (!problems.length)
299
+ return;
300
+ throw unsafe(`Raw SQL as the store's own role (storeRole) could run commands on the database server or ` +
301
+ `reach its files: the role ${problems.join("; ")}. Use a store role that can't (Cloud ` +
302
+ `SQL's postgres user can't, from PostgreSQL 16), or a maintenance role of its own ` +
303
+ `(maintenance: { url }).`);
304
+ }
238
305
  /**
239
306
  * PGlite's check, its one session being the store's: no other schema's
240
307
  * tables within reach (another store's, in this process), and no remote
@@ -37,13 +37,21 @@ export declare function postgresStore(options: {
37
37
  /** Hears every SQL statement sent, for debugging. */
38
38
  trace?: (sql: string) => void;
39
39
  /**
40
- * Where maintenance SQL connects (`inspect`, a transaction's `execute`;
41
- * see maintenance.ts): a role of its own, holding exactly maintenance's
42
- * grants on this schema. A new connection for each statement, closed
43
- * after, `connections` at most at once (2). On a server, raw SQL needs it.
40
+ * Who maintenance SQL runs as (`inspect`, a transaction's `execute`; see
41
+ * maintenance.ts), on a server, where raw SQL needs one or the other:
42
+ * `url`, a role of its own, holding exactly maintenance's grants on this
43
+ * schema; or, deliberately, `storeRole: true`, the store's own role (this
44
+ * store's `url`, or its pool's), so the key may do whatever that role may,
45
+ * dropping its tables included. Not both. Either way a new connection for
46
+ * each statement, closed after, `connections` at most at once (2).
44
47
  */
45
48
  maintenance?: {
46
49
  url: string;
50
+ storeRole?: never;
51
+ connections?: number;
52
+ } | {
53
+ storeRole: true;
54
+ url?: never;
47
55
  connections?: number;
48
56
  };
49
57
  }): Promise<Store & {