@simetrajs/designer 0.1.0 → 0.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.
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: simetra-metadata
3
- description: Use when you need to create, change, validate or explain Simetra metadata (a `metadata/` directory) with the `simetra` tools — compile it, read a compiler diagnostic (`file:line:col`, `--format json`, `--locale`), explain the compiled picture of an object, fix missing ids and `physicalName`, create, add, rename or delete elements, or promote an adopted table, through checked operations, compare a database with the metadata (`diff`), plan and apply the metadata to a database (`plan`, `apply`), or expose the tools to an agent with `simetra mcp` (`--read-only` to refuse writes). Also use when a pre-commit or CI metadata check fails. For bringing an existing database under metadata (`introspect`), use `simetra-adoption`.
3
+ description: Use when you need to create, change, validate or explain Simetra metadata (a `metadata/` directory) with the `simetra` tools — compile it, read a compiler diagnostic (`file:line:col`, `--format json`, `--locale`), explain the compiled picture of an object, fix missing ids and `physicalName`, create, add, rename or delete elements, or promote an adopted table, through checked operations, compare a database with the metadata (`diff`), plan and apply the metadata to a database (`plan`, `apply`), or expose the tools to an agent with `simetra mcp` (`--read-only` to refuse writes), including application data of the development database as a user (`data-read`, `data-command`). Also use when a pre-commit or CI metadata check fails. For bringing an existing database under metadata (`introspect`), use `simetra-adoption`.
4
4
  ---
5
5
 
6
6
  # Simetra metadata — compile, explain, fix, create, add, rename, delete, promote, introspect, diff, plan, apply
@@ -30,11 +30,14 @@ diagnostics themselves.
30
30
  | Does the database match the metadata? | `diff` | `simetra diff [dir] --tables a,b` | `diff` with `{tables?}` |
31
31
  | Plan bringing the database to the metadata (writes `plans/…plan.json`) | `plan` | `simetra plan [dir]` | `plan` with `{}` |
32
32
  | Apply a reviewed plan to the database | `apply` | `simetra apply <plan> [dir] --plan-hash <hex> --yes` | `apply` with `{plan, planHash, allow?, confirm}` |
33
+ | Create the platform's cluster roles once per server (before the first `plan`); reads the project file | `roles` | `simetra roles` | `roles` with `{}` |
34
+ | Restore rights access outside commands: the first global assignment, a global or tenant `fullAccess` owner nobody holds (as the migrator) | `rights-maintenance` | `simetra rights-maintenance '<json>' [dir] --yes` | not available: command line only |
35
+ | Read application data of the development database as a user, with that user's rights | `data-read` | `simetra data-read '<json>' [dir] --data-url-env <VAR> --as <uuid>` | `data-read` with `{query}` |
36
+ | Change application data of the development database as a user (seed test data, reproduce a bug) | `data-command` | `simetra data-command '<json>' [dir] --data-url-env <VAR> --as <uuid>` | `data-command` with `{operations}` |
33
37
 
34
- `compile`, `explain`, `diff`, `apply` only read files. `fix`, `create`, `add`,
35
- `rename`, `delete`, `promote`, `introspect` change files; `plan` writes only its
36
- plan file. `introspect`, `diff` and `plan` read the database; only `apply`
37
- writes to it.
38
+ `fix`, `create`, `add`, `rename`, `delete`, `promote`, `introspect` change files;
39
+ `plan` writes only its plan file; others only read files. `introspect`, `diff`,
40
+ `plan` read the database; `apply`, `roles`, `rights-maintenance` write to it.
38
41
 
39
42
  ## Examples
40
43
 
@@ -140,9 +143,10 @@ says why a mapping is wrong, so read its diagnostics):
140
143
  (`plan.service-role-access-lost`); keep the table a `CustomTable` until
141
144
  platform commands exist. An explicit adopted grant to the service role takes
142
145
  the class `removed`.
143
- - Functions in the closed shell move to the kind's module, and functions that
144
- read the promoted tables are rewritten in place. Other verbatim SQL that
145
- reads them is refused: rewrite or drop it first.
146
+ - Closed-shell query functions move to the kind's module; functions reading
147
+ the promoted tables are rewritten in place. Other SQL that reads or writes
148
+ them is refused (`promote.debt-writes-table`): rewrite or drop it first.
149
+ Incoming cascades become `restrict`: `references/sql-trust-boundary.md`.
146
150
  - `sharedFunctions` lists identities of those module functions that move to
