@izhimu/pi-caveman 0.2.1 → 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.
- package/CHANGELOG.md +79 -26
- package/README.md +133 -139
- package/extensions/caveman.ts +771 -596
- package/package.json +57 -30
- package/skills/caveman/README.md +68 -52
- package/skills/caveman/SKILL.md +89 -90
- package/skills/megacave/README.md +11 -0
- package/skills/megacave/SKILL.md +65 -0
- package/skills/ultracave/README.md +13 -0
- package/skills/ultracave/SKILL.md +63 -0
- package/skills/caveman-compress/README.md +0 -176
- package/skills/caveman-compress/SECURITY.md +0 -31
- package/skills/caveman-compress/SKILL.md +0 -111
- package/skills/caveman-compress/scripts/__init__.py +0 -9
- package/skills/caveman-compress/scripts/__main__.py +0 -3
- package/skills/caveman-compress/scripts/benchmark.py +0 -80
- package/skills/caveman-compress/scripts/cli.py +0 -85
- package/skills/caveman-compress/scripts/compress.py +0 -505
- package/skills/caveman-compress/scripts/detect.py +0 -139
- package/skills/caveman-compress/scripts/validate.py +0 -408
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
<p align="center">
|
|
2
|
-
<img src="https://em-content.zobj.net/source/apple/391/rock_1faa8.png" width="80" />
|
|
3
|
-
</p>
|
|
4
|
-
|
|
5
|
-
<h1 align="center">caveman-compress</h1>
|
|
6
|
-
|
|
7
|
-
<p align="center">
|
|
8
|
-
<strong>shrink memory file. save token every session.</strong>
|
|
9
|
-
</p>
|
|
10
|
-
|
|
11
|
-
---
|
|
12
|
-
|
|
13
|
-
A Claude Code skill that compresses project memory files (`CLAUDE.md`, todos,
|
|
14
|
-
preferences) into caveman format, reducing repeated input size.
|
|
15
|
-
|
|
16
|
-
Claude loads `CLAUDE.md` on every session start, so large files add repeated
|
|
17
|
-
input tokens. Caveman shortens supported natural-language files.
|
|
18
|
-
|
|
19
|
-
## What It Do
|
|
20
|
-
|
|
21
|
-
```
|
|
22
|
-
/caveman-compress CLAUDE.md
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
```
|
|
26
|
-
CLAUDE.md ← compressed (Claude reads smaller file each session)
|
|
27
|
-
CLAUDE.original.md ← human-readable backup (you edit this)
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
Original remains in data directory rather than next to live file, so skill
|
|
31
|
-
auto-loaders do not read it twice. Path is
|
|
32
|
-
`$XDG_DATA_HOME/caveman-compress/backups/<parent-dir-name>/` on macOS and Linux,
|
|
33
|
-
or `%LOCALAPPDATA%\caveman-compress\backups\<parent-dir-name>\` on Windows. Edit
|
|
34
|
-
`.original.md` there, then run skill again to re-compress.
|
|
35
|
-
|
|
36
|
-
## Benchmarks
|
|
37
|
-
|
|
38
|
-
Real results on real project files:
|
|
39
|
-
|
|
40
|
-
| File | Original | Compressed | Saved |
|
|
41
|
-
|------|----------:|----------:|------:|
|
|
42
|
-
| `claude-md-preferences.md` | 706 | 285 | 59.6% |
|
|
43
|
-
| `project-notes.md` | 1145 | 535 | 53.3% |
|
|
44
|
-
| `claude-md-project.md` | 1122 | 636 | 43.3% |
|
|
45
|
-
| `todo-list.md` | 627 | 388 | 38.1% |
|
|
46
|
-
| `mixed-with-code.md` | 888 | 560 | 36.9% |
|
|
47
|
-
| Average | 898 | 481 | 46% |
|
|
48
|
-
|
|
49
|
-
All fixture validations passed: headings, code blocks, URLs, and file paths were
|
|
50
|
-
preserved exactly.
|
|
51
|
-
|
|
52
|
-
## Before / After
|
|
53
|
-
|
|
54
|
-
<table>
|
|
55
|
-
<tr>
|
|
56
|
-
<td width="50%">
|
|
57
|
-
|
|
58
|
-
### Original (706 tokens)
|
|
59
|
-
|
|
60
|
-
> "I strongly prefer TypeScript with strict mode enabled for all new code. Please don't use `any` type unless there's genuinely no way around it, and if you do, leave a comment explaining the reasoning. I find that taking the time to properly type things catches a lot of bugs before they ever make it to runtime."
|
|
61
|
-
|
|
62
|
-
</td>
|
|
63
|
-
<td width="50%">
|
|
64
|
-
|
|
65
|
-
### <img src="../../docs/assets/dancing-rock.svg" width="20" height="20" alt="rock"/> Caveman (285 tokens)
|
|
66
|
-
|
|
67
|
-
> "Prefer TypeScript strict mode always. No `any` unless unavoidable; comment why if used. Proper types catch bugs early."
|
|
68
|
-
|
|
69
|
-
</td>
|
|
70
|
-
</tr>
|
|
71
|
-
</table>
|
|
72
|
-
|
|
73
|
-
This fixture produced 59.6% fewer counted tokens. Structural validation passed;
|
|
74
|
-
result does not prove semantic equivalence on other files or models.
|
|
75
|
-
|
|
76
|
-
## Security
|
|
77
|
-
|
|
78
|
-
`caveman-compress` is flagged as Snyk High Risk due to subprocess and file I/O
|
|
79
|
-
patterns detected by static analysis. See [SECURITY.md](./SECURITY.md) for why
|
|
80
|
-
these operations exist and how paths are constrained.
|
|
81
|
-
|
|
82
|
-
## Install
|
|
83
|
-
|
|
84
|
-
Compress is built in with the `caveman` plugin. Install `caveman` once, then use `/caveman-compress`.
|
|
85
|
-
|
|
86
|
-
If you need local files, the compress skill lives at:
|
|
87
|
-
|
|
88
|
-
```bash
|
|
89
|
-
skills/caveman-compress/
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
Requires Python 3.10 or newer.
|
|
93
|
-
|
|
94
|
-
## Usage
|
|
95
|
-
|
|
96
|
-
```
|
|
97
|
-
/caveman-compress <filepath>
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
Examples:
|
|
101
|
-
```
|
|
102
|
-
/caveman-compress CLAUDE.md
|
|
103
|
-
/caveman-compress docs/preferences.md
|
|
104
|
-
/caveman-compress todos.md
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
### What files work
|
|
108
|
-
|
|
109
|
-
| Type | Compress? |
|
|
110
|
-
|------|-----------|
|
|
111
|
-
| `.md`, `.txt`, `.rst`, `.typ`, `.typst`, `.tex` | Yes |
|
|
112
|
-
| Extensionless natural language | Yes |
|
|
113
|
-
| `.py`, `.js`, `.ts`, `.json`, `.yaml` | ❌ Skip (code/config) |
|
|
114
|
-
| `*.original.md` | ❌ Skip (backup files) |
|
|
115
|
-
|
|
116
|
-
## How It Work
|
|
117
|
-
|
|
118
|
-
```
|
|
119
|
-
/caveman-compress CLAUDE.md
|
|
120
|
-
↓
|
|
121
|
-
detect file type (no tokens)
|
|
122
|
-
↓
|
|
123
|
-
Claude compresses (tokens: one call)
|
|
124
|
-
↓
|
|
125
|
-
validate output (no tokens)
|
|
126
|
-
checks: headings, code blocks, URLs, file paths, bullets
|
|
127
|
-
↓
|
|
128
|
-
if errors: Claude fixes cherry-picked issues only (tokens: targeted fix)
|
|
129
|
-
does NOT recompress; only patches broken parts
|
|
130
|
-
↓
|
|
131
|
-
retry up to 2 times
|
|
132
|
-
↓
|
|
133
|
-
write compressed → CLAUDE.md
|
|
134
|
-
write original → CLAUDE.original.md
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
Only two things use tokens: initial compression + targeted fix if validation fails. Everything else is local Python.
|
|
138
|
-
|
|
139
|
-
## What Is Preserved
|
|
140
|
-
|
|
141
|
-
Caveman compress natural language. It never touch:
|
|
142
|
-
|
|
143
|
-
- Code blocks (` ``` ` fenced or indented)
|
|
144
|
-
- Inline code (`` `backtick content` ``)
|
|
145
|
-
- URLs and links
|
|
146
|
-
- File paths (`/src/components/...`)
|
|
147
|
-
- Commands (`npm install`, `git commit`)
|
|
148
|
-
- Technical terms, library names, API names
|
|
149
|
-
- Headings (exact text preserved)
|
|
150
|
-
- Tables (structure preserved, cell text compressed)
|
|
151
|
-
- Dates, version numbers, numeric values
|
|
152
|
-
|
|
153
|
-
## Why This Matter
|
|
154
|
-
|
|
155
|
-
`CLAUDE.md` loads on every session start. A 1,000-token project memory file adds
|
|
156
|
-
1,000 input tokens each time project opens, or 100,000 across 100 sessions.
|
|
157
|
-
|
|
158
|
-
Caveman reduced counted tokens by about 46% on five listed fixtures. Validators
|
|
159
|
-
confirmed headings, code blocks, URLs, and file paths. They did not establish
|
|
160
|
-
general semantic or task-quality equivalence.
|
|
161
|
-
|
|
162
|
-
```
|
|
163
|
-
┌────────────────────────────────────────────┐
|
|
164
|
-
│ TOKEN SAVINGS PER FILE █████ 46% │
|
|
165
|
-
│ FIXTURES IN TABLE 5 │
|
|
166
|
-
│ STRUCTURAL VALIDATION passed on all │
|
|
167
|
-
│ SETUP TIME █ 1x │
|
|
168
|
-
└────────────────────────────────────────────┘
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
## Part of Caveman
|
|
172
|
-
|
|
173
|
-
This skill is part of the [caveman](https://github.com/JuliusBrussee/caveman) toolkit.
|
|
174
|
-
|
|
175
|
-
- `caveman`: ask Claude to answer in shorter prose
|
|
176
|
-
- `caveman-compress`: shorten supported project-memory files with backups and validation
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
# Security
|
|
2
|
-
|
|
3
|
-
## Snyk High Risk Rating
|
|
4
|
-
|
|
5
|
-
`caveman-compress` receives a Snyk High Risk rating due to static analysis heuristics. This document explains what the skill does and does not do.
|
|
6
|
-
|
|
7
|
-
### What triggers the rating
|
|
8
|
-
|
|
9
|
-
1. **subprocess usage**: The skill calls the `claude` CLI via `subprocess.run()` as a fallback when `ANTHROPIC_API_KEY` is not set. The subprocess call uses a fixed argument list — no shell interpolation occurs. User file content is passed via stdin, not as a shell argument.
|
|
10
|
-
|
|
11
|
-
2. **File read/write**: The skill reads the file the user explicitly points it at, compresses it, and writes the result back to the same path. A `.original.md` backup is saved to an out-of-tree data dir (`$XDG_DATA_HOME/caveman-compress/backups/<parent-dir-name>/`, or `%LOCALAPPDATA%\caveman-compress\backups\<parent-dir-name>\` on Windows). Beyond the target file and that backup location, no files are read or written.
|
|
12
|
-
|
|
13
|
-
### What the skill does NOT do
|
|
14
|
-
|
|
15
|
-
- Does not execute user file content as code
|
|
16
|
-
- Does not make network requests except to Anthropic's API (via SDK or CLI)
|
|
17
|
-
- Does not access files outside the path the user provides
|
|
18
|
-
- Does not use shell=True or string interpolation in subprocess calls
|
|
19
|
-
- Does not collect or transmit any data beyond the file being compressed
|
|
20
|
-
|
|
21
|
-
### Auth behavior
|
|
22
|
-
|
|
23
|
-
If `ANTHROPIC_API_KEY` is set, the skill uses the Anthropic Python SDK directly (no subprocess). If not set, it falls back to the `claude` CLI, which uses the user's existing Claude desktop authentication.
|
|
24
|
-
|
|
25
|
-
### File size limit
|
|
26
|
-
|
|
27
|
-
Files larger than 500KB are rejected before any API call is made.
|
|
28
|
-
|
|
29
|
-
### Reporting a vulnerability
|
|
30
|
-
|
|
31
|
-
If you believe you've found a genuine security issue, please open a GitHub issue with the label `security`.
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: caveman-compress
|
|
3
|
-
description: >
|
|
4
|
-
Compress natural language memory files (CLAUDE.md, todos, preferences) into caveman format
|
|
5
|
-
to save input tokens. Preserves all technical substance, code, URLs, and structure.
|
|
6
|
-
Compressed version overwrites the original file. Human-readable backup saved as FILE.original.md.
|
|
7
|
-
Trigger: /caveman-compress FILEPATH or "compress memory file"
|
|
8
|
-
---
|
|
9
|
-
|
|
10
|
-
# Caveman Compress
|
|
11
|
-
|
|
12
|
-
## Purpose
|
|
13
|
-
|
|
14
|
-
Compress natural language files (CLAUDE.md, todos, preferences) into caveman-speak to reduce input tokens. Compressed version overwrites original. Human-readable backup saved as `<filename>.original.md`, but NOT beside the source file — it lives in an out-of-tree data dir (`$XDG_DATA_HOME/caveman-compress/backups/<parent-dir-name>/`, or `%LOCALAPPDATA%\caveman-compress\backups\<parent-dir-name>\` on Windows) so skill auto-loaders don't re-ingest it as a live file.
|
|
15
|
-
|
|
16
|
-
## Trigger
|
|
17
|
-
|
|
18
|
-
`/caveman-compress <filepath>` or when user asks to compress a memory file.
|
|
19
|
-
|
|
20
|
-
## Process
|
|
21
|
-
|
|
22
|
-
1. The compression scripts live in `scripts/` (adjacent to this SKILL.md). If the path is not immediately available, search for `scripts/__main__.py` next to this SKILL.md.
|
|
23
|
-
|
|
24
|
-
2. From the directory containing this SKILL.md, run:
|
|
25
|
-
|
|
26
|
-
python3 -m scripts <absolute_filepath>
|
|
27
|
-
|
|
28
|
-
3. The CLI will:
|
|
29
|
-
- detect file type (no tokens)
|
|
30
|
-
- call Claude to compress
|
|
31
|
-
- validate output (no tokens)
|
|
32
|
-
- if errors: cherry-pick fix with Claude (targeted fixes only, no recompression)
|
|
33
|
-
- retry up to 2 times
|
|
34
|
-
- if still failing after 2 retries: report error to user, leave original file untouched
|
|
35
|
-
|
|
36
|
-
4. Return result to user
|
|
37
|
-
|
|
38
|
-
## Compression Rules
|
|
39
|
-
|
|
40
|
-
### Remove
|
|
41
|
-
- Articles: a, an, the
|
|
42
|
-
- Filler: just, really, basically, actually, simply, essentially, generally
|
|
43
|
-
- Pleasantries: "sure", "certainly", "of course", "happy to", "I'd recommend"
|
|
44
|
-
- Hedging: "it might be worth", "you could consider", "it would be good to"
|
|
45
|
-
- Redundant phrasing: "in order to" → "to", "make sure to" → "ensure", "the reason is because" → "because"
|
|
46
|
-
- Connective fluff: "however", "furthermore", "additionally", "in addition"
|
|
47
|
-
|
|
48
|
-
### Preserve EXACTLY (never modify)
|
|
49
|
-
- Code blocks (fenced ``` and indented)
|
|
50
|
-
- Inline code (`backtick content`)
|
|
51
|
-
- URLs and links (full URLs, markdown links)
|
|
52
|
-
- File paths (`/src/components/...`, `./config.yaml`)
|
|
53
|
-
- Commands (`npm install`, `git commit`, `docker build`)
|
|
54
|
-
- Technical terms (library names, API names, protocols, algorithms)
|
|
55
|
-
- Proper nouns (project names, people, companies)
|
|
56
|
-
- Dates, version numbers, numeric values
|
|
57
|
-
- Environment variables (`$HOME`, `NODE_ENV`)
|
|
58
|
-
|
|
59
|
-
### Preserve Structure
|
|
60
|
-
- All markdown headings (keep exact heading text, compress body below)
|
|
61
|
-
- Bullet point hierarchy (keep nesting level)
|
|
62
|
-
- Numbered lists (keep numbering)
|
|
63
|
-
- Tables (compress cell text, keep structure)
|
|
64
|
-
- Frontmatter/YAML headers in markdown files
|
|
65
|
-
|
|
66
|
-
### Compress
|
|
67
|
-
- Use short synonyms: "big" not "extensive", "fix" not "implement a solution for", "use" not "utilize"
|
|
68
|
-
- Fragments OK: "Run tests before commit" not "You should always run tests before committing"
|
|
69
|
-
- Drop "you should", "make sure to", "remember to" — just state the action
|
|
70
|
-
- Merge redundant bullets that say the same thing differently
|
|
71
|
-
- Keep one example where multiple examples show the same pattern
|
|
72
|
-
|
|
73
|
-
CRITICAL RULE:
|
|
74
|
-
Anything inside ``` ... ``` must be copied EXACTLY.
|
|
75
|
-
Do not:
|
|
76
|
-
- remove comments
|
|
77
|
-
- remove spacing
|
|
78
|
-
- reorder lines
|
|
79
|
-
- shorten commands
|
|
80
|
-
- simplify anything
|
|
81
|
-
|
|
82
|
-
Inline code (`...`) must be preserved EXACTLY.
|
|
83
|
-
Do not modify anything inside backticks.
|
|
84
|
-
|
|
85
|
-
If file contains code blocks:
|
|
86
|
-
- Treat code blocks as read-only regions
|
|
87
|
-
- Only compress text outside them
|
|
88
|
-
- Do not merge sections around code
|
|
89
|
-
|
|
90
|
-
## Pattern
|
|
91
|
-
|
|
92
|
-
Original:
|
|
93
|
-
> You should always make sure to run the test suite before pushing any changes to the main branch. This is important because it helps catch bugs early and prevents broken builds from being deployed to production.
|
|
94
|
-
|
|
95
|
-
Compressed:
|
|
96
|
-
> Run tests before push to main. Catch bugs early, prevent broken prod deploys.
|
|
97
|
-
|
|
98
|
-
Original:
|
|
99
|
-
> The application uses a microservices architecture with the following components. The API gateway handles all incoming requests and routes them to the appropriate service. The authentication service is responsible for managing user sessions and JWT tokens.
|
|
100
|
-
|
|
101
|
-
Compressed:
|
|
102
|
-
> Microservices architecture. API gateway route all requests to services. Auth service manage user sessions + JWT tokens.
|
|
103
|
-
|
|
104
|
-
## Boundaries
|
|
105
|
-
|
|
106
|
-
- ONLY compress natural language files (.md, .txt, .typ, .typst, .tex, extensionless)
|
|
107
|
-
- NEVER modify: .py, .js, .ts, .json, .yaml, .yml, .toml, .env, .lock, .css, .html, .xml, .sql, .sh
|
|
108
|
-
- If file has mixed content (prose + code), compress ONLY the prose sections
|
|
109
|
-
- If unsure whether something is code or prose, leave it unchanged
|
|
110
|
-
- Original file is backed up as FILE.original.md before overwriting — in the out-of-tree backup data dir (see Purpose), not beside the source file
|
|
111
|
-
- Never compress FILE.original.md (skip it)
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
from pathlib import Path
|
|
3
|
-
import sys
|
|
4
|
-
|
|
5
|
-
# Support both direct execution and module import
|
|
6
|
-
try:
|
|
7
|
-
from .validate import validate
|
|
8
|
-
except ImportError:
|
|
9
|
-
sys.path.insert(0, str(Path(__file__).parent))
|
|
10
|
-
from validate import validate
|
|
11
|
-
|
|
12
|
-
try:
|
|
13
|
-
import tiktoken
|
|
14
|
-
_enc = tiktoken.get_encoding("o200k_base")
|
|
15
|
-
except ImportError:
|
|
16
|
-
_enc = None
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
def count_tokens(text):
|
|
20
|
-
if _enc is None:
|
|
21
|
-
return len(text.split()) # fallback: word count
|
|
22
|
-
return len(_enc.encode(text))
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
def benchmark_pair(orig_path: Path, comp_path: Path):
|
|
26
|
-
orig_text = orig_path.read_text(encoding="utf-8", errors="ignore")
|
|
27
|
-
comp_text = comp_path.read_text(encoding="utf-8", errors="ignore")
|
|
28
|
-
|
|
29
|
-
orig_tokens = count_tokens(orig_text)
|
|
30
|
-
comp_tokens = count_tokens(comp_text)
|
|
31
|
-
saved = 100 * (orig_tokens - comp_tokens) / orig_tokens if orig_tokens > 0 else 0.0
|
|
32
|
-
result = validate(orig_path, comp_path)
|
|
33
|
-
|
|
34
|
-
return (comp_path.name, orig_tokens, comp_tokens, saved, result.is_valid)
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
def print_table(rows):
|
|
38
|
-
print("\n| File | Original | Compressed | Saved % | Valid |")
|
|
39
|
-
print("|------|----------|------------|---------|-------|")
|
|
40
|
-
for r in rows:
|
|
41
|
-
print(f"| {r[0]} | {r[1]} | {r[2]} | {r[3]:.1f}% | {'✅' if r[4] else '❌'} |")
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
def main():
|
|
45
|
-
# Direct file pair: python3 benchmark.py original.md compressed.md
|
|
46
|
-
if len(sys.argv) == 3:
|
|
47
|
-
orig = Path(sys.argv[1]).resolve()
|
|
48
|
-
comp = Path(sys.argv[2]).resolve()
|
|
49
|
-
if not orig.exists():
|
|
50
|
-
print(f"❌ Not found: {orig}")
|
|
51
|
-
sys.exit(1)
|
|
52
|
-
if not comp.exists():
|
|
53
|
-
print(f"❌ Not found: {comp}")
|
|
54
|
-
sys.exit(1)
|
|
55
|
-
print_table([benchmark_pair(orig, comp)])
|
|
56
|
-
return
|
|
57
|
-
|
|
58
|
-
# Glob mode: repo_root/tests/caveman-compress/
|
|
59
|
-
# __file__ lives at <repo_root>/skills/caveman-compress/scripts/benchmark.py
|
|
60
|
-
# Walk up four dirs: scripts → caveman-compress → skills → repo_root.
|
|
61
|
-
tests_dir = Path(__file__).resolve().parents[3] / "tests" / "caveman-compress"
|
|
62
|
-
if not tests_dir.exists():
|
|
63
|
-
print(f"❌ Tests dir not found: {tests_dir}")
|
|
64
|
-
sys.exit(1)
|
|
65
|
-
|
|
66
|
-
rows = []
|
|
67
|
-
for orig in sorted(tests_dir.glob("*.original.md")):
|
|
68
|
-
comp = orig.with_name(orig.stem.removesuffix(".original") + ".md")
|
|
69
|
-
if comp.exists():
|
|
70
|
-
rows.append(benchmark_pair(orig, comp))
|
|
71
|
-
|
|
72
|
-
if not rows:
|
|
73
|
-
print("No compressed file pairs found.")
|
|
74
|
-
return
|
|
75
|
-
|
|
76
|
-
print_table(rows)
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if __name__ == "__main__":
|
|
80
|
-
main()
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
"""
|
|
3
|
-
Caveman Compress CLI
|
|
4
|
-
|
|
5
|
-
Usage:
|
|
6
|
-
caveman <filepath>
|
|
7
|
-
"""
|
|
8
|
-
|
|
9
|
-
import sys
|
|
10
|
-
|
|
11
|
-
# Force UTF-8 on stdout/stderr before any code can print. Windows consoles
|
|
12
|
-
# default to cp1252 and crash on the ❌ glyphs in error/validation branches,
|
|
13
|
-
# masking the real error and leaving the user with a half-compressed file.
|
|
14
|
-
for _stream in (sys.stdout, sys.stderr):
|
|
15
|
-
reconfigure = getattr(_stream, "reconfigure", None)
|
|
16
|
-
if callable(reconfigure):
|
|
17
|
-
try:
|
|
18
|
-
reconfigure(encoding="utf-8", errors="replace")
|
|
19
|
-
except Exception:
|
|
20
|
-
pass
|
|
21
|
-
|
|
22
|
-
from pathlib import Path
|
|
23
|
-
|
|
24
|
-
from .compress import backup_dir_for, compress_file
|
|
25
|
-
from .detect import detect_file_type, should_compress
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
def print_usage():
|
|
29
|
-
print("Usage: caveman <filepath>")
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
def main():
|
|
33
|
-
if len(sys.argv) != 2:
|
|
34
|
-
print_usage()
|
|
35
|
-
sys.exit(1)
|
|
36
|
-
|
|
37
|
-
filepath = Path(sys.argv[1])
|
|
38
|
-
|
|
39
|
-
# Check file exists
|
|
40
|
-
if not filepath.exists():
|
|
41
|
-
print(f"❌ File not found: {filepath}")
|
|
42
|
-
sys.exit(1)
|
|
43
|
-
|
|
44
|
-
if not filepath.is_file():
|
|
45
|
-
print(f"❌ Not a file: {filepath}")
|
|
46
|
-
sys.exit(1)
|
|
47
|
-
|
|
48
|
-
filepath = filepath.resolve()
|
|
49
|
-
|
|
50
|
-
# Detect file type
|
|
51
|
-
file_type = detect_file_type(filepath)
|
|
52
|
-
|
|
53
|
-
print(f"Detected: {file_type}")
|
|
54
|
-
|
|
55
|
-
# Check if compressible
|
|
56
|
-
if not should_compress(filepath):
|
|
57
|
-
print("Skipping: file is not natural language (code/config)")
|
|
58
|
-
sys.exit(0)
|
|
59
|
-
|
|
60
|
-
print("Starting caveman compression...\n")
|
|
61
|
-
|
|
62
|
-
try:
|
|
63
|
-
success = compress_file(filepath)
|
|
64
|
-
|
|
65
|
-
if success:
|
|
66
|
-
print("\nCompression completed successfully")
|
|
67
|
-
backup_path = backup_dir_for(filepath) / (filepath.stem + ".original.md")
|
|
68
|
-
print(f"Compressed: {filepath}")
|
|
69
|
-
print(f"Original: {backup_path}")
|
|
70
|
-
sys.exit(0)
|
|
71
|
-
else:
|
|
72
|
-
print("\n❌ Compression failed after retries")
|
|
73
|
-
sys.exit(2)
|
|
74
|
-
|
|
75
|
-
except KeyboardInterrupt:
|
|
76
|
-
print("\nInterrupted by user")
|
|
77
|
-
sys.exit(130)
|
|
78
|
-
|
|
79
|
-
except Exception as e:
|
|
80
|
-
print(f"\n❌ Error: {e}")
|
|
81
|
-
sys.exit(1)
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if __name__ == "__main__":
|
|
85
|
-
main()
|