@gjermundgaraba/effect-actions 0.22.0 → 0.23.0

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 (271) hide show
  1. package/README.md +7 -1
  2. package/dist/Action.d.mts +2 -2
  3. package/dist/Action.mjs +1 -0
  4. package/dist/ActionCli.mjs +0 -1
  5. package/dist/ActionHttp.d.mts +1 -1
  6. package/dist/ActionMcp.d.mts +0 -3
  7. package/dist/ActionMcp.mjs +0 -5
  8. package/dist/ActionRpc.d.mts +8 -15
  9. package/dist/ActionRpc.mjs +29 -54
  10. package/dist/ActionToolkit.d.mts +0 -2
  11. package/dist/ActionToolkit.mjs +0 -2
  12. package/dist/Event.d.mts +0 -23
  13. package/dist/Testing.mjs +3 -2
  14. package/dist/Webhook.mjs +2 -2
  15. package/dist/authentication/refusal.mjs +0 -1
  16. package/dist/cli/commands.mjs +0 -5
  17. package/dist/contract/authorizer.mjs +0 -5
  18. package/dist/contract/call.mjs +0 -23
  19. package/dist/contract/callback.mjs +0 -1
  20. package/dist/contract/caller.mjs +0 -11
  21. package/dist/contract/canonical.mjs +0 -5
  22. package/dist/contract/codec.d.mts +0 -1
  23. package/dist/contract/codec.mjs +0 -1
  24. package/dist/contract/codes.mjs +1 -15
  25. package/dist/contract/declared.mjs +0 -15
  26. package/dist/contract/digest.mjs +0 -1
  27. package/dist/contract/errors.d.mts +0 -4
  28. package/dist/contract/errors.mjs +0 -4
  29. package/dist/contract/feed.d.mts +0 -1
  30. package/dist/contract/feed.mjs +0 -20
  31. package/dist/contract/hints.mjs +0 -6
  32. package/dist/contract/hub.mjs +0 -3
  33. package/dist/contract/idempotency.mjs +0 -4
  34. package/dist/contract/implementation.d.mts +0 -1
  35. package/dist/contract/implementation.mjs +0 -27
  36. package/dist/contract/input-check.mjs +0 -1
  37. package/dist/contract/lease.mjs +0 -1
  38. package/dist/contract/listed.mjs +0 -6
  39. package/dist/contract/log.mjs +0 -8
  40. package/dist/contract/names.mjs +0 -1
  41. package/dist/contract/occurrence.mjs +0 -8
  42. package/dist/contract/pages.mjs +5 -12
  43. package/dist/contract/progress.mjs +2 -25
  44. package/dist/contract/push.d.mts +0 -5
  45. package/dist/contract/push.mjs +1 -24
  46. package/dist/contract/receiving.mjs +0 -7
  47. package/dist/contract/reported.mjs +0 -5
  48. package/dist/contract/serving.mjs +0 -18
  49. package/dist/contract/status.mjs +0 -3
  50. package/dist/contract/stream.mjs +0 -2
  51. package/dist/contract/subscribe.d.mts +0 -2
  52. package/dist/contract/subscribe.mjs +0 -8
  53. package/dist/contract/transient.mjs +0 -12
  54. package/dist/contract/watch.mjs +5 -17
  55. package/dist/contract/webhooks.mjs +0 -11
  56. package/dist/http/client.mjs +0 -1
  57. package/dist/http/events.d.mts +0 -10
  58. package/dist/http/events.mjs +2 -31
  59. package/dist/http/placed.d.mts +0 -2
  60. package/dist/http/streamed.d.mts +0 -1
  61. package/dist/http/streamed.mjs +4 -32
  62. package/dist/http/watch.mjs +0 -6
  63. package/dist/mcp/bridge.mjs +0 -25
  64. package/dist/mcp/client.d.mts +0 -1
  65. package/dist/mcp/client.mjs +0 -1
  66. package/dist/mcp/events.mjs +0 -6
  67. package/dist/mcp/progress.mjs +0 -7
  68. package/dist/mcp/protocol.mjs +0 -6
  69. package/dist/mcp/reply.mjs +0 -11
  70. package/dist/mcp/request.mjs +0 -1
  71. package/dist/mcp/watch.mjs +0 -6
  72. package/dist/rpc/watch.mjs +0 -6
  73. package/dist/stdio/console.mjs +2 -2
  74. package/dist/webhook/address.mjs +4 -9
  75. package/dist/webhook/attempt.mjs +2 -15
  76. package/dist/webhook/receiver.mjs +2 -8
  77. package/dist/webhook/signing.mjs +0 -3
  78. package/dist/webhook/stored.mjs +8 -17
  79. package/dist/webhook/subscriptions.mjs +19 -23
  80. package/docs/Action-streams.md +0 -12
  81. package/docs/Action.md +0 -10
  82. package/docs/ActionCli.md +2 -26
  83. package/docs/ActionHttp.md +0 -28
  84. package/docs/ActionMcp.md +2 -37
  85. package/docs/ActionRpc-events.md +0 -16
  86. package/docs/ActionRpc.md +0 -63
  87. package/docs/ActionToolkit.md +0 -11
  88. package/docs/Authentication-advanced.md +0 -31
  89. package/docs/Authentication.md +4 -13
  90. package/docs/Event.md +0 -37
  91. package/docs/README.md +1 -1
  92. package/docs/Testing.md +3 -13
  93. package/docs/Webhook.md +0 -46
  94. package/docs/setup.md +0 -5
  95. package/examples/README.md +7 -1
  96. package/examples/assistant/src/agent.ts +8 -31
  97. package/examples/assistant/src/cli.ts +2 -9
  98. package/examples/assistant/src/commands.ts +0 -8
  99. package/examples/assistant/src/identity/Identity.ts +0 -10
  100. package/examples/assistant/src/identity/authorize.ts +0 -4
  101. package/examples/assistant/src/identity/members.ts +1 -3
  102. package/examples/assistant/src/identity/permissions.ts +0 -5
  103. package/examples/assistant/src/platform/tasks-file.ts +1 -6
  104. package/examples/assistant/src/scripted.ts +0 -4
  105. package/examples/assistant/src/tasks/Tasks.ts +0 -2
  106. package/examples/assistant/src/tasks/handlers.ts +0 -3
  107. package/examples/assistant/src/tasks/store.ts +0 -7
  108. package/examples/assistant/src/toolkit.ts +0 -11
  109. package/examples/assistant/test/commands.test.ts +0 -5
  110. package/examples/assistant/test/identity/identity.test.ts +6 -6
  111. package/examples/assistant/test/platform/tasks-file.test.ts +2 -6
  112. package/examples/assistant/test/tasks/tasks.test.ts +13 -14
  113. package/examples/assistant/test/toolkit.test.ts +7 -18
  114. package/examples/build-events/README.md +2 -0
  115. package/examples/build-events/src/Api.ts +0 -3
  116. package/examples/build-events/src/Rpc.ts +0 -2
  117. package/examples/build-events/src/builds/Builds.ts +0 -15
  118. package/examples/build-events/src/builds/events.ts +0 -10
  119. package/examples/build-events/src/builds/handlers.ts +0 -6
  120. package/examples/build-events/src/builds/log.ts +0 -6
  121. package/examples/build-events/src/builds/status.ts +0 -4
  122. package/examples/build-events/src/builds/store.ts +0 -21
  123. package/examples/build-events/src/cli.ts +0 -2
  124. package/examples/build-events/src/commands.ts +0 -9
  125. package/examples/build-events/src/deploys/approvers.ts +0 -3
  126. package/examples/build-events/src/deploys/events.ts +0 -8
  127. package/examples/build-events/src/deploys/handlers.ts +0 -2
  128. package/examples/build-events/src/identity/Identity.ts +0 -11
  129. package/examples/build-events/src/identity/members.ts +1 -2
  130. package/examples/build-events/src/identity/verifier.ts +8 -9
  131. package/examples/build-events/src/notifier/Notices.ts +0 -3
  132. package/examples/build-events/src/notifier/routes.ts +0 -10
  133. package/examples/build-events/src/notifier.ts +0 -8
  134. package/examples/build-events/src/routes.ts +0 -24
  135. package/examples/build-events/src/server.ts +0 -14
  136. package/examples/build-events/test/builds/events.test.ts +0 -6
  137. package/examples/build-events/test/builds/log.test.ts +0 -1
  138. package/examples/build-events/test/builds/progress.test.ts +0 -1
  139. package/examples/build-events/test/commands.test.ts +0 -5
  140. package/examples/build-events/test/deploys/events.test.ts +0 -1
  141. package/examples/build-events/test/http.test.ts +0 -4
  142. package/examples/build-events/test/notifier.test.ts +0 -9
  143. package/examples/build-events/test/routes.test.ts +0 -9
  144. package/examples/build-events/test/rpc.test.ts +0 -4
  145. package/examples/build-events/test/support/as.ts +3 -4
  146. package/examples/build-events/test/support/memory.ts +0 -5
  147. package/examples/build-events/test/webhooks.test.ts +0 -12
  148. package/examples/kitchen-sink/README.md +1 -1
  149. package/examples/kitchen-sink/src/Actions.ts +0 -10
  150. package/examples/kitchen-sink/src/Api.ts +0 -8
  151. package/examples/kitchen-sink/src/Rpc.ts +0 -4
  152. package/examples/kitchen-sink/src/admin/Admin.ts +0 -9
  153. package/examples/kitchen-sink/src/admin/authenticate.ts +2 -6
  154. package/examples/kitchen-sink/src/admin/authorize.ts +0 -5
  155. package/examples/kitchen-sink/src/admin/grants.ts +0 -5
  156. package/examples/kitchen-sink/src/admin/handlers.ts +0 -2
  157. package/examples/kitchen-sink/src/admin/keys.ts +1 -3
  158. package/examples/kitchen-sink/src/agent.ts +2 -13
  159. package/examples/kitchen-sink/src/assistant/Assistant.ts +7 -24
  160. package/examples/kitchen-sink/src/assistant/conversations.ts +3 -21
  161. package/examples/kitchen-sink/src/assistant/demo-model.ts +0 -5
  162. package/examples/kitchen-sink/src/assistant/handlers.ts +1 -30
  163. package/examples/kitchen-sink/src/attachments/Attachments.ts +0 -4
  164. package/examples/kitchen-sink/src/attachments/download.ts +0 -10
  165. package/examples/kitchen-sink/src/attachments/handlers.ts +0 -3
  166. package/examples/kitchen-sink/src/attachments/store.ts +0 -3
  167. package/examples/kitchen-sink/src/browser.ts +0 -9
  168. package/examples/kitchen-sink/src/cli.ts +0 -19
  169. package/examples/kitchen-sink/src/config.ts +3 -11
  170. package/examples/kitchen-sink/src/identity/Identity.ts +0 -18
  171. package/examples/kitchen-sink/src/identity/authenticate.ts +3 -14
  172. package/examples/kitchen-sink/src/identity/authorize.ts +0 -6
  173. package/examples/kitchen-sink/src/identity/directory.ts +8 -18
  174. package/examples/kitchen-sink/src/identity/handlers.ts +0 -3
  175. package/examples/kitchen-sink/src/identity/members.ts +1 -5
  176. package/examples/kitchen-sink/src/identity/quota.ts +0 -11
  177. package/examples/kitchen-sink/src/issues/Issues.ts +0 -24
  178. package/examples/kitchen-sink/src/issues/assistant.ts +0 -10
  179. package/examples/kitchen-sink/src/issues/events.ts +2 -16
  180. package/examples/kitchen-sink/src/issues/export.ts +0 -5
  181. package/examples/kitchen-sink/src/issues/guide.ts +0 -5
  182. package/examples/kitchen-sink/src/issues/handlers.ts +0 -5
  183. package/examples/kitchen-sink/src/issues/store.ts +19 -23
  184. package/examples/kitchen-sink/src/issues/write-limit.ts +0 -12
  185. package/examples/kitchen-sink/src/memory.ts +0 -9
  186. package/examples/kitchen-sink/src/ops.ts +0 -6
  187. package/examples/kitchen-sink/src/request-policy.ts +0 -17
  188. package/examples/kitchen-sink/src/routes.ts +0 -48
  189. package/examples/kitchen-sink/src/sdk.ts +0 -12
  190. package/examples/kitchen-sink/src/seed.ts +0 -10
  191. package/examples/kitchen-sink/src/server.ts +0 -22
  192. package/examples/kitchen-sink/src/stdio.ts +2 -12
  193. package/examples/kitchen-sink/src/workspaces/Workspaces.ts +0 -4
  194. package/examples/kitchen-sink/src/workspaces/handlers.ts +0 -4
  195. package/examples/kitchen-sink/src/workspaces/registry.ts +0 -5
  196. package/examples/kitchen-sink/src/workspaces/tenant.ts +0 -4
  197. package/examples/kitchen-sink/test/admin/admin.test.ts +0 -1
  198. package/examples/kitchen-sink/test/assistant/assistant.test.ts +0 -18
  199. package/examples/kitchen-sink/test/attachments/attachments.test.ts +0 -9
  200. package/examples/kitchen-sink/test/identity/identity.test.ts +0 -10
  201. package/examples/kitchen-sink/test/identity/session.test.ts +0 -10
  202. package/examples/kitchen-sink/test/issues/assistant.test.ts +3 -7
  203. package/examples/kitchen-sink/test/issues/events.test.ts +2 -9
  204. package/examples/kitchen-sink/test/issues/export.test.ts +0 -3
  205. package/examples/kitchen-sink/test/issues/issues.test.ts +6 -12
  206. package/examples/kitchen-sink/test/request-policy.test.ts +0 -6
  207. package/examples/kitchen-sink/test/routes.test.ts +0 -6
  208. package/examples/kitchen-sink/test/support/as.ts +3 -5
  209. package/examples/kitchen-sink/test/support/scripted.ts +0 -3
  210. package/examples/kitchen-sink/test/support/services.ts +0 -3
  211. package/examples/live-rpc/src/Rpc.ts +0 -4
  212. package/examples/live-rpc/src/browser.ts +0 -24
  213. package/examples/live-rpc/src/chat/Chat.ts +0 -22
  214. package/examples/live-rpc/src/chat/authorize.ts +0 -3
  215. package/examples/live-rpc/src/chat/events.ts +0 -14
  216. package/examples/live-rpc/src/chat/handlers.ts +0 -5
  217. package/examples/live-rpc/src/chat/room.ts +0 -6
  218. package/examples/live-rpc/src/chat/store.ts +0 -9
  219. package/examples/live-rpc/src/client.ts +0 -6
  220. package/examples/live-rpc/src/identity/Identity.ts +0 -6
  221. package/examples/live-rpc/src/identity/members.ts +1 -3
  222. package/examples/live-rpc/src/identity/verifier.ts +6 -12
  223. package/examples/live-rpc/src/page.ts +0 -10
  224. package/examples/live-rpc/src/routes.ts +0 -13
  225. package/examples/live-rpc/src/server.ts +0 -16
  226. package/examples/live-rpc/test/browser.test.ts +0 -9
  227. package/examples/live-rpc/test/chat/chat.test.ts +3 -11
  228. package/examples/live-rpc/test/routes.test.ts +0 -8
  229. package/examples/live-rpc/test/server.test.ts +0 -13
  230. package/examples/mcp-tools/src/instructions.ts +0 -4
  231. package/examples/mcp-tools/src/platform/fake-farm.ts +0 -6
  232. package/examples/mcp-tools/src/platform/print-farm.ts +0 -16
  233. package/examples/mcp-tools/src/printers/Printers.ts +0 -21
  234. package/examples/mcp-tools/src/printers/farm.ts +0 -5
  235. package/examples/mcp-tools/src/printers/handlers.ts +0 -6
  236. package/examples/mcp-tools/src/routes.ts +0 -11
  237. package/examples/mcp-tools/src/server.ts +0 -9
  238. package/examples/mcp-tools/src/stdio.ts +1 -13
  239. package/examples/mcp-tools/test/printers/printers.test.ts +0 -10
  240. package/examples/mcp-tools/test/routes.test.ts +0 -14
  241. package/examples/notes-service/src/Api.ts +0 -4
  242. package/examples/notes-service/src/browser.ts +0 -12
  243. package/examples/notes-service/src/cli.ts +0 -2
  244. package/examples/notes-service/src/commands.ts +0 -30
  245. package/examples/notes-service/src/identity/Identity.ts +0 -2
  246. package/examples/notes-service/src/identity/authorize.ts +0 -4
  247. package/examples/notes-service/src/identity/users.ts +1 -3
  248. package/examples/notes-service/src/identity/verifier.ts +4 -11
  249. package/examples/notes-service/src/notes/Notes.ts +0 -12
  250. package/examples/notes-service/src/notes/handlers.ts +0 -4
  251. package/examples/notes-service/src/notes/store.ts +62 -48
  252. package/examples/notes-service/src/routes.ts +0 -12
  253. package/examples/notes-service/src/server.ts +0 -6
  254. package/examples/notes-service/src/status/Status.ts +0 -1
  255. package/examples/notes-service/src/status/handlers.ts +0 -1
  256. package/examples/notes-service/src/stdio.ts +2 -12
  257. package/examples/notes-service/src/worker.ts +0 -7
  258. package/examples/notes-service/test/commands.test.ts +0 -20
  259. package/examples/notes-service/test/notes/as.ts +3 -4
  260. package/examples/notes-service/test/notes/in-process.test.ts +3 -6
  261. package/examples/notes-service/test/notes/notes.test.ts +0 -6
  262. package/examples/notes-service/test/routes.test.ts +0 -5
  263. package/examples/notes-service/test/worker.test.ts +0 -1
  264. package/examples/quickstart/src/Api.ts +0 -1
  265. package/examples/quickstart/src/client.ts +0 -3
  266. package/examples/quickstart/src/greeting/Greeting.ts +0 -2
  267. package/examples/quickstart/src/greeting/handlers.ts +0 -1
  268. package/examples/quickstart/src/routes.ts +0 -1
  269. package/examples/quickstart/src/server.ts +3 -6
  270. package/examples/quickstart/test/routes.test.ts +0 -1
  271. package/package.json +2 -1
@@ -10,8 +10,6 @@ import { Directory } from "./directory.js";
10
10
  import { Login, scopes, Session } from "./Identity.js";
11
11
  import type { Member } from "./Identity.js";
12
12
 
13
- // What both verifiers check once a credential has named a member: an operator may have
14
- // suspended the workspace. Checked here, it holds on every remote surface.
15
13
  const inService = Effect.gen(function* () {
16
14
  const registry = yield* WorkspaceRegistry;
17
15
 
@@ -21,8 +19,6 @@ const inService = Effect.gen(function* () {
21
19
  Effect.orElseSucceed(() => true)
22
20
  );
23
21
 
24
- // No scopes named: signing in again grants nothing, so this is a plain 403, never a
25
- // challenge sending an OAuth client back to its authorization server.
26
22
  if (suspended) {
27
23
  return yield* new Action.Forbidden({
28
24
  message: "This workspace is suspended.",
@@ -33,24 +29,20 @@ const inService = Effect.gen(function* () {
33
29
  });
34
30
  });
35
31
 
36
- // The OAuth protected resource whose discovery the provider publishes, so an MCP client that
37
- // gets a 401 finds where to sign in, asking for the read scope first.
32
+ const demoAuthorizationServer = "https://auth.example.com";
33
+
38
34
  const protectedResource = Effect.map(
39
35
  publicUrl,
40
36
  (url) =>
41
37
  ({
42
38
  resource: `${url}/mcp`,
43
- // DEMO ONLY: no server is there. Yours is your authorization server's issuer.
44
- authorizationServers: ["https://auth.example.com"],
39
+ authorizationServers: [demoAuthorizationServer],
45
40
  scopesSupported: [scopes.read, scopes.write],
46
41
  scopesRequired: [scopes.read],
47
42
  resourceName: "Tracker",
48
43
  }) satisfies Authentication.ProtectedResource
49
44
  );
50
45
 
51
- // Server only: `Login`'s provider. The Effect is a builder, as `Action.implement` takes one:
52
- // it yields the startup services once, and returns the verifier, which runs per request and
53
- // may read request services, here the `Tenant` router middleware provides.
54
46
  export const authenticateBearer = Authentication.layer(
55
47
  Login,
56
48
  Effect.gen(function* () {
@@ -78,9 +70,6 @@ export const authenticateBearer = Authentication.layer(
78
70
  { protectedResource }
79
71
  );
80
72
 
81
- // `Session`'s provider: the same identity from another credential. Its verifier receives what
82
- // its scheme decodes, the cookie's value. A session belongs to one workspace, so it reads no
83
- // `Tenant`.
84
73
  export const authenticateSession = Authentication.layer(
85
74
  Session,
86
75
  Effect.gen(function* () {
@@ -4,15 +4,9 @@ import { Effect } from "effect";
4
4
 
5
5
  import { CurrentMember, scopes } from "./Identity.js";
6
6
 
7
- // The members' rule, read from each contract: a read needs the read scope, anything else the
8
- // write scope. The implementations of issues and attachments state it, so every surface serving
9
- // them runs it before the handler: HTTP, MCP, RPC, stdio, the assistant's tools and a test alike.
10
- // For a bearer token, naming the missing scope turns the 403 into the challenge an OAuth client
11
- // re-authorizes on.
12
7
  export const authorize = Effect.fn("authorize")(function* (
13
8
  contract: Action.Any | Event.Any
14
9
  ) {
15
- // A subscription to an event reads, as a `readOnly` action does.
16
10
  const scope =
17
11
  Event.isEvent(contract) || contract.readOnly ? scopes.read : scopes.write;
18
12
 
@@ -2,44 +2,34 @@ import * as Action from "@gjermundgaraba/effect-actions/Action";
2
2
  import { Context, Effect, Layer, Redacted } from "effect";
3
3
 
4
4
  import type { IssuerUnavailable, Member } from "./Identity.js";
5
- import { members } from "./members.js";
5
+ import { demoMembers } from "./members.js";
6
6
 
7
- /**
8
- * Who a credential names. It is a startup service: the verifiers yield it once, when their
9
- * layer is built, and call it per request.
10
- */
11
7
  export class Directory extends Context.Service<
12
8
  Directory,
13
9
  {
14
- /** Every membership of the account an access token was issued to. */
15
10
  readonly ofToken: (
16
11
  token: Redacted.Redacted
17
12
  ) => Effect.Effect<
18
13
  readonly Member[],
19
14
  Action.Unauthenticated | IssuerUnavailable
20
15
  >;
21
- /** The member a web session was opened for: a session belongs to one workspace. */
22
16
  readonly ofSession: (
23
17
  session: Redacted.Redacted
24
18
  ) => Effect.Effect<Member, Action.Unauthenticated>;
25
19
  }
26
20
  >()("tracker/Directory") {
27
- // DEMO ONLY: a token is an account's name (`ada`, `linus`, `kay`, `grace`) and a session a fixed
28
- // string. A real directory checks a signed token with the authorization server's library,
29
- // its audience included, failing with `IssuerUnavailable` when it cannot reach its keys, and
30
- // reads sessions from their store.
31
21
  static readonly layerDemo = Layer.sync(Directory, () => {
32
22
  const accounts = new Map<string, readonly Member[]>([
33
- ["ada", [members.ada, members.adaAtGlobex]],
34
- ["linus", [members.linus]],
35
- ["kay", [members.kay]],
36
- ["grace", [members.grace]],
23
+ ["ada", [demoMembers.ada, demoMembers.adaAtGlobex]],
24
+ ["linus", [demoMembers.linus]],
25
+ ["kay", [demoMembers.kay]],
26
+ ["grace", [demoMembers.grace]],
37
27
  ]);
38
28
 
39
29
  const sessions = new Map<string, Member>([
40
- ["ada-at-acme", members.ada],
41
- ["kay-at-acme", members.kay],
42
- ["grace-at-globex", members.grace],
30
+ ["ada-at-acme", demoMembers.ada],
31
+ ["kay-at-acme", demoMembers.kay],
32
+ ["grace-at-globex", demoMembers.grace],
43
33
  ]);
44
34
 
45
35
  return Directory.of({
@@ -2,9 +2,6 @@ import * as Action from "@gjermundgaraba/effect-actions/Action";
2
2
 
3
3
  import { CurrentMember, WhoAmI } from "./Identity.js";
4
4
 
5
- // `Action.allowAll`: verification is the whole policy here. Any authenticated member may ask
6
- // who they are, whatever their scopes, so there is no rule to state. It must still be said:
7
- // an implementation of a protected action without `authorize` does not compile.
8
5
  export const identity = Action.implement(WhoAmI, () => CurrentMember, {
9
6
  authorize: Action.allowAll,
10
7
  });
@@ -1,14 +1,10 @@
1
1
  import { scopes } from "./Identity.js";
2
2
  import type { Member } from "./Identity.js";
3
3
 
4
- // DEMO ONLY: the people of the demo, each a member of one workspace. A real application has no
5
- // such table: its directory reads a member from a signed token or a stored session. A local
6
- // host names its caller with one of these, and so does a test.
7
- export const members = {
4
+ export const demoMembers = {
8
5
  ada: { id: "ada", workspace: "acme", scopes: [scopes.read, scopes.write] },
9
6
  adaAtGlobex: { id: "ada", workspace: "globex", scopes: [scopes.read] },
10
7
  linus: { id: "linus", workspace: "acme", scopes: [scopes.read] },
11
- // Invited to acme, granted nothing yet.
12
8
  kay: { id: "kay", workspace: "acme", scopes: [] },
13
9
  grace: {
14
10
  id: "grace",
@@ -5,26 +5,16 @@ import { RpcMiddleware } from "effect/rpc";
5
5
 
6
6
  import { CurrentMember, QuotaExceeded } from "./Identity.js";
7
7
 
8
- // The API's request quota: 60 requests a minute per member. It is a surface's own middleware,
9
- // not a handler's check, because it must count before the input is decoded: a request whose
10
- // body does not decode counts too. It runs inside authentication, so it may require the
11
- // caller. HTTP and RPC each have their native middleware; an MCP endpoint takes none.
12
-
13
- /** Listed in `ActionHttp.layer`'s `middleware`. It fails only with an error the binding declares. */
14
8
  export class HttpQuota extends HttpApiMiddleware.Service<
15
9
  HttpQuota,
16
10
  { requires: CurrentMember }
17
11
  >()("tracker/HttpQuota", { error: QuotaExceeded }) {}
18
12
 
19
- /** Listed in `ActionRpc.layer`'s `middleware`: the same quota, counted per message. */
20
13
  export class RpcQuota extends RpcMiddleware.Service<
21
14
  RpcQuota,
22
15
  { requires: CurrentMember }
23
16
  >()("tracker/RpcQuota", { error: QuotaExceeded }) {}
24
17
 
25
- // What both middleware do before a call. Each middleware's layer builds this for itself, so it
26
- // runs twice; the count is one all the same, because both yield the one `RateLimiter` the
27
- // entrypoint provides and spend under the same key.
28
18
  const spend = Effect.map(RateLimiter.RateLimiter, (limiter) =>
29
19
  Effect.gen(function* () {
30
20
  const member = yield* CurrentMember;
@@ -46,7 +36,6 @@ const spend = Effect.map(RateLimiter.RateLimiter, (limiter) =>
46
36
  retryAfterSeconds: Duration.toSeconds(retryAfter),
47
37
  })
48
38
  ),
49
- // Any other reason is the limiter's store failing: a defect, never the caller's.
50
39
  Effect.die
51
40
  )
52
41
  );
@@ -22,8 +22,6 @@ export const Issue = Schema.Struct({
22
22
 
23
23
  export type Issue = typeof Issue.Type;
24
24
 
25
- // A declared error is part of the contract: every client decodes it as this class, and its
26
- // status is what HTTP answers with. Without `httpApiStatus` it would be a 422.
27
25
  export class IssueNotFound extends Schema.TaggedError<
28
26
  IssueNotFound,
29
27
  { readonly brand: unique symbol }
@@ -38,20 +36,11 @@ export class RateLimited extends Schema.TaggedError<
38
36
  { httpApiStatus: 429 }
39
37
  ) {}
40
38
 
41
- // A shared error list: what any write to an existing issue may fail with. Each such action
42
- // names this one array as its `error`, here and in attachments/Attachments.ts, so an error
43
- // added to the list is declared by all of them.
44
39
  export const IssueWriteErrors = [IssueNotFound, RateLimited] as const;
45
40
 
46
- // An option bundle. What the two actions changing an issue share is one constant object spread
47
- // into `Action.make`, typed as if written inline. `readOnly` and `description` stay on each
48
- // action.
49
41
  const change = {
50
42
  caller: CurrentMember,
51
43
  error: IssueWriteErrors,
52
- // MCP hints, in MCP's names. They tell a model what a tool does and enforce nothing; the
53
- // read-only hint is `readOnly` itself. A write is destructive unless it only adds, as a
54
- // comment does: closing is not stated `false`.
55
44
  mcp: { openWorldHint: false },
56
45
  } as const;
57
46
 
@@ -61,10 +50,7 @@ export const ListIssues = Action.make("listIssues", {
61
50
  description:
62
51
  "List your workspace's issues, newest first, without bodies or comments.",
63
52
  input: {
64
- // Optional: left out, issues of every status are listed.
65
53
  status: Schema.optional(Status),
66
- // Defaulted: left out, the handler receives 20. A field description is also the help text
67
- // of its CLI flag and the description in the tool's schema.
68
54
  limit: Schema.Int.check(
69
55
  Schema.isBetween({ minimum: 1, maximum: 100 })
70
56
  ).pipe(
@@ -130,11 +116,6 @@ export const Issues = [
130
116
  CloseIssue,
131
117
  ] as const;
132
118
 
133
- // An action answering a stream: its handler returns a `Stream` of issues, and every client's
134
- // call returns one, each issue read as the server sends it. It reports how far it is, too.
135
- // `Issues` leaves it out, so MCP's `Tools` and the assistant's tools do: a tool answers one
136
- // result, so it would collect the whole export before answering, every issue at once in a
137
- // model's context. HTTP, RPC and the CLI serve it.
138
119
  export const ExportIssues = Action.make("exportIssues", {
139
120
  description:
140
121
  "Stream every issue of your workspace, oldest first, with bodies and comments.",
@@ -152,25 +133,20 @@ export const ExportIssues = Action.make("exportIssues", {
152
133
  description: "How many the export holds.",
153
134
  }),
154
135
  },
155
- // An export is costly, so it is limited too: refused before its first issue, a 429.
156
136
  error: RateLimited,
157
137
  readOnly: true,
158
138
  caller: CurrentMember,
159
139
  });
160
140
 
161
- /** What changed about an issue, as `issue.changed` carries it. */
162
141
  export const IssueChange = Schema.Struct({
163
142
  workspace: Schema.String,
164
143
  id: Schema.String,
165
144
  title: Schema.String,
166
145
  status: Status,
167
146
  change: Schema.Literals(["filed", "commented", "closed"]),
168
- // The comment a change added; a subscriber asking `comments: false` receives none.
169
147
  comment: Schema.optional(Comment),
170
148
  });
171
149
 
172
- // An event type: what a subscription takes, what each occurrence carries, and who may
173
- // subscribe. `input` both filters the occurrences, by `status`, and shapes them, by `comments`.
174
150
  export const IssueChanged = Event.make("issue.changed", {
175
151
  description: "An issue in your workspace was filed, commented on or closed.",
176
152
  input: {
@@ -4,22 +4,12 @@ import { LanguageModel } from "effect/ai";
4
4
  import { issues } from "./handlers.js";
5
5
  import { CloseIssue, CommentOnIssue, GetIssue, ListIssues } from "./Issues.js";
6
6
 
7
- // The actions a model may call, listed here, where they are served. Filing issues is left to
8
- // people.
9
7
  const tools = [ListIssues, GetIssue, CommentOnIssue, CloseIssue] as const;
10
8
 
11
- // Some of the issue actions as a model's tools: Effect's native `Toolkit`, in process, with no
12
- // server between. `toolkit` holds the tool definitions and `layer` their handlers, the same
13
- // implementation every other surface serves, behind the same `authorize` and write limit.
14
9
  export const { toolkit, layer } = ActionToolkit.make(issues, {
15
10
  actions: tools,
16
- // One rule over every call a model makes, before the call runs: a write ends the turn with
17
- // an approval request instead of a result. Approval is not authorization: `authorize` still
18
- // runs for an approved call, as for any other.
19
11
  needsApproval: (call) => !call.action.readOnly,
20
12
  });
21
13
 
22
- // One turn of the assistant. The caller provides the model, and the member it acts for around
23
- // the whole turn: `authorize` and the handlers read that one caller.
24
14
  export const triage = (prompt: string) =>
25
15
  LanguageModel.generateText({ prompt, toolkit });
@@ -6,10 +6,6 @@ import { CurrentMember } from "../identity/Identity.js";
6
6
  import { IssueChanged } from "./Issues.js";
7
7
  import { IssueStore } from "./store.js";
8
8
 
9
- // A log source fed by a stream: the store pushes every change, of every workspace, and the
10
- // library consumes the stream once, from startup, keeping what it produced lately for
11
- // subscribers resuming from a cursor. `key`, `match` and `transform` run per subscriber, as the
12
- // subscriber.
13
9
  export const issueEvents = Event.implement(
14
10
  IssueChanged,
15
11
  Effect.gen(function* () {
@@ -17,30 +13,20 @@ export const issueEvents = Event.implement(
17
13
 
18
14
  return {
19
15
  log: {
20
- // No `eventId`: the library gives each change one, kept on every replay. An id of the
21
- // store's would have to stay unique across restarts, as a subscriber drops a repeated
22
- // one. Each change is keyed by its workspace, which reaches that workspace's
23
- // subscribers alone.
24
16
  emit: Stream.map(store.changes, (data) => ({
25
17
  data,
26
18
  key: data.workspace,
27
19
  })),
28
- // One log holds every workspace's changes: a subscriber resuming within an hour, and
29
- // within the last thousand changes of all workspaces, misses nothing; one resuming
30
- // later receives a gap first, then what is kept. A busy workspace shortens a quiet
31
- // one's hour.
32
20
  maxAge: "1 hour",
33
21
  maxEvents: 1000,
34
22
  },
35
- // Tenant isolation, as the store's methods hold it for the actions: a member follows
36
- // their own workspace's key. It is never `undefined`, which would follow every key.
37
- key: () => Effect.map(CurrentMember, (member) => member.workspace),
23
+ key: (): Effect.Effect<string, never, CurrentMember> =>
24
+ Effect.map(CurrentMember, (member) => member.workspace),
38
25
  match: ({ status }, change) =>
39
26
  status === undefined || change.status === status,
40
27
  transform: ({ comments }, change) =>
41
28
  comments === false ? Struct.omit(change, ["comment"]) : change,
42
29
  };
43
30
  }),
44
- // The members' rule, as for the actions: a subscription needs the read scope.
45
31
  { authorize }
46
32
  );
@@ -12,17 +12,12 @@ export const exports = Action.implement(
12
12
  const store = yield* IssueStore;
13
13
  const limited = yield* exportLimit;
14
14
 
15
- // The handler returns a `Stream`. The limit is spent before the first issue, so a refused
16
- // export fails as an action answering one value does, a 429 over HTTP; a failure after an
17
- // issue would end the stream instead, the issues before it already sent.
18
15
  return ({ status }, { progress }) =>
19
16
  Stream.unwrap(
20
17
  limited((member) =>
21
18
  Effect.map(store.count(member.workspace, status), (total) =>
22
19
  store.all(member.workspace, status).pipe(
23
20
  Stream.zipWithIndex,
24
- // `progress` never waits for the caller, dropping the oldest values a slow one has not taken:
25
- // `exported` may skip numbers, so it says how far the export is, not what arrived.
26
21
  Stream.mapEffect(([issue, index]) =>
27
22
  Effect.as(progress({ exported: index + 1, total }), issue)
28
23
  )
@@ -11,13 +11,8 @@ const guide = `# Triage
11
11
  - Writes are limited to ten a minute. When refused, wait as long as the error says.
12
12
  `;
13
13
 
14
- // The contracts keyed by name, each its exact contract. The prompt below names its tools through
15
- // them, so renaming an action is a compile error here, not a stale instruction to a model.
16
14
  const tools = Action.byName(Issues);
17
15
 
18
- // Effect's own MCP resources and prompts, served beside the actions' tools as an endpoint's
19
- // `features`: native, not actions. This guide is the same for every caller, so it reads no
20
- // caller, which `features` never receive.
21
16
  export const features = Layer.mergeAll(
22
17
  McpServer.resource({
23
18
  uri: "tracker://guides/triage",
@@ -7,10 +7,6 @@ import { Issues } from "./Issues.js";
7
7
  import { IssueStore } from "./store.js";
8
8
  import { writeLimit } from "./write-limit.js";
9
9
 
10
- // Several actions sharing one builder. It runs once, at startup, however many surfaces serve
11
- // these actions, and yields the startup services: the store, and the write limit built on the
12
- // limiter. `CurrentMember` is a per-request service: each handler reads it per call, so one
13
- // build serves every caller. No handler checks a scope: `authorize` has run before it.
14
10
  export const issues = Action.implement(
15
11
  Issues,
16
12
  Effect.gen(function* () {
@@ -35,7 +31,6 @@ export const issues = Action.implement(
35
31
  Effect.flatMap(CurrentMember, (member) =>
36
32
  store.get(member.workspace, id)
37
33
  ),
38
- // One function wraps every limited handler, and gives it the member it counted.
39
34
  createIssue: (draft) =>
40
35
  limited((member) => store.create(member.workspace, draft)),
41
36
  commentOnIssue: ({ id, text }) =>
@@ -3,11 +3,6 @@ import { Context, Effect, Layer, PubSub, Stream } from "effect";
3
3
  import { IssueNotFound } from "./Issues.js";
4
4
  import type { Comment, Issue, IssueChange, Status } from "./Issues.js";
5
5
 
6
- /**
7
- * Where issues live, each in one workspace. Every method but `changes` takes the workspace, so
8
- * tenant isolation is the data access's: no surface and no handler can skip it, and an issue of
9
- * another workspace is simply not found.
10
- */
11
6
  export class IssueStore extends Context.Service<
12
7
  IssueStore,
13
8
  {
@@ -16,7 +11,6 @@ export class IssueStore extends Context.Service<
16
11
  status: typeof Status.Type | undefined,
17
12
  limit: number
18
13
  ) => Effect.Effect<readonly Issue[]>;
19
- /** Every issue, oldest first. */
20
14
  readonly all: (
21
15
  workspace: string,
22
16
  status: typeof Status.Type | undefined
@@ -42,24 +36,15 @@ export class IssueStore extends Context.Service<
42
36
  workspace: string,
43
37
  id: string
44
38
  ) => Effect.Effect<Issue, IssueNotFound>;
45
- /**
46
- * Every change from now on, in the order the store makes them, of every workspace: whatever
47
- * reads it filters by workspace itself, as `issueEvents`' `match` does.
48
- */
49
39
  readonly changes: Stream.Stream<typeof IssueChange.Type>;
50
40
  }
51
41
  >()("tracker/IssueStore") {
52
- // DEMO ONLY: memory, lost when the process stops. A real store is another Layer of this
53
- // service, chosen by the entrypoint.
54
42
  static readonly layerMemory = Layer.effect(
55
43
  IssueStore,
56
44
  Effect.gen(function* () {
57
45
  const issues = new Map<string, Issue[]>();
58
46
  const changes = yield* PubSub.unbounded<typeof IssueChange.Type>();
59
47
 
60
- // Published in the step of the write, after it, so a subscriber never sees a change the
61
- // store did not make, and sees them in the order they were made. A database publishes
62
- // from its own change feed, or an outbox written in the same transaction.
63
48
  const publish = (
64
49
  workspace: string,
65
50
  issue: Issue,
@@ -98,21 +83,17 @@ export class IssueStore extends Context.Service<
98
83
  return issue;
99
84
  });
100
85
 
101
- // No `yield*` between the read and the write: a fiber may be set aside at one, and two
102
- // changes that each read the issue before either wrote would keep only the second. A
103
- // database does this in one statement or a transaction.
104
- // A change `to` answers with the same issue for is no change, and is not published.
105
- const change = Effect.fn("IssueStore.change")(function* (
86
+ const changedAtomically = (
106
87
  workspace: string,
107
88
  id: string,
108
89
  to: (issue: Issue) => Issue,
109
90
  kind: (typeof IssueChange.Type)["change"],
110
91
  comment?: typeof Comment.Type
111
- ) {
92
+ ): Issue | undefined => {
112
93
  const issue = of(workspace).find((candidate) => candidate.id === id);
113
94
 
114
95
  if (issue === undefined) {
115
- return yield* new IssueNotFound({ id });
96
+ return undefined;
116
97
  }
117
98
 
118
99
  const changed = to(issue);
@@ -130,6 +111,22 @@ export class IssueStore extends Context.Service<
130
111
 
131
112
  publish(workspace, changed, kind, comment);
132
113
 
114
+ return changed;
115
+ };
116
+
117
+ const change = Effect.fn("IssueStore.change")(function* (
118
+ workspace: string,
119
+ id: string,
120
+ to: (issue: Issue) => Issue,
121
+ kind: (typeof IssueChange.Type)["change"],
122
+ comment?: typeof Comment.Type
123
+ ) {
124
+ const changed = changedAtomically(workspace, id, to, kind, comment);
125
+
126
+ if (changed === undefined) {
127
+ return yield* new IssueNotFound({ id });
128
+ }
129
+
133
130
  return changed;
134
131
  });
135
132
 
@@ -138,7 +135,6 @@ export class IssueStore extends Context.Service<
138
135
  Effect.sync(() =>
139
136
  having(workspace, status).toReversed().slice(0, limit)
140
137
  ),
141
- // A database reads it in pages, by id, so an export never holds the workspace whole.
142
138
  all: (workspace, status) =>
143
139
  Stream.suspend(() => Stream.fromIterable(having(workspace, status))),
144
140
  count: (workspace, status) =>
@@ -33,7 +33,6 @@ const limitedBy = Effect.gen(function* () {
33
33
  retryAfterSeconds: Duration.toSeconds(retryAfter),
34
34
  })
35
35
  ),
36
- // Any other reason is the limiter's store failing: a defect, not the caller's.
37
36
  Effect.die
38
37
  )
39
38
  );
@@ -42,15 +41,6 @@ const limitedBy = Effect.gen(function* () {
42
41
  });
43
42
  });
44
43
 
45
- // The write limit: ten writes a minute per member. It is the handlers', not a surface's, so it
46
- // holds wherever they run: HTTP, MCP, RPC, stdio and the assistant's tools all count against
47
- // the one limiter. A builder yields this once and wraps each limited handler in the function
48
- // it gets, which fails with an error those actions' contracts declare; every surface sends it
49
- // as any declared error. The limiter is a service, so its counts are shared wherever it is
50
- // provided: memory.ts provides Effect's own, `RateLimiter.layer` over a store. `consume` counts
51
- // in a fixed window unless given `algorithm: "token-bucket"`: ten writes in each minute, so
52
- // twenty may pass across the boundary between two. A strict limit, ten in any sixty seconds,
53
- // is a service of your own of the same shape, yielded here, never part of the issue store.
54
44
  export const writeLimit = Effect.gen(function* () {
55
45
  const limited = yield* limitedBy;
56
46
 
@@ -58,8 +48,6 @@ export const writeLimit = Effect.gen(function* () {
58
48
  limited("writes", 10, write);
59
49
  });
60
50
 
61
- // The export limit: two exports a minute per member, on the same limiter under a key of its
62
- // own, so exporting spends no write.
63
51
  export const exportLimit = Effect.gen(function* () {
64
52
  const limited = yield* limitedBy;
65
53
 
@@ -10,21 +10,14 @@ import { Directory } from "./identity/directory.js";
10
10
  import { IssueStore } from "./issues/store.js";
11
11
  import { WorkspaceRegistry } from "./workspaces/registry.js";
12
12
 
13
- // DEMO ONLY: the application's services as their memory or fixed-data Layers, lost when the
14
- // process stops. A real entrypoint provides a database-backed Layer per service instead, and a
15
- // test replaces the one it is about.
16
-
17
- /** Both stores. The attachment store is built on the issue store: `provideMerge` keeps it. */
18
13
  export const stores = AttachmentStore.layerMemory.pipe(
19
14
  Layer.provideMerge(IssueStore.layerMemory)
20
15
  );
21
16
 
22
- /** Effect's own limiter, counting in memory. `RateLimiter.layerStoreRedis` counts across processes. */
23
17
  export const limiter = RateLimiter.layer.pipe(
24
18
  Layer.provide(RateLimiter.layerStoreMemory)
25
19
  );
26
20
 
27
- /** Everything the server needs, for both ports. A local entrypoint provides only what it serves. */
28
21
  export const memory = Layer.mergeAll(
29
22
  stores,
30
23
  limiter,
@@ -33,7 +26,5 @@ export const memory = Layer.mergeAll(
33
26
  Grants.layerDemo,
34
27
  Conversations.layerMemory,
35
28
  demoModel,
36
- // The issue changes' log, kept in this process for the subscribers its sockets and streams
37
- // hold.
38
29
  Event.layerMemory
39
30
  );
@@ -9,10 +9,6 @@ import { Grants } from "./admin/grants.js";
9
9
  import { admin } from "./admin/handlers.js";
10
10
  import { WorkspaceRegistry } from "./workspaces/registry.js";
11
11
 
12
- // Entrypoint: `node --import tsx examples/kitchen-sink/src/ops.ts list-workspaces`.
13
- // The operators' console: the admin actions run in this process, on the host's own services,
14
- // with no server and no key.
15
-
16
12
  const operator: TrustedOperator = { id: "console", trusted: true };
17
13
 
18
14
  const ops = ActionCli.make(admin, {
@@ -31,8 +27,6 @@ const ops = ActionCli.make(admin, {
31
27
  resumeWorkspace: { positional: ["workspace"] },
32
28
  },
33
29
  }).pipe(
34
- // Only what these actions are built from. DEMO ONLY: memory, so this console sees its own
35
- // process, not the server's data. A real one provides the database Layers the server does.
36
30
  Command.provide(Layer.merge(WorkspaceRegistry.layerMemory, Grants.layerDemo)),
37
31
  Command.provideSync(Operator, operator)
38
32
  );
@@ -5,13 +5,6 @@ import { allowedOrigins, publicUrl } from "./config.js";
5
5
 
6
6
  const loopback = ["localhost", "127.0.0.1"];
7
7
 
8
- // Which Host and Origin a request may carry: not the library's concern, but every request of
9
- // both ports passes it first. A Host that is neither loopback nor the public URL's is refused,
10
- // against DNS rebinding. The web app signs in with a cookie, which a browser attaches to
11
- // any page's request, so a foreign Origin must be refused before authentication reads it.
12
- // That is why this is global router middleware (`global: true`): it runs before routing, so
13
- // before every route's authentication. As a layer's own `middleware` it would run inside the
14
- // authentication, too late.
15
8
  export const requestPolicy = HttpRouter.middleware(
16
9
  Effect.gen(function* () {
17
10
  const hosts = new Set([...loopback, new URL(yield* publicUrl).hostname]);
@@ -31,10 +24,6 @@ export const requestPolicy = HttpRouter.middleware(
31
24
 
32
25
  const { origin } = request.headers;
33
26
 
34
- // The request's own origin is that of a page served from this port directly. Behind a
35
- // proxy ending TLS it is an `http:` one from Effect 4.0.6, whose `originalUrl` takes the
36
- // scheme of the connection, whatever `X-Forwarded-Proto` says, so the public URL's
37
- // origin is among `origins`.
38
27
  if (
39
28
  origin !== undefined &&
40
29
  origin !== url.value.origin &&
@@ -49,12 +38,6 @@ export const requestPolicy = HttpRouter.middleware(
49
38
  { global: true }
50
39
  );
51
40
 
52
- // CORS for the one cross-origin caller, a browser MCP client. `HttpRouter.cors` is global to
53
- // the router too: it answers the preflight before any route's authentication, and adds its
54
- // headers to refusals as well. An MCP endpoint's `allowedOrigins` is no CORS: it only refuses
55
- // origins. The headers are those an MCP client sends and must be able to read. A client of an
56
- // MCP revision with sessions sends its session's id on each request, reads it from the answer
57
- // that opened the session, and ends the session with a `DELETE`.
58
41
  export const cors = Layer.unwrap(
59
42
  Effect.map(allowedOrigins, (origins) =>
60
43
  HttpRouter.cors({