Skip to content

Commit fd5fef8

Browse files
mogulOpenCode Agent
andcommitted
fix(paseo-kit): reconcile pre-boot daemon lock
Reconcile a live Paseo supervisor whose lock predates the current kernel boot during startup, then verify default local CLI discovery. Co-authored-by: OpenCode Agent <[email protected]>
1 parent 665373d commit fd5fef8

4 files changed

Lines changed: 236 additions & 1 deletion

File tree

‎integrations/isolation/acq-kits/paseo/docs/decisions/paseo-0.9-daemon-run.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ CLI restart cannot run.
4949
- The kit no longer passes removed Paseo 0.9 flags.
5050
- The daemon still comes up automatically on sandbox start and remains supervised.
5151
- `paseo daemon status` and config tooling report the same listen/web UI/relay
52-
settings that the sandbox uses at runtime.
52+
settings that the sandbox uses at runtime. On a restored or migrated sandbox,
53+
the lock can retain a `startedAt` timestamp from before the current kernel
54+
boot even while the daemon process tree remains healthy. During startup only,
55+
the kit detects that condition, verifies the lock PID is Paseo's own supervisor,
56+
and performs a bounded graceful stop so the outer supervisor can write a fresh
57+
lock. Ambiguous, malformed, post-boot, or unreconciled locks are left intact;
58+
the startup script logs the reason and does not start a second daemon.
5359
- The `0.0.0.0:6767` bind remains explicit, preserving msb create-time port
5460
reachability.
5561
- Future Paseo version bumps must verify both the version pin and daemon command

‎integrations/isolation/acq-kits/paseo/files/home/paseo-start.sh‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,138 @@
3838

3939
set -eu
4040

