pi-crew 0.11.33 → 0.11.35
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/CHANGELOG.md +12 -0
- package/dist/index.mjs +488 -488
- package/package.json +1 -1
- package/src/config/env-allowlist-registry.ts +2 -0
- package/src/config/env-vars.ts +9 -1
- package/src/runtime/child-pi/inference-broker.ts +161 -42
package/package.json
CHANGED
|
@@ -214,6 +214,8 @@ const CREW_ENV_FORWARDING_DECL: Record<string, CrewEnvForwardDecision> = {
|
|
|
214
214
|
kind: "test-only",
|
|
215
215
|
reason: "v0.11.31 broker upstream override — TESTS ONLY (local mock), production never sets it",
|
|
216
216
|
},
|
|
217
|
+
PI_CREW_INFERENCE_UPSTREAM_ANTHROPIC: { kind: "test-only", reason: "v0.11.34 anthropic-only broker upstream override — TESTS ONLY" },
|
|
218
|
+
PI_CREW_INFERENCE_UPSTREAM_OPENAI: { kind: "test-only", reason: "v0.11.34 openai-only broker upstream override — TESTS ONLY" },
|
|
217
219
|
PI_CREW_STEERING_FILE: { kind: "child-write", reason: "child-pi-spawn per-run steering JSONL" },
|
|
218
220
|
PI_CREW_MAX_OUTPUT: { kind: "child-write", reason: "pi-args emits agent.maxTokens into built.env" },
|
|
219
221
|
PI_CREW_RPC_SECRET: { kind: "child-write", reason: "shared HMAC secret handed to the child at spawn — secret name, never allowlisted" },
|
package/src/config/env-vars.ts
CHANGED
|
@@ -510,7 +510,15 @@ export const CREW_ENV_VARS: Record<string, CrewEnvVarSpec> = {
|
|
|
510
510
|
},
|
|
511
511
|
PI_CREW_INFERENCE_UPSTREAM: {
|
|
512
512
|
name: "PI_CREW_INFERENCE_UPSTREAM",
|
|
513
|
-
doc: "v0.11.31 inference broker upstream override (
|
|
513
|
+
doc: "v0.11.31 inference broker upstream override applied to EVERY provider (defaults: anthropic https://api.anthropic.com, openai https://api.openai.com/v1). TESTS ONLY — points the broker at a local mock upstream; production callers must never set it",
|
|
514
|
+
},
|
|
515
|
+
PI_CREW_INFERENCE_UPSTREAM_ANTHROPIC: {
|
|
516
|
+
name: "PI_CREW_INFERENCE_UPSTREAM_ANTHROPIC",
|
|
517
|
+
doc: "v0.11.34 anthropic-only broker upstream override (beats PI_CREW_INFERENCE_UPSTREAM). TESTS ONLY — local mock; production must never set it",
|
|
518
|
+
},
|
|
519
|
+
PI_CREW_INFERENCE_UPSTREAM_OPENAI: {
|
|
520
|
+
name: "PI_CREW_INFERENCE_UPSTREAM_OPENAI",
|
|
521
|
+
doc: "v0.11.34 openai-only broker upstream override (beats PI_CREW_INFERENCE_UPSTREAM). TESTS ONLY — local mock; production must never set it",
|
|
514
522
|
},
|
|
515
523
|
PI_CREW_CLASSIFIER_ENABLED: {
|
|
516
524
|
name: "PI_CREW_CLASSIFIER_ENABLED",
|
|
@@ -50,6 +50,7 @@
|
|
|
50
50
|
import * as crypto from "node:crypto";
|
|
51
51
|
import * as fs from "node:fs";
|
|
52
52
|
import * as http from "node:http";
|
|
53
|
+
import * as os from "node:os";
|
|
53
54
|
import * as path from "node:path";
|
|
54
55
|
import { Readable, Transform } from "node:stream";
|
|
55
56
|
import { getCrewEnv, getCrewEnvBool, getCrewEnvInt } from "../../config/env-vars.ts";
|
|
@@ -67,7 +68,6 @@ export const INFERENCE_BUDGET_ENV = "PI_CREW_INFERENCE_BUDGET_TOKENS";
|
|
|
67
68
|
/** pi-native agent-dir override (SDK config.js ENV_AGENT_DIR) — set on the child. */
|
|
68
69
|
export const PI_AGENT_DIR_ENV = "PI_CODING_AGENT_DIR";
|
|
69
70
|
|
|
70
|
-
const DEFAULT_UPSTREAM = "https://api.anthropic.com";
|
|
71
71
|
const MAX_BODY_BYTES = 64 * 1024 * 1024; // generous: image attachments
|
|
72
72
|
const UPSTREAM_TIMEOUT_MS = 10 * 60 * 1000;
|
|
73
73
|
const MAX_INFLIGHT_PER_WORKER = 4;
|
|
@@ -102,14 +102,21 @@ export interface InferenceBrokerSnapshot {
|
|
|
102
102
|
baseUrl: string;
|
|
103
103
|
port: number;
|
|
104
104
|
scopedAgentDir: string | undefined;
|
|
105
|
+
/** Provider names with a resolvable upstream credential (v0.11.34). */
|
|
106
|
+
providers: string[];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** One provider's upstream route: base URL + substituted auth header. */
|
|
110
|
+
interface ProviderRoute {
|
|
111
|
+
name: "anthropic" | "openai";
|
|
112
|
+
upstream: string;
|
|
113
|
+
auth: { header: "x-api-key" | "authorization"; value: string };
|
|
105
114
|
}
|
|
106
115
|
|
|
107
116
|
interface BrokerState {
|
|
108
117
|
server: http.Server;
|
|
109
118
|
port: number;
|
|
110
|
-
|
|
111
|
-
/** x-api-key value OR Authorization bearer for the upstream. */
|
|
112
|
-
upstreamAuth: { header: "x-api-key" | "authorization"; value: string };
|
|
119
|
+
routes: Map<string, ProviderRoute>; // by path prefix ("/anthropic", "/openai")
|
|
113
120
|
workers: Map<string, WorkerEntry>; // by token
|
|
114
121
|
byKey: Map<string, WorkerEntry>; // by workerKey
|
|
115
122
|
scopedAgentDir: string;
|
|
@@ -120,12 +127,66 @@ interface BrokerState {
|
|
|
120
127
|
// stopped by stopInferenceBroker() (extension teardown / tests).
|
|
121
128
|
let broker: BrokerState | undefined;
|
|
122
129
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
130
|
+
/**
|
|
131
|
+
* Read the PARENT's auth.json credential store — the documented file shape is
|
|
132
|
+
* `{ "<provider>": { "type": "api_key", "key": "..." } }` (verified against
|
|
133
|
+
* the SDK store + a live file). Read ONCE at broker start; the key stays in
|
|
134
|
+
* memory exactly like an env-sourced key. Parse failures are logged WITHOUT
|
|
135
|
+
* the key and ignored (env credentials still work).
|
|
136
|
+
*/
|
|
137
|
+
function readAuthJsonCredentials(env: NodeJS.ProcessEnv): Record<string, { type?: string; key?: string }> {
|
|
138
|
+
const agentDir = env.PI_CODING_AGENT_DIR || path.join(os.homedir(), ".pi", "agent");
|
|
139
|
+
const authPath = path.join(agentDir, "auth.json");
|
|
140
|
+
try {
|
|
141
|
+
const raw = fs.readFileSync(authPath, "utf8");
|
|
142
|
+
const parsed: unknown = JSON.parse(raw);
|
|
143
|
+
if (!parsed || typeof parsed !== "object") return {};
|
|
144
|
+
return parsed as Record<string, { type?: string; key?: string }>;
|
|
145
|
+
} catch {
|
|
146
|
+
return {}; // absent/unreadable/malformed — env credentials remain the path
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Resolve every provider route (v0.11.34 multi-provider). Precedence per
|
|
152
|
+
* provider: env credential FIRST (CI behavior identical to v0.11.33), then
|
|
153
|
+
* the parent's auth.json file credential. Returns the routes that resolved
|
|
154
|
+
* and a human-readable trace of what was consulted (never contains keys).
|
|
155
|
+
*/
|
|
156
|
+
function resolveProviderRoutes(env: NodeJS.ProcessEnv): { routes: ProviderRoute[]; trace: string } {
|
|
157
|
+
const authJson = readAuthJsonCredentials(env);
|
|
158
|
+
const genericUpstream = getCrewEnv("PI_CREW_INFERENCE_UPSTREAM", env); // tests: applies to every provider
|
|
159
|
+
const routes: ProviderRoute[] = [];
|
|
160
|
+
const consulted: string[] = [];
|
|
161
|
+
|
|
162
|
+
const anthropicUpstream = getCrewEnv("PI_CREW_INFERENCE_UPSTREAM_ANTHROPIC", env) || genericUpstream || "https://api.anthropic.com";
|
|
163
|
+
const anthropicApiKey = env.ANTHROPIC_API_KEY;
|
|
164
|
+
const anthropicAuthToken = env.ANTHROPIC_AUTH_TOKEN;
|
|
165
|
+
const anthropicStored = authJson.anthropic;
|
|
166
|
+
consulted.push("env ANTHROPIC_API_KEY", "env ANTHROPIC_AUTH_TOKEN", "auth.json:anthropic");
|
|
167
|
+
if (anthropicApiKey && anthropicApiKey.length > 0) {
|
|
168
|
+
routes.push({ name: "anthropic", upstream: anthropicUpstream, auth: { header: "x-api-key", value: anthropicApiKey } });
|
|
169
|
+
} else if (anthropicAuthToken && anthropicAuthToken.length > 0) {
|
|
170
|
+
routes.push({
|
|
171
|
+
name: "anthropic",
|
|
172
|
+
upstream: anthropicUpstream,
|
|
173
|
+
auth: { header: "authorization", value: `Bearer ${anthropicAuthToken}` },
|
|
174
|
+
});
|
|
175
|
+
} else if (anthropicStored?.type === "api_key" && typeof anthropicStored.key === "string" && anthropicStored.key.length > 0) {
|
|
176
|
+
routes.push({ name: "anthropic", upstream: anthropicUpstream, auth: { header: "x-api-key", value: anthropicStored.key } });
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const openaiUpstream = getCrewEnv("PI_CREW_INFERENCE_UPSTREAM_OPENAI", env) || genericUpstream || "https://api.openai.com/v1";
|
|
180
|
+
const openaiApiKey = env.OPENAI_API_KEY;
|
|
181
|
+
const openaiStored = authJson.openai;
|
|
182
|
+
consulted.push("env OPENAI_API_KEY", "auth.json:openai");
|
|
183
|
+
if (openaiApiKey && openaiApiKey.length > 0) {
|
|
184
|
+
routes.push({ name: "openai", upstream: openaiUpstream, auth: { header: "authorization", value: `Bearer ${openaiApiKey}` } });
|
|
185
|
+
} else if (openaiStored?.type === "api_key" && typeof openaiStored.key === "string" && openaiStored.key.length > 0) {
|
|
186
|
+
routes.push({ name: "openai", upstream: openaiUpstream, auth: { header: "authorization", value: `Bearer ${openaiStored.key}` } });
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return { routes, trace: consulted.join(", ") };
|
|
129
190
|
}
|
|
130
191
|
|
|
131
192
|
/**
|
|
@@ -138,18 +199,30 @@ function resolveUpstreamAuth(): { header: "x-api-key" | "authorization"; value:
|
|
|
138
199
|
*/
|
|
139
200
|
export async function maybeStartInferenceBroker(env: NodeJS.ProcessEnv = process.env): Promise<InferenceBrokerSnapshot> {
|
|
140
201
|
if (broker)
|
|
141
|
-
return {
|
|
142
|
-
|
|
202
|
+
return {
|
|
203
|
+
active: true,
|
|
204
|
+
baseUrl: `http://127.0.0.1:${broker.port}`,
|
|
205
|
+
port: broker.port,
|
|
206
|
+
scopedAgentDir: broker.scopedAgentDir,
|
|
207
|
+
providers: [...broker.routes.values()].map((r) => r.name),
|
|
208
|
+
};
|
|
209
|
+
if (!getCrewEnvBool(INFERENCE_BROKER_ENABLE_ENV))
|
|
210
|
+
return { active: false, baseUrl: "", port: 0, scopedAgentDir: undefined, providers: [] };
|
|
143
211
|
|
|
144
|
-
const
|
|
145
|
-
if (
|
|
212
|
+
const { routes, trace } = resolveProviderRoutes(env);
|
|
213
|
+
if (routes.length === 0) {
|
|
146
214
|
throw new Error(
|
|
147
|
-
`${INFERENCE_BROKER_ENABLE_ENV}=1 but
|
|
215
|
+
`${INFERENCE_BROKER_ENABLE_ENV}=1 but no provider credential resolved for the inference broker (consulted: ${trace}) — fail-closed: workers would otherwise receive no usable credential, or worse, fall back to real provider keys. Provide ANTHROPIC_API_KEY / ANTHROPIC_AUTH_TOKEN / OPENAI_API_KEY in the parent env, an api_key entry for anthropic/openai in the parent's auth.json, or unset ${INFERENCE_BROKER_ENABLE_ENV}.`,
|
|
148
216
|
);
|
|
149
217
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
218
|
+
broker = await createBrokerServer(routes);
|
|
219
|
+
return {
|
|
220
|
+
active: true,
|
|
221
|
+
baseUrl: `http://127.0.0.1:${broker.port}`,
|
|
222
|
+
port: broker.port,
|
|
223
|
+
scopedAgentDir: broker.scopedAgentDir,
|
|
224
|
+
providers: [...broker.routes.values()].map((r) => r.name),
|
|
225
|
+
};
|
|
153
226
|
}
|
|
154
227
|
|
|
155
228
|
/**
|
|
@@ -160,8 +233,15 @@ export async function maybeStartInferenceBroker(env: NodeJS.ProcessEnv = process
|
|
|
160
233
|
*/
|
|
161
234
|
export function requireInferenceBrokerIfEnabled(): InferenceBrokerSnapshot {
|
|
162
235
|
if (broker)
|
|
163
|
-
return {
|
|
164
|
-
|
|
236
|
+
return {
|
|
237
|
+
active: true,
|
|
238
|
+
baseUrl: `http://127.0.0.1:${broker.port}`,
|
|
239
|
+
port: broker.port,
|
|
240
|
+
scopedAgentDir: broker.scopedAgentDir,
|
|
241
|
+
providers: [...broker.routes.values()].map((r) => r.name),
|
|
242
|
+
};
|
|
243
|
+
if (!getCrewEnvBool(INFERENCE_BROKER_ENABLE_ENV))
|
|
244
|
+
return { active: false, baseUrl: "", port: 0, scopedAgentDir: undefined, providers: [] };
|
|
165
245
|
throw new Error(
|
|
166
246
|
`${INFERENCE_BROKER_ENABLE_ENV}=1 but the broker is not started — run() must await maybeStartInferenceBroker() before prepareSpawnContext()`,
|
|
167
247
|
);
|
|
@@ -169,19 +249,23 @@ export function requireInferenceBrokerIfEnabled(): InferenceBrokerSnapshot {
|
|
|
169
249
|
|
|
170
250
|
/** Read-only view for spawn-option builders. Never starts anything. */
|
|
171
251
|
export function getInferenceBrokerSnapshot(): InferenceBrokerSnapshot {
|
|
172
|
-
if (!broker) return { active: false, baseUrl: "", port: 0, scopedAgentDir: undefined };
|
|
173
|
-
return {
|
|
252
|
+
if (!broker) return { active: false, baseUrl: "", port: 0, scopedAgentDir: undefined, providers: [] };
|
|
253
|
+
return {
|
|
254
|
+
active: true,
|
|
255
|
+
baseUrl: `http://127.0.0.1:${broker.port}`,
|
|
256
|
+
port: broker.port,
|
|
257
|
+
scopedAgentDir: broker.scopedAgentDir,
|
|
258
|
+
providers: [...broker.routes.values()].map((r) => r.name),
|
|
259
|
+
};
|
|
174
260
|
}
|
|
175
261
|
|
|
176
|
-
async function createBrokerServer(
|
|
177
|
-
upstream: string,
|
|
178
|
-
upstreamAuth: { header: "x-api-key" | "authorization"; value: string },
|
|
179
|
-
): Promise<BrokerState> {
|
|
262
|
+
async function createBrokerServer(routes: ProviderRoute[]): Promise<BrokerState> {
|
|
180
263
|
const workers = new Map<string, WorkerEntry>();
|
|
181
264
|
const byKey = new Map<string, WorkerEntry>();
|
|
265
|
+
const routeMap = new Map<string, ProviderRoute>(routes.map((r) => [`/${r.name}`, r]));
|
|
182
266
|
|
|
183
267
|
const server = http.createServer((req, res) => {
|
|
184
|
-
void handleRequest(req, res, {
|
|
268
|
+
void handleRequest(req, res, { routes: routeMap, workers });
|
|
185
269
|
});
|
|
186
270
|
server.unref(); // never hold the parent's event loop open
|
|
187
271
|
await new Promise<void>((resolve, reject) => {
|
|
@@ -195,9 +279,9 @@ async function createBrokerServer(
|
|
|
195
279
|
if (!address || typeof address === "string") throw new Error("inference broker failed to bind a loopback port");
|
|
196
280
|
const port = address.port;
|
|
197
281
|
|
|
198
|
-
const scopedAgentDir = buildScopedAgentDir(port);
|
|
282
|
+
const scopedAgentDir = buildScopedAgentDir(port, routes);
|
|
199
283
|
|
|
200
|
-
return { server, port,
|
|
284
|
+
return { server, port, routes: routeMap, workers, byKey, scopedAgentDir };
|
|
201
285
|
}
|
|
202
286
|
|
|
203
287
|
/**
|
|
@@ -206,17 +290,22 @@ async function createBrokerServer(
|
|
|
206
290
|
* built-in anthropic provider to the broker with per-worker token
|
|
207
291
|
* interpolation at request time. Mode 0o600 file / 0o700 dir.
|
|
208
292
|
*/
|
|
209
|
-
function buildScopedAgentDir(port: number): string {
|
|
293
|
+
function buildScopedAgentDir(port: number, routes: ProviderRoute[]): string {
|
|
210
294
|
const dir = createSafeTempDir(getPiTempBase(), "pi-crew-inference-");
|
|
211
295
|
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
296
|
+
// Every resolvable provider gets a PATH-PREFIXED baseUrl ("/anthropic",
|
|
297
|
+
// "/openai") so the broker routes deterministically by prefix and forwards
|
|
298
|
+
// the remaining path verbatim to that provider's upstream.
|
|
299
|
+
const providers: Record<string, { baseUrl: string; apiKey: string }> = {};
|
|
300
|
+
for (const route of routes) {
|
|
301
|
+
providers[route.name] = {
|
|
302
|
+
baseUrl: `http://127.0.0.1:${port}/${route.name}`,
|
|
303
|
+
apiKey: `$${INFERENCE_TOKEN_ENV}`,
|
|
304
|
+
};
|
|
305
|
+
}
|
|
212
306
|
const models = {
|
|
213
307
|
$schema: "https://pi.dev/schemas/models.schema.json",
|
|
214
|
-
providers
|
|
215
|
-
anthropic: {
|
|
216
|
-
baseUrl: `http://127.0.0.1:${port}`,
|
|
217
|
-
apiKey: `$${INFERENCE_TOKEN_ENV}`,
|
|
218
|
-
},
|
|
219
|
-
},
|
|
308
|
+
providers,
|
|
220
309
|
};
|
|
221
310
|
const file = path.join(dir, "models.json");
|
|
222
311
|
fs.writeFileSync(file, `${JSON.stringify(models, null, "\t")}\n`, { mode: 0o600 });
|
|
@@ -241,9 +330,26 @@ function deny(res: http.ServerResponse, status: number, message: string): void {
|
|
|
241
330
|
async function handleRequest(
|
|
242
331
|
req: http.IncomingMessage,
|
|
243
332
|
res: http.ServerResponse,
|
|
244
|
-
ctx: {
|
|
333
|
+
ctx: { routes: Map<string, ProviderRoute>; workers: Map<string, WorkerEntry> },
|
|
245
334
|
): Promise<void> {
|
|
246
335
|
try {
|
|
336
|
+
// (0) Provider routing: the FIRST path segment names the provider
|
|
337
|
+
// ("/anthropic/v1/messages", "/openai/chat/completions"); the broker
|
|
338
|
+
// forwards the REMAINING path verbatim to that provider's upstream.
|
|
339
|
+
const url = req.url ?? "/";
|
|
340
|
+
let route: ProviderRoute | undefined;
|
|
341
|
+
let upstreamPath = url;
|
|
342
|
+
for (const [prefix, candidate] of ctx.routes) {
|
|
343
|
+
if (url === prefix || url.startsWith(`${prefix}/`)) {
|
|
344
|
+
route = candidate;
|
|
345
|
+
upstreamPath = url.slice(prefix.length) || "/";
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
if (!route) {
|
|
350
|
+
deny(res, 404, "unknown provider route (expected /anthropic/... or /openai/...)");
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
247
353
|
// (1) Auth — fail-closed, generic errors (never echo tokens).
|
|
248
354
|
const token = extractToken(req);
|
|
249
355
|
const worker = token ? ctx.workers.get(token) : undefined;
|
|
@@ -278,8 +384,8 @@ async function handleRequest(
|
|
|
278
384
|
if (!FORWARD_REQUEST_HEADERS.has(name.toLowerCase())) continue;
|
|
279
385
|
if (typeof value === "string") headers[name.toLowerCase()] = value;
|
|
280
386
|
}
|
|
281
|
-
headers[
|
|
282
|
-
const upstreamRes = await fetch(`${
|
|
387
|
+
headers[route.auth.header] = route.auth.value;
|
|
388
|
+
const upstreamRes = await fetch(`${route.upstream}${upstreamPath}`, {
|
|
283
389
|
// eslint-disable-next-line
|
|
284
390
|
method: req.method ?? "POST",
|
|
285
391
|
headers,
|
|
@@ -353,9 +459,22 @@ class UsageCounter {
|
|
|
353
459
|
// Non-stream JSON body: top-level evt.usage, both absolute.
|
|
354
460
|
const usage = evt?.message?.usage ?? evt?.usage;
|
|
355
461
|
if (!usage || typeof usage !== "object") continue;
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
462
|
+
// Anthropic: input_tokens / output_tokens. OpenAI chat-completions:
|
|
463
|
+
// prompt_tokens / completion_tokens (final usage chunk, absolute).
|
|
464
|
+
const inTok =
|
|
465
|
+
typeof usage.input_tokens === "number"
|
|
466
|
+
? usage.input_tokens
|
|
467
|
+
: typeof usage.prompt_tokens === "number"
|
|
468
|
+
? usage.prompt_tokens
|
|
469
|
+
: undefined;
|
|
470
|
+
const outTok =
|
|
471
|
+
typeof usage.output_tokens === "number"
|
|
472
|
+
? usage.output_tokens
|
|
473
|
+
: typeof usage.completion_tokens === "number"
|
|
474
|
+
? usage.completion_tokens
|
|
475
|
+
: undefined;
|
|
476
|
+
if (typeof inTok === "number" && evt.type !== "message_delta") this.input = Math.max(this.input, inTok);
|
|
477
|
+
if (typeof outTok === "number") this.output = Math.max(this.output, outTok);
|
|
359
478
|
} catch {
|
|
360
479
|
// non-JSON line — ignore
|
|
361
480
|
}
|