Skip to content

Commit 4f4e623

Browse files
committed
docs(memory): describe shared-file leases and request outcomes
Document cooperative lease recovery, request budgets and cancellation, outcome labels, and capacity limits. State the lack of fencing and fsync guarantees and the unverified NFS/SMB assumptions.
1 parent 1d4699c commit 4f4e623

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

‎src/memory/README.md‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,103 @@ Example:
136136
- Returns the same shape as `read_graph` (entities and relations)
137137
- Mutation tools (`create_entities`, `create_relations`, `add_observations`, `delete_entities`, `delete_observations`, `delete_relations`) emit `notifications/resources/updated` for this URI, so subscribed clients see live changes
138138

139+
## Concurrent access and recovery
140+
141+
Multiple server processes can share the same memory file. Mutations first queue
142+
within each process, then acquire the adjacent `<memory-file>.lock` directory
143+
before loading, changing, and atomically replacing the graph. Read-only tools
144+
continue to read complete snapshots without taking the write lease. No additional
145+
locking dependency is required.
146+
147+
A writer prepares a private directory containing a uniquely named owner marker,
148+
then atomically renames that nonempty directory to `<memory-file>.lock`.
149+
Publication cannot replace another nonempty generation. Recovery and release
150+
unlink only the observed generation's marker before removing the empty directory;
151+
a delayed remover cannot remove a successor's nonempty directory. Ownership
152+
checks use the writer's own marker, not a snapshot adopted from the shared path.
153+
154+
The holder refreshes its owner marker's modification time every 10 seconds. A lease
155+
with no visible refresh for 60 seconds can be reclaimed by another writer, so a
156+
crashed or forcibly killed holder does not require manual lock cleanup. Contenders
157+
use randomized exponential backoff, capped at 250 milliseconds. Acquisition
158+
timeout or cancellation fails the mutation instead of writing unlocked.
159+
160+
### Request lifetime
161+
162+
Each tool call and resource read gets one request budget
163+
(`MEMORY_REQUEST_TIMEOUT_MS`, default 30 seconds). The budget starts when the call
164+
enters the server. Queueing, lease acquisition, loading and preparing the new
165+
graph all spend it, and it is never restarted. Client cancellation, including the
166+
client's own request timeout, also ends the request. The default is below the
167+
TypeScript SDK's 60-second client timeout, so clients need no timeout
168+
configuration.
169+
170+
The caller awaits the real execution of its mutation. Outcomes are labelled at
171+
the throw site, so success means success and failure means failure:
172+
173+
- Resolved: the graph file was replaced. A signal that aborts after the final
174+
pre-rename check does not recall the rename; the call resolves with the real
175+
result, although an MCP client that already cancelled never receives it (see
176+
below).
177+
- `NOT_COMMITTED`: the graph file was definitely not replaced, so retrying is
178+
safe. Causes: a full write queue, cancellation or expiry before or while
179+
queued, lock acquisition timeout or abort, lease loss detected before the
180+
rename, or an I/O failure before the rename (load, temporary-file write).
181+
- `COMMIT_UNKNOWN`: only when the rename itself rejects. Read the graph before
182+
retrying.
183+
- Business errors (for example `Entity with name X not found`) propagate
184+
unchanged and unlabelled.
185+
186+
After a killed lease holder, early calls return `NOT_COMMITTED` and a retry
187+
succeeds once the 60-second stale threshold passes; the AI's retries complete
188+
recovery, so no call needs to wait through it. A call whose client cancelled or
189+
timed out receives no response at all (the MCP SDK drops it), so the AI should
190+
read the graph before replaying it. A caller queued behind a writer stuck in
191+
issued I/O can wait past its budget, and only its client timeout ends that wait.
192+
A dispatched replacement is not recalled: the lease stays held and renewed until
193+
issued I/O settles, and the next queued mutation starts only afterwards. Each
194+
process admits up to 256 pending mutations; further mutations fail immediately
195+
with `NOT_COMMITTED` until the queue drains.
196+
197+
If the operation already resolved (the rename succeeded after a passing
198+
ownership check), a lease loss or cleanup failure found at release is logged and
199+
the successful result is returned.
200+
201+
Before publishing a new graph, the server checks that it still owns the lease.
202+
Detected lease loss fails the operation. This is a cooperative, time-based lease,
203+
**not storage-enforced fencing**: checking ownership and renaming the data file
204+
are separate operations. Arbitrarily long process pauses, network partitions,
205+
or delayed I/O can therefore permit an old writer to resume after takeover.
206+
Atomic replacement does not eliminate that limitation, and a failed/timed-out
207+
call must not be assumed to have rolled back an already submitted filesystem
208+
operation.
209+
210+
All writers must use this generation-based lock protocol, the same data/lock
211+
directory, and the same timing policy. Do not mix it with older non-locking servers
212+
or empty-directory lock implementations. Writers need permission to create,
213+
inspect, update, and delete both lock directories and their owner markers; shared
214+
Unix deployments need compatible ownership/group permissions. Do not manually
215+
delete or touch a live lease. Access through different symlink aliases is not
216+
supported. Stable wall clocks are required even locally: a large forward clock
217+
adjustment can make a live lease appear expired.
218+
219+
A crash before publication may leave an unused `<memory-file>.lock.<random>.tmp`
220+
candidate directory. It does not block later acquisitions and is not the active
221+
lock. Normal completion and handled failures clean up their candidates; crash-only
222+
orphans can be removed after all writers have stopped.
223+
224+
NFS/SMB deployments additionally require atomic directory renames that reject a
225+
nonempty destination, coherent data and metadata visibility (including owner
226+
markers), working timestamp updates, sufficiently synchronized client clocks,
227+
and cache/latency bounds comfortably below the stale interval. The stale timeout
228+
alone is not an NFS/SMB safety guarantee. Validate the actual client, server,
229+
protocol and mount settings with independent clients before relying on this
230+
mode. The bundled local multi-process tests do **not** validate NFS/SMB, and no
231+
network-filesystem configuration has been validated by this change. FUSE is
232+
outside the supported scope. Resource-update notifications remain local to the
233+
server process performing the mutation; leases do not broadcast notifications
234+
between processes.
235+
139236
# Usage with Claude Desktop
140237

141238
### Setup
@@ -231,6 +328,7 @@ On Windows, use:
231328
```
232329

233330
- `MEMORY_FILE_PATH`: Path to the memory storage JSONL file (default: `memory.jsonl` in the server directory)
331+
- `MEMORY_REQUEST_TIMEOUT_MS`: Per-request budget in milliseconds, covering queueing, lease acquisition and processing (default: `30000`). Must be a positive integer; invalid values stop startup. The default fits the TypeScript SDK's 60-second client timeout, so no client change is needed; see [Request lifetime](#request-lifetime).
234332

235333
# VS Code Installation Instructions
236334

0 commit comments

Comments
 (0)