pgserve 2.1.2 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (227) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/README.md +105 -1
  3. package/bin/autopg-wrapper.cjs +16 -0
  4. package/bin/pgserve-wrapper.cjs +31 -6
  5. package/bin/postgres-server.js +80 -7
  6. package/console/README.md +131 -0
  7. package/console/api.js +173 -0
  8. package/console/app.jsx +483 -0
  9. package/console/colors_and_type.css +227 -0
  10. package/console/components.jsx +167 -0
  11. package/console/console.css +1666 -0
  12. package/console/data.jsx +350 -0
  13. package/console/index.html +31 -0
  14. package/console/screens/databases.jsx +5 -0
  15. package/console/screens/health.jsx +5 -0
  16. package/console/screens/ingress.jsx +5 -0
  17. package/console/screens/optimizer.jsx +5 -0
  18. package/console/screens/rlm-sim.jsx +5 -0
  19. package/console/screens/rlm-trace.jsx +5 -0
  20. package/console/screens/security.jsx +5 -0
  21. package/console/screens/settings.jsx +611 -0
  22. package/console/screens/sql.jsx +5 -0
  23. package/console/screens/sync.jsx +5 -0
  24. package/console/screens/tables.jsx +5 -0
  25. package/console/tweaks-panel.jsx +425 -0
  26. package/package.json +11 -1
  27. package/src/cli-config.cjs +310 -0
  28. package/src/cli-install.cjs +98 -11
  29. package/src/cli-restart.cjs +228 -0
  30. package/src/cli-ui.cjs +580 -0
  31. package/src/cluster.js +43 -38
  32. package/src/postgres.js +141 -19
  33. package/src/settings-loader.cjs +235 -0
  34. package/src/settings-migrate.cjs +212 -0
  35. package/src/settings-pg-args.cjs +146 -0
  36. package/src/settings-schema.cjs +422 -0
  37. package/src/settings-validator.cjs +416 -0
  38. package/src/settings-writer.cjs +288 -0
  39. package/.claude/context/windows-debug.md +0 -119
  40. package/.genie/AGENTS.md +0 -15
  41. package/.genie/agents/README.md +0 -110
  42. package/.genie/agents/analyze.md +0 -176
  43. package/.genie/agents/forge.md +0 -290
  44. package/.genie/agents/garbage-cleaner.md +0 -324
  45. package/.genie/agents/garbage-collector.md +0 -596
  46. package/.genie/agents/github-issue-gc.md +0 -618
  47. package/.genie/agents/review.md +0 -380
  48. package/.genie/agents/semantic-analyzer/find-duplicates.md +0 -90
  49. package/.genie/agents/semantic-analyzer/find-orphans.md +0 -99
  50. package/.genie/agents/semantic-analyzer.md +0 -101
  51. package/.genie/agents/update.md +0 -182
  52. package/.genie/agents/wish.md +0 -357
  53. package/.genie/brainstorms/pgserve-v2/DESIGN.md +0 -174
  54. package/.genie/code/AGENTS.md +0 -694
  55. package/.genie/code/agents/audit/risk.md +0 -173
  56. package/.genie/code/agents/audit/security.md +0 -189
  57. package/.genie/code/agents/audit.md +0 -145
  58. package/.genie/code/agents/challenge.md +0 -230
  59. package/.genie/code/agents/change-reviewer.md +0 -295
  60. package/.genie/code/agents/code-garbage-collector.md +0 -425
  61. package/.genie/code/agents/code-quality.md +0 -410
  62. package/.genie/code/agents/commit-suggester.md +0 -255
  63. package/.genie/code/agents/commit.md +0 -124
  64. package/.genie/code/agents/consensus.md +0 -204
  65. package/.genie/code/agents/daily-standup.md +0 -722
  66. package/.genie/code/agents/docgen.md +0 -48
  67. package/.genie/code/agents/explore.md +0 -79
  68. package/.genie/code/agents/fix.md +0 -100
  69. package/.genie/code/agents/git/commit-advisory.md +0 -219
  70. package/.genie/code/agents/git/workflows/issue.md +0 -244
  71. package/.genie/code/agents/git/workflows/pr.md +0 -179
  72. package/.genie/code/agents/git/workflows/release.md +0 -460
  73. package/.genie/code/agents/git/workflows/report.md +0 -342
  74. package/.genie/code/agents/git.md +0 -432
  75. package/.genie/code/agents/implementor.md +0 -161
  76. package/.genie/code/agents/install.md +0 -515
  77. package/.genie/code/agents/issue-creator.md +0 -344
  78. package/.genie/code/agents/polish.md +0 -116
  79. package/.genie/code/agents/qa.md +0 -653
  80. package/.genie/code/agents/refactor.md +0 -294
  81. package/.genie/code/agents/release.md +0 -1129
  82. package/.genie/code/agents/roadmap.md +0 -885
  83. package/.genie/code/agents/tests.md +0 -557
  84. package/.genie/code/agents/tracer.md +0 -50
  85. package/.genie/code/agents/update/upstream-update.md +0 -85
  86. package/.genie/code/agents/update/versions/generic-update.md +0 -305
  87. package/.genie/code/agents/vibe.md +0 -1317
  88. package/.genie/code/spells/agent-configuration.md +0 -58
  89. package/.genie/code/spells/automated-rc-publishing.md +0 -106
  90. package/.genie/code/spells/branch-tracker-guidance.md +0 -28
  91. package/.genie/code/spells/debug.md +0 -320
  92. package/.genie/code/spells/emoji-naming-convention.md +0 -303
  93. package/.genie/code/spells/evidence-storage.md +0 -26
  94. package/.genie/code/spells/file-naming-rules.md +0 -35
  95. package/.genie/code/spells/forge-code-blueprints.md +0 -195
  96. package/.genie/code/spells/genie-integration.md +0 -153
  97. package/.genie/code/spells/publishing-protocol.md +0 -61
  98. package/.genie/code/spells/team-consultation-protocol.md +0 -284
  99. package/.genie/code/spells/tool-requirements.md +0 -20
  100. package/.genie/code/spells/triad-maintenance-protocol.md +0 -154
  101. package/.genie/code/teams/tech-council/council.md +0 -328
  102. package/.genie/code/teams/tech-council/jt.md +0 -352
  103. package/.genie/code/teams/tech-council/nayr.md +0 -305
  104. package/.genie/code/teams/tech-council/oettam.md +0 -375
  105. package/.genie/neurons/README.md +0 -193
  106. package/.genie/neurons/forge.md +0 -106
  107. package/.genie/neurons/genie.md +0 -63
  108. package/.genie/neurons/review.md +0 -106
  109. package/.genie/neurons/wish.md +0 -104
  110. package/.genie/product/README.md +0 -20
  111. package/.genie/product/cli-automation.md +0 -359
  112. package/.genie/product/environment.md +0 -60
  113. package/.genie/product/mission.md +0 -60
  114. package/.genie/product/roadmap.md +0 -44
  115. package/.genie/product/tech-stack.md +0 -34
  116. package/.genie/product/templates/context-template.md +0 -218
  117. package/.genie/product/templates/qa-done-report-template.md +0 -68
  118. package/.genie/product/templates/review-report-template.md +0 -89
  119. package/.genie/product/templates/wish-template.md +0 -120
  120. package/.genie/scripts/helpers/analyze-commit.js +0 -195
  121. package/.genie/scripts/helpers/bullet-counter.js +0 -194
  122. package/.genie/scripts/helpers/bullet-find.js +0 -289
  123. package/.genie/scripts/helpers/bullet-id.js +0 -244
  124. package/.genie/scripts/helpers/check-secrets.js +0 -237
  125. package/.genie/scripts/helpers/count-tokens.js +0 -200
  126. package/.genie/scripts/helpers/create-frontmatter.js +0 -456
  127. package/.genie/scripts/helpers/detect-markers.js +0 -293
  128. package/.genie/scripts/helpers/detect-todos.js +0 -267
  129. package/.genie/scripts/helpers/detect-unlabeled-blocks.js +0 -135
  130. package/.genie/scripts/helpers/embeddings.js +0 -344
  131. package/.genie/scripts/helpers/find-empty-sections.js +0 -158
  132. package/.genie/scripts/helpers/index.js +0 -319
  133. package/.genie/scripts/helpers/validate-frontmatter.js +0 -578
  134. package/.genie/scripts/helpers/validate-links.js +0 -207
  135. package/.genie/scripts/helpers/validate-paths.js +0 -373
  136. package/.genie/spells/README.md +0 -9
  137. package/.genie/spells/ace-protocol.md +0 -118
  138. package/.genie/spells/ask-one-at-a-time.md +0 -175
  139. package/.genie/spells/backup-analyzer.md +0 -542
  140. package/.genie/spells/blocker.md +0 -12
  141. package/.genie/spells/break-things-move-fast.md +0 -56
  142. package/.genie/spells/context-candidates.md +0 -72
  143. package/.genie/spells/context-critic.md +0 -51
  144. package/.genie/spells/defer-to-expertise.md +0 -278
  145. package/.genie/spells/delegate-dont-do.md +0 -292
  146. package/.genie/spells/error-investigation-protocol.md +0 -328
  147. package/.genie/spells/evidence-based-completion.md +0 -273
  148. package/.genie/spells/experiment.md +0 -65
  149. package/.genie/spells/file-creation-protocol.md +0 -229
  150. package/.genie/spells/forge-integration.md +0 -281
  151. package/.genie/spells/forge-orchestration.md +0 -514
  152. package/.genie/spells/gather-context.md +0 -18
  153. package/.genie/spells/global-health-check.md +0 -34
  154. package/.genie/spells/global-noop-roundtrip.md +0 -25
  155. package/.genie/spells/install-genie.md +0 -1232
  156. package/.genie/spells/install.md +0 -82
  157. package/.genie/spells/investigate-before-commit.md +0 -112
  158. package/.genie/spells/know-yourself.md +0 -288
  159. package/.genie/spells/learn.md +0 -828
  160. package/.genie/spells/mcp-diagnostic-protocol.md +0 -246
  161. package/.genie/spells/mcp-first.md +0 -124
  162. package/.genie/spells/multi-step-execution.md +0 -67
  163. package/.genie/spells/orchestration-boundary-protocol.md +0 -256
  164. package/.genie/spells/orchestrator-not-implementor.md +0 -189
  165. package/.genie/spells/prompt.md +0 -746
  166. package/.genie/spells/reflect.md +0 -404
  167. package/.genie/spells/routing-decision-matrix.md +0 -368
  168. package/.genie/spells/run-in-parallel.md +0 -12
  169. package/.genie/spells/session-state-updater-example.md +0 -196
  170. package/.genie/spells/session-state-updater.md +0 -220
  171. package/.genie/spells/track-long-running-tasks.md +0 -133
  172. package/.genie/spells/troubleshoot-infrastructure.md +0 -176
  173. package/.genie/spells/upgrade-genie.md +0 -415
  174. package/.genie/spells/url-presentation-protocol.md +0 -301
  175. package/.genie/spells/wish-initiation.md +0 -158
  176. package/.genie/spells/wish-issue-linkage.md +0 -410
  177. package/.genie/spells/wish-lifecycle.md +0 -100
  178. package/.genie/state/provider-status.json +0 -3
  179. package/.genie/state/version.json +0 -16
  180. package/.genie/wishes/canonical-pgserve-pm2-supervision/WISH.md +0 -290
  181. package/.genie/wishes/pgserve-v2/BRIEF-from-genie-pgserve.md +0 -99
  182. package/.genie/wishes/pgserve-v2/WISH.md +0 -442
  183. package/.genie/wishes/release-system-genie-pattern/WISH.md +0 -268
  184. package/.genie/wishes/release-system-genie-pattern/validation.md +0 -205
  185. package/.gitguardian.yaml +0 -29
  186. package/.gitguardianignore +0 -16
  187. package/.github/workflows/ci.yml +0 -122
  188. package/.github/workflows/release.yml +0 -289
  189. package/.github/workflows/version.yml +0 -228
  190. package/.husky/pre-commit +0 -2
  191. package/AGENTS.md +0 -433
  192. package/CLAUDE.md +0 -1
  193. package/Makefile +0 -285
  194. package/assets/icon.ico +0 -0
  195. package/bun.lock +0 -435
  196. package/bunfig.toml +0 -28
  197. package/ecosystem.config.cjs +0 -23
  198. package/eslint.config.js +0 -63
  199. package/examples/multi-tenant-demo.js +0 -104
  200. package/install.sh +0 -123
  201. package/knip.json +0 -9
  202. package/scripts/test-bun-self-heal.sh +0 -163
  203. package/scripts/test-npx.sh +0 -60
  204. package/tests/audit.test.js +0 -189
  205. package/tests/backpressure.test.js +0 -167
  206. package/tests/benchmarks/runner.js +0 -1197
  207. package/tests/benchmarks/vector-generator.js +0 -368
  208. package/tests/cli-install.test.js +0 -322
  209. package/tests/control-db.test.js +0 -285
  210. package/tests/daemon-control.test.js +0 -171
  211. package/tests/daemon-fingerprint-integration.test.js +0 -111
  212. package/tests/daemon-pr24-regression.test.js +0 -198
  213. package/tests/fingerprint.test.js +0 -263
  214. package/tests/fixtures/240-orphan-seed.sql +0 -30
  215. package/tests/multi-tenant.test.js +0 -374
  216. package/tests/orphan-cleanup.test.js +0 -390
  217. package/tests/pg-version-regex.test.js +0 -129
  218. package/tests/quick-bench.js +0 -135
  219. package/tests/router-handshake-retry.test.js +0 -119
  220. package/tests/router-handshake-watchdog.test.js +0 -110
  221. package/tests/sdk.test.js +0 -71
  222. package/tests/stale-postmaster-pid.test.js +0 -85
  223. package/tests/stress-test.js +0 -439
  224. package/tests/sync-perf-test.js +0 -150
  225. package/tests/tcp-listen.test.js +0 -368
  226. package/tests/tenancy.test.js +0 -403
  227. package/tests/wrapper-supervision.test.js +0 -107
