token-goat 2.9.19 → 2.9.21

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 (50) hide show
  1. package/README.md +3 -1
  2. package/dist/{token-goat-chunk-JNIFSVWM.mjs → token-goat-chunk-2ZHD3XCD.mjs} +5 -1
  3. package/dist/{token-goat-chunk-P46RTOAQ.mjs → token-goat-chunk-4IIU3GNS.mjs} +10 -7
  4. package/dist/{token-goat-chunk-2GTB5RBZ.mjs → token-goat-chunk-4V4OZHHV.mjs} +11 -271
  5. package/dist/token-goat-chunk-64OEF3UN.mjs +128 -0
  6. package/dist/{token-goat-chunk-AVRD7SBO.mjs → token-goat-chunk-6AQLZMCO.mjs} +270 -29
  7. package/dist/{token-goat-chunk-LACH3WKS.mjs → token-goat-chunk-BIEDTF6V.mjs} +3 -3
  8. package/dist/{token-goat-chunk-PIAESKVU.mjs → token-goat-chunk-BKHJRFDE.mjs} +568 -429
  9. package/dist/token-goat-chunk-CAHN4QU7.mjs +26 -0
  10. package/dist/{token-goat-chunk-MU6I4IDI.mjs → token-goat-chunk-CMAJGP3E.mjs} +11 -7
  11. package/dist/token-goat-chunk-CMHBTXMY.mjs +41 -0
  12. package/dist/{token-goat-chunk-HPMMLV37.mjs → token-goat-chunk-CZ5ZFJSN.mjs} +256 -341
  13. package/dist/{token-goat-chunk-7DGTVBWP.mjs → token-goat-chunk-DDP64XR7.mjs} +159 -1863
  14. package/dist/{token-goat-chunk-FX5DHKGF.mjs → token-goat-chunk-DISVHOW6.mjs} +4 -4
  15. package/dist/{token-goat-chunk-L2UOJBV4.mjs → token-goat-chunk-FZN77SZD.mjs} +27 -19
  16. package/dist/{token-goat-chunk-DA6TIDLI.mjs → token-goat-chunk-GO7OWONJ.mjs} +18 -13
  17. package/dist/{token-goat-chunk-EP7TCWHM.mjs → token-goat-chunk-IAMIQLCV.mjs} +83 -66
  18. package/dist/{token-goat-chunk-NJKNAC37.mjs → token-goat-chunk-IJUHQX2M.mjs} +1 -1
  19. package/dist/token-goat-chunk-KQI24O4B.mjs +292 -0
  20. package/dist/{token-goat-chunk-GYC34EGE.mjs → token-goat-chunk-KRPJIXCT.mjs} +2 -2
  21. package/dist/token-goat-chunk-KVAXTA2W.mjs +198 -0
  22. package/dist/{token-goat-chunk-FJZ5MZK7.mjs → token-goat-chunk-LK3YOKDF.mjs} +2 -2
  23. package/dist/token-goat-chunk-M43UJVYQ.mjs +1816 -0
  24. package/dist/{token-goat-chunk-SOKAPOOC.mjs → token-goat-chunk-M6CDVASA.mjs} +46 -47
  25. package/dist/token-goat-chunk-MECDAPXB.mjs +1989 -0
  26. package/dist/token-goat-chunk-MEKVJZUN.mjs +324 -0
  27. package/dist/token-goat-chunk-MQACN7VG.mjs +1379 -0
  28. package/dist/{token-goat-chunk-3W5P673V.mjs → token-goat-chunk-MZQORUX5.mjs} +1 -1
  29. package/dist/{token-goat-chunk-DHIXE6GV.mjs → token-goat-chunk-ORRDWKLA.mjs} +1 -1
  30. package/dist/{token-goat-chunk-F24NE3FO.mjs → token-goat-chunk-PCR43MCM.mjs} +2 -2
  31. package/dist/token-goat-chunk-PDYQA3AP.mjs +248 -0
  32. package/dist/{token-goat-chunk-S7RFIX4Y.mjs → token-goat-chunk-PHDNWISV.mjs} +45 -14
  33. package/dist/token-goat-chunk-PPYZS7TI.mjs +304 -0
  34. package/dist/{token-goat-chunk-BTKDZW7F.mjs → token-goat-chunk-QTIRVK3M.mjs} +1 -1
  35. package/dist/{token-goat-chunk-62VJ47WN.mjs → token-goat-chunk-QUM3UNWS.mjs} +5 -5
  36. package/dist/{token-goat-chunk-KWM6KBPA.mjs → token-goat-chunk-SHV6QQAS.mjs} +1 -1
  37. package/dist/{token-goat-chunk-FQYMZASI.mjs → token-goat-chunk-SMUYPBF5.mjs} +796 -4464
  38. package/dist/{token-goat-chunk-ZGQXRYKE.mjs → token-goat-chunk-TGBSMHPM.mjs} +1 -1
  39. package/dist/{token-goat-chunk-EXLLLIC3.mjs → token-goat-chunk-VXKT3W4C.mjs} +3 -3
  40. package/dist/{token-goat-chunk-WKO5TVNA.mjs → token-goat-chunk-XJUPKJF5.mjs} +544 -576
  41. package/dist/{token-goat-chunk-IBTFGZBX.mjs → token-goat-chunk-XQSSPX3X.mjs} +2 -2
  42. package/dist/{token-goat-chunk-VJQBYSXG.mjs → token-goat-chunk-ZJWPJL7O.mjs} +3 -3
  43. package/dist/token-goat-hook.mjs +17 -11
  44. package/dist/token-goat.core.mjs +27 -19
  45. package/docs/C4_RUNTIME_ARCHITECTURE.md +1 -1
  46. package/docs/cli.md +13 -12
  47. package/docs/install.md +1 -1
  48. package/docs/security.md +2 -2
  49. package/package.json +3 -2
  50. package/dist/token-goat-chunk-NYFYIRG3.mjs +0 -35
