fix(lib): prevent set -u unbound variable error for final_cmd in kind detection and harden test fixtures

This commit is contained in:
2026-08-13 09:36:07 +09:00
parent db6b064b62
commit 2bd59fce9b
4 changed files with 50 additions and 23 deletions
+26 -7
View File
@@ -87,12 +87,12 @@ if not os.path.exists(state_file):
sys.exit(0)
# Lock the state file exclusively to prevent concurrent race conditions
lock_f = open(state_file + ".lock", "w")
lock_f = open(state_file + ".lock", "a")
fcntl.flock(lock_f, fcntl.LOCK_EX)
state = {"workspaces": [], "agents": {}, "calls": []}
if os.path.exists(state_file):
for _retry in range(10):
for _retry in range(50):
try:
with open(state_file, 'r') as f:
content = f.read().strip()
@@ -100,16 +100,35 @@ if os.path.exists(state_file):
state = json.loads(content)
break
except Exception:
import time
time.sleep(0.05)
pass
time.sleep(0.02)
# Record the command call
state["calls"].append(sys.argv[1:])
def save_state():
disk_state = {"workspaces": [], "agents": {}, "calls": []}
if os.path.exists(state_file):
for _retry in range(50):
try:
with open(state_file, 'r') as f:
content = f.read().strip()
if content:
disk_state = json.loads(content)
break
except Exception:
pass
time.sleep(0.02)
disk_state["agents"] = state.get("agents", {})
if "workspaces" in state:
disk_state["workspaces"] = state["workspaces"]
disk_calls = disk_state.setdefault("calls", [])
if sys.argv[1:] and (not disk_calls or disk_calls[-1] != sys.argv[1:]):
disk_calls.append(sys.argv[1:])
tmp_state = state_file + f".tmp.{os.getpid()}"
with open(tmp_state, 'w') as f:
json.dump(state, f, indent=2)
json.dump(disk_state, f, indent=2)
f.flush()
os.fsync(f.fileno())
os.replace(tmp_state, state_file)
@@ -242,7 +261,7 @@ elif cmd1 == "agent":
"cwd": cwd or "TMP_PATH_PLACEHOLDER",
"workspace_id": ws or "w1",
"pid": 9999,
"pane_id": f"w1:p{len(agents)+1}",
"pane_id": f"w1:p_{name}",
"command": " ".join(agent_cmd),
"buffer": buffer_content
}
@@ -376,7 +395,7 @@ elif cmd1 == "agent":
agents = state.get("agents", {})
if name in agents:
agents[name]["sent_text"] = agents[name].get("sent_text", "") + text
if text == "C-m":
if text in ("C-m", "Enter"):
agents[name]["buffer"] = agents[name].get("buffer", "") + "\\nesc to interrupt"
else:
agents[name]["buffer"] = agents[name].get("buffer", "") + "\\n" + text