@jskit-ai/agent-docs 0.1.331 → 0.1.332

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 (73) hide show
  1. package/guide/agent/app-extras/realtime.md +11 -3
  2. package/guide/agent/app-setup/authentication.md +29 -24
  3. package/guide/agent/app-setup/initial-scaffolding.md +10 -9
  4. package/guide/agent/app-setup/more-than-one-server.md +111 -0
  5. package/guide/agent/app-setup/multi-homing.md +2 -2
  6. package/guide/agent/app-setup/package-with-nix.md +60 -15
  7. package/guide/agent/app-setup/upgrading-jskit.md +36 -1
  8. package/guide/agent/framework/environments.md +174 -0
  9. package/guide/agent/framework/sanity-checks.md +3 -3
  10. package/guide/agent/framework/security-pass.md +7 -5
  11. package/guide/agent/index.md +6 -2
  12. package/package.json +1 -1
  13. package/patterns/feature-package/example/booking-engine/package.json +1 -1
  14. package/patterns/minimal-foundation/example/.env.example +8 -0
  15. package/patterns/minimal-foundation/example/AGENTS.md +7 -5
  16. package/patterns/minimal-foundation/example/bin/develop.js +2 -0
  17. package/patterns/minimal-foundation/example/package.json +6 -6
  18. package/patterns/shell-foundation/example/.env.example +8 -0
  19. package/patterns/shell-foundation/example/AGENTS.md +7 -5
  20. package/patterns/shell-foundation/example/bin/develop.js +2 -0
  21. package/patterns/shell-foundation/example/package.json +7 -7
  22. package/patterns/shell-foundation/example/packages/main/package.json +1 -1
  23. package/reference/autogen/KERNEL_MAP.md +10 -11
  24. package/reference/autogen/PATTERN_INDEX.md +38 -38
  25. package/reference/autogen/packages/auth-core.md +2 -2
  26. package/reference/autogen/packages/auth-provider-local-core.md +9 -9
  27. package/reference/autogen/packages/auth-provider-oidc-core.md +1 -1
  28. package/reference/autogen/packages/auth-provider-supabase-core.md +2 -2
  29. package/reference/autogen/packages/connectors-core.md +2 -2
  30. package/reference/autogen/packages/kernel.md +32 -27
  31. package/reference/autogen/packages/oidc-issuer-core.md +4 -4
  32. package/reference/autogen/packages/payments-core.md +1 -1
  33. package/reference/autogen/packages/shell-web.md +13 -13
  34. package/reference/autogen/packages/storage-runtime.md +6 -0
  35. package/reference/autogen/packages/workspaces-core.md +4 -4
  36. package/reference/autogen/tooling/jskit-catalog.md +6 -1
  37. package/skills/jskit/SKILL.md +2 -1
  38. package/skills/jskit/references/environments.md +169 -0
  39. package/skills/jskit/references/heuristics/INDEX.md +1 -1
  40. package/skills/jskit/references/multi-homing.md +2 -2
  41. package/skills/jskit/references/package-with-nix.md +60 -15
  42. package/skills/jskit/references/pattern-index.md +38 -38
  43. package/skills/jskit/references/patterns/app/minimal-foundation/example/.env.example +8 -0
  44. package/skills/jskit/references/patterns/app/minimal-foundation/example/AGENTS.md +7 -5
  45. package/skills/jskit/references/patterns/app/minimal-foundation/example/bin/develop.js +2 -0
  46. package/skills/jskit/references/patterns/app/minimal-foundation/example/package.json +6 -6
  47. package/skills/jskit/references/patterns/app/shell-foundation/example/.env.example +8 -0
  48. package/skills/jskit/references/patterns/app/shell-foundation/example/AGENTS.md +7 -5
  49. package/skills/jskit/references/patterns/app/shell-foundation/example/bin/develop.js +2 -0
  50. package/skills/jskit/references/patterns/app/shell-foundation/example/package.json +7 -7
  51. package/skills/jskit/references/patterns/app/shell-foundation/example/packages/main/package.json +1 -1
  52. package/skills/jskit/references/patterns/auth/accounts-hub/example/package.json +3 -3
  53. package/skills/jskit/references/patterns/auth/oidc-sign-in/example/package.json +2 -2
  54. package/skills/jskit/references/patterns/auth/supabase-auth/example/package.json +1 -1
  55. package/skills/jskit/references/patterns/connectors/api-key-connection/PATTERN.md +4 -1
  56. package/skills/jskit/references/patterns/connectors/api-key-connection/example/connections.js +3 -3
  57. package/skills/jskit/references/patterns/connectors/calendar-cli/example/.env.example +1 -0
  58. package/skills/jskit/references/patterns/connectors/calendar-cli/example/package.json +5 -4
  59. package/skills/jskit/references/patterns/connectors/calendar-cli/example/scripts/calendar.js +3 -1
  60. package/skills/jskit/references/patterns/connectors/firebase-messaging/example/server/notifications.js +3 -3
  61. package/skills/jskit/references/patterns/connectors/oauth-connection/PATTERN.md +3 -1
  62. package/skills/jskit/references/patterns/crud/crud-feature-package/example/packages/books/package.json +3 -3
  63. package/skills/jskit/references/patterns/database/mysql-application/example/package.json +2 -2
  64. package/skills/jskit/references/patterns/database/postgres-application/example/package.json +2 -2
  65. package/skills/jskit/references/patterns/realtime/realtime-application/example/.env.example +1 -0
  66. package/skills/jskit/references/patterns/realtime/realtime-application/example/package.json +2 -2
  67. package/skills/jskit/references/patterns/server/feature-package/example/booking-engine/package.json +1 -1
  68. package/skills/jskit/references/patterns/users/user-administration-server/example/packages/users/package.json +3 -3
  69. package/skills/jskit/references/patterns/users/user-administration-server/example/packages/users-workspace/package.json +6 -6
  70. package/skills/jskit/references/sanity-checks.md +3 -3
  71. package/skills/jskit/references/security-pass.md +7 -5
  72. package/guide/agent/framework/side-effects.md +0 -77
  73. package/skills/jskit/references/side-effects.md +0 -72
