Heuristic:AGI Edgerunners LLM Agents Papers Emergency Save On Exception
| Knowledge Sources | |
|---|---|
| Domains | Debugging, Data_Safety, Data_Collection |
| Last Updated | 2026-10-10 14:30 GMT |
Overview
Crash recovery pattern that wraps the main pipeline entry point in a try/except block to persist all accumulated state to disk on any unhandled exception, preventing data loss during interactive labeling.
Description
The emergency save pattern wraps the main processing function in a try/except. If any exception occurs (including KeyboardInterrupt from the user pressing Ctrl+C during interactive labeling), the exception handler iterates the module-level stored_datas global and writes each year's accumulated data to its JSON file before re-raising or exiting.
This is critical for the interactive labeling phase of parse_paper(), where a user may spend hours manually categorizing papers. Without the emergency save, a single error or accidental Ctrl+C would lose all labeling work done in the current session.
Usage
Use this heuristic when:
- Running an interactive pipeline where the user manually labels data over an extended session
- The pipeline accumulates state in memory that is only persisted at the end of normal execution
- A crash or interruption would cause significant loss of manual work
- The pipeline processes items sequentially and state grows incrementally
The Insight (Rule of Thumb)
- Action: Wrap the main entry point in try: main() except Exception: save_all().
- Value: The except block iterates stored_datas (year-keyed dict of paper metadata) and calls write_json() for each year. This is the same save logic as the normal end-of-run save.
- Trade-off: The exception is swallowed (not re-raised), so the script exits silently on error. The user may not immediately notice the failure, but the data is safe. Adding a print or logging.error in the except block would improve visibility.
- Compatibility: Works with any in-memory data structure that can be iterated and serialized. The stored_datas global must be accessible from the except block's scope.
Reasoning
The parse_paper() function prompts the user for category labels interactively (input()). A session labeling hundreds of papers may take hours. The function only writes to disk at the end of the loop (script_v5_step1.py:L168-169). If an exception occurs mid-loop — whether from a network error, a malformed input, or the user pressing Ctrl+C — all accumulated labels would be lost.
The emergency save in the __main__ block (script_v5_step1.py:L172-177) catches any exception and writes the current state of stored_datas to disk. Since stored_datas is updated incrementally during the loop (line 165: stored_datas[year].update({md5_id: parsed})), the saved state reflects all work completed up to the point of failure.
Combined with Incremental_Skip_Existing, re-running the script after a crash will skip already-labeled papers and resume from where it left off.
Code Evidence
Emergency save in script_v5_step1.py:L172-177:
if __name__ == '__main__':
try:
parse_paper()
except Exception as e:
for year, datas in stored_datas.items():
write_json(f'{PARSED_FOLDER}/{year}.json', datas)
Normal end-of-run save in parse_paper (script_v5_step1.py:L168-169):
for year, datas in stored_datas.items():
write_json(f'{PARSED_FOLDER}/{year}.json', datas)
Incremental state update during the loop (script_v5_step1.py:L164-167):
if year in stored_datas:
stored_datas[year].update({md5_id: parsed})
else:
stored_datas[year] = {md5_id: parsed}