sub-pool-client 0.6.6__tar.gz → 0.6.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/PKG-INFO +1 -1
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/sub_pool_client/_cli_core.py +66 -5
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/sub_pool_client/_health_watcher.py +33 -7
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/sub_pool_client/_version.py +2 -2
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/sub_pool_client/client.py +17 -9
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/sub_pool_client/codex.py +14 -6
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/.github/workflows/publish.yml +0 -0
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/.gitignore +0 -0
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/README.md +0 -0
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/examples/codex_hello.py +0 -0
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/examples/hello.py +0 -0
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/meta/README.md +0 -0
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/meta/pyproject.toml +0 -0
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/pyproject.toml +0 -0
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/sub_pool_client/__init__.py +0 -0
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/sub_pool_client/_cli_provider.py +0 -0
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/sub_pool_client/_shared.py +0 -0
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/sub_pool_client/_swap.py +0 -0
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/sub_pool_client/cli_claude.py +0 -0
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/sub_pool_client/cli_codex.py +0 -0
- {sub_pool_client-0.6.6 → sub_pool_client-0.6.7}/sub_pool_client/errors.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sub-pool-client
|
|
3
|
-
Version: 0.6.
|
|
3
|
+
Version: 0.6.7
|
|
4
4
|
Summary: Python client + `sp-claude` / `sp-codex` CLIs for a sub-pool credential broker.
|
|
5
5
|
Project-URL: Homepage, https://github.com/subs-pool/sub-pool-client
|
|
6
6
|
Project-URL: Repository, https://github.com/subs-pool/sub-pool-client
|
|
@@ -16,6 +16,7 @@ import shutil
|
|
|
16
16
|
import signal
|
|
17
17
|
import sys
|
|
18
18
|
import tempfile
|
|
19
|
+
import time
|
|
19
20
|
from contextlib import suppress
|
|
20
21
|
from pathlib import Path
|
|
21
22
|
from typing import Any
|
|
@@ -428,6 +429,11 @@ async def run_session(
|
|
|
428
429
|
|
|
429
430
|
# Background tasks: token refresh + health-driven account swap.
|
|
430
431
|
current = {"lease": lease}
|
|
432
|
+
# Set by a successful swap so the expiry-anchored refresh loop
|
|
433
|
+
# re-anchors to the NEW lease's token instead of sleeping out a
|
|
434
|
+
# now-stale deadline — a swapped-in token may have far less life
|
|
435
|
+
# left than the one it replaced.
|
|
436
|
+
refresh_wake = asyncio.Event()
|
|
431
437
|
|
|
432
438
|
async def on_swap_needed(health: dict) -> None:
|
|
433
439
|
# Read the from-account BEFORE swap_credentials runs —
|
|
@@ -439,6 +445,10 @@ async def run_session(
|
|
|
439
445
|
pool, current, session_dir, provider,
|
|
440
446
|
account=chosen_account,
|
|
441
447
|
)
|
|
448
|
+
# swap_credentials already advanced current["lease"]
|
|
449
|
+
# (incl. token_expires_at) in its sync block; wake the
|
|
450
|
+
# refresh loop so it re-anchors to the new expiry.
|
|
451
|
+
refresh_wake.set()
|
|
442
452
|
if verbose:
|
|
443
453
|
log.info(
|
|
444
454
|
"swapped %s → %s (reason: %s)",
|
|
@@ -450,7 +460,10 @@ async def run_session(
|
|
|
450
460
|
log.info("swap deferred: %s", e)
|
|
451
461
|
|
|
452
462
|
refresher = asyncio.create_task(
|
|
453
|
-
_token_refresh_loop(
|
|
463
|
+
_token_refresh_loop(
|
|
464
|
+
pool, current, session_dir, provider, verbose,
|
|
465
|
+
wake=refresh_wake,
|
|
466
|
+
),
|
|
454
467
|
name=f"sp-{provider.name}-refresh",
|
|
455
468
|
)
|
|
456
469
|
watcher = asyncio.create_task(
|
|
@@ -520,15 +533,63 @@ async def _refresh_once(
|
|
|
520
533
|
async def _token_refresh_loop(
|
|
521
534
|
pool: PoolHTTP, current: dict, session_dir: Path,
|
|
522
535
|
provider: CliProvider, verbose: bool,
|
|
523
|
-
*,
|
|
536
|
+
*, wake: "asyncio.Event | None" = None,
|
|
537
|
+
slack_s: float = 600.0, min_sleep_s: float = 60.0,
|
|
538
|
+
fallback_s: float = 50 * 60,
|
|
524
539
|
) -> None:
|
|
525
|
-
"""
|
|
526
|
-
|
|
540
|
+
"""Rotate access_token via the pool BEFORE it expires.
|
|
541
|
+
|
|
542
|
+
Anthropic access tokens live only ~1h and the leased bundle is
|
|
543
|
+
sanitized (refreshToken=""), so the spawned CLI can't self-refresh —
|
|
544
|
+
the pool is the sole refresher, and the running `claude` re-reads the
|
|
545
|
+
rewritten .credentials.json across requests (see _swap.py).
|
|
546
|
+
|
|
547
|
+
The catch that broke this: a leased token is NOT freshly minted. The
|
|
548
|
+
pool only re-mints at seal time when under `slack_s` of life remains,
|
|
549
|
+
so a lease can hand out a token with as little as ~10min left. The old
|
|
550
|
+
fixed 50-min cadence therefore fired long AFTER such a token had
|
|
551
|
+
already died mid-session → an unrecoverable 401 (the exact failure
|
|
552
|
+
users hit). So anchor every sleep to the token's ACTUAL expiry
|
|
553
|
+
(mirrors PooledClient._poll_loop), and let a swap wake us to re-anchor
|
|
554
|
+
against the replacement token — which may itself be short-lived.
|
|
555
|
+
|
|
556
|
+
See `_refresh_once` for the concurrent-swap race-safety guard.
|
|
557
|
+
"""
|
|
558
|
+
if wake is None:
|
|
559
|
+
wake = asyncio.Event() # never set → wait_for always times out
|
|
527
560
|
while True:
|
|
561
|
+
exp = float(current["lease"].get("token_expires_at") or 0.0)
|
|
562
|
+
if exp <= 0:
|
|
563
|
+
# No usable expiry (e.g. a codex api-key lease / an exp-less JWT
|
|
564
|
+
# → the pool reports token_expires_at=0). We can't anchor, so
|
|
565
|
+
# fall back to the old fixed cadence instead of hammering /token
|
|
566
|
+
# every min_sleep_s (which anchoring-to-a-past-deadline would do).
|
|
567
|
+
sleep_s = fallback_s
|
|
568
|
+
else:
|
|
569
|
+
# A refresh that FAILS leaves exp near-now, so max()-clamping to
|
|
570
|
+
# min_sleep_s makes us retry every min_sleep_s until it lands —
|
|
571
|
+
# or the token dies, at which point a genuinely-dead refresh_token
|
|
572
|
+
# has already flipped the account INVALID and the health watcher
|
|
573
|
+
# swaps us off it.
|
|
574
|
+
sleep_s = max(min_sleep_s, exp - time.time() - slack_s)
|
|
528
575
|
try:
|
|
529
|
-
await asyncio.
|
|
576
|
+
await asyncio.wait_for(wake.wait(), timeout=sleep_s)
|
|
577
|
+
except asyncio.TimeoutError:
|
|
578
|
+
# A swap can land in the same tick the deadline fires (set the
|
|
579
|
+
# wake just after wait_for times out); honor it so we re-anchor
|
|
580
|
+
# to the swapped-in token rather than refreshing a lease the swap
|
|
581
|
+
# already wrote fresh credentials for.
|
|
582
|
+
if wake.is_set():
|
|
583
|
+
wake.clear()
|
|
584
|
+
continue
|
|
530
585
|
except asyncio.CancelledError:
|
|
531
586
|
return
|
|
587
|
+
else:
|
|
588
|
+
# Woken by a swap: current["lease"] already points at the new
|
|
589
|
+
# token (written by swap_credentials); re-anchor without
|
|
590
|
+
# refreshing, since the swap installed fresh credentials.
|
|
591
|
+
wake.clear()
|
|
592
|
+
continue
|
|
532
593
|
await _refresh_once(pool, current, session_dir, provider, verbose)
|
|
533
594
|
|
|
534
595
|
|
|
@@ -45,13 +45,39 @@ class HealthWatcher:
|
|
|
45
45
|
try:
|
|
46
46
|
data = await self._fetch()
|
|
47
47
|
except httpx.HTTPStatusError as e:
|
|
48
|
-
if e.response.status_code
|
|
49
|
-
#
|
|
50
|
-
#
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
#
|
|
54
|
-
|
|
48
|
+
if e.response.status_code in (404, 410):
|
|
49
|
+
# Our CURRENT lease died server-side while the session is
|
|
50
|
+
# still live: reaped after a health-poll gap (e.g. the host
|
|
51
|
+
# slept past LEASE_TTL_S), externally released, or the
|
|
52
|
+
# account deleted. Giving up here is what left a long
|
|
53
|
+
# session 401ing forever with no recovery. Instead recover
|
|
54
|
+
# through the same swap handler — it re-leases a fresh
|
|
55
|
+
# account (releasing the dead one is best-effort) — then
|
|
56
|
+
# keep watching the new lease. `lease_gone` tells the
|
|
57
|
+
# handler this is a re-lease, so a same-account replacement
|
|
58
|
+
# (new lease_id) is still adopted rather than discarded as
|
|
59
|
+
# "nothing to swap to".
|
|
60
|
+
#
|
|
61
|
+
# Operator note: because of this, a bare DELETE of the lease
|
|
62
|
+
# no longer evicts a live client — it re-leases within one
|
|
63
|
+
# tick (possibly onto the same account). To take an account
|
|
64
|
+
# out from under a running session, DISABLE the account
|
|
65
|
+
# (re-lease then 503s and the client quietly waits it out) or
|
|
66
|
+
# revoke its api-key.
|
|
67
|
+
try:
|
|
68
|
+
await self._on_unhealthy({
|
|
69
|
+
"healthy": False,
|
|
70
|
+
"state": "gone",
|
|
71
|
+
"lease_gone": True,
|
|
72
|
+
"reason": f"lease gone ({e.response.status_code}); "
|
|
73
|
+
f"re-leasing",
|
|
74
|
+
})
|
|
75
|
+
except asyncio.CancelledError:
|
|
76
|
+
return
|
|
77
|
+
except Exception as ex: # noqa: BLE001
|
|
78
|
+
log.warning("re-lease after %s raised: %s",
|
|
79
|
+
e.response.status_code, ex)
|
|
80
|
+
continue
|
|
55
81
|
# Other 4xx/5xx — log and keep going.
|
|
56
82
|
log.debug("health probe http error: %s", e)
|
|
57
83
|
continue
|
|
@@ -18,7 +18,7 @@ version_tuple: tuple[int | str, ...]
|
|
|
18
18
|
commit_id: str | None
|
|
19
19
|
__commit_id__: str | None
|
|
20
20
|
|
|
21
|
-
__version__ = version = '0.6.
|
|
22
|
-
__version_tuple__ = version_tuple = (0, 6,
|
|
21
|
+
__version__ = version = '0.6.7'
|
|
22
|
+
__version_tuple__ = version_tuple = (0, 6, 7)
|
|
23
23
|
|
|
24
24
|
__commit_id__ = commit_id = None
|
|
@@ -438,15 +438,18 @@ class PooledClient(ClaudeSDKClient):
|
|
|
438
438
|
f"{self._pool_url}/credentials/lease/{lease_id}/health",
|
|
439
439
|
headers={"authorization": f"Bearer {self._api_key}"},
|
|
440
440
|
)
|
|
441
|
-
# 404 (unknown lease) / 410 (lease ended) → HealthWatcher
|
|
442
|
-
# non-2xx are logged + retried
|
|
441
|
+
# 404 (unknown lease) / 410 (lease ended) → HealthWatcher recovers by
|
|
442
|
+
# re-leasing (lease_gone). Other non-2xx are logged + retried.
|
|
443
443
|
r.raise_for_status()
|
|
444
444
|
return r.json()
|
|
445
445
|
|
|
446
446
|
async def _on_unhealthy(self, health: dict) -> None:
|
|
447
|
-
await self._swap_account(
|
|
447
|
+
await self._swap_account(
|
|
448
|
+
reason=str(health.get("reason") or "unhealthy"),
|
|
449
|
+
lease_gone=bool(health.get("lease_gone")),
|
|
450
|
+
)
|
|
448
451
|
|
|
449
|
-
async def _swap_account(self, *, reason: str) -> None:
|
|
452
|
+
async def _swap_account(self, *, reason: str, lease_gone: bool = False) -> None:
|
|
450
453
|
"""Lease a replacement account and rewrite the shared credential in
|
|
451
454
|
place so the running CLI picks it up on its next request. Only the poll
|
|
452
455
|
leader runs this. Best-effort: if the pool has no other healthy account
|
|
@@ -479,16 +482,21 @@ class PooledClient(ClaudeSDKClient):
|
|
|
479
482
|
return
|
|
480
483
|
# Same-account guard keys on the immutable server account_id, NOT the
|
|
481
484
|
# name: a delete+recreate of the same name is a DIFFERENT account and
|
|
482
|
-
# must be allowed to swap in.
|
|
483
|
-
#
|
|
485
|
+
# must be allowed to swap in. For a cooled/unhealthy swap, the same
|
|
486
|
+
# account means "nothing to swap to" — keep the current, still-usable
|
|
487
|
+
# lease. But when the current lease is GONE (lease_gone, 410/404), the
|
|
488
|
+
# same account on a NEW lease_id IS the recovery: keeping the dead lease
|
|
489
|
+
# would strand the session 401ing. So only a byte-identical lease_id is
|
|
490
|
+
# truly nothing gained once the lease is gone.
|
|
491
|
+
same_lease = new_lease_id == old_lease_id
|
|
484
492
|
same_account = (
|
|
485
|
-
|
|
493
|
+
same_lease
|
|
486
494
|
or (new_account_id is not None and old_account_id is not None
|
|
487
495
|
and new_account_id == old_account_id)
|
|
488
496
|
)
|
|
489
|
-
if same_account:
|
|
497
|
+
if same_lease or (same_account and not lease_gone):
|
|
490
498
|
await self._release_swap_dup(new_lease_id, old_lease_id)
|
|
491
|
-
log.info("health swap:
|
|
499
|
+
log.info("health swap: nothing to swap to (still on %s)",
|
|
492
500
|
old_account)
|
|
493
501
|
return
|
|
494
502
|
|
|
@@ -439,14 +439,18 @@ class PooledCodexClient:
|
|
|
439
439
|
f"{self._pool_url}/credentials/lease/{lease_id}/health",
|
|
440
440
|
headers={"authorization": f"Bearer {self._api_key}"},
|
|
441
441
|
)
|
|
442
|
-
# 404 (unknown lease) / 410 (lease ended) → HealthWatcher
|
|
442
|
+
# 404 (unknown lease) / 410 (lease ended) → HealthWatcher recovers by
|
|
443
|
+
# re-leasing (lease_gone). Other non-2xx are logged + retried.
|
|
443
444
|
r.raise_for_status()
|
|
444
445
|
return r.json()
|
|
445
446
|
|
|
446
447
|
async def _on_unhealthy(self, health: dict) -> None:
|
|
447
|
-
await self._swap_account(
|
|
448
|
+
await self._swap_account(
|
|
449
|
+
reason=str(health.get("reason") or "unhealthy"),
|
|
450
|
+
lease_gone=bool(health.get("lease_gone")),
|
|
451
|
+
)
|
|
448
452
|
|
|
449
|
-
async def _swap_account(self, *, reason: str) -> None:
|
|
453
|
+
async def _swap_account(self, *, reason: str, lease_gone: bool = False) -> None:
|
|
450
454
|
"""Lease a replacement Codex account and rewrite the shared auth.json
|
|
451
455
|
in place so the running CLI picks it up on its next call. Only the poll
|
|
452
456
|
leader runs this. Best-effort: no other healthy account (503) → keep the
|
|
@@ -477,13 +481,17 @@ class PooledCodexClient:
|
|
|
477
481
|
return
|
|
478
482
|
# Same-account guard keys on the immutable server account_id, NOT the
|
|
479
483
|
# name (a delete+recreate of the same name is a DIFFERENT account and
|
|
480
|
-
# must swap in).
|
|
484
|
+
# must swap in). For a cooled swap the same account means "nothing to
|
|
485
|
+
# swap to" — keep the current lease. But when the lease is GONE
|
|
486
|
+
# (lease_gone, 410/404), the same account on a NEW lease_id IS the
|
|
487
|
+
# recovery; only a byte-identical lease_id is truly nothing gained.
|
|
488
|
+
same_lease = new_lease_id == old_lease_id
|
|
481
489
|
same_account = (
|
|
482
|
-
|
|
490
|
+
same_lease
|
|
483
491
|
or (new_account_id is not None and old_account_id is not None
|
|
484
492
|
and new_account_id == old_account_id)
|
|
485
493
|
)
|
|
486
|
-
if same_account:
|
|
494
|
+
if same_lease or (same_account and not lease_gone):
|
|
487
495
|
await self._release_swap_dup(new_lease_id, old_lease_id)
|
|
488
496
|
log.info("codex health swap: no other account available (still on %s)",
|
|
489
497
|
old_account)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|