源码快照 @ 2026-09-01 (source-only, 剔除非源码素材)
Publish Claude Code Binaries / Stage and publish binaries (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-arm64 (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build linux-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build windows-amd64 (push) Canceled after 0s
Sync Tutti Agent Skills / sync (push) Canceled after 0s
Windows Agent Adapters / Test Windows agent adapters (push) Canceled after 0s
Windows Daemon Adapters / Test Windows daemon adapters (push) Canceled after 0s
Publish Tutti App Runtime / Publish runtime catalog (push) Canceled after 0s
Publish Claude Code Binaries / Stage and publish binaries (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-arm64 (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build linux-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build windows-amd64 (push) Canceled after 0s
Sync Tutti Agent Skills / sync (push) Canceled after 0s
Windows Agent Adapters / Test Windows agent adapters (push) Canceled after 0s
Windows Daemon Adapters / Test Windows daemon adapters (push) Canceled after 0s
Publish Tutti App Runtime / Publish runtime catalog (push) Canceled after 0s
This commit is contained in:
commit
2ed3415fb7
8018 files changed
+1936476
No files matched your search
@@ -0,0 +1,66 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
extractAgentPatchPath,
|
||||
inferAgentPatchChangeType,
|
||||
normalizeAgentPatchText
|
||||
} from "./agentPatchMetadata";
|
||||
|
||||
describe("agentPatchMetadata", () => {
|
||||
it("canonicalizes provider no-newline markers with leading whitespace", () => {
|
||||
expect(
|
||||
normalizeAgentPatchText(
|
||||
"@@ -1 +1 @@\n-old\n \\ No newline at end of file\n+new\n \\ No newline at end of file"
|
||||
)
|
||||
).toBe(
|
||||
"@@ -1 +1 @@\n-old\n\\ No newline at end of file\n+new\n\\ No newline at end of file"
|
||||
);
|
||||
});
|
||||
|
||||
it("extracts metadata from modified git diffs", () => {
|
||||
const diffText = [
|
||||
"diff --git a/src/a.ts b/src/a.ts",
|
||||
"index 1111111..2222222 100644",
|
||||
"--- a/src/a.ts",
|
||||
"+++ b/src/a.ts",
|
||||
"@@ -1 +1 @@",
|
||||
"-const ready = false",
|
||||
"+const ready = true"
|
||||
].join("\n");
|
||||
|
||||
expect(extractAgentPatchPath(diffText)).toBe("src/a.ts");
|
||||
expect(inferAgentPatchChangeType(diffText)).toBe("modified");
|
||||
});
|
||||
|
||||
it("extracts metadata from deleted git diffs", () => {
|
||||
const deletedDiff = [
|
||||
"diff --git a/a.md b/a.md",
|
||||
"deleted file mode 100644",
|
||||
"--- a/a.md",
|
||||
"+++ /dev/null",
|
||||
"@@ -1 +0,0 @@",
|
||||
"-aaaaa"
|
||||
].join("\n");
|
||||
|
||||
expect(extractAgentPatchPath(deletedDiff)).toBe("a.md");
|
||||
expect(inferAgentPatchChangeType(deletedDiff)).toBe("deleted");
|
||||
});
|
||||
|
||||
it("extracts metadata from apply_patch patches", () => {
|
||||
const deletedPatch = [
|
||||
"*** Begin Patch",
|
||||
"*** Delete File: a.md",
|
||||
"@@",
|
||||
"-aaaaa",
|
||||
"*** End Patch"
|
||||
].join("\n");
|
||||
|
||||
expect(extractAgentPatchPath(deletedPatch)).toBe("a.md");
|
||||
expect(inferAgentPatchChangeType(deletedPatch)).toBe("deleted");
|
||||
});
|
||||
|
||||
it("normalizes JSON content wrappers and escaped newlines", () => {
|
||||
const wrapped = JSON.stringify({ content: "a\\nb" });
|
||||
|
||||
expect(normalizeAgentPatchText(wrapped)).toBe("a\nb");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,115 @@
|
||||
export type AgentPatchChangeType = "created" | "modified" | "deleted";
|
||||
|
||||
export function normalizeAgentPatchText(text: string): string {
|
||||
if (!text) {
|
||||
return text;
|
||||
}
|
||||
const contentField = tryExtractContentField(text);
|
||||
if (contentField) {
|
||||
return normalizeAgentPatchText(contentField);
|
||||
}
|
||||
const expanded =
|
||||
!text.includes("\n") && text.includes("\\n")
|
||||
? text.replace(/\\n/g, "\n")
|
||||
: text;
|
||||
return expanded
|
||||
.replace(/\r\n/g, "\n")
|
||||
.split("\n")
|
||||
.map((line) =>
|
||||
line.trimStart() === "\\ No newline at end of file"
|
||||
? "\\ No newline at end of file"
|
||||
: line
|
||||
)
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
export function extractAgentPatchPath(patchText: string | null): string | null {
|
||||
const normalized = normalizeAgentPatchText(patchText ?? "");
|
||||
if (!normalized.trim()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const applyPatchPath = extractApplyPatchPath(normalized);
|
||||
if (applyPatchPath) {
|
||||
return applyPatchPath;
|
||||
}
|
||||
|
||||
const changeType = inferAgentPatchChangeType(normalized);
|
||||
const gitHeader = normalized.match(/^diff --git a\/(.+?) b\/(.+)$/m);
|
||||
const oldPath = gitHeader?.[1]?.trim() ?? null;
|
||||
const newPath = gitHeader?.[2]?.trim() ?? null;
|
||||
if (oldPath || newPath) {
|
||||
return changeType === "deleted"
|
||||
? (nonNullPath(oldPath) ?? nonNullPath(newPath))
|
||||
: (nonNullPath(newPath) ?? nonNullPath(oldPath));
|
||||
}
|
||||
|
||||
if (changeType === "deleted") {
|
||||
return extractOldHeaderPath(normalized);
|
||||
}
|
||||
return extractNewHeaderPath(normalized);
|
||||
}
|
||||
|
||||
export function inferAgentPatchChangeType(
|
||||
patchText: string | null
|
||||
): AgentPatchChangeType {
|
||||
const normalized = normalizeAgentPatchText(patchText ?? "").trim();
|
||||
if (!normalized) {
|
||||
return "modified";
|
||||
}
|
||||
if (
|
||||
/^(\*\*\* Delete File:|deleted file mode\b|\+\+\+ \/dev\/null$)/m.test(
|
||||
normalized
|
||||
)
|
||||
) {
|
||||
return "deleted";
|
||||
}
|
||||
if (
|
||||
/^(\*\*\* Add File:|new file mode\b|--- \/dev\/null$)/m.test(normalized)
|
||||
) {
|
||||
return "created";
|
||||
}
|
||||
return "modified";
|
||||
}
|
||||
|
||||
function extractApplyPatchPath(text: string): string | null {
|
||||
const match = text.match(/^\*\*\* (?:Add|Delete|Update) File:\s+(.+?)\s*$/m);
|
||||
return match?.[1]?.trim() || null;
|
||||
}
|
||||
|
||||
function extractOldHeaderPath(text: string): string | null {
|
||||
const match = text.match(/^---\s+(?:a\/)?([^\n]+)$/m);
|
||||
return nonNullPath(match?.[1]?.trim() ?? null);
|
||||
}
|
||||
|
||||
function extractNewHeaderPath(text: string): string | null {
|
||||
const match = text.match(/^\+\+\+\s+(?:b\/)?([^\n]+)$/m);
|
||||
return nonNullPath(match?.[1]?.trim() ?? null);
|
||||
}
|
||||
|
||||
function nonNullPath(path: string | null): string | null {
|
||||
if (!path || path === "/dev/null" || path === "dev/null") {
|
||||
return null;
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
function tryExtractContentField(text: string): string | null {
|
||||
const trimmed = text.trim();
|
||||
if (!trimmed.startsWith("{") || !trimmed.endsWith("}")) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const parsed = JSON.parse(trimmed) as unknown;
|
||||
if (
|
||||
parsed &&
|
||||
typeof parsed === "object" &&
|
||||
typeof (parsed as { content?: unknown }).content === "string"
|
||||
) {
|
||||
return (parsed as { content: string }).content;
|
||||
}
|
||||
return null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildAgentTurnSummaryPatchDiff } from "./agentTurnSummaryPatchDiff";
|
||||
import { isAgentUnifiedDiffText } from "./agentUnifiedDiffValidation";
|
||||
|
||||
describe("buildAgentTurnSummaryPatchDiff", () => {
|
||||
it("wraps cwd-relative hunk diffs with git headers", () => {
|
||||
const diff = buildAgentTurnSummaryPatchDiff({
|
||||
cwd: "/workspace/project",
|
||||
toolCallId: "call-1",
|
||||
changes: [
|
||||
{
|
||||
path: "/workspace/project/src/app.ts",
|
||||
changeType: "modified",
|
||||
unifiedDiff: "@@ -1 +1 @@\n-old\n+new"
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
expect(diff).toBe(
|
||||
"diff --git a/src/app.ts b/src/app.ts\n--- a/src/app.ts\n+++ b/src/app.ts\n@@ -1 +1 @@\n-old\n+new\n"
|
||||
);
|
||||
});
|
||||
|
||||
it("generates add-file diffs from created content", () => {
|
||||
const diff = buildAgentTurnSummaryPatchDiff({
|
||||
cwd: "/workspace/project",
|
||||
toolCallId: "call-1",
|
||||
changes: [
|
||||
{
|
||||
path: "/workspace/project/src/new.ts",
|
||||
changeType: "created",
|
||||
content: "export const ready = true;\n"
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
expect(diff).toBe(
|
||||
"diff --git a/src/new.ts b/src/new.ts\nnew file mode 100644\n--- /dev/null\n+++ b/src/new.ts\n@@ -0,0 +1,1 @@\n+export const ready = true;\n"
|
||||
);
|
||||
});
|
||||
|
||||
it("makes Windows absolute paths relative to the patch cwd", () => {
|
||||
const diff = buildAgentTurnSummaryPatchDiff({
|
||||
cwd: "C:\\Users\\17940\\Documents\\tutti\\test_git",
|
||||
toolCallId: "call-1",
|
||||
changes: [
|
||||
{
|
||||
path: "C:/Users/17940/Documents/tutti/test_git/today.txt",
|
||||
changeType: "created",
|
||||
content: "2026-08-12\n"
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
expect(diff).toBe(
|
||||
"diff --git a/today.txt b/today.txt\nnew file mode 100644\n--- /dev/null\n+++ b/today.txt\n@@ -0,0 +1,1 @@\n+2026-08-12\n"
|
||||
);
|
||||
});
|
||||
|
||||
it("rebases Windows paths in complete unified diffs case-insensitively", () => {
|
||||
const absolutePath = "C:/Users/17940/Documents/tutti/test_git/today.txt";
|
||||
const diff = buildAgentTurnSummaryPatchDiff({
|
||||
cwd: "c:/users/17940/documents/tutti/test_git",
|
||||
toolCallId: "call-1",
|
||||
changes: [
|
||||
{
|
||||
path: absolutePath,
|
||||
changeType: "modified",
|
||||
unifiedDiff: [
|
||||
`diff --git a/${absolutePath} b/${absolutePath}`,
|
||||
`--- a/${absolutePath}`,
|
||||
`+++ b/${absolutePath}`,
|
||||
"@@ -1,2 +1,2 @@",
|
||||
"--- old option",
|
||||
"+++ new option",
|
||||
"-old",
|
||||
"+new"
|
||||
].join("\n")
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
expect(diff).toBe(
|
||||
"diff --git a/today.txt b/today.txt\n--- a/today.txt\n+++ b/today.txt\n@@ -1,2 +1,2 @@\n--- old option\n+++ new option\n-old\n+new\n"
|
||||
);
|
||||
});
|
||||
|
||||
it("builds a valid zero-old-count patch for the reported Kimi empty-file edit", () => {
|
||||
const content =
|
||||
"这是一个示例文本文件。\n\n当前时间:2026-08-20\n用途:测试文件创建\n\n你可以随时修改或删除此文件。";
|
||||
const diff = buildAgentTurnSummaryPatchDiff({
|
||||
cwd: "C:/Users/17940/Documents/tutti/test_git",
|
||||
toolCallId: "kimi-create-note",
|
||||
changes: [
|
||||
{
|
||||
path: "C:/Users/17940/Documents/tutti/test_git/note.txt",
|
||||
changeType: "modified",
|
||||
oldString: "",
|
||||
newString: content
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
expect(new TextEncoder().encode(content)).toHaveLength(132);
|
||||
expect(new TextEncoder().encode(diff)).toHaveLength(218);
|
||||
expect(diff).toContain("@@ -0,0 +1,6 @@");
|
||||
expect(isAgentUnifiedDiffText(diff)).toBe(true);
|
||||
});
|
||||
|
||||
it("normalizes CRLF while preserving a valid zero-new-count patch", () => {
|
||||
const diff = buildAgentTurnSummaryPatchDiff({
|
||||
cwd: "/workspace/project",
|
||||
toolCallId: "clear-file",
|
||||
changes: [
|
||||
{
|
||||
path: "/workspace/project/note.txt",
|
||||
changeType: "modified",
|
||||
oldString: "first\r\nsecond\r\n",
|
||||
newString: ""
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
expect(diff).toContain("@@ -1,2 +0,0 @@");
|
||||
expect(diff).not.toContain("\r");
|
||||
expect(isAgentUnifiedDiffText(diff)).toBe(true);
|
||||
});
|
||||
|
||||
it("fails closed for binary content without an executable binary diff", () => {
|
||||
const diff = buildAgentTurnSummaryPatchDiff({
|
||||
cwd: "/workspace/project",
|
||||
toolCallId: "binary-write",
|
||||
changes: [
|
||||
{
|
||||
path: "/workspace/project/image.bin",
|
||||
changeType: "modified",
|
||||
oldString: "old\0bytes",
|
||||
newString: "new\0bytes"
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
expect(diff).toBe("");
|
||||
});
|
||||
|
||||
it.each([
|
||||
["created", "0000000..e69de29"],
|
||||
["deleted", "e69de29..0000000"]
|
||||
] as const)("builds an exact %s empty-file patch", (changeType, index) => {
|
||||
const diff = buildAgentTurnSummaryPatchDiff({
|
||||
cwd: "/workspace/project",
|
||||
toolCallId: `empty-${changeType}`,
|
||||
changes: [
|
||||
{
|
||||
path: "/workspace/project/empty.txt",
|
||||
changeType,
|
||||
...(changeType === "created"
|
||||
? { newString: "", content: "" }
|
||||
: { oldString: "", content: "" })
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
expect(diff).toContain(`index ${index}`);
|
||||
expect(diff).not.toContain("@@");
|
||||
});
|
||||
it.each([
|
||||
{ oldString: "before\n", newString: undefined },
|
||||
{ oldString: undefined, newString: "after\n" }
|
||||
])(
|
||||
"does not invent a modified-file patch from one-sided content",
|
||||
(change) => {
|
||||
const diff = buildAgentTurnSummaryPatchDiff({
|
||||
cwd: "/workspace/project",
|
||||
toolCallId: "call-1",
|
||||
changes: [
|
||||
{
|
||||
path: "/workspace/project/src/app.ts",
|
||||
changeType: "modified",
|
||||
...change
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
expect(diff).toBe("");
|
||||
}
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,221 @@
|
||||
import type {
|
||||
AgentTurnSummaryPatchBatchVM,
|
||||
AgentTurnSummaryPatchChangeVM
|
||||
} from "../contracts/agentTurnSummaryRowVM";
|
||||
import {
|
||||
isInsideOrEqualWorkspaceFilePath,
|
||||
normalizeWorkspaceFilePath
|
||||
} from "../../../actions/workspaceFilePathCandidate";
|
||||
import { normalizeAgentPatchText } from "./agentPatchMetadata";
|
||||
import { isAgentUnifiedDiffText } from "./agentUnifiedDiffValidation";
|
||||
|
||||
export function buildAgentTurnSummaryPatchDiff(
|
||||
batch: AgentTurnSummaryPatchBatchVM
|
||||
): string {
|
||||
return batch.changes
|
||||
.map((change) => patchChangeToUnifiedDiff(change, batch.cwd))
|
||||
.filter((diff) => diff.trim().length > 0)
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
function patchChangeToUnifiedDiff(
|
||||
change: AgentTurnSummaryPatchChangeVM,
|
||||
cwd: string | null
|
||||
): string {
|
||||
if (patchChangeContainsBinaryContent(change)) {
|
||||
return "";
|
||||
}
|
||||
const path = patchPathRelativeToCwd(change.path, cwd);
|
||||
const rawDiff = normalizeAgentPatchText(change.unifiedDiff ?? "").trim();
|
||||
if (rawDiff && isAgentUnifiedDiffText(rawDiff)) {
|
||||
return ensureTrailingNewline(
|
||||
wrapUnifiedDiff(path, change.changeType, rawDiff, cwd)
|
||||
);
|
||||
}
|
||||
if (change.changeType === "created") {
|
||||
return fileContentPatch(
|
||||
path,
|
||||
"created",
|
||||
change.content ?? change.newString ?? rawDiff
|
||||
);
|
||||
}
|
||||
if (change.changeType === "deleted") {
|
||||
return fileContentPatch(
|
||||
path,
|
||||
"deleted",
|
||||
change.oldString ?? change.content ?? rawDiff
|
||||
);
|
||||
}
|
||||
if (change.oldString != null && change.newString != null) {
|
||||
return modifiedFilePatch(path, change.oldString, change.newString);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function wrapUnifiedDiff(
|
||||
path: string,
|
||||
changeType: AgentTurnSummaryPatchChangeVM["changeType"],
|
||||
diff: string,
|
||||
cwd: string | null
|
||||
): string {
|
||||
if (diff.startsWith("diff --git ")) {
|
||||
return rebaseUnifiedDiffPaths(diff, cwd);
|
||||
}
|
||||
const headers = [`diff --git a/${path} b/${path}`];
|
||||
if (changeType === "created") {
|
||||
headers.push("new file mode 100644", "--- /dev/null", `+++ b/${path}`);
|
||||
} else if (changeType === "deleted") {
|
||||
headers.push("deleted file mode 100644", `--- a/${path}`, "+++ /dev/null");
|
||||
} else if (!diff.startsWith("--- ") && !diff.includes("\n--- ")) {
|
||||
headers.push(`--- a/${path}`, `+++ b/${path}`);
|
||||
}
|
||||
return [...headers, diff].join("\n");
|
||||
}
|
||||
|
||||
function rebaseUnifiedDiffPaths(diff: string, cwd: string | null): string {
|
||||
let inHunk = false;
|
||||
return diff
|
||||
.split("\n")
|
||||
.map((line) => {
|
||||
if (line.startsWith("diff --git ")) {
|
||||
inHunk = false;
|
||||
} else if (line.startsWith("@@ ")) {
|
||||
inHunk = true;
|
||||
return line;
|
||||
} else if (inHunk) {
|
||||
return line;
|
||||
}
|
||||
|
||||
const gitHeader = line.match(/^diff --git a\/(.+?) b\/(.+)$/);
|
||||
if (gitHeader?.[1] && gitHeader[2]) {
|
||||
return `diff --git a/${patchPathRelativeToCwd(
|
||||
gitHeader[1],
|
||||
cwd
|
||||
)} b/${patchPathRelativeToCwd(gitHeader[2], cwd)}`;
|
||||
}
|
||||
|
||||
const fileHeader = line.match(/^(---|\+\+\+) ((?:a\/|b\/)?)(.+)$/);
|
||||
const [, marker, prefix, filePath] = fileHeader ?? [];
|
||||
if (
|
||||
!marker ||
|
||||
prefix === undefined ||
|
||||
!filePath ||
|
||||
filePath === "/dev/null"
|
||||
) {
|
||||
return line;
|
||||
}
|
||||
return `${marker} ${prefix}${patchPathRelativeToCwd(filePath, cwd)}`;
|
||||
})
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
function fileContentPatch(
|
||||
path: string,
|
||||
changeType: "created" | "deleted",
|
||||
content: string
|
||||
): string {
|
||||
if (content === "") {
|
||||
return ensureTrailingNewline(
|
||||
changeType === "created"
|
||||
? [
|
||||
`diff --git a/${path} b/${path}`,
|
||||
"new file mode 100644",
|
||||
"index 0000000..e69de29"
|
||||
].join("\n")
|
||||
: [
|
||||
`diff --git a/${path} b/${path}`,
|
||||
"deleted file mode 100644",
|
||||
"index e69de29..0000000"
|
||||
].join("\n")
|
||||
);
|
||||
}
|
||||
const lines = splitPatchContentLines(content);
|
||||
const count = Math.max(lines.length, 1);
|
||||
const prefix = changeType === "created" ? "+" : "-";
|
||||
const body =
|
||||
lines.length > 0 ? lines.map((line) => `${prefix}${line}`) : [`${prefix}`];
|
||||
const header =
|
||||
changeType === "created"
|
||||
? [
|
||||
`diff --git a/${path} b/${path}`,
|
||||
"new file mode 100644",
|
||||
"--- /dev/null",
|
||||
`+++ b/${path}`,
|
||||
`@@ -0,0 +1,${count} @@`
|
||||
]
|
||||
: [
|
||||
`diff --git a/${path} b/${path}`,
|
||||
"deleted file mode 100644",
|
||||
`--- a/${path}`,
|
||||
"+++ /dev/null",
|
||||
`@@ -1,${count} +0,0 @@`
|
||||
];
|
||||
return ensureTrailingNewline([...header, ...body].join("\n"));
|
||||
}
|
||||
|
||||
function modifiedFilePatch(
|
||||
path: string,
|
||||
oldContent: string,
|
||||
newContent: string
|
||||
): string {
|
||||
const oldLines = splitPatchContentLines(oldContent);
|
||||
const newLines = splitPatchContentLines(newContent);
|
||||
const oldCount = oldLines.length;
|
||||
const newCount = newLines.length;
|
||||
const oldStart = oldCount === 0 ? 0 : 1;
|
||||
const newStart = newCount === 0 ? 0 : 1;
|
||||
return ensureTrailingNewline(
|
||||
[
|
||||
`diff --git a/${path} b/${path}`,
|
||||
`--- a/${path}`,
|
||||
`+++ b/${path}`,
|
||||
`@@ -${oldStart},${oldCount} +${newStart},${newCount} @@`,
|
||||
...oldLines.map((line) => `-${line}`),
|
||||
...newLines.map((line) => `+${line}`)
|
||||
].join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
function patchChangeContainsBinaryContent(
|
||||
change: AgentTurnSummaryPatchChangeVM
|
||||
): boolean {
|
||||
return [
|
||||
change.unifiedDiff,
|
||||
change.content,
|
||||
change.oldString,
|
||||
change.newString
|
||||
].some((value) => value?.includes("\0") === true);
|
||||
}
|
||||
|
||||
function patchPathRelativeToCwd(path: string, cwd: string | null): string {
|
||||
const normalizedCwd = normalizeWorkspaceFilePath(cwd ?? "");
|
||||
const normalizedPath = normalizeWorkspaceFilePath(path, normalizedCwd);
|
||||
if (
|
||||
isAbsolutePatchPath(normalizedPath) &&
|
||||
normalizedCwd &&
|
||||
isInsideOrEqualWorkspaceFilePath(normalizedPath, normalizedCwd)
|
||||
) {
|
||||
const relativePath = normalizedPath
|
||||
.slice(normalizedCwd.length)
|
||||
.replace(/^\/+/, "");
|
||||
if (relativePath) {
|
||||
return relativePath;
|
||||
}
|
||||
}
|
||||
return normalizedPath.replace(/^\/+/, "");
|
||||
}
|
||||
|
||||
function isAbsolutePatchPath(path: string): boolean {
|
||||
return path.startsWith("/") || /^\/?[A-Za-z]:\//.test(path);
|
||||
}
|
||||
|
||||
function splitPatchContentLines(content: string): string[] {
|
||||
if (!content) {
|
||||
return [];
|
||||
}
|
||||
return content.replace(/\r\n/g, "\n").replace(/\n$/, "").split("\n");
|
||||
}
|
||||
|
||||
function ensureTrailingNewline(value: string): string {
|
||||
return value.endsWith("\n") ? value : `${value}\n`;
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
patchBatchDirectoryCwd,
|
||||
resolvePatchExecutionCwd
|
||||
} from "./agentTurnSummaryPatchRuntime";
|
||||
|
||||
describe("Agent Turn Summary patch cwd paths", () => {
|
||||
it("maps Git Bash drive paths to the Windows workspace drive", () => {
|
||||
const workspaceRoot = "C:\\Users\\demo\\project";
|
||||
const cwd = "/c/Users/demo/project";
|
||||
|
||||
expect(
|
||||
patchBatchDirectoryCwd(
|
||||
`${cwd}/README.md`,
|
||||
[{ path: `${cwd}/README.md` }],
|
||||
workspaceRoot
|
||||
)
|
||||
).toBe("C:/Users/demo/project");
|
||||
expect(resolvePatchExecutionCwd(cwd, workspaceRoot)).toBe(
|
||||
"C:/Users/demo/project"
|
||||
);
|
||||
});
|
||||
|
||||
it("preserves POSIX /c paths when the workspace root is POSIX", () => {
|
||||
expect(
|
||||
resolvePatchExecutionCwd("/c/Users/demo/project", "/Users/demo/project")
|
||||
).toBe("/c/Users/demo/project");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,135 @@
|
||||
import { formatWorkspaceFilePathForDisplay } from "@tutti-os/workspace-file-manager/services";
|
||||
import type {
|
||||
AgentTurnSummaryFileVM,
|
||||
AgentTurnSummaryPatchChangeVM
|
||||
} from "../contracts/agentTurnSummaryRowVM";
|
||||
import { normalizeWorkspaceFilePath } from "../../../actions/workspaceFilePathCandidate";
|
||||
|
||||
export function fileCanBuildPatch(file: AgentTurnSummaryFileVM): boolean {
|
||||
if (file.unifiedDiff != null) {
|
||||
return true;
|
||||
}
|
||||
if (file.changeType === "created") {
|
||||
return file.content != null || file.newString != null;
|
||||
}
|
||||
if (file.changeType === "deleted") {
|
||||
return file.oldString != null || file.content != null;
|
||||
}
|
||||
return file.oldString != null && file.newString != null;
|
||||
}
|
||||
|
||||
export function patchBatchDirectoryCwd(
|
||||
cwd: string | null,
|
||||
changes: readonly Pick<AgentTurnSummaryPatchChangeVM, "path">[],
|
||||
workspaceRoot?: string | null
|
||||
): string | null {
|
||||
const normalizedCwd = normalizePatchHostPath(cwd ?? "", workspaceRoot);
|
||||
if (!normalizedCwd) {
|
||||
return null;
|
||||
}
|
||||
const cwdIsChangedFilePath = changes.some(
|
||||
(change) =>
|
||||
normalizePatchHostPath(change.path, workspaceRoot) === normalizedCwd
|
||||
);
|
||||
return cwdIsChangedFilePath
|
||||
? dirnameForPatchHostPath(normalizedCwd)
|
||||
: normalizedCwd;
|
||||
}
|
||||
|
||||
export function resolvePatchExecutionCwd(
|
||||
cwd: string | null,
|
||||
workspaceRoot?: string | null
|
||||
): string | null {
|
||||
const normalizedRoot = normalizePatchHostPath(
|
||||
workspaceRoot ?? "",
|
||||
workspaceRoot
|
||||
);
|
||||
const normalizedCwd = normalizePatchHostPath(cwd ?? "", normalizedRoot);
|
||||
if (!normalizedCwd) {
|
||||
return normalizedRoot || null;
|
||||
}
|
||||
if (!normalizedRoot) {
|
||||
return normalizedCwd;
|
||||
}
|
||||
if (isSyntheticWorkspacePath(normalizedRoot)) {
|
||||
return normalizedCwd;
|
||||
}
|
||||
if (normalizedCwd === "/workspace") {
|
||||
return normalizedRoot;
|
||||
}
|
||||
if (normalizedCwd.startsWith("/workspace/")) {
|
||||
return `${normalizedRoot}/${normalizedCwd.slice("/workspace/".length)}`;
|
||||
}
|
||||
return normalizedCwd;
|
||||
}
|
||||
|
||||
export function resolvePatchDiffCwd({
|
||||
sourceCwd,
|
||||
executionCwd,
|
||||
changes
|
||||
}: {
|
||||
sourceCwd: string | null;
|
||||
executionCwd: string | null;
|
||||
changes: readonly Pick<AgentTurnSummaryPatchChangeVM, "path">[];
|
||||
}): string | null {
|
||||
const normalizedSourceCwd = normalizePatchHostPath(sourceCwd ?? "");
|
||||
const normalizedExecutionCwd = normalizePatchHostPath(executionCwd ?? "");
|
||||
if (
|
||||
!isSyntheticWorkspacePath(normalizedSourceCwd) ||
|
||||
!normalizedExecutionCwd
|
||||
) {
|
||||
return sourceCwd;
|
||||
}
|
||||
const hasSyntheticChangePath = changes.some((change) =>
|
||||
isInsideOrEqualPatchPath(
|
||||
normalizePatchHostPath(change.path),
|
||||
normalizedSourceCwd
|
||||
)
|
||||
);
|
||||
const hasHostChangePath = changes.some((change) =>
|
||||
isInsideOrEqualPatchPath(
|
||||
normalizePatchHostPath(change.path),
|
||||
normalizedExecutionCwd
|
||||
)
|
||||
);
|
||||
return hasHostChangePath && !hasSyntheticChangePath
|
||||
? normalizedExecutionCwd
|
||||
: sourceCwd;
|
||||
}
|
||||
|
||||
function normalizePatchHostPath(
|
||||
path: string,
|
||||
workspaceRoot?: string | null
|
||||
): string {
|
||||
const trimmed = path.trim();
|
||||
if (!trimmed) {
|
||||
return "";
|
||||
}
|
||||
const normalized = normalizeWorkspaceFilePath(trimmed, workspaceRoot);
|
||||
if (/^\/?[A-Za-z]:\//.test(normalized)) {
|
||||
return formatWorkspaceFilePathForDisplay(normalized, "win32")
|
||||
.replaceAll("\\", "/")
|
||||
.replace(/\/+$/, "");
|
||||
}
|
||||
return normalized.replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
function dirnameForPatchHostPath(path: string): string {
|
||||
const normalized = normalizePatchHostPath(path);
|
||||
const index = normalized.lastIndexOf("/");
|
||||
if (index < 0) {
|
||||
return "";
|
||||
}
|
||||
if (index === 0) {
|
||||
return "/";
|
||||
}
|
||||
return normalized.slice(0, index);
|
||||
}
|
||||
|
||||
function isSyntheticWorkspacePath(path: string): boolean {
|
||||
return path === "/workspace" || path.startsWith("/workspace/");
|
||||
}
|
||||
|
||||
function isInsideOrEqualPatchPath(path: string, root: string): boolean {
|
||||
return path === root || path.startsWith(`${root}/`);
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
import { normalizeAgentPatchText } from "./agentPatchMetadata";
|
||||
|
||||
const UNIFIED_DIFF_HUNK_PATTERN =
|
||||
/^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(?:.*)$/;
|
||||
|
||||
export function isAgentUnifiedDiffText(value: string): boolean {
|
||||
const normalized = normalizeAgentPatchText(value).replace(/\r/g, "\n").trim();
|
||||
if (!normalized) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const lines = normalized.split("\n");
|
||||
const isApplyPatch =
|
||||
normalized.includes("*** Begin Patch") &&
|
||||
(normalized.includes("*** Add File:") ||
|
||||
normalized.includes("*** Delete File:") ||
|
||||
normalized.includes("*** Update File:"));
|
||||
let hasHunk = false;
|
||||
let hunkActive = false;
|
||||
let hunkHasCounts = false;
|
||||
let hunkHasChange = false;
|
||||
let oldCount = 0;
|
||||
let newCount = 0;
|
||||
let expectedOld = 0;
|
||||
let expectedNew = 0;
|
||||
|
||||
const finishHunk = (): boolean => {
|
||||
if (!hunkActive) {
|
||||
return true;
|
||||
}
|
||||
if (!hunkHasCounts) {
|
||||
return isApplyPatch && hunkHasChange;
|
||||
}
|
||||
return (
|
||||
oldCount === expectedOld && newCount === expectedNew && hunkHasChange
|
||||
);
|
||||
};
|
||||
|
||||
for (const line of lines) {
|
||||
const hunkMatch = line.match(UNIFIED_DIFF_HUNK_PATTERN);
|
||||
if (hunkMatch) {
|
||||
if (!finishHunk()) {
|
||||
return false;
|
||||
}
|
||||
hasHunk = true;
|
||||
hunkActive = true;
|
||||
hunkHasCounts = true;
|
||||
hunkHasChange = false;
|
||||
oldCount = 0;
|
||||
newCount = 0;
|
||||
expectedOld = hunkCount(hunkMatch[2]);
|
||||
expectedNew = hunkCount(hunkMatch[4]);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line === "@@" && isApplyPatch) {
|
||||
if (!finishHunk()) {
|
||||
return false;
|
||||
}
|
||||
hasHunk = true;
|
||||
hunkActive = true;
|
||||
hunkHasCounts = false;
|
||||
hunkHasChange = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (hunkActive && line.startsWith("diff --git ")) {
|
||||
if (!finishHunk()) {
|
||||
return false;
|
||||
}
|
||||
hunkActive = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
!hunkActive &&
|
||||
(line.startsWith("diff --git ") ||
|
||||
line.startsWith("index ") ||
|
||||
line.startsWith("--- ") ||
|
||||
line.startsWith("+++ ") ||
|
||||
line.startsWith("new file mode ") ||
|
||||
line.startsWith("deleted file mode ") ||
|
||||
line.startsWith("old mode ") ||
|
||||
line.startsWith("new mode ") ||
|
||||
line.startsWith("similarity index ") ||
|
||||
line.startsWith("rename from ") ||
|
||||
line.startsWith("rename to ") ||
|
||||
line.startsWith("*** "))
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
if (hunkActive && isApplyPatch && line.startsWith("*** ")) {
|
||||
continue;
|
||||
}
|
||||
if (line === "\\ No newline at end of file") {
|
||||
continue;
|
||||
}
|
||||
if (!hasHunk) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!hunkHasCounts && isApplyPatch) {
|
||||
if (line.startsWith("+") || line.startsWith("-")) {
|
||||
hunkHasChange = true;
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith(" ")) {
|
||||
continue;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (line.startsWith("+")) {
|
||||
newCount += 1;
|
||||
hunkHasChange = true;
|
||||
} else if (line.startsWith("-")) {
|
||||
oldCount += 1;
|
||||
hunkHasChange = true;
|
||||
} else if (line.startsWith(" ")) {
|
||||
oldCount += 1;
|
||||
newCount += 1;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return hasHunk && finishHunk();
|
||||
}
|
||||
|
||||
function hunkCount(value: string | undefined): number {
|
||||
if (!value) {
|
||||
return 1;
|
||||
}
|
||||
return Number.parseInt(value, 10);
|
||||
}
|
||||
Reference in new issue
Block a user