Gzip loop-claude's archive and the review folders 30 days after each file was last written, and read gzipped files by their plain name
CI / tests (pull_request) Failing after 2m59s
CI / tests (pull_request) Failing after 2m59s
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
"""Compress every file of loop-claude's archive and the review folders that no tool writes to any more, keeping each one.
|
||||
|
||||
usage: compress-archives
|
||||
|
||||
It gzips each file last written over 30 days ago, beside its plain name as <name>.gz with the same
|
||||
times: in loop-claude's archive, $LOOP_CLAUDE_TRANSCRIPTS (default ~/.claude/loop-claude-transcripts),
|
||||
each transcript (*.jsonl); in the review folders, ~/.local/state/larv-review, every file. It writes
|
||||
the gzip, reads it back, and removes the plain file only once the gzip reads back its bytes. Every
|
||||
tool that reads these folders reads a gzipped file by its plain name. Install's crontab line runs it
|
||||
daily.
|
||||
|
||||
It prints how many files it compressed. Exit 1, saying why, at the first file it cannot compress,
|
||||
which it keeps plain with every file after it, or on any argument but --help.
|
||||
"""
|
||||
import os
|
||||
import pathlib
|
||||
import sys
|
||||
import time
|
||||
import typing
|
||||
|
||||
from failure import Failure
|
||||
from review_rules.review_folder import REPORTS
|
||||
from text_files.compressed import compress, SUFFIX
|
||||
from transcript_archive.location import archive_root
|
||||
|
||||
# Why 30 days: docs/decisions.md → `archives: loop-claude's archive and the review folders keep every file, and gzip each one 30 days after it was last written`.
|
||||
AGE_DAYS = 30
|
||||
|
||||
|
||||
def main(argv: list[str], env: typing.Mapping[str, str] = os.environ, home: pathlib.Path | None = None, now: float | None = None) -> int:
|
||||
if argv in (["-h"], ["--help"]):
|
||||
print(__doc__.strip())
|
||||
return 0
|
||||
if argv:
|
||||
print(f"unknown arguments {' '.join(argv)}; usage: compress-archives", file=sys.stderr)
|
||||
return 1
|
||||
home = home or pathlib.Path.home()
|
||||
before = (time.time() if now is None else now) - AGE_DAYS * 24 * 60 * 60
|
||||
count = 0
|
||||
for path in [*old_files(archive_root(home, env), "*.jsonl", before), *old_files(home / REPORTS, "*", before)]:
|
||||
if failed := compress(path):
|
||||
print(failed.reason, file=sys.stderr)
|
||||
return 1
|
||||
count += 1
|
||||
print(f"Compressed {count} files.")
|
||||
return 0
|
||||
|
||||
|
||||
def old_files(root: pathlib.Path, pattern: str, before: float) -> list[pathlib.Path]:
|
||||
"""Each plain file under root that pattern matches, last written before the time before, sorted; a link is left alone, since its target lies elsewhere."""
|
||||
found = []
|
||||
for path in root.rglob(pattern):
|
||||
try:
|
||||
if not path.name.endswith(SUFFIX) and path.is_file() and not path.is_symlink() and path.stat().st_mtime < before:
|
||||
found.append(path)
|
||||
# Removed while the walk ran, such as a file of review-replay's copy of a review folder.
|
||||
except FileNotFoundError:
|
||||
continue
|
||||
return sorted(found)
|
||||
@@ -0,0 +1,94 @@
|
||||
import contextlib
|
||||
import gzip
|
||||
import io
|
||||
import os
|
||||
import pathlib
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from compress_archives import main
|
||||
from failure import Failure
|
||||
|
||||
NOW = 1790000000
|
||||
DAY = 24 * 60 * 60
|
||||
|
||||
|
||||
class Main(unittest.TestCase):
|
||||
"""A home whose archive and review folders each hold a file last written 31 days ago and one written 29 days ago."""
|
||||
|
||||
def setUp(self):
|
||||
self.home = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory()))
|
||||
self.archive = self.home / ".claude" / "loop-claude-transcripts" / "proj" / "c1"
|
||||
self.reviews = self.home / ".local" / "state" / "larv-review" / "code" / "proj-item-1"
|
||||
for folder in (self.archive / "session" / "s" / "subagents", self.reviews / "probe"):
|
||||
folder.mkdir(parents=True)
|
||||
self.write(self.archive / "a.jsonl", 31)
|
||||
self.write(self.archive / "a.outcomes.md", 31)
|
||||
self.write(self.archive / "session" / "s" / "subagents" / "agent-a.jsonl", 31)
|
||||
self.write(self.archive / "session" / "s" / "subagents" / "agent-a.meta.json", 31)
|
||||
self.write(self.archive / "b.jsonl", 29)
|
||||
self.write(self.reviews / "stability-round-1.md", 31)
|
||||
self.write(self.reviews / "probe" / "out.js", 31)
|
||||
self.write(self.reviews / "branch-1.diff", 29)
|
||||
|
||||
def write(self, path, days_ago):
|
||||
path.write_bytes(f"{path.name}\n".encode())
|
||||
os.utime(path, (NOW - days_ago * DAY, NOW - days_ago * DAY))
|
||||
|
||||
def run_main(self, argv=(), env=None):
|
||||
out, err = io.StringIO(), io.StringIO()
|
||||
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
|
||||
code = main.main(list(argv), env or {}, self.home, NOW)
|
||||
return code, out.getvalue(), err.getvalue()
|
||||
|
||||
def kept(self):
|
||||
return sorted(str(path.relative_to(self.home)) for path in self.home.rglob("*") if path.is_file())
|
||||
|
||||
def test_gzips_the_archives_transcripts_and_every_review_file_last_written_over_30_days_ago(self):
|
||||
code, out, err = self.run_main()
|
||||
self.assertEqual((code, err), (0, ""))
|
||||
self.assertEqual(self.kept(), [
|
||||
".claude/loop-claude-transcripts/proj/c1/a.jsonl.gz",
|
||||
".claude/loop-claude-transcripts/proj/c1/a.outcomes.md",
|
||||
".claude/loop-claude-transcripts/proj/c1/b.jsonl",
|
||||
".claude/loop-claude-transcripts/proj/c1/session/s/subagents/agent-a.jsonl.gz",
|
||||
".claude/loop-claude-transcripts/proj/c1/session/s/subagents/agent-a.meta.json",
|
||||
".local/state/larv-review/code/proj-item-1/branch-1.diff",
|
||||
".local/state/larv-review/code/proj-item-1/probe/out.js.gz",
|
||||
".local/state/larv-review/code/proj-item-1/stability-round-1.md.gz",
|
||||
])
|
||||
self.assertEqual(gzip.decompress((self.archive / "a.jsonl.gz").read_bytes()), b"a.jsonl\n")
|
||||
self.assertEqual(out, "Compressed 4 files.\n")
|
||||
self.assertEqual(self.run_main(), (0, "Compressed 0 files.\n", ""))
|
||||
|
||||
def test_reads_the_archive_where_loop_claude_transcripts_puts_it(self):
|
||||
moved = self.home / "elsewhere"
|
||||
(self.home / ".claude" / "loop-claude-transcripts").rename(moved)
|
||||
code, _, err = self.run_main(env={"LOOP_CLAUDE_TRANSCRIPTS": str(moved)})
|
||||
self.assertEqual((code, err), (0, ""))
|
||||
self.assertTrue((moved / "proj" / "c1" / "a.jsonl.gz").is_file())
|
||||
|
||||
def test_runs_where_neither_folder_exists_yet(self):
|
||||
with tempfile.TemporaryDirectory() as empty:
|
||||
self.home = pathlib.Path(empty)
|
||||
self.assertEqual(self.run_main(), (0, "Compressed 0 files.\n", ""))
|
||||
|
||||
def test_stops_at_the_first_failure_keeping_that_file_and_every_later_one(self):
|
||||
with mock.patch.object(main, "compress", return_value=Failure("could not compress x: full")):
|
||||
code, out, err = self.run_main()
|
||||
self.assertEqual((code, out, err), (1, "", "could not compress x: full\n"))
|
||||
self.assertTrue((self.archive / "a.jsonl").is_file())
|
||||
|
||||
def test_prints_its_help_and_refuses_other_arguments(self):
|
||||
code, out, _ = self.run_main(["--help"])
|
||||
self.assertEqual(code, 0)
|
||||
self.assertTrue(out.startswith("Compress every file"), out)
|
||||
self.assertEqual(self.run_main(["x"])[0], 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user