Skip to content

fix: 修复空 catch 块 — 收窄异常类型、添加日志、注释意图性静默 (Issue #10)#26

Merged
urika merged 3 commits into
mainfrom
fix/issue-10-empty-catch
May 29, 2026
Merged

fix: 修复空 catch 块 — 收窄异常类型、添加日志、注释意图性静默 (Issue #10)#26
urika merged 3 commits into
mainfrom
fix/issue-10-empty-catch

Conversation

@urika
Copy link
Copy Markdown
Owner

@urika urika commented May 29, 2026

Summary

Closes #10

修复 26 个空 except: pass + 5 个近乎空的 catch 块,分布在 9 个模块中。同时修复了 PR #25 Review 反馈的所有测试问题。

Issue #10: 空 catch 块修复

模块级 Logger

为所有受影响模块添加 logging.getLogger(__name__),子模块 logger 自动继承 setup_logger() 配置的 handler,零 API 变更。

A类:添加 debug 日志(18 处)

except: pass 前添加 logger.debug(),不改控制流,提升可诊断性。

B类:收窄异常类型(5 处)

except Exception → 具体类型,防止吞没真实 bug:

文件 原异常 收窄为
agents.py:91 (json.JSONDecodeError, Exception) (json.JSONDecodeError, OSError, KeyError)
cli.py:542 Exception (json.JSONDecodeError, KeyError, IndexError)
cli.py:561 Exception ValueError
tui.py:31 Exception (json.JSONDecodeError, IndexError, KeyError)
tui.py:43 Exception ValueError

C类:注释意图性静默(2 处)

  • pipeline.py:40 — kill 进程可能已退出,静默正确
  • tui.py:182 — curses addstr 边界错误,静默正确

D类:近乎空 catch 补充日志(5 处)

有 return/assign 但无日志的 catch 块,补充 logger.debug()


PR #25 Review 反馈修复

🔴 Critical #1: 假测试重写

test_interrupt_sets_pausedtest_interrupt_handler_writes_paused

  • 通过 patch("signal.signal") 捕获 _run_pipeline 注册的信号处理器闭包
  • 直接调用并验证 sys.exit(0)meta.json 写入 pausedos.kill 被调用

🟡 Major #2-4

  • 交互模式测试添加 shutil.which mock
  • test_no_git_repo_copies_directory 断言加强为检查复制文件
  • 6 个 has_changes=True 测试添加 collect_change_stats mock

🟢 Minor #5,6,7,8,9

  • fast_logger 复用 conftest fixture
  • 移除重复 sys.path.insert
  • 否定断言→积极断言
  • 脆弱中文断言→verify_ok 检查
  • .git 目录→文件(匹配真实 worktree)

测试

pytest tests/ -q   # 212 passed

🤖 Generated with Claude Code

@urika urika force-pushed the fix/issue-10-empty-catch branch from 46e6481 to a3de8ba Compare May 29, 2026 23:11
@urika urika changed the title fix: 修复 26 个空 catch 块,添加日志记录和注释 (Issue #10) fix: 修复空 catch 块 — 收窄异常类型、添加日志、注释意图性静默 (Issue #10) May 29, 2026
jinsongwang and others added 2 commits May 30, 2026 07:51
Closes #10

- 为所有受影响模块添加模块级 logger (logging.getLogger(__name__))
- 26 个空 except:pass 添加 logger.debug() 提升可诊断性
- 5 个近乎空的 catch (return/assign 无日志) 补充 debug 日志
- 5 个 except Exception 收窄为具体异常类型防止吞没真实 bug:
  - agents.py:91 Exception → (json.JSONDecodeError, OSError, KeyError)
  - cli.py:542 Exception → (json.JSONDecodeError, KeyError, IndexError)
  - cli.py:561 Exception → ValueError
  - tui.py:31 Exception → (json.JSONDecodeError, IndexError, KeyError)
  - tui.py:43 Exception → ValueError
- 2 个意图性静默 catch 添加注释说明原因:
  - pipeline.py:40 — kill 进程可能已退出
  - tui.py:182 — curses addstr 边界错误
- 1 个 FileExistsError 添加注释说明 mkdir 重试逻辑

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Critical:
- test_interrupt_sets_paused 重写为真正测试 _on_interrupt 行为:
  通过捕获 _run_pipeline 注册的信号处理器闭包,直接调用并验证
  meta.json 写入 paused + sys.exit(0) + os.kill 被调用

Major:
- test_interactive_mode/test_verification_failure_marks_failed
  添加 shutil.which('greywall') mock,防止安装 greywall 时代码路径改变
- test_no_git_repo_copies_directory 断言加强:
  worktree.exists() → (worktree / 'file.txt').exists()
- 为 6 个 has_changes=True 的测试添加 collect_change_stats mock,
  避免依赖 subprocess.run 全局 mock 处理 metrics 调用

Minor:
- fast_logger fixture 复用 conftest.py 的 logger fixture
- test_completed_status 否定断言改为积极断言
- test_context_file_with_verification 脆弱中文断言改为 verify_ok 检查
- test_existing_worktree_reused .git 目录改为文件(匹配真实 worktree)
- 移除重复的 sys.path.insert(conftest.py 已处理)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@urika urika force-pushed the fix/issue-10-empty-catch branch from 11069b8 to d8e2a95 Compare May 29, 2026 23:52
PR #28 重构了 __init__.py,不再 re-export _is_safe_verification_command。
测试文件应直接从 agent_go.utils 导入。
@urika
Copy link
Copy Markdown
Owner Author

urika commented May 29, 2026

CI 失败分析

3 个失败测试是 预先存在的 CI 环境问题,与本 PR 无关:

  • test_skills.py::test_load_skills_multi — CI 环境无 ~/.agent_go/skills/ 目录
  • test_skills.py::test_list_skills — 同上
  • test_integration.py::test_skill_injection — 同上

PR 本身的 209 个测试全部通过。Rebase 已解决与 main 的冲突(__all__ import 区域 + test import 适配 PR #28)。

@urika urika merged commit 84added into main May 29, 2026
1 check failed
@urika urika deleted the fix/issue-10-empty-catch branch May 30, 2026 01:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Code Quality] 修复 27 个空 catch 块 (except: pass)

1 participant