shinjuku-shield 0.3.222 → 0.3.223

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/README.md CHANGED
@@ -5,12 +5,12 @@ web services and AI answers from your private balance, so the payment does
5
5
  not show who paid. It works inside your AI app (Claude, Cursor, Codex) and in
6
6
  a terminal. Only you hold the keys: they never leave your computer.
7
7
 
8
- This is version 0.3.222. The command is `shinjuku`.
8
+ This is version 0.3.223. The command is `shinjuku`.
9
9
 
10
10
  ## Set up
11
11
 
12
12
  ```sh
13
- npx -y shinjuku@0.3.222 setup
13
+ npx -y shinjuku@0.3.223 setup
14
14
  ```
15
15
 
16
16
  Setup does every step for you:
@@ -100,7 +100,7 @@ Add it yourself. The name of the wallet in your app is `shinjuku`.
100
100
  Claude Code:
101
101
 
102
102
  ```sh
103
- claude mcp add --scope user shinjuku -- npx -y --no-audit --no-fund shinjuku-shield@0.3.222 mcp
103
+ claude mcp add --scope user shinjuku -- npx -y --no-audit --no-fund shinjuku-shield@0.3.223 mcp
104
104
  ```
105
105
 
106
106
  ## For builders: check what you install
@@ -116,7 +116,7 @@ with the Releases table in
116
116
  Versions are published from GitHub Actions with npm provenance.
117
117
 
118
118
  `shinjuku-shield` is the package that holds the wallet. `shinjuku-shielded`
119
- 0.3.222, its older name, runs this package at the same version, so wallets
119
+ 0.3.223, its older name, runs this package at the same version, so wallets
120
120
  installed under that name keep updating. New installs use `shinjuku`.
121
121
 
122
122
  The full guide is
package/manifest.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "schema": "carbon-shielded-wallet/bundle-manifest/v1",
3
3
  "file": "shinjuku-wallet.mjs",
4
- "sha256": "a2402ed74f220f3e905e948b89b40c954b82b7dfe205a190256513a3b530976a",
5
- "bytes": 10301256,
6
- "sourceCommit": "af208b57f4e1150cd0770d9cbfdc232c90bf7372",
4
+ "sha256": "91d53b343519d56d1c1dc52e5418d8647f07822009ba8e42db9e3f1007660353",
5
+ "bytes": 10306995,
6
+ "sourceCommit": "69056ae8c7924fa78e539f36092963f2c2a7a606",
7
7
  "sourceDirty": false,
8
8
  "installRecordMatchesLockfile": true,
9
9
  "releaseRule": "valid only when two fresh npm ci --ignore-scripts builds on different OSes give the same sha256; installRecordMatchesLockfile true means every entry npm installed equals its package-lock.json entry and every entry not marked optional, devOptional included, is installed: an optional entry (a package for another platform) may be absent",
@@ -1075,8 +1075,8 @@
1075
1075
  ],
1076
1076
  "seller": {
1077
1077
  "file": "shinjuku-seller.mjs",
1078
- "sha256": "0278f5d89c7b8a73482c5e156f7c28a96ece6b4746aca02626447e79cd55d815",
1079
- "bytes": 4184815,
1078
+ "sha256": "f23a0db92725b1a8f4cbbd65089f08c32b82cf7e31fec14ef0ec08685aaf41e1",
1079
+ "bytes": 4187011,
1080
1080
  "entry": "wallet/src/seller/exact/index.mjs",
1081
1081
  "external": [
1082
1082
  "@x402/core/http",
@@ -1968,5 +1968,5 @@
1968
1968
  }
1969
1969
  ]
1970
1970
  },
1971
- "publishedAt": "2026-11-12T16:40:00.515Z"
1971
+ "publishedAt": "2026-11-12T17:36:48.817Z"
1972
1972
  }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "shinjuku-shield",
3
- "version": "0.3.222",
3
+ "version": "0.3.223",
4
4
  "mcpName": "io.github.ShinjukuStaition/shinjuku-shield",
5
5
  "description": "Shinjuku Shield, private x402 payments on Solana: the self-custody wallet CLI and its MCP server. Shield, pay, and unshield USDC from any MCP agent, under caps you set.",
6
- "shinjukuRelease": "a2402ed7",
7
- "shinjukuSha256": "a2402ed74f220f3e905e948b89b40c954b82b7dfe205a190256513a3b530976a",
6
+ "shinjukuRelease": "91d53b34",
7
+ "shinjukuSha256": "91d53b343519d56d1c1dc52e5418d8647f07822009ba8e42db9e3f1007660353",
8
8
  "license": "SEE LICENSE IN LICENSE.txt",
9
9
  "author": "Shinjuku Shield",
10
10
  "homepage": "https://github.com/ShinjukuStaition/shinjuku-shield#readme",
