copilotz 1.0.0-rc.10 → 1.0.0-rc.12

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 (205) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/README.md +2 -2
  3. package/package.json +1 -1
  4. package/plugins/access/accounts/actions/complete.js +2 -2
  5. package/plugins/access/accounts/actions/connect.js +2 -2
  6. package/plugins/access/accounts/actions/disconnect.js +2 -2
  7. package/plugins/access/accounts/collections/account.d.ts +1 -1
  8. package/plugins/access/accounts/collections/account.js +1 -2
  9. package/plugins/access/accounts/collections/flow.d.ts +1 -1
  10. package/plugins/access/accounts/shared/settings.js +2 -2
  11. package/plugins/access/directory/actions/host.js +1 -2
  12. package/plugins/access/directory/actions/invite.js +1 -2
  13. package/plugins/access/directory/actions/org.js +1 -2
  14. package/plugins/access/identity/actions/key.js +3 -3
  15. package/plugins/access/identity/actions/session.js +2 -2
  16. package/plugins/access/identity/collections/credential.js +2 -2
  17. package/plugins/access/spaces/actions/agents.js +4 -7
  18. package/plugins/access/spaces/actions/archive.js +4 -5
  19. package/plugins/access/spaces/actions/can.js +4 -4
  20. package/plugins/access/spaces/actions/create.js +2 -2
  21. package/plugins/access/spaces/actions/invitation.js +8 -11
  22. package/plugins/access/spaces/actions/leave.js +2 -2
  23. package/plugins/access/spaces/actions/member.js +32 -37
  24. package/plugins/access/spaces/actions/org.js +12 -15
  25. package/plugins/access/spaces/actions/personal.js +2 -2
  26. package/plugins/access/spaces/actions/rename.js +4 -5
  27. package/plugins/access/spaces/actions/request.js +8 -13
  28. package/plugins/access/spaces/actions/share.js +2 -3
  29. package/plugins/access/spaces/collections/invitation.js +2 -2
  30. package/plugins/access/spaces/collections/request.js +2 -2
  31. package/plugins/access/spaces/collections/space.js +31 -4
  32. package/plugins/access/spaces/contract.d.ts +2 -0
  33. package/plugins/access/spaces/contract.js +2 -0
  34. package/plugins/access/spaces/shared/can.d.ts +12 -0
  35. package/plugins/access/spaces/shared/can.js +24 -0
  36. package/plugins/access/spaces/shared/managers.js +1 -1
  37. package/plugins/access/spaces/shared/reach.d.ts +11 -1
  38. package/plugins/access/spaces/shared/reach.js +28 -3
  39. package/plugins/access/spaces/shared/rules.js +4 -2
  40. package/plugins/access/spaces/shared/seat.d.ts +9 -0
  41. package/plugins/access/spaces/shared/seat.js +22 -0
  42. package/plugins/access/spaces/views/elsewhere.d.ts +2 -1
  43. package/plugins/access/spaces/views/elsewhere.js +25 -7
  44. package/plugins/agents/chat/actions/fork.js +2 -2
  45. package/plugins/agents/chat/actions/move.js +2 -2
  46. package/plugins/agents/chat/actions/send.js +2 -2
  47. package/plugins/agents/chat/actions/stop.js +9 -2
  48. package/plugins/agents/chat/collections/file.js +2 -2
  49. package/plugins/agents/chat/collections/message.d.ts +44 -0
  50. package/plugins/agents/chat/collections/message.js +12 -3
  51. package/plugins/agents/chat/collections/thread.js +3 -3
  52. package/plugins/agents/chat/contract.d.ts +1 -0
  53. package/plugins/agents/chat/contract.js +1 -0
  54. package/plugins/agents/chat/processors/turn.js +12 -7
  55. package/plugins/agents/chat/shared/delivery.d.ts +3 -0
  56. package/plugins/agents/chat/shared/delivery.js +31 -5
  57. package/plugins/agents/chat/shared/origin.d.ts +22 -0
  58. package/plugins/agents/chat/shared/origin.js +11 -0
  59. package/plugins/agents/chat/shared/send.d.ts +5 -1
  60. package/plugins/agents/chat/shared/send.js +89 -3
  61. package/plugins/agents/chat/shared/types.d.ts +2 -0
  62. package/plugins/agents/chat/shared/types.js +3 -0
  63. package/plugins/agents/chat/views/message.d.ts +2 -1
  64. package/plugins/agents/chat/views/message.js +6 -3
  65. package/plugins/agents/chat/views/web/conversation.js +4 -1
  66. package/plugins/agents/chat/views/web/message.js +5 -1
  67. package/plugins/agents/memory/collections/memory.js +2 -2
  68. package/plugins/agents/memory/mod.js +2 -2
  69. package/plugins/agents/routines/actions/create.js +7 -9
  70. package/plugins/agents/routines/actions/delete.js +2 -2
  71. package/plugins/agents/routines/actions/origin.d.ts +1 -0
  72. package/plugins/agents/routines/actions/origin.js +32 -0
  73. package/plugins/agents/routines/actions/pause.js +2 -2
  74. package/plugins/agents/routines/actions/resume.js +2 -2
  75. package/plugins/agents/routines/actions/run.js +4 -4
  76. package/plugins/agents/routines/actions/update.js +12 -9
  77. package/plugins/agents/routines/collections/routine.d.ts +6 -0
  78. package/plugins/agents/routines/collections/routine.js +7 -4
  79. package/plugins/agents/routines/contract.d.ts +7 -1
  80. package/plugins/agents/routines/mod.d.ts +4 -0
  81. package/plugins/agents/routines/mod.js +8 -0
  82. package/plugins/agents/routines/resources/tools.js +9 -5
  83. package/plugins/agents/routines/shared/fire.d.ts +2 -0
  84. package/plugins/agents/routines/shared/fire.js +16 -8
  85. package/plugins/agents/routines/shared/routines.d.ts +3 -8
  86. package/plugins/agents/routines/shared/routines.js +5 -18
  87. package/plugins/agents/routines/shared/schedule.js +1 -1
  88. package/plugins/agents/routines/views/web/page.d.ts +2 -1
  89. package/plugins/agents/routines/views/web/page.js +10 -5
  90. package/plugins/agents/skills/actions/create.js +2 -2
  91. package/plugins/agents/skills/actions/delete.js +2 -2
  92. package/plugins/agents/skills/actions/update.js +2 -2
  93. package/plugins/agents/skills/collections/skill.js +4 -4
  94. package/plugins/agents/skills/shared/write.d.ts +2 -3
  95. package/plugins/agents/skills/shared/write.js +2 -1
  96. package/plugins/agents/team/actions/agent.js +5 -6
  97. package/plugins/agents/team/collections/agent.js +3 -3
  98. package/plugins/billing/usage/actions/org.js +6 -6
  99. package/plugins/billing/usage/actions/quote.js +2 -2
  100. package/plugins/billing/usage/collections/usage.js +2 -2
  101. package/plugins/billing/usage/shared/use.d.ts +0 -1
  102. package/plugins/billing/usage/shared/use.js +1 -8
  103. package/plugins/compute/apps/actions/configure.js +4 -6
  104. package/plugins/compute/apps/actions/publish.js +5 -4
  105. package/plugins/compute/apps/actions/redeploy.js +5 -6
  106. package/plugins/compute/apps/actions/remove.js +4 -6
  107. package/plugins/compute/apps/collections/app.d.ts +1 -1
  108. package/plugins/compute/apps/collections/app.js +2 -2
  109. package/plugins/compute/apps/resources/tools.js +7 -5
  110. package/plugins/compute/apps/shared/publish.d.ts +1 -5
  111. package/plugins/compute/apps/shared/publish.js +4 -12
  112. package/plugins/compute/cloud/actions/create.js +2 -2
  113. package/plugins/compute/cloud/actions/destroy.js +3 -3
  114. package/plugins/compute/cloud/actions/may.js +2 -2
  115. package/plugins/compute/cloud/actions/org.js +2 -2
  116. package/plugins/compute/cloud/collections/cloud-computer.js +3 -3
  117. package/plugins/compute/cloud/mod.js +1 -2
  118. package/plugins/compute/cloud/shared/host.js +1 -1
  119. package/plugins/compute/cloud/shared/people.d.ts +4 -9
  120. package/plugins/compute/cloud/shared/people.js +5 -12
  121. package/plugins/compute/computers/actions/allow-origin.d.ts +5 -0
  122. package/plugins/compute/computers/actions/allow-origin.js +48 -0
  123. package/plugins/compute/computers/actions/approve.js +18 -10
  124. package/plugins/compute/computers/actions/code.js +4 -4
  125. package/plugins/compute/computers/actions/default.js +4 -8
  126. package/plugins/compute/computers/actions/env.js +5 -7
  127. package/plugins/compute/computers/actions/forget-grant.d.ts +1 -0
  128. package/plugins/compute/computers/actions/forget-grant.js +16 -0
  129. package/plugins/compute/computers/actions/forget.js +2 -2
  130. package/plugins/compute/computers/actions/move.js +5 -4
  131. package/plugins/compute/computers/actions/preview.js +7 -10
  132. package/plugins/compute/computers/actions/provision.js +3 -3
  133. package/plugins/compute/computers/actions/rename.js +2 -2
  134. package/plugins/compute/computers/actions/share.js +5 -4
  135. package/plugins/compute/computers/actions/unshare.js +5 -8
  136. package/plugins/compute/computers/actions/view.js +2 -2
  137. package/plugins/compute/computers/actions/work.js +8 -4
  138. package/plugins/compute/computers/collections/approval.d.ts +57 -5
  139. package/plugins/compute/computers/collections/approval.js +16 -4
  140. package/plugins/compute/computers/collections/computer.js +2 -2
  141. package/plugins/compute/computers/collections/desk.js +3 -3
  142. package/plugins/compute/computers/collections/env-var.js +3 -3
  143. package/plugins/compute/computers/collections/grant.d.ts +20 -3
  144. package/plugins/compute/computers/collections/grant.js +21 -5
  145. package/plugins/compute/computers/collections/preview.d.ts +1 -1
  146. package/plugins/compute/computers/collections/preview.js +2 -2
  147. package/plugins/compute/computers/mod.js +7 -2
  148. package/plugins/compute/computers/processors/left.d.ts +8 -0
  149. package/plugins/compute/computers/processors/left.js +31 -0
  150. package/plugins/compute/computers/program/version.d.ts +1 -1
  151. package/plugins/compute/computers/program/version.js +1 -1
  152. package/plugins/compute/computers/resources/commands.js +13 -5
  153. package/plugins/compute/computers/resources/downloads.js +1 -1
  154. package/plugins/compute/computers/resources/previews.js +7 -2
  155. package/plugins/compute/computers/shared/ask.d.ts +4 -2
  156. package/plugins/compute/computers/shared/ask.js +16 -5
  157. package/plugins/compute/computers/shared/desks.d.ts +0 -4
  158. package/plugins/compute/computers/shared/desks.js +0 -11
  159. package/plugins/compute/computers/shared/grants.d.ts +26 -0
  160. package/plugins/compute/computers/shared/grants.js +49 -0
  161. package/plugins/compute/computers/shared/where.d.ts +2 -0
  162. package/plugins/compute/computers/shared/where.js +4 -2
  163. package/plugins/compute/computers/views/context.js +4 -4
  164. package/plugins/compute/computers/views/web/allowed.d.ts +16 -0
  165. package/plugins/compute/computers/views/web/allowed.js +38 -0
  166. package/plugins/compute/computers/views/web/asked.d.ts +7 -2
  167. package/plugins/compute/computers/views/web/asked.js +5 -1
  168. package/plugins/compute/computers/views/web/asks.d.ts +3 -1
  169. package/plugins/compute/computers/views/web/asks.js +6 -2
  170. package/plugins/compute/computers/views/web/routine-panel.d.ts +14 -0
  171. package/plugins/compute/computers/views/web/routine-panel.js +91 -0
  172. package/plugins/compute/computers/views/web/space-tab.js +4 -1
  173. package/plugins/compute/computers/views/web/style.d.ts +1 -1
  174. package/plugins/compute/computers/views/web/style.js +21 -0
  175. package/plugins/compute/computers/web.d.ts +4 -3
  176. package/plugins/compute/computers/web.js +8 -3
  177. package/plugins/interfaces/api/actions/create.js +2 -3
  178. package/plugins/interfaces/api/actions/revoke.js +2 -2
  179. package/plugins/interfaces/api/collections/key.d.ts +1 -1
  180. package/plugins/interfaces/api/collections/key.js +3 -3
  181. package/plugins/interfaces/api/mod.js +2 -0
  182. package/plugins/interfaces/api/processors/left.d.ts +6 -0
  183. package/plugins/interfaces/api/processors/left.js +12 -0
  184. package/plugins/interfaces/api/shared/actor.d.ts +3 -11
  185. package/plugins/interfaces/api/shared/actor.js +1 -23
  186. package/plugins/interfaces/api/shared/events.js +2 -2
  187. package/plugins/interfaces/api/shared/rules.d.ts +3 -10
  188. package/plugins/interfaces/api/shared/rules.js +8 -24
  189. package/plugins/interfaces/api/shared/server.js +1 -1
  190. package/plugins/interfaces/whatsapp/actions/connect.js +8 -10
  191. package/plugins/interfaces/whatsapp/actions/disconnect.js +3 -3
  192. package/plugins/interfaces/whatsapp/actions/link.js +10 -13
  193. package/plugins/interfaces/whatsapp/collections/phone.d.ts +1 -1
  194. package/plugins/interfaces/whatsapp/processors/receive.js +3 -10
  195. package/plugins/interfaces/whatsapp/shared/rules.d.ts +1 -3
  196. package/plugins/interfaces/whatsapp/shared/rules.js +1 -9
  197. package/plugins/models/shared/choice.js +1 -1
  198. package/plugins/models/shared/generate.js +1 -1
  199. package/runtime/actor.d.ts +9 -0
  200. package/{plugins/compute/computers/shared/person.js → runtime/actor.js} +1 -0
  201. package/runtime/errors.d.ts +18 -0
  202. package/runtime/errors.js +7 -0
  203. package/runtime/mod.d.ts +2 -0
  204. package/runtime/mod.js +2 -0
  205. package/plugins/compute/computers/shared/person.d.ts +0 -3
