tenjin-cli 0.1.0-alpha.25 → 0.1.0-alpha.26

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.
Files changed (99) hide show
  1. package/README.md +14 -41
  2. package/dist/{chunk-EG7P4SM6.js → chunk-4HAV6IFE.js} +5 -5
  3. package/dist/{chunk-IYLECN4D.js → chunk-5HS54CLI.js} +2 -2
  4. package/dist/{chunk-CEMXMUIU.js → chunk-5INS5ZZF.js} +35 -6
  5. package/dist/chunk-5INS5ZZF.js.map +1 -0
  6. package/dist/{chunk-CKDD554V.js → chunk-5NUF4BHR.js} +5 -3
  7. package/dist/{chunk-CKDD554V.js.map → chunk-5NUF4BHR.js.map} +1 -1
  8. package/dist/{chunk-NTYQHAIM.js → chunk-65EJJVT3.js} +2 -2
  9. package/dist/{chunk-GT6W3VJM.js → chunk-6JIYGYRB.js} +2 -52
  10. package/dist/chunk-6JIYGYRB.js.map +1 -0
  11. package/dist/{chunk-EOGJKIOG.js → chunk-ALJRB7NG.js} +10 -3
  12. package/dist/chunk-ALJRB7NG.js.map +1 -0
  13. package/dist/chunk-BOAI23CX.js +222 -0
  14. package/dist/chunk-BOAI23CX.js.map +1 -0
  15. package/dist/{chunk-KGTQRJGS.js → chunk-DI7WHXOZ.js} +5 -10
  16. package/dist/{chunk-KGTQRJGS.js.map → chunk-DI7WHXOZ.js.map} +1 -1
  17. package/dist/{chunk-DRXDTITB.js → chunk-EJXIK6WY.js} +9 -9
  18. package/dist/{chunk-ODH7UXIS.js → chunk-GHR4BTNJ.js} +3 -2
  19. package/dist/{chunk-ODH7UXIS.js.map → chunk-GHR4BTNJ.js.map} +1 -1
  20. package/dist/chunk-H4BOQLWW.js +99 -0
  21. package/dist/chunk-H4BOQLWW.js.map +1 -0
  22. package/dist/{chunk-VYONGSMQ.js → chunk-OBPZ36HG.js} +29 -3
  23. package/dist/chunk-OBPZ36HG.js.map +1 -0
  24. package/dist/chunk-PM2MOXJ3.js +66 -0
  25. package/dist/chunk-PM2MOXJ3.js.map +1 -0
  26. package/dist/chunk-PZ5N6BAL.js +268 -0
  27. package/dist/chunk-PZ5N6BAL.js.map +1 -0
  28. package/dist/{chunk-YSW57KUT.js → chunk-VNT3CQFV.js} +17 -17
  29. package/dist/chunk-VNT3CQFV.js.map +1 -0
  30. package/dist/{chunk-SCAE6PW6.js → chunk-W36HL4TB.js} +4 -4
  31. package/dist/{chunk-NYPFOHSJ.js → chunk-WT7DHJNJ.js} +2 -2
  32. package/dist/chunk-XMIVHQ35.js +55 -0
  33. package/dist/chunk-XMIVHQ35.js.map +1 -0
  34. package/dist/{chunk-3ZYPVSIU.js → chunk-YHWBDCGS.js} +234 -41
  35. package/dist/chunk-YHWBDCGS.js.map +1 -0
  36. package/dist/{chunk-CP7O6M55.js → chunk-YLCWI6ZR.js} +61 -20
  37. package/dist/chunk-YLCWI6ZR.js.map +1 -0
  38. package/dist/{chunk-EACMJEF3.js → chunk-Z7R2QDX2.js} +35 -171
  39. package/dist/chunk-Z7R2QDX2.js.map +1 -0
  40. package/dist/chunk-ZLD6MJVJ.js +30 -0
  41. package/dist/chunk-ZLD6MJVJ.js.map +1 -0
  42. package/dist/{cli-4WMGZPBP.js → cli-2U5JB7OQ.js} +34 -31
  43. package/dist/{cli-4WMGZPBP.js.map → cli-2U5JB7OQ.js.map} +1 -1
  44. package/dist/{config-VU6U7QOU.js → config-44EGO636.js} +11 -7
  45. package/dist/{doctor-26JS2SG3.js → doctor-BD42BBEG.js} +136 -94
  46. package/dist/doctor-BD42BBEG.js.map +1 -0
  47. package/dist/env-proxy-KEGEMWKX.js +15 -0
  48. package/dist/{fund-JJN3FRRF.js → fund-WQOOUESE.js} +20 -10
  49. package/dist/{fund-JJN3FRRF.js.map → fund-WQOOUESE.js.map} +1 -1
  50. package/dist/{hook-command-S2V4UNB3.js → hook-command-23LC5NRN.js} +12 -8
  51. package/dist/index.js +4 -2
  52. package/dist/index.js.map +1 -1
  53. package/dist/{install-GXQ33J4G.js → install-REPLHNU7.js} +19 -13
  54. package/dist/{install-identity-HFQUXPSI.js → install-identity-4RDWTLJ2.js} +4 -4
  55. package/dist/{install-wallet-27LTEHBI.js → install-wallet-USHWL7AX.js} +8 -6
  56. package/dist/{install-wallet-27LTEHBI.js.map → install-wallet-USHWL7AX.js.map} +1 -1
  57. package/dist/{mcp-AHCZVD2U.js → mcp-T5SYCLP7.js} +238 -64
  58. package/dist/mcp-T5SYCLP7.js.map +1 -0
  59. package/dist/{pay-3TCOPGCX.js → pay-276DSSYG.js} +10 -9
  60. package/dist/{payments-H3LSBGSL.js → payments-TZYLJXK3.js} +5 -4
  61. package/dist/{payments-H3LSBGSL.js.map → payments-TZYLJXK3.js.map} +1 -1
  62. package/dist/{send-LKCLN5GA.js → send-YHDKZPED.js} +6 -5
  63. package/dist/{send-LKCLN5GA.js.map → send-YHDKZPED.js.map} +1 -1
  64. package/dist/{status-2FDGI4LQ.js → status-X2INT6VD.js} +4 -3
  65. package/dist/{status-2FDGI4LQ.js.map → status-X2INT6VD.js.map} +1 -1
  66. package/dist/{status-line-wiring-ZDBYHNYR.js → status-line-wiring-LW4HFRHB.js} +4 -4
  67. package/dist/{uninstall-ZXWABDHS.js → uninstall-NVTGU7JD.js} +21 -15
  68. package/dist/{uninstall-ZXWABDHS.js.map → uninstall-NVTGU7JD.js.map} +1 -1
  69. package/dist/{update-HEIWVOP5.js → update-QXVH2DE7.js} +17 -10
  70. package/dist/update-QXVH2DE7.js.map +1 -0
  71. package/dist/{wallet-T7JYWI6O.js → wallet-CKKB23D5.js} +8 -6
  72. package/dist/wallet-CKKB23D5.js.map +1 -0
  73. package/docs/agent-permissions.md +14 -4
  74. package/docs/troubleshooting.md +13 -0
  75. package/package.json +2 -1
  76. package/dist/chunk-3ZYPVSIU.js.map +0 -1
  77. package/dist/chunk-CEMXMUIU.js.map +0 -1
  78. package/dist/chunk-CP7O6M55.js.map +0 -1
  79. package/dist/chunk-EACMJEF3.js.map +0 -1
  80. package/dist/chunk-EOGJKIOG.js.map +0 -1
  81. package/dist/chunk-GT6W3VJM.js.map +0 -1
  82. package/dist/chunk-VYONGSMQ.js.map +0 -1
  83. package/dist/chunk-YSW57KUT.js.map +0 -1
  84. package/dist/doctor-26JS2SG3.js.map +0 -1
  85. package/dist/mcp-AHCZVD2U.js.map +0 -1
  86. package/dist/update-HEIWVOP5.js.map +0 -1
  87. /package/dist/{chunk-EG7P4SM6.js.map → chunk-4HAV6IFE.js.map} +0 -0
  88. /package/dist/{chunk-IYLECN4D.js.map → chunk-5HS54CLI.js.map} +0 -0
  89. /package/dist/{chunk-NTYQHAIM.js.map → chunk-65EJJVT3.js.map} +0 -0
  90. /package/dist/{chunk-DRXDTITB.js.map → chunk-EJXIK6WY.js.map} +0 -0
  91. /package/dist/{chunk-SCAE6PW6.js.map → chunk-W36HL4TB.js.map} +0 -0
  92. /package/dist/{chunk-NYPFOHSJ.js.map → chunk-WT7DHJNJ.js.map} +0 -0
  93. /package/dist/{config-VU6U7QOU.js.map → config-44EGO636.js.map} +0 -0
  94. /package/dist/{hook-command-S2V4UNB3.js.map → env-proxy-KEGEMWKX.js.map} +0 -0
  95. /package/dist/{install-GXQ33J4G.js.map → hook-command-23LC5NRN.js.map} +0 -0
  96. /package/dist/{install-identity-HFQUXPSI.js.map → install-REPLHNU7.js.map} +0 -0
  97. /package/dist/{pay-3TCOPGCX.js.map → install-identity-4RDWTLJ2.js.map} +0 -0
  98. /package/dist/{status-line-wiring-ZDBYHNYR.js.map → pay-276DSSYG.js.map} +0 -0
  99. /package/dist/{wallet-T7JYWI6O.js.map → status-line-wiring-LW4HFRHB.js.map} +0 -0
