Skip to content

Working memory race condition with concurrent sessions #57

@dean0x

Description

@dean0x

Problem

Working memory (.docs/WORKING-MEMORY.md) is written by the Stop hook at the end of each session. If two Claude Code sessions are open on the same repo simultaneously, they will overwrite each other's working memory — last writer wins, losing context from the other session.

Scenarios

  1. Two terminals, same repo — both sessions write WORKING-MEMORY.md on stop, second one clobbers the first
  2. Session A compacts while Session B is active — PreCompact hook writes backup, but Session B's stop overwrites with stale state
  3. SessionStart reads stale memory — if Session A wrote memory but Session B already loaded, B operates on outdated context

Possible Solutions

  • Session-scoped memory files — each session writes its own file (e.g., WORKING-MEMORY-{session-id}.md), SessionStart merges them
  • Lock file / advisory locking — prevent concurrent writes, queue or skip if locked
  • Append-only log — instead of overwriting, append timestamped entries; SessionStart reads the latest
  • Accept the limitation — document that working memory is single-session only and warn users

Context

The current hooks: stop-update-memory.sh (writes), session-start-memory.sh (reads + injects), pre-compact-memory.sh (backup). All assume single-session usage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions