import { randomUUID, createHash } from 'node:crypto' import { getDb } from './db/sqlite' // ---------- Types ---------- export interface UserRecord { username: string createdAt: Date } export interface EnvironmentRecord { id: string secret: string machineName: string | null /** * Startup/default directory. Kept for display and as the default session * cwd; it is NOT an allowlist — sessions may target any directory the * worker's OS user can access. */ directory: string | null /** * Quick-pick directories surfaced to the Web UI (advisory only). First * entry is normally `directory`. Empty/undefined for legacy workers. */ workspaceDirs: string[] branch: string | null gitRepoUrl: string | null maxSessions: number workerType: string bridgeId: string | null capabilities: Record | null status: string username: string | null /** * Peer address the worker registered from (captured at POST * /v1/environments/bridge). In-memory only — like maxSessions/bridgeId the * environments table has no column for it, and workers re-register with a * fresh address on every restart. Null when the runtime couldn't resolve a * peer address (e.g. a WS-only ACP path); the Web UI simply omits it. */ ipAddress: string | null lastPollAt: Date | null createdAt: Date updatedAt: Date /** * Session Attach (P2): 'attach' marks a worker that registered in * wait-for-assignment mode — it created no session and is polling for the * Web UI to bind one. Surfaced as `attach_waiting` on GET /web/environments * so the Web UI can enumerate "waiting CLIs" to bind sessions to. Null = * default create-mode behavior. */ sessionMode?: string | null } export interface SessionRecord { id: string environmentId: string | null title: string | null status: string source: string permissionMode: string | null /** Absolute working directory for this session. NULL on legacy rows. */ cwd: string | null /** Persisted so reconnect/attach can reproduce the original spawn shape. */ spawnMode: 'same-dir' | 'worktree' | null workerEpoch: number username: string | null visibility: string createdAt: Date updatedAt: Date } export interface WorkItemRecord { id: string environmentId: string sessionId: string state: string secret: string // Per-session spawn mode chosen in Web UI at creation time. Undefined for // legacy work items or when the client didn't specify (worker falls back // to its startup default). spawnMode?: 'same-dir' | 'worktree' /** * Absolute session working directory, copied from the session record at * dispatch time. Work items are projections of their session: every * creation path (initial dispatch, reconnect, redispatch, attach) reads it * from the session so a resumed session never falls back to the worker's * startup directory. */ cwd?: string | null /** Session-level permission mode to hand to the spawned child. */ permissionMode?: string | null /** * Environment-scoped RPC delivered through the same poll channel as * session work. Present only on non-session items (sessionId is empty); * the worker answers over HTTP, correlated by requestId. */ rpc?: { requestId: string method: string params: Record } createdAt: Date updatedAt: Date } export interface SessionWorkerRecord { sessionId: string workerStatus: string | null externalMetadata: Record | null requiresActionDetails: Record | null lastHeartbeatAt: Date | null createdAt: Date updatedAt: Date } // ---------- Stores (in-memory Maps) ---------- const users = new Map() const tokenToUser = new Map() const environments = new Map() const sessions = new Map() const workItems = new Map() const sessionWorkers = new Map() // Session ownership tracked in SQLite session_owners table // ---------- User ---------- export function storeCreateUser(username: string): UserRecord { const existing = users.get(username) if (existing) return existing const record: UserRecord = { username, createdAt: new Date() } users.set(username, record) return record } export function storeGetUser(username: string): UserRecord | undefined { return users.get(username) } export function storeCreateToken(username: string, token: string): void { tokenToUser.set(token, { username, createdAt: new Date() }) } export function storeGetUserByToken( token: string, ): { username: string; createdAt: Date } | undefined { return tokenToUser.get(token) } export function storeDeleteToken(token: string): boolean { return tokenToUser.delete(token) } // ---------- Environment ---------- export function storeCreateEnvironment(req: { secret: string machineName?: string directory?: string workspaceDirs?: string[] branch?: string gitRepoUrl?: string maxSessions?: number workerType?: string bridgeId?: string username?: string ipAddress?: string capabilities?: Record ownerUserId?: string | null claimToken?: string | null claimExpiresAt?: Date | null sessionMode?: string | null }): EnvironmentRecord { const id = `env_${randomUUID().replace(/-/g, '')}` const now = new Date() const record: EnvironmentRecord = { id, secret: req.secret, machineName: req.machineName ?? null, directory: req.directory ?? null, workspaceDirs: req.workspaceDirs ?? (req.directory ? [req.directory] : []), branch: req.branch ?? null, gitRepoUrl: req.gitRepoUrl ?? null, maxSessions: req.maxSessions ?? 1, workerType: req.workerType ?? 'claude_code', bridgeId: req.bridgeId ?? null, capabilities: req.capabilities ?? null, status: 'active', username: req.username ?? null, ipAddress: req.ipAddress ?? null, lastPollAt: now, createdAt: now, updatedAt: now, sessionMode: req.sessionMode ?? null, } environments.set(id, record) try { const db = getDb() const secretHash = createHash('sha256').update(record.secret).digest('hex') db.query( `INSERT OR REPLACE INTO environments (id, secret_hash, machine_name, worker_type, status, last_poll_at, created_at, owner_user_id, claim_token, claim_expires_at, session_mode, workspace_dirs) VALUES ($id, $sh, $mn, $wt, $st, $lp, $ca, $ou, $ct, $ce, $sm, $wd)`, ).run({ $id: id, $sh: secretHash, $mn: record.machineName, $wt: record.workerType, $st: record.status, $lp: record.lastPollAt?.toISOString() ?? null, $ca: record.createdAt.toISOString(), $ou: req.ownerUserId ?? null, $ct: req.claimToken ?? null, $ce: req.claimExpiresAt?.toISOString() ?? null, $sm: record.sessionMode ?? null, $wd: JSON.stringify(record.workspaceDirs), }) } catch (err) { console.error('[store] DB write failed in storeCreateEnvironment:', err) throw err } return record } export function storeGetEnvironment(id: string): EnvironmentRecord | undefined { return environments.get(id) } export function storeUpdateEnvironment( id: string, patch: Partial< Pick< EnvironmentRecord, | 'status' | 'lastPollAt' | 'updatedAt' | 'capabilities' | 'machineName' | 'directory' | 'maxSessions' | 'bridgeId' | 'ipAddress' | 'sessionMode' | 'workspaceDirs' > >, ): boolean { const rec = environments.get(id) if (!rec) return false Object.assign(rec, patch, { updatedAt: new Date() }) try { const db = getDb() const sets: string[] = [] const params: Record = { $id: id } if (patch.status !== undefined) { sets.push('status = $status') params.$status = patch.status } if (patch.lastPollAt !== undefined) { sets.push('last_poll_at = $lastPollAt') params.$lastPollAt = patch.lastPollAt?.toISOString() ?? null } if (patch.machineName !== undefined) { sets.push('machine_name = $machineName') params.$machineName = patch.machineName } if (patch.sessionMode !== undefined) { sets.push('session_mode = $sessionMode') params.$sessionMode = patch.sessionMode } if (patch.workspaceDirs !== undefined) { sets.push('workspace_dirs = $workspaceDirs') params.$workspaceDirs = JSON.stringify(patch.workspaceDirs) } // bridgeId, maxSessions, capabilities, ipAddress, updatedAt are // in-memory only — the environments table has no corresponding columns. if (sets.length > 0) { db.query(`UPDATE environments SET ${sets.join(', ')} WHERE id = $id`).run( params, ) } } catch (err) { console.error('[store] DB write failed in storeUpdateEnvironment:', err) throw err } return true } export function storeListActiveEnvironments(): EnvironmentRecord[] { return [...environments.values()].filter(e => e.status === 'active') } /** * Return active environments enriched with team_id and team_name from SQLite. * LEFT JOINs team_environments + teams (excluding soft-deleted teams). */ export function storeListActiveEnvironmentsWithTeam( includeDisconnected = false, ): Array< EnvironmentRecord & { teamId: string | null teamName: string | null ownerUserId: string | null ownerUsername: string | null } > { const active = [...environments.values()].filter(e => includeDisconnected ? e.status === 'active' || e.status === 'disconnected' : e.status === 'active', ) if (active.length === 0) return [] // Build a map of env_id → { teamId, teamName } from SQLite const teamMap = new Map() // Build a map of env_id → { ownerUserId, ownerUsername } from SQLite. // The username is joined here (rather than in the response layer) so the // Web UI can attribute an environment to a human without a second query. const ownerMap = new Map< string, { userId: string; username: string | null } >() try { const db = getDb() const rows = db .query( `SELECT te.environment_id, te.team_id, t.name AS team_name FROM team_environments te JOIN teams t ON t.id = te.team_id AND t.deleted_at IS NULL`, ) .all() as Array<{ environment_id: string team_id: string team_name: string }> for (const row of rows) { teamMap.set(row.environment_id, { teamId: row.team_id, teamName: row.team_name, }) } const ownerRows = db .query( `SELECT e.id, e.owner_user_id, u.username AS owner_username FROM environments e LEFT JOIN users u ON u.id = e.owner_user_id WHERE e.owner_user_id IS NOT NULL`, ) .all() as Array<{ id: string owner_user_id: string owner_username: string | null }> for (const row of ownerRows) { ownerMap.set(row.id, { userId: row.owner_user_id, username: row.owner_username, }) } } catch (err) { console.error( '[store] DB read failed in storeListActiveEnvironmentsWithTeam:', err, ) // Fall through: return envs without team/owner info } return active.map(env => { const team = teamMap.get(env.id) const owner = ownerMap.get(env.id) return { ...env, teamId: team?.teamId ?? null, teamName: team?.teamName ?? null, ownerUserId: owner?.userId ?? null, ownerUsername: owner?.username ?? null, } }) } /** * Look up environment ownership fields from SQLite. * Returns null if the environment row doesn't exist. */ export function storeGetEnvironmentOwnership(envId: string): { ownerUserId: string | null teamId: string | null claimToken: string | null claimExpiresAt: string | null } | null { try { const db = getDb() const row = db .query( `SELECT e.owner_user_id, e.claim_token, e.claim_expires_at, (SELECT te.team_id FROM team_environments te WHERE te.environment_id = e.id) AS team_id FROM environments e WHERE e.id = $id`, ) .get({ $id: envId }) as { owner_user_id: string | null claim_token: string | null claim_expires_at: string | null team_id: string | null } | null if (!row) return null return { ownerUserId: row.owner_user_id, teamId: row.team_id, claimToken: row.claim_token, claimExpiresAt: row.claim_expires_at, } } catch (err) { console.error( '[store] DB read failed in storeGetEnvironmentOwnership:', err, ) throw err } } /** * Claim an unowned environment: write owner_user_id and clear claim_token. * Returns true if the row was updated, false if env not found or already * owned (idempotent — caller checks ownership state first). */ export function storeClaimEnvironment(envId: string, userId: string): boolean { try { const db = getDb() const result = db .query( `UPDATE environments SET owner_user_id = $userId, claim_token = NULL, claim_expires_at = NULL WHERE id = $envId AND owner_user_id IS NULL`, ) .run({ $envId: envId, $userId: userId }) return result.changes > 0 } catch (err) { console.error('[store] DB write failed in storeClaimEnvironment:', err) throw err } } /** * (Re)write an environment's claim token — DB-only (claim state lives in * SQLite, not in the in-memory record). Used by the idempotent reattach * path when an unowned env's previous claim expired and a fresh token was * minted, so the printed claim URL keeps pointing at a token that exists. * * Guarded on owner_user_id IS NULL — an owned env never gets a claim token. * Returns true if the row was updated. */ export function storeSetEnvironmentClaim( envId: string, claimToken: string | null, claimExpiresAt: Date | null, ): boolean { try { const db = getDb() const result = db .query( `UPDATE environments SET claim_token = $ct, claim_expires_at = $ce WHERE id = $id AND owner_user_id IS NULL`, ) .run({ $id: envId, $ct: claimToken, $ce: claimExpiresAt?.toISOString() ?? null, }) return result.changes > 0 } catch (err) { console.error('[store] DB write failed in storeSetEnvironmentClaim:', err) throw err } } /** * Bind all sessions under an environment to a user as 'user' owner. * * Used after storeClaimEnvironment: when an env is claimed, any sessions * already auto-created under it (e.g. ACP sessions created at env register * time before the env had an owner) are orphan — they have no * session_owners rows. This backfills them in one shot so the claimer can * see and access them in the Web UI. * * Idempotent: INSERT OR IGNORE skips sessions that already have a user * owner row. Returns the number of newly inserted rows. */ export function storeBindEnvSessionsToUser( envId: string, userId: string, ): number { try { const db = getDb() const result = db .query( `INSERT OR IGNORE INTO session_owners (session_id, owner_type, owner_id) SELECT s.id, 'user', $userId FROM sessions s WHERE s.environment_id = $envId AND NOT EXISTS ( SELECT 1 FROM session_owners so WHERE so.session_id = s.id AND so.owner_type = 'user' AND so.owner_id = $userId )`, ) .run({ $envId: envId, $userId: userId }) return result.changes } catch (err) { console.error('[store] DB write failed in storeBindEnvSessionsToUser:', err) throw err } } /** * Bind all sessions under an environment to a team as 'team' owner. * * Used after the route layer links an environment to a team: sessions * auto-created at register time (ACP reuse path) read env ownership from * SQLite BEFORE the team_environments link exists, so their team binding * is missing. Backfills it in one shot. * * Idempotent: INSERT OR IGNORE skips sessions that already have this team * owner row. Returns the number of newly inserted rows. */ export function storeBindEnvSessionsToTeam( envId: string, teamId: string, ): number { try { const db = getDb() const result = db .query( `INSERT OR IGNORE INTO session_owners (session_id, owner_type, owner_id) SELECT s.id, 'team', $teamId FROM sessions s WHERE s.environment_id = $envId AND NOT EXISTS ( SELECT 1 FROM session_owners so WHERE so.session_id = s.id AND so.owner_type = 'team' AND so.owner_id = $teamId )`, ) .run({ $envId: envId, $teamId: teamId }) return result.changes } catch (err) { console.error('[store] DB write failed in storeBindEnvSessionsToTeam:', err) throw err } } export function storeListActiveEnvironmentsByUsername( username: string, ): EnvironmentRecord[] { return [...environments.values()].filter( e => e.status === 'active' && e.username === username, ) } // ---------- Session ---------- export function storeCreateSession(req: { environmentId?: string | null title?: string | null source?: string permissionMode?: string | null cwd?: string | null spawnMode?: 'same-dir' | 'worktree' | null idPrefix?: string username?: string | null visibility?: string }): SessionRecord { const id = `${req.idPrefix || 'session_'}${randomUUID().replace(/-/g, '')}` const now = new Date() const record: SessionRecord = { id, environmentId: req.environmentId ?? null, title: req.title ?? null, status: 'idle', source: req.source ?? 'remote-control', permissionMode: req.permissionMode ?? null, cwd: req.cwd ?? null, spawnMode: req.spawnMode ?? null, workerEpoch: 0, username: req.username ?? null, visibility: req.visibility ?? 'private', createdAt: now, updatedAt: now, } sessions.set(id, record) try { const db = getDb() db.query( `INSERT OR REPLACE INTO sessions (id, environment_id, title, status, source, visibility, permission_mode, cwd, spawn_mode, created_at, updated_at) VALUES ($id, $eid, $title, $status, $source, $vis, $pm, $cwd, $sm, $ca, $ua)`, ).run({ $id: id, $eid: record.environmentId, $title: record.title, $status: record.status, $source: record.source, $vis: record.visibility, $pm: record.permissionMode, $cwd: record.cwd, $sm: record.spawnMode, $ca: record.createdAt.toISOString(), $ua: record.updatedAt.toISOString(), }) } catch (err) { console.error('[store] DB write failed in storeCreateSession:', err) throw err } return record } export function storeGetSession(id: string): SessionRecord | undefined { return sessions.get(id) } export function storeUpdateSession( id: string, patch: Partial< Pick >, ): boolean { const rec = sessions.get(id) if (!rec) return false Object.assign(rec, patch, { updatedAt: new Date() }) try { const db = getDb() db.query( `UPDATE sessions SET title = COALESCE($title, title), status = COALESCE($status, status), updated_at = $ua WHERE id = $id`, ).run({ $id: id, $title: patch.title ?? null, $status: patch.status ?? null, $ua: rec.updatedAt.toISOString(), }) } catch (err) { console.error('[store] DB write failed in storeUpdateSession:', err) throw err } return true } export function storeListSessions(): SessionRecord[] { return [...sessions.values()] } export function storeListSessionsByUsername(username: string): SessionRecord[] { return [...sessions.values()].filter(s => s.username === username) } export function storeListSessionsByEnvironment(envId: string): SessionRecord[] { return [...sessions.values()].filter(s => s.environmentId === envId) } /** * Re-point a session to a different environment (Session Attach). * * Only mutates environment_id + updated_at — session_owners (user/team) are * deliberately untouched: attach transfers *driving* of the session, never * ownership. The sessions.environment_id column predates this function; * storeUpdateSession's patch type intentionally excludes environment_id * because ownership-binding flows must never re-point a session as a side * effect. Attach is the one flow with an explicit re-bind contract. * * environmentId may be null — the rollback path (dispatch failure in * performAttach) restores "no environment" when the session had none. */ export function storeRebindSessionEnvironment( sessionId: string, environmentId: string | null, ): boolean { const rec = sessions.get(sessionId) if (!rec) return false rec.environmentId = environmentId rec.updatedAt = new Date() try { const db = getDb() db.query( 'UPDATE sessions SET environment_id = $envId, updated_at = $ua WHERE id = $id', ).run({ $envId: environmentId, $ua: rec.updatedAt.toISOString(), $id: sessionId, }) } catch (err) { console.error( '[store] DB write failed in storeRebindSessionEnvironment:', err, ) throw err } return true } export function storeDeleteSession(id: string): boolean { sessionWorkers.delete(id) const deleted = sessions.delete(id) try { const db = getDb() db.query('DELETE FROM sessions WHERE id = $id').run({ $id: id }) } catch (err) { console.error('[store] DB write failed in storeDeleteSession:', err) throw err } return deleted } // ---------- Session Worker ---------- export function storeGetSessionWorker( sessionId: string, ): SessionWorkerRecord | undefined { return sessionWorkers.get(sessionId) } export function storeUpsertSessionWorker( sessionId: string, patch: { workerStatus?: string | null externalMetadata?: Record | null requiresActionDetails?: Record | null lastHeartbeatAt?: Date | null }, ): SessionWorkerRecord { const now = new Date() const existing = sessionWorkers.get(sessionId) const record: SessionWorkerRecord = existing ?? { sessionId, workerStatus: null, externalMetadata: null, requiresActionDetails: null, lastHeartbeatAt: null, createdAt: now, updatedAt: now, } if (patch.workerStatus !== undefined) { record.workerStatus = patch.workerStatus } if (patch.externalMetadata !== undefined) { if (patch.externalMetadata === null) { record.externalMetadata = null } else { record.externalMetadata = { ...(record.externalMetadata ?? {}), ...patch.externalMetadata, } } } if (patch.requiresActionDetails !== undefined) { record.requiresActionDetails = patch.requiresActionDetails } if (patch.lastHeartbeatAt !== undefined) { record.lastHeartbeatAt = patch.lastHeartbeatAt } record.updatedAt = now sessionWorkers.set(sessionId, record) return record } // ---------- Work Items ---------- // ---------- Session Ownership (SQLite-backed) ---------- export function storeBindSession( sessionId: string, ownerId: string, ownerType: 'user' | 'team' = 'user', ): void { try { const db = getDb() db.query( `INSERT OR IGNORE INTO session_owners (session_id, owner_type, owner_id) VALUES ($sessionId, $ownerType, $ownerId)`, ).run({ $sessionId: sessionId, $ownerType: ownerType, $ownerId: ownerId, }) } catch (err) { console.error('[store] DB write failed in storeBindSession:', err) throw err } } export function storeIsSessionOwner(sessionId: string, uuid: string): boolean { try { const db = getDb() const row = db .query( `SELECT 1 FROM session_owners WHERE session_id = $sessionId AND owner_id = $ownerId LIMIT 1`, ) .get({ $sessionId: sessionId, $ownerId: uuid }) return !!row } catch (err) { console.error('[store] DB read failed in storeIsSessionOwner:', err) throw err } } export function storeGetSessionOwners( sessionId: string, ): Set | undefined { try { const db = getDb() const rows = db .query( `SELECT owner_id FROM session_owners WHERE session_id = $sessionId`, ) .all({ $sessionId: sessionId }) as Array<{ owner_id: string }> if (rows.length === 0) return undefined return new Set(rows.map(r => r.owner_id)) } catch (err) { console.error('[store] DB read failed in storeGetSessionOwners:', err) throw err } } export function storeListSessionsByOwnerUuid(uuid: string): SessionRecord[] { const result: SessionRecord[] = [] const resultIds = new Set() // Collect sessions owned by this user from SQLite try { const db = getDb() // Direct user ownership const ownedRows = db .query( `SELECT session_id FROM session_owners WHERE owner_type = 'user' AND owner_id = $ownerId`, ) .all({ $ownerId: uuid }) as Array<{ session_id: string }> for (const row of ownedRows) { const session = sessions.get(row.session_id) if (session) { result.push(session) resultIds.add(row.session_id) } } // Team ownership: sessions owned by a team the user belongs to const teamOwnedRows = db .query( `SELECT so.session_id FROM session_owners so JOIN team_members tm ON tm.team_id = so.owner_id WHERE so.owner_type = 'team' AND tm.user_id = $userId`, ) .all({ $userId: uuid }) as Array<{ session_id: string }> for (const row of teamOwnedRows) { if (resultIds.has(row.session_id)) continue const session = sessions.get(row.session_id) if (session) { result.push(session) resultIds.add(row.session_id) } } // Include public sessions const publicRows = db .query(`SELECT id FROM sessions WHERE visibility = 'public'`) .all() as Array<{ id: string }> for (const row of publicRows) { if (resultIds.has(row.id)) continue const session = sessions.get(row.id) if (session) { result.push(session) resultIds.add(row.id) } } // Include sessions explicitly shared with this user (non-expired) const directShareRows = db .query( `SELECT session_id FROM session_shares WHERE granted_to_user = $userId AND (expires_at IS NULL OR expires_at > strftime('%Y-%m-%dT%H:%M:%S.000Z', 'now'))`, ) .all({ $userId: uuid }) as Array<{ session_id: string }> for (const row of directShareRows) { if (resultIds.has(row.session_id)) continue const session = sessions.get(row.session_id) if (session) { result.push(session) resultIds.add(row.session_id) } } // Include sessions shared with a team the user belongs to (non-expired) const teamShareRows = db .query( `SELECT ss.session_id FROM session_shares ss JOIN team_members tm ON tm.team_id = ss.granted_to_team WHERE tm.user_id = $userId AND (ss.expires_at IS NULL OR ss.expires_at > strftime('%Y-%m-%dT%H:%M:%S.000Z', 'now'))`, ) .all({ $userId: uuid }) as Array<{ session_id: string }> for (const row of teamShareRows) { if (resultIds.has(row.session_id)) continue const session = sessions.get(row.session_id) if (session) { result.push(session) resultIds.add(row.session_id) } } } catch (err) { console.error( '[store] DB read failed in storeListSessionsByOwnerUuid:', err, ) throw err } return result } /** * Bulk-lookup session owners for a list of session IDs. * Returns a Map of session_id → { owner_type, owner_id } (first owner wins). */ export function storeGetSessionOwnersBulk( sessionIds: string[], ): Map { const result = new Map< string, { owner_type: 'user' | 'team'; owner_id: string } >() if (sessionIds.length === 0) return result try { const db = getDb() // Use a subquery with IN clause — safe because sessionIds are internal const placeholders = sessionIds.map((_, i) => `$s${i}`).join(',') const params: Record = {} sessionIds.forEach((id, i) => { params[`$s${i}`] = id }) const rows = db .query( `SELECT session_id, owner_type, owner_id FROM session_owners WHERE session_id IN (${placeholders})`, ) .all(params) as Array<{ session_id: string owner_type: 'user' | 'team' owner_id: string }> for (const row of rows) { // First owner wins (a session may have multiple owners) if (!result.has(row.session_id)) { result.set(row.session_id, { owner_type: row.owner_type, owner_id: row.owner_id, }) } } } catch (err) { console.error('[store] DB read failed in storeGetSessionOwnersBulk:', err) // Return empty map — caller will treat as null owner } return result } // ---------- Work Items (cont.) ---------- export function storeCreateWorkItem(req: { environmentId: string sessionId: string secret: string spawnMode?: 'same-dir' | 'worktree' cwd?: string | null permissionMode?: string | null rpc?: WorkItemRecord['rpc'] }): WorkItemRecord { const id = `work_${randomUUID().replace(/-/g, '')}` const now = new Date() const record: WorkItemRecord = { id, environmentId: req.environmentId, sessionId: req.sessionId, state: 'pending', secret: req.secret, spawnMode: req.spawnMode, cwd: req.cwd ?? null, permissionMode: req.permissionMode ?? null, rpc: req.rpc, createdAt: now, updatedAt: now, } workItems.set(id, record) return record } export function storeGetWorkItem(id: string): WorkItemRecord | undefined { return workItems.get(id) } /** Find the active (non-completed) work item for a given session. */ export function storeGetActiveWorkItemBySession( sessionId: string, ): WorkItemRecord | undefined { for (const item of workItems.values()) { if (item.sessionId === sessionId && item.state !== 'completed') { return item } } return undefined } export function storeListWorkItems(): WorkItemRecord[] { return Array.from(workItems.values()) } export function storeGetPendingWorkItem( environmentId: string, ): WorkItemRecord | undefined { for (const item of workItems.values()) { if (item.environmentId === environmentId && item.state === 'pending') { return item } } return undefined } export function storeUpdateWorkItem( id: string, patch: Partial>, ): boolean { const rec = workItems.get(id) if (!rec) return false Object.assign(rec, patch, { updatedAt: new Date() }) return true } // ---------- ACP Agent (reuses EnvironmentRecord with workerType="acp") ---------- /** List all ACP agents (environments with workerType="acp") */ export function storeListAcpAgents(): EnvironmentRecord[] { return [...environments.values()].filter(e => e.workerType === 'acp') } /** List ACP agents by channel group (stored in bridgeId field) */ export function storeListAcpAgentsByChannelGroup( channelGroupId: string, ): EnvironmentRecord[] { return [...environments.values()].filter( e => e.workerType === 'acp' && e.bridgeId === channelGroupId, ) } /** List online ACP agents */ export function storeListOnlineAcpAgents(): EnvironmentRecord[] { return [...environments.values()].filter( e => e.workerType === 'acp' && e.status === 'active', ) } /** Mark an ACP agent as offline */ export function storeMarkAcpAgentOffline(id: string): boolean { const rec = environments.get(id) if (!rec || rec.workerType !== 'acp') return false Object.assign(rec, { status: 'offline', updatedAt: new Date() }) return true } /** Mark an ACP agent as online (on reconnect) */ export function storeMarkAcpAgentOnline(id: string): boolean { const rec = environments.get(id) if (!rec || rec.workerType !== 'acp') return false Object.assign(rec, { status: 'active', lastPollAt: new Date(), updatedAt: new Date(), }) return true } // ---------- Session Attach codes ---------- export interface AttachCodeRecord { code: string sessionId: string expiresAt: Date } const attachCodes = new Map() /** * Issue a one-time, short-TTL attach code for a session (P3). The Web UI * session manager calls POST /web/sessions/:id/attach-code and hands the * code to a CLI operator (`/rc --attach atc_...`); the code is the attach * authorization — the CLI needs no manage rights of its own. * * In-memory only (no DB table): codes are single-use with a 15-minute TTL, * so losing them on a server restart is acceptable — the user just clicks * "copy attach code" again. */ export function storeIssueAttachCode( sessionId: string, ttlMs = 15 * 60 * 1000, ): AttachCodeRecord { // Sweep expired codes: consume() only removes a code when it is actually // presented, so unconsumed expired codes would otherwise accumulate for // the process lifetime. Map deletion during iteration is safe. const now = Date.now() for (const [code, record] of attachCodes) { if (record.expiresAt.getTime() <= now) attachCodes.delete(code) } const code = `atc_${randomUUID().replace(/-/g, '')}` const record: AttachCodeRecord = { code, sessionId, expiresAt: new Date(Date.now() + ttlMs), } attachCodes.set(code, record) return record } /** * Consume an attach code: single-use — returns the record and deletes it. * Expired or unknown codes return null. */ export function storeConsumeAttachCode(code: string): AttachCodeRecord | null { const record = attachCodes.get(code) if (!record) return null attachCodes.delete(code) if (record.expiresAt.getTime() <= Date.now()) return null return record } /** Test helper — drop all attach codes. */ export function storeResetAttachCodes() { attachCodes.clear() } /** Test helper — number of attach codes currently held (expired codes that * have not been swept yet still count). */ export function storeAttachCodesSize(): number { return attachCodes.size } // ---------- Reset (for tests) ---------- export function storeReset() { users.clear() tokenToUser.clear() environments.clear() sessions.clear() workItems.clear() sessionWorkers.clear() storeResetAttachCodes() try { const db = getDb() // Delete in FK-safe order: child tables before parent tables db.query('DELETE FROM session_tokens').run() db.query('DELETE FROM session_owners').run() db.query('DELETE FROM session_shares').run() db.query('DELETE FROM team_members').run() db.query('DELETE FROM team_environments').run() db.query('DELETE FROM invitations').run() db.query('DELETE FROM sessions').run() db.query('DELETE FROM environments').run() db.query('DELETE FROM teams').run() db.query('DELETE FROM users').run() } catch (err) { console.error('[store] DB cleanup failed in storeReset:', err) } }