Update environment.py
Browse files286 unmodified lines28728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038154 unmodified lines4364374384394404414424434444454462 unmodified lines4494504514524534544553 unmodified lines45946046146246346446546646746846947047147247347447512 unmodified lines4884894904914924934949 unmodified lines50450550650750850951051151251351451551651743 unmodified lines561562563564565566567568569570571572573574575576577578286 unmodified lines# ===================================================================def _compute_dense_reward(self,action: AnyAction,base_reward: float,action_type: str) -> float:"""Compute dense reward with:1. Delta-based improvement rewards2. Tool usage bonuses3. Exploration incentives4. Anti-hacking penaltiesFIXED: Reduced delta weight for ProposeFix to avoid double-counting"""reward = base_reward# FIXED: Reduce delta impact for ProposeFix (already includes test_score in base)effective_delta_weight = self.delta_weightif action_type == "propose_fix":effective_delta_weight *= 0.5 # Prevent double-counting# ============================================================# 1. DELTA-BASED REWARDS (credit assignment)# ============================================================test_delta = self._current_test_score - self._previous_test_scorelint_delta = self._current_lint_score - self._previous_lint_scoreif test_delta > 0:reward += effective_delta_weight * test_deltaelif test_delta < 0:reward += effective_delta_weight * test_delta * 0.5if lint_delta > 0:reward += effective_delta_weight * 0.5 * lint_delta# ============================================================# 2. TOOL USAGE BONUSES# ============================================================if action_type == "run_tests":if not self._tests_run:reward += self.tool_usage_bonusreward += 0.02elif action_type == "run_linter":if not self._linter_run:reward += self.tool_usage_bonusreward += 0.02elif action_type == "query_docs":if not self._docs_queried:reward += self.tool_usage_bonus * 0.5elif action_type == "ask_question":if 1 <= self._step_count <= 5:reward += 0.03# ============================================================# 3. EXPLORATION INCENTIVES# ============================================================if len(self._action_history) >= 3:recent_actions = self._action_history[-3:]action_counts = Counter(recent_actions)most_common_count = action_counts.most_common(1)[0][1]if most_common_count >= 3:reward -= 0.05 # Repetition penaltyelif len(set(recent_actions)) == 3:reward += self.diversity_bonus # Diversity bonus# ============================================================# 4. ANTI-HACKING PENALTIES# ============================================================if action_type == "propose_fix":if not self._tests_run:reward -= 0.2if self._step_count < 2:reward -= 0.15if self._tests_run and self._linter_run:reward += 0.1# ============================================================# 5. STEP PENALTY# ============================================================reward -= self.step_penalty# ============================================================# 6. NORMALIZE TO [-1, 1]# ============================================================reward = max(-1.0, min(1.0, reward))return reward54 unmodified linessuccess, stdout, stderr = execute_code(self._current_code)output = (stdout + stderr).strip() or "No output"self._test_results = f"[Execute] {'Success' if success else 'Failed'}\n{output[:300]}"base_reward = 0.01 if success else -0.05elif isinstance(action, Inspect):self._test_results = f"[Inspect]\n{self._current_code[:500]}"base_reward = 0.01elif isinstance(action, RunLinter):lint_output = ToolBox.run_linter(self._current_code)2 unmodified linesself._current_lint_score = self._run_linter_score(self._current_code)self._linter_run = Truebase_reward = 0.02elif isinstance(action, RunTests):runner = TestRunner(self._current_bug_id)3 unmodified linesself._tests_run = Trueself._test_results = f"[Tests] Score: {score:.2f}\n{output[:300]}"base_reward = 0.02if score > 0.8:base_reward += 0.05elif isinstance(action, QueryDocs):doc = ToolBox.query_docs(action.query_topic)self._doc_results = docself._test_results = f"[Docs]\n{doc[:400]}"self._docs_queried = Truebase_reward = 0.01# ==============================================================# COMMUNICATION ACTIONS12 unmodified linesself._comments.append(f"Author: {response}")self._test_results = f"[Comment] Author: {response[:200]}"base_reward = 0.01elif isinstance(action, AskQuestion):self._comments.append(f"Agent: {action.question}")9 unmodified linesself._comments.append(f"Author: {response}")self._test_results = f"[Question] Author: {response[:200]}"base_reward = 0.02# ==============================================================# FINAL FIX ACTION# ==============================================================elif isinstance(action, ProposeFix):if not action.fix_code:base_reward = -0.5self._done = Trueelse:self._current_code = action.fix_code43 unmodified lines# TERMINATION ACTIONS# ==============================================================elif isinstance(action, Skip):base_reward = -0.3self._done = Trueelif isinstance(action, Done):if self._tests_run:base_reward = self._current_test_score * 0.5 - 0.2else:base_reward = -0.4self._done = Trueelse:base_reward = -0.2self._done = True# ==============================================================286 unmodified lines28728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538654 unmodified lines4414424434444454464474484494504512 unmodified lines4544554564574584594603 unmodified lines46446546646746846947047147247347447547647747847948012 unmodified lines4934944954964974984999 unmodified lines50951051151251351451551651751851952052152243 unmodified lines566567568569570571572573574575576577578579580581582583286 unmodified lines# ===================================================================def _compute_dense_reward(self,action: AnyAction,base_reward: float,action_type: str) -> float:"""Stabilized dense reward:- Decoupled terminal bonus- Controlled base scaling- Symmetric delta handling- Reduced reward hacking surface"""# ============================================================# 0. BASE REWARD (controlled contribution)# ============================================================reward = 0.4 * base_reward # ↓ reduce dominance# ============================================================# 1. DELTA REWARDS (primary learning signal)# ============================================================effective_delta_weight = self.delta_weightif action_type == "propose_fix":effective_delta_weight *= 0.4 # stronger cut to avoid overlaptest_delta = self._current_test_score - self._previous_test_scorelint_delta = self._current_lint_score - self._previous_lint_score# symmetric (no artificial dampening for negatives)reward += effective_delta_weight * test_deltareward += 0.5 * effective_delta_weight * lint_delta# ============================================================# 2. TERMINAL SUCCESS BONUS (clean & isolated)# ============================================================if action_type == "propose_fix":if self._current_test_score > 0.95:reward += 0.4 # slightly reduced to prevent saturationelif self._current_test_score > 0.85:reward += 0.2 # smoother gradient instead of jump# ============================================================# 3. TOOL USAGE (early guidance only)# ============================================================if action_type == "run_tests":if not self._tests_run:reward += self.tool_usage_bonusreward += 0.015elif action_type == "run_linter":if not self._linter_run:reward += self.tool_usage_bonusreward += 0.015elif action_type == "query_docs":if not self._docs_queried:reward += self.tool_usage_bonus * 0.5elif action_type == "ask_question":if self._step_count <= 3:reward += 0.02 # tighter window# ============================================================# 4. EXPLORATION (less noisy)# ============================================================if len(self._action_history) >= 3:recent = self._action_history[-3:]unique = len(set(recent))if unique == 1:reward -= 0.05elif unique == 3:reward += self.diversity_bonus * 0.7 # reduce randomness bias# ============================================================# 5. ANTI-HACKING# ============================================================if action_type == "propose_fix":if not self._tests_run:reward -= 0.25 # stronger enforcementif self._step_count < 2:reward -= 0.1if self._tests_run and self._linter_run:reward += 0.02# ============================================================# 6. STEP PENALTY (progress pressure)# ============================================================reward -= self.step_penalty# ============================================================# 7. CLIP (final safety)# ============================================================return max(-1.0, min(1.0, reward))return reward54 unmodified linessuccess, stdout, stderr = execute_code(self._current_code)output = (stdout + stderr).strip() or "No output"self._test_results = f"[Execute] {'Success' if success else 'Failed'}\n{output[:300]}"base_reward = 0.001 if success else -0.05elif isinstance(action, Inspect):self._test_results = f"[Inspect]\n{self._current_code[:500]}"base_reward = 0.001elif isinstance(action, RunLinter):lint_output = ToolBox.run_linter(self._current_code)2 unmodified linesself._current_lint_score = self._run_linter_score(self._current_code)self._linter_run = Truebase_reward = 0.002elif isinstance(action, RunTests):runner = TestRunner(self._current_bug_id)3 unmodified linesself._tests_run = Trueself._test_results = f"[Tests] Score: {score:.2f}\n{output[:300]}"base_reward = 0.002if score > 0.8:base_reward += 0.005elif isinstance(action, QueryDocs):doc = ToolBox.query_docs(action.query_topic)self._doc_results = docself._test_results = f"[Docs]\n{doc[:400]}"self._docs_queried = Truebase_reward = 0.001# ==============================================================# COMMUNICATION ACTIONS12 unmodified linesself._comments.append(f"Author: {response}")self._test_results = f"[Comment] Author: {response[:200]}"base_reward = 0.001elif isinstance(action, AskQuestion):self._comments.append(f"Agent: {action.question}")9 unmodified linesself._comments.append(f"Author: {response}")self._test_results = f"[Question] Author: {response[:200]}"base_reward = 0.002# ==============================================================# FINAL FIX ACTION# ==============================================================elif isinstance(action, ProposeFix):if not action.fix_code:base_reward = -0.05self._done = Trueelse:self._current_code = action.fix_code43 unmodified lines# TERMINATION ACTIONS# ==============================================================elif isinstance(action, Skip):base_reward = -0.03self._done = Trueelif isinstance(action, Done):if self._tests_run:base_reward = self._current_test_score * 0.5 - 0.2else:base_reward = -0.04self._done = Trueelse:base_reward = -0.02self._done = True# ==============================================================
environment.py+105-100