Skill: x3r081/agent-memory-optimizer (version 1.0.0)

--- name: agent-memory-optimizer slug: agent-memory-optimizer displayName: Agent Memory Optimizer description: >- Archives older daily memory notes into month folders to keep active memory lean and reduce prompt token usage. Use when users want lower token overhead, cleaner memory directories, and low-maintenance memory operations. version: 1.0.0 metadata: openclaw: requires: bins: - python3 homepage: https://docs.openclaw.ai/tools/skills --- # Agent Memory Optimizer Optimize active memory context by archiving old daily notes into month-based folders. ## What it does - Scans `memory/*.md` - Moves files older than the current month into `memory/archive/YYYY-MM/` - Supports `--dry-run` to preview changes - Safe to run repeatedly ## Run ```bash python3 scripts/agent_memory_optimizer.py --dry-run python3 scripts/agent_memory_optimizer.py ``` Optional custom cutoff: ```bash python3 scripts/agent_memory_optimizer.py --before 2026-05 ``` ## Installation Install into your workspace skills directory: ```text <workspace>/skills/agent-memory-optimizer/ ``` Or install from ClawHub after publishing.

path: README.md
content: # Agent Memory Optimizer

Archive old daily memory files into month folders so active memory remains small, fast to search, and cheaper in prompt tokens.

## Install

Copy this folder into your OpenClaw workspace skills:

```text
<workspace>/skills/agent-memory-optimizer/
```

OpenClaw loads workspace skills on the next session.

## Usage

From the skill folder:

```bash
python3 scripts/agent_memory_optimizer.py --dry-run
python3 scripts/agent_memory_optimizer.py
```

## Behavior

- Source: `~/.openclaw/workspace/memory/*.md` (default)
- Target: `~/.openclaw/workspace/memory/archive/YYYY-MM/`
- Default cutoff: archive months older than current month
- Idempotent: reruns do not duplicate files

## Options

```text
--memory-dir PATH   Memory directory (default: ~/.openclaw/workspace/memory)
--before YYYY-MM    Archive months older than this month
--dry-run           Preview file moves without changes
```

## Data and privacy

- No API keys required
- No network calls
- No personal data hardcoded
- Operates only on local files you point it at

## License

MIT-0


path: scripts/agent_memory_optimizer.py
content: #!/usr/bin/env python3
"""Archive old daily memory files into month folders.

Default behavior:
- Move memory files older than the current month.
- Source:   <workspace>/memory/*.md
- Target:   <workspace>/memory/archive/YYYY-MM/<filename>
"""

from __future__ import annotations

import argparse
import datetime as dt
import json
import re
import shutil
from pathlib import Path

DATE_PREFIX = re.compile(r"^(\d{4})-(\d{2})(?:-\d{2})?.*\.md$")


def parse_month(value: str) -> tuple[int, int]:
    try:
        year_str, month_str = value.split("-", 1)
        year = int(year_str)
        month = int(month_str)
    except ValueError as exc:
        raise argparse.ArgumentTypeError("Expected YYYY-MM format.") from exc
    if not (1 <= month <= 12):
        raise argparse.ArgumentTypeError("Month must be 01..12.")
    return year, month


def iter_candidates(memory_dir: Path, cutoff: tuple[int, int]) -> list[tuple[Path, str]]:
    cutoff_key = cutoff[0] * 100 + cutoff[1]
    candidates: list[tuple[Path, str]] = []
    for path in sorted(memory_dir.glob("*.md")):
        match = DATE_PREFIX.match(path.name)
        if not match:
            continue
        year = int(match.group(1))
        month = int(match.group(2))
        file_key = year * 100 + month
        if file_key >= cutoff_key:
            continue
        month_folder = f"{year:04d}-{month:02d}"
        candidates.append((path, month_folder))
    return candidates


def archive_files(memory_dir: Path, dry_run: bool, before: tuple[int, int]) -> dict:
    archive_root = memory_dir / "archive"
    candidates = iter_candidates(memory_dir, before)
    moved: list[dict] = []

    for src, month_folder in candidates:
        dst_dir = archive_root / month_folder
        dst = dst_dir / src.name
        moved.append({"from": str(src), "to": str(dst)})
        if dry_run:
            continue
        dst_dir.mkdir(parents=True, exist_ok=True)
        if dst.exists():
            dst.unlink()
        shutil.move(str(src), str(dst))

    return {
        "memory_dir": str(memory_dir),
        "archive_root": str(archive_root),
        "before": f"{before[0]:04d}-{before[1]:02d}",
        "dry_run": dry_run,
        "candidate_count": len(candidates),
        "moved": moved,
    }


def main() -> int:
    parser = argparse.ArgumentParser(description="Archive old memory files by month.")
    parser.add_argument(
        "--memory-dir",
        default="~/.openclaw/workspace/memory",
        help="Path to memory directory (default: ~/.openclaw/workspace/memory)",
    )
    parser.add_argument(
        "--before",
        type=parse_month,
        help="Archive months older than this YYYY-MM. Default: current month.",
    )
    parser.add_argument(
        "--dry-run",
        action="store_true",
        help="Preview moves without changing files.",
    )
    args = parser.parse_args()

    memory_dir = Path(args.memory_dir).expanduser().resolve()
    if not memory_dir.exists():
        raise SystemExit(f"Memory directory does not exist: {memory_dir}")
    if not memory_dir.is_dir():
        raise SystemExit(f"Memory path is not a directory: {memory_dir}")
    now = dt.date.today()
    before = args.before or (now.year, now.month)

    result = archive_files(memory_dir=memory_dir, dry_run=args.dry_run, before=before)
    print(json.dumps(result, indent=2))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())


Security scan verdicts — ClawScan: clean | static analysis: clean | VirusTotal: clean | SkillSpector: clean (LOW)