openzoo 0.50.38 → 0.50.40

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/lib/proxy.js CHANGED
@@ -7,6 +7,7 @@ import { Readable } from 'node:stream';
7
7
  import {
8
8
  config, FUNDING_ASSETS, EVM_FUNDING_ASSETS, evmRpcFor, fundingLine, liveRails, railFundingHint, railFundingAddresses, unfundableRails, RAIL_FUNDING,
9
9
  } from './config.js';
10
+ import { execSync } from 'node:child_process';
10
11
  import { PayClient, QuoteTooHighError, UnderfundedError } from './pay.js';
11
12
  import { withOnrampLink } from './stripeOnramp.js';
12
13
  import { tokenBalance } from './x402.js';
@@ -37,6 +38,21 @@ import { receiptUsedCogs, receiptDirectUsd, pairActualBilled } from './racesettl
37
38
  import { fetchHeaders } from './fetch.js';
38
39
  import { attachX402Proof } from './spendProof.js';
39
40
 
41
+ /** Kill whatever is LISTEN on this port except this process. */
42
+ export function killListen(port, run = execSync) {
43
+ try {
44
+ const pids = run(`lsof -nP -iTCP:${Number(port)} -sTCP:LISTEN -t`, {
45
+ encoding: 'utf8', timeout: 2000,
46
+ }).trim().split('\n').map(Number).filter((n) => Number.isInteger(n) && n > 0 && n !== process.pid);
47
+ for (const pid of pids) {
48
+ try { run(`kill ${pid}`, { stdio: 'ignore', timeout: 2000 }); } catch { /* already gone */ }
49
+ }
50
+ return pids;
51
+ } catch {
52
+ return [];
53
+ }
54
+ }
55
+
40
56
  // THE SHIM IS A FACILITATOR, NOT A MIDDLEBOX.
41
57
  //
42
58
  // Everything that used to rewrite the request on the way through — model-id
@@ -817,9 +833,8 @@ export async function startProxy({ silent = false, requireToken = null, sessionM
817
833
  const bindHost = process.env.OPENZOO_BIND || '127.0.0.1';
818
834
  // SELF-HEAL A TAKEN PORT. Walk up to the next free port instead of dying;
819
835
  // the caller reads config.port back out, so every URL printed afterwards is
820
- // the one we actually bound. EXCEPTION: if the thing already on the port is
821
- // a HEALTHY openzoo proxy, reuse it rather than starting a rival that splits
822
- // spend across two wallets.
836
+ // the one we actually bound. A healthy proxy already on the port is KILLED
837
+ // — reusing it left a stale PayClient serving $0 after a TOKEN top-up.
823
838
  const wanted = config.port;
824
839
  for (let attempt = 0; ; attempt++) {
825
840
  try {
@@ -832,13 +847,12 @@ export async function startProxy({ silent = false, requireToken = null, sessionM
832
847
  } catch (e) {
833
848
  if (e?.code !== 'EADDRINUSE' || attempt >= 12) throw e;
834
849
  if (attempt === 0) {
835
- try {
836
- const probe = await fetch(`http://127.0.0.1:${config.port}/v1/models`, { signal: AbortSignal.timeout(2500) });
837
- if (probe.ok) {
838
- say(`openzoo: a healthy proxy is already on :${config.port} — reusing it`);
839
- return { server: null, client, reused: true, port: config.port, spent: () => 0, publicUrl: null, tunnelToken: null, tunnelError: null };
840
- }
841
- } catch { /* not ours, or wedged — take the next port */ }
850
+ const pids = killListen(config.port);
851
+ if (pids.length) {
852
+ say(`openzoo: killed proxy on :${config.port} (pids ${pids.join(',')})`);
853
+ await new Promise((r) => setTimeout(r, 400));
854
+ continue;
855
+ }
842
856
  }
843
857
  config.port += 1;
844
858
  say(`openzoo: :${config.port - 1} busy — trying :${config.port}`);
package/lib/xb ADDED
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openzoo",
3
- "version": "0.50.38",
3
+ "version": "0.50.40",
4
4
  "description": "Local x402-paying proxy + MCP server for openzoo.fun — point any OpenAI-compatible harness (Cursor, Claude Code, aider, SDKs) at localhost and it pays per call from a local burner wallet. Solana and Base rails live; Robinhood experimental.",
5
5
  "license": "MIT",
6
6
  "type": "module",