Skip to content

Commit 1fed345

Browse files
Zacharonclaude
andcommitted
fix(server): replaced transcripts on Windows import instead of reading as already imported
Effect's stat leaves `ino` empty when the file ID is above Number.MAX_SAFE_INTEGER, which NTFS IDs routinely are. The scanner stored that as null, two nulls compared equal, and a transcript replaced at the same path with the same size and times returned AlreadyImported without a read. Keep the full file ID as a decimal string, reading it from a bigint stat when Effect drops it. Stored sources may still carry the old numeric form; a stored number and its decimal string compare equal, and sources stored as null are read once so the importer re-records them with the full ID. Fixes #14838 Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent e58a485 commit 1fed345

3 files changed

Lines changed: 150 additions & 14 deletions

File tree

‎apps/server/src/project/AgentSessionScanner.test.ts‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
type ServerSettings as ContractServerSettings,
1010
} from "@t3tools/contracts";
1111
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
12+
import * as DateTime from "effect/DateTime";
1213
import * as Effect from "effect/Effect";
1314
import * as FileSystem from "effect/FileSystem";
1415
import * as Layer from "effect/Layer";
@@ -1910,6 +1911,112 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
19101911
}),
19111912
);
19121913

1914+
it.effect("tells a replaced transcript apart when its file ID is above 2^53", () =>
1915+
Effect.gen(function* () {
1916+
const path = yield* Path.Path;
1917+
const fileSystem = yield* FileSystem.FileSystem;
1918+
const nowMs = Date.parse("2026-08-24T12:00:00.000Z");
1919+
yield* TestClock.setTime(nowMs);
1920+
const claudeHomePath = yield* makeTempDir("t3code-large-id-claude-");
1921+
const codexHomePath = yield* makeTempDir("t3code-large-id-codex-");
1922+
const workspace = yield* makeTempDir("t3code-large-id-workspace-");
1923+
const filePath = path.join(
1924+
codexHomePath,
1925+
"sessions",
1926+
"2026",
1927+
"08",
1928+
"24",
1929+
"rollout-large-id.jsonl",
1930+
);
1931+
const contents = (sessionId: string) =>
1932+
[
1933+
encodeTranscriptRecord({
1934+
type: "session_meta",
1935+
payload: { id: sessionId, cwd: workspace },
1936+
}),
1937+
encodeTranscriptRecord({
1938+
type: "event_msg",
1939+
payload: { type: "user_message", message: "Imported prompt" },
1940+
}),
1941+
].join("\n");
1942+
yield* writeTranscript({
1943+
filePath,
1944+
contents: contents("original-session"),
1945+
mtimeMs: nowMs,
1946+
});
1947+
1948+
// Effect's stat omits file IDs above Number.MAX_SAFE_INTEGER, as on
1949+
// many NTFS volumes. Windows `utimes` cannot set the creation time, so
1950+
// pin it too; otherwise only a same-millisecond replacement collides.
1951+
const pinnedBirthtime = DateTime.toDateUtc(DateTime.makeUnsafe(nowMs));
1952+
const withLargeFileId = (target: string, info: FileSystem.File.Info) =>
1953+
target === filePath
1954+
? { ...info, ino: Option.none(), birthtime: Option.some(pinnedBirthtime) }
1955+
: info;
1956+
const largeFileIdFileSystem = FileSystem.FileSystem.of({
1957+
...fileSystem,
1958+
stat: (target) =>
1959+
fileSystem.stat(target).pipe(Effect.map((info) => withLargeFileId(target, info))),
1960+
open: (target, options) =>
1961+
fileSystem.open(target, options).pipe(
1962+
Effect.map((file) => ({
1963+
...file,
1964+
stat: file.stat.pipe(Effect.map((info) => withLargeFileId(target, info))),
1965+
readAlloc: (size: number) => file.readAlloc(size),
1966+
})),
1967+
),
1968+
});
1969+
1970+
yield* Effect.gen(function* () {
1971+
const scanner = yield* AgentSessionScanner.AgentSessionScanner;
1972+
const initial = yield* scanner.recentThreads(workspace).pipe(Stream.runCollect);
1973+
const imported = initial[0];
1974+
expect(imported?._tag).toBe("Importable");
1975+
if (imported?._tag !== "Importable") return;
1976+
expect(imported.source.inode).toEqual(expect.any(String));
1977+
const completed = yield* scanner
1978+
.recentThreads(workspace, [imported.source])
1979+
.pipe(Stream.runCollect);
1980+
expect(completed[0]?._tag).toBe("AlreadyImported");
1981+
// Older sources stored the ID as a number, or as null when it was too
1982+
// large. Null ones are read once so the importer records the full ID.
1983+
const numericInode = Number(imported.source.inode);
1984+
if (Number.isSafeInteger(numericInode)) {
1985+
const numeric = yield* scanner
1986+
.recentThreads(workspace, [{ ...imported.source, inode: numericInode }])
1987+
.pipe(Stream.runCollect);
1988+
expect(numeric[0]?._tag).toBe("AlreadyImported");
1989+
}
1990+
const legacy = yield* scanner
1991+
.recentThreads(workspace, [{ ...imported.source, inode: null }])
1992+
.pipe(Stream.runCollect);
1993+
expect(legacy[0]).toMatchObject({
1994+
_tag: "Importable",
1995+
source: { inode: imported.source.inode },
1996+
});
1997+
1998+
// Holding the old file open keeps its ID from being reused.
1999+
yield* fileSystem.open(filePath);
2000+
yield* fileSystem.remove(filePath);
2001+
yield* writeTranscript({
2002+
filePath,
2003+
contents: contents("replaced-session"),
2004+
mtimeMs: nowMs,
2005+
});
2006+
const replaced = yield* scanner
2007+
.recentThreads(workspace, [imported.source])
2008+
.pipe(Stream.runCollect);
2009+
expect(replaced[0]).toMatchObject({
2010+
_tag: "Importable",
2011+
thread: { providerSessionId: "replaced-session" },
2012+
});
2013+
}).pipe(
2014+
Effect.provide(layerScannerTest({ claudeHomePath, codexHomePath })),
2015+
Effect.provideService(FileSystem.FileSystem, largeFileIdFileSystem),
2016+
);
2017+
}),
2018+
);
2019+
19132020
it.effect("imports visible history from a transcript with an oversized tool record", () =>
19142021
Effect.gen(function* () {
19152022
const path = yield* Path.Path;

‎apps/server/src/project/AgentSessionScanner.ts‎

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @effect-diagnostics-next-line nodeBuiltinImport:off -- Effect's stat drops file IDs above 2^53.
2+
import * as NodeFSP from "node:fs/promises";
13
import * as NodeOS from "node:os";
24

35
import {
@@ -535,30 +537,47 @@ function extractCwd(line: string): string | null {
535537
return null;
536538
}
537539

538-
function transcriptIdentity(filePath: string, stats: FileSystem.File.Info) {
540+
type TranscriptIdentity = Pick<
541+
AgentSessionImportSource,
542+
"filePath" | "size" | "mtimeMs" | "device" | "inode" | "birthtimeMs"
543+
>;
544+
545+
// Effect's stat leaves `ino` empty when the ID exceeds Number.MAX_SAFE_INTEGER,
546+
// which many NTFS file IDs do. A transcript replaced at the same path with the
547+
// same size and times would then compare as the same file.
548+
const transcriptIdentity = Effect.fn("AgentSessionScanner.transcriptIdentity")(function* (
549+
filePath: string,
550+
stats: FileSystem.File.Info,
551+
) {
552+
const inode = Option.isSome(stats.ino)
553+
? String(stats.ino.value)
554+
: yield* Effect.tryPromise(() => NodeFSP.stat(filePath, { bigint: true })).pipe(
555+
Effect.map((info) => (info.ino === 0n ? null : info.ino.toString())),
556+
Effect.orElseSucceed(() => null),
557+
);
539558
return {
540559
filePath,
541560
size: Number(stats.size),
542561
mtimeMs: Option.match(stats.mtime, { onNone: () => null, onSome: (date) => date.getTime() }),
543562
device: Number(stats.dev),
544-
inode: Option.match(stats.ino, { onNone: () => null, onSome: Number }),
563+
inode,
545564
birthtimeMs: Option.match(stats.birthtime, {
546565
onNone: () => null,
547566
onSome: (date) => date.getTime(),
548567
}),
549-
};
550-
}
568+
} satisfies TranscriptIdentity;
569+
});
551570

552-
function sameTranscriptIdentity(
553-
left: ReturnType<typeof transcriptIdentity>,
554-
right: ReturnType<typeof transcriptIdentity>,
555-
): boolean {
571+
// Sources recorded before file IDs were strings stored a number.
572+
const fileIdKey = (inode: TranscriptIdentity["inode"]) => (inode === null ? null : String(inode));
573+
574+
function sameTranscriptIdentity(left: TranscriptIdentity, right: TranscriptIdentity): boolean {
556575
return (
557576
left.filePath === right.filePath &&
558577
left.size === right.size &&
559578
left.mtimeMs === right.mtimeMs &&
560579
left.device === right.device &&
561-
left.inode === right.inode &&
580+
fileIdKey(left.inode) === fileIdKey(right.inode) &&
562581
left.birthtimeMs === right.birthtimeMs
563582
);
564583
}
@@ -742,7 +761,7 @@ export const make = Effect.gen(function* () {
742761

743762
const readTranscript = Effect.fn("AgentSessionScanner.readTranscript")(function* (
744763
filePath: string,
745-
expected: ReturnType<typeof transcriptIdentity>,
764+
expected: TranscriptIdentity,
746765
recordLimit: number,
747766
source: AgentSessionSource,
748767
) {
@@ -752,7 +771,12 @@ export const make = Effect.gen(function* () {
752771
fileSystem.open(filePath, { flag: "r" }).pipe(
753772
Effect.flatMap((file) =>
754773
Effect.gen(function* () {
755-
if (!sameTranscriptIdentity(expected, transcriptIdentity(filePath, yield* file.stat))) {
774+
if (
775+
!sameTranscriptIdentity(
776+
expected,
777+
yield* transcriptIdentity(filePath, yield* file.stat),
778+
)
779+
) {
756780
return null;
757781
}
758782
const records: Array<DecodedTranscriptRecord> = [];
@@ -814,7 +838,10 @@ export const make = Effect.gen(function* () {
814838
}
815839

816840
if (recordStarted && !(yield* Effect.try(finishRecord))) return null;
817-
return sameTranscriptIdentity(expected, transcriptIdentity(filePath, yield* file.stat))
841+
return sameTranscriptIdentity(
842+
expected,
843+
yield* transcriptIdentity(filePath, yield* file.stat),
844+
)
818845
? { records, recordCount }
819846
: null;
820847
}),
@@ -1301,7 +1328,7 @@ export const make = Effect.gen(function* () {
13011328
if (Option.isNone(stats) || stats.value.type !== "File") {
13021329
return Option.some<AgentSessionRecentThread>({ _tag: "Skipped" });
13031330
}
1304-
const identity = transcriptIdentity(transcript.filePath, stats.value);
1331+
const identity = yield* transcriptIdentity(transcript.filePath, stats.value);
13051332
const completedSource = completed?.find(
13061333
(source) =>
13071334
source.provider === candidate.source && sameTranscriptIdentity(source, identity),

‎packages/contracts/src/agentSessions.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export const AgentSessionImportSource = Schema.Struct({
1313
size: NonNegativeInt,
1414
mtimeMs: Schema.NullOr(Schema.Number),
1515
device: Schema.Number,
16-
inode: Schema.NullOr(Schema.Number),
16+
// A decimal string keeps file IDs above 2^53 (common on NTFS) exact. Older
17+
// records stored a number.
18+
inode: Schema.NullOr(Schema.Union([Schema.String, Schema.Number])),
1719
birthtimeMs: Schema.NullOr(Schema.Number),
1820
});
1921
export type AgentSessionImportSource = typeof AgentSessionImportSource.Type;

0 commit comments

Comments
 (0)