@abarcenas/ai-workflow-template 1.2.0 → 1.13.0
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/.agents/instructions/learned-knowledge.instructions.md +46 -0
- package/{.github → .agents}/skills/architecture-blueprint-generator/SKILL.md +57 -1
- package/.agents/skills/cavecrew/SKILL.md +82 -0
- package/.agents/skills/caveman/SKILL.md +74 -0
- package/.agents/skills/caveman-commit/SKILL.md +65 -0
- package/.agents/skills/caveman-compress/README.md +163 -0
- package/.agents/skills/caveman-compress/SECURITY.md +31 -0
- package/.agents/skills/caveman-compress/SKILL.md +111 -0
- package/.agents/skills/caveman-compress/scripts/__init__.py +9 -0
- package/.agents/skills/caveman-compress/scripts/__main__.py +3 -0
- package/.agents/skills/caveman-compress/scripts/benchmark.py +78 -0
- package/.agents/skills/caveman-compress/scripts/cli.py +85 -0
- package/.agents/skills/caveman-compress/scripts/compress.py +254 -0
- package/.agents/skills/caveman-compress/scripts/detect.py +121 -0
- package/.agents/skills/caveman-compress/scripts/validate.py +213 -0
- package/.agents/skills/caveman-help/SKILL.md +59 -0
- package/.agents/skills/caveman-review/SKILL.md +55 -0
- package/.agents/skills/caveman-stats/SKILL.md +10 -0
- package/.agents/skills/compress/SKILL.md +111 -0
- package/.agents/skills/compress/scripts/__init__.py +9 -0
- package/.agents/skills/compress/scripts/__main__.py +3 -0
- package/.agents/skills/compress/scripts/benchmark.py +78 -0
- package/.agents/skills/compress/scripts/cli.py +85 -0
- package/.agents/skills/compress/scripts/compress.py +254 -0
- package/.agents/skills/compress/scripts/detect.py +121 -0
- package/.agents/skills/compress/scripts/validate.py +213 -0
- package/AGENTS.md +149 -0
- package/README.md +197 -60
- package/opencode.mcp.example.json +54 -0
- package/package.json +13 -5
- package/scripts/mcp/playwright-mcp-launcher.js +46 -0
- package/scripts/sync.js +50 -12
- package/.github/agents/implementation-plan.agent.md +0 -192
- package/.github/agents/plan.agent.md +0 -135
- package/.github/agents/research-spike.agent.md +0 -204
- package/.github/prompts/copilot-instructions-blueprint-generator.prompt.md +0 -317
- /package/{.github → .agents}/instructions/agent-safety.instructions.md +0 -0
- /package/{.github → .agents}/instructions/agent-skill.instructions.md +0 -0
- /package/{.github → .agents}/instructions/agent.instructions.md +0 -0
- /package/{.github → .agents}/instructions/ai-prompt-engineering-safety.instructions.md +0 -0
- /package/{.github → .agents}/instructions/angular.instructions.md +0 -0
- /package/{.github → .agents}/instructions/context7.instructions.md +0 -0
- /package/{.github → .agents}/instructions/go.instructions.md +0 -0
- /package/{.github → .agents}/instructions/html-to-css-guide.instructions.md +0 -0
- /package/{.github → .agents}/instructions/instructions.instructions.md +0 -0
- /package/{.github → .agents}/instructions/markdown.instructions.md +0 -0
- /package/{.github → .agents}/instructions/memory-bank.instructions.md +0 -0
- /package/{.github → .agents}/instructions/nodejs-vitest.instructions.md +0 -0
- /package/{.github → .agents}/instructions/playwright-typescript.instructions.md +0 -0
- /package/{.github → .agents}/instructions/prompt.instructions.md +0 -0
- /package/{.github → .agents}/instructions/security-owasp.instructions.md +0 -0
- /package/{.github → .agents}/instructions/shell.instructions.md +0 -0
- /package/{.github → .agents}/instructions/task-implementation.instructions.md +0 -0
- /package/{.github → .agents}/instructions/typescript.instructions.md +0 -0
- /package/{.github → .agents}/prompts/documentation-writer.prompt.md +0 -0
- /package/{.github → .agents}/prompts/editconfig.prompt.md +0 -0
- /package/{.github → .agents}/prompts/playwright-explore.prompt.md +0 -0
- /package/{.github → .agents}/prompts/playwright-generate-test.prompt.md +0 -0
- /package/{.github → .agents}/skills/agent-governance/SKILL.md +0 -0
- /package/{.github → .agents}/skills/boost-prompt/SKILL.md +0 -0
- /package/{.github → .agents}/skills/chrome-devtools/SKILL.md +0 -0
- /package/{.github → .agents}/skills/context-map/SKILL.md +0 -0
- /package/{.github → .agents}/skills/conventional-commit/SKILL.md +0 -0
- /package/{.github → .agents}/skills/create-implementation-plan/SKILL.md +0 -0
- /package/{.github → .agents}/skills/create-readme/SKILL.md +0 -0
- /package/{.github → .agents}/skills/create-specification/SKILL.md +0 -0
- /package/{.github → .agents}/skills/editorconfig/SKILL.md +0 -0
- /package/{.github → .agents}/skills/gh-cli/SKILL.md +0 -0
- /package/{.github → .agents}/skills/git-commit/SKILL.md +0 -0
- /package/{.github → .agents}/skills/github-issues/SKILL.md +0 -0
- /package/{.github → .agents}/skills/github-issues/references/templates.md +0 -0
- /package/{.github → .agents}/skills/microsoft-docs/SKILL.md +0 -0
- /package/{.github → .agents}/skills/playwright-explore-website/SKILL.md +0 -0
- /package/{.github → .agents}/skills/playwright-generate-test/SKILL.md +0 -0
- /package/{.github → .agents}/skills/polygot-test-agent/SKILL.md +0 -0
- /package/{.github → .agents}/skills/polygot-test-agent/unit-test-genearation.prompt.md +0 -0
- /package/{.github → .agents}/skills/prompt-builder/SKILL.md +0 -0
|
@@ -0,0 +1,78 @@
|
|
|
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()
|
|
27
|
+
comp_text = comp_path.read_text()
|
|
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
|
+
tests_dir = Path(__file__).parent.parent.parent / "tests" / "caveman-compress"
|
|
60
|
+
if not tests_dir.exists():
|
|
61
|
+
print(f"❌ Tests dir not found: {tests_dir}")
|
|
62
|
+
sys.exit(1)
|
|
63
|
+
|
|
64
|
+
rows = []
|
|
65
|
+
for orig in sorted(tests_dir.glob("*.original.md")):
|
|
66
|
+
comp = orig.with_name(orig.stem.removesuffix(".original") + ".md")
|
|
67
|
+
if comp.exists():
|
|
68
|
+
rows.append(benchmark_pair(orig, comp))
|
|
69
|
+
|
|
70
|
+
if not rows:
|
|
71
|
+
print("No compressed file pairs found.")
|
|
72
|
+
return
|
|
73
|
+
|
|
74
|
+
print_table(rows)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
if __name__ == "__main__":
|
|
78
|
+
main()
|
|
@@ -0,0 +1,85 @@
|
|
|
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 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 = filepath.with_name(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()
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Caveman Memory Compression Orchestrator
|
|
4
|
+
|
|
5
|
+
Usage:
|
|
6
|
+
python scripts/compress.py <filepath>
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import os
|
|
10
|
+
import re
|
|
11
|
+
import subprocess
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from typing import List
|
|
14
|
+
|
|
15
|
+
OUTER_FENCE_REGEX = re.compile(
|
|
16
|
+
r"\A\s*(`{3,}|~{3,})[^\n]*\n(.*)\n\1\s*\Z", re.DOTALL
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
# Filenames and paths that almost certainly hold secrets or PII. Compressing
|
|
20
|
+
# them ships raw bytes to the Anthropic API — a third-party data boundary that
|
|
21
|
+
# developers on sensitive codebases cannot cross. detect.py already skips .env
|
|
22
|
+
# by extension, but credentials.md / secrets.txt / ~/.aws/credentials would
|
|
23
|
+
# slip through the natural-language filter. This is a hard refuse before read.
|
|
24
|
+
SENSITIVE_BASENAME_REGEX = re.compile(
|
|
25
|
+
r"(?ix)^("
|
|
26
|
+
r"\.env(\..+)?"
|
|
27
|
+
r"|\.netrc"
|
|
28
|
+
r"|credentials(\..+)?"
|
|
29
|
+
r"|secrets?(\..+)?"
|
|
30
|
+
r"|passwords?(\..+)?"
|
|
31
|
+
r"|id_(rsa|dsa|ecdsa|ed25519)(\.pub)?"
|
|
32
|
+
r"|authorized_keys"
|
|
33
|
+
r"|known_hosts"
|
|
34
|
+
r"|.*\.(pem|key|p12|pfx|crt|cer|jks|keystore|asc|gpg)"
|
|
35
|
+
r")$"
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
SENSITIVE_PATH_COMPONENTS = frozenset({".ssh", ".aws", ".gnupg", ".kube", ".docker"})
|
|
39
|
+
|
|
40
|
+
SENSITIVE_NAME_TOKENS = (
|
|
41
|
+
"secret", "credential", "password", "passwd",
|
|
42
|
+
"apikey", "accesskey", "token", "privatekey",
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def is_sensitive_path(filepath: Path) -> bool:
|
|
47
|
+
"""Heuristic denylist for files that must never be shipped to a third-party API."""
|
|
48
|
+
name = filepath.name
|
|
49
|
+
if SENSITIVE_BASENAME_REGEX.match(name):
|
|
50
|
+
return True
|
|
51
|
+
lowered_parts = {p.lower() for p in filepath.parts}
|
|
52
|
+
if lowered_parts & SENSITIVE_PATH_COMPONENTS:
|
|
53
|
+
return True
|
|
54
|
+
# Normalize separators so "api-key" and "api_key" both match "apikey".
|
|
55
|
+
lower = re.sub(r"[_\-\s.]", "", name.lower())
|
|
56
|
+
return any(tok in lower for tok in SENSITIVE_NAME_TOKENS)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def strip_llm_wrapper(text: str) -> str:
|
|
60
|
+
"""Strip outer ```markdown ... ``` fence when it wraps the entire output."""
|
|
61
|
+
m = OUTER_FENCE_REGEX.match(text)
|
|
62
|
+
if m:
|
|
63
|
+
return m.group(2)
|
|
64
|
+
return text
|
|
65
|
+
|
|
66
|
+
from .detect import should_compress
|
|
67
|
+
from .validate import validate
|
|
68
|
+
|
|
69
|
+
MAX_RETRIES = 2
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
# ---------- Claude Calls ----------
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def call_claude(prompt: str) -> str:
|
|
76
|
+
api_key = os.environ.get("ANTHROPIC_API_KEY")
|
|
77
|
+
if api_key:
|
|
78
|
+
try:
|
|
79
|
+
import anthropic
|
|
80
|
+
|
|
81
|
+
client = anthropic.Anthropic(api_key=api_key)
|
|
82
|
+
msg = client.messages.create(
|
|
83
|
+
model=os.environ.get("CAVEMAN_MODEL", "claude-sonnet-4-5"),
|
|
84
|
+
max_tokens=8192,
|
|
85
|
+
messages=[{"role": "user", "content": prompt}],
|
|
86
|
+
)
|
|
87
|
+
return strip_llm_wrapper(msg.content[0].text.strip())
|
|
88
|
+
except ImportError:
|
|
89
|
+
pass # anthropic not installed, fall back to CLI
|
|
90
|
+
# Fallback: use claude CLI (handles desktop auth)
|
|
91
|
+
try:
|
|
92
|
+
result = subprocess.run(
|
|
93
|
+
["claude", "--print"],
|
|
94
|
+
input=prompt,
|
|
95
|
+
text=True,
|
|
96
|
+
capture_output=True,
|
|
97
|
+
check=True,
|
|
98
|
+
)
|
|
99
|
+
return strip_llm_wrapper(result.stdout.strip())
|
|
100
|
+
except subprocess.CalledProcessError as e:
|
|
101
|
+
raise RuntimeError(f"Claude call failed:\n{e.stderr}")
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def build_compress_prompt(original: str) -> str:
|
|
105
|
+
return f"""
|
|
106
|
+
Compress this markdown into caveman format.
|
|
107
|
+
|
|
108
|
+
STRICT RULES:
|
|
109
|
+
- Do NOT modify anything inside ``` code blocks
|
|
110
|
+
- Do NOT modify anything inside inline backticks
|
|
111
|
+
- Preserve ALL URLs exactly
|
|
112
|
+
- Preserve ALL headings exactly
|
|
113
|
+
- Preserve file paths and commands
|
|
114
|
+
- Return ONLY the compressed markdown body — do NOT wrap the entire output in a ```markdown fence or any other fence. Inner code blocks from the original stay as-is; do not add a new outer fence around the whole file.
|
|
115
|
+
|
|
116
|
+
Only compress natural language.
|
|
117
|
+
|
|
118
|
+
TEXT:
|
|
119
|
+
{original}
|
|
120
|
+
"""
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def build_fix_prompt(original: str, compressed: str, errors: List[str]) -> str:
|
|
124
|
+
errors_str = "\n".join(f"- {e}" for e in errors)
|
|
125
|
+
return f"""You are fixing a caveman-compressed markdown file. Specific validation errors were found.
|
|
126
|
+
|
|
127
|
+
CRITICAL RULES:
|
|
128
|
+
- DO NOT recompress or rephrase the file
|
|
129
|
+
- ONLY fix the listed errors — leave everything else exactly as-is
|
|
130
|
+
- The ORIGINAL is provided as reference only (to restore missing content)
|
|
131
|
+
- Preserve caveman style in all untouched sections
|
|
132
|
+
|
|
133
|
+
ERRORS TO FIX:
|
|
134
|
+
{errors_str}
|
|
135
|
+
|
|
136
|
+
HOW TO FIX:
|
|
137
|
+
- Missing URL: find it in ORIGINAL, restore it exactly where it belongs in COMPRESSED
|
|
138
|
+
- Code block mismatch: find the exact code block in ORIGINAL, restore it in COMPRESSED
|
|
139
|
+
- Heading mismatch: restore the exact heading text from ORIGINAL into COMPRESSED
|
|
140
|
+
- Do not touch any section not mentioned in the errors
|
|
141
|
+
|
|
142
|
+
ORIGINAL (reference only):
|
|
143
|
+
{original}
|
|
144
|
+
|
|
145
|
+
COMPRESSED (fix this):
|
|
146
|
+
{compressed}
|
|
147
|
+
|
|
148
|
+
Return ONLY the fixed compressed file. No explanation.
|
|
149
|
+
"""
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
# ---------- Core Logic ----------
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def compress_file(filepath: Path) -> bool:
|
|
156
|
+
# Resolve and validate path
|
|
157
|
+
filepath = filepath.resolve()
|
|
158
|
+
MAX_FILE_SIZE = 500_000 # 500KB
|
|
159
|
+
if not filepath.exists():
|
|
160
|
+
raise FileNotFoundError(f"File not found: {filepath}")
|
|
161
|
+
if filepath.stat().st_size > MAX_FILE_SIZE:
|
|
162
|
+
raise ValueError(f"File too large to compress safely (max 500KB): {filepath}")
|
|
163
|
+
|
|
164
|
+
# Refuse files that look like they contain secrets or PII. Compressing ships
|
|
165
|
+
# the raw bytes to the Anthropic API — a third-party boundary — so we fail
|
|
166
|
+
# loudly rather than silently exfiltrate credentials or keys. Override is
|
|
167
|
+
# intentional: the user must rename the file if the heuristic is wrong.
|
|
168
|
+
if is_sensitive_path(filepath):
|
|
169
|
+
raise ValueError(
|
|
170
|
+
f"Refusing to compress {filepath}: filename looks sensitive "
|
|
171
|
+
"(credentials, keys, secrets, or known private paths). "
|
|
172
|
+
"Compression sends file contents to the Anthropic API. "
|
|
173
|
+
"Rename the file if this is a false positive."
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
print(f"Processing: {filepath}")
|
|
177
|
+
|
|
178
|
+
if not should_compress(filepath):
|
|
179
|
+
print("Skipping (not natural language)")
|
|
180
|
+
return False
|
|
181
|
+
|
|
182
|
+
original_text = filepath.read_text(errors="ignore")
|
|
183
|
+
backup_path = filepath.with_name(filepath.stem + ".original.md")
|
|
184
|
+
|
|
185
|
+
if not original_text.strip():
|
|
186
|
+
print("❌ Refusing to compress: file is empty or whitespace-only.")
|
|
187
|
+
return False
|
|
188
|
+
|
|
189
|
+
# Check if backup already exists to prevent accidental overwriting
|
|
190
|
+
if backup_path.exists():
|
|
191
|
+
print(f"⚠️ Backup file already exists: {backup_path}")
|
|
192
|
+
print("The original backup may contain important content.")
|
|
193
|
+
print("Aborting to prevent data loss. Please remove or rename the backup file if you want to proceed.")
|
|
194
|
+
return False
|
|
195
|
+
|
|
196
|
+
# Step 1: Compress
|
|
197
|
+
print("Compressing with Claude...")
|
|
198
|
+
compressed = call_claude(build_compress_prompt(original_text))
|
|
199
|
+
|
|
200
|
+
if compressed is None or not compressed.strip():
|
|
201
|
+
print("❌ Compression aborted: Claude returned an empty response.")
|
|
202
|
+
print(" Original file is untouched (no backup created).")
|
|
203
|
+
return False
|
|
204
|
+
|
|
205
|
+
if compressed.strip() == original_text.strip():
|
|
206
|
+
print("❌ Compression aborted: output is identical to input.")
|
|
207
|
+
print(" Likely causes: Claude refused, returned the prompt verbatim, or the file is")
|
|
208
|
+
print(" already in caveman form. Original file is untouched (no backup created).")
|
|
209
|
+
return False
|
|
210
|
+
|
|
211
|
+
# Save original as backup, then verify the backup readback before
|
|
212
|
+
# touching the input file. If the filesystem dropped bytes (encoding,
|
|
213
|
+
# antivirus, disk full), unlink the bad backup and abort instead of
|
|
214
|
+
# leaving the user with a corrupt backup + compressed primary.
|
|
215
|
+
backup_path.write_text(original_text)
|
|
216
|
+
backup_readback = backup_path.read_text(errors="ignore")
|
|
217
|
+
if backup_readback != original_text:
|
|
218
|
+
print(f"❌ Backup write verification failed: {backup_path}")
|
|
219
|
+
print(" In-memory original differs from on-disk backup. Aborting before touching the input file.")
|
|
220
|
+
try:
|
|
221
|
+
backup_path.unlink()
|
|
222
|
+
except OSError:
|
|
223
|
+
pass
|
|
224
|
+
return False
|
|
225
|
+
filepath.write_text(compressed)
|
|
226
|
+
|
|
227
|
+
# Step 2: Validate + Retry
|
|
228
|
+
for attempt in range(MAX_RETRIES):
|
|
229
|
+
print(f"\nValidation attempt {attempt + 1}")
|
|
230
|
+
|
|
231
|
+
result = validate(backup_path, filepath)
|
|
232
|
+
|
|
233
|
+
if result.is_valid:
|
|
234
|
+
print("Validation passed")
|
|
235
|
+
break
|
|
236
|
+
|
|
237
|
+
print("❌ Validation failed:")
|
|
238
|
+
for err in result.errors:
|
|
239
|
+
print(f" - {err}")
|
|
240
|
+
|
|
241
|
+
if attempt == MAX_RETRIES - 1:
|
|
242
|
+
# Restore original on failure
|
|
243
|
+
filepath.write_text(original_text)
|
|
244
|
+
backup_path.unlink(missing_ok=True)
|
|
245
|
+
print("❌ Failed after retries — original restored")
|
|
246
|
+
return False
|
|
247
|
+
|
|
248
|
+
print("Fixing with Claude...")
|
|
249
|
+
compressed = call_claude(
|
|
250
|
+
build_fix_prompt(original_text, compressed, result.errors)
|
|
251
|
+
)
|
|
252
|
+
filepath.write_text(compressed)
|
|
253
|
+
|
|
254
|
+
return True
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Detect whether a file is natural language (compressible) or code/config (skip)."""
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
import re
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
# Extensions that are natural language and compressible
|
|
9
|
+
COMPRESSIBLE_EXTENSIONS = {".md", ".txt", ".markdown", ".rst", ".typ", ".typst", ".tex"}
|
|
10
|
+
|
|
11
|
+
# Extensions that are code/config and should be skipped
|
|
12
|
+
SKIP_EXTENSIONS = {
|
|
13
|
+
".py", ".js", ".ts", ".tsx", ".jsx", ".json", ".yaml", ".yml",
|
|
14
|
+
".toml", ".env", ".lock", ".css", ".scss", ".html", ".xml",
|
|
15
|
+
".sql", ".sh", ".bash", ".zsh", ".go", ".rs", ".java", ".c",
|
|
16
|
+
".cpp", ".h", ".hpp", ".rb", ".php", ".swift", ".kt", ".lua",
|
|
17
|
+
".dockerfile", ".makefile", ".csv", ".ini", ".cfg",
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
# Patterns that indicate a line is code
|
|
21
|
+
CODE_PATTERNS = [
|
|
22
|
+
re.compile(r"^\s*(import |from .+ import |require\(|const |let |var )"),
|
|
23
|
+
re.compile(r"^\s*(def |class |function |async function |export )"),
|
|
24
|
+
re.compile(r"^\s*(if\s*\(|for\s*\(|while\s*\(|switch\s*\(|try\s*\{)"),
|
|
25
|
+
re.compile(r"^\s*[\}\]\);]+\s*$"), # closing braces/brackets
|
|
26
|
+
re.compile(r"^\s*@\w+"), # decorators/annotations
|
|
27
|
+
re.compile(r'^\s*"[^"]+"\s*:\s*'), # JSON-like key-value
|
|
28
|
+
re.compile(r"^\s*\w+\s*=\s*[{\[\(\"']"), # assignment with literal
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _is_code_line(line: str) -> bool:
|
|
33
|
+
"""Check if a line looks like code."""
|
|
34
|
+
return any(p.match(line) for p in CODE_PATTERNS)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _is_json_content(text: str) -> bool:
|
|
38
|
+
"""Check if content is valid JSON."""
|
|
39
|
+
try:
|
|
40
|
+
json.loads(text)
|
|
41
|
+
return True
|
|
42
|
+
except (json.JSONDecodeError, ValueError):
|
|
43
|
+
return False
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _is_yaml_content(lines: list[str]) -> bool:
|
|
47
|
+
"""Heuristic: check if content looks like YAML."""
|
|
48
|
+
yaml_indicators = 0
|
|
49
|
+
for line in lines[:30]:
|
|
50
|
+
stripped = line.strip()
|
|
51
|
+
if stripped.startswith("---"):
|
|
52
|
+
yaml_indicators += 1
|
|
53
|
+
elif re.match(r"^\w[\w\s]*:\s", stripped):
|
|
54
|
+
yaml_indicators += 1
|
|
55
|
+
elif stripped.startswith("- ") and ":" in stripped:
|
|
56
|
+
yaml_indicators += 1
|
|
57
|
+
# If most non-empty lines look like YAML
|
|
58
|
+
non_empty = sum(1 for l in lines[:30] if l.strip())
|
|
59
|
+
return non_empty > 0 and yaml_indicators / non_empty > 0.6
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def detect_file_type(filepath: Path) -> str:
|
|
63
|
+
"""Classify a file as 'natural_language', 'code', 'config', or 'unknown'.
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
One of: 'natural_language', 'code', 'config', 'unknown'
|
|
67
|
+
"""
|
|
68
|
+
ext = filepath.suffix.lower()
|
|
69
|
+
|
|
70
|
+
# Extension-based classification
|
|
71
|
+
if ext in COMPRESSIBLE_EXTENSIONS:
|
|
72
|
+
return "natural_language"
|
|
73
|
+
if ext in SKIP_EXTENSIONS:
|
|
74
|
+
return "code" if ext not in {".json", ".yaml", ".yml", ".toml", ".ini", ".cfg", ".env"} else "config"
|
|
75
|
+
|
|
76
|
+
# Extensionless files (like CLAUDE.md, TODO) — check content
|
|
77
|
+
if not ext:
|
|
78
|
+
try:
|
|
79
|
+
text = filepath.read_text(errors="ignore")
|
|
80
|
+
except (OSError, PermissionError):
|
|
81
|
+
return "unknown"
|
|
82
|
+
|
|
83
|
+
lines = text.splitlines()[:50]
|
|
84
|
+
|
|
85
|
+
if _is_json_content(text[:10000]):
|
|
86
|
+
return "config"
|
|
87
|
+
if _is_yaml_content(lines):
|
|
88
|
+
return "config"
|
|
89
|
+
|
|
90
|
+
code_lines = sum(1 for l in lines if l.strip() and _is_code_line(l))
|
|
91
|
+
non_empty = sum(1 for l in lines if l.strip())
|
|
92
|
+
if non_empty > 0 and code_lines / non_empty > 0.4:
|
|
93
|
+
return "code"
|
|
94
|
+
|
|
95
|
+
return "natural_language"
|
|
96
|
+
|
|
97
|
+
return "unknown"
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def should_compress(filepath: Path) -> bool:
|
|
101
|
+
"""Return True if the file is natural language and should be compressed."""
|
|
102
|
+
if not filepath.is_file():
|
|
103
|
+
return False
|
|
104
|
+
# Skip backup files
|
|
105
|
+
if filepath.name.endswith(".original.md"):
|
|
106
|
+
return False
|
|
107
|
+
return detect_file_type(filepath) == "natural_language"
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
if __name__ == "__main__":
|
|
111
|
+
import sys
|
|
112
|
+
|
|
113
|
+
if len(sys.argv) < 2:
|
|
114
|
+
print("Usage: python detect.py <file1> [file2] ...")
|
|
115
|
+
sys.exit(1)
|
|
116
|
+
|
|
117
|
+
for path_str in sys.argv[1:]:
|
|
118
|
+
p = Path(path_str).resolve()
|
|
119
|
+
file_type = detect_file_type(p)
|
|
120
|
+
compress = should_compress(p)
|
|
121
|
+
print(f" {p.name:30s} type={file_type:20s} compress={compress}")
|