Skip to content

Commit e95ce30

Browse files
anandgupta42claude
andcommitted
fix: age truncation files by mtime — Identifier 48-bit timestamp wrapped
`Identifier.create` packs `timestamp * 4096 + counter` into 6 bytes, wrapping every 2^36 ms (~795.4 days). The 26th wrap since epoch landed 2026-08-14T11:19:55Z: post-wrap IDs decode to tiny timestamps, so both truncation cleanups computed a pre-wrap cutoff astronomically larger than every new file's decoded timestamp — every truncated tool output written after Aug 14 was deleted the moment cleanup ran, and the `Truncate > cleanup` test failed on every CI run since Aug 17 on unchanged code. Both cleanups (`tool/truncate.ts` Effect service and `tool/truncation.ts` legacy module, used by bootstrap/bash/prompt) now age files by mtime, which does not wrap; stat failures keep the file (deletion fails safe). Tagged `upstream_fix` — the wrap-prone encoding is upstream OpenCode code. Test updated to set explicit mtimes instead of ID-embedded timestamps. Closes #1112 Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent da952c1 commit e95ce30

3 files changed

Lines changed: 37 additions & 20 deletions

File tree

‎packages/opencode/src/tool/truncate.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { Agent } from "../agent/agent"
66
import { FSUtil } from "@opencode-ai/core/fs-util"
77
import { evaluate } from "@/permission/evaluate"
88
import { Config } from "@/config/config"
9-
import { Identifier } from "../id/id"
109
import { ToolID } from "./schema"
1110
import { TRUNCATION_DIR } from "./truncation-dir"
1211

@@ -52,17 +51,27 @@ export const layer = Layer.effect(
5251
const fs = yield* FSUtil.Service
5352

5453
const cleanup = Effect.fn("Truncate.cleanup")(function* () {
55-
const cutoff = Identifier.timestamp(
56-
Identifier.create("tool", "ascending", Date.now() - Duration.toMillis(RETENTION)),
57-
)
54+
// altimate_change start — upstream_fix: age files by mtime, not decoded ID timestamps.
55+
// Identifier packs `timestamp * 4096` into 48 bits, which wraps every
56+
// 2^36 ms (~795 days) — the 26th wrap landed 2026-08-14T11:19:55Z, after
57+
// which every new ID decoded as "ancient" and cleanup deleted files the
58+
// moment they were written. File mtime has no wrap.
59+
const cutoffMs = Date.now() - Duration.toMillis(RETENTION)
5860
const entries = yield* fs.readDirectory(TRUNCATION_DIR).pipe(
5961
Effect.map((all) => all.filter((name) => name.startsWith("tool_"))),
6062
Effect.catch(() => Effect.succeed([])),
6163
)
6264
for (const entry of entries) {
63-
if (Identifier.timestamp(entry) >= cutoff) continue
64-
yield* fs.remove(path.join(TRUNCATION_DIR, entry)).pipe(Effect.catch(() => Effect.void))
65+
const file = path.join(TRUNCATION_DIR, entry)
66+
const mtimeMs = yield* Effect.tryPromise(() => import("node:fs/promises").then((nfs) => nfs.stat(file))).pipe(
67+
Effect.map((st) => st.mtimeMs),
68+
// Unstat-able file: keep it — deletion must fail safe.
69+
Effect.catch(() => Effect.succeed(Number.POSITIVE_INFINITY)),
70+
)
71+
if (mtimeMs >= cutoffMs) continue
72+
yield* fs.remove(file).pipe(Effect.catch(() => Effect.void))
6573
}
74+
// altimate_change end
6675
})
6776

6877
const write = Effect.fn("Truncate.write")(function* (text: string) {

‎packages/opencode/src/tool/truncation.ts‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import fs from "fs/promises"
22
import path from "path"
33
import { Global } from "../global"
4-
import { Identifier } from "../id/id"
54
import { PermissionNext } from "../permission/next"
65
import type { Agent } from "../agent/agent"
76
import { Scheduler } from "../scheduler"
@@ -35,20 +34,24 @@ export namespace Truncate {
3534
}
3635

3736
export async function cleanup() {
38-
const cutoff = Identifier.timestamp(Identifier.create("tool", "ascending", Date.now() - RETENTION_MS))
37+
// altimate_change start — upstream_fix: age files by mtime, not decoded ID
38+
// timestamps. Identifier packs `timestamp * 4096` into 48 bits, wrapping
39+
// every 2^36 ms (~795 days; the 26th wrap: 2026-08-14T11:19:55Z), after
40+
// which every new ID decoded as "ancient" and cleanup deleted files the
41+
// moment they were written. File mtime has no wrap. Stat failures keep
42+
// the file — deletion must fail safe.
43+
const cutoffMs = Date.now() - RETENTION_MS
3944
const entries = await Glob.scan("tool_*", { cwd: DIR, include: "file" }).catch(() => [] as string[])
4045
for (const entry of entries) {
41-
// altimate_change start - tolerate stale/malformed tool-output files from older builds.
42-
let timestamp: number
43-
try {
44-
timestamp = Identifier.timestamp(entry)
45-
} catch {
46-
continue
47-
}
48-
if (timestamp >= cutoff) continue
49-
// altimate_change end
50-
await fs.unlink(path.join(DIR, entry)).catch(() => {})
46+
const file = path.join(DIR, entry)
47+
const mtimeMs = await fs
48+
.stat(file)
49+
.then((st) => st.mtimeMs)
50+
.catch(() => Number.POSITIVE_INFINITY)
51+
if (mtimeMs >= cutoffMs) continue
52+
await fs.unlink(file).catch(() => {})
5153
}
54+
// altimate_change end
5255
}
5356

5457
function hasTaskTool(agent?: Agent.Info): boolean {

‎packages/opencode/test/tool/truncation.test.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,16 @@ describe("Truncate", () => {
251251

252252
yield* fs.makeDirectory(Truncate.DIR, { recursive: true })
253253

254-
const old = path.join(Truncate.DIR, Identifier.create("tool", "ascending", Date.now() - 10 * DAY_MS))
255-
const recent = path.join(Truncate.DIR, Identifier.create("tool", "ascending", Date.now() - 3 * DAY_MS))
254+
// Age is judged by file mtime (ID-embedded timestamps wrap every
255+
// ~795 days — see Truncate.cleanup), so set mtimes explicitly.
256+
const old = path.join(Truncate.DIR, Identifier.create("tool", "ascending"))
257+
const recent = path.join(Truncate.DIR, Identifier.create("tool", "ascending"))
256258

257259
yield* writeFileStringScoped(old, "old content")
258260
yield* writeFileStringScoped(recent, "recent content")
261+
const nfs = yield* Effect.promise(() => import("node:fs/promises"))
262+
yield* Effect.promise(() => nfs.utimes(old, new Date(Date.now() - 10 * DAY_MS), new Date(Date.now() - 10 * DAY_MS)))
263+
yield* Effect.promise(() => nfs.utimes(recent, new Date(Date.now() - 3 * DAY_MS), new Date(Date.now() - 3 * DAY_MS)))
259264
yield* svc.cleanup()
260265

261266
expect(yield* fs.exists(old)).toBe(false)

0 commit comments

Comments
 (0)