@@ -183,9 +183,17 @@ events may still describe distinct intermediate states.
183
183
 
184
184
  ## Single process and Redis
185
185
 
186
- The in-process adapter is correct for one server process. Set
187
- `REALTIME_REDIS_URL` only when multiple processes need a shared backplane.
188
- Credentials stay outside Git and never enter public client config.
186
+ The in-process adapter is correct for one server process: an event reaches
187
+ only the browsers connected to the process that published it. Set
188
+ `REALTIME_REDIS_URL` only when multiple processes need a shared backplane, to
189
+ the same Redis on every one; each then delivers every event to its own
190
+ browsers. Socket.IO opens each connection with HTTP long-polling, whose
191
+ requests must all reach the process that opened it: keep each browser on one
192
+ process at the load balancer, or connect by WebSocket only with
193
+ `realtimeClient: { options: { transports: ["websocket"] } }` in the app's
194
+ public config. Credentials stay outside Git and never enter public client
195
+ config. [Run on more than one server](/guide/app-setup/more-than-one-server)
196
+ covers the rest of what several copies need.
189
197
 
190
198
  ## Shutdown
191
199
 
@@ -166,7 +166,7 @@ The login page shows either refusal, translated, with the wait.
166
166
  ## Sign-in cookies and SameSite
167
167
 
168
168
  Local auth's cookies (access, refresh, recovery and OAuth attempt) and
169
- auth-core's CSRF cookie are httpOnly, at `Path=/`, and in production Secure
169
+ auth-core's CSRF cookie are httpOnly, at `Path=/`, and in staging and production Secure
170
170
  with the `__Host-` prefix. One setting gives them all their SameSite:
171
171
 
172
172
  | Env | Default | Meaning |
@@ -202,10 +202,10 @@ choice, route by route or role by role; nothing asks for it by default.
202
202
  OIDC and Supabase sign-in answer `AUTH_OPERATION_UNSUPPORTED`, and
203
203
  `features.secondFactor` in the auth capabilities is false there.
204
204
 
205
- **Setting it up.** In production, set `AUTH_LOCAL_SECOND_FACTOR_KEY` to a
205
+ **Setting it up.** In staging and production, set `AUTH_LOCAL_SECOND_FACTOR_KEY` to a
206
206
  random secret of at least 32 characters, and `APP_PUBLIC_URL`, whose host the
207
- authenticator app shows beside the codes; without the key a production server
208
- has no second factor. Outside production the session secret stands in for the
207
+ authenticator app shows beside the codes; without the key a staging or production server
208
+ has no second factor. In development and test the session secret stands in for the
209
209
  key. The key seals each account's authenticator secret (AES-256-GCM, bound to
210
210
  the account) and keys the hashes of its recovery codes, so keep it: a factor
211
211
  sealed under one key does not open under another, and verifying it then fails
@@ -348,7 +348,7 @@ capability, an async function accepting `{ email, confirmationUrl }`, built
348
348
  like the recovery sender below; with it, `APP_PUBLIC_URL` is required and the
349
349
  link is never returned or logged. In development without one, the link is in
350
350
  the registration's answer (`confirmationUrl`) and the log, and the sign-in page
351
- shows it with a button that confirms it with the password typed. In production (`NODE_ENV=production`)
351
+ shows it with a button that confirms it with the password typed. In staging or production (`APP_ENV`)
352
352
  without one, while anyone may register, the server refuses to start, naming
353
353
  the ways out: install a sender, set `AUTH_LOCAL_EMAIL_CONFIRMATION=false`, or
354
354
  set `AUTH_LOCAL_SELF_REGISTRATION=false`. For direct library
@@ -357,14 +357,18 @@ composition, pass the sender as `confirmationSender` to
357
357
  `resolveLocalAuthConfig()`; without one, pass
358
358
  `createLoggedConfirmationSender(logger)`.
359
359
 
360
- In a test copy (`SIDE_EFFECTS` is not `live`, see
361
- [Side effects and test copies](/guide/framework/side-effects)), local auth puts
362
- its own simulated sender in place of an installed
363
- `auth.local.confirmation-sender`, whatever that sender does: it sends nothing,
364
- logs each link for the person testing, and keeps the link out of the answer.
365
- The application's sender never runs there, so no code may depend on it having
366
- run, such as code that confirms a registration with the link its sender was
367
- given: in a test copy that code gets no link.
360
+ An installed `auth.local.confirmation-sender` sends as `APP_ENV` says (see
361
+ [Environments](/guide/framework/environments)): in production to the address
362
+ that registered; in staging only to `STAGING_EMAIL_TO`, called with that
363
+ address as `email` and the registered one as `originalEmail`, which its email
364
+ should show (`markStagingEmail` from
365
+ `@jskit-ai/kernel/server/runtime/appEnvironment` does), or not at all when
366
+ `STAGING_EMAIL_TO` is unset; in development and test never. Where it is not
367
+ called, local auth logs each link for the person testing and keeps it out of
368
+ the answer. So no code may depend on the sender having run, such as code that
369
+ confirms a registration with the link its sender was given: in development,
370
+ test and staging without `STAGING_EMAIL_TO` that code gets no link. The
371
+ recovery sender is called the same way.
368
372
 
369
373
  An application that confirms new accounts at once while its email isn't set up
370
374
  says so with the optional `auth.local.confirmation-mode` capability, a function
