@jskit-ai/agent-docs 0.1.320 → 0.1.321

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 (30) hide show
  1. package/guide/agent/app-setup/database-layer.md +3 -0
  2. package/guide/agent/framework/data-access.md +147 -9
  3. package/package.json +1 -1
  4. package/patterns/feature-package/example/booking-engine/package.json +1 -1
  5. package/patterns/minimal-foundation/example/package.json +3 -3
  6. package/patterns/shell-foundation/example/package.json +4 -4
  7. package/patterns/shell-foundation/example/packages/main/package.json +1 -1
  8. package/reference/autogen/PATTERN_INDEX.md +38 -38
  9. package/reference/autogen/packages/resource-runtime.md +1 -1
  10. package/reference/autogen/packages/workspaces-core.md +7 -2
  11. package/skills/jskit/references/data-access.md +147 -9
  12. package/skills/jskit/references/database-layer.md +3 -0
  13. package/skills/jskit/references/heuristics/INDEX.md +1 -1
  14. package/skills/jskit/references/heuristics/data-access.md +4 -1
  15. package/skills/jskit/references/heuristics/row-policies.md +3 -1
  16. package/skills/jskit/references/pattern-index.md +38 -38
  17. package/skills/jskit/references/patterns/app/minimal-foundation/example/package.json +3 -3
  18. package/skills/jskit/references/patterns/app/shell-foundation/example/package.json +4 -4
  19. package/skills/jskit/references/patterns/app/shell-foundation/example/packages/main/package.json +1 -1
  20. package/skills/jskit/references/patterns/auth/accounts-hub/example/package.json +3 -3
  21. package/skills/jskit/references/patterns/auth/oidc-sign-in/example/package.json +2 -2
  22. package/skills/jskit/references/patterns/auth/supabase-auth/example/package.json +1 -1
  23. package/skills/jskit/references/patterns/connectors/calendar-cli/example/package.json +4 -4
  24. package/skills/jskit/references/patterns/crud/crud-feature-package/example/packages/books/package.json +3 -3
  25. package/skills/jskit/references/patterns/database/mysql-application/example/package.json +2 -2
  26. package/skills/jskit/references/patterns/database/postgres-application/example/package.json +2 -2
  27. package/skills/jskit/references/patterns/realtime/realtime-application/example/package.json +2 -2
  28. package/skills/jskit/references/patterns/server/feature-package/example/booking-engine/package.json +1 -1
  29. package/skills/jskit/references/patterns/users/user-administration-server/example/packages/users/package.json +3 -3
  30. package/skills/jskit/references/patterns/users/user-administration-server/example/packages/users-workspace/package.json +6 -6
@@ -330,6 +330,9 @@ the application's `knexfile.js`:
330
330
  prepare), the files run one at a time on the connected database. A copy
331
331
  named like the application's database or `PROD_DB_NAME`, or a server that
332
332
  cannot be reached, fails the run instead.
333
+ Every file starts from the snapshot, rows included: a copy an earlier file of
334
+ the run used is restored before the next file takes it, so no file sees what
335
+ another wrote.
333
336
  - The snapshot holds the schema as the engine reports it and the rows the
334
337
  migrations wrote, including the migration ledger. On MySQL and MariaDB it is
335
338
  each table's `SHOW CREATE TABLE` and rows, in
@@ -44,6 +44,7 @@ through its handle's calls, and every call takes the call's `options` last:
44
44
  | `patchMany(rows, options)` | many records, each with its own changes |
45
45
  | `patchWhere(filters, changes, options)`, `deleteWhere(filters, options)` | every matching row |
46
46
  | `lock(id, txOptions)`, `lockMany(ids, txOptions)`, `lockWhere(filters, txOptions)` | rows locked until the transaction ends |
