copilotz 1.0.0-rc.5 → 1.0.0-rc.6

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 (66) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +2 -2
  3. package/package.json +1 -1
  4. package/plugins/access/spaces/actions/org.js +2 -0
  5. package/plugins/compute/apps/actions/publish.js +2 -0
  6. package/plugins/compute/apps/resources/tools.js +4 -1
  7. package/plugins/compute/apps/shared/publish.d.ts +8 -0
  8. package/plugins/compute/apps/shared/publish.js +23 -2
  9. package/plugins/compute/apps/views/web/apps.d.ts +2 -0
  10. package/plugins/compute/apps/views/web/publish.js +2 -0
  11. package/plugins/compute/cloud/actions/create.js +9 -21
  12. package/plugins/compute/cloud/actions/may.d.ts +1 -0
  13. package/plugins/compute/cloud/actions/may.js +15 -0
  14. package/plugins/compute/cloud/actions/pool-tick.js +9 -4
  15. package/plugins/compute/cloud/actions/vms.d.ts +2 -0
  16. package/plugins/compute/cloud/actions/vms.js +17 -5
  17. package/plugins/compute/cloud/adapters/local.js +10 -1
  18. package/plugins/compute/cloud/collections/cloud-computer.js +8 -1
  19. package/plugins/compute/cloud/collections/pool-want.d.ts +94 -0
  20. package/plugins/compute/cloud/collections/pool-want.js +27 -0
  21. package/plugins/compute/cloud/mod.d.ts +5 -2
  22. package/plugins/compute/cloud/mod.js +13 -4
  23. package/plugins/compute/cloud/processors/mark.d.ts +9 -0
  24. package/plugins/compute/cloud/processors/mark.js +13 -0
  25. package/plugins/compute/cloud/shared/may.d.ts +13 -0
  26. package/plugins/compute/cloud/shared/may.js +23 -0
  27. package/plugins/compute/cloud/shared/records.d.ts +7 -0
  28. package/plugins/compute/cloud/shared/records.js +2 -0
  29. package/plugins/compute/cloud/views/web/cloud.d.ts +7 -1
  30. package/plugins/compute/cloud/views/web/cloud.js +10 -1
  31. package/plugins/compute/cloud/views/web/computer.d.ts +7 -1
  32. package/plugins/compute/cloud/views/web/computer.js +14 -4
  33. package/plugins/compute/cloud/views/web/new.d.ts +2 -0
  34. package/plugins/compute/cloud/views/web/new.js +9 -4
  35. package/plugins/compute/cloud/web.js +6 -0
  36. package/plugins/compute/computers/actions/mark.d.ts +1 -0
  37. package/plugins/compute/computers/actions/mark.js +36 -0
  38. package/plugins/compute/computers/actions/preview.d.ts +1 -4
  39. package/plugins/compute/computers/actions/preview.js +16 -10
  40. package/plugins/compute/computers/actions/scope.js +10 -6
  41. package/plugins/compute/computers/collections/computer.d.ts +46 -0
  42. package/plugins/compute/computers/collections/computer.js +10 -0
  43. package/plugins/compute/computers/collections/desk.d.ts +38 -0
  44. package/plugins/compute/computers/collections/desk.js +6 -0
  45. package/plugins/compute/computers/collections/preview.d.ts +2 -0
  46. package/plugins/compute/computers/collections/preview.js +8 -2
  47. package/plugins/compute/computers/mod.js +2 -0
  48. package/plugins/compute/computers/program/version.d.ts +1 -1
  49. package/plugins/compute/computers/program/version.js +1 -1
  50. package/plugins/compute/computers/resources/downloads.js +1 -1
  51. package/plugins/compute/computers/shared/desks.d.ts +1 -1
  52. package/plugins/compute/computers/shared/desks.js +1 -0
  53. package/plugins/compute/computers/shared/preview-url.d.ts +5 -0
  54. package/plugins/compute/computers/shared/preview-url.js +5 -0
  55. package/plugins/compute/computers/views/web/add.js +3 -1
  56. package/plugins/compute/computers/views/web/desks.js +2 -2
  57. package/plugins/compute/computers/views/web/kinds.d.ts +13 -1
  58. package/plugins/compute/computers/views/web/kinds.js +9 -0
  59. package/plugins/compute/computers/views/web/previews.d.ts +2 -7
  60. package/plugins/compute/computers/views/web/previews.js +0 -14
  61. package/plugins/compute/computers/views/web/serve.d.ts +3 -3
  62. package/plugins/compute/computers/views/web/serve.js +10 -46
  63. package/plugins/compute/computers/views/web/space-tab.js +11 -2
  64. package/plugins/compute/computers/views/web/style.d.ts +1 -1
  65. package/plugins/compute/computers/views/web/style.js +4 -0
  66. package/plugins/compute/computers/views/web/workspace.js +1 -0
