sub-pool-client 0.7.0__tar.gz → 0.8.0__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 (22) hide show
  1. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/PKG-INFO +2 -2
  2. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/sub_pool_client/_cli_core.py +3 -2
  3. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/sub_pool_client/_cli_provider.py +55 -6
  4. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/sub_pool_client/_version.py +2 -2
  5. sub_pool_client-0.8.0/sub_pool_client/cli_grok.py +61 -0
  6. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/.github/workflows/publish.yml +0 -0
  7. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/.gitignore +0 -0
  8. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/README.md +0 -0
  9. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/examples/codex_hello.py +0 -0
  10. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/examples/hello.py +0 -0
  11. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/meta/README.md +0 -0
  12. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/meta/pyproject.toml +0 -0
  13. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/pyproject.toml +0 -0
  14. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/sub_pool_client/__init__.py +0 -0
  15. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/sub_pool_client/_health_watcher.py +0 -0
  16. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/sub_pool_client/_shared.py +0 -0
  17. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/sub_pool_client/_swap.py +0 -0
  18. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/sub_pool_client/cli_claude.py +0 -0
  19. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/sub_pool_client/cli_codex.py +0 -0
  20. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/sub_pool_client/client.py +0 -0
  21. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/sub_pool_client/codex.py +0 -0
  22. {sub_pool_client-0.7.0 → sub_pool_client-0.8.0}/sub_pool_client/errors.py +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: sub-pool-client
3
- Version: 0.7.0
3
+ Version: 0.8.0
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
@@ -662,7 +662,7 @@ async def _token_refresh_loop(
662
662
  pool: PoolHTTP, current: dict, session_dir: Path,
663
663
  provider: CliProvider, verbose: bool,
664
664
  *, wake: "asyncio.Event | None" = None,
665
- slack_s: float = 600.0, min_sleep_s: float = 60.0,
665
+ slack_s: float | None = None, min_sleep_s: float = 60.0,
666
666
  fallback_s: float = 50 * 60,
667
667
  ) -> None:
668
668
  """Rotate access_token via the pool BEFORE it expires.
@@ -695,7 +695,8 @@ async def _token_refresh_loop(
695
695
  # watcher swaps us off it.
696
696
  sleep_s = next_refresh_sleep(
697
697
  exp, time.time(),
698
- slack_s=slack_s, min_sleep_s=min_sleep_s, fallback_s=fallback_s,
698
+ slack_s=(provider.refresh_slack_s if slack_s is None else slack_s),
699
+ min_sleep_s=min_sleep_s, fallback_s=fallback_s,
699
700
  )
700
701
  try:
701
702
  await asyncio.wait_for(wake.wait(), timeout=sleep_s)
@@ -1,4 +1,4 @@
1
- """Per-provider knobs for the sp-claude / sp-codex CLIs.
1
+ """Per-provider knobs for the sp-claude / sp-codex / sp-grok CLIs.
2
2
 
3
3
  The lease loop in `_cli_core` is provider-agnostic — same lease /
4
4
  refresh / release flow regardless of which upstream CLI gets spawned.
@@ -41,6 +41,24 @@ def _write_claude_credentials(path: Path, lease: dict) -> None:
41
41
  _atomic_write_json(path, payload)
42
42
 
43
43
 
44
+ def _write_grok_auth(path: Path, lease: dict) -> None:
45
+ """Write the leased `auth.json` for the `grok` CLI.
46
+
47
+ Same deal as codex — the pool already stripped `refresh_token`, so this
48
+ just persists the sanitized bundle to `$GROK_HOME/auth.json`. Note the
49
+ payload keys are grok-build's own: the access token is `key`, and expiry
50
+ is `expires_at` as an RFC3339 string. Neither is rewritten here; the pool
51
+ is the only thing that mints them.
52
+ """
53
+ creds = lease.get("credentials") or {}
54
+ auth = creds.get("auth_json")
55
+ if not isinstance(auth, dict):
56
+ raise RuntimeError(
57
+ "lease response did not include a Grok auth_json bundle"
58
+ )
59
+ _atomic_write_json(path, auth)
60
+
61
+
44
62
  def _write_codex_auth(path: Path, lease: dict) -> None:
45
63
  """Write the leased `auth.json` exactly as the pool returned it.
46
64
 
@@ -128,6 +146,13 @@ class CliProvider:
128
146
  # Pluggable credential writer — kept on the provider so the engine
129
147
  # never has to switch on `name`.
130
148
  write_credentials: Callable[[Path, dict], None]
149
+ # How long before expiry the pool re-mints this provider's token. The
150
+ # default suits a CLI that keeps working on a nearly-expired token; raise
151
+ # it for one that stops early. grok refuses to run once its token is
152
+ # within GROK_AUTH_EARLY_INVALIDATION_SECS (300s) of expiry — and since
153
+ # the leased copy has no refresh_token, it can't self-heal, it just
154
+ # reports "not authenticated". Verified against grok 1.0.5.
155
+ refresh_slack_s: float = 600.0
131
156
  # Optional first-run hook called on the persistent home dir before
