pi-crew 0.11.30 → 0.11.33

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.30",
3
+ "version": "0.11.33",
4
4
  "description": "Pi extension for coordinated AI teams, workflows, worktrees, and async task orchestration",
5
5
  "author": "baphuongna",
6
6
  "license": "MIT",
@@ -198,6 +198,22 @@ const CREW_ENV_FORWARDING_DECL: Record<string, CrewEnvForwardDecision> = {
198
198
  reason: "per-task compound broker token via built.env/pipe (F4) — secret, never allowlisted",
199
199
  },
200
200
  PI_CREW_BROKER_TASK_ID: { kind: "child-write", reason: "child-pi-spawn broker handshake" },
201
+ PI_CREW_INFERENCE_BROKER: {
202
+ kind: "gap",
203
+ reason: "v0.11.31 inference broker toggle — resolved in the PARENT only (ensureInferenceBroker); deliberately never forwarded to children (a child re-starting its own broker would be nonsensical)",
204
+ },
205
+ PI_CREW_INFERENCE_TOKEN: {
206
+ kind: "child-write",
207
+ reason: "v0.11.31 per-worker inference broker token via built.env — secret, never allowlisted",
208
+ },
209
+ PI_CREW_INFERENCE_BUDGET_TOKENS: {
210
+ kind: "gap",
211
+ reason: "v0.11.31 per-worker budget read by the parent broker at token issuance (registerInferenceWorker)",
212
+ },
213
+ PI_CREW_INFERENCE_UPSTREAM: {
214
+ kind: "test-only",
215
+ reason: "v0.11.31 broker upstream override — TESTS ONLY (local mock), production never sets it",
216
+ },
201
217
  PI_CREW_STEERING_FILE: { kind: "child-write", reason: "child-pi-spawn per-run steering JSONL" },
202
218
  PI_CREW_MAX_OUTPUT: { kind: "child-write", reason: "pi-args emits agent.maxTokens into built.env" },
203
219
  PI_CREW_RPC_SECRET: { kind: "child-write", reason: "shared HMAC secret handed to the child at spawn — secret name, never allowlisted" },
@@ -494,6 +494,24 @@ export const CREW_ENV_VARS: Record<string, CrewEnvVarSpec> = {
494
494
  name: "PI_CREW_WORKER_TRANSPORT",
495
495
  doc: "W7 EXPERIMENTAL worker transport override: 'rpc' selects the prototype RPC frame-client (src/runtime/rpc/) at the run-worker seam (wired e2728f68; D1/DR2 also plumbs config runtime.workerTransport to the same seam — env WINS over config when set, invalid env fails safe to stdio); anything else/unset → config value, default stdio child-pi path (rpc-worker.ts resolveWorkerTransport)",
496
496
  },
