token-goat 2.9.18 → 2.9.20

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 (55) hide show
  1. package/README.md +5 -1
  2. package/dist/token-goat-chunk-2CLWWRFR.mjs +26 -0
  3. package/dist/{token-goat-chunk-LTYWLKAG.mjs → token-goat-chunk-2Q3N5KUQ.mjs} +1283 -1012
  4. package/dist/{token-goat-chunk-6LGVCCMY.mjs → token-goat-chunk-3OGZOFLS.mjs} +26 -16
  5. package/dist/{token-goat-chunk-QML7ITHJ.mjs → token-goat-chunk-3VTJC7KU.mjs} +693 -438
  6. package/dist/{token-goat-chunk-QIDLJGXZ.mjs → token-goat-chunk-6LJNM4ZZ.mjs} +29 -5
  7. package/dist/{token-goat-chunk-CA6V36KW.mjs → token-goat-chunk-74ADIZZX.mjs} +68 -50
  8. package/dist/{token-goat-chunk-52IMXJ2L.mjs → token-goat-chunk-ASTBLE2R.mjs} +4 -4
  9. package/dist/token-goat-chunk-AU2K2QHR.mjs +20 -0
  10. package/dist/token-goat-chunk-BQIC3AVL.mjs +292 -0
  11. package/dist/token-goat-chunk-CBXLE54Q.mjs +30 -0
  12. package/dist/{token-goat-chunk-LMFO66YD.mjs → token-goat-chunk-DA43OE7L.mjs} +1 -1
  13. package/dist/{token-goat-chunk-ARNRFR6M.mjs → token-goat-chunk-DNAF26L4.mjs} +1 -1
  14. package/dist/{token-goat-chunk-BFEWOMA3.mjs → token-goat-chunk-DPHJU3P7.mjs} +1186 -4573
  15. package/dist/{token-goat-chunk-OWSJJY5R.mjs → token-goat-chunk-DW4VKU7D.mjs} +5 -5
  16. package/dist/token-goat-chunk-DYUBZZNP.mjs +198 -0
  17. package/dist/{token-goat-chunk-W3E65QOW.mjs → token-goat-chunk-ERGHDBC6.mjs} +310 -47
  18. package/dist/token-goat-chunk-EUZKG7ZO.mjs +304 -0
  19. package/dist/{token-goat-chunk-5MMJAJF3.mjs → token-goat-chunk-FWYZVS3E.mjs} +324 -31
  20. package/dist/token-goat-chunk-G6SYQQO7.mjs +41 -0
  21. package/dist/{token-goat-chunk-YQZE5NS6.mjs → token-goat-chunk-GIPMOGOI.mjs} +6 -6
  22. package/dist/{token-goat-chunk-BWCRC23L.mjs → token-goat-chunk-H4VC5TR6.mjs} +29 -20
  23. package/dist/{token-goat-chunk-V3ZWBEHM.mjs → token-goat-chunk-I5PQRHFE.mjs} +103 -24
  24. package/dist/token-goat-chunk-ITWYDOKP.mjs +341 -0
  25. package/dist/{token-goat-chunk-I67CPTDD.mjs → token-goat-chunk-K3ZNBN4U.mjs} +19 -14
  26. package/dist/{token-goat-chunk-HPOFR6QX.mjs → token-goat-chunk-K5XPIUVB.mjs} +222 -61
  27. package/dist/token-goat-chunk-KT75DZK4.mjs +1989 -0
  28. package/dist/{token-goat-chunk-H3FONFBO.mjs → token-goat-chunk-MNJWG2ZO.mjs} +3 -3
  29. package/dist/{token-goat-chunk-GRI6HWX3.mjs → token-goat-chunk-MUJV7BR3.mjs} +1 -1
  30. package/dist/{token-goat-chunk-ERTXEKB6.mjs → token-goat-chunk-NMTKNYGF.mjs} +2 -0
  31. package/dist/{token-goat-chunk-LA3O4JGD.mjs → token-goat-chunk-ODF7H7WS.mjs} +3 -3
  32. package/dist/{token-goat-chunk-K4DG6EEF.mjs → token-goat-chunk-ONMU7BON.mjs} +7 -8
  33. package/dist/{token-goat-chunk-4EXFN2AW.mjs → token-goat-chunk-OZRSREKR.mjs} +2 -2
  34. package/dist/{token-goat-chunk-EPXNOKIV.mjs → token-goat-chunk-P4XBHZXU.mjs} +2 -2
  35. package/dist/{token-goat-chunk-CTOYJ232.mjs → token-goat-chunk-PAVWB4L4.mjs} +173 -1840
  36. package/dist/{token-goat-chunk-UENEL6KX.mjs → token-goat-chunk-PTW22MRC.mjs} +2 -2
  37. package/dist/token-goat-chunk-SL454EN5.mjs +1816 -0
  38. package/dist/token-goat-chunk-SP5QR2HN.mjs +1379 -0
  39. package/dist/{token-goat-chunk-7VLWNQDQ.mjs → token-goat-chunk-T46UKRFM.mjs} +1 -1
  40. package/dist/{token-goat-chunk-L7VCX33I.mjs → token-goat-chunk-T63VHOL3.mjs} +40 -10
  41. package/dist/token-goat-chunk-VSJGPEBB.mjs +248 -0
  42. package/dist/{token-goat-chunk-HIZXQVNU.mjs → token-goat-chunk-VYXGHGUY.mjs} +4 -4
  43. package/dist/token-goat-chunk-W6QYCWAT.mjs +128 -0
  44. package/dist/{token-goat-chunk-WFX2V3OF.mjs → token-goat-chunk-Z4ALQADF.mjs} +579 -422
  45. package/dist/token-goat-chunk-ZHFM4DHJ.mjs +324 -0
  46. package/dist/token-goat-hook.mjs +18 -12
  47. package/dist/token-goat.core.mjs +29 -20
  48. package/docs/C4_RUNTIME_ARCHITECTURE.md +1 -1
  49. package/docs/cli.md +15 -13
  50. package/docs/install.md +1 -1
  51. package/docs/security.md +2 -2
  52. package/package.json +3 -2
  53. package/dist/token-goat-chunk-2KTZ6J7T.mjs +0 -173
  54. package/dist/token-goat-chunk-34OUJ2IE.mjs +0 -35
  55. package/dist/token-goat-chunk-RHHFEXKF.mjs +0 -93
