Extract branch sanitization to shared contracts module

Move sanitizeBranchFragment and sanitizeFeatureBranchName from three
duplicate definitions (CodexTextGeneration, GitManager, GitActionsControl)
into packages/contracts/src/git.ts. Remove the redundant double-sanitization
of branch names between the TextGeneration and GitManager layers —
CodexTextGeneration now owns sanitization of model output while GitManager
trusts the result and only sanitizes the custom commit message path.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
This commit is contained in:
Julius MarmingeandClaude Opus 4.6 authored and Julius Marminge committed 2026-03-02 20:46:05 -08:00
1 parent 78d89e3d35
commit b1608f41ad
4 files changed
+47 -79

No files matched your search

@@ -3,6 +3,8 @@ import { randomUUID } from "node:crypto";
import { Effect, FileSystem, Layer, Option, Path, Schema, Stream } from "effect";
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
import { sanitizeBranchFragment, sanitizeFeatureBranchName } from "@t3tools/contracts";
import { resolveAttachmentPath } from "../../attachmentStore.ts";
import { ServerConfig } from "../../config.ts";
import { TextGenerationError } from "../Errors.ts";
@@ -93,32 +95,6 @@ function sanitizePrTitle(raw: string): string {
return "Update project changes";
}
function sanitizeBranchName(raw: string): string {
const normalized = raw
.trim()
.toLowerCase()
.replace(/['"`]/g, "")
.replace(/^[./\s_-]+|[./\s_-]+$/g, "");
const branchFragment = normalized
.replace(/[^a-z0-9/_-]+/g, "-")
.replace(/\/+/g, "/")
.replace(/-+/g, "-")
.replace(/^[./_-]+|[./_-]+$/g, "")
.slice(0, 64)
.replace(/[./_-]+$/g, "");
return branchFragment.length > 0 ? branchFragment : "update";
}
function sanitizeFeatureBranchName(raw: string): string {
const sanitized = sanitizeBranchName(raw);
if (sanitized.includes("/")) {
return sanitized.startsWith("feature/") ? sanitized : `feature/${sanitized}`;
}
return `feature/${sanitized}`;
}
const makeCodexTextGeneration = Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
@@ -475,7 +451,7 @@ const makeCodexTextGeneration = Effect.gen(function* () {
});
return {
branch: sanitizeBranchName(generated.branch),
branch: sanitizeBranchFragment(generated.branch),
} satisfies BranchNameGenerationResult;
});
};
+3 -30
View File
@@ -1,6 +1,7 @@
import { randomUUID } from "node:crypto";
import { Effect, FileSystem, Layer, Path } from "effect";
import { sanitizeFeatureBranchName } from "@t3tools/contracts";
import { GitManagerError } from "../Errors.ts";
import { GitManager, type GitManagerShape } from "../Services/GitManager.ts";
@@ -104,32 +105,6 @@ function sanitizeCommitMessage(generated: {
};
}
function sanitizeBranchFragment(raw: string): string {
const normalized = raw
.trim()
.toLowerCase()
.replace(/['"`]/g, "")
.replace(/^[./\s_-]+|[./\s_-]+$/g, "");
const branchFragment = normalized
.replace(/[^a-z0-9/_-]+/g, "-")
.replace(/\/+/g, "/")
.replace(/-+/g, "-")
.replace(/^[./_-]+|[./_-]+$/g, "")
.slice(0, 64)
.replace(/[./_-]+$/g, "");
return branchFragment.length > 0 ? branchFragment : "update";
}
function sanitizeFeatureBranchName(raw: string): string {
const sanitized = sanitizeBranchFragment(raw);
if (sanitized.includes("/")) {
return sanitized.startsWith("feature/") ? sanitized : `feature/${sanitized}`;
}
return `feature/${sanitized}`;
}
function formatCommitMessage(subject: string, body: string): string {
const trimmedBody = body.trim();
if (trimmedBody.length === 0) {
@@ -324,16 +299,14 @@ export const makeGitManager = Effect.gen(function* () {
branch: input.branch,
stagedSummary: limitContext(context.stagedSummary, 8_000),
stagedPatch: limitContext(context.stagedPatch, 50_000),
...(input.includeBranch !== undefined ? { includeBranch: input.includeBranch } : {}),
...(input.includeBranch ? { includeBranch: true } : {}),
})
.pipe(Effect.map((result) => sanitizeCommitMessage(result)));
return {
subject: generated.subject,
body: generated.body,
...(generated.branch !== undefined
? { branch: sanitizeFeatureBranchName(generated.branch) }
: {}),
...(generated.branch !== undefined ? { branch: generated.branch } : {}),
commitMessage: formatCommitMessage(generated.subject, generated.body),
};
});
@@ -1,7 +1,8 @@
import type {
GitRunStackedActionResult,
GitStackedAction,
GitStatusResult,
import {
type GitRunStackedActionResult,
type GitStackedAction,
type GitStatusResult,
sanitizeBranchFragment,
} from "@t3tools/contracts";
export type GitActionIconName = "commit" | "push" | "pr";
@@ -321,24 +322,6 @@ export function resolveDefaultBranchActionDialogCopy(input: {
};
}
function sanitizeBranchFragment(raw: string): string {
const normalized = raw
.trim()
.toLowerCase()
.replace(/['"`]/g, "")
.replace(/^[./\s_-]+|[./\s_-]+$/g, "");
const branchFragment = normalized
.replace(/[^a-z0-9/_-]+/g, "-")
.replace(/\/+/g, "/")
.replace(/-+/g, "-")
.replace(/^[./_-]+|[./_-]+$/g, "")
.slice(0, 64)
.replace(/[./_-]+$/g, "");
return branchFragment.length > 0 ? branchFragment : "update";
}
export function resolveAutoFeatureBranchName(
existingBranchNames: readonly string[],
preferredBranch?: string,
+36
View File
@@ -179,3 +179,39 @@ export const GitPullResult = Schema.Struct({
upstreamBranch: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
});
export type GitPullResult = typeof GitPullResult.Type;
// Branch name utilities
/**
* Sanitize an arbitrary string into a valid, lowercase git branch fragment.
* Strips quotes, collapses separators, limits to 64 chars.
*/
export function sanitizeBranchFragment(raw: string): string {
const normalized = raw
.trim()
.toLowerCase()
.replace(/['"`]/g, "")
.replace(/^[./\s_-]+|[./\s_-]+$/g, "");
const branchFragment = normalized
.replace(/[^a-z0-9/_-]+/g, "-")
.replace(/\/+/g, "/")
.replace(/-+/g, "-")
.replace(/^[./_-]+|[./_-]+$/g, "")
.slice(0, 64)
.replace(/[./_-]+$/g, "");
return branchFragment.length > 0 ? branchFragment : "update";
}
/**
* Sanitize a string into a `feature/…` branch name.
* Preserves an existing `feature/` prefix or slash-separated namespace.
*/
export function sanitizeFeatureBranchName(raw: string): string {
const sanitized = sanitizeBranchFragment(raw);
if (sanitized.includes("/")) {
return sanitized.startsWith("feature/") ? sanitized : `feature/${sanitized}`;
}
return `feature/${sanitized}`;
}