497
+ PI_CREW_INFERENCE_BROKER: {
498
+ name: "PI_CREW_INFERENCE_BROKER",
499
+ parser: "boolean",
500
+ doc: "v0.11.31 (S-broker PoC) opt-in loopback inference broker: '1' starts a 127.0.0.1 HTTP broker in the parent; child workers get ONLY a per-worker token (PI_CREW_INFERENCE_TOKEN) + a scoped agent dir (PI_CODING_AGENT_DIR containing models.json that reroutes the built-in anthropic provider to the broker) instead of any real provider key. Real key (ANTHROPIC_API_KEY / ANTHROPIC_AUTH_TOKEN from the PARENT env only) is substituted after requests leave the worker. FAIL-CLOSED: flag set without an upstream credential throws at first spawn (inference-broker.ts). PoC: anthropic upstream only, env-credential users only (auth.json users unsupported yet)",
501
+ },
502
+ PI_CREW_INFERENCE_TOKEN: {
503
+ name: "PI_CREW_INFERENCE_TOKEN",
504
+ doc: "v0.11.31 child-side per-worker inference broker token (256-bit random, heap-only on the parent, injected via built.env by prepareSpawnContext when the broker is active; interpolated into models.json apiKey as $PI_CREW_INFERENCE_TOKEN by the child at request time). NEVER user-set; workerKey-scoped — re-registering a task id replaces (kills) the old token",
505
+ },
506
+ PI_CREW_INFERENCE_BUDGET_TOKENS: {
507
+ name: "PI_CREW_INFERENCE_BUDGET_TOKENS",
508
+ parser: "int",
509
+ doc: "v0.11.31 per-worker inference token budget counted from streamed Anthropic usage events; 0/unset = unlimited. Requests past the budget get 429 (inference-broker.ts registerInferenceWorker)",
510
+ },
511
+ PI_CREW_INFERENCE_UPSTREAM: {
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",
514
+ },
497
515
  PI_CREW_CLASSIFIER_ENABLED: {
498
516
  name: "PI_CREW_CLASSIFIER_ENABLED",
499
517
  parser: "boolean",
@@ -1,4 +1,5 @@
1
1
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ import { stopInferenceBroker } from "../runtime/child-pi/inference-broker.ts";
2
3
  import { cleanupAllTrackedTempDirs } from "../runtime/model/pi-args.ts";
3
4
  import { isSessionSwitchInFlight } from "../ui/inline-panel/view-session-store.ts";
4
5
  import { logInternalError } from "../utils/internal-error.ts";
@@ -89,6 +90,16 @@ export function registerCleanupHandler(pi: ExtensionAPI, opts?: { disposeTermina
89
90
  // Kill all child-pi processes
90
91
  await cleanupChildProcesses();
91
92
 
93
+ // v0.11.31: stop the loopback inference broker — revokes every
94
+ // worker token (kill-switch) and removes the scoped agent dir BEFORE
95
+ // generic temp cleanup (which would also catch it — this is the
96
+ // explicit, token-revoking path).
97
+ try {
98
+ stopInferenceBroker();
99
+ } catch (error) {
100
+ logInternalError("crew-cleanup.inference-broker", error);
101
+ }
102
+
92
103
  // Cleanup temp directories
93
104
  await cleanupTempDirectories();
94
105
 
@@ -28,6 +28,13 @@ import { buildPiWorkerArgs, createSafeTempDir, getPiTempBase } from "../model/pi
28
28
  import { getPiSpawnCommand } from "../pi-spawn.ts";
29
29
  import { findLatestScratchpadSnapshot } from "../scratchpad/snapshot-lookup.ts";
30
30
  import type { ChildPiRunInput, ChildPiRunResult } from "./child-pi.ts";
31
+ import {
32
+ getInferenceBrokerSnapshot,
33
+ INFERENCE_TOKEN_ENV,
34
+ PI_AGENT_DIR_ENV,
35
+ registerInferenceWorker,
36
+ requireInferenceBrokerIfEnabled,
37
+ } from "./inference-broker.ts";
31
38
 
32
39
  // ── Env allowlist (base set always passed to children) ──────────────────
33
40
  // Provider API keys are injected dynamically via buildScopedAllowList() only
@@ -102,7 +109,13 @@ export function buildChildPiSpawnOptions(
102
109
  // provider does not spawn keyless. When no model is given, only
103
110
  // BASE_ALLOWLIST system vars pass through — no provider keys leak.
104
111
  const scopeModels: (string | undefined)[] = modelChain && modelChain.length > 0 ? modelChain : model !== undefined ? [model] : [];
105
- const allowList = scopeModels.length > 0 ? buildScopedAllowList(BASE_ALLOWLIST, scopeModels) : BASE_ALLOWLIST;
112
+ // v0.11.31 inference broker: when active, the child's ONLY credential is
113
+ // its per-worker broker token — provider env keys are NEVER injected (they
114
+ // would defeat the broker by handing the worker the real reusable key
115
+ // again). The broker snapshot is read-only module state (no side effects —
116
+ // the broker is started by prepareSpawnContext, never here).
117
+ const inferenceBrokerActive = getInferenceBrokerSnapshot().active;
118
+ const allowList = !inferenceBrokerActive && scopeModels.length > 0 ? buildScopedAllowList(BASE_ALLOWLIST, scopeModels) : BASE_ALLOWLIST;
106
119
  // R6-8: warn when the scoped models resolve to ZERO provider env keys AND at
107
120
  // least one is a truly-bare id (no "provider/" prefix) — env-key auth + bare
108
121
  // id means the child worker will very likely fail authentication (observed as
@@ -244,6 +257,15 @@ export function buildFinalChildPiSpawnOptions(
244
257
  // process would never see steering file, kind, role, or broker creds.
245
258
  // Safe because step (a) just proved builtEnv holds no secret keys.
246
259
  spawnOptions.env = { ...spawnOptions.env, ...builtEnv };
260
+ // (c′) v0.11.31 inference broker: pi's NATIVE agent-dir override is NOT a
261
+ // control-namespace key, so it never travels through built.env (step (a)
262
+ // would reject it) — it is mapped here, at the spawn-options layer, from
263
+ // the broker snapshot when a per-worker token is present. The scoped dir
264
+ // contains ONLY models.json (no auth.json, no extensions, no skills).
265
+ const inferenceSnapshot = getInferenceBrokerSnapshot();
266
+ if (inferenceSnapshot.active && builtEnv[INFERENCE_TOKEN_ENV]) {
267
+ spawnOptions.env[PI_AGENT_DIR_ENV] = inferenceSnapshot.scopedAgentDir ?? "";
268
+ }
247
269
  // (d) Return the composed SpawnOptions for spawn(...).
248
270
  return spawnOptions;
249
271
  }
@@ -265,6 +287,8 @@ export interface SpawnContext {
265
287
  tempDir: string | undefined;
266
288
  /** The per-call built.env (control vars only) — for security canary assertions. */
267
289
  builtEnv: Record<string, string | undefined>;
290
+ /** v0.11.31: inference-broker worker key that owns this spawn's token (cleanup/revoke bookkeeping). */
291
+ inferenceWorkerKey?: string;
268
292
  }
269
293
 
270
294
  /**
@@ -308,6 +332,21 @@ export function prepareSpawnContext(
308
332
  // child env gets parentDepth+1 = the true grandchild depth.
309
333
  env: depthEnv,
310
334
  });
335
+ // v0.11.31 inference broker (opt-in PI_CREW_INFERENCE_BROKER=1): the broker
336
+ // must already be running (child-pi.ts run() awaits maybeStartInferenceBroker()
337
+ // first — http listen needs an event-loop turn, so the SYNC spawn path can
338
+ // only require, never start). Issue this worker's token. Control-namespace
339
+ // key → passes assertOnlyControlEnvKeys. FAIL-CLOSED: flag on without a
340
+ // started broker or an upstream credential throws here (never silently
341
+ // spawn keyless / real-key workers). Worker key: the task id when present
342
+ // (retries REPLACE the old token — registerInferenceWorker semantics), else
343
+ // a per-spawn unique.
344
+ const inferenceSnapshot = requireInferenceBrokerIfEnabled();
345
+ let inferenceWorkerKey: string | undefined;
346
+ if (inferenceSnapshot.active) {
347
+ inferenceWorkerKey = input.agentId ? `inference:${input.agentId}` : `inference:spawn:${process.pid}:${Date.now().toString(36)}`;
348
+ built.env[INFERENCE_TOKEN_ENV] = registerInferenceWorker(inferenceWorkerKey);
349
+ }
311
350
  // Pass steering file path to child for real-time steer injection
312
351
  if (input.steeringFile) built.env.PI_CREW_STEERING_FILE = input.steeringFile;
313
352
  // I5: run/task identity is threaded ALWAYS (not broker-gated) so the worker's
@@ -433,6 +472,7 @@ export function prepareSpawnContext(
433
472
  mergedEnv: { ...process.env, ...built.env },
434
473
  tempDir: built.tempDir,
435
474
  builtEnv: built.env,
475
+ inferenceWorkerKey,
436
476
  },
437
477
  };
438
478
  }
@@ -36,6 +36,7 @@ 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
40
  import { runMockChildPi } from "./mock-fixtures.ts";
40
41
  // W2 (session-file recovery): deterministic worker session flags + crash-path
41
42
  // tail-replay of the session JSONL (see session-recovery.ts header).
@@ -777,6 +778,9 @@ export async function runChildPi(input: ChildPiRunInput): Promise<ChildPiRunResu
777
778
  // guard below returns before the argv is ever consumed.
778
779
  const versionedInput: ChildPiRunInput =
779
780
  !input.signal?.aborted && !resolveHermeticWorkers(input.hermeticWorkers) ? { ...input, piVersion: await probePiVersion() } : input;
781
+ // v0.11.31: start the inference broker (flag-gated, fail-closed) BEFORE the
782
+ // sync spawn-prep path — http listen needs an event-loop turn.
783
+ await maybeStartInferenceBroker();
780
784
  const spawnPrep = prepareSpawnContext(brokerSpawn ? { ...versionedInput, brokerSpawn } : versionedInput, effectiveTask, depthEnv);
781
785
  if (spawnPrep.kind === "aborted") return spawnPrep.result;
782
786
  const { spawnSpec, mergedEnv, tempDir, builtEnv, builtArgs } = spawnPrep.ctx;
@@ -0,0 +1,434 @@
1
+ /**
2
+ * inference-broker.ts — v0.11.31 (S-broker PoC): loopback inference broker.
3
+ *
4
+ * WHY (the A7 endgame continuation): S\* (v0.11.30) made the child env EXACT
5
+ * the intended allowlisted set, but the intended set still must include a
6
+ * credential the child can USE to call its LLM — and any real provider key
7
+ * in the child is a reusable secret a prompt-injected worker could exfiltrate.
8
+ * The broker removes that requirement entirely: the child's ONLY credential
9
+ * is a per-worker token valid exclusively at this process's loopback proxy,
10
+ * which substitutes the real upstream key after the request leaves the
11
+ * worker. The real key never exists in any surface the worker can reach —
12
+ * env (S\* shadows it), auth.json (scoped agent dir has none), or process
13
+ * memory. Same pattern Docker Sandboxes/OpenShell sell as "credential
14
+ * substitution", self-hosted in-process (zero infrastructure).
15
+ *
16
+ * Model routing: the scoped agent dir's models.json overrides ONLY the
17
+ * built-in "anthropic" provider endpoint + apiKey (pi preserves built-in
18
+ * model metadata when a models.json provider entry supplies just baseUrl —
19
+ * docs/models.md "Configure a compatible endpoint"), so `--model
20
+ * anthropic/<id>` and bare `<id>` resolve exactly as before, but every
21
+ * request goes to 127.0.0.1:<port> with `x-api-key: $PI_CREW_INFERENCE_TOKEN`
22
+ * interpolated from the CHILD's env at request time. No model metadata is
23
+ * mirrored, no ids are rewritten, fallback chains inside anthropic work.
24
+ *
25
+ * Credential sources (parent side): ANTHROPIC_API_KEY (x-api-key upstream)
26
+ * or ANTHROPIC_AUTH_TOKEN (Authorization: Bearer upstream) — the documented
27
+ * env-var credential path (models.md "provide an API key through the
28
+ * provider's environment variable… useful in CI"). auth.json-only users are
29
+ * NOT supported by this PoC (reading the parent's auth.json into the broker
30
+ * is a separate, sensitive workstream). FAIL-CLOSED: PI_CREW_INFERENCE_BROKER=1
31
+ * with neither env var present THROWS at the first spawn — never silently
32
+ * falls back to passing real keys to workers.
33
+ *
34
+ * Enforcement: per-worker random 256-bit token; per-worker request budget
35
+ * (PI_CREW_INFERENCE_BUDGET_TOKENS, 0 = unlimited) counted from streamed
36
+ * Anthropic usage events; per-worker in-flight cap; revoke(workerKey) /
37
+ * revokeAll() kill-switch (revoked token → 401 on the NEXT request; in-flight
38
+ * requests are allowed to drain — killing sockets mid-stream corrupts
39
+ * transcripts without adding security, the token is already dead).
40
+ *
41
+ * Honest boundary: the broker protects the REUSABLE CREDENTIAL channel only.
42
+ * A worker can still read other host files (~/.ssh, ~/.aws, ~/.pi/agent/
43
+ * auth.json via bash) and make arbitrary network egress — those need an OS
44
+ * boundary (container/bwrap), which is a separate documented workstream.
45
+ *
46
+ * Version history: v0.11.30 (S\*) made env exact; THIS module removes the
47
+ * need for any real credential to be in the env at all.
48
+ */
49
+
50
+ import * as crypto from "node:crypto";
51
+ import * as fs from "node:fs";
52
+ import * as http from "node:http";
53
+ import * as path from "node:path";
54
+ import { Readable, Transform } from "node:stream";
55
+ import { getCrewEnv, getCrewEnvBool, getCrewEnvInt } from "../../config/env-vars.ts";
56
+ import { logInternalError } from "../../utils/internal-error.ts";
57
+ import { createSafeTempDir, getPiTempBase } from "../model/pi-args.ts";
58
+
59
+ // ── Env contract ─────────────────────────────────────────────────────────
60
+ // All child-side vars are control-namespace (PI_CREW_*) except
61
+ // PI_CODING_AGENT_DIR, which is pi's OWN native override (read by the SDK
62
+ // config loader) — it is injected at the spawn-options layer, never through
63
+ // built.env, so assertOnlyControlEnvKeys' contract is untouched.
64
+ export const INFERENCE_BROKER_ENABLE_ENV = "PI_CREW_INFERENCE_BROKER";
65
+ export const INFERENCE_TOKEN_ENV = "PI_CREW_INFERENCE_TOKEN";
66
+ export const INFERENCE_BUDGET_ENV = "PI_CREW_INFERENCE_BUDGET_TOKENS";
67
+ /** pi-native agent-dir override (SDK config.js ENV_AGENT_DIR) — set on the child. */
68
+ export const PI_AGENT_DIR_ENV = "PI_CODING_AGENT_DIR";
69
+
70
+ const DEFAULT_UPSTREAM = "https://api.anthropic.com";
71
+ const MAX_BODY_BYTES = 64 * 1024 * 1024; // generous: image attachments
72
+ const UPSTREAM_TIMEOUT_MS = 10 * 60 * 1000;
73
+ const MAX_INFLIGHT_PER_WORKER = 4;
74
+
75
+ /** Upstream headers we forward (client auth headers are NEVER forwarded). */
76
+ const FORWARD_REQUEST_HEADERS = new Set([
77
+ "content-type",
78
+ "accept",
79
+ "anthropic-version",
80
+ "anthropic-beta",
81
+ "user-agent",
82
+ "x-stainless-retry-count",
83
+ "x-stainless-timeout",
84
+ "x-request-id",
85
+ ]);
86
+ /** Response headers we must NOT copy (undici already decoded the body). */
87
+ const STRIP_RESPONSE_HEADERS = new Set(["content-encoding", "content-length", "transfer-encoding", "connection"]);
88
+
89
+ interface WorkerEntry {
90
+ workerKey: string;
91
+ token: string;
92
+ revoked: boolean;
93
+ requests: number;
94
+ inputTokens: number;
95
+ outputTokens: number;
96
+ inFlight: number;
97
+ maxTokens: number;
98
+ }
99
+
100
+ export interface InferenceBrokerSnapshot {
101
+ active: boolean;
102
+ baseUrl: string;
103
+ port: number;
104
+ scopedAgentDir: string | undefined;
105
+ }
106
+
107
+ interface BrokerState {
108
+ server: http.Server;
109
+ 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 };
113
+ workers: Map<string, WorkerEntry>; // by token
114
+ byKey: Map<string, WorkerEntry>; // by workerKey
115
+ scopedAgentDir: string;
116
+ }
117
+
118
+ // Module-level singleton (same pattern as the env-splice canary window).
119
+ // Started lazily by ensureInferenceBroker() from prepareSpawnContext;
120
+ // stopped by stopInferenceBroker() (extension teardown / tests).
121
+ let broker: BrokerState | undefined;
122
+
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;
129
+ }
130
+
131
+ /**
132
+ * Idempotent lazy start (ASYNC — http listen needs an event-loop turn; call
133
+ * sites: child-pi.ts run() BEFORE prepareSpawnContext, which stays sync).
134
+ * Returns the live snapshot, or `{active:false}` when the feature flag is
135
+ * off. FAIL-CLOSED: flag on + no upstream credential → THROWS (the run fails
136
+ * loudly; workers must never silently fall back to receiving real provider
137
+ * keys).
138
+ */
139
+ export async function maybeStartInferenceBroker(env: NodeJS.ProcessEnv = process.env): Promise<InferenceBrokerSnapshot> {
140
+ 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 };
143
+
144
+ const upstreamAuth = resolveUpstreamAuth();
145
+ if (!upstreamAuth) {
146
+ 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}.`,
148
+ );
149
+ }
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 };
153
+ }
154
+
155
+ /**
156
+ * SYNC guard for the spawn path: when the flag is on, the broker MUST already
157
+ * be running (child-pi.ts run() awaits maybeStartInferenceBroker() first).
158
+ * Direct prepareSpawnContext callers under an active flag without a started
159
+ * broker get a loud programming error instead of a keyless worker.
160
+ */
161
+ export function requireInferenceBrokerIfEnabled(): InferenceBrokerSnapshot {
162
+ 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 };
165
+ throw new Error(
166
+ `${INFERENCE_BROKER_ENABLE_ENV}=1 but the broker is not started — run() must await maybeStartInferenceBroker() before prepareSpawnContext()`,
167
+ );
168
+ }
169
+
170
+ /** Read-only view for spawn-option builders. Never starts anything. */
171
+ 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 };
174
+ }
175
+
176
+ async function createBrokerServer(
177
+ upstream: string,
178
+ upstreamAuth: { header: "x-api-key" | "authorization"; value: string },
179
+ ): Promise<BrokerState> {
180
+ const workers = new Map<string, WorkerEntry>();
181
+ const byKey = new Map<string, WorkerEntry>();
182
+
183
+ const server = http.createServer((req, res) => {
184
+ void handleRequest(req, res, { upstream, upstreamAuth, workers });
185
+ });
186
+ server.unref(); // never hold the parent's event loop open
187
+ await new Promise<void>((resolve, reject) => {
188
+ server.once("error", reject);
189
+ server.listen(0, "127.0.0.1", () => {
190
+ server.off("error", reject);
191
+ resolve();
192
+ });
193
+ });
194
+ const address = server.address();
195
+ if (!address || typeof address === "string") throw new Error("inference broker failed to bind a loopback port");
196
+ const port = address.port;
197
+
198
+ const scopedAgentDir = buildScopedAgentDir(port);
199
+
200
+ return { server, port, upstream, upstreamAuth, workers, byKey, scopedAgentDir };
201
+ }
202
+
203
+ /**
204
+ * Scoped agent dir for workers: models.json ONLY — no auth.json, no
205
+ * extensions, no skills, no settings. The single override reroutes the
206
+ * built-in anthropic provider to the broker with per-worker token
207
+ * interpolation at request time. Mode 0o600 file / 0o700 dir.
208
+ */
209
+ function buildScopedAgentDir(port: number): string {
210
+ const dir = createSafeTempDir(getPiTempBase(), "pi-crew-inference-");
211
+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
212
+ const models = {
213
+ $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
+ },
220
+ };
221
+ const file = path.join(dir, "models.json");
222
+ fs.writeFileSync(file, `${JSON.stringify(models, null, "\t")}\n`, { mode: 0o600 });
223
+ return dir;
224
+ }
225
+
226
+ /** Extract the caller token from x-api-key or Authorization: Bearer. */
227
+ function extractToken(req: http.IncomingMessage): string | undefined {
228
+ const apiKey = req.headers["x-api-key"];
229
+ if (typeof apiKey === "string" && apiKey.length > 0) return apiKey;
230
+ const auth = req.headers.authorization;
231
+ if (typeof auth === "string" && auth.startsWith("Bearer ")) return auth.slice("Bearer ".length);
232
+ return undefined;
233
+ }
234
+
235
+ function deny(res: http.ServerResponse, status: number, message: string): void {
236
+ const body = JSON.stringify({ type: "error", error: { type: "pi_crew_broker_error", message } });
237
+ res.writeHead(status, { "content-type": "application/json", "content-length": String(Buffer.byteLength(body)) });
238
+ res.end(body);
239
+ }
240
+
241
+ async function handleRequest(
242
+ req: http.IncomingMessage,
243
+ res: http.ServerResponse,
244
+ ctx: { upstream: string; upstreamAuth: { header: string; value: string }; workers: Map<string, WorkerEntry> },
245
+ ): Promise<void> {
246
+ try {
247
+ // (1) Auth — fail-closed, generic errors (never echo tokens).
248
+ const token = extractToken(req);
249
+ const worker = token ? ctx.workers.get(token) : undefined;
250
+ if (!worker || worker.revoked) {
251
+ deny(res, 401, "invalid or revoked inference token");
252
+ return;
253
+ }
254
+ // (2) Budget + concurrency (per worker).
255
+ if (worker.maxTokens > 0 && worker.inputTokens + worker.outputTokens >= worker.maxTokens) {
256
+ deny(res, 429, "worker inference token budget exhausted");
257
+ return;
258
+ }
259
+ if (worker.inFlight >= MAX_INFLIGHT_PER_WORKER) {
260
+ deny(res, 429, "too many concurrent inference requests for this worker");
261
+ return;
262
+ }
263
+ // (3) Read the request body (bounded).
264
+ const chunks: Buffer[] = [];
265
+ let size = 0;
266
+ for await (const chunk of req) {
267
+ size += (chunk as Buffer).length;
268
+ if (size > MAX_BODY_BYTES) {
269
+ deny(res, 413, "request body too large");
270
+ return;
271
+ }
272
+ chunks.push(chunk as Buffer);
273
+ }
274
+ const body = Buffer.concat(chunks);
275
+ // (4) Forward — client auth headers DROPPED, real key substituted.
276
+ const headers: Record<string, string> = {};
277
+ for (const [name, value] of Object.entries(req.headers)) {
278
+ if (!FORWARD_REQUEST_HEADERS.has(name.toLowerCase())) continue;
279
+ if (typeof value === "string") headers[name.toLowerCase()] = value;
280
+ }
281
+ headers[ctx.upstreamAuth.header] = ctx.upstreamAuth.value;
282
+ const upstreamRes = await fetch(`${ctx.upstream}${req.url ?? "/v1/messages"}`, {
283
+ // eslint-disable-next-line
284
+ method: req.method ?? "POST",
285
+ headers,
286
+ body: body.length > 0 ? body : undefined,
287
+ signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS),
288
+ });
289
+ // (5) Relay the response — strip transport framing, stream through,
290
+ // count usage from Anthropic SSE events / JSON bodies on the way.
291
+ const outHeaders: Record<string, string> = {};
292
+ upstreamRes.headers.forEach((value, name) => {
293
+ if (!STRIP_RESPONSE_HEADERS.has(name.toLowerCase())) outHeaders[name.toLowerCase()] = value;
294
+ });
295
+ res.writeHead(upstreamRes.status, outHeaders);
296
+ worker.requests += 1;
297
+ const usage = new UsageCounter();
298
+ const finish = () => {
299
+ worker.inFlight -= 1;
300
+ worker.inputTokens += usage.input;
301
+ worker.outputTokens += usage.output;
302
+ };
303
+ res.on("close", finish);
304
+ if (!upstreamRes.body) {
305
+ res.end();
306
+ return;
307
+ }
308
+ const nodeStream = Readable.fromWeb(upstreamRes.body as import("node:stream/web").ReadableStream);
309
+ nodeStream.on("error", (e) => {
310
+ console.error("[ib-debug] nodeStream error:", e?.message);
311
+ res.destroy();
312
+ });
313
+ nodeStream.pipe(usage.tee).pipe(res);
314
+ } catch (error) {
315
+ logInternalError("inference-broker.forward", undefined, error instanceof Error ? error.message : String(error), "warn");
316
+ if (!res.headersSent) deny(res, 502, "inference broker upstream failure");
317
+ else res.destroy();
318
+ }
319
+ }
320
+
321
+ /**
322
+ * Count Anthropic usage from a streamed response without buffering it:
323
+ * SSE `message_start` carries usage.input_tokens; `message_delta` carries
324
+ * usage.output_tokens (cumulative — take max). Non-stream JSON bodies carry a
325
+ * top-level usage object (also scanned — `data:`-less lines are parsed
326
+ * defensively). `message_start` nests usage under evt.message.usage.
327
+ */
328
+ class UsageCounter {
329
+ input = 0;
330
+ output = 0;
331
+ private leftover = "";
332
+ // NB: named `tee` — a field named `transform` would SHADOW the Transform
333
+ // base class method slot and silently break piping.
334
+ readonly tee = new Transform({
335
+ transform: (chunk: Buffer, _enc: string, cb: (e: Error | null, d?: Buffer) => void) => {
336
+ this.scan(chunk);
337
+ cb(null, chunk);
338
+ },
339
+ });
340
+
341
+ private scan(chunk: Buffer): void {
342
+ const text = this.leftover + chunk.toString("utf8");
343
+ const lines = text.split("\n");
344
+ this.leftover = lines.pop() ?? "";
345
+ for (const line of lines) {
346
+ const trimmed = line.trim();
347
+ const payload = trimmed.startsWith("data:") ? trimmed.slice(5).trim() : trimmed.startsWith("{") ? trimmed : "";
348
+ if (!payload || payload === "[DONE]") continue;
349
+ try {
350
+ const evt = JSON.parse(payload);
351
+ // SSE message_start: usage nested under evt.message.usage.
352
+ // SSE message_delta: evt.usage.output_tokens is CUMULATIVE (take max).
353
+ // Non-stream JSON body: top-level evt.usage, both absolute.
354
+ const usage = evt?.message?.usage ?? evt?.usage;
355
+ 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);
359
+ } catch {
360
+ // non-JSON line — ignore
361
+ }
362
+ }
363
+ }
364
+ }
365
+
366
+ /**
367
+ * Issue a per-worker token. Re-registering the same workerKey REPLACES the
368
+ * old token (old one dies immediately) — retries of one task must never
369
+ * accumulate live tokens.
370
+ */
371
+ export function registerInferenceWorker(workerKey: string): string {
372
+ if (!broker) throw new Error("inference broker not started");
373
+ const existing = broker.byKey.get(workerKey);
374
+ if (existing) {
375
+ existing.revoked = true;
376
+ broker.workers.delete(existing.token);
377
+ broker.byKey.delete(workerKey);
378
+ }
379
+ const token = crypto.randomBytes(32).toString("hex");
380
+ const maxTokens = getCrewEnvInt(INFERENCE_BUDGET_ENV) ?? 0;
381
+ const entry: WorkerEntry = { workerKey, token, revoked: false, requests: 0, inputTokens: 0, outputTokens: 0, inFlight: 0, maxTokens };
382
+ broker.workers.set(token, entry);
383
+ broker.byKey.set(workerKey, entry);
384
+ return token;
385
+ }
386
+
387
+ /** Kill-switch for one worker: its token dies on the NEXT request. */
388
+ export function revokeInferenceWorker(workerKey: string): boolean {
389
+ if (!broker) return false;
390
+ const entry = broker.byKey.get(workerKey);
391
+ if (!entry) return false;
392
+ entry.revoked = true;
393
+ broker.workers.delete(entry.token);
394
+ broker.byKey.delete(workerKey);
395
+ return true;
396
+ }
397
+
398
+ export function revokeAllInferenceWorkers(): number {
399
+ if (!broker) return 0;
400
+ const n = broker.byKey.size;
401
+ for (const entry of broker.byKey.values()) entry.revoked = true;
402
+ broker.workers.clear();
403
+ broker.byKey.clear();
404
+ return n;
405
+ }
406
+
407
+ /** Redacted per-worker stats (never includes tokens). */
408
+ export function inferenceBrokerStats(): Array<{ workerKey: string; requests: number; inputTokens: number; outputTokens: number }> {
409
+ if (!broker) return [];
410
+ return [...broker.byKey.values()].map((e) => ({
411
+ workerKey: e.workerKey,
412
+ requests: e.requests,
413
+ inputTokens: e.inputTokens,
414
+ outputTokens: e.outputTokens,
415
+ }));
416
+ }
417
+
418
+ /** Stop the broker: revoke every token, close the server, remove the scoped dir. */
419
+ export function stopInferenceBroker(): void {
420
+ if (!broker) return;
421
+ revokeAllInferenceWorkers();
422
+ broker.server.close();
423
+ try {
424
+ fs.rmSync(broker.scopedAgentDir, { recursive: true, force: true });
425
+ } catch {
426
+ // best-effort cleanup (tracked temp cleaner will catch it)
427
+ }
428
+ broker = undefined;
429
+ }
430
+
431
+ /** @internal Reset module state — TESTS ONLY (never call in production paths). */
432
+ export function __resetInferenceBrokerForTests(): void {
433
+ stopInferenceBroker();
434
+ }