letopis 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/AGENT-CHEATSHEET.en.md +117 -10
  2. package/AGENT-CHEATSHEET.md +110 -8
  3. package/CHANGELOG.md +739 -0
  4. package/README.en.md +873 -68
  5. package/README.md +830 -66
  6. package/dist/admin.d.ts +24 -4
  7. package/dist/admin.js +39 -9
  8. package/dist/cache.d.ts +33 -9
  9. package/dist/cache.js +81 -32
  10. package/dist/chain.d.ts +29 -7
  11. package/dist/chain.js +61 -12
  12. package/dist/cli.js +59 -7
  13. package/dist/errors.js +4 -2
  14. package/dist/import.d.ts +6 -0
  15. package/dist/import.js +196 -175
  16. package/dist/index.d.ts +27 -5
  17. package/dist/index.js +191 -29
  18. package/dist/internal.d.ts +16 -0
  19. package/dist/internal.js +14 -0
  20. package/dist/load.d.ts +63 -0
  21. package/dist/load.js +331 -44
  22. package/dist/mcp.d.ts +28 -0
  23. package/dist/mcp.js +810 -0
  24. package/dist/model.d.ts +34 -0
  25. package/dist/model.js +17 -0
  26. package/dist/project.d.ts +309 -0
  27. package/dist/project.js +1097 -0
  28. package/dist/registry.d.ts +17 -0
  29. package/dist/registry.js +13 -0
  30. package/dist/signals.d.ts +73 -0
  31. package/dist/signals.js +169 -0
  32. package/dist/sql.d.ts +1 -1
  33. package/dist/sql.js +26 -7
  34. package/dist/tx.d.ts +9 -7
  35. package/dist/tx.js +15 -5
  36. package/dist/typed.d.ts +21 -6
  37. package/dist/types.d.ts +77 -4
  38. package/dist/types.js +8 -3
  39. package/dist/up.d.ts +16 -2
  40. package/dist/up.js +34 -4
  41. package/dist/uuid.js +19 -8
  42. package/dist/watch.d.ts +23 -2
  43. package/dist/watch.js +44 -7
  44. package/dist/write.d.ts +11 -2
  45. package/dist/write.js +57 -10
  46. package/llms.txt +9 -2
  47. package/package.json +13 -2
  48. package/sql/10-core.sql +13 -2
  49. package/sql/15-errors.sql +4 -1
  50. package/sql/40-class.sql +57 -2
  51. package/sql/60-write.sql +611 -82
  52. package/sql/70-read.sql +33 -8
  53. package/sql/75-history.sql +358 -0
  54. package/sql/80-auth.sql +7 -7
  55. package/sql/88-derived.sql +1154 -0
  56. package/sql/90-time.sql +134 -21
  57. package/sql/95-seed.system.sql +24 -6
  58. package/sql/99-revision.sql +1 -1
  59. package/sql/README.md +5 -1
  60. package/sql/migrate/0006-changed-paths.sql +3 -0
@@ -1,4 +1,4 @@
1
- # letopis 1.0 — agent cheatsheet
1
+ # letopis 1.2 — agent cheatsheet
2
2
 
3
3
  One page instead of [README.en.md](README.en.md), for an AI agent that writes code on top of the package. It ships in
4
4
  the package next to the README; in Russian: [Русская версия](AGENT-CHEATSHEET.md). Migration from 0.21:
