glidercli 0.4.5 → 0.4.9

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.
@@ -1,91 +1,379 @@
1
1
  #!/bin/bash
2
2
 
3
- # Glider daemon - respawns relay forever with log rotation
4
-
5
-
3
+ # Glider daemon - respawns one relay forever with log rotation.
6
4
 
7
5
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
8
-
6
+ DAEMON_PATH="$SCRIPT_DIR/glider-daemon.sh"
9
7
  BSERVE="$SCRIPT_DIR/bserve.js"
8
+ LOG_DIR="${GLIDER_HOME:-$HOME/.glider}"
9
+ PORT="${GLIDER_PORT:-${RELAY_PORT:-19988}}"
10
+
11
+ case "$PORT" in
12
+ ''|*[!0-9]*|0|0*) echo "glider-daemon: invalid relay port: $PORT" >&2; exit 2 ;;
13
+ esac
14
+ if [ "$PORT" -gt 65535 ]; then
15
+ echo "glider-daemon: invalid relay port: $PORT" >&2
16
+ exit 2
17
+ fi
10
18
 
11
- LOG_DIR="$HOME/.glider"
12
-
13
- LOG_FILE="$LOG_DIR/daemon.log"
14
-
15
- PID_FILE="$LOG_DIR/daemon.pid"
16
-
17
- MAX_LOG_SIZE=10485760 # 10MB
19
+ PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:${PATH:-}"
20
+ export PATH
21
+ NODE_BIN="${GLIDER_NODE_BIN:-$(command -v node 2>/dev/null)}"
22
+ if [ -z "$NODE_BIN" ] || [ ! -x "$NODE_BIN" ]; then
23
+ echo "glider-daemon: node not found; set GLIDER_NODE_BIN to an executable Node.js 18+ binary" >&2
24
+ exit 127
25
+ fi
18
26
 
27
+ process_started_at() {
28
+ LC_ALL=C LANG=C TZ=UTC ps -p "$1" -o lstart= 2>/dev/null \
29
+ | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
30
+ }
19
31
 
32
+ LOG_FILE="$LOG_DIR/daemon.log"
33
+ PID_FILE="$LOG_DIR/daemon-$PORT.pid"
34
+ CLAIM_DIR="$LOG_DIR/daemon-$PORT.claim"
35
+ MANAGEMENT_LOCK="$LOG_DIR/daemon-$PORT.management.lock"
36
+ MAX_LOG_SIZE=10485760
37
+ child_pid=""
38
+ child_started=""
39
+ relay_ready=0
40
+ supervisor_started="$(process_started_at "$$")"
41
+ claim_token="$$-$RANDOM-$(date +%s)"
42
+ claim_owned=0
43
+ management_lock_owned=0
20
44
 
21
45
  mkdir -p "$LOG_DIR"
22
46
 
23
-
24
-
25
- # Rotate log if > 10MB
26
-
27
47
  rotate_log() {
28
-
29
- if [ -f "$LOG_FILE" ] && [ $(stat -f%z "$LOG_FILE" 2>/dev/null || echo 0) -gt $MAX_LOG_SIZE ]; then
30
-
31
- /bin/unlink "$LOG_FILE.3"
32
-
48
+ if [ -f "$LOG_FILE" ] && [ "$(stat -f%z "$LOG_FILE" 2>/dev/null || echo 0)" -gt "$MAX_LOG_SIZE" ]; then
49
+ /bin/unlink "$LOG_FILE.3" 2>/dev/null || true
33
50
  [ -f "$LOG_FILE.2" ] && mv "$LOG_FILE.2" "$LOG_FILE.3"
34
-
35
51
  [ -f "$LOG_FILE.1" ] && mv "$LOG_FILE.1" "$LOG_FILE.2"
36
-
37
52
  mv "$LOG_FILE" "$LOG_FILE.1"
38
-
39
53
  echo "[$(date)] Log rotated" > "$LOG_FILE"
40
-
41
54
  fi
42
-
43
55
  }
