Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions core/openmate_core/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
from .agent import model_label
from .hub import HubError
from .textutil import clip, one_line
from .ui import Fresh, Page, day_start, empty, row, stat, state_of, state_text, tile, when
from .ui import Fresh, NotYet, Page, day_start, empty, row, snapshot_notice, snapshot_source, stat, state_of, state_text, tile, when

log = logging.getLogger("openmate.agents")

PAGE = "openmate-agents"
CARD = "agents-card"
AHEAD = 20.0 # older than this, read again in the background
STALE = 600.0 # older than this, a draw waits for a new read
STALE = 600.0
KEEP = 120.0 # while running, read this often anyway
WAIT = 8.0
REFRESH = 60
Expand Down Expand Up @@ -113,7 +113,9 @@ class AgentsRead:
def __init__(self, app, clock: Callable[[], float] = time.time, spawn: Optional[Callable] = None):
self.app = app
self.clock = clock
self._fresh = Fresh(self._read, AHEAD, STALE, clock=clock, spawn=spawn, name="Agents")
source = snapshot_source(getattr(getattr(app, "hub", None), "base", ""))
self._fresh = Fresh(self._read, AHEAD, STALE, clock=clock, key=lambda: source, spawn=spawn, name="Agents",
persist=(getattr(app, "store", None), "agents"))

def warm(self) -> None:
self._fresh.warm(every=KEEP, stop=getattr(self.app, "closing", None))
Expand Down Expand Up @@ -200,7 +202,9 @@ def warm(self) -> None:

def _board(self) -> Tuple[Optional[Board], str]:
try:
sessions = self.agents.read()
sessions = self.agents.read(0)
except NotYet:
return None, ""
except HubError as e:
return None, e.message
except Exception as e: # never the page
Expand All @@ -212,15 +216,17 @@ def render(self, params: dict) -> dict:
version = int(params.get("ui_version") or 1)
board, missing = self._board()
if board is None:
note = empty("暂时读不到 Agent", missing + ",稍后自动重试", version)
return {"sections": [{"items": [note]}], "refresh_after": REFRESH} if self.card else \
{"title": "Agent 进度", "sections": [{"items": [note]}], "refresh_after": REFRESH}
if self.card:
return self._card(board, version)
st = state_of(params)
if st.get("session"):
return self._detail(board, str(st["session"]), version)
return self._page(board, version)
note = (empty("暂时读不到 Agent", missing + ",稍后自动重试", version) if missing
else empty("正在读取 Agent", "稍后自动刷新", version))
view = {"sections": [{"items": [note]}], "refresh_after": REFRESH}
if not self.card:
view["title"] = "Agent 进度"
elif self.card:
view = self._card(board, version)
else:
st = state_of(params)
view = self._detail(board, str(st["session"]), version) if st.get("session") else self._page(board, version)
return snapshot_notice(view, ("", self.agents._fresh))

# ---- pieces

Expand Down
13 changes: 10 additions & 3 deletions core/openmate_core/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ def shutdown(self) -> None:
self._done_wake.set()
with self.lock: # no hand-over begins after this (_take_in checks closing under this lock)
self._handed.set() # nothing waits on a sign-in's hand-over any more (closing says no to all of it)
ui = self.ui
if ui is not None:
ui.close()
held = self._done_mu.acquire(timeout=5.0) # a round touching the store finishes first
if self.today_api is not None:
try:
Expand Down Expand Up @@ -763,9 +766,13 @@ def _done_round(self, only: Optional[set]) -> int:
return today.service_done(needs, marked, states)

def _ui(self) -> "UI":
if self.ui is not None:
return self.ui
with self.lock:
if self.ui is None:
self.ui = UI(self, self.platform.ui_pages)
if self.closing.is_set():
self.ui.close()
return self.ui

def mark_changed(self, *scopes: str) -> None:
Expand Down Expand Up @@ -887,8 +894,7 @@ def account_epoch(self) -> int:
service is asked (a read) or listened to (the live link), it says whose the answer is: the adapter takes the
next account's session before the store is wiped for it, so what was asked under one generation is never the
next account's own."""
with self.lock:
return self._account_epoch
return self._account_epoch

def settle(self) -> bool:
"""While a sign-in's account is being taken in (the adapter may answer as it already), wait until the store is
Expand Down Expand Up @@ -1311,7 +1317,8 @@ def learn(m: Message, looked_up: bool = False) -> bool:
return new

def me_name(self) -> str:
return self.account.name if self.account and self.account.name else "我"
account = self.account
return account.name if account is not None and account.name else "我"

# ------------------------------------------------------------------ status

Expand Down
16 changes: 10 additions & 6 deletions core/openmate_core/people.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,19 @@ def _can(self) -> bool:
def bots(self, ids: Iterable[str], epoch: Optional[int] = None) -> Set[str]:
"""Those of ids known to be programs. The unknown ones are asked about behind, for the account generation the
caller read them for (epoch; None: now)."""
bots, unknown = self.classify(ids)
if unknown:
self.ask_unknown(unknown, epoch)
return bots

def classify(self, ids: Iterable[str]):
"""Known bots and unchecked ids, which a cached view can requeue without reading the store again."""
ids = {str(i) for i in ids if i}
store = getattr(self.app, "store", None)
if not ids or store is None:
return set()
return set(), set()
known = store.people_kinds(ids)
unknown = ids - set(known)
if unknown:
self._ask(unknown, epoch)
return {i for i, k in known.items() if k == "bot"}
return {i for i, k in known.items() if k == "bot"}, ids - set(known)

def _epoch(self) -> Optional[int]:
get = getattr(self.app, "account_epoch", None) # the connector's account generation (a stand-in may have none)
Expand Down Expand Up @@ -93,7 +97,7 @@ def kind_now(self, user_id: str, epoch: Optional[int] = None) -> str:
return kind
return ""

def _ask(self, ids: Set[str], epoch: Optional[int] = None) -> None:
def ask_unknown(self, ids: Set[str], epoch: Optional[int] = None) -> None:
if not self._can():
return
if epoch is None:
Expand Down
Loading
Loading