Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/opencode/src/diagnostic/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,13 @@ export namespace Memory {
export function start(label: string) {
if (one.timer) return

const cfg = env()
if (!cfg) return

void Memory.trim().catch((error) => {
log.error("memory trim failed", { error })
})

const cfg = env()
if (!cfg) return

one.file =
cfg.path || path.join(Global.Path.log, `memory-${label}-${new Date().toISOString().split(".")[0].replace(/:/g, "")}.ndjson`)

Expand Down
4 changes: 2 additions & 2 deletions packages/opencode/src/util/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export namespace Log {
export function file() {
return logpath
}
const max = 512 * 1024 * 1024
export const MAX = 128 * 1024 * 1024
const step = 1024 * 1024
const wait = 5_000
let size = 0
Expand Down Expand Up @@ -113,7 +113,7 @@ export namespace Log {
file?: string
}) {
const dir = input?.dir ?? Global.Path.log
const keep = input?.max ?? max
const keep = input?.max ?? MAX
const file = input?.file ?? logpath
const rows = (await entries(dir)).sort((a, b) => a.time - b.time)
let total = rows.reduce((sum, row) => sum + row.size, 0)
Expand Down
24 changes: 24 additions & 0 deletions packages/opencode/test/memory/retention.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,28 @@ describe("memory retention", () => {
expect(await fs.stat(one).catch(() => undefined)).toBeUndefined()
expect((await fs.readdir(tmp.path)).sort()).toEqual(["three", "two"])
})

test("defaults to keeping two snapshot directories", async () => {
await using tmp = await tmpdir()
const one = path.join(tmp.path, "one")
const two = path.join(tmp.path, "two")
const three = path.join(tmp.path, "three")

await fs.mkdir(one, { recursive: true })
await fs.mkdir(two, { recursive: true })
await fs.mkdir(three, { recursive: true })
await fs.writeFile(path.join(one, "meta.json"), "{}")
await fs.writeFile(path.join(two, "meta.json"), "{}")
await fs.writeFile(path.join(three, "meta.json"), "{}")
await fs.utimes(one, new Date("2026-03-01T00:00:00Z"), new Date("2026-03-01T00:00:00Z"))
await fs.utimes(two, new Date("2026-03-02T00:00:00Z"), new Date("2026-03-02T00:00:00Z"))
await fs.utimes(three, new Date("2026-03-03T00:00:00Z"), new Date("2026-03-03T00:00:00Z"))

await Memory.trim({
dir: tmp.path,
})

expect(await fs.stat(one).catch(() => undefined)).toBeUndefined()
expect((await fs.readdir(tmp.path)).sort()).toEqual(["three", "two"])
})
})
4 changes: 4 additions & 0 deletions packages/opencode/test/util/log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { Log } from "../../src/util/log"
import { tmpdir } from "../fixture/fixture"

describe("util.log", () => {
test("defaults log cap to 128MB", () => {
expect(Log.MAX).toBe(128 * 1024 * 1024)
})

test("deletes old logs and trims the active log to stay within the cap", async () => {
await using tmp = await tmpdir()
const old = path.join(tmp.path, "old.log")
Expand Down
Loading