@simetrajs/designer 0.0.0-stage → 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.
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env node
2
+ // src/tools/types.ts
3
+ /** Імена окремо від записів каталогу: `main.ts` реєструє лінощі підкоманди, не тягнучи компілятор. */
4
+ const TOOL_NAMES = [
5
+ "compile",
6
+ "explain",
7
+ "fix",
8
+ "create",
9
+ "add",
10
+ "rename",
11
+ "delete",
12
+ "promote",
13
+ "introspect",
14
+ "diff",
15
+ "plan",
16
+ "apply",
17
+ "roles",
18
+ "rights-maintenance",
19
+ "data-read",
20
+ "data-command"
21
+ ];
22
+ /**
23
+ * Каталог артефактів плану в корені проєкту, поруч із текою метаданих:
24
+ * єдиний виняток із межі теки для запису (спека designer §3.2).
25
+ */
26
+ const PLANS_DIR = "plans";
27
+ /** Виводить типи входу й даних з аргументів, щоб записи не писали generics. */
28
+ function defineTool(tool) {
29
+ return tool;
30
+ }
31
+ export { TOOL_NAMES as n, defineTool as r, PLANS_DIR as t };
package/package.json CHANGED
@@ -1,6 +1,70 @@
1
1
  {
2
2
  "name": "@simetrajs/designer",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.0",
4
+ "description": "Simetra developer tools: command line and MCP server over the metadata compiler",
5
+ "keywords": [
6
+ "simetra",
7
+ "metadata",
8
+ "postgres",
9
+ "cli",
10
+ "mcp",
11
+ "schema"
12
+ ],
13
+ "homepage": "https://github.com/VSydorenko/Simetra#readme",
14
+ "bugs": "https://github.com/VSydorenko/Simetra/issues",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/VSydorenko/Simetra.git",
18
+ "directory": "packages/designer"
19
+ },
20
+ "license": "Apache-2.0",
21
+ "type": "module",
22
+ "engines": {
23
+ "node": ">=24"
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "skills"
28
+ ],
29
+ "bin": {
30
+ "simetra": "./dist/main.js"
31
+ },
32
+ "dependencies": {
33
+ "@modelcontextprotocol/server": "2.4.0",
34
+ "@supabase/pg-delta": "1.0.0-alpha.60",
35
+ "citty": "0.2.2",
36
+ "pg": "8.23.1",
37
+ "smol-toml": "1.9.1",
38
+ "zod": "^4.6.7",
39
+ "simetra": "0.2.0"
40
+ },
41
+ "devDependencies": {
42
+ "@arethetypeswrong/cli": "0.18.5",
43
+ "@eslint/js": "^10.0.1",
44
+ "@modelcontextprotocol/client": "2.4.0",
45
+ "@types/node": "^24.19.2",
46
+ "@types/pg": "^8.23.1",
47
+ "@typescript/native": "npm:typescript@^7.0.2",
48
+ "eslint": "^10.14.0",
49
+ "publint": "0.3.25",
50
+ "tsdown": "0.23.5",
51
+ "tsx": "4.23.15",
52
+ "typescript": "npm:@typescript/typescript6@^6.0.2",
53
+ "typescript-eslint": "^8.72.1",
54
+ "vitest": "^5.1.0"
55
+ },
56
+ "publishConfig": {
57
+ "access": "public"
58
+ },
59
+ "scripts": {
60
+ "build": "tsc --noEmit",
61
+ "build:package": "tsdown",
62
+ "lint": "eslint",
63
+ "format": "prettier --write \"**/*.{ts,tsx}\"",
64
+ "typecheck": "tsc --noEmit",
65
+ "test": "vitest run --project unit",
66
+ "test:watch": "vitest --project unit",
67
+ "test:db": "vitest run --project db",
68
+ "test:packaging": "vitest run --project packaging"
69
+ }
6
70
  }
