pgserve 2.1.3 → 2.2.1
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.
- package/CHANGELOG.md +96 -0
- package/README.md +105 -1
- package/bin/autopg-wrapper.cjs +16 -0
- package/bin/pgserve-wrapper.cjs +32 -6
- package/bin/postgres-server.js +56 -0
- package/console/README.md +131 -0
- package/console/api.js +173 -0
- package/console/app.jsx +483 -0
- package/console/colors_and_type.css +227 -0
- package/console/components.jsx +167 -0
- package/console/console.css +1666 -0
- package/console/data.jsx +350 -0
- package/console/index.html +31 -0
- package/console/screens/databases.jsx +5 -0
- package/console/screens/health.jsx +5 -0
- package/console/screens/ingress.jsx +5 -0
- package/console/screens/optimizer.jsx +5 -0
- package/console/screens/rlm-sim.jsx +5 -0
- package/console/screens/rlm-trace.jsx +5 -0
- package/console/screens/security.jsx +5 -0
- package/console/screens/settings.jsx +611 -0
- package/console/screens/sql.jsx +5 -0
- package/console/screens/sync.jsx +5 -0
- package/console/screens/tables.jsx +5 -0
- package/console/tweaks-panel.jsx +425 -0
- package/package.json +14 -2
- package/scripts/postinstall.cjs +60 -0
- package/src/cli-config.cjs +310 -0
- package/src/cli-install.cjs +112 -11
- package/src/cli-restart.cjs +228 -0
- package/src/cli-ui.cjs +580 -0
- package/src/cluster.js +43 -38
- package/src/postgres.js +141 -19
- package/src/settings-loader.cjs +235 -0
- package/src/settings-migrate.cjs +212 -0
- package/src/settings-pg-args.cjs +146 -0
- package/src/settings-schema.cjs +422 -0
- package/src/settings-validator.cjs +416 -0
- package/src/settings-writer.cjs +288 -0
- package/src/upgrade/index.js +65 -0
- package/src/upgrade/runner.js +23 -0
- package/src/upgrade/steps/binary-cache-flush.js +67 -0
- package/src/upgrade/steps/consumer-signal.js +40 -0
- package/src/upgrade/steps/env-refresh.js +89 -0
- package/src/upgrade/steps/health-validate.js +53 -0
- package/src/upgrade/steps/plpgsql-resolve.js +66 -0
- package/src/upgrade/steps/port-reconcile.js +52 -0
- package/.claude/context/windows-debug.md +0 -119
- package/.genie/AGENTS.md +0 -15
- package/.genie/agents/README.md +0 -110
- package/.genie/agents/analyze.md +0 -176
- package/.genie/agents/forge.md +0 -290
- package/.genie/agents/garbage-cleaner.md +0 -324
- package/.genie/agents/garbage-collector.md +0 -596
- package/.genie/agents/github-issue-gc.md +0 -618
- package/.genie/agents/review.md +0 -380
- package/.genie/agents/semantic-analyzer/find-duplicates.md +0 -90
- package/.genie/agents/semantic-analyzer/find-orphans.md +0 -99
- package/.genie/agents/semantic-analyzer.md +0 -101
- package/.genie/agents/update.md +0 -182
- package/.genie/agents/wish.md +0 -357
- package/.genie/brainstorms/pgserve-v2/DESIGN.md +0 -174
- package/.genie/code/AGENTS.md +0 -694
- package/.genie/code/agents/audit/risk.md +0 -173
- package/.genie/code/agents/audit/security.md +0 -189
- package/.genie/code/agents/audit.md +0 -145
- package/.genie/code/agents/challenge.md +0 -230
- package/.genie/code/agents/change-reviewer.md +0 -295
- package/.genie/code/agents/code-garbage-collector.md +0 -425
- package/.genie/code/agents/code-quality.md +0 -410
- package/.genie/code/agents/commit-suggester.md +0 -255
- package/.genie/code/agents/commit.md +0 -124
- package/.genie/code/agents/consensus.md +0 -204
- package/.genie/code/agents/daily-standup.md +0 -722
- package/.genie/code/agents/docgen.md +0 -48
- package/.genie/code/agents/explore.md +0 -79
- package/.genie/code/agents/fix.md +0 -100
- package/.genie/code/agents/git/commit-advisory.md +0 -219
- package/.genie/code/agents/git/workflows/issue.md +0 -244
- package/.genie/code/agents/git/workflows/pr.md +0 -179
- package/.genie/code/agents/git/workflows/release.md +0 -460
- package/.genie/code/agents/git/workflows/report.md +0 -342
- package/.genie/code/agents/git.md +0 -432
- package/.genie/code/agents/implementor.md +0 -161
- package/.genie/code/agents/install.md +0 -515
- package/.genie/code/agents/issue-creator.md +0 -344
- package/.genie/code/agents/polish.md +0 -116
- package/.genie/code/agents/qa.md +0 -653
- package/.genie/code/agents/refactor.md +0 -294
- package/.genie/code/agents/release.md +0 -1129
- package/.genie/code/agents/roadmap.md +0 -885
- package/.genie/code/agents/tests.md +0 -557
- package/.genie/code/agents/tracer.md +0 -50
- package/.genie/code/agents/update/upstream-update.md +0 -85
- package/.genie/code/agents/update/versions/generic-update.md +0 -305
- package/.genie/code/agents/vibe.md +0 -1317
- package/.genie/code/spells/agent-configuration.md +0 -58
- package/.genie/code/spells/automated-rc-publishing.md +0 -106
- package/.genie/code/spells/branch-tracker-guidance.md +0 -28
- package/.genie/code/spells/debug.md +0 -320
- package/.genie/code/spells/emoji-naming-convention.md +0 -303
- package/.genie/code/spells/evidence-storage.md +0 -26
- package/.genie/code/spells/file-naming-rules.md +0 -35
- package/.genie/code/spells/forge-code-blueprints.md +0 -195
- package/.genie/code/spells/genie-integration.md +0 -153
- package/.genie/code/spells/publishing-protocol.md +0 -61
- package/.genie/code/spells/team-consultation-protocol.md +0 -284
- package/.genie/code/spells/tool-requirements.md +0 -20
- package/.genie/code/spells/triad-maintenance-protocol.md +0 -154
- package/.genie/code/teams/tech-council/council.md +0 -328
- package/.genie/code/teams/tech-council/jt.md +0 -352
- package/.genie/code/teams/tech-council/nayr.md +0 -305
- package/.genie/code/teams/tech-council/oettam.md +0 -375
- package/.genie/neurons/README.md +0 -193
- package/.genie/neurons/forge.md +0 -106
- package/.genie/neurons/genie.md +0 -63
- package/.genie/neurons/review.md +0 -106
- package/.genie/neurons/wish.md +0 -104
- package/.genie/product/README.md +0 -20
- package/.genie/product/cli-automation.md +0 -359
- package/.genie/product/environment.md +0 -60
- package/.genie/product/mission.md +0 -60
- package/.genie/product/roadmap.md +0 -44
- package/.genie/product/tech-stack.md +0 -34
- package/.genie/product/templates/context-template.md +0 -218
- package/.genie/product/templates/qa-done-report-template.md +0 -68
- package/.genie/product/templates/review-report-template.md +0 -89
- package/.genie/product/templates/wish-template.md +0 -120
- package/.genie/scripts/helpers/analyze-commit.js +0 -195
- package/.genie/scripts/helpers/bullet-counter.js +0 -194
- package/.genie/scripts/helpers/bullet-find.js +0 -289
- package/.genie/scripts/helpers/bullet-id.js +0 -244
- package/.genie/scripts/helpers/check-secrets.js +0 -237
- package/.genie/scripts/helpers/count-tokens.js +0 -200
- package/.genie/scripts/helpers/create-frontmatter.js +0 -456
- package/.genie/scripts/helpers/detect-markers.js +0 -293
- package/.genie/scripts/helpers/detect-todos.js +0 -267
- package/.genie/scripts/helpers/detect-unlabeled-blocks.js +0 -135
- package/.genie/scripts/helpers/embeddings.js +0 -344
- package/.genie/scripts/helpers/find-empty-sections.js +0 -158
- package/.genie/scripts/helpers/index.js +0 -319
- package/.genie/scripts/helpers/validate-frontmatter.js +0 -578
- package/.genie/scripts/helpers/validate-links.js +0 -207
- package/.genie/scripts/helpers/validate-paths.js +0 -373
- package/.genie/spells/README.md +0 -9
- package/.genie/spells/ace-protocol.md +0 -118
- package/.genie/spells/ask-one-at-a-time.md +0 -175
- package/.genie/spells/backup-analyzer.md +0 -542
- package/.genie/spells/blocker.md +0 -12
- package/.genie/spells/break-things-move-fast.md +0 -56
- package/.genie/spells/context-candidates.md +0 -72
- package/.genie/spells/context-critic.md +0 -51
- package/.genie/spells/defer-to-expertise.md +0 -278
- package/.genie/spells/delegate-dont-do.md +0 -292
- package/.genie/spells/error-investigation-protocol.md +0 -328
- package/.genie/spells/evidence-based-completion.md +0 -273
- package/.genie/spells/experiment.md +0 -65
- package/.genie/spells/file-creation-protocol.md +0 -229
- package/.genie/spells/forge-integration.md +0 -281
- package/.genie/spells/forge-orchestration.md +0 -514
- package/.genie/spells/gather-context.md +0 -18
- package/.genie/spells/global-health-check.md +0 -34
- package/.genie/spells/global-noop-roundtrip.md +0 -25
- package/.genie/spells/install-genie.md +0 -1232
- package/.genie/spells/install.md +0 -82
- package/.genie/spells/investigate-before-commit.md +0 -112
- package/.genie/spells/know-yourself.md +0 -288
- package/.genie/spells/learn.md +0 -828
- package/.genie/spells/mcp-diagnostic-protocol.md +0 -246
- package/.genie/spells/mcp-first.md +0 -124
- package/.genie/spells/multi-step-execution.md +0 -67
- package/.genie/spells/orchestration-boundary-protocol.md +0 -256
- package/.genie/spells/orchestrator-not-implementor.md +0 -189
- package/.genie/spells/prompt.md +0 -746
- package/.genie/spells/reflect.md +0 -404
- package/.genie/spells/routing-decision-matrix.md +0 -368
- package/.genie/spells/run-in-parallel.md +0 -12
- package/.genie/spells/session-state-updater-example.md +0 -196
- package/.genie/spells/session-state-updater.md +0 -220
- package/.genie/spells/track-long-running-tasks.md +0 -133
- package/.genie/spells/troubleshoot-infrastructure.md +0 -176
- package/.genie/spells/upgrade-genie.md +0 -415
- package/.genie/spells/url-presentation-protocol.md +0 -301
- package/.genie/spells/wish-initiation.md +0 -158
- package/.genie/spells/wish-issue-linkage.md +0 -410
- package/.genie/spells/wish-lifecycle.md +0 -100
- package/.genie/state/provider-status.json +0 -3
- package/.genie/state/version.json +0 -16
- package/.genie/wishes/canonical-pgserve-pm2-supervision/WISH.md +0 -290
- package/.genie/wishes/pgserve-v2/BRIEF-from-genie-pgserve.md +0 -99
- package/.genie/wishes/pgserve-v2/WISH.md +0 -442
- package/.genie/wishes/release-system-genie-pattern/WISH.md +0 -268
- package/.genie/wishes/release-system-genie-pattern/validation.md +0 -205
- package/.gitguardian.yaml +0 -29
- package/.gitguardianignore +0 -16
- package/.github/workflows/ci.yml +0 -122
- package/.github/workflows/release.yml +0 -289
- package/.github/workflows/version.yml +0 -228
- package/.husky/pre-commit +0 -2
- package/AGENTS.md +0 -433
- package/CLAUDE.md +0 -1
- package/Makefile +0 -285
- package/assets/icon.ico +0 -0
- package/bun.lock +0 -435
- package/bunfig.toml +0 -28
- package/ecosystem.config.cjs +0 -23
- package/eslint.config.js +0 -63
- package/examples/multi-tenant-demo.js +0 -104
- package/install.sh +0 -123
- package/knip.json +0 -9
- package/tests/audit.test.js +0 -189
- package/tests/backpressure.test.js +0 -167
- package/tests/benchmarks/runner.js +0 -1197
- package/tests/benchmarks/vector-generator.js +0 -368
- package/tests/cli-install.test.js +0 -322
- package/tests/control-db.test.js +0 -285
- package/tests/daemon-args.test.js +0 -86
- package/tests/daemon-control.test.js +0 -171
- package/tests/daemon-fingerprint-integration.test.js +0 -111
- package/tests/daemon-pr24-regression.test.js +0 -198
- package/tests/fingerprint.test.js +0 -263
- package/tests/fixtures/240-orphan-seed.sql +0 -30
- package/tests/multi-tenant.test.js +0 -374
- package/tests/orphan-cleanup.test.js +0 -390
- package/tests/pg-version-regex.test.js +0 -129
- package/tests/quick-bench.js +0 -135
- package/tests/router-handshake-retry.test.js +0 -119
- package/tests/router-handshake-watchdog.test.js +0 -110
- package/tests/sdk.test.js +0 -71
- package/tests/stale-postmaster-pid.test.js +0 -85
- package/tests/stress-test.js +0 -439
- package/tests/sync-perf-test.js +0 -150
- package/tests/tcp-listen.test.js +0 -368
- package/tests/tenancy.test.js +0 -403
- package/tests/wrapper-supervision.test.js +0 -107
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* PR #24 regression tests for the v2 daemon.
|
|
3
|
-
*
|
|
4
|
-
* The daemon (src/daemon.js) shares a PostgresManager lifecycle with the
|
|
5
|
-
* v1 router (src/router.js). PR #24's fixes for issue #24 (stale socketDir
|
|
6
|
-
* leaks across stop/start cycles) must remain in force after the v2 cut.
|
|
7
|
-
*
|
|
8
|
-
* Coverage:
|
|
9
|
-
* 1. PostgresManager.stop() nulls socketDir/databaseDir.
|
|
10
|
-
* 2. start() + stop() + start() yields a fresh socketDir (no leak).
|
|
11
|
-
* 3. Double start() is a no-op (re-entry guard).
|
|
12
|
-
* 4. Daemon mode does NOT introduce a new socketDir leak path under
|
|
13
|
-
* abnormal exit (kill -9): orphaned socket file + pid lock are cleaned
|
|
14
|
-
* up by the next `PgserveDaemon.start()` boot via stale-pid detection.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import { describe, test, expect } from 'bun:test';
|
|
18
|
-
import fs from 'fs';
|
|
19
|
-
import path from 'path';
|
|
20
|
-
|
|
21
|
-
import { PostgresManager } from '../src/postgres.js';
|
|
22
|
-
import { createLogger } from '../src/logger.js';
|
|
23
|
-
import {
|
|
24
|
-
PgserveDaemon,
|
|
25
|
-
acquirePidLock,
|
|
26
|
-
resolveControlSocketPath,
|
|
27
|
-
resolvePidLockPath,
|
|
28
|
-
isProcessAlive,
|
|
29
|
-
} from '../src/daemon.js';
|
|
30
|
-
|
|
31
|
-
function silentLogger() {
|
|
32
|
-
return createLogger({ level: 'warn' });
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// Each test uses a unique controlSocketDir under tmp so concurrent runs
|
|
36
|
-
// (and the existing host's real /run/user/<uid>/pgserve) cannot collide.
|
|
37
|
-
function makeDaemonDirs(tag) {
|
|
38
|
-
return fs.mkdtempSync(path.join('/tmp', `pgs-${tag}-`));
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
describe('PR #24 regression — PostgresManager lifecycle', () => {
|
|
42
|
-
test('stop() nulls socketDir/databaseDir', async () => {
|
|
43
|
-
const pg = new PostgresManager({ port: 16001, logger: silentLogger() });
|
|
44
|
-
await pg.start();
|
|
45
|
-
expect(pg.socketDir).not.toBeNull();
|
|
46
|
-
expect(fs.existsSync(pg.socketDir)).toBe(true);
|
|
47
|
-
const stale = pg.socketDir;
|
|
48
|
-
|
|
49
|
-
await pg.stop();
|
|
50
|
-
|
|
51
|
-
expect(pg.socketDir).toBeNull();
|
|
52
|
-
expect(pg.databaseDir).toBeNull();
|
|
53
|
-
expect(pg.getSocketPath()).toBeNull();
|
|
54
|
-
expect(fs.existsSync(stale)).toBe(false);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
test('start()+stop()+start() yields fresh socketDir, no leak', async () => {
|
|
58
|
-
const pg = new PostgresManager({ port: 16002, logger: silentLogger() });
|
|
59
|
-
|
|
60
|
-
await pg.start();
|
|
61
|
-
const dirA = pg.socketDir;
|
|
62
|
-
expect(dirA).not.toBeNull();
|
|
63
|
-
|
|
64
|
-
await pg.stop();
|
|
65
|
-
expect(pg.socketDir).toBeNull();
|
|
66
|
-
|
|
67
|
-
await pg.start();
|
|
68
|
-
const dirB = pg.socketDir;
|
|
69
|
-
expect(dirB).not.toBeNull();
|
|
70
|
-
expect(dirB).not.toBe(dirA);
|
|
71
|
-
expect(fs.existsSync(dirB)).toBe(true);
|
|
72
|
-
// Old dir must be gone; PR #24 guarantees no leak across cycles.
|
|
73
|
-
expect(fs.existsSync(dirA)).toBe(false);
|
|
74
|
-
|
|
75
|
-
await pg.stop();
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
test('double start() is a no-op (re-entry guard preserved)', async () => {
|
|
79
|
-
const pg = new PostgresManager({ port: 16003, logger: silentLogger() });
|
|
80
|
-
await pg.start();
|
|
81
|
-
const before = pg.socketDir;
|
|
82
|
-
|
|
83
|
-
const result = await pg.start();
|
|
84
|
-
expect(result).toBe(pg);
|
|
85
|
-
expect(pg.socketDir).toBe(before);
|
|
86
|
-
|
|
87
|
-
await pg.stop();
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
describe('PR #24 regression — daemon does not leak under abnormal exit', () => {
|
|
92
|
-
test('stale pid lock + orphaned socket are cleaned up by next daemon boot', async () => {
|
|
93
|
-
const dir = makeDaemonDirs('stale');
|
|
94
|
-
const socketPath = resolveControlSocketPath(dir);
|
|
95
|
-
const pidLockPath = resolvePidLockPath(dir);
|
|
96
|
-
|
|
97
|
-
// Simulate kill -9: write a pid file pointing at a guaranteed-dead pid
|
|
98
|
-
// and create a fake stale socket file beside it. PID 1 is always alive
|
|
99
|
-
// on Unix, so we manufacture a dead one by reading max_pid + 1 (Linux)
|
|
100
|
-
// or just using a high value not currently in use.
|
|
101
|
-
const deadPid = pickDeadPid();
|
|
102
|
-
expect(isProcessAlive(deadPid)).toBe(false);
|
|
103
|
-
|
|
104
|
-
fs.writeFileSync(pidLockPath, String(deadPid), { mode: 0o600 });
|
|
105
|
-
fs.writeFileSync(socketPath, ''); // stand-in for an orphaned socket file
|
|
106
|
-
expect(fs.existsSync(pidLockPath)).toBe(true);
|
|
107
|
-
expect(fs.existsSync(socketPath)).toBe(true);
|
|
108
|
-
|
|
109
|
-
const lock = acquirePidLock({
|
|
110
|
-
pidLockPath,
|
|
111
|
-
socketPath,
|
|
112
|
-
logger: silentLogger(),
|
|
113
|
-
});
|
|
114
|
-
expect(lock.acquired).toBe(true);
|
|
115
|
-
|
|
116
|
-
// The lock file now belongs to *us* (this test's process pid), and the
|
|
117
|
-
// orphaned socket placeholder must have been removed during stale-pid
|
|
118
|
-
// cleanup so the daemon can bind a fresh socket on the same path.
|
|
119
|
-
expect(fs.existsSync(pidLockPath)).toBe(true);
|
|
120
|
-
expect(fs.readFileSync(pidLockPath, 'utf8').trim()).toBe(String(process.pid));
|
|
121
|
-
expect(fs.existsSync(socketPath)).toBe(false);
|
|
122
|
-
|
|
123
|
-
// Cleanup the test's lock so we don't leak between tests.
|
|
124
|
-
fs.unlinkSync(pidLockPath);
|
|
125
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
test('PgserveDaemon.start refuses second invocation while first is alive', async () => {
|
|
129
|
-
const dir = makeDaemonDirs('singleton');
|
|
130
|
-
const d1 = new PgserveDaemon({
|
|
131
|
-
controlSocketDir: dir,
|
|
132
|
-
controlSocketPath: resolveControlSocketPath(dir),
|
|
133
|
-
pidLockPath: resolvePidLockPath(dir),
|
|
134
|
-
pgPort: 16010,
|
|
135
|
-
logger: silentLogger(),
|
|
136
|
-
});
|
|
137
|
-
await d1.start();
|
|
138
|
-
|
|
139
|
-
const d2 = new PgserveDaemon({
|
|
140
|
-
controlSocketDir: dir,
|
|
141
|
-
controlSocketPath: resolveControlSocketPath(dir),
|
|
142
|
-
pidLockPath: resolvePidLockPath(dir),
|
|
143
|
-
pgPort: 16011,
|
|
144
|
-
logger: silentLogger(),
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
let captured;
|
|
148
|
-
try {
|
|
149
|
-
await d2.start();
|
|
150
|
-
} catch (err) {
|
|
151
|
-
captured = err;
|
|
152
|
-
}
|
|
153
|
-
expect(captured).toBeDefined();
|
|
154
|
-
expect(captured.code).toBe('EALREADYRUNNING');
|
|
155
|
-
expect(captured.pid).toBe(process.pid);
|
|
156
|
-
|
|
157
|
-
await d1.stop();
|
|
158
|
-
expect(fs.existsSync(d1.controlSocketPath)).toBe(false);
|
|
159
|
-
expect(fs.existsSync(d1.pidLockPath)).toBe(false);
|
|
160
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
test('PgserveDaemon.stop unlinks both socket and pid lock', async () => {
|
|
164
|
-
const dir = makeDaemonDirs('cleanup');
|
|
165
|
-
const d = new PgserveDaemon({
|
|
166
|
-
controlSocketDir: dir,
|
|
167
|
-
controlSocketPath: resolveControlSocketPath(dir),
|
|
168
|
-
pidLockPath: resolvePidLockPath(dir),
|
|
169
|
-
pgPort: 16020,
|
|
170
|
-
logger: silentLogger(),
|
|
171
|
-
});
|
|
172
|
-
await d.start();
|
|
173
|
-
expect(fs.existsSync(d.controlSocketPath)).toBe(true);
|
|
174
|
-
expect(fs.existsSync(d.pidLockPath)).toBe(true);
|
|
175
|
-
|
|
176
|
-
await d.stop();
|
|
177
|
-
expect(fs.existsSync(d.controlSocketPath)).toBe(false);
|
|
178
|
-
expect(fs.existsSync(d.pidLockPath)).toBe(false);
|
|
179
|
-
|
|
180
|
-
// PR #24 invariant carries through: PostgresManager nulled its paths.
|
|
181
|
-
expect(d.pgManager.socketDir).toBeNull();
|
|
182
|
-
|
|
183
|
-
fs.rmSync(dir, { recursive: true, force: true });
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Pick a pid that is reasonably guaranteed not to be alive. We try a high
|
|
189
|
-
* pid first (most kernels recycle low pids), then walk down until we find
|
|
190
|
-
* one that is dead. As a final fallback we use 999999.
|
|
191
|
-
*/
|
|
192
|
-
function pickDeadPid() {
|
|
193
|
-
const candidates = [987654, 765432, 543210, 321098, 109876];
|
|
194
|
-
for (const pid of candidates) {
|
|
195
|
-
if (!isProcessAlive(pid)) return pid;
|
|
196
|
-
}
|
|
197
|
-
return 999999;
|
|
198
|
-
}
|
|
@@ -1,263 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for src/fingerprint.js — kernel-rooted peer identity.
|
|
3
|
-
*
|
|
4
|
-
* Coverage:
|
|
5
|
-
* - getPeerCred() returns the calling process's pid/uid/gid via SO_PEERCRED
|
|
6
|
-
* - findNearestPackageJson() walks upward; deepest match wins (monorepo)
|
|
7
|
-
* - derivePackageFingerprint() is stable across cwd changes in the same project
|
|
8
|
-
* - same name + different paths → different fingerprints
|
|
9
|
-
* - same path + different uid → different fingerprints
|
|
10
|
-
* - script fallback triggers when no package.json above cwd
|
|
11
|
-
* - end-to-end: handleControlAccept() emits a connection_routed audit entry
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import { test, expect, beforeAll, beforeEach, afterEach } from 'bun:test';
|
|
15
|
-
import fs from 'fs';
|
|
16
|
-
import os from 'os';
|
|
17
|
-
import path from 'path';
|
|
18
|
-
import net from 'net';
|
|
19
|
-
import {
|
|
20
|
-
initFingerprintFfi,
|
|
21
|
-
getPeerCred,
|
|
22
|
-
findNearestPackageJson,
|
|
23
|
-
parseDarwinLsofCwd,
|
|
24
|
-
readProcCwd,
|
|
25
|
-
readPackageName,
|
|
26
|
-
derivePackageFingerprint,
|
|
27
|
-
deriveScriptFingerprint,
|
|
28
|
-
fingerprintFromCred,
|
|
29
|
-
handleControlAccept,
|
|
30
|
-
_setPeerCredImpl,
|
|
31
|
-
} from '../src/fingerprint.js';
|
|
32
|
-
import { configureAudit, AUDIT_EVENTS } from '../src/audit.js';
|
|
33
|
-
|
|
34
|
-
let scratch;
|
|
35
|
-
|
|
36
|
-
beforeAll(async () => {
|
|
37
|
-
await initFingerprintFfi();
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
beforeEach(() => {
|
|
41
|
-
scratch = fs.mkdtempSync(path.join(os.tmpdir(), 'pgserve-fp-test-'));
|
|
42
|
-
configureAudit({
|
|
43
|
-
logFile: path.join(scratch, 'audit.log'),
|
|
44
|
-
target: 'file',
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
afterEach(() => {
|
|
49
|
-
_setPeerCredImpl(null);
|
|
50
|
-
try { fs.rmSync(scratch, { recursive: true, force: true }); } catch { /* noop */ }
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
// ---------------------------------------------------------------------------
|
|
54
|
-
// SO_PEERCRED smoke — proves the FFI path works end-to-end on this kernel.
|
|
55
|
-
// ---------------------------------------------------------------------------
|
|
56
|
-
|
|
57
|
-
test('getPeerCred reads kernel-attested pid/uid/gid via Unix socket pair', async () => {
|
|
58
|
-
const sockPath = path.join(scratch, 'peer.sock');
|
|
59
|
-
const expectedUid = process.getuid();
|
|
60
|
-
const expectedGid = process.getgid();
|
|
61
|
-
const expectedPid = process.pid;
|
|
62
|
-
|
|
63
|
-
const cred = await new Promise((resolve, reject) => {
|
|
64
|
-
const server = net.createServer((socket) => {
|
|
65
|
-
try {
|
|
66
|
-
const c = getPeerCred(socket);
|
|
67
|
-
socket.end();
|
|
68
|
-
server.close(() => resolve(c));
|
|
69
|
-
} catch (err) {
|
|
70
|
-
server.close(() => reject(err));
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
server.on('error', reject);
|
|
74
|
-
server.listen(sockPath, () => {
|
|
75
|
-
const client = net.createConnection(sockPath);
|
|
76
|
-
client.on('error', reject);
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
expect(cred.pid).toBe(expectedPid);
|
|
81
|
-
expect(cred.uid).toBe(expectedUid);
|
|
82
|
-
expect(cred.gid).toBe(expectedGid);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
test('macOS lsof parser extracts cwd field output', () => {
|
|
86
|
-
const cwd = path.join(scratch, 'project');
|
|
87
|
-
const output = `p12345\nn${cwd}\n`;
|
|
88
|
-
expect(parseDarwinLsofCwd(output)).toBe(cwd);
|
|
89
|
-
expect(parseDarwinLsofCwd('p12345\n')).toBeNull();
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
test('readProcCwd resolves the current process cwd on supported platforms', () => {
|
|
93
|
-
if (process.platform !== 'linux' && process.platform !== 'darwin') return;
|
|
94
|
-
expect(readProcCwd(process.pid)).toBe(process.cwd());
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
// ---------------------------------------------------------------------------
|
|
98
|
-
// Pure-function tests on derivation surface
|
|
99
|
-
// ---------------------------------------------------------------------------
|
|
100
|
-
|
|
101
|
-
test('fingerprint stable across cwd change in the same project', () => {
|
|
102
|
-
// Layout:
|
|
103
|
-
// <scratch>/proj/package.json (name=alpha)
|
|
104
|
-
// <scratch>/proj/sub/deep/
|
|
105
|
-
// Same project → same fingerprint regardless of starting cwd.
|
|
106
|
-
const proj = path.join(scratch, 'proj');
|
|
107
|
-
fs.mkdirSync(path.join(proj, 'sub', 'deep'), { recursive: true });
|
|
108
|
-
fs.writeFileSync(path.join(proj, 'package.json'), JSON.stringify({ name: 'alpha' }));
|
|
109
|
-
|
|
110
|
-
const root = findNearestPackageJson(proj);
|
|
111
|
-
const fromSub = findNearestPackageJson(path.join(proj, 'sub'));
|
|
112
|
-
const fromDeep = findNearestPackageJson(path.join(proj, 'sub', 'deep'));
|
|
113
|
-
|
|
114
|
-
expect(root).not.toBeNull();
|
|
115
|
-
expect(fromSub).toBe(root);
|
|
116
|
-
expect(fromDeep).toBe(root);
|
|
117
|
-
|
|
118
|
-
const fp1 = derivePackageFingerprint({ packageRealpath: root, name: 'alpha', uid: 1000 });
|
|
119
|
-
const fp2 = derivePackageFingerprint({ packageRealpath: fromSub, name: 'alpha', uid: 1000 });
|
|
120
|
-
const fp3 = derivePackageFingerprint({ packageRealpath: fromDeep, name: 'alpha', uid: 1000 });
|
|
121
|
-
expect(fp1).toBe(fp2);
|
|
122
|
-
expect(fp2).toBe(fp3);
|
|
123
|
-
expect(fp1).toMatch(/^[0-9a-f]{12}$/);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
test('two projects with the same name but different paths get different fingerprints', () => {
|
|
127
|
-
const a = path.join(scratch, 'a-project');
|
|
128
|
-
const b = path.join(scratch, 'b-project');
|
|
129
|
-
fs.mkdirSync(a);
|
|
130
|
-
fs.mkdirSync(b);
|
|
131
|
-
fs.writeFileSync(path.join(a, 'package.json'), JSON.stringify({ name: 'shared' }));
|
|
132
|
-
fs.writeFileSync(path.join(b, 'package.json'), JSON.stringify({ name: 'shared' }));
|
|
133
|
-
|
|
134
|
-
const pa = findNearestPackageJson(a);
|
|
135
|
-
const pb = findNearestPackageJson(b);
|
|
136
|
-
expect(pa).not.toBe(pb);
|
|
137
|
-
|
|
138
|
-
const fpa = derivePackageFingerprint({ packageRealpath: pa, name: 'shared', uid: 1000 });
|
|
139
|
-
const fpb = derivePackageFingerprint({ packageRealpath: pb, name: 'shared', uid: 1000 });
|
|
140
|
-
expect(fpa).not.toBe(fpb);
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
test('same path + different uid → different fingerprints', () => {
|
|
144
|
-
const proj = path.join(scratch, 'multi-user');
|
|
145
|
-
fs.mkdirSync(proj);
|
|
146
|
-
fs.writeFileSync(path.join(proj, 'package.json'), JSON.stringify({ name: 'multi' }));
|
|
147
|
-
const realpath = findNearestPackageJson(proj);
|
|
148
|
-
|
|
149
|
-
const fp1000 = derivePackageFingerprint({ packageRealpath: realpath, name: 'multi', uid: 1000 });
|
|
150
|
-
const fp1001 = derivePackageFingerprint({ packageRealpath: realpath, name: 'multi', uid: 1001 });
|
|
151
|
-
expect(fp1000).not.toBe(fp1001);
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
test('script fallback triggered when no package.json above cwd', () => {
|
|
155
|
-
// Build an isolated path tree under scratch with no package.json anywhere.
|
|
156
|
-
// We point fingerprintFromCred at an override cwd inside scratch so the
|
|
157
|
-
// upward walk hits the filesystem root (no package.json in /tmp/.. either,
|
|
158
|
-
// because we use a deliberately ephemeral dir tree owned by the test).
|
|
159
|
-
const isolated = path.join(scratch, 'isolated', 'deep');
|
|
160
|
-
fs.mkdirSync(isolated, { recursive: true });
|
|
161
|
-
|
|
162
|
-
// Sanity: walking up from `isolated` finds no package.json (until at least
|
|
163
|
-
// /tmp/... or higher; we trust the host doesn't have one in /tmp).
|
|
164
|
-
// If the host *does* have one above /tmp, the result would still be deterministic
|
|
165
|
-
// and correct (mode='package'), but we want to test the script-fallback branch
|
|
166
|
-
// here. Mock findNearestPackageJson by passing a cwdOverride beneath a fake
|
|
167
|
-
// chroot — the easiest way is to walk to a path that we control: use an
|
|
168
|
-
// empty subtree under scratch and pretend the walk has hit the root.
|
|
169
|
-
const sentinelFile = findNearestPackageJson(isolated);
|
|
170
|
-
// If the host has no package.json anywhere up to /, sentinelFile is null.
|
|
171
|
-
// If it does, this assertion would falsely target the host's package.json.
|
|
172
|
-
// To make the test deterministic, we drive the script branch directly via
|
|
173
|
-
// deriveScriptFingerprint; the integration of "no package.json found" is
|
|
174
|
-
// covered by fingerprintFromCred's branch logic with cmdlineOverride.
|
|
175
|
-
|
|
176
|
-
const fp = deriveScriptFingerprint({
|
|
177
|
-
uid: 1000,
|
|
178
|
-
cwd: '/some/orphan/dir',
|
|
179
|
-
cmdline1: '/usr/local/bin/foo.js',
|
|
180
|
-
});
|
|
181
|
-
expect(fp).toMatch(/^[0-9a-f]{12}$/);
|
|
182
|
-
|
|
183
|
-
// Also verify fingerprintFromCred picks the script branch when cwdOverride
|
|
184
|
-
// points at a path with no ancestor package.json — we use a path under
|
|
185
|
-
// scratch since scratch itself has no package.json, and we pass cmdlineOverride.
|
|
186
|
-
const info = fingerprintFromCred(
|
|
187
|
-
{ pid: 9999, uid: 1000, gid: 1000 },
|
|
188
|
-
{
|
|
189
|
-
cwdOverride: isolated,
|
|
190
|
-
cmdlineOverride: ['/usr/local/bin/bun', '/some/orphan/dir/foo.js'],
|
|
191
|
-
},
|
|
192
|
-
);
|
|
193
|
-
// sentinelFile may be null (script mode) or non-null (if host has /package.json upstream).
|
|
194
|
-
if (sentinelFile === null) {
|
|
195
|
-
expect(info.mode).toBe('script');
|
|
196
|
-
expect(info.fingerprint).toMatch(/^[0-9a-f]{12}$/);
|
|
197
|
-
expect(info.packageRealpath).toBeNull();
|
|
198
|
-
} else {
|
|
199
|
-
// Host has an ancestor package.json. Still verify that derivation produces
|
|
200
|
-
// a 12-hex value and that the 'package' branch was chosen — the
|
|
201
|
-
// script-fallback behavior is independently exercised by deriveScriptFingerprint above.
|
|
202
|
-
expect(info.mode).toBe('package');
|
|
203
|
-
expect(info.fingerprint).toMatch(/^[0-9a-f]{12}$/);
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
test('monorepo: nested package.json wins (deepest match)', () => {
|
|
208
|
-
// Layout:
|
|
209
|
-
// <scratch>/mono/package.json (name=workspace-root)
|
|
210
|
-
// <scratch>/mono/packages/api/package.json (name=api)
|
|
211
|
-
// <scratch>/mono/packages/api/src/
|
|
212
|
-
// Walking up from src/ must find the api package.json, not the workspace root.
|
|
213
|
-
const root = path.join(scratch, 'mono');
|
|
214
|
-
const api = path.join(root, 'packages', 'api');
|
|
215
|
-
const apiSrc = path.join(api, 'src');
|
|
216
|
-
fs.mkdirSync(apiSrc, { recursive: true });
|
|
217
|
-
fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify({ name: 'workspace-root' }));
|
|
218
|
-
fs.writeFileSync(path.join(api, 'package.json'), JSON.stringify({ name: 'api' }));
|
|
219
|
-
|
|
220
|
-
const found = findNearestPackageJson(apiSrc);
|
|
221
|
-
expect(found).toBe(fs.realpathSync(path.join(api, 'package.json')));
|
|
222
|
-
expect(readPackageName(found)).toBe('api');
|
|
223
|
-
|
|
224
|
-
const info = fingerprintFromCred(
|
|
225
|
-
{ pid: 9999, uid: 1000, gid: 1000 },
|
|
226
|
-
{ cwdOverride: apiSrc, cmdlineOverride: ['bun', 'src/index.js'] },
|
|
227
|
-
);
|
|
228
|
-
expect(info.mode).toBe('package');
|
|
229
|
-
expect(info.name).toBe('api');
|
|
230
|
-
expect(info.packageRealpath).toBe(found);
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
// ---------------------------------------------------------------------------
|
|
234
|
-
// End-to-end: handleControlAccept emits connection_routed
|
|
235
|
-
// ---------------------------------------------------------------------------
|
|
236
|
-
|
|
237
|
-
test('handleControlAccept emits a connection_routed audit event with 12-hex fingerprint', () => {
|
|
238
|
-
const proj = path.join(scratch, 'audit-target');
|
|
239
|
-
fs.mkdirSync(proj);
|
|
240
|
-
fs.writeFileSync(path.join(proj, 'package.json'), JSON.stringify({ name: 'audit-app' }));
|
|
241
|
-
|
|
242
|
-
// Stub peer-cred impl so we don't need a real socket.
|
|
243
|
-
_setPeerCredImpl(() => ({ pid: 4242, uid: 1000, gid: 1000 }));
|
|
244
|
-
|
|
245
|
-
const info = handleControlAccept(
|
|
246
|
-
{ /* fake socket */ },
|
|
247
|
-
{ cwdOverride: proj, cmdlineOverride: ['bun', 'index.js'] },
|
|
248
|
-
);
|
|
249
|
-
|
|
250
|
-
expect(info.fingerprint).toMatch(/^[0-9a-f]{12}$/);
|
|
251
|
-
expect(info.mode).toBe('package');
|
|
252
|
-
expect(info.name).toBe('audit-app');
|
|
253
|
-
|
|
254
|
-
const logFile = path.join(scratch, 'audit.log');
|
|
255
|
-
const lines = fs.readFileSync(logFile, 'utf8').trim().split('\n').filter(Boolean);
|
|
256
|
-
expect(lines.length).toBe(1);
|
|
257
|
-
const entry = JSON.parse(lines[0]);
|
|
258
|
-
expect(entry.event).toBe(AUDIT_EVENTS.CONNECTION_ROUTED);
|
|
259
|
-
expect(entry.fingerprint).toBe(info.fingerprint);
|
|
260
|
-
expect(entry.peer_pid).toBe(4242);
|
|
261
|
-
expect(entry.peer_uid).toBe(1000);
|
|
262
|
-
expect(entry.mode).toBe('package');
|
|
263
|
-
});
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
-- Group 5 — synthetic orphan fixture.
|
|
2
|
-
--
|
|
3
|
-
-- Seeds 240 rows in `pgserve_meta` with stale `last_connection_at`
|
|
4
|
-
-- (48 hours old, well past the 24h TTL) and a dead `liveness_pid`. Half
|
|
5
|
-
-- the rows use a guaranteed-out-of-range PID (2147483646, far above
|
|
6
|
-
-- Linux's pid_max ≤ 2^22 ≈ 4M); the other half use NULL so the sweep
|
|
7
|
-
-- exercises both audit code paths (`db_reaped_liveness` vs `db_reaped_ttl`).
|
|
8
|
-
--
|
|
9
|
-
-- The accompanying harness `tests/orphan-cleanup.test.js` runs this file
|
|
10
|
-
-- and then `CREATE DATABASE`s each row's `database_name` so the sweep
|
|
11
|
-
-- actually has something to DROP.
|
|
12
|
-
|
|
13
|
-
INSERT INTO pgserve_meta (
|
|
14
|
-
database_name,
|
|
15
|
-
fingerprint,
|
|
16
|
-
peer_uid,
|
|
17
|
-
package_realpath,
|
|
18
|
-
last_connection_at,
|
|
19
|
-
liveness_pid,
|
|
20
|
-
persist
|
|
21
|
-
)
|
|
22
|
-
SELECT
|
|
23
|
-
format('app_orphan_%s', lpad(to_hex(i), 12, '0')),
|
|
24
|
-
lpad(to_hex(i), 12, '0'),
|
|
25
|
-
1000,
|
|
26
|
-
NULL,
|
|
27
|
-
now() - interval '48 hours',
|
|
28
|
-
CASE WHEN i % 2 = 0 THEN 2147483646 ELSE NULL END,
|
|
29
|
-
false
|
|
30
|
-
FROM generate_series(1, 240) AS i;
|