From a06b9d50534259303924da1013d941957e42ff1b Mon Sep 17 00:00:00 2001 From: Hibryda Date: Wed, 11 Mar 2026 05:40:28 +0100 Subject: [PATCH] refactor(utils): apply branded types to session-persistence and auto-anchoring --- v2/src/lib/utils/auto-anchoring.ts | 3 ++- v2/src/lib/utils/session-persistence.ts | 17 +++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/v2/src/lib/utils/auto-anchoring.ts b/v2/src/lib/utils/auto-anchoring.ts index f87fd2b..604e5c0 100644 --- a/v2/src/lib/utils/auto-anchoring.ts +++ b/v2/src/lib/utils/auto-anchoring.ts @@ -1,6 +1,7 @@ // Auto-anchoring — creates session anchors on first compaction event // Extracted from agent-dispatcher.ts (SRP: anchor creation concern) +import type { ProjectId as ProjectIdType } from '../types/ids'; import type { AgentMessage } from '../adapters/claude-messages'; import type { SessionAnchor } from '../types/anchors'; import { getAnchorSettings, addAnchors } from '../stores/anchors.svelte'; @@ -11,7 +12,7 @@ import { notify } from '../stores/notifications.svelte'; /** Auto-anchor first N turns on first compaction event for a project */ export function triggerAutoAnchor( - projectId: string, + projectId: ProjectIdType, messages: AgentMessage[], sessionPrompt: string, ): void { diff --git a/v2/src/lib/utils/session-persistence.ts b/v2/src/lib/utils/session-persistence.ts index 3a25d33..da1825f 100644 --- a/v2/src/lib/utils/session-persistence.ts +++ b/v2/src/lib/utils/session-persistence.ts @@ -1,6 +1,7 @@ // Session persistence — maps session IDs to projects/providers and persists state to SQLite // Extracted from agent-dispatcher.ts (SRP: persistence concern) +import type { SessionId as SessionIdType, ProjectId as ProjectIdType } from '../types/ids'; import type { ProviderId } from '../providers/types'; import { getAgentSession } from '../stores/agents.svelte'; import { @@ -11,31 +12,31 @@ import { } from '../adapters/groups-bridge'; // Map sessionId -> projectId for persistence routing -const sessionProjectMap = new Map(); +const sessionProjectMap = new Map(); // Map sessionId -> provider for message adapter routing -const sessionProviderMap = new Map(); +const sessionProviderMap = new Map(); // Map sessionId -> start timestamp for metrics -const sessionStartTimes = new Map(); +const sessionStartTimes = new Map(); // In-flight persistence counter — prevents teardown from racing with async saves let pendingPersistCount = 0; -export function registerSessionProject(sessionId: string, projectId: string, provider: ProviderId = 'claude'): void { +export function registerSessionProject(sessionId: SessionIdType, projectId: ProjectIdType, provider: ProviderId = 'claude'): void { sessionProjectMap.set(sessionId, projectId); sessionProviderMap.set(sessionId, provider); } -export function getSessionProjectId(sessionId: string): string | undefined { +export function getSessionProjectId(sessionId: SessionIdType): ProjectIdType | undefined { return sessionProjectMap.get(sessionId); } -export function getSessionProvider(sessionId: string): ProviderId { +export function getSessionProvider(sessionId: SessionIdType): ProviderId { return sessionProviderMap.get(sessionId) ?? 'claude'; } -export function recordSessionStart(sessionId: string): void { +export function recordSessionStart(sessionId: SessionIdType): void { sessionStartTimes.set(sessionId, Date.now()); } @@ -47,7 +48,7 @@ export async function waitForPendingPersistence(): Promise { } /** Persist session state + messages to SQLite for the project that owns this session */ -export async function persistSessionForProject(sessionId: string): Promise { +export async function persistSessionForProject(sessionId: SessionIdType): Promise { const projectId = sessionProjectMap.get(sessionId); if (!projectId) return; // Not a project-scoped session