Skip to content

fix: use context manager for file reads in code_projects_api.py - #93

Open
nwalio wants to merge 57 commits into
OpenNSWM-Lab:mainfrom
nwalio:fix/code-projects-unclosed-files
Open

nwalio wants to merge 57 commits into
OpenNSWM-Lab:mainfrom
nwalio:fix/code-projects-unclosed-files

Conversation

@nwalio

@nwalio nwalio commented Aug 30, 2026

Copy link
Copy Markdown

Background

In backend/app/modules/code/code_projects_api.py, two file reads use the pattern json.loads(open(path, encoding="utf-8").read()), which opens a file handle without explicitly closing it. The handle is eventually collected by the garbage collector, but if json.loads raises before the handle is released, it leaks. Under PyPy or high-concurrency workloads, these accumulate.

Changes

Replaced both occurrences with the with open(...) as f: context manager pattern:

  • Line 1299 (get_pipeline_results): reading pipeline_results.json
  • Line 1374 (auto_fix_pipeline): reading previous pipeline results

Each change adds one line (the with statement) and re-indents the json.loads call inside the block. No behavioral change — the same data is read and parsed.

Before/After

# Before
data = json.loads(open(results_path, encoding="utf-8").read())

# After
with open(results_path, encoding="utf-8") as _f:
    data = json.loads(_f.read())

Notes

The existing encoding="utf-8" parameter is preserved — this was already correct. The only change is ensuring the file handle is deterministically closed via the context manager protocol rather than relying on GC timing.

Verification

  • py_compile confirms valid Python syntax
  • New test tests/test_pr_06_code_projects_files.py runs two assertions:
    1. No open(...).read() patterns remain in the module
    2. At least 3 context-managed results_path opens exist (2 reads + 1 write)
  • Both tests pass in the sandbox container

wzj and others added 30 commits July 7, 2026 17:10
1. ProblemFrame问题框定:scoped question/术语/变量/假设/边界/子问题
2. ResearchDossier构建器:内部模型→公共契约转换,通过52项测试
3. Evidence Gate三级分类:supporting/counter/context + 去重 + 完整字段
4. 证伪条件:每个假设绑定falsification criteria/confounders/alternative explanations
5. ResearchPlan:每步含输入/工具/方法/输出/指标/停止条件/依赖/风险
6. 预算分级:coverage/deep两种模式 + 4种降级状态(NO_API/SEARCH_FAILURE/INSUFFICIENT_EVIDENCE/TOPIC_DRIFT)
7. Review反馈循环:create_child_run + diff_dossiers v1/v2差异
8. API端点:POST /ideas/dossier + POST /ideas/dossier/diff
9. MODULE_HANDOFF文档
1. research_dossier.py: session.config.seedQuery 正确访问路径
2. research_dossier.py: confounders 空值回退(从假设推导)
3. 新增 test_idea_degraded_fixtures.py: 9项降级状态测试
4. .gitignore: 新增降级测试文件白名单
问题:13个候选中12个被Review Gate拒绝,唯一原因是referenceSupport
低于4.5。LLM评分因"only 8 references"系统性压低refSupport(2.5-4.1),
而其他维度分数均优秀(overall 6.6-7.2, novelty 6.8-7.3)。

修复:将referenceSupport三级阈值统一降至3.5
- 规则审查器阻塞阈值: < 4.5 → < 3.5
- 门控通过阈值: >= 4.5 → >= 3.5
- 宽松质量阈值: >= 4.3 → >= 3.5
- strict阈值4.8保持不变

效果:5个候选通过gate,selector选出2个final候选(1 strict + 1 relaxed)
验收:11/11 ALL PASS (≥2假设, ≥1反证, ProblemFrame, ResearchPlan,
Qwen trace, 证伪条件, 混杂因素)
48个idea模块测试全通过
1. create_child_run API端点 (POST /dossier/child-run)
   - 接收parentSessionId + reviewFindings
   - 创建child ScientificQuestionRun + 新idea session
   - 存储findings到session.qualityLoopSummary

2. 前端DossierViewer组件
   - 展示ProblemFrame (scopedQuestion/definitions/variables/assumptions)
   - 展示EvidenceMap (supporting/counter/context三分类tab)
   - 展示Hypotheses (scores/falsification/confounders/alternatives)
   - 展示ResearchPlan (steps含inputs/tools/method/metrics/stopConditions)
   - 集成到IdeaGenerationPanel (session完成后显示)

3. MODULE_HANDOFF文档更新
   - head commit: f387695 → 7285fb3
   - 测试数: 52→61 (加test_idea_degraded_fixtures.py)
   - cap值修正: INSUFFICIENT_EVIDENCE 0.4→0.5, TOPIC_DRIFT 0.3→0.4
   - 补充11/11 ALL PASS验收结果

4. .gitignore: 忽略17个临时诊断/测试脚本

5. 百炼调用trace已验证 (provider=bailian, model=qwen-turbo, 3.7s)
feat(idea): 搜索源扩充 + seed预检 + 公共契约对齐 + Review Gate修复 + 前端Dossier查看器
Restore README and frontend Node engine metadata to the current devtzb versions so the Code PR contains only module-related changes.
Ironknory and others added 27 commits August 12, 2026 13:11
# Conflicts:
#	backend/app/modules/code/execution_assessment.py
#	backend/app/modules/code/interfaces.py
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.

4 participants