Skip to content

Commit 0a80ef4

Browse files
authored
fix(opencode): avoid snapshotting files over 2MB (#19043)
1 parent 4f9667c commit 0a80ef4

2 files changed

Lines changed: 77 additions & 10 deletions

File tree

‎packages/opencode/src/snapshot/index.ts‎

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export namespace Snapshot {
3434

3535
const log = Log.create({ service: "snapshot" })
3636
const prune = "7.days"
37+
const limit = 2 * 1024 * 1024
3738
const core = ["-c", "core.longpaths=true", "-c", "core.symlinks=true"]
3839
const cfg = ["-c", "core.autocrlf=false", ...core]
3940
const quote = [...cfg, "-c", "core.quotepath=false"]
@@ -123,20 +124,69 @@ export namespace Snapshot {
123124
return file
124125
})
125126

126-
const sync = Effect.fnUntraced(function* () {
127+
const sync = Effect.fnUntraced(function* (list: string[] = []) {
127128
const file = yield* excludes()
128129
const target = path.join(state.gitdir, "info", "exclude")
130+
const text = [
131+
file ? (yield* read(file)).trimEnd() : "",
132+
...list.map((item) => `/${item.replaceAll("\\", "/")}`),
133+
]
134+
.filter(Boolean)
135+
.join("\n")
129136
yield* fs.ensureDir(path.join(state.gitdir, "info")).pipe(Effect.orDie)
130-
if (!file) {
131-
yield* fs.writeFileString(target, "").pipe(Effect.orDie)
132-
return
133-
}
134-
yield* fs.writeFileString(target, yield* read(file)).pipe(Effect.orDie)
137+
yield* fs.writeFileString(target, text ? `${text}\n` : "").pipe(Effect.orDie)
135138
})
136139

137140
const add = Effect.fnUntraced(function* () {
138141
yield* sync()
139-
yield* git([...cfg, ...args(["add", "."])], { cwd: state.directory })
142+
const [diff, other] = yield* Effect.all(
143+
[
144+
git([...quote, ...args(["diff-files", "--name-only", "-z", "--", "."])], {
145+
cwd: state.directory,
146+
}),
147+
git([...quote, ...args(["ls-files", "--others", "--exclude-standard", "-z", "--", "."])], {
148+
cwd: state.directory,
149+
}),
150+
],
151+
{ concurrency: 2 },
152+
)
153+
if (diff.code !== 0 || other.code !== 0) {
154+
log.warn("failed to list snapshot files", {
155+
diffCode: diff.code,
156+
diffStderr: diff.stderr,
157+
otherCode: other.code,
158+
otherStderr: other.stderr,
159+
})
160+
return
161+
}
162+
163+
const tracked = diff.text.split("\0").filter(Boolean)
164+
const all = Array.from(new Set([...tracked, ...other.text.split("\0").filter(Boolean)]))
165+
if (!all.length) return
166+
167+
const large = (yield* Effect.all(
168+
all.map((item) =>
169+
fs
170+
.stat(path.join(state.directory, item))
171+
.pipe(Effect.catch(() => Effect.void))
172+
.pipe(
173+
Effect.map((stat) => {
174+
if (!stat || stat.type !== "File") return
175+
const size = typeof stat.size === "bigint" ? Number(stat.size) : stat.size
176+
return size > limit ? item : undefined
177+
}),
178+
),
179+
),
180+
{ concurrency: 8 },
181+
)).filter((item): item is string => Boolean(item))
182+
yield* sync(large)
183+
const result = yield* git([...cfg, ...args(["add", "--sparse", "."])], { cwd: state.directory })
184+
if (result.code !== 0) {
185+
log.warn("failed to add snapshot files", {
186+
exitCode: result.code,
187+
stderr: result.stderr,
188+
})
189+
}
140190
})
141191

142192
const cleanup = Effect.fnUntraced(function* () {
@@ -177,7 +227,7 @@ export namespace Snapshot {
177227
const patch = Effect.fnUntraced(function* (hash: string) {
178228
yield* add()
179229
const result = yield* git(
180-
[...quote, ...args(["diff", "--no-ext-diff", "--name-only", hash, "--", "."])],
230+
[...quote, ...args(["diff", "--cached", "--no-ext-diff", "--name-only", hash, "--", "."])],
181231
{
182232
cwd: state.directory,
183233
},
@@ -245,7 +295,7 @@ export namespace Snapshot {
245295

246296
const diff = Effect.fnUntraced(function* (hash: string) {
247297
yield* add()
248-
const result = yield* git([...quote, ...args(["diff", "--no-ext-diff", hash, "--", "."])], {
298+
const result = yield* git([...quote, ...args(["diff", "--cached", "--no-ext-diff", hash, "--", "."])], {
249299
cwd: state.worktree,
250300
})
251301
if (result.code !== 0) {

‎packages/opencode/test/snapshot/snapshot.test.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ test("symlink handling", async () => {
181181
})
182182
})
183183

184-
test("large file handling", async () => {
184+
test("file under size limit handling", async () => {
185185
await using tmp = await bootstrap()
186186
await Instance.provide({
187187
directory: tmp.path,
@@ -196,6 +196,23 @@ test("large file handling", async () => {
196196
})
197197
})
198198

199+
test("large added files are skipped", async () => {
200+
await using tmp = await bootstrap()
201+
await Instance.provide({
202+
directory: tmp.path,
203+
fn: async () => {
204+
const before = await Snapshot.track()
205+
expect(before).toBeTruthy()
206+
207+
await Filesystem.write(`${tmp.path}/huge.txt`, new Uint8Array(2 * 1024 * 1024 + 1))
208+
209+
expect((await Snapshot.patch(before!)).files).toEqual([])
210+
expect(await Snapshot.diff(before!)).toBe("")
211+
expect(await Snapshot.track()).toBe(before)
212+
},
213+
})
214+
})
215+
199216
test("nested directory revert", async () => {
200217
await using tmp = await bootstrap()
201218
await Instance.provide({

0 commit comments

Comments
 (0)