token-goat 2.9.19 → 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.
- package/README.md +3 -1
- package/dist/token-goat-chunk-2CLWWRFR.mjs +26 -0
- package/dist/{token-goat-chunk-WKO5TVNA.mjs → token-goat-chunk-2Q3N5KUQ.mjs} +531 -571
- package/dist/{token-goat-chunk-DA6TIDLI.mjs → token-goat-chunk-3OGZOFLS.mjs} +18 -13
- package/dist/{token-goat-chunk-PIAESKVU.mjs → token-goat-chunk-3VTJC7KU.mjs} +568 -429
- package/dist/{token-goat-chunk-ZGQXRYKE.mjs → token-goat-chunk-6LJNM4ZZ.mjs} +1 -1
- package/dist/{token-goat-chunk-SOKAPOOC.mjs → token-goat-chunk-74ADIZZX.mjs} +46 -47
- package/dist/{token-goat-chunk-LACH3WKS.mjs → token-goat-chunk-ASTBLE2R.mjs} +3 -3
- package/dist/{token-goat-chunk-VJQBYSXG.mjs → token-goat-chunk-AU2K2QHR.mjs} +3 -3
- package/dist/token-goat-chunk-BQIC3AVL.mjs +292 -0
- package/dist/{token-goat-chunk-P46RTOAQ.mjs → token-goat-chunk-CBXLE54Q.mjs} +10 -7
- package/dist/{token-goat-chunk-3W5P673V.mjs → token-goat-chunk-DNAF26L4.mjs} +1 -1
- package/dist/{token-goat-chunk-FQYMZASI.mjs → token-goat-chunk-DPHJU3P7.mjs} +789 -4456
- package/dist/{token-goat-chunk-FX5DHKGF.mjs → token-goat-chunk-DW4VKU7D.mjs} +4 -4
- package/dist/token-goat-chunk-DYUBZZNP.mjs +198 -0
- package/dist/{token-goat-chunk-EP7TCWHM.mjs → token-goat-chunk-ERGHDBC6.mjs} +70 -65
- package/dist/token-goat-chunk-EUZKG7ZO.mjs +304 -0
- package/dist/{token-goat-chunk-AVRD7SBO.mjs → token-goat-chunk-FWYZVS3E.mjs} +270 -29
- package/dist/token-goat-chunk-G6SYQQO7.mjs +41 -0
- package/dist/{token-goat-chunk-62VJ47WN.mjs → token-goat-chunk-GIPMOGOI.mjs} +5 -5
- package/dist/{token-goat-chunk-L2UOJBV4.mjs → token-goat-chunk-H4VC5TR6.mjs} +27 -19
- package/dist/{token-goat-chunk-2GTB5RBZ.mjs → token-goat-chunk-I5PQRHFE.mjs} +11 -271
- package/dist/{token-goat-chunk-MU6I4IDI.mjs → token-goat-chunk-ITWYDOKP.mjs} +11 -7
- package/dist/{token-goat-chunk-IBTFGZBX.mjs → token-goat-chunk-K3ZNBN4U.mjs} +2 -2
- package/dist/{token-goat-chunk-S7RFIX4Y.mjs → token-goat-chunk-K5XPIUVB.mjs} +45 -14
- package/dist/token-goat-chunk-KT75DZK4.mjs +1989 -0
- package/dist/{token-goat-chunk-F24NE3FO.mjs → token-goat-chunk-MNJWG2ZO.mjs} +2 -2
- package/dist/{token-goat-chunk-NJKNAC37.mjs → token-goat-chunk-MUJV7BR3.mjs} +1 -1
- package/dist/{token-goat-chunk-FJZ5MZK7.mjs → token-goat-chunk-ODF7H7WS.mjs} +2 -2
- package/dist/{token-goat-chunk-GYC34EGE.mjs → token-goat-chunk-ONMU7BON.mjs} +2 -2
- package/dist/{token-goat-chunk-KWM6KBPA.mjs → token-goat-chunk-P4XBHZXU.mjs} +1 -1
- package/dist/{token-goat-chunk-7DGTVBWP.mjs → token-goat-chunk-PAVWB4L4.mjs} +159 -1863
- package/dist/{token-goat-chunk-DHIXE6GV.mjs → token-goat-chunk-PTW22MRC.mjs} +1 -1
- package/dist/token-goat-chunk-SL454EN5.mjs +1816 -0
- package/dist/token-goat-chunk-SP5QR2HN.mjs +1379 -0
- package/dist/{token-goat-chunk-BTKDZW7F.mjs → token-goat-chunk-T46UKRFM.mjs} +1 -1
- package/dist/{token-goat-chunk-JNIFSVWM.mjs → token-goat-chunk-T63VHOL3.mjs} +5 -1
- package/dist/token-goat-chunk-VSJGPEBB.mjs +248 -0
- package/dist/{token-goat-chunk-EXLLLIC3.mjs → token-goat-chunk-VYXGHGUY.mjs} +3 -3
- package/dist/token-goat-chunk-W6QYCWAT.mjs +128 -0
- package/dist/{token-goat-chunk-HPMMLV37.mjs → token-goat-chunk-Z4ALQADF.mjs} +256 -341
- package/dist/token-goat-chunk-ZHFM4DHJ.mjs +324 -0
- package/dist/token-goat-hook.mjs +17 -11
- package/dist/token-goat.core.mjs +27 -19
- package/docs/C4_RUNTIME_ARCHITECTURE.md +1 -1
- package/docs/cli.md +13 -12
- package/docs/install.md +1 -1
- package/docs/security.md +2 -2
- package/package.json +3 -2
- 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-BQIC3AVL.mjs";
|
|
7
|
+
import {
|
|
8
|
+
copilotCliUserRoot,
|
|
9
|
+
readCopilotMcpTools
|
|
10
|
+
} from "./token-goat-chunk-SP5QR2HN.mjs";
|
|
11
|
+
import {
|
|
12
|
+
canonicalize
|
|
13
|
+
} from "./token-goat-chunk-6LJNM4ZZ.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
|
+
};
|