1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! Stage 1 embedded-postgres bootstrap.
//!
//! On CLI startup, ensures a postmaster is alive for the current
//! `<config_base_dir>/db/` data dir. Subsequent invocations (or
//! concurrent siblings) see a live postmaster and return
//! immediately.
//!
//! - **Transport**: TCP loopback only, on every platform. Port
//! is OS-assigned via `Settings::port = 0` — the
//! `postgresql_embedded` crate's `start()` impl binds an
//! ephemeral free port via `TcpListener::bind(("0.0.0.0", 0))`
//! internally and hands the resolved port to `pg_ctl`.
//! Postgres writes the chosen port to
//! `<data_dir>/postmaster.pid` line 4 on every successful
//! start (format documented and stable across platforms);
//! every subsequent CLI invocation reads from there to
//! discover the live port.
//!
//! Unix domain sockets used to be the Unix transport here.
//! They were dropped to collapse the platform split: one
//! transport everywhere, no `#[cfg]`-gated probe paths, no
//! `socket_dir` setting that `postgresql_embedded` silently
//! ignores on Windows, no `tokio::net::UnixStream`
//! `#[cfg(unix)]` carve-out on the client side.
//!
//! - **Filesystem sandboxing**: every byte the cli writes lives
//! under `<config_base_dir>/`. The postgres binaries extract
//! to `<config_base_dir>/db-bin/` (NOT the crate's default
//! `~/.theseus/postgresql/`); the cluster data lives in
//! `<config_base_dir>/db/`; the password file at
//! `<config_base_dir>/.pgpass`; the bootstrap lock at
//! `<config_base_dir>/db.lock`. Two cli invocations against
//! distinct `config_base_dir`s share no on-disk state and
//! never contend for the bootstrap lock. The `bundled`
//! archive ships in our binary, so the extract is purely
//! local — no network, no shared cache.
//!
//! - **Lock**: [`crate::lock_file`] — the same OS-level claim-
//! file primitive `agents::message` / `agent_registry` use
//! (`CreateFileW + FILE_FLAG_DELETE_ON_CLOSE` on Windows,
//! `O_CREAT|O_EXCL + flock(LOCK_EX)` on Unix). File presence
//! ⇔ live owner; OS handles cleanup on process death. Used
//! only as a bootstrap mutex to serialize concurrent first
//! spawns against the same `data_dir`; after a postmaster is
//! alive every subsequent cli skips the lock entirely.
//!
//! - **Lifetime**: `std::mem::forget` the handle after `start()`
//! so the crate's `Drop` impl never calls `pg_ctl stop`.
//! `pg_ctl` itself daemonizes the postmaster (double-fork on
//! Unix → reparented to init; detached process on Windows),
//! so the postmaster outlives every CLI process — every
//! subsequent invocation reads `postmaster.pid`, finds the
//! listening port, and reconnects.
//!
//! Stage 1 does not connect to postgres, create databases, or
//! run migrations. It only ensures the postmaster is alive.
use ;
use Duration;
use crateError;
const PROBE_TIMEOUT: Duration = from_millis;
/// Read line 4 of `<data_dir>/postmaster.pid`. Postgres writes
/// this file on every successful start; format is documented
/// and stable across platforms:
///
/// ```text
/// Line 1: PID
/// Line 2: Data directory absolute path
/// Line 3: Postmaster start time (epoch)
/// Line 4: Port number
/// Line 5: Socket directory (empty if none)
/// Line 6: First listen_address (empty if listen_addresses='')
/// Line 7: Shared memory key (or 0)
/// Line 8: Postmaster status flag
/// ```
///
/// Returns `None` when the file is missing (postmaster never
/// started, or shut down cleanly), unreadable, or the port line
/// isn't parseable.
async
/// Look up the port the postmaster for `data_dir` is listening
/// on. `None` ⇒ no postmaster.pid (or the file's malformed).
/// Used by [`crate::db::init`] to build the connection URL —
/// the bootstrap ran first inside `Context::new`, so by the
/// time `init` reads this the file is in place.
pub async
/// Probe the postgres transport; if not alive, single-flight-
/// spawn postgres and detach it. Returns `Ok` once a postmaster
/// is reachable. Everything stays inside `config_base_dir`:
/// install at `db-bin/`, cluster data at `db/`, password file
/// at `.pgpass`, bootstrap lock at `db.lock`.
pub async
/// `true` if `postmaster.pid` carries a port and a TCP probe to
/// `127.0.0.1:<port>` succeeds within `PROBE_TIMEOUT`. Same
/// code path on every platform.
async
/// Hard-coded password the cluster is initdb'd with and that every
/// CLI invocation uses to authenticate. The embedded postgres
/// listens on TCP loopback only, so this isn't security — just
/// deterministic auth that sidesteps the `postgresql_embedded`
/// crate's per-`Settings::default()` random password mint (which
/// we'd have to parse from a file to recover on subsequent
/// starts). With a fixed value, `db::init` can build the
/// connection URL with the password baked in and the cluster's
/// default `pg_hba.conf` works untouched.
pub const PG_INITDB_PASSWORD: &str = "objectiveai";
async