@kapybara/zerosub 1.19.1 → 1.20.0
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.
- package/README.md +5 -5
- package/client/composer-item.tsx +13 -3
- package/package.json +1 -1
- package/server/codex.ts +1 -1
- package/server/families.ts +0 -1
- package/server/homes.ts +30 -6
- package/server/pi.ts +70 -19
- package/server/service.ts +9 -5
package/README.md
CHANGED
|
@@ -64,7 +64,7 @@ Then open **Accounts (ZeroSub)** in the sidebar.
|
|
|
64
64
|
|
|
65
65
|
### Choose accounts for new agents
|
|
66
66
|
|
|
67
|
-
Turn on **Spread new agents across accounts** to select an account from its usage. For ChatGPT agents, ZeroSub prefers an account when its weekly limit resets within one day and both limits have at least 20% left. Usage for every available account must be complete and less than 30 minutes old. Otherwise, and always for Claude agents, ZeroSub paces the accounts. It projects each account's 5-hour use an hour ahead from how fast it has used its current window, and adds 10% for each agent it placed there in the last 10 minutes, since readings lag behind new agents. It also compares weekly use with how much of the week has passed. An account projected to reach 80% of its 5-hour window, or already there, ranks behind every account that isn't. Among the rest, each account counts by the higher of its two figures, and the account with the lowest wins, so new agents use weekly room that would otherwise expire and avoid accounts running ahead of their week, and accounts don't all run out together. An account whose window has reset since its last reading counts by what it has left. While this is on, automatic switching picks the account to move to the same way. While it's off, switching picks the account with the most room, and an account at 80% of its 5-hour window ranks behind any under it. When a near weekly reset was the reason, the agent's account
|
|
67
|
+
Turn on **Spread new agents across accounts** to select an account from its usage. For ChatGPT agents, ZeroSub prefers an account when its weekly limit resets within one day and both limits have at least 20% left. Usage for every available account must be complete and less than 30 minutes old. Otherwise, and always for Claude agents, ZeroSub paces the accounts. It projects each account's 5-hour use an hour ahead from how fast it has used its current window, and adds 10% for each agent it placed there in the last 10 minutes, since readings lag behind new agents. It also compares weekly use with how much of the week has passed. An account projected to reach 80% of its 5-hour window, or already there, ranks behind every account that isn't. Among the rest, each account counts by the higher of its two figures, and the account with the lowest wins, so new agents use weekly room that would otherwise expire and avoid accounts running ahead of their week, and accounts don't all run out together. An account whose window has reset since its last reading counts by what it has left. While this is on, automatic switching picks the account to move to the same way. While it's off, switching picks the account with the most room, and an account at 80% of its 5-hour window ranks behind any under it. When a near weekly reset was the reason, the agent's account says so: **Weekly reset was near** beside the account in the **Account** control's list, or **Started here: weekly reset was near** in the account button's menu where there is no control. Otherwise, existing agents stay on their accounts.
|
|
68
68
|
|
|
69
69
|
With **Automatic switching** on too, ZeroSub also moves Claude agents that have been idle for an hour or more off an account projected to reach 80% of its 5-hour window. They go only to an account with room in both windows that stays under 80% of its 5-hour window with them. Agents used within the last hour stay put. Their next turn would cost more on another account, which has none of the conversation cached yet. Agents you put on an account yourself stay too, and so do ChatGPT agents, since their threads can't change accounts.
|
|
70
70
|
|
|
@@ -257,15 +257,15 @@ The model variables are set from the overrides, or blanked so a model named for
|
|
|
257
257
|
|
|
258
258
|
## Pi accounts
|
|
259
259
|
|
|
260
|
-
Paseo's built-in **Pi** agents get a section of their own once [Pi](https://github.com/earendil-works/pi) is installed where the daemon runs. Its first card is Pi's own login, `~/.pi/agent` (or `PI_CODING_AGENT_DIR` in the daemon's environment), with whatever it's signed in to. ZeroSub changes its files only when you edit its key or proxy on that card (see below; otherwise it only makes the directory and its `sessions` folder if they're missing, for added accounts to share), and a Pi agent on it runs as Paseo would run it without ZeroSub. Nothing else changes for Pi until you add an account: no scripts
|
|
260
|
+
Paseo's built-in **Pi** agents get a section of their own once [Pi](https://github.com/earendil-works/pi) is installed where the daemon runs. Its first card is Pi's own login, `~/.pi/agent` (or `PI_CODING_AGENT_DIR` in the daemon's environment), with whatever it's signed in to. ZeroSub changes its files only when you edit its key or proxy on that card (see below; otherwise it only makes the directory and its `sessions` folder if they're missing, for added accounts to share), While a proxy is set on that card, ZeroSub gives that login's Pi agents the proxy as environment variables (see below) and checks the address Anthropic sees; otherwise a Pi agent on it runs as Paseo would run it without ZeroSub. Nothing else changes for Pi until you add an account: no scripts and no folders outside ZeroSub's own. **Add account** under Pi adds an Anthropic API key as an account, each with:
|
|
261
261
|
|
|
262
262
|
- **Anthropic API key**, sent as `x-api-key`.
|
|
263
263
|
- **Base URL** (optional): a gateway that speaks Anthropic's API. Empty uses `https://api.anthropic.com`, even when Pi's own `models.json` sends Anthropic's requests somewhere else.
|
|
264
|
-
- **Proxy** (optional): every request its Pi agents make goes through it, Anthropic's and any other provider's alike. Change it later with the globe button on its card. The card shows the address Anthropic sees the account's requests come from (`IP: 203.0.113.7 (US)`), from Anthropic's `cdn-cgi/trace` fetched through its proxy when it's set: when the account is added or its proxy changes, and every half hour after.
|
|
264
|
+
- **Proxy** (optional): every request its Pi agents make goes through it, Anthropic's and any other provider's alike. Change it later with the globe button on its card. The card shows the address Anthropic sees the account's requests come from (`IP: 203.0.113.7 (US)`), from Anthropic's `cdn-cgi/trace` fetched through its proxy when it's set: when the account is added or its proxy changes, and every half hour after. Pi's own login is checked too, but only while a proxy is set on it, and not while its `models.json` sends Anthropic's requests elsewhere; an added account with another base URL isn't checked.
|
|
265
265
|
|
|
266
266
|
Pick an account for a Pi agent from the agent's account button or with `/account`, or make it the default. ZeroSub never moves agents onto a Pi account by itself, the CLI login included, since either may be billed per use: "Balance new agents" leaves new Pi agents on the default, and an agent whose key is refused stays where it is until you pick another account. A Pi agent moves between accounts in place: Pi reopens its session file on the other key. Pi doesn't report usage, so Pi accounts have no usage bars, banked resets or automatic switching, and they're never forked to another provider.
|
|
267
267
|
|
|
268
|
-
The key button on Pi's own login card changes the Anthropic API key and base URL that login uses, in Pi's own `auth.json` (`anthropic`; an Anthropic sign-in made with `/login` is replaced by the key) and `models.json` (`providers.anthropic.baseUrl`), so `pi` in a terminal uses them too. The globe button there sets its proxy as `httpProxy` in Pi's `settings.json`, which
|
|
268
|
+
The key button on Pi's own login card changes the Anthropic API key and base URL that login uses, in Pi's own `auth.json` (`anthropic`; an Anthropic sign-in made with `/login` is replaced by the key) and `models.json` (`providers.anthropic.baseUrl`), so `pi` in a terminal uses them too. The globe button there sets its proxy as `httpProxy` in Pi's `settings.json`, which `pi` in a terminal reads itself. ZeroSub also gives that login's agents the proxy as `https_proxy`, `HTTPS_PROXY`, `http_proxy` and `HTTP_PROXY`, so it wins over a proxy in the daemon's environment. Their `no_proxy`/`NO_PROXY` is the list they'd otherwise have, minus `*` and anything that would send Anthropic's traffic around it, plus `localhost`, `127.0.0.1` and `::1`. It does this only while no key or sign-in of the login's would cross the proxy over plain `http://`; otherwise it gives none, and Pi applies `httpProxy` itself. Each file is written where it really is (through a link to it), keeping the rest of it, under Pi's own lock on it, after a copy of it as it was goes to `$PASEO_HOME/zerosub/backups/` (the last five of each, readable only by you); a file that ends up holding a key or a proxy password is readable only by you. Leaving a key empty keeps it, a kept key only goes to the base URL it went to before, and a proxy is refused where a key or sign-in of the login's, for Anthropic or another provider, would go through it over plain `http://`: one in `auth.json`, in `models.json` (read as Pi reads it, comments and all) or in the environment, sent to the provider's base URL or one of its models'.
|
|
269
269
|
|
|
270
270
|
How it works:
|
|
271
271
|
|
|
@@ -274,7 +274,7 @@ How it works:
|
|
|
274
274
|
- Everything in Pi's own directory (settings, extensions, skills, prompts, sessions and the rest) is linked in, except Pi's sign-ins (`auth.json`), `models.json` and its model catalog cache (`models-store.json`).
|
|
275
275
|
- The account's key, base URL and proxy are in its folder's `zerosub.json` (readable only by you); the registry keeps only a short hash of the key. `auth.json` is made from it and Pi's own: the account's key for Anthropic, plus the API keys Pi's login keeps for other providers. Pi's subscription sign-ins (`/login`, OAuth) aren't copied, because each copy would refresh its token on its own and leave the other with a spent one: models that need one stay on Pi's login. Pi reads a stored key as config (a value starting with `!` is run as a command, and `$NAME` is filled in from the environment), so the account's key is written escaped (`$` as `$$`, a leading `!` as `$!`) and Pi reads it exactly as you typed it. Pi uses a stored key before one in the environment, and the account's agents get `ANTHROPIC_API_KEY`, `ANTHROPIC_AUTH_TOKEN` and `ANTHROPIC_OAUTH_TOKEN` blanked anyway, so nothing from the daemon's environment goes to the account's base URL in its place. So are `ANTHROPIC_CUSTOM_HEADERS`, whose headers the Anthropic SDK adds to every request (an `x-api-key` line there would replace the account's key), and the workload identity federation variables (`ANTHROPIC_FEDERATION_RULE_ID`, `ANTHROPIC_ORGANIZATION_ID`, `ANTHROPIC_IDENTITY_TOKEN_FILE`, `ANTHROPIC_SERVICE_ACCOUNT_ID`, `ANTHROPIC_WORKSPACE_ID`), which Pi would use for a token when no key comes out. `models.json` is Pi's own with Anthropic's base URL set to the account's, and any other key, headers, or a model's own `baseUrl` or `headers` for Anthropic left out, so no model carries the account's key to wherever Pi's own login sends it. Both are remade every 30 seconds and before an agent opens on the account, so don't edit them, or run `/login` with that directory.
|
|
276
276
|
- Agents on an account keep their sessions in the folder Pi's own login uses for that project (`~/.pi/agent/sessions/--<project path>--`, given as `PI_CODING_AGENT_SESSION_DIR` unless the daemon, the Pi provider's `env` or Pi's settings set a sessions folder already), so `pi --resume` finds them, and Paseo still resumes them after the account is removed.
|
|
277
|
-
- An account's proxy goes to its agents as `https_proxy`, `HTTPS_PROXY`, `http_proxy` and `HTTP_PROXY`, which Pi puts before its `httpProxy` setting, with `no_proxy` and `NO_PROXY` set to the list they'd otherwise have minus anything that would send Anthropic's or the base URL's traffic around it, plus `localhost`, `127.0.0.1` and `::1`. An account without one gets
|
|
277
|
+
- An account's proxy goes to its agents as `https_proxy`, `HTTPS_PROXY`, `http_proxy` and `HTTP_PROXY`, which Pi puts before its `httpProxy` setting, with `no_proxy` and `NO_PROXY` set to the list they'd otherwise have minus anything that would send Anthropic's or the base URL's traffic around it, plus `localhost`, `127.0.0.1` and `::1`. An account without one gets the CLI login's proxy the same way, while the login has one and the account's key wouldn't cross it over plain `http://` (its base URL's host stays in the no-proxy list if it's there). Otherwise it gets no proxy variables, and uses the daemon's network or `httpProxy` in Pi's settings, as Pi would. An added account's proxy is also written as `httpProxy` in a `settings.json` of its folder's own, made from Pi's with `httpProxy` on top (a link to Pi's again once the proxy goes), so `pi` started with that folder elsewhere goes through it too, unless its environment names a proxy. A kept key is never sent somewhere new: changing the base URL takes the key again, and so does a new proxy for an account whose base URL is plain `http://` to another machine (the proxy could read the key); remove and add that account again with both. Typing the key yourself is what allows it: the guard keeps a saved key from being sent somewhere new by someone who only sees its last characters, so a key typed together with a plain `http://` base URL and a proxy is used as given (the form warns about plain `http://`). A proxy is also refused while Pi's own `models.json` sends another provider's API key (which every account gets, stored or from the environment) over plain `http://` to another machine. Clearing an account's own proxy is refused the same way when its key goes over plain `http://` and the CLI login's proxy would then carry it. While a following account's folder is being moved, a new proxy for the CLI login is refused until the move is done; try again in a moment.
|
|
278
278
|
- Pi extensions are shared with every account, and one can still change where Anthropic's requests go after `models.json` is read (`pi.registerProvider("anthropic", { baseUrl })`, say): the account's key then goes there too. Only install extensions you trust with your keys.
|
|
279
279
|
- Anthropic refusing an account's key (`authentication_error`) marks it signed out; **Fix key** on its card takes a working one.
|
|
280
280
|
- Each account gets a script, `~/.pi-config/pi-<account>.sh`: put `~/.pi-config` on your `PATH` (the line to add is under the Pi accounts) and run `pi-work.sh`. It sets the account's `PI_CODING_AGENT_DIR`, reads the proxy variables its agents get in Paseo, and the same blank `ANTHROPIC_*` key, custom header and federation variables, from `zerosub.env` in its folder (ZeroSub keeps it up to date), so they win over what your shell exports, and runs `pi`. Scripts follow renames; ZeroSub never overwrites one it didn't write, and several daemons can share the directory.
|
package/client/composer-item.tsx
CHANGED
|
@@ -42,6 +42,9 @@ export interface AccountChoices {
|
|
|
42
42
|
icon: string;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
+
/** Beside an agent's account in its list, when the agent started there because the account's weekly reset was near. */
|
|
46
|
+
export const WEEKLY_RESET_NOTE = "Weekly reset was near";
|
|
47
|
+
|
|
45
48
|
/** The option that names no account: the agent starts, or goes back to, where the default sends it. */
|
|
46
49
|
export const FOLLOW = "follow";
|
|
47
50
|
/** The option that opens the Accounts screen. */
|
|
@@ -86,7 +89,8 @@ export function accountChoices(state: StateView, provider: string | null, chosen
|
|
|
86
89
|
/**
|
|
87
90
|
* What the account control offers for a running agent: the account it's on first (named with the
|
|
88
91
|
* one it's moving from while a move is under way, as on the account button), then the accounts it
|
|
89
|
-
* can move to, going back to the default, and the Accounts screen.
|
|
92
|
+
* can move to, going back to the default, and the Accounts screen. The agent's account also says
|
|
93
|
+
* when the agent started on it because its weekly reset was near. Null when the agent isn't routed
|
|
90
94
|
* or its provider has one account. What needs a confirmation, a banked reset or a fork to the other
|
|
91
95
|
* provider, stays with the account button, which shows for it (see pills.ts).
|
|
92
96
|
*/
|
|
@@ -101,7 +105,13 @@ export function agentChoices(state: StateView, agentId: string): AccountChoices
|
|
|
101
105
|
const { label } = pillText(current, running ?? null, route.waiting ?? (state.canReopen ? "idle" : "restart"));
|
|
102
106
|
// As on the account button: pacing's verdict names the account it picks for the next new agent.
|
|
103
107
|
const next = (account: AccountView) => state.pacedNext?.[account.family] === account.id;
|
|
104
|
-
|
|
108
|
+
// Why the agent is on this account, which the account button's menu says as "Started here: weekly
|
|
109
|
+
// reset was near": with the control there, that button doesn't show for an agent that isn't at a
|
|
110
|
+
// limit. Short, as a list cuts a description to one line. On this account it stands in place of
|
|
111
|
+
// pacing's verdict, which the others keep; without pacing the usage comes after it.
|
|
112
|
+
const options: AccountOption[] = [
|
|
113
|
+
{ value: current.id, label, description: detail(current, next(current), route.selectionReason === "weekly_reset" ? WEEKLY_RESET_NOTE : undefined) },
|
|
114
|
+
];
|
|
105
115
|
// Codex threads can't change accounts in place and continue in the task's tab for the account;
|
|
106
116
|
// Grok conversations stay on theirs, so the others aren't offered.
|
|
107
117
|
const stays = !route.movable && !HAND_OFF[route.family];
|
|
@@ -123,7 +133,7 @@ function takesAgents(state: StateView, account: AccountView): boolean {
|
|
|
123
133
|
}
|
|
124
134
|
|
|
125
135
|
function detail(account: AccountView, next: boolean, lead?: string): string | undefined {
|
|
126
|
-
// A pace and a lead
|
|
136
|
+
// A pace and a lead: the lead (a one-time pick, or why the agent started here) wins over pacing's verdict.
|
|
127
137
|
if (account.pace) return lead ?? paceVerdict(account.pace, next);
|
|
128
138
|
const parts = lead ? [lead] : [];
|
|
129
139
|
if (account.status === "limited") parts.push("Limit reached");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kapybara/zerosub",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.20.0",
|
|
4
4
|
"description": "ZeroSub: use several Claude and ChatGPT (Codex) subscriptions side by side in Paseo. Add accounts in-app, pick one per agent, and switch automatically when an account hits its limit.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"paseo",
|
package/server/codex.ts
CHANGED
|
@@ -71,7 +71,7 @@ class AppServer {
|
|
|
71
71
|
try {
|
|
72
72
|
await server.request(
|
|
73
73
|
"initialize",
|
|
74
|
-
{ clientInfo: { name: CLIENT_NAME, title: "Paseo ZeroSub", version: "1.
|
|
74
|
+
{ clientInfo: { name: CLIENT_NAME, title: "Paseo ZeroSub", version: "1.20.0" }, capabilities: { experimentalApi: false } },
|
|
75
75
|
60_000,
|
|
76
76
|
);
|
|
77
77
|
server.send({ method: "initialized" });
|
package/server/families.ts
CHANGED
|
@@ -159,7 +159,6 @@ export class FamilyResolver {
|
|
|
159
159
|
const { config } = await paseo.config.get();
|
|
160
160
|
const providers = asRecord(config.providers);
|
|
161
161
|
value = familiesFromConfig(providers);
|
|
162
|
-
// Pi's provider `env` (a proxy there, say) is what its agents inherit; see server/pi.ts.
|
|
163
162
|
rememberProviderEnvs(value, providers);
|
|
164
163
|
} catch (error) {
|
|
165
164
|
console.warn("[ZeroSub] could not read provider config; using built-in providers only", describe(error));
|
package/server/homes.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { ApiKeyKind, ApiSummary, Family, TrafficSwitches } from "../shared/
|
|
|
5
5
|
import { AZURE_FILE, isAzureHome, syncAzureConfig } from "./azure";
|
|
6
6
|
import { BEDROCK_FILE, bedrockSettings, isBedrockHome, readBedrockAccount } from "./bedrock";
|
|
7
7
|
import { Mutex, readJson } from "./json-file";
|
|
8
|
-
import { NO_PROXY_KEYS, PROXY_KEYS, REQUIRED_NO_PROXY, bypassesProxy, maskProxy, noProxyFromEnv, proxyFromEnv, withClaudeNoProxy, withoutClaudeDomains } from "./proxy";
|
|
8
|
+
import { NO_PROXY_KEYS, PROXY_KEYS, REQUIRED_NO_PROXY, bypassesProxy, maskProxy, noProxyFromEnv, proxyFromEnv, withClaudeNoProxy, withRequiredNoProxy, withoutClaudeDomains, withoutDomains } from "./proxy";
|
|
9
9
|
import { providerEnv } from "./provider-env";
|
|
10
10
|
import { homesDir, isOwnHome, mainClaudeDir, mainClaudeGlobalConfig, mainCodexHome, mainConfigHome, mainCursorConfigDir, mainGrokHome, mainPiDir } from "./paths";
|
|
11
11
|
|
|
@@ -1530,6 +1530,9 @@ export async function clearTextProviders(login: boolean): Promise<string[]> {
|
|
|
1530
1530
|
const providers = isObject(models) && isObject(models.providers) ? models.providers : {};
|
|
1531
1531
|
const env: Record<string, string | undefined> = { ...process.env, ...providerEnv("pi") };
|
|
1532
1532
|
const inEnv = (name: string) => (name === "anthropic" ? PI_ANTHROPIC_CREDENTIAL_ENV : [PI_KEY_ENV[name] ?? []].flat()).some((key) => env[key]?.trim());
|
|
1533
|
+
// The login's proxy is also `httpProxy` in Pi's own settings, which a `pi` run from a terminal
|
|
1534
|
+
// applies without ZeroSub's no-proxy list, and Pi adds none of its own: so for the login, this
|
|
1535
|
+
// machine's addresses count too unless the no-proxy list keeps them direct.
|
|
1533
1536
|
const clear = (value: unknown) => typeof value === "string" && (login ? crossesInheritedProxy(value, env) : sentInTheClear(value));
|
|
1534
1537
|
// Set to something: an empty key or header list, or `authHeader: false`, sends nothing.
|
|
1535
1538
|
const set = (value: unknown) => (isObject(value) ? Object.keys(value).length > 0 : value !== undefined && value !== null && value !== false && value !== "");
|
|
@@ -1575,13 +1578,34 @@ export function plainHttp(baseUrl: string | null): boolean {
|
|
|
1575
1578
|
}
|
|
1576
1579
|
}
|
|
1577
1580
|
|
|
1581
|
+
/** Where Anthropic's API and sign-in live; a proxy set for Pi must not be gone around for them. */
|
|
1582
|
+
const PI_ANTHROPIC_DOMAINS = ["anthropic.com", "claude.ai", "claude.com"];
|
|
1583
|
+
|
|
1584
|
+
/**
|
|
1585
|
+
* The no-proxy list ZeroSub gives a Pi agent it sends through a proxy: the one in `env`, the agent's
|
|
1586
|
+
* environment otherwise, without anything that would send Anthropic's traffic, or `baseHost`'s, past
|
|
1587
|
+
* the proxy (`*`, or an entry like `.com`; see `withoutDomains`), plus this machine's addresses.
|
|
1588
|
+
*/
|
|
1589
|
+
export function piAgentNoProxy(env: Record<string, string | undefined>, baseHost: string | null = null): string {
|
|
1590
|
+
return withRequiredNoProxy(withoutDomains(noProxyFromEnv(env), baseHost ? [...PI_ANTHROPIC_DOMAINS, baseHost] : PI_ANTHROPIC_DOMAINS));
|
|
1591
|
+
}
|
|
1592
|
+
|
|
1593
|
+
/**
|
|
1594
|
+
* Whether a Pi process goes straight to `target` past a proxy it inherits. Only when both no-proxy
|
|
1595
|
+
* lists keep it direct: the one in `env`, as a `pi` run from a terminal reads it (as undici does:
|
|
1596
|
+
* `no_proxy` wherever it's set, even empty, else `NO_PROXY`), and the one ZeroSub gives its agents
|
|
1597
|
+
* (`piAgentNoProxy`), which drops `*` and entries like `.com`.
|
|
1598
|
+
*/
|
|
1599
|
+
export function piKeepsDirect(target: string, env: Record<string, string | undefined>): boolean {
|
|
1600
|
+
return bypassesProxy(env.no_proxy ?? env.NO_PROXY ?? null, target) && bypassesProxy(piAgentNoProxy(env), target);
|
|
1601
|
+
}
|
|
1602
|
+
|
|
1578
1603
|
/**
|
|
1579
1604
|
* Whether requests to `baseUrl` would cross a proxy Pi takes from `httpProxy` or the environment
|
|
1580
|
-
* (`env`, the one Pi starts with) unencrypted: plain http:// that the no-proxy
|
|
1581
|
-
* direct. Pi adds nothing to
|
|
1582
|
-
*
|
|
1583
|
-
* it's set, even empty, else `NO_PROXY`.
|
|
1605
|
+
* (`env`, the one Pi starts with) unencrypted: plain http:// that the no-proxy lists don't keep
|
|
1606
|
+
* direct (see `piKeepsDirect`). Pi adds nothing to the list a `pi` from a terminal reads, and undici,
|
|
1607
|
+
* which Pi sends its requests with, keeps no host direct without it, this machine included.
|
|
1584
1608
|
*/
|
|
1585
1609
|
export function crossesInheritedProxy(baseUrl: string | null, env: Record<string, string | undefined> = { ...process.env, ...providerEnv("pi") }): boolean {
|
|
1586
|
-
return baseUrl !== null && plainHttp(baseUrl) && !
|
|
1610
|
+
return baseUrl !== null && plainHttp(baseUrl) && !piKeepsDirect(baseUrl, env);
|
|
1587
1611
|
}
|
package/server/pi.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { basename, dirname, join, resolve } from "node:path";
|
|
|
4
4
|
import type { ApiSummary, LoginMethod } from "../shared/model";
|
|
5
5
|
import type { EnvPatch, FamilyAdapter, Identity, LoginHandle, ProxyState, RedeemReply, TurnEvent, UsageRead } from "./adapter";
|
|
6
6
|
import { providerCommand } from "./binaries";
|
|
7
|
-
import { clearTextProviders, forgetSyncs, normalizeBaseUrl, piHomeHoldsAccount, piLiteral, preparePiHome, readPiAccount, readPiOwnJson, readPiAccountApi, readPiAccountForAgents, writePiAccountApi, writePiAccountProxy } from "./homes";
|
|
7
|
+
import { clearTextProviders, crossesInheritedProxy, forgetSyncs, normalizeBaseUrl, piAgentNoProxy, piHomeHoldsAccount, piKeepsDirect, piLiteral, preparePiHome, readPiAccount, readPiOwnJson, readPiAccountApi, readPiAccountForAgents, writePiAccountApi, writePiAccountProxy } from "./homes";
|
|
8
8
|
import { PI_LAUNCH_ENV } from "./launchers";
|
|
9
9
|
import { Mutex, readJson, writeJsonAtomic } from "./json-file";
|
|
10
10
|
import { eachProcessEnvironment, homesInEnvironment, parseTrace } from "./processes";
|
|
@@ -12,13 +12,11 @@ import { detectPiSignOut } from "./limits";
|
|
|
12
12
|
import { dataDir, mainPiDir, shownPath } from "./paths";
|
|
13
13
|
import { run } from "./process";
|
|
14
14
|
import { providerEnv } from "./provider-env";
|
|
15
|
-
import { NO_PROXY_KEYS, PROXY_KEYS, bypassesProxy,
|
|
15
|
+
import { NO_PROXY_KEYS, PROXY_KEYS, bypassesProxy, proxiedFetch, proxyFromEnv } from "./proxy";
|
|
16
16
|
|
|
17
17
|
const ANTHROPIC_API = "https://api.anthropic.com";
|
|
18
18
|
/** Anthropic's edge answers this with the address it sees the request come from (see `PiAdapter.egress`). */
|
|
19
19
|
const TRACE = `${ANTHROPIC_API}/cdn-cgi/trace`;
|
|
20
|
-
/** Where Anthropic's API and sign-in live; a proxy set here must not be gone around for them. */
|
|
21
|
-
const ANTHROPIC_DOMAINS = ["anthropic.com", "claude.ai", "claude.com"];
|
|
22
20
|
|
|
23
21
|
function isObject(value: unknown): value is Record<string, unknown> {
|
|
24
22
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
@@ -46,6 +44,25 @@ async function settingsProxy(): Promise<string | null> {
|
|
|
46
44
|
return isObject(settings) && typeof settings.httpProxy === "string" && settings.httpProxy.trim() ? settings.httpProxy.trim() : null;
|
|
47
45
|
}
|
|
48
46
|
|
|
47
|
+
/**
|
|
48
|
+
* ZeroSub: the CLI login's proxy as its agents may get it as variables at launch, or null. Setting it
|
|
49
|
+
* is refused while a key would cross it over plain http:// (see `writePiMainProxy` and
|
|
50
|
+
* `Service.refuseClearTextFollowers`), but what those checks read can change after it's set: a
|
|
51
|
+
* `models.json` edited, the no-proxy list narrowed to `*`, an account added, or a proxy set before
|
|
52
|
+
* ZeroSub gave it out at all. So the same checks run again here, and while one fails agents get no
|
|
53
|
+
* proxy variables: Pi then applies `httpProxy` itself with the environment's no-proxy list, as it
|
|
54
|
+
* does without ZeroSub. A `models.json` that doesn't parse fails them. `account` is an added
|
|
55
|
+
* account's, for one without a proxy of its own.
|
|
56
|
+
*/
|
|
57
|
+
async function loginProxyForAgents(account?: { key: string | null; baseUrl: string | null }): Promise<string | null> {
|
|
58
|
+
const proxy = await settingsProxy();
|
|
59
|
+
if (!proxy) return null;
|
|
60
|
+
const clear = await clearTextProviders(true).catch(() => null);
|
|
61
|
+
if (clear === null || clear.length > 0) return null;
|
|
62
|
+
if (account?.key && crossesInheritedProxy(account.baseUrl)) return null;
|
|
63
|
+
return proxy;
|
|
64
|
+
}
|
|
65
|
+
|
|
49
66
|
/**
|
|
50
67
|
* The folder Pi keeps the CLI login's sessions for `cwd` in: `sessions/--<cwd with / as ->--` in its
|
|
51
68
|
* agent directory, as Pi names it (`getDefaultSessionDirPath`, 1.4.0 to 1.5.2). Pi names it after its
|
|
@@ -74,7 +91,7 @@ async function sessionDirConfigured(): Promise<boolean> {
|
|
|
74
91
|
function proxyVariables(proxy: string, baseUrl: string | null): Record<string, string> {
|
|
75
92
|
const inherited = { ...process.env, ...providerEnv("pi") };
|
|
76
93
|
const base = hostOf(baseUrl);
|
|
77
|
-
const noProxy =
|
|
94
|
+
const noProxy = piAgentNoProxy(inherited, base);
|
|
78
95
|
const env: Record<string, string> = {};
|
|
79
96
|
for (const key of PROXY_KEYS) env[key] = proxy;
|
|
80
97
|
for (const key of NO_PROXY_KEYS) env[key] = noProxy;
|
|
@@ -87,12 +104,27 @@ function proxyVariables(proxy: string, baseUrl: string | null): Record<string, s
|
|
|
87
104
|
* settings, which the account shares. Null when it goes direct, or the no-proxy list sends `target`
|
|
88
105
|
* past it.
|
|
89
106
|
*/
|
|
90
|
-
export async function inheritedPiProxy(
|
|
107
|
+
export async function inheritedPiProxy(
|
|
108
|
+
target = ANTHROPIC_API,
|
|
109
|
+
account: { key: string | null; baseUrl: string | null } | null = null,
|
|
110
|
+
): Promise<{ proxy: string; from: string } | null> {
|
|
91
111
|
const provider = providerEnv("pi");
|
|
92
112
|
const env: Record<string, string | undefined> = { ...process.env, ...provider };
|
|
113
|
+
// ZeroSub: where its agents are given the CLI login's proxy as variables (see `PiAdapter.env`), it
|
|
114
|
+
// outranks what they inherit, and `target` goes past it only where both lists keep it direct: the
|
|
115
|
+
// environment's, which a `pi` started elsewhere applies to `httpProxy` in the account's settings,
|
|
116
|
+
// and the one the agents get (see `proxyVariables`), which leaves out `*` and entries like `.com`.
|
|
117
|
+
// `account` is an added account's (null for the CLI login, whose own proxy doesn't count here).
|
|
118
|
+
if (account) {
|
|
119
|
+
const given = await loginProxyForAgents(account);
|
|
120
|
+
if (given) return piKeepsDirect(target, env) ? null : { proxy: given, from: `the CLI login (httpProxy in ${shownPath(mainSettingsPath())})` };
|
|
121
|
+
}
|
|
122
|
+
// Otherwise nothing is given, and Pi goes as it would without ZeroSub: the environment's proxy, else
|
|
123
|
+
// `httpProxy` in the settings, past the environment's no-proxy list.
|
|
93
124
|
if (bypassesProxy(env.no_proxy ?? env.NO_PROXY ?? null, target)) return null;
|
|
94
125
|
const key = PROXY_KEYS.find((name) => env[name]?.trim());
|
|
95
126
|
if (key) return { proxy: env[key]!.trim(), from: key in provider ? "the Pi provider's env in Paseo's config" : "the daemon's environment" };
|
|
127
|
+
if (!account) return null;
|
|
96
128
|
const own = await settingsProxy();
|
|
97
129
|
return own ? { proxy: own, from: `httpProxy in ${shownPath(mainSettingsPath())}` } : null;
|
|
98
130
|
}
|
|
@@ -224,11 +256,12 @@ const mainWrites = new Mutex();
|
|
|
224
256
|
/**
|
|
225
257
|
* Sets (or clears, with `null`) Pi's own login's proxy as `httpProxy` in its `settings.json`, which
|
|
226
258
|
* Pi applies wherever it runs on that login while its environment names no proxy, and which added
|
|
227
|
-
* accounts without a proxy of their own share. ZeroSub gives Pi agents
|
|
228
|
-
*
|
|
229
|
-
* over plain http://, wherever Pi takes it from (see `clearTextProviders`): to this
|
|
230
|
-
* unless the no-proxy list keeps it direct, since
|
|
231
|
-
*
|
|
259
|
+
* accounts without a proxy of their own share. ZeroSub gives it to Pi agents as variables too (see
|
|
260
|
+
* `PiAdapter.env`). Like an added account's, a new proxy is refused while a key or sign-in it would
|
|
261
|
+
* carry goes over plain http://, wherever Pi takes it from (see `clearTextProviders`): to this
|
|
262
|
+
* machine too, unless the no-proxy list keeps it direct, since a `pi` run from a terminal applies the
|
|
263
|
+
* setting without ZeroSub's variables and Pi adds nothing to that list. The file is backed up first
|
|
264
|
+
* and written under Pi's own lock.
|
|
232
265
|
*/
|
|
233
266
|
async function writePiMainProxy(proxy: string | null): Promise<void> {
|
|
234
267
|
await mainWrites.run(async () => {
|
|
@@ -384,8 +417,15 @@ export class PiAdapter implements FamilyAdapter {
|
|
|
384
417
|
readonly mainApi = true;
|
|
385
418
|
|
|
386
419
|
async env(home: string | null, session?: { cwd: string }): Promise<EnvPatch> {
|
|
387
|
-
//
|
|
388
|
-
|
|
420
|
+
// ZeroSub: the CLI login's own proxy (`httpProxy` in Pi's settings) is given as variables too, with
|
|
421
|
+
// this machine's addresses kept direct: Pi only applies the setting while its environment names no
|
|
422
|
+
// proxy, and adds no no-proxy list of its own. Only while the clear-text checks still pass (see
|
|
423
|
+
// `loginProxyForAgents`). The directory is cleared, so an agent moved back from an added account
|
|
424
|
+
// doesn't keep running on that account's.
|
|
425
|
+
if (!home) {
|
|
426
|
+
const mainProxy = await loginProxyForAgents();
|
|
427
|
+
return { PI_CODING_AGENT_DIR: null, ...(mainProxy ? proxyVariables(mainProxy, null) : {}) };
|
|
428
|
+
}
|
|
389
429
|
// Throws while the folder is being moved: routing then opens the agent on the CLI login rather
|
|
390
430
|
// than on the account without its proxy (see `readPiAccountForAgents`).
|
|
391
431
|
const account = await readPiAccountForAgents(home);
|
|
@@ -400,7 +440,15 @@ export class PiAdapter implements FamilyAdapter {
|
|
|
400
440
|
if (session && !(await sessionDirConfigured())) patch.PI_CODING_AGENT_SESSION_DIR = await piSessionDir(session.cwd);
|
|
401
441
|
// Given as variables, so nothing the agent inherits outranks the account's own proxy. Without one,
|
|
402
442
|
// it inherits whatever the daemon and Pi's own settings give it, as Pi does without ZeroSub.
|
|
443
|
+
// ZeroSub: without a proxy of its own it goes through the CLI login's, given the same way, while
|
|
444
|
+
// the clear-text checks still pass for it (see `loginProxyForAgents`), but with its base URL's
|
|
445
|
+
// host left in the no-proxy list: the checks take a host the list keeps direct as direct (see
|
|
446
|
+
// `crossesInheritedProxy`), so its agents must go direct to it too.
|
|
403
447
|
if (account.proxy) Object.assign(patch, proxyVariables(account.proxy, account.baseUrl));
|
|
448
|
+
else {
|
|
449
|
+
const mainProxy = await loginProxyForAgents(account);
|
|
450
|
+
if (mainProxy) Object.assign(patch, proxyVariables(mainProxy, null));
|
|
451
|
+
}
|
|
404
452
|
return patch;
|
|
405
453
|
}
|
|
406
454
|
|
|
@@ -444,12 +492,12 @@ export class PiAdapter implements FamilyAdapter {
|
|
|
444
492
|
});
|
|
445
493
|
return found;
|
|
446
494
|
}
|
|
447
|
-
|
|
448
495
|
/** Everything a Pi account has is in its folder, so only the folder moves; its key says which one it is. */
|
|
449
496
|
holdsAccount(home: string): Promise<boolean> {
|
|
450
497
|
return piHomeHoldsAccount(home);
|
|
451
498
|
}
|
|
452
499
|
|
|
500
|
+
|
|
453
501
|
/**
|
|
454
502
|
* The CLI login always counts as signed in: Pi picks a sign-in per model from everything it has.
|
|
455
503
|
* An added account is signed in while it holds a key; a refused key is learned from a failed turn.
|
|
@@ -482,23 +530,26 @@ export class PiAdapter implements FamilyAdapter {
|
|
|
482
530
|
return { own, file: own ? shownPath(mainSettingsPath()) : null, inherited: own ? null : await inheritedPiProxy(ANTHROPIC_API) };
|
|
483
531
|
}
|
|
484
532
|
const account = await readPiAccount(home);
|
|
485
|
-
return { own: account.proxy, file: null, inherited: account.proxy ? null : await inheritedPiProxy(account.baseUrl ?? ANTHROPIC_API) };
|
|
533
|
+
return { own: account.proxy, file: null, inherited: account.proxy ? null : await inheritedPiProxy(account.baseUrl ?? ANTHROPIC_API, account) };
|
|
486
534
|
}
|
|
487
535
|
|
|
488
536
|
async setProxy(home: string | null, proxy: string | null, wanted?: () => Promise<boolean>): Promise<void> {
|
|
489
537
|
if (!home) return writePiMainProxy(proxy);
|
|
490
|
-
|
|
538
|
+
// What it would go through once its own is cleared: read now, as the account has it.
|
|
539
|
+
await writePiAccountProxy(home, proxy, wanted, async (baseUrl) => inheritedPiProxy(baseUrl ?? ANTHROPIC_API, await readPiAccount(home)));
|
|
491
540
|
await this.writeLaunchEnv(home);
|
|
492
541
|
}
|
|
493
542
|
|
|
494
543
|
/**
|
|
495
544
|
* The address Anthropic sees an added account's requests come from, asked of Anthropic's own host
|
|
496
545
|
* the way the account's Pi reaches it. Null for an account with another base URL (whatever is there
|
|
497
|
-
* reaches Anthropic from its own address)
|
|
546
|
+
* reaches Anthropic from its own address); for the CLI login that's an Anthropic base URL in its `models.json`.
|
|
498
547
|
*/
|
|
499
548
|
async egress(home: string | null): Promise<{ ip: string; country: string | null } | null> {
|
|
500
|
-
|
|
501
|
-
|
|
549
|
+
// ZeroSub: the CLI login's is checked too, but only while a proxy is set on it (`httpProxy`), and
|
|
550
|
+
// not while its `models.json` sends Anthropic's requests elsewhere. Without one nothing is sent.
|
|
551
|
+
if (!home && !(await settingsProxy())) return null;
|
|
552
|
+
const baseUrl = home ? (await readPiAccount(home)).baseUrl : await mainAnthropicBaseUrl();
|
|
502
553
|
if (baseUrl && hostOf(baseUrl) !== hostOf(ANTHROPIC_API)) return null;
|
|
503
554
|
const env: Record<string, string | undefined> = { ...process.env, ...providerEnv("pi") };
|
|
504
555
|
for (const [key, value] of Object.entries(await this.env(home))) if (value !== null) env[key] = value;
|
package/server/service.ts
CHANGED
|
@@ -4865,6 +4865,7 @@ export class Service {
|
|
|
4865
4865
|
return { account, proxy, kept };
|
|
4866
4866
|
});
|
|
4867
4867
|
if (account.kind === "main" && account.family === "pi") {
|
|
4868
|
+
void this.checkEgress(account);
|
|
4868
4869
|
await this.followMainSettings(account.family);
|
|
4869
4870
|
} else {
|
|
4870
4871
|
void this.checkEgress(account);
|
|
@@ -4877,8 +4878,9 @@ export class Service {
|
|
|
4877
4878
|
/**
|
|
4878
4879
|
* A new proxy on the CLI login is also every added account's without one of its own, so it's
|
|
4879
4880
|
* refused while one of those sends its key over plain http:// through it: to this machine too,
|
|
4880
|
-
* unless the no-proxy list keeps it direct
|
|
4881
|
-
* proxy
|
|
4881
|
+
* unless the no-proxy list keeps it direct. ZeroSub gives Paseo's agents this machine's addresses
|
|
4882
|
+
* as direct, but the proxy also reaches the account as `httpProxy` in its settings, which Pi
|
|
4883
|
+
* applies without that list wherever the variables are missing (see `crossesInheritedProxy`).
|
|
4882
4884
|
*/
|
|
4883
4885
|
private async refuseClearTextFollowers(family: Family): Promise<void> {
|
|
4884
4886
|
const adapter = this.adapters[family];
|
|
@@ -4947,13 +4949,15 @@ export class Service {
|
|
|
4947
4949
|
|
|
4948
4950
|
/**
|
|
4949
4951
|
* Checks where an account's requests come from; a failed check keeps the last address it saw.
|
|
4950
|
-
*
|
|
4951
|
-
*
|
|
4952
|
+
* The CLI logins are checked too, as their proxies can be set here; Pi's only while a proxy is set
|
|
4953
|
+
* on it, and not while its `models.json` sends Anthropic's requests somewhere else (see
|
|
4954
|
+
* `PiAdapter.egress`).
|
|
4952
4955
|
*/
|
|
4953
4956
|
private async checkEgress(account: StoredAccount): Promise<void> {
|
|
4954
4957
|
const adapter = this.adapters[account.family];
|
|
4955
4958
|
if (!adapter.egress || !account.signedIn) return;
|
|
4956
|
-
|
|
4959
|
+
// ZeroSub checks Pi's CLI login too: its proxy reaches its agents (see `PiAdapter.env`).
|
|
4960
|
+
if (account.kind === "main" ? account.family !== "claude" && account.family !== "pi" : !account.home) return;
|
|
4957
4961
|
this.egressCheckedAt.set(account.id, Date.now());
|
|
4958
4962
|
const checkedAt = new Date().toISOString();
|
|
4959
4963
|
try {
|