package/CHANGELOG.md CHANGED
@@ -1,3 +1,57 @@
1
+ ## 1.0.0-rc.12 — 2026-11-01
2
+
3
+ - **An agent can hand work to itself in another space.** "Itself" now means the same agent in the
4
+ same chat: North in Personal and North in Project management are two pieces of work. Whoever a
5
+ space says answers takes what's handed there, even the agent that sent it, instead of being
6
+ skipped for someone else, so a space where only that agent answers can receive work too. It reads
7
+ what it handed over, and the answer back, as input (`You (via delegated "Solo"): …`), not as
8
+ something it said.
9
+ - **Delegated messages name the other space:** "Delegated: Personal" there, "Delegated: Project
10
+ management" back, with an icon of their own.
11
+
12
+ ## 1.0.0-rc.11 — 2026-10-31
13
+
14
+ - **A routine can run with nobody watching.** What its agent needs a yes for can now be allowed for
15
+ that routine alone: "Allow for this routine" on the card, or "What it can do without asking" on
16
+ its page (run commands, start jobs, change files, per computer), ahead of its first run. Allowing
17
+ it answers every run already stopped on the same thing. What a run waits on shows on the routine's
18
+ page, with the way to its chat. In the terminal: `/approve <n> routine`.
19
+ - **That yes covers only that routine's runs, as it was.** A run's work carries the routine as its
20
+ `origin`, on the actor (`originOf`, chat's contract), which only code can set: a person's own
21
+ message to the same agent still asks, even one claiming `via` a routine. Changing what a routine
22
+ does (what it asks, whom, as whom) makes a new version (`routine.rev`), which the yes doesn't
23
+ cover: its page says so and offers "Allow again". Name and schedule don't count. Where a
24
+ computer's owner keeps the answers, only they can say it (`computer.allowOrigin`).
25
+ - **Plugins can add to a routine's page:** the `routinePanels` resource (`{ view, order? }`), a View
26
+ given the routine.
27
+ - **Authorization is one model.** `personOf`, `forbidden`, `invalid`, `failure`, `people` and
28
+ `inPerson` come from the runtime; `may`, `must` and `mustOwnOr` from the spaces contract,
29
+ replacing seven `personOf` copies, about 25 hand-written checks and about 30 local error helpers.
30
+ `reachOf` is the only answer to what someone may do, and respects every limit on them.
31
+ - **Fixed: taking over a space.** A thread named after a space could be used to become its manager.
32
+ A document's memberships now live under its own name (`note:n1`), and only shared collections can
33
+ be named.
34
+ - **Fixed: keys left their space.** Actions that checked reach, then wrote as the system, ignored a
35
+ key's limit. Reach respects it now (`within`, which replaces `at` on a key's actor), and the API's
36
+ own narrowing rules are gone. Keys can't answer approvals.
37
+ - **Fixed: agents asked by agents ran as the agent that asked.** A turn is always its own agent, for
38
+ the person the work is for, including where a computer's owner answered.
39
+ - **Publishing is asked every time.** Nothing wider than once is offered or honored.
40
+ - **Approvals can be taken back,** from the Computer tab. What's allowed for a routine comes from
41
+ the routine (`origins` resource), not from what the caller says.
42
+ - **Roles are given one way** (`seat`): a granted request can't name a role that doesn't exist or
43
+ make an agent a manager, and custom roles count everywhere.
44
+ - **An archived space isn't written in,** which only views enforced before.
45
+ - **Replacing or rebuilding an app** takes its publisher or a manager. **Where a computer's owner
46
+ answers,** only they change anything on it directly; others still read it.
47
+ - **Leaving ends what someone left behind,** however they left: desks, yeses and keys for that
48
+ space, and out of the org, all of them and their own computers. Cloud computers stay for those who
49
+ run the org. A preview's cookie asks whether its person is still a member on each request.
50
+ - **Agents can hand work to other spaces.** `send_message` takes `space`: any space the person can
51
+ write in, listed in the agent's context with who answers there. A new chat opens there, visible to
52
+ its members and marked as delegated, for the same person; the answer comes back to the chat it
53
+ came from, and stopping that chat stops the work. An agent can't hand work to itself.
54
+
1
55
  ## 1.0.0-rc.10 — 2026-10-31