44
56
 
57
+ write_pid_record() {
58
+ "$NODE_BIN" -e '
59
+ const fs = require("fs");
60
+ const [file, pid, port, entry, started, childPid, childEntry, childStarted, ready] = process.argv.slice(1);
61
+ const record = {
62
+ schema: 1,
63
+ pid: Number(pid),
64
+ port: Number(port),
65
+ entry: fs.realpathSync(entry),
66
+ started: started.trim(),
67
+ childPid: childPid ? Number(childPid) : null,
68
+ childEntry: fs.realpathSync(childEntry),
69
+ childStarted: childStarted ? childStarted.trim() : null,
70
+ ready: ready === "1",
71
+ };
72
+ const temp = `${file}.${process.pid}.tmp`;
73
+ fs.writeFileSync(temp, `${JSON.stringify(record, null, 2)}\n`, { mode: 0o600 });
74
+ fs.renameSync(temp, file);
75
+ ' "$PID_FILE" "$$" "$PORT" "$DAEMON_PATH" "$supervisor_started" "$child_pid" "$BSERVE" "$child_started" "$relay_ready"
76
+ }
45
77
 
78
+ record_matches_supervisor() {
79
+ "$NODE_BIN" -e '
80
+ const fs = require("fs");
81
+ const { execFileSync } = require("child_process");
82
+ const [file, expectedPort, expectedEntry] = process.argv.slice(1);
83
+ try {
84
+ const record = JSON.parse(fs.readFileSync(file, "utf8"));
85
+ if (record.schema !== 1 || record.port !== Number(expectedPort)) process.exit(1);
86
+ if (record.entry !== fs.realpathSync(expectedEntry)) process.exit(1);
87
+ process.kill(record.pid, 0);
88
+ const identityEnv = { ...process.env, LC_ALL: "C", LANG: "C", TZ: "UTC" };
89
+ const command = execFileSync("ps", ["-p", String(record.pid), "-o", "command="], {
90
+ encoding: "utf8",
91
+ env: identityEnv,
92
+ }).trim();
93
+ const started = execFileSync("ps", ["-p", String(record.pid), "-o", "lstart="], {
94
+ encoding: "utf8",
95
+ env: identityEnv,
96
+ }).trim();
97
+ if (!command.includes(record.entry) || started !== record.started) process.exit(1);
98
+ process.stdout.write(String(record.pid));
99
+ } catch {
100
+ process.exit(1);
101
+ }
102
+ ' "$PID_FILE" "$PORT" "$DAEMON_PATH"
103
+ }
46
104
 
47
- # Kill any existing
48
-
49
- if [ -f "$PID_FILE" ]; then
50
-
51
- kill $(cat "$PID_FILE") 2>/dev/null
105
+ record_owned_by_self() {
106
+ "$NODE_BIN" -e '
107
+ const fs = require("fs");
108
+ const [file, pid] = process.argv.slice(1);
109
+ try {
110
+ const record = JSON.parse(fs.readFileSync(file, "utf8"));
111
+ process.exit(record.schema === 1 && record.pid === Number(pid) ? 0 : 1);
112
+ } catch {
113
+ process.exit(1);
114
+ }
115
+ ' "$PID_FILE" "$$"
116
+ }
52
117
 
