Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,29 @@
namespace SharpClaw.Code.Infrastructure.Services;

/// <inheritdoc />
/// <param name="pathService">Path service.</param>
/// <param name="userProfilePaths">User profile paths.</param>
/// <param name="hostContextAccessor">Host context accessor.</param>
/// <param name="caseInsensitivePaths">
/// Whether workspace paths compare case-insensitively when deriving hosted storage keys.
/// Defaults to <see langword="true"/> on Windows.
/// </param>
public sealed class RuntimeStoragePathResolver(
IPathService pathService,
IUserProfilePaths userProfilePaths,
IRuntimeHostContextAccessor hostContextAccessor) : IRuntimeStoragePathResolver
IRuntimeHostContextAccessor hostContextAccessor,
bool? caseInsensitivePaths = null) : IRuntimeStoragePathResolver
{
private readonly bool foldPathCase = caseInsensitivePaths ?? OperatingSystem.IsWindows();

/// <inheritdoc />
public string GetSharpClawRoot(string workspacePath)
{
var workspace = pathService.GetFullPath(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)
Expand Down Expand Up @@ -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}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,43 @@ public void Session_ids_must_be_a_single_path_segment(string sessionId)
act.Should().Throw<ArgumentException>();
}

[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);

Expand Down