package/CHANGELOG.md CHANGED
@@ -1,3 +1,30 @@
1
+ ## 1.0.0-rc.6 — 2026-10-30
2
+
3
+ - **Computer program:** an app on another machine over plain `ws:` (an `http://` address that isn't
4
+ localhost) fails at once, saying to use https, instead of waiting to connect forever: Oxian
5
+ refuses plain `ws:` but to this machine.
6
+ - **Cloud pools:** a pool grown for a wake isn't shrunk while its VM boots. `pool.take` records what
7
+ it waits for (`poolWant`), and `pool.tick` sizes each pool to its floor, the takes of the last 2
8
+ minutes and the wakes still waiting. Before, the next minute's sizing set the pool back to its
9
+ floor, deleting the VM a wake had grown it for before it connected.
10
+ - **Cloud computers:** `cloudComputer.sizes` also says whether the person asking may make one now
11
+ (`create`, and `why` not: `admins`, `budget`), and the warm and view minutes; `cloud.may` says
12
+ just that. The web's "Use a cloud computer" choice shows why it can't be picked, and the copy says
13
+ the app's minutes.
14
+ - **Cloud computers shared into a space:** each computer's state is kept on it and its desks
15
+ (`status`: `{ kind: "cloud", state, size }`, set with the new system Action `computer.mark`, as
16
+ the cloud plugin sees it change), so the space's members see how anyone's is (asleep, waking,
17
+ awake), not "offline".
18
+ - **Previews:** `preview.here` records carry their address (`url`); the web no longer works it out.
19
+ `computer.preview` and `computer.scope` take a `desk` (one of the space's), and so does
20
+ `app.publish`: the web makes previews and publishes apps from the computer it's on, not the
21
+ space's default.
22
+ - **Apps:** an agent's `publish` asks its person with where the app will be ("wants to publish Shop
23
+ at https://shop.copilotz.app").
24
+ - **`org.me`** isn't recorded (it only reads), so reads (queries, Views) ask it too.
25
+ - **The local cloud host** is one per app and account, whichever executor asks: with several
26
+ executors, a VM one started was turned away by another.
27
+
1
28
  ## 1.0.0-rc.5 — 2026-10-30
2
29
 
3
30
  - **Cloud computers** (plugin `cloud`, see design/cloud-computers.md): computers the app hosts for
package/README.md CHANGED
@@ -22,7 +22,7 @@ bun add copilotz # Bun
22
22
  deno add jsr:@copilotz/copilotz # Deno (or `deno add npm:copilotz`)
23
23
  ```
24
24
 
25
- On Deno, name it `copilotz` in `deno.json` (`"copilotz": "jsr:@copilotz/copilotz@^1.0.0-rc.5"`), and
25
+ On Deno, name it `copilotz` in `deno.json` (`"copilotz": "jsr:@copilotz/copilotz@^1.0.0-rc.6"`), and
26
26
  the imports below are the same everywhere. Some pieces need a package of their own, installed beside
27
27
  it: `esbuild` to bundle the web host on Node, `@electric-sql/pglite` for PostgreSQL in the process,
28
28
  and `@types/react` for the front ends' types.
@@ -204,7 +204,7 @@ and an app serves it from there by default:
204
204
  import { computerDownloads } from "copilotz/plugins/compute/downloads";
205
205
 
206
206
  plugins: [...plugins, await computerDownloads()], // this Copilotz's release
207
- plugins: [...plugins, await computerDownloads({ dir: "dist/computer/1.0.0-rc.5" })], // its own: /computer/download/
207
+ plugins: [...plugins, await computerDownloads({ dir: "dist/computer/1.0.0-rc.6" })], // its own: /computer/download/
208
208
  plugins: [...plugins, await computerDownloads({ url: "https://downloads.example.com/computer/" })], // or elsewhere
209
209
  ```
210
210
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "copilotz",
3
- "version": "1.0.0-rc.5",
3
+ "version": "1.0.0-rc.6",
4
4
  "description": "Copilotz: one runtime for Node, Deno, Bun, Cloudflare Workers and the browser.",
5
5
  "keywords": [
6
6
  "agents",
@@ -124,6 +124,8 @@ export const removeFromOrgAction = defineAction({
124
124
  export const orgMeAction = defineAction({
125
125
  description: "Your standing in the org: your role, whether you're platform staff, and the org (in an app " +
126
126
  "serving many)",
127
+ // Only reads: not recorded, so reads (a query, a View) ask it too.
128
+ record: false,
127
129
  input: { type: "object", additionalProperties: false },
128
130
  allow: (actor) => actor.kind === "person",
129
131
  run: async (ctx) => ({
@@ -14,6 +14,8 @@ export const publishAction = defineAction({
14
14
  properties: {
15
15
  /** The space (default: your Personal space). */
16
16
  in: { type: "string" },
17
+ /** The space's computer to publish from (its desk there); else the space's own. */
18
+ desk: { type: "string" },
17
19
  name: { type: "string", minLength: 1, maxLength: 80 },
18
20
  /** The job whose port it serves, and whose command runs it. */
19
21
  job: { type: "string" },
@@ -1,6 +1,6 @@
1
1
  import { tool } from "../../../agents/chat/contract.js";
2
2
  import { messageOf } from "../shared/errors.js";
3
- import { can, detailOf, personOf, publish as publishApp } from "../shared/publish.js";
3
+ import { addressFor, can, detailOf, personOf, publish as publishApp } from "../shared/publish.js";
4
4
  const publish = tool({
5
5
  description: "Publishes what one of your jobs on this space's computer serves as an app " +
6
6
  "that runs on its own, without the computer, at an address of its own (https://<name>." +
@@ -33,11 +33,14 @@ const publish = tool({
33
33
  }
34
34
  if (!await can(ctx, thread.in, "write"))
35
35
  return "You can't publish from this space.";
36
+ // Its person reads where it'll be: "wants to publish Shop at https://shop.copilotz.app".
37
+ const address = await addressFor(ctx, thread.in, input.name).catch(() => undefined);
36
38
  const may = await ctx.call("computer.allowed", {
37
39
  thread: ctx.thread,
38
40
  agent: ctx.agent,
39
41
  kind: "publish",
40
42
  detail: detailOf(input),
43
+ ...(address ? { preview: address } : {}),
41
44
  });
42
45
  if (may !== true)
43
46
  return may;
@@ -11,6 +11,8 @@ import type { Actor, Context } from "../../../../runtime/mod.js";
11
11
  import type { AppDoc } from "../collections/app.js";
12
12
  export type PublishInput = {
13
13
  in: string;
14
+ /** The space's computer (its desk there), when the person says which. */
15
+ desk?: string;
14
16
  name: string;
15
17
  job?: string;
16
18
  port?: number;
@@ -27,6 +29,12 @@ export declare function personOf(actor: Actor | undefined): string | undefined;
27
29
  export declare const can: (ctx: Context, space: string, verb: "write" | "manage") => Promise<boolean>;
28
30
  /** What an agent asks its person to publish, as they read it (and a yes this once covers). */
29
31
  export declare const detailOf: (input: Omit<PublishInput, "in">) => string;
32
+ /**
33
+ * Where an app of this name in the space will be, as its person is asked: its
34
+ * address, published already; else its name's, when nothing here has it (one
35
+ * taken in another org gets a short suffix when it's published).
36
+ */
37
+ export declare function addressFor(ctx: Context, space: string, name: string): Promise<string | undefined>;
30
38
  /**
31
39
  * Publishes for `by` (the person; `who`: whose shell asks the computer,
32
40
  * `agent:<name>` or `person:<id>`). The caller has checked they may.
@@ -1,7 +1,7 @@
1
1
  import { WORK } from "../../computers/contract.js";
2
2
  import { codeOf, failure } from "./errors.js";
3
3
  import { hostOf } from "./host.js";
4
- import { claimLabel } from "./label.js";
4
+ import { claimLabel, kept, slugOf } from "./label.js";
5
5
  import { settingsOf } from "./settings.js";
6
6
  /** Whose work this is: the person acting, or whom an agent (`for`) or trusted code (`via`) acts for. */
7
7
  export function personOf(actor) {
@@ -21,6 +21,23 @@ export const detailOf = (input) => [
21
21
  input.port ? `on port ${input.port}` : "",
22
22
  input.path ? `from ${input.path}` : "",
23
23
  ].filter(Boolean).join(" ");
24
+ /**
25
+ * Where an app of this name in the space will be, as its person is asked: its
26
+ * address, published already; else its name's, when nothing here has it (one
27
+ * taken in another org gets a short suffix when it's published).
28
+ */
29
+ export async function addressFor(ctx, space, name) {
30
+ const apps = ctx.as().db.collection("app");
31
+ const had = await apps.findOne({ space, name: name.trim(), status: { $ne: "removed" } });
32
+ if (had)
33
+ return had.url;
34
+ const label = slugOf(name);
35
+ if (!label || kept(label))
36
+ return undefined;
37
+ if (await apps.findOne({ label, status: { $ne: "removed" } }))
38
+ return undefined;
39
+ return `https://${label}.${settingsOf(ctx).domain}`;
40
+ }
24
41
  /** When the scheduler builds `app`: now, as a timer of its own (one per app). */
25
42
  const buildNow = (app) => ({
26
43
  at: new Date().toISOString(),
@@ -69,7 +86,11 @@ export async function publish(ctx, input) {
69
86
  throw failure("An app needs a name.", "invalid");
70
87
  // Nowhere to publish to fails now, not once it's building.
71
88
  hostOf(ctx);
72
- const where = await ctx.as().call("computer.scope", { in: input.in, who: input.who });
89
+ const where = await ctx.as().call("computer.scope", {
90
+ in: input.in,
91
+ who: input.who,
92
+ ...(input.desk ? { desk: input.desk } : {}),
93
+ });
73
94
  const source = await sourceOf(ctx, where, input);
74
95
  const apps = ctx.as().db.collection("app");
75
96
  const had = await apps.findOne({ space: input.in, name, status: { $ne: "removed" } });
@@ -15,6 +15,8 @@ import type { AppDoc } from "../../collections/app.js";
15
15
  export type JobServed = {
16
16
  in: string;
17
17
  computer: string;
18
+ /** The computer's desk in the space, when it's known: what's published comes from it. */
19
+ desk?: string;
18
20
  job: string;
19
21
  name: string;
20
22
  command: string;
@@ -73,6 +73,8 @@ function PublishForm({ item, apps, call, cancel, published, from }) {
73
73
  try {
74
74
  const answer = await call("app.publish", {
75
75
  in: item.in,
76
+ // From the computer the job runs on, not the space's default.
77
+ ...(item.desk ? { desk: item.desk } : {}),
76
78
  name: name.trim(),
77
79
  job: item.job,
78
80
  ...(port ? { port: number } : {}),
@@ -1,11 +1,8 @@
1
1
  /**
2
2
  * `cloud.create` `{ size, in?, name? }`: a cloud computer for the person
3
3
  * asking, in a space they may work in (their Personal space unless they
4
- * say), shared into others as any computer is. Who may: the org's owners and
5
- * admins (and platform staff), its members too when the settings say
6
- * (`create: "members"`), and anyone in their own personal org; and only
7
- * while the org's cloud spend this month is under its budget (`budgetUsd`),
8
- * when it has one. Its record and secret are made directly (`computer.provision`),
4
+ * say), shared into others as any computer is. Who may, and when: see
5
+ * shared/may.ts (`cloud.may` asks it). Its record and secret are made directly (`computer.provision`),
9
6
  * its agents asking their person only what the settings' `approve` says
10
7
  * (nothing by default: publishing is always asked), then its disk, in the
11
8
  * first placement; its first wake formats it. Answers `{ computer }`.
@@ -13,17 +10,10 @@
13
10
  import { defineAction } from "../../../../runtime/mod.js";
14
11
  import { hostFor } from "../shared/host.js";
15
12
  import { diskOf } from "../shared/names.js";
16
- import { forbidden, invalid, runs, standingOf } from "../shared/people.js";
13
+ import { mayCreate } from "../shared/may.js";
14
+ import { forbidden, invalid } from "../shared/people.js";
17
15
  import { recordsOf } from "../shared/records.js";
18
16
  import { settingsOf } from "../shared/settings.js";
19
- /** What cloud computers have cost the org this month, at their price (none without a ledger). */
20
- async function spent(ctx, now) {
21
- const totals = await ctx.as().db.collection("usage").query("totals", {
22
- filter: { meter: "computer", at: { $gte: `${now.slice(0, 7)}-01T00:00:00.000Z` } },
23
- by: [],
24
- }).catch(() => []);
25
- return totals.reduce((sum, t) => sum + (t.price ?? 0), 0);
26
- }
27
17
  export const createAction = defineAction({
28
18
  description: "Makes a cloud computer, in a space (your Personal space unless you say)",
29
19
  input: {
@@ -43,16 +33,14 @@ export const createAction = defineAction({
43
33
  if (!size) {
44
34
  throw invalid(`No size "${input.size}": ${Object.keys(set.sizes).join(", ") || "none"}`);
45
35
  }
46
- const me = await standingOf(ctx);
47
- const personal = ctx.resources.org?.kind === "personal";
48
- const may = runs(me) || (set.create === "members" && me.role !== "guest") ||
49
- (personal && me.role === "owner");
50
- if (!may)
36
+ const may = await mayCreate(ctx);
37
+ if (may.why === "admins") {
51
38
  throw forbidden("Only the org's owners and admins make cloud computers here.");
52
- const now = new Date().toISOString();
53
- if (set.budgetUsd !== undefined && await spent(ctx, now) >= set.budgetUsd) {
39
+ }
40
+ if (may.why === "budget") {
54
41
  throw Object.assign(new Error("This month's budget for cloud computers is spent: no more can be made."), { code: "budget" });
55
42
  }
43
+ const now = new Date().toISOString();
56
44
  const placement = set.placements[0];
57
45
  if (!placement)
58
46
  throw invalid("The app has no placement for cloud computers.");
@@ -0,0 +1 @@
1
+ export declare const mayAction: import("../../../../runtime/define.js").ActionDef;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * `cloud.may` `{}`: whether the person asking may make a cloud computer now
3
+ * (`{ create }`), and why not (`why`: only admins make them here, or this
4
+ * month's budget is spent). Only reads, so it isn't recorded: the sizes
5
+ * people choose from say it too.
6
+ */
7
+ import { defineAction } from "../../../../runtime/mod.js";
8
+ import { mayCreate } from "../shared/may.js";
9
+ export const mayAction = defineAction({
10
+ description: "Whether you may make a cloud computer now, and why not",
11
+ record: false,
12
+ input: { type: "object", additionalProperties: false },
13
+ allow: (actor) => actor.kind === "person",
14
+ run: (ctx) => mayCreate(ctx),
15
+ });
@@ -4,7 +4,8 @@
4
4
  *
5
5
  * 1. **Sizing:** each placement's pool of each size is asked to keep the
6
6
  * floor for this hour (in its time zone) plus the VMs taken in the last 2
7
- * minutes (those expected before a replacement is ready), up to `max`.
7
+ * minutes (those expected before a replacement is ready) plus the wakes
8
+ * waiting for a VM it grew for them (`poolWant`), up to `max`.
8
9
  * 2. **Leases:** a VM given to a computer whose lease ran out (nobody renewed
9
10
  * it: its computer was released and the VM's deletion failed, a wake
10
11
  * ended halfway, the runtime that took it is gone) is deleted. Its disk
@@ -17,10 +18,10 @@
17
18
  */
18
19
  import { defineAction } from "../../../../runtime/mod.js";
19
20
  import { hostFor } from "../shared/host.js";
20
- import { poolsOf } from "../shared/records.js";
21
+ import { poolsOf, wantsOf } from "../shared/records.js";
21
22
  import { samePlace, settingsOf } from "../shared/settings.js";
22
23
  import { after, hourIn, minutes, nowOf } from "../shared/time.js";
23
- import { LEASE_MS } from "./vms.js";
24
+ import { LEASE_MS, VM_MS } from "./vms.js";
24
25
  const log = (what) => (error) => console.error(`cloud pool: ${what}:`, error instanceof Error ? error.message : error);
25
26
  /** How far back takes count toward the pool: about as long as a replacement VM takes. */
26
27
  const TAKES_MS = minutes(2);
@@ -51,12 +52,16 @@ export const poolTickAction = defineAction({
51
52
  const pools = poolsOf(ctx);
52
53
  // 1. Each pool, the size it should be now.
53
54
  const recent = await pools.find({ taken: { $gte: after(now, -TAKES_MS) } });
55
+ // Wakes waiting for a VM their pool grew for (a take that ended took its want with it).
56
+ await wantsOf(ctx).deleteMany({ at: { $lt: after(now, -2 * VM_MS) } });
57
+ const waiting = await wantsOf(ctx).find({});
54
58
  const sized = {};
55
59
  for (const placement of set.placements) {
56
60
  for (const size of Object.keys(set.sizes)) {
57
61
  try {
58
62
  const takes = recent.filter((vm) => vm.size === size && samePlace(vm.placement, placement));
59
- const desired = Math.min(set.pool.max, floorAt(set.pool.floor, placement.zone, size, now) + takes.length);
63
+ const wants = waiting.filter((w) => w.size === size && samePlace(w.placement, placement));
64
+ const desired = Math.min(set.pool.max, floorAt(set.pool.floor, placement.zone, size, now) + takes.length + wants.length);
60
65
  const host = await hostFor(ctx, placement);
61
66
  const { target } = await host.pool(placement, size);
62
67
  if (target !== desired)
@@ -1,5 +1,7 @@
1
1
  /** How long a VM given to a computer is its without a renewal. */
2
2
  export declare const LEASE_MS: number;
3
+ /** How long a take waits for a VM when none was free. */
4
+ export declare const VM_MS = 150000;
3
5
  export declare const takeAction: import("../../../../runtime/define.js").ActionDef;
4
6
  export declare const serveAction: import("../../../../runtime/define.js").ActionDef;
5
7
  export declare const stopAction: import("../../../../runtime/define.js").ActionDef;
@@ -23,13 +23,13 @@
23
23
  import { defineAction } from "../../../../runtime/mod.js";
24
24
  import { POOL, POOL_KIND, } from "../contract.js";
25
25
  import { hostFor } from "../shared/host.js";
26
- import { poolsOf } from "../shared/records.js";
26
+ import { poolsOf, wantsOf } from "../shared/records.js";
27
27
  import { samePlace } from "../shared/settings.js";
28
28
  import { after, minutes, nowOf, poll } from "../shared/time.js";
29
29
  /** How long a VM given to a computer is its without a renewal. */
30
30
  export const LEASE_MS = minutes(10);
31
31
  /** How long a take waits for a VM when none was free. */
32
- const VM_MS = 150_000;
32
+ export const VM_MS = 150_000;
33
33
  /**
34
34
  * How long a VM has to serve a computer (it answers once the computer's
35
35
  * connected: up to a minute for the disk to appear, formatting, up to a
@@ -92,9 +92,21 @@ export const takeAction = defineAction({
92
92
  };
93
93
  let vm = await claim();
94
94
  if (!vm) {
95
- const { target } = await host.pool(want.placement, want.size);
96
- await host.resize(want.placement, want.size, target + 1);
97
- vm = await poll(claim, VM_MS, { every: 500, signal: ctx.signal });
95
+ // Its pool grows by one for it; until the take ends, the pool's sizing counts it.
96
+ const { insertedId: wanted } = await wantsOf(ctx).insertOne({
97
+ placement: want.placement,
98
+ size: want.size,
99
+ computer: want.computer,
100
+ at: new Date().toISOString(),
101
+ });
102
+ try {
103
+ const { target } = await host.pool(want.placement, want.size);
104
+ await host.resize(want.placement, want.size, target + 1);
105
+ vm = await poll(claim, VM_MS, { every: 500, signal: ctx.signal });
106
+ }
107
+ finally {
108
+ await wantsOf(ctx).deleteOne({ _id: wanted }).catch(() => { });
109
+ }
98
110
  if (!vm)
99
111
  throw failure(`no VM was free, and none started within ${VM_MS / 1000} s`);
100
112
  }
@@ -23,7 +23,16 @@ function programLauncher(program) {
23
23
  }
24
24
  /** A local host: one per account it's made for, each in a folder of its own. */
25
25
  export function localHost(options = {}) {
26
- return (account, { resources }) => createLocalHost(options, account, resources);
26
+ // One local cloud per app and account, whichever of its executors asks: each executor has
27
+ // resources of its own, and a VM one of them started must be the one another verifies.
28
+ const hosts = new Map();
29
+ return (account, { resources }) => {
30
+ const key = `${options.app ?? appOf(resources) ?? ""}|${account ?? ""}`;
31
+ let host = hosts.get(key);
32
+ if (!host)
33
+ hosts.set(key, host = createLocalHost(options, account, resources));
34
+ return host;
35
+ };
27
36
  }
28
37
  export function createLocalHost(options, account, resources = {}) {
29
38
  const launch = options.launch ?? programLauncher(options.program);
@@ -52,7 +52,10 @@ export const cloudComputer = defineCollection({
52
52
  /**
53
53
  * The sizes people choose from, each with its price an hour, and what a
54
54
  * new computer's disk is: its GB, and storage's price per GB-month (a
55
- * disk's, and a snapshot's once it's cold).
55
+ * disk's, and a snapshot's once it's cold); how long one stays awake
56
+ * unused (`warmMinutes`) and while someone has it open (`viewMinutes`);
57
+ * and whether the asker may make one now (`create`; `why` not: only
58
+ * admins make them here, or this month's budget is spent).
56
59
  */
57
60
  sizes: {
58
61
  allow: (actor) => actor.kind === "person",
@@ -68,6 +71,7 @@ export const cloudComputer = defineCollection({
68
71
  ...(size.shared ? { shared: true } : {}),
69
72
  hour: await quote(SUBJECTS.vm(id), { seconds: 3600 }),
70
73
  })));
74
+ const may = await ctx.call("cloud.may", {}).catch(() => ({ create: false }));
71
75
  return {
72
76
  sizes,
73
77
  disk: {
@@ -75,6 +79,9 @@ export const cloudComputer = defineCollection({
75
79
  gbMonth: await quote(SUBJECTS.disk, { gbHours: MONTH_HOURS }),
76
80
  snapshotGbMonth: await quote(SUBJECTS.snapshot, { gbHours: MONTH_HOURS }),
77
81
  },
82
+ warmMinutes: set.warmMinutes,
83
+ viewMinutes: set.viewMinutes,
84
+ ...may,
78
85
  };
79
86
  },
80
87
  },
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Wakes waiting for a VM (the cloudPool plugin's): a take that found none
3
+ * free grew its pool by one, and waits for that VM. The pools are sized with
4
+ * them counted (`pool.tick`), so a pool grown for a wake isn't shrunk again
5
+ * before its VM connects. Each goes once its take ends. Only the system reads
6
+ * them.
7
+ */
8
+ import { type Infer } from "../../../../runtime/mod.js";
9
+ declare const poolWantSchema: {
10
+ readonly type: "object";
11
+ readonly properties: {
12
+ readonly placement: {
13
+ readonly type: "object";
14
+ readonly properties: {
15
+ readonly account: {
16
+ readonly type: "string";
17
+ };
18
+ readonly project: {
19
+ readonly type: "string";
20
+ };
21
+ readonly zone: {
22
+ readonly type: "string";
23
+ };
24
+ };
25
+ readonly required: readonly ["project", "zone"];
26
+ };
27
+ readonly size: {
28
+ readonly type: "string";
29
+ };
30
+ readonly computer: {
31
+ readonly type: "string";
32
+ };
33
+ readonly at: {
34
+ readonly type: "string";
35
+ };
36
+ };
37
+ readonly required: readonly ["placement", "size", "computer", "at"];
38
+ };
39
+ export declare const poolWant: {
40
+ schema: {
41
+ readonly type: "object";
42
+ readonly properties: {
43
+ readonly placement: {
44
+ readonly type: "object";
45
+ readonly properties: {
46
+ readonly account: {
47
+ readonly type: "string";
48
+ };
49
+ readonly project: {
50
+ readonly type: "string";
51
+ };
52
+ readonly zone: {
53
+ readonly type: "string";
54
+ };
55
+ };
56
+ readonly required: readonly ["project", "zone"];
57
+ };
58
+ readonly size: {
59
+ readonly type: "string";
60
+ };
61
+ readonly computer: {
62
+ readonly type: "string";
63
+ };
64
+ readonly at: {
65
+ readonly type: "string";
66
+ };
67
+ };
68
+ readonly required: readonly ["placement", "size", "computer", "at"];
69
+ };
70
+ events?: boolean;
71
+ records?: boolean;
72
+ durable?: false;
73
+ indexes?: string[][];
74
+ private?: string[];
75
+ queries?: Record<string, import("../../../../runtime/define.js").QueryDef<{
76
+ [x: string]: unknown;
77
+ at: string;
78
+ size: string;
79
+ placement: {
80
+ [x: string]: unknown;
81
+ account?: string | undefined;
82
+ project: string;
83
+ zone: string;
84
+ };
85
+ computer: string;
86
+ } & {
87
+ _id: string;
88
+ }>> | undefined;
89
+ vectors?: string[];
90
+ };
91
+ export type PoolWant = Infer<typeof poolWantSchema> & {
92
+ _id: string;
93
+ };
94
+ export {};
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Wakes waiting for a VM (the cloudPool plugin's): a take that found none
3
+ * free grew its pool by one, and waits for that VM. The pools are sized with
4
+ * them counted (`pool.tick`), so a pool grown for a wake isn't shrunk again
5
+ * before its VM connects. Each goes once its take ends. Only the system reads
6
+ * them.
7
+ */
8
+ import { defineCollection } from "../../../../runtime/mod.js";
9
+ const poolWantSchema = {
10
+ type: "object",
11
+ properties: {
12
+ placement: {
13
+ type: "object",
14
+ properties: {
15
+ account: { type: "string" },
16
+ project: { type: "string" },
17
+ zone: { type: "string" },
18
+ },
19
+ required: ["project", "zone"],
20
+ },
21
+ size: { type: "string" },
22
+ computer: { type: "string" },
23
+ at: { type: "string" },
24
+ },
25
+ required: ["placement", "size", "computer", "at"],
26
+ };
27
+ export const poolWant = defineCollection({ schema: poolWantSchema, indexes: [["at"]] });
@@ -23,8 +23,11 @@
23
23
  * `coldDays` go cold (a snapshot).
24
24
  * - **Destroying:** `cloud.destroy` (its owner, or the org's admins).
25
25
  * - **Reading:** `cloudComputer.mine` (yours), `cloudComputer.sizes` (what
26
- * people choose from, priced with `usage.quote`), `cloud.org` (the org's,
27
- * for its admins).
26
+ * people choose from, priced with `usage.quote`, with the warm and view
27
+ * minutes, and whether the asker may make one now: `create`, `why` not),
28
+ * `cloud.may` (just that), `cloud.org` (the org's, for its admins). Each
29
+ * computer's state is on its desks too (`computer.mark`, as it changes), so
30
+ * a space's members see how anyone's shared into it is.
28
31
  * - **Metering** (meter `computer`): VM seconds as `cloud/<size>`, disk and
29
32
  * snapshot GB-hours as `cloud/disk` and `cloud/snapshot`, priced by the
30
33
  * app's cost and price books (storage's rates per GB-hour: `per: 1`).
@@ -23,8 +23,11 @@
23
23
  * `coldDays` go cold (a snapshot).
24
24
  * - **Destroying:** `cloud.destroy` (its owner, or the org's admins).
25
25
  * - **Reading:** `cloudComputer.mine` (yours), `cloudComputer.sizes` (what
26
- * people choose from, priced with `usage.quote`), `cloud.org` (the org's,
27
- * for its admins).
26
+ * people choose from, priced with `usage.quote`, with the warm and view
27
+ * minutes, and whether the asker may make one now: `create`, `why` not),
28
+ * `cloud.may` (just that), `cloud.org` (the org's, for its admins). Each
29
+ * computer's state is on its desks too (`computer.mark`, as it changes), so
30
+ * a space's members see how anyone's shared into it is.
28
31
  * - **Metering** (meter `computer`): VM seconds as `cloud/<size>`, disk and
29
32
  * snapshot GB-hours as `cloud/disk` and `cloud/snapshot`, priced by the
30
33
  * app's cost and price books (storage's rates per GB-hour: `per: 1`).
@@ -70,6 +73,7 @@ import { definePlugin } from "../../../runtime/mod.js";
70
73
  import { createAction } from "./actions/create.js";
71
74
  import { dailyAction } from "./actions/daily.js";
72
75
  import { destroyAction } from "./actions/destroy.js";
76
+ import { mayAction } from "./actions/may.js";
73
77
  import { orgAction } from "./actions/org.js";
74
78
  import { poolAdmitAction, poolGoneAction, poolReadyAction } from "./actions/pool.js";
75
79
  import { poolTickAction } from "./actions/pool-tick.js";
@@ -82,7 +86,9 @@ import { wakeAction } from "./actions/wake.js";
82
86
  import { gceHost } from "./adapters/gce/index.js";
83
87
  import { localHost } from "./adapters/local.js";
84
88
  import { cloudComputer } from "./collections/cloud-computer.js";
89
+ import { markProcessor } from "./processors/mark.js";
85
90
  import { poolVm } from "./collections/pool-vm.js";
91
+ import { poolWant } from "./collections/pool-want.js";
86
92
  import { POOL, POOL_ACTIONS, POOL_KIND } from "./contract.js";
87
93
  import { personOf } from "./shared/people.js";
88
94
  import { DEFAULTS, settingsOf } from "./shared/settings.js";
@@ -116,9 +122,12 @@ export const cloud = definePlugin({
116
122
  "cloud.destroy": destroyAction,
117
123
  "cloud.touch": touchAction,
118
124
  "cloud.org": orgAction,
125
+ "cloud.may": mayAction,
119
126
  "cloud.tick": tickAction,
120
127
  "cloud.daily": dailyAction,
121
128
  },
129
+ // Its desks show how it is, to everyone in their spaces.
130
+ processors: { "cloud.mark": markProcessor },
122
131
  schedules: {
123
132
  "cloud.tick": { every: "1m", call: "cloud.tick", input: {}, retries: 0 },
124
133
  "cloud.daily": { cron: "30 3 * * *", call: "cloud.daily", input: {} },
@@ -140,7 +149,7 @@ export const cloud = definePlugin({
140
149
  /** The pools of VMs cloud computers wake on, where pool VMs connect: see the module's doc. */
141
150
  export const cloudPool = definePlugin({
142
151
  name: "cloud-pool",
143
- collections: { poolVm },
152
+ collections: { poolVm, poolWant },
144
153
  workers: {
145
154
  [POOL_KIND]: {
146
155
  admit: "pool.admit",
@@ -168,7 +177,7 @@ export const cloudPool = definePlugin({
168
177
  resources: { cloud: {} },
169
178
  adapters: { cloudHost: hosts },
170
179
  // Pool VMs: only the system.
171
- guard: (_actor, { collection }) => collection === "poolVm" ? { _id: { $in: [] } } : undefined,
180
+ guard: (_actor, { collection }) => collection === "poolVm" || collection === "poolWant" ? { _id: { $in: [] } } : undefined,
172
181
  });
173
182
  /**
174
183
  * The pool's Actions, forwarded to the runtime that has `cloudPool` (`call`:
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Each cloud computer's state, as its desks show it: as a record is made, or
3
+ * its state or size changes, the computers plugin is told
4
+ * (`computer.mark`: `{ kind: "cloud", state, size }`), so a space's members
5
+ * see how anyone's cloud computer shared into it is (asleep, waking, awake),
6
+ * as its owner does. In order, per computer.
7
+ */
8
+ import type { ProcessorDef } from "../../../../runtime/mod.js";
9
+ export declare const markProcessor: ProcessorDef;
@@ -0,0 +1,13 @@
1
+ export const markProcessor = {
2
+ on: ["cloudComputer.created", "cloudComputer.updated"],
3
+ // A computer used (each few seconds) or metered changes neither: no job for it.
4
+ where: { $or: [{ changed: { $exists: false } }, { changed: { $in: ["state", "size"] } }] },
5
+ key: (event) => String(event.data._id),
6
+ run: async (ctx, event) => {
7
+ const doc = event.data.doc;
8
+ await ctx.call("computer.mark", {
9
+ computer: doc._id,
10
+ status: { kind: "cloud", state: doc.state, size: doc.size },
11
+ });
12
+ },
13
+ };