53
- rm "$PID_FILE"
118
+ management_lock_is_inherited() {
119
+ [ -n "${GLIDER_DAEMON_MANAGEMENT_OWNER_PID:-}" ] || return 1
120
+ [ -n "${GLIDER_DAEMON_MANAGEMENT_OWNER_STARTED:-}" ] || return 1
121
+ [ "$GLIDER_DAEMON_MANAGEMENT_OWNER_PID" = "$PPID" ] || return 1
122
+ "$NODE_BIN" -e '
123
+ const fs = require("fs");
124
+ const { execFileSync } = require("child_process");
125
+ const [file, expectedPid, expectedStarted] = process.argv.slice(1);
126
+ try {
127
+ const record = JSON.parse(fs.readFileSync(file, "utf8"));
128
+ if (
129
+ record.schema !== 1
130
+ || record.pid !== Number(expectedPid)
131
+ || record.started !== expectedStarted
132
+ ) process.exit(1);
133
+ process.kill(record.pid, 0);
134
+ const started = execFileSync("ps", ["-p", String(record.pid), "-o", "lstart="], {
135
+ encoding: "utf8",
136
+ env: { ...process.env, LC_ALL: "C", LANG: "C", TZ: "UTC" },
137
+ }).trim();
138
+ process.exit(started === record.started ? 0 : 1);
139
+ } catch {
140
+ process.exit(1);
141
+ }
142
+ ' "$MANAGEMENT_LOCK" "$GLIDER_DAEMON_MANAGEMENT_OWNER_PID" "$GLIDER_DAEMON_MANAGEMENT_OWNER_STARTED"
143
+ }
54
144
 
55
- fi
145
+ acquire_management_lock() {
146
+ if management_lock_is_inherited; then
147
+ return 0
148
+ fi
149
+ "$NODE_BIN" -e '
150
+ const fs = require("fs");
151
+ const { execFileSync } = require("child_process");
152
+ const [file, pidValue, entry, started] = process.argv.slice(1);
153
+ const pid = Number(pidValue);
154
+ const record = { schema: 1, pid, entry: fs.realpathSync(entry), started };
155
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
156
+ const identityEnv = { ...process.env, LC_ALL: "C", LANG: "C", TZ: "UTC" };
157
+
158
+ async function main() {
159
+ const deadline = Date.now() + 7000;
160
+ while (Date.now() < deadline) {
161
+ try {
162
+ const fd = fs.openSync(file, "wx", 0o600);
163
+ try {
164
+ fs.writeFileSync(fd, `${JSON.stringify(record, null, 2)}\n`);
165
+ } finally {
166
+ fs.closeSync(fd);
167
+ }
168
+ return;
169
+ } catch (error) {
170
+ if (error.code !== "EEXIST") throw error;
171
+ }
172
+
173
+ let current = null;
174
+ let live = false;
175
+ try {
176
+ current = JSON.parse(fs.readFileSync(file, "utf8"));
177
+ process.kill(current.pid, 0);
178
+ const currentStarted = execFileSync(
179
+ "ps",
180
+ ["-p", String(current.pid), "-o", "lstart="],
181
+ { encoding: "utf8", env: identityEnv },
182
+ ).trim();
183
+ live = current.schema === 1 && current.started === currentStarted;
184
+ } catch {}
185
+
186
+ if (!live) {
187
+ let ageMs = Infinity;
188
+ try {
189
+ ageMs = Date.now() - fs.statSync(file).mtimeMs;
190
+ } catch (error) {
191
+ if (error.code === "ENOENT") continue;
192
+ throw error;
193
+ }
194
+ if (ageMs >= 2000) {
195
+ throw new Error(`Stale daemon management lock requires manual removal: ${file}`);
196
+ }
197
+ }
198
+ await sleep(100);
199
+ }
200
+ throw new Error(`Timed out waiting for daemon management lock: ${file}`);
201
+ }
202
+
203
+ main().catch((error) => {
204
+ console.error(`glider-daemon: ${error.message}`);
205
+ process.exit(1);
206
+ });
207
+ ' "$MANAGEMENT_LOCK" "$$" "$DAEMON_PATH" "$supervisor_started" || return $?
208
+ management_lock_owned=1
209
+ }
56
210
 
