Merge remote-tracking branch 'origin/main' into item-581
CI / tests (pull_request) Has been cancelled
CI / tests (pull_request) Has been cancelled
# Conflicts: # CHANGELOG.md # README.md
This commit is contained in:
+18
-14
@@ -17,7 +17,9 @@ from text_files.file_writes import replace_file
|
||||
LINKED = ("CLAUDE.md", "agents", "bin", "principles", "rules", "skills")
|
||||
# Stands for the clone's folder in the clone's settings.json. Every value that holds it is a shell command, such as a hook's.
|
||||
CLONE = "{clone}"
|
||||
# The file in the home that names the clone and the commit the home was built from.
|
||||
# Stands for the reports folder, which holds every chunk's review folder, in the clone's settings.json. Every value that holds it is a path, so unlike CLONE it isn't shell-quoted.
|
||||
REPORTS = "{reports}"
|
||||
# The file in the home that names the clone, its commit and the reports folder the home was built from.
|
||||
BUILT_FROM = "built-from"
|
||||
|
||||
# Runs `claude` with these arguments in the home.
|
||||
@@ -28,10 +30,10 @@ class Git(typing.Protocol):
|
||||
def __call__(self, *args: str, cwd: str | None = None) -> str | Failure: ...
|
||||
|
||||
|
||||
def unready(home: pathlib.Path, clone: pathlib.Path, claude: Claude, git: Git) -> str | None:
|
||||
"""Builds the home from clone, or refreshes it where the clone's commit changed since. Then why a claude cannot run in the home yet, said for the person to act on; None where one can."""
|
||||
def unready(home: pathlib.Path, clone: pathlib.Path, reports: pathlib.Path, claude: Claude, git: Git) -> str | None:
|
||||
"""Builds the home from clone, or refreshes it where the clone's commit or the reports folder changed since. Then why a claude cannot run in the home yet, said for the person to act on; None where one can."""
|
||||
try:
|
||||
built = refreshed(home, clone, claude, git)
|
||||
built = refreshed(home, clone, reports, claude, git)
|
||||
if isinstance(built, Failure):
|
||||
return f"could not build the loop's Claude home, {home}: {built.reason}"
|
||||
status = claude(["auth", "status"])
|
||||
@@ -61,11 +63,11 @@ def printed(done: subprocess.CompletedProcess[str]) -> str:
|
||||
return " ".join((done.stderr or done.stdout).split()) or f"it printed nothing and exited {done.returncode}"
|
||||
|
||||
|
||||
def refreshed(home: pathlib.Path, clone: pathlib.Path, claude: Claude, git: Git) -> Failure | None:
|
||||
def refreshed(home: pathlib.Path, clone: pathlib.Path, reports: pathlib.Path, claude: Claude, git: Git) -> Failure | None:
|
||||
commit = git("rev-parse", "HEAD", cwd=str(clone))
|
||||
if isinstance(commit, Failure):
|
||||
return commit
|
||||
built_from = f"{clone} at {commit.strip()}\n"
|
||||
built_from = f"{clone} at {commit.strip()}, reports in {reports}\n"
|
||||
home.mkdir(parents=True, exist_ok=True)
|
||||
# Several loops start at once after an update: one refreshes the home, and the others wait and find it done.
|
||||
with open(home / ".refresh-lock", "w", encoding="utf-8") as lock:
|
||||
@@ -74,7 +76,7 @@ def refreshed(home: pathlib.Path, clone: pathlib.Path, claude: Claude, git: Git)
|
||||
if stamp.is_file() and stamp.read_text(encoding="utf-8") == built_from:
|
||||
return None
|
||||
# The stamp is written last, so a refresh that failed halfway runs again.
|
||||
return linked(home, clone) or settings_written(home, clone) or plugins_installed(clone, claude) or replace_file(stamp, built_from.encode())
|
||||
return linked(home, clone) or settings_written(home, clone, reports) or plugins_installed(clone, claude) or replace_file(stamp, built_from.encode())
|
||||
|
||||
|
||||
def linked(home: pathlib.Path, clone: pathlib.Path) -> Failure | None:
|
||||
@@ -87,9 +89,9 @@ def linked(home: pathlib.Path, clone: pathlib.Path) -> Failure | None:
|
||||
return None
|
||||
|
||||
|
||||
def settings_written(home: pathlib.Path, clone: pathlib.Path) -> Failure | None:
|
||||
"""Writes the home's settings.json: the clone's, with the clone's folder in place of CLONE, and the model the home's file holds."""
|
||||
settings = placed(json.loads((clone / "settings.json").read_text(encoding="utf-8")), shlex.quote(str(clone)))
|
||||
def settings_written(home: pathlib.Path, clone: pathlib.Path, reports: pathlib.Path) -> Failure | None:
|
||||
"""Writes the home's settings.json: the clone's, with the clone's folder in place of CLONE and the reports folder in place of REPORTS, and the model the home's file holds."""
|
||||
settings = placed(json.loads((clone / "settings.json").read_text(encoding="utf-8")), {CLONE: shlex.quote(str(clone)), REPORTS: str(reports)})
|
||||
generated = home / "settings.json"
|
||||
try:
|
||||
old = json.loads(generated.read_text(encoding="utf-8")) if generated.is_file() else {}
|
||||
@@ -103,13 +105,15 @@ def settings_written(home: pathlib.Path, clone: pathlib.Path) -> Failure | None:
|
||||
return replace_file(generated, (json.dumps(settings, indent=2, sort_keys=True) + "\n").encode())
|
||||
|
||||
|
||||
def placed(value: typing.Any, clone: str) -> typing.Any:
|
||||
def placed(value: typing.Any, stand_for: dict[str, str]) -> typing.Any:
|
||||
if isinstance(value, str):
|
||||
return value.replace(CLONE, clone)
|
||||
for name, replacement in stand_for.items():
|
||||
value = value.replace(name, replacement)
|
||||
return value
|
||||
if isinstance(value, list):
|
||||
return [placed(each, clone) for each in value]
|
||||
return [placed(each, stand_for) for each in value]
|
||||
if isinstance(value, dict):
|
||||
return {key: placed(each, clone) for key, each in value.items()}
|
||||
return {key: placed(each, stand_for) for key, each in value.items()}
|
||||
return value
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user