token-goat 2.9.29 → 2.9.31

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 (197) hide show
  1. package/README.md +53 -42
  2. package/SECURITY.md +37 -11
  3. package/THIRD_PARTY_NOTICES.md +41 -4
  4. package/dist/native/linux-arm64/tg-hook +0 -0
  5. package/dist/native/linux-x64/tg-hook +0 -0
  6. package/dist/native/win32-arm64/tg-hook.exe +0 -0
  7. package/dist/native/win32-x64/tg-hook.exe +0 -0
  8. package/dist/ort-wasm-simd-threaded.mjs +59 -0
  9. package/dist/{token-goat-chunk-SE6E4BKJ.mjs → token-goat-chunk-25TRAWET.mjs} +23 -6
  10. package/dist/token-goat-chunk-2KXO6V2B.mjs +203 -0
  11. package/dist/{token-goat-chunk-UXSXQQIY.mjs → token-goat-chunk-2M7E4PF7.mjs} +43 -14
  12. package/dist/token-goat-chunk-2UPZGKKY.mjs +188 -0
  13. package/dist/token-goat-chunk-2WLPWKHV.mjs +135 -0
  14. package/dist/token-goat-chunk-2XBMMDBD.mjs +126 -0
  15. package/dist/token-goat-chunk-32ZOY4N5.mjs +23 -0
  16. package/dist/token-goat-chunk-3EZRNKTK.mjs +188 -0
  17. package/dist/token-goat-chunk-4ADZHI7J.mjs +63 -0
  18. package/dist/token-goat-chunk-4GBVPH3D.mjs +242 -0
  19. package/dist/{token-goat-chunk-2WOUYWLM.mjs → token-goat-chunk-4GILLLWY.mjs} +34 -7
  20. package/dist/token-goat-chunk-4NAPX7BA.mjs +90 -0
  21. package/dist/{token-goat-chunk-NMBWLRHW.mjs → token-goat-chunk-4SC7UJYV.mjs} +9981 -11563
  22. package/dist/token-goat-chunk-4SMRO4TO.mjs +366 -0
  23. package/dist/{token-goat-chunk-O6I2XU4P.mjs → token-goat-chunk-4ZBFWLXF.mjs} +310 -441
  24. package/dist/token-goat-chunk-57BIAXJL.mjs +286 -0
  25. package/dist/token-goat-chunk-5AC66HNP.mjs +115 -0
  26. package/dist/token-goat-chunk-5IMLKSOP.mjs +145 -0
  27. package/dist/token-goat-chunk-5RG5SBL6.mjs +221 -0
  28. package/dist/{token-goat-chunk-RRNZMM3A.mjs → token-goat-chunk-5Y6ZYL6U.mjs} +21 -2
  29. package/dist/token-goat-chunk-646E6U44.mjs +398 -0
  30. package/dist/token-goat-chunk-65N4RHP4.mjs +33 -0
  31. package/dist/{token-goat-chunk-6AK46ZT7.mjs → token-goat-chunk-6DG6HWP3.mjs} +13 -3
  32. package/dist/token-goat-chunk-6ENQSNRZ.mjs +573 -0
  33. package/dist/token-goat-chunk-6ZY4GNF2.mjs +91 -0
  34. package/dist/{token-goat-chunk-E5TK5DNO.mjs → token-goat-chunk-76FGDLRO.mjs} +574 -340
  35. package/dist/{token-goat-chunk-TGOXNAHW.mjs → token-goat-chunk-7EZTZOMC.mjs} +780 -767
  36. package/dist/token-goat-chunk-7MAOA6X4.mjs +3647 -0
  37. package/dist/token-goat-chunk-7R2ZVVAX.mjs +218 -0
  38. package/dist/{token-goat-chunk-TCQANXYC.mjs → token-goat-chunk-AM5V6GSX.mjs} +18 -2
  39. package/dist/token-goat-chunk-AUNGYYQX.mjs +184 -0
  40. package/dist/token-goat-chunk-AUQYRMMH.mjs +2985 -0
  41. package/dist/token-goat-chunk-B3PEKBYG.mjs +128 -0
  42. package/dist/{token-goat-chunk-KOPIR3KZ.mjs → token-goat-chunk-BA4HI6BW.mjs} +1910 -165
  43. package/dist/{token-goat-chunk-52NSDWYY.mjs → token-goat-chunk-BFCWY73P.mjs} +58 -20
  44. package/dist/token-goat-chunk-BPGCUMR7.mjs +30 -0
  45. package/dist/token-goat-chunk-BWGFUZZE.mjs +25 -0
  46. package/dist/token-goat-chunk-C75NA7PL.mjs +322 -0
  47. package/dist/token-goat-chunk-CBIQ7WTD.mjs +87 -0
  48. package/dist/token-goat-chunk-CETM74KC.mjs +307 -0
  49. package/dist/token-goat-chunk-CIMQ2BB3.mjs +233 -0
  50. package/dist/{token-goat-chunk-DNSPXQXQ.mjs → token-goat-chunk-CJLS3CDL.mjs} +70 -18
  51. package/dist/{token-goat-chunk-MELOKFDJ.mjs → token-goat-chunk-CO3UYX6G.mjs} +7 -1
  52. package/dist/token-goat-chunk-COPNXRJM.mjs +154 -0
  53. package/dist/token-goat-chunk-CSJ5L3NL.mjs +479 -0
  54. package/dist/token-goat-chunk-CVCEVUGK.mjs +315 -0
  55. package/dist/token-goat-chunk-CXHOFN7R.mjs +371 -0
  56. package/dist/token-goat-chunk-D735C4QM.mjs +603 -0
  57. package/dist/{token-goat-chunk-E4ZOJIZK.mjs → token-goat-chunk-DGS2LVNA.mjs} +70 -160
  58. package/dist/{token-goat-chunk-L7H3U27L.mjs → token-goat-chunk-DOWRSMWO.mjs} +1 -1
  59. package/dist/token-goat-chunk-E45INMY3.mjs +71 -0
  60. package/dist/token-goat-chunk-EC5F37F4.mjs +244 -0
  61. package/dist/{token-goat-chunk-XTQAOTSO.mjs → token-goat-chunk-EDBDRVH5.mjs} +4 -1
  62. package/dist/token-goat-chunk-EELTBKDM.mjs +613 -0
  63. package/dist/token-goat-chunk-EVMZJ27Z.mjs +147 -0
  64. package/dist/token-goat-chunk-EXKGNHEG.mjs +2096 -0
  65. package/dist/{token-goat-chunk-YOOVFPTP.mjs → token-goat-chunk-F4HYL64R.mjs} +12 -2
  66. package/dist/token-goat-chunk-F6PAUIEK.mjs +624 -0
  67. package/dist/token-goat-chunk-FDSREWLX.mjs +692 -0
  68. package/dist/{token-goat-chunk-NMTKNYGF.mjs → token-goat-chunk-FNFBKLRD.mjs} +74 -9
  69. package/dist/token-goat-chunk-G5PJ7AGM.mjs +36 -0
  70. package/dist/token-goat-chunk-GGGG7RWB.mjs +236 -0
  71. package/dist/{token-goat-chunk-TDUT2CL6.mjs → token-goat-chunk-GL3ETKSC.mjs} +276 -19
  72. package/dist/{token-goat-chunk-7CPMDFGB.mjs → token-goat-chunk-GVB2MUST.mjs} +265 -1567
  73. package/dist/token-goat-chunk-GWFLAUMQ.mjs +307 -0
  74. package/dist/token-goat-chunk-HAVAOB3S.mjs +87 -0
  75. package/dist/token-goat-chunk-HLPW63A7.mjs +48 -0
  76. package/dist/token-goat-chunk-HU4TORC5.mjs +1377 -0
  77. package/dist/token-goat-chunk-I7VKZZAW.mjs +35 -0
  78. package/dist/token-goat-chunk-IA6XSPE3.mjs +218 -0
  79. package/dist/token-goat-chunk-IAGXMHIK.mjs +863 -0
  80. package/dist/{token-goat-chunk-435UQTKD.mjs → token-goat-chunk-IK2DRMFX.mjs} +129 -74
  81. package/dist/{token-goat-chunk-ZER6EAXZ.mjs → token-goat-chunk-INNPTRUX.mjs} +2954 -2783
  82. package/dist/token-goat-chunk-JJ5BX4F4.mjs +129 -0
  83. package/dist/token-goat-chunk-JVFSHVFJ.mjs +129 -0
  84. package/dist/token-goat-chunk-KGEFFSHD.mjs +50 -0
  85. package/dist/token-goat-chunk-KKUFXAPD.mjs +25 -0
  86. package/dist/token-goat-chunk-KQBLRPBN.mjs +433 -0
  87. package/dist/token-goat-chunk-LCNQ2CNN.mjs +497 -0
  88. package/dist/token-goat-chunk-M3LFJDPU.mjs +58 -0
  89. package/dist/token-goat-chunk-M52KPT3C.mjs +87 -0
  90. package/dist/token-goat-chunk-MBEG42H7.mjs +150 -0
  91. package/dist/{token-goat-chunk-EVC4TOLE.mjs → token-goat-chunk-METECDOH.mjs} +3 -12
  92. package/dist/{token-goat-chunk-2AWQ2R5X.mjs → token-goat-chunk-MHX7XCC3.mjs} +3 -2
  93. package/dist/token-goat-chunk-MNWKPRAL.mjs +13 -0
  94. package/dist/token-goat-chunk-N5LSOVSU.mjs +137 -0
  95. package/dist/{token-goat-chunk-DPNLUJWB.mjs → token-goat-chunk-N5MMR2IW.mjs} +30 -8
  96. package/dist/token-goat-chunk-N7B3JQ2C.mjs +160 -0
  97. package/dist/{token-goat-chunk-VMFWNHGR.mjs → token-goat-chunk-NISNMD7T.mjs} +2545 -2953
  98. package/dist/token-goat-chunk-NMD42SFQ.mjs +434 -0
  99. package/dist/token-goat-chunk-NOSS5VWY.mjs +174 -0
  100. package/dist/token-goat-chunk-NSNSPPBG.mjs +69 -0
  101. package/dist/token-goat-chunk-OBOFIHKN.mjs +38 -0
  102. package/dist/token-goat-chunk-OBRFJ5O5.mjs +76 -0
  103. package/dist/token-goat-chunk-ODBT2USM.mjs +800 -0
  104. package/dist/token-goat-chunk-OS5EM5PP.mjs +107 -0
  105. package/dist/token-goat-chunk-OUKDTVNU.mjs +1178 -0
  106. package/dist/token-goat-chunk-OXFMP75N.mjs +566 -0
  107. package/dist/{token-goat-chunk-ASLWOQ3I.mjs → token-goat-chunk-OYIYKDDT.mjs} +2 -1
  108. package/dist/{token-goat-chunk-67BNIKKO.mjs → token-goat-chunk-P6HQSWQ3.mjs} +55 -47
  109. package/dist/token-goat-chunk-P6VGNTQT.mjs +71 -0
  110. package/dist/token-goat-chunk-PFWIMH2E.mjs +261 -0
  111. package/dist/token-goat-chunk-PJZXIS3G.mjs +39 -0
  112. package/dist/token-goat-chunk-PUO5INXP.mjs +606 -0
  113. package/dist/token-goat-chunk-PX5PQWTY.mjs +462 -0
  114. package/dist/token-goat-chunk-Q5QDBC6O.mjs +111 -0
  115. package/dist/token-goat-chunk-QFNQXSDT.mjs +40 -0
  116. package/dist/token-goat-chunk-QH7YVYOY.mjs +3957 -0
  117. package/dist/token-goat-chunk-QLMUHEPB.mjs +43 -0
  118. package/dist/{token-goat-chunk-PYSUADX4.mjs → token-goat-chunk-QQOJIHL3.mjs} +50 -14
  119. package/dist/token-goat-chunk-QQTWJY2O.mjs +85 -0
  120. package/dist/token-goat-chunk-QYHO5CPH.mjs +93 -0
  121. package/dist/token-goat-chunk-RELAVWAD.mjs +27 -0
  122. package/dist/token-goat-chunk-RSLPOBKQ.mjs +339 -0
  123. package/dist/token-goat-chunk-SAMFDIS2.mjs +712 -0
  124. package/dist/token-goat-chunk-SDELXLDM.mjs +48 -0
  125. package/dist/{token-goat-chunk-PGTH2U4C.mjs → token-goat-chunk-SOAFZNNZ.mjs} +164 -40
  126. package/dist/{token-goat-chunk-G23DWHV4.mjs → token-goat-chunk-SRTHT7KV.mjs} +33 -15
  127. package/dist/token-goat-chunk-T5P5ACNQ.mjs +212 -0
  128. package/dist/token-goat-chunk-T5YGG4V3.mjs +45 -0
  129. package/dist/token-goat-chunk-TAUZ4OFS.mjs +110 -0
  130. package/dist/token-goat-chunk-TM6MC754.mjs +40 -0
  131. package/dist/token-goat-chunk-TWUKBOMP.mjs +72 -0
  132. package/dist/token-goat-chunk-U2KSB4SG.mjs +56 -0
  133. package/dist/token-goat-chunk-U4WHMG6Y.mjs +898 -0
  134. package/dist/token-goat-chunk-UA5V6P3P.mjs +122 -0
  135. package/dist/token-goat-chunk-UGJHGR5A.mjs +52 -0
  136. package/dist/{token-goat-chunk-RQGGMPCR.mjs → token-goat-chunk-UOUDVA5U.mjs} +76 -16
  137. package/dist/{token-goat-chunk-HNFZZHFW.mjs → token-goat-chunk-URRJ33GR.mjs} +22 -4
  138. package/dist/token-goat-chunk-UZOROKIS.mjs +165 -0
  139. package/dist/token-goat-chunk-VIHGGGEI.mjs +4234 -0
  140. package/dist/token-goat-chunk-VTJLAA7P.mjs +21 -0
  141. package/dist/token-goat-chunk-VWDXLT4F.mjs +678 -0
  142. package/dist/token-goat-chunk-VWZA2H4P.mjs +368 -0
  143. package/dist/{token-goat-chunk-QO3LXVVR.mjs → token-goat-chunk-VXEXMJDL.mjs} +2964 -3153
  144. package/dist/token-goat-chunk-WBA5SKT4.mjs +547 -0
  145. package/dist/{token-goat-chunk-ONXH6NZ5.mjs → token-goat-chunk-WDH44VAU.mjs} +19 -22
  146. package/dist/token-goat-chunk-X2SUP3EI.mjs +107 -0
  147. package/dist/token-goat-chunk-X3BELZSJ.mjs +125 -0
  148. package/dist/{token-goat-chunk-RLP6J7WR.mjs → token-goat-chunk-XLRBNZCC.mjs} +40 -21
  149. package/dist/{token-goat-chunk-S4VSXURL.mjs → token-goat-chunk-XRKUX6CM.mjs} +4 -0
  150. package/dist/token-goat-chunk-XX6O25RN.mjs +82 -0
  151. package/dist/token-goat-chunk-XZSUHBVS.mjs +65 -0
  152. package/dist/token-goat-chunk-Y2ZTQOCL.mjs +35 -0
  153. package/dist/{token-goat-chunk-UA5BKWWQ.mjs → token-goat-chunk-YB4VMGK2.mjs} +15 -4
  154. package/dist/token-goat-chunk-YBIIY3HR.mjs +51 -0
  155. package/dist/{token-goat-chunk-PKRWMHVP.mjs → token-goat-chunk-YHVDKQBR.mjs} +389 -221
  156. package/dist/token-goat-chunk-YJB5XLWI.mjs +26 -0
  157. package/dist/{token-goat-chunk-PQ53BSCF.mjs → token-goat-chunk-YKB6AQG3.mjs} +53 -31
  158. package/dist/token-goat-chunk-YMTG2YA5.mjs +35 -0
  159. package/dist/token-goat-chunk-YPBZYQRV.mjs +40 -0
  160. package/dist/{token-goat-chunk-HUYUHODZ.mjs → token-goat-chunk-YY6AVUFL.mjs} +13246 -12247
  161. package/dist/token-goat-chunk-Z7N5OMMP.mjs +98 -0
  162. package/dist/{token-goat-chunk-BGHSSJUA.mjs → token-goat-chunk-ZFOBKUWH.mjs} +1 -1
  163. package/dist/token-goat-chunk-ZMBL6UVV.mjs +110 -0
  164. package/dist/token-goat-chunk-ZNMNUPAZ.mjs +938 -0
  165. package/dist/token-goat-hook-client.cjs +87 -23
  166. package/dist/token-goat-hook-client.mjs +3 -3
  167. package/dist/token-goat-hook.mjs +76 -31
  168. package/dist/token-goat.core.mjs +6 -6
  169. package/docs/C4_RUNTIME_ARCHITECTURE.md +2 -2
  170. package/docs/cli.md +113 -60
  171. package/docs/install.md +63 -20
  172. package/docs/security.md +10 -8
  173. package/package.json +18 -7
  174. package/scripts/verify-native-dist.mjs +386 -0
  175. package/dist/token-goat-chunk-2NNUA2LR.mjs +0 -143
  176. package/dist/token-goat-chunk-44BM7DDQ.mjs +0 -294
  177. package/dist/token-goat-chunk-BGYOZ6DO.mjs +0 -24
  178. package/dist/token-goat-chunk-C5YPNZKQ.mjs +0 -318
  179. package/dist/token-goat-chunk-CIQ2FTY3.mjs +0 -1990
  180. package/dist/token-goat-chunk-D4ETHJD6.mjs +0 -246
  181. package/dist/token-goat-chunk-DNRPXHYH.mjs +0 -1062
  182. package/dist/token-goat-chunk-EUSDXRXU.mjs +0 -1429
  183. package/dist/token-goat-chunk-FTP4U4QA.mjs +0 -64
  184. package/dist/token-goat-chunk-G7IWXCFF.mjs +0 -456
  185. package/dist/token-goat-chunk-GLXRTTLE.mjs +0 -36
  186. package/dist/token-goat-chunk-H5J3C56Q.mjs +0 -1511
  187. package/dist/token-goat-chunk-IVWI2OCK.mjs +0 -368
  188. package/dist/token-goat-chunk-JTKCQMQA.mjs +0 -418
  189. package/dist/token-goat-chunk-LZNBQTDK.mjs +0 -26
  190. package/dist/token-goat-chunk-ORNFMIER.mjs +0 -295
  191. package/dist/token-goat-chunk-RTMVLHWX.mjs +0 -7113
  192. package/dist/token-goat-chunk-VQ6F6T7X.mjs +0 -36
  193. package/dist/token-goat-chunk-VZQEWRVH.mjs +0 -312
  194. package/dist/token-goat-chunk-XYWPOSCN.mjs +0 -807
  195. package/dist/token-goat-chunk-YAGLESS3.mjs +0 -23
  196. package/dist/token-goat-chunk-YKSRAWUU.mjs +0 -33
  197. package/dist/token-goat-chunk-Z6KBMOBI.mjs +0 -53
