fix: prevent action data loss from partial line reads in _read_action_log#460
Open
voidborne-d wants to merge 1 commit into666ghj:mainfrom
Open
fix: prevent action data loss from partial line reads in _read_action_log#460voidborne-d wants to merge 1 commit into666ghj:mainfrom
voidborne-d wants to merge 1 commit into666ghj:mainfrom
Conversation
…_log When the simulation subprocess writes a JSONL action line, the monitor thread may read it before the write is flushed (no trailing newline). The old code used `for line in f` + `f.tell()` which advanced past the partial data. On the next poll the reader started mid-line, causing both halves to fail json.loads — the action was permanently lost. Root cause: `for line in f` returns partial lines at EOF (without \n) and then `f.tell()` reports a position past the incomplete data. Fix: - Switch from `for line in f` to `f.readline()` (which supports interleaved `f.tell()` calls) - Track `safe_position`: only advance after reading a complete line (one that ends with \n) - Partial lines (no trailing \n) cause the reader to stop and retry on the next poll cycle — no data is skipped Added 16 tests covering: - Partial line not consumed (core regression) - Partial line recovered on next poll - Multiple partial writes with no loss - Concurrent writer simulation - Basic action/event reading - Edge cases (malformed JSON, blank lines, Unicode, nonexistent file)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
During active simulations, the monitor thread can permanently lose agent actions from the JSONL action log (
twitter/actions.jsonl,reddit/actions.jsonl).Root cause
_read_action_log()usesfor line in fto iterate the log file while the simulation subprocess is actively writing to it. When the writer hasn't flushed a complete line yet (no trailing\n), Python's file iterator returns the partial data as a line. The sequence:{"agent_id": 1, "action_type": "CRE(partial, no\nyet)json.loadsfails → silently skipped (except json.JSONDecodeError: pass)f.tell()which is past the partial dataATE_POST", "round": 1}\nATE_POST", "round": 1}\n→json.loadsfails againThis affects any simulation with concurrent write/read — which is every running simulation, since the monitor thread polls every 2 seconds while the subprocess writes actions continuously.
Additional issue
The original code also called
f.tell()after afor line in floop. In CPython,tell()is disabled inside a file iterator and raisesOSError: telling position disabled by next() call. The current code only avoids this becausef.tell()is called after the loop exits — but it reports the position at EOF, including any partial data.Fix
for line in fwithf.readline()loop (which supports interleavedf.tell()calls)safe_position: only advance after reading a complete line (ends with\n)\n) cause the reader to stop and retry on the next poll — no data is skippedTests
Added 16 tests covering:
test_partial_line_not_consumed,test_partial_line_recovered_on_next_poll,test_multiple_partial_writes_no_losstest_empty_file,test_single_complete_action,test_multiple_actions,test_event_lines_parsed,test_simulation_end_event,test_incremental_readingtest_malformed_json_skipped,test_blank_lines_skipped,test_unicode_content,test_nonexistent_file,test_reddit_platform,test_only_partial_line_no_advancetest_writer_mid_line_then_completesAll 16 tests pass.
Changed files
backend/app/services/simulation_runner.py— fix_read_action_log()to usereadline()+safe_positiontrackingbackend/tests/test_read_action_log.py— 16 new tests (with lightweight dependency stubs)backend/tests/__init__.py— new test package