pi-crew 0.11.33 → 0.11.37

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-crew",
3
- "version": "0.11.33",
3
+ "version": "0.11.37",
4
4
  "description": "Pi extension for coordinated AI teams, workflows, worktrees, and async task orchestration",
5
5
  "author": "baphuongna",
6
6
  "license": "MIT",
@@ -214,6 +214,17 @@ 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" },
219
+ PI_CREW_INFERENCE_UPSTREAM_GOOGLE: { kind: "test-only", reason: "v0.11.36 google-only broker upstream override — TESTS ONLY" },
220
+ PI_CREW_SANDBOX: {
221
+ kind: "gap",
222
+ reason: "v0.11.36 OS-boundary knob read in the PARENT spawn path only (bwrap wrap decision) — deliberately never forwarded to children",
223
+ },
224
+ PI_CREW_SANDBOX_NET: {
225
+ kind: "gap",
226
+ reason: "v0.11.36 sandbox network profile (share|off) read in the PARENT spawn path only — deliberately never forwarded to children",
227
+ },
217
228
  PI_CREW_STEERING_FILE: { kind: "child-write", reason: "child-pi-spawn per-run steering JSONL" },
218
229
  PI_CREW_MAX_OUTPUT: { kind: "child-write", reason: "pi-args emits agent.maxTokens into built.env" },
219
230
  PI_CREW_RPC_SECRET: { kind: "child-write", reason: "shared HMAC secret handed to the child at spawn — secret name, never allowlisted" },
@@ -510,7 +510,27 @@ 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 (default https://api.anthropic.com). TESTS ONLY — points the broker at a local mock upstream; production callers must never set it",
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",
522
+ },
523
+ PI_CREW_INFERENCE_UPSTREAM_GOOGLE: {
524
+ name: "PI_CREW_INFERENCE_UPSTREAM_GOOGLE",
525
+ doc: "v0.11.36 google-only broker upstream override (beats PI_CREW_INFERENCE_UPSTREAM; default https://generativelanguage.googleapis.com, native google-generative-ai wire, x-goog-api-key auth). TESTS ONLY — local mock; production must never set it",
526
+ },
527
+ PI_CREW_SANDBOX: {
528
+ name: "PI_CREW_SANDBOX",
529
+ doc: "v0.11.36 OS-boundary sandbox, Linux opt-in: 'bwrap' wraps every headless child-pi worker in bubblewrap — the worker sees ONLY the project cwd (rw), the scoped inference agent dir, explicitly handed-in temp/scratch paths, and read-only OS runtime; ~/.pi (auth.json!), ~/.ssh and every other host path are NOT MOUNTED (fail-closed: forgotten paths yield ENOENT inside, never host access). Fail-closed knob: unsupported value / non-Linux / missing bwrap binary / net=off+broker all THROW before any spawn (never silently unsandboxed). Pass-1 boundary: TUI surface panes and the rpc transport are SKIPPED while on (fail-closed branch selection); PI_CREW_SANDBOX_NET=share keeps host networking — egress is NOT isolated unless net=off. Sandbox+broker-off = workers have no credentials at all (by isolation) — pair with PI_CREW_INFERENCE_BROKER=1 for sandboxed inference (sandbox.ts)",
530
+ },
531
+ PI_CREW_SANDBOX_NET: {
532
+ name: "PI_CREW_SANDBOX_NET",
533
+ doc: "v0.11.36 sandbox network profile: 'share' (default — host networking, loopback inference broker reachable, egress NOT isolated) or 'off' (unshare the net stack entirely: no egress, no loopback — REJECTED while the inference broker is active because broker-token workers would be keyless AND netless) (sandbox.ts)",
514
534
  },
