AI Wikis / Agentic Web
04-memoryendpoints-global-agent-and-memory-leases.md
Report summary
The proliferation of distributed agent swarms—where multiple autonomous agents execute concurrently on high-performance compute nodes or across global regions—necessitates a mathematically rigorous, mathematically provable ownership protocol. Within the MemoryEndpoints.com ecosystem, the LocalEndpoi
Key topics
- AI Wikis / Agentic Web
- AI Wikis
- Agentic Web
- AI
- UAI
- AI Memory
- GEO
- SQL
- LocalEndpoint
Research provenance
For citation, use the report title and canonical URL. Archival presence does not establish authorship or promote report statements into portfolio evidence.
This page renders the archived Markdown as safe, formatted HTML. It is background research and does not become a portfolio claim without evidence review.
Full report
On this page
Executive Summary
The proliferation of distributed agent swarms—where multiple autonomous agents execute concurrently on high-performance compute nodes or across global regions—necessitates a mathematically rigorous, mathematically provable ownership protocol. Within the MemoryEndpoints.com ecosystem, the LocalEndpoint Connect architecture supports multiple independent agent instances per host. Relying on host-level mutexes, workspace-scoped constraints, or coarse-grained account locks drastically diminishes concurrency and fails to protect the integrity of the underlying .uai memory instances. This technical report defines a globally authoritative protocol for atomic agent-name and memory-instance ownership. The architecture enforces service-wide mutual exclusion over logical .uai memory instances and canonical agent names without relying on human-in-the-loop approval workflows. By integrating the IETF Idempotency-Key draft specification, the protocol guarantees safe client recovery and exactly-once execution semantics across unreliable networks1. Furthermore, the protocol incorporates the PRECIS (Preparation, Enforcement, and Comparison of Internationalized Strings) framework alongside Unicode Technical Standard (UTS) \#39 to eliminate homograph spoofing and ensure robust global namespace canonicalization3. To prevent the "phantom write" phenomenon and protect against silent data corruption during network partitions or garbage-collection pauses, ownership is governed through bounded, timestamp-based leases enforced by monotonically increasing fencing tokens5. These tokens physically sever stale writers from the data plane, ensuring that expired leaseholders cannot mutate state. The resulting persistence model and HTTP API specification deliver a fault-tolerant, highly concurrent architecture explicitly designed for multi-region distributed databases such as PostgreSQL, CockroachDB, and Google Spanner.
Formal Invariants, Safety Properties, and Liveness Properties
The correctness of the MemoryEndpoints protocol is predicated on strict adherence to a defined set of mathematical boundaries. These invariants must hold irrespective of hardware failures, network partitions, split-brain scenarios, or arbitrary client behavior.
Formal Invariants
- Global Canonical Name Uniqueness: Two simultaneously active agents may never share the same canonicalized agent name. This constraint holds universally across all workspaces, accounts, host computers, regions, and processes. The workspace abstraction is utilized strictly for access control and billing, never as a namespace escape.
- Singular Memory Attachment: Two independent agents may never simultaneously attach to, lease, or mutate the same memory\_instance\_id. This isolation is enforced regardless of whether the agents reside in differing workspaces or on differing host machines.
- Immutable Identity: A memory instance's cryptographic and logical identity (memory\_instance\_id) remains strictly invariant during file copies, filesystem moves, or local path renames.
- Explicit Lineage on Branching: A deliberate branch operation generates a novel, immutable memory\_instance\_id and deterministically records its parent's lineage. A copied file is not recognized as a branch by the control plane.
- Concurrent Branch Execution: A source memory instance and its true branch constitute entirely distinct logical entities. They may execute concurrently and be leased under different active agent names without inducing locking contention.
- Atomic Acquisition: Agent-name ownership and memory-instance ownership must be claimed simultaneously within a single indivisible database transaction.
- Maximum Concurrency: Distinct canonical names accessing distinct memory identities must execute fully concurrently, unimpeded by global application locks.
- Asynchronous Expiration: Owners that crash, hang, or become partitioned automatically lose ownership upon lease expiration, requiring no manual intervention or heartbeat termination.
- Strict Fencing: A stale owner whose lease has expired or been forcefully usurped is mathematically fenced from executing all subsequent data-plane mutations.
- Automatic Adjudication: Concurrency conflicts are resolved deterministically by the system's transaction isolation mechanisms; no human-approval workflows are utilized.
- Content Privacy: Control-plane ownership acquisition strictly utilizes logical identifiers. Raw .uai contents and local file paths are never transmitted to the control plane merely to establish ownership leases.
Safety Properties
Safety properties ensure that catastrophic or contradictory states never occur within the system.
| Property | Description | Enforcement Mechanism |
|---|---|---|
| Mutual Exclusion | For any given timestamp [Figure omitted from source export] evaluated at the database, the cardinality of active owners for a canonical name [Figure omitted from source export] or a memory identity [Figure omitted from source export] is [Figure omitted from source export]. | Relational UNIQUE constraints and SELECT FOR UPDATE atomic row-level locks evaluated against database server time. |
| Fencing Monotonicity | Every lease acquisition or takeover generates a fencing token [Figure omitted from source export]. For any memory [Figure omitted from source export], the sequence of tokens is strictly increasing: [Figure omitted from source export]7. | Database transaction increments the previous token value. Data-plane storage rejects writes where [Figure omitted from source export]. |
| Idempotent Safety | A duplicate request (identical payload, identical idempotency key) never duplicates side-effects and always returns the cached outcome of the initial successful execution9. | Conformance to the IETF Idempotency-Key draft standard with a 24-hour persistence window1. |
Liveness Properties
Liveness properties ensure that the system continually makes progress and does not succumb to livelocks or permanent deadlocks.
| Property | Description | Progress Guarantee |
|---|---|---|
| Eventual Takeover | If a leaseholding agent crashes permanently, the lease will expire at exactly [Figure omitted from source export]. At any time [Figure omitted from source export], a competing agent is guaranteed to successfully acquire the lease. | Time-To-Live (TTL) timestamps embedded in the lease rows, actively evaluated by the server clock. |
| Progress under Contention | Optimistic concurrency control combined with exponential backoff ensures that competing requests for the same unleased resource will eventually result in a victor11. | Database conflict resolution, specifically 40001 Serialization Failure handlers, combined with randomized jitter in client retries12. |
State Machines
The lifecycle of agents, memories, and their corresponding leases are governed by four deterministic state machines.
1. Ownership Session State Machine
The ownership session state machine tracks the overarching lifecycle of a client's interaction with the control plane, independent of the specific leases it holds.
| Current State | Triggering Action | Target State | Condition / Consequence |
|---|---|---|---|
| DISCONNECTED | Client authenticates and initiates session via Connect. | AUTHENTICATED | Ephemeral session token granted. |
| AUTHENTICATED | Client submits POST /v1/leases. | ACQUIRING | Enters transactional idempotency barrier. |
| ACQUIRING | Transaction succeeds. | ESTABLISHED | Client receives fencing token and TTL. |
| ACQUIRING | Transaction fails (Conflict, Invalid Name). | AUTHENTICATED | Client receives HTTP 409 or 422\. |
| ESTABLISHED | Client fails to renew before TTL. | TERMINATED | Session invalidation; local agent must halt. |
2. Agent-Name Lease State Machine
The agent-name lease governs the global uniqueness of the canonical name.
| Current State | Triggering Action | Target State | Condition / Consequence |
|---|---|---|---|
| UNREGISTERED | Agent requests novel name. | AVAILABLE | Canonicalization pipeline executed and passed. |
| AVAILABLE | Agent acquires name atomically. | ACTIVE | Bounded TTL assigned. Name locked globally. |
| ACTIVE | Leasing agent calls renew. | ACTIVE | TTL timestamp extended. |
| ACTIVE | Database wall-clock exceeds [Figure omitted from source export]. | EXPIRED | Passive transition. System evaluates row as available. |
| ACTIVE | Leasing agent calls release. | RELEASED | Name immediately available for takeover. |
| EXPIRED | Competing agent calls claim. | ACTIVE | Fencing token incremented. Old owner fenced. |
3. Memory Lease State Machine
The memory lease enforces singular attachment to an immutable .uai file.
| Current State | Triggering Action | Target State | Condition / Consequence |
|---|---|---|---|
| UNREGISTERED | Agent registers new memory\_id. | AVAILABLE | UUIDv7 identity persisted. |
| AVAILABLE | Agent claims memory atomically. | ACTIVE | Bounded TTL assigned. Memory locked. |
| ACTIVE | Leasing agent calls renew. | ACTIVE | TTL timestamp extended. |
| ACTIVE | Database wall-clock exceeds [Figure omitted from source export]. | EXPIRED | Memory available for external takeover. |
| EXPIRED | Competing agent calls claim. | ACTIVE | Fencing token incremented. Old agent fenced. |
4. Branch Identity State Machine
Branch identities represent diverging memory states with immutable lineage.
| Current State | Triggering Action | Target State | Condition / Consequence |
|---|---|---|---|
| NULL | Agent calls branch on parent. | INSTANTIATED | New UUIDv7 generated. Parent lineage recorded. |
| INSTANTIATED | Agent begins writing to branch. | DIVERGED | Branch operates as a completely distinct memory identity. |
| DIVERGED | Original parent is deleted. | DIVERGED | Branch survives. Lineage points to tombstoned parent. |
Recommended Relational Database Schema
The persistence layer serves as the ultimate arbiter of concurrency. To maximize B-Tree index locality, minimize page splits, and avoid write amplification, all primary keys utilize UUIDv7 rather than the highly fragmented UUIDv414. UUIDv7 embeds a 48-bit UNIX timestamp, allowing inserts to append sequentially to the rightmost leaf of the B-Tree, yielding substantial performance improvements at scale15.
Table Definitions
SQL \-- Enable UUIDv7 generation (PostgreSQL 16+ native, or via extension) CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
\-- 1\. Memory Instances \-- Defines the immutable logical identity of a memory instance. CREATE TABLE memory\_instances ( memory\_id UUID PRIMARY KEY, \-- Strictly UUIDv7 tenant\_id UUID NOT NULL, parent\_memory\_id UUID REFERENCES memory\_instances(memory\_id), branched\_at\_token BIGINT, created\_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); CREATE INDEX idx\_memory\_tenant ON memory\_instances(tenant\_id);
\-- 2\. Agent Names \-- Enforces canonical name uniqueness service-wide. CREATE TABLE agent\_names ( canonical\_name TEXT PRIMARY KEY, display\_name TEXT NOT NULL, tenant\_id UUID NOT NULL, created\_at TIMESTAMPTZ NOT NULL DEFAULT NOW() );
\-- 3\. Active Leases (Unified Name and Memory Lease) \-- The unified lease table ensuring atomic ownership of both resources. CREATE TABLE active\_leases ( lease\_id UUID PRIMARY KEY DEFAULT gen\_random\_uuid(), tenant\_id UUID NOT NULL, canonical\_name TEXT NOT NULL UNIQUE REFERENCES agent\_names(canonical\_name), memory\_id UUID NOT NULL UNIQUE REFERENCES memory\_instances(memory\_id), fencing\_token BIGINT NOT NULL DEFAULT 1, expires\_at TIMESTAMPTZ NOT NULL, owner\_client\_id TEXT NOT NULL, idempotency\_key VARCHAR(255) ); CREATE INDEX idx\_active\_leases\_expiry ON active\_leases(expires\_at);
\-- 4\. Idempotency Records \-- IETF Idempotency-Key persistence CREATE TABLE idempotency\_records ( idempotency\_key VARCHAR(255) PRIMARY KEY, tenant\_id UUID NOT NULL, request\_fingerprint TEXT NOT NULL, status TEXT NOT NULL CHECK (status IN ('PROCESSING', 'COMPLETED', 'FAILED')), response\_code INT, response\_body JSONB, expires\_at TIMESTAMPTZ NOT NULL ); CREATE INDEX idx\_idemp\_expiry ON idempotency\_records(expires\_at);
\-- 5\. Audit Events \-- Redacted audit trail for security and abuse monitoring CREATE TABLE audit\_events ( event\_id UUID PRIMARY KEY, \-- UUIDv7 tenant\_id UUID NOT NULL, event\_type TEXT NOT NULL, redacted\_name\_hash TEXT NOT NULL, redacted\_memory\_hash TEXT NOT NULL, created\_at TIMESTAMPTZ NOT NULL DEFAULT NOW() );
Schema Analysis
The architecture employs a unified active\_leases table. By maintaining a single table with UNIQUE constraints on both canonical\_name and memory\_id, the system natively fulfills the invariant that an agent must claim the name and memory atomically. If an agent attempts to claim an available name but a locked memory, the entire transaction rolls back, preventing distributed partial states17. The fencing\_token is stored directly within the lease row and is mathematically incremented upon takeover, avoiding the bottleneck of global sequences18.
Transaction Algorithms and SQL Pseudocode
The following algorithms define the required operations. To prevent Time-of-Check to Time-of-Use (TOCTOU) races, operations are enclosed in strict transactional boundaries.
1. Create/Register Memory
SQL BEGIN; INSERT INTO memory\_instances (memory\_id, tenant\_id) VALUES ($1, $2); COMMIT;
2. Create Branch
The branch creation preserves lineage by linking to the parent and capturing the specific fencing token under which the branch was executed, ensuring exact causality18.
SQL BEGIN; INSERT INTO memory\_instances (memory\_id, tenant\_id, parent\_memory\_id, branched\_at\_token) VALUES ($1, $2, $3, $4); COMMIT;
3. Atomic Claim
This operation incorporates the Idempotency barrier and the lease acquisition. The ON CONFLICT DO UPDATE clause acts as an atomic write-lock, guaranteeing that only the victor of a race condition successfully acquires the lease10.
SQL BEGIN;
\-- Step 1: Idempotency Barrier INSERT INTO idempotency\_records (idempotency\_key, tenant\_id, request\_fingerprint, status, expires\_at) VALUES ($1, $2, $3, 'PROCESSING', NOW() \+ INTERVAL '24 hours') ON CONFLICT (idempotency\_key) DO NOTHING;
\-- If rows\_affected \== 0, query existing status. \-- If 'COMPLETED', return cached response\_body. If 'PROCESSING', return 409 Conflict.
\-- Step 2: Ensure agent name exists INSERT INTO agent\_names (canonical\_name, display\_name, tenant\_id) VALUES ($4, $5, $2) ON CONFLICT DO NOTHING;
\-- Step 3: Atomic Lease Upsert INSERT INTO active\_leases (tenant\_id, canonical\_name, memory\_id, fencing\_token, expires\_at, owner\_client\_id, idempotency\_key) VALUES ($2, $4, $6, 1, NOW() \+ INTERVAL '30 seconds', $7, $1) ON CONFLICT (canonical\_name) DO UPDATE SET owner\_client\_id \= EXCLUDED.owner\_client\_id, memory\_id \= EXCLUDED.memory\_id, fencing\_token \= active\_leases.fencing\_token \+ 1, expires\_at \= EXCLUDED.expires\_at, idempotency\_key \= EXCLUDED.idempotency\_key WHERE active\_leases.expires\_at \< NOW() OR active\_leases.owner\_client\_id \= EXCLUDED.owner\_client\_id RETURNING fencing\_token, expires\_at;
\-- Step 4: Finalize Idempotency Record UPDATE idempotency\_records SET status \= 'COMPLETED', response\_code \= 201, response\_body \= $JSON WHERE idempotency\_key \= $1;
COMMIT;
4. Exact-Owner Idempotent Retry
If an agent restarts and transmits the exact same request body and Idempotency-Key, Step 1 of the Atomic Claim algorithm detects the existing key. The database returns the previously generated JSON body containing the active fencing\_token, averting the generation of a redundant lease or an erroneous 423 Locked rejection20.
5. Renew
Renewal requires the client to prove current ownership via the fencing\_token.
SQL BEGIN; UPDATE active\_leases SET expires\_at \= NOW() \+ INTERVAL '30 seconds' WHERE canonical\_name \= $1 AND memory\_id \= $2 AND fencing\_token \= $3 AND owner\_client\_id \= $4 AND expires\_at \>= NOW() RETURNING expires\_at; \-- If rows\_affected \== 0, return 409 Conflict (Stale Fence) or 410 Gone (Expired). COMMIT;
6. Release
SQL BEGIN; DELETE FROM active\_leases WHERE canonical\_name \= $1 AND memory\_id \= $2 AND fencing\_token \= $3 AND owner\_client\_id \= $4; COMMIT;
7. Expiry Takeover
Expiry takeover is fundamentally handled by the WHERE active\_leases.expires\_at \< NOW() clause within the Atomic Claim algorithm. The database server's strictly monotonic clock governs this transition, neutralizing any client-side clock skew vulnerabilities6.
8. Fenced Mutation
When syncing .uai data to the final storage medium (the Data Plane), the storage service must validate the token.
SQL BEGIN; SELECT fencing\_token FROM active\_leases WHERE memory\_id \= $1; \-- Application logic: If requested\_token \< DB\_token, REJECT. \-- Proceed with data mutation... COMMIT;
9. Conflict Cleanup
Stale idempotency records are systematically purged via a background asynchronous worker.
SQL DELETE FROM idempotency\_records WHERE expires\_at \< NOW();
Isolation-Level Analysis
The required consistency level depends entirely on the underlying distributed database engine deployed for MemoryEndpoints.
PostgreSQL (READ COMMITTED vs. SERIALIZABLE)
In PostgreSQL, the ON CONFLICT DO UPDATE clause acts as an atomic, row-level write-lock. Under READ COMMITTED isolation, PostgreSQL handles the serialization of the conflicted row seamlessly by re-evaluating the WHERE clause against the latest row version if a concurrent transaction modifies it12. This provides safe, highly concurrent execution without exposing the application to complex retry logic, making READ COMMITTED the optimal choice for PostgreSQL deployments.
CockroachDB (SERIALIZABLE)
CockroachDB defaults to SERIALIZABLE isolation and utilizes an optimistic concurrency control protocol. In a highly contested scenario (e.g., ten agents starting simultaneously and requesting the same name), CockroachDB may abort transactions to prevent write skew, yielding an ERROR 40001: restart transaction11.
- Implementation Requirement: The data access layer must implement an explicit exponential backoff loop catching 40001 (SerializationFailure) and re-issuing the entire transaction11. Without this loop, valid concurrent requests will permanently fail.
Google Spanner (External Consistency)
Spanner utilizes TrueTime and a commit-wait phase to ensure strict external consistency (linearizability)24. A transaction will actively wait out the clock uncertainty ([Figure omitted from source export]ms) before committing. This guarantees that if a lease expires in wall-clock time, no node in any global region will mistakenly evaluate it as active due to local clock drift26. For a multi-region deployment demanding absolute global mutual exclusion, Spanner or CockroachDB are the strictly superior architectural choices over standard PostgreSQL28.
Complete Proposed HTTP API
The API enforces strict adherence to RESTful principles and RFC 9457 structured errors. All endpoints require TLS 1.3 and Bearer token authentication.
POST /v1/leases
Description: Atomically claims an agent name and memory identity.Headers:
- Authorization: Bearer \<token\>
- Idempotency-Key: \<UUIDv7\> (Required)1
Request JSON:
JSON { "display\_name": "My Agent", "memory\_instance\_id": "018f3b3a-9b5c-7d12-bc01-9a2cfe123456", "owner\_client\_id": "worker-node-alpha-99", "ttl\_seconds": 30 }
Success (201 Created) / Cached Retry (200 OK):
JSON { "lease\_id": "018f3b3b-1a2b-7c34-de02-1a2b3c4d5e6f", "canonical\_name": "myagent", "fencing\_token": 42, "expires\_at": "2026-07-14T19:12:50Z" }
POST /v1/leases/renew
Description: Renews an active lease.Headers:
- Authorization: Bearer \<token\>
Request JSON:
JSON { "canonical\_name": "myagent", "memory\_instance\_id": "018f3b3a-9b5c-7d12-bc01-9a2cfe123456", "fencing\_token": 42, "owner\_client\_id": "worker-node-alpha-99", "ttl\_seconds": 30 }
Success (200 OK):
JSON { "expires\_at": "2026-07-14T19:13:20Z" }
POST /v1/leases/release
Description: Explicitly terminates a lease before its natural TTL expiration.Request JSON: Requires canonical\_name, memory\_instance\_id, fencing\_token, and owner\_client\_id.Success (204 No Content).
POST /v1/memories/branch
Description: Establishes a new logical memory identity branching from a parent.Request JSON:
JSON { "parent\_memory\_instance\_id": "018f3b3a-9b5c-7d12-bc01-9a2cfe123456", "parent\_fencing\_token": 42 }
Success (201 Created):
JSON { "memory\_instance\_id": "018f3b3b-9c9c-8e23-de02-9a2cfe123456" }
Stable Error Taxonomy
Machine-readable errors strictly conform to RFC 9457 (Problem Details for HTTP APIs), allowing robust programmatic remediation29.
| Error Identifier | HTTP Status | Description | Actionable Client Resolution |
|---|---|---|---|
| agent\_name\_in\_use | 423 Locked | The canonical name is actively leased by another identity. | Execute jittered backoff and retry, or prompt for a different name. |
| memory\_instance\_in\_use | 423 Locked | The memory identity is currently locked by another agent. | Execute jittered backoff and retry. |
| combined\_conflict | 423 Locked | Atomic claim failed; name and memory are held by distinct parties. | Execute jittered backoff and retry. |
| stale\_fence | 409 Conflict | The provided fencing token is lower than the active token. | Halt execution. The lease was lost to a competing takeover6. |
| expired\_lease | 410 Gone | Attempted to renew a lease that has already elapsed. | Discard local state and re-acquire via POST /v1/leases. |
| released\_lease | 410 Gone | Attempted to mutate using a released lease. | Halt execution. |
| invalid\_identity | 400 Bad Request | Name violates PRECIS/UTS \#39 rules (e.g., mixed scripts). | Prompt user to input a valid alphanumeric name. |
| idempotent\_conflict | 409 Conflict | Another request with the same Idempotency-Key is processing. | Poll until original request finishes2. |
| idempotent\_mismatch | 422 Unprocessable | Idempotency-Key reused with a different payload. | Generate a novel UUIDv7 key2. |
| unauthorized | 401 Unauthorized | Missing or invalid Bearer token. | Re-authenticate. |
| forbidden | 403 Forbidden | Tenant lacks access to the requested workspace/memory. | Validate tenant permissions. |
| rate\_limited | 429 Too Many Req. | Rate limit exceeded. | Observe Retry-After header29. |
Lease-Token and Fencing Security Requirements
A distributed lease is critically unsafe if it relies strictly on time and lacks a mechanism to reject delayed network packets. Relying solely on a TTL creates the "Phantom Write" vulnerability: if an application experiences a 30-second garbage collection pause or network partition, it may "wake up" assuming it still holds the lease and perform unauthorized mutations, corrupting the database5.
- Monotonicity: The fencing\_token is a 64-bit integer that increments strictly on every lease acquisition or takeover.
- Mandatory Propagation: The client must propagate this token to all downstream data-plane services (e.g., when syncing the .uai state to object storage or executing embeddings).
- Data-Plane Enforcement: The downstream service MUST track the highest token seen for a given memory\_id and strictly reject any mutation carrying a token [Figure omitted from source export]18.
- Decoupled Clocks: The protocol never trusts the client's wall-clock to determine lease validity. Time is authoritative solely at the database server layer.
Sequence Diagrams
To ensure precise algorithmic implementation, the following sequences detail the exact actor interactions.
| Sequence Scenario | Actor / Trigger | Control Plane Evaluation | Consequence / Data Plane Response |
|---|---|---|---|
| Successful Claim, Renew, Release | Client sends Claim via POST /v1/leases. | Name and Memory are available. Upserts active\_leases. | Returns 201 Created with Token 1\. Client calls Renew before TTL; Control Plane updates expires\_at. Client calls Release; Control Plane deletes lease row. |
| Same-Name Race | Client A and Client B simultaneously request Name X. | Database lock prioritizes Client A. Client A upsert succeeds. Client B's transaction evaluates expires\_at and fails. | Client A receives Token 1\. Client B receives 423 Locked. |
| Same-Memory Race | Client A and Client B request Memory Y. | Database lock prioritizes Client A. Client A upsert succeeds. | Client A receives Token 1\. Client B receives 423 Locked. |
| Partial Conflict Rollback | Client requests available Name X and locked Memory Y. | The unified UNIQUE constraint triggers. The database rejects the update to active\_leases. | Transaction rolls back. Name X remains entirely available. Client receives 423 Locked. |
| Client Crash & Recovery | Client requests lease, DB succeeds, network drops response. | Idempotency record is marked COMPLETED with a cached HTTP 201 body2. | Client restarts, resends request with exact same Idempotency-Key. DB returns cached 200 OK body. |
| Partition and Expiry Takeover | Client A holds lease, suffers 40-second partition. Client B requests lease. | DB evaluates [Figure omitted from source export]. Upsert overwrites Client A's lease. Fencing Token increments to 2\. | Client B receives Token 2\. |
| Stale Owner Returning | Client A recovers from partition, attempts write to Data Plane with Token 1\. | Data Plane compares Token 1 against the highest seen (Token 2). | Data Plane REJECTS the write (stale\_fence / 409 Conflict)7. |
| Concurrent Source & Branch | Agent 1 runs Source Memory. Agent 2 runs Branch Memory. | Source and Branch possess distinct memory\_ids. Name and Memory locks do not intersect. | Both execute concurrently with zero database contention. |
Unicode Canonicalization and Agent Name Specification
To achieve global uniqueness and prevent spoofing, standard ASCII case-folding is profoundly insufficient. The protocol utilizes the PRECIS framework, specifically the UsernameCaseMapped profile defined in RFC 82653, alongside Unicode Technical Standard \#39.
The Canonicalization Pipeline
- Width Mapping and Normalization: All full-width and half-width characters are decomposed, and the string is normalized to Unicode Normalization Form C (NFC)3.
- Case Folding: Uppercase characters are mapped to lowercase using Unicode Default Case Folding3.
- Invalid Character Rejection: The string is evaluated against the PRECIS IdentifierClass. Spaces, punctuation, symbols, and control characters are outright rejected, ensuring the name contains only safe alphanumeric sequences36.
- Confusable Skeleton Generation (UTS \#39): To prevent homoglyph attacks, the protocol enforces the Highly Restrictive profile, prohibiting the mixing of scripts (e.g., Latin and Cyrillic) in a single name38. Finally, the string passes through the UTS \#39 skeleton mapping algorithm, which converts confusable characters into a single, canonical representative skeleton4.
Test Vectors for Canonicalization
| Input Name | Vector Type | Output / Consequence |
|---|---|---|
| AgentOne / agentone | Case Equivalence | Both map to the canonical skeleton agentone. |
| Agent / agent | Width Equivalence | Full-width A normalizes to a. Maps to agent. |
| аpple | Mixed Script Rejection | Cyrillic а (U+0430) triggers a Highly Restrictive error38. Output: 400 Bad Request. |
| ѕсоре | Confusable Skeleton | All Cyrillic ѕсоре yields the exact same UTS \#39 skeleton as Latin scope. Attempting to register the Cyrillic after the Latin yields 423 Locked4. |
Migration Plan for Preexisting Names and Memories
Systems transitioning to this strict protocol will inherently possess legacy data violating invariants (e.g., non-normalized duplicate names).
- Normalization Phase: An asynchronous background job applies the PRECIS and UTS \#39 pipeline to all legacy display names, backfilling the canonical\_name column.
- Deterministic Conflict Resolution: Where canonical collisions manifest, the system resolves them deterministically: the entity possessing the oldest created\_at timestamp retains the canonical name. Competing entities receive an appended random UUIDv7 suffix (e.g., agentone-018f3b...).
- Identity Minting: Legacy memories lacking UUIDv7 identifiers are systematically assigned new ones, with local clients forced to re-sync their identity maps on the next connection.
- Enforcement Activation: A global configuration flag activates the active\_leases table, ensuring all future requests traverse the idempotency and fencing barrier.
Multi-Region Consistency Analysis
MemoryEndpoints must operate reliably globally. Multi-region patterns require specific database capabilities to handle cross-region consensus latency28.
- Single Primary (PostgreSQL): Offers strict consistency but introduces high cross-region latency for distant clients. During a failover event, asynchronous replication can induce split-brain scenarios if fencing tokens are not rigidly enforced at the data-plane31.
- Distributed SQL (CockroachDB/Spanner): CockroachDB utilizes Raft consensus, surviving region failures and providing strictly serializable distributed transactions40. However, clock skew exceeding \~500ms will crash nodes rather than serve inconsistent data42. Spanner utilizes Paxos and TrueTime to ensure strict external consistency (linearizability)24. A transaction waits out the TrueTime uncertainty window ([Figure omitted from source export]ms), guaranteeing that if a lease expires in wall-clock time, no node will mistakenly evaluate it as active due to clock drift26.
Recommendation: To guarantee global mutual exclusion without the risk of split-brain anomalies, CockroachDB (for multi-cloud portability) or Google Spanner (for GCP-native deployments) is strongly recommended over standard active-passive PostgreSQL setups28.
Abuse, Privacy, and Rate-Limit Analysis
- Abuse Prevention (Name Squatting): Active canonical name uniqueness strictly requires an active lease. To prevent permanent squatting by disconnected agents or malicious actors, all leases possess a hard upper-bound TTL (e.g., a maximum of 5 minutes per renewal). If an agent disconnects, the name is mathematically guaranteed to free up.
- Content Privacy: The control plane knows only abstract memory\_instance\_id UUIDs and canonical names. It has absolutely no access to the actual .uai files, memory contents, vector embeddings, or local host filesystem paths. This establishes a strict zero-knowledge security boundary.
- Rate Limiting: Enforced via standard token-bucket algorithms at the API Gateway layer, evaluated prior to database transactional execution. Standard 429 Too Many Requests responses with Retry-After headers prevent database connection pool exhaustion29.
Required Metrics and Redacted Audit Events
Observability infrastructure must track system health without violating tenant privacy or exposing raw memory identifiers to log aggregators. Required Metrics:
- lease\_renewal\_latency\_p99: High latency indicates DB contention. If latency approaches the renewal interval, false lease expirations are imminent31.
- fencing\_rejection\_rate: Spikes explicitly indicate active network partitions or extensive garbage collection pauses in the client fleet31.
- idempotency\_cache\_hit\_rate: Tracks network unreliability and client retry volumes.
Redacted Audit Events: The audit\_events table logs cryptographically hashed identifiers, not plaintext names.
JSON { "event\_type": "LEASE\_ACQUIRED", "redacted\_name\_hash": "SHA256(canonical\_name \+ tenant\_salt)", "redacted\_memory\_hash": "SHA256(memory\_id \+ tenant\_salt)" }
Rigorous Test Plan
The validation protocol must verify the system's invariants under extremely adverse network and compute conditions using a production database adapter (e.g., testing against real CockroachDB instances).
Testing Methodology
- Parallel Transactions: Launch 100+ concurrent workers attempting to claim the exact same name and memory simultaneously to validate atomic write-locks.
- Fault Injection: Utilize iptables to randomly drop TCP packets between a leaseholder and the database, forcing an artificial timeout and subsequent competitor takeover.
- Process Pauses: Simulate a zombie process using SIGSTOP and SIGCONT to force a write attempt with an expired fencing token.
30 Named Test Cases
| Case ID | Category | Scenario Description | Exact Expected Outcome |
|---|---|---|---|
| idemp\_01 | Idempotency | First valid claim request. | 201 Created, token=1, cache populated. |
| idemp\_02 | Idempotency | Network timeout simulation, identical replay. | 200 OK, exact cached body, no DB mutation30. |
| idemp\_03 | Idempotency | Two identical requests arrive at exact same millisecond. | Thread A: 201, Thread B: 409 Conflict (in-progress lock)2. |
| idemp\_04 | Idempotency | Same Idempotency-Key, different memory ID payload. | 422 Unprocessable Content20. |
| idemp\_05 | Idempotency | Valid retry utilizing an expired 25-hour-old key. | Processed as completely new request; new lease issued. |
| uni\_06 | Unicode | Claim AgentOne, attempt to claim agentone. | 423 Locked (Case equivalence). |
| uni\_07 | Unicode | Claim agent, attempt to claim Agent (Full-width). | 423 Locked (Width equivalence). |
| uni\_08 | Unicode | Attempt claim аpple (Cyrillic a). | 400 Bad Request (Mixed script rejection)38. |
| uni\_09 | Unicode | Claim scope (Latin), attempt ѕсоре (Cyrillic). | 423 Locked (Skeleton match)4. |
| uni\_10 | Unicode | Attempt claim Agent. | Trimmed to agent, succeeds if available. |
| uni\_11 | Unicode | Attempt claim Agent\!@\#. | 400 Bad Request (PRECIS exception)36. |
| uni\_12 | Unicode | Attempt claim Agent🚀. | 400 Bad Request (Emoji rejected). |
| lease\_13 | Concurrency | Clean claim of available resources. | 201 Created, token=1. |
| lease\_14 | Concurrency | Name leased by A; B requests same name, different memory. | 423 Locked on name. |
| lease\_15 | Concurrency | Memory leased by A; B requests different name, same memory. | 423 Locked on memory. |
| lease\_16 | Concurrency | B requests Name1 (available) and Memory1 (locked). | Transaction rolls back. Name1 remains available. 423 Locked. |
| lease\_17 | Concurrency | Tenant B requests Name leased by Tenant A. | 423 Locked (Enforces global uniqueness). |
| lease\_18 | Concurrency | Owner calls renew 1 second before TTL expires. | 200 OK, expires\_at successfully extended. |
| exp\_19 | Expiry | A acquires lease, sleeps past TTL. B requests lease. | 201 Created for B, token=2. |
| exp\_20 | Expiry | B requests lease 1 ms before A's TTL expires. | 423 Locked. |
| exp\_21 | Expiry | A's lease expires. A tries to call renew API. | 410 Gone. |
| exp\_22 | Expiry | A's lease expires. A calls POST /v1/leases to re-acquire. | 201 Created, token=2. |
| exp\_23 | Expiry | A calls explicit release. B immediately claims. | 201 Created for B, token=2. |
| exp\_24 | Expiry | A's lease taken by B. A attempts to release. | 409 Conflict (Stale fence). |
| mut\_25 | Fencing | A writes to Data Plane with active token. | Write accepted by storage layer. |
| mut\_26 | Fencing | A's lease taken by B (Token=2). A writes with Token=1. | Write rejected by storage layer7. |
| mut\_27 | Fencing | Adversary fabricates Token=999. | Data plane rejects (Token exceeds known lease max). |
| branch\_28 | Branching | A calls branch on active memory. | 201 Created, new UUIDv7 returned. |
| branch\_29 | Branching | A runs Source, B runs Branch simultaneously. | Both succeed fully concurrently. |
| branch\_30 | Branching | A calls branch but provides stale fencing token. | 409 Conflict (Branch request denied). |
Alternatives Considered
Local Process Mutex / Workspace-Scoped Locks A local OS-level mutex entirely fails in distributed swarms running across multiple machines. Furthermore, downgrading global uniqueness to a workspace-scoped lock violates the core system invariant. If names are only unique per workspace, an attacker could register a popular name in a different workspace, causing identity confusion and global spoofing. Unique Registration Row (No TTL) Maintaining a traditional is\_active boolean in the database without a time-to-live parameter creates permanent deadlocks. If a host hard-crashes (e.g., OOMKilled containers or severed power) without executing graceful connection teardowns, the lock is never released, requiring manual administrative intervention to unstick the agent. Unfenced TTL Leases Relying solely on a TTL expiration without a corresponding fencing token leads directly to the "Phantom Write" problem. An agent might pause due to a 30-second garbage collection cycle. During this pause, its lease expires and is subsequently granted to another agent. When the original agent awakens, it assumes it still holds the lease and writes to the .uai memory simultaneously with the new owner, silently corrupting the database state5. Monotonic fencing tokens mathematically prevent this race condition. Database Connections as Locks (PostgreSQL Session Locks) Holding an open SELECT FOR UPDATE transaction for the duration of an agent's entire lifetime exhausts database connection pools (e.g., PgBouncer limits). The proposed timestamp-and-token lease model occupies database connections only for milliseconds during the API request, allowing limitless scaling of long-running edge agents18.
Works cited
- draft-ietf-httpapi-idempotency-key-header-07, https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-idempotency-key-header
- idempotency-key-patterns | Skills Ma... \- LobeHub, https://lobehub.com/zh/skills/curiositech-windags-skills-idempotency-key-patterns
- RFC 7613 \- Preparation, Enforcement, and Comparison of Internationalized Strings Representing Usernames and Passwords \- IETF Datatracker, https://datatracker.ietf.org/doc/html/rfc7613
- Confusable Detection 101: Unicode Skeletons and Mixed-Script Checks for Your Brands |, https://www.namesilo.com/blog/en/brand-protection/confusable-detection-101-unicode-skeletons-and-mixed-script-checks-for-your-brands
- The Phantom Write Problem: Why Your Idempotency Implementation Is Silently Losing Data, https://dzone.com/articles/phantom-write-idempotency-data-loss
- Distributed Locks and Leases \- Meridian Space, https://rustycloud.org/distributed\_systems\_track/module-05-coordination/lesson-01-distributed-locks-leases.html
- lowkey: Building a Distributed Lock Service That Actually Works \- DEV Community, https://dev.to/pixperk/lowkey-building-a-distributed-lock-service-that-actually-works-38gc
- System Design: Job Scheduler (10M Jobs/day, DAG Dependencies, Effectively-Once Execution) | CrackingWalnuts, https://crackingwalnuts.com/post/job-scheduler-system-design
- draft-idempotency-header-01 \- Datatracker \- IETF, https://datatracker.ietf.org/doc/html/draft-idempotency-header-01
- Idempotency Patterns: Building Retry-Safe Distributed Systems | BackendBytes, https://backendbytes.com/articles/idempotency-patterns-distributed-systems/
- Transaction Retry Error Example \- CockroachDB, https://www.cockroachlabs.com/docs/stable/transaction-retry-error-example
- Read Committed isolation level | YugabyteDB Docs, https://docs.yugabyte.com/stable/architecture/transactions/read-committed/
- What to do when a transaction fails in CockroachDB, https://www.cockroachlabs.com/blog/what-to-do-when-a-transaction-fails-in-cockroachdb/
- UUIDv4 vs UUIDv7 in PostgreSQL \- DEV Community, https://dev.to/fazal\_mansuri\_/uuidv4-vs-uuidv7-in-postgresql-2m0l
- PostgreSQL UUID Performance: Benchmarking Random (v4) and Time-based (v7) UUIDs, https://dev.to/umangsinha12/postgresql-uuid-performance-benchmarking-random-v4-and-time-based-v7-uuids-n9b
- A deeper look at old UUIDv4 vs new UUIDv7 in PostgreSQL 18 \- credativ GmbH, https://www.credativ.de/en/blog/postgresql-en/a-deeper-look-at-old-uuidv4-vs-new-uuidv7-in-postgresql-18/
- Implementing Distributed Locks Correctly | by Alex Razkevich | Towards Dev \- Medium, https://medium.com/towardsdev/implementing-distributed-locks-correctly-5a35179422a6
- pivovarit/fencepost: PostgreSQL-backed distributed queueing and locking with fencing tokens for Java \- GitHub, https://github.com/pivovarit/fencepost
- Why we built Restate, https://restate.dev/blog/why-we-built-restate/
- Idempotency \- lemon.markets Brokerage API, https://developer.lemon.markets/docs/idempotency
- Idempotent Operations: Safe Retries in Distributed Systems \- Layrs, https://layrs.me/course/hld/08-asynchronous-processing/idempotent-operations
- Transaction Retry Error Reference \- CockroachDB, https://www.cockroachlabs.com/docs/stable/transaction-retry-error-reference
- Advanced Client-side Transaction Retries \- CockroachDB, https://www.cockroachlabs.com/docs/stable/advanced-client-side-transaction-retries
- Spanner: Google's Globally-Distributed Database, https://research.google.com/archive/spanner-osdi2012.pdf
- TrueTime and external consistency | Spanner Omni \- Google Cloud Documentation, https://docs.cloud.google.com/spanner-omni/true-time-external-consistency
- How does waiting 7ms and atomic clock help GCP spanner create external consistency? : r/AskComputerScience \- Reddit, https://www.reddit.com/r/AskComputerScience/comments/1ar84sg/how\_does\_waiting\_7ms\_and\_atomic\_clock\_help\_gcp/
- Latitude. Thinking in Spanner (Part I) | by Thomas F McGeehan V | Medium, https://medium.com/@tfmv/latitude-558dc30a500c
- TiDB vs CockroachDB (2026) Comparison Guide for Platform Teams, https://www.pingcap.com/compare/cockroachdb-vs-tidb/
- API Error Handling and Resilience: 2026 Reference Guide \- Digital Applied, https://www.digitalapplied.com/blog/api-error-handling-resilience-2026-engineering-reference
- Announcing idempot-js: Idempotency Middleware for Node.js, Bun, and Deno | roderick.dk, https://roderick.dk/posts/2026-04-06-announcing-idempot-js/
- The network partition that forced a consistency choice | by System Design with Sage, https://medium.com/@systemdesignwithsage/the-network-partition-that-forced-a-consistency-choice-29ad9b5fc533
- Distributed lock with TTL \- Stack Overflow, https://stackoverflow.com/questions/72210246/distributed-lock-with-ttl
- Unicode support \- Maddy Mail Server, https://maddy.email/internals/unicode/
- ekscrypto/SwiftEmailValidator: Proper international email validation in Swift \- GitHub, https://github.com/ekscrypto/SwiftEmailValidator
- Is it okay to normalize unicode passwords with NFC/NFD?, https://security.stackexchange.com/questions/51948/is-it-okay-to-normalize-unicode-passwords-with-nfc-nfd
- RFC 7564 \- PRECIS Framework: Preparation, Enforcement, and Comparison of Internationalized Strings in Application Protocols \- IETF Datatracker, https://datatracker.ietf.org/doc/html/rfc7564
- XEP-0106: JID Escaping \- XMPP, https://xmpp.org/extensions/xep-0106.html
- Homoglyph Attacks Explained: IDN Spoofing, Unicode Confusables, and Defenses \- Stingrai, https://www.stingrai.io/blog/homoglyph-attacks-explained
- UTS \#39: Unicode Security Mechanisms, https://www.unicode.org/reports/tr39/
- How to Configure CockroachDB for High Availability \- OneUptime, https://oneuptime.com/blog/post/2026-02-02-cockroachdb-high-availability/view
- Insights from paper: CockroachDB: The Resilient Geo-Distributed SQL Database, https://hemantkgupta.medium.com/insights-from-paper-cockroachdb-the-resilient-geo-distributed-sql-database-85cd81e4b4b1
- Observing Clock Skew ERROR: 40001 \- Restart read required \- DEV Community, https://dev.to/yugabyte/observing-clock-skew-error-40001-restart-read-required-580j