@@ -11,15 +11,18 @@ import {
11
11
  runPay,
12
12
  toClientEvmSigner,
13
13
  x402Client
14
- } from "./chunk-DRXDTITB.js";
14
+ } from "./chunk-EJXIK6WY.js";
15
+ import "./chunk-LHHMV4GU.js";
15
16
  import {
17
+ DEPOSIT_GATE_TIMEOUT_MS,
18
+ GATE_TIMEOUT_MS,
16
19
  claimSpecPayment,
17
20
  readSpec,
18
21
  runHookKind,
19
22
  settleSpecPayment,
20
23
  storeFullResult,
21
24
  storeSpecs
22
- } from "./chunk-CP7O6M55.js";
25
+ } from "./chunk-YLCWI6ZR.js";
23
26
  import {
24
27
  MAX_MEDIA_FILES,
25
28
  appendPaidRecord,
@@ -30,20 +33,27 @@ import {
30
33
  recordedSent,
31
34
  saveMedia,
32
35
  writeAll
33
- } from "./chunk-IYLECN4D.js";
36
+ } from "./chunk-5HS54CLI.js";
34
37
  import "./chunk-LAIZ2AQA.js";
35
38
  import {
36
39
  FileClientChannelStorage
37
40
  } from "./chunk-YHKXNYTB.js";
