forked from NousResearch/hermes-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhermes_state_holders.py
More file actions
292 lines (266 loc) · 10.6 KB
/
Copy pathhermes_state_holders.py
File metadata and controls
292 lines (266 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""Process and descriptor authority for state.db structural maintenance.
This module owns the proof that no foreign process still holds the active or
an unlinked SQLite DB/WAL/SHM generation. ``hermes_state`` supplies only the
SQLite connection factory needed by the final lock probe.
"""
from __future__ import annotations
import errno
import logging
import os
import sqlite3
import sys
from pathlib import Path
from typing import Callable, List, Optional, Sequence, Set, Tuple
try: # Hard dependency, but tolerate scaffold-phase imports before pip install.
import psutil
except ImportError: # pragma: no cover - stripped/scaffold installs only
psutil = None # type: ignore[assignment]
logger = logging.getLogger(__name__)
_IS_WINDOWS = sys.platform == "win32"
_HERMES_EXECUTABLES = frozenset({"hermes", "hermes-agent", "hermes-acp"})
_HERMES_PYTHON_MODULES = frozenset({"acp_adapter", "hermes_cli.main"})
_HERMES_PYTHON_SCRIPTS = frozenset({"hermes_cli/main.py", "run_agent.py"})
_PYTHON_SHORT_OPTIONS_WITH_OPERANDS = frozenset({"Q", "W", "X"})
_PYTHON_LONG_OPTIONS_WITH_OPERANDS = frozenset(
{"--check-hash-based-pycs", "--jit"}
)
def _read_proc_argv(pid: int) -> Optional[List[str]]:
"""Read /proc/<pid>/cmdline without losing argv boundaries."""
try:
with open(f"/proc/{pid}/cmdline", "rb") as handle:
raw = handle.read()
if not raw:
return None
argv = raw.decode("utf-8", "replace").split("\x00")
if argv[-1] == "":
argv.pop()
return argv or None
except OSError:
return None
def _looks_like_python_executable(program: str) -> bool:
name = os.path.basename(program).lower().removesuffix(".exe")
for prefix in ("python", "pypy"):
if name.startswith(prefix):
suffix = name[len(prefix) :]
return not suffix or all(char.isdigit() or char == "." for char in suffix)
return False
def _python_execution_target(argv: Sequence[str]) -> Optional[Tuple[str, str]]:
"""Return the Python module or script selected by interpreter options."""
index = 1
while index < len(argv):
arg = argv[index]
if arg == "--":
index += 1
return ("script", argv[index]) if index < len(argv) else None
if arg in _PYTHON_LONG_OPTIONS_WITH_OPERANDS:
index += 2
continue
if arg.startswith("--check-hash-based-pycs=") or arg.startswith("--jit="):
index += 1
continue
if arg.startswith("--"):
index += 1
continue
if arg.startswith("-") and arg != "-":
options = arg[1:]
option_index = 0
consumed_next = False
while option_index < len(options):
option = options[option_index]
attached = options[option_index + 1 :]
if option == "c":
return None
if option == "m":
if attached:
return "module", attached
index += 1
return ("module", argv[index]) if index < len(argv) else None
if option in _PYTHON_SHORT_OPTIONS_WITH_OPERANDS:
consumed_next = not attached
break
option_index += 1
index += 2 if consumed_next else 1
continue
return "script", arg
return None
def _looks_like_hermes(argv: Sequence[str]) -> bool:
"""Return whether argv identifies a supported Hermes execution target."""
if not argv:
return False
program = os.path.basename(argv[0]).lower().removesuffix(".exe")
if program in _HERMES_EXECUTABLES:
return True
if not _looks_like_python_executable(program):
return False
target = _python_execution_target(argv)
if target is None:
return False
kind, value = target
normalized = value.lower().replace("\\", "/")
if kind == "module":
return normalized in _HERMES_PYTHON_MODULES
return any(
normalized == script or normalized.endswith(f"/{script}")
for script in _HERMES_PYTHON_SCRIPTS
)
def canonical_sqlite_path(path: str) -> str:
"""Normalize a /proc fd target, stripping the Linux `` (deleted)`` suffix."""
return os.path.normcase(os.path.abspath(path.removesuffix(" (deleted)")))
def foreign_state_db_holders(db_path: Path) -> List[Tuple[int, str]]:
"""Return foreign holders of the DB or one of its WAL sidecars.
A scan failure is represented as an unknown holder. Structural maintenance
must not assume quiescence when an old, unlinked SQLite generation may
still be open by another process.
"""
if _IS_WINDOWS:
return []
db_path_str = os.path.abspath(os.fspath(db_path))
watched = {
canonical_sqlite_path(db_path_str),
canonical_sqlite_path(db_path_str + "-wal"),
canonical_sqlite_path(db_path_str + "-shm"),
}
holders: List[Tuple[int, str]] = []
watched_ids: Set[Tuple[int, int]] = set()
db_dev: Optional[int] = None
for candidate in (db_path_str, db_path_str + "-wal", db_path_str + "-shm"):
try:
stat_result = os.stat(candidate)
except OSError as exc:
if exc.errno not in (errno.ENOENT, errno.ESRCH):
holders.append(
(-1, f"watched-file stat failed: {candidate}: {exc}")
)
continue
watched_ids.add((stat_result.st_dev, stat_result.st_ino))
if candidate == db_path_str:
db_dev = stat_result.st_dev
if sys.platform.startswith("linux"):
try:
own_pid = os.getpid()
for pid_str in os.listdir("/proc"):
if not pid_str.isdigit():
continue
pid = int(pid_str)
if pid == own_pid:
continue
fd_dir = f"/proc/{pid}/fd"
try:
fds = os.listdir(fd_dir)
except OSError:
argv = _read_proc_argv(pid)
if argv is not None and _looks_like_hermes(argv):
cmdline = " ".join(argv)
holders.append((pid, f"uninspectable holder: {cmdline[:80]}"))
continue
for fd in fds:
fd_path = f"{fd_dir}/{fd}"
try:
target = os.readlink(fd_path)
except OSError as exc:
if exc.errno in (errno.ENOENT, errno.ESRCH):
continue
argv = _read_proc_argv(pid)
if argv is not None and _looks_like_hermes(argv):
holders.append(
(
pid,
f"uninspectable descriptor: {fd_path}: {exc}",
)
)
continue
target_is_watched = canonical_sqlite_path(target) in watched
try:
fd_stat = os.stat(fd_path)
except OSError as exc:
if exc.errno in (errno.ENOENT, errno.ESRCH):
continue
if target_is_watched:
holders.append(
(pid, f"uninspectable descriptor: {target}: {exc}")
)
else:
argv = _read_proc_argv(pid)
if argv is not None and _looks_like_hermes(argv):
holders.append(
(
pid,
"uninspectable descriptor: "
f"{target}: {exc}",
)
)
continue
if (fd_stat.st_dev, fd_stat.st_ino) in watched_ids or (
target_is_watched
and target.endswith(" (deleted)")
and db_dev is not None
and fd_stat.st_dev == db_dev
):
holders.append((pid, target))
except Exception as exc:
logger.warning(
"Could not prove state.db has no foreign holders; "
"deferring structural maintenance: %s",
exc,
)
holders.append((-1, f"open-file scan failed: {exc}"))
return holders
if psutil is None:
return [(-1, "open-file scan unavailable")]
try:
for process in psutil.process_iter(["pid", "open_files"]):
info = process.info
pid = int(info["pid"])
if pid == os.getpid():
continue
for opened in info.get("open_files") or ():
path = getattr(opened, "path", "")
if path and canonical_sqlite_path(path) in watched:
holders.append((pid, path))
except Exception as exc:
logger.warning(
"Could not prove state.db has no foreign holders; "
"deferring structural maintenance: %s",
exc,
)
holders.append((-1, f"open-file scan failed: {exc}"))
return holders
def live_writer_holds_db(
db_path: Path,
*,
connect_repair_durable: Callable[..., sqlite3.Connection],
) -> bool:
"""Return whether repair lacks proven exclusive ownership of ``db_path``."""
foreign_holders = foreign_state_db_holders(db_path)
if any(
pid < 0
or path.startswith("uninspectable holder:")
or path.startswith("uninspectable descriptor:")
or path.endswith(" (deleted)")
for pid, path in foreign_holders
):
return True
probe = None
try:
probe = connect_repair_durable(db_path, timeout=0.0)
probe.execute("PRAGMA locking_mode=EXCLUSIVE")
probe.execute("BEGIN IMMEDIATE")
probe.execute("ROLLBACK")
return False
except sqlite3.OperationalError as exc:
lowered = str(exc).lower()
return "locked" in lowered or "busy" in lowered
except sqlite3.DatabaseError:
return False
except Exception:
return False
finally:
if probe is not None:
try:
probe.execute("PRAGMA locking_mode=NORMAL")
except Exception:
pass
try:
probe.close()
except Exception:
pass