sub-pool-client 0.6.7__tar.gz → 0.6.9__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.
Files changed (21) hide show
  1. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/PKG-INFO +2 -2
  2. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/pyproject.toml +1 -1
  3. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/sub_pool_client/_cli_core.py +11 -13
  4. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/sub_pool_client/_shared.py +30 -0
  5. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/sub_pool_client/_version.py +2 -2
  6. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/sub_pool_client/client.py +5 -4
  7. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/sub_pool_client/codex.py +6 -3
  8. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/.github/workflows/publish.yml +0 -0
  9. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/.gitignore +0 -0
  10. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/README.md +0 -0
  11. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/examples/codex_hello.py +0 -0
  12. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/examples/hello.py +0 -0
  13. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/meta/README.md +0 -0
  14. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/meta/pyproject.toml +0 -0
  15. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/sub_pool_client/__init__.py +0 -0
  16. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/sub_pool_client/_cli_provider.py +0 -0
  17. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/sub_pool_client/_health_watcher.py +0 -0
  18. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/sub_pool_client/_swap.py +0 -0
  19. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/sub_pool_client/cli_claude.py +0 -0
  20. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/sub_pool_client/cli_codex.py +0 -0
  21. {sub_pool_client-0.6.7 → sub_pool_client-0.6.9}/sub_pool_client/errors.py +0 -0
@@ -1,11 +1,11 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sub-pool-client
3
- Version: 0.6.7
3
+ Version: 0.6.9
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
7
7
  Requires-Python: >=3.12
8
- Requires-Dist: claude-agent-sdk>=0.1.63
8
+ Requires-Dist: claude-agent-sdk>=0.2.115
9
9
  Requires-Dist: click>=8.1
10
10
  Requires-Dist: httpx>=0.27
11
11
  Description-Content-Type: text/markdown
@@ -5,7 +5,7 @@ description = "Python client + `sp-claude` / `sp-codex` CLIs for a sub-pool cred
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.12"
7
7
  dependencies = [
8
- "claude-agent-sdk>=0.1.63",
8
+ "claude-agent-sdk>=0.2.115",
9
9
  "httpx>=0.27",
10
10
  "click>=8.1",
11
11
  ]
@@ -26,6 +26,7 @@ import httpx
26
26
 
27
27
  from sub_pool_client._cli_provider import CliProvider
28
28
  from sub_pool_client._health_watcher import HealthWatcher
29
+ from sub_pool_client._shared import next_refresh_sleep
29
30
  from sub_pool_client._swap import NoSwapAvailable, swap_credentials
30
31
 
31
32
 
@@ -559,19 +560,16 @@ async def _token_refresh_loop(
559
560
  wake = asyncio.Event() # never set → wait_for always times out
560
561
  while True:
561
562
  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)
563
+ # Shared with both SDK poll loops (see _shared.next_refresh_sleep) so the
564
+ # three schedulers can't drift again. A refresh that FAILS leaves exp
565
+ # near-now, so the min_sleep_s floor makes us retry every min_sleep_s
566
+ # until it lands — or the token dies, at which point a genuinely-dead
567
+ # refresh_token has already flipped the account INVALID and the health
568
+ # watcher swaps us off it.
569
+ sleep_s = next_refresh_sleep(
570
+ exp, time.time(),
571
+ slack_s=slack_s, min_sleep_s=min_sleep_s, fallback_s=fallback_s,
572
+ )
575
573
  try:
576
574
  await asyncio.wait_for(wake.wait(), timeout=sleep_s)
577
575
  except asyncio.TimeoutError:
@@ -41,6 +41,36 @@ DEFAULT_ROOT = Path(
41
41
  )
42
42
 
43
43
 