211
+ release_management_lock() {
212
+ if [ "$management_lock_owned" -ne 1 ]; then
213
+ return 0
214
+ fi
215
+ "$NODE_BIN" -e '
216
+ const fs = require("fs");
217
+ const [file, pidValue, started] = process.argv.slice(1);
218
+ try {
219
+ const record = JSON.parse(fs.readFileSync(file, "utf8"));
220
+ if (record.schema === 1 && record.pid === Number(pidValue) && record.started === started) {
221
+ fs.unlinkSync(file);
222
+ }
223
+ } catch {}
224
+ ' "$MANAGEMENT_LOCK" "$$" "$supervisor_started"
225
+ management_lock_owned=0
226
+ }
57
227
 
228
+ relay_is_running() {
229
+ "$NODE_BIN" -e '
230
+ const http = require("http");
231
+ const port = Number(process.argv[1]);
232
+ const request = http.get(
233
+ { host: "127.0.0.1", port, path: "/status", timeout: 500 },
234
+ (response) => {
235
+ response.resume();
236
+ process.exit(response.statusCode >= 200 && response.statusCode < 300 ? 0 : 1);
237
+ },
238
+ );
239
+ request.on("error", () => process.exit(1));
240
+ request.on("timeout", () => request.destroy());
241
+ ' "$PORT"
242
+ }
58
243
 
59
- echo $$ > "$PID_FILE"
244
+ relay_owned_by_child() {
245
+ "$NODE_BIN" -e '
246
+ const http = require("http");
247
+ const [portValue, childPidValue] = process.argv.slice(1);
248
+ const request = http.get(
249
+ { host: "127.0.0.1", port: Number(portValue), path: "/status", timeout: 500 },
250
+ (response) => {
251
+ let raw = "";
252
+ response.on("data", (chunk) => { raw += chunk; });
253
+ response.on("end", () => {
254
+ try {
255
+ const status = JSON.parse(raw);
256
+ process.exit(
257
+ response.statusCode >= 200
258
+ && response.statusCode < 300
259
+ && status.pid === Number(childPidValue)
260
+ ? 0
261
+ : 1,
262
+ );
263
+ } catch {
264
+ process.exit(1);
265
+ }
266
+ });
267
+ },
268
+ );
269
+ request.on("error", () => process.exit(1));
270
+ request.on("timeout", () => request.destroy());
271
+ ' "$PORT" "$child_pid"
272
+ }
60
273
 
274
+ acquire_claim() {
275
+ if mkdir "$CLAIM_DIR" 2>/dev/null; then
276
+ printf '%s\n' "$claim_token" > "$CLAIM_DIR/owner"
277
+ claim_owned=1
278
+ return 0
279
+ fi
280
+ i=0
281
+ while [ "$i" -lt 20 ]; do
282
+ existing_pid="$(record_matches_supervisor 2>/dev/null)" && {
283
+ echo "glider-daemon: already running as PID $existing_pid on port $PORT" >&2
284
+ exit 0
285
+ }
286
+ sleep 0.1
287
+ i=$((i + 1))
288
+ done
289
+ echo "glider-daemon: supervisor claim exists without a verified owner: $CLAIM_DIR" >&2
290
+ exit 1
291
+ }
61
292
 
293
+ release_claim() {
294
+ if [ "$claim_owned" -eq 1 ] && [ "$(cat "$CLAIM_DIR/owner" 2>/dev/null)" = "$claim_token" ]; then
295
+ rm -f "$CLAIM_DIR/owner"
296
+ rmdir "$CLAIM_DIR" 2>/dev/null || true
297
+ fi
298
+ }
62
299
 
63
300
  cleanup() {
301
+ status=$?
302
+ trap - EXIT SIGTERM SIGINT
303
+ if [ -n "$child_pid" ] && kill -0 "$child_pid" 2>/dev/null; then
304
+ kill "$child_pid" 2>/dev/null
305
+ wait "$child_pid" 2>/dev/null
306
+ fi
307
+ if [ -f "$PID_FILE" ] && record_owned_by_self; then
308
+ rm -f "$PID_FILE"
309
+ fi
310
+ release_claim
311
+ release_management_lock
312
+ exit "$status"
313
+ }
64
314
 