@@ -376,9 +380,9 @@ account it creates has an address nobody proved, so answer `"without-link"`
376
380
  only when the application accepts that. Any other answer means `"link"` and
377
381
  logs a warning. Local auth asks only while email confirmation is on and the
378
382
  address isn't proven already (an invitation or the first account's setup
379
- code). The mode sends nothing, so a test copy keeps it and registers as the
380
- live copy does, while the sender is still replaced. It does not stand in for a
381
- sender: a production server still needs one, or
383
+ code). The mode sends nothing, so it applies in every environment, whatever
384
+ happens to the sender's emails there. It does not stand in for a
385
+ sender: a staging or production server still needs one, or
382
386
  `AUTH_LOCAL_EMAIL_CONFIRMATION=false`, and `"link"` registrations are sent
383
387
  through it. An application that never sends a link sets
384
388
  `AUTH_LOCAL_EMAIL_CONFIRMATION=false` instead.
@@ -475,9 +479,9 @@ and treat null as a refused link. Their types are `AuthLocalAccounts`,
475
479
  ## The first account in production (local auth)
476
480
 
477
481
  The first account to register becomes the application's owner, so on a
478
- production server (`NODE_ENV=production`) whose local auth has no account yet,
482
+ staging or production server (`APP_ENV`) whose local auth has no account yet,
479
483
  registering needs a one-time setup code that only the server's operator can
480
- read. Development needs no code.
484
+ read. Development and test need no code.
481
485
 
482
486
  Each start of such a server makes a new code, such as
483
487
  `7KQ2M-X9D4F-0RWHN-3TBZC`, and keeps only its hash. It writes the code:
@@ -507,8 +511,8 @@ A hosting tool that shows the code to the operator itself supplies it in
507
511
  `AUTH_LOCAL_SETUP_CODE`, at least 20 letters and digits, for example a
508
512
  generated production secret. The server then asks for that code at every start
509
513
  and on every server, writes no `first-account-setup-code` file, and logs only
510
- that the code is in `AUTH_LOCAL_SETUP_CODE`, never the code itself. Outside
511
- production the variable is ignored.
514
+ that the code is in `AUTH_LOCAL_SETUP_CODE`, never the code itself. In
515
+ development and test the variable is ignored.
512
516
 
513
517
  ## One-time pass sign-in (local auth)
514
518
 
@@ -708,10 +712,11 @@ taken. A test that composes the library directly and reads the sent email
708
712
  waits for it: it passes `deliveries: createLocalDeliveries({ logger })` to
709
713
  `createLocalAuthService()` and awaits `deliveries.settled()`.
710
714
 
711
- Production is `NODE_ENV=production`; anything else, unset included, is
712
- development, so the host that serves the application sets it. A production
713
- server with `AUTH_LOCAL_RECOVERY_DEV_OUTPUT` set refuses to start and names the
714
- setting to remove, rather than ignoring it.
715
+ Staging and production are `APP_ENV=staging` and `APP_ENV=production` (see
716
+ [Environments](/guide/framework/environments)), which the host that serves the
717
+ application sets. A staging or production server with
718
+ `AUTH_LOCAL_RECOVERY_DEV_OUTPUT` set refuses to start and names the setting to
719
+ remove, rather than ignoring it.
715
720
 
716
721
  ### Pre-release migration
717
722
 
@@ -128,15 +128,16 @@ these headers itself keeps its own value.
128
128
  | --- | --- | --- |
129
129
  | `X-Content-Type-Options: nosniff` | always | on |
130
130
  | `Referrer-Policy` | always | `strict-origin-when-cross-origin` |
131
- | `Strict-Transport-Security` | production | `max-age=31536000` |
132
- | `Content-Security-Policy: frame-ancestors` | production | `'self'` |
133
- | `Content-Security-Policy-Report-Only` | production | the content policy below |
134
-
135
- Production means `NODE_ENV=production`. A host that starts the server with a
136
- plain `npm start` runs it in development; its Serve command must set the
137
- variable, for example `env NODE_ENV=production npm start`. Development sends only
138
- nosniff and the Referrer-Policy, so an editor's preview, such as Vibe64's, can
139
- frame the application and inject its bridge script.
131
+ | `Strict-Transport-Security` | staging, production | `max-age=31536000` |
132
+ | `Content-Security-Policy: frame-ancestors` | staging, production | `'self'` |
133
+ | `Content-Security-Policy-Report-Only` | staging, production | the content policy below |
134
+
135
+ Staging and production are `APP_ENV=staging` and `APP_ENV=production` (see
136
+ [Environments](/guide/framework/environments)), which the host sets beside
137
+ `NODE_ENV=production`, for example `env APP_ENV=production NODE_ENV=production
138
+ npm start`. Development and test send only nosniff and the Referrer-Policy, so
139
+ an editor's preview, such as Vibe64's, can frame the application and inject its
140
+ bridge script.
140
141
 
141
142
  The content policy allows JSKIT's client: the application's own scripts and
142
143
  WebAssembly, inline styles (Vuetify and the loading shell use them), images
