Keep formats.py to Claude Code's shapes, with paths and file reads at their callers
This commit is contained in:
@@ -1,26 +1,9 @@
|
||||
"""Every Claude Code format loop-claude reads, each under a sample of it, as Claude Code 2.1.284 writes it."""
|
||||
import glob
|
||||
"""Every Claude Code shape loop-claude reads, each under a sample of it, as Claude Code 2.1.284 writes it."""
|
||||
import json
|
||||
import os
|
||||
|
||||
from loop_claude.outcome import Failure
|
||||
|
||||
|
||||
def locations(home):
|
||||
return home / ".claude.json", home / ".claude" / "cache" / "model-catalog", home / ".claude" / "projects"
|
||||
|
||||
|
||||
# <claude_code_transcripts>/-home-me-code-log/<session id>.jsonl
|
||||
def transcript_path(claude_code_transcripts, session_id):
|
||||
hit = glob.glob(os.path.join(glob.escape(str(claude_code_transcripts)), "*", f"{session_id}.jsonl"))
|
||||
return hit[0] if hit else ""
|
||||
|
||||
|
||||
# <session id>.jsonl, <session id>/subagents/, and from older versions <session id>.superseded-1789290815002/ and <session id>.orphaned-*.jsonl
|
||||
def session_files(transcript):
|
||||
return glob.glob(glob.escape(transcript.removesuffix(".jsonl")) + "*")
|
||||
|
||||
|
||||
def parse(raw):
|
||||
try:
|
||||
entry = json.loads(raw)
|
||||
@@ -114,14 +97,10 @@ def mark_trusted(config, roots):
|
||||
|
||||
|
||||
# <model catalog>/*.json: {"fetchedAt": 1790618085238, "catalog": {"config": {"models": [{"id": "claude-opus-5-5", "name": "Opus 5.5", ...}]}}}
|
||||
def catalog(folder):
|
||||
def catalog(files):
|
||||
"""The (id, name) of each model in the newest of the parsed catalog files."""
|
||||
newest = {}
|
||||
for path in folder.glob("*.json"):
|
||||
try:
|
||||
with open(path, encoding="utf-8") as handle:
|
||||
data = json.load(handle)
|
||||
except (OSError, ValueError):
|
||||
continue
|
||||
for data in files:
|
||||
if isinstance(data, dict) and isinstance(data.get("fetchedAt"), (int, float)) and data["fetchedAt"] > newest.get("fetchedAt", -1):
|
||||
newest = data
|
||||
models = newest
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import dataclasses
|
||||
import glob
|
||||
import os
|
||||
import pathlib
|
||||
import shutil
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
from loop_claude.claude_code.formats import session_files, transcript_path
|
||||
|
||||
NAMESPACE = uuid.UUID("64fb586b-d926-4bb3-a44c-c903e86d3eec")
|
||||
|
||||
|
||||
@@ -24,15 +23,18 @@ def session_for(project, paths):
|
||||
return Session(id=uuid.uuid5(NAMESPACE, name), claude_code_transcripts=paths.claude_code_transcripts, name=name)
|
||||
|
||||
|
||||
# <claude_code_transcripts>/-home-me-code-log/<session id>.jsonl
|
||||
def transcript_file(session):
|
||||
return transcript_path(session.claude_code_transcripts, session.id)
|
||||
hit = glob.glob(os.path.join(glob.escape(str(session.claude_code_transcripts)), "*", f"{session.id}.jsonl"))
|
||||
return hit[0] if hit else ""
|
||||
|
||||
|
||||
def delete_session(session):
|
||||
path = transcript_file(session)
|
||||
if not path:
|
||||
return
|
||||
for sibling in session_files(path):
|
||||
# <session id>.jsonl, <session id>/subagents/, and from older versions <session id>.superseded-1789290815002/ and <session id>.orphaned-*.jsonl
|
||||
for sibling in glob.glob(glob.escape(path.removesuffix(".jsonl")) + "*"):
|
||||
try:
|
||||
shutil.rmtree(sibling) if os.path.isdir(sibling) else os.remove(sibling)
|
||||
except OSError as failure:
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import dataclasses
|
||||
import pathlib
|
||||
|
||||
from loop_claude.claude_code.formats import locations
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Paths:
|
||||
@@ -16,12 +14,11 @@ class Paths:
|
||||
|
||||
def paths_from(home, env):
|
||||
claude = home / ".claude"
|
||||
claude_json, model_catalog, claude_code_transcripts = locations(home)
|
||||
return Paths(
|
||||
archive=pathlib.Path(env["LOOP_CLAUDE_TRANSCRIPTS"]).expanduser() if env.get("LOOP_CLAUDE_TRANSCRIPTS") else claude / "loop-claude-transcripts",
|
||||
claude_code_transcripts=claude_code_transcripts,
|
||||
claude_json=claude_json,
|
||||
claude_code_transcripts=claude / "projects",
|
||||
claude_json=home / ".claude.json",
|
||||
code=home / "code",
|
||||
model_catalog=model_catalog,
|
||||
model_catalog=claude / "cache" / "model-catalog",
|
||||
models_file=claude / "loop-claude-models.json",
|
||||
)
|
||||
|
||||
@@ -57,10 +57,19 @@ def read_models(saved):
|
||||
return models
|
||||
|
||||
|
||||
def read_catalog(folder):
|
||||
for path in folder.glob("*.json"):
|
||||
try:
|
||||
with open(path, encoding="utf-8") as handle:
|
||||
yield json.load(handle)
|
||||
except (OSError, ValueError):
|
||||
continue
|
||||
|
||||
|
||||
def ask_model(name, saved, folder):
|
||||
if stop := human_needed.off_terminal(f"pick {name}'s model, or merge {{\"{name}\": \"<model>\"}} into {saved}"):
|
||||
return stop
|
||||
offered = catalog(folder)
|
||||
offered = catalog(read_catalog(folder))
|
||||
print(f"\nThe model to run {name} on, saved in {saved}:", file=sys.stderr)
|
||||
for number, (model_id, label) in enumerate(offered, 1):
|
||||
print(f" {number}. {label} ({model_id})", file=sys.stderr)
|
||||
|
||||
@@ -8,7 +8,6 @@ The rebuild lifts every dimension, one chunk per item, until the last item opens
|
||||
|
||||
The places the panel on `rebuild` at 2f9ab2f named (`docs/decisions.md`), capping dimension first:
|
||||
|
||||
- **Keep `formats.py` to Claude Code's shapes: `locations` into `paths.py`, `catalog`'s file reads out.** Three seats found paths and I/O in a file named for formats.
|
||||
- **Group `bin/loop_claude/`'s top-level modules, and name `claude_runs` apart from `claude_code`.** Two seats: eleven modules of mixed altitude side by side.
|
||||
- **Say at `written_by_human` why a `<` prefix marks Claude Code's own entry, and at `trust` why a failure only warns.** Three seats found either only in `CHANGELOG.md`.
|
||||
- **Say in the README why the tests run on the host and need `uv`.** Two seats: `CLAUDE.md` says never run a runtime on the host, and only `test_python_floor.py` says what `uv` is for.
|
||||
|
||||
Reference in New Issue
Block a user