41+
# Reconcile a lock left behind by a sandbox restore/migration. This runs only in
42+
# this startup process, never from the daemon's respawn loop. A lock is eligible
43+
# only when its timestamp predates the current kernel boot and its PID is the
44+
# Paseo-owned supervisor-entrypoint, not merely any live process.
45+
reconcile_preboot_lock() {
46+
_lock="${PASEO_HOME_DIR:-${PASEO_HOME:-$HOME/.paseo}}/paseo.pid"
47+
_log="${STATE_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/paseo}/paseo-start.log"
48+
_proc_root=/proc
49+
_btime=""
50+
_pid=""
51+
_started=""
52+
_started_epoch=""
53+
54+
mkdir -p "$(dirname "$_log")" 2>/dev/null || true
55+
56+
[ -f "$_lock" ] || return 0
57+
_btime="$(awk '$1 == "btime" { print $2; exit }' "$_proc_root/stat" 2>/dev/null || true)"
58+
if [ "${PASEO_START_RECONCILE_TEST:-}" = 1 ]; then
59+
_proc_root="${PASEO_PROC_ROOT:-$_proc_root}"
60+
_btime="${PASEO_KERNEL_BOOT_TIME:-$_btime}"
61+
fi
62+
if ! case "$_btime" in ''|*[!0-9]*) false ;; *) true ;; esac; then
63+
echo "paseo: pre-boot lock check: kernel boot time unavailable; leaving $_lock untouched" >>"$_log" 2>/dev/null || true
64+
return 1
65+
fi
66+
67+
# Node parses the lock's JSON and normalizes ISO or numeric startedAt values.
68+
# It emits one tab-separated record and rejects extra/malformed fields.
69+
_lock_record="$(node -e '
70+
const fs = require("fs");
71+
try {
72+
const value = JSON.parse(fs.readFileSync(process.argv[1], "utf8"));
73+
const pid = Number(value.pid);
74+
const started = value.startedAt;
75+
const epoch = typeof started === "number" ? started : Date.parse(String(started)) / 1000;
76+
if (!Number.isInteger(pid) || pid <= 1 || !Number.isFinite(epoch)) process.exit(2);
77+
process.stdout.write(`${pid}\t${Math.floor(epoch)}\n`);
78+
} catch { process.exit(2); }
79+
' "$_lock" 2>/dev/null || true)"
80+
_tab="$(printf '\t')"
81+
_pid="${_lock_record%%$_tab*}"
82+
_started="${_lock_record#*$_tab}"
83+
case "$_pid" in
84+
''|*[!0-9]*)
85+
echo "paseo: pre-boot lock check: malformed or ambiguous lock $_lock; leaving it untouched" >>"$_log" 2>/dev/null || true
86+
return 1 ;;
87+
esac
88+
case "$_started" in
89+
''|*[!0-9]*)
90+
echo "paseo: pre-boot lock check: malformed or ambiguous lock $_lock; leaving it untouched" >>"$_log" 2>/dev/null || true
91+
return 1 ;;
92+
esac
93+
_started_epoch="$_started"
94+
if [ "$_started_epoch" -ge "$_btime" ] 2>/dev/null; then
95+
return 0
96+
fi
97+
98+
_cmdline="$(tr '\0' ' ' <"$_proc_root/$_pid/cmdline" 2>/dev/null || true)"
99+
_start_ticks="$(awk '{ print $22 }' "$_proc_root/$_pid/stat" 2>/dev/null || true)"
100+
_uid="$(awk '/^Uid:/ { print $2; exit }' "$_proc_root/$_pid/status" 2>/dev/null || true)"
101+
_self_uid="$(id -u)"
102+
case "$_cmdline" in
103+
*supervisor-entrypoint.js*)
104+
case "$_start_ticks" in
105+
''|*[!0-9]*)
106+
echo "paseo: pre-boot lock check: PID $_pid has unreadable process identity; leaving $_lock untouched" >>"$_log" 2>/dev/null || true
107+
return 1 ;;
108+
esac
109+
if [ "$_uid" != "$_self_uid" ]; then
110+
echo "paseo: pre-boot lock check: PID $_pid has unexpected owner; leaving $_lock untouched" >>"$_log" 2>/dev/null || true
111+
return 1
112+
fi ;;
113+
'')
114+
echo "paseo: pre-boot lock check: PID $_pid is not live; leaving $_lock for Paseo to reclaim" >>"$_log" 2>/dev/null || true
115+
return 0 ;;
116+
*)
117+
echo "paseo: pre-boot lock check: PID $_pid is live but is not the Paseo supervisor; leaving $_lock untouched" >>"$_log" 2>/dev/null || true
118+
return 1 ;;
119+
esac
120+
121+
_tree_pids="$_pid"
122+
_pending="$_pid"
123+
while [ -n "$_pending" ]; do
124+
_children=""
125+
for _parent in $_pending; do
126+
_children="$_children $(pgrep -P "$_parent" 2>/dev/null || true)"
127+
done
128+
_pending=""
129+
for _child in $_children; do
130+
case " $_tree_pids " in *" $_child "*) ;; *) _tree_pids="$_tree_pids $_child"; _pending="$_pending $_child" ;; esac
131+
done
132+
done
133+
134+
echo "paseo: pre-boot lock check: stopping restored Paseo supervisor PID $_pid" >>"$_log" 2>/dev/null || true
135+
_check_start_ticks="$(awk '{ print $22 }' "$_proc_root/$_pid/stat" 2>/dev/null || true)"
136+
_check_cmdline="$(tr '\0' ' ' <"$_proc_root/$_pid/cmdline" 2>/dev/null || true)"
137+
[ "$_check_start_ticks" = "$_start_ticks" ] && [ "$_check_cmdline" = "$_cmdline" ] || {
138+
echo "paseo: pre-boot lock check: supervisor identity changed before stop; leaving $_lock untouched" >>"$_log" 2>/dev/null || true
139+
return 1
140+
}
141+
kill -TERM "$_pid" 2>/dev/null || {
142+
echo "paseo: pre-boot lock check: could not stop PID $_pid; leaving $_lock untouched" >>"$_log" 2>/dev/null || true
143+
return 1
144+
}
145+
_deadline=$(( $(date +%s) + 20 ))
146+
while :; do
147+
_tree_live=""
148+
for _tree_pid in $_tree_pids; do
149+
[ -e "$_proc_root/$_tree_pid" ] && _tree_live=yes
150+
done
151+
[ -z "$_tree_live" ] && [ ! -f "$_lock" ] && {
152+
echo "paseo: pre-boot lock check: restored supervisor stopped and lock released" >>"$_log" 2>/dev/null || true
153+
return 0
154+
}
155+
if [ "$(date +%s)" -ge "$_deadline" ]; then
156+
echo "paseo: pre-boot lock check: timeout waiting for PID $_pid and $_lock; leaving lock untouched" >>"$_log" 2>/dev/null || true
157+
return 1
158+
fi
159+
sleep 1
160+
done
161+
}
162+
163+
# Test-only entry point used by the kit regression suite. It is deliberately
164+
# checked before installation and daemon startup, so it cannot affect a sandbox.
165+
if [ "${PASEO_START_RECONCILE_TEST:-}" = 1 ]; then
166+
PASEO_HOME_DIR="${PASEO_HOME:-$HOME/.paseo}"
167+
STATE_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/paseo"
168+
mkdir -p "$STATE_DIR"
169+
reconcile_preboot_lock
170+
exit $?
171+
fi
172+
41173
# Daemon bind. Keep in sync with the kit spec's publishedPorts guest port (6767)
42174
# and PASEO_LISTEN env. We derive the port from PASEO_LISTEN so a single override
43175
# moves both the bind and the health probe. The default is 0.0.0.0 (all guest
@@ -197,6 +329,11 @@ if [ -z "$config_ok" ]; then
197329
exit 0
198330
fi
199331