132
157
  # symlinks get laid down. Used by Claude to seed a no-onboarding
133
158
  # `.claude.json`; Codex doesn't need it.
@@ -165,9 +190,33 @@ CODEX = CliProvider(
165
190
  )
166
191
 
167
192
 
193
+ GROK = CliProvider(
194
+ name="grok",
195
+ default_bin="grok",
196
+ config_dir_env="GROK_HOME",
197
+ cred_filename="auth.json",
198
+ cred_skip_prefixes=("auth.json",),
199
+ home_subdir="grok-home",
200
+ # An ambient XAI_API_KEY makes grok skip the leased auth.json entirely and
201
+ # bill the pay-per-token API instead of the subscription we leased.
202
+ bypass_env_vars=("XAI_API_KEY",),
203
+ lease_body_extras={"provider": "grok"},
204
+ write_credentials=_write_grok_auth,
205
+ # 30 min, six times grok's own 300s cut-off. Its tokens live 6h, so this
206
+ # still means one rotation per session at most — cheap insurance against
207
+ # a late or briefly-failing refresh dropping the CLI into a login prompt
208
+ # it cannot complete.
209
+ refresh_slack_s=1800.0,
210
+ )
211
+
212
+
213
+ _PROVIDERS = {p.name: p for p in (CLAUDE, CODEX, GROK)}
214
+
215
+
168
216
  def get_provider(name: str) -> CliProvider:
169
- if name == "claude":
170
- return CLAUDE
171
- if name == "codex":
172
- return CODEX
173
- raise ValueError(f"unknown provider {name!r}")
217
+ try:
218
+ return _PROVIDERS[name]
219
+ except KeyError:
220
+ raise ValueError(
221
+ f"unknown provider {name!r} (known: {', '.join(sorted(_PROVIDERS))})"
222
+ ) from None
@@ -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.7.0'
22
- __version_tuple__ = version_tuple = (0, 7, 0)
21
+ __version__ = version = '0.8.0'
22
+ __version_tuple__ = version_tuple = (0, 8, 0)
23
23
 
24
24
  __commit_id__ = commit_id = None
@@ -0,0 +1,61 @@
1
+ """sp-grok — run the `grok` CLI under a credential leased from sub-pool.
2
+
3
+ sp-grok # interactive TUI, equivalent to bare `grok`
4
+ sp-grok -- --help # arguments after `--` go straight to grok
5
+
6
+ Setup / introspection:
7
+
8
+ sp-grok --setup # one-time wizard, writes ~/.sub-pool/cli.toml
9
+ sp-grok --status # print current pool URL + last-used account
10
+ sp-grok --account NAME # bind to a specific Grok account
11
+ sp-grok --grok-bin /path # override `grok` binary location
12
+ sp-grok --verbose # print swap / refresh events to stderr
13
+
14
+ While the session is alive `$GROK_HOME/auth.json` is kept current — token
15
+ rotations and mid-session account swaps happen transparently in the
16
+ background, so the leased account stays in rotation across long runs.
17
+
18
+ Grok Build refreshes proactively ~5 min before expiry
19
+ (`GROK_AUTH_EARLY_INVALIDATION_SECS`), and the copy it holds carries no
20
+ refresh_token — the pool owns that chain. The client's refresh loop is
21
+ anchored to expiry and rewrites auth.json well before that window, so grok
22
+ never reaches the point of asking for an interactive login.
23
+ """
24
+ from __future__ import annotations
25
+
26
+ import asyncio
27
+ import sys
28
+
29
+ import click
30
+
31
+ from sub_pool_client._cli_core import run_session, run_setup, run_status
32
+ from sub_pool_client._cli_provider import GROK
33
+
34
+
35
+ @click.command(
36
+ context_settings={"ignore_unknown_options": True, "allow_extra_args": True},
37
+ help="Run `grok` CLI with credentials leased from a sub-pool server.",
38
+ )
39
+ @click.argument("grok_args", nargs=-1, type=click.UNPROCESSED)
40
+ @click.option("--setup", is_flag=True, help="One-time pool config wizard.")
41
+ @click.option("--status", is_flag=True, help="Show current config + exit.")
42
+ @click.option("--account", default=None,
43
+ help="Bind to a specific Grok account, skipping round-robin "
44
+ "order. Subject to the api key's strategy eligibility.")
45
+ @click.option("--grok-bin", default=None,
46
+ help="Override path to `grok` binary.")
47
+ @click.option("--verbose", is_flag=True,
48
+ help="Print swap / refresh events to stderr.")
49
+ def main(grok_args, setup, status, account, grok_bin, verbose) -> None:
50
+ if setup:
51
+ sys.exit(run_setup(GROK))
52
+ if status:
53
+ sys.exit(run_status(GROK))
54
+ code = asyncio.run(run_session(
55
+ GROK, grok_args, account, grok_bin, verbose,
56
+ ))
57
+ sys.exit(code)
58
+
59
+
60
+ if __name__ == "__main__":
61
+ main()