@@ -0,0 +1,304 @@
1
+ import { createRequire as __cjsRequire } from 'node:module';
2
+ const require = __cjsRequire(import.meta.url);
3
+ import {
4
+ findLatestTranscript,
5
+ readFileLines
6
+ } from "./token-goat-chunk-KQI24O4B.mjs";
7
+ import {
8
+ copilotCliUserRoot,
9
+ readCopilotMcpTools
10
+ } from "./token-goat-chunk-MQACN7VG.mjs";
11
+ import {
12
+ canonicalize
13
+ } from "./token-goat-chunk-TGBSMHPM.mjs";
14
+ import {
15
+ init_define_import_meta_env
16
+ } from "./token-goat-chunk-A37V4PBF.mjs";
17
+
18
+ // src/copilot_waste.ts
19
+ init_define_import_meta_env();
20
+ import * as fs from "node:fs";
21
+ import * as path from "node:path";
22
+ var HOOK_RECORD_TYPES = /* @__PURE__ */ new Set(["hook.start", "hook.end"]);
23
+ function splitInjectedBlocks(transformed) {
24
+ const out = [];
25
+ const blockRe = /<([a-z_][a-z0-9_]*)>([\s\S]*?)<\/\1>/gi;
26
+ let m;
27
+ while ((m = blockRe.exec(transformed)) !== null) {
28
+ const tag = (m[1] ?? "").toLowerCase();
29
+ const body = m[2] ?? "";
30
+ let kind = tag;
31
+ if (tag === "system_reminder") {
32
+ const inner = /^\s*<([a-z_][a-z0-9_]*)>/i.exec(body);
33
+ kind = inner !== null ? `reminder:${(inner[1] ?? "").toLowerCase()}` : "reminder:text";
34
+ }
35
+ out.push({ kind, body: m[0] });
36
+ }
37
+ return out;
38
+ }
39
+ function pathsMatch(a, b) {
40
+ if (a === void 0 || b === void 0 || a === "" || b === "") return false;
41
+ return canonicalize(a) === canonicalize(b);
42
+ }
43
+ function readCopilotWorkspace(sessionDir) {
44
+ const wsFile = path.join(sessionDir, "workspace.yaml");
45
+ let raw;
46
+ try {
47
+ raw = fs.readFileSync(wsFile, "utf8");
48
+ } catch {
49
+ return null;
50
+ }
51
+ let cwd;
52
+ let gitRoot;
53
+ let id;
54
+ for (const line of raw.split("\n")) {
55
+ const trimmed = line.trim();
56
+ if (trimmed.startsWith("cwd:")) {
57
+ cwd = trimmed.slice(4).trim().replace(/^['"]|['"]$/g, "");
58
+ } else if (trimmed.startsWith("git_root:")) {
59
+ gitRoot = trimmed.slice(9).trim().replace(/^['"]|['"]$/g, "");
60
+ } else if (trimmed.startsWith("id:")) {
61
+ id = trimmed.slice(3).trim().replace(/^['"]|['"]$/g, "");
62
+ }
63
+ }
64
+ return { cwd, gitRoot, id };
65
+ }
66
+ function isProcessAlive(pid) {
67
+ if (pid <= 0 || !Number.isInteger(pid)) return false;
68
+ try {
69
+ process.kill(pid, 0);
70
+ return true;
71
+ } catch (err) {
72
+ return err?.code === "EPERM";
73
+ }
74
+ }
75
+ function isCopilotSessionActive(sessionDir, sessionId) {
76
+ const sid = sessionId ?? path.basename(sessionDir);
77
+ const envSessionId = process.env["COPILOT_AGENT_SESSION_ID"];
78
+ if (envSessionId !== void 0 && envSessionId.trim() === sid) {
79
+ return true;
80
+ }
81
+ try {
82
+ const files = fs.readdirSync(sessionDir);
83
+ for (const f of files) {
84
+ if (f.startsWith("inuse.") && f.endsWith(".lock")) {
85
+ const pidStr = f.slice(6, -5);
86
+ const pid = parseInt(pidStr, 10);
87
+ if (!Number.isNaN(pid) && isProcessAlive(pid)) {
88
+ return true;
89
+ }
90
+ }
91
+ }
92
+ } catch {
93
+ }
94
+ try {
95
+ const opLock = path.join(path.dirname(sessionDir), ".session-operation-locks", `${sid}.lock`);
96
+ if (fs.existsSync(opLock)) {
97
+ return true;
98
+ }
99
+ } catch {
100
+ }
101
+ return false;
102
+ }
103
+ function isCopilotTranscript(filePath) {
104
+ if (path.basename(filePath) === "events.jsonl") return true;
105
+ try {
106
+ const fd = fs.openSync(filePath, "r");
107
+ const buf = Buffer.alloc(512);
108
+ const bytesRead = fs.readSync(fd, buf, 0, 512, 0);
109
+ fs.closeSync(fd);
110
+ const prefix = buf.toString("utf8", 0, bytesRead);
111
+ return /"type"\s*:\s*"(?:session\.|user\.message|assistant\.message|tool\.)/.test(prefix);
112
+ } catch {
113
+ return false;
114
+ }
115
+ }
116
+ function findActiveCopilotSession(projectRoot) {
117
+ return findLatestCopilotSession({ projectRoot, onlyActive: true });
118
+ }
119
+ function findLatestCopilotSession(projectRootOrOpts) {
120
+ const opts = typeof projectRootOrOpts === "string" ? { projectRoot: projectRootOrOpts } : projectRootOrOpts ?? {};
121
+ const root = path.join(copilotCliUserRoot(), "session-state");
122
+ const envSessionId = process.env["COPILOT_AGENT_SESSION_ID"];
123
+ if (envSessionId !== void 0 && envSessionId.trim() !== "") {
124
+ const sid = envSessionId.trim();
125
+ const sessionDir = path.join(root, sid);
126
+ const candidate = path.join(sessionDir, "events.jsonl");
127
+ if (fs.existsSync(candidate)) {
128
+ try {
129
+ if (fs.statSync(candidate).isFile()) {
130
+ if (opts.projectRoot !== void 0) {
131
+ const ws = readCopilotWorkspace(sessionDir);
132
+ if (ws && (pathsMatch(ws.cwd, opts.projectRoot) || pathsMatch(ws.gitRoot, opts.projectRoot))) {
133
+ return candidate;
134
+ }
135
+ } else {
136
+ return candidate;
137
+ }
138
+ }
139
+ } catch {
140
+ }
141
+ }
142
+ }
143
+ let entries;
144
+ try {
145
+ entries = fs.readdirSync(root);
146
+ } catch {
147
+ return null;
148
+ }
149
+ let bestActive = null;
150
+ let bestActiveMtime = -Infinity;
151
+ let bestInactive = null;
152
+ let bestInactiveMtime = -Infinity;
153
+ for (const entry of entries) {
154
+ if (entry.startsWith(".")) continue;
155
+ const sessionDir = path.join(root, entry);
156
+ const candidate = path.join(sessionDir, "events.jsonl");
157
+ let st;
158
+ try {
159
+ st = fs.statSync(candidate);
160
+ if (!st.isFile()) continue;
161
+ } catch {
162
+ continue;
163
+ }
164
+ if (opts.projectRoot !== void 0) {
165
+ const ws = readCopilotWorkspace(sessionDir);
166
+ if (!ws || !pathsMatch(ws.cwd, opts.projectRoot) && !pathsMatch(ws.gitRoot, opts.projectRoot)) {
167
+ continue;
168
+ }
169
+ }
170
+ const active = isCopilotSessionActive(sessionDir, entry);
171
+ if (active) {
172
+ if (st.mtimeMs > bestActiveMtime) {
173
+ bestActiveMtime = st.mtimeMs;
174
+ bestActive = candidate;
175
+ }
176
+ } else if (opts.onlyActive !== true) {
177
+ if (st.mtimeMs > bestInactiveMtime) {
178
+ bestInactiveMtime = st.mtimeMs;
179
+ bestInactive = candidate;
180
+ }
181
+ }
182
+ }
183
+ return bestActive ?? (opts.onlyActive === true ? null : bestInactive);
184
+ }
185
+ function findProjectSession(projectRoot) {
186
+ const copilotSession = findActiveCopilotSession(projectRoot) ?? findLatestCopilotSession(projectRoot);
187
+ const claudeTranscript = findLatestTranscript(projectRoot);
188
+ if (copilotSession !== null && claudeTranscript !== null) {
189
+ if (findActiveCopilotSession(projectRoot) !== null) {
190
+ return { path: copilotSession, kind: "copilot" };
191
+ }
192
+ let cpTime = 0;
193
+ let clTime = 0;
194
+ try {
195
+ cpTime = fs.statSync(copilotSession).mtimeMs;
196
+ } catch {
197
+ }
198
+ try {
199
+ clTime = fs.statSync(claudeTranscript).mtimeMs;
200
+ } catch {
201
+ }
202
+ return cpTime >= clTime ? { path: copilotSession, kind: "copilot" } : { path: claudeTranscript, kind: "claude" };
203
+ }
204
+ if (copilotSession !== null) {
205
+ return { path: copilotSession, kind: "copilot" };
206
+ }
207
+ if (claudeTranscript !== null) {
208
+ return { path: claudeTranscript, kind: "claude" };
209
+ }
210
+ return null;
211
+ }
212
+ function readNumber(source, key) {
213
+ const value = source[key];
214
+ return typeof value === "number" && Number.isFinite(value) ? value : 0;
215
+ }
216
+ function buildCopilotWasteReport(eventsPath) {
217
+ const report = {
218
+ sessionPath: eventsPath,
219
+ sessionId: path.basename(path.dirname(eventsPath)),
220
+ turns: 0,
221
+ tokens: null,
222
+ blocks: [],
223
+ compactions: [],
224
+ hookRecordBytes: 0,
225
+ // Measured off the file rather than off a string of its contents: an event log large enough for this number to matter is exactly the one readFileSync cannot return, since V8 caps a string at about 512 MB.
226
+ totalEventBytes: fs.statSync(eventsPath).size,
227
+ // Read through the real resolution chain rather than passed in. Copilot's
228
+ // own COPILOT_CACHE_HOME override is what tests point at a fixture, so the
229
+ // shipping path is the tested path and there is no seam here that only a
230
+ // test ever supplies.
231
+ mcpTools: readCopilotMcpTools()
232
+ };
233
+ const classes = /* @__PURE__ */ new Map();
234
+ const seen = /* @__PURE__ */ new Map();
235
+ for (const line of readFileLines(eventsPath)) {
236
+ const trimmed = line.trim();
237
+ if (trimmed === "") continue;
238
+ let event;
239
+ try {
240
+ event = JSON.parse(trimmed);
241
+ } catch {
242
+ continue;
243
+ }
244
+ const type = typeof event["type"] === "string" ? event["type"] : "";
245
+ const data = event["data"] ?? {};
246
+ if (HOOK_RECORD_TYPES.has(type)) {
247
+ report.hookRecordBytes += Buffer.byteLength(trimmed, "utf-8");
248
+ continue;
249
+ }
250
+ if (type === "session.shutdown") {
251
+ report.tokens = {
252
+ systemTokens: readNumber(data, "systemTokens"),
253
+ toolDefinitionsTokens: readNumber(data, "toolDefinitionsTokens"),
254
+ conversationTokens: readNumber(data, "conversationTokens"),
255
+ currentTokens: readNumber(data, "currentTokens")
256
+ };
257
+ continue;
258
+ }
259
+ if (type === "session.compaction_complete") {
260
+ const summary = typeof data["summaryContent"] === "string" ? data["summaryContent"] : "";
261
+ report.compactions.push({
262
+ trigger: typeof data["trigger"] === "string" ? data["trigger"] : "unknown",
263
+ summaryBytes: Buffer.byteLength(summary, "utf-8"),
264
+ preTokens: readNumber(data, "preCompactionTokens"),
265
+ postTokens: readNumber(data, "postCompactionTokens")
266
+ });
267
+ continue;
268
+ }
269
+ if (type !== "user.message") continue;
270
+ const transformed = typeof data["transformedContent"] === "string" ? data["transformedContent"] : "";
271
+ if (transformed === "") continue;
272
+ report.turns += 1;
273
+ for (const block of splitInjectedBlocks(transformed)) {
274
+ const bytes = Buffer.byteLength(block.body, "utf-8");
275
+ let cls = classes.get(block.kind);
276
+ if (cls === void 0) {
277
+ cls = { kind: block.kind, count: 0, bytes: 0, repeatBytes: 0, repeatCount: 0 };
278
+ classes.set(block.kind, cls);
279
+ }
280
+ cls.count += 1;
281
+ cls.bytes += bytes;
282
+ let bucket = seen.get(block.kind);
283
+ if (bucket === void 0) {
284
+ bucket = /* @__PURE__ */ new Set();
285
+ seen.set(block.kind, bucket);
286
+ }
287
+ if (bucket.has(block.body)) {
288
+ cls.repeatBytes += bytes;
289
+ cls.repeatCount += 1;
290
+ } else {
291
+ bucket.add(block.body);
292
+ }
293
+ }
294
+ }
295
+ report.blocks = [...classes.values()].sort((a, b) => b.bytes - a.bytes);
296
+ return report;
297
+ }
298
+
299
+ export {
300
+ isCopilotTranscript,
301
+ findLatestCopilotSession,
302
+ findProjectSession,
303
+ buildCopilotWasteReport
304
+ };
@@ -12,7 +12,7 @@ import {
12
12
  } from "./token-goat-chunk-AH6QILZM.mjs";
13
13
  import {
14
14
  pushAll
15
- } from "./token-goat-chunk-ZGQXRYKE.mjs";
15
+ } from "./token-goat-chunk-TGBSMHPM.mjs";
16
16
  import {
17
17
  DocumentRefusedError,
18
18
  MAX_DOCUMENT_WORK_MILLIS
@@ -20,17 +20,17 @@ import {
20
20
  setOcrTimeoutForTesting,
21
21
  setTesseractEntryForTesting,
22
22
  verifyOcrLangCache
23
- } from "./token-goat-chunk-F24NE3FO.mjs";
24
- import "./token-goat-chunk-S7RFIX4Y.mjs";
25
- import "./token-goat-chunk-JNIFSVWM.mjs";
23
+ } from "./token-goat-chunk-PCR43MCM.mjs";
24
+ import "./token-goat-chunk-PHDNWISV.mjs";
25
+ import "./token-goat-chunk-2ZHD3XCD.mjs";
26
26
  import {
27
27
  DEFAULT_OCR_LANG,
28
28
  isSupportedOcrLang,
29
29
  resolveOcrLangs
30
- } from "./token-goat-chunk-SOKAPOOC.mjs";
30
+ } from "./token-goat-chunk-M6CDVASA.mjs";
31
31
  import "./token-goat-chunk-EEIDFMEM.mjs";
32
32
  import "./token-goat-chunk-OSUFN2FV.mjs";
33
- import "./token-goat-chunk-ZGQXRYKE.mjs";
33
+ import "./token-goat-chunk-TGBSMHPM.mjs";
34
34
  import "./token-goat-chunk-NMTKNYGF.mjs";
35
35
  import "./token-goat-chunk-GMOUBOX4.mjs";
36
36
  import "./token-goat-chunk-A37V4PBF.mjs";
@@ -5,7 +5,7 @@ import {
5
5
  } from "./token-goat-chunk-3BTK54F3.mjs";
6
6
  import {
7
7
  filtersFilteredToEmptyNotice
8
- } from "./token-goat-chunk-ZGQXRYKE.mjs";
8
+ } from "./token-goat-chunk-TGBSMHPM.mjs";
9
9
  import {
10
10
  displaySafeText
11
11
  } from "./token-goat-chunk-NMTKNYGF.mjs";