@@ -0,0 +1,146 @@
1
+ /**
2
+ * Build PostgreSQL `-c key=value` arg pairs from a settings tree.
3
+ *
4
+ * Boot-time policy is "drop + warn": invalid GUC entries (curated or
5
+ * `_extra`) are skipped and the offending entry is logged via
6
+ * `logger.warn`. Postgres itself is the final validator for value
7
+ * semantics — we only enforce the structural invariants that the
8
+ * settings-validator already guarantees on write (GUC name regex +
9
+ * scalar value safety).
10
+ *
11
+ * Curated keys win over `_extra`: when the same name appears in both,
12
+ * the curated value is the one that lands in the spawn args. This is
13
+ * the behavior the wish prescribes (build the map with `_extra` first,
14
+ * curated second) so curated overwrites.
15
+ *
16
+ * The helper is CJS so it can be required by tests that pre-date the
17
+ * bun-only daemon path and by ES-module callers (postgres.js) via
18
+ * Bun's CJS interop.
19
+ */
20
+
21
+ 'use strict';
22
+
23
+ const {
24
+ GUC_NAME_REGEX,
25
+ FORBIDDEN_VALUE_CHARS,
26
+ } = require('./settings-schema.cjs');
27
+
28
+ /**
29
+ * Drop+warn check for a curated postgres leaf. Returns true when the
30
+ * entry is safe to emit; false when it was dropped (logger.warn called).
31
+ */
32
+ function validateLeafEntry(name, value, logger) {
33
+ if (typeof name !== 'string' || !GUC_NAME_REGEX.test(name)) {
34
+ logger?.warn?.(
35
+ { guc: name, value },
36
+ `dropping invalid postgres GUC name (must match ${GUC_NAME_REGEX})`,
37
+ );
38
+ return false;
39
+ }
40
+ return assertScalarSafe(name, value, logger);
41
+ }
42
+
43
+ /**
44
+ * Drop+warn check for an entry under `postgres._extra`. Same shape as
45
+ * validateLeafEntry but the message is scoped to `_extra` so operators
46
+ * can locate the offending row.
47
+ */
48
+ function validateExtraGuc(name, value, logger) {
49
+ if (typeof name !== 'string' || !GUC_NAME_REGEX.test(name)) {
50
+ logger?.warn?.(
51
+ { guc: name, value, source: '_extra' },
52
+ `dropping invalid postgres._extra GUC name (must match ${GUC_NAME_REGEX})`,
53
+ );
54
+ return false;
55
+ }
56
+ return assertScalarSafe(`_extra.${name}`, value, logger);
57
+ }
58
+
59
+ function assertScalarSafe(label, value, logger) {
60
+ if (typeof value !== 'string' && typeof value !== 'number' && typeof value !== 'boolean') {
61
+ logger?.warn?.(
62
+ { guc: label, value },
63
+ 'dropping postgres GUC: value must be a scalar primitive (string|number|boolean)',
64
+ );
65
+ return false;
66
+ }
67
+ if (typeof value === 'string') {
68
+ if (FORBIDDEN_VALUE_CHARS.test(value)) {
69
+ logger?.warn?.(
70
+ { guc: label, value },
71
+ 'dropping postgres GUC: value contains forbidden control character (\\n, \\r, or \\0)',
72
+ );
73
+ return false;
74
+ }
75
+ if (value.startsWith('-')) {
76
+ logger?.warn?.(
77
+ { guc: label, value },
78
+ 'dropping postgres GUC: value must not start with "-" (looks like a CLI flag)',
79
+ );
80
+ return false;
81
+ }
82
+ }
83
+ return true;
84
+ }
85
+
86
+ /**
87
+ * Stringify a GUC value for the `-c key=value` form.
88
+ *
89
+ * Booleans collapse to `on`/`off` (postgresql's canonical truth-y form)
90
+ * so `autovacuum=true` lands as `autovacuum=on`. Numbers stringify as
91
+ * decimal. Strings pass through unchanged (already validated).
92
+ */
93
+ function formatGucValue(value) {
94
+ if (typeof value === 'boolean') return value ? 'on' : 'off';
95
+ return String(value);
96
+ }
97
+
98
+ /**
99
+ * Build the ordered list of `-c key=value` pairs for the embedded
100
+ * postgres process. Returns a flat array suitable for splicing into
101
+ * the spawn argv (each pair is two consecutive entries: `-c`, `k=v`).
102
+ *
103
+ * Order:
104
+ * 1. `_extra` first — populated, then overwritten by…
105
+ * 2. Curated keys (`postgres.<key>`, excluding `_extra`).
106
+ *
107
+ * Drops invalid entries with logger.warn. Returns `{ args, applied }`
108
+ * where `applied` is the final `{ key: stringValue }` map for tests
109
+ * to inspect.
110
+ */
111
+ function buildPostgresArgs(postgresSettings, { logger } = {}) {
112
+ const merged = new Map();
113
+
114
+ const extras = postgresSettings && postgresSettings._extra;
115
+ if (extras && typeof extras === 'object' && !Array.isArray(extras)) {
116
+ for (const [k, v] of Object.entries(extras)) {
117
+ if (validateExtraGuc(k, v, logger)) {
118
+ merged.set(k, formatGucValue(v));
119
+ }
120
+ }
121
+ }
122
+
123
+ if (postgresSettings && typeof postgresSettings === 'object') {
124
+ for (const [k, v] of Object.entries(postgresSettings)) {
125
+ if (k === '_extra') continue;
126
+ if (validateLeafEntry(k, v, logger)) {
127
+ merged.set(k, formatGucValue(v));
128
+ }
129
+ }
130
+ }
131
+
132
+ const args = [];
133
+ const applied = {};
134
+ for (const [k, v] of merged) {
135
+ args.push('-c', `${k}=${v}`);
136
+ applied[k] = v;
137
+ }
138
+ return { args, applied };
139
+ }
140
+
141
+ module.exports = {
142
+ buildPostgresArgs,
143
+ validateLeafEntry,
144
+ validateExtraGuc,
145
+ formatGucValue,
146
+ };
@@ -0,0 +1,422 @@
1
+ /**
2
+ * autopg settings schema (version 1).
3
+ *
4
+ * Single source of truth shared by:
5
+ * - settings-loader.js (defaults + env merge)
6
+ * - settings-validator.js (type / range / enum checks)
7
+ * - settings-writer.js (validateAll on write)
8
+ * - cli-config.cjs (list / get / set surface)
9
+ * - daemon (cluster.js, postgres.js) (effective config consumer)
10
+ * - console UI (form rendering)
11
+ *
12
+ * Schema model:
13
+ * - 6 sections: server, runtime, sync, supervision, postgres, ui.
14
+ * - Each leaf: { type, default, env?, range?, enum?, description? }.
15
+ * - `postgres._extra` is a free-form passthrough map: { [gucName]: scalar }
16
+ * validated dynamically against the GUC name regex + scalar value rules.
17
+ *
18
+ * `env` lists env var names checked in priority order (AUTOPG first, PGSERVE
19
+ * second). Loader uses this list to compute precedence and source attribution.
20
+ *
21
+ * No frontmatter / version field on individual leaves — top-level SCHEMA_VERSION
22
+ * tracks the schema shape itself.
23
+ */
24
+
25
+ 'use strict';
26
+
27
+ const SCHEMA_VERSION = 1;
28
+
29
+ /**
30
+ * GUC name regex — lower-case ASCII identifier, no leading digit, no spaces.
31
+ * Postgres GUC names are case-insensitive but conventionally written
32
+ * lower-case; we enforce lower-case at the schema boundary so `_extra`
33
+ * keys round-trip cleanly through `-c key=value`.
34
+ */
35
+ const GUC_NAME_REGEX = /^[a-z][a-z0-9_]*$/;
36
+
37
+ /**
38
+ * Scalar value rule for postgres GUCs (curated and `_extra`):
39
+ * - must be string | number | boolean
40
+ * - if string: no \n, \r, \0
41
+ * - if string: no leading `-` (would look like a CLI flag to Bun.spawn array form)
42
+ *
43
+ * Defense-in-depth on top of Bun.spawn's array form (which avoids shell
44
+ * interpretation entirely).
45
+ */
46
+ const FORBIDDEN_VALUE_CHARS = /[\n\r\0]/;
47
+
48
+ const SCHEMA = {
49
+ server: {
50
+ port: {
51
+ type: 'int',
52
+ default: 8432,
53
+ env: ['AUTOPG_PORT', 'PGSERVE_PORT'],
54
+ range: [1, 65535],
55
+ description: 'Router TCP port (clients connect here)',
56
+ },
57
+ host: {
58
+ type: 'string',
59
+ default: '127.0.0.1',
60
+ env: ['AUTOPG_HOST', 'PGSERVE_HOST'],
61
+ description: 'Bind address for the router',
62
+ },
63
+ pgPort: {
64
+ type: 'int',
65
+ default: 6432,
66
+ env: ['AUTOPG_PG_PORT', 'PGSERVE_PG_PORT'],
67
+ range: [1, 65535],
68
+ description: 'Internal PostgreSQL backend port',
69
+ },
70
+ pgSocketPath: {
71
+ type: 'string',
72
+ default: '',
73
+ env: ['AUTOPG_PG_SOCKET', 'PGSERVE_PG_SOCKET'],
74
+ description: 'Unix socket path for backend (empty = TCP only)',
75
+ nullable: true,
76
+ },
77
+ pgUser: {
78
+ type: 'string',
79
+ default: 'postgres',
80
+ env: ['AUTOPG_PG_USER', 'PGSERVE_PG_USER'],
81
+ description: 'Backend superuser',
82
+ },
83
+ pgPassword: {
84
+ type: 'string',
85
+ default: 'postgres',
86
+ env: ['AUTOPG_PG_PASSWORD', 'PGSERVE_PG_PASSWORD'],
87
+ secret: true,
88
+ description: 'Backend superuser password (file is chmod 0600)',
89
+ },
90
+ },
91
+
92
+ runtime: {
93
+ logLevel: {
94
+ type: 'enum',
95
+ default: 'info',
96
+ enum: ['debug', 'info', 'warn', 'error'],
97
+ env: ['AUTOPG_LOG_LEVEL', 'PGSERVE_LOG_LEVEL', 'LOG_LEVEL'],
98
+ description: 'Log verbosity',
99
+ },
100
+ autoProvision: {
101
+ type: 'bool',
102
+ default: true,
103
+ env: ['AUTOPG_AUTO_PROVISION', 'PGSERVE_AUTO_PROVISION'],
104
+ description: 'Auto-create missing databases on first connect',
105
+ },
106
+ enablePgvector: {
107
+ type: 'bool',
108
+ default: false,
109
+ env: ['AUTOPG_ENABLE_PGVECTOR', 'PGSERVE_ENABLE_PGVECTOR'],
110
+ description: 'Load pgvector extension on database create',
111
+ },
112
+ dataDir: {
113
+ type: 'string',
114
+ default: '',
115
+ env: ['AUTOPG_DATA_DIR', 'PGSERVE_DATA_DIR'],
116
+ description: 'PG cluster data directory (empty = <configDir>/data)',
117
+ nullable: true,
118
+ },
119
+ cluster: {
120
+ type: 'enum',
121
+ default: 'auto',
122
+ enum: ['auto', 'on', 'off'],
123
+ env: ['AUTOPG_CLUSTER', 'PGSERVE_CLUSTER'],
124
+ description: 'Cluster mode (auto = on for multi-core hosts)',
125
+ },
126
+ workers: {
127
+ type: 'int',
128
+ default: 0,
129
+ range: [0, 256],
130
+ env: ['AUTOPG_WORKERS', 'PGSERVE_WORKERS'],
131
+ description: 'Worker processes (0 = CPU cores)',
132
+ },
133
+ ramMode: {
134
+ type: 'bool',
135
+ default: false,
136
+ env: ['AUTOPG_RAM', 'PGSERVE_RAM'],
137
+ description: 'Use /dev/shm storage (Linux only, ~2x faster)',
138
+ },
139
+ statsDashboard: {
140
+ type: 'bool',
141
+ default: true,
142
+ env: ['AUTOPG_STATS', 'PGSERVE_STATS'],
143
+ description: 'Show TTY stats dashboard when running in foreground',
144
+ },
145
+ },
146
+
147
+ sync: {
148
+ enabled: {
149
+ type: 'bool',
150
+ default: false,
151
+ env: ['AUTOPG_SYNC_ENABLED', 'PGSERVE_SYNC_ENABLED'],
152
+ description: 'Enable WAL-based logical replication',
153
+ },
154
+ url: {
155
+ type: 'string',
156
+ default: '',
157
+ env: ['AUTOPG_SYNC_TO', 'PGSERVE_SYNC_TO'],
158
+ description: 'Upstream PostgreSQL URL for replication (--sync-to)',
159
+ secret: true,
160
+ nullable: true,
161
+ },
162
+ databases: {
163
+ type: 'string',
164
+ default: '*',
165
+ env: ['AUTOPG_SYNC_DATABASES', 'PGSERVE_SYNC_DATABASES'],
166
+ description: 'Database glob patterns to sync, comma-separated (--sync-databases)',
167
+ },
168
+ },
169
+
170
+ supervision: {
171
+ maxMemory: {
172
+ type: 'string',
173
+ default: '4G',
174
+ env: ['AUTOPG_MAX_MEMORY', 'PGSERVE_MAX_MEMORY'],
175
+ description: 'pm2 memory ceiling (e.g. 4G, 8G)',
176
+ },
177
+ maxRestarts: {
178
+ type: 'int',
179
+ default: 50,
180
+ env: ['AUTOPG_MAX_RESTARTS', 'PGSERVE_MAX_RESTARTS'],
181
+ range: [1, 1000],
182
+ description: 'pm2 max rapid restarts before giving up',
183
+ },
184
+ minUptimeMs: {
185
+ type: 'int',
186
+ default: 10000,
187
+ env: ['AUTOPG_MIN_UPTIME_MS', 'PGSERVE_MIN_UPTIME_MS'],
188
+ range: [0, 600000],
189
+ description: 'pm2 min uptime to count as a healthy start',
190
+ },
191
+ restartDelayMs: {
192
+ type: 'int',
193
+ default: 4000,
194
+ range: [0, 600000],
195
+ env: ['AUTOPG_RESTART_DELAY_MS', 'PGSERVE_RESTART_DELAY_MS'],
196
+ description: 'pm2 fixed delay before each restart',
197
+ },
198
+ expBackoffRestartDelayMs: {
199
+ type: 'int',
200
+ default: 100,
201
+ range: [0, 600000],
202
+ env: ['AUTOPG_EXP_BACKOFF_DELAY_MS', 'PGSERVE_EXP_BACKOFF_DELAY_MS'],
203
+ description: 'pm2 initial exponential-backoff delay',
204
+ },
205
+ expBackoffMaxMs: {
206
+ type: 'int',
207
+ default: 60000,
208
+ range: [1000, 600000],
209
+ env: ['AUTOPG_EXP_BACKOFF_MAX_MS', 'PGSERVE_EXP_BACKOFF_MAX_MS'],
210
+ description: 'pm2 exponential-backoff ceiling (ramp cap ~60s)',
211
+ },
212
+ killTimeoutMs: {
213
+ type: 'int',
214
+ default: 60000,
215
+ env: ['AUTOPG_KILL_TIMEOUT_MS', 'PGSERVE_KILL_TIMEOUT_MS'],
216
+ range: [1000, 600000],
217
+ description: 'Graceful shutdown window before SIGKILL',
218
+ },
219
+ logDateFormat: {
220
+ type: 'string',
221
+ default: 'YYYY-MM-DD HH:mm:ss.SSS',
222
+ env: ['AUTOPG_LOG_DATE_FORMAT', 'PGSERVE_LOG_DATE_FORMAT'],
223
+ description: 'pm2 log timestamp format string',
224
+ },
225
+ },
226
+
227
+ security: {
228
+ handshakeDeadlineMs: {
229
+ type: 'int',
230
+ default: 5000,
231
+ range: [100, 60000],
232
+ env: ['AUTOPG_HANDSHAKE_DEADLINE_MS', 'PGSERVE_HANDSHAKE_DEADLINE_MS'],
233
+ description: 'Control-socket peer handshake deadline before forced close',
234
+ },
235
+ },
236
+
237
+ audit: {
238
+ target: {
239
+ type: 'string',
240
+ default: '',
241
+ env: ['AUTOPG_AUDIT_TARGET', 'PGSERVE_AUDIT_TARGET'],
242
+ description: 'Audit event destination (JSONL file path or HTTP endpoint)',
243
+ nullable: true,
244
+ },
245
+ },
246
+
247
+ postgres: {
248
+ // Curated set: 14 commonly-tuned GUCs + the WAL replication block.
249
+ // Hardcoded values previously in postgres.js (`max_connections=1000` and the
250
+ // sync-conditional WAL block) are promoted here as defaults.
251
+ max_connections: {
252
+ type: 'int',
253
+ default: 1000,
254
+ range: [1, 262143],
255
+ guc: true,
256
+ description: 'Maximum concurrent connections',
257
+ },
258
+ shared_buffers: {
259
+ type: 'string',
260
+ default: '128MB',
261
+ guc: true,
262
+ description: 'Shared memory buffer pool',
263
+ },
264
+ work_mem: {
265
+ type: 'string',
266
+ default: '4MB',
267
+ guc: true,
268
+ description: 'Per-operation work memory',
269
+ },
270
+ maintenance_work_mem: {
271
+ type: 'string',
272
+ default: '64MB',
273
+ guc: true,
274
+ description: 'Memory for VACUUM / CREATE INDEX',
275
+ },
276
+ effective_cache_size: {
277
+ type: 'string',
278
+ default: '4GB',
279
+ guc: true,
280
+ description: 'Planner estimate of OS cache',
281
+ },
282
+ wal_level: {
283
+ type: 'enum',
284
+ default: 'logical',
285
+ enum: ['minimal', 'replica', 'logical'],
286
+ guc: true,
287
+ description: 'WAL detail level (logical = replication-ready)',
288
+ },
289
+ max_replication_slots: {
290
+ type: 'int',
291
+ default: 10,
292
+ range: [0, 1000],
293
+ guc: true,
294
+ description: 'Max replication slots',
295
+ },
296
+ max_wal_senders: {
297
+ type: 'int',
298
+ default: 10,
299
+ range: [0, 1000],
300
+ guc: true,
301
+ description: 'Max WAL sender processes',
302
+ },
303
+ wal_keep_size: {
304
+ type: 'string',
305
+ default: '512MB',
306
+ guc: true,
307
+ description: 'WAL retention for replicas to catch up',
308
+ },
309
+ log_statement: {
310
+ type: 'enum',
311
+ default: 'none',
312
+ enum: ['none', 'ddl', 'mod', 'all'],
313
+ guc: true,
314
+ description: 'SQL statement logging level',
315
+ },
316
+ log_min_duration_statement: {
317
+ type: 'int',
318
+ default: -1,
319
+ range: [-1, 2147483647],
320
+ guc: true,
321
+ description: 'Slow query threshold (ms, -1 = off)',
322
+ },
323
+ statement_timeout: {
324
+ type: 'int',
325
+ default: 0,
326
+ range: [0, 2147483647],
327
+ guc: true,
328
+ description: 'Statement timeout (ms, 0 = none)',
329
+ },
330
+ idle_in_transaction_session_timeout: {
331
+ type: 'int',
332
+ default: 0,
333
+ range: [0, 2147483647],
334
+ guc: true,
335
+ description: 'Idle-in-transaction timeout (ms, 0 = none)',
336
+ },
337
+ autovacuum: {
338
+ type: 'bool',
339
+ default: true,
340
+ guc: true,
341
+ description: 'Enable autovacuum daemon',
342
+ },
343
+ // Free-form passthrough for additional GUCs not curated above.
344
+ // Validated dynamically against GUC_NAME_REGEX + scalar value rules.
345
+ _extra: {
346
+ type: 'guc_map',
347
+ default: {},
348
+ description: 'Raw passthrough GUC map (key=value applied as -c flags)',
349
+ },
350
+ },
351
+
352
+ ui: {
353
+ theme: {
354
+ type: 'enum',
355
+ default: 'mdr',
356
+ enum: ['mdr', 'lumon'],
357
+ description: 'Console theme',
358
+ },
359
+ phosphor: {
360
+ type: 'enum',
361
+ default: 'amber',
362
+ enum: ['amber', 'green', 'white'],
363
+ description: 'CRT phosphor color',
364
+ },
365
+ density: {
366
+ type: 'enum',
367
+ default: 'comfortable',
368
+ enum: ['compact', 'comfortable', 'spacious'],
369
+ description: 'Layout density',
370
+ },
371
+ crt: {
372
+ type: 'bool',
373
+ default: true,
374
+ description: 'CRT scanline effect',
375
+ },
376
+ },
377
+ };
378
+
379
+ /**
380
+ * Build a map of every leaf key (dotted path) to its descriptor.
381
+ * Nested example: `server.port` -> { type: 'int', default: 8432, ... }.
382
+ * `postgres._extra` is treated as a leaf (its descriptor is the guc_map type).
383
+ */
384
+ function flattenSchema(schema = SCHEMA) {
385
+ const out = {};
386
+ for (const [section, fields] of Object.entries(schema)) {
387
+ for (const [key, descriptor] of Object.entries(fields)) {
388
+ out[`${section}.${key}`] = descriptor;
389
+ }
390
+ }
391
+ return out;
392
+ }
393
+
394
+ /**
395
+ * Default settings tree — useful as a baseline for the loader and as the
396
+ * seed for `autopg config init`.
397
+ */
398
+ function buildDefaults(schema = SCHEMA) {
399
+ const out = {};
400
+ for (const [section, fields] of Object.entries(schema)) {
401
+ out[section] = {};
402
+ for (const [key, descriptor] of Object.entries(fields)) {
403
+ // Clone defaults so callers can't mutate the schema's reference.
404
+ const def = descriptor.default;
405
+ if (def && typeof def === 'object') {
406
+ out[section][key] = Array.isArray(def) ? [...def] : { ...def };
407
+ } else {
408
+ out[section][key] = def;
409
+ }
410
+ }
411
+ }
412
+ return out;
413
+ }
414
+
415
+ module.exports = {
416
+ SCHEMA,
417
+ SCHEMA_VERSION,
418
+ GUC_NAME_REGEX,
419
+ FORBIDDEN_VALUE_CHARS,
420
+ flattenSchema,
421
+ buildDefaults,
422
+ };