diff --git a/translate-proxy.py b/translate-proxy.py index 76ac271..9c180a5 100755 --- a/translate-proxy.py +++ b/translate-proxy.py @@ -5896,26 +5896,36 @@ class Handler(http.server.BaseHTTPRequestHandler): f"{_ANTIGRAVITY_MAX_TOOL_CALLS_PER_TASK - cumulative_calls} remaining before forced stop. " f"STOP READING FILES AND APPLY YOUR EDITS NOW."}]}) - # CHANGE 2: File-path read-loop detection + # CHANGE 2: Read-vs-write loop detection + _READ_CMDS = ("cat ", "head ", "tail ", "less ", "more ", "grep ", "find ", "ls ", "sed -n", "wc ", "file ", "stat ", "du ", "pwd", "which ", "type ", "python3 -c \"\nwith open(", "python3 -c \"\nimport re; open(", ".read(", "read_file", "view_file", "search_file", "list_dir") + _WRITE_CMDS = ("write(", ".write", " > ", " >> ", "sed -i", "patch ", "mv ", "cp ", "install ", "mkdir ", "rm ", "chmod ", "chown ", "truncate", "insert_text", "replace_text", "write_file", "create_file") with _ANTIGRAVITY_LOOP_TRACKER_LOCK: if ag_key not in _ANTIGRAVITY_FILE_TRACKER: - _ANTIGRAVITY_FILE_TRACKER[ag_key] = {"last_path": None, "path_counts": {}, "total_reads": 0} + _ANTIGRAVITY_FILE_TRACKER[ag_key] = {"reads": 0, "writes": 0, "write_detected": False} ft = _ANTIGRAVITY_FILE_TRACKER[ag_key] - for item in reversed(input_data): + for item in input_data: if isinstance(item, dict) and item.get("type") == "function_call": - args_str = json.dumps(item.get("arguments", {})) - file_match = re.search(r'(/[\w/.-]+\.(?:html|py|js|ts|css|json|md|yaml|yml|xml|txt|sh))', args_str) - if file_match: - detected_path = file_match.group(1) - ft["total_reads"] += 1 - ft["path_counts"][detected_path] = ft["path_counts"].get(detected_path, 0) + 1 - ft["last_path"] = detected_path - if ft["path_counts"][detected_path] >= 8 or ft["total_reads"] > 40: - ag_state["force_finalize"] = True - print(f"[antigravity-loop] FILE READ LOOP: {detected_path} read " - f"{ft['path_counts'][detected_path]}x, total={ft['total_reads']}", - file=sys.stderr) - break + args_str = item.get("arguments", "{}") + if not isinstance(args_str, str): + args_str = json.dumps(args_str) + args_lower = args_str.lower() + is_read = any(k in args_lower for k in _READ_CMDS) + is_write = any(k in args_lower for k in _WRITE_CMDS) + if is_write: + ft["writes"] += 1 + ft["write_detected"] = True + elif is_read: + ft["reads"] += 1 + n_reads = ft["reads"] + n_writes = ft["writes"] + if n_reads >= 12 and n_writes == 0: + ag_state["force_finalize"] = True + print(f"[antigravity-loop] READ-WRITE IMBALANCE: {n_reads} reads, {n_writes} writes — model never writes", file=sys.stderr) + elif n_reads >= 8 and n_writes == 0 and not ag_state.get("force_finalize"): + contents.append({"role": "user", "parts": [{"text": + f"WARNING: You have made {n_reads} tool calls and ZERO writes. " + f"You MUST apply your edit NOW using exec_command with a python write. " + f"Do NOT read any more files. WRITE YOUR CHANGES IMMEDIATELY."}]}) null_tool_names = {"get_goal", "get_remaining_tokens", "get_completion_budget", "status"} consecutive_null = 0 @@ -6731,22 +6741,31 @@ class Handler(http.server.BaseHTTPRequestHandler): with _ANTIGRAVITY_LOOP_TRACKER_LOCK: if ag_key not in _ANTIGRAVITY_FILE_TRACKER: - _ANTIGRAVITY_FILE_TRACKER[ag_key] = {"last_path": None, "path_counts": {}, "total_reads": 0} + _ANTIGRAVITY_FILE_TRACKER[ag_key] = {"reads": 0, "writes": 0, "write_detected": False} ft = _ANTIGRAVITY_FILE_TRACKER[ag_key] - for item in reversed(input_data): + for item in input_data: if isinstance(item, dict) and item.get("type") == "function_call": - args_str = json.dumps(item.get("arguments", {})) - file_match = re.search(r'(/[\w/.-]+\.(?:html|py|js|ts|css|json|md|yaml|yml|xml|txt|sh))', args_str) - if file_match: - dp = file_match.group(1) - ft["total_reads"] += 1 - ft["path_counts"][dp] = ft["path_counts"].get(dp, 0) + 1 - ft["last_path"] = dp - if ft["path_counts"][dp] >= 8 or ft["total_reads"] > 40: - ag_state["force_finalize"] = True - print(f"[antigravity-loop] FILE READ LOOP: {dp} read " - f"{ft['path_counts'][dp]}x, total={ft['total_reads']}", file=sys.stderr) - break + args_str = item.get("arguments", "{}") + if not isinstance(args_str, str): + args_str = json.dumps(args_str) + args_lower = args_str.lower() + is_read = any(k in args_lower for k in _READ_CMDS) + is_write = any(k in args_lower for k in _WRITE_CMDS) + if is_write: + ft["writes"] += 1 + ft["write_detected"] = True + elif is_read: + ft["reads"] += 1 + n_reads = ft["reads"] + n_writes = ft["writes"] + if n_reads >= 12 and n_writes == 0: + ag_state["force_finalize"] = True + print(f"[antigravity-loop] READ-WRITE IMBALANCE: {n_reads} reads, {n_writes} writes — model never writes", file=sys.stderr) + elif n_reads >= 8 and n_writes == 0 and not ag_state.get("force_finalize"): + contents.append({"role": "user", "parts": [{"text": + f"WARNING: You have made {n_reads} tool calls and ZERO writes. " + f"You MUST apply your edit NOW using exec_command with a python write. " + f"Do NOT read any more files. WRITE YOUR CHANGES IMMEDIATELY."}]}) null_tool_names = {"get_goal", "get_remaining_tokens", "get_completion_budget", "status"} consecutive_null = 0