65
- /bin/unlink "$PID_FILE"
315
+ trap cleanup EXIT
316
+ trap 'exit 0' SIGTERM SIGINT
66
317
 
67
- exit 0
318
+ acquire_management_lock || exit $?
68
319
 
69
- }
320
+ if [ -f "$PID_FILE" ]; then
321
+ existing_pid="$(record_matches_supervisor 2>/dev/null)" && {
322
+ echo "glider-daemon: already running as PID $existing_pid on port $PORT" >&2
323
+ exit 0
324
+ }
325
+ fi
70
326
 
71
- trap cleanup SIGTERM SIGINT
327
+ if relay_is_running; then
328
+ echo "glider-daemon: relay port $PORT is already owned by another process" >&2
329
+ exit 1
330
+ fi
72
331
 
332
+ acquire_claim
73
333
 
334
+ if [ -f "$PID_FILE" ]; then
335
+ rm -f "$PID_FILE"
336
+ fi
74
337
 
75
- while true; do
338
+ write_pid_record
76
339
 
340
+ initial_handoff=1
341
+ while true; do
77
342
  rotate_log
343
+ echo "[$(date)] Starting relay on port $PORT..." >> "$LOG_FILE"
344
+
345
+ "$NODE_BIN" "$BSERVE" >> "$LOG_FILE" 2>&1 &
346
+ child_pid=$!
347
+ child_started="$(process_started_at "$child_pid")"
348
+ relay_ready=0
349
+ write_pid_record
350
+
351
+ handoff_attempt=0
352
+ while [ "$handoff_attempt" -lt 50 ]; do
353
+ relay_owned_by_child && break
354
+ kill -0 "$child_pid" 2>/dev/null || break
355
+ sleep 0.1
356
+ handoff_attempt=$((handoff_attempt + 1))
357
+ done
358
+ if relay_owned_by_child; then
359
+ relay_ready=1
360
+ write_pid_record
361
+ if [ "$initial_handoff" -eq 1 ]; then
362
+ initial_handoff=0
363
+ release_management_lock
364
+ fi
365
+ elif [ "$initial_handoff" -eq 1 ]; then
366
+ echo "glider-daemon: relay child failed to own port $PORT" >&2
367
+ exit 1
368
+ fi
78
369
 
79
- echo "[$(date)] Starting relay..." >> "$LOG_FILE"
80
-
81
- node "$BSERVE" >> "$LOG_FILE" 2>&1
82
-
370
+ wait "$child_pid"
83
371
  EXIT_CODE=$?
372
+ child_pid=""
373
+ child_started=""
374
+ relay_ready=0
375
+ write_pid_record
84
376
 
85
377
  echo "[$(date)] Relay exited with code $EXIT_CODE, restarting in 2s..." >> "$LOG_FILE"
86
-
87
378
  sleep 2
88
-
89
379
  done
90
-
91
-
package/lib/guard.js CHANGED
@@ -1,10 +1,12 @@
1
1
  'use strict';
2
2
 
3
3
  const fs = require('fs');
4
- const os = require('os');
5
4
  const path = require('path');
5
+ const { gliderHome } = require('./paths.js');
6
6
 
7
- const DOMAIN_CONFIG = path.join(os.homedir(), '.glider', 'config', 'allowed-domains.json');
7
+ function domainConfigPath() {
8
+ return path.join(gliderHome(), 'config', 'allowed-domains.json');
9
+ }
8
10
 
