From f6ee587c3ca1f516c02e40f24ce39c42bd8b4784 Mon Sep 17 00:00:00 2001 From: "takemi.ohama" Date: Sun, 22 Nov 2026 00:40:16 +0000 Subject: [PATCH 01/11] =?UTF-8?q?Test:=20=E4=BD=BF=E7=94=A8=E7=8E=87?= =?UTF-8?q?=E3=81=AE=20API=20=E3=81=AE=E5=8F=96=E5=BE=97=E3=81=AE=E8=A8=98?= =?UTF-8?q?=E9=8C=B2=E3=83=BB=E5=8F=96=E5=BE=97=E3=81=AE=E6=8E=92=E4=BB=96?= =?UTF-8?q?=E3=83=BB=E5=8F=96=E3=82=8A=E7=9B=B4=E3=81=97=E3=81=AE=E5=88=A4?= =?UTF-8?q?=E5=AE=9A=E3=81=AE=E5=A4=B1=E6=95=97=E3=81=99=E3=82=8B=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=82=92=E8=B6=B3=E3=81=99=EF=BC=88#3604=20#?= =?UTF-8?q?3864=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KYHP7z4QjHh8vkcxvdbGUP --- .../ndf/tests/domains/relay/test_relay_cli.py | 37 ++++ .../domains/relay/test_relay_usage_fetch.py | 186 ++++++++++++++++++ 2 files changed, 223 insertions(+) create mode 100644 plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py diff --git a/plugins/ndf/tests/domains/relay/test_relay_cli.py b/plugins/ndf/tests/domains/relay/test_relay_cli.py index 7001fd436..0d474a28e 100644 --- a/plugins/ndf/tests/domains/relay/test_relay_cli.py +++ b/plugins/ndf/tests/domains/relay/test_relay_cli.py @@ -627,3 +627,40 @@ def test_i8_remove_rerun_after_a_new_registration_keeps_it(tmp_path, monkeypatch assert len(logouts) == 1 and account_registry.names(store) == ["work1"] and account_registry.generation(store, "work1") == new assert cli.main(["account", "remove", "work1"]) == 0 # 残りが無くなった後の削除で今の登録を外す assert len(logouts) == 2 and account_registry.names(store) == [] + + +def test_i16_add_and_remove_wait_for_the_fetch_lock_and_forget_inside_it(tmp_path, monkeypatch, capsys, last_result, hold_lock): + from ndf.domains.relay import account_login, account_registry + + store = tmp_path / "store" + monkeypatch.setenv("NDF_ACCOUNTS_DIR", str(store)) + _relogin(monkeypatch, tmp_path) + monkeypatch.setattr(accounts, "fetch_wait", lambda: 0.0) + fd = hold_lock(accounts.fetch_lock_path(store, "work1")) # 別のラッパーの取得が排他を持つ + assert cli.main(["account", "add", "work1", "--code", "c"]) == causes.exit_code("store_in_use") + assert last_result(capsys.readouterr().out)["cause"]["kind"] == "store_in_use" + assert account_registry.names(store) == [] + os.close(fd) + assert cli.main(["account", "add", "work1", "--code", "c"]) == 0 + first = account_registry.generation(store, "work1") + accounts.write_fetch(accounts.fetch_path(store, "work1", first), accounts.claude_usage.Usage(five_hour=(10.0, 4_000_000_000.0)), 1.0) + inside = [] + real = accounts.forget + + def forget(*a): # 後始末は取得の排他の中で打つ(取得は待たずに諦める) + with accounts.fetch_lock(store, "work1", 0) as got: + inside.append(not got) + real(*a) + + monkeypatch.setattr(accounts, "forget", forget) + assert cli.main(["account", "add", "work1", "--code", "c"]) == 0 + assert inside == [True] and not accounts.fetch_path(store, "work1", first).exists() + monkeypatch.setattr(cli_account.shutil, "which", lambda _: None) + monkeypatch.setattr(account_login, "logout", lambda *a: True) + fd = hold_lock(accounts.fetch_lock_path(store, "work1")) + assert cli.main(["account", "remove", "work1"]) == causes.exit_code("store_in_use") + assert account_registry.names(store) == ["work1"] + os.close(fd) + capsys.readouterr() + assert cli.main(["account", "remove", "work1"]) == 0 + assert inside == [True, True] and account_registry.names(store) == [] diff --git a/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py b/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py new file mode 100644 index 000000000..dd658c503 --- /dev/null +++ b/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py @@ -0,0 +1,186 @@ +"""中継のアカウントの使用率の API の取得の記録・取得の排他・取り直しの判定(#3864 の決定 4、I16)。 + +偽の宛先と認証ファイルを使う取得の手順(`fetch_usage`・`_refresh`)は、起動の前の確かめ(U2・U3)の部品の後に足す。 +""" + +from __future__ import annotations + +import json +import os +import stat +import threading +import time + +from ndf.adapters import claude_usage +from ndf.domains.relay import accounts +from ndf.domains.relay.account_decide import Usage + +NOW = 1_795_000_000.0 +LATER = NOW + 3600 +WEEK = NOW + 5 * 86400 +INTERVAL = 300.0 + + +def _fetch_record(path) -> dict: + return json.loads(path.read_text(encoding="utf-8")) + + +def _got(**kw) -> claude_usage.Usage: + return claude_usage.Usage(fetched_at=NOW, **kw) + + +def test_decision4_fetch_record_lives_in_the_store_and_reads_from_any_state_dir(tmp_path): + store, state_a, state_b = tmp_path / "store", tmp_path / "state-a", tmp_path / "state-b" + path = accounts.fetch_path(store, "work2", "g2") + assert path == store / ".usage-fetch" / "work2" / "g2.json" + assert accounts.fetch_path(store, "work2", "") == path.with_name("_.json") + accounts.write_fetch(path, _got(five_hour=(40.0, LATER), seven_day=(20.0, WEEK), scoped=[("opus", 95.0, WEEK)]), NOW) + rec = _fetch_record(path) + assert rec["error"] is None and rec["windows"]["seven_day_scoped"] == {"opus": {"percent": 95.0, "resets_at": WEEK}} + assert stat.S_IMODE(path.parent.stat().st_mode) == 0o700 + # 状態の保存先には何も書かず、保存先の違う 2 つのラッパーが同じ記録を読む(モデル別の週の枠 95 を使用率に読む) + for state in (state_a, state_b): + assert accounts.usages(state, store, NOW, {"work2": "g2"}) == [Usage("work2", 95.0, False)] + assert not accounts.usage_dir(state).exists() + # 旧い世代の取得の記録は読まない + assert accounts.usages(state_a, store, NOW, {"work2": "g3"}) == [] + + +def test_decision4_usages_merges_the_status_line_and_the_fetch_record_per_window(tmp_path): + store, state = tmp_path / "store", tmp_path / "state" + accounts.write_usage(accounts.usage_path(state, "work1", "g1"), {"five_hour": (50.0, LATER)}, NOW) + accounts.write_fetch(accounts.fetch_path(store, "work1", "g1"), _got(five_hour=(30.0, LATER), seven_day=(60.0, WEEK)), NOW) + assert accounts.usages(state, store, NOW, {"work1": "g1"}) == [Usage("work1", 60.0, False)] + # 取得の記録の窓が新しい resets_at を持てば、使用率が小さくてもそれを採る + accounts.write_fetch(accounts.fetch_path(store, "work1", "g1"), _got(five_hour=(5.0, LATER + 18000)), NOW) + windows = accounts.read_windows(accounts.usage_path(state, "work1", "g1"), accounts.fetch_path(store, "work1", "g1")) + assert windows["five_hour"] == (5.0, LATER + 18000) + # 片方が読めなければもう片方だけで読む + accounts.fetch_path(store, "work1", "g1").write_text("{", encoding="utf-8") + assert accounts.usages(state, store, NOW, {"work1": "g1"}) == [Usage("work1", 50.0, False)] + + +def test_decision4_write_fetch_keeps_model_windows_apart_and_missing_windows(tmp_path): + path = accounts.fetch_path(tmp_path, "work1", "g1") + thursday, friday = NOW + 4 * 86400, NOW + 5 * 86400 + accounts.write_fetch(path, _got(five_hour=(10.0, LATER), scoped=[("a", 95.0, friday)]), NOW) + # 次の応答で別の識別の枠 B が 100。A と B を別々に残し、7 日を 100 と読む。応答に無い 5 時間の窓は前の値が残る + accounts.write_fetch(path, _got(scoped=[("b", 100.0, thursday)]), NOW) + windows = _fetch_record(path)["windows"] + assert windows["seven_day_scoped"] == {"a": {"percent": 95.0, "resets_at": friday}, "b": {"percent": 100.0, "resets_at": thursday}} + assert windows["five_hour"] == {"percent": 10.0, "resets_at": LATER} + assert accounts.usages(tmp_path / "state", tmp_path, NOW, {"work1": "g1"}) == [Usage("work1", 100.0, True)] + # 崩れたモデル別の枠(None)は前の値を残し、古い入力・失効した入力で下げない + accounts.write_fetch(path, _got(five_hour=(3.0, LATER - 60), scoped=None), NOW) + accounts.write_fetch(path, _got(five_hour=(1.0, NOW - 1)), NOW) + assert _fetch_record(path)["windows"] == windows + + +def test_decision4_spend_limit_holds_until_an_explicit_false(tmp_path): + store, state = tmp_path / "store", tmp_path / "state" + path = accounts.fetch_path(store, "work2", "g2") + # 最初の応答が窓を持たず spend_limit_reached が真なら、窓もキャッシュも無くても上限の行で返る + accounts.write_fetch(path, _got(spend_limit_reached=True), NOW) + assert accounts.usages(state, store, NOW, {"work2": "g2"}) == [Usage("work2", None, True)] # type: ignore[arg-type] + accounts.write_fetch(path, _got(five_hour=(10.0, LATER)), NOW) # 応答に無い(None)なら真が残る + assert accounts.usages(state, store, NOW, {"work2": "g2"}) == [Usage("work2", 10.0, True)] + accounts.write_fetch(path, _got(five_hour=(10.0, LATER), spend_limit_reached=False), NOW) + assert accounts.usages(state, store, NOW, {"work2": "g2"}) == [Usage("work2", 10.0, False)] + + +def test_decision4_write_fetch_keeps_the_attempt_time_and_clears_the_error(tmp_path): + path = accounts.fetch_path(tmp_path, "work1", "g1") + path.parent.mkdir(parents=True) + path.write_text(json.dumps({"at": NOW - 5, "error": "network", "spend_limit_reached": None, "windows": {}}), encoding="utf-8") + accounts.write_fetch(path, _got(five_hour=(10.0, LATER)), NOW) + rec = _fetch_record(path) + assert rec["at"] == NOW - 5 and rec["error"] is None and rec["spend_limit_reached"] is None + + +def _attempted(store, at, windows=None): + path = accounts.fetch_path(store, "work1", "g1") + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(json.dumps({"at": at, "error": None, "spend_limit_reached": None, "windows": windows or {}}), encoding="utf-8") + return path + + +def test_i16_due_once_per_interval(tmp_path): + usage = accounts.usage_path(tmp_path / "state", "work1", "g1") + fetch = accounts.fetch_path(tmp_path, "work1", "g1") + assert accounts.due(usage, fetch, NOW, INTERVAL, None, None) # 試したことが無い + _attempted(tmp_path, NOW) + assert not accounts.due(usage, fetch, NOW + 10, INTERVAL, None, None) # 別のラッパー(tried を持たない)も記録の at で数える + assert accounts.due(usage, fetch, NOW + INTERVAL, INTERVAL, None, None) + assert not accounts.due(usage, fetch, NOW + INTERVAL, INTERVAL, None, NOW + 10) # このラッパーがキューへ入れた時刻 + fetch.write_text("{", encoding="utf-8") # 読めない取得の記録は at が無い + assert accounts.due(usage, fetch, NOW + 10, INTERVAL, None, None) + + +def test_i16_due_once_per_reset_after_the_last_attempt(tmp_path): + usage = accounts.usage_path(tmp_path / "state", "work1", "g1") + fetch = _attempted(tmp_path, NOW, {"five_hour": {"percent": 100.0, "resets_at": NOW + 100}}) + assert not accounts.due(usage, fetch, NOW + 50, INTERVAL, None, None) + assert accounts.due(usage, fetch, NOW + 150, INTERVAL, None, None) # 最後に試した後にリセットを過ぎた + # そのリセットで試した後は、取得が失敗して過ぎた窓が残っても真にならない + _attempted(tmp_path, NOW + 120, {"five_hour": {"percent": 100.0, "resets_at": NOW + 100}}) + assert not accounts.due(usage, fetch, NOW + 150, INTERVAL, None, None) + # status line のキャッシュのモデル別の枠のリセットと、上限の観測の until でも取り直す + _attempted(tmp_path, NOW) + accounts.write_usage(usage, {"seven_day_scoped/opus": (90.0, NOW + 100)}, NOW) + assert accounts.due(usage, fetch, NOW + 150, INTERVAL, None, None) + assert not accounts.due(usage, fetch, NOW + 150, INTERVAL, None, NOW + 120) + fetch.unlink() + usage.unlink() + _attempted(tmp_path, NOW) + assert accounts.due(usage, fetch, NOW + 150, INTERVAL, NOW + 100, None) + assert not accounts.due(usage, fetch, NOW + 150, INTERVAL, NOW + 200, None) + + +def test_i16_fetch_lock_is_one_per_store_and_name_and_never_taken_from_a_live_owner(tmp_path, hold_lock): + store = tmp_path / "store" # 旧い登録簿(.fetch-locks も .usage-fetch も無い)でも親を作って通る + lock = accounts.fetch_lock_path(store, "work1") + assert lock == store / ".fetch-locks" / "work1.lock" + with accounts.fetch_lock(store, "work1", 0) as got: + assert got + assert stat.S_IMODE(lock.parent.stat().st_mode) == 0o700 + with accounts.fetch_lock(store, "work1", 0) as again: # 同じ名前の 2 つ目の取得は待たずに諦める + assert not again + with accounts.fetch_lock(store, "work2", 0) as other: + assert other + fd = hold_lock(lock) + os.utime(lock, (NOW - 86400, NOW - 86400)) # 古いファイルでも、持ち主が生きている間は奪わない + with accounts.fetch_lock(store, "work1", 0) as got: + assert not got + os.close(fd) # 持ち主が死ねば取れる + with accounts.fetch_lock(store, "work1", 0) as got: + assert got + + +def test_i16_forget_drops_the_fetch_record_of_the_generation_but_not_the_lock(tmp_path): + store, state = tmp_path / "store", tmp_path / "state" + for g in ("old", "new"): + accounts.write_fetch(accounts.fetch_path(store, "work1", g), _got(five_hour=(10.0, LATER)), NOW) + with accounts.fetch_lock(store, "work1", 0): + pass + accounts.forget(state, store, "work1", "old") + assert sorted(p.name for p in accounts.fetch_path(store, "work1", "new").parent.iterdir()) == ["new.json"] + accounts.forget(state, store, "work1", "new") + assert not accounts.fetch_path(store, "work1", "new").parent.exists() + assert accounts.fetch_lock_path(store, "work1").is_file() + + +def test_i16_fetch_gate_close_waits_for_a_get_in_flight_and_refuses_new_ones(): + gate = accounts.FetchGate() + assert gate.start_get() and gate.begin_refresh() + closed = threading.Event() + t = threading.Thread(target=lambda: (gate.close(), closed.set())) + t.start() + time.sleep(0.05) + assert not closed.is_set() # 送信中の GET と更新の予約が終わるまで待つ + gate.end_get() + time.sleep(0.05) + assert not closed.is_set() + gate.end_refresh() + t.join(2) + assert closed.is_set() + assert not gate.start_get() and not gate.begin_refresh() # 閉じた後は始めない From 723d8176f45a54c5130c6b92cecff7a62fa080bf Mon Sep 17 00:00:00 2001 From: "takemi.ohama" Date: Sun, 22 Nov 2026 00:43:27 +0000 Subject: [PATCH 02/11] =?UTF-8?q?Add:=20=E4=BD=BF=E7=94=A8=E7=8E=87?= =?UTF-8?q?=E3=81=AE=20API=20=E3=81=AE=E5=8F=96=E5=BE=97=E3=81=AE=E8=A8=98?= =?UTF-8?q?=E9=8C=B2=E3=83=BB=E5=8F=96=E5=BE=97=E3=81=AE=E6=8E=92=E4=BB=96?= =?UTF-8?q?=E3=83=BB=E5=8F=96=E3=82=8A=E7=9B=B4=E3=81=97=E3=81=AE=E5=88=A4?= =?UTF-8?q?=E5=AE=9A=E3=82=92=E8=B6=B3=E3=81=97=E3=80=81=E7=99=BB=E9=8C=B2?= =?UTF-8?q?=E3=81=A8=E5=89=8A=E9=99=A4=E3=82=92=E5=8F=96=E5=BE=97=E3=81=AE?= =?UTF-8?q?=E6=8E=92=E4=BB=96=E3=81=AE=E4=B8=AD=E3=81=A7=E8=A1=8C=E3=81=86?= =?UTF-8?q?=EF=BC=88#3604=20#3864=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fetch_path・fetch_lock_path・fetch_lock・read_windows・write_fetch・due・FetchGate を足し、usages が 取得の記録を読み、forget が取得の記録を消すようにした。契約に relay-usage-fetch・relay-account-store と 上限の値 4 つを足した。fetch_usage・_refresh・_read_auth・_refresh_lock は起動の前の確かめ(U2・U3)の部品を 使うため、この PR には入れていない。 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KYHP7z4QjHh8vkcxvdbGUP --- plugins/ndf/contracts/domains/locations.json | 1 + plugins/ndf/contracts/domains/ndf-files.json | 1 + plugins/ndf/contracts/limits.json | 4 + plugins/ndf/docs/contracts/files.md | 1 + plugins/ndf/docs/contracts/index.md | 2 +- plugins/ndf/src/ndf/domains/relay/accounts.py | 181 ++++++++++++++++-- .../ndf/src/ndf/domains/relay/cli_account.py | 24 ++- .../domains/relay/test_relay_usage_fetch.py | 14 +- 8 files changed, 198 insertions(+), 30 deletions(-) diff --git a/plugins/ndf/contracts/domains/locations.json b/plugins/ndf/contracts/domains/locations.json index 4c5cea9c2..8bf1f86e7 100644 --- a/plugins/ndf/contracts/domains/locations.json +++ b/plugins/ndf/contracts/domains/locations.json @@ -27,6 +27,7 @@ {"id": "issue-request", "root": "env", "path": ".", "tracked": false, "env": "NDF_ISSUE_REQUEST_FILE"}, {"id": "relay-wrappers", "root": "state", "path": "relay/wrappers", "tracked": false}, {"id": "relay-accounts", "root": "state", "path": "relay/accounts", "tracked": false}, + {"id": "relay-account-store", "root": "config", "path": "ndf/accounts", "tracked": false, "env": "NDF_ACCOUNTS_DIR"}, {"id": "relay-markers", "root": "state", "path": "relay/markers", "tracked": false}, {"id": "relay-copy", "root": "config", "path": "ndf", "tracked": false}, {"id": "relay-plugin-update", "root": "config", "path": "ndf/plugin-update.lock", "tracked": false} diff --git a/plugins/ndf/contracts/domains/ndf-files.json b/plugins/ndf/contracts/domains/ndf-files.json index 0f4a621ea..c522b7ea8 100644 --- a/plugins/ndf/contracts/domains/ndf-files.json +++ b/plugins/ndf/contracts/domains/ndf-files.json @@ -78,6 +78,7 @@ {"id": "relay-question", "location": "relay-wrappers", "path": "<名前>/question.json", "kind": "exchange", "tracked": false, "writer": "PreToolUse の hook(M3a)", "ends": "run"}, {"id": "relay-stop", "location": "relay-wrappers", "path": "<名前>/stop", "kind": "exchange", "tracked": false, "writer": "ndf relay stop(M3b)と人(touch)", "ends": "run"}, {"id": "relay-usage", "location": "relay-accounts", "path": "usage/<名前>/<世代>.json", "kind": "measured", "tracked": false, "writer": "NDF の status line(statusline.sh)", "ends": "replaced", "keys": ["windows", "recorded_at"]}, + {"id": "relay-usage-fetch", "location": "relay-account-store", "path": ".usage-fetch/<名前>/<世代>.json", "kind": "measured", "tracked": false, "writer": "中継のラッパー(使用率の API の取得)", "ends": "replaced", "keys": ["at", "error", "spend_limit_reached", "windows"]}, {"id": "relay-auth-held", "location": "relay-accounts", "path": "held.json", "kind": "measured", "tracked": false, "writer": "中継のラッパー(M3b)", "ends": "replaced", "keys": []}, {"id": "relay-limit-seen", "location": "relay-accounts", "path": "limited.json", "kind": "measured", "tracked": false, "writer": "中継のラッパー(M3b)", "ends": "replaced", "keys": []}, {"id": "gws-install-material", "location": "google-workspace", "path": "install.md", "kind": "document", "tracked": false, "writer": "gws の確認(S1d)", "ends": "replaced"}, diff --git a/plugins/ndf/contracts/limits.json b/plugins/ndf/contracts/limits.json index 1b072f278..8113aa117 100644 --- a/plugins/ndf/contracts/limits.json +++ b/plugins/ndf/contracts/limits.json @@ -145,6 +145,10 @@ "relay.signal-lock": {"summary": "シグナルファイル(次のセグメントのマーカー・質問)の排他を待つ上限。hook の締め切りの中に収める", "seconds": 0.3}, "relay.auth-hold": {"summary": "認証の失敗で、そのアカウントを替え先と指定の候補から外す秒(#3864)", "seconds": 3600}, "relay.limit-unknown": {"summary": "上限の行を見たアカウントの使用率の記録に有効な窓が無いとき、上限として扱う秒(5 時間の窓の長さ)", "seconds": 18000}, + "relay.usage-fetch": {"summary": "1 つのアカウントの使用率の API を呼ぶ間隔の下限。取得の記録の at で数え、プロセスをまたぐ。間隔の中では、最後に試した後に過ぎたリセット 1 つにつき 1 回だけ取り直す(決定 4)", "seconds": 300}, + "relay.usage-http": {"summary": "使用率の API とトークンの更新の API の 1 回の要求の上限(決定 4)", "seconds": 10}, + "relay.oauth-refresh": {"summary": "トークンを更新する API の応答を、更新の排他の中で待つ上限(決定 4)", "seconds": 3}, + "relay.token-margin": {"summary": "残りがこの秒以下のアクセストークンを期限切れとみなす(決定 4)", "seconds": 60}, "relay.usage-guard": {"summary": "利用上限の前にカットポイントでアカウントを替える使用率(百分率。#3336)", "count": 90}, "relay.transcript-tail": {"summary": "ラッパーが文脈の量を読む会話の記録の末尾の大きさ(バイト)", "count": 262144}, "relay.context": {"summary": "中継の下のセッションを次のセグメントへ切り替える文脈の量(token。#2595)", "count": 800000}, diff --git a/plugins/ndf/docs/contracts/files.md b/plugins/ndf/docs/contracts/files.md index 6bf200387..a9ac78465 100644 --- a/plugins/ndf/docs/contracts/files.md +++ b/plugins/ndf/docs/contracts/files.md @@ -65,6 +65,7 @@ NDF の工程が書くファイル(設定・計測値・実行の記録・文 | `relay-question` | `relay-wrappers` state:relay/wrappers | `<名前>/question.json` | exchange | — | PreToolUse の hook(M3a) | run | 内部(予告なく変えてよい) | | `relay-stop` | `relay-wrappers` state:relay/wrappers | `<名前>/stop` | exchange | — | ndf relay stop(M3b)と人(touch) | run | 内部(予告なく変えてよい) | | `relay-usage` | `relay-accounts` state:relay/accounts | `usage/<名前>/<世代>.json` | measured | — | NDF の status line(statusline.sh) | replaced | 内部(予告なく変えてよい) | +| `relay-usage-fetch` | `relay-account-store` config:ndf/accounts(NDF_ACCOUNTS_DIR で差し替え) | `.usage-fetch/<名前>/<世代>.json` | measured | — | 中継のラッパー(使用率の API の取得) | replaced | 内部(予告なく変えてよい) | | `relay-wrapper` | `relay-wrappers` state:relay/wrappers | `<名前>/wrapper.json` | exchange | — | 中継のラッパー(M3b) | replaced | 内部(予告なく変えてよい) | | `relay-wrapper-lock` | `relay-wrappers` state:relay/wrappers | `<名前>/relay.lock` | exchange | — | 中継のラッパー(M3b) | replaced | 内部(予告なく変えてよい) | | `relay-wrapper-log` | `relay-wrappers` state:relay/wrappers | `<名前>/log.jsonl` | record | — | 中継のラッパー(M3b) | cleanup | 内部(予告なく変えてよい) | diff --git a/plugins/ndf/docs/contracts/index.md b/plugins/ndf/docs/contracts/index.md index 830edbea0..26f341941 100644 --- a/plugins/ndf/docs/contracts/index.md +++ b/plugins/ndf/docs/contracts/index.md @@ -14,7 +14,7 @@ NDF を外から呼ぶ面(設定・サブコマンド・環境変数・受け | [サブコマンドの引数と結果](commands.md) | 公開 0・内部 28 | | [終了コードと停止理由](exit-codes.md) | 終了コード 11・停止理由 33 | | [環境変数](env.md) | 公開 10・内部 69 | -| [受け渡しのファイル](files.md) | 公開 12・内部 58 | +| [受け渡しのファイル](files.md) | 公開 12・内部 59 | | [hook の入出力](hooks.md) | 公開 6・内部 11 | | [Skill と起動スクリプト](skills.md) | 公開 1・内部 48 | diff --git a/plugins/ndf/src/ndf/domains/relay/accounts.py b/plugins/ndf/src/ndf/domains/relay/accounts.py index f3c958a8a..47e9f6125 100644 --- a/plugins/ndf/src/ndf/domains/relay/accounts.py +++ b/plugins/ndf/src/ndf/domains/relay/accounts.py @@ -8,17 +8,25 @@ 使用率は、アカウントごと・登録の世代ごとのキャッシュ(`usage/<名前>/<世代>.json`)を NDF の status line が書き、 中継は読むだけである(#3864 の決定 4)。認証の失敗で外した間は `held.json`、上限の行の観測は `limited.json` が持ち、 どちらも名前の下に世代ごとの項目を持つ(書き手は自分の世代の項目だけを書く)。 + +使用率の API の取得の記録(`<アカウントの登録簿>/.usage-fetch/<名前>/<世代>.json`)は登録簿の側に置き、状態の保存先の +違うラッパーも同じ記録で取り直しを判定して同じ窓を読む(I16)。取得の重複は名前ごとの取得の排他(`fetch_lock`)で防ぐ。 """ from __future__ import annotations import json +import math +import os import re +import threading +from contextlib import contextmanager from datetime import datetime, timedelta, tzinfo from pathlib import Path, PurePosixPath -from typing import Any, Mapping, Optional +from typing import Any, Iterator, Mapping, Optional from zoneinfo import ZoneInfo +from ndf.adapters import claude_usage from ndf.contracts import facts, limits from ndf.domains.relay import account_decide, account_registry, signals @@ -27,6 +35,7 @@ SCOPED = "seven_day_scoped" # モデル別の週の窓。窓の名前は `seven_day_scoped/<識別>` FULL = 100.0 WINDOW_WORDS = (("five_hour", re.compile(r"\bsession\b", re.I)), ("seven_day", re.compile(r"\bweek(?:ly)?\b", re.I))) +FETCH_LOCKS = ".fetch-locks" # 取得の排他の錠の保存先(登録簿の下。アカウントのディレクトリの外) RESETS_RE = re.compile(r"\bresets\s+(\d{1,2})(?::(\d{2}))?\s*([ap]m)\b(?:\s*\(([^)]+)\))?", re.I) @@ -175,6 +184,16 @@ def _merge(prev: Optional[tuple[float, float]], new: tuple[float, float], now: f return (max(prev[0], new[0]), prev[1]) +def _merged(prev: Windows, windows: Mapping[str, tuple[float, float]], now: float) -> Windows: + """前の窓へ新しい窓を窓ごとに合わせる(書き込みの規則)。入力に無い窓は前の値を残す。""" + merged = dict(prev) + for k, w in windows.items(): + got = _merge(prev.get(k), w, now) + if got is not None: + merged[k] = got + return merged + + def write_usage(path: Path, windows: Mapping[str, tuple[float, float]], now: float) -> None: """status line と同じ形・同じ合わせ方で 1 つの記録を書く。錠が取れなければその回は書かない。 @@ -189,11 +208,7 @@ def write_usage(path: Path, windows: Mapping[str, tuple[float, float]], now: flo if not got: return prev = _read_windows(path) - merged = dict(prev) - for k, w in windows.items(): - got_w = _merge(prev.get(k), w, now) - if got_w is not None: - merged[k] = got_w + merged = _merged(prev, windows, now) if merged != prev: signals.replace_json(path, {"windows": _nested(merged), "recorded_at": now}) @@ -209,13 +224,21 @@ def usages( リセットの時刻を過ぎた窓は 0% として読み、上限は過ぎていない窓のどれかが 100 以上か、上限の観測が続くとき。 読めない・窓の無い記録は返さない(使用率の分からないアカウント)。ただし上限の観測が続く名前は使用率の - 分からない上限の行(`percent` が None)で返す。`store` は登録簿の側の取得の記録を読む単位(U5b)で使う。 - `generations` を省くと旧い 1 つのキャッシュを読む(ラッパーの今の呼び手のため。`usage_path` の行)。 + 分からない上限の行(`percent` が None)で返す。`store` があれば登録簿の側の取得の記録(`fetch_path`)の窓を + `read_windows` で合わせて読み、その `spend_limit_reached` が真の名前を上限にする。`generations` を省くと旧い 1 つのキャッシュを読む(ラッパーの今の呼び手のため。`usage_path` の行)。 """ if generations is None or now is None: return _legacy_usages(state_dir) - seen = {n: _read_windows(usage_path(state_dir, n, g)) for n, g in generations.items()} - limited = limits_seen(state_dir, now, generations, seen) + seen, spent = {}, set() + for n, g in generations.items(): + cache = usage_path(state_dir, n, g) + if store is None: + seen[n] = _read_windows(cache) + continue + seen[n] = read_windows(cache, fetch_path(store, n, g)) + if _read_json(fetch_path(store, n, g)).get("spend_limit_reached") is True: + spent.add(n) + limited = set(limits_seen(state_dir, now, generations, seen)) | spent rows = [] for name in sorted(generations): w = seen[name] @@ -388,12 +411,11 @@ def _line_reset(line: str, seen_at: float, tz: tzinfo) -> Optional[tuple[float, def forget(state_dir: Path, store: Path, name: str, drop: str) -> None: - """`name` の `drop` の世代(操作した登録簿の前の世代)の外した間・上限の観測・キャッシュを消す。ほかの世代は残す。 + """`name` の `drop` の世代(操作した登録簿の前の世代)の外した間・上限の観測・キャッシュ・取得の記録を消す。ほかの世代は残す。 - 登録簿の排他の中で呼ぶ(`account_registry.register`・`unregister` の `settle`)。世代は登録ごとに新しいので、同じ状態の - 保存先を別の登録簿(`NDF_ACCOUNTS_DIR`)と共有していても、ほかの登録簿の同じ名前の記録は消さない。 - 登録簿の側の取得の記録(`store` の `.usage-fetch/<名前>/`)を消すのは、取得の記録を足す単位(U5b)で入れる。 - 錠を取れなければ `TimeoutError`、消せなければ `OSError`。 + 取得の排他(`fetch_lock`)と登録簿の排他の中で呼ぶ(`account_registry.register`・`unregister` の `settle`)。世代は登録 + ごとに新しいので、同じ状態の保存先を別の登録簿(`NDF_ACCOUNTS_DIR`)と共有していても、ほかの登録簿の同じ名前の記録は + 消さない。取得の排他の錠のファイル(`fetch_lock_path`)は消さない。錠を取れなければ `TimeoutError`、消せなければ `OSError`。 """ key = _gen(drop) @@ -406,8 +428,129 @@ def edit(data: dict[str, dict[str, dict[str, Any]]]) -> None: if name in entries and key in entries[name]: _rewrite(path, edit) cache = usage_path(state_dir, name, drop) - lock = _lock_path(cache) - for p in (cache, lock): + _unlink_all(cache, _lock_path(cache)) + _unlink_all(fetch_path(store, name, drop)) + + +def _unlink_all(*paths: Path) -> None: + """同じディレクトリのファイルを消し、ほかのファイルが残らなければディレクトリも消す(ほかの世代が残れば残す)。""" + for p in paths: p.unlink(missing_ok=True) - if cache.parent.is_dir() and not any(cache.parent.iterdir()): # ほかの世代のファイルが残れば名前のディレクトリも残す - cache.parent.rmdir() + parent = paths[0].parent + if parent.is_dir() and not any(parent.iterdir()): + parent.rmdir() + + +# ---------------------------------------------------------------- 使用率の API の取得の記録と取得の排他(決定 4・I16) + + +def fetch_path(store: Path, name: str, generation: str) -> Path: + """取得の記録(ファイルの表の `relay-usage-fetch`。`<登録簿>/.usage-fetch/<名前>/<世代>.json`。世代が空文字なら `_.json`)。""" + if not account_registry.valid_name(name): + raise AccountError(f"アカウントの名前の形が違う: {name}") + top = PurePosixPath(facts.value("ndf-files", "relay-usage-fetch", "path")).parts[0] + return store / top / name / f"{_gen(generation)}.json" + + +def fetch_lock_path(store: Path, name: str) -> Path: + """取得の排他の錠(`<登録簿>/.fetch-locks/<名前>.lock`)。状態の保存先と世代によらず、登録簿と名前で 1 つ。""" + if not account_registry.valid_name(name): + raise AccountError(f"アカウントの名前の形が違う: {name}") + return store / FETCH_LOCKS / f"{name}.lock" + + +@contextmanager +def fetch_lock(store: Path, name: str, wait: float) -> Iterator[bool]: + """取得の排他を `wait` 秒まで取りにいく(取り方はこれだけ。`flock` で、オーナーが生きている間は奪えず、死ねば OS が解く)。""" + path = fetch_lock_path(store, name) + os.makedirs(path.parent, mode=0o700, exist_ok=True) + with signals.held(path, wait) as got: + yield got + + +def fetch_wait() -> float: + """登録・削除のコマンドが取得の排他を待つ上限(1 回の取得が排他を持つ最長)。""" + http_s, refresh_s = limits.seconds("relay.usage-http"), limits.seconds("relay.oauth-refresh") + return http_s * 2 + refresh_s + limits.seconds("claude-accounts.lock") * 4 + + +def read_windows(usage: Path, fetch: Path) -> Windows: + """status line のキャッシュと取得の記録の窓を読み取り用に合わせる(新しい `resets_at`、同じなら大きいほう。失効した窓も残す)。""" + return _merged(_read_windows(usage), _read_windows(fetch), -math.inf) + + +def write_fetch(path: Path, got: claude_usage.Usage, now: float) -> None: + """成功した取得の窓を取得の記録へ窓ごとに合わせて書く(取得の排他の中で呼ぶ。書き手は同時に 1 つ)。 + + `error` を null にし、`spend_limit_reached` は応答が真偽を持つときだけ置き換える。`at` は前の値を残す。 + """ + prev = _read_json(path) + windows = {k: w for k, w in (("five_hour", got.five_hour), ("seven_day", got.seven_day)) if w is not None} + if got.scoped is not None: # 崩れていた(None)ならモデル別の窓は前の値を残す + windows.update({f"{SCOPED}/{ident}": (p, r) for ident, p, r in got.scoped}) + spend = got.spend_limit_reached if got.spend_limit_reached is not None else prev.get("spend_limit_reached") + row = { + "at": _number(prev.get("at")), + "error": None, + "spend_limit_reached": spend if isinstance(spend, bool) else None, + "windows": _nested(_merged(_flat(prev.get("windows")), windows, now)), + } + os.makedirs(path.parent, mode=0o700, exist_ok=True) + signals.replace_json(path, row) + + +def due(usage: Path, fetch: Path, now: float, interval: float, until: Optional[float], tried: Optional[float]) -> bool: + """取り直すか(I16)。最後に試した時刻から `interval` を過ぎたか、その後に過ぎたリセット(窓・上限の観測の `until`)があれば真。 + + 最後に試した時刻は取得の記録の `at` と `tried`(このラッパーが最後にキューへ入れた時刻)の大きいほう。 + """ + tried_at = [t for t in (_number(_read_json(fetch).get("at")), tried) if t is not None] + if not tried_at: + return True + last = max(tried_at) + if now - last >= interval: + return True + resets = [r for _, r in read_windows(usage, fetch).values()] + if until is not None: + resets.append(until) + return any(last < t <= now for t in resets) + + +class FetchGate: + """ラッパー 1 つに 1 つの、停止と GET の開始と更新の予約の排他。`close` の後は GET も更新も始めない。""" + + def __init__(self) -> None: + self._cv = threading.Condition() + self._closed = False + self._gets = 0 + self._refreshes = 0 + + def _enter(self, attr: str) -> bool: + with self._cv: + if self._closed: + return False + setattr(self, attr, getattr(self, attr) + 1) + return True + + def _leave(self, attr: str) -> None: + with self._cv: + setattr(self, attr, getattr(self, attr) - 1) + self._cv.notify_all() + + def start_get(self) -> bool: + return self._enter("_gets") + + def end_get(self) -> None: + self._leave("_gets") + + def begin_refresh(self) -> bool: + return self._enter("_refreshes") + + def end_refresh(self) -> None: + self._leave("_refreshes") + + def close(self) -> None: + """閉じたことを記録し、送信中の GET と更新の予約がどちらも 0 になるまで待つ(どちらの往復も期限を持つ)。""" + with self._cv: + self._closed = True + self._cv.wait_for(lambda: self._gets == 0 and self._refreshes == 0) diff --git a/plugins/ndf/src/ndf/domains/relay/cli_account.py b/plugins/ndf/src/ndf/domains/relay/cli_account.py index 7e0afb60d..0cc327d96 100644 --- a/plugins/ndf/src/ndf/domains/relay/cli_account.py +++ b/plugins/ndf/src/ndf/domains/relay/cli_account.py @@ -11,8 +11,9 @@ import shutil import sys import time +from contextlib import contextmanager from pathlib import Path -from typing import Any, Callable, NamedTuple, Optional +from typing import Any, Callable, Iterator, NamedTuple, Optional from ndf import cli, identity from ndf.contracts import result @@ -74,11 +75,27 @@ def run(gone: Optional[str]) -> None: return run +@contextmanager +def _fetching(store: Path, name: str) -> Iterator[None]: + """世代を変える操作の前に、同じ名前の取得の排他を待って取る(I16)。取れなければ `AccountLockTimeout`。 + + 形の違う名前は排他を取らずに通し、名前の検査は呼んだ操作に任せる。 + """ + if not account_registry.valid_name(name): + yield + return + with accounts.fetch_lock(store, name, accounts.fetch_wait()) as got: + if not got: + raise account_registry.AccountLockTimeout("使用率の取得が終わらない。少し待ってから打ち直す") + yield + + def _acc_add(store: Path, a: argparse.Namespace) -> int: code = (sys.stdin.readline() if a.code == "-" else a.code) if a.code is not None else None warnings: list[str] = [] forget = _forget_after(identity.load().state_dir, store, a.name, warnings) - got = account_login.add(store, a.name, code, os.environ, settle=lambda old, _new: forget(old)) + with _fetching(store, a.name): + got = account_login.add(store, a.name, code, os.environ, settle=lambda old, _new: forget(old)) if got.needs_code: detail = f"認可の URL: {got.url} をブラウザで開き、出た認可コードを {got.expires_at} までに渡す: printf '%s\\n' '<コード>' | ndf relay account add {a.name} --code -" return result.emit( @@ -165,7 +182,8 @@ def release() -> bool: return account_login.logout(store, name, os.environ) warnings: list[str] = [] - got = account_registry.remove_account(store, name, release, _forget_after(identity.load().state_dir, store, name, warnings)) + with _fetching(store, name): + got = account_registry.remove_account(store, name, release, _forget_after(identity.load().state_dir, store, name, warnings)) item = {"name": name, "kind": "oauth"} if got.leftovers: # 前の削除が消し残した世代だけを消した。今の登録は外していない(外すのは残りが無くなった後の削除) diff --git a/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py b/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py index dd658c503..b49d3d27c 100644 --- a/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py +++ b/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py @@ -1,6 +1,6 @@ """中継のアカウントの使用率の API の取得の記録・取得の排他・取り直しの判定(#3864 の決定 4、I16)。 -偽の宛先と認証ファイルを使う取得の手順(`fetch_usage`・`_refresh`)は、起動の前の確かめ(U2・U3)の部品の後に足す。 +偽の API と認証ファイルを使う取得の手順(`fetch_usage`・`_refresh`)は、起動の前の確かめ(U2・U3)の部品の後に足す。 """ from __future__ import annotations @@ -38,7 +38,7 @@ def test_decision4_fetch_record_lives_in_the_store_and_reads_from_any_state_dir( rec = _fetch_record(path) assert rec["error"] is None and rec["windows"]["seven_day_scoped"] == {"opus": {"percent": 95.0, "resets_at": WEEK}} assert stat.S_IMODE(path.parent.stat().st_mode) == 0o700 - # 状態の保存先には何も書かず、保存先の違う 2 つのラッパーが同じ記録を読む(モデル別の週の枠 95 を使用率に読む) + # 状態の保存先には何も書かず、保存先の違う 2 つのラッパーが同じ記録を読む(モデル別の週の窓 95 を使用率に読む) for state in (state_a, state_b): assert accounts.usages(state, store, NOW, {"work2": "g2"}) == [Usage("work2", 95.0, False)] assert not accounts.usage_dir(state).exists() @@ -64,13 +64,13 @@ def test_decision4_write_fetch_keeps_model_windows_apart_and_missing_windows(tmp path = accounts.fetch_path(tmp_path, "work1", "g1") thursday, friday = NOW + 4 * 86400, NOW + 5 * 86400 accounts.write_fetch(path, _got(five_hour=(10.0, LATER), scoped=[("a", 95.0, friday)]), NOW) - # 次の応答で別の識別の枠 B が 100。A と B を別々に残し、7 日を 100 と読む。応答に無い 5 時間の窓は前の値が残る + # 次の応答で別の識別の窓 B が 100。A と B を別々に残し、7 日を 100 と読む。応答に無い 5 時間の窓は前の値が残る accounts.write_fetch(path, _got(scoped=[("b", 100.0, thursday)]), NOW) windows = _fetch_record(path)["windows"] assert windows["seven_day_scoped"] == {"a": {"percent": 95.0, "resets_at": friday}, "b": {"percent": 100.0, "resets_at": thursday}} assert windows["five_hour"] == {"percent": 10.0, "resets_at": LATER} assert accounts.usages(tmp_path / "state", tmp_path, NOW, {"work1": "g1"}) == [Usage("work1", 100.0, True)] - # 崩れたモデル別の枠(None)は前の値を残し、古い入力・失効した入力で下げない + # 崩れたモデル別の窓(None)は前の値を残し、古い入力・失効した入力で下げない accounts.write_fetch(path, _got(five_hour=(3.0, LATER - 60), scoped=None), NOW) accounts.write_fetch(path, _got(five_hour=(1.0, NOW - 1)), NOW) assert _fetch_record(path)["windows"] == windows @@ -124,7 +124,7 @@ def test_i16_due_once_per_reset_after_the_last_attempt(tmp_path): # そのリセットで試した後は、取得が失敗して過ぎた窓が残っても真にならない _attempted(tmp_path, NOW + 120, {"five_hour": {"percent": 100.0, "resets_at": NOW + 100}}) assert not accounts.due(usage, fetch, NOW + 150, INTERVAL, None, None) - # status line のキャッシュのモデル別の枠のリセットと、上限の観測の until でも取り直す + # status line のキャッシュのモデル別の窓のリセットと、上限の観測の until でも取り直す _attempted(tmp_path, NOW) accounts.write_usage(usage, {"seven_day_scoped/opus": (90.0, NOW + 100)}, NOW) assert accounts.due(usage, fetch, NOW + 150, INTERVAL, None, None) @@ -148,10 +148,10 @@ def test_i16_fetch_lock_is_one_per_store_and_name_and_never_taken_from_a_live_ow with accounts.fetch_lock(store, "work2", 0) as other: assert other fd = hold_lock(lock) - os.utime(lock, (NOW - 86400, NOW - 86400)) # 古いファイルでも、持ち主が生きている間は奪わない + os.utime(lock, (NOW - 86400, NOW - 86400)) # 古いファイルでも、オーナーが生きている間は奪わない with accounts.fetch_lock(store, "work1", 0) as got: assert not got - os.close(fd) # 持ち主が死ねば取れる + os.close(fd) # オーナーが死ねば取れる with accounts.fetch_lock(store, "work1", 0) as got: assert got From 6123fab947aed48bb1da9b9e4a315b53712a6d3b Mon Sep 17 00:00:00 2001 From: "takemi.ohama" Date: Sun, 22 Nov 2026 00:46:33 +0000 Subject: [PATCH 03/11] =?UTF-8?q?Fix:=20=E5=89=8A=E9=99=A4=E3=81=AE?= =?UTF-8?q?=E6=89=93=E3=81=A1=E7=9B=B4=E3=81=97=E3=81=A7=E3=80=81=E5=8F=96?= =?UTF-8?q?=E5=BE=97=E3=81=AE=E6=8E=92=E4=BB=96=E3=82=92=E5=BE=85=E3=81=A4?= =?UTF-8?q?=E5=89=8D=E3=81=AB=E6=AE=8B=E3=82=8A=E3=82=92=E8=A6=8B=E3=81=A6?= =?UTF-8?q?=E3=80=81=E5=BE=85=E3=81=A4=E9=96=93=E3=81=AB=E6=AE=8B=E3=82=8A?= =?UTF-8?q?=E3=81=8C=E7=89=87=E4=BB=98=E3=81=84=E3=81=A6=E3=82=82=E4=BB=8A?= =?UTF-8?q?=E3=81=AE=E7=99=BB=E9=8C=B2=E3=82=92=E5=A4=96=E3=81=95=E3=81=AA?= =?UTF-8?q?=E3=81=84=EF=BC=88#3604=20#3864=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- .../src/ndf/domains/relay/account_registry.py | 13 ++++++-- .../ndf/src/ndf/domains/relay/cli_account.py | 5 +++- .../ndf/tests/domains/relay/test_relay_cli.py | 30 +++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/plugins/ndf/src/ndf/domains/relay/account_registry.py b/plugins/ndf/src/ndf/domains/relay/account_registry.py index ddec96d56..031920505 100644 --- a/plugins/ndf/src/ndf/domains/relay/account_registry.py +++ b/plugins/ndf/src/ndf/domains/relay/account_registry.py @@ -452,7 +452,13 @@ class Removal(NamedTuple): logged_out: Optional[bool] = None # 今の登録を外したときのログアウトの成否(残りだけを消したときは None) -def remove_account(store: Path, name: str, release: Callable[[], bool], settle: Optional[Callable[[str], None]] = None) -> Removal: +def remove_account( + store: Path, + name: str, + release: Callable[[], bool], + settle: Optional[Callable[[str], None]] = None, + seen: Optional[bool] = None, +) -> Removal: """`account remove <名前>` の本体。すべてを登録(`register`)と同じ排他の中で行う。 1. 前の削除の残り(`removing`)があれば、残った世代ごとの `settle` と削除だけをやり直して返す。今の登録には触れない @@ -462,11 +468,14 @@ def remove_account(store: Path, name: str, release: Callable[[], bool], settle: 排他を待つ前に残りを見ていれば、残りの後始末の呼び出しとして扱う。待つ間にほかの端末が残りを片付けても、2 へ進まずに返す (残りの後始末として打ち直した呼び出しが、残りの後に登録し直された今の登録を外さない)。 + 呼び出し元がこの前にほかの排他(使用率の取得の排他)を待つときは、それを待つ前に見た `removing` を `seen` へ渡す。 + 省けばここで見る。 登録も残りも無ければ `NotRegistered`、消え残れば `OSError`(打ち直すと 1 から)。 """ check_name(name) - seen = removing(store, name) + if seen is None: + seen = removing(store, name) with locked(store, name): if seen or removing(store, name): _finish_removal(store, name, settle) diff --git a/plugins/ndf/src/ndf/domains/relay/cli_account.py b/plugins/ndf/src/ndf/domains/relay/cli_account.py index 0cc327d96..30f6a134b 100644 --- a/plugins/ndf/src/ndf/domains/relay/cli_account.py +++ b/plugins/ndf/src/ndf/domains/relay/cli_account.py @@ -182,8 +182,11 @@ def release() -> bool: return account_login.logout(store, name, os.environ) warnings: list[str] = [] + # 取得の排他を待つ前に残りを見る。待つ間にほかの端末が残りを片付けても、この呼び出しは残りの後始末だけで返す + seen = account_registry.removing(store, name) with _fetching(store, name): - got = account_registry.remove_account(store, name, release, _forget_after(identity.load().state_dir, store, name, warnings)) + forget = _forget_after(identity.load().state_dir, store, name, warnings) + got = account_registry.remove_account(store, name, release, forget, seen=seen) item = {"name": name, "kind": "oauth"} if got.leftovers: # 前の削除が消し残した世代だけを消した。今の登録は外していない(外すのは残りが無くなった後の削除) diff --git a/plugins/ndf/tests/domains/relay/test_relay_cli.py b/plugins/ndf/tests/domains/relay/test_relay_cli.py index 0d474a28e..867606abe 100644 --- a/plugins/ndf/tests/domains/relay/test_relay_cli.py +++ b/plugins/ndf/tests/domains/relay/test_relay_cli.py @@ -629,6 +629,36 @@ def test_i8_remove_rerun_after_a_new_registration_keeps_it(tmp_path, monkeypatch assert len(logouts) == 2 and account_registry.names(store) == [] +def test_i8_remove_rerun_keeps_a_new_registration_when_leftovers_vanish_during_the_fetch_wait(tmp_path, monkeypatch, capsys, last_result): + from ndf.domains.relay import account_login, account_registry + + store = tmp_path / "store" + monkeypatch.setenv("NDF_ACCOUNTS_DIR", str(store)) + _relogin(monkeypatch, tmp_path) + assert cli.main(["account", "add", "work1", "--code", "c"]) == 0 + monkeypatch.setattr(cli_account.shutil, "which", lambda _: None) + logouts = [] + monkeypatch.setattr(account_login, "logout", lambda *a: logouts.append(a) or True) + real = account_registry.shutil.rmtree + monkeypatch.setattr(account_registry.shutil, "rmtree", lambda path, ignore_errors=False: None) + assert cli.main(["account", "remove", "work1"]) != 0 + monkeypatch.setattr(account_registry.shutil, "rmtree", real) + assert cli.main(["account", "add", "work1", "--code", "c"]) == 0 + new = account_registry.generation(store, "work1") + capsys.readouterr() + real_lock = accounts.fetch_lock + + def other_terminal_first(s, n, wait): # 取得の排他を待つ間に、別の端末の打ち直しが残りを片付けた + account_registry._finish_removal(s, n, None) + return real_lock(s, n, wait) + + monkeypatch.setattr(accounts, "fetch_lock", other_terminal_first) + assert cli.main(["account", "remove", "work1"]) == 0 + got = last_result(capsys.readouterr().out) + assert got["status"] == "ok" and "残りを消した" in got["summary"] + assert len(logouts) == 1 and account_registry.names(store) == ["work1"] and account_registry.generation(store, "work1") == new + + def test_i16_add_and_remove_wait_for_the_fetch_lock_and_forget_inside_it(tmp_path, monkeypatch, capsys, last_result, hold_lock): from ndf.domains.relay import account_login, account_registry From 763c93c4ed17e560b3304b111380625efdabbe6f Mon Sep 17 00:00:00 2001 From: "takemi.ohama" Date: Sun, 22 Nov 2026 00:47:42 +0000 Subject: [PATCH 04/11] =?UTF-8?q?Fix:=20usages=20=E3=81=A7=E5=8F=96?= =?UTF-8?q?=E5=BE=97=E3=81=AE=E8=A8=98=E9=8C=B2=E3=82=92=201=20=E5=BA=A6?= =?UTF-8?q?=E3=81=A0=E3=81=91=E8=AA=AD=E3=81=BF=E3=80=81=E7=AA=93=E3=81=A8?= =?UTF-8?q?=E4=B8=8A=E9=99=90=E3=81=AE=E5=8D=B0=E3=82=92=E5=90=8C=E3=81=98?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E3=81=8B=E3=82=89=E5=8F=96=E3=82=8B=EF=BC=88?= =?UTF-8?q?#3604=20#3864=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- plugins/ndf/src/ndf/domains/relay/accounts.py | 12 ++++++++--- .../domains/relay/test_relay_usage_fetch.py | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/plugins/ndf/src/ndf/domains/relay/accounts.py b/plugins/ndf/src/ndf/domains/relay/accounts.py index 47e9f6125..9976918fe 100644 --- a/plugins/ndf/src/ndf/domains/relay/accounts.py +++ b/plugins/ndf/src/ndf/domains/relay/accounts.py @@ -235,8 +235,9 @@ def usages( if store is None: seen[n] = _read_windows(cache) continue - seen[n] = read_windows(cache, fetch_path(store, n, g)) - if _read_json(fetch_path(store, n, g)).get("spend_limit_reached") is True: + fetched = _read_json(fetch_path(store, n, g)) # 1 度だけ読み、窓と上限の印を同じ内容から取る(間の置き換えを混ぜない) + seen[n] = _read_merged(cache, fetched) + if fetched.get("spend_limit_reached") is True: spent.add(n) limited = set(limits_seen(state_dir, now, generations, seen)) | spent rows = [] @@ -476,7 +477,12 @@ def fetch_wait() -> float: def read_windows(usage: Path, fetch: Path) -> Windows: """status line のキャッシュと取得の記録の窓を読み取り用に合わせる(新しい `resets_at`、同じなら大きいほう。失効した窓も残す)。""" - return _merged(_read_windows(usage), _read_windows(fetch), -math.inf) + return _read_merged(usage, _read_json(fetch)) + + +def _read_merged(usage: Path, fetched: Mapping[str, Any]) -> Windows: + """`read_windows` の本体。取得の記録は読んだ後の内容で受け取る。""" + return _merged(_read_windows(usage), _flat(fetched.get("windows")), -math.inf) def write_fetch(path: Path, got: claude_usage.Usage, now: float) -> None: diff --git a/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py b/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py index b49d3d27c..31a05a891 100644 --- a/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py +++ b/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py @@ -88,6 +88,27 @@ def test_decision4_spend_limit_holds_until_an_explicit_false(tmp_path): assert accounts.usages(state, store, NOW, {"work2": "g2"}) == [Usage("work2", 10.0, False)] +def test_decision4_usages_reads_the_fetch_record_once_per_account(tmp_path, monkeypatch): + store, state = tmp_path / "store", tmp_path / "state" + path = accounts.fetch_path(store, "work2", "g2") + accounts.write_fetch(path, _got(five_hour=(10.0, LATER), spend_limit_reached=True), NOW) + real, reads = accounts._read_json, [] + + def replaced_after_first_read(p): # 1 度読んだ直後に、書き手が記録を「上限は解けた・使用率 100」へ置き換える + got = real(p) + if p == path: + reads.append(p) + if len(reads) == 1: + monkeypatch.setattr(accounts, "_read_json", real) # 書き手の読みは数えない + accounts.write_fetch(path, _got(five_hour=(100.0, LATER + 1), spend_limit_reached=False), NOW) + monkeypatch.setattr(accounts, "_read_json", replaced_after_first_read) + return got + + monkeypatch.setattr(accounts, "_read_json", replaced_after_first_read) + # 同じ 1 回の内容から窓と上限の印を取る(差し替え前の 10% と差し替え後の上限なしを組み合わせない) + assert accounts.usages(state, store, NOW, {"work2": "g2"}) == [Usage("work2", 10.0, True)] and len(reads) == 1 + + def test_decision4_write_fetch_keeps_the_attempt_time_and_clears_the_error(tmp_path): path = accounts.fetch_path(tmp_path, "work1", "g1") path.parent.mkdir(parents=True) From 5072ce448eea97605d8cd2665c9fd0361c1fee8a Mon Sep 17 00:00:00 2001 From: "takemi.ohama" Date: Sun, 22 Nov 2026 00:50:47 +0000 Subject: [PATCH 05/11] =?UTF-8?q?fix(relay):=20account=20remove=20?= =?UTF-8?q?=E3=81=AF=E5=89=8A=E9=99=A4=E3=81=AE=E6=AE=8B=E3=82=8A=E3=82=92?= =?UTF-8?q?=201=20=E5=BA=A6=E3=81=A0=E3=81=91=E8=A6=8B=E3=81=A6=E7=B5=8C?= =?UTF-8?q?=E8=B7=AF=E3=81=AE=E5=88=A4=E5=AE=9A=E3=81=A8=20remove=5Faccoun?= =?UTF-8?q?t=20=E3=81=AB=E4=BD=BF=E3=81=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 経路の判定で removing を見た結果を捨てて読み直すと、間に別の端末が残りを片付けて 同じ名前で登録し直したとき、打ち直しの削除が新しい登録をログアウトして外していた。 Co-Authored-By: Claude Opus 5.5 --- .../ndf/src/ndf/domains/relay/cli_account.py | 9 +++-- .../ndf/tests/domains/relay/test_relay_cli.py | 36 +++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/plugins/ndf/src/ndf/domains/relay/cli_account.py b/plugins/ndf/src/ndf/domains/relay/cli_account.py index 30f6a134b..6ce24de17 100644 --- a/plugins/ndf/src/ndf/domains/relay/cli_account.py +++ b/plugins/ndf/src/ndf/domains/relay/cli_account.py @@ -169,7 +169,11 @@ def _acc_capacity(store: Path, a: argparse.Namespace) -> int: def _acc_remove(store: Path, a: argparse.Namespace) -> int: name = a.target - if not (account_registry.valid_name(name) and (name in account_registry.names(store) or account_registry.removing(store, name))): + valid = account_registry.valid_name(name) + # 残りは 1 度だけ見て、経路の判定と remove_account の両方に使う。読み直すと、間にほかの端末が残りを片付けて登録し直したとき + # 打ち直しの削除が新しい登録を外してしまう + seen = valid and account_registry.removing(store, name) + if not (valid and (seen or name in account_registry.names(store))): row = account_metered.remove(store, name) return result.emit(result.result(TOOL, result.OK, f"外した: {name}", items=[row])) @@ -182,8 +186,7 @@ def release() -> bool: return account_login.logout(store, name, os.environ) warnings: list[str] = [] - # 取得の排他を待つ前に残りを見る。待つ間にほかの端末が残りを片付けても、この呼び出しは残りの後始末だけで返す - seen = account_registry.removing(store, name) + # 残りは取得の排他を待つ前に見た(上の seen)。待つ間にほかの端末が残りを片付けても、この呼び出しは残りの後始末だけで返す with _fetching(store, name): forget = _forget_after(identity.load().state_dir, store, name, warnings) got = account_registry.remove_account(store, name, release, forget, seen=seen) diff --git a/plugins/ndf/tests/domains/relay/test_relay_cli.py b/plugins/ndf/tests/domains/relay/test_relay_cli.py index 867606abe..903123b9b 100644 --- a/plugins/ndf/tests/domains/relay/test_relay_cli.py +++ b/plugins/ndf/tests/domains/relay/test_relay_cli.py @@ -659,6 +659,42 @@ def other_terminal_first(s, n, wait): # 取得の排他を待つ間に、別の assert len(logouts) == 1 and account_registry.names(store) == ["work1"] and account_registry.generation(store, "work1") == new +def test_i8_remove_rerun_reads_leftovers_once_when_another_terminal_reregisters_right_after(tmp_path, monkeypatch, capsys, last_result): + from ndf.domains.relay import account_login, account_registry + + store = tmp_path / "store" + monkeypatch.setenv("NDF_ACCOUNTS_DIR", str(store)) + _relogin(monkeypatch, tmp_path) + assert cli.main(["account", "add", "work1", "--code", "c"]) == 0 + monkeypatch.setattr(cli_account.shutil, "which", lambda _: None) + logouts = [] + monkeypatch.setattr(account_login, "logout", lambda *a: logouts.append(a) or True) + real = account_registry.shutil.rmtree + monkeypatch.setattr(account_registry.shutil, "rmtree", lambda path, ignore_errors=False: None) + assert cli.main(["account", "remove", "work1"]) != 0 # 登録は外れ、残りだけがある + monkeypatch.setattr(account_registry.shutil, "rmtree", real) + assert account_registry.names(store) == [] and account_registry.removing(store, "work1") + capsys.readouterr() + real_removing = account_registry.removing + calls = [] + + def other_terminal_right_after(s, n): # 残りを見た直後に、別の端末が残りを片付けて同じ名前で登録し直した + got = real_removing(s, n) + if not calls: + calls.append(n) + account_registry._finish_removal(s, n, None) + assert cli.main(["account", "add", "work1", "--code", "c"]) == 0 + calls.append(account_registry.generation(s, n)) + return got + + monkeypatch.setattr(account_registry, "removing", other_terminal_right_after) + assert cli.main(["account", "remove", "work1"]) == 0 + got = last_result(capsys.readouterr().out) + assert got["status"] == "ok" and "残りを消した" in got["summary"] + assert len(logouts) == 1 and account_registry.names(store) == ["work1"] + assert account_registry.generation(store, "work1") == calls[1] + + def test_i16_add_and_remove_wait_for_the_fetch_lock_and_forget_inside_it(tmp_path, monkeypatch, capsys, last_result, hold_lock): from ndf.domains.relay import account_login, account_registry From 54469563ec6cbf388826dc4bdc6049128f480652 Mon Sep 17 00:00:00 2001 From: "takemi.ohama" Date: Sun, 22 Nov 2026 00:52:42 +0000 Subject: [PATCH 06/11] =?UTF-8?q?Fix:=20account=20remove=20=E3=81=A7?= =?UTF-8?q?=E7=99=BB=E9=8C=B2=E3=81=A8=E6=AE=8B=E3=82=8A=E3=81=AE=E4=B8=A1?= =?UTF-8?q?=E6=96=B9=E3=81=8C=E8=A6=8B=E3=81=88=E3=81=AA=E3=81=84=E3=81=A8?= =?UTF-8?q?=E3=81=8D=E3=81=AF=E5=8F=96=E5=BE=97=E3=81=A8=E7=99=BB=E9=8C=B2?= =?UTF-8?q?=E3=81=AE=E6=8E=92=E4=BB=96=E3=81=AE=E4=B8=8B=E3=81=A7=E8=A6=8B?= =?UTF-8?q?=E7=9B=B4=E3=81=97=E3=81=A6=E3=81=8B=E3=82=89=20API=20=E3=82=AD?= =?UTF-8?q?=E3=83=BC=E3=81=AE=E5=89=8A=E9=99=A4=E3=81=B8=E5=9B=9E=E3=81=99?= =?UTF-8?q?=EF=BC=88#3604=20#3864=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- .../ndf/src/ndf/domains/relay/cli_account.py | 9 +++++- .../ndf/tests/domains/relay/test_relay_cli.py | 32 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/plugins/ndf/src/ndf/domains/relay/cli_account.py b/plugins/ndf/src/ndf/domains/relay/cli_account.py index 6ce24de17..828349f0f 100644 --- a/plugins/ndf/src/ndf/domains/relay/cli_account.py +++ b/plugins/ndf/src/ndf/domains/relay/cli_account.py @@ -173,7 +173,14 @@ def _acc_remove(store: Path, a: argparse.Namespace) -> int: # 残りは 1 度だけ見て、経路の判定と remove_account の両方に使う。読み直すと、間にほかの端末が残りを片付けて登録し直したとき # 打ち直しの削除が新しい登録を外してしまう seen = valid and account_registry.removing(store, name) - if not (valid and (seen or name in account_registry.names(store))): + oauth = valid and (seen or name in account_registry.names(store)) + if valid and not oauth: + # 残りを見てから一覧を読むまでに、ほかの端末の削除が登録を残りの場所へ移したかもしれない。登録と残りの両方が + # 見えないときだけ、取得の排他と登録の排他の下で見直してから API キーの削除へ回す(最初に見た残りはそのまま使う) + with _fetching(store, name), account_registry.locked(store, name): + seen = account_registry.removing(store, name) + oauth = seen or name in account_registry.names(store) + if not oauth: row = account_metered.remove(store, name) return result.emit(result.result(TOOL, result.OK, f"外した: {name}", items=[row])) diff --git a/plugins/ndf/tests/domains/relay/test_relay_cli.py b/plugins/ndf/tests/domains/relay/test_relay_cli.py index 903123b9b..040dcdeb9 100644 --- a/plugins/ndf/tests/domains/relay/test_relay_cli.py +++ b/plugins/ndf/tests/domains/relay/test_relay_cli.py @@ -695,6 +695,38 @@ def other_terminal_right_after(s, n): # 残りを見た直後に、別の端末 assert account_registry.generation(store, "work1") == calls[1] +def test_i8_remove_rechecks_under_the_locks_when_another_terminal_moves_the_registration_right_after( + tmp_path, monkeypatch, capsys, last_result +): + from ndf.domains.relay import account_login, account_registry + + store = tmp_path / "store" + monkeypatch.setenv("NDF_ACCOUNTS_DIR", str(store)) + _relogin(monkeypatch, tmp_path) + assert cli.main(["account", "add", "work1", "--code", "c"]) == 0 + gen = account_registry.generation(store, "work1") + capsys.readouterr() + monkeypatch.setattr(cli_account.shutil, "which", lambda _: None) + monkeypatch.setattr(account_login, "logout", lambda *a: True) + real_removing = account_registry.removing + calls = [] + + def other_terminal_right_after(s, n): # 残りが無いと見た直後に、別の端末の削除が登録を残りの場所へ移して止まった + got = real_removing(s, n) + if not calls: + calls.append(n) + pending = account_registry.removing_dir(s, n) + pending.mkdir(parents=True) + os.replace(account_registry.account_dir(s, n), pending / gen) + return got + + monkeypatch.setattr(account_registry, "removing", other_terminal_right_after) + assert cli.main(["account", "remove", "work1"]) == 0 + got = last_result(capsys.readouterr().out) + assert got["status"] == "ok" and "残りを消した" in got["summary"] and got["items"][0]["kind"] == "oauth" + assert account_registry.names(store) == [] and not real_removing(store, "work1") + + def test_i16_add_and_remove_wait_for_the_fetch_lock_and_forget_inside_it(tmp_path, monkeypatch, capsys, last_result, hold_lock): from ndf.domains.relay import account_login, account_registry From 593b0bcd0e0c286da88c9098843097734c1064fb Mon Sep 17 00:00:00 2001 From: "takemi.ohama" Date: Sun, 22 Nov 2026 00:55:19 +0000 Subject: [PATCH 07/11] =?UTF-8?q?fix(relay):=20account=20remove=20?= =?UTF-8?q?=E3=81=AF=E6=8E=92=E4=BB=96=E3=82=92=E5=BE=85=E3=81=A4=E9=96=93?= =?UTF-8?q?=E3=81=AB=E3=81=A7=E3=81=8D=E3=81=9F=E5=90=8C=E5=90=8D=E3=81=AE?= =?UTF-8?q?=20OAuth=20=E3=81=AE=E7=99=BB=E9=8C=B2=E3=82=92=E6=B6=88?= =?UTF-8?q?=E3=81=95=E3=81=9A=E3=81=AB=E6=AD=A2=E3=81=BE=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 最初の確認で登録も残りも見えず、排他の下の見直しで登録だけが見えたときは、 待つ間にほかの端末の account add が登録したものとして store_in_use で止まり、 打ち直しを案内する。OAuth の削除へ回すのは残りが見えたときだけ(r4-1)。 Co-Authored-By: Claude Opus 5.5 --- .../ndf/src/ndf/domains/relay/cli_account.py | 7 +++++- .../ndf/tests/domains/relay/test_relay_cli.py | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/plugins/ndf/src/ndf/domains/relay/cli_account.py b/plugins/ndf/src/ndf/domains/relay/cli_account.py index 828349f0f..62ad5df06 100644 --- a/plugins/ndf/src/ndf/domains/relay/cli_account.py +++ b/plugins/ndf/src/ndf/domains/relay/cli_account.py @@ -179,7 +179,12 @@ def _acc_remove(store: Path, a: argparse.Namespace) -> int: # 見えないときだけ、取得の排他と登録の排他の下で見直してから API キーの削除へ回す(最初に見た残りはそのまま使う) with _fetching(store, name), account_registry.locked(store, name): seen = account_registry.removing(store, name) - oauth = seen or name in account_registry.names(store) + appeared = not seen and name in account_registry.names(store) + if appeared: + # 待つ間にほかの端末の追加(account add の --code)が同じ名前を登録した。その資格情報は消さず、何も変えずに止まる + detail = f"取得と登録の排他を待つ間に同じ名前の OAuth の登録ができた(何も外していない)。外すなら ndf relay account remove {name} をもう一度打つ" + return result.emit(result.stopped(TOOL, "store_in_use", detail, items=[{"name": name, "kind": "oauth"}])) + oauth = seen if not oauth: row = account_metered.remove(store, name) return result.emit(result.result(TOOL, result.OK, f"外した: {name}", items=[row])) diff --git a/plugins/ndf/tests/domains/relay/test_relay_cli.py b/plugins/ndf/tests/domains/relay/test_relay_cli.py index 040dcdeb9..f2b65c01d 100644 --- a/plugins/ndf/tests/domains/relay/test_relay_cli.py +++ b/plugins/ndf/tests/domains/relay/test_relay_cli.py @@ -727,6 +727,31 @@ def other_terminal_right_after(s, n): # 残りが無いと見た直後に、別 assert account_registry.names(store) == [] and not real_removing(store, "work1") +def test_i8_remove_keeps_a_registration_added_while_waiting_for_the_locks(tmp_path, monkeypatch, capsys, last_result): + from ndf.domains.relay import account_login, account_registry + + store = tmp_path / "store" + monkeypatch.setenv("NDF_ACCOUNTS_DIR", str(store)) + _relogin(monkeypatch, tmp_path) + monkeypatch.setattr(cli_account.shutil, "which", lambda _: None) + logouts = [] + monkeypatch.setattr(account_login, "logout", lambda *a: logouts.append(a) or True) + real_lock = accounts.fetch_lock + calls = [] + + def other_terminal_adds(s, n, wait): # 排他を待つ間に、別の端末の account add --code が同じ名前を登録した + if not calls: + calls.append(n) + assert cli.main(["account", "add", "work1", "--code", "c"]) == 0 + return real_lock(s, n, wait) + + monkeypatch.setattr(accounts, "fetch_lock", other_terminal_adds) + assert cli.main(["account", "remove", "work1"]) != 0 + got = last_result(capsys.readouterr().out) + assert got["status"] == "stopped" and got["cause"]["kind"] == "store_in_use" and "もう一度" in got["cause"]["detail"] + assert logouts == [] and account_registry.names(store) == ["work1"] + + def test_i16_add_and_remove_wait_for_the_fetch_lock_and_forget_inside_it(tmp_path, monkeypatch, capsys, last_result, hold_lock): from ndf.domains.relay import account_login, account_registry From c1ec9d6754f0d8a6ec1b4560e452a15deda80bfa Mon Sep 17 00:00:00 2001 From: "takemi.ohama" Date: Sun, 22 Nov 2026 00:57:24 +0000 Subject: [PATCH 08/11] =?UTF-8?q?Fix:=20=E3=82=B3=E3=83=A1=E3=83=B3?= =?UTF-8?q?=E3=83=88=E3=81=AE=E5=BB=83=E6=AD=A2=E3=81=97=E3=81=9F=E8=AA=9E?= =?UTF-8?q?=E3=80=8C=E5=8D=B0=E3=80=8D=E3=82=92=E8=A8=80=E3=81=84=E6=8F=9B?= =?UTF-8?q?=E3=81=88=E3=82=8B=EF=BC=88#3604=20#3864=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KYHP7z4QjHh8vkcxvdbGUP --- plugins/ndf/src/ndf/domains/relay/accounts.py | 2 +- plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/ndf/src/ndf/domains/relay/accounts.py b/plugins/ndf/src/ndf/domains/relay/accounts.py index 9976918fe..f3222db95 100644 --- a/plugins/ndf/src/ndf/domains/relay/accounts.py +++ b/plugins/ndf/src/ndf/domains/relay/accounts.py @@ -235,7 +235,7 @@ def usages( if store is None: seen[n] = _read_windows(cache) continue - fetched = _read_json(fetch_path(store, n, g)) # 1 度だけ読み、窓と上限の印を同じ内容から取る(間の置き換えを混ぜない) + fetched = _read_json(fetch_path(store, n, g)) # 1 度だけ読み、窓と上限の到達(spend_limit_reached)を同じ内容から取る(間の置き換えを混ぜない) seen[n] = _read_merged(cache, fetched) if fetched.get("spend_limit_reached") is True: spent.add(n) diff --git a/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py b/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py index 31a05a891..dce79b20a 100644 --- a/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py +++ b/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py @@ -105,7 +105,7 @@ def replaced_after_first_read(p): # 1 度読んだ直後に、書き手が記 return got monkeypatch.setattr(accounts, "_read_json", replaced_after_first_read) - # 同じ 1 回の内容から窓と上限の印を取る(差し替え前の 10% と差し替え後の上限なしを組み合わせない) + # 同じ 1 回の内容から窓と上限の到達を取る(差し替え前の 10% と差し替え後の上限なしを組み合わせない) assert accounts.usages(state, store, NOW, {"work2": "g2"}) == [Usage("work2", 10.0, True)] and len(reads) == 1 From 464cab324c4cfb7780251222c41af61eda95e6fa Mon Sep 17 00:00:00 2001 From: "takemi.ohama" Date: Sun, 22 Nov 2026 00:58:10 +0000 Subject: [PATCH 09/11] =?UTF-8?q?Fix:=20=E8=A8=80=E3=81=84=E6=8F=9B?= =?UTF-8?q?=E3=81=88=E3=81=A7=E9=95=B7=E3=81=8F=E3=81=AA=E3=81=A3=E3=81=9F?= =?UTF-8?q?=E8=A1=8C=E3=82=92=E6=95=B4=E5=BD=A2=E3=81=99=E3=82=8B=EF=BC=88?= =?UTF-8?q?#3604=20#3864=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KYHP7z4QjHh8vkcxvdbGUP --- plugins/ndf/src/ndf/domains/relay/accounts.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/ndf/src/ndf/domains/relay/accounts.py b/plugins/ndf/src/ndf/domains/relay/accounts.py index f3222db95..334f4f259 100644 --- a/plugins/ndf/src/ndf/domains/relay/accounts.py +++ b/plugins/ndf/src/ndf/domains/relay/accounts.py @@ -235,7 +235,9 @@ def usages( if store is None: seen[n] = _read_windows(cache) continue - fetched = _read_json(fetch_path(store, n, g)) # 1 度だけ読み、窓と上限の到達(spend_limit_reached)を同じ内容から取る(間の置き換えを混ぜない) + fetched = _read_json( + fetch_path(store, n, g) + ) # 1 度だけ読み、窓と上限の到達(spend_limit_reached)を同じ内容から取る(間の置き換えを混ぜない) seen[n] = _read_merged(cache, fetched) if fetched.get("spend_limit_reached") is True: spent.add(n) From d4aaadaab00cc2fd917568282086ba3979e8483f Mon Sep 17 00:00:00 2001 From: "takemi.ohama" Date: Sun, 22 Nov 2026 01:01:46 +0000 Subject: [PATCH 10/11] =?UTF-8?q?Fix:=20=E8=A8=88=E6=B8=AC=E5=80=A4?= =?UTF-8?q?=E3=81=AE=E7=BD=AE=E3=81=8D=E5=A0=B4=E3=81=AE=E7=A2=BA=E3=81=8B?= =?UTF-8?q?=E3=82=81=E3=81=AB=E3=80=81=E8=BF=BD=E8=B7=A1=E3=81=95=E3=82=8C?= =?UTF-8?q?=E3=81=AA=E3=81=84=E8=A8=AD=E5=AE=9A=E3=81=AE=E5=81=B4=EF=BC=88?= =?UTF-8?q?=E7=99=BB=E9=8C=B2=E7=B0=BF=E3=81=AE=E5=8F=96=E5=BE=97=E3=81=AE?= =?UTF-8?q?=E8=A8=98=E9=8C=B2=EF=BC=89=E3=82=92=E8=B6=B3=E3=81=99=EF=BC=88?= =?UTF-8?q?#3604=20#3864=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 詳細設計は relay-usage-fetch を root: config の measured と定める。テストの意図(計測値を追跡されるファイルへ書かない)は保つ。 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KYHP7z4QjHh8vkcxvdbGUP --- plugins/ndf/tests/domains/test_ndf_settings.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/ndf/tests/domains/test_ndf_settings.py b/plugins/ndf/tests/domains/test_ndf_settings.py index 35784e14f..f1010e868 100644 --- a/plugins/ndf/tests/domains/test_ndf_settings.py +++ b/plugins/ndf/tests/domains/test_ndf_settings.py @@ -17,8 +17,10 @@ def test_untracked_locations_hold_only_untracked_files(): def test_measured_values_are_never_written_to_a_tracked_file(): + # 計測値は状態の保存先か、追跡されない設定の側(アカウントの登録簿の取得の記録。#3864 の決定 4)にだけ置く for r in facts.rows(FILES, kind="measured"): - assert r["tracked"] is False and facts.value("locations", r["location"], "root") == "state", r["id"] + loc = facts.row("locations", r["location"]) + assert r["tracked"] is False and loc["root"] in {"state", "config"} and loc["tracked"] is False, r["id"] def test_test_command_is_declared_in_one_file(): From e2df7265e6fb0b350891a7608835b397bd82352c Mon Sep 17 00:00:00 2001 From: "takemi.ohama" Date: Sun, 22 Nov 2026 01:10:48 +0000 Subject: [PATCH 11/11] =?UTF-8?q?Fix:=20=E5=8F=96=E5=BE=97=E3=81=AE?= =?UTF-8?q?=E8=A8=98=E9=8C=B2=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=81=AE?= =?UTF-8?q?=E6=9C=9F=E5=BE=85=E5=80=A4=E3=82=92=E7=AA=93=E3=81=94=E3=81=A8?= =?UTF-8?q?=E3=81=AE=E5=80=A4=E3=82=92=E6=8C=81=E3=81=A4=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=8E=87=E3=81=AE=E5=BD=A2=E3=81=AB=E3=81=99=E3=82=8B=EF=BC=88?= =?UTF-8?q?#3604=20#3864=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit U6 が account_decide.Usage に 5 時間・7 日の使用率と 7 日のリセットの時刻を足したため、 取得の記録を読む usages の 5 件のテストの期待値を新しい形で書き直す。確かめる意図は変えない。 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KYHP7z4QjHh8vkcxvdbGUP --- .../domains/relay/test_relay_usage_fetch.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py b/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py index dce79b20a..cfa8e1d59 100644 --- a/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py +++ b/plugins/ndf/tests/domains/relay/test_relay_usage_fetch.py @@ -40,7 +40,7 @@ def test_decision4_fetch_record_lives_in_the_store_and_reads_from_any_state_dir( assert stat.S_IMODE(path.parent.stat().st_mode) == 0o700 # 状態の保存先には何も書かず、保存先の違う 2 つのラッパーが同じ記録を読む(モデル別の週の窓 95 を使用率に読む) for state in (state_a, state_b): - assert accounts.usages(state, store, NOW, {"work2": "g2"}) == [Usage("work2", 95.0, False)] + assert accounts.usages(state, store, NOW, {"work2": "g2"}) == [Usage("work2", 95.0, 40.0, 95.0, WEEK, False)] assert not accounts.usage_dir(state).exists() # 旧い世代の取得の記録は読まない assert accounts.usages(state_a, store, NOW, {"work2": "g3"}) == [] @@ -50,14 +50,14 @@ def test_decision4_usages_merges_the_status_line_and_the_fetch_record_per_window store, state = tmp_path / "store", tmp_path / "state" accounts.write_usage(accounts.usage_path(state, "work1", "g1"), {"five_hour": (50.0, LATER)}, NOW) accounts.write_fetch(accounts.fetch_path(store, "work1", "g1"), _got(five_hour=(30.0, LATER), seven_day=(60.0, WEEK)), NOW) - assert accounts.usages(state, store, NOW, {"work1": "g1"}) == [Usage("work1", 60.0, False)] + assert accounts.usages(state, store, NOW, {"work1": "g1"}) == [Usage("work1", 60.0, 50.0, 60.0, WEEK, False)] # 取得の記録の窓が新しい resets_at を持てば、使用率が小さくてもそれを採る accounts.write_fetch(accounts.fetch_path(store, "work1", "g1"), _got(five_hour=(5.0, LATER + 18000)), NOW) windows = accounts.read_windows(accounts.usage_path(state, "work1", "g1"), accounts.fetch_path(store, "work1", "g1")) assert windows["five_hour"] == (5.0, LATER + 18000) # 片方が読めなければもう片方だけで読む accounts.fetch_path(store, "work1", "g1").write_text("{", encoding="utf-8") - assert accounts.usages(state, store, NOW, {"work1": "g1"}) == [Usage("work1", 50.0, False)] + assert accounts.usages(state, store, NOW, {"work1": "g1"}) == [Usage("work1", 50.0, 50.0, None, None, False)] def test_decision4_write_fetch_keeps_model_windows_apart_and_missing_windows(tmp_path): @@ -69,7 +69,7 @@ def test_decision4_write_fetch_keeps_model_windows_apart_and_missing_windows(tmp windows = _fetch_record(path)["windows"] assert windows["seven_day_scoped"] == {"a": {"percent": 95.0, "resets_at": friday}, "b": {"percent": 100.0, "resets_at": thursday}} assert windows["five_hour"] == {"percent": 10.0, "resets_at": LATER} - assert accounts.usages(tmp_path / "state", tmp_path, NOW, {"work1": "g1"}) == [Usage("work1", 100.0, True)] + assert accounts.usages(tmp_path / "state", tmp_path, NOW, {"work1": "g1"}) == [Usage("work1", 100.0, 10.0, 100.0, None, True)] # 崩れたモデル別の窓(None)は前の値を残し、古い入力・失効した入力で下げない accounts.write_fetch(path, _got(five_hour=(3.0, LATER - 60), scoped=None), NOW) accounts.write_fetch(path, _got(five_hour=(1.0, NOW - 1)), NOW) @@ -81,11 +81,11 @@ def test_decision4_spend_limit_holds_until_an_explicit_false(tmp_path): path = accounts.fetch_path(store, "work2", "g2") # 最初の応答が窓を持たず spend_limit_reached が真なら、窓もキャッシュも無くても上限の行で返る accounts.write_fetch(path, _got(spend_limit_reached=True), NOW) - assert accounts.usages(state, store, NOW, {"work2": "g2"}) == [Usage("work2", None, True)] # type: ignore[arg-type] + assert accounts.usages(state, store, NOW, {"work2": "g2"}) == [Usage("work2", None, None, None, None, True)] accounts.write_fetch(path, _got(five_hour=(10.0, LATER)), NOW) # 応答に無い(None)なら真が残る - assert accounts.usages(state, store, NOW, {"work2": "g2"}) == [Usage("work2", 10.0, True)] + assert accounts.usages(state, store, NOW, {"work2": "g2"}) == [Usage("work2", 10.0, 10.0, None, None, True)] accounts.write_fetch(path, _got(five_hour=(10.0, LATER), spend_limit_reached=False), NOW) - assert accounts.usages(state, store, NOW, {"work2": "g2"}) == [Usage("work2", 10.0, False)] + assert accounts.usages(state, store, NOW, {"work2": "g2"}) == [Usage("work2", 10.0, 10.0, None, None, False)] def test_decision4_usages_reads_the_fetch_record_once_per_account(tmp_path, monkeypatch): @@ -106,7 +106,7 @@ def replaced_after_first_read(p): # 1 度読んだ直後に、書き手が記 monkeypatch.setattr(accounts, "_read_json", replaced_after_first_read) # 同じ 1 回の内容から窓と上限の到達を取る(差し替え前の 10% と差し替え後の上限なしを組み合わせない) - assert accounts.usages(state, store, NOW, {"work2": "g2"}) == [Usage("work2", 10.0, True)] and len(reads) == 1 + assert accounts.usages(state, store, NOW, {"work2": "g2"}) == [Usage("work2", 10.0, 10.0, None, None, True)] and len(reads) == 1 def test_decision4_write_fetch_keeps_the_attempt_time_and_clears_the_error(tmp_path):