332+
if ! reconcile_preboot_lock; then
333+
echo "paseo: pre-boot lock reconciliation did not complete; not starting a second daemon" >&2
334+
exit 0
335+
fi
336+
200337
# --- Supervise the Paseo daemon (idempotent). --------------------------------
201338
# The daemon runs UNSECURED (no PASEO_PASSWORD) — the sandbox is the security
202339
# boundary, and the published host port is loopback only. `daemon run` is Paseo

‎integrations/isolation/acq-kits/paseo/scripts/verify‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,19 @@ else
395395
in_sbx 'tail -n 20 ~/.local/state/paseo/paseo-daemon.log' | sed 's/^/ | /'
396396
fi
397397

398+
info "5a. Default local CLI discovery uses the running daemon"
399+
local_status="$(in_sbx 'paseo daemon status --home "${PASEO_HOME:-$HOME/.paseo}" --json 2>/dev/null')"
400+
if printf '%s' "$local_status" | node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{try { const v=JSON.parse(s); const running=v.status === "running" || v.state === "running" || v.localDaemon?.status === "running" || v.localDaemon?.state === "running"; process.exit(running ? 0 : 1); } catch { process.exit(1); }})'; then
401+
ok "local daemon status reports running without an explicit host"
402+
else
403+
bad "local daemon status did not report running: ${local_status:-no output}"
404+
fi
405+
if [ "$(in_sbx 'paseo ls --home "${PASEO_HOME:-$HOME/.paseo}" --json >/dev/null 2>&1 && echo yes')" = "yes" ]; then
406+
ok "paseo ls succeeds through default local discovery without --host"
407+
else
408+
bad "paseo ls failed through default local discovery without --host"
409+
fi
410+
398411
config_assert="$(in_sbx "node /home/agent/paseo-assert-daemon-config.mjs --paseo-home \"\${PASEO_HOME:-\$HOME/.paseo}\" --listen '0.0.0.0:$PASEO_PORT' 2>&1 && echo ok")"
399412
if [ "$config_assert" = "ok" ]; then
400413
ok "config.json persists required daemon settings (listen, web UI, relay disabled)"
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import assert from "node:assert/strict";
2+
import { execFileSync, spawn } from "node:child_process";
3+
import { existsSync, mkdtempSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
4+
import { tmpdir } from "node:os";
5+
import { join } from "node:path";
6+
import { test } from "node:test";
7+
8+
const startup = join(import.meta.dirname, "../files/home/paseo-start.sh");
9+
10+
function runReconcile({ startedAt, commandLine, bootTime = 2_000 }) {
11+
const root = mkdtempSync(join(tmpdir(), "paseo-reconcile-"));
12+
const home = join(root, "home");
13+
const proc = join(root, "proc");
14+
const state = join(root, "state");
15+
mkdirSync(home);
16+
mkdirSync(proc);
17+
writeFileSync(join(proc, "stat"), `btime ${bootTime}\n`);
18+
19+
const supervisor = spawn("sh", ["-c", `trap 'rm -f "$1"; rm -rf "$FAKE_PROC_ROOT/$$"; exit 0' TERM INT; while :; do sleep 1; done`, "sh", join(home, "paseo.pid")], {
20+
env: { ...process.env, FAKE_PROC_ROOT: proc },
21+
stdio: "ignore",
22+
});
23+
mkdirSync(join(proc, String(supervisor.pid)));
24+
writeFileSync(join(proc, String(supervisor.pid), "cmdline"), `${commandLine}\0`);
25+
writeFileSync(join(proc, String(supervisor.pid), "stat"), `1 (sh) S 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 12345\n`);
26+
writeFileSync(join(proc, String(supervisor.pid), "status"), `Name:\tsh\nUid:\t${process.getuid()}\t${process.getuid()}\t${process.getuid()}\t${process.getuid()}\n`);
27+
writeFileSync(join(home, "paseo.pid"), JSON.stringify({ pid: supervisor.pid, startedAt }));
28+
29+
let result;
30+
try {
31+
result = execFileSync("sh", [startup], {
32+
env: {
33+
...process.env,
34+
HOME: home,
35+
PASEO_HOME: home,
36+
XDG_STATE_HOME: state,
37+
PASEO_PROC_ROOT: proc,
38+
PASEO_KERNEL_BOOT_TIME: String(bootTime),
39+
PASEO_START_RECONCILE_TEST: "1",
40+
},
41+
encoding: "utf8",
42+
stdio: "pipe",
43+
});
44+
} catch (error) {
45+
result = `${error.stdout ?? ""}${error.stderr ?? ""}`;
46+
}
47+
supervisor.kill("SIGKILL");
48+
const logPath = join(state, "paseo", "paseo-start.log");
49+
return { home, log: existsSync(logPath) ? readFileSync(logPath, "utf8") : "", result };
50+
}
51+
52+
test("reconciles a live supervisor with a pre-boot lock", () => {
53+
const { home, log } = runReconcile({
54+
startedAt: "1970-01-01T00:00:01Z",
55+
commandLine: "node supervisor-entrypoint.js",
56+
});
57+
assert.equal(existsSync(join(home, "paseo.pid")), false, `the supervisor trap releases the lock (${log})`);
58+
assert.match(log, /restored supervisor stopped and lock released/);
59+
});
60+
61+
test("does not interrupt a post-boot supervisor lock", () => {
62+
const { home, log } = runReconcile({
63+
startedAt: "1970-01-01T00:33:20Z",
64+
commandLine: "node supervisor-entrypoint.js",
65+
bootTime: 2_000,
66+
});
67+
assert.match(readFileSync(join(home, "paseo.pid"), "utf8"), /startedAt/);
68+
assert.doesNotMatch(log, /stopping restored/);
69+
});
70+
71+
test("leaves malformed and ambiguously owned locks untouched", () => {
72+
const malformed = runReconcile({ startedAt: "not-a-time", commandLine: "node supervisor-entrypoint.js" });
73+
assert.match(readFileSync(join(malformed.home, "paseo.pid"), "utf8"), /not-a-time/);
74+
assert.match(`${malformed.log}${malformed.result}`, /malformed or ambiguous/);
75+
76+
const ambiguous = runReconcile({ startedAt: "1970-01-01T00:00:01Z", commandLine: "node unrelated-process.js" });
77+
assert.match(readFileSync(join(ambiguous.home, "paseo.pid"), "utf8"), /startedAt/);
78+
assert.match(ambiguous.log, /not the Paseo supervisor/);
79+
});

0 commit comments

Comments
 (0)