943acbc573
Durcit la convention de nommage des projets (dérive constatée : 'Sliding Automation', 'code_versioning'... au lieu des formes canoniques). - trilium_api.py : projets_canoniques() lit le référentiel = valeurs du label projet sur les notes de type=projet (source unique, pas de constante en dur). Note-projet CodeVersioning créée (manquait). - mcp_server.py : _valider_projet() branché dans les 6 tools de création (add_decision/history/backlog, new_conversation, create_entite, add_skill). Refuse un projet non canonique (suggestion si faute) ou inconnu (renvoi au processus de création de projet). Ne verrouille pas si référentiel illisible. - lint_audit.py : VAL-nommage aligné sur le référentiel (attrape casse, espace ET snake_case ; l'ancien 'contient un espace' ratait code_versioning). - Données : 79 notes ré-étiquetées vers les 3 formes canoniques. Quality by design : l'erreur de nommage devient impossible à l'écriture, le Lint n'est plus que le filet de sécurité.
42 lines
1.5 KiB
Python
42 lines
1.5 KiB
Python
from collections.abc import AsyncGenerator
|
|
from contextlib import AbstractContextManager
|
|
from contextlib import asynccontextmanager as asynccontextmanager
|
|
from typing import TypeVar
|
|
|
|
import anyio.to_thread
|
|
from anyio import CapacityLimiter
|
|
from starlette.concurrency import iterate_in_threadpool as iterate_in_threadpool # noqa
|
|
from starlette.concurrency import run_in_threadpool as run_in_threadpool # noqa
|
|
from starlette.concurrency import ( # noqa
|
|
run_until_first_complete as run_until_first_complete,
|
|
)
|
|
|
|
_T = TypeVar("_T")
|
|
|
|
|
|
@asynccontextmanager
|
|
async def contextmanager_in_threadpool(
|
|
cm: AbstractContextManager[_T],
|
|
) -> AsyncGenerator[_T, None]:
|
|
# blocking __exit__ from running waiting on a free thread
|
|
# can create race conditions/deadlocks if the context manager itself
|
|
# has its own internal pool (e.g. a database connection pool)
|
|
# to avoid this we let __exit__ run without a capacity limit
|
|
# since we're creating a new limiter for each call, any non-zero limit
|
|
# works (1 is arbitrary)
|
|
exit_limiter = CapacityLimiter(1)
|
|
try:
|
|
yield await run_in_threadpool(cm.__enter__)
|
|
except Exception as e:
|
|
ok = bool(
|
|
await anyio.to_thread.run_sync(
|
|
cm.__exit__, type(e), e, e.__traceback__, limiter=exit_limiter
|
|
)
|
|
)
|
|
if not ok:
|
|
raise e
|
|
else:
|
|
await anyio.to_thread.run_sync(
|
|
cm.__exit__, None, None, None, limiter=exit_limiter
|
|
)
|