@@ -0,0 +1,292 @@
1
+ import { createRequire as __cjsRequire } from 'node:module';
2
+ const require = __cjsRequire(import.meta.url);
3
+ import {
4
+ accumulateResidentLine,
5
+ claudeConfigDir,
6
+ createResidentContextStats,
7
+ estimateTokens,
8
+ summarizeResidentContext
9
+ } from "./token-goat-chunk-ZHFM4DHJ.mjs";
10
+ import {
11
+ commandHash,
12
+ getBashOutput,
13
+ normalizeCommandForCacheKey
14
+ } from "./token-goat-chunk-SL454EN5.mjs";
15
+ import {
16
+ init_define_import_meta_env
17
+ } from "./token-goat-chunk-A37V4PBF.mjs";
18
+
19
+ // src/waste.ts
20
+ init_define_import_meta_env();
21
+ import * as fs from "node:fs";
22
+ import * as path from "node:path";
23
+ import { StringDecoder } from "node:string_decoder";
24
+ function projectTranscriptsDir(projectRoot) {
25
+ const rootStr = path.resolve(projectRoot);
26
+ const slug = rootStr.replace(/[^A-Za-z0-9]/g, "-");
27
+ return path.join(claudeConfigDir(), "projects", slug);
28
+ }
29
+ function findLatestTranscript(projectRoot) {
30
+ const dir = projectTranscriptsDir(projectRoot);
31
+ let entries;
32
+ try {
33
+ entries = fs.readdirSync(dir).filter((f) => f.endsWith(".jsonl"));
34
+ } catch {
35
+ return null;
36
+ }
37
+ let best = null;
38
+ for (const name of entries) {
39
+ const full = path.join(dir, name);
40
+ let stat;
41
+ try {
42
+ stat = fs.statSync(full);
43
+ } catch {
44
+ continue;
45
+ }
46
+ if (best === null || stat.mtimeMs > best.mtimeMs) best = { file: full, mtimeMs: stat.mtimeMs };
47
+ }
48
+ return best?.file ?? null;
49
+ }
50
+ function safeStringify(value) {
51
+ try {
52
+ return JSON.stringify(value) ?? "";
53
+ } catch {
54
+ return "";
55
+ }
56
+ }
57
+ var FILE_PATH_TOOLS = /* @__PURE__ */ new Set(["Read", "Edit", "Write", "NotebookEdit", "MultiEdit"]);
58
+ function extractFilePath(name, input) {
59
+ if (!FILE_PATH_TOOLS.has(name)) return null;
60
+ if (input === null || typeof input !== "object") return null;
61
+ const o = input;
62
+ const fp = o["file_path"];
63
+ if (typeof fp === "string") return fp;
64
+ const notebookPath = o["notebook_path"];
65
+ return typeof notebookPath === "string" ? notebookPath : null;
66
+ }
67
+ function extractCommand(name, input) {
68
+ if (name !== "Bash") return null;
69
+ if (input === null || typeof input !== "object") return null;
70
+ const cmd = input["command"];
71
+ return typeof cmd === "string" ? cmd : null;
72
+ }
73
+ function summarizeInput(name, input, filePath, command) {
74
+ if (filePath !== null) return filePath;
75
+ if (command !== null) return command.length > 100 ? `${command.slice(0, 100)}\u2026` : command;
76
+ if (input === null || typeof input !== "object") return "";
77
+ const o = input;
78
+ for (const key of ["pattern", "query", "path", "description", "prompt"]) {
79
+ const v = o[key];
80
+ if (typeof v === "string") return v.length > 100 ? `${v.slice(0, 100)}\u2026` : v;
81
+ }
82
+ const s = safeStringify(input);
83
+ return s.length > 100 ? `${s.slice(0, 100)}\u2026` : s;
84
+ }
85
+ function extractResultText(content) {
86
+ if (typeof content === "string") return content;
87
+ if (Array.isArray(content)) {
88
+ return content.map((b) => {
89
+ if (b !== null && typeof b === "object" && b["type"] === "text") {
90
+ const t = b["text"];
91
+ return typeof t === "string" ? t : "";
92
+ }
93
+ return "";
94
+ }).join("\n");
95
+ }
96
+ return "";
97
+ }
98
+ function* readFileLines(filePath) {
99
+ const fd = fs.openSync(filePath, "r");
100
+ try {
101
+ const buf = Buffer.allocUnsafe(1 << 20);
102
+ const decoder = new StringDecoder("utf8");
103
+ let carry = "";
104
+ for (; ; ) {
105
+ const bytes = fs.readSync(fd, buf, 0, buf.length, null);
106
+ if (bytes === 0) break;
107
+ const lines = (carry + decoder.write(buf.subarray(0, bytes))).split("\n");
108
+ carry = lines.pop() ?? "";
109
+ for (const line of lines) yield line;
110
+ }
111
+ carry += decoder.end();
112
+ if (carry !== "") yield carry;
113
+ } finally {
114
+ fs.closeSync(fd);
115
+ }
116
+ }
117
+ function parseTranscript(transcriptPath) {
118
+ const calls = [];
119
+ const resultTextById = /* @__PURE__ */ new Map();
120
+ const assistantTurns = [];
121
+ const resident = createResidentContextStats();
122
+ let seq = 0;
123
+ for (const line of readFileLines(transcriptPath)) {
124
+ const trimmed = line.trim();
125
+ if (trimmed === "") continue;
126
+ let obj;
127
+ try {
128
+ obj = JSON.parse(trimmed);
129
+ } catch {
130
+ continue;
131
+ }
132
+ if (obj === null || typeof obj !== "object") continue;
133
+ const o = obj;
134
+ accumulateResidentLine(resident, o, trimmed.length);
135
+ const message = o["message"];
136
+ if (message === null || typeof message !== "object") continue;
137
+ const messageObj = message;
138
+ const content = messageObj["content"];
139
+ if (!Array.isArray(content)) continue;
140
+ const role = typeof messageObj["role"] === "string" ? messageObj["role"] : null;
141
+ const cwd = typeof o["cwd"] === "string" ? o["cwd"] : null;
142
+ let turnTextTokens = 0;
143
+ let sawTextBlock = false;
144
+ for (const block of content) {
145
+ if (block === null || typeof block !== "object") continue;
146
+ const b = block;
147
+ if (b["type"] === "tool_use" && typeof b["id"] === "string" && typeof b["name"] === "string") {
148
+ const name = b["name"];
149
+ const input = b["input"];
150
+ const filePath = extractFilePath(name, input);
151
+ const command = extractCommand(name, input);
152
+ calls.push({
153
+ seq: seq++,
154
+ id: b["id"],
155
+ name,
156
+ input,
157
+ filePath,
158
+ command,
159
+ summary: summarizeInput(name, input, filePath, command),
160
+ cwd
161
+ });
162
+ } else if (b["type"] === "tool_result" && typeof b["tool_use_id"] === "string") {
163
+ const id = b["tool_use_id"];
164
+ if (!resultTextById.has(id)) {
165
+ resultTextById.set(id, extractResultText(b["content"]));
166
+ }
167
+ } else if (role === "assistant" && b["type"] === "text" && typeof b["text"] === "string") {
168
+ sawTextBlock = true;
169
+ turnTextTokens += estimateTokens(b["text"]);
170
+ }
171
+ }
172
+ if (role === "assistant" && sawTextBlock) assistantTurns.push(turnTextTokens);
173
+ }
174
+ return { calls, resultTextById, assistantTurns, resident };
175
+ }
176
+ function costPerCall(parsed) {
177
+ return parsed.calls.map((c) => ({
178
+ ...c,
179
+ tokens: estimateTokens(parsed.resultTextById.get(c.id) ?? "")
180
+ }));
181
+ }
182
+ function aggregateSortedDesc(pairs) {
183
+ return [...pairs].map(([key, tokens]) => ({ key, tokens })).sort((a, b) => b.tokens - a.tokens);
184
+ }
185
+ function tokensByTool(costs) {
186
+ const m = /* @__PURE__ */ new Map();
187
+ for (const c of costs) m.set(c.name, (m.get(c.name) ?? 0) + c.tokens);
188
+ return aggregateSortedDesc(m);
189
+ }
190
+ function tokensByFile(costs) {
191
+ const m = /* @__PURE__ */ new Map();
192
+ for (const c of costs) {
193
+ if (c.filePath === null) continue;
194
+ m.set(c.filePath, (m.get(c.filePath) ?? 0) + c.tokens);
195
+ }
196
+ return aggregateSortedDesc(m);
197
+ }
198
+ function topExpensiveCalls(costs, n) {
199
+ return [...costs].sort((a, b) => b.tokens - a.tokens).slice(0, n);
200
+ }
201
+ function neverTouchedAgain(costs) {
202
+ const readsByFile = /* @__PURE__ */ new Map();
203
+ for (const c of costs) {
204
+ if (c.name !== "Read" || c.filePath === null) continue;
205
+ const arr = readsByFile.get(c.filePath) ?? [];
206
+ arr.push(c);
207
+ readsByFile.set(c.filePath, arr);
208
+ }
209
+ const out = [];
210
+ for (const [filePath, reads] of readsByFile) {
211
+ const firstSeq = Math.min(...reads.map((r) => r.seq));
212
+ const touchedLater = costs.some((c) => {
213
+ if (c.seq <= firstSeq) return false;
214
+ if (c.filePath === filePath) return true;
215
+ return safeStringify(c.input).includes(filePath);
216
+ });
217
+ if (!touchedLater) {
218
+ out.push({ filePath, tokens: reads.reduce((n, r) => n + r.tokens, 0) });
219
+ }
220
+ }
221
+ return out.sort((a, b) => b.tokens - a.tokens);
222
+ }
223
+ async function repeatedUncompressedBashCommands(costs) {
224
+ const groups = /* @__PURE__ */ new Map();
225
+ for (const c of costs) {
226
+ if (c.name !== "Bash" || c.command === null) continue;
227
+ const norm = normalizeCommandForCacheKey(c.command);
228
+ const arr = groups.get(norm) ?? [];
229
+ arr.push(c);
230
+ groups.set(norm, arr);
231
+ }
232
+ const out = [];
233
+ for (const [norm, calls] of groups) {
234
+ if (calls.length < 2) continue;
235
+ let cached = false;
236
+ for (const c of calls) {
237
+ try {
238
+ const hash = await commandHash(c.command, c.cwd);
239
+ if (getBashOutput(hash) !== null) {
240
+ cached = true;
241
+ break;
242
+ }
243
+ } catch {
244
+ }
245
+ }
246
+ if (cached) continue;
247
+ const totalTokens = calls.reduce((n, c) => n + c.tokens, 0);
248
+ out.push({
249
+ normalized: norm,
250
+ count: calls.length,
251
+ totalTokens,
252
+ avgTokens: Math.round(totalTokens / calls.length)
253
+ });
254
+ }
255
+ return out.sort((a, b) => b.totalTokens - a.totalTokens);
256
+ }
257
+ function assistantOutputCost(assistantTurns) {
258
+ const turnCount = assistantTurns.length;
259
+ let generatedTokens = 0;
260
+ let resendCeilingTokens = 0;
261
+ for (let i = 0; i < turnCount; i++) {
262
+ const tokens = assistantTurns[i] ?? 0;
263
+ generatedTokens += tokens;
264
+ resendCeilingTokens += tokens * (turnCount - 1 - i);
265
+ }
266
+ return { turnCount, generatedTokens, resendCeilingTokens };
267
+ }
268
+ async function buildWasteReport(transcriptPath, opts = {}) {
269
+ const parsed = parseTranscript(transcriptPath);
270
+ const costs = costPerCall(parsed);
271
+ const topN = opts.topN ?? 10;
272
+ return {
273
+ transcriptPath,
274
+ totalTokens: costs.reduce((n, c) => n + c.tokens, 0),
275
+ tokensByTool: tokensByTool(costs),
276
+ tokensByFile: tokensByFile(costs),
277
+ topCalls: topExpensiveCalls(costs, topN).map((c) => ({ seq: c.seq, name: c.name, summary: c.summary, tokens: c.tokens })),
278
+ neverTouchedAgain: neverTouchedAgain(costs),
279
+ repeatedUncompressedBash: await repeatedUncompressedBashCommands(costs),
280
+ assistantOutput: assistantOutputCost(parsed.assistantTurns),
281
+ residentContext: summarizeResidentContext(parsed.resident)
282
+ };
283
+ }
284
+
285
+ export {
286
+ projectTranscriptsDir,
287
+ findLatestTranscript,
288
+ safeStringify,
289
+ extractResultText,
290
+ readFileLines,
291
+ buildWasteReport
292
+ };
@@ -0,0 +1,30 @@
1
+ import { createRequire as __cjsRequire } from 'node:module';
2
+ const require = __cjsRequire(import.meta.url);
3
+ import {
4
+ DEFAULT_HEARTBEAT_INTERVAL_MS,
5
+ DEFAULT_TIMEOUT_SECONDS,
6
+ MAX_CAPTURE_BYTES,
7
+ resolveFilter,
8
+ run,
9
+ runRaw
10
+ } from "./token-goat-chunk-ITWYDOKP.mjs";
11
+ import "./token-goat-chunk-PAVWB4L4.mjs";
12
+ import "./token-goat-chunk-SL454EN5.mjs";
13
+ import "./token-goat-chunk-K5XPIUVB.mjs";
14
+ import "./token-goat-chunk-K3ZNBN4U.mjs";
15
+ import "./token-goat-chunk-T63VHOL3.mjs";
16
+ import "./token-goat-chunk-74ADIZZX.mjs";
17
+ import "./token-goat-chunk-EEIDFMEM.mjs";
18
+ import "./token-goat-chunk-OSUFN2FV.mjs";
19
+ import "./token-goat-chunk-6LJNM4ZZ.mjs";
20
+ import "./token-goat-chunk-NMTKNYGF.mjs";
21
+ import "./token-goat-chunk-GMOUBOX4.mjs";
22
+ import "./token-goat-chunk-A37V4PBF.mjs";
23
+ export {
24
+ DEFAULT_HEARTBEAT_INTERVAL_MS,
25
+ DEFAULT_TIMEOUT_SECONDS,
26
+ MAX_CAPTURE_BYTES,
27
+ resolveFilter,
28
+ run,
29
+ runRaw
30
+ };
@@ -5,7 +5,7 @@ import {
5
5
  } from "./token-goat-chunk-OSUFN2FV.mjs";
6
6
  import {
7
7
  displaySafeText
8
- } from "./token-goat-chunk-ERTXEKB6.mjs";
8
+ } from "./token-goat-chunk-NMTKNYGF.mjs";
9
9
  import {
10
10
  init_define_import_meta_env
11
11
  } from "./token-goat-chunk-A37V4PBF.mjs";
@@ -12,7 +12,7 @@ import {
12
12
  parseOoxmlPart,
13
13
  readOoxmlZip,
14
14
  sortNumberedParts
15
- } from "./token-goat-chunk-7VLWNQDQ.mjs";
15
+ } from "./token-goat-chunk-T46UKRFM.mjs";
16
16
  import {
17
17
  compileGuardedRegex
18
18
  } from "./token-goat-chunk-GMOUBOX4.mjs";