tokless 0.5.0 → 0.5.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.
- package/README.en.md +19 -18
- package/README.md +19 -18
- package/bin/tokless.js +8 -3
- package/install.js +10 -3
- package/package.json +2 -2
- package/plugin.json +2 -2
- package/rules/AGENTS.md +1 -1
- package/scripts/read-guard-hook.js +10 -27
- package/skills/code-skeleton/SKILL.md +2 -2
- package/targets/antigravity.js +8 -0
- package/targets/claude-code.js +21 -6
- package/tools/repo-skeleton/lib/langs/javascript/index.js +167 -4
package/README.en.md
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
**Zero-dependency token optimizer for AI coding agents (Google Antigravity, Claude Code, Codex CLI, and extensible via `targets/`).**
|
|
14
14
|
|
|
15
|
-
`tokless` cuts runaway prompt-cache snowballing without sacrificing code quality by combining sub-100ms `[Lstart-Lend]` AST-like code skeleton navigation,
|
|
15
|
+
`tokless` cuts runaway prompt-cache snowballing without sacrificing code quality by combining sub-100ms `[Lstart-Lend]` AST-like code skeleton navigation, an opt-in `--with-guard` `PreToolUse` blind-read navigator, MCP-stripped subagents, and 0-token local session billing/compaction audits.
|
|
16
16
|
|
|
17
17
|
In a controlled A/B bug-hunt and fix benchmark on a production Tauri + Rust + React desktop codebase, `tokless` reduced **total token throughput by 53.2% (-11.38M tokens)**, cut **cumulative Cache Read tokens by 53.8% (-11.23M tokens)**, and finished in **34.4% fewer model turns** while finding and fixing the exact same **7 deep concurrency/logic bugs** (*Note: this A/B benchmark was recorded on early `v0.1.0` with only the baseline skeletonizer and unsliced blind-read block; we have not yet re-run a full long-horizon comparison on newer releases, which add MCP-stripped lean subagents and refined line-range pass-through for even stronger theoretical savings*).
|
|
18
18
|
|
|
@@ -22,17 +22,18 @@ In a controlled A/B bug-hunt and fix benchmark on a production Tauri + Rust + Re
|
|
|
22
22
|
|
|
23
23
|
### Option 1: One-Line CLI Setup (Recommended)
|
|
24
24
|
|
|
25
|
-
Auto-detects and installs into Google Antigravity, Claude Code (`~/.claude`), and Codex CLI (`~/.codex`). When invoked via `npx`, `tokless` automatically switches to file-copy mode so
|
|
25
|
+
Auto-detects and installs into Google Antigravity, Claude Code (`~/.claude`), and Codex CLI (`~/.codex`). By default, `tokless` installs skills, rules, and lean subagents **without mounting the `PreToolUse` child-process hook** (and re-running default install automatically removes any previously mounted stale hook). When invoked via `npx`, `tokless` automatically switches to file-copy mode so assets never break after npm cache cleanup:
|
|
26
26
|
|
|
27
27
|
```bash
|
|
28
28
|
npx tokless install
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
Opt in to the `PreToolUse` guard (`>800`-line unsliced blind-read navigator), target specific hosts, or cleanly uninstall anytime:
|
|
32
32
|
|
|
33
33
|
```bash
|
|
34
|
+
npx tokless install --with-guard # Opt-in PreToolUse hook (returns skeleton only on unsliced reads >800 lines)
|
|
34
35
|
npx tokless install --target agy # Google Antigravity only
|
|
35
|
-
npx tokless install --target claude #
|
|
36
|
+
npx tokless install --target claude # Claude Code ~/.claude/tokless + skills (add --with-guard to merge settings.json hook)
|
|
36
37
|
npx tokless install --target codex # Install ~/.codex/skills/code-skeleton
|
|
37
38
|
npx tokless install --target all # Install across all supported hosts
|
|
38
39
|
npx tokless uninstall --target all # Cleanly remove hooks/skills and restore original settings
|
|
@@ -43,7 +44,7 @@ npx tokless uninstall --target all # Cleanly remove hooks/skills and restore
|
|
|
43
44
|
Drop this directly into Antigravity or Claude Code and let the agent wire itself up:
|
|
44
45
|
|
|
45
46
|
```text
|
|
46
|
-
Please run `npx tokless install` to enable tokless (or clone https://github.com/daoif/tokless and run `node install.js`), then verify that the
|
|
47
|
+
Please run `npx tokless install` to enable tokless (or clone https://github.com/daoif/tokless and run `node install.js`), then verify that the skills and rules are active.
|
|
47
48
|
```
|
|
48
49
|
|
|
49
50
|
---
|
|
@@ -57,7 +58,7 @@ A common question from heavy AI coding users: *"My IDE already has `@Codebase` s
|
|
|
57
58
|
- **The Cache Read Snowball:** In multi-step agent loops, `Cumulative Cache Read ≈ Average Context Size × Model Turns`. Dumping 30K tokens of irrelevant code on step 3 across a 50-turn task charges you 47 times—over **1.4M wasted Cache Read tokens** from a single lazy read.
|
|
58
59
|
- **Compaction Thresholds:** Generation-3 models commonly trigger forced context compaction around **256K tokens** (`CHECKPOINT: # Resuming from a compaction`, collapsing context into a lossy summary), while Generation-4 models have been observed running past 440K without compacting (widely estimated around **512K**). Regardless of where the ceiling sits, keeping per-turn context lean and cutting redundant turns directly determines how fast your quota drains.
|
|
59
60
|
|
|
60
|
-
`tokless` works alongside `@Codebase`: once the agent knows which directory or file to inspect, `tokless` generates `[Lstart-Lend]` function/class maps in ~80ms with zero pre-indexing, and its `PreToolUse` hook
|
|
61
|
+
`tokless` works alongside `@Codebase`: once the agent knows which directory or file to inspect, `tokless` generates `[Lstart-Lend]` function/class/test maps in ~80ms with zero pre-indexing, and its optional `--with-guard` `PreToolUse` hook intercepts unsliced blind reads over 800 lines (returning the file's skeleton inline while allowing any explicit line-range read).
|
|
61
62
|
|
|
62
63
|
---
|
|
63
64
|
|
|
@@ -81,21 +82,21 @@ Two isolated Git worktrees from the same commit (`3908815`), running the exact s
|
|
|
81
82
|
|
|
82
83
|
---
|
|
83
84
|
|
|
84
|
-
## Four Core Capabilities & Platform Boundaries (v0.5.
|
|
85
|
+
## Four Core Capabilities & Platform Boundaries (v0.5.1)
|
|
85
86
|
|
|
86
87
|
`tokless` draws a transparent line between **universal capabilities that work with every AI coding tool** and **host-specific hooks or log parsers**:
|
|
87
88
|
|
|
88
|
-
| Host Agent | 1. `tokless skeleton` + `AGENTS.md` Rules | 2. `PreToolUse` Blind-Read Guard | 3. Lean Subagents (`inheritMcp: false`) | 4. `tokless audit` Inspector |
|
|
89
|
+
| Host Agent | 1. `tokless skeleton` + `AGENTS.md` Rules | 2. `PreToolUse` Blind-Read Guard (`--with-guard` Opt-In) | 3. Lean Subagents (`inheritMcp: false`) | 4. `tokless audit` Inspector |
|
|
89
90
|
| :--- | :---: | :---: | :---: | :---: |
|
|
90
|
-
| **Platform Scope** | **100% Universal across all AI coding tools** | **
|
|
91
|
-
| **Google Antigravity** | Native Skill + CLI + `rules/AGENTS.md` |
|
|
92
|
-
| **Claude Code** | `~/.claude/skills` + CLI |
|
|
91
|
+
| **Platform Scope** | **100% Universal across all AI coding tools** | **Disabled by default; opt-in via `--with-guard` (Antigravity & Claude Code)** | **Antigravity Plugin Only** | **`audit project` Universal; `session / compare` Antigravity Only** |
|
|
92
|
+
| **Google Antigravity** | Native Skill + CLI + `rules/AGENTS.md` | Optional `hooks.json` (`read_file` / `view_file`) | `tokless-coder` / `tokless-reviewer` | Full (`project` + `session` / `compare`) |
|
|
93
|
+
| **Claude Code** | `~/.claude/skills` + CLI | Optional merged `~/.claude/settings.json` (`Read`) | — | `audit project` (rules & MCP tax) |
|
|
93
94
|
| **Codex CLI** | `~/.codex/skills` + CLI + `AGENTS.md` | — | — | `audit project` (rules density) |
|
|
94
95
|
| **Other Agents** (Cursor / Windsurf / Cline / Roo Code / Trae / OpenCode, etc.) | `npm i -g tokless` + root `AGENTS.md` (Ready Today) | PRs welcome via `targets/` (see `claude-code.js`) | — | `audit project` (rules density) |
|
|
95
96
|
|
|
96
97
|
### 1. `tokless skeleton` (alias `repo-skeleton`) + `rules/AGENTS.md` — [100% Universal]
|
|
97
98
|
|
|
98
|
-
Zero pre-indexing, pure Node.js standard library. Extracts class/function signatures and exact **`[Lstart-Lend]` line ranges** across `.js/.jsx/.ts/.tsx/.vue/.svelte/.py/.go/.rs/.java/.kt/.c/.cpp/.cs/.md` in milliseconds (lazy-loading only the scanned language parser), cutting exploration tokens by **85%~97%** when paired with [`rules/AGENTS.md`](./rules/AGENTS.md):
|
|
99
|
+
Zero pre-indexing, pure Node.js standard library. Extracts class/function signatures, JS/TS test suites (`describe`/`test`/`it`), and exact **`[Lstart-Lend]` line ranges** across `.js/.jsx/.ts/.tsx/.vue/.svelte/.py/.go/.rs/.java/.kt/.c/.cpp/.cs/.md` in milliseconds (lazy-loading only the scanned language parser), cutting exploration tokens by **85%~97%** when paired with [`rules/AGENTS.md`](./rules/AGENTS.md):
|
|
99
100
|
|
|
100
101
|
```bash
|
|
101
102
|
npx tokless skeleton . --map
|
|
@@ -103,9 +104,9 @@ npx tokless skeleton ./src --depth 2
|
|
|
103
104
|
npx tokless skeleton ./src/App.tsx --json
|
|
104
105
|
```
|
|
105
106
|
|
|
106
|
-
### 2. `PreToolUse` Blind-Read
|
|
107
|
+
### 2. Optional `PreToolUse` Blind-Read Navigator (`scripts/read-guard-hook.js`) — [Disabled by Default · Opt-In via `--with-guard`]
|
|
107
108
|
|
|
108
|
-
|
|
109
|
+
`npx tokless install` omits `PreToolUse` hooks by default (zero child-process latency on file reads, and re-running default install cleans up any previously mounted stale hook). Pass `--with-guard` when you want strict guardrails: the default threshold aligns with `800` lines (configurable via `TOKLESS_READ_MAX_LINES`), **intercepting only completely unsliced blind reads over 800 lines** to return the file's `[Lstart-Lend]` skeleton inline, while **100% allowing any read that supplies explicit line numbers (including `L1..EOF`)**.
|
|
109
110
|
|
|
110
111
|
### 3. MCP-Stripped Lean Subagents (`agents/`) — [Antigravity Plugin Only]
|
|
111
112
|
|
|
@@ -133,11 +134,11 @@ npx tokless audit compare <before-id> <after-id>
|
|
|
133
134
|
### 1. Built-in One-Command Targets (`npx tokless install`)
|
|
134
135
|
|
|
135
136
|
- **Google Antigravity** (`--target agy`):
|
|
136
|
-
- **Installed to**: `~/.gemini/config/plugins/agy-tokless/` (`plugin.json`, `
|
|
137
|
-
- **How it triggers**: Active automatically in new sessions—`
|
|
137
|
+
- **Installed to**: `~/.gemini/config/plugins/agy-tokless/` (`plugin.json`, `rules/AGENTS.md`, `skills/`, `agents/`; plus `hooks.json` when `--with-guard` is passed).
|
|
138
|
+
- **How it triggers**: Active automatically in new sessions—`code-skeleton` guides ranged reads (with optional `--with-guard` returning skeletons on unsliced reads `>800` lines), `tokless-coder` / `tokless-reviewer` run without MCP tax, and `token-audit` / `npx tokless audit session <id>` inspects session bills.
|
|
138
139
|
- **Claude Code** (`--target claude`):
|
|
139
|
-
- **Installed to**: Core runtime in `~/.claude/tokless/`, skill in `~/.claude/skills/code-skeleton/SKILL.md`, and non-destructively merged `PreToolUse` (`Read`) hook in `~/.claude/settings.json` (
|
|
140
|
-
- **How it triggers**:
|
|
140
|
+
- **Installed to**: Core runtime in `~/.claude/tokless/`, skill in `~/.claude/skills/code-skeleton/SKILL.md`, and—when `--with-guard` is passed—a non-destructively merged `PreToolUse` (`Read`) hook in `~/.claude/settings.json` (omitted by default, and stale hooks are automatically removed on default re-install while preserving custom hooks).
|
|
141
|
+
- **How it triggers**: Claude discovers `code-skeleton` automatically; if `--with-guard` is enabled, unsliced `Read` calls over 800 lines receive an inline `[Lstart-Lend]` map so Claude re-reads the exact target lines.
|
|
141
142
|
- **Codex CLI** (`--target codex`):
|
|
142
143
|
- **Installed to**: `~/.codex/skills/code-skeleton/` (`SKILL.md` + bundled `tools/repo-skeleton`).
|
|
143
144
|
- **How it triggers**: Codex discovers the `code-skeleton` skill automatically; copy [`rules/AGENTS.md`](./rules/AGENTS.md) into your repo root `AGENTS.md` for consistent proactive triggering.
|
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
> **核心初心:在不损失一行代码排查与修复质量的前提下,把 AI 编程工具里无意义的盲读膨胀与滚雪球 Token 消耗砍到最低。**
|
|
14
14
|
|
|
15
|
-
**面向主流 AI 编程 Agent(原生支持 Google Antigravity、Claude Code、Codex CLI,开放 `targets/` 插件架构扩展更多工具)的纯 Node.js 零依赖省流套件——毫秒级 `[L起-L止]`
|
|
15
|
+
**面向主流 AI 编程 Agent(原生支持 Google Antigravity、Claude Code、Codex CLI,开放 `targets/` 插件架构扩展更多工具)的纯 Node.js 零依赖省流套件——毫秒级 `[L起-L止]` 语法骨架导航、可选 `--with-guard` 超大文件盲读骨架护栏、剥离 MCP 空载税的净身角色与 0 Token 本地账单/缓存断层体检。**
|
|
16
16
|
|
|
17
17
|
在真实跨语言生产工程(Tauri + Rust + React)的严格 A/B 盲测中,启用 `tokless` 后单次任务减少 **34.4% 模型往返轮次**、砍掉 **53.2% 总 Token 吞吐(净省 1,138 万 Token)**、滚雪球 Cache Read 净省 **53.8%(少烧 1,123 万 Token)**,7 个真实深层 Bug 检出与修复质量零折损(*注:该组盲测数据完成于早期 `v0.1.0` 版本,当时仅含基础语法骨架与无行号盲读拦截,尚未针对后续新增净身角色与优化行号放行机制的新版本重跑完整长任务对照,当前版本理论综合省流表现更强*)。
|
|
18
18
|
|
|
@@ -22,17 +22,18 @@
|
|
|
22
22
|
|
|
23
23
|
### 方式一:终端一键挂载(推荐)
|
|
24
24
|
|
|
25
|
-
自动检测并挂载本机已安装的 Antigravity、Claude Code(`~/.claude`)与 Codex CLI(`~/.codex
|
|
25
|
+
自动检测并挂载本机已安装的 Antigravity、Claude Code(`~/.claude`)与 Codex CLI(`~/.codex`)。遵循「递地图赋能而非捆绑限制 AI 发挥」原则,默认纯增益安装技能、省流规则与净身角色(**默认不挂载 `PreToolUse` 子进程钩子**,且重跑默认安装会自动清理此前残留的旧钩子;从 `npx` 临时目录运行时自动切换为实体文件拷贝,杜绝缓存清理后软链悬空):
|
|
26
26
|
|
|
27
27
|
```bash
|
|
28
28
|
npx tokless install
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
按需启用 `PreToolUse` 超大文件(`>800` 行无行号盲读)骨架护栏、指定宿主或一键干净卸载:
|
|
32
32
|
|
|
33
33
|
```bash
|
|
34
|
+
npx tokless install --with-guard # 可选启用 PreToolUse 护栏(仅对 >800 行无行号盲读返回骨架,传行号一律放行)
|
|
34
35
|
npx tokless install --target agy # 仅挂载 Google Antigravity
|
|
35
|
-
npx tokless install --target claude #
|
|
36
|
+
npx tokless install --target claude # 仅安装 Claude Code ~/.claude/tokless 与 code-skeleton 技能(加 --with-guard 则合并 settings.json 钩子)
|
|
36
37
|
npx tokless install --target codex # 仅安装 Codex CLI ~/.codex/skills/code-skeleton
|
|
37
38
|
npx tokless install --target all # 挂载全部已支持宿主
|
|
38
39
|
npx tokless uninstall --target all # 一键卸载并还原原有宿主配置
|
|
@@ -43,7 +44,7 @@ npx tokless uninstall --target all # 一键卸载并还原原有宿主配置
|
|
|
43
44
|
不想自己切终端,直接把下面这句话发给 Antigravity 或 Claude Code 对话框即可:
|
|
44
45
|
|
|
45
46
|
```text
|
|
46
|
-
请运行 npx tokless install 帮我安装启用 tokless 省流套件(若网络受限可克隆 https://github.com/daoif/tokless 后运行 node install.js
|
|
47
|
+
请运行 npx tokless install 帮我安装启用 tokless 省流套件(若网络受限可克隆 https://github.com/daoif/tokless 后运行 node install.js),装好后帮我确认技能与省流规则已生效。
|
|
47
48
|
```
|
|
48
49
|
|
|
49
50
|
### 方式三:源码克隆本地安装(适合二次开发或离线环境)
|
|
@@ -68,7 +69,7 @@ node install.js
|
|
|
68
69
|
| :--- | :--- | :--- | :--- |
|
|
69
70
|
| **语义搜索引擎** | IDE 自带 `@Codebase` | 向量/关键词检索,帮 AI 找到“大概在哪个文件” | **只管找路,不管怎么读**。AI 拿到文件名后,下一步依然习惯性调用无行号的 `read_file` / `Read`,顺手把整份一两千行源码全量塞进上下文。 |
|
|
70
71
|
| **全仓打包器** | `repomix` / `gitingest` | 把整个仓库压成单文件喂给网页聊天框 | 适合无本地工具权限的网页问答;若塞进本地 Agent 会话,相当于第 1 步直接背上几万 Token 包袱。 |
|
|
71
|
-
|
|
|
72
|
+
| **省流骨架与可选护栏** | **`tokless`** | **80ms 零索引出 `[L起-L止]` 骨架 + 可选 `--with-guard` 盲读导航** | 先用零开销语法骨架定位 `[L起-L止]` 行号再按需切片精读;若启用 `--with-guard`,当 AI 完全不传行号盲读 `>800` 行超大文件时会自动返回全文骨架引导按行号读取(传了行号一律放行)。 |
|
|
72
73
|
|
|
73
74
|
`tokless` 与 `@Codebase` 完全不冲突,而是天然互补:`@Codebase` 负责回答“去哪几个文件找”,`tokless` 负责在读取那一刻踩住刹车,只让真正要改的那几十行代码进入上下文。
|
|
74
75
|
|
|
@@ -105,21 +106,21 @@ $$\text{累计 Cache Read} \approx \text{平均上下文水位} \times \text{模
|
|
|
105
106
|
|
|
106
107
|
---
|
|
107
108
|
|
|
108
|
-
## 四大核心能力与全平台支持边界(v0.5.
|
|
109
|
+
## 四大核心能力与全平台支持边界(v0.5.1)
|
|
109
110
|
|
|
110
111
|
`tokless` 把「跨所有 AI 编程工具通用的省流内核」与「特定客户端的深度钩子/日志解析」拆得清清楚楚,绝不混为一谈:
|
|
111
112
|
|
|
112
|
-
| 宿主工具 | 1. `tokless skeleton` 语法骨架 + `AGENTS.md` 规范 | 2. `PreToolUse`
|
|
113
|
+
| 宿主工具 | 1. `tokless skeleton` 语法骨架 + `AGENTS.md` 规范 | 2. `PreToolUse` 盲读骨架护栏(`--with-guard` 可选) | 3. 净身子角色 (`inheritMcp: false`) | 4. `tokless audit` 体检仪 |
|
|
113
114
|
| :--- | :---: | :---: | :---: | :---: |
|
|
114
|
-
| **能力通用边界** | **100% 所有 AI 编程工具通用** |
|
|
115
|
-
| **Google Antigravity** | 原生 Skill + CLI + `rules/AGENTS.md` |
|
|
116
|
-
| **Claude Code** | `~/.claude/skills` + CLI |
|
|
115
|
+
| **能力通用边界** | **100% 所有 AI 编程工具通用** | **默认关闭,按需 `--with-guard` 启用(已打通 Antigravity / Claude Code)** | **Antigravity 插件专用** | **`audit project` 跨端通用;`session / compare` 属 Antigravity 专用** |
|
|
116
|
+
| **Google Antigravity** | 原生 Skill + CLI + `rules/AGENTS.md` | 可选 `hooks.json` (`read_file` / `view_file`) | `tokless-coder` / `tokless-reviewer` | 全功能支持(`project` + `session` / `compare`) |
|
|
117
|
+
| **Claude Code** | `~/.claude/skills` + CLI | 可选无损合并 `~/.claude/settings.json` (`Read`) | — | 支持 `audit project` 规则与 MCP 税体检 |
|
|
117
118
|
| **Codex CLI** | `~/.codex/skills` + CLI + `AGENTS.md` | — | — | 支持 `audit project` 规则体检 |
|
|
118
119
|
| **其他工具**(Cursor / Windsurf / Cline / Roo Code / Trae / OpenCode 等) | `npm i -g tokless` + 项目根 `AGENTS.md` 立即可用 | 欢迎参考 `targets/claude-code.js` 共创适配 PR | — | 支持 `audit project` 规则体检 |
|
|
119
120
|
|
|
120
121
|
### 1. `tokless skeleton`(兼容 `repo-skeleton`)+ `rules/AGENTS.md` 省流规范 —【100% 所有 AI 编程工具通用】
|
|
121
122
|
|
|
122
|
-
无需预建索引数据库,纯 Node.js 标准库毫秒级提取 `.js/.jsx/.ts/.tsx/.vue/.svelte/.py/.go/.rs/.java/.kt/.c/.cpp/.cs/.md`
|
|
123
|
+
无需预建索引数据库,纯 Node.js 标准库毫秒级提取 `.js/.jsx/.ts/.tsx/.vue/.svelte/.py/.go/.rs/.java/.kt/.c/.cpp/.cs/.md` 等文件的类名、函数签名、接口、单测套件/用例(`describe`/`test`/`it`)与精确 **`[L起-L止]` 行号范围**(按需惰性加载对应语言解析器),配合 [`rules/AGENTS.md`](./rules/AGENTS.md) 中的两步精准读取规范,探索期单文件省流 **85%~97%**:
|
|
123
124
|
|
|
124
125
|
```bash
|
|
125
126
|
# 快速生成全仓目录行数与字数鸟瞰地图(不展开函数签名)
|
|
@@ -132,9 +133,9 @@ npx tokless skeleton ./src --depth 3
|
|
|
132
133
|
npx tokless skeleton ./src/App.tsx
|
|
133
134
|
```
|
|
134
135
|
|
|
135
|
-
### 2. `PreToolUse`
|
|
136
|
+
### 2. 可选 `PreToolUse` 超大文件盲读护栏(`scripts/read-guard-hook.js`)—【默认关闭,`--with-guard` 按需启用】
|
|
136
137
|
|
|
137
|
-
|
|
138
|
+
遵循「赋能导航而非捆绑手脚」原则,`npx tokless install` 默认**不挂载** `PreToolUse` 钩子(彻底消除每次读取工具调用拉起子进程的开销,且重跑默认安装会自动清理已挂旧钩子)。需要强管控时可通过 `npx tokless install --with-guard` 开启:默认阈值对齐单次 `800` 行物理上限(可通过环境变量 `TOKLESS_READ_MAX_LINES` 调整),**仅对 `>800` 行且完全未传行号的盲读返回该文件的 `[L起-L止]` 语法骨架导航;凡传入了任何显式行号参数(含 `L1~末尾`)代表 AI 心中有数,一律 100% 放行**,杜绝报错重试与强拆多轮反噬 Cache Read。
|
|
138
139
|
|
|
139
140
|
### 3. 剥离外部 MCP 空载底座税的净身子角色(`agents/`)—【Antigravity 插件专用】
|
|
140
141
|
|
|
@@ -162,11 +163,11 @@ npx tokless audit compare <优化前ID> <优化后ID>
|
|
|
162
163
|
### 1. 已内置一键挂载的三大工具(运行 `npx tokless install` 后分别装到了哪里、怎么触发)
|
|
163
164
|
|
|
164
165
|
- **Google Antigravity**(`--target agy`):
|
|
165
|
-
- **安装位置**:`~/.gemini/config/plugins/agy-tokless/`(挂载 `plugin.json`、`
|
|
166
|
-
-
|
|
166
|
+
- **安装位置**:`~/.gemini/config/plugins/agy-tokless/`(挂载 `plugin.json`、`rules/AGENTS.md`、`skills/`、`agents/`;加 `--with-guard` 时额外生成 `hooks.json`)。
|
|
167
|
+
- **日常触发**:装好后新开对话即自动生效——探索大文件时触发 `code-skeleton` 技能按行号精读(若开启 `--with-guard` 则对 `>800` 行无行号盲读自动返回骨架导航);派发纯编码或审查子任务时可指定 `tokless-coder` / `tokless-reviewer` 卸除 MCP 税;需要查账单时直接让 AI 调用 `token-audit` 技能或手动运行 `npx tokless audit session <ID>`。
|
|
167
168
|
- **Claude Code**(`--target claude`):
|
|
168
|
-
- **安装位置**:核心脚本存入 `~/.claude/tokless/`,技能写入 `~/.claude/skills/code-skeleton/SKILL.md
|
|
169
|
-
- **日常触发**:在任意项目启动 `claude`
|
|
169
|
+
- **安装位置**:核心脚本存入 `~/.claude/tokless/`,技能写入 `~/.claude/skills/code-skeleton/SKILL.md`;加 `--with-guard` 时将 `Read` 盲读导航钩子无损合并进 `~/.claude/settings.json`(默认不挂载且重跑默认安装会自动剥离旧钩子,完整保留原有权限与自定义钩子)。
|
|
170
|
+
- **日常触发**:在任意项目启动 `claude` 后自动发现 `code-skeleton` 技能;若启用了 `--with-guard`,当 Claude 试图不带 `offset/limit` 盲读超过 800 行的超大代码文件时,`PreToolUse` 钩子会把 `[L起-L止]` 骨架直接喂回给它引导按行号精读。
|
|
170
171
|
- **Codex CLI**(`--target codex`):
|
|
171
172
|
- **安装位置**:`~/.codex/skills/code-skeleton/`(含 `SKILL.md` 与零依赖 `tools/repo-skeleton` 脚本)。
|
|
172
173
|
- **日常触发**:Codex 会自动发现并按需调用 `code-skeleton` 技能先看行号骨架再精读源码;建议同时将 [`rules/AGENTS.md`](./rules/AGENTS.md) 中的省流规范合入项目根目录的 `AGENTS.md` 以强化主动触发。
|
package/bin/tokless.js
CHANGED
|
@@ -9,19 +9,23 @@ const { parseCliArgs, runInstall, runUninstall } = require('../install');
|
|
|
9
9
|
const REPO_ROOT = path.resolve(__dirname, '..');
|
|
10
10
|
|
|
11
11
|
function printHelp() {
|
|
12
|
-
console.log(`tokless v${pkg.version} - AST codebase skeletonizer, blind-read guard & session token auditor
|
|
12
|
+
console.log(`tokless v${pkg.version} - AST codebase skeletonizer, optional blind-read guard & session token auditor
|
|
13
13
|
|
|
14
14
|
Usage:
|
|
15
|
-
tokless install [--target agy|claude|codex|all] [--copy]
|
|
15
|
+
tokless install [--target agy|claude|codex|all] [--with-guard] [--copy]
|
|
16
16
|
tokless uninstall [--target agy|claude|codex|all]
|
|
17
17
|
tokless skeleton <file-or-dir> [--depth <N>] [--map] [--json]
|
|
18
18
|
tokless audit <session|compare|project> [args...] [--json]
|
|
19
19
|
tokless --version | -v
|
|
20
20
|
tokless --help | -h
|
|
21
21
|
|
|
22
|
+
Install Options:
|
|
23
|
+
--with-guard Opt-in PreToolUse hook (>800-line unsliced blind-read skeleton navigator; disabled by default)
|
|
24
|
+
--copy Copy files instead of linking (auto-enabled in _npx / node_modules)
|
|
25
|
+
|
|
22
26
|
Targets:
|
|
23
27
|
agy Antigravity (~/.gemini/config/plugins/agy-tokless)
|
|
24
|
-
claude Claude Code (~/.claude/tokless + skills
|
|
28
|
+
claude Claude Code (~/.claude/tokless + skills; optional settings.json PreToolUse Read hook via --with-guard)
|
|
25
29
|
codex Codex CLI (~/.codex/skills/code-skeleton)
|
|
26
30
|
all Install/uninstall across all supported targets
|
|
27
31
|
(omit) Auto-install Antigravity + any detected ~/.claude or ~/.codex host`);
|
|
@@ -62,6 +66,7 @@ function main() {
|
|
|
62
66
|
sourceDir: REPO_ROOT,
|
|
63
67
|
target: parsed.target,
|
|
64
68
|
copy: parsed.copy,
|
|
69
|
+
withGuard: parsed.withGuard,
|
|
65
70
|
mode: 'auto',
|
|
66
71
|
});
|
|
67
72
|
} catch (err) {
|
package/install.js
CHANGED
|
@@ -8,6 +8,7 @@ const { shouldForceCopy, resolveHomeDir } = require('./targets/shared');
|
|
|
8
8
|
function parseCliArgs(argv) {
|
|
9
9
|
let target = null;
|
|
10
10
|
let copy = false;
|
|
11
|
+
let withGuard = false;
|
|
11
12
|
let uninstall = false;
|
|
12
13
|
let help = false;
|
|
13
14
|
|
|
@@ -17,6 +18,8 @@ function parseCliArgs(argv) {
|
|
|
17
18
|
help = true;
|
|
18
19
|
} else if (arg === '--copy') {
|
|
19
20
|
copy = true;
|
|
21
|
+
} else if (arg === '--with-guard') {
|
|
22
|
+
withGuard = true;
|
|
20
23
|
} else if (arg === '--uninstall') {
|
|
21
24
|
uninstall = true;
|
|
22
25
|
} else if (arg === '--target' || arg === '-t') {
|
|
@@ -29,13 +32,14 @@ function parseCliArgs(argv) {
|
|
|
29
32
|
}
|
|
30
33
|
}
|
|
31
34
|
|
|
32
|
-
return { target, copy, uninstall, help };
|
|
35
|
+
return { target, copy, withGuard, uninstall, help };
|
|
33
36
|
}
|
|
34
37
|
|
|
35
38
|
function runInstall(options = {}) {
|
|
36
39
|
const sourceDir = options.sourceDir || __dirname;
|
|
37
40
|
const homeDir = resolveHomeDir(options);
|
|
38
41
|
const forceCopy = shouldForceCopy(sourceDir, Boolean(options.copy));
|
|
42
|
+
const withGuard = Boolean(options.withGuard);
|
|
39
43
|
const mode = options.mode || 'antigravity-only';
|
|
40
44
|
const adapters = resolveTargets(options.target, { homeDir, mode });
|
|
41
45
|
|
|
@@ -46,6 +50,7 @@ function runInstall(options = {}) {
|
|
|
46
50
|
sourceDir,
|
|
47
51
|
homeDir,
|
|
48
52
|
copy: forceCopy,
|
|
53
|
+
withGuard,
|
|
49
54
|
silent: options.silent,
|
|
50
55
|
})
|
|
51
56
|
);
|
|
@@ -71,9 +76,10 @@ function runUninstall(options = {}) {
|
|
|
71
76
|
}
|
|
72
77
|
|
|
73
78
|
function printHelp() {
|
|
74
|
-
console.log(`Usage: node install.js [--target agy|claude|codex|all] [--copy] [--uninstall]
|
|
75
|
-
(default) Install Antigravity plugin into ~/.gemini/config/plugins/agy-tokless
|
|
79
|
+
console.log(`Usage: node install.js [--target agy|claude|codex|all] [--with-guard] [--copy] [--uninstall]
|
|
80
|
+
(default) Install Antigravity plugin into ~/.gemini/config/plugins/agy-tokless (PreToolUse hook disabled by default)
|
|
76
81
|
--target <hosts> Target host(s): agy, claude, codex, all (comma-separated supported)
|
|
82
|
+
--with-guard Opt-in PreToolUse hook to return syntax skeleton on unsliced blind reads >800 lines (disabled by default)
|
|
77
83
|
--copy Copy all files instead of creating links (auto-enabled in _npx / node_modules)
|
|
78
84
|
--uninstall Remove installed tokless plugin/hooks for the selected target(s)`);
|
|
79
85
|
}
|
|
@@ -95,6 +101,7 @@ function main() {
|
|
|
95
101
|
runInstall({
|
|
96
102
|
target: parsed.target,
|
|
97
103
|
copy: parsed.copy,
|
|
104
|
+
withGuard: parsed.withGuard,
|
|
98
105
|
mode: 'antigravity-only',
|
|
99
106
|
});
|
|
100
107
|
} catch (err) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tokless",
|
|
3
|
-
"version": "0.5.
|
|
4
|
-
"description": "Token optimizer, AST codebase skeletonizer, PreToolUse blind-read guard, lightweight role templates, and cache/compaction auditor for AI coding agents.",
|
|
3
|
+
"version": "0.5.1",
|
|
4
|
+
"description": "Token optimizer, AST codebase skeletonizer, optional PreToolUse blind-read guard, lightweight role templates, and cache/compaction auditor for AI coding agents.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "./index.js",
|
|
7
7
|
"bin": {
|
package/plugin.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agy-tokless",
|
|
3
3
|
"displayName": "AGY Tokless — Antigravity 省流护栏与账单透视套件",
|
|
4
|
-
"version": "0.5.
|
|
5
|
-
"description": "专为 AI
|
|
4
|
+
"version": "0.5.1",
|
|
5
|
+
"description": "专为 AI 编程打造的省流导航与账单审计插件:提供代码语法骨架提取(repo-skeleton)、可选超大文件盲读骨架导航钩子(PreToolUse Hook,--with-guard 启用)、轻量净身角色模板(agents)以及 Prefix Cache 断层与 MCP 底座税体检工具(agy-audit)。",
|
|
6
6
|
"disabled": false,
|
|
7
7
|
"suggestedPrompts": [
|
|
8
8
|
"使用 token-audit 检查最近会话的 Token 消耗黑洞、Prefix Cache 断层与当前项目的 MCP 底座空载税",
|
package/rules/AGENTS.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# AI 编程省流读取、净身角色与缓存保护规范 (tokless)
|
|
2
2
|
|
|
3
3
|
## 1. 先看代码骨架,再按行号精准读取 (`tokless skeleton` / `repo-skeleton`)
|
|
4
|
-
-
|
|
4
|
+
- **拒绝无行号盲读大代码文件**:探索陌生模块或大源码文件时先提取语法骨架定位行号,避免不带起止行号(`StartLine`/`EndLine` 或 `offset`/`limit`)直接全量盲读大文件(可选启用 `--with-guard` 读前护栏后,仅对 `>800` 行无行号盲读自动返回全文骨架辅助导航,凡传入显式行号参数一律放行)。
|
|
5
5
|
- **两步精准定位法**:
|
|
6
6
|
1. 探索陌生目录或大文件时,先用 `tokless skeleton`(兼容 `repo-skeleton`)提取语法签名与 `[L起-L止]` 行号(0 模型 Token,比盲读全文省 85%~97% 探索开销):
|
|
7
7
|
- 全局 CLI(推荐): `tokless skeleton <目录或文件路径> --depth 2`
|
|
@@ -39,9 +39,9 @@ function getMaxLinesThreshold(overrideVal) {
|
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
const raw = process.env.TOKLESS_READ_MAX_LINES || process.env.AGY_READ_MAX_LINES;
|
|
42
|
-
if (!raw) return
|
|
42
|
+
if (!raw) return 800;
|
|
43
43
|
const parsed = parseInt(raw, 10);
|
|
44
|
-
return Number.isInteger(parsed) && parsed > 0 ? parsed :
|
|
44
|
+
return Number.isInteger(parsed) && parsed > 0 ? parsed : 800;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
function parseLineArg(val) {
|
|
@@ -140,13 +140,12 @@ function evaluateReadGuard(payloadOrJson, options = {}) {
|
|
|
140
140
|
|
|
141
141
|
const hasStart = startNum !== null;
|
|
142
142
|
const hasEnd = endNum !== null;
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
// 陷阱: 严禁设 300 行死跨度硬拦截(会导致报错重试+拆轮倒贴数十万 Cache Read);正常局部行号切片直接放行,仅核查盲读与 1~EOF 假切片
|
|
146
|
-
if (hasStart && startNum > 2) {
|
|
143
|
+
// 陷阱: 凡传入了显式行号参数一律直接放行(禁设单次跨度上限或拦截 L1~EOF,避免报错重试+拆轮反噬数十万 Cache Read)
|
|
144
|
+
if (hasStart || hasEnd) {
|
|
147
145
|
return { decision: 'allow', host };
|
|
148
146
|
}
|
|
149
147
|
|
|
148
|
+
const maxLines = getMaxLinesThreshold(options.maxLines);
|
|
150
149
|
const generateFileSkeleton = loadSkeletonGenerator();
|
|
151
150
|
const skeleton = generateFileSkeleton(resolvedPath, { includeHeader: false });
|
|
152
151
|
|
|
@@ -158,28 +157,12 @@ function evaluateReadGuard(payloadOrJson, options = {}) {
|
|
|
158
157
|
? '重新调用 Read 并传入目标区间的 offset 与 limit'
|
|
159
158
|
: '重新调用 read_file 并传入目标区间的 StartLine 和 EndLine';
|
|
160
159
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
`${skeleton.formatted}`;
|
|
166
|
-
|
|
167
|
-
return { decision: 'deny', reason, host };
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const effectiveStart = hasStart ? Math.max(1, startNum) : 1;
|
|
171
|
-
const requestedEnd = hasEnd ? endNum : skeleton.totalLines;
|
|
172
|
-
|
|
173
|
-
if (effectiveStart <= 2 && requestedEnd >= skeleton.totalLines - 2) {
|
|
174
|
-
const reason =
|
|
175
|
-
`[tokless 省流护栏] 目标文件共 ${skeleton.totalLines} 行,检测到 L${effectiveStart}-L${requestedEnd} 覆盖全文首尾的假切片全读,已拦截以保护上下文额度。\n` +
|
|
176
|
-
`请根据下方该文件的语法骨架与 [L起-L止] 行号范围,${rangeHint}精准读取真实需要的函数或逻辑块:\n\n` +
|
|
177
|
-
`${skeleton.formatted}`;
|
|
178
|
-
|
|
179
|
-
return { decision: 'deny', reason, host };
|
|
180
|
-
}
|
|
160
|
+
const reason =
|
|
161
|
+
`[tokless 省流护栏] 目标文件共 ${skeleton.totalLines} 行(超过 ${maxLines} 行无行号读取阈值),已返回语法骨架辅助定位。\n` +
|
|
162
|
+
`请根据下方该文件的语法骨架与 [L起-L止] 行号范围,${rangeHint}精准读取所需片段:\n\n` +
|
|
163
|
+
`${skeleton.formatted}`;
|
|
181
164
|
|
|
182
|
-
return { decision: '
|
|
165
|
+
return { decision: 'deny', reason, host };
|
|
183
166
|
} catch (_) {
|
|
184
167
|
return { decision: 'allow', host: 'antigravity' };
|
|
185
168
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: code-skeleton
|
|
3
|
-
description: 提取代码文件或目录的紧凑语法骨架(仅保留类名、函数签名、导出接口与精确 [L起-L止] 行号,剥离具体实现代码)。在探索代码库、排查 Bug
|
|
3
|
+
description: 提取代码文件或目录的紧凑语法骨架(仅保留类名、函数签名、导出接口与精确 [L起-L止] 行号,剥离具体实现代码)。在探索代码库、排查 Bug 或读取中大型文件前使用此技能快速定位行号,可减少 85%~97% 的探索期 Token 消耗。
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# 代码骨架极速提取指南 (code-skeleton)
|
|
@@ -31,4 +31,4 @@ node "$env:USERPROFILE\.gemini\config\plugins\agy-tokless\tools\repo-skeleton\cl
|
|
|
31
31
|
node "$env:USERPROFILE\.gemini\config\plugins\agy-tokless\tools\repo-skeleton\cli.js" <文件路径>
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
拿到目标函数的 `[L起-L止]` 行号后,直接调用 `read_file(Path, StartLine, EndLine)`(或 Claude Code `Read(file_path, offset, limit)
|
|
34
|
+
拿到目标函数的 `[L起-L止]` 行号后,直接调用 `read_file(Path, StartLine, EndLine)`(或 Claude Code `Read(file_path, offset, limit)`)按需完整读取对应代码段(无单次行数跨度限制,凡传入显式行号一律放行;若启用 `--with-guard` 护栏,仅对 `>800` 行无行号盲读返回全文骨架辅助导航)。
|
package/targets/antigravity.js
CHANGED
|
@@ -51,6 +51,7 @@ function install(options = {}) {
|
|
|
51
51
|
const sourceDir = options.sourceDir || path.resolve(__dirname, '..');
|
|
52
52
|
const homeDir = resolveHomeDir(options);
|
|
53
53
|
const forceCopy = shouldForceCopy(sourceDir, options.copy);
|
|
54
|
+
const withGuard = Boolean(options.withGuard);
|
|
54
55
|
const manifestPath = path.join(sourceDir, 'plugin.json');
|
|
55
56
|
|
|
56
57
|
if (!fs.existsSync(manifestPath)) {
|
|
@@ -67,6 +68,9 @@ function install(options = {}) {
|
|
|
67
68
|
|
|
68
69
|
const copiedFiles = [];
|
|
69
70
|
for (const fileName of ROOT_FILES) {
|
|
71
|
+
if (fileName === 'hooks.json' && !withGuard) {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
70
74
|
const srcFile = path.join(sourceDir, fileName);
|
|
71
75
|
if (fs.existsSync(srcFile)) {
|
|
72
76
|
const destFile = path.join(targetDir, fileName);
|
|
@@ -96,6 +100,9 @@ function install(options = {}) {
|
|
|
96
100
|
console.log(`[tokless:antigravity] 已安装至 ${targetDir}`);
|
|
97
101
|
console.log(` 插件源目录 : ${sourceDir}`);
|
|
98
102
|
console.log(` 清单文件 : ${copiedFiles.join(', ')}`);
|
|
103
|
+
console.log(
|
|
104
|
+
` 读前护栏 : ${withGuard ? '已启用 (--with-guard, >800 行无行号导航)' : '默认关闭 (需启用请加 --with-guard)'}`
|
|
105
|
+
);
|
|
99
106
|
console.log(
|
|
100
107
|
` 模块挂载 : ${Object.entries(dirModes)
|
|
101
108
|
.map(([k, v]) => `${k}[${v}]`)
|
|
@@ -107,6 +114,7 @@ function install(options = {}) {
|
|
|
107
114
|
target: 'antigravity',
|
|
108
115
|
targetDir,
|
|
109
116
|
copiedFiles,
|
|
117
|
+
withGuard,
|
|
110
118
|
dirModes,
|
|
111
119
|
};
|
|
112
120
|
}
|
package/targets/claude-code.js
CHANGED
|
@@ -139,6 +139,7 @@ function install(options = {}) {
|
|
|
139
139
|
const sourceDir = options.sourceDir || path.resolve(__dirname, '..');
|
|
140
140
|
const homeDir = resolveHomeDir(options);
|
|
141
141
|
const forceCopy = shouldForceCopy(sourceDir, options.copy);
|
|
142
|
+
const withGuard = Boolean(options.withGuard);
|
|
142
143
|
|
|
143
144
|
const claudeRoot = path.join(homeDir, '.claude');
|
|
144
145
|
const coreDir = path.join(claudeRoot, 'tokless');
|
|
@@ -180,16 +181,28 @@ function install(options = {}) {
|
|
|
180
181
|
);
|
|
181
182
|
}
|
|
182
183
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
184
|
+
let hookCommand = null;
|
|
185
|
+
let cleanedStaleHook = false;
|
|
186
|
+
if (withGuard) {
|
|
187
|
+
const hookScriptPath = path.join(coreDir, 'scripts', 'read-guard-hook.js');
|
|
188
|
+
const quotedNode = quoteCommandBinary(resolveNodeBinary());
|
|
189
|
+
hookCommand = `${quotedNode} ${JSON.stringify(hookScriptPath)}`;
|
|
190
|
+
mergeClaudeSettings(settingsPath, hookCommand);
|
|
191
|
+
} else if (fs.existsSync(settingsPath)) {
|
|
192
|
+
cleanedStaleHook = unmergeClaudeSettings(settingsPath);
|
|
193
|
+
}
|
|
187
194
|
|
|
188
195
|
if (!options.silent) {
|
|
189
196
|
console.log('');
|
|
190
197
|
console.log(`[tokless:claude] 已安装核心至 ${coreDir}`);
|
|
191
198
|
console.log(` 技能目录 : ${skillDestDir}`);
|
|
192
|
-
|
|
199
|
+
if (withGuard) {
|
|
200
|
+
console.log(` 钩子配置 : ${settingsPath} (已启用 PreToolUse -> Read, >800 行无行号导航)`);
|
|
201
|
+
} else if (cleanedStaleHook) {
|
|
202
|
+
console.log(` 钩子配置 : 已从 ${settingsPath} 自动移除旧 PreToolUse 钩子 (默认关闭,需启用请加 --with-guard)`);
|
|
203
|
+
} else {
|
|
204
|
+
console.log(` 钩子配置 : 默认关闭 (需启用 PreToolUse -> Read 请加 --with-guard)`);
|
|
205
|
+
}
|
|
193
206
|
}
|
|
194
207
|
|
|
195
208
|
return {
|
|
@@ -198,6 +211,8 @@ function install(options = {}) {
|
|
|
198
211
|
skillDestDir,
|
|
199
212
|
settingsPath,
|
|
200
213
|
hookCommand,
|
|
214
|
+
withGuard,
|
|
215
|
+
cleanedStaleHook,
|
|
201
216
|
dirModes,
|
|
202
217
|
};
|
|
203
218
|
}
|
|
@@ -236,7 +251,7 @@ function uninstall(options = {}) {
|
|
|
236
251
|
module.exports = {
|
|
237
252
|
id: 'claude',
|
|
238
253
|
aliases: ['claude', 'claude-code'],
|
|
239
|
-
label: 'Claude Code (~/.claude/tokless + PreToolUse Read hook)',
|
|
254
|
+
label: 'Claude Code (~/.claude/tokless + skills; optional PreToolUse Read hook)',
|
|
240
255
|
mergeClaudeSettings,
|
|
241
256
|
unmergeClaudeSettings,
|
|
242
257
|
detect,
|
|
@@ -59,6 +59,136 @@ function findIifeRange(analyzed, startIdx) {
|
|
|
59
59
|
return null;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
function parseTestCallBlock(analyzed, startIdx) {
|
|
63
|
+
const startClean = analyzed[startIdx].clean;
|
|
64
|
+
const trimmed = startClean.trim();
|
|
65
|
+
const m = trimmed.match(
|
|
66
|
+
/^(?:await\s+)?(describe|test|it)((?:\.(?:only|skip|concurrent|serial|sequential))*)\s*\(/
|
|
67
|
+
);
|
|
68
|
+
if (!m) return null;
|
|
69
|
+
|
|
70
|
+
const baseFn = m[1];
|
|
71
|
+
const callee = baseFn + (m[2] || '');
|
|
72
|
+
const openParenCol = startClean.indexOf('(', startClean.indexOf(baseFn));
|
|
73
|
+
if (openParenCol === -1) return null;
|
|
74
|
+
|
|
75
|
+
let relParen = 0;
|
|
76
|
+
let relBracket = 0;
|
|
77
|
+
let relBrace = 0;
|
|
78
|
+
let firstCommaLine = -1;
|
|
79
|
+
let firstCommaCol = -1;
|
|
80
|
+
let paramCleanSinceSep = '';
|
|
81
|
+
let bodyOpenLine = -1;
|
|
82
|
+
let bodyDepth = -1;
|
|
83
|
+
let callEndLine = -1;
|
|
84
|
+
|
|
85
|
+
for (let j = startIdx; j < analyzed.length; j++) {
|
|
86
|
+
const cleanLine = analyzed[j].clean;
|
|
87
|
+
const kStart = j === startIdx ? openParenCol : 0;
|
|
88
|
+
let curBraceDepth = analyzed[j].braceStart;
|
|
89
|
+
if (j === startIdx) {
|
|
90
|
+
for (let p = 0; p < kStart; p++) {
|
|
91
|
+
if (cleanLine[p] === '{') curBraceDepth++;
|
|
92
|
+
else if (cleanLine[p] === '}') curBraceDepth = Math.max(0, curBraceDepth - 1);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
for (let k = kStart; k < cleanLine.length; k++) {
|
|
97
|
+
const ch = cleanLine[k];
|
|
98
|
+
if (ch === '{') curBraceDepth++;
|
|
99
|
+
else if (ch === '}') curBraceDepth = Math.max(0, curBraceDepth - 1);
|
|
100
|
+
|
|
101
|
+
if (j === startIdx && k === openParenCol) {
|
|
102
|
+
relParen = 1;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (ch === '(') {
|
|
107
|
+
relParen++;
|
|
108
|
+
if (relParen === 2 && relBracket === 0 && relBrace === 0) {
|
|
109
|
+
paramCleanSinceSep += '(';
|
|
110
|
+
}
|
|
111
|
+
} else if (ch === ')') {
|
|
112
|
+
relParen--;
|
|
113
|
+
if (relParen === 0) {
|
|
114
|
+
callEndLine = j;
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
if (relParen === 1 && relBracket === 0 && relBrace === 0) {
|
|
118
|
+
paramCleanSinceSep += ')';
|
|
119
|
+
}
|
|
120
|
+
} else if (ch === '[') {
|
|
121
|
+
relBracket++;
|
|
122
|
+
} else if (ch === ']') {
|
|
123
|
+
relBracket = Math.max(0, relBracket - 1);
|
|
124
|
+
} else if (ch === '{') {
|
|
125
|
+
if (relParen === 1 && relBracket === 0 && relBrace === 0 && bodyOpenLine === -1) {
|
|
126
|
+
const prev = paramCleanSinceSep.trim();
|
|
127
|
+
if (/=>\s*$/.test(prev) || /^(?:async\s+)?function\b/.test(prev)) {
|
|
128
|
+
bodyOpenLine = j;
|
|
129
|
+
bodyDepth = curBraceDepth;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
relBrace++;
|
|
133
|
+
} else if (ch === '}') {
|
|
134
|
+
relBrace = Math.max(0, relBrace - 1);
|
|
135
|
+
} else if (ch === ',' && relParen === 1 && relBracket === 0 && relBrace === 0) {
|
|
136
|
+
if (firstCommaLine === -1) {
|
|
137
|
+
firstCommaLine = j;
|
|
138
|
+
firstCommaCol = k;
|
|
139
|
+
}
|
|
140
|
+
paramCleanSinceSep = '';
|
|
141
|
+
} else if (relParen === 1 && relBracket === 0 && relBrace === 0) {
|
|
142
|
+
paramCleanSinceSep += ch;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (callEndLine !== -1) break;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (callEndLine === -1 || bodyOpenLine === -1 || firstCommaLine === -1) {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const rawArgChunks = [];
|
|
154
|
+
for (let j = startIdx; j <= firstCommaLine; j++) {
|
|
155
|
+
const rawLine = analyzed[j].raw;
|
|
156
|
+
const from = j === startIdx ? openParenCol + 1 : 0;
|
|
157
|
+
const to = j === firstCommaLine ? firstCommaCol : rawLine.length;
|
|
158
|
+
rawArgChunks.push(rawLine.slice(from, to));
|
|
159
|
+
}
|
|
160
|
+
const firstArgText = rawArgChunks.join(' ').replace(/\s+/g, ' ').trim();
|
|
161
|
+
if (!firstArgText) return null;
|
|
162
|
+
|
|
163
|
+
let label = firstArgText;
|
|
164
|
+
if (
|
|
165
|
+
(label.startsWith("'") && label.endsWith("'") && label.length >= 2) ||
|
|
166
|
+
(label.startsWith('"') && label.endsWith('"') && label.length >= 2) ||
|
|
167
|
+
(label.startsWith('`') && label.endsWith('`') && label.length >= 2)
|
|
168
|
+
) {
|
|
169
|
+
label = label.slice(1, -1);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
let displayArg = firstArgText;
|
|
173
|
+
if (displayArg.length > 110) {
|
|
174
|
+
const q = displayArg[0];
|
|
175
|
+
if (q === "'" || q === '"' || q === '`') {
|
|
176
|
+
displayArg = displayArg.slice(0, 106) + '...' + q;
|
|
177
|
+
} else {
|
|
178
|
+
displayArg = displayArg.slice(0, 107) + '...';
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
kind: baseFn === 'describe' ? 'suite' : 'test',
|
|
184
|
+
name: label,
|
|
185
|
+
signature: `${callee}(${displayArg})`,
|
|
186
|
+
headerEndIdx: bodyOpenLine,
|
|
187
|
+
endIdx: callEndLine,
|
|
188
|
+
bodyDepth,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
62
192
|
function parse(lines, ext = '.js', options = {}) {
|
|
63
193
|
if (ext && typeof ext === 'object') {
|
|
64
194
|
options = ext;
|
|
@@ -71,6 +201,7 @@ function parse(lines, ext = '.js', options = {}) {
|
|
|
71
201
|
const analyzed = preprocessBraceLines(lines, 'js');
|
|
72
202
|
const items = [];
|
|
73
203
|
const classStack = [];
|
|
204
|
+
const suiteStack = [];
|
|
74
205
|
let activeIifeEnd = -1;
|
|
75
206
|
let activeIifeBodyDepth = -1;
|
|
76
207
|
|
|
@@ -82,12 +213,20 @@ function parse(lines, ext = '.js', options = {}) {
|
|
|
82
213
|
while (classStack.length > 0 && i > classStack[classStack.length - 1].endIdx) {
|
|
83
214
|
classStack.pop();
|
|
84
215
|
}
|
|
216
|
+
while (suiteStack.length > 0 && i > suiteStack[suiteStack.length - 1].endIdx) {
|
|
217
|
+
suiteStack.pop();
|
|
218
|
+
}
|
|
85
219
|
if (activeIifeEnd !== -1 && i > activeIifeEnd) {
|
|
86
220
|
activeIifeEnd = -1;
|
|
87
221
|
activeIifeBodyDepth = -1;
|
|
88
222
|
}
|
|
89
223
|
|
|
90
|
-
if (
|
|
224
|
+
if (
|
|
225
|
+
info.braceStart === 0 &&
|
|
226
|
+
activeIifeEnd === -1 &&
|
|
227
|
+
classStack.length === 0 &&
|
|
228
|
+
suiteStack.length === 0
|
|
229
|
+
) {
|
|
91
230
|
const iife = findIifeRange(analyzed, i);
|
|
92
231
|
if (iife) {
|
|
93
232
|
activeIifeEnd = iife.endIdx;
|
|
@@ -99,13 +238,37 @@ function parse(lines, ext = '.js', options = {}) {
|
|
|
99
238
|
|
|
100
239
|
const topScopeDepth =
|
|
101
240
|
activeIifeEnd !== -1 && i <= activeIifeEnd ? activeIifeBodyDepth : 0;
|
|
241
|
+
const currentSuite = suiteStack.length > 0 ? suiteStack[suiteStack.length - 1] : null;
|
|
242
|
+
const activeScopeDepth = currentSuite !== null ? currentSuite.bodyDepth : topScopeDepth;
|
|
102
243
|
const currentClass = classStack.length > 0 ? classStack[classStack.length - 1] : null;
|
|
103
244
|
const inClassBody =
|
|
104
245
|
currentClass !== null &&
|
|
105
246
|
i <= currentClass.endIdx &&
|
|
106
247
|
info.braceStart === currentClass.bodyDepth;
|
|
107
248
|
|
|
108
|
-
if (info.braceStart ===
|
|
249
|
+
if (info.braceStart === activeScopeDepth && !inClassBody) {
|
|
250
|
+
const testBlock = parseTestCallBlock(analyzed, i);
|
|
251
|
+
if (testBlock) {
|
|
252
|
+
items.push({
|
|
253
|
+
kind: testBlock.kind,
|
|
254
|
+
name: testBlock.name,
|
|
255
|
+
signature: testBlock.signature,
|
|
256
|
+
startLine: i + 1 + lineOffset,
|
|
257
|
+
endLine: testBlock.endIdx + 1 + lineOffset,
|
|
258
|
+
depth: suiteStack.length,
|
|
259
|
+
});
|
|
260
|
+
if (testBlock.kind === 'suite' && testBlock.headerEndIdx < testBlock.endIdx) {
|
|
261
|
+
suiteStack.push({
|
|
262
|
+
endIdx: testBlock.endIdx,
|
|
263
|
+
bodyDepth: testBlock.bodyDepth,
|
|
264
|
+
});
|
|
265
|
+
i = testBlock.headerEndIdx;
|
|
266
|
+
} else {
|
|
267
|
+
i = testBlock.endIdx;
|
|
268
|
+
}
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
|
|
109
272
|
let kind = null;
|
|
110
273
|
let name = '';
|
|
111
274
|
let m;
|
|
@@ -217,7 +380,7 @@ function parse(lines, ext = '.js', options = {}) {
|
|
|
217
380
|
signature,
|
|
218
381
|
startLine: i + 1 + lineOffset,
|
|
219
382
|
endLine: range.endIdx + 1 + lineOffset,
|
|
220
|
-
depth:
|
|
383
|
+
depth: suiteStack.length,
|
|
221
384
|
});
|
|
222
385
|
|
|
223
386
|
if (kind === 'class' && range.hasBlock) {
|
|
@@ -268,7 +431,7 @@ function parse(lines, ext = '.js', options = {}) {
|
|
|
268
431
|
signature,
|
|
269
432
|
startLine: i + 1 + lineOffset,
|
|
270
433
|
endLine: range.endIdx + 1 + lineOffset,
|
|
271
|
-
depth: classStack.length,
|
|
434
|
+
depth: suiteStack.length + classStack.length,
|
|
272
435
|
});
|
|
273
436
|
i = range.endIdx;
|
|
274
437
|
continue;
|