2
56
 
3
57
  - **A routine made from a chat posts back in that chat.** `schedule_routine` keeps the chat it was
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.10"`),
25
+ On Deno, name it `copilotz` in `deno.json` (`"copilotz": "jsr:@copilotz/copilotz@^1.0.0-rc.12"`),
26
26
  and the imports below are the same everywhere. Some pieces need a package of their own, installed
27
27
  beside it: `esbuild` to bundle the web host on Node, `@electric-sql/pglite` for PostgreSQL in the
28
28
  process, 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.10" })], // its own: /computer/download/
207
+ plugins: [...plugins, await computerDownloads({ dir: "dist/computer/1.0.0-rc.12" })], // 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.10",
3
+ "version": "1.0.0-rc.12",
4
4
  "description": "Copilotz: one runtime for Node, Deno, Bun, Cloudflare Workers and the browser.",
5
5
  "keywords": [
6
6
  "agents",
@@ -5,7 +5,7 @@
5
5
  * which must be the person's own sign-in, there, in time. Unrecorded: the
6
6
  * code stays out of the log. It answers the account, as its person sees it.
7
7
  */
8
- import { defineAction } from "../../../../runtime/mod.js";
8
+ import { defineAction, people } from "../../../../runtime/mod.js";
9
9
  import { invalid } from "../shared/errors.js";
10
10
  import { finish, where } from "../shared/flow.js";
11
11
  import { providerOf } from "../shared/settings.js";
@@ -18,7 +18,7 @@ export const completeAction = defineAction({
18
18
  required: ["provider", "address"],
19
19
  additionalProperties: false,
20
20
  },
21
- allow: (actor) => actor.kind === "person",
21
+ allow: people,
22
22
  run: (ctx, input) => {
23
23
  const { title } = providerOf(ctx, input.provider);
24
24
  let url;
@@ -7,7 +7,7 @@
7
7
  * Unrecorded: its answer carries the sign-in's state, which stays out of
8
8
  * the log.
9
9
  */
10
- import { defineAction } from "../../../../runtime/mod.js";
10
+ import { defineAction, people } from "../../../../runtime/mod.js";
11
11
  import { start } from "../shared/flow.js";
12
12
  export const connectAction = defineAction({
13
13
  description: "Starts connecting one of your accounts: where to sign in",
@@ -18,6 +18,6 @@ export const connectAction = defineAction({
18
18
  required: ["provider"],
19
19
  additionalProperties: false,
20
20
  },
21
- allow: (actor) => actor.kind === "person",
21
+ allow: people,
22
22
  run: (ctx, input) => start(ctx, input.provider, input.origin),
23
23
  });
@@ -5,7 +5,7 @@
5
5
  * timer, saying `account.changed`. Agents working for them then use the
6
6
  * app's own models, where it has them.
7
7
  */
8
- import { defineAction } from "../../../../runtime/mod.js";
8
+ import { defineAction, people } from "../../../../runtime/mod.js";
9
9
  import { revoke } from "../shared/oauth.js";
10
10
  import { unseal } from "../shared/seal.js";
11
11
  import { accountId, providerOf, settingsOf, timerOf } from "../shared/settings.js";
@@ -17,7 +17,7 @@ export const disconnectAction = defineAction({
17
17
  required: ["provider"],
18
18
  additionalProperties: false,
19
19
  },
20
- allow: (actor) => actor.kind === "person",
20
+ allow: people,
21
21
  run: async (ctx, input) => {
22
22
  const person = ctx.actor.id;
23
23
  const provider = providerOf(ctx, input.provider);
@@ -99,8 +99,8 @@ export declare const account: {
99
99
  plan?: string | undefined;
100
100
  refreshed?: string | undefined;
101
101
  status: "revoked" | "connected" | "needs_reconnect";
102
- provider: string;
103
102
  person: string;
103
+ provider: string;
104
104
  label: string;
105
105
  connected: string;
106
106
  } & {
@@ -10,7 +10,7 @@
10
10
  * Queries, for people: `account.providers`, what can be connected and how;
11
11
  * `account.uses`, which of some models would run on the asker's accounts.
12
12
  */
13
- import { defineCollection } from "../../../../runtime/mod.js";
13
+ import { defineCollection, people } from "../../../../runtime/mod.js";
14
14
  import { providersOf } from "../shared/settings.js";
15
15
  import { usesOf } from "../shared/uses.js";
16
16
  const accountSchema = {
@@ -30,7 +30,6 @@ const accountSchema = {
30
30
  },
31
31
  required: ["person", "provider", "status", "label", "connected"],
32
32
  };
33
- const people = (actor) => actor.kind === "person";
34
33
  export const account = defineCollection({
35
34
  schema: accountSchema,
36
35
  events: false,
@@ -56,8 +56,8 @@ export declare const accountFlow: {
56
56
  private?: string[];
57
57
  queries?: Record<string, import("../../../../runtime/define.js").QueryDef<{
58
58
  [x: string]: unknown;
59
- provider: string;
60
59
  person: string;
60
+ provider: string;
61
61
  expires: string;
62
62
  verifier: string;
63
63
  redirect: string;
@@ -1,3 +1,4 @@
1
+ import { personOf } from "../../../../runtime/mod.js";
1
2
  import { forbidden, invalid } from "./errors.js";
2
3
  export const settingsOf = (ctx) => (ctx.resources.accounts ?? {});
3
4
  /** The providers declared, by name, without those taken away (`null`). */
@@ -22,8 +23,7 @@ export const timerOf = (account) => `account.refresh:${account}`;
22
23
  export function personFor(actor, named) {
23
24
  if (!actor || actor.kind === "system")
24
25
  return named;
25
- const who = actor.kind === "agent" ? actor.for : actor;
26
- const person = who?.kind === "person" ? who.id : undefined;
26
+ const person = actor.kind === "person" || actor.kind === "agent" ? personOf(actor) : undefined;
27
27
  if (named !== undefined && named !== person) {
28
28
  throw forbidden("Only the system asks for another person's account");
29
29
  }
@@ -7,8 +7,7 @@
7
7
  * its target changes); refused when it's another org's;
8
8
  * - `host.release` `{ org, host }`: it's no longer the org's (if it was).
9
9
  */
10
- import { defineAction } from "../../../../runtime/mod.js";
11
- const forbidden = (message) => Object.assign(new Error(message), { code: "forbidden" });
10
+ import { defineAction, forbidden } from "../../../../runtime/mod.js";
12
11
  /** A host, as claims keep it: lowercase letters, digits, dots and hyphens, and a port maybe. */
13
12
  const host = { type: "string", pattern: "^[a-z0-9.-]+(:[0-9]{1,5})?$", maxLength: 253 };
14
13
  export const claimHostAction = defineAction({
@@ -4,8 +4,7 @@
4
4
  * theirs when they sign in, and accepting one makes them belong, with the
5
5
  * invitation's role for their first entering. All are the system's.
6
6
  */
7
- import { defineAction } from "../../../../runtime/mod.js";
8
- const forbidden = (message) => Object.assign(new Error(message), { code: "forbidden" });
7
+ import { defineAction, forbidden } from "../../../../runtime/mod.js";
9
8
  export const sendInviteAction = defineAction({
10
9
  description: "Invites an email to an org, with a role",
11
10
  input: {
@@ -11,9 +11,8 @@
11
11
  * - `org.leave`: someone no longer belongs (their org took them out);
12
12
  * - `org.suspend`, `org.rename`, `org.report` (its summary), `org.list`.
13
13
  */
14
- import { defineAction } from "../../../../runtime/mod.js";
14
+ import { defineAction, forbidden } from "../../../../runtime/mod.js";
15
15
  import { isStaff, placementOf } from "../shared/settings.js";
16
- const forbidden = (message) => Object.assign(new Error(message), { code: "forbidden" });
17
16
  const person = { type: "string" };
18
17
  export const createOrgAction = defineAction({
19
18
  description: "Makes an org (personal or a team's); its maker belongs as its owner",
@@ -2,7 +2,7 @@
2
2
  * Keys: a person makes one for code that acts as them (a script, a CI job),
3
3
  * sees its secret once, and revokes it by name.
4
4
  */
5
- import { defineAction } from "../../../../runtime/mod.js";
5
+ import { defineAction, inPerson, people } from "../../../../runtime/mod.js";
6
6
  import { hash, secret } from "../collections/credential.js";
7
7
  export const createKey = defineAction({
8
8
  description: "Makes a key that acts as you; its secret is shown once",
@@ -14,7 +14,7 @@ export const createKey = defineAction({
14
14
  required: ["name"],
15
15
  additionalProperties: false,
16
16
  },
17
- allow: (actor) => actor.kind === "person" && !actor.key,
17
+ allow: inPerson,
18
18
  run: async (ctx, input) => {
19
19
  const made = secret("key");
20
20
  await ctx.as().db.collection("credential").insertOne({
@@ -36,7 +36,7 @@ export const revokeKey = defineAction({
36
36
  required: ["name"],
37
37
  additionalProperties: false,
38
38
  },
39
- allow: (actor) => actor.kind === "person",
39
+ allow: people,
40
40
  run: async (ctx, input) => {
41
41
  const removed = await ctx.as().db.collection("credential").deleteMany({
42
42
  person: ctx.actor.id,
@@ -3,7 +3,7 @@
3
3
  * verified (the system calls it: a sign-in plugin's Action, never a person);
4
4
  * `session.end` ends the one a person shows.
5
5
  */
6
- import { defineAction } from "../../../../runtime/mod.js";
6
+ import { defineAction, people } from "../../../../runtime/mod.js";
7
7
  import { hash, secret } from "../collections/credential.js";
8
8
  /** How long a session lasts unless its sign-in says (30 days). */
9
9
  const LASTS = 30 * 86_400_000;
@@ -41,7 +41,7 @@ export const endSession = defineAction({
41
41
  required: ["session"],
42
42
  additionalProperties: false,
43
43
  },
44
- allow: (actor) => actor.kind === "person",
44
+ allow: people,
45
45
  run: async (ctx, input) => {
46
46
  const ended = await ctx.as().db.collection("credential").deleteOne({
47
47
  _id: await hash(input.session),
@@ -4,7 +4,7 @@
4
4
  * hash of each secret is kept: the secret is shown once, when it's made.
5
5
  * Deleting one revokes it.
6
6
  */
7
- import { defineCollection } from "../../../../runtime/mod.js";
7
+ import { defineCollection, people } from "../../../../runtime/mod.js";
8
8
  const credentialSchema = {
9
9
  type: "object",
10
10
  properties: {
@@ -43,7 +43,7 @@ export const credential = defineCollection({
43
43
  },
44
44
  /** A person's keys, without their secrets. */
45
45
  keys: {
46
- allow: (actor) => actor.kind === "person",
46
+ allow: people,
47
47
  run: (credentials, params) => credentials.find({ person: params.person, kind: "key" }, { sort: { at: 1 } }),
48
48
  },
49
49
  },
@@ -5,9 +5,9 @@
5
5
  * that are editors there, and that one. As the asker: a space they don't
6
6
  * reach has nobody for them.
7
7
  */
8
- import { defineAction } from "../../../../runtime/mod.js";
8
+ import { defineAction, people } from "../../../../runtime/mod.js";
9
9
  import { rosterIn } from "../shared/agents.js";
10
- import { reachOf } from "../shared/reach.js";
10
+ import { must } from "../shared/can.js";
11
11
  export const agentAction = defineAction({
12
12
  description: "Chooses the agent that answers a space's new chats: one working there",
13
13
  input: {
@@ -16,12 +16,9 @@ export const agentAction = defineAction({
16
16
  required: ["space", "agent"],
17
17
  additionalProperties: false,
18
18
  },
19
- allow: (actor) => actor.kind === "person",
19
+ allow: people,
20
20
  run: async (ctx, input) => {
21
- const reach = await reachOf(ctx.actor, ctx.as());
22
- if (!reach.get(input.space)?.can.has("manage")) {
23
- throw Object.assign(new Error(`You don't manage "${input.space}".`), { code: "forbidden" });
24
- }
21
+ await must(ctx, "manage", input.space, `You don't manage "${input.space}".`);
25
22
  const system = ctx.as();
26
23
  if (!(await rosterIn(system, input.space)).includes(input.agent)) {
27
24
  throw Object.assign(new Error(`"${input.agent}" doesn't answer in this space: add it as an editor first.`), { code: "invalid" });
@@ -5,9 +5,9 @@
5
5
  * they can read. Nothing in it changes, and it comes back as it was. A
6
6
  * Personal space can't be put away.
7
7
  */
8
- import { defineAction } from "../../../../runtime/mod.js";
8
+ import { defineAction, people } from "../../../../runtime/mod.js";
9
9
  import { PERSONAL } from "../shared/personal.js";
10
- import { reachOf } from "../shared/reach.js";
10
+ import { may } from "../shared/can.js";
11
11
  const refuse = (message, code = "forbidden") => Object.assign(new Error(message), { code });
12
12
  export const archiveAction = defineAction({
13
13
  description: "Archives a space you manage, or brings it back",
@@ -17,12 +17,11 @@ export const archiveAction = defineAction({
17
17
  required: ["space", "archived"],
18
18
  additionalProperties: false,
19
19
  },
20
- allow: (actor) => actor.kind === "person",
20
+ allow: people,
21
21
  run: async (ctx, input) => {
22
22
  const system = ctx.as();
23
23
  const found = await system.db.collection("space").findOne({ _id: input.space });
24
- const reach = await reachOf(ctx.actor, system);
25
- if (!found || !reach.get(input.space)?.can.has("manage")) {
24
+ if (!found || !await may(ctx, "manage", input.space)) {
26
25
  throw refuse(`You don't manage "${input.space}".`);
27
26
  }
28
27
  if (found.kind === PERSONAL)
@@ -5,7 +5,7 @@
5
5
  * only by name: computers check it before someone uses a space's computer.
6
6
  */
7
7
  import { defineAction } from "../../../../runtime/mod.js";
8
- import { reachOf } from "../shared/reach.js";
8
+ import { may } from "../shared/can.js";
9
9
  export const canAction = defineAction({
10
10
  description: "Whether you may read, write in or manage a space",
11
11
  input: {
@@ -17,10 +17,10 @@ export const canAction = defineAction({
17
17
  required: ["space", "verb"],
18
18
  additionalProperties: false,
19
19
  },
20
+ // A question, asked on every preview request: it records nothing.
21
+ record: false,
20
22
  allow: (actor) => actor.kind === "person" || actor.kind === "agent",
21
23
  run: async (ctx, input) => {
22
- // Reach is worked out from every membership, as the rules do.
23
- const reach = await reachOf(ctx.actor, ctx.as());
24
- return { can: reach.get(input.space)?.can.has(input.verb) ?? false };
24
+ return { can: await may(ctx, input.verb, input.space) };
25
25
  },
26
26
  });
@@ -2,7 +2,7 @@
2
2
  * `space.create`: a new space (shared by default), managed by whoever makes
3
3
  * it, with the app's default agents working there.
4
4
  */
5
- import { defineAction } from "../../../../runtime/mod.js";
5
+ import { defineAction, people } from "../../../../runtime/mod.js";
6
6
  import { seat } from "../shared/agents.js";
7
7
  import { memberKey, orgRoleOf } from "../shared/reach.js";
8
8
  export const createAction = defineAction({
@@ -13,7 +13,7 @@ export const createAction = defineAction({
13
13
  required: ["name"],
14
14
  additionalProperties: false,
15
15
  },
16
- allow: (actor) => actor.kind === "person",
16
+ allow: people,
17
17
  run: async (ctx, input) => {
18
18
  if (await orgRoleOf(ctx.actor, ctx.as()) === "guest") {
19
19
  throw Object.assign(new Error("Guests work in the spaces they're invited to."), {
@@ -5,9 +5,10 @@
5
5
  * in keeps their role) or turns it down. Its managers take one back the same
6
6
  * way. Sending answers the same whether the address has an account or not.
7
7
  */
8
- import { defineAction } from "../../../../runtime/mod.js";
8
+ import { defineAction, people } from "../../../../runtime/mod.js";
9
9
  import { joinable } from "../shared/personal.js";
10
- import { memberKey, orgRoleOf, reachOf, ROLES } from "../shared/reach.js";
10
+ import { may, must } from "../shared/can.js";
11
+ import { memberKey, orgRoleOf, ROLES } from "../shared/reach.js";
11
12
  import { namesFor } from "../shared/names.js";
12
13
  /** An email address, as invitations hold it. */
13
14
  const EMAIL = /^[^\s@]+@[^\s@]+$/;
@@ -20,8 +21,7 @@ async function answerable(ctx, id) {
20
21
  if (found) {
21
22
  if (found.email === await emailOf(ctx))
22
23
  return { ...found, mine: true };
23
- const reach = await reachOf(ctx.actor, ctx.as());
24
- if (reach.get(found.space)?.can.has("manage"))
24
+ if (await may(ctx, "manage", found.space))
25
25
  return { ...found, mine: false };
26
26
  }
27
27
  throw refused("forbidden", "No such invitation for you.");
@@ -38,7 +38,7 @@ export const sendInvitationAction = defineAction({
38
38
  required: ["space", "email"],
39
39
  additionalProperties: false,
40
40
  },
41
- allow: (actor) => actor.kind === "person",
41
+ allow: people,
42
42
  run: async (ctx, input) => {
43
43
  const email = input.email.trim().toLowerCase();
44
44
  if (!EMAIL.test(email))
@@ -47,10 +47,7 @@ export const sendInvitationAction = defineAction({
47
47
  if (!(role in { ...ROLES, ...(ctx.resources.roles ?? {}) })) {
48
48
  throw refused("invalid", `No role "${role}".`);
49
49
  }
50
- const reach = await reachOf(ctx.actor, ctx.as());
51
- if (!reach.get(input.space)?.can.has("manage")) {
52
- throw refused("forbidden", `You don't manage "${input.space}".`);
53
- }
50
+ await must(ctx, "manage", input.space, `You don't manage "${input.space}".`);
54
51
  const space = await ctx.as().db.collection("space").findOne({ _id: input.space });
55
52
  joinable(space, "person:");
56
53
  const invitation = `${input.space}|${email}`;
@@ -92,7 +89,7 @@ export const acceptInvitationAction = defineAction({
92
89
  required: ["invitation"],
93
90
  additionalProperties: false,
94
91
  },
95
- allow: (actor) => actor.kind === "person",
92
+ allow: people,
96
93
  run: async (ctx, input) => {
97
94
  const found = await answerable(ctx, input.invitation);
98
95
  if (!found.mine)
@@ -117,7 +114,7 @@ export const declineInvitationAction = defineAction({
117
114
  required: ["invitation"],
118
115
  additionalProperties: false,
119
116
  },
120
- allow: (actor) => actor.kind === "person",
117
+ allow: people,
121
118
  run: async (ctx, input) => {
122
119
  const found = await answerable(ctx, input.invitation);
123
120
  await ctx.as().db.collection("invitation").deleteOne({ _id: found._id });
@@ -3,7 +3,7 @@
3
3
  * which is yours; and not as its only manager: make someone else one first.
4
4
  * What you added there stays, the space's.
5
5
  */
6
- import { defineAction } from "../../../../runtime/mod.js";
6
+ import { defineAction, people } from "../../../../runtime/mod.js";
7
7
  import { keepsAManager } from "../shared/managers.js";
8
8
  import { PERSONAL } from "../shared/personal.js";
9
9
  import { memberKey } from "../shared/reach.js";
@@ -15,7 +15,7 @@ export const leaveAction = defineAction({
15
15
  required: ["space"],
16
16
  additionalProperties: false,
17
17
  },
18
- allow: (actor) => actor.kind === "person",
18
+ allow: people,
19
19
  run: async (ctx, input) => {
20
20
  const db = ctx.as().db;
21
21
  const found = await db.collection("space").findOne({ _id: input.space });
@@ -9,34 +9,37 @@
9
9
  * An agent joins at once, as a viewer or an editor, if the manager adding
10
10
  * it has met it (see agents.ts); taken out, it no longer answers there.
11
11
  */
12
- import { defineAction } from "../../../../runtime/mod.js";
12
+ import { defineAction, forbidden, people } from "../../../../runtime/mod.js";
13
13
  import { mustKnow, unseat } from "../shared/agents.js";
14
14
  import { keepsAManager } from "../shared/managers.js";
15
15
  import { joinable } from "../shared/personal.js";
16
- import { memberKey, ORG, reachOf } from "../shared/reach.js";
16
+ import { may, must } from "../shared/can.js";
17
+ import { checkRole, seat } from "../shared/seat.js";
18
+ import { docKey, memberKey, ORG } from "../shared/reach.js";
17
19
  const who = {
18
20
  type: "object",
19
21
  properties: { kind: { type: "string" }, id: { type: "string" } },
20
22
  required: ["kind", "id"],
21
23
  };
22
- const forbidden = (message) => Object.assign(new Error(message), { code: "forbidden" });
23
- /** Throws unless the caller manages `space` (or, for a document, the space it's in). */
24
+ /**
25
+ * Throws unless the caller manages `space` (or, for a document of a shared
26
+ * collection, it or the space it's in). Answers where its members are kept.
27
+ */
24
28
  async function manages(ctx, space, on) {
25
- // Reach is worked out from every membership, as the rules do.
26
- const reach = await reachOf(ctx.actor, ctx.as());
27
- let where = space;
28
- if (on) {
29
- const doc = await ctx.as().db.collection(on).findOne({ _id: space });
30
- const field = (ctx.resources.shared ?? {})[on]?.field ??
31
- "in";
32
- if (!doc)
33
- throw forbidden(`No ${on} "${space}".`);
34
- if (reach.get(space)?.can.has("manage"))
35
- return;
36
- where = String(doc[field]);
29
+ if (!on) {
30
+ await must(ctx, "manage", space, `You don't manage "${space}".`);
31
+ return space;
32
+ }
33
+ const shared = (ctx.resources.shared ?? {})[on];
34
+ const doc = shared ? await ctx.as().db.collection(on).findOne({ _id: space }) : null;
35
+ if (!doc)
36
+ throw forbidden(`No ${on} "${space}".`);
37
+ const key = docKey(on, space);
38
+ const home = String(doc[shared.field ?? "in"]);
39
+ if (!await may(ctx, "manage", key)) {
40
+ await must(ctx, "manage", home, `You don't manage "${home}".`);
37
41
  }
38
- if (!reach.get(where)?.can.has("manage"))
39
- throw forbidden(`You don't manage "${where}".`);
42
+ return key;
40
43
  }
41
44
  export const setMemberAction = defineAction({
42
45
  description: "Makes someone a member of a space, with a role",
@@ -51,34 +54,26 @@ export const setMemberAction = defineAction({
51
54
  required: ["space", "member", "role"],
52
55
  additionalProperties: false,
53
56
  },
54
- allow: (actor) => actor.kind === "person",
57
+ allow: people,
55
58
  run: async (ctx, input) => {
56
59
  if (input.space === ORG)
57
60
  throw forbidden("Org roles change with org.role.");
58
- await manages(ctx, input.space, input.on);
61
+ const place = await manages(ctx, input.space, input.on);
59
62
  const key = memberKey(input.member);
63
+ checkRole(ctx, key, input.role);
60
64
  if (!input.on) {
61
65
  joinable(await ctx.as().db.collection("space").findOne({ _id: input.space }), key);
62
- if (input.member.kind === "agent") {
63
- if (input.role === "manager") {
64
- throw Object.assign(new Error("Agents work here as viewers or editors."), {
65
- code: "invalid",
66
- });
67
- }
66
+ if (input.member.kind === "agent")
68
67
  await mustKnow(ctx, input.member.id);
69
- }
70
68
  if (input.member.kind === "person" &&
71
69
  !await ctx.as().db.collection("member").findOne({ _id: `${input.space}|${key}` })) {
72
70
  throw Object.assign(new Error("People join by invitation: invite them by email, and they join once they accept."), { code: "invalid" });
73
71
  }
74
- await keepsAManager(ctx, input.space, key, input.role);
72
+ await seat(ctx, place, key, input.role);
73
+ }
74
+ else {
75
+ await ctx.as().db.collection("member").replaceOne({ _id: `${place}|${key}` }, { member: key, space: place, role: input.role, on: input.on }, { upsert: true });
75
76
  }
76
- await ctx.as().db.collection("member").replaceOne({ _id: `${input.space}|${key}` }, {
77
- member: key,
78
- space: input.space,
79
- role: input.role,
80
- ...(input.on ? { on: input.on } : {}),
81
- }, { upsert: true });
82
77
  // An agent made a viewer answers there no more.
83
78
  if (!input.on && input.member.kind === "agent") {
84
79
  await unseat(ctx.as(), input.member.id, [input.space]);
@@ -93,15 +88,15 @@ export const removeMemberAction = defineAction({
93
88
  required: ["space", "member"],
94
89
  additionalProperties: false,
95
90
  },
96
- allow: (actor) => actor.kind === "person",
91
+ allow: people,
97
92
  run: async (ctx, input) => {
98
93
  if (input.space === ORG)
99
94
  throw forbidden("People leave the org with org.remove.");
100
- await manages(ctx, input.space, input.on);
95
+ const place = await manages(ctx, input.space, input.on);
101
96
  if (!input.on)
102
97
  await keepsAManager(ctx, input.space, memberKey(input.member));
103
98
  await ctx.as().db.collection("member").deleteOne({
104
- _id: `${input.space}|${memberKey(input.member)}`,
99
+ _id: `${place}|${memberKey(input.member)}`,
105
100
  });
106
101
  if (!input.on && input.member.kind === "agent") {
107
102
  await unseat(ctx.as(), input.member.id, [input.space]);