38
41
  import "./chunk-LFZJM56J.js";
42
+ import {
43
+ clearRouterMemo,
44
+ readRouterMemo,
45
+ writeRouterMemo
46
+ } from "./chunk-BOAI23CX.js";
39
47
  import {
40
48
  CHANNEL_BUSY,
49
+ DEADLINE_AFTER_PAYMENT_SENT,
41
50
  PAID_PATH_ABSENT,
42
51
  PAYMENT_FAILED,
43
52
  RouteSkipped,
53
+ isUnreachable,
44
54
  reportSpecOutcome,
45
55
  requestDecision
46
- } from "./chunk-EACMJEF3.js";
56
+ } from "./chunk-Z7R2QDX2.js";
47
57
  import {
48
58
  x402HTTPClient
49
59
  } from "./chunk-7EKYXMUO.js";
@@ -52,17 +62,17 @@ import "./chunk-TTNESUPQ.js";
52
62
  import "./chunk-HPWO4O6S.js";
53
63
  import "./chunk-POUWHWID.js";
54
64
  import "./chunk-XZKD7DIF.js";
55
- import "./chunk-LHHMV4GU.js";
56
65
  import "./chunk-YTM7OURD.js";
57
66
  import {
58
67
  resolveSpendAuthorizer,
59
68
  resolveWalletProvider
60
- } from "./chunk-EG7P4SM6.js";
69
+ } from "./chunk-4HAV6IFE.js";
61
70
  import {
62
71
  createLocalSpendAuthorizer,
63
72
  releaseUnchargedExposure
64
- } from "./chunk-CKDD554V.js";
65
- import "./chunk-GT6W3VJM.js";
73
+ } from "./chunk-5NUF4BHR.js";
74
+ import "./chunk-6JIYGYRB.js";
75
+ import "./chunk-UDWGT4FX.js";
66
76
  import {
67
77
  createPublicClient,
68
78
  http
@@ -74,30 +84,48 @@ import "./chunk-EGELTSPU.js";
74
84
  import "./chunk-KG3D7FIF.js";
75
85
  import "./chunk-RBJPAZFI.js";
76
86
  import "./chunk-BPMWXTFX.js";
87
+ import {
88
+ mask
89
+ } from "./chunk-7QKHZQKW.js";
90
+ import {
91
+ openLookupFooter
92
+ } from "./chunk-QF4SSCQV.js";
93
+ import "./chunk-ZLD6MJVJ.js";
94
+ import {
95
+ httpRequest
96
+ } from "./chunk-5INS5ZZF.js";
97
+ import {
98
+ unpaidNotice
99
+ } from "./chunk-PM2MOXJ3.js";
77
100
  import {
78
101
  CHANNEL_DEPOSIT_ATOMIC,
79
102
  FEE_REQUIRED,
80
103
  MIN_DEPOSIT_ATOMIC,
81
104
  ROUTE_PAID_PATH,
82
105
  ROUTING_FEE_ATOMIC,
106
+ feeRefusal,
83
107
  isFeeRequired,
84
108
  payerDir,
109
+ refusedWhy,
85
110
  unpaidSentence
86
- } from "./chunk-VYONGSMQ.js";
87
- import "./chunk-UDWGT4FX.js";
111
+ } from "./chunk-OBPZ36HG.js";
88
112
  import {
89
- mask
90
- } from "./chunk-7QKHZQKW.js";
91
- import {
92
- openLookupFooter
93
- } from "./chunk-QF4SSCQV.js";
113
+ HOOK_KINDS,
114
+ HOOK_TOOL,
115
+ HOOK_TOOL_FIELDS,
116
+ eventFromToolInput
117
+ } from "./chunk-AKWGE6JQ.js";
94
118
  import {
95
- httpRequest
96
- } from "./chunk-CEMXMUIU.js";
119
+ MCP_SERVER_NAME
120
+ } from "./chunk-TGLKKLQG.js";
97
121
  import "./chunk-MEFB5KHA.js";