@@ -1,368 +0,0 @@
1
- import { createRequire as __cjsRequire } from 'node:module';
2
- const require = __cjsRequire(import.meta.url);
3
- import {
4
- deliveredOutputBytes
5
- } from "./token-goat-chunk-L7H3U27L.mjs";
6
- import {
7
- PassthroughFilter,
8
- compressedTokensSaved,
9
- deliverCompressed,
10
- dispatchArgv,
11
- filterByName,
12
- selectFilter
13
- } from "./token-goat-chunk-ZER6EAXZ.mjs";
14
- import {
15
- storeBashOutputSync
16
- } from "./token-goat-chunk-O6I2XU4P.mjs";
17
- import {
18
- capTokens,
19
- shlexSplit
20
- } from "./token-goat-chunk-2WOUYWLM.mjs";
21
- import {
22
- recordStat
23
- } from "./token-goat-chunk-CIQ2FTY3.mjs";
24
- import {
25
- loadConfig
26
- } from "./token-goat-chunk-TGOXNAHW.mjs";
27
- import {
28
- init_define_import_meta_env
29
- } from "./token-goat-chunk-A37V4PBF.mjs";
30
-
31
- // src/bash_runner.ts
32
- init_define_import_meta_env();
33
- import { spawn, spawnSync } from "node:child_process";
34
- import * as os from "node:os";
35
-
36
- // src/shell.ts
37
- init_define_import_meta_env();
38
- import * as fs from "node:fs";
39
- import * as path from "node:path";
40
- var WSL_LAUNCHER_SEGMENTS = /* @__PURE__ */ new Set(["system32", "syswow64", "windowsapps"]);
41
- function isExecutable(p) {
42
- try {
43
- return fs.statSync(p).isFile();
44
- } catch {
45
- return false;
46
- }
47
- }
48
- function isWslLauncherDir(dir) {
49
- return dir.toLowerCase().split(/[\\/]+/).some((seg) => WSL_LAUNCHER_SEGMENTS.has(seg));
50
- }
51
- function locateBashOnPath(pathValue, isExe = isExecutable) {
52
- for (const dir of (pathValue ?? "").split(path.delimiter)) {
53
- if (!dir || isWslLauncherDir(dir)) continue;
54
- const cand = path.join(dir, "bash.exe");
55
- if (isExe(cand)) return cand;
56
- }
57
- return null;
58
- }
59
- function knownGitBashPaths() {
60
- const localPrograms = process.env["LOCALAPPDATA"] ? path.join(process.env["LOCALAPPDATA"], "Programs") : void 0;
61
- const bases = [
62
- process.env["ProgramFiles"],
63
- process.env["ProgramFiles(x86)"],
64
- process.env["ProgramW6432"],
65
- localPrograms
66
- ];
67
- const out = [];
68
- for (const base of bases) {
69
- if (!base) continue;
70
- out.push(path.join(base, "Git", "bin", "bash.exe"));
71
- out.push(path.join(base, "Git", "usr", "bin", "bash.exe"));
72
- }
73
- return out;
74
- }
75
- function resolveWindowsBash() {
76
- if (process.platform !== "win32") return null;
77
- const override = process.env["TOKEN_GOAT_BASH"];
78
- if (override && isExecutable(override)) return override;
79
- const onPath = locateBashOnPath(process.env["PATH"]);
80
- if (onPath) return onPath;
81
- for (const cand of knownGitBashPaths()) {
82
- if (isExecutable(cand)) return cand;
83
- }
84
- return null;
85
- }
86
- function wrappedShell() {
87
- if (process.platform !== "win32") return true;
88
- return resolveWindowsBash() ?? true;
89
- }
90
- function canRunWrappedShell() {
91
- return process.platform !== "win32" || resolveWindowsBash() !== null;
92
- }
93
- function resolvePowerShell(env = process.env, isExe = isExecutable) {
94
- const override = env["TOKEN_GOAT_POWERSHELL"] || env["TOKEN_GOAT_PWSH"];
95
- if (override && isExe(override)) return override;
96
- if (process.platform === "win32") {
97
- for (const dir of (env["PATH"] ?? "").split(path.delimiter)) {
98
- if (!dir) continue;
99
- const cand = path.join(dir, "pwsh.exe");
100
- if (isExe(cand)) return cand;
101
- }
102
- const systemRoot = env["SystemRoot"] ?? env["windir"] ?? "C:\\Windows";
103
- const defaultPs = path.join(systemRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
104
- if (isExe(defaultPs)) return defaultPs;
105
- return "powershell.exe";
106
- }
107
- for (const dir of (env["PATH"] ?? "").split(path.delimiter)) {
108
- if (!dir) continue;
109
- const cand = path.join(dir, "pwsh");
110
- if (isExe(cand)) return cand;
111
- }
112
- for (const cand of ["/usr/bin/pwsh", "/usr/local/bin/pwsh", "/opt/microsoft/powershell/7/pwsh"]) {
113
- if (isExe(cand)) return cand;
114
- }
115
- return "pwsh";
116
- }
117
- function canRunPowerShell(env = process.env, isExe = isExecutable) {
118
- if (process.platform === "win32") return true;
119
- try {
120
- const ps = resolvePowerShell(env, isExe);
121
- return isExe(ps);
122
- } catch {
123
- return false;
124
- }
125
- }
126
-
127
- // src/bash_runner.ts
128
- var DEFAULT_TIMEOUT_SECONDS = 600;
129
- var MAX_CAPTURE_BYTES = 32 * 1024 * 1024;
130
- var MIN_RECORD_STAT_BYTES = 32;
131
- var DEFAULT_HEARTBEAT_INTERVAL_MS = 15e3;
132
- var CaptureSink = class {
133
- constructor(cap) {
134
- this.cap = cap;
135
- }
136
- cap;
137
- chunks = [];
138
- bytes = 0;
139
- overflowed = false;
140
- push(chunk) {
141
- if (this.overflowed) return;
142
- this.bytes += chunk.length;
143
- if (this.bytes > this.cap) {
144
- this.overflowed = true;
145
- return;
146
- }
147
- this.chunks.push(chunk);
148
- }
149
- text() {
150
- return decode(Buffer.concat(this.chunks));
151
- }
152
- };
153
- function scheduleHeartbeat(startTime, intervalMs, write) {
154
- let interval = intervalMs;
155
- let timer;
156
- const arm = () => {
157
- timer = setTimeout(() => {
158
- write(Math.round((Date.now() - startTime) / 1e3));
159
- interval *= 2;
160
- arm();
161
- }, interval);
162
- timer.unref();
163
- };
164
- arm();
165
- return {
166
- onOutput: () => {
167
- interval = intervalMs;
168
- if (timer !== void 0) clearTimeout(timer);
169
- arm();
170
- },
171
- stop: () => {
172
- if (timer !== void 0) clearTimeout(timer);
173
- }
174
- };
175
- }
176
- function resolveFilter(command, filterName, cwd) {
177
- const effectiveCwd = cwd ?? process.cwd();
178
- let split;
179
- try {
180
- split = shlexSplit(command);
181
- } catch {
182
- split = null;
183
- }
184
- const argv = split === null ? [command] : dispatchArgv(split, effectiveCwd).argv;
185
- if (filterName) {
186
- const named = filterByName(filterName);
187
- if (named !== null) return { filter: named, argv };
188
- }
189
- if (split === null) return { filter: null, argv };
190
- return { filter: selectFilter(split, effectiveCwd), argv };
191
- }
192
- function baseSpawnOptions(timeout, cwd) {
193
- return { timeout: timeout * 1e3, cwd };
194
- }
195
- function spawnTarget(command, nativeShell, shellType) {
196
- if (shellType === "pwsh" || shellType === "powershell") {
197
- const ps = resolvePowerShell();
198
- return {
199
- file: ps,
200
- args: [
201
- "-NoProfile",
202
- "-NonInteractive",
203
- "-Command",
204
- "$c = $env:TG_CMD; Remove-Item Env:\\TG_CMD -ErrorAction SilentlyContinue; [scriptblock]::Create($c).Invoke(); if ($LASTEXITCODE -ne $null -and $LASTEXITCODE -ne 0) { exit $LASTEXITCODE }"
205
- ],
206
- shell: false,
207
- cmdEnv: { TG_CMD: command }
208
- };
209
- }
210
- const shell = nativeShell || shellType === "native" ? true : wrappedShell();
211
- if (typeof shell === "string") return { file: shell, args: ["-c", 'eval "$TG_CMD"'], shell: false, cmdEnv: { TG_CMD: command } };
212
- return { file: command, args: [], shell };
213
- }
214
- function mergeSpawnEnv(env, cmdEnv) {
215
- return cmdEnv ? { ...env ?? process.env, ...cmdEnv } : env;
216
- }
217
- function signalExitCode(signal) {
218
- const num = os.constants.signals[signal];
219
- return 128 + (num ?? 0);
220
- }
221
- function resolveProfile(explicit) {
222
- if (explicit) return explicit;
223
- try {
224
- const p = loadConfig().compression.profile;
225
- return p && p !== "auto" ? p : "balanced";
226
- } catch {
227
- return "balanced";
228
- }
229
- }
230
- function resolveCompressLimits() {
231
- try {
232
- const bc = loadConfig().bash_compress;
233
- return { maxLines: bc.max_lines, maxBytes: bc.max_bytes };
234
- } catch {
235
- return { maxLines: 1e3, maxBytes: 64 * 1024 };
236
- }
237
- }
238
- async function run(command, opts = {}) {
239
- const timeout = opts.timeout ?? DEFAULT_TIMEOUT_SECONDS;
240
- const { filter, argv } = resolveFilter(command, opts.filterName, opts.cwd);
241
- if (filter === null) {
242
- if ((opts.maxTokens ?? 0) > 0 || opts.quietSuccess) {
243
- return wrapAndCompress(command, argv, new PassthroughFilter(), timeout, resolveProfile(opts.compressionProfile), opts);
244
- }
245
- return passthrough(command, timeout, opts.cwd, opts.env, opts.nativeShell, opts.shellType);
246
- }
247
- return wrapAndCompress(command, argv, filter, timeout, resolveProfile(opts.compressionProfile), opts);
248
- }
249
- function runRaw(command, timeout = DEFAULT_TIMEOUT_SECONDS, nativeShell, shellType) {
250
- return passthrough(command, timeout, void 0, void 0, nativeShell, shellType);
251
- }
252
- function passthrough(command, timeout, cwd, env, nativeShell, shellType) {
253
- const { file, args, shell, cmdEnv } = spawnTarget(command, nativeShell, shellType);
254
- const result = spawnSync(file, args, {
255
- ...baseSpawnOptions(timeout, cwd),
256
- shell,
257
- env: mergeSpawnEnv(env, cmdEnv),
258
- stdio: "inherit"
259
- });
260
- if (isTimeout(result.error)) return 124;
261
- if (result.status !== null) return result.status;
262
- if (result.signal) return signalExitCode(result.signal);
263
- return 0;
264
- }
265
- function isTimeout(error) {
266
- return !!error && error.code === "ETIMEDOUT";
267
- }
268
- function decode(buf) {
269
- if (buf == null) return "";
270
- return typeof buf === "string" ? buf : buf.toString("utf8");
271
- }
272
- async function wrapAndCompress(command, argv, filter, timeout, profile, opts) {
273
- const writeStdout = opts.writeStdout ?? ((s) => process.stdout.write(s));
274
- const writeStderr = opts.writeStderr ?? ((s) => process.stderr.write(s));
275
- const startTime = Date.now();
276
- const { file, args, shell, cmdEnv } = spawnTarget(command, opts.nativeShell, opts.shellType);
277
- const stdoutSink = new CaptureSink(MAX_CAPTURE_BYTES);
278
- const stderrSink = new CaptureSink(MAX_CAPTURE_BYTES);
279
- let timedOut = false;
280
- let overflowed = false;
281
- const child = spawn(file, args, { cwd: opts.cwd, shell, env: mergeSpawnEnv(opts.env, cmdEnv), stdio: ["ignore", "pipe", "pipe"] });
282
- const heartbeat = scheduleHeartbeat(startTime, opts.heartbeatIntervalMs ?? DEFAULT_HEARTBEAT_INTERVAL_MS, (elapsed) => {
283
- writeStderr(`[token-goat compress] still running, ${elapsed}s elapsed
284
- `);
285
- });
286
- const timeoutHandle = setTimeout(() => {
287
- timedOut = true;
288
- child.kill();
289
- }, timeout * 1e3);
290
- const onData = (sink) => (chunk) => {
291
- heartbeat.onOutput();
292
- sink.push(chunk);
293
- if (sink.overflowed) {
294
- overflowed = true;
295
- child.kill();
296
- }
297
- };
298
- child.stdout?.on("data", onData(stdoutSink));
299
- child.stderr?.on("data", onData(stderrSink));
300
- const { code, signal } = await new Promise((resolve) => {
301
- child.on("close", (code2, signal2) => resolve({ code: code2, signal: signal2 }));
302
- });
303
- heartbeat.stop();
304
- clearTimeout(timeoutHandle);
305
- let stdoutText = stdoutSink.text();
306
- let stderrText = stderrSink.text();
307
- if (overflowed) {
308
- stdoutText += `
309
- [token-goat: capture capped at ${MAX_CAPTURE_BYTES / (1024 * 1024)} MiB]`;
310
- }
311
- let exitCode;
312
- if (timedOut) {
313
- exitCode = 124;
314
- stderrText = (stderrText ? stderrText + "\n" : "") + `[token-goat: command exceeded ${timeout}s timeout and was killed]`;
315
- } else if (code !== null) {
316
- exitCode = code;
317
- } else if (signal) {
318
- exitCode = signalExitCode(signal);
319
- } else {
320
- exitCode = 0;
321
- }
322
- if (opts.quietSuccess && exitCode === 0) {
323
- const duration = ((Date.now() - startTime) / 1e3).toFixed(1) + "s";
324
- const fullRaw = (stdoutText + (stderrText ? "\n" + stderrText : "")).trim();
325
- const id = storeBashOutputSync(command, fullRaw, 0, opts.cwd ?? null);
326
- const summary = `[tg: ok] ${command} (${duration}, recall: token-goat bash-output ${id})
327
- `;
328
- writeStdout(summary);
329
- return 0;
330
- }
331
- const limits = resolveCompressLimits();
332
- const { applied, marker, compressed, text: delivered } = deliverCompressed(filter, stdoutText, stderrText, exitCode, argv, {
333
- compressionProfile: profile,
334
- maxLines: limits.maxLines,
335
- maxBytes: limits.maxBytes
336
- });
337
- let text = delivered;
338
- const maxTokens = opts.maxTokens ?? 0;
339
- if (maxTokens > 0) text = capTokens(text, maxTokens);
340
- const capped = text !== delivered;
341
- let body = text + marker;
342
- if (applied || capped) {
343
- const fullRaw = (stdoutText + (stderrText ? "\n" + stderrText : "")).trim();
344
- const recallId = storeBashOutputSync(command, fullRaw, exitCode, opts.cwd ?? null);
345
- body += "\n[token-goat] full output: bash-output " + recallId + " --full";
346
- if (opts.capHint) body += "\n[token-goat] " + opts.capHint;
347
- }
348
- writeStdout(body.endsWith("\n") ? body : body + "\n");
349
- if (applied) recordSavings(compressed);
350
- return exitCode;
351
- }
352
- function recordSavings(result) {
353
- const delivered = deliveredOutputBytes(result.originalBytes);
354
- const bytesSaved = Math.max(0, delivered - result.compressedBytes);
355
- if (bytesSaved < MIN_RECORD_STAT_BYTES) return;
356
- recordStat(`bash_compress:${result.filterName}`, bytesSaved, compressedTokensSaved(bytesSaved));
357
- }
358
-
359
- export {
360
- canRunWrappedShell,
361
- canRunPowerShell,
362
- DEFAULT_TIMEOUT_SECONDS,
363
- MAX_CAPTURE_BYTES,
364
- DEFAULT_HEARTBEAT_INTERVAL_MS,
365
- resolveFilter,
366
- run,
367
- runRaw
368
- };