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.

fix(auth): address follow-up review findings

cyh05ffeef80768
diff --git a/modules/agents/claude_agent.py b/modules/agents/claude_agent.py
index 5e282d3ea..bca59d7be 100644
--- a/modules/agents/claude_agent.py
+++ b/modules/agents/claude_agent.py
@@ -650,7 +650,7 @@ class ClaudeAgent(BaseAgent):
return False
normalized_subtype = (subtype or "").strip().lower()
- if normalized_subtype and normalized_subtype not in {"error", "failed"}:
+ if normalized_subtype not in {"error", "failed"}:
return False
if not classify_auth_error("claude", text):
diff --git a/modules/agents/opencode/server.py b/modules/agents/opencode/server.py
index 7f0252f40..6b47b757b 100644
--- a/modules/agents/opencode/server.py
+++ b/modules/agents/opencode/server.py
@@ -128,10 +128,11 @@ class OpenCodeServerManager:
cmd = self._get_pid_command(pid)
if cmd and self._is_opencode_serve_cmd(cmd, self.port):
targets.append(pid)
- elif isinstance(info, dict) and info.get("port") == self.port:
+ elif isinstance(info, dict) and info.get("port") == self.port and self._pid_owns_listening_port(pid, self.port):
logger.info(
- "Trusting OpenCode pid file for pid=%s because command lookup is unavailable",
+ "Trusting OpenCode pid file for pid=%s because it still owns port %s",
pid,
+ self.port,
)
targets.append(pid)
@@ -223,6 +224,53 @@ class OpenCodeServerManager:
return False
return "opencode" in command and " serve" in command and f"--port={port}" in command
+ @staticmethod
+ def _pid_owns_listening_port(pid: int, port: int) -> bool:
+ if os.name == "nt" or pid <= 0:
+ return False
+
+ proc_fd_dir = f"/proc/{pid}/fd"
+ try:
+ fd_entries = os.listdir(proc_fd_dir)
+ except OSError:
+ return False
+
+ inodes: set[str] = set()
+ for entry in fd_entries:
+ try:
+ target = os.readlink(f"{proc_fd_dir}/{entry}")
+ except OSError:
+ continue
+ if target.startswith("socket:[") and target.endswith("]"):
+ inodes.add(target[8:-1])
+
+ if not inodes:
+ return False
+
+ for table in ("/proc/net/tcp", "/proc/net/tcp6"):
+ try:
+ with open(table, encoding="utf-8") as handle:
+ next(handle, None)
+ for raw_line in handle:
+ parts = raw_line.split()
+ if len(parts) < 10:
+ continue
+ local_address = parts[1]
+ state = parts[3]
+ inode = parts[9]
+ if state != "0A" or inode not in inodes:
+ continue
+ _, _, port_hex = local_address.rpartition(":")
+ try:
+ if int(port_hex, 16) == port:
+ return True
+ except ValueError:
+ continue
+ except OSError:
+ continue
+
+ return False
+
def _is_port_available(self) -> bool:
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
@@ -333,10 +381,11 @@ class OpenCodeServerManager:
if self._pid_exists(pid):
if cmd and self._is_opencode_serve_cmd(cmd, self.port):
await self._terminate_pid(pid, reason="orphaned and unhealthy")
- elif cmd is None:
+ elif cmd is None and self._pid_owns_listening_port(pid, self.port):
logger.info(
- "Trusting OpenCode pid file for orphan cleanup pid=%s because command lookup is unavailable",
+ "Trusting OpenCode pid file for orphan cleanup pid=%s because it still owns port %s",
pid,
+ self.port,
)
await self._terminate_pid(pid, reason="orphaned and unhealthy")
self._clear_pid_file()
diff --git a/tests/test_claude_agent_sessions.py b/tests/test_claude_agent_sessions.py
index a3bda8b18..4a40f54a1 100644
--- a/tests/test_claude_agent_sessions.py
+++ b/tests/test_claude_agent_sessions.py
@@ -231,6 +231,22 @@ class ClaudeAgentSessionTests(unittest.IsolatedAsyncioTestCase):
self.assertNotIn(composite_key, controller.receiver_tasks)
self.assertNotIn(composite_key, controller.claude_sessions)
+ async def test_handle_auth_failure_result_requires_explicit_error_subtype(self):
+ controller = _StubController()
+ controller.agent_auth_service.maybe_emit_auth_recovery_message = AsyncMock(return_value=True)
+ agent = ClaudeAgent(controller)
+ context = SimpleNamespace()
+
+ handled = await agent._handle_auth_failure_result(
+ context,
+ "session-1:/tmp/work",
+ "",
+ "Let's talk about oauth login after this task finishes.",
+ )
+
+ self.assertFalse(handled)
+ controller.agent_auth_service.maybe_emit_auth_recovery_message.assert_not_awaited()
+
if __name__ == "__main__":
unittest.main()
diff --git a/tests/test_opencode_server.py b/tests/test_opencode_server.py
index edf5a5749..9594d86c2 100644
--- a/tests/test_opencode_server.py
+++ b/tests/test_opencode_server.py
@@ -168,6 +168,7 @@ class OpenCodeServerTests(unittest.IsolatedAsyncioTestCase):
manager._read_pid_file = lambda: {"pid": 654, "port": 4096} # type: ignore[method-assign]
manager._pid_exists = lambda pid: pid == 654 # type: ignore[method-assign]
manager._get_pid_command = lambda pid: None # type: ignore[method-assign]
+ manager._pid_owns_listening_port = lambda pid, port: pid == 654 and port == 4096 # type: ignore[method-assign]
terminated = []
manager._terminate_pid = lambda pid, reason: terminated.append((pid, reason)) or _async_none() # type: ignore[method-assign]
manager._clear_pid_file = lambda: terminated.append(("cleared", "")) # type: ignore[method-assign]
@@ -178,6 +179,28 @@ class OpenCodeServerTests(unittest.IsolatedAsyncioTestCase):
self.assertIn((654, "auth refresh"), terminated)
self.assertIn(("cleared", ""), terminated)
+ async def test_restart_for_auth_refresh_does_not_trust_pid_file_without_port_ownership(self):
+ manager = OpenCodeServerManager(binary="opencode", port=4096)
+ fake_session = _FakeSession()
+ manager._http_session = fake_session
+ manager._http_session_loop = object()
+ manager._process = object()
+ manager._base_url = "http://127.0.0.1:4096"
+ manager._read_pid_file = lambda: {"pid": 654, "port": 4096} # type: ignore[method-assign]
+ manager._pid_exists = lambda pid: pid == 654 # type: ignore[method-assign]
+ manager._get_pid_command = lambda pid: None # type: ignore[method-assign]
+ manager._pid_owns_listening_port = lambda pid, port: False # type: ignore[method-assign]
+ manager._find_opencode_serve_pids = lambda port: [] # type: ignore[method-assign]
+ terminated = []
+ manager._terminate_pid = lambda pid, reason: terminated.append((pid, reason)) or _async_none() # type: ignore[method-assign]
+ manager._clear_pid_file = lambda: terminated.append(("cleared", "")) # type: ignore[method-assign]
+
+ await manager.restart_for_auth_refresh()
+
+ self.assertTrue(fake_session.closed)
+ self.assertNotIn((654, "auth refresh"), terminated)
+ self.assertEqual(terminated, [("cleared", "")])
+
async def test_restart_for_auth_refresh_defers_while_requests_are_active(self):
manager = OpenCodeServerManager(binary="opencode", port=4096)
fake_session = _FakeSession()