Skip to content

fix: prevent Stage 4 literature search from silently falling back - #310

Merged
Jiaaqiliu merged 1 commit into
aiming-lab:mainfrom
pikaqiu2333:codex/fix-stage4-os-scope
Aug 19, 2026
Merged

fix: prevent Stage 4 literature search from silently falling back#310
Jiaaqiliu merged 1 commit into
aiming-lab:mainfrom
pikaqiu2333:codex/fix-stage4-os-scope

Conversation

@pikaqiu2333

@pikaqiu2333 pikaqiu2333 commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Remove the function-local os import that broke environment-variable lookup in Stage 4.
  • Restore Semantic Scholar and OpenAlex API key resolution from configuration and environment variables.
  • Add regression coverage for key precedence, search configuration, Web Search augmentation, orchestrator execution, and fallback behavior.

Root Cause

A function-local import os caused Python to treat os as a local variable throughout _execute_literature_collect.

Stage 4 accessed os.environ before reaching that import, raising UnboundLocalError. The existing broad exception handler then caught the error and silently skipped the real literature APIs.

This regression was introduced by my changes in #299. I’m sorry I missed the interaction with the existing function-local import during testing. Thank you for reporting it in #309. This PR fixes the issue and adds orchestration-level regression coverage to prevent the same silent fallback from recurring.

Validation

  • Stage 4 regression tests: 8 passed
  • Configuration and literature tests: 78 passed
  • Executor tests: 207 passed
  • Full suite: 2869 passed, 57 skipped
  • Real literature retrieval:
    • 18 real candidates
    • 12 OpenAlex and 6 arXiv results
    • 0 placeholders
    • 18 BibTeX entries
    • Semantic Scholar rate limiting was handled without breaking the remaining providers

Two pre-existing Windows shell-related tests remain environment-specific and are unrelated to this change.

Fixes #309

@pikaqiu2333
pikaqiu2333 marked this pull request as ready for review July 23, 2026 07:34
@pikaqiu2333
pikaqiu2333 force-pushed the codex/fix-stage4-os-scope branch from 5e125c6 to 5a53c86 Compare July 23, 2026 07:44
@Jiaaqiliu
Jiaaqiliu merged commit 9ab5526 into aiming-lab:main Aug 19, 2026
@Jiaaqiliu

Copy link
Copy Markdown
Collaborator

Merged. This one fixed the highest-impact bug in the queue, and the test coverage is why it landed ahead of the two other PRs carrying the same one-line deletion (#315, #307).

Confirming the severity for the record, since the PR title undersells it. The function-local import os made os local to all of _execute_literature_collect, so the earlier uses raised:

UnboundLocalError: cannot access local variable 'os' where it is not associated with a value
  _literature.py:392, in _execute_literature_collect
    or os.environ.get(literature_config.s2_api_key_env, "")

Line 392 is inside the try: wrapping the entire real-API search, under a bare except Exception that logs [rate-limit] Literature search failed — falling back to LLM. So Stage 4 was not sometimes falling back — Semantic Scholar, OpenAlex and arXiv were unreachable on every run, and every pipeline silently produced LLM-generated candidates while logging what reads as a transient rate-limit. For a tool whose value rests on real citations, that's about as bad as a swallowed exception gets.

Reproduced before merge and confirmed gone after.

The ~400 lines of TestLiteratureCollectConfiguration are the right response: parametrised coverage of key resolution across inline/llm/env precedence and custom env names means the next refactor that breaks Stage 4's search path fails a test instead of quietly returning hallucinated papers. Post-merge: test_rc_executor.py + test_rc_literature.py → 254 passed. Full suite: 2956 passed.

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.

Stage 4 literature search silently falls back due to UnboundLocalError: os

2 participants