47
+ | `lockPages(filters, txOptions)` | every matching row locked, handed over a page at a time ([Layer 4](#layer-4-locks)) |
47
48
  | `lockOrCreate({ id, ...initial }, options, work)` | a keyed record locked, created first when missing, for `work` |
48
49
  | `claim({ filters, sort, take }, txOptions)` | the next rows no other worker holds |
49
50
  | `withTransaction(options, work)` | one transaction; `work` gets its `txOptions` |
@@ -88,6 +89,7 @@ the filters match.
88
89
  | scoped query | The read-only Knex builder `scopedQuery()` returns, with ownership and the row policy already applied. |
89
90
  | system scope | Why a feature works across every workspace and user, declared with `systemScope`. |
90
91
  | system context | The context of that work, from `systemContext(reason)`; `forWorkspace`, `forUser` and `forWorkspaceUser` narrow it. |
92
+ | member context | The context of work done as one member of one workspace, from `workspaces.memberContext({ workspaceId, userId }, { context: systemContext(reason) })`: resolved as the member's request is, and signed in as them. |
91
93
  | `txOptions` | What `withTransaction(options, work)` hands `work`: only the call's context and its transaction, `{ context, trx }`, passed to every call inside. |
92
94
  | record | What a handle answers: `{ id, ...fields }`, relationships as ids, included records nested. |
93
95
 
@@ -333,6 +335,43 @@ code reads by the key with a filter and writes by it with an upsert,
333
335
  guide](/guide/app-setup/existing-application-migration#moving-hand-written-sql-behind-resources)
334
336
  has the migration for a table that exists already.
335
337
 
338
+ A table its feature counts but must never read back row by row (who took part
339
+ in an anonymous survey whose answers are stored without names) takes a `uuid`
340
+ primary key the code makes with `crypto.randomUUID()`, never a sequence, and a
341
+ unique index on the columns that identify a row, the ownership column among
342
+ them. A random v4 id carries no time and no order, and MySQL and MariaDB store
343
+ rows in primary key order, so the stored order is random too; a sequence or a
344
+ time-ordered id (UUIDv7, or the v1 that MySQL's and MariaDB's `UUID()` makes)
345
+ would let whoever reads the rows match them to the order of the answers.
346
+ PostgreSQL's heap keeps the order rows were inserted in, which a direct read
347
+ of the table shows whatever the key, so the protection that holds everywhere is
348
+ that no code reads the rows: the owning feature offers only an insert and a
349
+ count, functions of its own over its handle, and ownership keeps every other
350
+ feature from reading the table.
351
+
352
+ ```js
353
+ import { randomUUID } from "node:crypto";
354
+
355
+ // the owning feature's whole API over the table: record that a member took part, and count who did
356
+ const recordParticipation = ({ recordId, userId }, options) =>
357
+ participations.createMany([{ id: randomUUID(), recordId, userId }], {
358
+ ...options,
359
+ onConflict: { keys: ["workspaceId", "recordId", "userId"], action: "ignore" }
360
+ });
361
+ const countParticipants = async (recordId, options) => {
362
+ const [{ participants }] = await participations.aggregate({ measures: { participants: { count: "*" } }, filters: { recordId } }, options);
363
+ return participants;
364
+ };
365
+ ```
366
+
367
+ `onConflict` ignore makes a second participation a no-op that keeps the stored
368
+ row and its id, and a new row takes the id it brings. Only a clash on the keys'
369
+ unique index is ignored, so an id that is already another row's still fails.
370
+ Plain `create({ id: randomUUID(), recordId, userId }, options)` refuses the
371
+ second participation instead, with the unique key's error; inside a
372
+ transaction the failed insert fails the whole transaction (PostgreSQL aborts it
373
+ at once), so work that must go on uses the ignore.
374
+
336
375
  A route that reads or writes without an action (a provider's callback, a
337
376
  download it streams) takes its options from `await request.callOptions()`: the
338
377
  context the same request's action would get, its user, workspace and route
@@ -736,11 +775,14 @@ could update or hide another workspace's row. `merge` is refused under a row
736
775
  policy; use `ignore`, then `patchMany`. A table with no owners and no row
737
776
  policy, keyed by a natural id such as an email, may key the upsert on the id
738
777
  alone: `onConflict: { keys: ["id"], action: "merge" }`, with an id on every
739
- row, creates the record or replaces the fields given in one statement. Of two
740
- upserts at once the later stays, so a counter that must count both is created
741
- plainly and its duplicate retried instead. Inserting and catching the
742
- duplicate works only outside a transaction: inside one, the failed insert
743
- fails the whole transaction, so use an upsert there.
778
+ row, creates the record or replaces the fields given in one statement. A merge's
779
+ rows carry no other id; an `ignore`'s may, for ids the code makes (a random
780
+ `uuid`): a new row takes its own, and a row the keys match keeps the stored one
781
+ (see the count-only table above). Of two upserts at once the later stays, so a
782
+ counter that must count both is created plainly and its duplicate retried
783
+ instead. Inserting and catching the duplicate works only outside a transaction:
784
+ inside one, the failed insert fails the whole transaction, so use an upsert
785
+ there.
744
786
 
745
787
  A transaction whose insert loses a race for a unique key (another request with
746
788
  the same idempotency key committed first) runs once more:
@@ -828,6 +870,43 @@ const [booked] = await bookings.queryAll({ filters: { seatIds: [seatId] }, take:
828
870
  await seats.lock(seatId, txOptions);
829
871
  ```
830
872
 
873
+ A whole set of rows the work decides on (a register's entries, an account's
874
+ movements) is read the same way: every writer of the set first locks the one
875
+ row that governs it (the register, the account), and the work locks that row
876
+ before its first plain read. The plain reads after it (`queryAll`,
877
+ `queryPages`, `aggregate`) are then current and stay so until commit, at any
878
+ size, for one statement more. Work that joins its caller's transaction cannot
879
+ know whether the caller already read plainly, and on MySQL and MariaDB a lock
880
+ taken after that no longer moves the snapshot, so it reads the set with
881
+ locking reads, which see the latest rows whatever the snapshot:
882
+ `lockPages(filters, txOptions)` locks every row the filters select, a page of
883
+ up to 1,000 (`pageSize`) at a time, in id order, each page one locking read
884
+ after the last id of the page before. It still locks the governing row first,
885
+ since PostgreSQL keeps no new row out of a page already locked. In a request it
886
+ throws past the request row limit, as `queryPages` does. Isolation is not the
887
+ tool: MySQL and MariaDB fix it when a transaction starts, and READ COMMITTED
888
+ alone reads each page at a different moment. `patchWhere` and `deleteWhere`
889
+ select their rows with a locking read on MySQL and MariaDB, so they too see
890
+ what committed after the transaction's first plain read. With MariaDB's
891
+ `innodb_snapshot_isolation` on, a locking read that meets a row changed since
892
+ the snapshot fails (errno 1020) instead, and `withTransaction` runs the work
893
+ again when it opened the transaction.
894
+
895
+ ```js
896
+ // good: inside the caller's transaction, the register locked, then every entry locked and read as it is now
897
+ const closeRegister = (registerId, options) => registers.withTransaction({ ...options, joinOnly: true }, async (txOptions) => {
898
+ await registers.lock(registerId, { ...txOptions, lockMode: "share", fields: ["id"] });
899
+ let total = 0;
900
+ for await (const page of entries.lockPages({ registerId }, { ...txOptions, lockMode: "share", fields: ["amount"] })) {
901
+ total += page.reduce((sum, entry) => sum + entry.amount, 0);
902
+ }
903
+ return total;
904
+ });
905
+
906
+ // bad: a plain read inside a joined transaction reads the caller's snapshot, which can miss entries
907
+ const all = await entries.queryAll({ filters: { registerId } }, txOptions);
908
+ ```
909
+
831
910
  On MySQL and MariaDB a locking read locks every row it examines, so two rules
832
911
  apply there. Lock ids taken from your own scoped reads, never a list straight
833
912
  from a request: every id listed is locked, a hidden one too, before it is left
@@ -840,6 +919,8 @@ the ownership columns and the columns the filters fix to one value, it holds
840
919
  the sort's columns, ascending, and then nothing but the id, and the sort's
841
920
  fields go one way (all ascending or all descending). A unique key the filters
842
921
  fix whole, as a membership's workspace and user are, needs nothing more.
922
+ `lockPages` locks in id order, so its index holds nothing after the ownership
923
+ columns and the columns the filters fix but the id.
843
924
 
844
925
  A worker taking the next job uses `claim({ filters, sort, take }, txOptions)`,
845
926
  which skips rows another worker holds. A record that serializes work on a key
@@ -1296,6 +1377,38 @@ const SettlementFeature = defineFeature({
1296
1377
  request's `caller.userId`, so a job for a person sees what that person may
1297
1378
  see, and a job for no one its whole scope. Work on one person's rows only
1298
1379
  takes their scope (`forUser`, `forWorkspaceUser`) or filters its reads.
1380
+ - Work done as one member of a workspace, running what their own request would
1381
+ (an action that checks their permissions, code that reads `context.actor`):
1382
+ a record started for the person it is assigned to, a notification for one
1383
+ recipient, a submission synced for the person who made it. It asks
1384
+ workspaces-core for the member's context, never builds one by hand, since a
1385
+ hand-built context drifts from what a request resolves:
1386
+
1387
+ ```js
1388
+ // requires: { workspaces: "workspaces.core", actions: "runtime.actions" }; the feature declares systemScope
1389
+ const member = await workspaces.memberContext(
1390
+ { workspaceId: record.workspaceId, userId: record.assigneeId },
1391
+ { context: systemContext("start each scheduled record for the person it is assigned to") }
1392
+ );
1393
+ await actions.execute({ actionId: "crud.visits.create", input, context: { ...member, surface: "app" } });
1394
+ ```
1395
+
1396
+ `memberContext` resolves the member as their request on a `workspace_user`
1397
+ route does, by the same reads: the workspace, their active membership (the
1398
+ owner's ensured), their role's permissions and the settings. It refuses as
1399
+ that request does: a missing or deleted workspace is 404
1400
+ `workspaces.workspace.notFound`, and a user who is no active member of it
1401
+ (removed, inactive, never one, or no user) 403
1402
+ `workspaces.workspace.accessDenied`. The context signs the member in, unlike
1403
+ `{ actorId }`: `resolveActorId` answers them, an action checks their
1404
+ permissions, a row policy is told `caller.kind` "user", request row limits
1405
+ apply and other owners' hidden fields stay hidden. Its scope is the workspace
1406
+ and the member, so an event reaches that member there. Only a system context
1407
+ makes one, and an action it runs names the surface the member's request would
1408
+ use. It reads no sign-in state: JSKIT keeps no disabled flag on users, so an
1409
+ account its auth provider refuses at sign-in (a local account marked
1410
+ disabled) is still resolved; a job that must skip such people asks the
1411
+ provider first.
1299
1412
  - A job finds its units with an `aggregate` grouped by the ownership field,
1300
1413
  which a system context may group by (and count distinct) even when the table
1301
1414
  hides it, then runs each unit scoped to it. It reads the units in pages, all
@@ -1316,6 +1429,27 @@ const SettlementFeature = defineFeature({
1316
1429
  }
1317
1430
  }
1318
1431
  ```
1432
+ - A job whose units are the workspaces themselves (each workspace's daily
1433
+ digest, whether or not it has rows yet) pages through them with the
1434
+ `workspaces` owner API, never the table by name:
1435
+ `workspaces.listForSystem({ afterId, limit }, { context: systemContext(reason) })`
1436
+ answers the `{ id, slug }` of the workspaces that are not deleted, in id
1437
+ order, after `afterId` (from the first when it is null), at most `limit` (1 to
1438
+ `pageLimits().request`, 100 by default). Each page is one statement however
1439
+ many workspaces there are: pass a page's last id as the next `afterId`, and a
1440
+ page shorter than `limit` is the last. Any other context, a `forWorkspace`
1441
+ job's too, is refused, as are an `afterId` that is no workspace id and a
1442
+ `limit` out of range:
1443
+
1444
+ ```js
1445
+ // requires: { workspaceRecords: "workspaces" }; the feature declares systemScope
1446
+ for (let afterId = null; ;) {
1447
+ const page = await workspaceRecords.listForSystem({ afterId, limit: 100 }, { context: systemContext("send every workspace's daily digest") });
1448
+ for (const { id } of page) await sendDigest({ context: systemContext("send every workspace's daily digest").forWorkspace(id) });
1449
+ if (page.length < 100) break;
1450
+ afterId = page.at(-1).id;
1451
+ }
1452
+ ```
1319
1453
  - A job that must learn each row's owner across workspaces names the hidden
1320
1454
  ownership field in its read's `fields`, `queryAll({ filters, fields:
1321
1455
  ["workspaceId", "email"] }, { context })`, as a system context may do through
@@ -1331,9 +1465,10 @@ const SettlementFeature = defineFeature({
1331
1465
  its reason, read from the string literal at the call: a reason kept in a
1332
1466
  constant reads as none.
1333
1467
  - A system context comes only from `systemScope`, and a context's scope only
1334
- from the request or from `forWorkspace`, `forUser` and `forWorkspaceUser`:
1335
- lint refuses `createSystemContext` and hand-written `scopeValues`, and a
1336
- context naming two workspaces or two users throws.
1468
+ from the request, from `forWorkspace`, `forUser` and `forWorkspaceUser`, or
1469
+ from `workspaces.memberContext`: lint refuses `createSystemContext` and
1470
+ hand-written `scopeValues`, and a context naming two workspaces or two users
1471
+ throws.
1337
1472
  - A table only the feature's own background work reads and writes, whose rows
1338
1473
  belong to no workspace or user (an agent's runs, a server's leases, a queue
1339
1474
  keyed by something other than an owner), takes ownership `system`. Every call
@@ -1375,7 +1510,7 @@ never open one of its own, passes `{ ...options, joinOnly: true }`: called
1375
1510
  outside a transaction it throws a TypeError before the work runs. Code never
1376
1511
  asks whether it runs in a transaction: `withTransaction` joins the caller's,
1377
1512
  `outermost` refuses one, `joinOnly` requires one, and `lock`, `lockMany`,
1378
- `lockWhere` and `claim` refuse to run outside one; `lockOrCreate` opens its own
1513
+ `lockWhere`, `lockPages` and `claim` refuse to run outside one; `lockOrCreate` opens its own
1379
1514
  (and retries a lost insert race only then).
1380
1515
 
1381
1516
  ```js
@@ -1446,6 +1581,9 @@ workspaces. A report that reads people, their settings, workspaces or
1446
1581
  memberships beside its own rows joins `joinSource({ alias, filters },
1447
1582
  options)` of `users.profiles`, `users.settings`, `workspaces` or
1448
1583
  `workspaces.memberships` ([Layer 5](#layer-5-reports-the-resource-cannot-express)).
1584
+ A background job that visits every workspace pages them with
1585
+ `workspaces.listForSystem({ afterId, limit }, options)`, in its system context
1586
+ ([Background work](#background-work)).
1449
1587
 
1450
1588
  An app that had a table resource of its own over a JSKIT table (such as
1451
1589
  `users`) deletes it: its CRUD contract is built on the package's table resource
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/agent-docs",
3
- "version": "0.1.320",
3
+ "version": "0.1.321",
4
4
  "description": "Distributed JSKIT agent references, prompts, guides, and generated reference maps.",
5
5
  "type": "module",
6
6
  "files": [
@@ -5,7 +5,7 @@
5
5
  "private": true,
6
6
  "type": "module",
7
7
  "dependencies": {
8
- "@jskit-ai/kernel": "0.2.23",
8
+ "@jskit-ai/kernel": "0.2.24",
9
9
  "json-rest-schema": "^1.0.20"
10
10
  },
11
11
  "exports": {
@@ -34,7 +34,7 @@
34
34
  "dependencies": {
35
35
  "@local/main": "0.1.0",
36
36
  "@fastify/static": "^10.1.3",
37
- "@jskit-ai/kernel": "0.2.23",
37
+ "@jskit-ai/kernel": "0.2.24",
38
38
  "@jskit-ai/material-web": "0.1.6",
39
39
  "@tanstack/vue-query": "^5.101.0",
40
40
  "fastify": "^5.12.7",
@@ -44,13 +44,13 @@
44
44
  "vue": "^3.5.44",
45
45
  "vue-router": "^5.3.1",
46
46
  "vuetify": "^4.3.0",
47
- "@jskit-ai/http-runtime": "0.2.23",
47
+ "@jskit-ai/http-runtime": "0.2.24",
48
48
  "@fastify/fast-json-stringify-compiler": "^5.1.0",
49
49
  "@vue/devtools-api": "^8.2.1"
50
50
  },
51
51
  "devDependencies": {
52
52
  "@jskit-ai/config-eslint": "0.1.252",
53
- "@jskit-ai/jskit-catalog": "0.2.72",
53
+ "@jskit-ai/jskit-catalog": "0.2.73",
54
54
  "@playwright/test": "1.61.1",
55
55
  "@vitejs/plugin-vue": "^6.0.7",
56
56
  "eslint": "^10.8.0",
@@ -34,7 +34,7 @@
34
34
  "dependencies": {
35
35
  "@local/main": "0.1.0",
36
36
  "@fastify/static": "^10.1.3",
37
- "@jskit-ai/kernel": "0.2.23",
37
+ "@jskit-ai/kernel": "0.2.24",
38
38
  "@jskit-ai/material-web": "0.1.6",
39
39
  "@tanstack/vue-query": "^5.101.0",
40
40
  "fastify": "^5.12.7",
@@ -44,14 +44,14 @@
44
44
  "vue": "^3.5.44",
45
45
  "vue-router": "^5.3.1",
46
46
  "vuetify": "^4.3.0",
47
- "@jskit-ai/http-runtime": "0.2.23",
48
- "@jskit-ai/shell-web": "0.2.32",
47
+ "@jskit-ai/http-runtime": "0.2.24",
48
+ "@jskit-ai/shell-web": "0.2.33",
49
49
  "@fastify/fast-json-stringify-compiler": "^5.1.0",
50
50
  "@vue/devtools-api": "^8.2.1"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@jskit-ai/config-eslint": "0.1.252",
54
- "@jskit-ai/jskit-catalog": "0.2.72",
54
+ "@jskit-ai/jskit-catalog": "0.2.73",
55
55
  "@playwright/test": "1.61.1",
56
56
  "@vitejs/plugin-vue": "^6.0.7",
57
57
  "eslint": "^10.8.0",
@@ -9,7 +9,7 @@
9
9
  },
10
10
  "description": "App-local runtime composition and lightweight glue.",
11
11
  "dependencies": {
12
- "@jskit-ai/kernel": "0.2.23"
12
+ "@jskit-ai/kernel": "0.2.24"
13
13
  },
14
14
  "jskit": {
15
15
  "kind": "runtime",