Skip to content

Commit 198a4ff

Browse files
committed
Scope status-file reload checks to the active instance
1 parent 97048ae commit 198a4ff

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

MCPForUnity/UnityMcpServer~/src/unity_connection.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,23 +235,39 @@ def send_command(self, command_type: str, params: Dict[str, Any] = None) -> Dict
235235
attempts = max(config.max_retries, 5)
236236
base_backoff = max(0.5, config.retry_delay)
237237

238-
def read_status_file() -> dict | None:
238+
def read_status_file(target_hash: str | None = None) -> dict | None:
239239
try:
240-
status_files = sorted(Path.home().joinpath(
241-
'.unity-mcp').glob('unity-mcp-status-*.json'), key=lambda p: p.stat().st_mtime, reverse=True)
240+
base_path = Path.home().joinpath('.unity-mcp')
241+
status_files = sorted(
242+
base_path.glob('unity-mcp-status-*.json'),
243+
key=lambda p: p.stat().st_mtime,
244+
reverse=True,
245+
)
242246
if not status_files:
243247
return None
244-
latest = status_files[0]
245-
with latest.open('r') as f:
248+
if target_hash:
249+
for status_path in status_files:
250+
if status_path.stem.endswith(target_hash):
251+
with status_path.open('r') as f:
252+
return json.load(f)
253+
# Fallback: return most recent regardless of hash
254+
with status_files[0].open('r') as f:
246255
return json.load(f)
247256
except Exception:
248257
return None
249258

250259
last_short_timeout = None
251260

261+
# Extract hash suffix from instance id (e.g., Project@hash)
262+
target_hash: str | None = None
263+
if self.instance_id and '@' in self.instance_id:
264+
maybe_hash = self.instance_id.split('@', 1)[1].strip()
265+
if maybe_hash:
266+
target_hash = maybe_hash
267+
252268
# Preflight: if Unity reports reloading, return a structured hint so clients can retry politely
253269
try:
254-
status = read_status_file()
270+
status = read_status_file(target_hash)
255271
if status and (status.get('reloading') or status.get('reason') == 'reloading'):
256272
return MCPResponse(
257273
success=False,
@@ -361,7 +377,7 @@ def read_status_file() -> dict | None:
361377

362378
if attempt < attempts:
363379
# Heartbeat-aware, jittered backoff
364-
status = read_status_file()
380+
status = read_status_file(target_hash)
365381
# Base exponential backoff
366382
backoff = base_backoff * (2 ** attempt)
367383
# Decorrelated jitter multiplier

Server/unity_connection.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,23 +235,39 @@ def send_command(self, command_type: str, params: Dict[str, Any] = None) -> Dict
235235
attempts = max(config.max_retries, 5)
236236
base_backoff = max(0.5, config.retry_delay)
237237

238-
def read_status_file() -> dict | None:
238+
def read_status_file(target_hash: str | None = None) -> dict | None:
239239
try:
240-
status_files = sorted(Path.home().joinpath(
241-
'.unity-mcp').glob('unity-mcp-status-*.json'), key=lambda p: p.stat().st_mtime, reverse=True)
240+
base_path = Path.home().joinpath('.unity-mcp')
241+
status_files = sorted(
242+
base_path.glob('unity-mcp-status-*.json'),
243+
key=lambda p: p.stat().st_mtime,
244+
reverse=True,
245+
)
242246
if not status_files:
243247
return None
244-
latest = status_files[0]
245-
with latest.open('r') as f:
248+
if target_hash:
249+
for status_path in status_files:
250+
if status_path.stem.endswith(target_hash):
251+
with status_path.open('r') as f:
252+
return json.load(f)
253+
# Fallback: return most recent regardless of hash
254+
with status_files[0].open('r') as f:
246255
return json.load(f)
247256
except Exception:
248257
return None
249258

250259
last_short_timeout = None
251260

261+
# Extract hash suffix from instance id (e.g., Project@hash)
262+
target_hash: str | None = None
263+
if self.instance_id and '@' in self.instance_id:
264+
maybe_hash = self.instance_id.split('@', 1)[1].strip()
265+
if maybe_hash:
266+
target_hash = maybe_hash
267+
252268
# Preflight: if Unity reports reloading, return a structured hint so clients can retry politely
253269
try:
254-
status = read_status_file()
270+
status = read_status_file(target_hash)
255271
if status and (status.get('reloading') or status.get('reason') == 'reloading'):
256272
return MCPResponse(
257273
success=False,
@@ -361,7 +377,7 @@ def read_status_file() -> dict | None:
361377

362378
if attempt < attempts:
363379
# Heartbeat-aware, jittered backoff
364-
status = read_status_file()
380+
status = read_status_file(target_hash)
365381
# Base exponential backoff
366382
backoff = base_backoff * (2 ** attempt)
367383
# Decorrelated jitter multiplier

0 commit comments

Comments
 (0)