44
+ # ---- token-refresh scheduling -------------------------------------------
45
+ # Shared by the sp-claude/sp-codex CLI refresh loop (_cli_core) AND both SDK
46
+ # poll loops (PooledClient / PooledCodexClient). Kept in ONE place because the
47
+ # three drifted apart twice: the CLI once regressed to a fixed cadence that
48
+ # outlived short-lived tokens (→ mid-session 401), and the exp<=0 fallback below
49
+ # once existed only in the CLI (→ codex api-key SDK leases polling every 60s).
50
+ POLL_SLACK_S = 600.0 # refresh this long BEFORE the token actually expires
51
+ POLL_MIN_SLEEP_S = 60.0 # floor so tiny-TTL tests / churn don't bash the pool
52
+ POLL_FALLBACK_S = 50 * 60.0 # no usable expiry (token_expires_at<=0, e.g. a codex
53
+ # api-key lease / an exp-less JWT) → fixed cadence
54
+
55
+
56
+ def next_refresh_sleep(
57
+ expires_at: float, now: float, *,
58
+ slack_s: float = POLL_SLACK_S,
59
+ min_sleep_s: float = POLL_MIN_SLEEP_S,
60
+ fallback_s: float = POLL_FALLBACK_S,
61
+ ) -> float:
62
+ """Seconds to sleep before the next pool token refresh.
63
+
64
+ Anchored to the token's ACTUAL expiry: wake `slack_s` before it, floored at
65
+ `min_sleep_s`. When `expires_at` carries no usable value (<=0), fall back to
66
+ a fixed cadence instead of anchoring to a long-past deadline — that would
67
+ refresh every `min_sleep_s` forever (a needless churn storm that hit codex
68
+ api-key leases, whose token has no expiry)."""
69
+ if expires_at <= 0:
70
+ return fallback_s
71
+ return max(min_sleep_s, expires_at - now - slack_s)
72
+
73
+
44
74
  def dir_key(
45
75
  *,
46
76
  pool_url: str,
@@ -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.7'
22
- __version_tuple__ = version_tuple = (0, 6, 7)
21
+ __version__ = version = '0.6.9'
22
+ __version_tuple__ = version_tuple = (0, 6, 9)
23
23
 
24
24
  __commit_id__ = commit_id = None
@@ -55,6 +55,7 @@ from sub_pool_client._shared import (
55
55
  cleanup_dir,
56
56
  dir_key,
57
57
  locked_meta,
58
+ next_refresh_sleep,
58
59
  shared_dir,
59
60
  write_credentials,
60
61
  )
@@ -329,10 +330,10 @@ class PooledClient(ClaudeSDKClient):
329
330
  try:
330
331
  while True:
331
332
  self._poll_wake.clear()
332
- remaining = self._token_expires_at - time.time()
333
- # Wake up 10 min before expiry, minimum 60s sleep so we
334
- # don't bash the pool during tiny-TTL tests.
335
- sleep_s = max(60.0, remaining - 600.0)
333
+ # Wake ~10 min before expiry (min 60s), or a fixed cadence when
334
+ # the lease has no usable expiry — shared with the CLI + codex
335
+ # so the three schedulers can't drift (see next_refresh_sleep).
336
+ sleep_s = next_refresh_sleep(self._token_expires_at, time.time())
336
337
  # Interruptible sleep: a health swap sets _poll_wake so we
337
338
  # recompute the schedule against the NEW (possibly much shorter)
338
339
  # token expiry instead of oversleeping on the old account's.
@@ -32,6 +32,7 @@ from sub_pool_client._shared import (
32
32
  cleanup_dir,
33
33
  dir_key,
34
34
  locked_meta,
35
+ next_refresh_sleep,
35
36
  shared_dir,
36
37
  write_codex_auth,
37
38
  )
@@ -338,9 +339,11 @@ class PooledCodexClient:
338
339
  try:
339
340
  while True:
340
341
  self._poll_wake.clear()
341
- remaining = self._token_expires_at - time.time()
342
- # Wake up 10 min before expiry, minimum 60s sleep.
343
- sleep_s = max(60.0, remaining - 600.0)
342
+ # Wake ~10 min before expiry (min 60s), or a fixed cadence when
343
+ # the lease has no usable expiry (codex api-key tokens have none)
344
+ # — shared with the CLI + claude SDK so the three schedulers
345
+ # can't drift (see next_refresh_sleep).
346
+ sleep_s = next_refresh_sleep(self._token_expires_at, time.time())
344
347
  # Interruptible: a health swap sets _poll_wake so we reschedule
345
348
  # against the NEW account's token expiry instead of oversleeping
346
349
  # on the old one (Codex tokens can be days out).