Shared State
"Single-writer principle prevents race conditions. Use Edit tool for atomic updates."
Core Principle
Each file has a primary owner to avoid conflicts. Only the owner may write to that file.
File Ownership Matrix (v2.0 - Per-Agent State Files)
State Files
| File |
Primary Owner |
Other Agents |
Size |
Purpose |
current-task-developer.json |
Developer |
PM (read-only) |
~1KB |
Developer's task state |
current-task-qa.json |
QA |
PM (read-only) |
~1KB |
QA's task state |
current-task-techartist.json |
Tech Artist |
PM (read-only) |
~1KB |
Tech Artist's task state |
| `current-task-gamedesigner.json |
Game Designer |
PM (read-only) |
~1KB |
Game Designer's task state |
current-task-pm.json |
PM |
Workers (read-only) |
~2KB |
PM coordinator state |
prd.json |
PM (full access) |
Workers DO NOT read |
110KB |
Full PRD (PM-ONLY in v2.0) |
Session Files
| File |
Primary Owner |
Other Agents |
prd.json.session |
PM |
Workers check their state file for sessionStatus (NOT prd.json directly) |
prd.json.items[{taskId}] |
PM (creates) |
Workers do NOT update directly - PM updates based on messages |
prd.json |
PM (fields) |
Workers DO NOT read in v2.0 - PM handles all PRD updates |
session.log |
All agents |
All append-only |
coordinator-progress.txt |
PM |
All append-only |
{agent}-progress.txt |
Respective agent |
PM may append notes |
Key Changes in v2.0
OLD (v1.x):
- Workers read prd.json (110KB) for status
- Workers updated prd.json.agents.{role} directly
NEW (v2.0):
- Workers read ONLY their ~1KB state file
- Workers update ONLY their state file
- PM reads all state files and syncs to prd.json
- Workers NEVER read prd.json (saves 109KB per worker read)
What Each Agent CAN Write To
PM Coordinator
| Can Write |
Notes |
.claude/session/* |
All session files |
prd.json |
FULL ACCESS - Task status fields: passes, status, assignedAt, assignedTo, completedAt |
prd.json.session |
Full ownership of session state |
prd.json.agents.* |
Full ownership of agent status tracking |
current-task-pm.json |
Full ownership of coordinator state |
current-task-*.md |
Reads all worker state files, WRITES to update worker task assignments |
coordinator-progress.txt |
Full ownership |
developer-progress.txt |
May append notes |
qa-progress.txt |
May append notes |
| ❌ Source code |
Read-only |
| ❌ Test files |
Read-only |
| ❌ Config files |
Read-only |
Developer Worker
| Can Write |
Notes |
.claude/session/session.log |
Append log entries |
.claude/session/developer-progress.txt |
Full ownership |
current-task-developer.json |
PRIMARY STATE FILE - Update: status, lastSeen, currentTaskId |
❌ prd.json |
DO NOT READ (110KB - PM only) |
Source files (src/, etc.) |
Full ownership |
❌ coordinator-progress.txt |
Read-only |
❌ qa-progress.txt |
Read-only |
QA Worker
| Can Write |
Notes |
.claude/session/session.log |
Append log entries |
.claude/session/qa-progress.txt |
Full ownership |
current-task-qa.json |
PRIMARY STATE FILE - Update: status, lastSeen, currentTaskId, passes |
❌ prd.json |
DO NOT READ (110KB - PM only) |
Test files (src/tests/, tests/e2e/) |
Full ownership |
| ❌ Source code |
Read-only (validates only) |
❌ coordinator-progress.txt |
Read-only |
❌ developer-progress.txt |
Read-only |
Game Designer Worker
| Can Write |
Notes |
.claude/session/session.log |
Append log entries |
.claude/session/gamedesigner-progress.txt |
Full ownership |
current-task-gamedesigner.json |
PRIMARY STATE FILE - Update: status, lastSeen, currentTaskId |
docs/design/ |
Full ownership of design artifacts |
❌ prd.json |
DO NOT READ (110KB - PM only) |
| ❌ Source code |
Read-only |
❌ prd.json task descriptions |
Read-only (PM only) |
Tech Artist Worker
| Can Write |
Notes |
.claude/session/session.log |
Append log entries |
.claude/session/techartist-progress.txt |
Full ownership |
current-task-techartist.json |
PRIMARY STATE FILE - Update: status, lastSeen, currentTaskId |
src/assets/ |
All 3D models, textures, materials |
src/components/**/*.{materials,shaders,effects}* |
Visual components |
src/styles/ |
UI styles |
src/vfx/ |
Particle systems |
❌ prd.json |
DO NOT READ (110KB - PM only) |
| ❌ Core game logic |
Read-only (store/, hooks/, utils/) |
| ❌ Network code |
Read-only (server/) |
Commit Permissions
ALL agents MUST commit their file changes.
| Agent |
Must Commit |
Commit Scope |
| PM |
✅ Yes |
prd.json, current-task-*.md (all), .claude/session/coordinator-progress.txt, skill files, retrospectives |
| Developer |
✅ Yes |
Source files, tests, current-task-developer.json, own progress files |
| Tech Artist |
✅ Yes |
Assets, shaders, visual components, current-task-techartist.json, own progress files |
| QA |
✅ Yes |
current-task-qa.json, bug reports, own progress files, test files |
| Game Designer |
✅ Yes |
docs/design/, GDD, current-task-gamedesigner.json, own progress files |
Commit Format (from shared-core):
[ralph] [{AGENT}] {TASK_ID}: {Brief description}
- Change 1
- Change 2
PRD: {TASK_ID} | Agent: {AGENT} | Iteration: {N}
No Commit Exceptions:
- Heartbeat updates (
prd.json.agents.{agent}.lastSeen timestamps)
- Temporary message files (deleted after processing)
Atomic Updates Using Edit Tool
The Edit tool handles atomic writes automatically. No manual temp file pattern needed.
Using Edit Tool
Step 1: Read the file
Read: prd.json
Step 2: Use Edit tool to make changes
Edit: prd.json
Replace: "status": "pending"
With: "status": "assigned"
The Edit tool internally:
- Reads the file
- Applies your changes
- Writes atomically (temp file + rename)
Edit Tool vs Manual Pattern
| Approach |
Lines of Code |
Safe? |
| Manual temp file |
5+ lines |
Risky if interrupted |
| Edit tool |
1 call |
✅ Safe |
❌ DO NOT use manual temp file pattern:
# Don't do this anymore
echo "$NEW_STATE" > file.json.tmp && mv file.json.tmp file.json
✅ DO use Edit tool instead:
Read: file.json
Edit: file.json
Master Branch Coordination (Worktree System)
When working in a git worktree, ALL coordination files must be updated in the master branch.
Worktree system uses isolated branches for code/assets, but ALL state coordination must happen in master branch to ensure:
- PM sees worker status immediately
- Watchdog monitors heartbeats
- Message queue works for all agents
What Goes to Master Branch:
| File |
Purpose |
prd.json |
Task status, agent status, session state |
.claude/session/messages/ |
Event queue |
.claude/session/*.json |
State files |
What Goes to Worktree Branch:
| Directory |
Purpose |
src/ |
Code changes |
src/assets/ |
Asset changes |
From worktree, access master branch:
Read: ../agentic-threejs/prd.json
Edit: ../agentic-threejs/prd.json
Concurrency Rules
- Read-modify-write atomically — Use Edit tool
- Only update fields you own — Never overwrite another agent's data
- Append-only for logs — Never delete or reorder entries
- Retry on conflict — If write fails, re-read and retry once
- Use per-agent files for frequently updated data
Conflict Resolution
If you encounter a write conflict:
- Re-read the file — Get latest state
- Re-apply your changes — On top of new state
- Write again — Using Edit tool
- If still conflicts — Log issue, wait 30 seconds
When Atomic Updates Matter Most
- High-contention files —
prd.json (session state, agent status, task items)
- Shared log files — Use append-only instead
- PRD file — Multiple agents may update different fields
When Atomic Updates Don't Apply
- Agent-specific files —
{agent}-progress.txt (only one writer)
- Append-only logs — Don't need atomic pattern
- New file creation — No existing content to protect
Logging Best Practices
- Use structured logs — JSON format where possible
- Include timestamps — ISO 8601 format
- Include agent identifier — Who made the change
- Append only — Never rewrite log files
- Archive, don't delete — PM may archive old logs
Anti-Patterns
| Don't |
Do Instead |
| Use PowerShell Get-Content/Set-Content |
Use Read/Write tools |
| Manual temp file + Move-Item |
Use Edit tool |
| Overwrite entire files |
Update specific fields only |
| Modify other agents' sections |
Only update your own |
| Delete log entries |
Append only |
References
shared-core — Commit format, session structure
shared-messaging — Message queue ownership
shared-worktree — Git worktree setup
1---2name: shared-state3description: File ownership, atomic updates using Edit tool, concurrency rules for Ralph agents4---5
6# Shared State
7
8> "Single-writer principle prevents race conditions. Use Edit tool for atomic updates."
9
10---
11
12## Core Principle
13
14Each file has a **primary owner** to avoid conflicts. Only the owner may write to that file.
15
16---
17
18## File Ownership Matrix (v2.0 - Per-Agent State Files)
19
20### State Files
21
22| File | Primary Owner | Other Agents | Size | Purpose |
23| ----------------------------- | --------------- | --------------------- | ----- | ------------------------------ |
24| `current-task-developer.json` | Developer | PM (read-only) | ~1KB | Developer's task state |
25| `current-task-qa.json` | QA | PM (read-only) | ~1KB | QA's task state |
26| `current-task-techartist.json` | Tech Artist | PM (read-only) | ~1KB | Tech Artist's task state |
27| `current-task-gamedesigner.json | Game Designer | PM (read-only) | ~1KB | Game Designer's task state |
28| `current-task-pm.json` | PM | Workers (read-only) | ~2KB | PM coordinator state |
29| `prd.json` | PM (full access) | **Workers DO NOT read** | 110KB | Full PRD (PM-ONLY in v2.0) |
30
31### Session Files
32
33| File | Primary Owner | Other Agents |
34| -------------------------- | ---------------- | ------------------------------------------------------------------------------- |
35| `prd.json.session` | PM | Workers check their state file for sessionStatus (NOT prd.json directly) |
36| `prd.json.items[{taskId}]` | PM (creates) | Workers do NOT update directly - PM updates based on messages |
37| `prd.json` | PM (fields) | Workers DO NOT read in v2.0 - PM handles all PRD updates |
38| `session.log` | All agents | All append-only |
39| `coordinator-progress.txt` | PM | All append-only |
40| `{agent}-progress.txt` | Respective agent | PM may append notes |
41
42### Key Changes in v2.0
43
44**OLD (v1.x):**
45- Workers read prd.json (110KB) for status
46- Workers updated prd.json.agents.{role} directly
47
48**NEW (v2.0):**
49- Workers read ONLY their ~1KB state file
50- Workers update ONLY their state file
51- PM reads all state files and syncs to prd.json
52- Workers NEVER read prd.json (saves 109KB per worker read)
53
54---
55
56## What Each Agent CAN Write To
57
58### PM Coordinator
59
60| Can Write | Notes |
61| -------------------------- | --------------------------------------------------------------------------------- |
62| `.claude/session/*` | All session files |
63| `prd.json` | **FULL ACCESS** - Task status fields: `passes`, `status`, `assignedAt`, `assignedTo`, `completedAt` |
64| `prd.json.session` | Full ownership of session state |
65| `prd.json.agents.*` | Full ownership of agent status tracking |
66| `current-task-pm.json` | Full ownership of coordinator state |
67| `current-task-*.md` | **Reads all** worker state files, **WRITES** to update worker task assignments |
68| `coordinator-progress.txt` | Full ownership |
69| `developer-progress.txt` | May append notes |
70| `qa-progress.txt` | May append notes |
71| ❌ Source code | Read-only |
72| ❌ Test files | Read-only |
73| ❌ Config files | Read-only |
74
75### Developer Worker
76
77| Can Write | Notes |
78| ---------------------------------------- | ------------------------------------------------------------- |
79| `.claude/session/session.log` | Append log entries |
80| `.claude/session/developer-progress.txt` | Full ownership |
81| `current-task-developer.json` | **PRIMARY STATE FILE** - Update: `status`, `lastSeen`, `currentTaskId` |
82| ❌ `prd.json` | **DO NOT READ** (110KB - PM only) |
83| Source files (`src/`, etc.) | Full ownership |
84| ❌ `coordinator-progress.txt` | Read-only |
85| ❌ `qa-progress.txt` | Read-only |
86
87### QA Worker
88
89| Can Write | Notes |
90| --------------------------------- | ---------------------------------------------------- |
91| `.claude/session/session.log` | Append log entries |
92| `.claude/session/qa-progress.txt` | Full ownership |
93| `current-task-qa.json` | **PRIMARY STATE FILE** - Update: `status`, `lastSeen`, `currentTaskId`, `passes` |
94| ❌ `prd.json` | **DO NOT READ** (110KB - PM only) |
95| Test files (`src/tests/`, `tests/e2e/`) | Full ownership |
96| ❌ Source code | Read-only (validates only) |
97| ❌ `coordinator-progress.txt` | Read-only |
98| ❌ `developer-progress.txt` | Read-only |
99
100### Game Designer Worker
101
102| Can Write | Notes |
103| ------------------------------------------- | ----------------------------------------------- |
104| `.claude/session/session.log` | Append log entries |
105| `.claude/session/gamedesigner-progress.txt` | Full ownership |
106| `current-task-gamedesigner.json` | **PRIMARY STATE FILE** - Update: `status`, `lastSeen`, `currentTaskId` |
107| `docs/design/` | Full ownership of design artifacts |
108| ❌ `prd.json` | **DO NOT READ** (110KB - PM only) |
109| ❌ Source code | Read-only |
110| ❌ `prd.json` task descriptions | Read-only (PM only) |
111
112### Tech Artist Worker
113
114| Can Write | Notes |
115| -------------------------------------------------- | ----------------------------------------------- |
116| `.claude/session/session.log` | Append log entries |
117| `.claude/session/techartist-progress.txt` | Full ownership |
118| `current-task-techartist.json` | **PRIMARY STATE FILE** - Update: `status`, `lastSeen`, `currentTaskId` |
119| `src/assets/` | All 3D models, textures, materials |
120| `src/components/**/*.{materials,shaders,effects}*` | Visual components |
121| `src/styles/` | UI styles |
122| `src/vfx/` | Particle systems |
123| ❌ `prd.json` | **DO NOT READ** (110KB - PM only) |
124| ❌ Core game logic | Read-only (store/, hooks/, utils/) |
125| ❌ Network code | Read-only (server/) |
126
127---
128
129## Commit Permissions
130
131**ALL agents MUST commit their file changes.**
132
133| Agent | Must Commit | Commit Scope |
134| ----------------- | ----------- | ------------------------------------------------------------------------------------------- |
135| **PM** | ✅ Yes | `prd.json`, `current-task-*.md` (all), `.claude/session/coordinator-progress.txt`, skill files, retrospectives |
136| **Developer** | ✅ Yes | Source files, tests, `current-task-developer.json`, own progress files |
137| **Tech Artist** | ✅ Yes | Assets, shaders, visual components, `current-task-techartist.json`, own progress files |
138| **QA** | ✅ Yes | `current-task-qa.json`, bug reports, own progress files, test files |
139| **Game Designer** | ✅ Yes | `docs/design/`, GDD, `current-task-gamedesigner.json`, own progress files |
140
141**Commit Format** (from `shared-core`):
142
143```
144[ralph] [{AGENT}] {TASK_ID}: {Brief description}
145
146- Change 1
147- Change 2
148
149PRD: {TASK_ID} | Agent: {AGENT} | Iteration: {N}
150```
151
152**No Commit Exceptions:**
153
154- Heartbeat updates (`prd.json.agents.{agent}.lastSeen` timestamps)
155- Temporary message files (deleted after processing)
156
157---
158
159## Atomic Updates Using Edit Tool
160
161**The Edit tool handles atomic writes automatically.** No manual temp file pattern needed.
162
163### Using Edit Tool
164
165**Step 1:** Read the file
166
167```
168Read: prd.json
169```
170
171**Step 2:** Use Edit tool to make changes
172
173```
174Edit: prd.json
175 Replace: "status": "pending"
176 With: "status": "assigned"
177```
178
179The Edit tool internally:
180
1811. Reads the file
1822. Applies your changes
1833. Writes atomically (temp file + rename)
184
185### Edit Tool vs Manual Pattern
186
187| Approach | Lines of Code | Safe? |
188| ---------------- | ------------- | -------------------- |
189| Manual temp file | 5+ lines | Risky if interrupted |
190| **Edit tool** | 1 call | ✅ Safe |
191
192**❌ DO NOT use manual temp file pattern:**
193
194```bash
195# Don't do this anymore
196echo "$NEW_STATE" > file.json.tmp && mv file.json.tmp file.json
197```
198
199**✅ DO use Edit tool instead:**
200
201```
202Read: file.json
203Edit: file.json
204```
205
206### Master Branch Coordination (Worktree System)
207
208**When working in a git worktree, ALL coordination files must be updated in the master branch.**
209
210Worktree system uses isolated branches for code/assets, but ALL state coordination must happen in master branch to ensure:
211
212- PM sees worker status immediately
213- Watchdog monitors heartbeats
214- Message queue works for all agents
215
216**What Goes to Master Branch:**
217| File | Purpose |
218|------|---------|
219| `prd.json` | Task status, agent status, session state |
220| `.claude/session/messages/` | Event queue |
221| `.claude/session/*.json` | State files |
222
223**What Goes to Worktree Branch:**
224| Directory | Purpose |
225|-----------|---------|
226| `src/` | Code changes |
227| `src/assets/` | Asset changes |
228
229**From worktree, access master branch:**
230
231```
232Read: ../agentic-threejs/prd.json
233Edit: ../agentic-threejs/prd.json
234```
235
236---
237
238## Concurrency Rules
239
2401. **Read-modify-write atomically** — Use Edit tool
2412. **Only update fields you own** — Never overwrite another agent's data
2423. **Append-only for logs** — Never delete or reorder entries
2434. **Retry on conflict** — If write fails, re-read and retry once
2445. **Use per-agent files** for frequently updated data
245
246---
247
248## Conflict Resolution
249
250If you encounter a write conflict:
251
2521. **Re-read the file** — Get latest state
2532. **Re-apply your changes** — On top of new state
2543. **Write again** — Using Edit tool
2554. **If still conflicts** — Log issue, wait 30 seconds
256
257---
258
259## When Atomic Updates Matter Most
260
261- **High-contention files** — `prd.json` (session state, agent status, task items)
262- **Shared log files** — Use append-only instead
263- **PRD file** — Multiple agents may update different fields
264
265## When Atomic Updates Don't Apply
266
267- **Agent-specific files** — `{agent}-progress.txt` (only one writer)
268- **Append-only logs** — Don't need atomic pattern
269- **New file creation** — No existing content to protect
270
271---
272
273## Logging Best Practices
274
275- **Use structured logs** — JSON format where possible
276- **Include timestamps** — ISO 8601 format
277- **Include agent identifier** — Who made the change
278- **Append only** — Never rewrite log files
279- **Archive, don't delete** — PM may archive old logs
280
281---
282
283## Anti-Patterns
284
285| Don't | Do Instead |
286| -------------------------------------- | --------------------------- |
287| Use PowerShell Get-Content/Set-Content | Use Read/Write tools |
288| Manual temp file + Move-Item | Use Edit tool |
289| Overwrite entire files | Update specific fields only |
290| Modify other agents' sections | Only update your own |
291| Delete log entries | Append only |
292
293---
294
295## References
296
297- `shared-core` — Commit format, session structure
298- `shared-messaging` — Message queue ownership
299- `shared-worktree` — Git worktree setup