fix(tools): keep memory tool available when fcntl is unavailable

This commit is contained in:
Dusk1e 2026-04-14 14:35:06 +03:00 committed by Teknium
parent 449c17e9a9
commit 420d27098f
2 changed files with 63 additions and 4 deletions

View file

@ -23,7 +23,6 @@ Design:
- Frozen snapshot pattern: system prompt is stable, tool responses show live state
"""
import fcntl
import json
import logging
import os
@ -34,6 +33,16 @@ from pathlib import Path
from hermes_constants import get_hermes_home
from typing import Dict, Any, List, Optional
try:
import fcntl
except ImportError:
fcntl = None
try:
import msvcrt
except ImportError:
msvcrt = None
logger = logging.getLogger(__name__)
# Where memory files live — resolved dynamically so profile overrides
@ -139,12 +148,31 @@ class MemoryStore:
"""
lock_path = path.with_suffix(path.suffix + ".lock")
lock_path.parent.mkdir(parents=True, exist_ok=True)
fd = open(lock_path, "w")
if fcntl is None and msvcrt is None:
yield
return
if msvcrt and (not lock_path.exists() or lock_path.stat().st_size == 0):
lock_path.write_text(" ", encoding="utf-8")
fd = open(lock_path, "r+" if msvcrt else "a+")
try:
fcntl.flock(fd, fcntl.LOCK_EX)
if fcntl:
fcntl.flock(fd, fcntl.LOCK_EX)
else:
fd.seek(0)
msvcrt.locking(fd.fileno(), msvcrt.LK_LOCK, 1)
yield
finally:
fcntl.flock(fd, fcntl.LOCK_UN)
if fcntl:
fcntl.flock(fd, fcntl.LOCK_UN)
elif msvcrt:
try:
fd.seek(0)
msvcrt.locking(fd.fileno(), msvcrt.LK_UNLCK, 1)
except (OSError, IOError):
pass
fd.close()
@staticmethod