@@ -34889,9 +34889,11 @@ function tunnelClient(Dispatcher, proxyPort, proxyHost) {
34889
34889
  async connect(params) {
34890
34890
  const target = String(params?.path ?? "");
34891
34891
  if (optimisticTarget(target)) return optimisticConnect(params, { proxyHost, proxyPort, closedInRow });
34892
+ const startedAt = Date.now();
34892
34893
  try {
34893
34894
  const answer = await direct.connect(params);
34894
34895
  closedInRow.delete(target);
34896
+ if (answer?.socket && typeof answer.socket === "object") tunnelConnectMs.set(answer.socket, Date.now() - startedAt);
34895
34897
  return answer;
34896
34898
  } catch (error) {
34897
34899
  if (error?.code !== "UND_ERR_SOCKET") throw error;
@@ -35142,13 +35144,15 @@ ${Object.entries(params.headers ?? {}).filter(([name, value2]) => value2 !== voi
35142
35144
  throw error;
35143
35145
  });
35144
35146
  }
35145
- var circuitProbe, RUN_MARK, TUNNEL_CLOSED_TRIES, REPLAY_LIMIT;
35147
+ var circuitProbe, RUN_MARK, tunnelConnectMs, tunnelConnectMsOf, TUNNEL_CLOSED_TRIES, REPLAY_LIMIT;
35146
35148
  var init_tunnelGuard = __esm({
35147
35149
  "wallet/src/net/tunnelGuard.mjs"() {
35148
35150
  "use strict";
35149
35151
  init_keepAlive();
35150
35152
  init_phaseTiming();
35151
35153
  RUN_MARK = "SHINJUKU_WALLET_PROXIED";
35154
+ tunnelConnectMs = /* @__PURE__ */ new WeakMap();
35155
+ tunnelConnectMsOf = (socket) => socket && typeof socket === "object" ? tunnelConnectMs.get(socket) : void 0;
35152
35156
  TUNNEL_CLOSED_TRIES = 3;
35153
35157
  REPLAY_LIMIT = 8 * 1024;
35154
35158
  }
@@ -35247,7 +35251,7 @@ function untilAborted(promise, signal) {
35247
35251
  promise.then(resolve18, reject).finally(() => signal.removeEventListener("abort", onAbort));
35248
35252
  });
35249
35253
  }
35250
- var SITE_ONIONS, SITE_SPARE_ONIONS, PINNED_FACILITATOR_ONIONS, PINNED_SPARE_ONIONS, HISTORY_ONIONS, HISTORY_SPARE_ONIONS, PINNED_HISTORY_ONIONS, PINNED_HISTORY_SPARE_ONIONS, KEPT_ANSWER_BYTES, streamScope, requestStreams, socketRequests, guarded, STREAM_CHANNELS, SPARE_CAUSE, routedOrigins, routedGroupOf, answeredOn, onionDispatchers, ownTag, ownTagDispatchers, onOwnTagNow, ownTagInit;
35254
+ var SITE_ONIONS, SITE_SPARE_ONIONS, PINNED_FACILITATOR_ONIONS, PINNED_SPARE_ONIONS, HISTORY_ONIONS, HISTORY_SPARE_ONIONS, PINNED_HISTORY_ONIONS, PINNED_HISTORY_SPARE_ONIONS, KEPT_ANSWER_BYTES, streamScope, requestStreams, socketRequests, guarded, STREAM_CHANNELS, SPARE_CAUSE, routedOrigins, routedGroupOf, answeredOn, tagRoutes, isKey, noteRoute, TAG_LETTERS, onionDispatchers, ownTag, ownTagDispatchers, onOwnTagNow, ownTagInit;
35251
35255
  var init_facilitatorOnion = __esm({
35252
35256
  "wallet/src/net/facilitatorOnion.mjs"() {
35253
35257
  "use strict";
@@ -35306,12 +35310,27 @@ var init_facilitatorOnion = __esm({
35306
35310
  if (!stream || stream.request !== request || stream.sentAt !== void 0) return;
35307
35311
  stream.sentAt = Date.now();
35308
35312
  stream.kind = !known ? "unknown" : sent === 1 ? "new" : "reused";
35313
+ if (stream.kind === "new") stream.connectMs = tunnelConnectMsOf(socket);
35314
+ if (known && Number.isInteger(socket.localPort)) stream.localPort = socket.localPort;
35315
+ if (stream.route) noteRoute(stream.route.dispatcher, stream.route.onion);
35309
35316
  })
35310
35317
  });
35311
35318
  SPARE_CAUSE = Object.freeze({ silent: "no pinned onion answered", failed: "every pinned probe failed", wait: "a request's route wait ended" });
35312
35319
  routedOrigins = /* @__PURE__ */ new Map();
35313
35320
  routedGroupOf = /* @__PURE__ */ new Map();
35314
35321
  answeredOn = /* @__PURE__ */ new WeakMap();
35322
+ tagRoutes = /* @__PURE__ */ new WeakMap();
35323
+ isKey = (value2) => value2 !== null && (typeof value2 === "object" || typeof value2 === "function");
35324
+ noteRoute = (dispatcher, onion) => {
35325
+ try {
35326
+ if (!isKey(dispatcher) || typeof onion !== "string") return;
35327
+ let onions = tagRoutes.get(dispatcher);
35328
+ if (!onions) tagRoutes.set(dispatcher, onions = /* @__PURE__ */ new Set());
35329
+ onions.add(onion);
35330
+ } catch {
35331
+ }
35332
+ };
35333
+ TAG_LETTERS = Object.freeze({ run: "r", own: "o", given: "g", none: "n" });
35315
35334
  onionDispatchers = /* @__PURE__ */ new Map();
35316
35335
  ownTag = new AsyncLocalStorage2();
35317
35336
  ownTagDispatchers = /* @__PURE__ */ new Set();
@@ -79827,7 +79846,7 @@ AUTHCHALLENGE SAFECOOKIE ${clientNonce.toString("hex")}\r
79827
79846
  }
79828
79847
  if (step === 2 && typeof events === "function") {
79829
79848
  step = 6;
79830
- socket.write("SETEVENTS HS_DESC CIRC\r\n");
79849
+ socket.write("SETEVENTS HS_DESC CIRC STREAM\r\n");
79831
79850
  continue;
79832
79851
  }
79833
79852
  if (step === 6) {
@@ -80522,7 +80541,7 @@ setTunnelCircuitProbe(async ({ proxyHost, proxyPort } = {}) => {
80522
80541
  return answer && answer.tunnelPort === Number(proxyPort) ? answer.circuits : void 0;
80523
80542
  });
80524
80543
  var EVENT_LINE_MAX = 4096;
80525
- var TOR_TIMING_MAX = 200;
80544
+ var TOR_TIMING_MAX = 800;
80526
80545
  var HS_DESC_ACTIONS = /* @__PURE__ */ new Set(["REQUESTED", "RECEIVED", "FAILED"]);
80527
80546
  var HS_DESC_REASONS = /* @__PURE__ */ new Set([
80528
80547
  "BAD_DESC",
@@ -80562,6 +80581,7 @@ var CIRC_REASONS = /* @__PURE__ */ new Set([
80562
80581
  "MEASUREMENT_EXPIRED",
80563
80582
  "IP_NOW_REDUNDANT"
80564
80583
  ]);
80584
+ var STREAM_STATUSES = /* @__PURE__ */ new Set(["NEW", "SENTCONNECT", "SUCCEEDED", "FAILED", "CLOSED", "DETACHED"]);
80565
80585
  function ownOnionPrefixes() {
80566
80586
  const all = [
80567
80587
  ...FACILITATOR_SITE_ONIONS,
@@ -80585,6 +80605,13 @@ function torTimingMark(line, ours) {
80585
80605
  if (!prefix2 || !HS_DESC_ACTIONS.has(desc[1]) || reason2 !== void 0 && !HS_DESC_REASONS.has(reason2)) return void 0;
80586
80606
  return { phase: `hs-desc-${desc[1].toLowerCase()}`, outcome: `onion_${prefix2}${reason2 ? `_${reason2.toLowerCase()}` : ""}` };
80587
80607
  }
80608
+ const stream = /^650 STREAM (\d{1,20}) ([A-Z_]+) (\d{1,10}) ([a-z2-7]{56})\.onion:\d{1,5}(?: |$)/.exec(line);
80609
+ if (stream) {
80610
+ const prefix2 = ours.get(stream[4]);
80611
+ if (!prefix2 || !STREAM_STATUSES.has(stream[2])) return void 0;
80612
+ const port = stream[2] === "NEW" ? /(?:^| )SOURCE_ADDR=127\.0\.0\.1:(\d{1,5})(?: |$)/.exec(line)?.[1] : void 0;
80613
+ return { phase: `stream-${stream[2].toLowerCase()}`, outcome: `onion_${prefix2}_s${stream[1]}_c${stream[3]}${port ? `_lp${port}` : ""}` };
80614
+ }
80588
80615
  const circ = /^650 CIRC (\d{1,10}) ([A-Z_]+) /.exec(`${line} `);
80589
80616
  if (!circ || !CIRC_STATUSES.has(circ[2])) return void 0;
80590
80617
  const rend = /(?:^| )REND_QUERY=([a-z2-7]{56})(?: |$)/.exec(line)?.[1];
@@ -80597,7 +80624,7 @@ function torTimingMark(line, ours) {
80597
80624
  }
80598
80625
  function startTorTimingMarks() {
80599
80626
  const run2 = String(process.env.SHINJUKU_WALLET_PROXIED ?? "");
80600
- if (!run2.startsWith("tor ") || !process.argv.includes("--verbose")) return;
80627
+ if (!run2.startsWith("tor ") || !(process.argv.includes("--verbose") || process.argv[2] === "mcp")) return;
80601
80628
  setImmediate(async () => {
80602
80629
  try {
80603
80630
  const home = setupHome(process.env);
@@ -80613,17 +80640,34 @@ function startTorTimingMarks() {
80613
80640
  const ours = ownOnionPrefixes();
80614
80641
  const mark = createTimingLog(join24(home, pool2, "wallet.json.timing.log"), { component: "tor" }).start({ startedAt: Date.now() - performance.now() });
80615
80642
  let marks = 0;
80643
+ let session;
80644
+ let spent = false;
80645
+ const closeSession = () => {
80646
+ try {
80647
+ session?.close();
80648
+ } catch {
80649
+ }
80650
+ };
80616
80651
  const once2 = (phase, outcome) => {
80617
80652
  if (marks >= TOR_TIMING_MAX) return;
80618
80653
  marks += 1;
80619
80654
  mark(phase, outcome);
80655
+ if (marks >= TOR_TIMING_MAX) {
80656
+ spent = true;
80657
+ closeSession();
80658
+ }
80620
80659
  };
80621
- const session = await controlCheck(home, state2, { unref: true, events: (line) => {
80660
+ session = await controlCheck(home, state2, { unref: true, events: (line) => {
80661
+ if (spent) return;
80622
80662
  const one = torTimingMark(line, ours);
80623
80663
  if (one) once2(one.phase, one.outcome);
80624
80664
  } });
80625
80665
  if (!session) return;
80626
- process.once("exit", () => session.close());
80666
+ if (spent) {
80667
+ closeSession();
80668
+ return;
80669
+ }
80670
+ process.once("exit", closeSession);
80627
80671
  once2("events-on");
80628
80672
  } catch {
80629
80673
  }
@@ -43797,9 +43797,11 @@ function tunnelClient(Dispatcher, proxyPort, proxyHost) {
43797
43797
  async connect(params) {
43798
43798
  const target = String(params?.path ?? "");
43799
43799
  if (optimisticTarget(target)) return optimisticConnect(params, { proxyHost, proxyPort, closedInRow });
43800
+ const startedAt = Date.now();
43800
43801
  try {
43801
43802
  const answer = await direct.connect(params);
43802
43803
  closedInRow.delete(target);
43804
+ if (answer?.socket && typeof answer.socket === "object") tunnelConnectMs.set(answer.socket, Date.now() - startedAt);
43803
43805
  return answer;
43804
43806
  } catch (error) {
43805
43807
  if (error?.code !== "UND_ERR_SOCKET") throw error;
@@ -44050,13 +44052,15 @@ ${Object.entries(params.headers ?? {}).filter(([name, value2]) => value2 !== voi
44050
44052
  throw error;
44051
44053
  });
44052
44054
  }
44053
- var circuitProbe, RUN_MARK, TUNNEL_CLOSED_TRIES, REPLAY_LIMIT;
44055
+ var circuitProbe, RUN_MARK, tunnelConnectMs, tunnelConnectMsOf, TUNNEL_CLOSED_TRIES, REPLAY_LIMIT;
44054
44056
  var init_tunnelGuard = __esm({
44055
44057
  "wallet/src/net/tunnelGuard.mjs"() {
44056
44058
  "use strict";
44057
44059
  init_keepAlive();
44058
44060
  init_phaseTiming();
44059
44061
  RUN_MARK = "SHINJUKU_WALLET_PROXIED";
44062
+ tunnelConnectMs = /* @__PURE__ */ new WeakMap();
44063
+ tunnelConnectMsOf = (socket) => socket && typeof socket === "object" ? tunnelConnectMs.get(socket) : void 0;
44060
44064
  TUNNEL_CLOSED_TRIES = 3;
44061
44065
  REPLAY_LIMIT = 8 * 1024;
44062
44066
  }
@@ -44156,6 +44160,19 @@ function unsubscribeStreamChannels() {
44156
44160
  }
44157
44161
  }
44158
44162
  }
44163
+ function tagOf(init, routedInit) {
44164
+ try {
44165
+ let base = init?.dispatcher;
44166
+ if (base && typeof base.tagDispatcher === "function") base = base.tagDispatcher() ?? base;
44167
+ const dispatcher = base ?? routedInit?.dispatcher;
44168
+ if (!isKey(dispatcher)) return { dispatcher: void 0, tag: "none" };
44169
+ if (ownTagDispatchers.has(dispatcher)) return { dispatcher, tag: "own" };
44170
+ for (const kept3 of onionDispatchers.values()) if (kept3 === dispatcher) return { dispatcher, tag: "run" };
44171
+ return { dispatcher, tag: "given" };
44172
+ } catch {
44173
+ return { dispatcher: void 0, tag: "none" };
44174
+ }
44175
+ }
44159
44176
  function onionRouteCold(url, sentAtMs2 = Date.now()) {
44160
44177
  let origin;
44161
44178
  try {
@@ -44185,6 +44202,7 @@ function markFirstOnionRaces(timingLog) {
44185
44202
  firstRacesMarked = true;
44186
44203
  moveTimingLog = timingLog;
44187
44204
  for (const move of moveRecords.splice(0)) writeMove(move);
44205
+ for (const record of requestRecords.splice(0)) writeRequest(record);
44188
44206
  for (const record of firstRaceRecords) {
44189
44207
  record.then((race) => {
44190
44208
  const mark = timingLog.start({ startedAt: race.startedAt });
@@ -44562,6 +44580,7 @@ async function routeOverOnions(argv2, {
44562
44580
  });
44563
44581
  group.pending = raced.winner.then((onion) => {
44564
44582
  if (!unanswered2 && !raced.stopped) noteAnswered(group, raceDispatcher);
44583
+ if (!unanswered2 && !raced.stopped) noteRoute(raceDispatcher, onion);
44565
44584
  const answer = raced.answer();
44566
44585
  group.answer = answer && !raced.stopped ? { ...answer, path: SITE_PROBE_PATH, atMs: Date.now() } : void 0;
44567
44586
  if (first) Object.assign(first, { endedAt: Date.now(), answered: !unanswered2 });
@@ -44603,7 +44622,7 @@ async function routeOverOnions(argv2, {
44603
44622
  } } : {}
44604
44623
  }).startSpares;
44605
44624
  }
44606
- const moveOn = (group, onion, why, { request, ageMs }) => {
44625
+ const moveOn = (group, onion, why, { request, ageMs, tag: tag2 }) => {
44607
44626
  if (stopped || group.winner !== onion || group.pending) return;
44608
44627
  endHedge(group, onion);
44609
44628
  group.moves = (group.moves ?? 0) + 1;
@@ -44619,7 +44638,7 @@ async function routeOverOnions(argv2, {
44619
44638
  group.live = group.onions.length;
44620
44639
  group.winner = void 0;
44621
44640
  const role = roleOf(group, onion);
44622
- const move = { at: Date.now(), role, request, cause: why === "the stream failed" ? "stream_failed" : "timeout", ageMs };
44641
+ const move = { at: Date.now(), role, request, cause: why === "the stream failed" ? "stream_failed" : "timeout", ageMs, tag: tag2 };
44623
44642
  if (moveTimingLog) writeMove(move);
44624
44643
  else moveRecords.push(move);
44625
44644
  err(`tor: the onion ${hostOf(onion)} of ${group.label} gave no answer (${why}); its requests move to the first of ${others.map(hostOf).join(", ")} to answer (move ${group.moves} of ${max}; the request was ${request} to ${role}, sent ${(ageMs / 1e3).toFixed(1)} s before)
@@ -44664,6 +44683,8 @@ async function routeOverOnions(argv2, {
44664
44683
  hedgeAborts.add(hedge.abort);
44665
44684
  const role = roleOf(group, onion);
44666
44685
  const streamNow = streamWords(stream);
44686
+ const tagNow = tagWords(stream);
44687
+ const hedgeDispatcher = onionInit({}).dispatcher;
44667
44688
  err(`tor: the onion ${hostOf(onion)} of ${group.label} has not started its answer in ${HEDGE_MS / 1e3} s (the request was ${request} to ${role}, on ${streamText(stream)}); the other onions race for the next requests, and that request keeps waiting
44668
44689
  `);
44669
44690
  raced.winner.then((won) => {
@@ -44673,6 +44694,7 @@ async function routeOverOnions(argv2, {
44673
44694
  hedge.waiters = [];
44674
44695
  if (hedge.ended || !answered2 || stopped || group.winner !== onion || group.pending) return;
44675
44696
  group.winner = won;
44697
+ noteRoute(hedgeDispatcher, won);
44676
44698
  (group.hedgedFrom ??= /* @__PURE__ */ new Set()).add(onion);
44677
44699
  const move = {
44678
44700
  at: Date.now(),
@@ -44681,7 +44703,8 @@ async function routeOverOnions(argv2, {
44681
44703
  request,
44682
44704
  cause: `to_${roleOf(group, won).replaceAll("-", "_")}`,
44683
44705
  ageMs: ageMs + (Date.now() - hedge.startedAt),
44684
- stream: streamNow
44706
+ stream: streamNow,
44707
+ tag: tagNow
44685
44708
  };
44686
44709
  if (moveTimingLog) writeMove(move);
44687
44710
  else moveRecords.push(move);
@@ -44737,12 +44760,18 @@ async function routeOverOnions(argv2, {
44737
44760
  return original(input instanceof Request ? new Request(target, input) : target, routedInit);
44738
44761
  };
44739
44762
  const sentAt = Date.now();
44740
- const stream = {};
44763
+ const tagged = tagOf(init, routedInit);
44764
+ const stream = streamRecord(tagged, onion);
44741
44765
  const sent = streamScope.run(stream, () => sendTo(onion));
44742
44766
  sent.then(() => noteAnswered(group, routedInit.dispatcher), () => {
44743
44767
  });
44744
- if (group.onions.length < 2) return sent;
44745
44768
  const request = requestLabel(group, method, url);
44769
+ const markSend = (promise, to, record, phase) => promise.then(
44770
+ () => noteRequest({ at: Date.now(), role: roleOf(group, to), request, stream: record, phase }),
44771
+ () => noteRequest({ at: Date.now(), role: roleOf(group, to), request, stream: record, phase, failed: true })
44772
+ );
44773
+ markSend(sent, onion, stream, "onion:request");
44774
+ if (group.onions.length < 2) return sent;
44746
44775
  const hedgeable = group.kind !== "history" && !init?.ownLane && onion === group.winner && !CHAIN_WAIT_LABELS.has(request);
44747
44776
  let headTimer;
44748
44777
  const clearHead = () => {
@@ -44762,7 +44791,7 @@ async function routeOverOnions(argv2, {
44762
44791
  }, (error) => {
44763
44792
  clearHead();
44764
44793
  const why = noAnswer(error, signal);
44765
- if (why && !init?.ownLane) moveOn(group, onion, why, { request, ageMs: Date.now() - sentAt });
44794
+ if (why && !init?.ownLane) moveOn(group, onion, why, { request, ageMs: Date.now() - sentAt, tag: tagWords(stream) });
44766
44795
  throw error;
44767
44796
  });
44768
44797
  if (!hedgeable) return tracked;
@@ -44789,7 +44818,10 @@ async function routeOverOnions(argv2, {
44789
44818
  onSwitch = (won) => {
44790
44819
  if (done || signal?.aborted || runEnded()) return;
44791
44820
  open5 += 1;
44792
- sendTo(won).then(win, lose);
44821
+ const resendStream = streamRecord(tagged, won);
44822
+ const resent = streamScope.run(resendStream, () => sendTo(won));
44823
+ markSend(resent, won, resendStream, "onion:resend");
44824
+ resent.then(win, lose);
44793
44825
  };
44794
44826
  });
44795
44827
  headTimer = setTimeout(() => {
@@ -44823,7 +44855,7 @@ async function routeOverOnions(argv2, {
44823
44855
  }
44824
44856
  };
44825
44857
  }
44826
- var SITE_ONIONS, SITE_SPARE_ONIONS, PINNED_FACILITATOR_ONIONS, PINNED_SPARE_ONIONS, HISTORY_ONIONS, HISTORY_SPARE_ONIONS, PINNED_HISTORY_ONIONS, PINNED_HISTORY_SPARE_ONIONS, HISTORY_PROBE_PATH, SITE_PROBE_PATH, SITE_PROBE_ANSWERS, KEPT_ANSWER_MS, KEPT_ANSWER_BYTES, KEPT_BODY_MS, FACILITATOR_FLAGS, V3_ONION, DISCOVERY_TIMEOUT_MS, WARM_TIMEOUT_MS, RACE_TIMEOUT_MS, ROUTE_WAIT_MS, SPARE_AFTER_MS, HISTORY_SPARE_AFTER_MS, HEDGE_MS, streamScope, requestStreams, socketRequests, guarded, STREAM_CHANNELS, streamSubscribers, streamWords, streamText, RESEND_LABELS, CHAIN_WAIT_LABELS, SPARE_CAUSE, routedGroups, liveOnions, hostOf, noAnswer, routedOnions, routedOrigins, onionsOfOrigin, routesOverOnion, routedGroupOf, answeredOn, noteAnswered, firstRaces, firstRaceRecords, firstRacesMarked, roleOf, ROUTE_LABELS, requestLabel, moveRecords, moveTimingLog, writeMove, firstOnionRace, onionDispatchers, ownTag, ownTagDispatchers, onOwnTagNow, ownTagInit;
44858
+ var SITE_ONIONS, SITE_SPARE_ONIONS, PINNED_FACILITATOR_ONIONS, PINNED_SPARE_ONIONS, HISTORY_ONIONS, HISTORY_SPARE_ONIONS, PINNED_HISTORY_ONIONS, PINNED_HISTORY_SPARE_ONIONS, HISTORY_PROBE_PATH, SITE_PROBE_PATH, SITE_PROBE_ANSWERS, KEPT_ANSWER_MS, KEPT_ANSWER_BYTES, KEPT_BODY_MS, FACILITATOR_FLAGS, V3_ONION, DISCOVERY_TIMEOUT_MS, WARM_TIMEOUT_MS, RACE_TIMEOUT_MS, ROUTE_WAIT_MS, SPARE_AFTER_MS, HISTORY_SPARE_AFTER_MS, HEDGE_MS, streamScope, requestStreams, socketRequests, guarded, STREAM_CHANNELS, streamSubscribers, streamWords, streamText, RESEND_LABELS, CHAIN_WAIT_LABELS, SPARE_CAUSE, routedGroups, liveOnions, hostOf, noAnswer, routedOnions, routedOrigins, onionsOfOrigin, routesOverOnion, routedGroupOf, answeredOn, tagRoutes, isKey, tagHasRoute, noteRoute, streamRecord, TAG_LETTERS, tagWords, OUTCOME_MAX, noteAnswered, firstRaces, firstRaceRecords, firstRacesMarked, roleOf, ROUTE_LABELS, requestLabel, moveRecords, moveTimingLog, writeMove, REQUEST_RECORDS_MAX, requestRecords, requestOutcome, writeRequest, REQUEST_MARK_FAST_MS, noteRequest, firstOnionRace, onionDispatchers, ownTag, ownTagDispatchers, onOwnTagNow, ownTagInit;
44827
44859
  var init_facilitatorOnion = __esm({
44828
44860
  "wallet/src/net/facilitatorOnion.mjs"() {
44829
44861
  "use strict";
@@ -44896,6 +44928,9 @@ var init_facilitatorOnion = __esm({
44896
44928
  if (!stream || stream.request !== request || stream.sentAt !== void 0) return;
44897
44929
  stream.sentAt = Date.now();
44898
44930
  stream.kind = !known ? "unknown" : sent === 1 ? "new" : "reused";
44931
+ if (stream.kind === "new") stream.connectMs = tunnelConnectMsOf(socket);
44932
+ if (known && Number.isInteger(socket.localPort)) stream.localPort = socket.localPort;
44933
+ if (stream.route) noteRoute(stream.route.dispatcher, stream.route.onion);
44899
44934
  })
44900
44935
  });
44901
44936
  streamSubscribers = 0;
@@ -44932,6 +44967,31 @@ var init_facilitatorOnion = __esm({
44932
44967
  routesOverOnion = (origin) => routedOrigins.has(origin);
44933
44968
  routedGroupOf = /* @__PURE__ */ new Map();
44934
44969
  answeredOn = /* @__PURE__ */ new WeakMap();
44970
+ tagRoutes = /* @__PURE__ */ new WeakMap();
44971
+ isKey = (value2) => value2 !== null && (typeof value2 === "object" || typeof value2 === "function");
44972
+ tagHasRoute = (dispatcher, onion) => {
44973
+ try {
44974
+ return isKey(dispatcher) && typeof onion === "string" ? tagRoutes.get(dispatcher)?.has(onion) === true : void 0;
44975
+ } catch {
44976
+ return void 0;
44977
+ }
44978
+ };
44979
+ noteRoute = (dispatcher, onion) => {
44980
+ try {
44981
+ if (!isKey(dispatcher) || typeof onion !== "string") return;
44982
+ let onions = tagRoutes.get(dispatcher);
44983
+ if (!onions) tagRoutes.set(dispatcher, onions = /* @__PURE__ */ new Set());
44984
+ onions.add(onion);
44985
+ } catch {
44986
+ }
44987
+ };
44988
+ streamRecord = (tagged, onion) => {
44989
+ const known = tagHasRoute(tagged.dispatcher, onion);
44990
+ return { startedAt: Date.now(), tag: tagged.tag, first: known === void 0 ? void 0 : !known, route: { dispatcher: tagged.dispatcher, onion } };
44991
+ };
44992
+ TAG_LETTERS = Object.freeze({ run: "r", own: "o", given: "g", none: "n" });
44993
+ tagWords = (stream) => `t${TAG_LETTERS[stream?.tag] ?? "x"}${stream?.first === true ? "f" : stream?.first === false ? "l" : "x"}`;
44994
+ OUTCOME_MAX = 96;
44935
44995
  noteAnswered = (group, dispatcher) => {
44936
44996
  if (!dispatcher) return;
44937
44997
  let seen = answeredOn.get(group);
@@ -44978,11 +45038,30 @@ var init_facilitatorOnion = __esm({
44978
45038
  moveRecords = [];
44979
45039
  writeMove = (move) => {
44980
45040
  try {
44981
- const outcome = `${move.role.replaceAll("-", "_")}_${move.request}_${move.cause}_age_${move.ageMs}${move.stream ? `_${move.stream}` : ""}`;
45041
+ let outcome = `${move.role.replaceAll("-", "_")}_${move.request}_${move.cause}_age_${move.ageMs}${move.stream ? `_${move.stream}` : ""}${move.tag ? `_${move.tag}` : ""}`;
45042
+ if (outcome.length > OUTCOME_MAX) outcome = outcome.slice(0, OUTCOME_MAX);
44982
45043
  moveTimingLog?.start({ startedAt: move.at - move.ageMs })(move.phase ?? "onion:move", outcome, move.at);
44983
45044
  } catch {
44984
45045
  }
44985
45046
  };
45047
+ REQUEST_RECORDS_MAX = 256;
45048
+ requestRecords = [];
45049
+ requestOutcome = ({ role, request, stream, failed }) => {
45050
+ const ms = (value2) => Number.isFinite(value2) && value2 >= 0 ? String(Math.round(value2)) : "na";
45051
+ return `${role.replaceAll("-", "_")}_${request}_${tagWords(stream)}_${streamWords(stream)}_connect_${ms(stream.connectMs)}_sent_${ms(stream.sentAt - stream.startedAt)}_lp${ms(stream.localPort)}${failed ? "_failed" : ""}`.slice(0, OUTCOME_MAX);
45052
+ };
45053
+ writeRequest = (record) => {
45054
+ try {
45055
+ moveTimingLog?.start({ startedAt: record.stream.startedAt })(record.phase ?? "onion:request", requestOutcome(record), record.at);
45056
+ } catch {
45057
+ }
45058
+ };
45059
+ REQUEST_MARK_FAST_MS = 1e3;
45060
+ noteRequest = (record) => {
45061
+ if (!record.failed && record.stream.kind === "reused" && record.at - record.stream.startedAt < REQUEST_MARK_FAST_MS) return;
45062
+ if (moveTimingLog) writeRequest(record);
45063
+ else if (requestRecords.length < REQUEST_RECORDS_MAX) requestRecords.push(record);
45064
+ };
44986
45065
  firstOnionRace = (origin) => {
44987
45066
  const race = firstRaces.get(origin);
44988
45067
  return race === void 0 ? void 0 : { ...race };
@@ -106614,7 +106693,10 @@ function sellerWaitMs(record) {
106614
106693
  function sellerAnswerInit(url, init, ms) {
106615
106694
  const target = () => init.dispatcher ?? routeDispatcherOf(url) ?? activeGlobalDispatcher();
106616
106695
  if (target() === void 0) return init;
106617
- return { ...init, dispatcher: { dispatch: (options, handler) => target().dispatch({ ...options, headersTimeout: ms, bodyTimeout: ms }, handler) } };
106696
+ return { ...init, dispatcher: {
106697
+ dispatch: (options, handler) => target().dispatch({ ...options, headersTimeout: ms, bodyTimeout: ms }, handler),
106698
+ tagDispatcher: target
106699
+ } };
106618
106700
  }
106619
106701
  async function sendWatchingStall(fetchImpl, url, init, waitMs, {
106620
106702
  mark = () => {
@@ -162019,7 +162101,7 @@ AUTHCHALLENGE SAFECOOKIE ${clientNonce.toString("hex")}\r
162019
162101
  }
162020
162102
  if (step === 2 && typeof events === "function") {
162021
162103
  step = 6;
162022
- socket.write("SETEVENTS HS_DESC CIRC\r\n");
162104
+ socket.write("SETEVENTS HS_DESC CIRC STREAM\r\n");
162023
162105
  continue;
162024
162106
  }
162025
162107
  if (step === 6) {
@@ -162747,6 +162829,13 @@ function torTimingMark(line, ours) {
162747
162829
  if (!prefix2 || !HS_DESC_ACTIONS.has(desc[1]) || reason2 !== void 0 && !HS_DESC_REASONS.has(reason2)) return void 0;
162748
162830
  return { phase: `hs-desc-${desc[1].toLowerCase()}`, outcome: `onion_${prefix2}${reason2 ? `_${reason2.toLowerCase()}` : ""}` };
162749
162831
  }
162832
+ const stream = /^650 STREAM (\d{1,20}) ([A-Z_]+) (\d{1,10}) ([a-z2-7]{56})\.onion:\d{1,5}(?: |$)/.exec(line);
162833
+ if (stream) {
162834
+ const prefix2 = ours.get(stream[4]);
162835
+ if (!prefix2 || !STREAM_STATUSES.has(stream[2])) return void 0;
162836
+ const port = stream[2] === "NEW" ? /(?:^| )SOURCE_ADDR=127\.0\.0\.1:(\d{1,5})(?: |$)/.exec(line)?.[1] : void 0;
162837
+ return { phase: `stream-${stream[2].toLowerCase()}`, outcome: `onion_${prefix2}_s${stream[1]}_c${stream[3]}${port ? `_lp${port}` : ""}` };
162838
+ }
162750
162839
  const circ = /^650 CIRC (\d{1,10}) ([A-Z_]+) /.exec(`${line} `);
162751
162840
  if (!circ || !CIRC_STATUSES.has(circ[2])) return void 0;
162752
162841
  const rend = /(?:^| )REND_QUERY=([a-z2-7]{56})(?: |$)/.exec(line)?.[1];
@@ -162759,7 +162848,7 @@ function torTimingMark(line, ours) {
162759
162848
  }
162760
162849
  function startTorTimingMarks() {
162761
162850
  const run2 = String(process.env.SHINJUKU_WALLET_PROXIED ?? "");
162762
- if (!run2.startsWith("tor ") || !process.argv.includes("--verbose")) return;
162851
+ if (!run2.startsWith("tor ") || !(process.argv.includes("--verbose") || process.argv[2] === "mcp")) return;
162763
162852
  setImmediate(async () => {
162764
162853
  try {
162765
162854
  const home = setupHome(process.env);
@@ -162775,23 +162864,40 @@ function startTorTimingMarks() {
162775
162864
  const ours = ownOnionPrefixes();
162776
162865
  const mark = createTimingLog(join51(home, pool2, "wallet.json.timing.log"), { component: "tor" }).start({ startedAt: Date.now() - performance.now() });
162777
162866
  let marks = 0;
162867
+ let session;
162868
+ let spent = false;
162869
+ const closeSession = () => {
162870
+ try {
162871
+ session?.close();
162872
+ } catch {
162873
+ }
162874
+ };
162778
162875
  const once2 = (phase, outcome) => {
162779
162876
  if (marks >= TOR_TIMING_MAX) return;
162780
162877
  marks += 1;
162781
162878
  mark(phase, outcome);
162879
+ if (marks >= TOR_TIMING_MAX) {
162880
+ spent = true;
162881
+ closeSession();
162882
+ }
162782
162883
  };
162783
- const session = await controlCheck(home, state2, { unref: true, events: (line) => {
162884
+ session = await controlCheck(home, state2, { unref: true, events: (line) => {
162885
+ if (spent) return;
162784
162886
  const one = torTimingMark(line, ours);
162785
162887
  if (one) once2(one.phase, one.outcome);
162786
162888
  } });
162787
162889
  if (!session) return;
162788
- process.once("exit", () => session.close());
162890
+ if (spent) {
162891
+ closeSession();
162892
+ return;
162893
+ }
162894
+ process.once("exit", closeSession);
162789
162895
  once2("events-on");
162790
162896
  } catch {
162791
162897
  }
162792
162898
  });
162793
162899
  }
162794
- var ownTorPlatform, MAC, LINUX, SYSTEM2, bundle, managedTorDir, stateFile, bundleDir, torExe, MAC_SIGNED, COMPLETE, LINUX_TOR, BUNDLE_FILES, logFile, BOOTSTRAP_STALL_MS, BOOTSTRAP_MAX_MS, STALL_RESTART_PERCENT, RESTART_MIN_MS, REACHED_RELAY_PERCENT, phaseWords, DOWNLOAD_TIMEOUT_MS, BIND_TRIES, POLL_MS2, pidAlive5, WIN_OWN_TOR, STOP_WAIT_MS, cookieFile, CONTROL_TIMEOUT_MS, SERVER_KEY, CLIENT_KEY, failure2, sleep4, tunnelOf, bundleComplete, LINUX_HASHES, fileSha256, startErrorsFile, NOTICE_READ_MAX, TUNNEL_LISTENER_LINE, ownPortsFile, OWN_PORTS_MAX, OWN_PORTS_READ_MAX, EVENT_LINE_MAX, TOR_TIMING_MAX, HS_DESC_ACTIONS, HS_DESC_REASONS, CIRC_STATUSES, CIRC_PURPOSES, CIRC_HS_STATES, CIRC_REASONS;
162900
+ var ownTorPlatform, MAC, LINUX, SYSTEM2, bundle, managedTorDir, stateFile, bundleDir, torExe, MAC_SIGNED, COMPLETE, LINUX_TOR, BUNDLE_FILES, logFile, BOOTSTRAP_STALL_MS, BOOTSTRAP_MAX_MS, STALL_RESTART_PERCENT, RESTART_MIN_MS, REACHED_RELAY_PERCENT, phaseWords, DOWNLOAD_TIMEOUT_MS, BIND_TRIES, POLL_MS2, pidAlive5, WIN_OWN_TOR, STOP_WAIT_MS, cookieFile, CONTROL_TIMEOUT_MS, SERVER_KEY, CLIENT_KEY, failure2, sleep4, tunnelOf, bundleComplete, LINUX_HASHES, fileSha256, startErrorsFile, NOTICE_READ_MAX, TUNNEL_LISTENER_LINE, ownPortsFile, OWN_PORTS_MAX, OWN_PORTS_READ_MAX, EVENT_LINE_MAX, TOR_TIMING_MAX, HS_DESC_ACTIONS, HS_DESC_REASONS, CIRC_STATUSES, CIRC_PURPOSES, CIRC_HS_STATES, CIRC_REASONS, STREAM_STATUSES;
162795
162901
  var init_managedTor = __esm({
162796
162902
  "wallet/src/net/managedTor.mjs"() {
162797
162903
  "use strict";
@@ -162936,7 +163042,7 @@ public static class ShinjukuOwnTor {
162936
163042
  return answer && answer.tunnelPort === Number(proxyPort) ? answer.circuits : void 0;
162937
163043
  });
162938
163044
  EVENT_LINE_MAX = 4096;
162939
- TOR_TIMING_MAX = 200;
163045
+ TOR_TIMING_MAX = 800;
162940
163046
  HS_DESC_ACTIONS = /* @__PURE__ */ new Set(["REQUESTED", "RECEIVED", "FAILED"]);
162941
163047
  HS_DESC_REASONS = /* @__PURE__ */ new Set([
162942
163048
  "BAD_DESC",
@@ -162976,6 +163082,7 @@ public static class ShinjukuOwnTor {
162976
163082
  "MEASUREMENT_EXPIRED",
162977
163083
  "IP_NOW_REDUNDANT"
162978
163084
  ]);
163085
+ STREAM_STATUSES = /* @__PURE__ */ new Set(["NEW", "SENTCONNECT", "SUCCEEDED", "FAILED", "CLOSED", "DETACHED"]);
162979
163086
  startTorTimingMarks();
162980
163087
  }
162981
163088
  });