@feltdb/feltkit 0.3.11 → 0.3.13

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 (146) hide show
  1. package/README.md +4 -3
  2. package/dist/app.d.ts +98 -3
  3. package/dist/app.js +63 -0
  4. package/dist/app.js.map +1 -1
  5. package/dist/appboundry/build.d.ts +39 -1
  6. package/dist/appboundry/build.js +57 -6
  7. package/dist/appboundry/build.js.map +1 -1
  8. package/dist/appboundry/guest.js +46 -5
  9. package/dist/appboundry/guest.js.map +1 -1
  10. package/dist/appboundry/host.d.ts +24 -5
  11. package/dist/appboundry/host.js +234 -141
  12. package/dist/appboundry/host.js.map +1 -1
  13. package/dist/appboundry/runtime.d.ts +44 -0
  14. package/dist/appboundry/runtime.js +99 -0
  15. package/dist/appboundry/runtime.js.map +1 -0
  16. package/dist/cli/entry.d.ts +1 -0
  17. package/dist/cli/entry.js +4 -0
  18. package/dist/cli/entry.js.map +1 -0
  19. package/dist/cli/main.d.ts +5 -0
  20. package/dist/cli/main.js +550 -13
  21. package/dist/cli/main.js.map +1 -1
  22. package/dist/cli/test-reporter.d.ts +9 -0
  23. package/dist/cli/test-reporter.js +26 -0
  24. package/dist/cli/test-reporter.js.map +1 -0
  25. package/dist/client/index.d.ts +3 -0
  26. package/dist/client/index.js +3 -0
  27. package/dist/client/index.js.map +1 -1
  28. package/dist/dashboard/assets/index-BSnW2DC-.js +15 -0
  29. package/dist/dashboard/assets/{index-91I1zDK-.css → index-BXTNQAg1.css} +1 -1
  30. package/dist/dashboard/index.html +3 -3
  31. package/dist/docs.d.ts +12 -0
  32. package/dist/docs.js +42 -0
  33. package/dist/docs.js.map +1 -0
  34. package/dist/errors.d.ts +15 -0
  35. package/dist/errors.js +9 -0
  36. package/dist/errors.js.map +1 -1
  37. package/dist/index.d.ts +1 -1
  38. package/dist/index.js.map +1 -1
  39. package/dist/platform/changes.d.ts +340 -0
  40. package/dist/platform/changes.js +355 -0
  41. package/dist/platform/changes.js.map +1 -0
  42. package/dist/platform/deployments.d.ts +653 -0
  43. package/dist/platform/deployments.js +532 -0
  44. package/dist/platform/deployments.js.map +1 -0
  45. package/dist/platform/operations.d.ts +74 -0
  46. package/dist/platform/operations.js +198 -0
  47. package/dist/platform/operations.js.map +1 -0
  48. package/dist/platform/platform.d.ts +78 -0
  49. package/dist/platform/platform.js +264 -0
  50. package/dist/platform/platform.js.map +1 -0
  51. package/dist/platform/server.d.ts +25 -0
  52. package/dist/platform/server.js +165 -0
  53. package/dist/platform/server.js.map +1 -0
  54. package/dist/platform/source.d.ts +27 -0
  55. package/dist/platform/source.js +142 -0
  56. package/dist/platform/source.js.map +1 -0
  57. package/dist/runtime/activity.d.ts +25 -0
  58. package/dist/runtime/activity.js +38 -12
  59. package/dist/runtime/activity.js.map +1 -1
  60. package/dist/runtime/agent.d.ts +16 -0
  61. package/dist/runtime/agent.js +92 -28
  62. package/dist/runtime/agent.js.map +1 -1
  63. package/dist/runtime/backup-environment.d.ts +32 -0
  64. package/dist/runtime/backup-environment.js +92 -0
  65. package/dist/runtime/backup-environment.js.map +1 -0
  66. package/dist/runtime/backup.d.ts +129 -0
  67. package/dist/runtime/backup.js +344 -0
  68. package/dist/runtime/backup.js.map +1 -0
  69. package/dist/runtime/capabilities.js +4 -2
  70. package/dist/runtime/capabilities.js.map +1 -1
  71. package/dist/runtime/changes.js +2 -2
  72. package/dist/runtime/changes.js.map +1 -1
  73. package/dist/runtime/contract.js +4 -3
  74. package/dist/runtime/contract.js.map +1 -1
  75. package/dist/runtime/data.d.ts +36 -5
  76. package/dist/runtime/data.js +165 -55
  77. package/dist/runtime/data.js.map +1 -1
  78. package/dist/runtime/decisions.js +2 -1
  79. package/dist/runtime/decisions.js.map +1 -1
  80. package/dist/runtime/instance.js +17 -0
  81. package/dist/runtime/instance.js.map +1 -1
  82. package/dist/runtime/integrations.js +14 -6
  83. package/dist/runtime/integrations.js.map +1 -1
  84. package/dist/runtime/loader.js +28 -4
  85. package/dist/runtime/loader.js.map +1 -1
  86. package/dist/runtime/migration-context.d.ts +25 -0
  87. package/dist/runtime/migration-context.js +69 -0
  88. package/dist/runtime/migration-context.js.map +1 -0
  89. package/dist/runtime/outbox.d.ts +6 -82
  90. package/dist/runtime/outbox.js +14 -59
  91. package/dist/runtime/outbox.js.map +1 -1
  92. package/dist/runtime/readiness.js +18 -1
  93. package/dist/runtime/readiness.js.map +1 -1
  94. package/dist/runtime/runtime.d.ts +84 -8
  95. package/dist/runtime/runtime.js +372 -131
  96. package/dist/runtime/runtime.js.map +1 -1
  97. package/dist/runtime/services.d.ts +41 -5
  98. package/dist/runtime/services.js +49 -4
  99. package/dist/runtime/services.js.map +1 -1
  100. package/dist/runtime/sessions.js +4 -3
  101. package/dist/runtime/sessions.js.map +1 -1
  102. package/dist/runtime/state-contract.d.ts +167 -0
  103. package/dist/runtime/state-contract.js +438 -0
  104. package/dist/runtime/state-contract.js.map +1 -0
  105. package/dist/runtime/transactions.d.ts +199 -0
  106. package/dist/runtime/transactions.js +246 -0
  107. package/dist/runtime/transactions.js.map +1 -0
  108. package/dist/schema.d.ts +13 -1
  109. package/dist/schema.js +21 -2
  110. package/dist/schema.js.map +1 -1
  111. package/dist/server/admin.d.ts +3 -2
  112. package/dist/server/admin.js +62 -10
  113. package/dist/server/admin.js.map +1 -1
  114. package/dist/server/index.d.ts +6 -1
  115. package/dist/server/index.js +90 -9
  116. package/dist/server/index.js.map +1 -1
  117. package/dist/server/mcp.js +1 -1
  118. package/dist/server/mcp.js.map +1 -1
  119. package/dist/server/project.d.ts +5 -0
  120. package/dist/server/project.js +13 -2
  121. package/dist/server/project.js.map +1 -1
  122. package/dist/testing/index.d.ts +10 -3
  123. package/dist/testing/index.js +33 -7
  124. package/dist/testing/index.js.map +1 -1
  125. package/docs/AGENT-OPERATIONS.md +515 -0
  126. package/docs/APPBOUNDRY.md +240 -0
  127. package/docs/ARCHITECTURE.md +126 -0
  128. package/docs/BACKUP.md +115 -0
  129. package/docs/GUARANTEES.md +457 -0
  130. package/docs/PATH-TO-PRODUCTION.md +117 -0
  131. package/docs/README.md +15 -0
  132. package/docs/SECURITY.md +182 -0
  133. package/docs/TRANSACTIONS.md +216 -0
  134. package/docs/UPSTREAM-GAPS.md +270 -0
  135. package/docs/WHY-FELTKIT.md +64 -0
  136. package/docs/assets/feltkit-logo-dark.png +0 -0
  137. package/docs/assets/feltkit-logo.png +0 -0
  138. package/package.json +10 -8
  139. package/skills/.claude-plugin/marketplace.json +1 -1
  140. package/skills/.claude-plugin/plugin.json +3 -3
  141. package/skills/feltkit-agent/SKILL.md +9 -5
  142. package/skills/feltkit-build/SKILL.md +16 -4
  143. package/skills/feltkit-data/SKILL.md +20 -3
  144. package/skills/feltkit-services/SKILL.md +13 -7
  145. package/dist/dashboard/assets/index-BhFOquA7.js +0 -15
  146. package/dist/dashboard/theme.js +0 -11
