Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): wait for Docker schema, fix escape-hatch env knob

Adam Eivyd94cbd5f693c
diff --git a/scripts/setup-db.js b/scripts/setup-db.js
index 5b0d29bea8..6c660264c6 100644
--- a/scripts/setup-db.js
+++ b/scripts/setup-db.js
@@ -130,7 +130,30 @@ function isPortOSDbReady(port = PG_PORT_NATIVE) {
}
}
-// Wait for PostgreSQL to accept connections
+// Whether the Docker container has finished applying init-db.sql. pg_isready
+// only proves the server accepts connections — it can pass while the
+// docker-entrypoint-initdb.d schema load is still running, so the `memories`
+// table (and the rest of the required schema) may not exist yet. Probing the
+// schema inside the container avoids reporting "ready" to a server that would
+// then fail-fast on its own boot-time schema gate. `psql -tAc` exits 0 even on
+// an empty result, so we require the literal "1".
+function isDockerSchemaReady() {
+ try {
+ const output = execFileSync(
+ 'docker',
+ ['compose', 'exec', '-T', 'db', 'psql', '-X', '-U', 'portos', '-d', 'portos', '-tAc',
+ "SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'memories' LIMIT 1"],
+ { stdio: 'pipe', cwd: rootDir }
+ ).toString();
+ return output.trim() === '1';
+ } catch {
+ return false;
+ }
+}
+
+// Wait for PostgreSQL to accept connections AND for the required schema to be
+// in place. Both must hold before we report success, since PortOS now
+// fail-fasts at boot when the schema is missing.
function waitForHealth(maxAttempts = 30) {
for (let i = 0; i < maxAttempts; i++) {
try {
@@ -138,11 +161,12 @@ function waitForHealth(maxAttempts = 30) {
stdio: 'pipe',
cwd: rootDir
});
- return true;
+ if (isDockerSchemaReady()) return true;
} catch {
- if (i < maxAttempts - 1) {
- Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 1000);
- }
+ // not accepting connections yet — fall through to the wait below
+ }
+ if (i < maxAttempts - 1) {
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 1000);
}
}
return false;
diff --git a/server/index.js b/server/index.js
index af5f3d815b..75f778483c 100644
--- a/server/index.js
+++ b/server/index.js
@@ -799,7 +799,7 @@ ensureSelf()
const reason = health.connected ? 'required schema missing' : `unreachable (${health.error || 'connection failed'})`;
console.error(`❌ PostgreSQL is required but ${reason} — refusing to start.`);
console.error(' Set up the database with: npm run setup:db');
- console.error(' Dev/test only: set MEMORY_BACKEND=file in .env to boot without PostgreSQL (unsupported for production).');
+ console.error(' Dev/test only: set PGMODE=file in .env to boot without PostgreSQL (unsupported for production).');
process.exit(1);
}
if (dbEscapeHatch && !dbReady) {
diff --git a/server/services/memoryBackend.js b/server/services/memoryBackend.js
index c4e45cb55f..f622f34f8d 100644
--- a/server/services/memoryBackend.js
+++ b/server/services/memoryBackend.js
@@ -76,7 +76,7 @@ async function getBackend() {
// (server/index.js) fails fast on the same condition.
console.error(`❌ Memory backend unavailable: PostgreSQL is required but ${health.connected ? 'the schema is missing' : `unreachable (${health.error || 'connection failed'})`}`);
console.error(' PostgreSQL is a mandatory dependency for PortOS. Set it up with: npm run setup:db');
- console.error(' Dev/test only: set MEMORY_BACKEND=file in .env to use the (unsupported) file backend.');
+ console.error(' Dev/test only: set PGMODE=file in .env (the launcher maps it to MEMORY_BACKEND=file) to use the (unsupported) file backend.');
throw new Error('PostgreSQL is required for the memory backend — run `npm run setup:db`');
}