open_session truncates a live session file under the lock
Problem
core/agent_harness/session/persistence/jsonl_store.py:127:
def open_session(self, session: SessionPersistenceSource) -> None:
with contextlib.suppress(Exception):
path = session_path(session.session_id)
with self._locked(path):
...
with path.open("w", encoding="utf-8") as fh: # <-- unconditional truncate
fh.write(json.dumps(record, ensure_ascii=False) + "\n")"w" truncates. Held inside the cross-process lock, this means a second process
waits its turn and then erases the file the first one just wrote.
Nothing catches it: the store has no cross-process test, and the three tests in
tests/core/agent_harness/session/test_jsonl_store_lock.py are all same-process.
Why it matters
docs/deployment.mdx:156 states "The lock keeps N tasks correct" and line 111
says to enable it "so two tasks never corrupt one". Neither is true while this
line stands — the lock makes the truncation orderly, not absent.
Every transport without inbound affinity — Slack Socket Mode, Telegram, Discord — spreads events across tasks by design, so any conversation can hit a second task.
Steps
Test first, in this PR — the red test is the evidence the fix was needed.
- Write a cross-process test: process A opens a session and appends turns;
process B calls
open_sessionfor the same id. Assert A's turns survive. Use real processes, not threads — N Fargate tasks are processes andthreading.Lockstate is not shared. Port the spawn pattern fromgateway/tests/test_multi_actor_concurrency.py. - Confirm it fails, with
OPENSRE_SESSION_FILE_LOCK=1. A failure with the lock enabled is the whole point; if it only fails with the lock off you have written a different test. - Change
open_sessionto create-if-absent: write the session header only when the file does not exist. An existing session file is opened, never recreated. - For any full-file write, use the atomic pattern already proven in this repo —
gateway/core/storage/session/file_bindings.py:125: lock, write temp,fsync,os.replace. - Confirm the test now passes.
Do not "fix" this by switching "w" to "a". That appends a second header to an
existing file, and the one-header assertion then fails for a different reason.
Expected result
Two processes opening the same session end up with one header and both sets of
turns. deployment.mdx becomes true.
Verification
OPENSRE_SESSION_FILE_LOCK=1 uv run python -m pytest \
tests/core/agent_harness/session/test_jsonl_store_lock.py -qExpected: green, including the new cross-process test.
Prove the test is real — revert the open_session change, re-run, and confirm it
fails. Put the fix back. Say in the PR that you did this.
Integrity of a real file after concurrent opens:
uv run python -c "
import json,sys
recs=[json.loads(l) for l in open(sys.argv[1]) if l.strip()]
print('lines:',len(recs))
print('headers:',sum(1 for r in recs if r.get('type')=='session'))
" ~/.opensre/sessions/<session_id>.jsonlExpected: headers: 1, and a line count covering both processes' turns.
Checklist
- Cross-process test written first and shown to fail with the lock enabled
-
open_sessionnever truncates an existing file - Full-file writes go through lock → temp →
fsync→os.replace - Exactly one session header after concurrent opens
- Revert demonstration run and described in the PR
-
make lint format typecheck test
Out of scope
The lock-timeout drop policy, and the default value of
OPENSRE_SESSION_FILE_LOCK. Both are tracked separately and neither blocks this.
Claiming this issue
Ask in OpenSRE Discord before you start so a maintainer can assign it. Ask OpenSRE maintainers and write a commit under this issue.
Testing this from a surface
The cross-process case, which no current test covers:
- Start the gateway with a transport that has no inbound affinity — Slack Socket Mode, Telegram, or Discord.
- Send two messages to the same conversation close enough together to land on different tasks.
uv run opensre --resume <session-id>and read the history; also inspect the session JSONL under~/.opensre.
Pass: every turn appears exactly once. Fail (today): the second process waits for the lock and then truncates what the first wrote.
Source: Tracer-Cloud/opensre