tokless 0.0.0-stage → 0.4.1

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.
@@ -0,0 +1,103 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const os = require('os');
5
+ const path = require('path');
6
+
7
+ function resolveHomeDir(options = {}) {
8
+ return options.homeDir || os.homedir();
9
+ }
10
+
11
+ function shouldForceCopy(sourceDir, explicitCopy) {
12
+ if (explicitCopy) return true;
13
+ const normalized = String(sourceDir || '').replace(/\\/g, '/');
14
+ return normalized.includes('/_npx/') || normalized.includes('/node_modules/');
15
+ }
16
+
17
+ function removePathSafe(targetPath) {
18
+ let stat;
19
+ try {
20
+ stat = fs.lstatSync(targetPath);
21
+ } catch (err) {
22
+ if (err.code === 'ENOENT') return;
23
+ throw err;
24
+ }
25
+
26
+ if (stat.isSymbolicLink()) {
27
+ fs.unlinkSync(targetPath);
28
+ return;
29
+ }
30
+
31
+ if (stat.isDirectory()) {
32
+ for (const entry of fs.readdirSync(targetPath)) {
33
+ removePathSafe(path.join(targetPath, entry));
34
+ }
35
+ fs.rmdirSync(targetPath);
36
+ return;
37
+ }
38
+
39
+ fs.unlinkSync(targetPath);
40
+ }
41
+
42
+ function copyDirRecursive(srcDir, destDir) {
43
+ fs.mkdirSync(destDir, { recursive: true });
44
+ for (const entry of fs.readdirSync(srcDir, { withFileTypes: true })) {
45
+ if (entry.name === '.git' || entry.name === 'node_modules' || entry.name === 'dist') {
46
+ continue;
47
+ }
48
+ const srcPath = path.join(srcDir, entry.name);
49
+ const destPath = path.join(destDir, entry.name);
50
+ if (entry.isDirectory()) {
51
+ copyDirRecursive(srcPath, destPath);
52
+ } else if (entry.isFile()) {
53
+ fs.copyFileSync(srcPath, destPath);
54
+ }
55
+ }
56
+ }
57
+
58
+ function linkOrCopyDir(srcDir, destDir, forceCopy) {
59
+ if (!fs.existsSync(srcDir)) return 'skipped';
60
+
61
+ if (!forceCopy) {
62
+ const linkType = process.platform === 'win32' ? 'junction' : 'dir';
63
+ try {
64
+ fs.symlinkSync(srcDir, destDir, linkType);
65
+ return `link(${linkType})`;
66
+ } catch (_) {}
67
+ }
68
+
69
+ copyDirRecursive(srcDir, destDir);
70
+ return 'copy';
71
+ }
72
+
73
+ function resolveNodeBinary() {
74
+ // 陷阱: macOS Homebrew 升级 Node 会使 Cellar 版本化路径失效,优先复用稳定软链
75
+ if (process.platform === 'darwin') {
76
+ for (const stable of ['/opt/homebrew/bin/node', '/usr/local/bin/node']) {
77
+ try {
78
+ if (fs.existsSync(stable) && fs.realpathSync(stable) === fs.realpathSync(process.execPath)) {
79
+ return stable;
80
+ }
81
+ } catch (_) {}
82
+ }
83
+ }
84
+ const argv0 = process.argv[0];
85
+ if (argv0 && path.isAbsolute(argv0) && fs.existsSync(argv0)) {
86
+ return argv0;
87
+ }
88
+ return process.execPath;
89
+ }
90
+
91
+ function quoteCommandBinary(binPath) {
92
+ return `"${String(binPath).replace(/"/g, '\\"')}"`;
93
+ }
94
+
95
+ module.exports = {
96
+ resolveHomeDir,
97
+ shouldForceCopy,
98
+ removePathSafe,
99
+ copyDirRecursive,
100
+ linkOrCopyDir,
101
+ resolveNodeBinary,
102
+ quoteCommandBinary,
103
+ };
@@ -0,0 +1,114 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ const { analyzeSession } = require('./lib/analyzer');
5
+ const { compareSessions } = require('./lib/compare');
6
+ const { auditProject } = require('./lib/project');
7
+ const {
8
+ formatSessionMarkdown,
9
+ formatCompareMarkdown,
10
+ formatProjectMarkdown
11
+ } = require('./lib/formatter');
12
+
13
+ function parseArgs(argv) {
14
+ const flags = {};
15
+ const positionals = [];
16
+ for (let i = 0; i < argv.length; i++) {
17
+ const arg = argv[i];
18
+ if (arg.startsWith('--')) {
19
+ const key = arg.slice(2);
20
+ if (i + 1 < argv.length && !argv[i + 1].startsWith('--')) {
21
+ flags[key] = argv[i + 1];
22
+ i++;
23
+ } else {
24
+ flags[key] = true;
25
+ }
26
+ } else {
27
+ positionals.push(arg);
28
+ }
29
+ }
30
+ return { flags, positionals };
31
+ }
32
+
33
+ function printHelp() {
34
+ console.log(`
35
+ Antigravity 本地账单体检与前后对比工具 (agy-audit CLI · 0 Token 运行)
36
+
37
+ 使用说明:
38
+ node cli.js session <id-or-prefix-or-path> [--json]
39
+ 解剖单个 Antigravity 会话日志(自动搜索 ~/.gemini/antigravity/brain 与 ~/.gemini/antigravity-cli/brain,或直接传入 .jsonl 文件 / brain 目录路径),统计总耗时与最慢步骤 Top 3、read_file 盲读与按行读取统计、实际挂载外部 MCP 服务/前缀列表、每轮上下文净增波动与 Prefix Cache 命中率/断层告警、上下文水位与自动压缩诊断及 Top 5 烧钱步骤。
40
+
41
+ node cli.js compare <id_or_path_before> <id_or_path_after> [--json]
42
+ 对比两个会话的量化指标、缓存命中率与节省比例 (%),输出 Markdown 对比表。
43
+
44
+ node cli.js project <dirPath> [--json]
45
+ 检查目标项目的常驻规则与 Skill 肥胖度,并扫描全局 ~/.gemini/config/mcp_config.json 与项目 .agents/agents/ 估算每步 MCP 与工具底座空载税及 inheritMcp: false 净身收益。
46
+
47
+ 示例:
48
+ node tools/agy-audit/cli.js session 3e4efc2c
49
+ node tools/agy-audit/cli.js compare 3e4efc2c e966d1b6
50
+ node tools/agy-audit/cli.js project ./my-project
51
+ `);
52
+ }
53
+
54
+ function main() {
55
+ const argv = process.argv.slice(2);
56
+ if (argv.length === 0 || argv.includes('--help') || argv.includes('-h')) {
57
+ printHelp();
58
+ return;
59
+ }
60
+
61
+ const { flags, positionals } = parseArgs(argv);
62
+ const cmd = (positionals[0] || '').toLowerCase();
63
+
64
+ try {
65
+ if (cmd === 'session') {
66
+ const id = positionals[1];
67
+ if (!id) {
68
+ console.error('❌ 请提供会话 ID 或短前缀,例如: node cli.js session 3e4efc2c');
69
+ process.exit(1);
70
+ }
71
+ const report = analyzeSession(id);
72
+ if (flags.json) {
73
+ console.log(JSON.stringify(report, null, 2));
74
+ } else {
75
+ console.log(formatSessionMarkdown(report));
76
+ }
77
+ } else if (cmd === 'compare') {
78
+ const idBefore = positionals[1];
79
+ const idAfter = positionals[2];
80
+ if (!idBefore || !idAfter) {
81
+ console.error('❌ 请提供两个会话 ID 进行对比,例如: node cli.js compare <before> <after>');
82
+ process.exit(1);
83
+ }
84
+ const cmp = compareSessions(idBefore, idAfter);
85
+ if (flags.json) {
86
+ console.log(JSON.stringify(cmp, null, 2));
87
+ } else {
88
+ console.log(formatCompareMarkdown(cmp));
89
+ }
90
+ } else if (cmd === 'project') {
91
+ const dirPath = positionals[1];
92
+ if (!dirPath) {
93
+ console.error('❌ 请提供单个目标项目路径,例如: node cli.js project ./my-project');
94
+ process.exit(1);
95
+ }
96
+ const options = flags.home ? { homeDir: flags.home } : {};
97
+ const report = auditProject(dirPath, options);
98
+ if (flags.json) {
99
+ console.log(JSON.stringify(report, null, 2));
100
+ } else {
101
+ console.log(formatProjectMarkdown(report));
102
+ }
103
+ } else {
104
+ console.error(`❌ 未知子命令 "${cmd}"。支持: session, compare, project`);
105
+ printHelp();
106
+ process.exit(1);
107
+ }
108
+ } catch (err) {
109
+ console.error(`❌ 执行失败: ${err.message}`);
110
+ process.exit(1);
111
+ }
112
+ }
113
+
114
+ main();