billion-context 0.1.140 → 0.1.141

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.
@@ -43469,6 +43469,8 @@ ${captureLines}` : capture2.stack;
43469
43469
  // src/claude-native-bootstrap.ts
43470
43470
  import { fileURLToPath as fileURLToPath4 } from "url";
43471
43471
  import path16 from "path";
43472
+ import { readFileSync as readFileSync2 } from "fs";
43473
+ import { execFileSync as execFileSync4 } from "child_process";
43472
43474
 
43473
43475
  // src/launcher.ts
43474
43476
  import { randomUUID as randomUUID2 } from "crypto";
@@ -47360,21 +47362,138 @@ function planClaudeNativeBootstrap(env) {
47360
47362
  if (!nativeBootstrapGate(env, "BILI_NATIVE_CLAUDE")) return { action: "exit", port };
47361
47363
  return { action: "start", port };
47362
47364
  }
47365
+ var CLAUDE_HOST_MAX_WALK = 8;
47366
+ function defaultExec(cmd, args) {
47367
+ try {
47368
+ return execFileSync4(cmd, args, { encoding: "utf8", timeout: 5e3 });
47369
+ } catch {
47370
+ return null;
47371
+ }
47372
+ }
47373
+ function readProcInfo(pid) {
47374
+ let argv = null;
47375
+ let ppid = null;
47376
+ try {
47377
+ const parts = readFileSync2(`/proc/${pid}/cmdline`, "utf8").split("\0").filter((p2) => p2.length > 0);
47378
+ if (parts.length > 0) argv = parts;
47379
+ } catch {
47380
+ return null;
47381
+ }
47382
+ try {
47383
+ const stat = readFileSync2(`/proc/${pid}/stat`, "utf8");
47384
+ const tail = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
47385
+ if (tail.length > 1) ppid = Number(tail[1]);
47386
+ } catch {
47387
+ }
47388
+ return { argv, ppid };
47389
+ }
47390
+ function readPsProcInfo(pid, exec = defaultExec) {
47391
+ const out = exec("ps", ["-ww", "-o", "ppid=", "-o", "args=", "-p", String(pid)]);
47392
+ if (out === null) return null;
47393
+ const line = out.split("\n").find((l) => l.trim().length > 0);
47394
+ if (line === void 0) return null;
47395
+ const [ppidToken, ...rest] = line.trim().split(/\s+/);
47396
+ const ppid = Number(ppidToken);
47397
+ return { argv: rest.length > 0 ? rest : null, ppid: Number.isFinite(ppid) ? ppid : null };
47398
+ }
47399
+ function readWinProcInfo(pid, exec = defaultExec) {
47400
+ const out = exec("powershell", [
47401
+ "-NoProfile",
47402
+ "-Command",
47403
+ `(Get-CimInstance Win32_Process -Filter 'ProcessId=${pid}') | ForEach-Object { "$($_.ParentProcessId)\`t$($_.CommandLine)" }`
47404
+ ]);
47405
+ if (out === null) return null;
47406
+ const line = out.split("\n").find((l) => l.trim().length > 0);
47407
+ if (line === void 0) return null;
47408
+ const tab = line.indexOf(" ");
47409
+ if (tab < 0) return null;
47410
+ const ppid = Number(line.slice(0, tab).trim());
47411
+ const argv = line.slice(tab + 1).trim().split(/\s+/).filter((part) => part.length > 0);
47412
+ return { argv: argv.length > 0 ? argv : null, ppid: Number.isFinite(ppid) ? ppid : null };
47413
+ }
47414
+ function defaultProcReader() {
47415
+ if (process.platform === "win32") return (pid) => readWinProcInfo(pid);
47416
+ if (process.platform === "linux") return readProcInfo;
47417
+ return (pid) => readPsProcInfo(pid);
47418
+ }
47419
+ function isClaudeHostArgv(argv) {
47420
+ const base = (p2) => {
47421
+ const parts = p2.split(/[\\/]/).filter((seg) => seg.length > 0);
47422
+ return parts[parts.length - 1] ?? "";
47423
+ };
47424
+ if (/^claude(\.exe)?$/i.test(base(argv[0] ?? ""))) return true;
47425
+ if (/^(node|bun|deno)(\.exe)?$/i.test(base(argv[0] ?? ""))) {
47426
+ return argv.slice(1).some((arg) => /^claude(\.exe)?$/i.test(base(arg)) || /@anthropic-ai[\\/]claude-code/.test(arg));
47427
+ }
47428
+ return false;
47429
+ }
47430
+ function resolveClaudeHostPid(opts = {}) {
47431
+ const read = opts.read ?? defaultProcReader();
47432
+ let pid = opts.startPid ?? process.pid;
47433
+ for (let hop = 0; hop < CLAUDE_HOST_MAX_WALK; hop++) {
47434
+ const info = read(pid);
47435
+ if (info === null) return void 0;
47436
+ if (info.argv !== null && isClaudeHostArgv(info.argv)) return pid;
47437
+ if (info.ppid === null || info.ppid <= 1) return void 0;
47438
+ pid = info.ppid;
47439
+ }
47440
+ return void 0;
47441
+ }
47442
+ function isTransientShArgv(argv) {
47443
+ const parts = (argv[0] ?? "").split(/[\\/]/).filter((seg) => seg.length > 0);
47444
+ const shell = parts[parts.length - 1] ?? "";
47445
+ if (/^(sh|bash|dash|zsh|ksh|ash)(\.exe)?$/i.test(shell)) {
47446
+ return argv.some((arg, i) => i > 0 && /^-[^-]*c$/.test(arg));
47447
+ }
47448
+ if (/^cmd(\.exe)?$/i.test(shell)) {
47449
+ return argv.some((arg, i) => i > 0 && /^[-/]c$/i.test(arg));
47450
+ }
47451
+ if (/^(powershell|pwsh)(\.exe)?$/i.test(shell)) {
47452
+ return argv.some((arg, i) => i > 0 && /^-?(command|encodedcommand)$/i.test(arg));
47453
+ }
47454
+ return false;
47455
+ }
47456
+ function chooseWatchdogParentPid(opts = {}) {
47457
+ const read = opts.read ?? defaultProcReader();
47458
+ const host = resolveClaudeHostPid({ read });
47459
+ if (host !== void 0) return host;
47460
+ const parentPid = opts.parentPid ?? process.ppid;
47461
+ const parentInfo = read(parentPid);
47462
+ if (parentInfo !== null && parentInfo.argv !== null && isTransientShArgv(parentInfo.argv)) {
47463
+ const grandPid = parentInfo.ppid;
47464
+ if (grandPid !== null && grandPid > 1) return grandPid;
47465
+ }
47466
+ return parentPid;
47467
+ }
47363
47468
  async function run() {
47364
47469
  const plan = planClaudeNativeBootstrap(process.env);
47365
47470
  if (plan.action === "exit") return;
47366
47471
  try {
47472
+ const watchPid = chooseWatchdogParentPid();
47367
47473
  const handle = await ensureProxyRunning(
47368
47474
  {
47369
47475
  host: LAUNCHER_DEFAULT_HOST,
47370
47476
  port: plan.port,
47371
47477
  passthrough: plan.action === "passthrough",
47372
47478
  debug: false,
47373
- parentPid: process.ppid,
47479
+ parentPid: watchPid,
47374
47480
  strictPort: true
47375
47481
  },
47376
47482
  { scriptPath: proxyScriptPath() }
47377
47483
  );
47484
+ if (handle.attached) {
47485
+ try {
47486
+ const res = await fetch(`${handle.origin}/__bili/watcher`, {
47487
+ method: "POST",
47488
+ headers: { "content-type": "application/json" },
47489
+ body: JSON.stringify({ pid: watchPid }),
47490
+ signal: AbortSignal.timeout(2e3)
47491
+ });
47492
+ if (!res.ok && res.status !== 409) log2(`watcher registration returned HTTP ${res.status} \u2014 the shared proxy may exit when its first owner does`);
47493
+ } catch (err) {
47494
+ log2(`watcher registration failed \u2014 the shared proxy may exit when its first owner does (${err instanceof Error ? err.message : String(err)})`);
47495
+ }
47496
+ }
47378
47497
  log2(`proxy ${handle.attached ? "attached" : "started"} at ${handle.origin}${plan.action === "passthrough" ? " (passthrough \u2014 compression off)" : ""}`);
47379
47498
  } catch (err) {
47380
47499
  log2(
@@ -47393,7 +47512,13 @@ if (process.argv[1] && /claude-native-bootstrap\.(?:ts|js)$/.test(process.argv[1
47393
47512
  hookMain();
47394
47513
  }
47395
47514
  export {
47396
- planClaudeNativeBootstrap
47515
+ chooseWatchdogParentPid,
47516
+ isClaudeHostArgv,
47517
+ isTransientShArgv,
47518
+ planClaudeNativeBootstrap,
47519
+ readPsProcInfo,
47520
+ readWinProcInfo,
47521
+ resolveClaudeHostPid
47397
47522
  };
47398
47523
  /*! Bundled license information:
47399
47524