SessionRuntimeModule · library/session/src/repos/sessions.ts:124

SessionsRepo

component

No JSDoc comment in source yet.

API

@Component()
export class SessionsRepo implements SessionIndex {
  @Use() private readonly db!: Db;
  insert(row: SessionRow): void;
  insertOrIgnore(row: SessionRow): void;
  insertFork(sourceSessionId: string, row: SessionRow): SessionRow;
  markOrgClient(sessionIds: readonly string[]): void;
  markHasTrace(sessionId: string): void;
  markDriven(sessionId: string, at: string): void;
  touchLastActive(sessionId: string, at: string): void;
  findById(sessionId: string): SessionRow | null;
  listByAgent(projectId: string, agentId: string): SessionRow[];
  listByProject(projectId: string): SessionRow[];
  findBySuffix(projectId: string, suffix: string): SessionRow[];
  updateApprovalMode(sessionId: string, mode: ApprovalMode): void;
  updateSandbox(sessionId: string, sandbox: SandboxSettings): void;
  updateThinkingLevel(sessionId: string, level: ThinkingLevelName): void;
  updateTitle(sessionId: string, title: string): void;
  updateModel(sessionId: string, provider: string, modelId: string): void;
  updateTitleIfNull(sessionId: string, title: string): void;
  setListing(sessionIds: readonly string[], listing: "archived" | "kept", at: string): void;
  replaceId(oldSessionId: string, newSessionId: string): void;
  deleteByAgent(projectId: string, agentId: string): void;
  deleteByProject(projectId: string): void;
  deleteById(sessionId: string): void;
}

Dependencies & Wiring

Extracted from class decorators
implementsinterfaces
@Userequires (1)
db: Db

Methods (22)

insert

insert(row: SessionRow): void
@paramrowSessionRow
@returnsvoid

insertOrIgnore

insertOrIgnore(row: SessionRow): void

Idempotent insert: used when Trace directory discovery backfills a row (concurrent listing discovering the same Session no longer triggers a UNIQUE violation).

@paramrowSessionRow
@returnsvoid

insertFork

insertFork(sourceSessionId: string, row: SessionRow): SessionRow

Allocate the source Session's next persistent fork number and insert its fork atomically. Keeping both writes in one transaction prevents concurrent requests from sharing a number, while retaining fork_count on the source means deleting an older fork never reuses it.

@paramsourceSessionIdstring
@paramrowSessionRow
@returnsSessionRow

markOrgClient

markOrgClient(sessionIds: readonly string[]): void

Stamp client = "org" on the rows an organization's files name, leaving the ones already stamped alone. The reconcile pass calls it with every session the desk ledger and the tickets' sessions fields claim, so a row opened before the marker existed carries the organization's provenance from the next pass on — and keeps it when the organization is deleted or company mode is switched off, which is exactly when the organization caches can no longer answer for it. Chunked because the id list is unbounded while SQLite's parameter count is not.

@paramsessionIdsreadonly string[]
@returnsvoid

markHasTrace

markHasTrace(sessionId: string): void

Flip the has_trace cache once a Trace record exists (discovery hydration); idempotent.

@paramsessionIdstring
@returnsvoid

markDriven

markDriven(sessionId: string, at: string): void

A driven run touched this Session: flip the has_trace cache and stamp last_active_at in ONE statement (drive's run start — one WAL commit instead of two). MAX(COALESCE(...)) keeps the stamp monotonic: a backwards clock step (NTP, resume from suspend) leaves the stored value alone rather than regressing the row.

@paramsessionIdstring
@paramatstring
@returnsvoid

touchLastActive

touchLastActive(sessionId: string, at: string): void

Stamp last_active_at alone (drive's run end); monotonic like markDriven.

@paramsessionIdstring
@paramatstring
@returnsvoid

findById

findById(sessionId: string): SessionRow | null
@paramsessionIdstring
@returnsSessionRow | null

listByAgent

listByAgent(projectId: string, agentId: string): SessionRow[]

An Agent's rows, newest first (the list order the sidebar shows; served by idx_sessions_agent_created). Never filtered by client — every row is listed whichever client created it.

@paramprojectIdstring
@paramagentIdstring
@returnsSessionRow[]

listByProject

listByProject(projectId: string): SessionRow[]
@paramprojectIdstring
@returnsSessionRow[]

findBySuffix

findBySuffix(projectId: string, suffix: string): SessionRow[]

The Session's original case and punctuation matter; the suffix is literal SQL data.

@paramprojectIdstring
@paramsuffixstring
@returnsSessionRow[]

updateApprovalMode

updateApprovalMode(sessionId: string, mode: ApprovalMode): void
@paramsessionIdstring
@parammodeApprovalMode
@returnsvoid

updateSandbox

updateSandbox(sessionId: string, sandbox: SandboxSettings): void

Replace the Session's sandbox policy snapshot (applies from its next command).

@paramsessionIdstring
@paramsandboxSandboxSettings
@returnsvoid

updateThinkingLevel

updateThinkingLevel(sessionId: string, level: ThinkingLevelName): void

Pin the session's thinking level (runs without one of their own then use it).

@paramsessionIdstring
@paramlevelThinkingLevelName
@returnsvoid

updateTitle

updateTitle(sessionId: string, title: string): void
@paramsessionIdstring
@paramtitlestring
@returnsvoid

updateModel

updateModel(sessionId: string, provider: string, modelId: string): void

The session's current model after an in-session switch (or a reconcile against its Trace).

@paramsessionIdstring
@paramproviderstring
@parammodelIdstring
@returnsvoid

updateTitleIfNull

updateTitleIfNull(sessionId: string, title: string): void

Writes only if the title is still NULL. Subagent-session registration and Trace directory discovery backfill can race on the same row, and both are insert-only: whichever inserts first determines the title. Discovery backfill can only supply NULL, so this method fills the title back in without overwriting an existing one (including a user rename or an already-generated title).

@paramsessionIdstring
@paramtitlestring
@returnsvoid

setListing

setListing(sessionIds: readonly string[], listing: "archived" | "kept", at: string): void

Writes the user's listing decision on these Sessions: archived stamps archived_at and clears the keep mark, kept clears archived_at and stamps the keep mark — at most one of the two is ever set. Chunked like markOrgClient, since a batch names up to 500 ids.

@paramsessionIdsreadonly string[]
@paramlisting"archived" | "kept"
@paramatstring
@returnsvoid

replaceId

replaceId(oldSessionId: string, newSessionId: string): void

Self-healing: after rebuilding a broken Session with no Trace, update the primary key to the new id.

@paramoldSessionIdstring
@paramnewSessionIdstring
@returnsvoid

deleteByAgent

deleteByAgent(projectId: string, agentId: string): void
@paramprojectIdstring
@paramagentIdstring
@returnsvoid

deleteByProject

deleteByProject(projectId: string): void
@paramprojectIdstring
@returnsvoid

deleteById

deleteById(sessionId: string): void
@paramsessionIdstring
@returnsvoid