147
151
  `sql/<schema>/<function>.sql` instead; `consumerLabels` the values by which
148
152
  the consumer's data encodes the object (the record's `labelTranslation`
@@ -170,11 +174,10 @@ offending field (for example `target.kind`); nothing is written.
170
174
  - Exit code: `0` ok (warnings allowed), `1` the result has error diagnostics
171
175
  (this includes refused operations such as `operation.delete-referenced`)
172
176
  or, for `diff`, differences or errors — `0` from `diff` means the database
173
- matches the metadata,
174
- `2` the call itself was refused: input that does not match the tool's schema
175
- (bad JSON, unknown field), a missing `--yes`, a usage or I/O error (bad flag,
176
- missing directory) or an unknown `explain` target. In MCP the same cases are
177
- `isError`, with diagnostics for the exit-1 ones.
177
+ matches the metadata, `2` the call itself was refused: input that does not match
178
+ the tool's schema (bad JSON, unknown field), a missing `--yes`, a usage or I/O
179
+ error (bad flag, missing directory) or an unknown `explain` target. In MCP the
180
+ same cases are `isError`, with diagnostics for the exit-1 ones.
178
181
  - Text diagnostics look like `path/File.meta.json:LINE:COL severity code message`.
179
182
  A diagnostic without a position (the input as a whole, a missing file) has no
180
183
  `LINE:COL`. The code (`file.kind-mismatch`, …) is the stable handle; do not
@@ -184,7 +187,8 @@ offending field (for example `target.kind`); nothing is written.
184
187
  - `--locale en|uk` picks the message language (default `en`); code and
185
188
  location are identical in both.
186
189
  - Start from the first error: one root cause often produces several follow-ups.
187
- - Artifacts (`snapshot.json`, `desired-state.sql`, `entities.d.ts`) are written
190
+ - Artifacts (`snapshot.json`, `desired-state.sql`, `entities.d.ts`,
191
+ `runtime-catalog.json`) are written
188
192
  only with `compile --out <dir>`, and `--out` takes one metadata directory per run.
189
193
 
190
194
  ## Mutations are checked, never raw
@@ -194,8 +198,7 @@ offending field (for example `target.kind`); nothing is written.
194
198
  - `create`, `add`, `rename`, `delete` also need the current metadata to compile
195
199
  cleanly (`operation.input-invalid` otherwise): run `fix` or repair the file by
196
200
  hand first.
197
- - `--dry-run` (MCP: `dryRun: true`) reports the changes without writing. Use it
198
- before the real run.
201
+ - `--dry-run` (MCP: `dryRun: true`) reports the changes without writing. Use it first.
199
202
  - `delete` is destructive: it needs `--yes` (MCP: `confirm: true`) and is refused
200
203
  while anything references the target: the result then carries