@@ -0,0 +1,111 @@
1
+ <!-- Generated by `npm run agent-docs:build` from `packages/agent-docs/site/guide/app-setup/more-than-one-server.md`. Do not edit manually. -->
2
+
3
+ ---
4
+ title: Run on More than One Server
5
+ description: Run a JSKIT app as several copies behind a load balancer, with files in an S3 bucket, live updates through Redis, sessions in the database, the same secrets on every copy, a lock for scheduled work and one migration run.
6
+ ---
7
+
8
+ # Run on More than One Server
9
+
10
+ An app can run as several copies, on one machine or several, behind a load
11
+ balancer that sends each request to any of them. The copies share the
12
+ database. What one copy keeps on its own disk or in its own memory, the others
13
+ never see. Settle each item below before starting a second copy.
14
+
15
+ ## Files
16
+
17
+ `runtime.storage`, where uploads and profile pictures go, keeps its files in a
18
+ folder on the machine by default, so a picture saved through one copy is
19
+ missing on the others. Point every copy at the same bucket:
20
+
21
+ ```bash
22
+ JSKIT_STORAGE_DRIVER=s3
23
+ JSKIT_STORAGE_S3_BUCKET=shop-files
24
+ JSKIT_STORAGE_S3_REGION=eu-west-1
25
+ # R2, MinIO or another S3-compatible store; empty for AWS.
26
+ JSKIT_STORAGE_S3_ENDPOINT=
27
+ JSKIT_STORAGE_S3_ACCESS_KEY_ID=
28
+ JSKIT_STORAGE_S3_SECRET_ACCESS_KEY=
29
+ ```
30
+
31
+ A missing required setting stops the app at startup, naming it. Give the
32
+ storage a bucket of its own. The `@jskit-ai/storage-runtime` README lists what
33
+ the `s3` driver does not do, such as listing past 1,000 objects; JSKIT's own
34
+ packages need none of it.
35
+
36
+ ## Live updates
37
+
38
+ With `@jskit-ai/realtime`, each copy delivers an event only to the browsers
39
+ connected to it, unless every copy names the same Redis:
40
+
41
+ ```bash
42
+ REALTIME_REDIS_URL=redis://redis.internal:6379
43
+ # Only when other apps share this Redis: keeps this app's channels apart.
44
+ REDIS_NAMESPACE=shop
45
+ ```
46
+
47
+ Socket.IO starts each connection with HTTP long-polling, whose requests must
48
+ all reach the copy that opened it. Keep each browser on one copy at the load
49
+ balancer (sticky sessions), or connect by WebSocket only with
50
+ `realtimeClient: { options: { transports: ["websocket"] } }` in the app's
51
+ public config.
52
+
53
+ An assistant conversation's live progress reaches only the browsers connected
54
+ to the copy running its turn, with or without Redis; a browser on another copy
55
+ sees the saved conversation when it opens it again.
56
+
57
+ ## Sign-in sessions
58
+
59
+ - Local sign-in with `@jskit-ai/auth-provider-local-db-core` keeps accounts and
60
+ sessions in the database, so any copy reads any session and a sign-out or
61
+ revocation holds on all of them. Without that package, they are files in
62
+ `AUTH_LOCAL_STORE_DIR` (`.jskit/auth`) on each machine, which several copies
63
+ cannot share, not even on a shared folder: its lock assumes one machine.
64
+ - OIDC sign-in keeps sessions in the database, and Supabase sign-in in the
65
+ browser's cookies; both work on any copy.
66
+ - Every copy needs the same `AUTH_LOCAL_SESSION_SECRET`,
67
+ `AUTH_LOCAL_SECOND_FACTOR_KEY`, `AUTH_OIDC_SESSION_SECRET` and
68
+ `OIDC_HUB_SECRET`, as the app uses them. Staging and production (`APP_ENV`,
69
+ see [Environments](/guide/framework/environments)) refuse to start without
70
+ the session secrets; in development and test a missing one is generated into
71
+ a file on each machine, so set them there too for several copies.
72
+ - On an empty staging or production database each copy makes its own
73
+ first-account setup code, and a code from one copy is refused by the others.
74
+ Set `AUTH_LOCAL_SETUP_CODE` on every copy, or run one copy until the first
75
+ account exists.
76
+
77
+ ## What each copy keeps to itself
78
+
79
+ - Request limits, a route's `rateLimit` and auth's limits on sign-in,
80
+ registration, recovery and codes, count in each copy's memory: with three
81
+ copies a client gets up to three times the limit. Local sign-in's wait for
82
+ an account after repeated failures is kept in the database and holds across
83
+ copies.
84
+ - An assistant conversation's one-turn-at-a-time check, steer and stop act on
85
+ the copy running the turn. Sent to another copy, a second message is not
86
+ refused and a stop does not stop the turn. Sticky sessions keep a browser's
87
+ requests on one copy.
88
+ - `@jskit-ai/agent-runtime` writes command output under the system's temporary
89
+ folder, so another copy cannot read a command's full output; point
90
+ `createCommands`' `outputDir` at a folder every copy shares.
91
+
92
+ ## Scheduled work
93
+
94
+ Work the app runs on a timer inside its own process, with `setInterval` or a
95
+ cron library, runs once in every copy. Make each run take a lock before it
96
+ works, as `@jskit-ai/agent-runtime`'s goal, run and summary loops do: one copy
97
+ claims a lease row with a conditional update, and the others find it taken
98
+ and skip. Work that only deletes expired rows can run in every copy, as the
99
+ OIDC hub's upkeep does.
100
+
101
+ ## Migrations
102
+
103
+ Where the host has a Migrate step, run migrations once per release through
104
+ it, `npm run db:migrate` (`<name>-migrate` in a
105
+ [Nix package](/guide/app-setup/package-with-nix)), before the new copies
106
+ start. `npm run db:prepare` also runs the app's seed, so it runs once too, not
107
+ on every copy. A host with no such step migrates from each copy's start
108
+ (`<name> --migrate`) and restarts a copy that fails: Knex's migration lock
109
+ lets one copy migrate, and on PostgreSQL the others wait and then find nothing
110
+ left to do, while on MySQL they fail and start again, as the Nix guide's
111
+ [Migrations](/guide/app-setup/package-with-nix#migrations) section says.
@@ -228,8 +228,8 @@ To send invitation email, provide the optional `workspaces.invite-mailer`
228
228
  capability from one of the application's providers. Without one, invitations
229
229
  are still created and delivery reports `mailer_unconfigured`. After **Send
230
230
  invite**, the Members panel's notice follows the delivery status: `sent` says
231
- the invite was sent; `simulated` (a test copy, whose `SIDE_EFFECTS` is not
232
- `live`) and `mailer_unconfigured` say no email was sent; `held` says email
231
+ the invite was sent; `simulated` (logged outside production, see
232
+ [Environments](/guide/framework/environments)) and `mailer_unconfigured` say no email was sent; `held` says email
233
233
  isn't set up yet, so the invite will be sent once it is; `failed` is a warning
234
234
  that the email failed. Each of those four points to the invite link, which
235
235
  the panel shows under the form with its **Copy** button. For a status the
@@ -83,36 +83,81 @@ environment's. The option is not called `--env-file` because Node reads an
83
83
 
84
84
  | Program | What it does |
85
85
  | --- | --- |
86
- | `<name> [--env FILE] [--migrate]` | Runs the app's `start` script as `npm start` would, without npm, in place of itself: from `lib/<name>`, with `node_modules/.bin` and Node first on `PATH`, and `NODE_ENV=production` unless the environment or the file says otherwise. With `--migrate`, runs `db:migrate` first and starts nothing when it fails. |
86
+ | `<name> [--env FILE] [--migrate]` | Runs the app's `start` script as `npm start` would, without npm, in place of itself: from `lib/<name>`, with `node_modules/.bin` and Node first on `PATH`, and `NODE_ENV=production` unless the environment or the file says otherwise. With `--migrate`, runs `db:migrate` first and starts nothing when it fails; an app with no `db:migrate` script has nothing to migrate, so it starts (and says so), and one service command fits every app. |
87
87
  | `<name>-migrate [--env FILE]` | Runs the `db:migrate` script the same way. Only an app with one has it. |
88
88
  | `<name>-check [--env FILE] [--wait SECONDS]` | Exits 0 once `GET /api/health` answers 200 on `HOST`:`PORT` (127.0.0.1 when `HOST` is unset or any address; port 3000 when `PORT` is unset), and 1 after `--wait` seconds (60). |
89
- | `<name>-prepare --settings JSON --env FILE [--user NAME]` | Writes the settings, one JSON object of environment variables (each a string, a number or `true`/`false`), as `FILE`, mode 0600, owned by `NAME` (`chown NAME:`). It is for a host that hands an app's settings to a step run as root before its services start. |
89
+ | `<name>-prepare --settings JSON --env FILE [--user NAME]` | Writes the settings, one JSON object of environment variables (each a string, a number or `true`/`false`), as `FILE`, mode 0600, owned by `NAME` (`chown NAME:`). It also makes each folder the settings name for the app's files with an absolute path (`JSKIT_STORAGE_FS_BASE_PATH`, `AUTH_LOCAL_STORE_DIR`, `AUTH_OIDC_STORE_DIR`), mode 0700 when it makes it, owned by `NAME`. It is for a host that hands an app's settings to a step run as root before its services start. |
90
90
 
91
91
  ## Running it
92
92
 
93
93
  - The app's folder is read-only. A JSKIT package that writes files keeps them
94
94
  under the app's folder unless told otherwise, so point those at a writable
95
95
  data folder in the settings: `JSKIT_STORAGE_FS_BASE_PATH` (uploads and
96
- `runtime.storage`; `data/storage` by default) and `AUTH_LOCAL_STORE_DIR`
96
+ `runtime.storage`; `data/storage` by default), `AUTH_LOCAL_STORE_DIR`
97
97
  (local sign-in's files, such as the first account's setup code;
98
- `.jskit/auth` by default).
98
+ `.jskit/auth` by default) and, with OIDC sign-in, `AUTH_OIDC_STORE_DIR`.
99
+ `<name>-prepare --user NAME` makes those folders for the user the service
100
+ runs as, so a host's data folder that root owns needs nothing else. With
101
+ `JSKIT_STORAGE_DRIVER=s3`, `runtime.storage` writes to an S3 bucket instead,
102
+ as several instances need.
99
103
  - Secrets come at run time, in the env file or the service's environment,
