P0-8 扩大: do_mode.py finish_worker 全专家插件强制路由 - GUI→XDB, network→NDB, C/C++→SDB, done→Rvr, blocked/failed→Dbg - 证据去重: 已有 xdbSessions/ndbSessions/sdbReports/rvrReviewed 则跳过 P1-GAP17: 事件 emit 规范化 - 新增 7 个事件常量 (TASK_ENTERED, TASK_FINISHED, ENGINE_ENTERED 等) - 全部 emit 调用替换字符串字面量为常量,零残留 - 30 个事件类型常量全部定义且唯一 P1-GAP18: 事件日志原子轮转 - emit 计数器每 128 次检查轮转,避免每次 emit 读文件 - 清除未使用的 _emit_with_completion/_pending_merge_complete - 原子轮转: tempfile+os.replace 保证不损坏 eng 极端接管: 强制调用全部专家插件 (Dbg/XDB/NDB/SDB/Rvr) commands/do.md: 更新为全专家插件路由文档 全量测试: 69 通过, 0 失败 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
45 lines
1.3 KiB
Python
Executable File
45 lines
1.3 KiB
Python
Executable File
"""
|
|
文件级并发控制 — 解决 V1 P0-4 零并发控制问题。
|
|
基于 fcntl.flock 的进程级文件锁,超时自动释放。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import fcntl
|
|
import os
|
|
import time
|
|
from pathlib import Path
|
|
|
|
|
|
class FileLock:
|
|
"""基于 fcntl.flock(LOCK_EX | LOCK_NB) 的进程级文件锁"""
|
|
|
|
def __init__(self, path: Path, timeout: float = 10.0):
|
|
self._path = path.with_suffix(path.suffix + ".lock") if not path.suffix.endswith(".lock") else path
|
|
self._timeout = timeout
|
|
self._fd: int | None = None
|
|
|
|
def __enter__(self) -> FileLock:
|
|
self._fd = os.open(self._path, os.O_CREAT | os.O_RDWR)
|
|
deadline = time.monotonic() + self._timeout
|
|
while True:
|
|
try:
|
|
fcntl.flock(self._fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
return self
|
|
except OSError:
|
|
if time.monotonic() >= deadline:
|
|
os.close(self._fd)
|
|
self._fd = None
|
|
raise TimeoutError(f"lock timeout after {self._timeout}s: {self._path}")
|
|
time.sleep(0.1)
|
|
|
|
def __exit__(self, *exc) -> None:
|
|
if self._fd is not None:
|
|
fcntl.flock(self._fd, fcntl.LOCK_UN)
|
|
os.close(self._fd)
|
|
self._fd = None
|
|
|
|
@property
|
|
def path(self) -> Path:
|
|
return self._path
|