Compare commits

...
Author SHA1 Message Date
Cursor AgentandJulius Marminge 2d8abd6312 Fix deterministic random helper typing
Co-authored-by: Julius Marminge <[email protected]>
2026-05-06 03:32:40 +00:00
Cursor AgentandJulius Marminge ad5b3a6a29 Use Effect services in shared git helpers
Co-authored-by: Julius Marminge <[email protected]>
2026-05-06 03:29:38 +00:00
5 changed files with 116 additions and 38 deletions

No files matched your search

+2 -1
View File
@@ -72,6 +72,7 @@ import {
togglePendingUserInputOptionSelection,
type PendingUserInputDraftAnswer,
} from "../pendingUserInput";
import * as Effect from "effect/Effect";
import {
selectProjectsAcrossEnvironments,
selectThreadsAcrossEnvironments,
@@ -2845,7 +2846,7 @@ export default function ChatView(props: ChatViewProps) {
prepareWorktree: {
projectCwd: activeProject.cwd,
baseBranch: baseBranchForWorktree,
branch: buildTemporaryWorktreeBranchName(),
branch: Effect.runSync(buildTemporaryWorktreeBranchName),
},
runSetupScript: true,
}
+39 -4
View File
@@ -1,5 +1,7 @@
import type { VcsStatusRemoteResult, VcsStatusResult } from "@t3tools/contracts";
import { describe, expect, it } from "vitest";
import { assert, describe, expect, it } from "@effect/vitest";
import { Effect } from "effect";
import * as Random from "effect/Random";
import {
applyGitStatusStreamEvent,
@@ -10,6 +12,22 @@ import {
WORKTREE_BRANCH_PREFIX,
} from "./git.ts";
function deterministicRandom(seed = 0x1234_5678): {
nextIntUnsafe: () => number;
nextDoubleUnsafe: () => number;
} {
let state = seed >>> 0;
const nextIntUnsafe = (): number => {
state = (Math.imul(1_664_525, state) + 1_013_904_223) >>> 0;
return state;
};
return {
nextIntUnsafe,
nextDoubleUnsafe: () => nextIntUnsafe() / 0x1_0000_0000,
};
}
describe("normalizeGitRemoteUrl", () => {
it("canonicalizes equivalent GitHub remotes across protocol variants", () => {
expect(normalizeGitRemoteUrl("[email protected]:T3Tools/T3Code.git")).toBe(
@@ -54,9 +72,26 @@ describe("parseGitHubRepositoryNameWithOwnerFromRemoteUrl", () => {
});
describe("isTemporaryWorktreeBranch", () => {
it("matches the generated temporary worktree refName format", () => {
expect(isTemporaryWorktreeBranch(buildTemporaryWorktreeBranchName())).toBe(true);
});
it.effect("matches the generated temporary worktree refName format", () =>
Effect.gen(function* () {
const branch = yield* buildTemporaryWorktreeBranchName;
assert.equal(isTemporaryWorktreeBranch(branch), true);
}),
);
it.effect("uses the Effect Random service for deterministic temporary refs", () =>
Effect.gen(function* () {
const firstBranch = yield* buildTemporaryWorktreeBranchName.pipe(
Effect.provideService(Random.Random, deterministicRandom()),
);
const secondBranch = yield* buildTemporaryWorktreeBranchName.pipe(
Effect.provideService(Random.Random, deterministicRandom()),
);
assert.equal(firstBranch, secondBranch);
assert.equal(isTemporaryWorktreeBranch(firstBranch), true);
}),
);
it("matches generated temporary worktree refs", () => {
expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef`)).toBe(true);
+16 -7
View File
@@ -7,6 +7,7 @@ import type {
VcsStatusStreamEvent,
} from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
import * as Random from "effect/Random";
import { detectSourceControlProviderFromRemoteUrl } from "./sourceControl.ts";
@@ -86,10 +87,12 @@ export function deriveLocalBranchNameFromRemoteRef(branchName: string): string {
return branchName.slice(firstSeparatorIndex + 1);
}
export function buildTemporaryWorktreeBranchName(): string {
const token = Effect.runSync(Random.nextUUIDv4).replace(/-/g, "").slice(0, 8).toLowerCase();
return `${WORKTREE_BRANCH_PREFIX}/${token}`;
}
export const buildTemporaryWorktreeBranchName = Random.nextUUIDv4.pipe(
Effect.map((uuid) => {
const token = uuid.replace(/-/g, "").slice(0, 8).toLowerCase();
return `${WORKTREE_BRANCH_PREFIX}/${token}`;
}),
);
export function isTemporaryWorktreeBranch(refName: string): boolean {
return TEMP_WORKTREE_BRANCH_PATTERN.test(refName.trim().toLowerCase());
@@ -131,10 +134,12 @@ export function normalizeGitRemoteUrl(value: string): string {
/**
* Best-effort parse of a GitHub `owner/repo` identifier from common remote URL shapes.
*/
export function parseGitHubRepositoryNameWithOwnerFromRemoteUrl(url: string | null): string | null {
export function parseGitHubRepositoryNameWithOwnerFromRemoteUrlOption(
url: string | null,
): Option.Option<string> {
const trimmed = url?.trim() ?? "";
if (trimmed.length === 0) {
return null;
return Option.none();
}
const match =
@@ -142,7 +147,11 @@ export function parseGitHubRepositoryNameWithOwnerFromRemoteUrl(url: string | nu
trimmed,
);
const repositoryNameWithOwner = match?.[1]?.trim() ?? "";
return repositoryNameWithOwner.length > 0 ? repositoryNameWithOwner : null;
return repositoryNameWithOwner.length > 0 ? Option.some(repositoryNameWithOwner) : Option.none();
}
export function parseGitHubRepositoryNameWithOwnerFromRemoteUrl(url: string | null): string | null {
return Option.getOrNull(parseGitHubRepositoryNameWithOwnerFromRemoteUrlOption(url));
}
function deriveLocalBranchNameCandidatesFromRemoteRef(
+44 -20
View File
@@ -1,59 +1,83 @@
import { describe, expect, it } from "vitest";
import { assert, describe, it } from "@effect/vitest";
import { Option } from "effect";
import {
detectSourceControlProviderFromRemoteUrl,
getChangeRequestTerminologyForKind,
parseRemoteHostForSourceControl,
resolveChangeRequestPresentation,
} from "./sourceControl.ts";
describe("source control presentation", () => {
it("uses merge request terminology for GitLab", () => {
expect(getChangeRequestTerminologyForKind("gitlab")).toEqual({
assert.deepEqual(getChangeRequestTerminologyForKind("gitlab"), {
shortLabel: "MR",
singular: "merge request",
});
});
it("uses pull request terminology for GitHub-compatible providers", () => {
expect(getChangeRequestTerminologyForKind("github")).toEqual({
assert.deepEqual(getChangeRequestTerminologyForKind("github"), {
shortLabel: "PR",
singular: "pull request",
});
expect(getChangeRequestTerminologyForKind("azure-devops")).toEqual({
assert.deepEqual(getChangeRequestTerminologyForKind("azure-devops"), {
shortLabel: "PR",
singular: "pull request",
});
expect(getChangeRequestTerminologyForKind("bitbucket")).toEqual({
assert.deepEqual(getChangeRequestTerminologyForKind("bitbucket"), {
shortLabel: "PR",
singular: "pull request",
});
});
it("falls back to generic change request copy for unknown providers", () => {
expect(
resolveChangeRequestPresentation({ kind: "unknown", name: "forge", baseUrl: "" }),
).toEqual(
expect.objectContaining({
shortName: "change request",
longName: "change request",
}),
);
const presentation = resolveChangeRequestPresentation({
kind: "unknown",
name: "forge",
baseUrl: "",
});
assert.equal(presentation.shortName, "change request");
assert.equal(presentation.longName, "change request");
});
});
describe("detectSourceControlProviderFromRemoteUrl", () => {
it("detects common source control hosts", () => {
expect(detectSourceControlProviderFromRemoteUrl("[email protected]:owner/repo.git")?.kind).toBe(
assert.equal(
detectSourceControlProviderFromRemoteUrl("[email protected]:owner/repo.git")?.kind,
"github",
);
expect(
assert.equal(
detectSourceControlProviderFromRemoteUrl("https://gitlab.com/group/repo.git")?.kind,
).toBe("gitlab");
expect(
"gitlab",
);
assert.equal(
detectSourceControlProviderFromRemoteUrl("https://dev.azure.com/org/project/_git/repo")?.kind,
).toBe("azure-devops");
expect(
"azure-devops",
);
assert.equal(
detectSourceControlProviderFromRemoteUrl("[email protected]:workspace/repo.git")?.kind,
).toBe("bitbucket");
"bitbucket",
);
});
});
describe("parseRemoteHostOption", () => {
it("returns parsed hosts as options", () => {
assert.deepEqual(
parseRemoteHostForSourceControl("[email protected]:owner/repo.git"),
Option.some("github.com"),
);
assert.deepEqual(
parseRemoteHostForSourceControl("https://gitlab.com/group/repo.git"),
Option.some("gitlab.com"),
);
});
it("returns none for empty or invalid remotes", () => {
assert.equal(Option.isNone(parseRemoteHostForSourceControl(" ")), true);
assert.equal(Option.isNone(parseRemoteHostForSourceControl("not a url")), true);
});
});
+15 -6
View File
@@ -1,4 +1,5 @@
import type { SourceControlProviderInfo, SourceControlProviderKind } from "@t3tools/contracts";
import { Option } from "effect";
export interface ChangeRequestPresentation {
readonly icon: "github" | "gitlab" | "azure-devops" | "bitbucket" | "change-request";
@@ -133,28 +134,36 @@ export function getChangeRequestTerminologyForKind(
};
}
function parseRemoteHost(remoteUrl: string): string | null {
function parseRemoteHostOption(remoteUrl: string): Option.Option<string> {
const trimmed = remoteUrl.trim();
if (trimmed.length === 0) {
return null;
return Option.none();
}
if (trimmed.startsWith("git@")) {
const hostWithPath = trimmed.slice("git@".length);
const separatorIndex = hostWithPath.search(/[:/]/);
if (separatorIndex <= 0) {
return null;
return Option.none();
}
return hostWithPath.slice(0, separatorIndex).toLowerCase();
return Option.some(hostWithPath.slice(0, separatorIndex).toLowerCase());
}
try {
return new URL(trimmed).hostname.toLowerCase();
return Option.some(new URL(trimmed).hostname.toLowerCase());
} catch {
return null;
return Option.none();
}
}
function parseRemoteHost(remoteUrl: string): string | null {
return Option.getOrNull(parseRemoteHostOption(remoteUrl));
}
export function parseRemoteHostForSourceControl(remoteUrl: string): Option.Option<string> {
return parseRemoteHostOption(remoteUrl);
}
function toBaseUrl(host: string): string {
return `https://${host}`;
}