sync update from LAN repo (source master @ ca48c4ec)
This commit is contained in:
1 parent
8bc8b0e082
commit
ea9dcff9c0
873 files changed
+107866
-14011
No files matched your search
+49
-10
@@ -4,8 +4,11 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
from abc import ABC, abstractmethod
|
||||
from collections.abc import Iterable
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
|
||||
from deerflow.skills.types import SKILL_MD_FILE, Skill, SkillCategory # noqa: F401
|
||||
@@ -26,6 +29,15 @@ class SkillStorage(ABC):
|
||||
|
||||
def __init__(self, container_path: str = "/mnt/skills") -> None:
|
||||
self._container_root = container_path
|
||||
# Parsing SKILL.md is expensive when a catalogue contains many skills.
|
||||
# Cache parsed metadata per storage instance and recheck file metadata
|
||||
# at most once per second. A normal lock is required because callers
|
||||
# also use this method from asyncio worker threads.
|
||||
self._skills_cache_lock = threading.Lock()
|
||||
self._skills_cache_signature: tuple[tuple[str, str, int, int], ...] | None = None
|
||||
self._skills_cache: tuple[Skill, ...] = ()
|
||||
self._skills_cache_checked_at = 0.0
|
||||
self._skills_cache_check_interval_seconds = 1.0
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Static protocol helpers (not storage-specific)
|
||||
@@ -227,17 +239,45 @@ class SkillStorage(ABC):
|
||||
"""
|
||||
from deerflow.skills.parser import parse_skill_file
|
||||
|
||||
skills_by_name: dict[str, Skill] = {}
|
||||
for category, category_root, md_path in self._iter_skill_files():
|
||||
skill = parse_skill_file(
|
||||
md_path,
|
||||
category=category,
|
||||
relative_path=md_path.parent.relative_to(category_root),
|
||||
with self._skills_cache_lock:
|
||||
now = time.monotonic()
|
||||
should_check_files = (
|
||||
self._skills_cache_signature is None
|
||||
or now - self._skills_cache_checked_at >= self._skills_cache_check_interval_seconds
|
||||
)
|
||||
if skill:
|
||||
skills_by_name[skill.name] = skill
|
||||
if should_check_files:
|
||||
skill_files = list(self._iter_skill_files())
|
||||
signature_parts: list[tuple[str, str, int, int]] = []
|
||||
for category, _category_root, md_path in skill_files:
|
||||
try:
|
||||
stat = md_path.stat()
|
||||
except OSError:
|
||||
# The file may disappear between the directory walk
|
||||
# and stat. The next signature check will observe it.
|
||||
continue
|
||||
signature_parts.append((category.value, str(md_path), stat.st_mtime_ns, stat.st_size))
|
||||
signature = tuple(sorted(signature_parts))
|
||||
|
||||
skills = list(skills_by_name.values())
|
||||
if signature != self._skills_cache_signature:
|
||||
valid_paths = {(category, path) for category, path, _mtime, _size in signature}
|
||||
skills_by_name: dict[str, Skill] = {}
|
||||
for category, category_root, md_path in skill_files:
|
||||
if (category.value, str(md_path)) not in valid_paths:
|
||||
continue
|
||||
skill = parse_skill_file(
|
||||
md_path,
|
||||
category=category,
|
||||
relative_path=md_path.parent.relative_to(category_root),
|
||||
)
|
||||
if skill:
|
||||
skills_by_name[skill.name] = skill
|
||||
self._skills_cache = tuple(sorted(skills_by_name.values(), key=lambda skill: skill.name))
|
||||
self._skills_cache_signature = signature
|
||||
self._skills_cache_checked_at = now
|
||||
|
||||
# Enabled state is read on every call below. Return independent
|
||||
# models so runtime configuration never mutates the cached source.
|
||||
skills = [replace(skill) for skill in self._skills_cache]
|
||||
|
||||
# Merge enabled state from extensions config (re-read every call so
|
||||
# changes made by another process are picked up immediately).
|
||||
@@ -253,7 +293,6 @@ class SkillStorage(ABC):
|
||||
if enabled_only:
|
||||
skills = [s for s in skills if s.enabled]
|
||||
|
||||
skills.sort(key=lambda s: s.name)
|
||||
return skills
|
||||
|
||||
def ensure_custom_skill_is_editable(self, name: str) -> None:
|
||||
|
||||
Reference in new issue
Block a user