100
104
  never in the package: anyone who can read the Nix store can read a package.
101
- - The server listens where its `server.js` says: in a JSKIT app, `HOST`
102
- (0.0.0.0) and `PORT` (3000).
105
+ - The server listens where its `server.js` says: `HOST` (0.0.0.0 in a JSKIT
106
+ app) and `PORT`, whose default differs between apps (3000 or 3001). Set
107
+ `PORT` in the settings, so the server, the check and the host agree.
103
108
  - The database comes from the app's own settings: `DATABASE_URL`, or
104
- `DB_CLIENT`, `DB_HOST`, `DB_NAME` and the rest, as the
105
- [database layer](/guide/app-setup/database-layer) guide says.
106
- - Migrations run with `<name>-migrate` before the release starts, or with
107
- `<name> --migrate` where the host has no such step. When several instances
108
- start at once, Knex's migration lock lets one run them; the others fail and
109
- start again once it is done, as a service restarted on failure does.
109
+ `DB_CLIENT`, `DB_HOST`, `DB_PORT`, `DB_NAME`, `DB_USER` and `DB_PASSWORD`
110
+ (each of these wins over the same part of the URL), as the
111
+ [database layer](/guide/app-setup/database-layer) guide says. Set
112
+ `DB_CLIENT` (`pg` or `mysql2`) when the app's own server code picks a
113
+ default for it. `/api/health` answers 200 only while the database answers,
114
+ so a host's check of a new build needs a database too.
115
+ - `APP_ENV` says where the app runs: `production`, or `staging` for a copy
116
+ that must not act for real, such as a build-check machine, which leaves the
117
+ `STAGING_*` addresses unset so everything is logged
118
+ ([Environments](/guide/framework/environments)). The host sets it: the
119
+ package never guesses it, and a server without one refuses to start. The
120
+ programs default `NODE_ENV` to `production`, the build the package holds.
121
+ - Behind a load balancer or proxy, `TRUSTED_PROXIES` names its addresses, as
122
+ the [authentication](/guide/app-setup/authentication) guide says, and
123
+ `APP_PUBLIC_URL` is the address people use.
124
+
125
+ Several instances of one app behind a load balancer need the same settings
126
+ on every instance and nothing kept on one server's disk: files in S3,
127
+ sessions in the database, the same secrets and setup code, and Redis for live
128
+ updates, as [Run on more than one server](/guide/app-setup/more-than-one-server)
129
+ says.
130
+
131
+ ## Migrations
132
+
133
+ Run them in the service: `<name> --migrate`. A host that runs no release step
134
+ needs nothing else, and a database that is not up yet only delays the start:
135
+ the migration fails, the service exits, and the host's restart tries again.
136
+
137
+ When several instances start at once, each runs `db:migrate` and Knex's own
138
+ lock lets one migrate. Knex takes that lock in the transaction its migrations
139
+ run in (every JSKIT migration runs in one), so:
140
+
141
+ - on PostgreSQL the others wait for that transaction, then find nothing left
142
+ to migrate and start. A migrator that dies has its transaction rolled back,
143
+ its lock with it, and the next start migrates;
144
+ - on MySQL and MariaDB a table change commits at once, so the others fail
145
+ (`Migration table is already locked`) and start again once their service
146
+ restarts them, and a migrator that dies part way leaves the lock set until
147
+ `knex migrate:unlock` frees it;
148
+ - on a database that has never been migrated, instances that start at the
149
+ same moment can both try to create Knex's own tables: one fails, and its
150
+ restart finds them there.
151
+
152
+ So every host that starts several instances must restart a failed one, and
153
+ migrations must be written to run next to the release before them while a
154
+ rollout replaces instances (add first, remove in a later release).
110
155
 
