crumb-spec
Crumb Protocol specification and filesystem standard for sovereign AI agents
Brought by drakestapleton
crumb-spec / examples / reference-clients / drop.py
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Zero-dependency reference writer to plant scents, whispers, and action vectors into .crumb.local. |
| 4 | Usage: |
| 5 | python3 drop.py whisper "Refactoring in progress" --from CoderAgent |
| 6 | python3 drop.py action modify src/lib.rs "Fix timeout" --vector resilience |
| 7 | """ |
| 8 | |
| 9 | import argparse |
| 10 | import json |
| 11 | import os |
| 12 | from datetime import datetime, timezone |
| 13 | |
| 14 | def load_or_create_local(directory): |
| 15 | local_path = os.path.join(directory, ".crumb.local") |
| 16 | if os.path.exists(local_path): |
| 17 | try: |
| 18 | with open(local_path, "r", encoding="utf-8") as f: |
| 19 | return json.load(f), local_path |
| 20 | except Exception: |
| 21 | pass |
| 22 | return { |
| 23 | "schema_version": "1.0.0", |
| 24 | "directory": os.path.relpath(directory), |
| 25 | "active_scents": {}, |
| 26 | "locks": {}, |
| 27 | "whispers": [], |
| 28 | "history": [] |
| 29 | }, local_path |
| 30 | |
| 31 | def save_local(data, local_path): |
| 32 | with open(local_path, "w", encoding="utf-8") as f: |
| 33 | json.dump(data, f, indent=2) |
| 34 | |
| 35 | def main(): |
| 36 | parser = argparse.ArgumentParser(description="Plant scents or whispers into .crumb.local") |
| 37 | subparsers = parser.add_subparsers(dest="command") |
| 38 | |
| 39 | w_parser = subparsers.add_parser("whisper") |
| 40 | w_parser.add_argument("message", help="Whisper message") |
| 41 | w_parser.add_argument("--from-agent", default="AutonomousAgent", help="Agent identifier") |
| 42 | w_parser.add_argument("--dir", default=".", help="Target directory") |
| 43 | w_parser.add_argument("--target-file", help="Optional target file") |
| 44 | |
| 45 | a_parser = subparsers.add_parser("action") |
| 46 | a_parser.add_argument("action", help="Action type (modify, create, delete, test)") |
| 47 | a_parser.add_argument("target", help="Target file name") |
| 48 | a_parser.add_argument("intent", help="Intent description") |
| 49 | a_parser.add_argument("--vector", default="general", help="Vector description") |
| 50 | a_parser.add_argument("--agent", default="AutonomousAgent", help="Agent identifier") |
| 51 | a_parser.add_argument("--dir", default=".", help="Target directory") |
| 52 | |
| 53 | args = parser.parse_args() |
| 54 | now_iso = datetime.now(timezone.utc).isoformat() |
| 55 | |
| 56 | if args.command == "whisper": |
| 57 | data, path = load_or_create_local(args.dir) |
| 58 | whisper_entry = { |
| 59 | "from": args.from_agent, |
| 60 | "message": args.message, |
| 61 | "timestamp": now_iso |
| 62 | } |
| 63 | if args.target_file: |
| 64 | whisper_entry["target_file"] = args.target_file |
| 65 | data.setdefault("whispers", []).append(whisper_entry) |
| 66 | save_local(data, path) |
| 67 | print(f"Planted whisper in {path}") |
| 68 | |
| 69 | elif args.command == "action": |
| 70 | data, path = load_or_create_local(args.dir) |
| 71 | action_entry = { |
| 72 | "agent": args.agent, |
| 73 | "action": args.action, |
| 74 | "target": args.target, |
| 75 | "intent": args.intent, |
| 76 | "vector": args.vector, |
| 77 | "timestamp": now_iso |
| 78 | } |
| 79 | data.setdefault("history", []).append(action_entry) |
| 80 | if len(data["history"]) > 20: |
| 81 | data["history"] = data["history"][-20:] |
| 82 | save_local(data, path) |
| 83 | print(f"Recorded action vector in {path}") |
| 84 | |
| 85 | if __name__ == "__main__": |
| 86 | main() |