@pdlc-os/pdlc 0.1.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.
@@ -0,0 +1,88 @@
1
+ (function() {
2
+ const WS_URL = 'ws://' + window.location.host;
3
+ let ws = null;
4
+ let eventQueue = [];
5
+
6
+ function connect() {
7
+ ws = new WebSocket(WS_URL);
8
+
9
+ ws.onopen = () => {
10
+ eventQueue.forEach(e => ws.send(JSON.stringify(e)));
11
+ eventQueue = [];
12
+ };
13
+
14
+ ws.onmessage = (msg) => {
15
+ const data = JSON.parse(msg.data);
16
+ if (data.type === 'reload') {
17
+ window.location.reload();
18
+ }
19
+ };
20
+
21
+ ws.onclose = () => {
22
+ setTimeout(connect, 1000);
23
+ };
24
+ }
25
+
26
+ function sendEvent(event) {
27
+ event.timestamp = Date.now();
28
+ if (ws && ws.readyState === WebSocket.OPEN) {
29
+ ws.send(JSON.stringify(event));
30
+ } else {
31
+ eventQueue.push(event);
32
+ }
33
+ }
34
+
35
+ // Capture clicks on choice elements
36
+ document.addEventListener('click', (e) => {
37
+ const target = e.target.closest('[data-choice]');
38
+ if (!target) return;
39
+
40
+ sendEvent({
41
+ type: 'click',
42
+ text: target.textContent.trim(),
43
+ choice: target.dataset.choice,
44
+ id: target.id || null
45
+ });
46
+
47
+ // Update indicator bar (defer so toggleSelect runs first)
48
+ setTimeout(() => {
49
+ const indicator = document.getElementById('indicator-text');
50
+ if (!indicator) return;
51
+ const container = target.closest('.options') || target.closest('.cards');
52
+ const selected = container ? container.querySelectorAll('.selected') : [];
53
+ if (selected.length === 0) {
54
+ indicator.textContent = 'Click an option above, then return to the terminal';
55
+ } else if (selected.length === 1) {
56
+ const label = selected[0].querySelector('h3, .content h3, .card-body h3')?.textContent?.trim() || selected[0].dataset.choice;
57
+ indicator.innerHTML = '<span class="selected-text">' + label + ' selected</span> — return to terminal to continue';
58
+ } else {
59
+ indicator.innerHTML = '<span class="selected-text">' + selected.length + ' selected</span> — return to terminal to continue';
60
+ }
61
+ }, 0);
62
+ });
63
+
64
+ // Frame UI: selection tracking
65
+ window.selectedChoice = null;
66
+
67
+ window.toggleSelect = function(el) {
68
+ const container = el.closest('.options') || el.closest('.cards');
69
+ const multi = container && container.dataset.multiselect !== undefined;
70
+ if (container && !multi) {
71
+ container.querySelectorAll('.option, .card').forEach(o => o.classList.remove('selected'));
72
+ }
73
+ if (multi) {
74
+ el.classList.toggle('selected');
75
+ } else {
76
+ el.classList.add('selected');
77
+ }
78
+ window.selectedChoice = el.dataset.choice;
79
+ };
80
+
81
+ // Expose API for explicit use
82
+ window.pdlc = {
83
+ send: sendEvent,
84
+ choice: (value, metadata = {}) => sendEvent({ type: 'choice', value, ...metadata })
85
+ };
86
+
87
+ connect();
88
+ })();
@@ -0,0 +1,357 @@
1
+ const crypto = require('crypto');
2
+ const http = require('http');
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+
6
+ // ========== WebSocket Protocol (RFC 6455) ==========
7
+
8
+ const OPCODES = { TEXT: 0x01, CLOSE: 0x08, PING: 0x09, PONG: 0x0A };
9
+ const WS_MAGIC = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
10
+
11
+ function computeAcceptKey(clientKey) {
12
+ return crypto.createHash('sha1').update(clientKey + WS_MAGIC).digest('base64');
13
+ }
14
+
15
+ function encodeFrame(opcode, payload) {
16
+ const fin = 0x80;
17
+ const len = payload.length;
18
+ let header;
19
+
20
+ if (len < 126) {
21
+ header = Buffer.alloc(2);
22
+ header[0] = fin | opcode;
23
+ header[1] = len;
24
+ } else if (len < 65536) {
25
+ header = Buffer.alloc(4);
26
+ header[0] = fin | opcode;
27
+ header[1] = 126;
28
+ header.writeUInt16BE(len, 2);
29
+ } else {
30
+ header = Buffer.alloc(10);
31
+ header[0] = fin | opcode;
32
+ header[1] = 127;
33
+ header.writeBigUInt64BE(BigInt(len), 2);
34
+ }
35
+
36
+ return Buffer.concat([header, payload]);
37
+ }
38
+
39
+ function decodeFrame(buffer) {
40
+ if (buffer.length < 2) return null;
41
+
42
+ const secondByte = buffer[1];
43
+ const opcode = buffer[0] & 0x0F;
44
+ const masked = (secondByte & 0x80) !== 0;
45
+ let payloadLen = secondByte & 0x7F;
46
+ let offset = 2;
47
+
48
+ if (!masked) throw new Error('Client frames must be masked');
49
+
50
+ if (payloadLen === 126) {
51
+ if (buffer.length < 4) return null;
52
+ payloadLen = buffer.readUInt16BE(2);
53
+ offset = 4;
54
+ } else if (payloadLen === 127) {
55
+ if (buffer.length < 10) return null;
56
+ payloadLen = Number(buffer.readBigUInt64BE(2));
57
+ offset = 10;
58
+ }
59
+
60
+ const maskOffset = offset;
61
+ const dataOffset = offset + 4;
62
+ const totalLen = dataOffset + payloadLen;
63
+ if (buffer.length < totalLen) return null;
64
+
65
+ const mask = buffer.slice(maskOffset, dataOffset);
66
+ const data = Buffer.alloc(payloadLen);
67
+ for (let i = 0; i < payloadLen; i++) {
68
+ data[i] = buffer[dataOffset + i] ^ mask[i % 4];
69
+ }
70
+
71
+ return { opcode, payload: data, bytesConsumed: totalLen };
72
+ }
73
+
74
+ // ========== Configuration ==========
75
+
76
+ const PORT = process.env.PDLC_BRAINSTORM_PORT || (49152 + Math.floor(Math.random() * 16383));
77
+ const HOST = process.env.PDLC_HOST || '127.0.0.1';
78
+ const URL_HOST = process.env.PDLC_URL_HOST || (HOST === '127.0.0.1' ? 'localhost' : HOST);
79
+ const SESSION_DIR = process.env.PDLC_BRAINSTORM_DIR || '/tmp/pdlc-brainstorm';
80
+ const CONTENT_DIR = path.join(SESSION_DIR, 'content');
81
+ const STATE_DIR = path.join(SESSION_DIR, 'state');
82
+ let ownerPid = process.env.PDLC_OWNER_PID ? Number(process.env.PDLC_OWNER_PID) : null;
83
+
84
+ const MIME_TYPES = {
85
+ '.html': 'text/html', '.css': 'text/css', '.js': 'application/javascript',
86
+ '.json': 'application/json', '.png': 'image/png', '.jpg': 'image/jpeg',
87
+ '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.svg': 'image/svg+xml'
88
+ };
89
+
90
+ // ========== Templates and Constants ==========
91
+
92
+ const WAITING_PAGE = `<!DOCTYPE html>
93
+ <html>
94
+ <head><meta charset="utf-8"><title>PDLC Visual Companion</title>
95
+ <style>
96
+ body { font-family: system-ui, sans-serif; padding: 2rem; max-width: 800px; margin: 0 auto; background: #0d1b2a; color: #e2e8f0; }
97
+ h1 { color: #60a5fa; }
98
+ p { color: #94a3b8; }
99
+ </style>
100
+ </head>
101
+ <body><h1>PDLC Visual Companion</h1>
102
+ <p>Waiting for the agent to push a screen...</p></body></html>`;
103
+
104
+ const frameTemplate = fs.readFileSync(path.join(__dirname, 'frame-template.html'), 'utf-8');
105
+ const helperScript = fs.readFileSync(path.join(__dirname, 'helper.js'), 'utf-8');
106
+ const helperInjection = '<script>\n' + helperScript + '\n</script>';
107
+
108
+ // ========== Helper Functions ==========
109
+
110
+ function isFullDocument(html) {
111
+ const trimmed = html.trimStart().toLowerCase();
112
+ return trimmed.startsWith('<!doctype') || trimmed.startsWith('<html');
113
+ }
114
+
115
+ function wrapInFrame(content) {
116
+ return frameTemplate.replace('<!-- CONTENT -->', content);
117
+ }
118
+
119
+ function getNewestScreen() {
120
+ const files = fs.readdirSync(CONTENT_DIR)
121
+ .filter(f => f.endsWith('.html'))
122
+ .map(f => {
123
+ const fp = path.join(CONTENT_DIR, f);
124
+ return { path: fp, mtime: fs.statSync(fp).mtime.getTime() };
125
+ })
126
+ .sort((a, b) => b.mtime - a.mtime);
127
+ return files.length > 0 ? files[0].path : null;
128
+ }
129
+
130
+ // ========== HTTP Request Handler ==========
131
+
132
+ function handleRequest(req, res) {
133
+ touchActivity();
134
+ if (req.method === 'GET' && req.url === '/') {
135
+ const screenFile = getNewestScreen();
136
+ let html = screenFile
137
+ ? (raw => isFullDocument(raw) ? raw : wrapInFrame(raw))(fs.readFileSync(screenFile, 'utf-8'))
138
+ : WAITING_PAGE;
139
+
140
+ if (html.includes('</body>')) {
141
+ html = html.replace('</body>', helperInjection + '\n</body>');
142
+ } else {
143
+ html += helperInjection;
144
+ }
145
+
146
+ res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
147
+ res.end(html);
148
+ } else if (req.method === 'GET' && req.url.startsWith('/files/')) {
149
+ const fileName = req.url.slice(7);
150
+ const filePath = path.join(CONTENT_DIR, path.basename(fileName));
151
+ if (!fs.existsSync(filePath)) {
152
+ res.writeHead(404);
153
+ res.end('Not found');
154
+ return;
155
+ }
156
+ const ext = path.extname(filePath).toLowerCase();
157
+ const contentType = MIME_TYPES[ext] || 'application/octet-stream';
158
+ res.writeHead(200, { 'Content-Type': contentType });
159
+ res.end(fs.readFileSync(filePath));
160
+ } else {
161
+ res.writeHead(404);
162
+ res.end('Not found');
163
+ }
164
+ }
165
+
166
+ // ========== WebSocket Connection Handling ==========
167
+
168
+ const clients = new Set();
169
+
170
+ function handleUpgrade(req, socket) {
171
+ const key = req.headers['sec-websocket-key'];
172
+ if (!key) { socket.destroy(); return; }
173
+
174
+ const accept = computeAcceptKey(key);
175
+ socket.write(
176
+ 'HTTP/1.1 101 Switching Protocols\r\n' +
177
+ 'Upgrade: websocket\r\n' +
178
+ 'Connection: Upgrade\r\n' +
179
+ 'Sec-WebSocket-Accept: ' + accept + '\r\n\r\n'
180
+ );
181
+
182
+ let buffer = Buffer.alloc(0);
183
+ clients.add(socket);
184
+
185
+ socket.on('data', (chunk) => {
186
+ buffer = Buffer.concat([buffer, chunk]);
187
+ while (buffer.length > 0) {
188
+ let result;
189
+ try {
190
+ result = decodeFrame(buffer);
191
+ } catch (e) {
192
+ socket.end(encodeFrame(OPCODES.CLOSE, Buffer.alloc(0)));
193
+ clients.delete(socket);
194
+ return;
195
+ }
196
+ if (!result) break;
197
+ buffer = buffer.slice(result.bytesConsumed);
198
+
199
+ switch (result.opcode) {
200
+ case OPCODES.TEXT:
201
+ handleMessage(result.payload.toString());
202
+ break;
203
+ case OPCODES.CLOSE:
204
+ socket.end(encodeFrame(OPCODES.CLOSE, Buffer.alloc(0)));
205
+ clients.delete(socket);
206
+ return;
207
+ case OPCODES.PING:
208
+ socket.write(encodeFrame(OPCODES.PONG, result.payload));
209
+ break;
210
+ case OPCODES.PONG:
211
+ break;
212
+ default: {
213
+ const closeBuf = Buffer.alloc(2);
214
+ closeBuf.writeUInt16BE(1003);
215
+ socket.end(encodeFrame(OPCODES.CLOSE, closeBuf));
216
+ clients.delete(socket);
217
+ return;
218
+ }
219
+ }
220
+ }
221
+ });
222
+
223
+ socket.on('close', () => clients.delete(socket));
224
+ socket.on('error', () => clients.delete(socket));
225
+ }
226
+
227
+ function handleMessage(text) {
228
+ let event;
229
+ try {
230
+ event = JSON.parse(text);
231
+ } catch (e) {
232
+ console.error('Failed to parse WebSocket message:', e.message);
233
+ return;
234
+ }
235
+ touchActivity();
236
+ console.log(JSON.stringify({ source: 'user-event', ...event }));
237
+ if (event.choice) {
238
+ const eventsFile = path.join(STATE_DIR, 'events');
239
+ fs.appendFileSync(eventsFile, JSON.stringify(event) + '\n');
240
+ }
241
+ }
242
+
243
+ function broadcast(msg) {
244
+ const frame = encodeFrame(OPCODES.TEXT, Buffer.from(JSON.stringify(msg)));
245
+ for (const socket of clients) {
246
+ try { socket.write(frame); } catch (e) { clients.delete(socket); }
247
+ }
248
+ }
249
+
250
+ // ========== Activity Tracking ==========
251
+
252
+ const IDLE_TIMEOUT_MS = 30 * 60 * 1000; // 30 minutes
253
+ let lastActivity = Date.now();
254
+
255
+ function touchActivity() {
256
+ lastActivity = Date.now();
257
+ }
258
+
259
+ // ========== File Watching ==========
260
+
261
+ const debounceTimers = new Map();
262
+
263
+ // ========== Server Startup ==========
264
+
265
+ function startServer() {
266
+ if (!fs.existsSync(CONTENT_DIR)) fs.mkdirSync(CONTENT_DIR, { recursive: true });
267
+ if (!fs.existsSync(STATE_DIR)) fs.mkdirSync(STATE_DIR, { recursive: true });
268
+
269
+ // Track known files to distinguish new screens from updates.
270
+ // macOS fs.watch reports 'rename' for both new files and overwrites,
271
+ // so we can't rely on eventType alone.
272
+ const knownFiles = new Set(
273
+ fs.readdirSync(CONTENT_DIR).filter(f => f.endsWith('.html'))
274
+ );
275
+
276
+ const server = http.createServer(handleRequest);
277
+ server.on('upgrade', handleUpgrade);
278
+
279
+ const watcher = fs.watch(CONTENT_DIR, (eventType, filename) => {
280
+ if (!filename || !filename.endsWith('.html')) return;
281
+
282
+ if (debounceTimers.has(filename)) clearTimeout(debounceTimers.get(filename));
283
+ debounceTimers.set(filename, setTimeout(() => {
284
+ debounceTimers.delete(filename);
285
+ const filePath = path.join(CONTENT_DIR, filename);
286
+
287
+ if (!fs.existsSync(filePath)) return; // file was deleted
288
+ touchActivity();
289
+
290
+ if (!knownFiles.has(filename)) {
291
+ knownFiles.add(filename);
292
+ const eventsFile = path.join(STATE_DIR, 'events');
293
+ if (fs.existsSync(eventsFile)) fs.unlinkSync(eventsFile);
294
+ console.log(JSON.stringify({ type: 'screen-added', file: filePath }));
295
+ } else {
296
+ console.log(JSON.stringify({ type: 'screen-updated', file: filePath }));
297
+ }
298
+
299
+ broadcast({ type: 'reload' });
300
+ }, 100));
301
+ });
302
+ watcher.on('error', (err) => console.error('fs.watch error:', err.message));
303
+
304
+ function shutdown(reason) {
305
+ console.log(JSON.stringify({ type: 'server-stopped', reason }));
306
+ const infoFile = path.join(STATE_DIR, 'server-info');
307
+ if (fs.existsSync(infoFile)) fs.unlinkSync(infoFile);
308
+ fs.writeFileSync(
309
+ path.join(STATE_DIR, 'server-stopped'),
310
+ JSON.stringify({ reason, timestamp: Date.now() }) + '\n'
311
+ );
312
+ watcher.close();
313
+ clearInterval(lifecycleCheck);
314
+ server.close(() => process.exit(0));
315
+ }
316
+
317
+ function ownerAlive() {
318
+ if (!ownerPid) return true;
319
+ try { process.kill(ownerPid, 0); return true; } catch (e) { return e.code === 'EPERM'; }
320
+ }
321
+
322
+ // Check every 60s: exit if owner process died or idle for 30 minutes
323
+ const lifecycleCheck = setInterval(() => {
324
+ if (!ownerAlive()) shutdown('owner process exited');
325
+ else if (Date.now() - lastActivity > IDLE_TIMEOUT_MS) shutdown('idle timeout');
326
+ }, 60 * 1000);
327
+ lifecycleCheck.unref();
328
+
329
+ // Validate owner PID at startup. If it's already dead, the PID resolution
330
+ // was wrong (common on WSL, Tailscale SSH, and cross-user scenarios).
331
+ // Disable monitoring and rely on the idle timeout instead.
332
+ if (ownerPid) {
333
+ try { process.kill(ownerPid, 0); }
334
+ catch (e) {
335
+ if (e.code !== 'EPERM') {
336
+ console.log(JSON.stringify({ type: 'owner-pid-invalid', pid: ownerPid, reason: 'dead at startup' }));
337
+ ownerPid = null;
338
+ }
339
+ }
340
+ }
341
+
342
+ server.listen(PORT, HOST, () => {
343
+ const info = JSON.stringify({
344
+ type: 'server-started', port: Number(PORT), host: HOST,
345
+ url_host: URL_HOST, url: 'http://' + URL_HOST + ':' + PORT,
346
+ screen_dir: CONTENT_DIR, state_dir: STATE_DIR
347
+ });
348
+ console.log(info);
349
+ fs.writeFileSync(path.join(STATE_DIR, 'server-info'), info + '\n');
350
+ });
351
+ }
352
+
353
+ if (require.main === module) {
354
+ startServer();
355
+ }
356
+
357
+ module.exports = { computeAcceptKey, encodeFrame, decodeFrame, OPCODES };
@@ -0,0 +1,173 @@
1
+ #!/usr/bin/env bash
2
+ # Start the PDLC visual companion server and output connection info.
3
+ # Usage: start-server.sh [--project-dir <path>] [--feature <name>] [--host <bind-host>] [--url-host <display-host>] [--foreground] [--background]
4
+ #
5
+ # Starts server on a random high port, outputs JSON with URL.
6
+ # Each session gets its own directory under .pdlc/brainstorm/<session-id>/
7
+ #
8
+ # Options:
9
+ # --project-dir <path> Store session files under <path>/.pdlc/brainstorm/<feature>-<id>/
10
+ # instead of /tmp. Files persist after server stops.
11
+ # --feature <name> Name the session by feature (e.g. user-auth, checkout-flow).
12
+ # Used as a prefix in the session directory name.
13
+ # --host <bind-host> Host/interface to bind (default: 127.0.0.1).
14
+ # Use 0.0.0.0 in remote/containerized environments.
15
+ # --url-host <host> Hostname shown in returned URL JSON.
16
+ # --foreground Run server in the current terminal (no backgrounding).
17
+ # --background Force background mode (overrides Codex auto-foreground).
18
+
19
+ SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
20
+
21
+ # Parse arguments
22
+ PROJECT_DIR=""
23
+ FEATURE_NAME=""
24
+ FOREGROUND="false"
25
+ FORCE_BACKGROUND="false"
26
+ BIND_HOST="127.0.0.1"
27
+ URL_HOST=""
28
+
29
+ while [[ $# -gt 0 ]]; do
30
+ case "$1" in
31
+ --project-dir)
32
+ PROJECT_DIR="$2"
33
+ shift 2
34
+ ;;
35
+ --feature)
36
+ FEATURE_NAME="$2"
37
+ shift 2
38
+ ;;
39
+ --host)
40
+ BIND_HOST="$2"
41
+ shift 2
42
+ ;;
43
+ --url-host)
44
+ URL_HOST="$2"
45
+ shift 2
46
+ ;;
47
+ --foreground|--no-daemon)
48
+ FOREGROUND="true"
49
+ shift
50
+ ;;
51
+ --background|--daemon)
52
+ FORCE_BACKGROUND="true"
53
+ shift
54
+ ;;
55
+ *)
56
+ echo "{\"error\": \"Unknown argument: $1\"}"
57
+ exit 1
58
+ ;;
59
+ esac
60
+ done
61
+
62
+ if [[ -z "$URL_HOST" ]]; then
63
+ if [[ "$BIND_HOST" == "127.0.0.1" || "$BIND_HOST" == "localhost" ]]; then
64
+ URL_HOST="localhost"
65
+ else
66
+ URL_HOST="$BIND_HOST"
67
+ fi
68
+ fi
69
+
70
+ # Some environments reap detached/background processes. Auto-foreground when detected.
71
+ if [[ -n "${CODEX_CI:-}" && "$FOREGROUND" != "true" && "$FORCE_BACKGROUND" != "true" ]]; then
72
+ FOREGROUND="true"
73
+ fi
74
+
75
+ # Windows/Git Bash reaps nohup background processes. Auto-foreground when detected.
76
+ if [[ "$FOREGROUND" != "true" && "$FORCE_BACKGROUND" != "true" ]]; then
77
+ case "${OSTYPE:-}" in
78
+ msys*|cygwin*|mingw*) FOREGROUND="true" ;;
79
+ esac
80
+ if [[ -n "${MSYSTEM:-}" ]]; then
81
+ FOREGROUND="true"
82
+ fi
83
+ fi
84
+
85
+ # Generate unique session ID, optionally prefixed with feature name
86
+ SESSION_ID="$$-$(date +%s)"
87
+ if [[ -n "$FEATURE_NAME" ]]; then
88
+ # Sanitise feature name: lowercase, alphanumeric + hyphens only
89
+ SAFE_FEATURE="$(echo "$FEATURE_NAME" | tr '[:upper:]' '[:lower:]' | tr -cs '[:alnum:]-' '-' | sed 's/^-//;s/-$//')"
90
+ SESSION_SLUG="${SAFE_FEATURE}-${SESSION_ID}"
91
+ else
92
+ SESSION_SLUG="$SESSION_ID"
93
+ fi
94
+
95
+ if [[ -n "$PROJECT_DIR" ]]; then
96
+ SESSION_DIR="${PROJECT_DIR}/.pdlc/brainstorm/${SESSION_SLUG}"
97
+ else
98
+ SESSION_DIR="/tmp/pdlc-brainstorm-${SESSION_SLUG}"
99
+ fi
100
+
101
+ STATE_DIR="${SESSION_DIR}/state"
102
+ PID_FILE="${STATE_DIR}/server.pid"
103
+ LOG_FILE="${STATE_DIR}/server.log"
104
+
105
+ # Create fresh session directory with content and state peers
106
+ mkdir -p "${SESSION_DIR}/content" "$STATE_DIR"
107
+
108
+ # Kill any existing server for this session path (should be a fresh session, but be safe)
109
+ if [[ -f "$PID_FILE" ]]; then
110
+ old_pid=$(cat "$PID_FILE")
111
+ kill "$old_pid" 2>/dev/null
112
+ rm -f "$PID_FILE"
113
+ fi
114
+
115
+ cd "$SCRIPT_DIR"
116
+
117
+ # Resolve the harness PID (grandparent of this script).
118
+ # $PPID is the ephemeral shell the harness spawned to run us — it dies
119
+ # when this script exits. The harness itself is $PPID's parent.
120
+ OWNER_PID="$(ps -o ppid= -p "$PPID" 2>/dev/null | tr -d ' ')"
121
+ if [[ -z "$OWNER_PID" || "$OWNER_PID" == "1" ]]; then
122
+ OWNER_PID="$PPID"
123
+ fi
124
+
125
+ # Foreground mode for environments that reap detached/background processes.
126
+ if [[ "$FOREGROUND" == "true" ]]; then
127
+ echo "$$" > "$PID_FILE"
128
+ env \
129
+ PDLC_BRAINSTORM_DIR="$SESSION_DIR" \
130
+ PDLC_HOST="$BIND_HOST" \
131
+ PDLC_URL_HOST="$URL_HOST" \
132
+ PDLC_OWNER_PID="$OWNER_PID" \
133
+ node server.cjs
134
+ exit $?
135
+ fi
136
+
137
+ # Start server, capturing output to log file.
138
+ # Use nohup to survive shell exit; disown to remove from job table.
139
+ nohup env \
140
+ PDLC_BRAINSTORM_DIR="$SESSION_DIR" \
141
+ PDLC_HOST="$BIND_HOST" \
142
+ PDLC_URL_HOST="$URL_HOST" \
143
+ PDLC_OWNER_PID="$OWNER_PID" \
144
+ node server.cjs > "$LOG_FILE" 2>&1 &
145
+ SERVER_PID=$!
146
+ disown "$SERVER_PID" 2>/dev/null
147
+ echo "$SERVER_PID" > "$PID_FILE"
148
+
149
+ # Wait for server-started message (check log file)
150
+ for i in {1..50}; do
151
+ if grep -q "server-started" "$LOG_FILE" 2>/dev/null; then
152
+ # Verify server is still alive after a short window (catches process reapers)
153
+ alive="true"
154
+ for _ in {1..20}; do
155
+ if ! kill -0 "$SERVER_PID" 2>/dev/null; then
156
+ alive="false"
157
+ break
158
+ fi
159
+ sleep 0.1
160
+ done
161
+ if [[ "$alive" != "true" ]]; then
162
+ echo "{\"error\": \"Server started but was killed. Retry in a persistent terminal with: $SCRIPT_DIR/start-server.sh${PROJECT_DIR:+ --project-dir $PROJECT_DIR}${FEATURE_NAME:+ --feature $FEATURE_NAME} --host $BIND_HOST --url-host $URL_HOST --foreground\"}"
163
+ exit 1
164
+ fi
165
+ grep "server-started" "$LOG_FILE" | head -1
166
+ exit 0
167
+ fi
168
+ sleep 0.1
169
+ done
170
+
171
+ # Timeout — server didn't start in 5 seconds
172
+ echo '{"error": "Server failed to start within 5 seconds"}'
173
+ exit 1
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env bash
2
+ # Stop the PDLC visual companion server and clean up.
3
+ # Usage: stop-server.sh <session_dir>
4
+ #
5
+ # Kills the server process. Only deletes the session directory if it's
6
+ # under /tmp (ephemeral). Persistent directories (.pdlc/) are kept so
7
+ # generated mockups can be reviewed after the session ends.
8
+
9
+ SESSION_DIR="$1"
10
+
11
+ if [[ -z "$SESSION_DIR" ]]; then
12
+ echo '{"error": "Usage: stop-server.sh <session_dir>"}'
13
+ exit 1
14
+ fi
15
+
16
+ STATE_DIR="${SESSION_DIR}/state"
17
+ PID_FILE="${STATE_DIR}/server.pid"
18
+
19
+ if [[ -f "$PID_FILE" ]]; then
20
+ pid=$(cat "$PID_FILE")
21
+
22
+ # Try graceful shutdown first
23
+ kill "$pid" 2>/dev/null || true
24
+
25
+ # Wait for graceful exit (up to ~2s)
26
+ for i in {1..20}; do
27
+ if ! kill -0 "$pid" 2>/dev/null; then
28
+ break
29
+ fi
30
+ sleep 0.1
31
+ done
32
+
33
+ # Escalate to SIGKILL if still running
34
+ if kill -0 "$pid" 2>/dev/null; then
35
+ kill -9 "$pid" 2>/dev/null || true
36
+ sleep 0.1
37
+ fi
38
+
39
+ if kill -0 "$pid" 2>/dev/null; then
40
+ echo '{"status": "failed", "error": "process still running"}'
41
+ exit 1
42
+ fi
43
+
44
+ rm -f "$PID_FILE" "${STATE_DIR}/server.log"
45
+
46
+ # Only delete ephemeral /tmp directories; keep .pdlc/ session dirs
47
+ if [[ "$SESSION_DIR" == /tmp/* ]]; then
48
+ rm -rf "$SESSION_DIR"
49
+ fi
50
+
51
+ echo '{"status": "stopped"}'
52
+ else
53
+ echo '{"status": "not_running"}'
54
+ fi