Compare commits

...

2 Commits

Author SHA1 Message Date
Dax Raad e2e32b359f fix(opencode): align share and patch test diff types 2026-04-06 20:49:24 -04:00
Dax Raad aac9bb3a95 refactor(snapshot): store unified patches in file diffs 2026-04-06 18:57:13 -04:00
10 changed files with 65 additions and 59 deletions
@@ -2130,7 +2130,7 @@ function ApplyPatch(props: ToolProps<typeof ApplyPatchTool>) {
</text>
}
>
<Diff diff={file.diff} filePath={file.filePath} />
<Diff diff={file.patch} filePath={file.filePath} />
<Diagnostics diagnostics={props.metadata.diagnostics} filePath={file.movePath ?? file.filePath} />
</Show>
</BlockTool>
+16 -3
View File
@@ -7,7 +7,6 @@ import { makeRuntime } from "@/effect/run-service"
import { AppFileSystem } from "@/filesystem"
import { FileWatcher } from "@/file/watcher"
import { Git } from "@/git"
import { Snapshot } from "@/snapshot"
import { Log } from "@/util/log"
import { Instance } from "./instance"
import z from "zod"
@@ -63,7 +62,7 @@ export namespace Vcs {
additions: stat?.additions ?? (item.status === "added" ? count(after) : 0),
deletions: stat?.deletions ?? (item.status === "deleted" ? count(before) : 0),
status: item.status,
} satisfies Snapshot.FileDiff
} satisfies FileDiff
}),
{ concurrency: 8 },
)
@@ -125,11 +124,25 @@ export namespace Vcs {
})
export type Info = z.infer<typeof Info>
export const FileDiff = z
.object({
file: z.string(),
before: z.string(),
after: z.string(),
additions: z.number(),
deletions: z.number(),
status: z.enum(["added", "deleted", "modified"]).optional(),
})
.meta({
ref: "VcsFileDiff",
})
export type FileDiff = z.infer<typeof FileDiff>
export interface Interface {
readonly init: () => Effect.Effect<void>
readonly branch: () => Effect.Effect<string | undefined>
readonly defaultBranch: () => Effect.Effect<string | undefined>
readonly diff: (mode: Mode) => Effect.Effect<Snapshot.FileDiff[]>
readonly diff: (mode: Mode) => Effect.Effect<FileDiff[]>
}
interface State {
+1 -1
View File
@@ -154,7 +154,7 @@ export const InstanceRoutes = (upgrade: UpgradeWebSocket, app: Hono = new Hono()
description: "VCS diff",
content: {
"application/json": {
schema: resolver(Snapshot.FileDiff.array()),
schema: resolver(Vcs.FileDiff.array()),
},
},
},
+1 -1
View File
@@ -59,7 +59,7 @@ export namespace ShareNext {
}
| {
type: "session_diff"
data: SDK.FileDiff[]
data: SDK.SnapshotFileDiff[]
}
| {
type: "model"
+6 -9
View File
@@ -1,6 +1,6 @@
import { NodeFileSystem, NodePath } from "@effect/platform-node"
import { Cause, Duration, Effect, Layer, Schedule, Semaphore, ServiceMap, Stream } from "effect"
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
import { formatPatch, structuredPatch } from "diff"
import path from "path"
import z from "zod"
import * as CrossSpawnSpawner from "@/effect/cross-spawn-spawner"
@@ -22,14 +22,13 @@ export namespace Snapshot {
export const FileDiff = z
.object({
file: z.string(),
before: z.string(),
after: z.string(),
patch: z.string(),
additions: z.number(),
deletions: z.number(),
status: z.enum(["added", "deleted", "modified"]).optional(),
})
.meta({
ref: "FileDiff",
ref: "SnapshotFileDiff",
})
export type FileDiff = z.infer<typeof FileDiff>
@@ -521,8 +520,6 @@ export namespace Snapshot {
const map = new Map<string, { before: string; after: string }>()
const dec = new TextDecoder()
let i = 0
// Parse the default `git cat-file --batch` stream: one header line,
// then exactly `size` bytes of blob content, then a trailing newline.
for (const ref of refs) {
let end = i
while (end < out.length && out[end] !== 10) end += 1
@@ -620,8 +617,9 @@ export namespace Snapshot {
]
})
const step = 100
const patch = (file: string, before: string, after: string) =>
formatPatch(structuredPatch(file, file, before, after, "", "", { context: Number.MAX_SAFE_INTEGER }))
// Keep batches bounded so a large diff does not buffer every blob at once.
for (let i = 0; i < rows.length; i += step) {
const run = rows.slice(i, i + step)
const text = yield* load(run)
@@ -631,8 +629,7 @@ export namespace Snapshot {
const [before, after] = row.binary ? ["", ""] : text ? [hit.before, hit.after] : yield* show(row)
result.push({
file: row.file,
before,
after,
patch: row.binary ? "" : patch(row.file, before, after),
additions: row.additions,
deletions: row.deletions,
status: row.status,
+1 -3
View File
@@ -164,9 +164,7 @@ export const ApplyPatchTool = Tool.define("apply_patch", {
filePath: change.filePath,
relativePath: path.relative(Instance.worktree, change.movePath ?? change.filePath).replaceAll("\\", "/"),
type: change.type,
diff: change.diff,
before: change.oldContent,
after: change.newContent,
patch: change.diff,
additions: change.additions,
deletions: change.deletions,
movePath: change.movePath,
+1 -2
View File
@@ -123,8 +123,7 @@ export const EditTool = Tool.define("edit", {
const filediff: Snapshot.FileDiff = {
file: filePath,
before: contentOld,
after: contentNew,
patch: diff,
additions: 0,
deletions: 0,
}
@@ -974,8 +974,7 @@ test("diffFull with new file additions", async () => {
const newFileDiff = diffs[0]
expect(newFileDiff.file).toBe("new.txt")
expect(newFileDiff.before).toBe("")
expect(newFileDiff.after).toBe("new content")
expect(newFileDiff.patch).toContain("+new content")
expect(newFileDiff.additions).toBe(1)
expect(newFileDiff.deletions).toBe(0)
},
@@ -1020,26 +1019,23 @@ test("diffFull with a large interleaved mixed diff", async () => {
for (let i = 0; i < ids.length; i++) {
const m = map.get(fwd("mix", `${ids[i]}-mod.txt`))
expect(m).toBeDefined()
expect(m!.before).toBe(`before-${ids[i]}\n🙂\nline`)
expect(m!.after).toBe(`after-${ids[i]}\n🚀\nline`)
expect(m!.patch).toContain(`-before-${ids[i]}`)
expect(m!.patch).toContain(`+after-${ids[i]}`)
expect(m!.status).toBe("modified")
const d = map.get(fwd("mix", `${ids[i]}-del.txt`))
expect(d).toBeDefined()
expect(d!.before).toBe(`gone-${ids[i]}\n你好`)
expect(d!.after).toBe("")
expect(d!.patch).toContain(`-gone-${ids[i]}`)
expect(d!.status).toBe("deleted")
const a = map.get(fwd("mix", `${ids[i]}-add.txt`))
expect(a).toBeDefined()
expect(a!.before).toBe("")
expect(a!.after).toBe(`new-${ids[i]}\nこんにちは`)
expect(a!.patch).toContain(`+new-${ids[i]}`)
expect(a!.status).toBe("added")
const b = map.get(fwd("mix", `${ids[i]}-bin.bin`))
expect(b).toBeDefined()
expect(b!.before).toBe("")
expect(b!.after).toBe("")
expect(b!.patch).toBe("")
expect(b!.additions).toBe(0)
expect(b!.deletions).toBe(0)
expect(b!.status).toBe("modified")
@@ -1092,8 +1088,8 @@ test("diffFull with file modifications", async () => {
const modifiedFileDiff = diffs[0]
expect(modifiedFileDiff.file).toBe("b.txt")
expect(modifiedFileDiff.before).toBe(tmp.extra.bContent)
expect(modifiedFileDiff.after).toBe("modified content")
expect(modifiedFileDiff.patch).toContain(`-${tmp.extra.bContent}`)
expect(modifiedFileDiff.patch).toContain("+modified content")
expect(modifiedFileDiff.additions).toBeGreaterThan(0)
expect(modifiedFileDiff.deletions).toBeGreaterThan(0)
},
@@ -1118,8 +1114,7 @@ test("diffFull with file deletions", async () => {
const removedFileDiff = diffs[0]
expect(removedFileDiff.file).toBe("a.txt")
expect(removedFileDiff.before).toBe(tmp.extra.aContent)
expect(removedFileDiff.after).toBe("")
expect(removedFileDiff.patch).toContain(`-${tmp.extra.aContent}`)
expect(removedFileDiff.additions).toBe(0)
expect(removedFileDiff.deletions).toBe(1)
},
@@ -1144,8 +1139,8 @@ test("diffFull with multiple line additions", async () => {
const multiDiff = diffs[0]
expect(multiDiff.file).toBe("multi.txt")
expect(multiDiff.before).toBe("")
expect(multiDiff.after).toBe("line1\nline2\nline3")
expect(multiDiff.patch).toContain("+line1")
expect(multiDiff.patch).toContain("+line3")
expect(multiDiff.additions).toBe(3)
expect(multiDiff.deletions).toBe(0)
},
@@ -1171,15 +1166,13 @@ test("diffFull with addition and deletion", async () => {
const addedFileDiff = diffs.find((d) => d.file === "added.txt")
expect(addedFileDiff).toBeDefined()
expect(addedFileDiff!.before).toBe("")
expect(addedFileDiff!.after).toBe("added content")
expect(addedFileDiff!.patch).toContain("+added content")
expect(addedFileDiff!.additions).toBe(1)
expect(addedFileDiff!.deletions).toBe(0)
const removedFileDiff = diffs.find((d) => d.file === "a.txt")
expect(removedFileDiff).toBeDefined()
expect(removedFileDiff!.before).toBe(tmp.extra.aContent)
expect(removedFileDiff!.after).toBe("")
expect(removedFileDiff!.patch).toContain(`-${tmp.extra.aContent}`)
expect(removedFileDiff!.additions).toBe(0)
expect(removedFileDiff!.deletions).toBe(1)
},
@@ -1263,7 +1256,7 @@ test("diffFull with binary file changes", async () => {
const binaryDiff = diffs[0]
expect(binaryDiff.file).toBe("binary.bin")
expect(binaryDiff.before).toBe("")
expect(binaryDiff.patch).toBe("")
},
})
})
@@ -27,9 +27,7 @@ type AskInput = {
filePath: string
relativePath: string
type: "add" | "update" | "delete" | "move"
diff: string
before: string
after: string
patch: string
additions: number
deletions: number
movePath?: string
@@ -112,12 +110,12 @@ describe("tool.apply_patch freeform", () => {
const addFile = permissionCall.metadata.files.find((f) => f.type === "add")
expect(addFile).toBeDefined()
expect(addFile!.relativePath).toBe("nested/new.txt")
expect(addFile!.after).toBe("created\n")
expect(addFile!.patch).toContain("+created")
const updateFile = permissionCall.metadata.files.find((f) => f.type === "update")
expect(updateFile).toBeDefined()
expect(updateFile!.before).toContain("line2")
expect(updateFile!.after).toContain("changed")
expect(updateFile!.patch).toContain("-line2")
expect(updateFile!.patch).toContain("+changed")
const added = await fs.readFile(path.join(fixture.path, "nested", "new.txt"), "utf-8")
expect(added).toBe("created\n")
@@ -151,8 +149,8 @@ describe("tool.apply_patch freeform", () => {
expect(moveFile.type).toBe("move")
expect(moveFile.relativePath).toBe("renamed/dir/name.txt")
expect(moveFile.movePath).toBe(path.join(fixture.path, "renamed/dir/name.txt"))
expect(moveFile.before).toBe("old content\n")
expect(moveFile.after).toBe("new content\n")
expect(moveFile.patch).toContain("-old content")
expect(moveFile.patch).toContain("+new content")
},
})
})
+18 -10
View File
@@ -347,10 +347,9 @@ export type EventCommandExecuted = {
}
}
export type FileDiff = {
export type SnapshotFileDiff = {
file: string
before: string
after: string
patch: string
additions: number
deletions: number
status?: "added" | "deleted" | "modified"
@@ -360,7 +359,7 @@ export type EventSessionDiff = {
type: "session.diff"
properties: {
sessionID: string
diff: Array<FileDiff>
diff: Array<SnapshotFileDiff>
}
}
@@ -542,7 +541,7 @@ export type UserMessage = {
summary?: {
title?: string
body?: string
diffs: Array<FileDiff>
diffs: Array<SnapshotFileDiff>
}
agent: string
model: {
@@ -917,7 +916,7 @@ export type Session = {
additions: number
deletions: number
files: number
diffs?: Array<FileDiff>
diffs?: Array<SnapshotFileDiff>
}
share?: {
url: string
@@ -1078,7 +1077,7 @@ export type SyncEventSessionUpdated = {
additions: number
deletions: number
files: number
diffs?: Array<FileDiff>
diffs?: Array<SnapshotFileDiff>
} | null
share?: {
url: string | null
@@ -1803,7 +1802,7 @@ export type GlobalSession = {
additions: number
deletions: number
files: number
diffs?: Array<FileDiff>
diffs?: Array<SnapshotFileDiff>
}
share?: {
url: string
@@ -2009,6 +2008,15 @@ export type VcsInfo = {
default_branch?: string
}
export type VcsFileDiff = {
file: string
before: string
after: string
additions: number
deletions: number
status?: "added" | "deleted" | "modified"
}
export type Command = {
name: string
description?: string
@@ -3503,7 +3511,7 @@ export type SessionDiffResponses = {
/**
* Successfully retrieved diff
*/
200: Array<FileDiff>
200: Array<SnapshotFileDiff>
}
export type SessionDiffResponse = SessionDiffResponses[keyof SessionDiffResponses]
@@ -5159,7 +5167,7 @@ export type VcsDiffResponses = {
/**
* VCS diff
*/
200: Array<FileDiff>
200: Array<VcsFileDiff>
}
export type VcsDiffResponse = VcsDiffResponses[keyof VcsDiffResponses]