@open-mercato/cli 0.11.1-develop.8805.1.6357054d1a → 0.11.1-develop.8880.1.3acdbfc734

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 (199) hide show
  1. package/.turbo/turbo-build.log +8 -2
  2. package/dist/agentic/guides/module-facts.json +1575 -307
  3. package/dist/agentic/guides/module-facts.v2.json +1575 -307
  4. package/dist/agentic/guides/modules/agent_orchestrator/exact-override-targets.md +1 -1
  5. package/dist/agentic/guides/modules/agent_orchestrator/index.md +1 -1
  6. package/dist/agentic/guides/modules/agent_orchestrator/workers.md +1 -1
  7. package/dist/agentic/guides/modules/ai_assistant/index.md +1 -1
  8. package/dist/agentic/guides/modules/api_docs/index.md +1 -1
  9. package/dist/agentic/guides/modules/api_keys/index.md +1 -1
  10. package/dist/agentic/guides/modules/assisted_selling/index.md +1 -1
  11. package/dist/agentic/guides/modules/attachments/index.md +1 -1
  12. package/dist/agentic/guides/modules/attachments/umes-hosts.md +1 -0
  13. package/dist/agentic/guides/modules/audit_logs/index.md +1 -1
  14. package/dist/agentic/guides/modules/auth/di-registrations-rich.md +2 -1
  15. package/dist/agentic/guides/modules/auth/di-service-tokens.md +2 -1
  16. package/dist/agentic/guides/modules/auth/exact-override-targets.md +2 -1
  17. package/dist/agentic/guides/modules/auth/index.md +2 -2
  18. package/dist/agentic/guides/modules/availability/index.md +1 -1
  19. package/dist/agentic/guides/modules/backups/index.md +1 -1
  20. package/dist/agentic/guides/modules/business_rules/index.md +1 -1
  21. package/dist/agentic/guides/modules/carrier_inpost/index.md +1 -1
  22. package/dist/agentic/guides/modules/cart/index.md +1 -1
  23. package/dist/agentic/guides/modules/catalog/active-extension-bindings.md +1 -1
  24. package/dist/agentic/guides/modules/catalog/exact-override-targets.md +4 -4
  25. package/dist/agentic/guides/modules/catalog/index.md +2 -2
  26. package/dist/agentic/guides/modules/catalog/umes-hosts.md +3 -1
  27. package/dist/agentic/guides/modules/catalog/workers.md +4 -4
  28. package/dist/agentic/guides/modules/channel_apns/index.md +1 -1
  29. package/dist/agentic/guides/modules/channel_discord/index.md +1 -1
  30. package/dist/agentic/guides/modules/channel_expo/index.md +1 -1
  31. package/dist/agentic/guides/modules/channel_fcm/index.md +1 -1
  32. package/dist/agentic/guides/modules/channel_gmail/index.md +1 -1
  33. package/dist/agentic/guides/modules/channel_imap/index.md +1 -1
  34. package/dist/agentic/guides/modules/channel_resend/index.md +1 -1
  35. package/dist/agentic/guides/modules/channel_ses/index.md +1 -1
  36. package/dist/agentic/guides/modules/channel_smtp/index.md +1 -1
  37. package/dist/agentic/guides/modules/checkout/index.md +1 -1
  38. package/dist/agentic/guides/modules/communication_channels/encryption.md +2 -1
  39. package/dist/agentic/guides/modules/communication_channels/entities.md +1 -0
  40. package/dist/agentic/guides/modules/communication_channels/events.md +2 -1
  41. package/dist/agentic/guides/modules/communication_channels/exact-override-targets.md +4 -2
  42. package/dist/agentic/guides/modules/communication_channels/index.md +3 -3
  43. package/dist/agentic/guides/modules/communication_channels/umes-hosts.md +2 -0
  44. package/dist/agentic/guides/modules/communication_channels/workers.md +2 -1
  45. package/dist/agentic/guides/modules/configs/entities.md +0 -1
  46. package/dist/agentic/guides/modules/configs/index.md +1 -1
  47. package/dist/agentic/guides/modules/configs/umes-hosts.md +1 -1
  48. package/dist/agentic/guides/modules/content/index.md +1 -1
  49. package/dist/agentic/guides/modules/currencies/index.md +1 -1
  50. package/dist/agentic/guides/modules/currencies/umes-hosts.md +1 -0
  51. package/dist/agentic/guides/modules/customer_accounts/active-extension-bindings.md +3 -3
  52. package/dist/agentic/guides/modules/customer_accounts/index.md +1 -1
  53. package/dist/agentic/guides/modules/customer_accounts/umes-hosts.md +1 -1
  54. package/dist/agentic/guides/modules/customer_groups/active-extension-bindings.md +1 -1
  55. package/dist/agentic/guides/modules/customer_groups/index.md +1 -1
  56. package/dist/agentic/guides/modules/customer_groups/umes-hosts.md +1 -1
  57. package/dist/agentic/guides/modules/customers/acl-features.md +2 -1
  58. package/dist/agentic/guides/modules/customers/active-extension-bindings.md +12 -12
  59. package/dist/agentic/guides/modules/customers/backend-pages.md +1 -0
  60. package/dist/agentic/guides/modules/customers/domain-commands.md +1 -1
  61. package/dist/agentic/guides/modules/customers/exact-override-targets.md +2 -0
  62. package/dist/agentic/guides/modules/customers/index.md +1 -1
  63. package/dist/agentic/guides/modules/dashboards/active-extension-bindings.md +3 -3
  64. package/dist/agentic/guides/modules/dashboards/index.md +1 -1
  65. package/dist/agentic/guides/modules/data_sync/active-extension-bindings.md +3 -3
  66. package/dist/agentic/guides/modules/data_sync/index.md +1 -1
  67. package/dist/agentic/guides/modules/design_system/index.md +1 -1
  68. package/dist/agentic/guides/modules/devices/active-extension-bindings.md +1 -1
  69. package/dist/agentic/guides/modules/devices/index.md +1 -1
  70. package/dist/agentic/guides/modules/devices/owned-contract-module-metadata.md +1 -1
  71. package/dist/agentic/guides/modules/dictionaries/active-extension-bindings.md +2 -2
  72. package/dist/agentic/guides/modules/dictionaries/index.md +1 -1
  73. package/dist/agentic/guides/modules/directory/active-extension-bindings.md +1 -1
  74. package/dist/agentic/guides/modules/directory/index.md +1 -1
  75. package/dist/agentic/guides/modules/documents/index.md +1 -1
  76. package/dist/agentic/guides/modules/ecommerce/index.md +1 -1
  77. package/dist/agentic/guides/modules/ecommerce/owned-contract-module-metadata.md +1 -1
  78. package/dist/agentic/guides/modules/entities/active-extension-bindings.md +1 -1
  79. package/dist/agentic/guides/modules/entities/index.md +1 -1
  80. package/dist/agentic/guides/modules/eudr/index.md +1 -1
  81. package/dist/agentic/guides/modules/events/index.md +1 -1
  82. package/dist/agentic/guides/modules/feature_toggles/active-extension-bindings.md +1 -1
  83. package/dist/agentic/guides/modules/feature_toggles/index.md +1 -1
  84. package/dist/agentic/guides/modules/forms/index.md +1 -1
  85. package/dist/agentic/guides/modules/gateway_autopay/index.md +1 -1
  86. package/dist/agentic/guides/modules/gateway_stripe/index.md +1 -1
  87. package/dist/agentic/guides/modules/gateway_tpay/index.md +1 -1
  88. package/dist/agentic/guides/modules/generators/index.md +1 -1
  89. package/dist/agentic/guides/modules/inbox_ops/active-extension-bindings.md +3 -3
  90. package/dist/agentic/guides/modules/inbox_ops/index.md +1 -1
  91. package/dist/agentic/guides/modules/integrations/index.md +1 -1
  92. package/dist/agentic/guides/modules/ledger/index.md +2 -2
  93. package/dist/agentic/guides/modules/ledger/umes-hosts.md +1 -0
  94. package/dist/agentic/guides/modules/maintenance/contribution-resolutions.md +11 -0
  95. package/dist/agentic/guides/modules/maintenance/events.md +14 -0
  96. package/dist/agentic/guides/modules/maintenance/exact-override-targets.md +11 -0
  97. package/dist/agentic/guides/modules/maintenance/index.md +17 -0
  98. package/dist/agentic/guides/modules/maintenance/owned-contract-module-metadata.md +11 -0
  99. package/dist/agentic/guides/modules/maintenance/umes-contributions.md +11 -0
  100. package/dist/agentic/guides/modules/maintenance/umes-hosts.md +14 -0
  101. package/dist/agentic/guides/modules/messages/index.md +1 -1
  102. package/dist/agentic/guides/modules/notifications/index.md +1 -1
  103. package/dist/agentic/guides/modules/onboarding/index.md +1 -1
  104. package/dist/agentic/guides/modules/payment_gateways/active-extension-bindings.md +1 -1
  105. package/dist/agentic/guides/modules/payment_gateways/exact-override-targets.md +3 -3
  106. package/dist/agentic/guides/modules/payment_gateways/index.md +1 -1
  107. package/dist/agentic/guides/modules/payment_gateways/setup.md +1 -1
  108. package/dist/agentic/guides/modules/perspectives/active-extension-bindings.md +2 -2
  109. package/dist/agentic/guides/modules/perspectives/index.md +1 -1
  110. package/dist/agentic/guides/modules/phone_calls/index.md +1 -1
  111. package/dist/agentic/guides/modules/phone_calls/owned-contract-module-metadata.md +1 -1
  112. package/dist/agentic/guides/modules/planner/active-extension-bindings.md +1 -1
  113. package/dist/agentic/guides/modules/planner/index.md +1 -1
  114. package/dist/agentic/guides/modules/portal/index.md +1 -1
  115. package/dist/agentic/guides/modules/progress/index.md +1 -1
  116. package/dist/agentic/guides/modules/promotions/domain-commands.md +1 -0
  117. package/dist/agentic/guides/modules/promotions/index.md +2 -2
  118. package/dist/agentic/guides/modules/promotions/umes-hosts.md +2 -0
  119. package/dist/agentic/guides/modules/push_notifications/index.md +2 -2
  120. package/dist/agentic/guides/modules/push_notifications/umes-hosts.md +1 -0
  121. package/dist/agentic/guides/modules/query_index/active-extension-bindings.md +1 -1
  122. package/dist/agentic/guides/modules/query_index/index.md +1 -1
  123. package/dist/agentic/guides/modules/record_locks/index.md +1 -1
  124. package/dist/agentic/guides/modules/resources/active-extension-bindings.md +1 -1
  125. package/dist/agentic/guides/modules/resources/index.md +1 -1
  126. package/dist/agentic/guides/modules/sales/active-extension-bindings.md +1 -1
  127. package/dist/agentic/guides/modules/sales/index.md +1 -1
  128. package/dist/agentic/guides/modules/sales/umes-hosts.md +1 -0
  129. package/dist/agentic/guides/modules/sales_invoices_gl_posting/acl-features.md +12 -0
  130. package/dist/agentic/guides/modules/sales_invoices_gl_posting/entities.md +12 -0
  131. package/dist/agentic/guides/modules/sales_invoices_gl_posting/exact-override-targets.md +13 -0
  132. package/dist/agentic/guides/modules/sales_invoices_gl_posting/index.md +17 -0
  133. package/dist/agentic/guides/modules/sales_invoices_gl_posting/owned-contract-module-metadata.md +11 -0
  134. package/dist/agentic/guides/modules/sales_invoices_gl_posting/setup.md +11 -0
  135. package/dist/agentic/guides/modules/sales_invoices_gl_posting/umes-hosts.md +12 -0
  136. package/dist/agentic/guides/modules/scheduler/index.md +1 -1
  137. package/dist/agentic/guides/modules/search/index.md +1 -1
  138. package/dist/agentic/guides/modules/security/index.md +1 -1
  139. package/dist/agentic/guides/modules/seeds/index.md +1 -1
  140. package/dist/agentic/guides/modules/shipping_carriers/active-extension-bindings.md +1 -1
  141. package/dist/agentic/guides/modules/shipping_carriers/index.md +1 -1
  142. package/dist/agentic/guides/modules/sso/index.md +1 -1
  143. package/dist/agentic/guides/modules/staff/index.md +1 -1
  144. package/dist/agentic/guides/modules/storage_s3/index.md +1 -1
  145. package/dist/agentic/guides/modules/sync_akeneo/index.md +1 -1
  146. package/dist/agentic/guides/modules/sync_excel/index.md +1 -1
  147. package/dist/agentic/guides/modules/sync_excel/umes-hosts.md +1 -0
  148. package/dist/agentic/guides/modules/system_status_overlays/index.md +1 -1
  149. package/dist/agentic/guides/modules/telemetry/index.md +1 -1
  150. package/dist/agentic/guides/modules/tillio/index.md +1 -1
  151. package/dist/agentic/guides/modules/translations/active-extension-bindings.md +2 -2
  152. package/dist/agentic/guides/modules/translations/index.md +1 -1
  153. package/dist/agentic/guides/modules/upgrade_actions/cli-commands.md +11 -0
  154. package/dist/agentic/guides/modules/upgrade_actions/entities.md +11 -0
  155. package/dist/agentic/guides/modules/upgrade_actions/exact-override-targets.md +1 -0
  156. package/dist/agentic/guides/modules/upgrade_actions/index.md +5 -2
  157. package/dist/agentic/guides/modules/upgrade_actions/umes-hosts.md +11 -0
  158. package/dist/agentic/guides/modules/warranty_claims/index.md +1 -1
  159. package/dist/agentic/guides/modules/webhooks/index.md +1 -1
  160. package/dist/agentic/guides/modules/wms/active-extension-bindings.md +1 -1
  161. package/dist/agentic/guides/modules/wms/exact-override-targets.md +2 -2
  162. package/dist/agentic/guides/modules/wms/index.md +1 -1
  163. package/dist/agentic/guides/modules/wms/setup.md +1 -1
  164. package/dist/agentic/guides/modules/workflows/domain-commands.md +3 -0
  165. package/dist/agentic/guides/modules/workflows/index.md +1 -1
  166. package/dist/agentic/guides/modules/workflows/umes-hosts.md +2 -0
  167. package/dist/agentic/guides/production-deployment.md +6 -0
  168. package/dist/agentic/guides/reference-module-facts.json +12 -13
  169. package/dist/agentic/guides/reference-modules/example/index.md +1 -1
  170. package/dist/agentic/guides/reference-modules/example/umes-hosts.md +1 -1
  171. package/dist/agentic/guides/upstream/AGENTS.md +2 -3
  172. package/dist/agentic/guides/upstream/BACKWARD_COMPATIBILITY.md +18 -9
  173. package/dist/agentic/guides/upstream/manifest.json +3 -3
  174. package/dist/agentic/shared/AGENTS.md.template +1 -1
  175. package/dist/agentic/shared/ai/harness/cases.json +7 -7
  176. package/dist/lib/generators/module-extension-facts.js +5 -6
  177. package/dist/lib/generators/module-extension-facts.js.map +2 -2
  178. package/dist/lib/generators/module-registry.js +28 -0
  179. package/dist/lib/generators/module-registry.js.map +2 -2
  180. package/dist/lib/testing/ephemeral-database-guard.js +89 -0
  181. package/dist/lib/testing/ephemeral-database-guard.js.map +7 -0
  182. package/dist/lib/testing/integration.js +4 -0
  183. package/dist/lib/testing/integration.js.map +2 -2
  184. package/dist/mercato.js +26 -3
  185. package/dist/mercato.js.map +2 -2
  186. package/package.json +7 -7
  187. package/src/__tests__/init-ephemeral-database.test.ts +112 -0
  188. package/src/__tests__/mercato.test.ts +15 -0
  189. package/src/lib/__tests__/modules-config.test.ts +1 -0
  190. package/src/lib/generators/__tests__/module-extension-facts.test.ts +18 -0
  191. package/src/lib/generators/__tests__/module-facts.customers.fixture.test.ts +2 -1
  192. package/src/lib/generators/__tests__/module-registry-requires-enforcement.test.ts +18 -0
  193. package/src/lib/generators/__tests__/registry-variant-parity.test.ts +95 -19
  194. package/src/lib/generators/module-extension-facts.ts +5 -10
  195. package/src/lib/generators/module-registry.ts +44 -0
  196. package/src/lib/testing/__tests__/ephemeral-database-guard.test.ts +88 -0
  197. package/src/lib/testing/ephemeral-database-guard.ts +105 -0
  198. package/src/lib/testing/integration.ts +4 -0
  199. package/src/mercato.ts +26 -3
