Regex/stdlib passes (dedupe, alias, ANSI/timestamp/blob strip, JSON minify, caveman word-drop) measured at 8.9-12.2x on real logs with o200k counts. Optional LLMLingua-2 wrap. grunts.py mines Claude Code transcripts for retyped prompts -> slash-command stubs. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
92 lines
3.8 KiB
Python
92 lines
3.8 KiB
Python
import subprocess
|
|
import sys
|
|
|
|
from lessismore import (compress, count_tokens, dedupe_lines, squash_blobs,
|
|
strip_filler, strip_timestamps, two_sticks)
|
|
|
|
|
|
def test():
|
|
# dedupe: long-line runs collapse, short runs where the marker wouldn't pay stay
|
|
line = "ERROR connection refused by upstream database"
|
|
assert dedupe_lines((line + "\n") * 5 + "b") == \
|
|
line + "\n[previous line repeated 4 more times]\nb"
|
|
assert dedupe_lines("a\n" * 5 + "b") == "a\n" * 5 + "b"
|
|
assert dedupe_lines(line + "\n" + line + "\nb") == line + "\n" + line + "\nb"
|
|
|
|
# blobs squash with head+tail stub; URLs are not blobs
|
|
assert "[+84 chars]" in squash_blobs("token=" + "A" * 100)
|
|
url = ("GET https://api.dealgod.pro/api/v1/users/12345/orders/67890/"
|
|
"items/status/pending/verbose HTTP/1.1")
|
|
assert squash_blobs(url) == url
|
|
|
|
# distinct blobs keep distinct stubs — no false "repeated" merge at level 2
|
|
blobs = "\n".join("stored blob deadbeefcafe" + f"{i:052x}" for i in range(4))
|
|
assert "repeated" not in compress(blobs, 2)
|
|
|
|
# timestamps
|
|
assert strip_timestamps("2026-07-07T10:00:00.123Z boom") == "boom"
|
|
|
|
# filler stripping never inverts negation
|
|
assert strip_filler("This is not just a warning.") == "This is not just a warning."
|
|
assert strip_filler("just do it") == "do it"
|
|
|
|
# level 1 is code-safe: indentation and content untouched
|
|
code = "def f(x):\n return x + 1\n"
|
|
assert compress(code, 1) == code
|
|
|
|
# two_sticks drops function words, never negations, and spares acronyms
|
|
assert two_sticks("The server is down") == "server down"
|
|
assert "not" in two_sticks("do not delete the backup")
|
|
assert two_sticks("The IT team said it is a US issue") == "IT team said US issue"
|
|
|
|
# interleaved repeats defeat consecutive dedupe but get dictionary-aliased
|
|
inter = ("db connection refused on host alpha-primary\n"
|
|
"redis queue depth exceeded on host beta-cache\n") * 30
|
|
out = compress(inter, 2)
|
|
assert "[repeated lines aliased:]" in out and "@1" in out
|
|
assert count_tokens(out) < count_tokens(inter) / 2
|
|
|
|
# a repetitive log shrinks hard, and stripping leaves no doubled spaces
|
|
log = "2026-07-07T10:00:01Z ERROR conn refused db=dealgod retry\n" * 500
|
|
out = compress(log, 2)
|
|
assert count_tokens(out) < count_tokens(log) / 50
|
|
assert " " not in out
|
|
|
|
# \r progress redraws keep only the final state
|
|
assert compress("downloading 1%\rdownloading 99%\rdone\n", 2) == "done\n"
|
|
|
|
# ANSI escapes stripped
|
|
assert compress("\x1b[32mok\x1b[0m\n", 2) == "ok\n"
|
|
|
|
# venv path spam squashed, file:line still unique
|
|
tb = 'File "/Users/j/venv/lib/python3.14/site-packages/httpx/_client.py", line 5\n'
|
|
assert compress(tb, 2) == 'File "…/httpx/_client.py", line 5\n'
|
|
|
|
# uuids shorten to 8-hex prefix
|
|
assert compress("id=550e8400-e29b-41d4-a716-446655440000 done\n", 2) == "id=550e8400… done\n"
|
|
|
|
# same-words-different-numbers runs collapse, endpoints kept
|
|
sim = "\n".join(f"Downloading chunk {i} of 50 at {i * 3}kbps" for i in range(50))
|
|
out = compress(sim, 2)
|
|
assert "[48 similar lines omitted]" in out
|
|
assert "chunk 0 of" in out and "chunk 49 of" in out
|
|
|
|
# whole-input pretty JSON minifies losslessly
|
|
import json
|
|
pretty = json.dumps({"items": [{"id": i, "name": f"x{i}"} for i in range(10)]}, indent=2)
|
|
out = compress(pretty, 2)
|
|
assert json.loads(out) == json.loads(pretty)
|
|
assert len(out) < len(pretty) / 1.5
|
|
|
|
# CLI survives empty and non-UTF-8 stdin
|
|
for stdin in (b"", b"ok\n\xff\xfe\n"):
|
|
r = subprocess.run([sys.executable, "lessismore.py"],
|
|
input=stdin, capture_output=True)
|
|
assert r.returncode == 0, r.stderr
|
|
|
|
print("ok")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test()
|