Refactor Legacy Code: a Tokeny case study
Identify code smells and provide refactoring plans to gradually improve code maintainability. Follow the recorded task, execution screenshots, actual files and verification notes.
By the Tokeny team · Windows · Tokeny 1.7.8 · gpt-6-sol
Prepare the inputs
Create a separate workspace in Tokeny, download and unpack the sample inputs, then select a model that supports tools. The sample uses synthetic data or the public sources named in the article.
- legacy.py
Send the task to Tokeny
The original task was executed in Chinese. Copying these instructions reproduces the same task; the results below include an English report.
保留行为重构订单统计函数,拆分解析、金额计算和分组逻辑;先写回归测试,再对新旧实现做结果比较。 交付文件:refactored.py、test_refactor.py、refactor.md。 验收要点: - 覆盖空列表、取消订单和同地区累加 - 金额仍使用整数分 - 实际测试通过 这是使用合成演示输入或指定公开资料的真实操作案例。请先读取工作空间输入,再实际生成文件并自检。所有写入、脚本与临时服务限于当前工作空间;临时服务只监听 127.0.0.1,执行后关闭;不触达外部收件人、不发布、不付款、不更改系统设置,不调用子智能体。需要公开资料时可读取给定 URL。只启用完成任务必需的工具。另写 report.md(中文)和 report.en.md(英文),简洁、完整,包含输入、处理步骤、真实结果、检查方式和局限;失败或未验证部分如实记录,不能宣称成功。最终回复用中文列出产物、关键结果与待核实项。
Follow-up instructions after review (1)
复核发现重构存在行为回归:legacy.summarize([{cancelled: True}]) 会直接跳过并返回 {},而新版先 parse_order,读取缺失的 region/qty/price_cents,会抛异常。Python 示例应使用 {"cancelled": True}。请先实际复现,修复为对取消订单在字段解析前跳过,补充取消订单缺字段和非法金额的回归测试,对比新旧行为。保留已有通过的测试,更新双语报告如实记录修复与测试数量,无需固定字数。The recorded run used an isolated demonstration workspace. Review the commands and file paths before approving operations in your own workspace.
What actually ran
The task was submitted through the Tokeny input box. An online model read the inputs and executed tools. This run covers the local sample task and its actual files. Results and checks are recorded below.
Results and verification
Input
The workspace input was legacy.py. Its summarize(rows) first skips truthy cancelled orders, then accumulates int(qty) * int(price_cents) by region. ENVIRONMENT.md identifies this as an isolated Windows demonstration workspace.
This round first reproduced the reported input: [{"cancelled": True}].
Processing
- Reproduced the behavior difference between the legacy implementation and the previous refactor for a cancelled order with missing fields.
- Changed
refactored.pyto check cancellation before parsing order fields. - Kept the existing regression tests and added legacy/refactored comparisons for a cancelled order with missing fields and an invalid amount.
- Ran the full regression suite, Python compilation checks, and independent comparisons for both edge cases.
Actual Results
- Reproduction before the fix: the legacy implementation returned
{}; the previous refactor raisedKeyError: 'region'. - After the fix: both implementations return
{}for a cancelled order with missing fields. - For an invalid amount, both implementations raise the same
ValueErrorwith the same message. - Regression tests: all 7 passed in about 0.002 seconds.
- Syntax checks:
legacy.py,refactored.py, andtest_refactor.pypassedpy_compile. - Deterministic randomized comparison: all 100 inputs matched.
- Valid amounts remain Python
intvalues representing integer cents.
Checks
Commands executed:
python -m unittest -v test_refactor.py
python -m py_compile legacy.py refactored.py test_refactor.py
An independent Python script called legacy.summarize and refactored.summarize directly and compared the result or exception type and message for the two edge cases.
Limitations
Other combinations of missing fields, complex exception objects, performance benchmarks, and concurrency were not tested. For non-cancelled rows with missing fields, the refactor preserves the legacy error behavior and does not add validation.
Download and inspect the outputs
The archive contains the files created during this run. Use these checks when adapting the example to your own data.
- Reconcile the reported results against the downloadable sample inputs.
- Open the generated files and check the contents relevant to your task.
- Review the limitations and any unverified steps in the report.