@@ -0,0 +1,240 @@
1
+ # The AppBoundry target
2
+
3
+ `feltkit build --target appboundry` compiles an application into an artifact that
4
+ [AppBoundry](https://github.com/rkendel1/appboundry) runs on its own. FeltKit produces the
5
+ artifact; AppBoundry loads, authorizes and executes it; FeltDB and AppPort Services hold
6
+ its state, reached only through AppBoundry's host interfaces.
7
+
8
+ ```bash
9
+ npx feltkit build --target appboundry # [--out <dir>]
10
+ ```
11
+
12
+ ## Running an artifact
13
+
14
+ - **AppBoundry serves it**, `@appport/appboundry` **1.3.0 or later**:
15
+ `npx @appport/appboundry@^1.3.0 run my-app.app --state <dir>`.
16
+ - **Older releases can't, or not with the same guarantees.** 1.1.x has no `run`. 1.2.x runs
17
+ the code but ignores `state.contract`, so FeltKit treats it as incompatible.
18
+ - **Checking compatibility.** `appboundry --version --json` reports the format, ABI and host
19
+ interfaces a runtime implements. FeltKit checks that report against the manifest
20
+ (`src/appboundry/runtime.ts`), both in `feltkit build --target appboundry`, for a runtime
21
+ installed in the project, and in the e2e test. An older runtime, or one that does not report
22
+ `stateContract: "enforced"`, is refused by name.
23
+ - **The e2e test.** `tests/e2e/appboundry-artifact.test.mjs` installs the published package
24
+ from npm (before publication, `APPBOUNDRY_TARBALL=<packed tarball>`), never an AppBoundry
25
+ checkout. It runs storage, authorization, the change stream, files, notifications,
26
+ configuration, jobs, webhooks and a restart.
27
+ - **The AppBoundry side.** AppBoundry's CI runs the same flow against its own packed tarball,
28
+ using a FeltKit application from npm.
29
+ - **`createArtifactTestApp(path)`** (`@feltdb/feltkit/testing`) runs it in an in-memory reference
30
+ host (`src/appboundry/host.ts`) with the same `as(who).query/mutation` calls as `createTestApp`,
31
+ so a project can run its own tests against the compiled module. It loads only `manifest.json`
32
+ and `module.wasm` (checking the module's SHA-256), speaks the guest ABI, checks access before
33
+ arguments, keeps records in memory with atomic per-call commits validated by the manifest's
34
+ schemas, and provides files, notifications and configuration (seed it with
35
+ `{ configuration: {...} }`); jobs and webhook events are recorded (`t.host.jobs`,
36
+ `t.host.webhookEvents`), not run. It is for verification, not serving.
37
+
38
+ ## What is in `my-app.app`
39
+
40
+ | File | Contents |
41
+ |---|---|
42
+ | `manifest.json` | format `appboundry.app/1`; guest ABI and host-interface versions; the application; every function (kind, access, argument and result JSON Schemas, the capability it publishes); every collection and its schema; roles, dev users and session lifetime; the services the code uses; the host permissions (collections, services, `network: "none"`, `filesystem: "none"`); the AppPort manifest; SHA-256 of the module, the program and every frontend file; and, in words, what the artifact leaves out |
43
+ | `module.wasm` | the guest engine (`appboundry/engine.wasm`, a JavaScript engine built from `appboundry/engine`) with the program in its `appboundry.program` section. It imports nothing |
44
+ | `assets/` | `dist/client`, the built frontend |
45
+
46
+ Every description comes from the loaded application: functions, collections and roles from
47
+ its definitions, and the AppPort contract from the same capability definitions the FeltKit
48
+ runtime serves (`src/runtime/capabilities.ts`). Nothing is described twice.
49
+
50
+ The program is your `feltkit/` modules bundled with FeltKit's guest runtime
51
+ (`src/appboundry/guest.ts`). The build loads the finished module the way AppBoundry will and
52
+ checks that it defines exactly the functions the build does.
53
+
54
+ ## What stays the same
55
+
56
+ Handlers run with the same `ctx`:
57
+
58
+ - **`ctx.db`**: reads and writes are AppBoundry operations. A mutation's writes are buffered
59
+ and commit atomically when it returns (nothing if it throws), with FeltKit's version
60
+ preconditions and read fences; reads see the mutation's own pending writes; a query cannot
61
+ write. Records are validated by the guest with your schemas, and again by AppBoundry with the
62
+ manifest's.
63
+ - **`ctx.auth`**: the principal and its permissions, from your roles. `access` is enforced by
64
+ AppBoundry before the handler runs.
65
+ - **`ctx.services`**: files, notifications, configuration, jobs (`enqueue`, run as the
66
+ enqueuer) and webhooks (`emit`), run by AppPort Services as the caller.
67
+ - Errors: `fail()` and `FeltKitError` reach the caller with the same codes and messages.
68
+ - The frontend: `@feltdb/feltkit/client` and `/react` work unchanged, live queries included.
69
+ - AppPort: the published capabilities are served at `/appport`.
70
+
71
+ ## Signing in, and where the data is
72
+
73
+ An artifact has no development sign-in in production and no FeltKit CLI beside it, so
74
+ `npx feltkit session create` does not apply to it. AppBoundry's operators create users with
75
+ the roles your app declares and issue their sessions (`appboundry admin users add`,
76
+ `appboundry admin sessions issue`); a production user is never asked to run a FeltKit
77
+ command. AppBoundry also decides where the data is: a local state directory, or a Managed
78
+ FeltDB environment from its own runtime configuration. The artifact, and your code, are the
79
+ same either way; FeltKit does not know which one runs it, and nothing is rebuilt to switch.
80
+
81
+ ## Schema lifecycle in an artifact
82
+
83
+ `manifest.json` carries `state.contract`: the FeltDB state contract the application's
84
+ collections compile to (`s.ref` references, unique constraints, indexes), at version 1.
85
+
86
+ **What a host does with it:**
87
+
88
+ - **First load.** The host installs the contract on FeltDB.
89
+ - **A later artifact version.** The host installs the new contract only when every stored
90
+ record satisfies it. Otherwise it refuses to load the new version and changes nothing, and
91
+ the previous version keeps running on the same data.
92
+ - **A populated collection the new version drops** is refused too, as on a FeltKit server.
93
+
94
+ **Migrations.** The application's `app.migration()`s are bundled into `module.wasm` with every
95
+ other function; no code travels as data, and there is no second migration format. The manifest
96
+ lists them by name, in declaration order:
97
+
98
+ ```json
99
+ "runtime": { "interfaces": { "...": 1, "migrations": 1 } },
100
+ "state": { "contract": { "...": "..." }, "migrations": { "format": "appboundry.migrations/1", "order": ["tasks get a priority"] } }
101
+ ```
102
+
103
+ Before serving a new artifact version, a host:
104
+
105
+ 1. runs each pending migration (one not yet recorded in the store's history) once, in order, in
106
+ the guest (invocation `{ migration: name }`). It runs through FeltKit's own migration context
107
+ (`src/runtime/migration-context.ts`, the one FeltKit's runtime uses): reads are the host
108
+ operation `migration.records` (application collections only), and writes are staged;
109
+ 2. commits the staged writes, the new contract and a history record in one FeltDB evolution,
110
+ in which FeltDB re-validates every record. Writes to a collection the new contract drops
111
+ go through an intermediate contract version, as on a FeltKit server;
112
+ 3. records what it actually applied: the migrations, records put and deleted, the contract
113
+ versions, and who authorized the start.
114
+
115
+ A migration that throws, or leaves a record the new contract refuses, refuses the new version and
116
+ changes nothing. The previous version keeps running on the same data. An applied migration never
117
+ runs again. Only `appboundry.migrations/1` is accepted; any other format is refused when the
118
+ artifact loads. An artifact without migrations doesn't require the `migrations@1` interface.
119
+
120
+ **The reference host does this:** `createArtifactTestApp(path, { stateDir })` runs successive
121
+ artifact versions on one FeltDB state directory. `test/schema-lifecycle-artifact.test.ts`
122
+ covers an unchanged schema, an optional field, a refused required field, a dropped
123
+ collection, and references and unique constraints answering as on a FeltKit server.
124
+
125
+ **AppBoundry (1.3.0 and later) does the same; 1.4.0 and later also runs the migrations.**
126
+ Before serving, it:
127
+
128
+ 1. validates the contract with FeltDB's validator;
129
+ 2. refuses one that redefines a platform (Services-owned) collection or doesn't match the
130
+ manifest's collections;
131
+ 3. composes it with the AppPort Services collections the artifact uses;
132
+ 4. runs the pending migrations (1.4.0), then installs or evolves the contract with
133
+ `db.stateContract.evolve`, the migrations' writes included.
134
+
135
+ A change the stored records don't satisfy (after the migrations), a failing migration, or one
136
+ that drops a populated collection, refuses to start with `SCHEMA_INCOMPATIBLE`. The error's
137
+ details list the changes, the migration and its error code or the failing record, and the
138
+ previous version and data stay usable. History is in `_appboundry_contract`.
139
+ `appboundry run --no-migrate` refuses any schema change or pending migration instead, for
140
+ deployments that authorize those separately. Authorizing a change and applying it are different
141
+ records: the host records what it applied, whoever allowed the start.
142
+
143
+ Contract refusals answer with FeltKit's codes:
144
+
145
+ | Refusal | Code |
146
+ |---|---|
147
+ | Duplicate unique value | `CONFLICT` |
148
+ | Missing or still-referenced record | `FAILED_PRECONDITION` |
149
+ | Other contract rules | `VALIDATION_FAILED` |
150
+ | Undeclared collection | `NOT_FOUND` |
151
+
152
+ `tests/e2e/state-contract-parity.test.mjs` requires FeltKit's runtime, the AppBoundry source
153
+ build and the AppBoundry package to give the same outcome and code for every step:
154
+
155
+ - a valid write;
156
+ - a missing required field;
157
+ - a duplicate unique value;
158
+ - a missing reference;
159
+ - deleting a referenced record;
160
+ - an undeclared collection;
161
+ - a restart;
162
+ - a compatible upgrade;
163
+ - an incompatible upgrade;
164
+ - the previous version again;
165
+ - a required field with a backfill migration;
166
+ - a field removed with a transformation migration;
167
+ - a failing migration (refused);
168
+ - the previous version again, unchanged, with no migration run twice.
169
+
170
+ The records each runtime leaves must be the same, too.
171
+
172
+ An artifact with migrations needs AppBoundry 1.4.0 or later: `feltkit build --target appboundry`
173
+ names an installed 1.3 runtime as not implementing `migrations@1`.
174
+
175
+ ## What the build refuses
176
+
177
+ Each of these makes the build fail, naming where it is used, rather than producing an
178
+ artifact that behaves differently from your application:
179
+
180
+ | Use | Why |
181
+ |---|---|
182
+ | `ctx.unsafe.feltdb` | it writes FeltDB around validation and transactions; an artifact reaches state only through `ctx.db` |
183
+ | `ctx.agent` | FeltKit's agent runs in the FeltKit server |
184
+ | `app.integration(...)` | integrations call external APIs; an artifact has no network access |
185
+ | `feltkit({ auth: { authenticate } })` | not yet supported; identity comes from AppBoundry sessions |
186
+ | `Intl`, `toLocaleString` / `toLocaleDateString` / `toLocaleTimeString`, `localeCompare` | the guest engine has no locale data (see below), so they would give different output than under `feltkit dev` |
187
+ | Node APIs (`node:*`, `fs`, …) in backend code | the artifact runs without Node |
188
+ | imports of `@feltdb/feltkit/<subpath>` in backend code | only `@feltdb/feltkit` exists in the guest |
189
+
190
+ Only the handler context is checked: `ctx.agent` (or the first parameter of a `handler`,
191
+ whatever it is named, and `({ agent })` destructured) is refused, while `roles.agent` or
192
+ `req.agent` is not. `ctx.unsafe`, `ctx.agent` and the locale methods also throw at run time if
193
+ reached some other way.
194
+
195
+ ## Sequences and after-commit effects (transactions@1)
196
+
197
+ An application that uses `ctx.db.sequence` or `ctx.afterCommit` (or opts a mutation into
198
+ `retry`) builds to an artifact that requires the host interface `transactions@1`; a host without it
199
+ refuses the artifact when it loads, and `feltkit build` names the AppBoundry release that has it
200
+ (1.5.0). The guest asks the host for `db.sequence { name }` (the host draws it in the mutation's
201
+ transaction) and, when the handler returns, `outbox.stage { effects }` (FeltKit's own
202
+ `afterCommit`, validated again by the host, with the caller and request taken from the call). The
203
+ host commits both with the mutation's writes, performs the effects after the commit as the caller,
204
+ recovers them after a restart, and lets an operator list and retry failed ones
205
+ (`appboundry admin outbox list | retry <id>`). A manifest function's `retry: { sequenceConflicts }`
206
+ is honored the same way as on a FeltKit server. The contract: [TRANSACTIONS.md](TRANSACTIONS.md). A plain `feltkit build`
207
+ prints the same list as a warning ("Not portable to --target appboundry").
208
+
209
+ ## The guest's ECMAScript surface
210
+
211
+ The guest engine is Boa (`boa_engine` 0.22, `appboundry/engine/Cargo.toml`) built without
212
+ its `intl` feature, to keep `engine.wasm` small and free of ICU data. ES2022 code runs as
213
+ compiled by esbuild (`target: es2022`). What differs from Node:
214
+
215
+ | Feature | In the artifact |
216
+ |---|---|
217
+ | `Intl.*` | refuses with `CONFIGURATION` ("Intl is not available in an AppBoundry artifact") |
218
+ | `Number/BigInt/Array.prototype.toLocaleString`, `Date.prototype.toLocaleString/DateString/TimeString` | refuse the same way, instead of formatting differently (`(12400).toLocaleString("en-US", …)` would be `"12400"`, not `"12,400.00"`) |
219
+ | `String.prototype.localeCompare` | refuses; compare with `<` and `>` (code-unit order) |
220
+ | Time zones | the host gives the clock (`abg_clock`, UTC milliseconds); there is no time-zone database, so format dates in the frontend or with explicit UTC arithmetic |
221
+ | `TextEncoder` / `TextDecoder` | UTF-8, installed by the guest |
222
+ | Node globals (`Buffer`, `process`, `require`) | absent; Node imports fail the build |
223
+
224
+ Format numbers and dates for people in the frontend, or with explicit code (`toFixed`,
225
+ your own digit grouping).
226
+
227
+ ## What is not in the artifact
228
+
229
+ The build lists these (`omitted` in the manifest) instead of pretending to carry them:
230
+ Studio and the MCP endpoint, API keys, schedules, intelligence, and webhook delivery
231
+ (`emit()` records the event; AppBoundry delivers nothing because the artifact has no network).
232
+
233
+ ## Rebuilding the engine
234
+
235
+ `appboundry/engine.wasm` is prebuilt so applications never need Rust. To rebuild it (Rust with
236
+ the `wasm32-unknown-unknown` target):
237
+
238
+ ```bash
239
+ node packages/sdk/scripts/build-appboundry-engine.mjs
240
+ ```
@@ -0,0 +1,126 @@
1
+ # Architecture
2
+
3
+ ```
4
+ Developer ─► feltkit (definitions, CLI) ─► FeltKitRuntime ─┬─► AppPort (capability boundary)
5
+ ├─► AppPort Services (Files)
6
+ └─► FeltDB instance for this environment
7
+ ```
8
+
9
+ Every arrow that reaches state ends at the same FeltDB instance. Operations take the
10
+ shortest correct path:
11
+
12
+ | Operation | Path |
13
+ |---|---|
14
+ | App query/mutation over HTTP | server → `runtime.call` → authority → `ctx.db` → FeltDB |
15
+ | Capability via AppPort or MCP | AppPort `HttpBinding`/MCP bridge → AppPort dispatch (validate → authorize → execute) → `runtime.call` → FeltDB |
16
+ | Service effect | `ctx.services.files` → AppPort Services `ServiceGateway` (authorize → effect → evidence) → FeltDB |
17
+ | Live query update | FeltDB operation journal → change feed → SSE → client re-runs the authorized query |
18
+ | Dashboard | admin API (admin token) → the same runtime and instance |
19
+
20
+ ## Components
21
+
22
+ | Module | Responsibility |
23
+ |---|---|
24
+ | `runtime/instance.ts` | `InstanceFactory`: the only `createFeltDB` call. One instance per environment; refuses duplicates and aliased environments in the process, and maps FeltDB's owner lock (`STATE_LOCKED`, another process) to `DUPLICATE_INSTANCE`. Hands out component handles. |
25
+ | `runtime/environment.ts` | Resolves an environment to its storage (local path, FeltDB server, memory). |
26
+ | `runtime/data.ts` | `ctx.db`. Read-only `Reader` for queries; `Writer` buffers writes and commits one FeltDB transaction with version preconditions. |
27
+ | `runtime/authority.ts` | Roles → permissions evaluated by AppPort's `permissionAuthorizer`; adapters for the function boundary and AppPort Services' `ServiceAuthorizer`. |
28
+ | `runtime/sessions.ts` | AppPort `SessionStore` implemented on FeltDB; token digests only. |
29
+ | `runtime/services.ts` | AppPort Services via `createServices({ db })` on the shared instance, checked with `services.usesInstance`; bindings for `ctx.services`; service states from `services.health()`; evidence lookups (`services.evidence.findByRunId`); configuration export/import; the durable-work tick. |
30
+ | `runtime/credentials.ts` | Credential custody, AppPort Services' `SecretCustody` (withSecret, store, status, remove): `credential-ref:<id>` → `FELTKIT_CREDENTIAL_<ID>` when set, else the environment's encrypted vault (`.feltkit/<env>/credentials.vault`; production only with `FELTKIT_CREDENTIAL_VAULT=1` + `FELTKIT_CREDENTIAL_VAULT_KEY`). One instance per runtime, given to `createServices({ credentials })`; values are resolved inside the effect only and written only through `credential.store`. |
31
+ | `runtime/agent.ts` | The FeltKit agent: FeltDB `defineAgent`/executions (`findAgentExecution`, `resumeAgentExecution`, `cancelAgentExecution`) + `openAgentState` (`listTasks`, `listHandoffs`) on the shared instance; actions only through AppPort and AppPort Services; the action lifecycle (compare-and-swap state changes on FeltDB agent memory, restart reconciliation from evidence); approvals; playbooks; handoffs. |
32
+ | `runtime/idempotency.ts` | AppPort's published `IdempotencyStore` on FeltDB: atomic in-progress claims (a FeltDB `requireAbsent` / `ifVersion` transaction), records, release, and lookup by request id, so a replay after a crash is "outcome unknown" and records survive a restart. Checked by `@appport/testing`'s conformance suite. |
33
+ | `runtime/activity.ts` | Durable activity log built from commit results; links to Services evidence. |
34
+ | `runtime/changes.ts` | Change feed from FeltDB's operation journal (kept over collection subscriptions: see docs/UPSTREAM-GAPS.md F1/F7). |
35
+ | `runtime/content.ts` | File bytes in `_feltkit_file_content`: stored before the file record, verified on read, removed with the file (FeltDB's content store is not used yet: no deletion, no memory runtime). |
36
+ | `runtime/runtime.ts` | Lifecycle, hot reload, `call()`, AppPort application construction. |
37
+ | `runtime/loader.ts` | Loads `feltkit/` (tsx in development, the esbuild bundle in production). |
38
+ | `server/` | HTTP server, AppPort binding, MCP endpoint (application tools + `feltkit_agent_*`), admin API, dev/build/start/deploy workflows. |
39
+ | `server/management.ts` | AppPort Services' `createManagementRouter`: embedded in the admin API for Studio's Services screens (as a dev identity), and the services console on a loopback origin, authenticated by FeltKit sessions. |
40
+ | `client/`, `react/` | Browser client, typed references, live queries, hooks. |
41
+ | `cli/` | The `feltkit` command, including `feltkit skills`. |
42
+ | `skills/` | The FeltKit Agent Skills and their Claude Code plugin manifest (shipped in the tarball). |
43
+ | `dashboard/` | The dashboard SPA (built into `dist/dashboard`). |
44
+
45
+ ## Component handles
46
+
47
+ `instance.handle("appport-services")` returns a `Proxy` over the environment's
48
+ `StateFirstDB`. Calls forward to the same object unchanged; the proxy records which
49
+ collections each component reached and refuses `close()` (the runtime owns the
50
+ lifecycle). Tests use `instance.owns(x)` to prove a component's database *is* this
51
+ instance, not one with matching configuration.
52
+
53
+ Components and the handles they receive:
54
+
55
+ | Component | Handle |
56
+ |---|---|
57
+ | Application functions | `app` (and `app.unsafe` for the escape hatch) |
58
+ | AppPort application (`createApplication({ db })`) | `appport` |
59
+ | AppPort Services stores, gateway evidence | `appport-services` |
60
+ | File bytes | `feltkit.files` |
61
+ | Sessions | `feltkit.sessions` |
62
+ | Activity log | `feltkit.activity` |
63
+ | Change feed | `feltkit.changes` |
64
+ | Dashboard / admin API | `feltkit.admin` |
65
+ | FeltKit agent (FeltDB agent definition, executions, agent state) | `feltdb.agent` |
66
+ | AppPort idempotency records | `appport.idempotency` |
67
+ | Services console | none of its own: it mounts the services above, which hold `appport-services` |
68
+
69
+ ## How AppPort Services is constructed
70
+
71
+ `runtime/services.ts` calls the package's own `createServices({ db })` with the shared
72
+ instance's `appport-services` handle and FeltKit's `ServiceAuthorizer`, then refuses to
73
+ start unless `services.usesInstance(handle)` is true and `services.ownsDb` is false (the
74
+ services run on the host's instance and never opened their own). Everything else FeltKit
75
+ needs from the package is public API (`@appport/services` 0.9.1; these APIs arrived in 0.6.0, the credential write path in 0.8.0, the contract fragment in 0.9.0 and its indexes in 0.9.1):
76
+
77
+ - service state: `services.health({ tenantId })` (counts and reasons only; a trusted
78
+ in-process read that authorizes nothing and writes nothing);
79
+ - effect evidence for a request: `services.evidence.findByRunId(requestId)`, where the
80
+ request id is the `runId` FeltKit gives every service effect inside it; `started` with no
81
+ completion means the outcome is unknown;
82
+ - the agent's delivery diagnosis: `invoke('webhooks.deliveries.read')` as the task owner;
83
+ - `feltkit config export|import`: `services.configuration.export/import`;
84
+ - storing a credential value: `invoke('credential.store')` as the caller, with FeltKit's
85
+ `SecretCustody` given to `createServices({ credentials })` (runtime/credentials.ts);
86
+ - the version: `VERSION`.
87
+
88
+ One private read remains: `credentialReferences()` reads `ConfigurationSecrets` (names and
89
+ references) to report whether each reference resolves in FeltKit's custody, because the
90
+ public reads are authorized effects that write evidence (docs/UPSTREAM-GAPS.md, S3b).
91
+ `test/services.test.ts` and `test/services-released.test.ts` verify each service and each
92
+ of these APIs against the real package on the shared instance.
93
+
94
+ ## One copy of FeltDB
95
+
96
+ FeltKit depends on `@feltdb/core` `>=0.20.0 <1.0.0` and `@appport/services` `>=0.13.1 <1.0.0`, and
97
+ ships npm's latest of both. FeltDB 0.20 refuses `collection.find()` past 1,000 matches; FeltKit reads
98
+ every multi-record result in `db.query` pages instead (docs/UPSTREAM-GAPS.md, "Review 2026-11-03"). AppPort Services takes `@feltdb/core` as a peer dependency, so it uses the
99
+ application's copy and npm installs exactly one, with no `overrides`. Two module copies in one process
100
+ would mean two runtimes and two sets of error classes, which is why Services must not bring its own
101
+ (releases before 0.11.0 pinned one exactly; with those, a project needed an override, and `feltkit
102
+ doctor` now says to update Services instead).
103
+
104
+ `feltkit doctor`, the first-run test, and the architecture test all fail if more than
105
+ one copy resolves, and `npm run test:release` fails on a second copy, on any override, or if the
106
+ installed Services release does not take `@feltdb/core` as a peer dependency.
107
+
108
+ ## Request lifecycle of a mutation
109
+
110
+ 1. Parse and validate arguments against the function's `args` schema.
111
+ 2. Authorize: AppPort's authorizer evaluates the caller's roles against `access`.
112
+ 3. Run the handler with a `Writer`. Reads see pending writes; writes are validated and buffered.
113
+ 4. Validate the return value against `returns`, if declared.
114
+ 5. Commit one FeltDB transaction: inserts as `requireAbsent`, updates/deletes with
115
+ `ifVersion`, and unwritten point reads as preconditions. A failed precondition
116
+ rejects the entire transaction (`CONFLICT`).
117
+ 6. Nudge the change feed; record activity with the commit's transaction id, revision, and writes.
118
+
119
+ ## Lifecycle
120
+
121
+ `FeltKitRuntime.start` opens the instance, builds every component, probes the
122
+ instance, checks services, and starts the change feed; if any step fails, the
123
+ instance is closed before the error propagates. `reload` swaps definitions (functions,
124
+ policy, AppPort application) without touching state. `close` stops the change feed,
125
+ waits for in-flight calls, flushes the activity log, and closes the instance.
126
+ `feltkit dev` and `feltkit start` handle SIGINT/SIGTERM with a 10-second deadline.
package/docs/BACKUP.md ADDED
@@ -0,0 +1,115 @@
1
+ # Application backup and restore
2
+
3
+ Status: **implemented** for local FeltDB state (development, test, and production on a local
4
+ volume). The audit below comes first: it records what FeltDB already guarantees, measured on
5
+ FeltDB 0.18, before deciding what FeltKit adds.
6
+
7
+ ## 1. Audit: what FeltDB provides
8
+
9
+ | Path | What it is | Application-level? | Covers FeltKit's local store? |
10
+ |---|---|---|---|
11
+ | `FeltDb::write_backup` / `restore_backup` (Rust engine) | Physical backup of one engine database. Staged to `<path>.partial` and renamed when complete; restore writes `<target>.restoring`, reopens it, checks its meaning digest, and renames it into place | No: a database, without the application it belongs to | **No.** FeltKit's local state is the JS file store (`createFeltDB({ mode: "local" })`, `packages/core/src/file-db.ts`), not the Rust engine |
12
+ | `feltdb-server backup create\|verify\|restore`, `POST /admin/backups` | Physical bundle of a whole server's state: files with SHA-256s, provenance, staged and fsynced, atomic publish. Restore refuses an existing target, and an interrupted restore leaves `*.restore.partial` | No: every application and tenant on the server, operator-level | No (server only) |
13
+ | `<state>.backup` in the JS file store | The previous snapshot, kept to recover from a torn write | No: crash recovery inside one store, not a user backup | Internal |
14
+ | `db.exportOperations()` (JS) | The local operation journal | No | A building block: it names every collection a local store has written |
15
+ | `db.stateContract.active()` / `evolve()` | The installed contract (document, version, hash). Measured: `evolve` installs any version on an empty store, and versions may jump (3, then 5) | — | Yes |
16
+
17
+ So FeltDB's physical backups are sound (staged, verified, atomic publish, refusal to overwrite),
18
+ but they don't cover the store FeltKit runs on locally. They also don't know what an application
19
+ is: which collections belong to it, which records are credentials, what schema and migrations its
20
+ data is at, or whether a store is safe to resume. **Local and managed or server FeltDB do not have
21
+ equivalent backup semantics.** A server bundle is a whole server; nothing in FeltDB backs up one
22
+ application.
23
+
24
+ **What a populated FeltKit store holds** (measured: FeltDB's operation log after a mutation with a
25
+ sequence, after-commit effects and a configuration write):
26
+
27
+ | Kind | Collections | In a backup |
28
+ |---|---|---|
29
+ | Application records | the app's collections (`invoices`, …) | yes |
30
+ | FeltKit platform | `_feltkit_migrations` (applied migrations: history and identity), `_feltkit_outbox` (after-commit effects, pending ones included), `_feltkit_sequences`, `_feltkit_collections`, `_feltkit_activity`, idempotency records | yes |
31
+ | AppPort Services | jobs, webhook endpoints and deliveries, notifications and deliveries, configuration, files, their audit records, `service_effect_evidence` | yes |
32
+ | Credentials | `_feltkit_sessions` | **no:** a restored application issues new sessions |
33
+ | FeltDB internals | `__indexes__`, `_feltdb.state_contract`, `_flow_*` agent state | **no:** the contract travels as a document and is reinstalled with `stateContract.evolve`; indexes are rebuilt by FeltDB; FeltDB's agent state is FeltDB's own |
34
+
35
+ ## 2. The portable unit: application state
36
+
37
+ A backup is **one application's state**: its contract (document, version, hash), its migration
38
+ history, its records, its platform and Services records, and its operational state (outbox,
39
+ sequences). It carries enough context to decide whether it can safely resume somewhere else. It is
40
+ not a copy of database files. FeltDB stays responsible for durable storage and its guarantees:
41
+ transactions, `requireAbsent`, contract enforcement. FeltKit defines what a recoverable application
42
+ is, and reads and writes it through FeltDB's public API.
43
+
44
+ ## 3. Format: `feltkit.backup/1`
45
+
46
+ Newline-delimited JSON, the same envelope as AppBoundry's `appboundry.backup/1`:
47
+
48
+ ```
49
+ {"format":"feltkit.backup/1","createdAt":…,"application":{"name","version"},"environment":…,
50
+ "feltkit":…,"feltdb":…,"contract":{"name","version","hash","document"},"migrations":[…],
51
+ "collections":[{"name","records"}],"excluded":[…]}
52
+ {"c":"invoices","r":{…}} one line per record, by collection then id
53
+ {"end":true,"records":N,"sha256":"…"} SHA-256 over every preceding line
54
+ ```
55
+
56
+ Record contents and ids are kept. FeltDB assigns new record versions on restore, so version
57
+ guards (the outbox claim, sequences) start from the restored records' new versions.
58
+
59
+ ## 4. Lifecycle
60
+
61
+ | Command | What it does |
62
+ |---|---|
63
+ | `feltkit backup create [--env] [--out]` | Writes the environment's backup (default `.feltkit/backups/<env>/<app>-<time>.ndjson`). A running development server is paused for the read: new calls wait and in-flight calls finish, so the backup holds each transaction entirely or not at all. A stopped environment is read directly through FeltDB, with no runtime (no worker changes state mid-read) |
64
+ | `feltkit backup verify <file>` | The whole file: format, digest, counts, the contract parses and its hash matches, and the migrations listed are in the records |
65
+ | `feltkit backup list [--dir]` | The backups in a directory, with application, environment, time, contract version and record count, and whether each verifies |
66
+ | `feltkit backup restore <file> --env <env>` | Into the environment's **empty** local state only |
67
+
68
+ **Size.** Every step reads and writes a line or a FeltDB page (1,000 records) at a time, never a whole
69
+ collection or file:
70
+
71
+ - `create` counts each collection, then streams its records in cursor-paged reads to
72
+ `<out>.partial`. A running server streams the lines over its admin API as NDJSON; a failure after
73
+ the first line ends the stream with an `{"error": …}` line, and a stream cut short has no end
74
+ record, which verification refuses. The partial file is verified from disk, then renamed. A
75
+ failure removes it.
76
+ - `verify`, `list` and `restore` read the file line by line. `restore` verifies the whole file
77
+ first, then reads it again to write, and checks the digest again on the way, so a file changed in
78
+ between is not marked complete.
79
+ - Not paged: collections are found from FeltDB's operation journal, which FeltDB returns whole (it
80
+ has no collection listing; docs/UPSTREAM-GAPS.md F19).
81
+
82
+ Tested with 1,001 records in a collection (`test/large-collections.test.ts`), and through
83
+ `change.verify` and the deployment cutover (`test/platform-large-state.test.ts`).
84
+
85
+ **Restore, in order:**
86
+
87
+ 1. Verify the whole file (read line by line).
88
+ 2. Refuse a different application (the project's name) or a non-empty target, before anything is
89
+ written.
90
+ 3. Write the marker `_feltkit_restore` as `in-progress`.
91
+ 4. Install the backup's exact contract (same version and hash).
92
+ 5. Write the records in batches of 200 with `requireAbsent`, so FeltDB checks each against the
93
+ contract, reading the file again a line at a time.
94
+ 6. Check the file's digest again, each collection's count (counted in pages), and the contract hash.
95
+ 7. Mark the restore `complete`.
96
+
97
+ **The runtime refuses to start on a state whose restore marker is not complete**, and a second
98
+ restore refuses the now non-empty target. So an interrupted restore can't be mistaken for a
99
+ restored application.
100
+
101
+ A restored application starts like the original: same contract and migration history, so no
102
+ schema change is pending unless the project changed since. Its pending outbox work is recovered by
103
+ its worker. Sessions are re-issued.
104
+
105
+ ## 5. Not covered
106
+
107
+ - **Managed or server FeltDB** (`FELTDB_URL`). Backup and restore are refused explicitly. Through a
108
+ remote authority FeltKit can't enumerate collections from a local journal, and an
109
+ application-scoped key may not read FeltDB's internals; that path is not verified. For a whole
110
+ server, use `feltdb-server backup` (a physical bundle).
111
+ - **A running production server.** Its admin API admits only verified operators. Stop it, or back up
112
+ its local volume's environment directly.
113
+ - **Restoring a FeltKit backup into AppBoundry, or the reverse.** The envelope is the same, but the
114
+ platform collections differ (`_feltkit_*` / `_appboundry_*`).
115
+ - Out of scope by design: a backup storage service, replication, point-in-time recovery.