111
156
  A host that runs apps as Nix packages typically runs the app `shop` as:
112
157
 
113
158
  - a step run as root at each start, before the service:
114
159
  `bin/shop-prepare --settings <the host's settings file> --env /run/shop/env --user shop`;
115
- - one service, run as the user `shop`:
116
- `bin/shop --env /run/shop/env --migrate`, on port 3000, healthy when
117
- `/api/health` answers 200;
160
+ - one service, run as the user `shop` and restarted when it fails:
161
+ `bin/shop --env /run/shop/env --migrate`, on the port its `PORT` setting
162
+ names, healthy when `/api/health` answers 200;
118
163
  - a check after it starts: `bin/shop-check --env /run/shop/env`.
@@ -338,7 +338,7 @@ that advertises it.
338
338
 
339
339
  ## Host-only production cookies
340
340
 
341
- In production (`NODE_ENV=production`, served over HTTPS) the session cookies of
341
+ In staging and production (`APP_ENV`, served over HTTPS) the session cookies of
342
342
  the local, Supabase and OIDC auth providers and the CSRF secret cookie use the
343
343
  `__Host-` prefix, for example `__Host-jskit_local_access_token` and
344
344
  `__Host-_csrf`. Browsers accept these only with `Secure`, `Path=/` and no
@@ -370,3 +370,38 @@ defaults need no change and an unreachable read makes three requests, not nine.
370
370
  Writes and `requestStream()` have no default overall deadline. They accept an
371
371
  explicit `timeoutMs`; long-lived streams still need lifecycle cancellation.
372
372
  These safeguards do not limit request volume or govern raw `fetch` calls.