@@ -0,0 +1,88 @@
1
+ import { createHash } from 'node:crypto'
2
+ import { chmodSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
3
+ import os from 'node:os'
4
+ import path from 'node:path'
5
+ import {
6
+ assertEphemeralDatabaseConnection,
7
+ assertEphemeralDatabaseOwnership,
8
+ readEphemeralDatabaseGuard,
9
+ type EphemeralDatabaseGuard,
10
+ } from '../ephemeral-database-guard'
11
+
12
+ describe('ephemeral database initialization ownership', () => {
13
+ const databaseUrl = 'postgresql://owned:private-password@127.0.0.1:6543/owned_database'
14
+ const guard: EphemeralDatabaseGuard = {
15
+ version: 1,
16
+ databaseName: 'owned_database',
17
+ connectionDigest: createHash('sha256').update('postgresql://owned@127.0.0.1:6543/owned_database').digest('hex'),
18
+ ownershipMarker: 'open-mercato-ephemeral:38ee43df-19e4-444e-b5c5-8e56b55c9d0c',
19
+ }
20
+ let directory: string
21
+ let proofPath: string
22
+
23
+ beforeEach(() => {
24
+ directory = mkdtempSync(path.join(os.tmpdir(), 'ephemeral-db-guard-'))
25
+ proofPath = path.join(directory, 'guard.json')
26
+ writeFileSync(proofPath, JSON.stringify(guard), { mode: 0o600 })
27
+ })
28
+
29
+ afterEach(() => {
30
+ rmSync(directory, { recursive: true, force: true })
31
+ })
32
+
33
+ it('keeps ordinary init compatible and reads the private argument proof', () => {
34
+ expect(readEphemeralDatabaseGuard(['--reinstall'])).toBeNull()
35
+ expect(readEphemeralDatabaseGuard([`--ephemeral-database-guard=${proofPath}`])).toEqual(guard)
36
+ })
37
+
38
+ it('rejects missing, duplicate, malformed and public proofs', () => {
39
+ expect(() => readEphemeralDatabaseGuard(['--ephemeral-database-guard'])).toThrow('ownership proof file')
40
+ expect(() => readEphemeralDatabaseGuard([`--ephemeral-database-guard=${proofPath}`, `--ephemeral-database-guard=${proofPath}`])).toThrow('ownership proof file')
41
+ writeFileSync(proofPath, JSON.stringify({ ...guard, ownershipMarker: '' }))
42
+ expect(() => readEphemeralDatabaseGuard([`--ephemeral-database-guard=${proofPath}`])).toThrow('proof is invalid')
43
+ if (process.platform !== 'win32') {
44
+ chmodSync(proofPath, 0o644)
45
+ expect(() => readEphemeralDatabaseGuard([`--ephemeral-database-guard=${proofPath}`])).toThrow('private regular file')
46
+ }
47
+ })
48
+
49
+ it('does not follow a replacement symlink', () => {
50
+ const alias = path.join(directory, 'alias.json')
51
+ symlinkSync(proofPath, alias)
52
+ expect(() => readEphemeralDatabaseGuard([`--ephemeral-database-guard=${alias}`])).toThrow('private regular file')
53
+ })
54
+
55
+ it('rejects stripped or dotenv fallback destinations without exposing credentials', () => {
56
+ expect(() => assertEphemeralDatabaseConnection(guard, undefined)).toThrow('explicit database connection')
57
+ expect(() => assertEphemeralDatabaseConnection(guard, 'postgresql://fallback:other-secret@127.0.0.1:6544/control_database')).toThrow('not the owned disposable target')
58
+ expect(() => assertEphemeralDatabaseConnection(guard, databaseUrl)).not.toThrow()
59
+ try {
60
+ assertEphemeralDatabaseConnection(guard, databaseUrl.replace('owned_database', 'wrong_database'))
61
+ } catch (error) {
62
+ expect(String(error)).not.toContain('private-password')
63
+ expect(String(error)).not.toContain('wrong_database')
64
+ }
65
+ })
66
+
67
+ it('binds the owned target identity rather than the credential', () => {
68
+ expect(() => assertEphemeralDatabaseConnection(guard, 'postgresql://owned:rotated-secret@127.0.0.1:6543/owned_database')).not.toThrow()
69
+ expect(() => assertEphemeralDatabaseConnection(guard, 'postgresql://other_role:private-password@127.0.0.1:6543/owned_database')).toThrow('not the owned disposable target')
70
+ })
71
+
72
+ it('reads the connected database identity and ownership marker before accepting it', async () => {
73
+ const query = jest.fn().mockResolvedValue({ rows: [{ database_name: guard.databaseName, ownership_marker: guard.ownershipMarker }] })
74
+ await expect(assertEphemeralDatabaseOwnership(guard, { query })).resolves.toBeUndefined()
75
+ expect(query).toHaveBeenCalledTimes(1)
76
+ expect(query.mock.calls[0][0]).toMatch(/^SELECT current_database\(\)/)
77
+ })
78
+
79
+ it.each([
80
+ { database_name: 'control_database', ownership_marker: guard.ownershipMarker },
81
+ { database_name: guard.databaseName, ownership_marker: null },
82
+ { database_name: guard.databaseName, ownership_marker: 'another-owner' },
83
+ ])('refuses a connected database without matching positive ownership: %j', async (identity) => {
84
+ const query = jest.fn().mockResolvedValue({ rows: [identity] })
85
+ await expect(assertEphemeralDatabaseOwnership(guard, { query })).rejects.toThrow('ownership could not be verified')
86
+ expect(query.mock.calls.every(([statement]) => String(statement).startsWith('SELECT'))).toBe(true)
87
+ })
88
+ })
@@ -0,0 +1,105 @@
1
+ import { createHash, randomUUID } from 'node:crypto'
2
+ import { lstatSync, readFileSync } from 'node:fs'
3
+ import { mkdir, writeFile } from 'node:fs/promises'
4
+ import path from 'node:path'
5
+
6
+ type DatabaseIdentity = { database_name: unknown; ownership_marker: unknown }
7
+
8
+ export type EphemeralDatabaseConnection = {
9
+ query: (statement: string) => Promise<{ rows: DatabaseIdentity[] }>
10
+ }
11
+
12
+ export type EphemeralDatabaseGuard = {
13
+ version: 1
14
+ databaseName: string
15
+ connectionDigest: string
16
+ ownershipMarker: string
17
+ }
18
+
19
+ const identityQuery = "SELECT current_database() AS database_name, shobj_description(oid, 'pg_database') AS ownership_marker FROM pg_database WHERE datname = current_database()"
20
+
21
+ function connectionTarget(databaseUrl: string | undefined): string {
22
+ if (!databaseUrl) throw new Error('[internal] Ephemeral initialization requires an explicit database connection.')
23
+ let parsed: URL
24
+ try {
25
+ parsed = new URL(databaseUrl)
26
+ } catch {
27
+ throw new Error('[internal] Ephemeral database connection is invalid.')
28
+ }
29
+ if (!['postgres:', 'postgresql:'].includes(parsed.protocol) || !parsed.hostname || parsed.pathname.length < 2) {
30
+ throw new Error('[internal] Ephemeral database connection is invalid.')
31
+ }
32
+ const databaseName = decodeURIComponent(parsed.pathname.slice(1))
33
+ const port = parsed.port || '5432'
34
+ return `postgresql://${decodeURIComponent(parsed.username)}@${parsed.hostname.toLowerCase()}:${port}/${databaseName}`
35
+ }
36
+
37
+ function connectionDigest(databaseUrl: string | undefined): string {
38
+ return createHash('sha256').update(connectionTarget(databaseUrl)).digest('hex')
39
+ }
40
+
41
+ export function readEphemeralDatabaseGuard(args: string[]): EphemeralDatabaseGuard | null {
42
+ const prefix = '--ephemeral-database-guard='
43
+ const values = args.filter((argument) => argument === '--ephemeral-database-guard' || argument.startsWith(prefix))
44
+ if (values.length === 0) return null
45
+ if (values.length !== 1 || !values[0].startsWith(prefix) || !values[0].slice(prefix.length)) {
46
+ throw new Error('[internal] Ephemeral initialization requires one database ownership proof file.')
47
+ }
48
+ const proofPath = values[0].slice(prefix.length)
49
+ const metadata = lstatSync(proofPath)
50
+ if (!metadata.isFile() || metadata.isSymbolicLink() || metadata.size > 4096
51
+ || (process.platform !== 'win32' && ((metadata.mode & 0o077) !== 0 || metadata.uid !== process.getuid?.()))) {
52
+ throw new Error('[internal] Ephemeral database ownership proof must be a private regular file.')
53
+ }
54
+ const value: unknown = JSON.parse(readFileSync(proofPath, 'utf8'))
55
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
56
+ throw new Error('[internal] Ephemeral database ownership proof is invalid.')
57
+ }
58
+ const record = value as Record<string, unknown>
59
+ if (record.version !== 1 || typeof record.databaseName !== 'string' || !record.databaseName
60
+ || typeof record.connectionDigest !== 'string' || !/^[a-f0-9]{64}$/.test(record.connectionDigest)
61
+ || typeof record.ownershipMarker !== 'string' || !/^open-mercato-ephemeral:[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/.test(record.ownershipMarker)) {
62
+ throw new Error('[internal] Ephemeral database ownership proof is invalid.')
63
+ }
64
+ return { version: 1, databaseName: record.databaseName, connectionDigest: record.connectionDigest, ownershipMarker: record.ownershipMarker }
65
+ }
66
+
67
+ export function assertEphemeralDatabaseConnection(guard: EphemeralDatabaseGuard, databaseUrl: string | undefined): void {
68
+ if (connectionDigest(databaseUrl) !== guard.connectionDigest) {
69
+ throw new Error('[internal] Refusing initialization: the effective database connection is not the owned disposable target.')
70
+ }
71
+ }
72
+
73
+ export async function assertEphemeralDatabaseOwnership(guard: EphemeralDatabaseGuard, client: EphemeralDatabaseConnection): Promise<void> {
74
+ const identity = (await client.query(identityQuery)).rows[0]
75
+ if (identity?.database_name !== guard.databaseName || identity?.ownership_marker !== guard.ownershipMarker) {
76
+ throw new Error('[internal] Refusing initialization: database ownership could not be verified.')
77
+ }
78
+ }
79
+
80
+ export async function createEphemeralDatabaseGuard(databaseUrl: string, databaseName: string, directory: string): Promise<string> {
81
+ const digest = connectionDigest(databaseUrl)
82
+ if (decodeURIComponent(new URL(databaseUrl).pathname.slice(1)) !== databaseName) {
83
+ throw new Error('[internal] Disposable database identity does not match its allocation.')
84
+ }
85
+ const { Client } = await import('pg')
86
+ const client = new Client({ connectionString: databaseUrl, connectionTimeoutMillis: 5000 })
87
+ const ownershipMarker = `open-mercato-ephemeral:${randomUUID()}`
88
+ try {
89
+ await client.connect()
90
+ const identity = (await client.query<DatabaseIdentity>(identityQuery)).rows[0]
91
+ if (identity?.database_name !== databaseName) {
92
+ throw new Error('[internal] Connected database identity does not match its allocation.')
93
+ }
94
+ const quotedName = `"${databaseName.replace(/"/g, '""')}"`
95
+ await client.query(`COMMENT ON DATABASE ${quotedName} IS '${ownershipMarker}'`)
96
+ const guard: EphemeralDatabaseGuard = { version: 1, databaseName, connectionDigest: digest, ownershipMarker }
97
+ await assertEphemeralDatabaseOwnership(guard, client)
98
+ await mkdir(directory, { recursive: true, mode: 0o700 })
99
+ const proofPath = path.join(directory, `${randomUUID()}.json`)
100
+ await writeFile(proofPath, JSON.stringify(guard), { mode: 0o600, flag: 'wx' })
101
+ return proofPath
102
+ } finally {
103
+ await client.end()
104
+ }
105
+ }
@@ -2280,6 +2280,8 @@ function buildReusableEnvironment(
2280
2280
  OM_TEST_EMAIL_CAPTURE_ACCESS_TOKEN: TEST_EMAIL_CAPTURE_ACCESS_TOKEN,
2281
2281
  OM_TEST_EMAIL_CAPTURE_CORRELATION_TOKEN: TEST_EMAIL_CAPTURE_CORRELATION_TOKEN,
2282
2282
  SYSTEM_EMAIL_PROVIDER: '__test_seed__',
2283
+ // Queued system email specs wait on real retries; keep the backoff short.
2284
+ OM_SYSTEM_EMAIL_RETRY_BASE_MS: '500',
2283
2285
  EMAIL_FROM: process.env.EMAIL_FROM ?? 'system@test-seed.local',
2284
2286
  NOTIFICATIONS_EMAIL_FROM: process.env.NOTIFICATIONS_EMAIL_FROM ?? 'notifications@test-seed.local',
2285
2287
  ADMIN_EMAIL: process.env.ADMIN_EMAIL ?? 'admin@test-seed.local',
@@ -3903,6 +3905,8 @@ export async function startEphemeralEnvironment(options: EphemeralRuntimeOptions
3903
3905
  OM_TEST_EMAIL_CAPTURE_ACCESS_TOKEN: TEST_EMAIL_CAPTURE_ACCESS_TOKEN,
3904
3906
  OM_TEST_EMAIL_CAPTURE_CORRELATION_TOKEN: TEST_EMAIL_CAPTURE_CORRELATION_TOKEN,
3905
3907
  SYSTEM_EMAIL_PROVIDER: '__test_seed__',
3908
+ // Queued system email specs wait on real retries; keep the backoff short.
3909
+ OM_SYSTEM_EMAIL_RETRY_BASE_MS: '500',
3906
3910
  EMAIL_FROM: process.env.EMAIL_FROM ?? 'system@test-seed.local',
3907
3911
  NOTIFICATIONS_EMAIL_FROM: process.env.NOTIFICATIONS_EMAIL_FROM ?? 'notifications@test-seed.local',
3908
3912
  ADMIN_EMAIL: process.env.ADMIN_EMAIL ?? 'admin@test-seed.local',
package/src/mercato.ts CHANGED
@@ -12,6 +12,7 @@ import { getRedisUrl, getRedisUrlOrThrow, parseRedisUrl } from '@open-mercato/sh
12
12
  import { formatInitSummary } from './lib/init-summary'
13
13
  import { resolveInitDerivedSecrets, resolveInitSuperadminCredentials } from './lib/init-secrets'
14
14
  import { decideExistingDatabase, formatSkipIfInitializedMessage } from './lib/init-existing-database'
15
+ import { assertEphemeralDatabaseConnection, assertEphemeralDatabaseOwnership, readEphemeralDatabaseGuard } from './lib/testing/ephemeral-database-guard'
15
16
  import {
16
17
  resolveAutoSpawnWorkersMode,
17
18
  resolveLazySpawnMode,
@@ -1082,6 +1083,18 @@ export async function run(argv = process.argv) {
1082
1083
  try {
1083
1084
  const initArgs = parts.slice(1).filter(Boolean)
1084
1085
  const reinstall = initArgs.includes('--reinstall') || initArgs.includes('-r')
1086
+ const ephemeralDatabaseGuard = readEphemeralDatabaseGuard(initArgs)
1087
+ if (ephemeralDatabaseGuard) {
1088
+ assertEphemeralDatabaseConnection(ephemeralDatabaseGuard, process.env.DATABASE_URL)
1089
+ const { Client } = await import('pg')
1090
+ const verificationClient = new Client({ connectionString: process.env.DATABASE_URL, ssl: getSslConfig(), connectionTimeoutMillis: 5000 })
1091
+ try {
1092
+ await verificationClient.connect()
1093
+ await assertEphemeralDatabaseOwnership(ephemeralDatabaseGuard, verificationClient)
1094
+ } finally {
1095
+ await verificationClient.end()
1096
+ }
1097
+ }
1085
1098
  process.env.OM_INIT_FLOW = 'true'
1086
1099
  if (reinstall) {
1087
1100
  process.env.OM_INIT_REINSTALL = 'true'
@@ -1151,11 +1164,15 @@ export async function run(argv = process.argv) {
1151
1164
  console.error('DATABASE_URL is not set. Aborting reinstall.')
1152
1165
  return 1
1153
1166
  }
1154
- const dbExists = await ensureDatabaseExists(dbUrl)
1167
+ const dbExists = ephemeralDatabaseGuard ? true : await ensureDatabaseExists(dbUrl)
1155
1168
  if (!dbExists) return 1
1156
1169
  const client = new Client({ connectionString: dbUrl, ssl: getSslConfig() })
1157
1170
  try {
1158
1171
  await client.connect()
1172
+ if (ephemeralDatabaseGuard) {
1173
+ assertEphemeralDatabaseConnection(ephemeralDatabaseGuard, dbUrl)
1174
+ await assertEphemeralDatabaseOwnership(ephemeralDatabaseGuard, client)
1175
+ }
1159
1176
  // Collect all user tables in the configured schema (uses search_path from DATABASE_URL)
1160
1177
  const res = await client.query(`SELECT tablename FROM pg_tables WHERE schemaname = current_schema()`)
1161
1178
  const dropTargets = new Set<string>((res.rows || []).map((r: any) => String(r.tablename)))
@@ -1193,7 +1210,7 @@ export async function run(argv = process.argv) {
1193
1210
  // a stray ioredis client with auto-reconnect would otherwise spam
1194
1211
  // ETIMEDOUT errors for the rest of the process lifetime.
1195
1212
  const redisUrl = getRedisUrl()
1196
- if (redisUrl) {
1213
+ if (redisUrl && !ephemeralDatabaseGuard) {
1197
1214
  const Redis = (await import('ioredis')).default
1198
1215
  const redis = new Redis({
1199
1216
  ...parseRedisUrl(redisUrl),
@@ -1214,6 +1231,8 @@ export async function run(argv = process.argv) {
1214
1231
  } finally {
1215
1232
  try { redis.disconnect() } catch {}
1216
1233
  }
1234
+ } else if (ephemeralDatabaseGuard) {
1235
+ console.log('[internal] Redis flush skipped: ephemeral database initialization does not own Redis.')
1217
1236
  } else {
1218
1237
  console.log(' Redis flush skipped (REDIS_URL not configured).')
1219
1238
  }
@@ -1229,7 +1248,7 @@ export async function run(argv = process.argv) {
1229
1248
  }
1230
1249
 
1231
1250
  const { Client } = await import('pg')
1232
- const dbExists = await ensureDatabaseExists(dbUrl)
1251
+ const dbExists = ephemeralDatabaseGuard ? true : await ensureDatabaseExists(dbUrl)
1233
1252
  if (!dbExists) return 1
1234
1253
  const client = new Client({ connectionString: dbUrl, ssl: getSslConfig() })
1235
1254
  try {
@@ -2900,6 +2919,10 @@ export async function run(argv = process.argv) {
2900
2919
 
2901
2920
  const mod = all.find((m) => m.id === modName)
2902
2921
  if (!mod) {
2922
+ if (modName === 'maintenance') {
2923
+ console.error('Maintenance mode requires the Enterprise maintenance module. Set OM_ENABLE_ENTERPRISE_MODULES=true and regenerate the app after enabling Enterprise.')
2924
+ return 1
2925
+ }
2903
2926
  console.error(`❌ Module not found: "${modName}"`)
2904
2927
  return 1
2905
2928
  }