Context Domain API
Use context records for explicit workflow trees, participants and version history. They are distinct from the runtime context handler/witness and stage barriers; caller context data cannot select policy or grant access.
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.
Hierarchy, versions and deletion
create requires purpose and memorySpaceId; optional owner, parent, conversation reference and extensible data describe the tracked workflow. Statuses are active, completed, cancelled and blocked. update applies status/description/data changes and supports completedAt for recorded completion. Invalid purpose, IDs, status or data object are rejected before mutation.
get(id) returns a context or null. get(id, { includeChain: true }) returns ContextChain with current/root/parent, children/siblings, ancestors/descendants, depth and node count. With includeConversation: true, the overload can also return ContextWithConversation; narrow the result before accessing conversation fields. getChildren is immediate by default, or recursive with recursive: true, and may filter by status. grantAccess is explicit context sharing metadata; it does not mint a runtime authorization grant.
Versions retain status, data, timestamp and optional updater. getVersion(id, version) and getAtTimestamp(id, Date) return null when no retained version matches; getHistory returns the retained sequence. Context list/search returns an array, with space/owner/status/parent/root/depth filters and an optional limit, rather than an offset-page envelope.
import type { Cortex } from '@cortexmemory/sdk';
export async function trackApprovedTask(cortex: Cortex, memorySpaceId: string) {
const root = await cortex.contexts.create({ purpose: 'delivery-check',
memorySpaceId, userId: 'user-123', data: { orderId: 'order-42' } });
const child = await cortex.contexts.create({ purpose: 'confirm-window',
memorySpaceId, parentId: root.contextId, data: { window: 'Wednesday' } });
await cortex.contexts.update(child.contextId, { status: 'completed',
completedAt: Date.now(), data: { confirmed: true } });
const chain = await cortex.contexts.get(child.contextId, { includeChain: true });
const history = await cortex.contexts.getHistory(child.contextId);
const previous = await cortex.contexts.getVersion(child.contextId, 1);
return { chain, history, previous };
}
delete has explicit child handling: cascadeChildren removes descendants; orphanChildren preserves children without the removed parent. Inspect descendantsDeleted and optional orphanedChildren in the result. updateMany/deleteMany return affected context IDs and counts. export supports JSON/CSV, optional chain and version history, returning serialized data/count/export timestamp. These operations are separate from a runtime run's durable Workflow lifecycle.
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.
CreateContextParams
export interface CreateContextParams {
purpose: string;
memorySpaceId: string;
description?: string;
userId?: string;
parentId?: string;
conversationRef?: {
conversationId: string;
messageIds?: string[];
};
data?: Record<string, unknown>;
status?: "active" | "completed" | "cancelled" | "blocked";
}
UpdateContextParams
export interface UpdateContextParams {
status?: "active" | "completed" | "cancelled" | "blocked";
description?: string;
data?: Record<string, unknown>;
completedAt?: number;
}
Context
export interface Context extends GraphCaptureMetadata {
_id: string;
contextId: string;
memorySpaceId: string;
tenantId?: string;
purpose: string;
description?: string;
userId?: string;
parentId?: string;
rootId?: string;
depth: number;
childIds: string[];
status: "active" | "completed" | "cancelled" | "blocked";
conversationRef?: {
conversationId: string;
messageIds?: string[];
};
participants: string[];
grantedAccess?: Array<{
memorySpaceId: string;
scope: string;
grantedAt: number;
}>;
data?: Record<string, unknown>;
metadata?: Record<string, unknown>;
version: number;
previousVersions?: ContextVersion[];
createdAt: number;
updatedAt: number;
completedAt?: number;
}
Also inherits GraphCaptureMetadata. See shared contracts.
ContextChain
export interface ContextChain {
current: Context;
parent?: Context;
root: Context;
children: Context[];
siblings: Context[];
ancestors: Context[];
descendants: Context[];
depth: number;
totalNodes: number;
}
ContextWithConversation
export interface ContextWithConversation extends ContextChain {
conversation?: {
_id: string;
conversationId: string;
messages: Array<{
id: string;
role: string;
content: string;
timestamp: number;
}>;
};
triggerMessages?: Array<{
id: string;
role: string;
content: string;
timestamp: number;
}>;
}
Also inherits ContextChain. See shared contracts.
ListContextsFilter
export interface ListContextsFilter {
memorySpaceId?: string;
userId?: string;
status?: "active" | "completed" | "cancelled" | "blocked";
parentId?: string;
rootId?: string;
depth?: number;
limit?: number;
}
CountContextsFilter
export interface CountContextsFilter {
memorySpaceId?: string;
userId?: string;
status?: "active" | "completed" | "cancelled" | "blocked";
}
ContextVersion
export interface ContextVersion {
version: number;
status: string;
data?: Record<string, unknown>;
timestamp: number;
updatedBy?: string;
}
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.
create
Create a new context
cortex.contexts.create(params: CreateContextParams, _options?: CreateContextOptions): Promise<Context>
get
Get context by ID
cortex.contexts.get(contextId: string, options?: { includeChain?: false; includeConversation?: boolean }): Promise<Context | null>
cortex.contexts.get(contextId: string, options: { includeChain: true; includeConversation: true }): Promise<ContextChain | ContextWithConversation | null>
cortex.contexts.get(contextId: string, options: { includeChain: true; includeConversation?: false }): Promise<ContextChain | null>
cortex.contexts.get(contextId: string, options: { includeChain?: boolean; includeConversation?: boolean }): Promise<Context | ContextChain | ContextWithConversation | null>
cortex.contexts.get(contextId: string, options?: { includeChain?: boolean; includeConversation?: boolean }): Promise<Context | ContextChain | ContextWithConversation | null>
update
Update a context
cortex.contexts.update(contextId: string, updates: UpdateContextParams, _options?: UpdateContextOptions): Promise<Context>
delete
Delete a context
cortex.contexts.delete(contextId: string, options?: DeleteContextOptions & { cascadeChildren?: boolean; orphanChildren?: boolean; }): Promise<{ deleted: boolean; contextId: string; descendantsDeleted: number; orphanedChildren?: string[]; }>
list
List contexts
cortex.contexts.list(filter?: ListContextsFilter): Promise<Context[]>
count
Count contexts
cortex.contexts.count(filter?: CountContextsFilter): Promise<number>
search
Search contexts (alias for list with filters)
cortex.contexts.search(filter?: ListContextsFilter): Promise<Context[]>
getChain
Get complete chain from any context
cortex.contexts.getChain(contextId: string): Promise<ContextChain>
getRoot
Get root context
cortex.contexts.getRoot(contextId: string): Promise<Context>
getChildren
Get children of a context
cortex.contexts.getChildren(contextId: string, options?: { status?: "active" | "completed" | "cancelled" | "blocked"; recursive?: boolean; }): Promise<Context[]>
addParticipant
Add participant to context
cortex.contexts.addParticipant(contextId: string, participantId: string): Promise<Context>
grantAccess
Grant cross-space access to a context (Collaboration Mode)
cortex.contexts.grantAccess(contextId: string, targetMemorySpaceId: string, scope: string): Promise<Context>
updateMany
Update many contexts matching filters
cortex.contexts.updateMany(filters: { memorySpaceId?: string; userId?: string; status?: "active" | "completed" | "cancelled" | "blocked"; parentId?: string; rootId?: string; }, updates: { status?: "active" | "completed" | "cancelled" | "blocked"; data?: Record<string, unknown>; }): Promise<{ updated: number; contextIds: string[] }>
deleteMany
Delete many contexts matching filters
cortex.contexts.deleteMany(filters: { memorySpaceId?: string; userId?: string; status?: "active" | "completed" | "cancelled" | "blocked"; completedBefore?: number; }, options?: { cascadeChildren?: boolean }): Promise<{ deleted: number; contextIds: string[] }>
export
Export contexts to JSON or CSV
cortex.contexts.export(filters?: { memorySpaceId?: string; userId?: string; status?: "active" | "completed" | "cancelled" | "blocked"; }, options?: { format: "json" | "csv"; includeChain?: boolean; includeVersionHistory?: boolean; }): Promise<{ format: string; data: string; count: number; exportedAt: number; }>
removeParticipant
Remove participant from context
cortex.contexts.removeParticipant(contextId: string, participantId: string): Promise<Context>
getByConversation
Get all contexts originating from a specific conversation
cortex.contexts.getByConversation(conversationId: string): Promise<Context[]>
findOrphaned
Find contexts whose parent no longer exists
cortex.contexts.findOrphaned(): Promise<Context[]>
getVersion
Get specific version of a context
cortex.contexts.getVersion(contextId: string, version: number): Promise<{ version: number; status: string; data?: Record<string, unknown>; timestamp: number; updatedBy?: string; } | null>
getHistory
Get all versions of a context
cortex.contexts.getHistory(contextId: string): Promise< Array<{ version: number; status: string; data?: Record<string, unknown>; timestamp: number; updatedBy?: string; }> >
getAtTimestamp
Get context state at specific point in time
cortex.contexts.getAtTimestamp(contextId: string, timestamp: Date): Promise<{ version: number; status: string; data?: Record<string, unknown>; timestamp: number; updatedBy?: string; } | null>
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.