515
535
  PI_CREW_CLASSIFIER_ENABLED: {
516
536
  name: "PI_CREW_CLASSIFIER_ENABLED",
@@ -31,6 +31,7 @@ import type { ChildPiRunInput, ChildPiRunResult } from "./child-pi.ts";
31
31
  import {
32
32
  getInferenceBrokerSnapshot,
33
33
  INFERENCE_TOKEN_ENV,
34
+ inferenceWorkerKeyForAgent,
34
35
  PI_AGENT_DIR_ENV,
35
36
  registerInferenceWorker,
36
37
  requireInferenceBrokerIfEnabled,
@@ -344,7 +345,12 @@ export function prepareSpawnContext(
344
345
  const inferenceSnapshot = requireInferenceBrokerIfEnabled();
345
346
  let inferenceWorkerKey: string | undefined;
346
347
  if (inferenceSnapshot.active) {
347
- inferenceWorkerKey = input.agentId ? `inference:${input.agentId}` : `inference:spawn:${process.pid}:${Date.now().toString(36)}`;
348
+ // v0.11.36: the agentId derivation is SHARED with the exit-revoke wrapper
349
+ // (runChildPi finally) via inferenceWorkerKeyForAgent — both sides must
350
+ // compute the SAME key or revoke-on-exit silently misses.
351
+ inferenceWorkerKey = input.agentId
352
+ ? inferenceWorkerKeyForAgent(input.agentId)
353
+ : `inference:spawn:${process.pid}:${Date.now().toString(36)}`;
348
354
  built.env[INFERENCE_TOKEN_ENV] = registerInferenceWorker(inferenceWorkerKey);
349
355
  }
350
356
  // Pass steering file path to child for real-time steer injection
@@ -36,8 +36,9 @@ import { ChildPiSteeringController } from "./child-pi-steering.ts";
36
36
  import { ChildPiLineObserver, isFinalAssistantEvent } from "./child-pi-streams.ts";
37
37
  // Phase 2.3: the six timer constructs moved to child-pi-timers.ts (pure motion).
38
38
  import { createChildPiTimers } from "./child-pi-timers.ts";
39
- import { maybeStartInferenceBroker } from "./inference-broker.ts";
39
+ import { getInferenceBrokerSnapshot, maybeStartInferenceBroker, revokeInferenceWorkerForAgent } from "./inference-broker.ts";
40
40
  import { runMockChildPi } from "./mock-fixtures.ts";
41
+ import { applySandboxToSpawnSpec, assertSandboxAvailable, resolveSandboxConfig, type SandboxConfig } from "./sandbox.ts";
41
42
  // W2 (session-file recovery): deterministic worker session flags + crash-path
42
43
  // tail-replay of the session JSONL (see session-recovery.ts header).
43
44
  import {
@@ -703,6 +704,21 @@ async function trySurfaceBranch(
703
704
  export const __test__trySurfaceBranch = trySurfaceBranch;
704
705
 
705
706
  export async function runChildPi(input: ChildPiRunInput): Promise<ChildPiRunResult> {
707
+ // v0.11.36 exit-revoke WRAPPER: whatever the inner run does — normal exit,
708
+ // crash, B5 pre-spawn abort, surface/rpc branch, even a thrown error — the
709
+ // task's inference token dies with the run. prepareSpawnContext registers
710
+ // `inference:<agentId>` BEFORE its B5 abort check, so an aborted run that
711
+ // never spawned still leaves a token behind unless revoked HERE. Retries
712
+ // are safe: they call runChildPi again and re-register a FRESH token
713
+ // (register-replace semantics) after this revoke already ran.
714
+ try {
715
+ return await runChildPiInner(input);
716
+ } finally {
717
+ revokeInferenceWorkerForAgent(input.agentId);
718
+ }
719
+ }
720
+
721
+ async function runChildPiInner(input: ChildPiRunInput): Promise<ChildPiRunResult> {
706
722
  // Phase 1 (live-session parity): prepend parent context when inheritContext is true.
707
723
  // This mirrors the effectivePrompt logic in live-session-runtime.ts so that
708
724
  // child-process workers receive the same inherited-context treatment.
@@ -781,6 +797,14 @@ export async function runChildPi(input: ChildPiRunInput): Promise<ChildPiRunResu
781
797
  // v0.11.31: start the inference broker (flag-gated, fail-closed) BEFORE the
782
798
  // sync spawn-prep path — http listen needs an event-loop turn.
783
799
  await maybeStartInferenceBroker();
800
+ // v0.11.36 OS boundary (bwrap, Linux opt-in): resolve + VALIDATE + probe
801
+ // BEFORE any spawn. Every misconfiguration throws here (fail-closed — a
802
+ // sandbox that cannot start must never degrade to an unsandboxed spawn).
803
+ const sandbox: SandboxConfig = resolveSandboxConfig(process.env, {
804
+ platform: process.platform,
805
+ brokerActive: getInferenceBrokerSnapshot().active,
806
+ });
807
+ assertSandboxAvailable(sandbox);
784
808
  const spawnPrep = prepareSpawnContext(brokerSpawn ? { ...versionedInput, brokerSpawn } : versionedInput, effectiveTask, depthEnv);
785
809
  if (spawnPrep.kind === "aborted") return spawnPrep.result;
786
810
  const { spawnSpec, mergedEnv, tempDir, builtEnv, builtArgs } = spawnPrep.ctx;
@@ -805,7 +829,10 @@ export async function runChildPi(input: ChildPiRunInput): Promise<ChildPiRunResu
805
829
  // MuxSurface A1 (spec §13.1): attempt booting the worker in a mux pane BEFORE
806
830
  // spawning a stdio pipe. Returns a finished result in surface mode (pane exit
807
831
  // awaited) or null → fall through to the classic headless spawn below.
808
- const surfaceResult = await trySurfaceBranch(input, depthEnv, builtArgs, mergedEnv, builtEnv, tempDir);
832
+ // v0.11.36: TUI panes are NOT sandboxed (pass 1) — when the sandbox is on
833
+ // the branch is SKIPPED so a sandboxed request can never land in an
834
+ // unsandboxed pane (fail-closed branch selection).
835
+ const surfaceResult = sandbox.enabled ? null : await trySurfaceBranch(input, depthEnv, builtArgs, mergedEnv, builtEnv, tempDir);
809
836
  if (surfaceResult) return surfaceResult;
810
837
  // U14 (child-pi protocol v2 — upgrade-spec 2026-10-09): `--mode rpc`
811
838
  // transport, opt-in via PI_CREW_CHILD_PI_MODE=rpc (json remains the
@@ -814,7 +841,7 @@ export async function runChildPi(input: ChildPiRunInput): Promise<ChildPiRunResu
814
841
  // json branch below stays the fail-safe path. Surface TUI panes are
815
842
  // unaffected — trySurfaceBranch consumed builtArgs above and never
816
843
  // reaches this point in surface mode.
817
- if (resolveChildPiRpcMode()) {
844
+ if (!sandbox.enabled && resolveChildPiRpcMode()) {
818
845
  const rpcResult = await tryChildPiRpcRun({ input, effectiveTask, ctx: spawnPrep.ctx, workerSession, sessionRecoveryEnabled });
819
846
  if (rpcResult) return rpcResult;
820
847
  }
@@ -824,7 +851,38 @@ export async function runChildPi(input: ChildPiRunInput): Promise<ChildPiRunResu
824
851
  // owned by buildFinalChildPiSpawnOptions (see child-pi-spawn.ts, BLOCKER 2 / S5).
825
852
  // R6-8: modelChain threads the candidate chain for provider-key scoping.
826
853
  const spawnOptions = buildFinalChildPiSpawnOptions(input.cwd, mergedEnv, builtEnv, input.model, input.modelChain);
827
- const child = spawn(spawnSpec.command, spawnSpec.args, spawnOptions);
854
+ // v0.11.36 OS boundary: when the sandbox is on, the headless spawn is
855
+ // wrapped in bwrap. Mounts: cwd RW (the worker edits the project), the
856
+ // scoped inference agent dir RW (pi writes session state under it), and
857
+ // the parent-side paths this run handed the worker via env/argv
858
+ // (task.md tempDir, scratchpad snapshot/restore, steering file) READ-ONLY
859
+ // plus the inter-pi broker socket RW (connect() needs write perm). A
860
+ // forgotten path fails CLOSED inside the namespace (ENOENT) — it never
861
+ // silently reaches the host.
862
+ let spawnCommand = spawnSpec.command;
863
+ let spawnArgs = spawnSpec.args;
864
+ if (sandbox.enabled) {
865
+ const inferenceSnap = getInferenceBrokerSnapshot();
866
+ const wrapped = applySandboxToSpawnSpec(
867
+ { command: spawnSpec.command, args: spawnSpec.args },
868
+ {
869
+ cwd: input.cwd,
870
+ net: sandbox.net,
871
+ agentDir: inferenceSnap.active ? inferenceSnap.scopedAgentDir : undefined,
872
+ roBindPaths: [
873
+ tempDir,
874
+ builtEnv.PI_CREW_SCRATCHPAD_SNAPSHOT,
875
+ builtEnv.PI_CREW_SCRATCHPAD_RESTORE,
876
+ builtEnv.PI_CREW_STEERING_FILE,
877
+ ].filter((p): p is string => typeof p === "string" && p.length > 0),
878
+ rwBindPaths: [input.brokerSpawn?.socketPath].filter((p): p is string => typeof p === "string" && p.length > 0),
879
+ },
880
+ );
881
+ spawnCommand = wrapped.command;
882
+ spawnArgs = wrapped.args;
883
+ spawnOptions.env = { ...spawnOptions.env, ...wrapped.envOverrides };
884
+ }
885
+ const child = spawn(spawnCommand, spawnArgs, spawnOptions);
828
886
  if (child.pid) {
829
887
  registerActiveChild(child.pid, child);
830
888
  input.onSpawn?.(child.pid);
@@ -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,25 @@ 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. The
110
+ * upstream DEFAULT carries the provider's version path (openai …/v1, google
111
+ * …/v1beta) exactly like the built-in models' baseUrls do — the scoped
112
+ * models.json replaces the WHOLE baseUrl with a bare /<provider> prefix, so
113
+ * the version path must never be duplicated on the broker side. */
114
+ interface ProviderRoute {
115
+ name: "anthropic" | "openai" | "google";
116
+ upstream: string;
117
+ auth: { header: "x-api-key" | "authorization" | "x-goog-api-key"; value: string };
105
118
  }
106
119
 
107
120
  interface BrokerState {
108
121
  server: http.Server;
109
122
  port: number;
110
- upstream: string;
111
- /** x-api-key value OR Authorization bearer for the upstream. */
112
- upstreamAuth: { header: "x-api-key" | "authorization"; value: string };
123
+ routes: Map<string, ProviderRoute>; // by path prefix ("/anthropic", "/openai")
113
124
  workers: Map<string, WorkerEntry>; // by token
114
125
  byKey: Map<string, WorkerEntry>; // by workerKey
115
126
  scopedAgentDir: string;
@@ -120,12 +131,89 @@ interface BrokerState {
120
131
  // stopped by stopInferenceBroker() (extension teardown / tests).
121
132
  let broker: BrokerState | undefined;
122
133
 
123
- function resolveUpstreamAuth(): { header: "x-api-key" | "authorization"; value: string } | undefined {
124
- const apiKey = process.env.ANTHROPIC_API_KEY;
125
- if (apiKey && apiKey.length > 0) return { header: "x-api-key", value: apiKey };
126
- const authToken = process.env.ANTHROPIC_AUTH_TOKEN;
127
- if (authToken && authToken.length > 0) return { header: "authorization", value: `Bearer ${authToken}` };
128
- return undefined;
134
+ /**
135
+ * Read the PARENT's auth.json credential store — the documented file shape is
136
+ * `{ "<provider>": { "type": "api_key", "key": "..." } }` (verified against
137
+ * the SDK store + a live file). Read ONCE at broker start; the key stays in
138
+ * memory exactly like an env-sourced key. Parse failures are logged WITHOUT
139
+ * the key and ignored (env credentials still work).
140
+ */
141
+ function readAuthJsonCredentials(env: NodeJS.ProcessEnv): Record<string, { type?: string; key?: string }> {
142
+ const agentDir = env.PI_CODING_AGENT_DIR || path.join(os.homedir(), ".pi", "agent");
143
+ const authPath = path.join(agentDir, "auth.json");
144
+ try {
145
+ const raw = fs.readFileSync(authPath, "utf8");
146
+ const parsed: unknown = JSON.parse(raw);
147
+ if (!parsed || typeof parsed !== "object") return {};
148
+ return parsed as Record<string, { type?: string; key?: string }>;
149
+ } catch {
150
+ return {}; // absent/unreadable/malformed — env credentials remain the path
151
+ }
152
+ }
153
+
154
+ /**
155
+ * Resolve every provider route (v0.11.34 multi-provider). Precedence per
156
+ * provider: env credential FIRST (CI behavior identical to v0.11.33), then
157
+ * the parent's auth.json file credential. Returns the routes that resolved
158
+ * and a human-readable trace of what was consulted (never contains keys).
159
+ */
160
+ function resolveProviderRoutes(env: NodeJS.ProcessEnv): { routes: ProviderRoute[]; trace: string } {
161
+ const authJson = readAuthJsonCredentials(env);
162
+ const genericUpstream = getCrewEnv("PI_CREW_INFERENCE_UPSTREAM", env); // tests: applies to every provider
163
+ const routes: ProviderRoute[] = [];
164
+ const consulted: string[] = [];
165
+
166
+ const anthropicUpstream = getCrewEnv("PI_CREW_INFERENCE_UPSTREAM_ANTHROPIC", env) || genericUpstream || "https://api.anthropic.com";
167
+ const anthropicApiKey = env.ANTHROPIC_API_KEY;
168
+ const anthropicAuthToken = env.ANTHROPIC_AUTH_TOKEN;
169
+ const anthropicStored = authJson.anthropic;
170
+ consulted.push("env ANTHROPIC_API_KEY", "env ANTHROPIC_AUTH_TOKEN", "auth.json:anthropic");
171
+ if (anthropicApiKey && anthropicApiKey.length > 0) {
172
+ routes.push({ name: "anthropic", upstream: anthropicUpstream, auth: { header: "x-api-key", value: anthropicApiKey } });
173
+ } else if (anthropicAuthToken && anthropicAuthToken.length > 0) {
174
+ routes.push({
175
+ name: "anthropic",
176
+ upstream: anthropicUpstream,
177
+ auth: { header: "authorization", value: `Bearer ${anthropicAuthToken}` },
178
+ });
179
+ } else if (anthropicStored?.type === "api_key" && typeof anthropicStored.key === "string" && anthropicStored.key.length > 0) {
180
+ routes.push({ name: "anthropic", upstream: anthropicUpstream, auth: { header: "x-api-key", value: anthropicStored.key } });
181
+ }
182
+
183
+ const openaiUpstream = getCrewEnv("PI_CREW_INFERENCE_UPSTREAM_OPENAI", env) || genericUpstream || "https://api.openai.com/v1";
184
+ const openaiApiKey = env.OPENAI_API_KEY;
185
+ const openaiStored = authJson.openai;
186
+ consulted.push("env OPENAI_API_KEY", "auth.json:openai");
187
+ if (openaiApiKey && openaiApiKey.length > 0) {
188
+ routes.push({ name: "openai", upstream: openaiUpstream, auth: { header: "authorization", value: `Bearer ${openaiApiKey}` } });
189
+ } else if (openaiStored?.type === "api_key" && typeof openaiStored.key === "string" && openaiStored.key.length > 0) {
190
+ routes.push({ name: "openai", upstream: openaiUpstream, auth: { header: "authorization", value: `Bearer ${openaiStored.key}` } });
191
+ }
192
+
193
+ // v0.11.36 google (native google-generative-ai wire): the built-in google
194
+ // provider speaks `x-goog-api-key` and paths under /v1beta (verified against
195
+ // the pi catalog: ids like "gemini-2.5-pro", baseUrl ...
196
+ // /v1beta). Credential precedence mirrors the others: env FIRST
197
+ // (GOOGLE_API_KEY, then GEMINI_API_KEY — both conventional), then auth.json:google.
198
+ const googleUpstream =
199
+ getCrewEnv("PI_CREW_INFERENCE_UPSTREAM_GOOGLE", env) || genericUpstream || "https://generativelanguage.googleapis.com/v1beta";
200
+ const googleApiKey = env.GOOGLE_API_KEY;
201
+ const geminiApiKey = env.GEMINI_API_KEY;
202
+ const googleStored = authJson.google;
203
+ consulted.push("env GOOGLE_API_KEY", "env GEMINI_API_KEY", "auth.json:google");
204
+ const googleCredential =
205
+ googleApiKey && googleApiKey.length > 0
206
+ ? googleApiKey
207
+ : geminiApiKey && geminiApiKey.length > 0
208
+ ? geminiApiKey
209
+ : googleStored?.type === "api_key" && typeof googleStored.key === "string" && googleStored.key.length > 0
210
+ ? googleStored.key
211
+ : undefined;
212
+ if (googleCredential) {
213
+ routes.push({ name: "google", upstream: googleUpstream, auth: { header: "x-goog-api-key", value: googleCredential } });
214
+ }
215
+
216
+ return { routes, trace: consulted.join(", ") };
129
217
  }
130
218
 
131
219
  /**
@@ -138,18 +226,30 @@ function resolveUpstreamAuth(): { header: "x-api-key" | "authorization"; value:
138
226
  */
139
227
  export async function maybeStartInferenceBroker(env: NodeJS.ProcessEnv = process.env): Promise<InferenceBrokerSnapshot> {
140
228
  if (broker)
141
- return { active: true, baseUrl: `http://127.0.0.1:${broker.port}`, port: broker.port, scopedAgentDir: broker.scopedAgentDir };
142
- if (!getCrewEnvBool(INFERENCE_BROKER_ENABLE_ENV)) return { active: false, baseUrl: "", port: 0, scopedAgentDir: undefined };
229
+ return {
230
+ active: true,
231
+ baseUrl: `http://127.0.0.1:${broker.port}`,
232
+ port: broker.port,
233
+ scopedAgentDir: broker.scopedAgentDir,
234
+ providers: [...broker.routes.values()].map((r) => r.name),
235
+ };
236
+ if (!getCrewEnvBool(INFERENCE_BROKER_ENABLE_ENV))
237
+ return { active: false, baseUrl: "", port: 0, scopedAgentDir: undefined, providers: [] };
143
238
 
144
- const upstreamAuth = resolveUpstreamAuth();
145
- if (!upstreamAuth) {
239
+ const { routes, trace } = resolveProviderRoutes(env);
240
+ if (routes.length === 0) {
146
241
  throw new Error(
147
- `${INFERENCE_BROKER_ENABLE_ENV}=1 but neither ANTHROPIC_API_KEY nor ANTHROPIC_AUTH_TOKEN is set in the parent environment — the inference broker refuses to start (fail-closed: workers would otherwise receive no usable credential, or worse, fall back to real provider keys). Set the provider env key or unset ${INFERENCE_BROKER_ENABLE_ENV}.`,
242
+ `${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 / GOOGLE_API_KEY / GEMINI_API_KEY in the parent env, an api_key entry for anthropic/openai/google in the parent's auth.json, or unset ${INFERENCE_BROKER_ENABLE_ENV}.`,
148
243
  );
149
244
  }
150
- const upstream = getCrewEnv("PI_CREW_INFERENCE_UPSTREAM", env) || DEFAULT_UPSTREAM; // tests inject a local mock
151
- broker = await createBrokerServer(upstream, upstreamAuth);
152
- return { active: true, baseUrl: `http://127.0.0.1:${broker.port}`, port: broker.port, scopedAgentDir: broker.scopedAgentDir };
245
+ broker = await createBrokerServer(routes);
246
+ return {
247
+ active: true,
248
+ baseUrl: `http://127.0.0.1:${broker.port}`,
249
+ port: broker.port,
250
+ scopedAgentDir: broker.scopedAgentDir,
251
+ providers: [...broker.routes.values()].map((r) => r.name),
252
+ };
153
253
  }
154
254
 
155
255
  /**
@@ -160,8 +260,15 @@ export async function maybeStartInferenceBroker(env: NodeJS.ProcessEnv = process
160
260
  */
161
261
  export function requireInferenceBrokerIfEnabled(): InferenceBrokerSnapshot {
162
262
  if (broker)
163
- return { active: true, baseUrl: `http://127.0.0.1:${broker.port}`, port: broker.port, scopedAgentDir: broker.scopedAgentDir };
164
- if (!getCrewEnvBool(INFERENCE_BROKER_ENABLE_ENV)) return { active: false, baseUrl: "", port: 0, scopedAgentDir: undefined };
263
+ return {
264
+ active: true,
265
+ baseUrl: `http://127.0.0.1:${broker.port}`,
266
+ port: broker.port,
267
+ scopedAgentDir: broker.scopedAgentDir,
268
+ providers: [...broker.routes.values()].map((r) => r.name),
269
+ };
270
+ if (!getCrewEnvBool(INFERENCE_BROKER_ENABLE_ENV))
271
+ return { active: false, baseUrl: "", port: 0, scopedAgentDir: undefined, providers: [] };
165
272
  throw new Error(
166
273
  `${INFERENCE_BROKER_ENABLE_ENV}=1 but the broker is not started — run() must await maybeStartInferenceBroker() before prepareSpawnContext()`,
167
274
  );
@@ -169,19 +276,23 @@ export function requireInferenceBrokerIfEnabled(): InferenceBrokerSnapshot {
169
276
 
170
277
  /** Read-only view for spawn-option builders. Never starts anything. */
171
278
  export function getInferenceBrokerSnapshot(): InferenceBrokerSnapshot {
172
- if (!broker) return { active: false, baseUrl: "", port: 0, scopedAgentDir: undefined };
173
- return { active: true, baseUrl: `http://127.0.0.1:${broker.port}`, port: broker.port, scopedAgentDir: broker.scopedAgentDir };
279
+ if (!broker) return { active: false, baseUrl: "", port: 0, scopedAgentDir: undefined, providers: [] };
280
+ return {
281
+ active: true,
282
+ baseUrl: `http://127.0.0.1:${broker.port}`,
283
+ port: broker.port,
284
+ scopedAgentDir: broker.scopedAgentDir,
285
+ providers: [...broker.routes.values()].map((r) => r.name),
286
+ };
174
287
  }
175
288
 
176
- async function createBrokerServer(
177
- upstream: string,
178
- upstreamAuth: { header: "x-api-key" | "authorization"; value: string },
179
- ): Promise<BrokerState> {
289
+ async function createBrokerServer(routes: ProviderRoute[]): Promise<BrokerState> {
180
290
  const workers = new Map<string, WorkerEntry>();
181
291
  const byKey = new Map<string, WorkerEntry>();
292
+ const routeMap = new Map<string, ProviderRoute>(routes.map((r) => [`/${r.name}`, r]));
182
293
 
183
294
  const server = http.createServer((req, res) => {
184
- void handleRequest(req, res, { upstream, upstreamAuth, workers });
295
+ void handleRequest(req, res, { routes: routeMap, workers });
185
296
  });
186
297
  server.unref(); // never hold the parent's event loop open
187
298
  await new Promise<void>((resolve, reject) => {
@@ -195,9 +306,9 @@ async function createBrokerServer(
195
306
  if (!address || typeof address === "string") throw new Error("inference broker failed to bind a loopback port");
196
307
  const port = address.port;
197
308
 
198
- const scopedAgentDir = buildScopedAgentDir(port);
309
+ const scopedAgentDir = buildScopedAgentDir(port, routes);
199
310
 
200
- return { server, port, upstream, upstreamAuth, workers, byKey, scopedAgentDir };
311
+ return { server, port, routes: routeMap, workers, byKey, scopedAgentDir };
201
312
  }
202
313
 
203
314
  /**
@@ -206,27 +317,34 @@ async function createBrokerServer(
206
317
  * built-in anthropic provider to the broker with per-worker token
207
318
  * interpolation at request time. Mode 0o600 file / 0o700 dir.
208
319
  */
209
- function buildScopedAgentDir(port: number): string {
320
+ function buildScopedAgentDir(port: number, routes: ProviderRoute[]): string {
210
321
  const dir = createSafeTempDir(getPiTempBase(), "pi-crew-inference-");
211
322
  fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
323
+ // Every resolvable provider gets a PATH-PREFIXED baseUrl ("/anthropic",
324
+ // "/openai") so the broker routes deterministically by prefix and forwards
325
+ // the remaining path verbatim to that provider's upstream.
326
+ const providers: Record<string, { baseUrl: string; apiKey: string }> = {};
327
+ for (const route of routes) {
328
+ providers[route.name] = {
329
+ baseUrl: `http://127.0.0.1:${port}/${route.name}`,
330
+ apiKey: `$${INFERENCE_TOKEN_ENV}`,
331
+ };
332
+ }
212
333
  const models = {
213
334
  $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
- },
335
+ providers,
220
336
  };
221
337
  const file = path.join(dir, "models.json");
222
338
  fs.writeFileSync(file, `${JSON.stringify(models, null, "\t")}\n`, { mode: 0o600 });
223
339
  return dir;
224
340
  }
225
341
 
226
- /** Extract the caller token from x-api-key or Authorization: Bearer. */
342
+ /** Extract the caller token from x-api-key, x-goog-api-key, or Authorization: Bearer. */
227
343
  function extractToken(req: http.IncomingMessage): string | undefined {
228
344
  const apiKey = req.headers["x-api-key"];
229
345
  if (typeof apiKey === "string" && apiKey.length > 0) return apiKey;
346
+ const googKey = req.headers["x-goog-api-key"];
347
+ if (typeof googKey === "string" && googKey.length > 0) return googKey;
230
348
  const auth = req.headers.authorization;
231
349
  if (typeof auth === "string" && auth.startsWith("Bearer ")) return auth.slice("Bearer ".length);
232
350
  return undefined;
@@ -241,9 +359,26 @@ function deny(res: http.ServerResponse, status: number, message: string): void {
241
359
  async function handleRequest(
242
360
  req: http.IncomingMessage,
243
361
  res: http.ServerResponse,
244
- ctx: { upstream: string; upstreamAuth: { header: string; value: string }; workers: Map<string, WorkerEntry> },
362
+ ctx: { routes: Map<string, ProviderRoute>; workers: Map<string, WorkerEntry> },
245
363
  ): Promise<void> {
246
364
  try {
365
+ // (0) Provider routing: the FIRST path segment names the provider
366
+ // ("/anthropic/v1/messages", "/openai/chat/completions"); the broker
367
+ // forwards the REMAINING path verbatim to that provider's upstream.
368
+ const url = req.url ?? "/";
369
+ let route: ProviderRoute | undefined;
370
+ let upstreamPath = url;
371
+ for (const [prefix, candidate] of ctx.routes) {
372
+ if (url === prefix || url.startsWith(`${prefix}/`)) {
373
+ route = candidate;
374
+ upstreamPath = url.slice(prefix.length) || "/";
375
+ break;
376
+ }
377
+ }
378
+ if (!route) {
379
+ deny(res, 404, "unknown provider route (expected /anthropic/..., /openai/..., or /google/...)");
380
+ return;
381
+ }
247
382
  // (1) Auth — fail-closed, generic errors (never echo tokens).
248
383
  const token = extractToken(req);
249
384
  const worker = token ? ctx.workers.get(token) : undefined;
@@ -278,8 +413,8 @@ async function handleRequest(
278
413
  if (!FORWARD_REQUEST_HEADERS.has(name.toLowerCase())) continue;
279
414
  if (typeof value === "string") headers[name.toLowerCase()] = value;
280
415
  }
281
- headers[ctx.upstreamAuth.header] = ctx.upstreamAuth.value;
282
- const upstreamRes = await fetch(`${ctx.upstream}${req.url ?? "/v1/messages"}`, {
416
+ headers[route.auth.header] = route.auth.value;
417
+ const upstreamRes = await fetch(`${route.upstream}${upstreamPath}`, {
283
418
  // eslint-disable-next-line
284
419
  method: req.method ?? "POST",
285
420
  headers,
@@ -352,10 +487,37 @@ class UsageCounter {
352
487
  // SSE message_delta: evt.usage.output_tokens is CUMULATIVE (take max).
353
488
  // Non-stream JSON body: top-level evt.usage, both absolute.
354
489
  const usage = evt?.message?.usage ?? evt?.usage;
490
+ // Google native (google-generative-ai): usageMetadata with
491
+ // promptTokenCount / candidatesTokenCount (+ thoughtsTokenCount) —
492
+ // CUMULATIVE per stream, so max() below lands on the final totals.
493
+ const meta = evt?.usageMetadata;
494
+ if (meta && typeof meta === "object") {
495
+ const prompt = typeof meta.promptTokenCount === "number" ? meta.promptTokenCount : 0;
496
+ const candidates = typeof meta.candidatesTokenCount === "number" ? meta.candidatesTokenCount : 0;
497
+ const thoughts = typeof meta.thoughtsTokenCount === "number" ? meta.thoughtsTokenCount : 0;
498
+ this.input = Math.max(this.input, prompt);
499
+ this.output = Math.max(this.output, candidates + thoughts);
500
+ }
355
501
  if (!usage || typeof usage !== "object") continue;
356
- if (typeof usage.input_tokens === "number" && evt.type !== "message_delta")
357
- this.input = Math.max(this.input, usage.input_tokens);
358
- if (typeof usage.output_tokens === "number") this.output = Math.max(this.output, usage.output_tokens);
502
+ // Anthropic: input_tokens / output_tokens. OpenAI chat-completions:
503
+ // prompt_tokens / completion_tokens (final usage chunk, absolute).
504
+ // Google native (google-generative-ai): usageMetadata with
505
+ // promptTokenCount / candidatesTokenCount (+ thoughtsTokenCount) —
506
+ // CUMULATIVE per stream, so max() below lands on the final totals.
507
+ const inTok =
508
+ typeof usage.input_tokens === "number"
509
+ ? usage.input_tokens
510
+ : typeof usage.prompt_tokens === "number"
511
+ ? usage.prompt_tokens
512
+ : undefined;
513
+ const outTok =
514
+ typeof usage.output_tokens === "number"
515
+ ? usage.output_tokens
516
+ : typeof usage.completion_tokens === "number"
517
+ ? usage.completion_tokens
518
+ : undefined;
519
+ if (typeof inTok === "number" && evt.type !== "message_delta") this.input = Math.max(this.input, inTok);
520
+ if (typeof outTok === "number") this.output = Math.max(this.output, outTok);
359
521
  } catch {
360
522
  // non-JSON line — ignore
361
523
  }
@@ -384,6 +546,28 @@ export function registerInferenceWorker(workerKey: string): string {
384
546
  return token;
385
547
  }
386
548
 
549
+ /** Deterministic worker key for a task-owned worker — SHARED by the spawn side (prepareSpawnContext) and the exit-revoke wrapper (runChildPi finally). */
550
+ export function inferenceWorkerKeyForAgent(agentId: string): string {
551
+ return `inference:${agentId}`;
552
+ }
553
+
554
+ /**
555
+ * Non-throwing exit hook (v0.11.36): revoke a task worker's inference token
556
+ * when its runChildPi call settles — normal exit, crash, or abort. Anonymous
557
+ * (agentId-less) spawns use per-spawn unique keys that only the spawn path
558
+ * knows; their tokens die at register-replacement or broker stop. Broker off
559
+ * → no-op. NEVER throws (must not mask a worker result).
560
+ */
561
+ export function revokeInferenceWorkerForAgent(agentId: string | undefined): boolean {
562
+ if (!agentId) return false;
563
+ try {
564
+ if (!getInferenceBrokerSnapshot().active) return false;
565
+ return revokeInferenceWorker(inferenceWorkerKeyForAgent(agentId));
566
+ } catch {
567
+ return false;
568
+ }
569
+ }
570
+
387
571
  /** Kill-switch for one worker: its token dies on the NEXT request. */
388
572
  export function revokeInferenceWorker(workerKey: string): boolean {
389
573
  if (!broker) return false;