122
+ import "./chunk-XMIVHQ35.js";
98
123
  import {
99
124
  routerSettings
100
125
  } from "./chunk-OZ3LWLCI.js";
126
+ import {
127
+ package_default
128
+ } from "./chunk-GHR4BTNJ.js";
101
129
  import "./chunk-2I4QTOLB.js";
102
130
  import {
103
131
  resolveContextSettings
@@ -119,6 +147,7 @@ import {
119
147
  intersection,
120
148
  iso_exports,
121
149
  literal,
150
+ loadRawConfig,
122
151
  looseObject,
123
152
  normalizeParams,
124
153
  number,
@@ -137,18 +166,6 @@ import {
137
166
  union,
138
167
  unknown
139
168
  } from "./chunk-XICKN3CH.js";
140
- import {
141
- HOOK_KINDS,
142
- HOOK_TOOL,
143
- HOOK_TOOL_FIELDS,
144
- eventFromToolInput
145
- } from "./chunk-AKWGE6JQ.js";
146
- import {
147
- MCP_SERVER_NAME
148
- } from "./chunk-TGLKKLQG.js";
149
- import {
150
- package_default
151
- } from "./chunk-ODH7UXIS.js";
152
169
  import {
153
170
  CliError
154
171
  } from "./chunk-JLEF5WHM.js";
@@ -9861,6 +9878,8 @@ function wrapFetchWithPayment(fetch2, client) {
9861
9878
  // src/router/routing-payer.ts
9862
9879
  var ROUTING_SPEND_CAP = "$0.05";
9863
9880
  var DEPOSIT_MULTIPLIER = 5;
9881
+ var DEPOSIT_EXTRA_MS = DEPOSIT_GATE_TIMEOUT_MS - GATE_TIMEOUT_MS;
9882
+ var DETACHED_CEILING_MS = 15e3;
9864
9883
  var ABSENT_TTL_MS = 60 * 6e4;
9865
9884
  var BALANCE_TIMEOUT_MS = 1e3;
9866
9885
  var RPC_TIMEOUT_MS = 2e3;
@@ -9877,33 +9896,33 @@ var routingPolicy = (version, requirements) => canonicalUsdcOnly(version, requir
9877
9896
  (r) => r.scheme === "batch-settlement" && r.amount === ROUTING_FEE_ATOMIC.toString()
9878
9897
  );
9879
9898
  var CHANNEL_BUSY_ERROR = "invalid_batch_settlement_evm_channel_busy";
9880
- var REFUSED = {
9881
- not_allowlisted: "not_allowlisted",
9882
- session_budget_exceeded: "budget_reached",
9883
- above_auto_spend: "limit_below_deposit"
9884
- };
9885
9899
  var RoutingPayer = class {
9886
9900
  constructor(deps) {
9887
9901
  this.deps = deps;
9888
9902
  }
9889
9903
  deps;
9890
- /** Paid-path URLs that answered no paid path, until when. */
9891
- absent = /* @__PURE__ */ new Map();
9892
9904
  channel = null;
9893
9905
  call = null;
9906
+ /** The call `pay` ran last, for its one diagnostic line. */
9907
+ last = null;
9894
9908
  queue = Promise.resolve();
9909
+ /** A detached request still finishing; the next call waits for it. */
9910
+ tail = Promise.resolve();
9895
9911
  /**
9896
9912
  * The paid path for one call, or null for the free path for an hour after
9897
- * the server answered no paid path.
9913
+ * the server answered no paid path. That answer is the machine's `router-memo`,
9914
+ * so every `tenjin mcp` process shares it.
9898
9915
  */
9899
9916
  async routeFor(baseUrl) {
9900
- const probe = new URL(ROUTE_PAID_PATH, baseUrl).toString();
9901
- if ((this.absent.get(probe) ?? 0) > this.now()) return null;
9917
+ if (await readRouterMemo(this.deps.dataDir, "paid-path-absent", baseUrl, this.now())) {
9918
+ return null;
9919
+ }
9902
9920
  return {
9903
9921
  path: ROUTE_PAID_PATH,
9904
9922
  send: (url, options) => {
9905
- const until = this.now() + options.timeoutMs;
9906
- return this.turn(() => this.pay(url, options, until));
9923
+ const sent = this.now();
9924
+ const until = sent + options.timeoutMs;
9925
+ return this.turn(() => this.noted(sent, () => this.pay(url, options, until)), until, sent);
9907
9926
  }
9908
9927
  };
9909
9928
  }
@@ -9916,11 +9935,116 @@ var RoutingPayer = class {
9916
9935
  `tenjin mcp: routing fee: ${line}`
9917
9936
  );
9918
9937
  }
9919
- /** Calls inside one process take turns on its channel. */
9920
- turn(run) {
9921
- const next = this.queue.then(run, run);
9922
- this.queue = next.catch(() => void 0);
9923
- return next;
9938
+ /** Calls inside one process take turns on its channel, a detached
9939
+ * request's tail included, so none starts from a stale channel. A queued
9940
+ * call gives up at its own deadline (`busy`) rather than wait out the
9941
+ * call ahead of it, and then never starts. The deadline covers the wait
9942
+ * only: once a call runs, its own budget applies. */
9943
+ turn(run, until, sent) {
9944
+ let gaveUp = false;
9945
+ let timer;
9946
+ const go = () => {
9947
+ if (gaveUp) return Promise.reject(new RouteSkipped("busy"));
9948
+ clearTimeout(timer);
9949
+ return run();
9950
+ };
9951
+ const next = this.queue.then(go, go);
9952
+ this.queue = next.catch(() => void 0).then(() => this.tail);
9953
+ const deadline = new Promise((_resolve, reject) => {
9954
+ timer = setTimeout(
9955
+ () => {
9956
+ gaveUp = true;
9957
+ this.note("busy", null, sent);
9958
+ reject(new RouteSkipped("busy"));
9959
+ },
9960
+ Math.max(0, until - this.now())
9961
+ );
9962
+ });
9963
+ return Promise.race([next, deadline]).finally(() => clearTimeout(timer));
9964
+ }
9965
+ /** One stderr line for a call the paid path did not answer; a detached
9966
+ * one says it when its request ends. */
9967
+ async noted(sent, run) {
9968
+ this.last = null;
9969
+ try {
9970
+ return await run();
9971
+ } catch (err) {
9972
+ const call = this.last;
9973
+ if (err instanceof RouteSkipped && call?.detached !== true) this.note(err.why, call, sent);
9974
+ throw err;
9975
+ }
9976
+ }
9977
+ note(why, call, sent, late2) {
9978
+ const result = call?.result;
9979
+ const cause = late2 ?? (call === null ? "queued" : call.skipped !== void 0 ? "refused" : result === void 0 ? "none" : result.ok ? `http ${result.status}` : call.expired === true ? "timeout" : result.kind);
9980
+ const stage = call === null ? "queue" : call.paid ? call.deposit === true ? "deposit" : "voucher" : result?.ok === true ? "answer" : "challenge";
9981
+ this.warn(`${why} (cause ${cause}, ${this.now() - sent}ms, stage ${stage})`);
9982
+ }
9983
+ /**
9984
+ * THE DEADLINE CUTS NOTHING ALREADY PAID. Before a payment goes out, the
9985
+ * call's budget aborts it as always. Once one is out, aborting would lose
9986
+ * the answer that records it (the SDK's channel and the ledger) while the
9987
+ * server settles it anyway, so the call returns at its deadline and the
9988
+ * request runs on here, up to {@link DETACHED_CEILING_MS}; the next call
9989
+ * waits for it. `httpRequest`'s own timeout is the outer bound.
9990
+ */
9991
+ detachable(call, sent, run) {
9992
+ return (input, init) => {
9993
+ const caller = init?.signal ?? void 0;
9994
+ const own = new AbortController();
9995
+ const work = run(input, { ...init, signal: own.signal });
9996
+ return new Promise((resolve2, reject) => {
9997
+ let timer;
9998
+ const settle = () => {
9999
+ clearTimeout(timer);
10000
+ caller?.removeEventListener("abort", onAbort);
10001
+ };
10002
+ const stop = (reason) => {
10003
+ settle();
10004
+ if (!call.paid) return own.abort(reason);
10005
+ call.detached = true;
10006
+ this.tail = this.finishDetached(call, sent, work, own);
10007
+ reject(reason);
10008
+ };
10009
+ const onAbort = () => stop(caller?.reason);
10010
+ const check = () => {
10011
+ const left = call.until - this.now();
10012
+ if (left > 0) timer = setTimeout(check, left);
10013
+ else {
10014
+ call.expired = true;
10015
+ stop(new DOMException("The routing call ran out of time.", "TimeoutError"));
10016
+ }
10017
+ };
10018
+ check();
10019
+ if (caller?.aborted === true) onAbort();
10020
+ else caller?.addEventListener("abort", onAbort);
10021
+ work.then(
10022
+ (response) => {
10023
+ settle();
10024
+ resolve2(call.detached === true ? response : bounded(response, caller, own));
10025
+ },
10026
+ (err) => {
10027
+ settle();
10028
+ reject(err);
10029
+ }
10030
+ );
10031
+ });
10032
+ };
10033
+ }
10034
+ /** The detached request's end: its answer read and dropped, its one line. */
10035
+ async finishDetached(call, sent, work, own) {
10036
+ const ceiling = setTimeout(() => own.abort(), DETACHED_CEILING_MS);
10037
+ ceiling.unref?.();
10038
+ try {
10039
+ const response = await work;
10040
+ await response.arrayBuffer().catch(() => void 0);
10041
+ const why = response.ok ? DEADLINE_AFTER_PAYMENT_SENT : PAYMENT_FAILED;
10042
+ this.note(why, call, sent, `late http ${response.status}`);
10043
+ } catch {
10044
+ this.note(PAYMENT_FAILED, call, sent, own.signal.aborted ? "late ceiling" : "late network");
10045
+ } finally {
10046
+ clearTimeout(ceiling);
10047
+ }
9924
10048
  }
9925
10049
  /**
9926
10050
  * ONE PAID CALL on the wallet's channel, inside the caller's budget. The
@@ -9929,13 +10053,22 @@ var RoutingPayer = class {
9929
10053
  */
9930
10054
  async pay(url, options, until) {
9931
10055
  if (until - this.now() <= 0) throw new RouteSkipped("busy");
9932
- const call = { host: new URL(url).host, until, paid: false, deposits: [] };
10056
+ const call = {
10057
+ host: new URL(url).host,
10058
+ until,
10059
+ paid: false,
10060
+ answered: false,
10061
+ deposits: []
10062
+ };
9933
10063
  const base = options.fetchImpl ?? this.deps.fetchImpl ?? fetch;
9934
10064
  const askFirst = async (input, init) => {
9935
10065
  const request = new Request(input, init);
9936
10066
  let asked = await base(request.clone());
10067
+ call.answered = true;
9937
10068
  if (asked.status !== 402) return asked;
9938
10069
  try {
10070
+ const refused = feeRefusal(await this.deps.policy(), call.host);
10071
+ if (refused !== null) throw new RouteSkipped(refused);
9939
10072
  call.channel = this.channelFor(await this.signer());
9940
10073
  if (until - this.now() <= 0) throw new RouteSkipped("busy");
9941
10074
  } catch (err) {
@@ -9961,21 +10094,32 @@ var RoutingPayer = class {
9961
10094
  return wrapFetchWithPayment(watched, call.channel.http)(request);
9962
10095
  };
9963
10096
  this.call = call;
10097
+ this.last = call;
9964
10098
  let response;
9965
10099
  try {
9966
- response = await httpRequest(url, {
10100
+ response = call.result = await httpRequest(url, {
9967
10101
  ...options,
9968
- timeoutMs: until - this.now(),
9969
- fetchImpl: askFirst
10102
+ // The outer bound: a deposit's budget, which a call without one never reaches.
10103
+ timeoutMs: until + DEPOSIT_EXTRA_MS - this.now(),
10104
+ fetchImpl: this.detachable(call, until - options.timeoutMs, askFirst)
9970
10105
  });
9971
10106
  } finally {
9972
10107
  this.call = null;
9973
10108
  await this.settleDeposits(call);
9974
10109
  }
9975
10110
  if (call.skipped !== void 0) throw new RouteSkipped(call.skipped);
9976
- if (!response.ok) throw new RouteSkipped(PAYMENT_FAILED);
10111
+ if (!response.ok && !call.answered && isUnreachable(response)) {
10112
+ throw new RouteSkipped(PAYMENT_FAILED, response);
10113
+ }
10114
+ if (!response.ok) {
10115
+ const late2 = call.detached === true || call.paid && response.kind === "timeout";
10116
+ throw new RouteSkipped(late2 ? DEADLINE_AFTER_PAYMENT_SENT : PAYMENT_FAILED);
10117
+ }
9977
10118
  if (!call.paid && response.status === 404) {
9978
- this.absent.set(new URL(ROUTE_PAID_PATH, url).toString(), this.now() + ABSENT_TTL_MS);
10119
+ await writeRouterMemo(this.deps.dataDir, "paid-path-absent", url, {
10120
+ now: this.now(),
10121
+ ttlMs: ABSENT_TTL_MS
10122
+ });
9979
10123
  throw new RouteSkipped(PAID_PATH_ABSENT);
9980
10124
  }
9981
10125
  if (response.status === 402) {
@@ -10047,7 +10191,7 @@ var RoutingPayer = class {
10047
10191
  });
10048
10192
  if (auth.decision !== "allow") {
10049
10193
  await authorizer.release(auth.reservationId);
10050
- this.refuse(REFUSED[auth.reason] ?? auth.reason);
10194
+ this.refuse(refusedWhy(auth.reason));
10051
10195
  }
10052
10196
  call.deposits.push({
10053
10197
  authorizer,
@@ -10074,7 +10218,9 @@ var RoutingPayer = class {
10074
10218
  return toClientEvmSigner(
10075
10219
  {
10076
10220
  address: signer.address,
10077
- signTypedData: (message) => signer.signTypedData(message)
10221
+ // Nothing is signed outside a call: a detached request finishes and
10222
+ // starts no retry, whose answer nobody would read.
10223
+ signTypedData: (message) => this.call === null ? Promise.reject(new RouteSkipped("busy")) : signer.signTypedData(message)
10078
10224
  },
10079
10225
  this.deps.readContract !== void 0 ? { readContract: (args) => this.chainRead(this.deps.readContract, args) } : void 0
10080
10226
  );
@@ -10105,13 +10251,30 @@ var RoutingPayer = class {
10105
10251
  const scheme = new BatchSettlementEvmScheme(signer, {
10106
10252
  storage: new FileClientChannelStorage({ directory: dir }),
10107
10253
  depositPolicy: { depositMultiplier: DEPOSIT_MULTIPLIER },
10108
- depositStrategy: (ctx) => this.deposit(ctx)
10254
+ depositStrategy: (ctx) => {
10255
+ if (this.call !== null && this.call.deposit !== true) {
10256
+ this.call.deposit = true;
10257
+ this.call.until += DEPOSIT_EXTRA_MS;
10258
+ }
10259
+ return this.deposit(ctx);
10260
+ }
10109
10261
  });
10110
10262
  const client = new x402Client().register("eip155:*", scheme).registerPolicy(routingPolicy).setSpendControls({ maxAmountPerPayment: ROUTING_SPEND_CAP });
10111
10263
  this.channel = { payer, http: new x402HTTPClient(client) };
10112
10264
  return this.channel;
10113
10265
  }
10114
10266
  };
10267
+ function bounded(response, caller, own) {
10268
+ if (caller === void 0 || response.body === null) return response;
10269
+ if (caller.aborted) own.abort(caller.reason);
10270
+ else caller.addEventListener("abort", () => own.abort(caller.reason), { once: true });
10271
+ const body = response.body.pipeThrough(new TransformStream(), { signal: own.signal });
10272
+ return new Response(body, {
10273
+ status: response.status,
10274
+ statusText: response.statusText,
10275
+ headers: response.headers
10276
+ });
10277
+ }
10115
10278
  function paymentError(channel, response) {
10116
10279
  try {
10117
10280
  return channel.http.getPaymentRequiredResponse((name) => response.header(name), response.json).error;
@@ -10352,17 +10515,26 @@ async function runRequestTool(args, deps) {
10352
10515
  if (spec !== null && input !== void 0) {
10353
10516
  return runSpec(id, spec, input, deps, footer, decisionDeps);
10354
10517
  }
10355
- const route = await deps.route?.(settings.baseUrl) ?? null;
10356
- const fresh = await requestDecision(
10357
- "tool",
10358
- { query, ...id !== void 0 ? { id } : {} },
10359
- { ...decisionDeps, ...route !== null ? { route } : {} }
10360
- );
10518
+ const ask = async () => {
10519
+ const route = await deps.route?.(settings.baseUrl) ?? null;
10520
+ const outcome = await requestDecision(
10521
+ "tool",
10522
+ { query, ...id !== void 0 ? { id } : {} },
10523
+ { ...decisionDeps, ...route !== null ? { route } : {} }
10524
+ );
10525
+ return { route, outcome };
10526
+ };
10527
+ const first = await ask();
10528
+ let fresh = first.outcome;
10529
+ if (deps.route !== void 0 && first.route === null && fresh.status === "decided" && isFeeRequired(fresh.decision)) {
10530
+ await clearRouterMemo(deps.ctx.dataDir, "paid-path-absent", settings.baseUrl);
10531
+ fresh = (await ask()).outcome;
10532
+ }
10361
10533
  if (fresh.status === "skipped") {
10362
10534
  await footer.done("native");
10363
10535
  return fail(
10364
10536
  "native",
10365
- `${unpaidSentence(fresh.why) ?? ""} This call ran out of time before the free path answered, so nothing was routed or paid.`.trim(),
10537
+ fresh.why === DEADLINE_AFTER_PAYMENT_SENT ? "A routing payment (possibly the channel deposit) went out, and this call ran out of time before its answer. `tenjin mcp` lets it finish so it is recorded; nothing else was paid." : `${unpaidSentence(fresh.why) ?? ""} This call ran out of time before the free path answered, so nothing was routed or paid.`.trim(),
10366
10538
  { nextStep: "Use your own tools for this one." }
10367
10539
  );
10368
10540
  }
@@ -10374,10 +10546,12 @@ async function runRequestTool(args, deps) {
10374
10546
  }
10375
10547
  const { decision, note } = fresh.decision;
10376
10548
  if (isFeeRequired(fresh.decision)) {
10549
+ await clearRouterMemo(deps.ctx.dataDir, "paid-path-absent", settings.baseUrl);
10377
10550
  await footer.done("native");
10551
+ const raw = await loadRawConfig(deps.ctx.dataDir).catch(() => ({}));
10378
10552
  return fail(
10379
10553
  "native",
10380
- unpaidSentence(fresh.freePath ?? FEE_REQUIRED) ?? unpaidSentence(FEE_REQUIRED),
10554
+ unpaidNotice(fresh.freePath ?? FEE_REQUIRED, raw, "agent") ?? unpaidSentence(FEE_REQUIRED),
10381
10555
  { nextStep: "Tell the user this once, and use your own tools for now." }
10382
10556
  );
10383
10557
  }
@@ -11075,4 +11249,4 @@ export {
11075
11249
  buildRouterMcpServer,
11076
11250
  runRouterMcpServer
11077
11251
  };
11078
- //# sourceMappingURL=mcp-AHCZVD2U.js.map
11252
+ //# sourceMappingURL=mcp-T5SYCLP7.js.map