diff --git a/plugins/ndf/contracts/limits.json b/plugins/ndf/contracts/limits.json index 1b072f278..4a589f987 100644 --- a/plugins/ndf/contracts/limits.json +++ b/plugins/ndf/contracts/limits.json @@ -145,7 +145,9 @@ "relay.signal-lock": {"summary": "シグナルファイル(次のセグメントのマーカー・質問)の排他を待つ上限。hook の締め切りの中に収める", "seconds": 0.3}, "relay.auth-hold": {"summary": "認証の失敗で、そのアカウントを替え先と指定の候補から外す秒(#3864)", "seconds": 3600}, "relay.limit-unknown": {"summary": "上限の行を見たアカウントの使用率の記録に有効な窓が無いとき、上限として扱う秒(5 時間の窓の長さ)", "seconds": 18000}, - "relay.usage-guard": {"summary": "利用上限の前にカットポイントでアカウントを替える使用率(百分率。#3336)", "count": 90}, + "relay.five-hour-guard": {"summary": "閾値 A。5 時間の窓の使用率がこれ以上のアカウントを使い切りの組に入れ、見回りで替える(百分率。#3864 の決定 5)", "count": 85}, + "relay.weekly-prefer": {"summary": "閾値 B。7 日の窓の使用率がこれより下のアカウントを余裕の組に入れる。カットポイントと替え先の選び方だけに効く(百分率)", "count": 70}, + "relay.weekly-guard": {"summary": "閾値 C。7 日の窓の使用率がこれ以上のアカウントを使い切りの組に入れ、見回りで替える(百分率)", "count": 90}, "relay.transcript-tail": {"summary": "ラッパーが文脈の量を読む会話の記録の末尾の大きさ(バイト)", "count": 262144}, "relay.context": {"summary": "中継の下のセッションを次のセグメントへ切り替える文脈の量(token。#2595)", "count": 800000}, "release-steps.head-moved": {"summary": "HEAD が動いたときに比べ直して入れ直す回数(最初の 1 回を含む)", "attempts": 4}, diff --git a/plugins/ndf/src/ndf/domains/relay/account_decide.py b/plugins/ndf/src/ndf/domains/relay/account_decide.py index 9053aa666..c8c11ced8 100644 --- a/plugins/ndf/src/ndf/domains/relay/account_decide.py +++ b/plugins/ndf/src/ndf/domains/relay/account_decide.py @@ -1,67 +1,174 @@ -"""アカウントを替えるかの判断(#3336・#3337)。時計とファイルを読まない純粋な関数。 - -- 利用上限の前(#3336): カットポイント(子が次のセグメントのマーカーを書いた時点)で、今のアカウントの使用率が閾値以上で、 - 閾値より下のアカウントがあれば、そのアカウントで次のセグメントを始める。下のアカウントが無ければ替えない -- 利用上限の後(#3337): 上限に当たったら、背景の待ち(`wait`・`merge-when-green` など)が残っていても替える。 - 背景の待ちは会話の記録に残り、`--resume` した先のセッションが受け取るので、替えない理由にしない -- 替える先が無ければ `wait_reset` を返し、呼ぶ側はリセットの後に同じアカウントで再起動する(止まったまま残さない) +"""アカウントを替えるかの判断(#3336・#3337・#3864 の決定 5・11)。時計とファイルを読まない純粋な関数。 + +- 選ぶ順の組: 0 余裕の組(5 時間が閾値 A より下か窓が無く、7 日が閾値 B より下か窓が無い)・1 使用率の分からないもの・ + 2 B 越えの組(5 時間が A より下か窓が無く、7 日が B 以上 C より下)・3 使い切りの組(5 時間が A 以上か 7 日が C 以上)。 + 上限のものはどの組にも入れない。並びは組の番号 → 7 日の窓のリセットの早い順 → 7 日の使用率の大きい順 → 名前の順 +- カットポイント(`before_limit`): 今の組より番号の小さい組の候補があれば替える(同じ組なら替えない。切り替えの余裕) +- 見回り(`guard`): 今が使い切りの組で、閾値の内側(組 0〜2)の候補があれば替える(I12) +- 上限の後(`after_limit`): 背景の待ちが残っていても替える。替え先は使い切りの組も入れ、無ければ従量の接続、それも + 無ければ `wait_reset`。上限が解けた今のアカウントが最良の候補より悪い組でなければ同じアカウントで再開する +- 認証の失敗の後(`after_auth`): 順の先頭、無ければ従量の接続、それも無ければ `stalled` + +`candidates` は呼ぶ側が渡す起動できる登録済みアカウントの名前で、判断の関数は候補の外の名前を返さない。 """ from __future__ import annotations +import math from typing import NamedTuple, Optional +from ndf.domains.relay.account_registry import METERED + +KEY_PREFIX = "key:" # API キーの子の起動先の名前(`key:<キーの名前>`) +LIMITED = 4 # 今のアカウントが上限のときに比べる組の番号 +GROUP_NAMES = ("余裕の組", "使用率の分からないもの", "B 越えの組", "使い切りの組", "上限") + class Usage(NamedTuple): - """アカウント 1 つの使用率(5 時間と週のクォータの大きいほうの百分率)と、上限に当たっているか。""" + """アカウント 1 つの使用率。`percent` は窓の最大(一覧の表示)、`five`・`seven` は窓ごと(窓が無ければ None)。 + + `percent` が None の行は、使用率は分からないが上限のもの(`limited` が真)だけである。 + """ name: str - percent: float + percent: Optional[float] + five: Optional[float] + seven: Optional[float] + seven_resets_at: Optional[float] limited: bool +class Guards(NamedTuple): + """閾値 A・B・C(`relay.five-hour-guard`・`relay.weekly-prefer`・`relay.weekly-guard`)。""" + + five: float + prefer: float + seven: float + + class Pick(NamedTuple): - """`kind` は `keep`(替えない)・`switch`(`account` へ替える)・`wait_reset`(替える先が無い)。""" + """`kind` は `keep`(替えない)・`switch`(`account` へ替える)・`wait_reset`(替える先が無い)・ + `stalled`(認証の失敗で替える先が無い)。""" kind: str account: Optional[str] reason: str -def _best(current: Optional[str], rows: list[Usage], below: float) -> Optional[Usage]: - """今のアカウントを除き、上限に当たっておらず使用率が `below` より下で、最も空いているもの。""" - usable = [u for u in rows if u.name != current and not u.limited and u.percent < below] - return min(usable, key=lambda u: (u.percent, u.name)) if usable else None +def metered(name: Optional[str]) -> bool: + """従量の接続(`metered`・`key:<名前>`)の起動先か。""" + return name is not None and (name == METERED or name.startswith(KEY_PREFIX)) + + +def group(u: Optional[Usage], g: Guards) -> Optional[int]: + """選ぶ順の組の番号。上限のものは None。""" + if u is None: + return 1 + if u.limited: + return None + if (u.five is not None and u.five >= g.five) or (u.seven is not None and u.seven >= g.seven): + return 3 + return 0 if u.seven is None or u.seven < g.prefer else 2 + + +def order(exclude: Optional[str], rows: list[Usage], candidates: list[str], g: Guards) -> list[str]: + """`candidates` から `exclude` と上限のものを除き、選ぶ順に並べた名前。""" + by = {u.name: u for u in rows} + keyed = [] + for name in dict.fromkeys(candidates): + u = by.get(name) + n = group(u, g) + if name == exclude or n is None: + continue + resets = u.seven_resets_at if u is not None and u.seven_resets_at is not None else math.inf # 分からないものは後 + seven = -u.seven if u is not None and u.seven is not None else math.inf + keyed.append(((n, resets, seven, name), name)) + return [name for _, name in sorted(keyed)] + +def _group_of(name: str, rows: list[Usage], g: Guards) -> int: + n = group(next((u for u in rows if u.name == name), None), g) + return LIMITED if n is None else n -def before_limit(current: Optional[str], rows: list[Usage], threshold: float) -> Pick: - """カットポイントで、上限に当たる前に替えるか(#3336)。""" + +def _state(name: str, u: Usage, g: Guards) -> str: + """今のアカウントの組と、組を決めた窓と閾値(名で書く)。""" + if u.limited: + return f"{name} が上限" + if u.five is not None and u.five >= g.five: + return f"{name} の 5 時間 {u.five:g}% が閾値 A {g.five:g}% 以上" + if u.seven is not None and u.seven >= g.seven: + return f"{name} の 7 日 {u.seven:g}% が閾値 C {g.seven:g}% 以上" + if u.seven is not None and u.seven >= g.prefer: + return f"{name} の 7 日 {u.seven:g}% が閾値 B {g.prefer:g}% 以上(B 越えの組)" + return f"{name} は余裕の組" + + +def _to(name: str, rows: list[Usage], g: Guards) -> str: + return f"{name}({GROUP_NAMES[_group_of(name, rows, g)]})" + + +def _back(current: str, ranked: list[str], rows: list[Usage], g: Guards) -> Pick: + """従量の接続から登録済みアカウントへ戻る。候補が無ければそのまま。""" + if not ranked: + return Pick("keep", current, f"{current}(従量の接続)から戻れる登録済みアカウントが無い") + return Pick("switch", ranked[0], f"{current}(従量の接続)から {_to(ranked[0], rows, g)} へ戻る") + + +def before_limit(current: Optional[str], rows: list[Usage], candidates: list[str], g: Guards) -> Pick: + """カットポイントで、上限に当たる前に替えるか(決定 5)。""" + ranked = order(current, rows, candidates, g) + if metered(current): + return _back(str(current), ranked, rows, g) now = next((u for u in rows if u.name == current), None) - if now is None: - return Pick("keep", current, "今のアカウントの使用率が分からない(センサーの記録が無い)") - if now.percent < threshold and not now.limited: - return Pick("keep", current, f"{current} の使用率 {now.percent:g}% は閾値 {threshold:g}% より下") - best = _best(current, rows, threshold) - if best is None: - return Pick("keep", current, f"{current} は {now.percent:g}% だが、閾値より空いたアカウントが無い") - return Pick( - "switch", best.name, f"{current} の使用率 {now.percent:g}% が閾値 {threshold:g}% 以上。{best.name}({best.percent:g}%)へ替える" - ) - - -def after_limit(current: Optional[str], rows: list[Usage]) -> Pick: - """上限に当たった後に替えるか(#3337)。背景の待ちが残っていても替える。 - - 今のアカウントの上限が解けていれば(リセットの後)、同じアカウントで再開する。 - """ + if current is None or now is None: + return Pick("keep", current, "既定のログインか、今のアカウントの使用率が分からない(見回りと上限の後に任せる)") + mine = _group_of(current, rows, g) + if not ranked or _group_of(ranked[0], rows, g) >= mine: + return Pick("keep", current, f"{_state(current, now, g)}。より前の組の候補が無いので替えない") + return Pick("switch", ranked[0], f"{_state(current, now, g)}。{_to(ranked[0], rows, g)} へ替える") + + +def guard(current: Optional[str], rows: list[Usage], candidates: list[str], g: Guards) -> Pick: + """見回りで、今のアカウントが使い切りの組なら閾値の内側の候補へ替えるか(決定 11・I12)。""" + ranked = order(current, rows, candidates, g) + if metered(current): + return _back(str(current), ranked, rows, g) + now = next((u for u in rows if u.name == current), None) + if current is None or now is None or group(now, g) != 3: + return Pick("keep", current, "今のアカウントは使い切りの組に無いか、使用率が分からない") + if not ranked or _group_of(ranked[0], rows, g) > 2: + return Pick("keep", current, f"{_state(current, now, g)}。閾値の内側の候補が無いので使い切る") + return Pick("switch", ranked[0], f"{_state(current, now, g)}。{_to(ranked[0], rows, g)} へ替える") + + +def _fallback(current: Optional[str], fallbacks: list[str]) -> Optional[str]: + return next((f for f in fallbacks if f != current), None) + + +def after_limit(current: Optional[str], rows: list[Usage], candidates: list[str], fallbacks: list[str], g: Guards) -> Pick: + """上限に当たった後に替えるか(#3337・決定 8)。背景の待ちが残っていても替える。""" + ranked = order(current, rows, candidates, g) now = next((u for u in rows if u.name == current), None) - if now is not None and not now.limited: - return Pick("switch", current, f"{current} の上限が解けた。同じアカウントで再開する") - best = _best(current, rows, 100.0) - if best is None: - return Pick("wait_reset", current, f"{current} が上限に当たり、替える先が無い。リセットの後に同じアカウントで再開する") - return Pick( - "switch", - best.name, - f"{current} が上限に当たった。{best.name}({best.percent:g}%)で再開する。背景の待ちは再開した先のセッションが受け取る", - ) + if current in candidates and now is not None and not now.limited: + if not ranked or _group_of(current, rows, g) <= _group_of(ranked[0], rows, g): + return Pick("switch", current, f"{current} の上限が解けた。同じアカウントで再開する") + if ranked: + return Pick( + "switch", ranked[0], f"{current} が上限に当たった。{_to(ranked[0], rows, g)} で再開する。背景の待ちは再開した先が受け取る" + ) + nxt = _fallback(current, fallbacks) + if nxt is not None: + return Pick("switch", nxt, f"{current} が上限に当たり、登録済みの替え先が無い。従量の接続 {nxt} で再開する") + return Pick("wait_reset", current, f"{current} が上限に当たり、替える先が無い。リセットの後に同じアカウントで再開する") + + +def after_auth(current: Optional[str], rows: list[Usage], candidates: list[str], fallbacks: list[str], g: Guards) -> Pick: + """認証の失敗の後の替え先(決定 6)。""" + ranked = order(current, rows, candidates, g) + if ranked: + return Pick("switch", ranked[0], f"{current} の認証に失敗した。{_to(ranked[0], rows, g)} で再開する") + nxt = _fallback(current, fallbacks) + if nxt is not None: + return Pick("switch", nxt, f"{current} の認証に失敗し、登録済みの替え先が無い。従量の接続 {nxt} で再開する") + return Pick("stalled", current, f"{current} の認証に失敗し、替える先が無い") diff --git a/plugins/ndf/src/ndf/domains/relay/accounts.py b/plugins/ndf/src/ndf/domains/relay/accounts.py index f3c958a8a..1fcc3e5b9 100644 --- a/plugins/ndf/src/ndf/domains/relay/accounts.py +++ b/plugins/ndf/src/ndf/domains/relay/accounts.py @@ -221,14 +221,30 @@ def usages( w = seen[name] if not w: if name in limited: - rows.append(account_decide.Usage(name, None, True)) # type: ignore[arg-type] # 型の変更は U6 + rows.append(account_decide.Usage(name, None, None, None, None, True)) continue - percent = max(p if r > now else 0.0 for p, r in w.values()) - full = any(p >= FULL for p, r in w.values() if r > now) - rows.append(account_decide.Usage(name, percent, full or name in limited)) + rows.append(_windowed(name, w, now, name in limited)) return rows +def _windowed(name: str, w: Windows, now: float, seen_limit: bool) -> account_decide.Usage: + """窓ごとの値の行。過ぎた窓は 0% で、7 日の窓が過ぎていればそのリセットの時刻は分からない(None)。 + + 7 日の使用率は `seven_day` と、モデル別のスロットのうち過ぎていないものの最大の、大きいほう。 + """ + + def live(k: str) -> Optional[float]: + return None if k not in w else (w[k][0] if w[k][1] > now else 0.0) + + scoped = [p for k, (p, r) in w.items() if k.startswith(f"{SCOPED}/") and r > now] + weekly = [v for v in (live("seven_day"), *scoped) if v is not None] + seven = max(weekly) if weekly else None + resets = w["seven_day"][1] if "seven_day" in w and w["seven_day"][1] > now else None + percent = max(p if r > now else 0.0 for p, r in w.values()) + full = any(p >= FULL for p, r in w.values() if r > now) + return account_decide.Usage(name, percent, live("five_hour"), seven, resets, full or seen_limit) + + def _legacy_usages(state_dir: Path) -> list[account_decide.Usage]: path = usage_path(state_dir) if not path.is_file(): @@ -244,9 +260,15 @@ def _legacy_usages(state_dir: Path) -> list[account_decide.Usage]: def _usage(row: Any, path: Path) -> account_decide.Usage: + """旧い 1 つのキャッシュの行。窓ごとの値(`five`・`seven`)を持たない従来の形の行は、窓の最大の `percent` を + 5 時間と 7 日の両方の値として読む(従来の `percent` と閾値の比べ方を保つ)。""" try: - return account_decide.Usage(name=str(row["name"]), percent=float(row["percent"]), limited=bool(row["limited"])) - except (KeyError, TypeError, ValueError) as exc: + opt = {k: None if row.get(k) is None else float(row[k]) for k in ("five", "seven", "seven_resets_at")} + percent = float(row["percent"]) + if "five" not in row and "seven" not in row: + opt["five"] = opt["seven"] = percent + return account_decide.Usage(name=str(row["name"]), percent=percent, limited=bool(row["limited"]), **opt) + except (AttributeError, KeyError, TypeError, ValueError) as exc: raise AccountError(f"使用率のキャッシュの行の形が違う({path}): {row!r}") from exc diff --git a/plugins/ndf/src/ndf/domains/relay/cli.py b/plugins/ndf/src/ndf/domains/relay/cli.py index 0b8d83dff..17e00febb 100644 --- a/plugins/ndf/src/ndf/domains/relay/cli.py +++ b/plugins/ndf/src/ndf/domains/relay/cli.py @@ -284,7 +284,11 @@ def wrapper_settings(name: str, rest: list[str], parsed: Optional[claude_args.Pa stall=limits.seconds("relay.stall"), spin=hub.spin_of(environ), context_limit=int(limits.value("relay.context", "count")), - usage_threshold=limits.value("relay.usage-guard", "count"), + guards=account_decide.Guards( + limits.value("relay.five-hour-guard", "count"), + limits.value("relay.weekly-prefer", "count"), + limits.value("relay.weekly-guard", "count"), + ), transcript_bytes=int(limits.value("relay.transcript-tail", "count")), ask_plan=lane_question.plan_asker(environ, name, cwd, ident.root, limits.seconds("relay.lanes-ask")), update=updater, diff --git a/plugins/ndf/src/ndf/domains/relay/observe.py b/plugins/ndf/src/ndf/domains/relay/observe.py index b100e6ac9..9794ab24f 100644 --- a/plugins/ndf/src/ndf/domains/relay/observe.py +++ b/plugins/ndf/src/ndf/domains/relay/observe.py @@ -2,15 +2,20 @@ - 画面の末尾: 利用上限の行(`ndf.adapters.agents.usage_limit`)と、認証の失敗で `/goal` が外れた行 - 会話の記録(`/projects/<作業ディレクトリのキー>/<セッション>.jsonl`)の末尾: - 最後の応答の文脈の量(入力・キャッシュの読み・キャッシュの作成の token の和) + 最後の応答の文脈の量(入力・キャッシュの読み・キャッシュの作成の token の和)と、止めてよい時点の事実 + (道具の実行の途中か・応答が終わったか。#3864 の決定 11)。道具の実行中は会話の行の最後が `stop_reason: tool_use` の + `assistant` の `tool_use` のままで、終わると `user` の `tool_result` が同じ `tool_use_id` で続く。ファイルの最後の行は + 会話の行でないことがある(`last-prompt` など。#4018 の確かめ 3) """ from __future__ import annotations import json +import os import re +from datetime import datetime from pathlib import Path -from typing import Mapping, Optional +from typing import Any, Iterator, Mapping, Optional from ndf.adapters import agents from ndf.domains.relay import signals @@ -21,6 +26,7 @@ ANSI = re.compile(r"\x1b\[[0-9;?]*[ -/]*[@-~]|\x1b\][^\x07]*\x07") # 行頭の字下げと罫線(画面のスロット・応答のマーカー ⎿ など)。利用上限の行は行頭で照らすので外す LEAD = re.compile(r"^[^\w{]+", re.MULTILINE) +ENDED = ("end_turn", "stop_sequence") # 応答が終わった `stop_reason` USAGE_KEYS = ("input_tokens", "cache_read_input_tokens", "cache_creation_input_tokens") @@ -62,3 +68,102 @@ def _usage_total(line: str) -> Optional[int]: if not isinstance(usage, dict): return None return sum(int(usage[k]) for k in USAGE_KEYS if isinstance(usage.get(k), int)) + + +class _Unsure(ValueError): + """止めてよい時点を確定できない(記録を読めない・完全な行が壊れている・会話の記録の形が違う)。""" + + +def _lines_backward(path: Path, size: int) -> Iterator[str]: + """末尾 `size` バイトの完全な行を新しい順に。行の境は JSONL の改行で決め、範囲の端で切れた行だけを飛ばす。 + + 範囲の始まりがファイルの先頭でなければ最初の行は切れている。改行で終わらない最後の行は書きかけである。 + 読めなければ `_Unsure`(無いファイルは呼ぶ側が先に調べる)。 + """ + try: + with open(path, "rb") as fh: + begin = max(0, os.fstat(fh.fileno()).st_size - size) + fh.seek(begin) + data = fh.read() + except OSError as exc: + raise _Unsure(str(exc)) from exc + lo = 0 + if begin > 0: + cut = data.find(b"\n") + lo = len(data) if cut < 0 else cut + 1 + hi = data.rfind(b"\n") + while hi >= lo: + prev = max(data.rfind(b"\n", lo, hi), lo - 1) # 範囲内に改行が無ければ最初の完全な行は lo から + yield data[prev + 1 : hi].decode("utf-8", errors="replace") + hi = prev + + +def _talk(path: Path, size: int) -> Optional[list[dict[str, Any]]]: + """末尾から読み、最後の会話の記録(`user`・`assistant`)と、最後の `assistant` から後ろの会話の記録を古い順に。 + + 最後の `assistant` を読んだところで読むのを止める。ファイルが無ければ None。会話でない行は飛ばし、 + 読めない・完全な行が JSON でない・形の違う会話の記録(`message` が object でない)は `_Unsure`。 + """ + if not path.is_file(): + return None + rows: list[dict[str, Any]] = [] + for line in _lines_backward(path, size): + if not line.strip(): + continue + try: + row = json.loads(line) + except ValueError as exc: + raise _Unsure("壊れた行") from exc + if not isinstance(row, dict) or row.get("type") not in ("user", "assistant"): # 会話の記録だけ + continue + if not isinstance(row.get("message"), dict): + raise _Unsure(str(row.get("type"))) + rows.append(row) + if row["type"] == "assistant": + break + rows.reverse() + return rows + + +def _blocks(row: Mapping[str, Any], kind: str) -> list[Mapping[str, Any]]: + content = row["message"].get("content") + return [b for b in content if isinstance(b, dict) and b.get("type") == kind] if isinstance(content, list) else [] + + +def _pending(rows: list[dict[str, Any]]) -> Optional[bool]: + last = next((i for i in range(len(rows) - 1, -1, -1) if rows[i]["type"] == "assistant"), None) + if last is None: + return None + ids = {b.get("id") for b in _blocks(rows[last], "tool_use")} + done = {b.get("tool_use_id") for r in rows[last + 1 :] if r["type"] == "user" for b in _blocks(r, "tool_result")} + return bool(ids - done) + + +def tool_pending(path: Path, size: int) -> Optional[bool]: + """最後の `assistant` の記録の `tool_use` に対応する `tool_result` が後ろに無いか。読めない・壊れた・形が違えば None。""" + try: + rows = _talk(path, size) + except _Unsure: # noqa: NDF401 ファイル: 確定できない記録は止めてよい時点にしない(None) + return None + return None if rows is None else _pending(rows) + + +def turn_done(path: Path, size: int) -> Optional[tuple[bool, Optional[float]]]: + """最後の会話の記録が終わった応答なら `(True, その時刻)`、途中なら `(False, None)`。読めない・壊れた・形が違えば None。""" + try: + rows = _talk(path, size) + except _Unsure: # noqa: NDF401 ファイル: 確定できない記録は止めてよい時点にしない(None) + return None + if not rows: + return None + last = rows[-1] + if last["type"] != "assistant" or last["message"].get("stop_reason") not in ENDED or _pending(rows): + return (False, None) + return (True, _time(last.get("timestamp"))) + + +def _time(value: Any) -> Optional[float]: + try: + return datetime.fromisoformat(str(value).replace("Z", "+00:00")).timestamp() + except ValueError: # noqa: NDF401 ファイル: 時刻の読めない記録は時刻を持たない + return None diff --git a/plugins/ndf/src/ndf/domains/relay/wrapper.py b/plugins/ndf/src/ndf/domains/relay/wrapper.py index 9f23759c5..e6bfd1887 100644 --- a/plugins/ndf/src/ndf/domains/relay/wrapper.py +++ b/plugins/ndf/src/ndf/domains/relay/wrapper.py @@ -114,7 +114,7 @@ class Settings: stall: float spin: switch_decide.Spin context_limit: int - usage_threshold: float + guards: account_decide.Guards transcript_bytes: int ask_plan: Optional[AskPlan] = None first_args: tuple[str, ...] = () @@ -412,7 +412,8 @@ def _limit_pick(self, sec: Section, screen: str) -> Optional[account_decide.Pick if not cache.is_file() or cache.stat().st_mtime < sec.limit_at: # 上限を見た後に測っていない使用率では、今のアカウントの上限が解けたと読まない rows = [r._replace(limited=True) if r.name == sec.account else r for r in rows] - return account_decide.after_limit(sec.account, rows) + # 候補は読めた使用率の行の名前(起動できる候補を渡すのは U8 の `account_choice.candidates`) + return account_decide.after_limit(sec.account, rows, [r.name for r in rows], [], self.s.guards) def _act(self, step: switch_decide.Step) -> Optional[Ended]: """手の種類に応じた動きを打つ。表に無い種類(`wait`)は何もしない。終わるときは `Ended` を返す。 @@ -450,7 +451,8 @@ def _ask_plan(self, question: Mapping[str, Any]) -> str: def _on_switch(self, step: switch_decide.Step, sec: Section) -> Optional[Ended]: # アカウントは止める前に選び、記録の行とマーカーの削除は止めた後にする(止めなければ何も変えない。#3814) - pick = account_decide.before_limit(sec.account, accounts.usages(self.s.state_dir), self.s.usage_threshold) + rows = accounts.usages(self.s.state_dir) + pick = account_decide.before_limit(sec.account, rows, [r.name for r in rows], self.s.guards) stopped = self._stop_if_clear(sec) if stopped != "stopped": return self._not_stopped(stopped, sec) diff --git a/plugins/ndf/tests/domains/relay/test_account_registry.py b/plugins/ndf/tests/domains/relay/test_account_registry.py index 50d105190..911549618 100644 --- a/plugins/ndf/tests/domains/relay/test_account_registry.py +++ b/plugins/ndf/tests/domains/relay/test_account_registry.py @@ -398,7 +398,7 @@ def test_list_rows_hide_secrets_and_mark_broken(tmp_path): "keys": {"key1": {"key": "sk-secret", "account": "work1", "added_at": "x", "generation": "g", "monthly_usd": 100}}, } reg.write(reg.book_path(tmp_path), book) - usage = [account_decide.Usage(name="work1", percent=40.0, limited=False)] + usage = [account_decide.Usage(name="work1", percent=40.0, five=40.0, seven=None, seven_resets_at=None, limited=False)] rows, warnings = reg.list_rows( tmp_path, {"NDF_SUPERVISE_CLAUDE_FALLBACK": "AWS_REGION=us-east-1 AWS_PROFILE=p"}, "work1", lambda _: usage ) @@ -494,8 +494,8 @@ def test_list_rows_states_saved_metered_and_key_rows(tmp_path): } reg.write(reg.book_path(tmp_path), book) usage = [ - account_decide.Usage(name="c-limited", percent=100.0, limited=True), - account_decide.Usage(name="a-relogin", percent=5.0, limited=False), + account_decide.Usage(name="c-limited", percent=100.0, five=100.0, seven=None, seven_resets_at=None, limited=True), + account_decide.Usage(name="a-relogin", percent=5.0, five=5.0, seven=None, seven_resets_at=None, limited=False), ] rows, warnings = reg.list_rows(tmp_path, {}, None, lambda _: usage) assert warnings == [] diff --git a/plugins/ndf/tests/domains/relay/test_relay_decide.py b/plugins/ndf/tests/domains/relay/test_relay_decide.py index 0ca73bc73..632426d77 100644 --- a/plugins/ndf/tests/domains/relay/test_relay_decide.py +++ b/plugins/ndf/tests/domains/relay/test_relay_decide.py @@ -246,26 +246,131 @@ def test_target_key_of_the_default_login_does_not_collide_with_a_registered_name assert [switch_decide.target_key(n) for n in ("work1", "metered", "key:main")] == ["work1", "metered", "key:main"] -ROWS = [Usage("team", 94.0, False), Usage("main", 20.0, False), Usage("gmail", 50.0, False)] - - -def test_usage_guard_switches_at_the_cut_point_before_the_limit(): - # 閾値以上なら、閾値より空いたアカウントへ替える(#3336) - got = account_decide.before_limit("team", ROWS, 90.0) - assert (got.kind, got.account) == ("switch", "main") - assert account_decide.before_limit("main", ROWS, 90.0).kind == "keep" - assert account_decide.before_limit("team", [Usage("team", 94.0, False), Usage("main", 95.0, False)], 90.0).kind == "keep" - assert account_decide.before_limit("unknown", ROWS, 90.0).kind == "keep" - - -def test_after_limit_switches_even_with_background_waits_and_resumes_after_reset(): - # 背景の待ちが残っていても替える(#3337)。替える先が無ければリセットを待ち、解けたら同じアカウント - limited = [Usage("team", 100.0, True), Usage("main", 20.0, False)] - assert account_decide.after_limit("team", limited) == Pick("switch", "main", account_decide.after_limit("team", limited).reason) - only = [Usage("team", 100.0, True), Usage("main", 100.0, True)] - assert account_decide.after_limit("team", only).kind == "wait_reset" - reset = [Usage("team", 3.0, False)] - assert account_decide.after_limit("team", reset) == Pick("switch", "team", account_decide.after_limit("team", reset).reason) +G = account_decide.Guards(85.0, 70.0, 90.0) # 閾値 A・B・C(`contracts/limits.json` の既定) +R = 1_800_000_000.0 # 7 日の窓のリセットの時刻の基準 +GONE = Usage("gone", None, None, None, None, True) # 使用率は分からないが上限 + + +def use(name, five=None, seven=None, resets=None, limited=False): + percent = max((v for v in (five, seven) if v is not None), default=0.0) + return Usage(name, percent, five, seven, resets, limited) + + +def test_3864_5_group_reads_the_five_hour_and_weekly_windows_against_a_b_c(): + group = account_decide.group + assert [group(use("a", 40, 30), G), group(use("a", 40, 75), G), group(use("a", 86, 30), G), group(use("a", 40, 91), G)] == [0, 2, 3, 3] + # 境界: A ちょうどは使い切り、B ちょうどは B 越え、C ちょうどは使い切り。5 時間の窓の無いプランは A に掛からない + assert [group(use("a", 85, 10), G), group(use("a", 10, 70), G), group(use("a", 10, 90), G), group(use("a", None, 30), G)] == [ + 3, + 2, + 3, + 0, + ] + assert group(None, G) == 1 # 使用率の分からないもの + assert group(GONE, G) is None and group(use("a", 100, 10, limited=True), G) is None # 上限はどの組にも入れない + + +def test_3864_5_order_is_group_then_weekly_reset_then_weekly_use_then_name(): + rows = [ + use("full", 90, 10, R), + use("over", 10, 75, R), + use("late", 10, 10, R + 50), + use("early", 10, 20, R + 10), + use("lim", 100, 10, R, True), + GONE, + use("me", 10, 10, R), + use("outside", 1, 1, R), + ] + candidates = ["full", "over", "late", "early", "lim", "gone", "unknown", "me"] + assert account_decide.order("me", rows, candidates, G) == ["early", "late", "unknown", "over", "full"] + # 同じ組: リセットの早い順 → 7 日の使用率の大きい順 → 名前の順。リセットの分からないものは後 + same = [use("b", 10, 20, R), use("a", 10, 20, R), use("c", 10, 40, R), use("d", 10, 50, None)] + assert account_decide.order(None, same, ["d", "b", "a", "c"], G) == ["c", "a", "b", "d"] + assert account_decide.order(None, rows, [], G) == [] # 候補の外の行を返さない + + +def test_3864_4_cut_point_switches_to_a_lower_group_and_keeps_within_the_same_group(): + rows = [use("work1", 92, 30, R + 100), use("work2", 10, 10, R + 200), use("work3", 10, 10, R + 50)] + got = account_decide.before_limit("work1", rows, ["work1", "work2"], G) + assert (got.kind, got.account) == ("switch", "work2") and "work1" in got.reason and "閾値 A" in got.reason + # 同じ組の候補が 2 つなら 7 日のリセットの早いほう + assert account_decide.before_limit("work1", rows, ["work1", "work2", "work3"], G).account == "work3" + # B 越えの組から余裕の組へ替える + over = [use("work1", 10, 75, R), use("work2", 10, 10, R + 200)] + assert account_decide.before_limit("work1", over, ["work1", "work2"], G).account == "work2" + # 余裕の組なら、同じ組の候補のリセットが早くても替えない(切り替えの余裕) + calm = [use("work1", 10, 10, R + 500), use("work2", 10, 10, R)] + assert account_decide.before_limit("work1", calm, ["work1", "work2"], G).kind == "keep" + # 使い切りの組どうし・候補が無い・候補の外の空いた行・今が分からない・既定のログインでは替えない + both = [use("work1", 92, 10, R), use("work2", 95, 10, R)] + assert account_decide.before_limit("work1", both, ["work1", "work2"], G).kind == "keep" + assert account_decide.before_limit("work1", rows, ["work1"], G).kind == "keep" + assert account_decide.before_limit("work9", rows, ["work2"], G).kind == "keep" + assert account_decide.before_limit(None, rows, ["work2"], G).kind == "keep" + # 今が上限なら組 4 として比べ、使い切りの組へも替える + limited = [use("work1", 100, 10, R, True), use("work2", 95, 10, R)] + assert account_decide.before_limit("work1", limited, ["work1", "work2"], G).account == "work2" + # 従量の接続からは順の先頭へ戻り、候補が無ければそのまま + assert account_decide.before_limit("metered", rows, ["work1", "work2"], G).account == "work2" + assert account_decide.before_limit("key:k1", rows, [], G).kind == "keep" + + +def test_i12_guard_switches_only_from_the_spent_group_to_an_inner_candidate(): + calm = use("work2", 10, 10, R) + for spent in (use("work1", 86, 30, R), use("work1", 40, 91, R)): # 5 時間が A 以上・7 日が C 以上 + got = account_decide.guard("work1", [spent, calm], ["work1", "work2"], G) + assert (got.kind, got.account) == ("switch", "work2") + # 7 日が B 越え・C より下では途中で替えない + assert account_decide.guard("work1", [use("work1", 10, 75, R), calm], ["work1", "work2"], G).kind == "keep" + # 閾値の内側の候補が無ければ替えずに使い切る + assert account_decide.guard("work1", [use("work1", 86, 30, R), use("work2", 95, 10, R)], ["work1", "work2"], G).kind == "keep" + # 使用率の分からない候補(組 1)へは替える + assert account_decide.guard("work1", [use("work1", 86, 30, R)], ["work1", "work2"], G).account == "work2" + # API キーの子は、使い切りの組の登録済みアカウントにも戻る。候補が無ければそのまま + assert account_decide.guard("key:k1", [use("work1", 86, 30, R)], ["work1"], G).account == "work1" + assert account_decide.guard("metered", [], [], G).kind == "keep" + # 既定のログイン・使用率の分からない今のアカウントでは替えない + assert account_decide.guard(None, [calm], ["work2"], G).kind == "keep" + assert account_decide.guard("work1", [calm], ["work1", "work2"], G).kind == "keep" + + +def test_3864_5_after_limit_prefers_inner_candidates_then_spent_then_fallbacks(): + limited = use("work1", 100, 10, R, True) + # 使用率の分からない候補へも替える。背景の待ちが残っていても替える(#3337) + got = account_decide.after_limit("work1", [limited], ["work1", "work2"], [], G) + assert (got.kind, got.account) == ("switch", "work2") + # 閾値の内側が使い切りの組より先。内側が無ければ使い切りの組(7 日 95%)へも替える + spent = use("work2", 10, 95, R) + assert account_decide.after_limit("work1", [limited, spent, use("work3", 10, 10, R)], ["work2", "work3"], [], G).account == "work3" + assert account_decide.after_limit("work1", [limited, spent], ["work1", "work2"], [], G).account == "work2" + # 上限が解けた今のアカウント(組 0)へ戻る + freed = use("work1", 5, 10, R) + assert account_decide.after_limit("work1", [freed, spent], ["work1", "work2"], [], G) == Pick( + "switch", "work1", account_decide.after_limit("work1", [freed, spent], ["work1", "work2"], [], G).reason + ) + # 登録が 1 つだけなら、解けるまでは待ち、解ければ同じアカウント + assert account_decide.after_limit("work1", [limited], ["work1"], [], G).kind == "wait_reset" + assert account_decide.after_limit("work1", [freed], ["work1"], [], G).account == "work1" + # 使用率の分からない上限の行・候補の外(外した間・再登録が要る)の今のアカウントには戻らない + gone = Usage("work1", None, None, None, None, True) + assert account_decide.after_limit("work1", [gone], ["work1"], [], G).kind == "wait_reset" + assert account_decide.after_limit("work1", [freed], [], [], G).kind == "wait_reset" + # 候補が無ければ従量の接続、続けて API キー、それも無ければ待つ + fallbacks = ["metered", "key:k1"] + assert account_decide.after_limit("work1", [limited], [], fallbacks, G).account == "metered" + assert account_decide.after_limit("work1", [limited], [], ["key:k1"], G).account == "key:k1" + assert account_decide.after_limit("metered", [], [], fallbacks, G).account == "key:k1" + assert account_decide.after_limit("metered", [], [], ["metered"], G).kind == "wait_reset" + + +def test_after_auth_picks_the_order_then_fallbacks_and_stalls_without_any(): + rows = [use("work2", 10, 10, R)] + assert account_decide.after_auth("work1", rows, ["work1", "work2"], [], G).account == "work2" + assert account_decide.after_auth(None, rows, ["work2"], [], G).account == "work2" # 既定のログインでも替える + assert account_decide.after_auth("metered", [], ["work1"], ["metered"], G).account == "work1" + assert account_decide.after_auth("key:k1", [], [], ["key:k1", "key:k2"], G).account == "key:k2" + assert account_decide.after_auth("work1", [], ["work1"], [], G).kind == "stalled" + assert account_decide.after_auth("metered", [], [], ["metered"], G).kind == "stalled" def test_restart_judges_limits_before_stopping_and_excludes_the_target(): diff --git a/plugins/ndf/tests/domains/relay/test_relay_observe.py b/plugins/ndf/tests/domains/relay/test_relay_observe.py new file mode 100644 index 000000000..461ac8e4c --- /dev/null +++ b/plugins/ndf/tests/domains/relay/test_relay_observe.py @@ -0,0 +1,112 @@ +"""会話の記録の末尾から読む止めてよい時点の事実(`observe.tool_pending`・`turn_done`。#3864 の決定 11)。 + +記録の形は U1 の確かめ 3(PR #4018)に合わせる: 道具の実行中は会話の行の最後が `stop_reason: tool_use` の +`assistant` の `tool_use` のままで、終わると `user` の `tool_result` が同じ `tool_use_id` で続く。 +ファイルの最後の行は会話の行でないことがある。 +""" + +from __future__ import annotations + +import json + +from ndf.domains.relay import observe + +SIZE = 4096 +AT = "2026-11-22T01:02:03.000Z" + + +def _assistant(stop, *blocks, at=AT): + return {"type": "assistant", "timestamp": at, "message": {"role": "assistant", "stop_reason": stop, "content": list(blocks)}} + + +def _tool_use(tid): + return {"type": "tool_use", "id": tid, "name": "Bash", "input": {"command": "sleep 30"}} + + +def _tool_result(tid): + return {"type": "user", "timestamp": AT, "message": {"role": "user", "content": [{"type": "tool_result", "tool_use_id": tid}]}} + + +TEXT = {"type": "text", "text": "終わった"} +PROMPT = {"type": "user", "timestamp": AT, "message": {"role": "user", "content": "次をして"}} +TAIL_ROW = {"type": "last-prompt", "lastPrompt": "x"} # 会話でない行 + + +def _write(tmp_path, rows, tail=""): + path = tmp_path / "s.jsonl" + path.write_text("".join(json.dumps(r, ensure_ascii=False) + "\n" for r in rows) + tail, encoding="utf-8") + return path + + +def test_tool_pending_is_true_until_the_matching_tool_result(tmp_path): + running = [PROMPT, _assistant("tool_use", _tool_use("t1")), TAIL_ROW] + assert observe.tool_pending(_write(tmp_path, running), SIZE) is True + done = [*running, _tool_result("t1"), TAIL_ROW] + assert observe.tool_pending(_write(tmp_path, done), SIZE) is False + # 別の id の結果では閉じない + assert observe.tool_pending(_write(tmp_path, [*running, _tool_result("t0")]), SIZE) is True + # 最後が本文だけの応答なら偽 + assert observe.tool_pending(_write(tmp_path, [*done, _assistant("end_turn", TEXT)]), SIZE) is False + + +def test_tool_pending_skips_a_cut_line_and_reads_none_without_a_record(tmp_path): + rows = [PROMPT, _assistant("tool_use", _tool_use("t1"))] + assert observe.tool_pending(_write(tmp_path, rows, tail='{"type": "user", "mess'), SIZE) is True + assert observe.tool_pending(tmp_path / "missing.jsonl", SIZE) is None + assert observe.tool_pending(_write(tmp_path, [{"type": "assistant", "message": "x"}]), SIZE) is None + + +def test_turn_done_is_true_with_the_time_only_after_an_ended_reply(tmp_path): + ended = [PROMPT, _assistant("tool_use", _tool_use("t1")), _tool_result("t1"), _assistant("end_turn", TEXT), TAIL_ROW] + done, at = observe.turn_done(_write(tmp_path, ended), SIZE) + assert done is True and at is not None and at > 0 + assert observe.turn_done(_write(tmp_path, [*ended[:3], _assistant("stop_sequence", TEXT)]), SIZE)[0] is True + # 最後が利用者の入力・tool_result・stop_reason の無い応答・道具の途中なら偽 + for rows in ( + [*ended, PROMPT], + ended[:3], + [*ended[:3], _assistant(None, TEXT)], + ended[:2], + ): + assert observe.turn_done(_write(tmp_path, rows), SIZE) == (False, None) + assert observe.turn_done(tmp_path / "missing.jsonl", SIZE) is None + assert observe.turn_done(_write(tmp_path, [TAIL_ROW]), SIZE) is None + + +def test_a_broken_complete_line_is_unsure_but_a_cut_edge_is_skipped(tmp_path): + ended = [PROMPT, _assistant("end_turn", TEXT)] + # 改行で終わる壊れた行は、その前の応答で完了と読まない + broken = _write(tmp_path, ended, tail='{"type": "user", "mess\n') + assert observe.turn_done(broken, SIZE) is None and observe.tool_pending(broken, SIZE) is None + # 範囲の始まりで切れた最初の行は飛ばす + path = _write(tmp_path, [PROMPT, *ended]) + first = len(json.dumps(PROMPT, ensure_ascii=False).encode()) + 1 + assert observe.turn_done(path, path.stat().st_size - first + 5)[0] is True + + +def test_the_first_complete_line_after_a_cut_edge_is_read_alone(tmp_path): + # 切れた断片の直後の完全な行が最後の応答でも、断片と混ぜずに読む + reply = _assistant("end_turn", TEXT) + path = _write(tmp_path, [PROMPT, reply, TAIL_ROW]) + tail = len(json.dumps(reply, ensure_ascii=False).encode()) + len(json.dumps(TAIL_ROW).encode()) + 2 + assert observe.turn_done(path, tail + 5)[0] is True + assert observe.tool_pending(path, tail + 5) is False + + +def test_reading_stops_at_the_last_assistant(tmp_path): + # 最後の assistant より前の行は読まない(壊れていても判定に効かない) + path = _write(tmp_path, [], tail='{"broken\n' + "".join(json.dumps(r) + "\n" for r in [_assistant("end_turn", TEXT), TAIL_ROW])) + assert observe.turn_done(path, SIZE)[0] is True and observe.tool_pending(path, SIZE) is False + + +def test_an_unreadable_record_is_none(tmp_path, monkeypatch): + path = _write(tmp_path, [PROMPT, _assistant("end_turn", TEXT)]) + real = open + + def deny(file, *a, **k): + if str(file) == str(path): + raise PermissionError(file) + return real(file, *a, **k) + + monkeypatch.setattr("builtins.open", deny) + assert observe.turn_done(path, SIZE) is None and observe.tool_pending(path, SIZE) is None diff --git a/plugins/ndf/tests/domains/relay/test_relay_parts.py b/plugins/ndf/tests/domains/relay/test_relay_parts.py index 9f4c381f1..474cd3809 100644 --- a/plugins/ndf/tests/domains/relay/test_relay_parts.py +++ b/plugins/ndf/tests/domains/relay/test_relay_parts.py @@ -480,7 +480,7 @@ def test_account_pin_round_trip_and_broken_pin_stops(tmp_path): def test_usage_cache_round_trip(tmp_path): assert accounts.usages(tmp_path) == [] - rows = [Usage("team", 94.0, False), Usage("main", 20.0, True)] + rows = [Usage("team", 94.0, 94.0, 30.0, 1.0, False), Usage("main", 20.0, None, None, None, True)] accounts.write_usages(tmp_path, rows) assert accounts.usages(tmp_path) == rows accounts.usage_path(tmp_path).write_text('{"accounts": [{"name": "x"}]}', encoding="utf-8") @@ -544,8 +544,10 @@ def test_relay_writes_create_the_parent_and_leave_no_temporary_file(tmp_path, mo assert json.loads(path.read_text(encoding="utf-8")) == {"k": "日本語", "n": 1} accounts.use(state, "main") assert accounts.pin_path(state).read_text(encoding="utf-8") == '{"account": "main"}' - accounts.write_usages(state, [Usage("チーム", 1.5, False)]) - assert accounts.usage_path(state).read_text(encoding="utf-8") == '{"accounts": [{"name": "チーム", "percent": 1.5, "limited": false}]}' + accounts.write_usages(state, [Usage("チーム", 1.5, 1.5, None, None, False)]) + assert accounts.usage_path(state).read_text(encoding="utf-8") == ( + '{"accounts": [{"name": "チーム", "percent": 1.5, "five": 1.5, "seven": null, "seven_resets_at": null, "limited": false}]}' + ) d = registry.wrapper_dir(state, "t1") registry.write_state(d, {"state": "running", "reason": "セグメント 1"}) assert (d / registry.STATE_FILE).read_text(encoding="utf-8") == ( diff --git a/plugins/ndf/tests/domains/relay/test_relay_usage_cache.py b/plugins/ndf/tests/domains/relay/test_relay_usage_cache.py index e970fe17d..7982bdd5d 100644 --- a/plugins/ndf/tests/domains/relay/test_relay_usage_cache.py +++ b/plugins/ndf/tests/domains/relay/test_relay_usage_cache.py @@ -96,7 +96,7 @@ def test_i6_one_account_record_leaves_the_other_files_alone(tmp_path): accounts.write_usage(a, {"five_hour": (15.0, LATER)}, NOW) assert _windows(b) == {"five_hour": {"percent": 20.0, "resets_at": LATER}} got = accounts.usages(tmp_path, tmp_path / "store", NOW, {"work1": "g1", "work2": "g2"}) - assert got == [Usage("work1", 15.0, False), Usage("work2", 20.0, False)] + assert got == [Usage("work1", 15.0, 15.0, None, None, False), Usage("work2", 20.0, 20.0, None, None, False)] def test_3864_3_reset_windows_read_as_zero_and_not_limited(tmp_path): @@ -113,7 +113,12 @@ def test_3864_3_reset_windows_read_as_zero_and_not_limited(tmp_path): _cache(tmp_path, "work5", "g5", {"seven_day": {"percent": 10.0, "resets_at": LATER}, "seven_day_scoped": scoped}) _cache(tmp_path, "other", "g", {"five_hour": {"percent": 1.0, "resets_at": LATER}}) # 登録の無い名前は返さない got = accounts.usages(tmp_path, tmp_path / "store", NOW, gens) - assert got == [Usage("work1", 0.0, False), Usage("work2", 100.0, True), Usage("work5", 70.0, False)] + # 窓ごとの値: 過ぎた窓は 0% で 7 日のリセットは分からない。7 日はモデル別のスロットのうち過ぎていないものの最大と比べる + assert got == [ + Usage("work1", 0.0, 0.0, 0.0, None, False), + Usage("work2", 100.0, 20.0, 100.0, LATER, True), + Usage("work5", 70.0, None, 70.0, LATER, False), + ] def test_i7_unreadable_usage_files_read_as_unknown(tmp_path): @@ -124,7 +129,7 @@ def test_i7_unreadable_usage_files_read_as_unknown(tmp_path): assert accounts.usages(tmp_path, tmp_path / "store", NOW, {"work1": "g1"}) == [] # 壊れた記録へも次の書き込みは通る accounts.write_usage(path, {"five_hour": (10.0, LATER)}, NOW) - assert accounts.usages(tmp_path, tmp_path / "store", NOW, {"work1": "g1"}) == [Usage("work1", 10.0, False)] + assert accounts.usages(tmp_path, tmp_path / "store", NOW, {"work1": "g1"}) == [Usage("work1", 10.0, 10.0, None, None, False)] def test_i8_held_accounts_until_the_end_and_unhold(tmp_path): @@ -166,7 +171,7 @@ def test_i11_a_seen_limit_holds_until_until_or_a_new_window(tmp_path): {"five_hour": {"percent": 92.0, "resets_at": LATER}, "seven_day": {"percent": 50.0, "resets_at": LATER + 86400}}, ) accounts.see_limit(tmp_path, "work1", "g1", NOW, (LATER, "five_hour", LATER)) - assert accounts.usages(tmp_path, store, NOW, gens) == [Usage("work1", 92.0, True)] # 古い 92% の記録でも上限 + assert accounts.usages(tmp_path, store, NOW, gens) == [Usage("work1", 92.0, 92.0, 50.0, LATER + 86400, True)] # 古い 92% の記録でも上限 # 同じ窓の値の増加・上限より前の入力では解かない accounts.write_usage(accounts.usage_path(tmp_path, "work1", "g1"), {"five_hour": (93.0, LATER)}, NOW) accounts.write_usage(accounts.usage_path(tmp_path, "work1", "g1"), {"five_hour": (10.0, LATER - 600)}, NOW) @@ -177,7 +182,7 @@ def test_i11_a_seen_limit_holds_until_until_or_a_new_window(tmp_path): assert accounts.usages(tmp_path, store, NOW + 60, gens)[0].limited # 観測の窓が新しい窓(resets_at が window_resets_at と until より後で 100 未満)に替われば until の前でも解く accounts.write_usage(accounts.usage_path(tmp_path, "work1", "g1"), {"five_hour": (4.0, LATER + 18000)}, NOW) - assert accounts.usages(tmp_path, store, NOW + 60, gens) == [Usage("work1", 50.0, False)] + assert accounts.usages(tmp_path, store, NOW + 60, gens) == [Usage("work1", 50.0, 4.0, 50.0, LATER + 86400, False)] def test_i11_estimated_and_weekly_limits_are_not_released_by_another_window(tmp_path): @@ -202,7 +207,7 @@ def test_i11_estimated_and_weekly_limits_are_not_released_by_another_window(tmp_ # 使用率のファイルが無い名前に観測だけがあると、使用率の分からない上限の行になる accounts.see_limit(tmp_path, "work3", "g3", NOW, (LATER, None, None)) rows = accounts.usages(tmp_path, store, NOW, {"work3": "g3"}) - assert rows == [Usage("work3", None, True)] + assert rows == [Usage("work3", None, None, None, None, True)] assert accounts.usages(tmp_path, store, LATER, {"work3": "g3"}) == [] accounts.limits_path(tmp_path).write_text("{", encoding="utf-8") assert accounts.limits_seen(tmp_path, NOW, gens, windows) == {} diff --git a/plugins/ndf/tests/domains/relay/test_relay_wrapper.py b/plugins/ndf/tests/domains/relay/test_relay_wrapper.py index 1db095169..4c71c98e4 100644 --- a/plugins/ndf/tests/domains/relay/test_relay_wrapper.py +++ b/plugins/ndf/tests/domains/relay/test_relay_wrapper.py @@ -33,7 +33,7 @@ def test_next_block_starts_a_new_session_with_the_block_and_a_relay_mark(tmp_path): def prepare(d, state): - accounts.write_usages(state, [Usage("team", 95.0, False), Usage("main", 10.0, False)]) + accounts.write_usages(state, [Usage("team", 95.0, 95.0, None, None, False), Usage("main", 10.0, 10.0, None, None, False)]) accounts.use(state, "team") ended, spawn, w = run_wrapper( @@ -54,10 +54,24 @@ def prepare(d, state): assert registry.read_state(w.dir)["state"] == "stopped" +def test_cut_point_switches_on_the_legacy_usage_cache_without_windows(tmp_path): + # 窓ごとの値を持たない従来の形のキャッシュでも、`percent` が閾値以上なら空いたアカウントへ替える + def prepare(d, state): + rows = [{"name": "team", "percent": 95.0, "limited": False}, {"name": "main", "percent": 10.0, "limited": False}] + signals.replace_json(accounts.usage_path(state), {"accounts": rows}) + accounts.use(state, "team") + + ended, spawn, w = run_wrapper( + tmp_path, [FakeChild([b"working\r\n"], None), FakeChild([], 0)], prepare, markers={0: "/goal 次のセグメント"} + ) + (_, first_env), (_, second_env) = spawn.calls + assert (first_env["NDF_CLAUDE_ACCOUNT"], second_env["NDF_CLAUDE_ACCOUNT"]) == ("team", "main") + + def test_usage_limit_resumes_on_another_account_even_with_background_waits(tmp_path): # 背景の待ちを残したまま上限に当たっても、替える先で --resume して続ける(#3337) def prepare(d, state): - accounts.write_usages(state, [Usage("team", 100.0, True), Usage("main", 5.0, False)]) + accounts.write_usages(state, [Usage("team", 100.0, 100.0, None, None, True), Usage("main", 5.0, 5.0, None, None, False)]) accounts.use(state, "team") limited = FakeChild([b" \xe2\x8e\xbf You've hit your individual spend limit \xc2\xb7 resets 10:40pm\r\n"], None) @@ -309,7 +323,7 @@ def prepare(d, state): def test_i15_short_switches_back_to_launched_accounts_end_on_the_third_return(tmp_path): # 決定 13(替えの扱い): 上限の観測が書けず team→main→team→main と既に起動した起動先へ戻る短い替えは数え、3 つ目の戻りで止まる - prepare = _toggling([Usage("team", 50.0, False), Usage("main", 5.0, False)]) + prepare = _toggling([Usage("team", 50.0, 50.0, None, None, False), Usage("main", 5.0, 5.0, None, None, False)]) children = [FakeChild([LIMIT_LINE], None) for _ in range(3)] + [FakeChild([LIMIT_LINE, b"", b""], 1), FakeChild([], 0)] ended, spawn, w, _ = _spin_run(tmp_path, children, Clock(), prepare=prepare) assert [env["NDF_CLAUDE_ACCOUNT"] for _, env in spawn.calls] == ["team", "main", "team", "main"] @@ -318,7 +332,13 @@ def test_i15_short_switches_back_to_launched_accounts_end_on_the_third_return(tm def test_i15_short_switches_to_first_time_accounts_are_not_counted(tmp_path): # 決定 13(替えの扱い): 初めての起動先への替え(team→main→gmail)は数えず、戻る替え 1 つでは止まらない - prepare = _toggling([Usage("team", 50.0, False), Usage("main", 5.0, False), Usage("gmail", 30.0, False)]) + # 選ぶ順の組で main(余裕)→ gmail(B 越え)→ team(使い切り)の順に並べる + rows = [ + Usage("team", 86.0, 86.0, None, None, False), + Usage("main", 5.0, 5.0, None, None, False), + Usage("gmail", 75.0, 5.0, 75.0, None, False), + ] + prepare = _toggling(rows) children = [FakeChild([LIMIT_LINE], None) for _ in range(3)] + [FakeChild([], 0)] ended, spawn, w, _ = _spin_run(tmp_path, children, Clock(), prepare=prepare) assert [env["NDF_CLAUDE_ACCOUNT"] for _, env in spawn.calls] == ["team", "main", "gmail", "main"] @@ -352,7 +372,7 @@ def test_state_file_records_the_wrapper_and_the_section(tmp_path): def _limited_run(tmp_path, mtime_offset): def prepare(d, state): - accounts.write_usages(state, [Usage("team", 50.0, False), Usage("main", 5.0, False)]) + accounts.write_usages(state, [Usage("team", 50.0, 50.0, None, None, False), Usage("main", 5.0, 5.0, None, None, False)]) accounts.use(state, "team") at = time.time() + mtime_offset os.utime(accounts.usage_path(state), (at, at)) @@ -525,7 +545,7 @@ def test_i3_hands_that_continue_the_same_conversation_only_confirm(tmp_path): def test_i3_account_switch_on_the_limit_only_confirms(tmp_path): def prepare(d, state): - accounts.write_usages(state, [Usage("team", 100.0, True), Usage("main", 5.0, False)]) + accounts.write_usages(state, [Usage("team", 100.0, 100.0, None, None, True), Usage("main", 5.0, 5.0, None, None, False)]) accounts.use(state, "team") hands = Hands(updated=[_same()], targets=[Target(VERSION, None)]) @@ -861,7 +881,7 @@ def _run_failing(tmp_path, children, error, fail_at=0, markers=None, **changes): def _limit_usages(d, state): - accounts.write_usages(state, [Usage("team", 100.0, True), Usage("main", 5.0, False)]) + accounts.write_usages(state, [Usage("team", 100.0, 100.0, None, None, True), Usage("main", 5.0, 5.0, None, None, False)]) accounts.use(state, "team") diff --git a/plugins/ndf/tests/domains/relay/test_relay_wrapper_question.py b/plugins/ndf/tests/domains/relay/test_relay_wrapper_question.py index b8ace6b59..5900f6e34 100644 --- a/plugins/ndf/tests/domains/relay/test_relay_wrapper_question.py +++ b/plugins/ndf/tests/domains/relay/test_relay_wrapper_question.py @@ -142,7 +142,7 @@ def _race_setup(kind: str, first: FakeChild, monkeypatch) -> tuple[dict, object] if kind == "switch": def prepare(d, state): - accounts.write_usages(state, [Usage("team", 95.0, False), Usage("main", 10.0, False)]) + accounts.write_usages(state, [Usage("team", 95.0, 95.0, None, None, False), Usage("main", 10.0, 10.0, None, None, False)]) accounts.use(state, "team") return {"markers": {0: "/goal 次"}}, prepare @@ -150,7 +150,7 @@ def prepare(d, state): first.outputs = [LIMIT_LINE] def prepare(d, state): - accounts.write_usages(state, [Usage("team", 100.0, True), Usage("main", 5.0, False)]) + accounts.write_usages(state, [Usage("team", 100.0, 100.0, None, None, True), Usage("main", 5.0, 5.0, None, None, False)]) accounts.use(state, "team") return {}, prepare diff --git a/plugins/ndf/tests/domains/statusline/test_statusline_usage.py b/plugins/ndf/tests/domains/statusline/test_statusline_usage.py index 60958d9da..7733d8dc3 100644 --- a/plugins/ndf/tests/domains/statusline/test_statusline_usage.py +++ b/plugins/ndf/tests/domains/statusline/test_statusline_usage.py @@ -166,7 +166,9 @@ def test_status_line_and_usages_use_the_same_path(tmp_path): state = tmp_path / "state" usage = accounts.usage_path(state, "work1", "g1") _run(tmp_path, _input(_limits(five=42.0, seven=18.0)), usage) - assert accounts.usages(state, tmp_path / "store", LATER - 10, {"work1": "g1"}) == [Usage("work1", 42.0, False)] + assert accounts.usages(state, tmp_path / "store", LATER - 10, {"work1": "g1"}) == [ + Usage("work1", 42.0, 42.0, 18.0, LATER + 86400, False) + ] def test_status_line_never_takes_a_lock_another_writer_holds(tmp_path): diff --git a/plugins/ndf/tests/progress/test_plan_lane_question.py b/plugins/ndf/tests/progress/test_plan_lane_question.py index d96e8ad0e..39b91c96d 100644 --- a/plugins/ndf/tests/progress/test_plan_lane_question.py +++ b/plugins/ndf/tests/progress/test_plan_lane_question.py @@ -15,7 +15,7 @@ import pytest from ndf.domains.hooks import main as hook_main -from ndf.domains.relay import hub, lane_question, registry, signals, switch_decide, wrapper +from ndf.domains.relay import account_decide, hub, lane_question, registry, signals, switch_decide, wrapper from ndf.progress import command, plan from ndf.progress.lane import launch from ndf.progress.plan import cli as plan_cli @@ -120,7 +120,7 @@ def _settings(state: Path, cwd: Path, name: str, env: dict, ask) -> wrapper.Sett stall=1000.0, spin=switch_decide.Spin(3, 0.0), context_limit=800_000, - usage_threshold=90.0, + guards=account_decide.Guards(85.0, 70.0, 90.0), transcript_bytes=4096, ask_plan=ask, ) diff --git a/plugins/ndf/tests/support/relay_children.py b/plugins/ndf/tests/support/relay_children.py index 6a938aee7..166aa0624 100644 --- a/plugins/ndf/tests/support/relay_children.py +++ b/plugins/ndf/tests/support/relay_children.py @@ -9,7 +9,7 @@ import os from pathlib import Path -from ndf.domains.relay import registry, signals, switch_decide, wrapper +from ndf.domains.relay import account_decide, registry, signals, switch_decide, wrapper VERSION = "11.0.0-dev.4" LIMIT_LINE = b" \xe2\x8e\xbf You've hit your individual spend limit \xc2\xb7 resets 10:40pm\r\n" @@ -140,7 +140,7 @@ def make_settings(tmp_path: Path, **changes) -> wrapper.Settings: # 偽の時計は呼ぶたびに 1 秒しか進まず、どのセグメントも短い。再起動ループの検出を確かめるテストだけが秒を渡す spin=switch_decide.Spin(3, 0.0), context_limit=800_000, - usage_threshold=90.0, + guards=account_decide.Guards(85.0, 70.0, 90.0), transcript_bytes=4096, ) values.update(changes)