Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

Add shutdown sender diagnostics

cyh37fd808512b3
diff --git a/core/process_diagnostics.py b/core/process_diagnostics.py
index 75e5ca82d..7800f6de5 100644
--- a/core/process_diagnostics.py
+++ b/core/process_diagnostics.py
@@ -8,6 +8,9 @@ import subprocess
from typing import Callable
+ProcessParts = list[str]
+
+
def _run_ps(args: list[str]) -> str:
try:
result = subprocess.run(
@@ -56,22 +59,62 @@ def process_row(pid: int) -> str:
return _run_ps(["-p", str(pid), "-o", "pid=,ppid=,pgid=,sess=,stat=,command="])
-def _process_table_rows(predicate: Callable[[list[str]], bool], *, limit: int) -> tuple[int, list[str]]:
+def _read_process_table() -> tuple[str | None, list[ProcessParts]]:
output = _run_ps(["-axo", "pid=,ppid=,pgid=,sess=,stat=,command="])
if output.startswith("<"):
- return 0, [output]
+ return output, []
- rows: list[str] = []
- total = 0
+ rows: list[ProcessParts] = []
for line in output.splitlines():
parts = line.split(None, 5)
if len(parts) < 6:
continue
+ rows.append(parts)
+ return None, rows
+
+
+def _process_table_rows(predicate: Callable[[ProcessParts], bool], *, limit: int) -> tuple[int, list[str]]:
+ error, table = _read_process_table()
+ if error:
+ return 0, [error]
+
+ rows: list[str] = []
+ total = 0
+ for parts in table:
if not predicate(parts):
continue
total += 1
if len(rows) < limit:
- rows.append(line.strip())
+ rows.append(" ".join(parts))
+ return total, rows
+
+
+def _descendant_rows(pid: int, *, limit: int) -> tuple[int, list[str]]:
+ error, table = _read_process_table()
+ if error:
+ return 0, [error]
+
+ children: dict[int, list[ProcessParts]] = {}
+ for parts in table:
+ try:
+ ppid = int(parts[1])
+ except ValueError:
+ continue
+ children.setdefault(ppid, []).append(parts)
+
+ rows: list[str] = []
+ total = 0
+ stack = list(reversed(children.get(pid, [])))
+ while stack:
+ parts = stack.pop()
+ total += 1
+ if len(rows) < limit:
+ rows.append(" ".join(parts))
+ try:
+ child_pid = int(parts[0])
+ except ValueError:
+ continue
+ stack.extend(reversed(children.get(child_pid, [])))
return total, rows
@@ -88,6 +131,7 @@ def log_process_snapshot(
*,
pid: int | None = None,
limit: int = 30,
+ related_terms: tuple[str, ...] = (),
) -> None:
"""Log a compact process snapshot around service lifecycle events."""
if not logger.isEnabledFor(logging.INFO):
@@ -133,3 +177,24 @@ def log_process_snapshot(
total,
_format_rows(total, rows),
)
+
+ total, rows = _descendant_rows(target_pid, limit=limit)
+ logger.info(
+ "Process snapshot (%s) descendants (%s): %s",
+ reason,
+ total,
+ _format_rows(total, rows),
+ )
+
+ normalized_terms = tuple(term.lower() for term in related_terms if term)
+ if normalized_terms:
+ total, rows = _process_table_rows(
+ lambda parts: any(term in parts[5].lower() for term in normalized_terms),
+ limit=limit,
+ )
+ logger.info(
+ "Process snapshot (%s) related processes (%s): %s",
+ reason,
+ total,
+ _format_rows(total, rows),
+ )
diff --git a/main.py b/main.py
index b47d9778d..e98b70491 100644
--- a/main.py
+++ b/main.py
@@ -84,6 +84,11 @@ def main():
logger.info("Starting vibe-remote service...")
logger.info(f"Working directory: {config.runtime.default_cwd}")
+ logger.info(
+ "Shutdown intent guard enabled=%s env=%s",
+ shutdown_intent_required(),
+ os.environ.get("VIBE_REQUIRE_SHUTDOWN_INTENT"),
+ )
log_process_snapshot(logger, "service-start")
report = prepare_sqlite_state(config)
logger.info(
@@ -106,8 +111,18 @@ def main():
return
shutdown_initiated = True
try:
- logger.info(f"Received signal {signum}, shutting down...")
- log_process_snapshot(logger, f"signal-{signum}")
+ logger.info("Received signal %s", signum)
+ log_process_snapshot(
+ logger,
+ f"signal-{signum}",
+ related_terms=(
+ "codex app-server",
+ "/codex ",
+ " claude",
+ "/vibe restart",
+ ".local/bin/vibe restart",
+ ),
+ )
if signum == signal.SIGTERM and shutdown_intent_required():
intent = consume_shutdown_intent(os.getpid(), signum)
if intent is None:
@@ -118,6 +133,7 @@ def main():
shutdown_initiated = False
return
logger.info("Accepted managed shutdown intent: %s", intent)
+ logger.info("Shutting down after signal %s", signum)
except Exception:
pass
try:
diff --git a/modules/agents/codex/transport.py b/modules/agents/codex/transport.py
index 17586cb09..f0a1747ee 100644
--- a/modules/agents/codex/transport.py
+++ b/modules/agents/codex/transport.py
@@ -339,7 +339,7 @@ class CodexTransport:
if not line:
break
decoded = line.decode(errors="ignore").rstrip()
- logger.debug("Codex stderr: %s", decoded)
+ logger.info("Codex stderr: %s", decoded[:2000])
except asyncio.CancelledError:
return
except Exception:
diff --git a/tests/test_process_diagnostics.py b/tests/test_process_diagnostics.py
new file mode 100644
index 000000000..d92cf72d0
--- /dev/null
+++ b/tests/test_process_diagnostics.py
@@ -0,0 +1,46 @@
+import logging
+
+from core import process_diagnostics
+
+
+def test_log_process_snapshot_includes_recursive_descendants_and_related(caplog, monkeypatch):
+ table = "\n".join(
+ [
+ "100 1 100 100 Ss /usr/bin/python main.py",
+ "200 100 200 200 Ss node codex --dangerously-bypass-approvals-and-sandbox app-server",
+ "201 200 200 200 S /vendor/codex app-server",
+ "300 1 300 300 S claude",
+ ]
+ )
+
+ def fake_run_ps(args):
+ if args[:2] == ["-p", "100"]:
+ return "100 1 100 100 Ss /usr/bin/python main.py"
+ if args[:2] == ["-p", "1"]:
+ return "1 0 1 1 Ss launchd"
+ if args[:2] == ["-axo", "pid=,ppid=,pgid=,sess=,stat=,command="]:
+ return table
+ return "<none>"
+
+ monkeypatch.setattr(process_diagnostics, "_run_ps", fake_run_ps)
+ monkeypatch.setattr(
+ process_diagnostics,
+ "process_identity",
+ lambda pid=None: {"pid": 100, "ppid": 1, "pgid": 100, "sid": 100},
+ )
+
+ logger = logging.getLogger("test.process_diagnostics")
+ with caplog.at_level(logging.INFO, logger=logger.name):
+ process_diagnostics.log_process_snapshot(
+ logger,
+ "test",
+ pid=100,
+ related_terms=("codex app-server", "claude"),
+ )
+
+ messages = "\n".join(record.getMessage() for record in caplog.records)
+ assert "Process snapshot (test) descendants (2):" in messages
+ assert "200 100 200 200 Ss node codex" in messages
+ assert "201 200 200 200 S /vendor/codex app-server" in messages
+ assert "Process snapshot (test) related processes (2):" in messages
+ assert "300 1 300 300 S claude" in messages
diff --git a/vibe/runtime.py b/vibe/runtime.py
index 7c06c6a59..772d7bf7d 100644
--- a/vibe/runtime.py
+++ b/vibe/runtime.py
@@ -155,6 +155,7 @@ def write_shutdown_intent(
}
try:
write_json(get_shutdown_intent_path(), payload)
+ logger.info("Recorded managed shutdown intent: %s", payload)
except OSError:
logger.warning("Failed to write shutdown intent for pid=%s", target_pid, exc_info=True)
@@ -334,6 +335,11 @@ def stop_pid(pid: int, timeout: float = 5) -> bool:
write_shutdown_intent(pid, signum=signal.SIGTERM, reason="stop_pid")
try:
+ logger.info(
+ "Sending managed SIGTERM to pid=%s command=%s",
+ pid,
+ get_process_command(pid),
+ )
os.kill(pid, signal.SIGTERM)
except ProcessLookupError:
return False
@@ -345,6 +351,7 @@ def stop_pid(pid: int, timeout: float = 5) -> bool:
return True
time.sleep(0.2)
try:
+ logger.warning("Sending managed SIGKILL to pid=%s command=%s", pid, get_process_command(pid))
os.kill(pid, signal.SIGKILL)
except ProcessLookupError:
return True