373
+
374
+ ## Say where the application runs: APP_ENV
375
+
376
+ `APP_ENV` (`development`, `test`, `staging` or `production`) replaces
377
+ `SIDE_EFFECTS` and every decision `NODE_ENV` used to make; `NODE_ENV` now only
378
+ picks optimised builds ([Environments](/guide/framework/environments)). A server
379
+ without a known `APP_ENV` refuses to start, naming the fix.
380
+
381
+ - Development: add `APP_ENV=development` to `.env` and to `.env.example`. The
382
+ foundations' `bin/develop.js` sets it, and their `dev` scripts run
383
+ `APP_ENV=development vite`; an older application adds the same.
384
+ - Staging: set `APP_ENV=staging` and `NODE_ENV=production`, plus
385
+ `STAGING_EMAIL_TO` and `STAGING_SMS_TO` for the testers' inbox and phone
386
+ (unset, email and SMS are logged). Staging needs production's secrets.
387
+ - Production: set `APP_ENV=production` and `NODE_ENV=production`, and remove
388
+ `SIDE_EFFECTS`.
389
+ - Tests: `jskit test` and the browser test servers set `APP_ENV=test`. A test
390
+ that passes its own environment object to a runtime, a provider or a gate adds
391
+ `APP_ENV`, since nothing guesses it; one that proves a real sender passes
392
+ `APP_ENV: "production"` instead of `SIDE_EFFECTS: "live"`.
393
+ - Code: `createConnectionService` and `createPaymentCheckoutService` take
394
+ `appEnv: appEnvironment(env)` in place of `sideEffects`, and require it. The
395
+ live-payment refusal's code is `payment_sandbox_only`, not
396
+ `payment_test_copy`. `@jskit-ai/kernel/shared/support/sideEffects`,
397
+ `@jskit-ai/kernel/server/runtime/sideEffects` and
398
+ `@jskit-ai/kernel/server/runtime/runtimeMode` are gone: read
399
+ `@jskit-ai/kernel/shared/support/appEnvironment` (`appEnvironment`,
400
+ `isDeployed`) and `@jskit-ai/kernel/server/runtime/appEnvironment`
401
+ (`deliverMessage`, `markStagingEmail`, `logSideEffect`,
402
+ `assertDevelopmentOnlySettings`). The page's meta tag is
403
+ `jskit-app-environment` and the banner's test id
404
+ `jskit-app-environment-banner`.
405
+ - An installed `auth.local.confirmation-sender` or `auth.local.recovery-sender`
406
+ receives `originalEmail` in staging; show it in the email
407
+ (`markStagingEmail`).
@@ -0,0 +1,174 @@
1
+ <!-- Generated by `npm run agent-docs:build` from `packages/agent-docs/site/guide/framework/environments.md`. Do not edit manually. -->
2
+
3
+ ---
4
+ title: Environments
5
+ description: APP_ENV says where an application runs, development, test, staging or production, and decides everything that depends on it; NODE_ENV only picks optimised builds.
6
+ ---
7
+
8
+ # Environments
9
+
10
+ Where an application runs is one environment variable, `APP_ENV`, with four
11
+ values. Everything JSKIT does differently by place reads it: what the
12
+ application sends, the development shortcuts, the security headers and the
13
+ banner on each page. `NODE_ENV` decides nothing else: it only picks optimised
14
+ builds, and staging and production set it to `production`.
15
+
16
+ | `APP_ENV` | Where | Email and SMS | Push, webhooks, connector writes | Payments | Development shortcuts | Banner |
17
+ | --- | --- | --- | --- | --- | --- | --- |
18
+ | `development` | a developer's machine, a session, a preview | logged | logged | sandbox only | allowed | "Development" |
19
+ | `test` | automated test runs | logged | logged | sandbox only | allowed | "Test" |
20
+ | `staging` | a deployed copy for checking the application | only to `STAGING_EMAIL_TO` and `STAGING_SMS_TO`; logged when unset | logged | sandbox only | refused | "Staging" |
21
+ | `production` | the application serving real people | sent | sent | live | refused | none |
22
+
23
+ The development shortcuts are dev sign-in (`AUTH_DEV_BYPASS_ENABLED`), cookies
24
+ without `Secure`, links in answers (`AUTH_LOCAL_RECOVERY_DEV_OUTPUT`), a first
25
+ account without a setup code, test databases, and a server that refuses to
26
+ start over a role grant no action requires (staging and production log it
27
+ instead). Staging and production add HSTS and the content security policy, and
28
+ need production's secrets, such as `AUTH_LOCAL_SESSION_SECRET` and a sender for
29
+ the emails they send.
30
+
31
+ ## Who sets what
32
+
33
+ The host sets `APP_ENV`; a request never chooses it. JSKIT's own commands set
34
+ it where they own the run.
35
+
36
+ | Who | `APP_ENV` | `NODE_ENV` | `STAGING_EMAIL_TO`, `STAGING_SMS_TO` |
37
+ | --- | --- | --- | --- |
38
+ | `npm run develop`, `npm run dev` on a developer's machine | `development`, set by the scripts | unset | unset |
39
+ | A hosted session or preview | `development` | unset | unset |
40
+ | `jskit test` and the browser test servers | `test`, set by JSKIT | anything | unset |
41
+ | A build-check machine that starts the packaged build to see it start and answer health | `staging` | `production` | unset, so everything is logged |
42
+ | A staging server | `staging` | `production` | the testers' inbox and phone |
43
+ | A production server | `production` | `production` | ignored |
44
+
45
+ A server started with `npm start` reads `APP_ENV` from its environment, or from
46
+ `.env` (`APP_ENV=development` on a developer's machine; the foundations'
47
+ `.env.example` says so). A host puts both variables in the server's
48
+ environment, beside its other settings:
49
+
50
+ ```sh
51
+ APP_ENV=staging NODE_ENV=production STAGING_EMAIL_TO=qa@example.com npm start
52
+ ```
53
+
54
+ `jskit test` and the browser test servers run as `APP_ENV=test` whatever the
55
+ shell says, and refuse to run where `APP_ENV` is `staging` or `production`.
56
+
57
+ ## Starting refuses what is not clear
58
+
59
+ A server stops at startup, saying what to do, when:
60
+
61
+ - `APP_ENV` is missing: `APP_ENV is not set. Set APP_ENV to development, test,
62
+ staging or production: development on a developer's machine or in a preview,
63
+ staging or production (with NODE_ENV=production) on a server.`
64
+ - `APP_ENV` is anything else: `APP_ENV is "dev", which is not an environment
65
+ JSKIT knows. Set APP_ENV to development, test, staging or production.`
66
+ - `APP_ENV` is `staging` or `production` and `NODE_ENV` is not `production`:
67
+ `APP_ENV is staging, so NODE_ENV must be production, and it is not set. Set
68
+ NODE_ENV=production: it only picks optimised builds, and APP_ENV decides
69
+ everything else.`
70
+ - `SIDE_EFFECTS`, which `APP_ENV` replaced, is set: `SIDE_EFFECTS was replaced
71
+ by APP_ENV: remove SIDE_EFFECTS. APP_ENV=production acts for real; staging
72
+ sends email and SMS only to STAGING_EMAIL_TO and STAGING_SMS_TO; development
73
+ and test only log.`
74
+ - A development-only setting is set in staging or production, for example
75
+ `AUTH_LOCAL_RECOVERY_DEV_OUTPUT is for development only and APP_ENV is
76
+ staging. Remove it from the staging environment.`
77
+
78
+ It then logs one line saying where it runs and what that means, such as
79
+ `APP_ENV is staging: email goes only to qa@example.com (STAGING_EMAIL_TO), SMS
80
+ is logged (STAGING_SMS_TO is not set); …`.
81
+
82
+ ## Staging's test addresses
83
+
84
+ Staging is production's code and settings with nobody real at the other end.
85
+ `STAGING_EMAIL_TO` is one address, such as the testers' shared inbox; every
86
+ email goes there instead of to its recipient. The email's first line, in its
87
+ text and HTML, says `Staging: this email was for ada@example.com.`, it carries
88
+ an `X-Staging-Original-To` header, and the log says
89
+ `Staging: the password reset email to ada@example.com was sent to
90
+ qa@example.com (STAGING_EMAIL_TO) instead.` `STAGING_SMS_TO` is one phone
91
+ number, and SMS goes there the same way. Unset, each is logged as development
92
+ logs it, with what a tester needs, such as its link. Push notifications,
93
+ webhooks and connector writes are always logged in staging, and live payments
94
+ are refused there, as everywhere but production.
95
+
96
+ ## What JSKIT does
97
+
98
+ - Every HTML page the server sends, and every page the Vite development server
99
+ sends, names its environment in a `jskit-app-environment` meta tag. The
100
+ shell (`@jskit-ai/shell-web`) shows a small "Development", "Test" or
101
+ "Staging" banner from it, with no application code, and none in production.
102
+ The banner never takes a click or a tap. Screen readers read it as a note,
103
+ with a sentence such as "Development: emails and messages are logged, not
104
+ sent; payments use the sandbox." as its description. It has no label, so a
105
+ browser test's `getByLabel("Email")` never finds it.
106
+ - Local-password emails (confirmation and password reset) with an installed
107
+ sender go out as the table says. In staging the sender gets
108
+ `STAGING_EMAIL_TO` as `email` and the person's address as `originalEmail`,
109
+ which its email should show (`markStagingEmail` does). An application that
110
+ confirms new accounts at once while its email isn't set up uses
111
+ `auth.local.confirmation-mode`, which applies everywhere (see
112
+ [Authentication](/guide/app-setup/authentication)).
113
+ - Workspace invitations with a provided mailer go out as the table says, the
114
+ staging copy marked with whom it was for. A logged invitation answers
115
+ `status: "simulated"`, and the inviter copies the link.
116
+ - Accounts-hub email goes out as the table says.
117
+ - Connector operations that act on the world (send an email or message, post,
118
+ book or cancel, charge, invoice, trigger another system's events) are marked
119
+ `sideEffect`. Outside production they are checked like real ones, then
120
+ logged, and `invoke` answers `{ simulated: true }`. Reads run for real.
121
+ `createConnectorsFeature` reads `APP_ENV` itself; give
122
+ `createConnectionService` and `createPaymentCheckoutService`
123
+ `appEnv: appEnvironment(env)`, which they require.
124
+ - Payment checkout and the billing portal in the live payment environment are
125
+ refused outside production with `payment_sandbox_only`; the sandbox
126
+ environment works everywhere.
127
+
128
+ Supabase sends its own authentication emails from Supabase's servers; JSKIT
129
+ cannot redirect them. Point development and staging at a separate Supabase
130
+ project.
131
+
132
+ ## The application's own side effects
133
+
134
+ Code the application writes that reaches the world follows the same rule. Send
135
+ email and SMS through `deliverMessage`, which sends, redirects or logs each as
136
+ `APP_ENV` says; log anything else outside production with `logSideEffect`.
137
+
138
+ ```js
139
+ import { deliverMessage, logSideEffect, markStagingEmail } from "@jskit-ai/kernel/server/runtime/appEnvironment";
140
+ import { appEnvironment } from "@jskit-ai/kernel/shared/support/appEnvironment";
141
+
142
+ function sendReminder({ env, logger, mailer }, message) {
143
+ return deliverMessage({
144
+ env,
145
+ logger,
146
+ channel: "email",
147
+ to: message.to,
148
+ summary: `the reminder to ${message.to}`,
149
+ details: { subject: message.subject },
150
+ send: (to, { originalTo }) => mailer.send(markStagingEmail({ ...message, to }, originalTo))
151
+ });
152
+ }
153
+
154
+ async function notifyDevice({ env, logger, push }, notification) {
155
+ const appEnv = appEnvironment(env);
156
+ if (appEnv !== "production") {
157
+ logSideEffect(logger, appEnv, {
158
+ kind: "push notification",
159
+ summary: `the reminder to device ${notification.deviceId}`,
160
+ details: { deviceId: notification.deviceId }
161
+ });
162
+ return { simulated: true };
163
+ }
164
+ return push.send(notification);
165
+ }
166
+ ```
167
+
168
+ - Read `env` from the `runtime.env` capability, never a request.
169
+ - A configured driver or key (`EMAIL_DRIVER=ses`, a live payment key) does not
170
+ make anything real; only `APP_ENV=production` does.
171
+ - Test each environment the code treats differently. A test that proves a real
172
+ sender against a fake transport passes `APP_ENV: "production"` (and
173
+ `NODE_ENV: "production"` when it starts a whole server), and a staging test
174
+ sets `STAGING_EMAIL_TO` and checks that only that address received it.
@@ -167,9 +167,9 @@ A consolidation drops behaviour there without a failing build.
167
167
 
168
168
  ### Production settings
169
169
 
170
- - `NODE_ENV=production` is JSKIT's only production switch. Without it a
171
- deployed server runs as development: no first-account setup code, and
172
- development-only settings accepted.
170
+ - A deployed server sets `APP_ENV=staging` or `production` and
171
+ `NODE_ENV=production` ([Environments](/guide/framework/environments)); only
172
+ then does local auth ask for a setup code and refuse development settings.
173
173
  - On an empty production database, local auth asks for the first account's
174
174
  setup code, so whoever reaches a new deployment first cannot own it ([The
175
175
  first account in