import argparse
import json
import subprocess
from pathlib import Path
from peoplebot import ContextPolicy, StateRef, assemble_context
parser = argparse.ArgumentParser()
parser.add_argument("--limit", type=int, default=128)
args = parser.parse_args()
if args.limit not in (128, 4096):
parser.error("For this tutorial, choose --limit 128 or --limit 4096.")
project = Path("practice-project")
if not (project / ".git").exists():
raise SystemExit("Complete lesson 2 first; practice-project must be here.")
def git(*arguments):
return subprocess.run(
["git", "-C", str(project), *arguments],
check=True,
capture_output=True,
text=True,
timeout=15,
).stdout.strip()
policy_path = "tutorial-context-policy.json"
policy_data = {
"format": "peoplebot.context-policy.v0",
"max_entries": 4,
"max_blob_bytes": args.limit,
"max_total_blob_bytes": 8192,
"exclusions": [],
}
(project / "long-note.txt").write_text("x" * 2000, encoding="utf-8")
(project / policy_path).write_text(
json.dumps(policy_data) + "\n", encoding="utf-8"
)
git("add", "long-note.txt", policy_path)
if git("diff", "--cached", "--name-only", "--", "long-note.txt", policy_path):
git(
"-c", "user.name=PeopleBot Tutorial",
"-c", "user.email=tutorial@example.invalid",
"commit", "-m", f"Set tutorial file limit to {args.limit}",
"--", "long-note.txt", policy_path,
)
commit = git("rev-parse", "HEAD")
repository = "local:peoplebot-practice"
policy = ContextPolicy.from_dict(
StateRef(repository, commit, policy_path), policy_data
)
requested = ("welcome.txt", "long-note.txt")
assembly = assemble_context(
project, StateRef(repository, commit), requested, policy
)
included = {document.source.path for document in assembly.documents}
print(f"Per-file limit: {args.limit} bytes")
for path in requested:
print(f"{path}: {'INCLUDED' if path in included else 'LEFT OUT'}")