@@ -0,0 +1,185 @@
1
+ ---
2
+ name: simetra-adoption
3
+ description: Use when bringing an existing live PostgreSQL database under Simetra metadata for the first time — reading its schemas into a `metadata/` directory with `simetra introspect`, then proving with `simetra diff` that the metadata and the database match. Covers choosing the scope (`--schemas`, `--tables`), the connection and shadow database settings, exit codes, and how to treat unrepresentable objects and non-empty results. Not for everyday editing of metadata (that is `simetra-metadata`).
4
+ ---
5
+
6
+ # Simetra adoption — introspect a live database, then diff
7
+
8
+ Adoption is a one-time onboarding: the database already exists, and the
9
+ metadata is derived from it. `introspect` reads the database into metadata;
10
+ `diff` compares the database with the metadata through a throwaway shadow
11
+ database. Neither ever writes to the database. Run the CLI from the project root
12
+ as `pnpm exec simetra …`; every command defaults to `./metadata`. The same tools
13
+ are served by `simetra mcp` with the same input.
14
+
15
+ ## The connection
16
+
17
+ - The connection string comes **only from the environment**: the variable
18
+ `SIMETRA_DATABASE_URL`, or another one named with
19
+ `--database-url-env <NAME>` (CLI and `simetra mcp`). Export it in the shell
20
+ or the server's environment; never paste a connection string into a command,
21
+ a file or a tool input. Output shows only `host:port/db`.
22
+ - Without the variable the call is refused with its name (exit 2).
23
+ - The target is only read, in short read-only transactions that leave no
24
+ session state behind.
25
+ - `diff` builds the shadow database on the target's server by default, so the
26
+ connecting role needs `CREATEDB`. Otherwise name a variable with
27
+ `--shadow-url-env <NAME>` pointing at another server of the **same
28
+ PostgreSQL major version**; a different major version is refused.
29
+ - Roles are cluster infrastructure: they are not described in metadata, and
30
+ grants to them are. A role the database grants to is assumed to exist; a
31
+ shadow on another server must have the same roles, or the metadata does not
32
+ load there.
33
+
34
+ ## Steps
35
+
36
+ 1. **Choose the scope.** A directory without `project.meta.json` needs
37
+ `--schemas` (MCP: `schemas`); the first schema becomes the project's default
38
+ schema. In an existing project the scope is the schemas of its metadata plus
39
+ any named with `--schemas`. Provider schemas (`auth`, `storage`, …) are not
40
+ introspected, and naming the platform's own schema `simetra` is refused. The project file is never rewritten: a `--project-name` or
41
+ `--attribute-case` that differs from it is an error, not a silent override.
42
+ A new project records its database provider explicitly (MCP:
43
+ `project.database.provider`, CLI: `--database-provider`; the default is
44
+ `supabase`); an existing project keeps the one in its file.
45
+ 2. **Preview.** Run `introspect` with `--dry-run` (MCP: `dryRun: true`) and read
46
+ the diagnostics.
47
+ 3. **Unrepresentable objects are reported, not worked around.** When anything
48
+ cannot be represented (an `EXCLUDE` constraint, an owner other than the
49
+ session role, …), `introspect` writes nothing and the diagnostics name the
50
+ object. Do not edit the database or the generated files to get past it;
51
+ report the object to the owner.
52
+ 4. **Introspect.** Run it without `--dry-run`. Tables become `CustomTable`,
53
+ enum types `PgEnum`, everything else verbatim `.sql` files; ids of objects
54
+ already described are kept, so a second run on an unchanged database
55
+ changes nothing.
56
+ The platform's row mechanics (`simetra.touch()`, its grants and the
57
+ `<table>_simetra_touch` triggers calling it) are never written, even into
58
+ a directory without metadata: the platform generates them. Another trigger
59
+ that calls `simetra.touch()` is the table's own and is written.
60
+ A foreign key to the table of an object of a kind is written by its key —
61
+ `references: { object, key: "ref" }` or `key: "scopedRef"` for the
62
+ in-tenant pair (scope column, key); a foreign key on other columns of such
63
+ a table is unrepresentable.
64
+ 5. **Diff.** Run `diff` on the same directory. **An empty diff right after
65
+ introspect is the baseline; any non-empty result is a finding** — report it
66
+ instead of editing the metadata until it disappears.
67
+ The one expected exception: adopting from scratch a database that
68
+ Simetra deployed shows the removal of each `<table>_simetra_touch`
69
+ trigger, a step that needs a permission, because a kind table comes back
70
+ as a `CustomTable` without the row mechanics (the adoption row of the
71
+ system schema and users spec in the Simetra repository,
72
+ `docs/superpowers/specs/2026-10-07-system-schema-users-design.md`).
73
+ 6. **Narrow when reconciling part of the schema.** `--tables` (MCP: `tables`)
74
+ narrows the plan, the differences and the diagnostics to the named tables
75
+ (`schema.table` or `table`) together with their enum types and sequences;
76
+ an unknown table is refused. A diagnostic that cannot be tied to a table
77
+ (one about the whole schema, a function, a view) stays, so a narrowed run
78
+ can still exit 1 because of an object outside the tables: judge the plan
79
+ and the differences, not the exit code alone.
80
+
81
+ ## Examples
82
+
83
+ A new project from the `app` schema:
84
+
85
+ ```json simetra:introspect
86
+ {
87
+ "schemas": ["app"],
88
+ "project": { "name": "Shop", "attributeCase": "snake_case" }
89
+ }
90
+ ```
91
+
92
+ Compare two tables of it:
93
+
94
+ ```json simetra:diff
95
+ { "tables": ["app.orders", "app.order_lines"] }
96
+ ```
97
+
98
+ The same from a shell:
99
+
100
+ ```sh
101
+ pnpm exec simetra introspect --schemas app --project-name Shop --dry-run
102
+ pnpm exec simetra introspect --schemas app --project-name Shop
103
+ pnpm exec simetra diff
104
+ pnpm exec simetra diff --tables app.orders,app.order_lines --format json
105
+ pnpm exec simetra diff --database-url-env STAGING_DATABASE_URL --shadow-url-env SHADOW_DATABASE_URL
106
+ ```
107
+
108
+ ## SQL debt
109
+
110
+ `introspect` also writes `metadata/sql-debt.json`: the verbatim SQL it could
111
+ not express through metadata (CustomTable and PgEnum modules, shared
112
+ `metadata/sql/`, statements a closed form does not accept).
113
+
114
+ - The list is a ratchet and only shrinks. `introspect` is the only writer and
115
+ derives it from the database; `fix` only removes stale entries; `compile`
116
+ fails with `sql.debt-grows` on a debt unit that is not listed and with
117
+ `sql.debt-stale` on an entry that is no longer debt. Without that, verbatim
118
+ SQL would grow back unnoticed.
119
+ - Never edit the list by hand to get a green compile. Replace the statement
120
+ with a property, an `EventSubscription` or a closed-shell function (see
121
+ `simetra-metadata`), then run `fix`: it removes the entry that became stale.
122
+ - A project that already has a `sql/` folder but no list is seeded by running
123
+ `introspect` again on the same directory.
124
+ - Debt is not a free pass. A function with DDL, a non-literal `EXECUTE`
125
+ (partition maintenance, statistics through `EXECUTE format(…)`), a call of
126
+ a function that executes SQL text, or a body in another language fails
127
+ `compile` with `sql.function-body-statement`; SQL that writes a table of a
128
+ kind fails with `sql.kind-table-verbatim`. Rewrite such a function with
129
+ static SQL or remove it.
130
+
131
+ ## Adopted triggers
132
+
133
+ An adopted trigger stays verbatim debt while its table is a `CustomTable`.
134
+ When `promote` turns the table into a kind, a trigger gets the
135
+ class `platform` only when the kind replaces it, and `plan` refuses
136
+ (`plan.platform-replacement-missing`) otherwise:
137
+
138
+ - `moddatetime` fired `BEFORE UPDATE … FOR EACH ROW` on the column that becomes
139
+ `updatedAt` → the row mechanics (`<table>_simetra_touch`). The mechanics also
140
+ fires on insert, which only makes the date and the version right there too.
141
+ - A trigger calling the same handler with the same timing, level and events →
142
+ an `EventSubscription` with that handler (see `simetra-metadata`), but only
143
+ when the handler only checks and assigns `NEW`. A handler that writes a
144
+ table, runs `EXECUTE` or calls a function that writes is refused
145
+ (`sql.head-not-allowed`): move the write into a command or a TypeScript
146
+ hook first, or keep the table a `CustomTable`.
147
+ - `INSERT OR UPDATE OF <columns>` → a subscription with `whenChanged` on the
148
+ same columns. The plan warns (`plan.update-of-narrowed`): the subscription
149
+ fires only when a value actually changes, not on every `UPDATE` that sets
150
+ the column.
151
+ - A trigger on `UPDATE` alone has no replacement: a write subscription fires
152
+ on insert too. Give it the class `removed` (the plan then needs the
153
+ `remove:` permission) and let the subscription's handler check `TG_OP`.
154
+ - A numbering trigger or default has no replacement until the platform
155
+ generates counters: keep the table a `CustomTable`, or remove it with the
156
+ permission and accept that the number is no longer filled.
157
+ - Anything else — another `WHEN`, other events, trigger arguments, another
158
+ function — has no replacement either.
159
+ - A foreign trigger on a provider table (`auth.users`) that holds the name of
160
+ a generated one → `plan` refuses with `plan.provider-trigger-taken`, or with
161
+ `engine.shadow-load-failed` "trigger … already exists" when the trigger's
162
+ function lives in the provider schema. Rename or drop the existing trigger;
163
+ for a subscription, another `physicalName` also works. Introspecting the
164
+ trigger does not help: it becomes a debt unit, and a subscription with the
165
+ same name is then a duplicate (`sql.unit-duplicate`).
166
+
167
+ ## Exit codes
168
+
169
+ | Code | `introspect` | `diff` |
170
+ | ---- | ------------------------------------- | --------------------------------- |
171
+ | `0` | written (or previewed) without errors | the database matches the metadata |
172
+ | `1` | error diagnostics, nothing written | differences or error diagnostics |
173
+ | `2` | the call was refused | the call was refused |
174
+
175
+ - Refused (exit 2; MCP `isError` without diagnostics): no connection variable,
176
+ an unreachable database, a failed login, a missing database or privilege, a
177
+ bad flag or input. Fix the connection or the call and rerun.
178
+ - A failure during the database work itself is the diagnostic
179
+ `database.failed` with its SQLSTATE (exit 1; MCP `isError`). The driver's
180
+ text is withheld because it may carry credentials.
181
+ - `--format json` (MCP `structuredContent`) carries the plan, the differences
182
+ and the diagnostics as data.
183
+ - `diff` also reports `applyState`: `absent` while the database holds none of
184
+ the platform's apply state (the first `apply` creates it, so it is not a
185
+ difference), `present` otherwise.