@@ -41,6 +41,14 @@ const r = await up({ dsn: ADMIN, schema: 'myapp', // schema 'v2
41
41
  policy), `allowReset`, `serviceKey`, `quiet`, `pglite: true` (a database for development without PostgreSQL; then
42
42
  `connect({ dsn: r.pglite.dsn, listen: false, max: 1, allowBypassRls: true, … })` — a superuser connection, RLS
43
43
  does not apply on it).
44
+ - `signals` — what `NOTIFY` signals show to any role that can connect to the database. The channel is the full
45
+ schema name (`LISTEN "v2.myapp"`); the write signal payload: `'class'` (default) — `Class`; `'tenant'` —
46
+ `Class@tenant token` (the cache is cleared only in the tenant of the write); `'opaque'` — `class token@tenant
47
+ token`; `'none'` — no signals (the cache works only with `ttlOnly`, subscriptions poll the log). Service signals:
48
+ `session:<16 hex>` is a revoked session, `session:*` and `*` are resets (`reset`). The mode is stored in the
49
+ schema: `up()` without the option keeps it; after a change, restart the processes. The signal key (precise cache
50
+ clearing) goes to services — the `letopis.signals` permission. The `up()` result also has `signals` and `warnings`
51
+ (PostgreSQL below 18.2 — upgrade).
44
52
  - `tenant` — the first tenant and user without a seed of your own: the tenant gets the rule "authenticated users may
45
53
  do everything" (`grantAll`, on by default, weight 10); the user gets a membership with the roles `user.roles`
46
54
  (`['owner']` by default), a password for signing in with the login `login`, and a session in the tenant. Repeating
@@ -108,8 +116,9 @@ export const Payment = link('Payment', { opId: z.string(), amount: z.number().in
108
116
  export const Note = hub('Note', { text: z.string() }, {}) // no key: the database assigns the id
109
117
  ```
110
118
  - Full form: `hub(name, { alias, extends, abstract, ends, fields: z.object(…) | { fields }, key, onDelete, history,
111
- ownerDefault: 'tenant' | 'actor', cache: { ttl }, meta })`; `link` works the same way. `extends` also works in
112
- the short form, but TypeScript infers the data types of a descendant only in the full form.
119
+ ownerDefault: 'tenant' | 'actor', cache: { ttl }, meta, derivedBy, aggregate })`; `link` works the same way.
120
+ `derivedBy` and `aggregate` are in sections 12 and 13 below. `extends` also works in the short form, but TypeScript
121
+ infers the data types of a descendant only in the full form.
113
122
  - Ends: `one(A)` (required), `one(A, B)` (union), `one('Name')` (target by name, for a class that refers to itself),
114
123
  `.optional()`, `.onDelete('restrict' | 'cascade' | 'unset')` (default `restrict`; `unset` only for an optional
115
124
  end), `many(A, { min, max, onDelete })`.
@@ -203,10 +212,14 @@ const rep = await adm.load({
203
212
  }, { existing: 'skip' }) // duplicates 'error'|'first'|'last', existing 'error'|'skip'|'update', commitEvery, withHistory, verify, tenant, as
204
213
  // rep: { rows, loaded, updated, skipped, duplicates, byClass, external, portions, ms }
205
214
  ```
215
+ Initial load: `adm.load(rows, { initial: true })`, `letopis load --initial`, `letopis import --initial` — only into an
216
+ empty schema (otherwise `not_empty`), in one transaction; about 2.2 times faster than a regular load on a million
217
+ objects; no `commitEvery`, `existing` or `{ as }`.
218
+
206
219
  NDJSON files (one object per line; all files form one load):
207
220
  ```sh
208
221
  npx letopis load shops.ndjson wallets.ndjson --dsn "$ADMIN" --schema v2.myapp --token "$TOKEN" [--class Wallet] \
209
- [--duplicates first] [--existing skip] [--commit-every 100000] [--verify]
222
+ [--duplicates first] [--existing skip] [--commit-every 100000] [--verify] [--initial]
210
223
  # {"$class":"Shop","name":"South"}
211
224
  # {"$class":"Wallet","number":"W-10","Shop":{"$key":["South"]}}
212
225
  # {"$class":"Note","text":"x","$ext":"n1"} ← $ext is the row's external key for references { "$ext": "n1" }
@@ -272,7 +285,15 @@ const was = await db.Wallet(w).asOf('2026-10-01T00:00:00Z').first() // as it w
272
285
  await db.Shop(shop).Wallet().asOf(new Date('2026-10-01')).rows()
273
286
  const history = await db.Wallet(w).versions() // all versions; the deletion version has $deleted
274
287
  await db.Wallet(w200).versions({ follow: true }) // also across id changes (reclass, rekey)
288
+ await db.Wallet(w).diff() // what changed: previous version → latest
289
+ await db.Wallet(w).blame() // who, when and why (verb option reason) changed fields
275
290
  ```
291
+ `diff()` is an array of `DiffResult`: `{ id, class, from, to, changes }`; `from` and `to` are `{ rev, at, id? }`, `to`
292
+ also has `deleted?` (then `changes` is empty), `from: null` means the object did not exist then. A change, `DiffChange`,
293
+ is `{ path, kind: 'added' | 'removed' | 'changed', before?, after? }`. `blame()` is an array of `BlameResult`:
294
+ `{ id, class, rev, complete, fields }`; `fields` are the `BlameField` leaves in path order:
295
+ `{ path, rev, at, author, op, exact, agent?, reason?, id? }`. `path` is an array of segments: `['data', 'price']`;
296
+ empty `agent`, `reason`, `before`, `after` are omitted. The types come from `letopis`.
276
297
  A class with `history: false` does not provide `versions`, `asOf`, `restore` or subscription events.
277
298
 
278
299
  ### 11. Import from 0.21
@@ -300,6 +321,87 @@ npx letopis import --from "$ADMIN" --from-schema v1.fixture --dsn "$ADMIN" --sch
300
321
  `credentials`, `resources`, `rules`, `objects`, `uuidFields`, `droppedFields`, `ownerLost`, `verify`, `ms`; the exit
301
322
  code is 1 if `verify` failed. Repeating the import adds what is missing, without duplicates.
302
323
 
324
+ ### 12. Projectors
325
+
326
+ A projector builds rows of derived classes from the journal: summaries, cards, anything that may be computed with a
327
+ lag. A derived class is marked with `derivedBy: 'projector-name'`, and only this projector writes its rows. The
328
+ derived class itself (own or inherited) is created, edited and deleted only by an administrator: `syncModels` on an
329
+ admin connection holding a tenant session (`connect({ dsn: ADMIN, schema, token, allowBypassRls: true })`) or an
330
+ account with the System permission `letopis.projector`; for an application session it is `acl_denied`. Everyone whom
331
+ the rules give `READ` on a derived class sees its totals, even without permissions on the source rows.
332
+ ```ts
333
+ const p = adm.projector('daily', { from: ['Payment'], into: ['DayTotal'], version: 1, withBefore: true,
334
+ handle(changes, out) { /* out.inc, out.upsert, out.delete, await out.get: into classes, { tenant: c.tenant } */ } })
335
+ await p.run({ once: true }) // catch up with the journal; run() follows it until stop(); rebuild(), status(), close()
336
+ p.run().catch(report) // run() without once needs a catch: after a projector error it stops
337
+ ```
338
+ Only an admin connection (otherwise `admin_required`). A batch is one transaction, every change is applied exactly
339
+ once, there is one writer (`rebuild()` while another process writes a batch gives `projector_busy`). The first
340
+ start rebuilds the projection from the current state, in batches. `purge`, `trim` and `reset` come as changes with `rev` 1, so check `op` first.
341
+ Another `version` gives `rebuild_required`; a cursor older than the `all` tier gives `cursor_expired`
342
+ (`onExpired: 'rebuild'` rebuilds by itself, in batches); the tier is per tenant: if it has expired for some participants, only
343
+ they drop out. A tenant with a source with `history: false` drops out (`invalid_class`). `c.before` (with
344
+ `withBefore`) is the previous version of the same id or `null`: `null` for a creation, for the first version of a new
345
+ id after `reclass` and `rekey` (the tombstone of the former id subtracts the former data), during a rebuild and when
346
+ the previous version is a deletion (after `restore`) or lies outside the `from` family; `purge`, `trim` and `reset`
347
+ have no such field, so subtract `before` only after checking it for `null`. A class change that keeps the id (a class
348
+ without a key) and takes the row out of the `from` family comes as a deletion (`op` `delete`, `row` is the last
349
+ version in the family with `$deleted`). `handle` gets the changes of one tenant. An error of its data (the
350
+ database rejected its results, or `out` rejected its row or delta) rolls back only this tenant: it drops out
351
+ (`status().failed`: `{ id: { cursor, code, message } }`) until `rebuild({ tenant: id })` (`{ tenant, chunked: true }` — in batches while the tail goes on for all;
352
+ `status().rebuildingTenants`), and the others go on; with `onExpired: 'rebuild'` a tenant dropped by the tier rebuilds
353
+ itself in batches, in any process;
354
+ `tenants: [id, …]` is an explicit list of participants.
355
+ `status().state`: `'new'` means there is no projection yet, `'running'` means the projection is
356
+ built and kept up (not "a process is running now"), `'rebuilding'` means a batch rebuild is in progress or was
357
+ interrupted.
358
+ A daily total is a fold: subtract the previous version (`c.before`), add the new one (`c.row` unless it is `$deleted`)
359
+ with `out.inc('DayTotal', { day }, { total: ±amount }, { tenant: c.tenant })`; `DayTotal` has the key `['day']`,
360
+ `derivedBy: 'daily'` and `history: false`. Take the day from the data of the row, not from `at`: every edit changes the
361
+ version time. `out` itself folds the increments of one row by key into one edit per batch (as long as there is no
362
+ `upsert`, `delete` or `get` between them) and checks the class rule on their total. The full recipe is in
363
+ [the README, "Recipe: daily revenue"](README.en.md#recipe-daily-revenue).
364
+
365
+ ### 13. Aggregates in the database
366
+
367
+ The database itself keeps a sum or a count of source rows — for totals that must match even for raw SQL:
368
+ ```ts
369
+ const Wallet = hub('Wallet', { number: z.string(), balance: z.number().int().min(0) }, {
370
+ key: ['number'], aggregate: { balance: { sum: 'amount', of: 'Payment', via: 'Wallet' } } }) // or count: true; where is a JSON Schema
371
+ await db.Wallet(w).Payment().create({ opId: 'op-9', amount: 500 }).rows() // the balance grew by itself, no inc
372
+ ```
373
+ A trigger recalculates the field after every statement (the library, raw SQL, a cascade, `restore`, `reclass`,
374
+ `rekey`, the loader); going below `minimum` is `invalid_data`, every change is a version of the target. Live rows of
375
+ the same tenant are counted (a System row is not). Repair: `adm.rebuildAggregates('Wallet')` on an admin connection;
376
+ discrepancies: `verify({ data: true })`. Only someone who reads all rows of the source and its descendants without
377
+ conditions can describe or change an aggregate, otherwise `acl_denied`. A hot counter (more than ~150 writes per
378
+ second per object) is a projector that folds a batch, not an aggregate.
379
+
380
+ ### 14. Agents and MCP
381
+
382
+ MCP tools for an agent, under a user session (needs `npm i @modelcontextprotocol/server`):
383
+ ```ts
384
+ import { createMcpServer } from 'letopis/mcp'
385
+ const server = await createMcpServer(await svc.as(userId)) // letopis_model, letopis_query, letopis_history; { write: true } adds letopis_write
386
+ // stdio: letopis mcp --dsn … --schema v2.x --token <user token> [--write]; HTTP: createMcpHandler(() => createMcpServer(…)) from the SDK
387
+ ```
388
+ Only a user session or `svc.as(user)`: a connection that bypasses RLS gets `bypass_rls`, the service root without
389
+ `as()` and a session in the System tenant (without `allowSystemAdmin`) get `user_session_required`; `letopis_write`
390
+ does not write the permission classes without `allowAclWrite` (`acl_denied`); the command line does not accept
391
+ `--api-key` but also takes `--dsn`,
392
+ `--schema` and `--token` from `LETOPIS_DSN`, `LETOPIS_SCHEMA`, `LETOPIS_TOKEN`. Reading is a JSON chain
393
+ (`class`, `filter` with `$gt`, `$in`, `$or` and so on, `steps`, `sort: { field, dir: 'asc' | 'desc' }`, `cursor`),
394
+ with no SQL; an answer holds up to `maxRows` rows (100) and `maxBytes` bytes (50,000) from the `createMcpServer`
395
+ options, then comes a `cursor`. `structuredContent` is `{ notice, data }` (`notice` is a constant note that this is
396
+ data, not instructions); `data` by terminal: `rows` — `{ rows, count, more, cursor? }`, `ids` —
397
+ `{ ids, count, more, cursor? }` (`count` is the rows on the page), `first` — `{ row }`, `count` — `{ count }`,
398
+ `countBy` — `{ counts, truncated? }`, `sum`, `avg`, `min`, `max` —
399
+ `{ value }`. A refusal is `isError: true` with a `structuredContent` of the form `{ notice, error }` (`error` is
400
+ `{ code, message, similar? }`). Writing only
401
+ with `write: true` (without it `letopis_write` is not in the list, and a call is an SDK protocol error): `update` with
402
+ `rev`; `delete` without `confirm` is a preview, with `confirm` a human confirms it (elicitation), and without that it
403
+ is `confirmation_required`. Data from the database in an answer sits inside `<untrusted-data-…>`.
404
+
303
405
  ### Operations
304
406
 
305
407
  `db.verify({ data: true })` and `letopis verify` — hash chains, the anchor; `db.maintain()` and
@@ -319,7 +421,8 @@ await adm.close(); await db.close(); await svc.close()
319
421
  without a filter and without `data.id`, all rows of the step are edited. `target_not_found` comes with `{ rev }` or
320
422
  if the row was deleted between the lookup and the edit.
321
423
  3. **Verbs return a chain.** Without a terminal (`.rows()`, `.first()` …) nothing runs. `delete()` without
322
- `{ confirm: true }` is only a preview; `purge` works only on deleted entities (`not_deleted`).
424
+ `{ confirm: true }` is only a preview; `purge` works only on deleted entities (`not_deleted`) and once a projector
425
+ of the class has read the deletion (`projector_pending`).
323
426
  4. **Money and counters**: `update({ balance: inc(n) })` — the database adds inside the statement, without a read and
324
427
  without lost updates; the `minimum` check in the schema catches a drop below zero. For a total computed from what
325
428
  you read, use `{ rev }` (re-read and retry on `conflict`) or `forUpdate()` in `db.begin()`. Idempotency comes from
@@ -334,9 +437,9 @@ await adm.close(); await db.close(); await svc.close()
334
437
  by themselves.
335
438
  7. **`commit_unknown`** — the connection dropped during `COMMIT`: the transaction may have been committed. Do not
336
439
  retry blindly; repeat the same operation with the same key (`exists` means "already written").
337
- 8. **The loader needs an admin connection** (`allowBypassRls: true`, the owner role), otherwise `admin_required`;
338
- inside `db.begin()` it gives `invalid_query`. A regular application uses the application role, without bypassing
339
- RLS.
440
+ 8. **The loader, a projector and `rebuildAggregates` need an admin connection** (`allowBypassRls: true`, the owner
441
+ role), otherwise `admin_required`; inside `db.begin()` they give `invalid_query`. A regular application uses the
442
+ application role, without bypassing RLS.
340
443
  9. **Data is under RLS.** Without a session (`token`/`apiKey`) a write gives `no_session`; a row of another tenant or
341
444
  an invisible row cannot be told apart from a missing one: the step does not find it, and a reference to it gives
342
445
  `target_not_found`; without a permission you get an empty result or `acl_denied`. `` db.sql`…` `` and raw SQL also run under the session and RLS.
@@ -359,10 +462,14 @@ await adm.close(); await db.close(); await svc.close()
359
462
  `letopis: <code>: …`; data errors (`ValidationError`) have `err.issues: [{ path, keyword, message }]`.
360
463
  16. **A subscription with `'now'` starts when the database answers.** `db.watch()` returns immediately, and the
361
464
  subscription takes the "now" moment with its first query to the database. A write made right after `db.watch()`
362
- can be committed earlier and miss the stream. If you need a guarantee, subscribe from a saved cursor or from
363
- `'start'`.
465
+ can be committed earlier and miss the stream. If you need a guarantee — `await sub.ready()` before the write, a
466
+ saved cursor or `'start'`. Parallel `next()` calls go in turn and get different events.
364
467
  17. **The terminal returns the rows of the last step.** `…Payment().create(…).Wallet().update(…).rows()` returns the
365
468
  `Wallet` rows, not the created payment; if you need the payment, end the chain on it.
366
469
  18. **A batch queue belongs to its facade.** `db.batch('x')` on the root, on each `await db.as(…)` and on each
367
470
  `db.begin()` are different queues; a plan runs on behalf of whoever queued it. Collect plans through one facade
368
471
  object.
472
+ 19. **Only its projector writes the rows of a derived class.** The application, raw SQL and the loader get
473
+ `derived_class`; ends may not refer to such rows either. To recompute them, use the projector's `rebuild()`.
474
+ 20. **Only the database writes an aggregate field.** A value in `create`, `update`, `upsert`, `rekey`, `reclass` and raw
475
+ SQL is `aggregate_field`; do not pass it. A raw upsert of a target: `set data = excluded.data - array['balance']`.
@@ -1,4 +1,4 @@
1
- # letopis 1.0 — шпаргалка для агента
1
+ # letopis 1.2 — шпаргалка для агента
2
2
 
3
3
  Одна страница вместо [README.md](README.md) — для ИИ-агента, который пишет код поверх пакета. Лежит в пакете рядом с
4
4
  README; английская версия — [AGENT-CHEATSHEET.en.md](AGENT-CHEATSHEET.en.md). Переход с 0.21 — [MIGRATION.md](MIGRATION.md);
@@ -38,6 +38,13 @@ const r = await up({ dsn: ADMIN, schema: 'myapp', // схема
38
38
  `'booking'` — демо-домен, или путь к своему `.sql`), `history` (политика хранения), `allowReset`, `serviceKey`,
39
39
  `quiet`, `pglite: true` (база для разработки без PostgreSQL; затем `connect({ dsn: r.pglite.dsn, listen: false,
40
40
  max: 1, allowBypassRls: true, … })` — подключение суперпользователя, RLS на нём не действует).
41
+ - `signals` — что видно в сигналах `NOTIFY` любой роли, способной подключиться к базе. Канал — полное имя схемы
42
+ (`LISTEN "v2.myapp"`), нагрузка сигнала записи: `'class'` (по умолчанию) — `Класс`; `'tenant'` —
43
+ `Класс@токен арендатора` (кэш сбрасывается только в арендаторе записи); `'opaque'` — `токен класса@токен
44
+ арендатора`; `'none'` — сигналов нет (кэш — только `ttlOnly`, подписка опрашивает журнал). Служебные сигналы:
45
+ `session:<16 hex>` — погашенная сессия, `session:*` и `*` — сбросы (`reset`). Режим хранится в схеме: `up()` без
46
+ опции его не меняет; после смены — перезапуск процессов. Ключ сигналов (точный сброс кэша) получают сервисы — право
47
+ `letopis.signals`. Результат `up()`: ещё `signals` и `warnings` (PostgreSQL ниже 18.2 — обновить).
41
48
  - `tenant` — первый арендатор и пользователь без своего сида: арендатор получает правило «аутентифицированным можно
42
49
  всё» (`grantAll`, по умолчанию да, вес 10), пользователь — членство с ролями `user.roles` (по умолчанию `['owner']`),
43
50
  пароль для входа по логину `login` и сессию в арендаторе. Повтор с тем же именем и логином ничего не дублирует и роли
@@ -103,8 +110,9 @@ export const Payment = link('Payment', { opId: z.string(), amount: z.number().in
103
110
  export const Note = hub('Note', { text: z.string() }, {}) // без ключа: id выдаёт база
104
111
  ```
105
112
  - Полная форма: `hub(name, { alias, extends, abstract, ends, fields: z.object(…) | { поля }, key, onDelete, history,
106
- ownerDefault: 'tenant' | 'actor', cache: { ttl }, meta })`; `link` — так же. `extends` работает и в короткой
107
- форме, но типы данных потомка TypeScript выводит только в полной.
113
+ ownerDefault: 'tenant' | 'actor', cache: { ttl }, meta, derivedBy, aggregate })`; `link` — так же. `derivedBy` и
114
+ `aggregate` — разделы 12 и 13 ниже. `extends` работает и в короткой форме, но типы данных потомка TypeScript
115
+ выводит только в полной.
108
116
  - Концы: `one(A)` (обязательный), `one(A, B)` (союз), `one('Имя')` (цель по имени — ссылка класса на себя),
109
117
  `.optional()`, `.onDelete('restrict' | 'cascade' | 'unset')` (по умолчанию `restrict`; `unset` — только для
110
118
  необязательного), `many(A, { min, max, onDelete })`.
@@ -197,10 +205,14 @@ const rep = await adm.load({
197
205
  }, { existing: 'skip' }) // duplicates 'error'|'first'|'last', existing 'error'|'skip'|'update', commitEvery, withHistory, verify, tenant, as
198
206
  // rep: { rows, loaded, updated, skipped, duplicates, byClass, external, portions, ms }
199
207
  ```
208
+ Первая заливка: `adm.load(rows, { initial: true })`, `letopis load --initial`, `letopis import --initial` — только в пустую
209
+ схему (иначе `not_empty`), одной транзакцией; на миллионе объектов — примерно в 2,2 раза быстрее обычной загрузки;
210
+ без `commitEvery`, `existing` и `{ as }`.
211
+
200
212
  Файлы NDJSON (объект на строку, все файлы — одна загрузка):
201
213
  ```sh
202
214
  npx letopis load shops.ndjson wallets.ndjson --dsn "$ADMIN" --schema v2.myapp --token "$TOKEN" [--class Wallet] \
203
- [--duplicates first] [--existing skip] [--commit-every 100000] [--verify]
215
+ [--duplicates first] [--existing skip] [--commit-every 100000] [--verify] [--initial]
204
216
  # {"$class":"Shop","name":"Юг"}
205
217
  # {"$class":"Wallet","number":"W-10","Shop":{"$key":["Юг"]}}
206
218
  # {"$class":"Note","text":"x","$ext":"n1"} ← $ext — внешний ключ строки для ссылок { "$ext": "n1" }
@@ -264,7 +276,15 @@ const was = await db.Wallet(w).asOf('2026-10-01T00:00:00Z').first() // как
264
276
  await db.Shop(shop).Wallet().asOf(new Date('2026-10-01')).rows()
265
277
  const history = await db.Wallet(w).versions() // все версии; версия удаления — $deleted
266
278
  await db.Wallet(w200).versions({ follow: true }) // и через смену id (reclass, rekey)
279
+ await db.Wallet(w).diff() // что изменилось: предыдущая версия → последняя
280
+ await db.Wallet(w).blame() // кто, когда и почему (опция reason глагола) менял поля
267
281
  ```
282
+ `diff()` — массив `DiffResult`: `{ id, class, from, to, changes }`; `from` и `to` — `{ rev, at, id? }`, у `to` ещё
283
+ `deleted?` (тогда `changes` пуст), `from: null` — объекта тогда не было. Изменение `DiffChange` —
284
+ `{ path, kind: 'added' | 'removed' | 'changed', before?, after? }`. `blame()` — массив `BlameResult`:
285
+ `{ id, class, rev, complete, fields }`; `fields` — листья `BlameField` по порядку пути:
286
+ `{ path, rev, at, author, op, exact, agent?, reason?, id? }`. `path` — массив сегментов: `['data', 'price']`; пустые
287
+ `agent`, `reason`, `before`, `after` опускаются. Типы — из `letopis`.
268
288
  Класс с `history: false` не даёт `versions`, `asOf`, `restore` и событий подписки.
269
289
 
270
290
  ### 11. Импорт из 0.21
@@ -292,6 +312,81 @@ npx letopis import --from "$ADMIN" --from-schema v1.fixture --dsn "$ADMIN" --sch
292
312
  `objects`, `uuidFields`, `droppedFields`, `ownerLost`, `verify`, `ms`; код выхода 1, если `verify` не прошёл.
293
313
  Повтор импорта дописывает недостающее без дублей.
294
314
 
315
+ ### 12. Проекторы
316
+
317
+ Проектор строит строки производных классов из журнала: сводки, карточки, всё, что можно считать с отставанием.
318
+ Производный класс помечен `derivedBy: 'имя-проектора'`, и его строки пишет только этот проектор. Сам производный
319
+ класс (свой или унаследованный) заводит, меняет и удаляет только администратор: `syncModels` администраторским
320
+ подключением с сессией арендатора (`connect({ dsn: ADMIN, schema, token, allowBypassRls: true })`) или аккаунт с
321
+ правом System `letopis.projector`; у сессии приложения — `acl_denied`. Итоги производного класса видит каждый, кому
322
+ правила дают `READ` на него, даже без прав на строки источников.
323
+ ```ts
324
+ const p = adm.projector('daily', { from: ['Payment'], into: ['DayTotal'], version: 1, withBefore: true,
325
+ handle(changes, out) { /* out.inc, out.upsert, out.delete, await out.get — классы into, { tenant: c.tenant } */ } })
326
+ await p.run({ once: true }) // догнать журнал; run() — следовать за ним до stop(); rebuild(), status(), close()
327
+ p.run().catch(report) // run() без once — с catch: после ошибки проектора он остановлен
328
+ ```
329
+ Только администраторское подключение (иначе `admin_required`). Пачка — одна транзакция, каждое изменение
330
+ применяется ровно один раз, писатель один (`rebuild()`, пока другой процесс пишет пачку, — `projector_busy`).
331
+ Первый запуск перестраивает проекцию с текущего состояния, пачками. `purge`, `trim` и `reset` приходят изменениями с `rev` 1 — сначала
332
+ разберите `op`. Другая `version` — `rebuild_required`; курсор старше яруса `all` — `cursor_expired`
333
+ (`onExpired: 'rebuild'` перестраивает сам, пачками); ярус — по арендатору: истёк у части участников — выбывают только они.
334
+ Арендатор с источником `history: false` выбывает (`invalid_class`). `c.before` (с `withBefore`) —
335
+ прошлая версия того же id или `null`: `null` при создании, у первой версии нового id после `reclass` и `rekey`
336
+ (прежние данные вычитает надгробие прежнего id), при перестройке и когда прошлая версия — удаление (после `restore`)
337
+ или вне семейства `from`; у `purge`, `trim` и `reset` поля нет — вычитайте `before`, только проверив его на `null`.
338
+ Смена класса без смены id (класс без ключа), уводящая строку из семейства `from`, приходит удалением (`op` `delete`,
339
+ `row` — последняя версия в семействе с `$deleted`). `handle` получает изменения
340
+ одного арендатора. Ошибку его данных (база отклонила его результаты или `out` — его строку или приращение)
341
+ откатывает только он: арендатор выбывает (`status().failed`: `{ id: { cursor, code, message } }`) до
342
+ `rebuild({ tenant: id })` (`{ tenant, chunked: true }` — пачками, пока хвост идёт для всех; `status().rebuildingTenants`),
343
+ остальные идут дальше; с `onExpired: 'rebuild'` выбывший по ярусу перестраивается пачками сам, в любом процессе; `tenants: [id, …]` — явный список участников. `status().state`: `'new'` —
344
+ проекции ещё нет, `'running'` — проекция построена и ведётся (а не «процесс сейчас работает»), `'rebuilding'` — идёт
345
+ или прервана перестройка пачками.
346
+ Дневной итог — свёртка: прошлую версию (`c.before`) вычесть, новую (`c.row`, если не `$deleted`) прибавить через
347
+ `out.inc('DayTotal', { day }, { total: ±amount }, { tenant: c.tenant })`; у `DayTotal` ключ `['day']`,
348
+ `derivedBy: 'daily'` и `history: false`. День берите из данных строки, а не из `at`: время версии меняет каждая правка.
349
+ Приращения одной строки по ключу `out` складывает сам в одну правку за пачку (пока между ними нет `upsert`, `delete`
350
+ или `get`), а правило класса проверяет над их итогом. Полный рецепт —
351
+ [README, «Рецепт: дневная выручка»](README.md#рецепт-дневная-выручка).
352
+
353
+ ### 13. Агрегаты в базе
354
+
355
+ Сумму или счётчик строк источника держит сама база — для итогов, которые обязаны совпадать и для сырого SQL:
356
+ ```ts
357
+ const Wallet = hub('Wallet', { number: z.string(), balance: z.number().int().min(0) }, {
358
+ key: ['number'], aggregate: { balance: { sum: 'amount', of: 'Payment', via: 'Wallet' } } }) // или count: true; where — JSON Schema
359
+ await db.Wallet(w).Payment().create({ opId: 'op-9', amount: 500 }).rows() // баланс вырос сам, без inc
360
+ ```
361
+ Поле пересчитывает триггер после каждого оператора (библиотека, сырой SQL, каскад, `restore`, `reclass`, `rekey`,
362
+ загрузчик); уход ниже `minimum` — `invalid_data`, каждое изменение — версия цели. Считаются живые строки того же
363
+ арендатора (строка System — нет). Ремонт — `adm.rebuildAggregates('Wallet')` на администраторском подключении,
364
+ расхождения — `verify({ data: true })`. Описать или изменить агрегат может только тот, кто без условий читает все строки
365
+ источника и его потомков, иначе `acl_denied`. Горячий счётчик (больше ~150 записей в секунду на объект) —
366
+ проектор со свёрткой пачкой, а не агрегат.
367
+
368
+ ### 14. Агенты и MCP
369
+
370
+ Агенту — инструменты MCP под сессией пользователя (нужен `npm i @modelcontextprotocol/server`):
371
+ ```ts
372
+ import { createMcpServer } from 'letopis/mcp'
373
+ const server = await createMcpServer(await svc.as(userId)) // letopis_model, letopis_query, letopis_history; { write: true } — letopis_write
374
+ // stdio: letopis mcp --dsn … --schema v2.x --token <токен пользователя> [--write]; HTTP — createMcpHandler(() => createMcpServer(…)) из SDK
375
+ ```
376
+ Только сессия пользователя или `svc.as(пользователь)`: подключение в обход RLS — `bypass_rls`, корень сервиса без `as()`
377
+ и сессия в арендаторе System (без `allowSystemAdmin`) — `user_session_required`; классы прав `letopis_write` не пишет без
378
+ `allowAclWrite` (`acl_denied`); `--api-key` командная строка не принимает, а `--dsn`, `--schema` и `--token` берёт и из
379
+ `LETOPIS_DSN`, `LETOPIS_SCHEMA`, `LETOPIS_TOKEN`. Чтение — JSON-цепочкой (`class`, `filter` с `$gt`, `$in`, `$or` и
380
+ т. п., `steps`, `sort: { field, dir: 'asc' | 'desc' }`, `cursor`), без SQL; ответ — до `maxRows` строк (100) и
381
+ `maxBytes` байт (50 000) из опций `createMcpServer`, дальше `cursor`. `structuredContent` — `{ notice, data }`
382
+ (`notice` — постоянное пояснение, что это данные, а не инструкции); `data` по терминалам: `rows` —
383
+ `{ rows, count, more, cursor? }`, `ids` — `{ ids, count, more, cursor? }` (`count` — строк на странице), `first` —
384
+ `{ row }`, `count` — `{ count }`, `countBy` — `{ counts, truncated? }`, `sum`, `avg`, `min`, `max` — `{ value }`.
385
+ Отказ — `isError: true` и `structuredContent` вида `{ notice, error }` (`error` — `{ code, message, similar? }`). Запись — только `write: true`
386
+ (без него `letopis_write` нет в списке, а вызов — ошибка протокола SDK): `update` с `rev`; `delete` без `confirm` —
387
+ превью, с `confirm` — подтверждение человека (elicitation), без него — `confirmation_required`. Данные из базы в
388
+ ответе — внутри `<untrusted-data-…>`.
389
+
295
390
  ### Эксплуатация
296
391
 
297
392
  `db.verify({ data: true })` и `letopis verify` — цепочки хэшей, якорь; `db.maintain()` и `connect({ maintain: '1h' })` —
@@ -311,7 +406,8 @@ await adm.close(); await db.close(); await svc.close()
311
406
  без `data.id` правятся все строки шага. `target_not_found` — при `{ rev }` или если строку удалили между поиском
312
407
  и правкой.
313
408
  3. **Глаголы возвращают цепочку.** Без терминала (`.rows()`, `.first()` …) ничего не исполнится. `delete()` без
314
- `{ confirm: true }` — только превью; `purge` — только для удалённых (`not_deleted`).
409
+ `{ confirm: true }` — только превью; `purge` — только для удалённых (`not_deleted`) и после того, как проектор
410
+ класса прочёл удаление (`projector_pending`).
315
411
  4. **Деньги и счётчики** — `update({ balance: inc(n) })`: база прибавляет внутри оператора, без чтения и потерянных
316
412
  обновлений; проверка `minimum` в схеме ловит уход в минус. Итог, вычисленный из прочитанного, — `{ rev }`
317
413
  (перечитать и повторить при `conflict`) или `forUpdate()` в `db.begin()`. Идемпотентность — ключом операции
@@ -325,8 +421,9 @@ await adm.close(); await db.close(); await svc.close()
325
421
  В `db.begin()` повторов нет: на `40P01` повторите весь блок. Операции из пула сами повторяют `40001`/`40P01`.
326
422
  7. **`commit_unknown`** — связь оборвалась во время `COMMIT`: транзакция могла зафиксироваться. Не повторяйте
327
423
  вслепую — повторите ту же операцию с тем же ключом (`exists` значит «уже записано»).
328
- 8. **Загрузчику нужно администраторское подключение** (`allowBypassRls: true`, роль-владелец), иначе
329
- `admin_required`; внутри `db.begin()` — `invalid_query`. Обычное приложение — прикладной ролью, без обхода RLS.
424
+ 8. **Загрузчику, проектору и `rebuildAggregates` нужно администраторское подключение** (`allowBypassRls: true`,
425
+ роль-владелец), иначе `admin_required`; внутри `db.begin()` — `invalid_query`. Обычное приложение — прикладной
426
+ ролью, без обхода RLS.
330
427
  9. **Данные под RLS.** Без сессии (`token`/`apiKey`) запись — `no_session`; чужая или невидимая строка неотличима от
331
428
  несуществующей: шаг её не находит, а ссылка на неё — `target_not_found`; без права — пусто или `acl_denied`. `` db.sql`…` `` и сырой SQL — тоже под
332
429
  сессией и RLS.
@@ -347,8 +444,13 @@ await adm.close(); await db.close(); await svc.close()
347
444
  у ошибок данных (`ValidationError`) — `err.issues: [{ path, keyword, message }]`.
348
445
  16. **Подписка с `'now'` — с момента ответа базы.** `db.watch()` возвращается сразу, а момент «now» подписка берёт
349
446
  первым запросом к базе. Запись, сделанная сразу после `db.watch()`, может зафиксироваться раньше и в поток не
350
- попасть. Нужна гарантия — подписывайтесь с сохранённого курсора или с `'start'`.
447
+ попасть. Нужна гарантия — `await sub.ready()` перед записью, сохранённый курсор или `'start'`. Параллельные
448
+ `next()` идут по очереди и получают разные события.
351
449
  17. **Терминал отдаёт строки последнего шага.** `…Payment().create(…).Wallet().update(…).rows()` вернёт строки
352
450
  `Wallet`, а не созданной операции; нужна операция — заканчивайте цепочку на ней.
353
451
  18. **Очередь батча — своя у фасада.** `db.batch('x')` у корня, у каждого `await db.as(…)` и у каждой `db.begin()` —
354
452
  разные очереди; план исполняется от имени того, кто его поставил. Копите планы через один объект фасада.
453
+ 19. **Строки производного класса пишет только его проектор.** Приложение, сырой SQL и загрузчик получат
454
+ `derived_class`; ссылаться на такие строки концами тоже нельзя. Пересчитать их — `rebuild()` проектора.
455
+ 20. **Агрегатное поле пишет только база.** Значение в `create`, `update`, `upsert`, `rekey`, `reclass` и сыром SQL —
456
+ `aggregate_field`; не передавайте его. Сырой upsert цели — `set data = excluded.data - array['balance']`.