Skip to content

"CLI-Anything: Making ALL Software Agent-Native" -- CLI-Hub: https://clianything.cc/

Python

fix: address PR review feedback

Vicky Tamb9698b31699c
diff --git a/musescore/agent-harness/cli_anything/musescore/core/instruments.py b/musescore/agent-harness/cli_anything/musescore/core/instruments.py
index b9fba6c31..89e6c4d6e 100644
--- a/musescore/agent-harness/cli_anything/musescore/core/instruments.py
+++ b/musescore/agent-harness/cli_anything/musescore/core/instruments.py
@@ -195,16 +195,27 @@ def reorder_instruments(path: str, output_path: str,
name = ln.text if ln is not None else ""
parts_by_name[name.lower()] = part
+ # Validate: new_order must contain all instruments
+ provided = {n.lower() for n in new_order}
+ existing = set(parts_by_name.keys())
+ missing = existing - provided
+ if missing:
+ missing_names = [n for n in parts_by_name if n in missing]
+ raise ValueError(
+ f"new_order is missing instruments: {missing_names}. "
+ f"All instruments must be included to prevent data loss."
+ )
+ unknown = provided - existing
+ if unknown:
+ raise ValueError(f"Instruments not found in score: {list(unknown)}")
+
# Remove all parts
for part in score.findall("Part"):
score.remove(part)
# Re-add in new order
for name in new_order:
- part = parts_by_name.get(name.lower())
- if part is None:
- raise ValueError(f"Instrument '{name}' not found in score")
- score.append(part)
+ score.append(parts_by_name[name.lower()])
xml_utils.write_mscz(output_path, data)
diff --git a/musescore/agent-harness/cli_anything/musescore/core/session.py b/musescore/agent-harness/cli_anything/musescore/core/session.py
index 5bee1dfbe..73eb3f9aa 100644
--- a/musescore/agent-harness/cli_anything/musescore/core/session.py
+++ b/musescore/agent-harness/cli_anything/musescore/core/session.py
@@ -5,9 +5,13 @@ undo/redo stacks and safe file locking for concurrent access.
"""
import copy
-import fcntl
import json
import os
+
+try:
+ import fcntl
+except ImportError:
+ fcntl = None # Windows — file locking unavailable
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
@@ -109,25 +113,30 @@ class Session:
def _locked_save_json(path: str, data: Any):
- """Save JSON data with file locking (fcntl.flock).
+ """Save JSON data with file locking where available.
- Opens in r+ mode (never w which truncates before lock).
- Creates the file first if it doesn't exist.
+ Uses fcntl.flock on Unix (r+ mode to avoid truncation before lock).
+ Falls back to plain write on Windows where fcntl is unavailable.
"""
- # Ensure file exists
- if not os.path.exists(path):
- Path(path).parent.mkdir(parents=True, exist_ok=True)
+ Path(path).parent.mkdir(parents=True, exist_ok=True)
+
+ if fcntl is not None:
+ # Unix: lock-safe write
+ if not os.path.exists(path):
+ with open(path, "w") as f:
+ f.write("{}")
+ with open(path, "r+") as f:
+ fcntl.flock(f.fileno(), fcntl.LOCK_EX)
+ try:
+ f.seek(0)
+ f.truncate()
+ json.dump(data, f, indent=2, default=str)
+ finally:
+ fcntl.flock(f.fileno(), fcntl.LOCK_UN)
+ else:
+ # Windows: plain write (session state is not concurrent-critical)
with open(path, "w") as f:
- f.write("{}")
-
- with open(path, "r+") as f:
- fcntl.flock(f.fileno(), fcntl.LOCK_EX)
- try:
- f.seek(0)
- f.truncate()
json.dump(data, f, indent=2, default=str)
- finally:
- fcntl.flock(f.fileno(), fcntl.LOCK_UN)
# ── Singleton ─────────────────────────────────────────────────────────
diff --git a/musescore/agent-harness/cli_anything/musescore/musescore_cli.py b/musescore/agent-harness/cli_anything/musescore/musescore_cli.py
index c31975a06..27489dbfc 100644
--- a/musescore/agent-harness/cli_anything/musescore/musescore_cli.py
+++ b/musescore/agent-harness/cli_anything/musescore/musescore_cli.py
@@ -12,6 +12,7 @@ Usage:
cli-anything-musescore # Enter interactive REPL
"""
+import functools
import sys
import os
import json
@@ -70,6 +71,7 @@ def _print_list(items: list, indent: int = 0):
def handle_error(func):
"""Decorator for consistent error handling across commands."""
+ @functools.wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
@@ -87,8 +89,6 @@ def handle_error(func):
click.echo(f"Error: {e}", err=True)
if not _repl_mode:
sys.exit(1)
- wrapper.__name__ = func.__name__
- wrapper.__doc__ = func.__doc__
return wrapper
@@ -524,7 +524,8 @@ def repl():
_repl_help(skin)
continue
- args = line.split()
+ import shlex
+ args = shlex.split(line)
try:
cli.main(args, standalone_mode=False)
except SystemExit: