Mutable Store API
Use namespaces/keys for explicit mutable records and atomic operations. Metadata cannot provision runtime permissions. Keep current tenant/scope authorization, concurrency and resource lifecycle separate from a record key.
Usage boundary
Access the namespace on an explicitly configured root Cortex client. Host JWT and deployment-operator transports are distinct; validate current resource scope and do not infer authority from AuthContext metadata. See administrative operations.
Values, counters and transactions
set(namespace, key, value, userId?, metadata?) stores a supported Convex value and returns the record. get returns the raw value or null; getRecord returns the record with scope/owner/metadata/timestamps or null. Namespace/key and amount/updater inputs are validated. Numeric increments/decrements default to amount 1 and execute on the backend; missing keys or nonnumeric values are rejected.
update<T>(namespace, key, updater) reads a value, applies a local synchronous function, then writes the result. It is not a compare-and-swap around that entire read/write sequence; concurrent callers can overwrite one another. Use backend counters or a transaction when operations must be committed together.
transaction accepts set/update/delete/increment/decrement operations. Set/update requires value; counters accept amount; non-set operations require an existing value. The backend validates authority and functional operations for the whole batch before its first write, and commits within one Convex mutation. Failure rolls back the batch. The result has success, operationsExecuted and ordered results; individual entries may be records or acknowledgement objects depending on read authority. Write permission does not grant permission to reveal stored private values.
import type { Cortex } from '@cortexmemory/sdk';
export async function updateApprovedInventory(cortex: Cortex) {
const result = await cortex.mutable.transaction([
{ op: 'set', namespace: 'inventory', key: 'available', value: 20 },
{ op: 'decrement', namespace: 'inventory', key: 'available', amount: 2 },
{ op: 'set', namespace: 'inventory', key: 'last-order', value: 'order-42' },
]);
return { committed: result.success, count: result.operationsExecuted,
results: result.results };
}
list requires a namespace, supports key prefix/owner/time filters, limit/offset and sorting by key, updatedAt or accessCount, and returns an array. Its documented default limit is 100. purgeNamespace supports dryRun and returns count, namespace, optional keys and the dry-run flag. purgeMany returns affected keys. For managed runtime resources, deletion uses the backend's tombstone/authority fences; an acknowledged delete does not certify deletion across all user data families.
Parameter and result contracts
These are reference definitions for the explicit server APIs. Optional fields use ?; timestamps are milliseconds since epoch unless an operation accepts a Date. Resource identifiers and participant metadata do not grant runtime authority.
SetMutableInput
export interface SetMutableInput {
namespace: string;
key: string;
value: unknown;
tenantId?: string;
userId?: string;
metadata?: Record<string, unknown>;
}
MutableRecord
export interface MutableRecord extends GraphCaptureMetadata {
_id: string;
namespace: string;
key: string;
value: unknown;
tenantId?: string;
userId?: string;
metadata?: Record<string, unknown>;
createdAt: number;
updatedAt: number;
}
Also inherits GraphCaptureMetadata. See shared contracts.
ListMutableFilter
export interface ListMutableFilter {
namespace: string;
keyPrefix?: string;
tenantId?: string;
userId?: string;
limit?: number;
offset?: number;
updatedAfter?: number;
updatedBefore?: number;
sortBy?: "key" | "updatedAt" | "accessCount";
sortOrder?: "asc" | "desc";
}
CountMutableFilter
export interface CountMutableFilter {
namespace: string;
tenantId?: string;
userId?: string;
keyPrefix?: string;
updatedAfter?: number;
updatedBefore?: number;
}
PurgeNamespaceOptions
export interface PurgeNamespaceOptions extends GraphSyncOption {
dryRun?: boolean;
tenantId?: string;
}
| Field | Meaning and defaults |
|---|---|
tenantId | Tenant ID for multi-tenancy isolation (auto-injected from AuthContext) |
Also inherits GraphSyncOption. See shared contracts.
PurgeManyFilter
export interface PurgeManyFilter {
namespace: string;
keyPrefix?: string;
userId?: string;
updatedBefore?: number;
tenantId?: string;
}
| Field | Meaning and defaults |
|---|---|
namespace | Required namespace to purge from |
keyPrefix | Filter by key prefix |
userId | Filter by user ID |
updatedBefore | Delete keys updated before this timestamp (ms since epoch) |
tenantId | Tenant ID for multi-tenancy isolation (auto-injected from AuthContext) |
Public methods
The following signatures describe the TypeScript namespace. Parameter and result fields are defined on this page; the installed @cortexmemory/sdk declarations also provide their compile-time types. Signatures are reference notation, not application code to execute.
set
Set a key to a value (creates or overwrites)
cortex.mutable.set(namespace: string, key: string, value: unknown, userId?: string, metadata?: Record<string, unknown>, _options?: SetMutableOptions): Promise<MutableRecord>
get
Get current value for a key
cortex.mutable.get(namespace: string, key: string): Promise<unknown | null>
getRecord
Get full record (including metadata)
cortex.mutable.getRecord(namespace: string, key: string): Promise<MutableRecord | null>
update
Atomic update using updater function
cortex.mutable.update<T = unknown>(namespace: string, key: string, updater: (current: T) => T): Promise<MutableRecord>
increment
Increment a numeric value
cortex.mutable.increment(namespace: string, key: string, amount = 1): Promise<MutableRecord>
decrement
Decrement a numeric value
cortex.mutable.decrement(namespace: string, key: string, amount = 1): Promise<MutableRecord>
exists
Check if key exists
cortex.mutable.exists(namespace: string, key: string): Promise<boolean>
list
List keys in namespace
cortex.mutable.list(filter: ListMutableFilter): Promise<MutableRecord[]>
count
Count keys in namespace
cortex.mutable.count(filter: CountMutableFilter): Promise<number>
delete
Delete a key
cortex.mutable.delete(namespace: string, key: string, _options?: DeleteMutableOptions): Promise<{ deleted: boolean; namespace: string; key: string }>
purge
Purge a key (alias for delete - for API consistency)
cortex.mutable.purge(namespace: string, key: string): Promise<{ deleted: boolean; namespace: string; key: string }>
purgeNamespace
Purge all keys in a namespace
cortex.mutable.purgeNamespace(namespace: string, options?: PurgeNamespaceOptions): Promise<{ deleted: number; namespace: string; keys?: string[]; dryRun: boolean; }>
transaction
Execute multiple operations atomically
cortex.mutable.transaction(operations: Array<{ op: "set" | "update" | "delete" | "increment" | "decrement"; namespace: string; key: string; value?: unknown; amount?: number; }>): Promise<{ success: boolean; operationsExecuted: number; results: unknown[]; }>
purgeMany
Bulk delete keys matching filters
cortex.mutable.purgeMany(filter: PurgeManyFilter): Promise<{ deleted: number; namespace: string; keys: string[]; }>
Errors and lifecycle
Validation, missing capability, authorization, stale version and partial/unknown committed outcomes are explicit. Inspect operation/write/deletion receipts before recovery; do not repeat an external effect under a new identity after uncertainty. Close owned clients and graph resources at the end of the server workflow.
See runtime client for ordinary app execution and error handling for recovery boundaries.