diff --git a/src/SharpClaw.Code.Infrastructure/Services/RuntimeStoragePathResolver.cs b/src/SharpClaw.Code.Infrastructure/Services/RuntimeStoragePathResolver.cs index 55b842c..b86c893 100644 --- a/src/SharpClaw.Code.Infrastructure/Services/RuntimeStoragePathResolver.cs +++ b/src/SharpClaw.Code.Infrastructure/Services/RuntimeStoragePathResolver.cs @@ -6,11 +6,21 @@ namespace SharpClaw.Code.Infrastructure.Services; /// +/// Path service. +/// User profile paths. +/// Host context accessor. +/// +/// Whether workspace paths compare case-insensitively when deriving hosted storage keys. +/// Defaults to on Windows. +/// public sealed class RuntimeStoragePathResolver( IPathService pathService, IUserProfilePaths userProfilePaths, - IRuntimeHostContextAccessor hostContextAccessor) : IRuntimeStoragePathResolver + IRuntimeHostContextAccessor hostContextAccessor, + bool? caseInsensitivePaths = null) : IRuntimeStoragePathResolver { + private readonly bool foldPathCase = caseInsensitivePaths ?? OperatingSystem.IsWindows(); + /// public string GetSharpClawRoot(string workspacePath) { @@ -18,10 +28,7 @@ public string GetSharpClawRoot(string workspacePath) var hostContext = hostContextAccessor.Current; var baseRoot = string.IsNullOrWhiteSpace(hostContext?.StorageRoot) ? workspace - : pathService.Combine( - pathService.GetFullPath(hostContext.StorageRoot!), - "workspaces", - BuildWorkspaceKey(workspace)); + : ResolveHostedWorkspaceRoot(pathService.GetFullPath(hostContext.StorageRoot!), workspace); var sharpClawRoot = pathService.Combine(baseRoot, ".sharpclaw"); return string.IsNullOrWhiteSpace(hostContext?.TenantId) @@ -142,12 +149,28 @@ public string GetUserSharpClawRoot() : pathService.Combine(root, "tenants", BuildTenantSegment(hostContext!.TenantId!)); } - private string BuildWorkspaceKey(string workspacePath) + private string ResolveHostedWorkspaceRoot(string storageRoot, string workspace) + { + var workspacesRoot = pathService.Combine(storageRoot, "workspaces"); + var current = pathService.Combine(workspacesRoot, BuildWorkspaceKey(workspace, foldPathCase)); + if (!foldPathCase || Directory.Exists(current)) + { + return current; + } + + // Before keys were case-folded, "C:\Repo" and "c:\repo" hashed differently. Keep using a root created + // under the old, exact-case key so existing sessions do not disappear. + var legacy = pathService.Combine(workspacesRoot, BuildWorkspaceKey(workspace, foldCase: false)); + return Directory.Exists(legacy) ? legacy : current; + } + + private string BuildWorkspaceKey(string workspacePath, bool foldCase) { var normalized = pathService.GetFullPath(workspacePath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); var name = pathService.GetFileName(normalized); var slug = string.IsNullOrWhiteSpace(name) ? "workspace" : SanitizeSegment(name); - var hash = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(normalized))).ToLowerInvariant()[..12]; + var hashInput = foldCase ? normalized.ToUpperInvariant() : normalized; + var hash = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(hashInput))).ToLowerInvariant()[..12]; return $"{slug}-{hash}"; } diff --git a/tests/SharpClaw.Code.UnitTests/Infrastructure/RuntimeStoragePathResolverTests.cs b/tests/SharpClaw.Code.UnitTests/Infrastructure/RuntimeStoragePathResolverTests.cs index 0729e55..422463c 100644 --- a/tests/SharpClaw.Code.UnitTests/Infrastructure/RuntimeStoragePathResolverTests.cs +++ b/tests/SharpClaw.Code.UnitTests/Infrastructure/RuntimeStoragePathResolverTests.cs @@ -50,6 +50,43 @@ public void Session_ids_must_be_a_single_path_segment(string sessionId) act.Should().Throw(); } + [Fact] + public void Hosted_workspace_keys_ignore_path_casing_when_paths_are_case_insensitive() + { + var storageRoot = Path.Combine(userRoot, "hosted"); + var resolver = new RuntimeStoragePathResolver(pathService, new FixedUserProfilePaths(userRoot), accessor, caseInsensitivePaths: true); + using var scope = accessor.BeginScope(new RuntimeHostContext("test-host", StorageRoot: storageRoot)); + + var upper = resolver.GetSharpClawRoot(Path.Combine(userRoot, "Work", "Repo")); + var lower = resolver.GetSharpClawRoot(Path.Combine(userRoot, "work", "repo")); + + upper.ToUpperInvariant().Should().Be(lower.ToUpperInvariant(), "C:\\Repo and c:\\repo are the same workspace on Windows"); + } + + [Fact] + public void Hosted_workspace_keys_keep_using_an_existing_legacy_exact_case_root() + { + var storageRoot = Path.Combine(userRoot, "hosted-legacy"); + var workspacePath = Path.Combine(userRoot, "Work", "Repo"); + using var scope = accessor.BeginScope(new RuntimeHostContext("test-host", StorageRoot: storageRoot)); + var legacyRoot = new RuntimeStoragePathResolver(pathService, new FixedUserProfilePaths(userRoot), accessor, caseInsensitivePaths: false) + .GetSharpClawRoot(workspacePath); + var legacyWorkspaceDirectory = Path.GetDirectoryName(legacyRoot)!; + Directory.CreateDirectory(legacyWorkspaceDirectory); + + try + { + var resolved = new RuntimeStoragePathResolver(pathService, new FixedUserProfilePaths(userRoot), accessor, caseInsensitivePaths: true) + .GetSharpClawRoot(workspacePath); + + resolved.Should().Be(legacyRoot, "existing hosted sessions must not disappear after the key change"); + } + finally + { + Directory.Delete(userRoot, recursive: true); + } + } + private RuntimeStoragePathResolver CreateResolver() => new(pathService, new FixedUserProfilePaths(userRoot), accessor);