201
204
  `operation.delete-referenced` listing every reference (exit 1, nothing
@@ -223,7 +226,7 @@ Attribute properties (an element of `attributes`):
223
226
  `{ "empty": "object" | "array" }` on a scalar `Json`.
224
227
  - `unique`: `true`, or `"ignoreCase"` on a scalar String or Text. `uniqueWithin`: `"owner"` or `"parent"`,
225
228
  only on a catalog's own attributes and only when the catalog has owners or a
226
- hierarchy.
229
+ hierarchy. Write no cycle check: the database rejects a `parent` cycle from any writer; a `parent` change needs `READ COMMITTED`.
227
230
  - Numbers (scalar only): `nonNegative`, `positive`, `minValue`, `maxValue`.
228
231
  - Strings and text (scalar only): `minLength`, `pattern`. The pattern must
229
232
  mean the same in JavaScript and Postgres: no named groups, `\p{}`, `\k<>`,
@@ -264,18 +267,20 @@ events only) and `handler` (`{ schema?, name }`, a no-argument function that
264
267
  `FOR EACH ROW` triggers, named after `physicalName`, on every source: `before*`
265
268
  is `BEFORE`, `on*` is `AFTER`, `*Write` is `INSERT OR UPDATE`, `*Delete` is
266
269
  `DELETE`. With `whenChanged`, `<physicalName>` fires on an `UPDATE` that changes
267
- a listed column (`IS DISTINCT FROM`), `<physicalName>_insert` on every `INSERT`.
268
- No event is insert-only; `TG_OP` tells insert from update. A handler on a
269
- provider table (`auth.users`) that writes app tables must be `SECURITY DEFINER`
270
- with `SET search_path = ''`: the provider role has no rights on your schemas.
270
+ a listed column (`IS DISTINCT FROM`), `<physicalName>_insert` on every `INSERT`;
271
+ on a table with carrier copies (below) a subscription without `whenChanged`
272
+ gets the same pair, its `UPDATE` trigger firing on a content change only.
273
+ No event is insert-only; `TG_OP` tells insert from update. The same events as
274
+ TypeScript hooks of a command (object modules): `references/object-modules.md`.
271
275
 
272
276
  Catalogs and Documents get the row mechanics trigger `<table>_simetra_touch`
273
277
  (the single platform function `simetra.touch()`): an insert sets `version` to 1,
274
- an update adds 1, both set `updatedAt` to `now()` whatever the client writes.
275
- Never maintain them (`moddatetime`) or list them in `whenChanged`. A
276
- `beforeWrite` handler never assigns `NEW.updated_at` or `NEW.version` and never
277
- replaces the row (`NEW := …`, `SELECT … INTO NEW`): it returns `NEW`, or `NULL`
278
- to skip the write, never `OLD` or a copy of the row.
278
+ an update adds 1, both set `updatedAt` to `now()` whatever the client writes;
279
+ on a table with carrier copies the update is `<table>_simetra_touch_update`,
280
+ firing on a content change only. Never maintain them (`moddatetime`) or list
281
+ them in `whenChanged`. A `beforeWrite` handler never assigns `NEW.updated_at`
282
+ or `NEW.version` and never replaces the row (`NEW := …`, `SELECT … INTO NEW`):
283
+ it returns `NEW`, or `NULL` to skip the write, never `OLD` or a copy of the row.
279
284
 
280
285
  ## Users, membership and rights
281
286
 
@@ -289,8 +294,10 @@ declarations too: `customActions`, `ownership`, `rightsContainer`,
289
294
  them is a reference, so `rename` rewrites it and `delete` refuses while it is
290
295
  used; the compiler checks them (rules `users.*`, `rights.*`) and `explain`
291
296
  shows the derived rights contract, a role's contract and the rows of the
292
- rights projection. Forms of each field:
293
- `references/people-and-rights.md`.
297
+ rights projection. Forms of each field, who reads what, and what carrier copies
298
+ forbid a `beforeWrite` handler: `references/people-and-rights.md` (also the
299
+ system rights catalogs and tenant creation: a scope root whose creator gets a
300
+ `rootCreator` role, with `creatorMember`).
294
301
 
295
302
  ```json simetra:add
296
303
  {
@@ -300,41 +307,35 @@ rights projection. Forms of each field:
300
307
  }
301
308
  ```
302
309
 
303
- The platform layer (identities, the current user, provisioning) lives in the
304
- PG schema `simetra`, generated when a users catalog exists. The schema is
305
- reserved: no object, `defaultSchema`, set function, external reference,
306
- event handler or SQL unit may use it, and there is no `metadata/sql/simetra/`
307
- folder (`schema.reserved`). In SQL, the
308
- current user is `(select simetra.current_user_id())`, written exactly so in a
309
- policy (`sql.bare-current-user` rejects a bare call there); it is `NULL` for
310
- an anonymous session or an anonymous sign-in of the provider, a service
311
- session and an invalid user.
310
+ The PG schema `simetra` (identities, the current user, provisioning) belongs
311
+ to the platform and is reserved (`schema.reserved`); in SQL the current user is
312
+ `(select simetra.current_user_id())`. Both rules in full: the same reference.
312
313
 
313
314
  ## The SQL module: closed forms only
314
315
 
315
316
  The `.sql` module of an object of a 1C kind accepts only:
316
317
 
317
- - Functions in the closed shell: `LANGUAGE sql` or `plpgsql`, an explicit
318
- volatility, and `SET search_path = ''` for every `SECURITY DEFINER`. No
319
- overloads.
318
+ - Functions in the closed shell, with `SET search_path = ''`, an explicit
319
+ volatility and no overloads: a subscription handler in `plpgsql` that only
320
+ checks, any other function a query function (`LANGUAGE sql`, one `SELECT`).
320
321
  - Row rules: `ALTER TABLE <own table> ADD CONSTRAINT <name> CHECK (<rule>)`
321
322
  within a small grammar. The constraint needs a name and must target the
322
323
  object's own table or one of its own tabular-section tables.
323
324
  - Movement query blocks in a document module.
324
325
 
326
+ A verbatim policy or trigger on a table of a kind, or SQL that writes one, is
327
+ an error, never debt (`sql.kind-table-verbatim`): use a right, an
328
+ `EventSubscription` or a command. Function bodies, session settings
329
+ (`sql.command-context-write`), code inside a command (`sql.head-not-allowed`),
330
+ domains, foreign keys, scope roots, extensions and promotion gates:
331
+ `references/sql-trust-boundary.md`.
332
+
325
333
  Any other statement is rejected (`sql.statement-not-allowed`, `sql.closed-shell`,
326
334
  `sql.row-rule-grammar`, …) with a hint that names the property that replaces it.
327
335
  Follow the hint instead of rewording the statement. `CustomTable` and `PgEnum`
328
336
  modules and the shared `metadata/sql/` folder are not closed: their content is
329
- debt (below).
330
-
331
- Privileges on what the platform owns are generated: `EXECUTE` on a declared
332
- scope set function and on a subscription handler, and every grant on a kind's
333
- tables. In debt SQL (a `CustomTable` or `PgEnum` module, the shared
334
- `metadata/sql/`) a verbatim `GRANT`/`REVOKE` on them fails with
335
- `sql.generated-grant` (a kind's own module rejects any). `simetra fix` removes
336
- the statement (and its file, if left empty) only when every target belongs to
337
- the platform; one that also names other objects you split by hand. A grant on another overload of the same name is not affected.
337
+ debt (below). Privileges on what the platform owns are generated
338
+ (`sql.generated-grant`).
338
339
 
339
340
  A row rule shape:
340
341
 
@@ -356,6 +357,9 @@ tolerates. It only shrinks:
356
357
  function; moving it to another file does not make it acceptable.
357
358
  - A project with an existing `sql/` folder and no list is seeded by running
358
359
  `introspect` again (see `simetra-adoption`).
360
+ - Debt SQL must not write `simetra.*` (`sql.command-context-write`), and its
361
+ function bodies follow `sql.function-body-statement` (`references/sql-trust-boundary.md`);
362
+ `sql.definer-reads-rls` marks reads past RLS (`references/people-and-rights.md`).
359
363
 
360
364
  ## The database: introspect and diff
361
365
 
@@ -375,6 +379,14 @@ that holds `metadata/`); the database is only read. Its output shows drift
375
379
  first, then each step with its class and lock level, then the `planHash`.
376
380
  Show the plan to the owner before applying it.
377
381
 
382
+ The generated SQL grants to platform roles (`simetra_command`) that live in the
383
+ cluster, not in the database, so `plan` does not create them. Without one on
384
+ the target or the shadow server, `plan`, `diff` and `rehearse` refuse with
385
+ `plan.platform-role-missing`: run `simetra roles` once with a role that has
386
+ `CREATEROLE` (with a project file it also creates the login role: "Platform
387
+ roles" in `references/people-and-rights.md`). It only checks an existing role;
388
+ an unsafe shape (`plan.platform-role-shape`) is fixed by hand, not by `roles`.
389
+
378
390
  `apply` applies exactly that file in one transaction. It needs the `planHash`
379
391
  of the reviewed plan, confirmation (`--yes`, MCP `confirm: true`), and in
380
392
  `allow` the id of every step that drops data or relaxes a guarantee and
@@ -400,9 +412,9 @@ If the environment changes before the apply, it is refused
400
412
  (`apply.target-changed`); plan again. A plan file from an older tool version is
401
413
  refused too: rebuild it with `plan`.
402
414
 
403
- A promotion plan's `allow` also takes the `access:<table>`,
404
- `delete:<table>.<constraint>`, `remove:<table>` and `repair:<table>` items its
405
- output lists; a repair rewrites existing values with the fill expression shown.
415
+ A promotion plan's `allow` also takes its listed `access:<table>`, `remove:<t>`,
416
+ `repair:<t>`, `delete:<t>.<constraint>` and `update:<t>.<constraint>` items; a
417
+ repair rewrites existing values with the fill expression shown.
406
418
  A promotion is applied only with a valid rehearsal report: rehearse the plan on
407
419
  a copy of the target with data first. Point the rehearsal at the copy through
408
420
  the connection variable: `simetra apply --rehearse --database-url-env <VAR>`
@@ -456,23 +468,14 @@ pnpm exec simetra mcp
456
468
  pnpm exec simetra mcp --read-only
457
469
  pnpm exec simetra mcp --database-url-env STAGING_DATABASE_URL
458
470
  pnpm exec simetra mcp --allow-database-write
471
+ pnpm exec simetra mcp --data-url-env SIMETRA_DEV_DATA_URL --as <user-uuid>
459
472
  ```
460
473
 
461
- - By default the write tools change files in the served directory, under the
462
- same rules as the CLI: the metadata is in git, and nothing is written unless
463
- the result compiles. `--read-only` is a flag of `simetra mcp` only; the CLI
464
- has none (preview with `--dry-run`).
465
- - With `--read-only` every tool is still listed, but a write tool is refused
466
- with a hint to remove `--read-only` from the server's args. Ask the owner
467
- before restarting the server without it.
468
- - `apply` writes to the database only when the server runs with
469
- `--allow-database-write`; without it `apply` is refused with a hint to add
470
- the flag. Ask the owner; never add it yourself. `--read-only` refuses
471
- `plan` and `apply` too: they write their files under `plans/`.
472
- - `plan` and `apply` have no `dryRun`: review with `plan`, and check on the
473
- database with `rehearse`.
474
- - The server reads the disk on every call, so edits made by hand between calls
475
- are seen.
474
+ `simetra mcp` serves the same tools as the CLI and reads the disk on every
475
+ call. Its launch flags are the owner's decision: a refused call names the flag
476
+ that would allow it; ask the owner, never change the server's args yourself.
477
+ What each flag allows, and the data tools over the development database:
478
+ `references/agents-over-mcp.md`.
476
479
 
477
480
  ## Pre-commit and CI
478
481
 
@@ -488,4 +491,4 @@ pnpm exec simetra compile --all --staged
488
491
 
489
492
  A rejected commit means the staged content does not compile: run
490
493
  `simetra compile --all` for the working tree, fix, `git add`, commit again. Do
491
- not bypass the hook with `--no-verify`.
494
+ not bypass the hook with `--no-verify`. Setup and CI: `references/project-setup.md`.
@@ -0,0 +1,119 @@
1
+ # Agents over MCP
2
+
3
+ Contents: launch flags; data of the development database (connect, flags,
4
+ examples, the database marker, limits).
5
+
6
+ `simetra mcp` serves the tool catalog to an agent over stdio. `simetra init`
7
+ writes its entry in the MCP client config; flags added to that entry's `args`
8
+ are kept on the next `init`.
9
+
10
+ ## Launch flags
11
+
12
+ - By default the write tools change files in the served directory, under the
13
+ same rules as the CLI: the metadata is in git, and nothing is written unless
14
+ the result compiles. `--read-only` is a flag of `simetra mcp` only; the CLI
15
+ has none (preview with `--dry-run`).
16
+ - With `--read-only` every tool is still listed, but a write tool is refused
17
+ with a hint to remove `--read-only` from the server's args. Ask the owner
18
+ before restarting the server without it.
19
+ - `apply` and `roles` write to the database only when the server runs with
20
+ `--allow-database-write`; without it they are refused with a hint to add
21
+ the flag. Ask the owner; never add it yourself. `--read-only` refuses
22
+ `plan` and `apply` too: they write their files under `plans/`.
23
+ - `plan` and `apply` have no `dryRun`: review with `plan`, and check on the
24
+ database with `rehearse`.
25
+
26
+ ## Data of the development database
27
+
28
+ `data-read` and `data-command` read and change application data through the
29
+ same runtime commands as the application server, as the user named by `--as`
30
+ and with that user's rights. `--agent <uuid>` only records the agent in the
31
+ session (`act` claim); it grants nothing. Use them to seed test data, to
32
+ check what different users may see and do, and to reproduce a bug.
33
+
34
+ The owner turns them on by adding flags to the server's `args` in the MCP
35
+ client config; `simetra init` does not add them, because the user and the
36
+ variable are the owner's choice. The CLI takes the same flags per call:
37
+
38
+ ```sh
39
+ pnpm exec simetra mcp --data-url-env SIMETRA_DEV_DATA_URL --as <user-uuid>
40
+ pnpm exec simetra mcp --data-url-env SIMETRA_DEV_DATA_URL --as <user-uuid> --allow-data-write
41
+ pnpm exec simetra data-read '{"query":{"object":{"kind":"Catalog","name":"Currency"},"where":{},"limit":50}}' --data-url-env SIMETRA_DEV_DATA_URL --as <user-uuid>
42
+ ```
43
+
44
+ - The variable holds the connection string of the runtime login role
45
+ `simetra_runtime` (the platform gives it no password: the owner sets one on
46
+ the development database). A login that is a superuser, bypasses RLS, or
47
+ belongs to the migrator or the provider's service role is refused.
48
+ - Without `--data-url-env` and `--as` the data tools are refused with a hint;
49
+ there is no default variable.
50
+ - `--allow-data-write` (MCP only): without it `data-command` is refused with a
51
+ hint. Ask the owner; never add it yourself. The CLI writes data without it:
52
+ the person who runs the command is the decision. `--read-only` refuses
53
+ `data-command` and keeps `data-read`.
54
+ - `--allow-rights-write` (MCP and CLI): without it `data-command` refuses to
55
+ write the rights catalogs (roles, role assignments) and to create a scope
56
+ root whose creator receives a role. Membership catalogs are written without
57
+ it, under the rights of `--as`; active membership is what makes a role
58
+ assignment effective, so writing membership still changes who holds rights.
59
+ - An object with a module (`<Name>.module.ts` next to its `.meta.json`) is
60
+ refused with `command.invalid`, reason `object-module`, until a later
61
+ version: the tools do not load modules, so its hooks would not run.
62
+ Creating a scope root whose creator receives a role (with or without
63
+ `creatorMember`) also inserts the creator's membership row, so it needs both
64
+ `--allow-data-write` and `--allow-rights-write`, and it is refused the same
65
+ way when the membership catalog of that scope kind has a module.
66
+ - Every call compiles the directory and checks it against the applied model.
67
+ After a metadata change the call gets `command.model-mismatch` until you
68
+ `plan` and `apply` it.
69
+ - A runtime refusal comes back as `error` with a `code` (and a `reason`);
70
+ `BigInt` values come as decimal strings and may be passed back as strings.
71
+
72
+ Read a page of rows:
73
+
74
+ ```json simetra:data-read
75
+ {
76
+ "query": {
77
+ "object": { "kind": "Catalog", "name": "Currency" },
78
+ "where": { "symbol": "$" },
79
+ "limit": 50
80
+ }
81
+ }
82
+ ```
83
+
84
+ Seed rows in one transaction (`id` is a new UUID you generate; `update`,
85
+ `markDeletion` and `delete` also need `expectedVersion`):
86
+
87
+ ```json simetra:data-command
88
+ {
89
+ "operations": [
90
+ {
91
+ "action": "create",
92
+ "object": { "kind": "Catalog", "name": "Currency" },
93
+ "id": "0b9d4c5e-8f1a-4c3b-9e2d-6a7f8b9c0d1e",
94
+ "values": { "description": "US dollar", "symbol": "$" }
95
+ }
96
+ ]
97
+ }
98
+ ```
99
+
100
+ The tools work only on a development database. Before connecting they check
101
+ that the host is this machine (`localhost`, `127.0.0.0/8`, `::1` or a unix
102
+ socket); after connecting, that the database itself carries
103
+ `simetra.environment = 'development'` (a session setting such as
104
+ `options=-c …` does not count). Only a superuser sets it. On a local Supabase
105
+ stack, once per database volume (`<project_id>` is from
106
+ `supabase/config.toml`):
107
+
108
+ ```bash
109
+ docker exec -i supabase_db_<project_id> psql -U supabase_admin -d postgres -c "ALTER DATABASE postgres SET simetra.environment = 'development'"
110
+ ```
111
+
112
+ Never set it on a shared or hosted database: agent access to other databases
113
+ comes in a later version.
114
+
115
+ These checks are accident guards, not a security boundary. They stop a
116
+ launch against the wrong database; they do not stop an agent: an agent with
117
+ a shell can call the CLI itself, flags included. The boundary is the
118
+ `simetra_runtime` credentials: whoever holds them can act as any user. Keep
119
+ them only in the environment of the development database.
@@ -0,0 +1,60 @@
1
+ # Object modules: TypeScript hooks of a command
2
+
3
+ How the server code of an application adds behaviour to the generated
4
+ commands of an object; the router is `SKILL.md`. The full contract is §6.9 of
5
+ the P4 runtime spec in the Simetra repository
6
+ (`docs/superpowers/specs/2026-10-26-p4-runtime-design.md`).
7
+
8
+ ## Shape
9
+
10
+ An object module is a plain object typed by the generated entity type
11
+ (`entities.d.ts` from `compile --out`) and keyed by 1C write events:
12
+
13
+ ```ts
14
+ import { defineObjectModule } from "simetra/model"
15
+ import type { Catalogs } from "./generated/entities" // the compile --out file
16
+
17
+ export const client = defineObjectModule<Catalogs.Client>({
18
+ beforeWrite({ ctx, draft }) {
19
+ return draft
20
+ },
21
+ onWrite({ ctx, row }) {},
22
+ })
23
+ ```
24
+
25
+ - `beforeWrite` (1C `ПередЗаписью`) runs before the core function and returns
26
+ the draft `{ values?, tabularSections? }` the phase writes. It never adds
27
+ `id` or `mark`; the runtime checks the returned draft again.
28
+ - `onWrite` (1C `ПриЗаписи`) sees the written row in the same transaction.
29
+ - Both run on the operations `create`, `update` and `markDeletion`;
30
+ `delete` raises no module event. On `markDeletion` the draft is empty and
31
+ the mark is `ctx.mark`.
32
+ - The host passes modules to `createCommandRuntime` (`simetra/server`) as
33
+ `objectModules`, keyed `"<Kind>.<Name>"` (`"Catalog.Client"`). An unknown
34
+ object or hook key fails runtime creation, not the first command.
35
+
36
+ ## The context
37
+
38
+ - `ctx.action` is the operation (`create`, `update`, `markDeletion`),
39
+ `ctx.isNew` is `create`; `ctx.user` is the current user.
40
+ - `ctx.additionalProperties` is shared by the hooks of one phase only.
41
+ - `ctx.run(operation)` runs a phase of another object in the same
42
+ transaction, with its own rights check and hooks; `ctx.read(query)` reads in
43
+ that transaction.
44
+ - `ctx.reject(code, path?)` fails the command with `command.rejected`.
45
+ - A `ctx` method works only while its hook runs. Never keep `ctx` for later:
46
+ a late call fails the command (`command.internal`).
47
+ - A failure the hook catches still fails the command; await every `ctx.run`.
48
+
49
+ ## No external I/O in a hook
50
+
51
+ A serialization failure or a deadlock reruns the whole transaction, hooks
52
+ included. A hook never calls HTTP, mail or a queue outside the database. Put
53
+ such work into `ctx.afterCommit(fn)`: it runs after `COMMIT`, at most once,
54
+ and only for non-critical work (a crash after `COMMIT` loses it); its error
55
+ does not undo the command. On a serverless host, pass `defer` (`waitUntil`) to
56
+ `createCommandRuntime`, or `run` waits for this work before it returns.
57
+
58
+ A hook gets no database client and no SQL text. A row rule that must hold for
59
+ every writer belongs in the database (`EventSubscription`, row rules, rights),
60
+ not in a hook.