Skip to main content

Session Domain API

These session records track explicit application lifecycle. They do not authenticate a runtime actor or provision grants. Runtime authentication uses verified JWTs and client.session(). Session administration and idle expiry remain scoped capabilities.

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.

Explicit session lifecycle​

create requires a user ID and accepts an optional generated/supplied session ID, selected tenant/space, expiry timestamp and extensible metadata. Session status, startedAt/lastActiveAt, optional endedAt/expiresAt and message/memory counters are returned. get returns null when absent; getOrCreate returns an existing active session or creates one for the selected user. touch updates activity; end terminates that session; getActive returns the user's active sessions.

list supports user/tenant/space/status plus limit/offset and returns an array; count returns the selected count. Authenticated tenant scope remains authoritative. endAll returns ended count and session IDs; always select the intended tenant in administrative work rather than assuming a user ID spans only one domain.

import type { Cortex } from '@cortexmemory/sdk';

export async function listApprovedSessions(cortex: Cortex, tenantId: string) {
const sessions = await cortex.sessions.list({ tenantId, status: 'active', limit: 25, offset: 0 });
return sessions.map(session => ({ id: session.sessionId,
userId: session.userId, lastActiveAt: session.lastActiveAt }));
}

expireIdle is retained in the public namespace for compatibility but is backend-maintenance-only: it throws SessionCapabilityError with BACKEND_MAINTENANCE_ONLY, outcome: "not_dispatched" and required execution trusted_backend_worker. Idle cleanup belongs to trusted backend workers, not an application polling loop. These explicit profile sessions are distinct from the runtime client's verified authority session and its revocation fencing.

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.

CreateSessionParams​

interface CreateSessionParams {
sessionId?: string;
userId: string;
tenantId?: string;
memorySpaceId?: string;
expiresAt?: number;
metadata?: SessionMetadata;
}

Session​

interface Session {
_id: string;
sessionId: string;
userId: string;
tenantId?: string;
memorySpaceId?: string;
status: SessionStatus;
startedAt: number;
lastActiveAt: number;
endedAt?: number;
expiresAt?: number;
metadata?: SessionMetadata;
messageCount: number;
memoryCount: number;
}

SessionFilters​

interface SessionFilters {
userId?: string;
tenantId?: string;
memorySpaceId?: string;
status?: SessionStatus;
limit?: number;
offset?: number;
}

EndAllOptions​

interface EndAllOptions {
tenantId?: string;
}

EndSessionsResult​

interface EndSessionsResult {
ended: number;
sessionIds: string[];
}

ExpireSessionsOptions​

interface ExpireSessionsOptions {
tenantId?: string;
idleTimeout?: number;
}

SessionPolicy​

export interface SessionPolicy {
lifecycle: SessionLifecyclePolicy;
cleanup: SessionCleanupPolicy;
limits?: SessionLimitsPolicy;
}

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 session for a user.

cortex.sessions.create(params: CreateSessionParams): Promise<Session>

get​

Get a session by ID.

cortex.sessions.get(sessionId: string): Promise<Session | null>

getOrCreate​

Get or create a session for a user.

cortex.sessions.getOrCreate(userId: string, metadata?: Record<string, unknown>): Promise<Session>

touch​

Update session activity timestamp (touch).

cortex.sessions.touch(sessionId: string): Promise<void>

end​

End a session.

cortex.sessions.end(sessionId: string): Promise<void>

endAll​

End all sessions for a user.

cortex.sessions.endAll(userId: string, options?: EndAllOptions): Promise<EndSessionsResult>

list​

List sessions with filters.

cortex.sessions.list(filters: SessionFilters): Promise<Session[]>

count​

Count sessions with filters.

cortex.sessions.count(filters: SessionFilters): Promise<number>

getActive​

Get active sessions for a user.

cortex.sessions.getActive(userId: string): Promise<Session[]>

expireIdle​

Request idle session expiration, which requires a trusted backend worker.

cortex.sessions.expireIdle(options?: ExpireSessionsOptions): Promise<{ expired: number }>

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.