Files
Gabriel VidalandClaude Opus 5.5 9ebc2de810 perf(plugins): plugin pages paint at once and look native
Speed
- seed_registry.effective() cached the "any provider plugin active?" check on
  the plugin store's version: it deep-copied all 46 manifests on every price
  lookup (9.4 ms → 8 µs per rates_for) — half the hub's CPU, and what made
  Graph / Goals / Plan usage bridges take 17–180 s.
- GET /api/plugins/ui: the slim list every page reads (UI descriptors,
  provides, each active plugin's glyph + config), a few KB instead of the
  253 KB full list, which is now for Settings → Plugins only.
- Identical readOnly actions in flight share one handler run
  (plugins/singleflight.py).
- composer.shortcuts / composer.mode are stale-while-revalidate
  (plugins/swr.py); inbox.waiting stays fresh but concurrent calls share one
  run, auto-archive reuses a ≤ 30 s answer; provider lookups copy only the
  matching records (store.active_providing / active_ids).
- aiAgentPlugin.query(): host-kept reads — the last answer at once (own
  IndexedDB store, not busted per deploy), then the fresh one; learned
  preload starts a page's reads as its route mounts; UI documents kept the
  same way.

UI
- The page's subtitle, buttons and a refresh that spins while its reads run
  live in the app's header (setHeader / onHeaderAction / onRefresh); the
  frame is borderless across the content area; a skeleton (after 150 ms)
  holds its place until the frame painted, then it fades in.
- A UI kit (aia-* classes, the app's font and tokens) injected into every
  plugin frame.
- Dashboards / Files nav: plugin pages in the same rearrangeable list, with
  the plugin's description as subtitle.
- Mock: a demo plugin page (/plugins/demo/board?mock=1).

Docs: docs/plugins.md → "Pages that feel native: reads, header, kit".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 21:22:43 +01:00

54 lines
1.9 KiB
Python

"""Identical read-only plugin actions in flight share one run.
A plugin page, its slot in the Details drawer and a second tab often ask for
the same ``readOnly`` action with the same payload within the same second;
each used to spawn its own handler subprocess (and its own hub-bridge read).
:func:`shared` runs the first and hands its answer (or its error) to every
identical call that arrives while it runs. Only read-only actions go through
it — a write must run once per call.
The run is its own task, so the first caller hanging up (a closed tab) never
cancels it for the others.
"""
from __future__ import annotations
import asyncio
import json
from collections.abc import Awaitable, Callable
from typing import Any
_inflight: dict[tuple, asyncio.Task] = {}
def key_of(*parts: Any, payload: Any = None) -> tuple:
"""A hashable key: the parts as given, the payload as canonical JSON."""
try:
body = json.dumps(payload, sort_keys=True, separators=(",", ":"), default=str)
except (TypeError, ValueError):
body = repr(payload)
return (*parts, body)
def _retrieve(task: asyncio.Task) -> None:
# Every waiter may have gone (all tabs closed): read the outcome so asyncio
# never logs "exception was never retrieved".
if not task.cancelled():
task.exception()
async def shared(key: tuple, make: Callable[[], Awaitable[Any]]) -> Any:
"""``await make()`` once for every concurrent caller with ``key``."""
task = _inflight.get(key)
if task is None:
task = asyncio.ensure_future(make())
_inflight[key] = task
task.add_done_callback(_retrieve)
task.add_done_callback(lambda t, k=key: _inflight.pop(k, None) if _inflight.get(k) is t else None)
return await asyncio.shield(task)
def in_flight() -> int:
"""How many distinct runs are going (tests, metrics)."""
return len(_inflight)