refactor: follow-up tech debt cleanup (except Exception + Any 治理) #9
Loading…
Reference in New Issue
No description provided.
Delete Branch "refactor/followup-tech-debt"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Follow-up Tech Debt Cleanup: except Exception + Any 治理
Summary
承接 PR #8 (U1-U5 主债务清理) 的 Known Residuals,本 PR 集中治理 3 类系统性技术债务残留:
server/routes/的except Exception窄化(23 文件,131 处)— 把框架边界外的宽泛异常捕获替换为具体异常元组,框架边界保留并加asyncio.CancelledError守卫llm/+memory/+client/的Any消除(31 文件,172 处)— 用 TypeAlias /object/ TYPE_CHECKING Protocol 替换Anyorchestrator/+bitable/的Any消除(19 文件,112 处)— 同上策略,并对RecalcTask用具体类型,对Coroutine[Any, Any, Any]用Coroutine[object, object, object]总计 415 处治理,覆盖 73 个文件。
Key Design Decisions
except Exception分类策略(PR #1)按调用场景分类异常元组,而非一刀切:
(ConnectionError, RuntimeError, asyncio.TimeoutError)(ConnectionError, OSError, asyncio.TimeoutError, ValueError, KeyError, RuntimeError)(asyncio.QueueFull, RuntimeError, ConnectionError)(ValueError, TypeError, KeyError)(RuntimeError, asyncio.TimeoutError, ConnectionError)框架边界保留:
auth.py的auth_flow、ws.py的 WebSocket 主循环等框架入口仍需except Exception兜底防止进程崩溃,但加except asyncio.CancelledError: raise守卫确保取消信号正确传播。2 处 noqa 保留:
skills.py的 DB 韧性边界(业务上需要捕获所有异常以保证 skill 加载不阻塞)。Any治理策略(PR #2 + #3)按优先级选择替代类型:
MetadataValue/MetadataDict/RAGSearchResult/SkillConfigDict等object(最严格任意类型)—dict[str, Any]→dict[str, object];**kwargs: Any→**kwargs: object;list[Any]→list[object];返回值Any→object_RedisLike/_RedisPipelineLike/_LLMGatewayLike/_QuotaServiceLike/_RAGServiceLike/_GraphServiceLike/_PlanLike等Coroutine[Any, Any, Any]→Coroutine[object, object, object]RecalcTask)替代Any关键决策:避免递归 TypeAlias
PR #3 初版在
bitable/models.py定义了递归 TypeAliasFieldValue = str | int | ... | list["FieldValue"] | dict[str, "FieldValue"],但 Pydantic v2 无法为递归命名别名构建 schema(RecursionError: maximum recursion depth exceeded)。改用dict[str, object]并加ponytail:注释说明天花板与升级路径。Testing Notes
server/routes/llm/+memory/+client/orchestrator/bitable/ruff: All checks passed (
ruff check src/agentkit/llm/ src/agentkit/memory/ src/agentkit/client/ src/agentkit/orchestrator/ src/agentkit/bitable/)验证方法: 对每个失败用
git stash && git checkout main对比基线确认非回归。Commits
aa6367f—refactor(server/routes): classify except Exception in 23 route files(131 处)34a89c4—refactor(llm+memory+client): remove Any from type signatures(172 处)7b1b198—refactor(orchestrator+bitable): remove Any from type signatures(112 处)Post-Deploy Monitoring & Validation
No additional operational monitoring required — 本 PR 是纯类型注解 + 异常分类重构,不改变运行时行为:
except Exception窄化后,原本被吞掉的异常现在会向上传播;若调用方有更上层兜底,行为不变;若无,则可能暴露原本被隐藏的 bug(这是预期改进,但需关注)server/routes/相关端点的 500 错误率,若某端点 500 上升,可能是窄化过严漏掉了某类运行时异常,需回查该端点的 except 元组except Exception并补充遗漏的异常类型Known Residuals
无。本 PR 完成了 PR #8 Known Residuals 中列出的全部 3 项 follow-up 工作。剩余的
except Exception和Any残留(如有)属于其他子系统,可在后续 PR 中处理。