9
11
  function parseDomainList(raw) {
10
12
  if (!raw) return [];
@@ -15,9 +17,10 @@ function parseDomainList(raw) {
15
17
  }
16
18
 
17
19
  function loadConfigDomains() {
18
- if (!fs.existsSync(DOMAIN_CONFIG)) return [];
20
+ const configPath = domainConfigPath();
21
+ if (!fs.existsSync(configPath)) return [];
19
22
  try {
20
- const data = JSON.parse(fs.readFileSync(DOMAIN_CONFIG, 'utf8'));
23
+ const data = JSON.parse(fs.readFileSync(configPath, 'utf8'));
21
24
  return Array.isArray(data) ? data : data.allowed || data.domains || [];
22
25
  } catch {
23
26
  return [];
package/lib/paths.js CHANGED
@@ -3,7 +3,7 @@
3
3
  const os = require('os');
4
4
  const path = require('path');
5
5
 
6
- /** Browser runtime data — always ~/.glider unless GLIDER_HOME set. */
6
+ /** Browser runtime data - always ~/.glider unless GLIDER_HOME set. */
7
7
  function gliderHome() {
8
8
  return process.env.GLIDER_HOME || path.join(os.homedir(), '.glider');
9
9
  }
@@ -32,7 +32,7 @@ function configPath(name) {
32
32
  ];
33
33
  }
34
34
 
35
- /** Domain intel: $AGREGISTRY/warch or ~/.glider/warch when AGREGISTRY unset. */
35
+ /** Domain intel: AGREGISTRY/warch or ~/.glider/warch when AGREGISTRY unset. */
36
36
  function warchRoot() {
37
37
  return path.join(registryBase(), 'warch');
38
38
  }
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ function relayPort(env = process.env) {
4
+ const raw = env.GLIDER_PORT || env.RELAY_PORT || '19988';
5
+ const port = Number.parseInt(raw, 10);
6
+ if (!Number.isInteger(port) || port < 1 || port > 65535 || String(port) !== String(raw).trim()) {
7
+ throw new Error(`Invalid relay port "${raw}" (expected an integer from 1 to 65535)`);
8
+ }
9
+ return port;
10
+ }
11
+
12
+ function relayHttpUrl(env = process.env) {
13
+ return env.RELAY_HTTP || `http://127.0.0.1:${relayPort(env)}`;
14
+ }
15
+
16
+ function relayWebSocketUrl(env = process.env) {
17
+ return env.RELAY_URL || `ws://127.0.0.1:${relayPort(env)}/cdp`;
18
+ }
19
+
20
+ module.exports = {
21
+ relayHttpUrl,
22
+ relayPort,
23
+ relayWebSocketUrl,
24
+ };
package/package.json CHANGED
@@ -1,13 +1,19 @@
1
1
  {
2
2
  "name": "glidercli",
3
- "version": "0.4.5",
4
- "description": "Browser automation CLI. Control Chrome from terminal via CDP, run YAML task files, autonomous loops until completion.",
5
- "main": "index.js",
3
+ "version": "0.4.9",
4
+ "description": "Browser automation CLI. Control a configured Chromium browser through an extension CDP relay, run YAML tasks, and execute bounded autonomous loops.",
6
5
  "bin": {
7
6
  "glider": "bin/glider.js"
8
7
  },
9
8
  "scripts": {
10
- "test": "node bin/glider.js --help"
9
+ "test": "node --test tests/*.test.js",
10
+ "test:syntax": "node --check bin/glider.js && node --check lib/bserve.js",
11
+ "test:regression": "node --test tests/browser-config.test.js tests/core.test.js tests/cli-regression.test.js tests/relay-integration.test.js",
12
+ "test:integration": "node --test tests/relay-integration.test.js tests/daemon-lifecycle.test.js",
13
+ "test:pack": "node --test tests/npm-tarball.test.js",
14
+ "test:smoke": "node tests/live-browser.smoke.js",
15
+ "test:live": "node tests/live-browser.smoke.js",
16
+ "prepublishOnly": "npm run test:syntax && npm test"
11
17
  },
12
18
  "keywords": [
13
19
  "browser",
@@ -39,7 +45,6 @@
39
45
  "lib/",
40
46
  "docs/",
41
47
  "config/",
42
- "index.js",
43
48
  "README.md"
44
49
  ]
45
50
  }