sub-pool-client 0.1.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.
Potentially problematic release.
This version of sub-pool-client might be problematic. Click here for more details.
- sub_pool_client-0.1.0/.github/workflows/publish.yml +83 -0
- sub_pool_client-0.1.0/.gitignore +40 -0
- sub_pool_client-0.1.0/PKG-INFO +207 -0
- sub_pool_client-0.1.0/README.md +195 -0
- sub_pool_client-0.1.0/examples/codex_hello.py +46 -0
- sub_pool_client-0.1.0/examples/hello.py +70 -0
- sub_pool_client-0.1.0/meta/README.md +21 -0
- sub_pool_client-0.1.0/meta/pyproject.toml +24 -0
- sub_pool_client-0.1.0/pyproject.toml +35 -0
- sub_pool_client-0.1.0/sub_pool_client/__init__.py +22 -0
- sub_pool_client-0.1.0/sub_pool_client/_cli_core.py +527 -0
- sub_pool_client-0.1.0/sub_pool_client/_cli_provider.py +142 -0
- sub_pool_client-0.1.0/sub_pool_client/_health_watcher.py +73 -0
- sub_pool_client-0.1.0/sub_pool_client/_shared.py +306 -0
- sub_pool_client-0.1.0/sub_pool_client/_swap.py +104 -0
- sub_pool_client-0.1.0/sub_pool_client/_version.py +24 -0
- sub_pool_client-0.1.0/sub_pool_client/cli_claude.py +56 -0
- sub_pool_client-0.1.0/sub_pool_client/cli_codex.py +58 -0
- sub_pool_client-0.1.0/sub_pool_client/client.py +462 -0
- sub_pool_client-0.1.0/sub_pool_client/codex.py +398 -0
- sub_pool_client-0.1.0/sub_pool_client/errors.py +47 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
name: publish
|
|
2
|
+
|
|
3
|
+
# Tag-driven publish for sub-pool-client. TestPyPI runs first
|
|
4
|
+
# unconditionally; the PyPI job is gated by the `pypi` GitHub
|
|
5
|
+
# environment (configure it to require a reviewer so the real publish
|
|
6
|
+
# doesn't go out without a click).
|
|
7
|
+
#
|
|
8
|
+
# Trusted Publishing — no long-lived tokens. Two pending publishers
|
|
9
|
+
# need to be registered (owner=Zephyroam, repo=sub-pool-client,
|
|
10
|
+
# workflow=publish.yml):
|
|
11
|
+
#
|
|
12
|
+
# - PyPI env=pypi project=sub-pool-client
|
|
13
|
+
# - TestPyPI env=testpypi project=sub-pool-client
|
|
14
|
+
#
|
|
15
|
+
# The companion `sub-pool` placeholder distribution (see meta/) is
|
|
16
|
+
# wired up in source but NOT in this workflow — register a pending
|
|
17
|
+
# publisher for it and add the build/publish jobs back when you want
|
|
18
|
+
# to claim the short name on PyPI.
|
|
19
|
+
|
|
20
|
+
on:
|
|
21
|
+
push:
|
|
22
|
+
tags:
|
|
23
|
+
- "v*"
|
|
24
|
+
|
|
25
|
+
permissions:
|
|
26
|
+
contents: read
|
|
27
|
+
|
|
28
|
+
jobs:
|
|
29
|
+
build:
|
|
30
|
+
runs-on: ubuntu-latest
|
|
31
|
+
steps:
|
|
32
|
+
- uses: actions/checkout@v4
|
|
33
|
+
with:
|
|
34
|
+
# hatch-vcs derives the version from the nearest tag — needs
|
|
35
|
+
# the full commit + tag graph, not a shallow clone.
|
|
36
|
+
fetch-depth: 0
|
|
37
|
+
- uses: actions/setup-python@v5
|
|
38
|
+
with:
|
|
39
|
+
python-version: "3.12"
|
|
40
|
+
- name: Install build
|
|
41
|
+
run: pip install build
|
|
42
|
+
- name: Build sub-pool-client
|
|
43
|
+
run: python -m build
|
|
44
|
+
- uses: actions/upload-artifact@v4
|
|
45
|
+
with:
|
|
46
|
+
name: dist
|
|
47
|
+
path: dist/
|
|
48
|
+
|
|
49
|
+
testpypi:
|
|
50
|
+
needs: build
|
|
51
|
+
runs-on: ubuntu-latest
|
|
52
|
+
environment:
|
|
53
|
+
name: testpypi
|
|
54
|
+
url: https://test.pypi.org/project/sub-pool-client/
|
|
55
|
+
permissions:
|
|
56
|
+
id-token: write
|
|
57
|
+
steps:
|
|
58
|
+
- uses: actions/download-artifact@v4
|
|
59
|
+
with:
|
|
60
|
+
name: dist
|
|
61
|
+
path: dist/
|
|
62
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
63
|
+
with:
|
|
64
|
+
repository-url: https://test.pypi.org/legacy/
|
|
65
|
+
# Allow re-running the workflow on the same tag (cosmetic
|
|
66
|
+
# README amends etc.) without hard-failing on a duplicate
|
|
67
|
+
# version.
|
|
68
|
+
skip-existing: true
|
|
69
|
+
|
|
70
|
+
pypi:
|
|
71
|
+
needs: testpypi
|
|
72
|
+
runs-on: ubuntu-latest
|
|
73
|
+
environment:
|
|
74
|
+
name: pypi
|
|
75
|
+
url: https://pypi.org/project/sub-pool-client/
|
|
76
|
+
permissions:
|
|
77
|
+
id-token: write
|
|
78
|
+
steps:
|
|
79
|
+
- uses: actions/download-artifact@v4
|
|
80
|
+
with:
|
|
81
|
+
name: dist
|
|
82
|
+
path: dist/
|
|
83
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
__pycache__/
|
|
2
|
+
*.py[cod]
|
|
3
|
+
*$py.class
|
|
4
|
+
*.so
|
|
5
|
+
|
|
6
|
+
.Python
|
|
7
|
+
build/
|
|
8
|
+
develop-eggs/
|
|
9
|
+
dist/
|
|
10
|
+
downloads/
|
|
11
|
+
eggs/
|
|
12
|
+
.eggs/
|
|
13
|
+
lib/
|
|
14
|
+
lib64/
|
|
15
|
+
parts/
|
|
16
|
+
sdist/
|
|
17
|
+
var/
|
|
18
|
+
wheels/
|
|
19
|
+
*.egg-info/
|
|
20
|
+
.installed.cfg
|
|
21
|
+
*.egg
|
|
22
|
+
|
|
23
|
+
.venv/
|
|
24
|
+
venv/
|
|
25
|
+
env/
|
|
26
|
+
ENV/
|
|
27
|
+
|
|
28
|
+
.pytest_cache/
|
|
29
|
+
.mypy_cache/
|
|
30
|
+
.ruff_cache/
|
|
31
|
+
.tox/
|
|
32
|
+
.coverage
|
|
33
|
+
htmlcov/
|
|
34
|
+
|
|
35
|
+
sub_pool_client/_version.py
|
|
36
|
+
|
|
37
|
+
.idea/
|
|
38
|
+
.vscode/
|
|
39
|
+
*.swp
|
|
40
|
+
.DS_Store
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sub-pool-client
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python client + `sp-claude` / `sp-codex` CLIs for a sub-pool credential broker.
|
|
5
|
+
Project-URL: Homepage, https://github.com/Zephyroam/sub-pool-client
|
|
6
|
+
Project-URL: Repository, https://github.com/Zephyroam/sub-pool-client
|
|
7
|
+
Requires-Python: >=3.12
|
|
8
|
+
Requires-Dist: claude-agent-sdk>=0.1.63
|
|
9
|
+
Requires-Dist: click>=8.1
|
|
10
|
+
Requires-Dist: httpx>=0.27
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# sub-pool-client
|
|
14
|
+
|
|
15
|
+
Python client + interactive CLIs for a `sub-pool` server — a
|
|
16
|
+
credential broker for N Claude **and** OpenAI Codex subscription
|
|
17
|
+
accounts.
|
|
18
|
+
|
|
19
|
+
You get three things in one package:
|
|
20
|
+
|
|
21
|
+
1. **`sp-claude`** — a `claude` CLI wrapper. Leases a Claude account
|
|
22
|
+
from the pool, runs the real `claude` binary against a per-session
|
|
23
|
+
`CLAUDE_CONFIG_DIR`, rotates the access_token in the background, and
|
|
24
|
+
swaps accounts transparently if the leased one cools mid-run.
|
|
25
|
+
2. **`sp-codex`** — same flow for OpenAI Codex. Leases a Codex
|
|
26
|
+
`auth.json`, drops it into an isolated `CODEX_HOME`, spawns the
|
|
27
|
+
real `codex` binary, and rotates the token in the background.
|
|
28
|
+
3. **`PooledClient` / `PooledCodexClient`** — Python SDK classes for
|
|
29
|
+
programs and services. `PooledClient` is a drop-in subclass of
|
|
30
|
+
`ClaudeSDKClient` from the official `claude-agent-sdk`;
|
|
31
|
+
`PooledCodexClient` is a thin async wrapper around `codex exec`.
|
|
32
|
+
|
|
33
|
+
The agent loop — tool execution, filesystem I/O, hooks, MCP servers,
|
|
34
|
+
the codex subprocess — all stays on the consumer's machine. The pool
|
|
35
|
+
only sees lease lifecycles.
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install git+https://github.com/Zephyroam/sub-pool-client
|
|
41
|
+
# or
|
|
42
|
+
uv pip install git+https://github.com/Zephyroam/sub-pool-client
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Environment
|
|
46
|
+
|
|
47
|
+
Both the CLIs and the SDK read:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
export SUB_POOL_URL=http://your-pool-host:8787
|
|
51
|
+
export SUB_POOL_KEY=cp-... # admin issues this from the sub-pool UI
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`SUB_POOL_KEY`'s strategy decides which accounts you can reach.
|
|
55
|
+
|
|
56
|
+
## 1. CLIs
|
|
57
|
+
|
|
58
|
+
### `sp-claude` — `claude` wrapper
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
sp-claude --setup # one-time interactive config wizard
|
|
62
|
+
sp-claude # start the claude REPL on a leased account
|
|
63
|
+
sp-claude "explain this" # single-prompt mode
|
|
64
|
+
sp-claude --account alice # pin to a specific account
|
|
65
|
+
sp-claude --status # show effective config and exit
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`sp-claude --setup` writes pool URL + API key to `~/.sub-pool/cli.toml`
|
|
69
|
+
at `0o600`. Subsequent invocations exec `claude` against a per-session
|
|
70
|
+
config dir that holds the leased `.credentials.json`. The persistent
|
|
71
|
+
home (`~/.sub-pool/claude-home/`) keeps your conversation history
|
|
72
|
+
across sessions and is isolated from `~/.claude/` — `sp-claude` never
|
|
73
|
+
reads or writes your real claude config.
|
|
74
|
+
|
|
75
|
+
If the leased account starts cooling (Anthropic rate-limit or quota
|
|
76
|
+
window), a background watcher swaps to a different account
|
|
77
|
+
transparently. Your running session sees one continuous `claude`
|
|
78
|
+
process; only the access_token under the hood changes.
|
|
79
|
+
|
|
80
|
+
### `sp-codex` — `codex` wrapper
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
sp-codex --setup
|
|
84
|
+
sp-codex # interactive REPL on a leased Codex account
|
|
85
|
+
sp-codex exec "summarize ." # one-shot
|
|
86
|
+
sp-codex --account codex1
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Shares `~/.sub-pool/cli.toml` with `sp-claude`. Persistent
|
|
90
|
+
`CODEX_HOME` lives at `~/.sub-pool/codex-home/`.
|
|
91
|
+
|
|
92
|
+
## 2. Python SDK
|
|
93
|
+
|
|
94
|
+
### `PooledClient` (Claude)
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
import asyncio
|
|
98
|
+
from claude_agent_sdk import ClaudeAgentOptions
|
|
99
|
+
from sub_pool_client import PooledClient
|
|
100
|
+
|
|
101
|
+
async def main():
|
|
102
|
+
options = ClaudeAgentOptions(system_prompt="Be terse.")
|
|
103
|
+
async with PooledClient(options=options) as client:
|
|
104
|
+
print(f"leased {client.account} lease_id={client.lease_id}")
|
|
105
|
+
await client.query("List three prime numbers.")
|
|
106
|
+
async for msg in client.receive_response():
|
|
107
|
+
print(type(msg).__name__, "->", msg)
|
|
108
|
+
|
|
109
|
+
asyncio.run(main())
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
`PooledClient` subclasses `ClaudeSDKClient`, so every option the SDK
|
|
113
|
+
accepts (hooks, MCP servers, `cwd`, `allowed_tools`,
|
|
114
|
+
`permission_mode`) works unchanged.
|
|
115
|
+
|
|
116
|
+
#### Rate-limit handling
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
from claude_agent_sdk import ClaudeSDKError
|
|
120
|
+
|
|
121
|
+
async with PooledClient(options=options) as client:
|
|
122
|
+
try:
|
|
123
|
+
await client.query("...")
|
|
124
|
+
async for msg in client.receive_response():
|
|
125
|
+
...
|
|
126
|
+
except ClaudeSDKError as e:
|
|
127
|
+
if "429" in str(e) or "rate" in str(e).lower():
|
|
128
|
+
await client.report_error("RateLimit", str(e))
|
|
129
|
+
raise
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
`report_error` posts to `/credentials/lease/{id}/report-error`; the
|
|
133
|
+
pool marks the account COOLING so the next lease skips it.
|
|
134
|
+
|
|
135
|
+
#### Concurrent agents
|
|
136
|
+
|
|
137
|
+
Multiple `async with PooledClient()` calls in the same process — or
|
|
138
|
+
across sibling processes — with identical routing inputs (`user_id`,
|
|
139
|
+
`required_model`) **share a single lease**. Coordination happens
|
|
140
|
+
through `~/.sub-pool/client/<hash>/` (flock + holder list). First
|
|
141
|
+
arriver leases; later arrivers refcount; last out releases. So N
|
|
142
|
+
parallel agents on one account work up to Anthropic's per-account
|
|
143
|
+
concurrency limit. Vary `user_id` (with the `sticky_user` strategy)
|
|
144
|
+
to force different accounts in parallel.
|
|
145
|
+
|
|
146
|
+
### `PooledCodexClient` (Codex)
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
import asyncio
|
|
150
|
+
from sub_pool_client import PooledCodexClient
|
|
151
|
+
|
|
152
|
+
async def main():
|
|
153
|
+
async with PooledCodexClient() as codex:
|
|
154
|
+
proc = await codex.exec(
|
|
155
|
+
"summarize this repository",
|
|
156
|
+
cwd=".",
|
|
157
|
+
model="gpt-5.5",
|
|
158
|
+
)
|
|
159
|
+
stdout, stderr = await proc.communicate()
|
|
160
|
+
print(stdout.decode())
|
|
161
|
+
|
|
162
|
+
asyncio.run(main())
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
`PooledCodexClient` writes the leased (sanitized) `auth.json` into an
|
|
166
|
+
isolated `CODEX_HOME` and spawns `codex exec` against it. A
|
|
167
|
+
background poll task rotates the on-disk `auth.json` before the
|
|
168
|
+
access_token expires. On a backend error, call
|
|
169
|
+
`await codex.report_error("RateLimit", "...")` so the pool cools the
|
|
170
|
+
account.
|
|
171
|
+
|
|
172
|
+
## Pool invariants this client relies on
|
|
173
|
+
|
|
174
|
+
- The pool exclusively owns the `refresh_token` chain (both Anthropic
|
|
175
|
+
and OpenAI). Leases hand out only the access_token; the
|
|
176
|
+
`refreshToken` field in the leased credentials file is blank.
|
|
177
|
+
- Token rotation happens through `POST
|
|
178
|
+
/credentials/lease/{lease_id}/token` — a consumer crash can never
|
|
179
|
+
leak the pool's refresh state.
|
|
180
|
+
- `account_name`s come from the pool's admin config. The pool decides
|
|
181
|
+
which account fulfills each lease based on the API key's strategy.
|
|
182
|
+
|
|
183
|
+
The `sub-pool` server (admin UI, strategy reference, account
|
|
184
|
+
lifecycle) is hosted separately — point this client at whatever pool
|
|
185
|
+
URL your admin gave you via `SUB_POOL_URL` above.
|
|
186
|
+
|
|
187
|
+
## Layout under `~/.sub-pool/`
|
|
188
|
+
|
|
189
|
+
```
|
|
190
|
+
~/.sub-pool/
|
|
191
|
+
├── cli.toml # sp-claude / sp-codex shared config (0o600)
|
|
192
|
+
├── claude-home/ # sp-claude persistent CLAUDE_CONFIG_DIR
|
|
193
|
+
├── codex-home/ # sp-codex persistent CODEX_HOME
|
|
194
|
+
└── client/<hash>/ # PooledClient / PooledCodexClient
|
|
195
|
+
# cross-process lease coordination
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Override the SDK shared-dir root with `SUB_POOL_CLIENT_DIR`.
|
|
199
|
+
|
|
200
|
+
## Examples
|
|
201
|
+
|
|
202
|
+
- `examples/hello.py` — `PooledClient` end-to-end with a retry loop on 429.
|
|
203
|
+
- `examples/codex_hello.py` — `PooledCodexClient` running `codex exec`.
|
|
204
|
+
|
|
205
|
+
## License
|
|
206
|
+
|
|
207
|
+
(TBD — defer to upstream)
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
# sub-pool-client
|
|
2
|
+
|
|
3
|
+
Python client + interactive CLIs for a `sub-pool` server — a
|
|
4
|
+
credential broker for N Claude **and** OpenAI Codex subscription
|
|
5
|
+
accounts.
|
|
6
|
+
|
|
7
|
+
You get three things in one package:
|
|
8
|
+
|
|
9
|
+
1. **`sp-claude`** — a `claude` CLI wrapper. Leases a Claude account
|
|
10
|
+
from the pool, runs the real `claude` binary against a per-session
|
|
11
|
+
`CLAUDE_CONFIG_DIR`, rotates the access_token in the background, and
|
|
12
|
+
swaps accounts transparently if the leased one cools mid-run.
|
|
13
|
+
2. **`sp-codex`** — same flow for OpenAI Codex. Leases a Codex
|
|
14
|
+
`auth.json`, drops it into an isolated `CODEX_HOME`, spawns the
|
|
15
|
+
real `codex` binary, and rotates the token in the background.
|
|
16
|
+
3. **`PooledClient` / `PooledCodexClient`** — Python SDK classes for
|
|
17
|
+
programs and services. `PooledClient` is a drop-in subclass of
|
|
18
|
+
`ClaudeSDKClient` from the official `claude-agent-sdk`;
|
|
19
|
+
`PooledCodexClient` is a thin async wrapper around `codex exec`.
|
|
20
|
+
|
|
21
|
+
The agent loop — tool execution, filesystem I/O, hooks, MCP servers,
|
|
22
|
+
the codex subprocess — all stays on the consumer's machine. The pool
|
|
23
|
+
only sees lease lifecycles.
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install git+https://github.com/Zephyroam/sub-pool-client
|
|
29
|
+
# or
|
|
30
|
+
uv pip install git+https://github.com/Zephyroam/sub-pool-client
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Environment
|
|
34
|
+
|
|
35
|
+
Both the CLIs and the SDK read:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
export SUB_POOL_URL=http://your-pool-host:8787
|
|
39
|
+
export SUB_POOL_KEY=cp-... # admin issues this from the sub-pool UI
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
`SUB_POOL_KEY`'s strategy decides which accounts you can reach.
|
|
43
|
+
|
|
44
|
+
## 1. CLIs
|
|
45
|
+
|
|
46
|
+
### `sp-claude` — `claude` wrapper
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
sp-claude --setup # one-time interactive config wizard
|
|
50
|
+
sp-claude # start the claude REPL on a leased account
|
|
51
|
+
sp-claude "explain this" # single-prompt mode
|
|
52
|
+
sp-claude --account alice # pin to a specific account
|
|
53
|
+
sp-claude --status # show effective config and exit
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`sp-claude --setup` writes pool URL + API key to `~/.sub-pool/cli.toml`
|
|
57
|
+
at `0o600`. Subsequent invocations exec `claude` against a per-session
|
|
58
|
+
config dir that holds the leased `.credentials.json`. The persistent
|
|
59
|
+
home (`~/.sub-pool/claude-home/`) keeps your conversation history
|
|
60
|
+
across sessions and is isolated from `~/.claude/` — `sp-claude` never
|
|
61
|
+
reads or writes your real claude config.
|
|
62
|
+
|
|
63
|
+
If the leased account starts cooling (Anthropic rate-limit or quota
|
|
64
|
+
window), a background watcher swaps to a different account
|
|
65
|
+
transparently. Your running session sees one continuous `claude`
|
|
66
|
+
process; only the access_token under the hood changes.
|
|
67
|
+
|
|
68
|
+
### `sp-codex` — `codex` wrapper
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
sp-codex --setup
|
|
72
|
+
sp-codex # interactive REPL on a leased Codex account
|
|
73
|
+
sp-codex exec "summarize ." # one-shot
|
|
74
|
+
sp-codex --account codex1
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Shares `~/.sub-pool/cli.toml` with `sp-claude`. Persistent
|
|
78
|
+
`CODEX_HOME` lives at `~/.sub-pool/codex-home/`.
|
|
79
|
+
|
|
80
|
+
## 2. Python SDK
|
|
81
|
+
|
|
82
|
+
### `PooledClient` (Claude)
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
import asyncio
|
|
86
|
+
from claude_agent_sdk import ClaudeAgentOptions
|
|
87
|
+
from sub_pool_client import PooledClient
|
|
88
|
+
|
|
89
|
+
async def main():
|
|
90
|
+
options = ClaudeAgentOptions(system_prompt="Be terse.")
|
|
91
|
+
async with PooledClient(options=options) as client:
|
|
92
|
+
print(f"leased {client.account} lease_id={client.lease_id}")
|
|
93
|
+
await client.query("List three prime numbers.")
|
|
94
|
+
async for msg in client.receive_response():
|
|
95
|
+
print(type(msg).__name__, "->", msg)
|
|
96
|
+
|
|
97
|
+
asyncio.run(main())
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
`PooledClient` subclasses `ClaudeSDKClient`, so every option the SDK
|
|
101
|
+
accepts (hooks, MCP servers, `cwd`, `allowed_tools`,
|
|
102
|
+
`permission_mode`) works unchanged.
|
|
103
|
+
|
|
104
|
+
#### Rate-limit handling
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
from claude_agent_sdk import ClaudeSDKError
|
|
108
|
+
|
|
109
|
+
async with PooledClient(options=options) as client:
|
|
110
|
+
try:
|
|
111
|
+
await client.query("...")
|
|
112
|
+
async for msg in client.receive_response():
|
|
113
|
+
...
|
|
114
|
+
except ClaudeSDKError as e:
|
|
115
|
+
if "429" in str(e) or "rate" in str(e).lower():
|
|
116
|
+
await client.report_error("RateLimit", str(e))
|
|
117
|
+
raise
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
`report_error` posts to `/credentials/lease/{id}/report-error`; the
|
|
121
|
+
pool marks the account COOLING so the next lease skips it.
|
|
122
|
+
|
|
123
|
+
#### Concurrent agents
|
|
124
|
+
|
|
125
|
+
Multiple `async with PooledClient()` calls in the same process — or
|
|
126
|
+
across sibling processes — with identical routing inputs (`user_id`,
|
|
127
|
+
`required_model`) **share a single lease**. Coordination happens
|
|
128
|
+
through `~/.sub-pool/client/<hash>/` (flock + holder list). First
|
|
129
|
+
arriver leases; later arrivers refcount; last out releases. So N
|
|
130
|
+
parallel agents on one account work up to Anthropic's per-account
|
|
131
|
+
concurrency limit. Vary `user_id` (with the `sticky_user` strategy)
|
|
132
|
+
to force different accounts in parallel.
|
|
133
|
+
|
|
134
|
+
### `PooledCodexClient` (Codex)
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
import asyncio
|
|
138
|
+
from sub_pool_client import PooledCodexClient
|
|
139
|
+
|
|
140
|
+
async def main():
|
|
141
|
+
async with PooledCodexClient() as codex:
|
|
142
|
+
proc = await codex.exec(
|
|
143
|
+
"summarize this repository",
|
|
144
|
+
cwd=".",
|
|
145
|
+
model="gpt-5.5",
|
|
146
|
+
)
|
|
147
|
+
stdout, stderr = await proc.communicate()
|
|
148
|
+
print(stdout.decode())
|
|
149
|
+
|
|
150
|
+
asyncio.run(main())
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
`PooledCodexClient` writes the leased (sanitized) `auth.json` into an
|
|
154
|
+
isolated `CODEX_HOME` and spawns `codex exec` against it. A
|
|
155
|
+
background poll task rotates the on-disk `auth.json` before the
|
|
156
|
+
access_token expires. On a backend error, call
|
|
157
|
+
`await codex.report_error("RateLimit", "...")` so the pool cools the
|
|
158
|
+
account.
|
|
159
|
+
|
|
160
|
+
## Pool invariants this client relies on
|
|
161
|
+
|
|
162
|
+
- The pool exclusively owns the `refresh_token` chain (both Anthropic
|
|
163
|
+
and OpenAI). Leases hand out only the access_token; the
|
|
164
|
+
`refreshToken` field in the leased credentials file is blank.
|
|
165
|
+
- Token rotation happens through `POST
|
|
166
|
+
/credentials/lease/{lease_id}/token` — a consumer crash can never
|
|
167
|
+
leak the pool's refresh state.
|
|
168
|
+
- `account_name`s come from the pool's admin config. The pool decides
|
|
169
|
+
which account fulfills each lease based on the API key's strategy.
|
|
170
|
+
|
|
171
|
+
The `sub-pool` server (admin UI, strategy reference, account
|
|
172
|
+
lifecycle) is hosted separately — point this client at whatever pool
|
|
173
|
+
URL your admin gave you via `SUB_POOL_URL` above.
|
|
174
|
+
|
|
175
|
+
## Layout under `~/.sub-pool/`
|
|
176
|
+
|
|
177
|
+
```
|
|
178
|
+
~/.sub-pool/
|
|
179
|
+
├── cli.toml # sp-claude / sp-codex shared config (0o600)
|
|
180
|
+
├── claude-home/ # sp-claude persistent CLAUDE_CONFIG_DIR
|
|
181
|
+
├── codex-home/ # sp-codex persistent CODEX_HOME
|
|
182
|
+
└── client/<hash>/ # PooledClient / PooledCodexClient
|
|
183
|
+
# cross-process lease coordination
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
Override the SDK shared-dir root with `SUB_POOL_CLIENT_DIR`.
|
|
187
|
+
|
|
188
|
+
## Examples
|
|
189
|
+
|
|
190
|
+
- `examples/hello.py` — `PooledClient` end-to-end with a retry loop on 429.
|
|
191
|
+
- `examples/codex_hello.py` — `PooledCodexClient` running `codex exec`.
|
|
192
|
+
|
|
193
|
+
## License
|
|
194
|
+
|
|
195
|
+
(TBD — defer to upstream)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""Minimal Codex usage through the pool.
|
|
2
|
+
|
|
3
|
+
Before running:
|
|
4
|
+
1. Install: uv pip install -e .
|
|
5
|
+
2. Have a running sub-pool server with at least one codex account
|
|
6
|
+
connected via the admin UI (provider: codex).
|
|
7
|
+
3. Create an API key on the same server.
|
|
8
|
+
4. Have the official `codex` CLI on PATH.
|
|
9
|
+
5. Run: SUB_POOL_URL=http://localhost:8787 \
|
|
10
|
+
SUB_POOL_KEY=cp-... \
|
|
11
|
+
python examples/codex_hello.py
|
|
12
|
+
|
|
13
|
+
The pool leases an auth.json onto your machine, the local codex CLI
|
|
14
|
+
runs against it, and the pool rotates the access_token transparently
|
|
15
|
+
through a background poll. The refresh_token never leaves the pool.
|
|
16
|
+
"""
|
|
17
|
+
import asyncio
|
|
18
|
+
|
|
19
|
+
from sub_pool_client import PooledCodexClient
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
async def main() -> None:
|
|
23
|
+
async with PooledCodexClient() as codex:
|
|
24
|
+
print(f"leased account={codex.account} lease_id={codex.lease_id}")
|
|
25
|
+
print(f"CODEX_HOME={codex.env['CODEX_HOME']}")
|
|
26
|
+
|
|
27
|
+
proc = await codex.exec(
|
|
28
|
+
"Print the string 'hello, codex' and nothing else.",
|
|
29
|
+
"--cd", ".",
|
|
30
|
+
)
|
|
31
|
+
stdout, stderr = await proc.communicate()
|
|
32
|
+
|
|
33
|
+
if proc.returncode != 0:
|
|
34
|
+
err = stderr.decode(errors="replace")
|
|
35
|
+
print(f"codex exit={proc.returncode}: {err[:400]}")
|
|
36
|
+
# Tell the pool if it looks like an upstream rate-limit so
|
|
37
|
+
# the account gets cooled and the next lease skips it.
|
|
38
|
+
if "rate" in err.lower() or "429" in err:
|
|
39
|
+
await codex.report_error("RateLimit", err[:500])
|
|
40
|
+
return
|
|
41
|
+
|
|
42
|
+
print(stdout.decode())
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
if __name__ == "__main__":
|
|
46
|
+
asyncio.run(main())
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""Minimal end-to-end usage (credential-broker mode).
|
|
2
|
+
|
|
3
|
+
Before running:
|
|
4
|
+
1. Install: uv pip install -e .
|
|
5
|
+
2. Have a running sub-pool server with at least one connected Claude
|
|
6
|
+
account and an API key.
|
|
7
|
+
3. Run: SUB_POOL_URL=http://localhost:8787 \
|
|
8
|
+
SUB_POOL_KEY=cp-... \
|
|
9
|
+
python examples/hello.py
|
|
10
|
+
|
|
11
|
+
The pool is a *credential broker*: it leases an OAuth access token to this
|
|
12
|
+
process, and the official `claude` CLI spawns locally. Tools run on this
|
|
13
|
+
box, files land in this directory, hooks fire here. Pool only sees the
|
|
14
|
+
lease request/release + any usage you choose to report back.
|
|
15
|
+
|
|
16
|
+
## Handling rate limits
|
|
17
|
+
|
|
18
|
+
If Anthropic returns 429 mid-agent-run, catch it and call
|
|
19
|
+
`client.report_error(code, message)` so the pool marks the account
|
|
20
|
+
COOLING. A follow-up `async with PooledClient(...)` picks another
|
|
21
|
+
account (if you have more than one) or sleeps until the cooldown
|
|
22
|
+
expires. See `_with_retry()` below for the pattern.
|
|
23
|
+
"""
|
|
24
|
+
import asyncio
|
|
25
|
+
|
|
26
|
+
from claude_agent_sdk import ClaudeAgentOptions, ClaudeSDKError
|
|
27
|
+
from sub_pool_client import PooledClient
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
async def _run_once(prompt: str) -> bool:
|
|
31
|
+
"""Returns True on success, False if the account is rate-limited
|
|
32
|
+
(caller should retry)."""
|
|
33
|
+
options = ClaudeAgentOptions(
|
|
34
|
+
system_prompt="You are terse. Answer in one short sentence.",
|
|
35
|
+
)
|
|
36
|
+
async with PooledClient(options=options) as client:
|
|
37
|
+
print(f"leased account={client.account} lease_id={client.lease_id}")
|
|
38
|
+
try:
|
|
39
|
+
await client.query(prompt)
|
|
40
|
+
async for msg in client.receive_response():
|
|
41
|
+
content = getattr(msg, "content", None)
|
|
42
|
+
if content:
|
|
43
|
+
for block in content:
|
|
44
|
+
text = getattr(block, "text", None)
|
|
45
|
+
if text:
|
|
46
|
+
print(f"<< {text}")
|
|
47
|
+
except ClaudeSDKError as e:
|
|
48
|
+
msg_text = str(e).lower()
|
|
49
|
+
if any(k in msg_text for k in ("429", "rate", "quota")):
|
|
50
|
+
# Tell the pool so this account is cooled immediately —
|
|
51
|
+
# then re-enter PooledClient on a different account.
|
|
52
|
+
await client.report_error("RateLimit", str(e)[:500])
|
|
53
|
+
return False
|
|
54
|
+
raise
|
|
55
|
+
return True
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
async def main() -> None:
|
|
59
|
+
prompt = "List three prime numbers."
|
|
60
|
+
for attempt in range(3):
|
|
61
|
+
ok = await _run_once(prompt)
|
|
62
|
+
if ok:
|
|
63
|
+
return
|
|
64
|
+
print(f"attempt {attempt + 1} rate-limited; retrying with next account")
|
|
65
|
+
await asyncio.sleep(1)
|
|
66
|
+
print("all attempts rate-limited — try again later or add more accounts")
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
if __name__ == "__main__":
|
|
70
|
+
asyncio.run(main())
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# sub-pool
|
|
2
|
+
|
|
3
|
+
This distribution exists to **reserve the `sub-pool` name on PyPI**. It
|
|
4
|
+
has no Python module of its own — it depends on
|
|
5
|
+
[`sub-pool-client`](https://pypi.org/project/sub-pool-client/), so
|
|
6
|
+
`pip install sub-pool` actually gets you the client.
|
|
7
|
+
|
|
8
|
+
If you came here looking for the `sub-pool` server, that lives upstream
|
|
9
|
+
and is not (currently) on PyPI.
|
|
10
|
+
|
|
11
|
+
## What you probably want
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install sub-pool-client
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Then either use the CLIs (`sp-claude`, `sp-codex`) or the Python SDK
|
|
18
|
+
(`from sub_pool_client import PooledClient, PooledCodexClient`).
|
|
19
|
+
|
|
20
|
+
See <https://github.com/Zephyroam/sub-pool-client> for the full client
|
|
21
|
+
docs.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "sub-pool"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
description = "Reserved name — `pip install sub-pool` resolves to `sub-pool-client`. The pool server itself is upstream-only."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.12"
|
|
7
|
+
dependencies = ["sub-pool-client"]
|
|
8
|
+
|
|
9
|
+
[project.urls]
|
|
10
|
+
Homepage = "https://github.com/Zephyroam/sub-pool-client"
|
|
11
|
+
Repository = "https://github.com/Zephyroam/sub-pool-client"
|
|
12
|
+
|
|
13
|
+
[build-system]
|
|
14
|
+
requires = ["hatchling"]
|
|
15
|
+
build-backend = "hatchling.build"
|
|
16
|
+
|
|
17
|
+
# Placeholder distribution — no Python module shipped, so no namespace
|
|
18
|
+
# collision with the upstream `sub_pool` server module. Hatchling needs
|
|
19
|
+
# `force-include` to build a wheel with no Python packages.
|
|
20
|
+
[tool.hatch.build.targets.wheel]
|
|
21
|
+
bypass-selection = true
|
|
22
|
+
|
|
23
|
+
[tool.hatch.build.targets.sdist]
|
|
24
|
+
include = ["pyproject.toml", "README.md"]
|