From e4a4dc4a1026bce60ee804de9215fd04ceec3610 Mon Sep 17 00:00:00 2001 From: Jack Herrington Date: Sun, 29 Nov 2026 19:45:46 -0800 Subject: [PATCH 1/6] =?UTF-8?q?fix(cms-skills):=20break=20engine=20?= =?UTF-8?q?=E2=86=94=20registry=20import=20cycle=20(TDZ=20in=20bundle)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The packed-consumer smoke failed at runtime with 'Cannot access 'formsLeadCaptureSkill' before initialization'. engine.ts imported CORE_SKILLS from the registry while every registry skill file imported defineSkill back from engine.ts, forming a cycle whose module-init order trips a TDZ once the package is bundled. Move the trivial defineSkill identity helper into a leaf module (engine/define.ts, importing only types); re-export it from engine.ts for compatibility. Skill files now import it from the leaf, so the registry no longer depends on engine.ts. Co-Authored-By: Claude Opus 4.8 --- packages/cms-skills/src/engine/define.ts | 17 +++++++++++++++++ packages/cms-skills/src/engine/engine.ts | 13 ++++--------- packages/cms-skills/src/registry/atproto.ts | 2 +- .../src/registry/faceted-directory.ts | 2 +- .../src/registry/forms-lead-capture.ts | 2 +- .../src/registry/redirects-and-broken-links.ts | 2 +- .../cms-skills/src/registry/seo-autonomous.ts | 2 +- 7 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 packages/cms-skills/src/engine/define.ts diff --git a/packages/cms-skills/src/engine/define.ts b/packages/cms-skills/src/engine/define.ts new file mode 100644 index 00000000..1e6b3ba2 --- /dev/null +++ b/packages/cms-skills/src/engine/define.ts @@ -0,0 +1,17 @@ +import type { CmsSkillManifest } from './types' + +/** + * Identity helper for authoring a {@link CmsSkillManifest}. Preserves literal + * type inference (via the `const` type parameter) the way other seams in the + * repo do with `defineCollection`/`defineBlock`/etc, while still statically + * checking the object against the manifest contract. + * + * This lives in its own leaf module — importing only types — so the registry's + * skill files can author manifests without importing `engine.ts`, which imports + * the registry back. That cycle (engine → registry → skill files → engine) trips + * a module-init TDZ (`Cannot access 'formsLeadCaptureSkill' before + * initialization`) in the bundled package. + */ +export function defineSkill(skill: T): T { + return skill +} diff --git a/packages/cms-skills/src/engine/engine.ts b/packages/cms-skills/src/engine/engine.ts index 62efc09b..fb48b89b 100644 --- a/packages/cms-skills/src/engine/engine.ts +++ b/packages/cms-skills/src/engine/engine.ts @@ -441,15 +441,10 @@ export const ${colName}Collection = ${JSON.stringify( } } -/** - * Identity helper for authoring a {@link CmsSkillManifest}. Preserves literal - * type inference (via the `const` type parameter) the way other seams in the - * repo do with `defineCollection`/`defineBlock`/etc, while still statically - * checking the object against the manifest contract. - */ -export function defineSkill(skill: T): T { - return skill -} +// `defineSkill` moved to ./define (a leaf module) to break the engine ↔ registry +// import cycle; re-exported here so existing `@tanstack/cms-skills` consumers and +// the package barrel keep importing it from the engine. +export { defineSkill } from './define' /** * Creates a fresh, isolated {@link SkillEngine} instance pre-registered with diff --git a/packages/cms-skills/src/registry/atproto.ts b/packages/cms-skills/src/registry/atproto.ts index bfc04447..6337bed8 100644 --- a/packages/cms-skills/src/registry/atproto.ts +++ b/packages/cms-skills/src/registry/atproto.ts @@ -1,4 +1,4 @@ -import { defineSkill } from '../engine/engine' +import { defineSkill } from '../engine/define' export const atprotoConnectorSkill = defineSkill({ id: '@tanstack/skill-connect-atproto', diff --git a/packages/cms-skills/src/registry/faceted-directory.ts b/packages/cms-skills/src/registry/faceted-directory.ts index ff48e6d8..94b739c2 100644 --- a/packages/cms-skills/src/registry/faceted-directory.ts +++ b/packages/cms-skills/src/registry/faceted-directory.ts @@ -1,4 +1,4 @@ -import { defineSkill } from '../engine/engine' +import { defineSkill } from '../engine/define' export const facetedDirectorySkill = defineSkill({ id: '@tanstack/skill-faceted-directory', diff --git a/packages/cms-skills/src/registry/forms-lead-capture.ts b/packages/cms-skills/src/registry/forms-lead-capture.ts index b4196e7a..5d3b9410 100644 --- a/packages/cms-skills/src/registry/forms-lead-capture.ts +++ b/packages/cms-skills/src/registry/forms-lead-capture.ts @@ -1,4 +1,4 @@ -import { defineSkill } from '../engine/engine' +import { defineSkill } from '../engine/define' export const formsLeadCaptureSkill = defineSkill({ id: '@tanstack/skill-forms-lead-capture', diff --git a/packages/cms-skills/src/registry/redirects-and-broken-links.ts b/packages/cms-skills/src/registry/redirects-and-broken-links.ts index b6b3b3f0..3b0ec454 100644 --- a/packages/cms-skills/src/registry/redirects-and-broken-links.ts +++ b/packages/cms-skills/src/registry/redirects-and-broken-links.ts @@ -1,4 +1,4 @@ -import { defineSkill } from '../engine/engine' +import { defineSkill } from '../engine/define' export const redirectsAndBrokenLinksSkill = defineSkill({ id: '@tanstack/skill-redirects-and-broken-links', diff --git a/packages/cms-skills/src/registry/seo-autonomous.ts b/packages/cms-skills/src/registry/seo-autonomous.ts index 11354149..4c4f6a56 100644 --- a/packages/cms-skills/src/registry/seo-autonomous.ts +++ b/packages/cms-skills/src/registry/seo-autonomous.ts @@ -1,4 +1,4 @@ -import { defineSkill } from '../engine/engine' +import { defineSkill } from '../engine/define' import type { DataField } from '@tanstack/cms-core' export const seoMixinField: DataField = { From d381e7ff6593483a06a9022861ea5c4c6829b334 Mon Sep 17 00:00:00 2001 From: Jack Herrington Date: Sun, 29 Nov 2026 19:56:40 -0800 Subject: [PATCH 2/6] fix(examples): ship PGlite runtime assets for the embedded-engine starters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit payload-after, astro-starlight-after, emdash-after, wordpress-after, and blog-voting run on the embedded PGlite engine but 500'd on every content page in test:build — the production build inlines PGlite into the nitro server chunk and repoints its asset lookups where the WASM runtime, fs image, and contrib bundles (pglite.data / pg_trgm.tar.gz) don't ship. blog already fixed this with a per-app copy script; generalize it to a shared scripts/copy-pglite-assets.mjs (app root from cwd, PGlite resolved from the app's deps), wire it into each build, and declare @electric-sql/pglite + @tanstack/cms-store-postgres. Verified: blog-voting smoke-ssr now serves / and /content 200, admin paths 404, no 5xx. Co-Authored-By: Claude Opus 4.8 --- examples/astro-starlight-after/package.json | 4 +- examples/blog-voting/package.json | 4 +- examples/emdash-after/package.json | 4 +- examples/payload-after/package.json | 4 +- examples/wordpress-after/package.json | 4 +- pnpm-lock.yaml | 30 ++++++++ scripts/copy-pglite-assets.mjs | 78 +++++++++++++++++++++ 7 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 scripts/copy-pglite-assets.mjs diff --git a/examples/astro-starlight-after/package.json b/examples/astro-starlight-after/package.json index 9be52ebb..a9c3c545 100644 --- a/examples/astro-starlight-after/package.json +++ b/examples/astro-starlight-after/package.json @@ -5,7 +5,7 @@ "description": "Result of the Astro Starlight → TanStack CMS migration. A thin @tanstack/cms-core consumer.", "scripts": { "dev": "vite dev --port 3013", - "build": "vite build", + "build": "vite build && node ../../scripts/copy-pglite-assets.mjs", "test:build": "node ../../scripts/smoke-ssr.mjs", "serve": "vite preview", "start": "node .output/server/index.mjs", @@ -14,6 +14,7 @@ "migrate:data": "node scripts/migrate-starlight.mjs ../astro-starlight-before" }, "dependencies": { + "@electric-sql/pglite": "^0.5.15", "@mcp-b/transports": "^5.1.0", "@modelcontextprotocol/sdk": "^1.32.2", "@tanstack/ai": "^0.71.0", @@ -21,6 +22,7 @@ "@tanstack/ai-gemini": "^0.38.2", "@tanstack/ai-mcp": "^0.9.2", "@tanstack/cms-core": "workspace:*", + "@tanstack/cms-store-postgres": "workspace:*", "@tanstack/cms-ui-react": "workspace:*", "@tanstack/react-router": "^1.170.46", "@tanstack/react-start": "^1.169.4", diff --git a/examples/blog-voting/package.json b/examples/blog-voting/package.json index b611b97d..a15e0d87 100644 --- a/examples/blog-voting/package.json +++ b/examples/blog-voting/package.json @@ -5,7 +5,7 @@ "description": "Example TanStack CMS app — a blog with voting. Thin consumer of @tanstack/cms-core.", "scripts": { "dev": "vite dev --port 3004", - "build": "vite build", + "build": "vite build && node ../../scripts/copy-pglite-assets.mjs", "test:build": "node ../../scripts/smoke-ssr.mjs", "serve": "vite preview", "start": "node .output/server/index.mjs", @@ -14,6 +14,7 @@ "test": "vitest run" }, "dependencies": { + "@electric-sql/pglite": "^0.5.15", "@mcp-b/transports": "^5.1.0", "@modelcontextprotocol/sdk": "^1.32.2", "@tanstack/ai": "^0.71.0", @@ -21,6 +22,7 @@ "@tanstack/ai-gemini": "^0.38.2", "@tanstack/ai-mcp": "^0.9.2", "@tanstack/cms-core": "workspace:*", + "@tanstack/cms-store-postgres": "workspace:*", "@tanstack/cms-ui-react": "workspace:*", "@tanstack/react-router": "^1.170.46", "@tanstack/react-start": "^1.169.4", diff --git a/examples/emdash-after/package.json b/examples/emdash-after/package.json index ab8aa05f..d4ad6e3e 100644 --- a/examples/emdash-after/package.json +++ b/examples/emdash-after/package.json @@ -5,7 +5,7 @@ "description": "Result of the EmDash → TanStack CMS migration. A thin @tanstack/cms-core consumer.", "scripts": { "dev": "vite dev --port 3015", - "build": "vite build", + "build": "vite build && node ../../scripts/copy-pglite-assets.mjs", "test:build": "node ../../scripts/smoke-ssr.mjs", "serve": "vite preview", "start": "node .output/server/index.mjs", @@ -14,6 +14,7 @@ "migrate:data": "node scripts/migrate-emdash.mjs ../emdash-before/seed/seed.json" }, "dependencies": { + "@electric-sql/pglite": "^0.5.15", "@mcp-b/transports": "^5.1.0", "@modelcontextprotocol/sdk": "^1.32.2", "@tanstack/ai": "^0.71.0", @@ -21,6 +22,7 @@ "@tanstack/ai-gemini": "^0.38.2", "@tanstack/ai-mcp": "^0.9.2", "@tanstack/cms-core": "workspace:*", + "@tanstack/cms-store-postgres": "workspace:*", "@tanstack/cms-ui-react": "workspace:*", "@tanstack/react-router": "^1.170.46", "@tanstack/react-start": "^1.169.4", diff --git a/examples/payload-after/package.json b/examples/payload-after/package.json index 1c7ab966..d3f1fd0e 100644 --- a/examples/payload-after/package.json +++ b/examples/payload-after/package.json @@ -5,7 +5,7 @@ "description": "Result of the Payload → TanStack CMS migration. A thin @tanstack/cms-core consumer.", "scripts": { "dev": "vite dev --port 3011", - "build": "vite build", + "build": "vite build && node ../../scripts/copy-pglite-assets.mjs", "test:build": "node ../../scripts/smoke-ssr.mjs", "serve": "vite preview", "start": "node .output/server/index.mjs", @@ -14,6 +14,7 @@ "migrate:data": "node scripts/migrate-from-payload.mjs" }, "dependencies": { + "@electric-sql/pglite": "^0.5.15", "@mcp-b/transports": "^5.1.0", "@modelcontextprotocol/sdk": "^1.32.2", "@tanstack/ai": "^0.71.0", @@ -21,6 +22,7 @@ "@tanstack/ai-gemini": "^0.38.2", "@tanstack/ai-mcp": "^0.9.2", "@tanstack/cms-core": "workspace:*", + "@tanstack/cms-store-postgres": "workspace:*", "@tanstack/cms-ui-react": "workspace:*", "@tanstack/react-router": "^1.170.46", "@tanstack/react-start": "^1.169.4", diff --git a/examples/wordpress-after/package.json b/examples/wordpress-after/package.json index e64f5ad4..0e7e2257 100644 --- a/examples/wordpress-after/package.json +++ b/examples/wordpress-after/package.json @@ -5,7 +5,7 @@ "description": "Result of the WordPress → TanStack CMS migration. A thin @tanstack/cms-core consumer.", "scripts": { "dev": "vite dev --port 3016", - "build": "vite build", + "build": "vite build && node ../../scripts/copy-pglite-assets.mjs", "test:build": "node ../../scripts/smoke-ssr.mjs", "serve": "vite preview", "start": "node .output/server/index.mjs", @@ -14,6 +14,7 @@ "migrate:data": "node scripts/migrate-wordpress.mjs ../wordpress-before/export.wxr.xml" }, "dependencies": { + "@electric-sql/pglite": "^0.5.15", "@mcp-b/transports": "^5.1.0", "@modelcontextprotocol/sdk": "^1.32.2", "@tanstack/ai": "^0.71.0", @@ -21,6 +22,7 @@ "@tanstack/ai-gemini": "^0.38.2", "@tanstack/ai-mcp": "^0.9.2", "@tanstack/cms-core": "workspace:*", + "@tanstack/cms-store-postgres": "workspace:*", "@tanstack/cms-ui-react": "workspace:*", "@tanstack/react-router": "^1.170.46", "@tanstack/react-start": "^1.169.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f0a2769d..7de4dd1a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -455,6 +455,9 @@ importers: examples/astro-starlight-after: dependencies: + '@electric-sql/pglite': + specifier: ^0.5.15 + version: 0.5.15 '@mcp-b/transports': specifier: ^5.1.0 version: 5.1.0 @@ -476,6 +479,9 @@ importers: '@tanstack/cms-core': specifier: workspace:* version: link:../../packages/core + '@tanstack/cms-store-postgres': + specifier: workspace:* + version: link:../../packages/store-postgres '@tanstack/cms-ui-react': specifier: workspace:* version: link:../../packages/ui-react @@ -628,6 +634,9 @@ importers: examples/blog-voting: dependencies: + '@electric-sql/pglite': + specifier: ^0.5.15 + version: 0.5.15 '@mcp-b/transports': specifier: ^5.1.0 version: 5.1.0 @@ -649,6 +658,9 @@ importers: '@tanstack/cms-core': specifier: workspace:* version: link:../../packages/core + '@tanstack/cms-store-postgres': + specifier: workspace:* + version: link:../../packages/store-postgres '@tanstack/cms-ui-react': specifier: workspace:* version: link:../../packages/ui-react @@ -850,6 +862,9 @@ importers: examples/emdash-after: dependencies: + '@electric-sql/pglite': + specifier: ^0.5.15 + version: 0.5.15 '@mcp-b/transports': specifier: ^5.1.0 version: 5.1.0 @@ -871,6 +886,9 @@ importers: '@tanstack/cms-core': specifier: workspace:* version: link:../../packages/core + '@tanstack/cms-store-postgres': + specifier: workspace:* + version: link:../../packages/store-postgres '@tanstack/cms-ui-react': specifier: workspace:* version: link:../../packages/ui-react @@ -923,6 +941,9 @@ importers: examples/payload-after: dependencies: + '@electric-sql/pglite': + specifier: ^0.5.15 + version: 0.5.15 '@mcp-b/transports': specifier: ^5.1.0 version: 5.1.0 @@ -944,6 +965,9 @@ importers: '@tanstack/cms-core': specifier: workspace:* version: link:../../packages/core + '@tanstack/cms-store-postgres': + specifier: workspace:* + version: link:../../packages/store-postgres '@tanstack/cms-ui-react': specifier: workspace:* version: link:../../packages/ui-react @@ -1127,6 +1151,9 @@ importers: examples/wordpress-after: dependencies: + '@electric-sql/pglite': + specifier: ^0.5.15 + version: 0.5.15 '@mcp-b/transports': specifier: ^5.1.0 version: 5.1.0 @@ -1148,6 +1175,9 @@ importers: '@tanstack/cms-core': specifier: workspace:* version: link:../../packages/core + '@tanstack/cms-store-postgres': + specifier: workspace:* + version: link:../../packages/store-postgres '@tanstack/cms-ui-react': specifier: workspace:* version: link:../../packages/ui-react diff --git a/scripts/copy-pglite-assets.mjs b/scripts/copy-pglite-assets.mjs new file mode 100644 index 00000000..1d887206 --- /dev/null +++ b/scripts/copy-pglite-assets.mjs @@ -0,0 +1,78 @@ +// PGlite loads its WASM runtime (`pglite.wasm`/`initdb.wasm`), filesystem image +// (`pglite.data`), and contrib extension bundles (`pg_trgm.tar.gz`, …) as binary +// files at runtime, resolving them relative to its own module via +// `import.meta.url` / emscripten's `locateFile`. The production build inlines +// PGlite into the nitro server chunk, which repoints those lookups at +// `.output/server/_ssr` (and the server root) where the assets don't ship — so +// the embedded engine 500s with "Extension bundle not found" / "ENOENT +// pglite.data". nitro doesn't trace these non-JS assets, so copy them into both +// locations the inlined loader probes, after `vite build`. +// +// Shared across every example that runs on the embedded PGlite engine. Run it +// from the app directory (`node ../../scripts/copy-pglite-assets.mjs`); the app +// root is taken from the current working directory. +import { createRequire } from 'node:module' +import { + cpSync, + existsSync, + mkdirSync, + readFileSync, + readdirSync, + statSync, +} from 'node:fs' +import { dirname, join, resolve } from 'node:path' + +const root = resolve(process.cwd()) +// Resolve PGlite from the calling app's dependencies (this shared script lives +// at the repo root, which doesn't declare PGlite — each embedded-engine app +// does). The package blocks `./package.json` in its `exports`, so resolve the +// main entry (`dist/index.js`) and take its directory as the asset source. +const require = createRequire(join(root, 'package.json')) +const distDir = dirname(require.resolve('@electric-sql/pglite')) + +const serverDir = join(root, '.output/server') +if (!existsSync(serverDir)) { + console.error( + `copy-pglite-assets: ${serverDir} not found; run \`vite build\` first.`, + ) + process.exit(1) +} + +// The inlined PGlite chunk resolves its data/wasm via `import.meta.url`, so the +// assets must sit next to it — but nitro's chunk directory (`_libs`, `_ssr`, …) +// isn't stable across builds. Find every chunk that references `pglite.data` +// and target its directory, plus the server root (emscripten `locateFile`'s +// base for the extension tarballs). +function chunkDirs(dir) { + const found = new Set() + const walk = (d) => { + for (const name of readdirSync(d)) { + const full = join(d, name) + if (statSync(full).isDirectory()) walk(full) + else if ( + name.endsWith('.mjs') && + readFileSync(full, 'utf8').includes('pglite.data') + ) + found.add(d) + } + } + walk(dir) + return [...found] +} + +const targets = [serverDir, ...chunkDirs(serverDir)] +const isAsset = (name) => + /\.(data|wasm)$/.test(name) || name.endsWith('.tar.gz') + +let copied = 0 +for (const name of readdirSync(distDir).filter(isAsset)) { + for (const target of targets) { + mkdirSync(target, { recursive: true }) + cpSync(join(distDir, name), join(target, name)) + copied++ + } +} +const rel = targets.map((t) => t.slice(serverDir.length) || '/').join(', ') +console.log( + `copy-pglite-assets: copied ${copied} files into .output/server [${rel}]`, +) From aa251c62c2cdd6d70947f38ee03488ffcdc37b43 Mon Sep 17 00:00:00 2001 From: Jack Herrington Date: Sun, 29 Nov 2026 20:02:22 -0800 Subject: [PATCH 3/6] test: update stale route expectations; raise cms-core test timeout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three pre-existing failures CI surfaced: - verify-definitions: the clean fixture's route count grew 17→20 (admin UI gained /media and /profile); update the expected accessor snapshot. - admin-only seam: createRoutes now mounts /media and /profile; add them to the expected mounted-route list (contact-cards/team-ops configs don't mount them). - cms-core:test:lib flaked on 2 PGlite-backed tests timing out at the 5s default on the minimum-Node runner (647/647 pass locally). Raise the suite testTimeout to 20s — these spin up the embedded engine and one bulk-imports 1000 docs. Co-Authored-By: Claude Opus 4.8 --- packages/core/vitest.config.ts | 4 ++++ scripts/verify-definitions.test.ts | 2 +- starters/admin-only/tests/seam.test.ts | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/core/vitest.config.ts b/packages/core/vitest.config.ts index 91fdf808..03ad03c0 100644 --- a/packages/core/vitest.config.ts +++ b/packages/core/vitest.config.ts @@ -8,6 +8,10 @@ export default defineConfig({ watch: false, globals: true, environment: 'node', + // Many tests spin up the embedded PGlite engine (WASM init) and some bulk- + // import thousands of documents; the 5s default flakes out on loaded CI + // runners (e.g. the minimum-Node job). Give DB-backed tests headroom. + testTimeout: 20_000, include: ['tests/**/*.test.ts', 'src/**/*.test.ts'], coverage: { provider: 'v8', diff --git a/scripts/verify-definitions.test.ts b/scripts/verify-definitions.test.ts index 6987d809..dfb69c9b 100644 --- a/scripts/verify-definitions.test.ts +++ b/scripts/verify-definitions.test.ts @@ -28,7 +28,7 @@ describe('verifyDefinition', { timeout: 30_000 }, () => { adminBasePath: '"/admin"', collections: '1 collection(s)', pluginRoutes: '0 route(s), 1 admin route(s)', - routeKeys: '17 key(s)', + routeKeys: '20 key(s)', }) }) diff --git a/starters/admin-only/tests/seam.test.ts b/starters/admin-only/tests/seam.test.ts index afa8e538..802c5034 100644 --- a/starters/admin-only/tests/seam.test.ts +++ b/starters/admin-only/tests/seam.test.ts @@ -38,6 +38,8 @@ describe('starter-admin-only — Mode #2 (Seam S1, S2, S3) acceptance suite', () '/agent', '/review', '/settings', + '/media', + '/profile', '/api/cms', '/api/cms/v1/$', '/api/mcp', From b150b3a905e53dafda000768843991e49bbbb328 Mon Sep 17 00:00:00 2001 From: Jack Herrington Date: Sun, 29 Nov 2026 20:06:07 -0800 Subject: [PATCH 4/6] test(base,prm): raise PGlite-backed test timeout to 20s prm crm.test.ts and base worker/wp-evacuation tests flaked at the 5s default on loaded CI runners (all pass locally); they drive the embedded PGlite engine and multi-step governance flows. Match the cms-core bump. Co-Authored-By: Claude Opus 4.8 --- app/base/vitest.config.ts | 3 +++ app/prm/vitest.config.ts | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/base/vitest.config.ts b/app/base/vitest.config.ts index 6cb6403c..ace66a6b 100644 --- a/app/base/vitest.config.ts +++ b/app/base/vitest.config.ts @@ -5,6 +5,9 @@ export default defineConfig({ environment: 'node', include: ['tests/**/*.test.ts'], fileParallelism: false, + // Tests drive the embedded PGlite engine (WASM init) and multi-step + // governance flows; the 5s default flakes out on loaded CI runners. + testTimeout: 20_000, coverage: { provider: 'v8', include: ['src/**/*.ts'], diff --git a/app/prm/vitest.config.ts b/app/prm/vitest.config.ts index 7a74312d..237455a8 100644 --- a/app/prm/vitest.config.ts +++ b/app/prm/vitest.config.ts @@ -1,5 +1,10 @@ import { defineConfig } from 'vitest/config' export default defineConfig({ - test: { environment: 'node', include: ['tests/**/*.test.ts'] }, + test: { + environment: 'node', + include: ['tests/**/*.test.ts'], + // PGlite-backed governance tests exceed the 5s default on loaded CI runners. + testTimeout: 20_000, + }, }) From fe20a976aa7208a5ad8279bbd5bc39626ff08367 Mon Sep 17 00:00:00 2001 From: Jack Herrington Date: Sun, 29 Nov 2026 20:15:13 -0800 Subject: [PATCH 5/6] fix(base): port recover-owner to the embedded PGlite store MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit recover-owner.mjs was written for the old SQLite store: it opened cms.db via node:sqlite and ran raw column UPDATEs. Base now runs on embedded PGlite, which keeps each auth user's fields in a record JSONB column, prefixes its tables (default cms_), and reshaped audit — so the script threw 'No existing shop database found.' Reopen ${CMS_DATA_DIR}/pglite with @electric-sql/pglite, discover the table prefix from information_schema, rewrite the owner's record JSONB (reset password, role=admin, clear disabled/invite), delete its user_roles rows, append an audit entry in the new shape, and rotate the auth-secret file. Declare @electric-sql/pglite in base so the offline script can import it. Verified: base.test.ts green (18/18), with and without POSTGRES_TEST_URL. Co-Authored-By: Claude Opus 4.8 --- app/base/package.json | 1 + app/base/scripts/recover-owner.mjs | 92 ++++++++++++++++++++---------- pnpm-lock.yaml | 3 + 3 files changed, 67 insertions(+), 29 deletions(-) diff --git a/app/base/package.json b/app/base/package.json index a4aeeeaa..48f3c9d7 100644 --- a/app/base/package.json +++ b/app/base/package.json @@ -18,6 +18,7 @@ "test:review": "node scripts/review-e2e.mjs" }, "dependencies": { + "@electric-sql/pglite": "^0.5.15", "@jitl/quickjs-wasmfile-debug-asyncify": "^0.32.0", "@jitl/quickjs-wasmfile-debug-sync": "^0.32.0", "@jitl/quickjs-wasmfile-release-asyncify": "^0.32.0", diff --git a/app/base/scripts/recover-owner.mjs b/app/base/scripts/recover-owner.mjs index 5abd3699..d4e61307 100644 --- a/app/base/scripts/recover-owner.mjs +++ b/app/base/scripts/recover-owner.mjs @@ -1,8 +1,14 @@ // Offline maintenance only. Stop Base first. Password comes from stdin. +// +// Base runs on the embedded PGlite engine (a directory under CMS_DATA_DIR, not a +// SQLite file). The CMS store keeps each auth user's fields in a `record` JSONB +// column and prefixes its tables (default `cms_`); this script reopens that +// database offline and rewrites the owner's record so a locked-out operator can +// regain admin access, then rotates the session secret to invalidate old cookies. import { randomBytes, randomUUID, scryptSync } from 'node:crypto' import { existsSync, readFileSync, writeFileSync } from 'node:fs' import { join } from 'node:path' -import { DatabaseSync } from 'node:sqlite' +import { PGlite } from '@electric-sql/pglite' const email = process.argv[2]?.trim().toLowerCase() if (!email) @@ -16,40 +22,68 @@ const salt = randomBytes(16) const hash = scryptSync(password, salt, 64, { N: 16384, r: 8, p: 1 }).toString( 'hex', ) + const dir = process.env.CMS_DATA_DIR ?? '.data' -if (!existsSync(join(dir, 'cms.db'))) - throw new Error('No existing shop database found.') -const db = new DatabaseSync(join(dir, 'cms.db'), { open: true }) +const dataDir = join(dir, 'pglite') +if (!existsSync(dataDir)) throw new Error('No existing shop database found.') + +const db = await PGlite.create({ dataDir }) try { - db.exec('BEGIN IMMEDIATE') - const user = db - .prepare('SELECT id FROM auth_users WHERE email = ?') - .get(email) - if (!user) + // Tables carry the store's prefix (default `cms_`); discover it so this works + // regardless of how the instance was configured. + const { rows: tables } = await db.query( + `SELECT table_name FROM information_schema.tables + WHERE table_schema = 'public' AND table_name LIKE '%auth_users'`, + ) + const authTable = tables[0]?.table_name + if (!authTable) throw new Error('No existing shop database found.') + const prefix = authTable.slice(0, -'auth_users'.length) + + await db.query('BEGIN') + const { rows } = await db.query( + `SELECT record FROM ${authTable} WHERE email = $1`, + [email], + ) + const record = rows[0]?.record + if (!record) throw new Error( 'No existing account matches that email. Recovery never creates an account.', ) - db.prepare( - "UPDATE auth_users SET password_hash = ?, role = 'admin', disabled_at = NULL, invite_token_hash = NULL, invite_expires_at = NULL, updated_at = ? WHERE id = ?", - ).run( - `scrypt$16384$8$1$${salt.toString('hex')}$${hash}`, - new Date().toISOString(), - user.id, + + const now = new Date().toISOString() + const recovered = { + ...record, + passwordHash: `scrypt$16384$8$1$${salt.toString('hex')}$${hash}`, + role: 'admin', + updatedAt: now, + } + // Clear the disabled flag and any pending invite/reset so the owner can sign in. + delete recovered.disabledAt + delete recovered.inviteTokenHash + delete recovered.inviteExpiresAt + + await db.query( + `UPDATE ${authTable} SET record = $1::jsonb, invite_token_hash = NULL WHERE id = $2`, + [JSON.stringify(recovered), record.id], ) - db.prepare('DELETE FROM user_roles WHERE user_id = ? OR email = ?').run( - user.id, - email, + await db.query( + `DELETE FROM ${prefix}user_roles WHERE user_id = $1 OR email = $2`, + [record.id, email], ) - db.prepare( - 'INSERT INTO audit (id, actor, at, action, target) VALUES (?, ?, ?, ?, ?)', - ).run( - randomUUID(), - 'offline-maintenance', - new Date().toISOString(), - 'base.owner.recovered', - user.id, + const audit = { + id: randomUUID(), + actor: 'offline-maintenance', + at: now, + action: 'base.owner.recovered', + target: record.id, + } + await db.query( + `INSERT INTO ${prefix}audit (id, collection, doc_id, record, at, change_set) + VALUES ($1, $2, $3, $4::jsonb, $5, $6)`, + [audit.id, null, null, JSON.stringify(audit), now, null], ) - db.exec('COMMIT') + await db.query('COMMIT') + if (!process.env.CMS_AUTH_SECRET) writeFileSync( join(dir, 'auth-secret'), @@ -63,8 +97,8 @@ try { : 'Session secret rotated; all existing cookies are invalid.'), ) } catch (error) { - if (db.isTransaction) db.exec('ROLLBACK') + await db.query('ROLLBACK').catch(() => {}) throw error } finally { - db.close() + await db.close() } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7de4dd1a..75a95d39 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -83,6 +83,9 @@ importers: app/base: dependencies: + '@electric-sql/pglite': + specifier: ^0.5.15 + version: 0.5.15 '@jitl/quickjs-wasmfile-debug-asyncify': specifier: ^0.32.0 version: 0.32.0 From 83c854f9ceecc6fe3db98e42be9d7ffe11c1b710 Mon Sep 17 00:00:00 2001 From: Jack Herrington Date: Sun, 29 Nov 2026 20:30:25 -0800 Subject: [PATCH 6/6] test(cms-api): raise test timeout to 30s for the minimum-Node runner The Minimum Node job runs core/api/cli vitest; cms-core and cms-cli already have headroom but cms-api's DB-backed readiness/access tests still flaked at the 5s default on the loaded runner. Match cms-cli's 30s. Co-Authored-By: Claude Opus 4.8 --- packages/api/vitest.config.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/api/vitest.config.ts b/packages/api/vitest.config.ts index 7a54f175..9b8c0639 100644 --- a/packages/api/vitest.config.ts +++ b/packages/api/vitest.config.ts @@ -7,6 +7,9 @@ export default defineConfig({ dir: './', watch: false, environment: 'node', + // DB-backed tests (embedded PGlite) flake at the 5s default on the loaded + // minimum-Node runner; match cms-cli's headroom. + testTimeout: 30_000, include: ['tests/**/*.test.ts'], coverage: { provider: 'v8',