The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 00:30 UTC
fix(server): detect replaced transcripts with large file identities (#21464)
Julius Marminge committed
Oct 19, 2026 at 19:53 UTC
16e442b9bce4db42ed4e9e5d88dc647a524c9334
2 files changed
+160
-52
apps/server/src/project/AgentSessionScanner.test.ts
+149
-50
@@ -2,6 +2,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
2
import * as NodeOS from "node:os";
3
import { describe, expect, it } from "@effect/vitest";
4
import {
5
+ AgentSessionImportSource,
6
type OrchestrationProjectShell,
7
ProjectId,
8
ProviderDriverKind,
@@ -9,6 +10,7 @@ import {
10
type ServerSettings as ContractServerSettings,
11
} from "@t3tools/contracts";
12
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
13
+import * as DateTime from "effect/DateTime";
14
import * as Effect from "effect/Effect";
15
import * as FileSystem from "effect/FileSystem";
16
import * as Layer from "effect/Layer";
@@ -121,6 +123,8 @@ const claudeSessionLine = (cwd: string) =>
123
const codexRolloutLine = (cwd: string) =>
124
`${JSON.stringify({ timestamp: "2026-01-01T00:00:00.000Z", type: "session_meta", payload: { id: "r1", cwd } })}\n`;
125
126
+const decodeImportSource = Schema.decodeUnknownSync(AgentSessionImportSource);
127
+
128
const encodeTranscriptRecord = Schema.encodeUnknownSync(Schema.fromJsonString(Schema.Unknown));
129
130
function makeRecordLimitTranscript(cwd: string, overflow: boolean): string {
@@ -148,6 +152,87 @@ function makeRecordLimitTranscript(cwd: string, overflow: boolean): string {
152
: records;
153
}
154
155
+const makeFileIdentityFixture = Effect.fn("AgentSessionScanner.test.makeFileIdentityFixture")(
156
+ function* (originalInode = 10_000_000_000_000_001n, replacementInode = 10_000_000_000_000_000n) {
157
+ const path = yield* Path.Path;
158
+ const fileSystem = yield* FileSystem.FileSystem;
159
+ const nowMs = Date.parse("2026-08-24T12:00:00.000Z");
160
+ yield* TestClock.setTime(nowMs);
161
+ const claudeHomePath = yield* makeTempDir("t3code-large-id-claude-");
162
+ const codexHomePath = yield* makeTempDir("t3code-large-id-codex-");
163
+ const workspace = yield* makeTempDir("t3code-large-id-workspace-");
164
+ const filePath = path.join(
165
+ codexHomePath,
166
+ "sessions",
167
+ "2026",
168
+ "08",
169
+ "24",
170
+ "rollout-identity.jsonl",
171
+ );
172
+ const replacementPath = `${filePath}.replacement`;
173
+ const savedPath = `${filePath}.saved`;
174
+ for (const [target, sessionId] of [
175
+ [filePath, "original-session"],
176
+ [replacementPath, "replaced-session"],
177
+ ] as const) {
178
+ yield* writeTranscript({
179
+ filePath: target,
180
+ contents: [
181
+ encodeTranscriptRecord({
182
+ type: "session_meta",
183
+ payload: { id: sessionId, cwd: workspace },
184
+ }),
185
+ encodeTranscriptRecord({
186
+ type: "event_msg",
187
+ payload: { type: "user_message", message: "Imported prompt" },
188
+ }),
189
+ ].join("\n"),
190
+ mtimeMs: nowMs,
191
+ });
192
+ }
193
+
194
+ // Adjacent NTFS IDs round to the same number; birthtimes can also collide.
195
+ const birthtime = Option.some(DateTime.toDateUtc(DateTime.makeUnsafe(nowMs)));
196
+ const ids = new Map<bigint, bigint>();
197
+ const effectIdentity = (target: string, info: FileSystem.File.Info) => {
198
+ if (target !== filePath) return info;
199
+ const realInode = Option.getOrThrow(info.ino);
200
+ const inode = ids.get(realInode) ?? (ids.size === 0 ? originalInode : replacementInode);
201
+ ids.set(realInode, inode);
202
+ return { ...info, ino: Option.some(inode), birthtime };
203
+ };
204
+ let opens = 0;
205
+ const simulatedFileSystem = FileSystem.FileSystem.of({
206
+ ...fileSystem,
207
+ stat: (target) =>
208
+ fileSystem.stat(target).pipe(Effect.map((info) => effectIdentity(target, info))),
209
+ open: (target, options) => {
210
+ if (target === filePath) opens += 1;
211
+ return fileSystem.open(target, options).pipe(
212
+ Effect.map((file) => ({
213
+ ...file,
214
+ stat: file.stat.pipe(Effect.map((info) => effectIdentity(target, info))),
215
+ readAlloc: (size: number) => file.readAlloc(size),
216
+ })),
217
+ );
218
+ },
219
+ });
220
+ const scanner = yield* AgentSessionScanner.AgentSessionScanner.pipe(
221
+ Effect.provide(layerScannerTest({ claudeHomePath, codexHomePath })),
222
+ Effect.provideService(FileSystem.FileSystem, simulatedFileSystem),
223
+ );
224
+ return {
225
+ scanner,
226
+ workspace,
227
+ opens: () => opens,
228
+ replace: Effect.gen(function* () {
229
+ yield* fileSystem.rename(filePath, savedPath);
230
+ yield* fileSystem.rename(replacementPath, filePath);
231
+ }).pipe(Effect.orDie),
232
+ };
233
+ },
234
+);
235
+
236
it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
237
describe("scan", () => {
238
it.effect("reads Claude project cwds from transcripts, newest first", () =>
@@ -1905,70 +1990,84 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
1990
1991
it.effect("checks file identity and provider before skipping completed history", () =>
1992
Effect.gen(function* () {
1908
- const path = yield* Path.Path;
1909
- const fileSystem = yield* FileSystem.FileSystem;
1910
- const nowMs = Date.parse("2026-08-24T12:00:00.000Z");
1911
- yield* TestClock.setTime(nowMs);
1912
- const claudeHomePath = yield* makeTempDir("t3code-completed-claude-");
1913
- const codexHomePath = yield* makeTempDir("t3code-completed-codex-");
1914
- const workspace = yield* makeTempDir("t3code-completed-workspace-");
1915
- const filePath = path.join(
1916
- codexHomePath,
1917
- "sessions",
1918
- "2026",
1919
- "08",
1920
- "24",
1921
- "rollout-replaced.jsonl",
1922
- );
1923
- const contents = (sessionId: string) =>
1924
- [
1925
- encodeTranscriptRecord({
1926
- type: "session_meta",
1927
- payload: { id: sessionId, cwd: workspace },
1928
- }),
1929
- encodeTranscriptRecord({
1930
- type: "event_msg",
1931
- payload: { type: "user_message", message: "Imported prompt" },
1932
- }),
1933
- ].join("\n");
1934
- yield* writeTranscript({
1935
- filePath,
1936
- contents: contents("original-session"),
1937
- mtimeMs: nowMs,
1993
+ const fixture = yield* makeFileIdentityFixture(42n, 43n);
1994
+ const { scanner, workspace } = fixture;
1995
+ const initial = yield* scanner.recentThreads(workspace).pipe(Stream.runCollect);
1996
+ const imported = initial[0];
1997
+ expect(imported?._tag).toBe("Importable");
1998
+ if (imported?._tag !== "Importable") return;
1999
+ const legacy = decodeImportSource({ ...imported.source, inode: 42 });
2000
+ const opensBeforeCompleted = fixture.opens();
2001
+ const completed = yield* scanner.recentThreads(workspace, [legacy]).pipe(Stream.runCollect);
2002
+ expect(completed[0]?._tag).toBe("AlreadyImported");
2003
+ expect(fixture.opens()).toBe(opensBeforeCompleted);
2004
+ const wrongProvider = yield* scanner
2005
+ .recentThreads(workspace, [{ ...imported.source, provider: "claudeAgent" }])
2006
+ .pipe(Stream.runCollect);
2007
+ expect(wrongProvider[0]?._tag).toBe("Importable");
2008
+
2009
+ yield* fixture.replace;
2010
+ const replaced = yield* scanner
2011
+ .recentThreads(workspace, [imported.source])
2012
+ .pipe(Stream.runCollect);
2013
+ expect(replaced[0]).toMatchObject({
2014
+ _tag: "Importable",
2015
+ thread: { providerSessionId: "replaced-session" },
2016
+ source: { size: imported.source.size, mtimeMs: imported.source.mtimeMs },
2017
});
2018
+ }),
2019
+ );
2020
1940
- yield* Effect.gen(function* () {
1941
- const scanner = yield* AgentSessionScanner.AgentSessionScanner;
2021
+ it.effect(
2022
+ "imports same-metadata replacements with exact large file IDs and backfills legacy IDs once",
2023
+ () =>
2024
+ Effect.gen(function* () {
2025
+ const fixture = yield* makeFileIdentityFixture();
2026
+ const { scanner, workspace } = fixture;
2027
const initial = yield* scanner.recentThreads(workspace).pipe(Stream.runCollect);
2028
const imported = initial[0];
2029
expect(imported?._tag).toBe("Importable");
2030
if (imported?._tag !== "Importable") return;
1946
- const completed = yield* scanner
1947
- .recentThreads(workspace, [imported.source])
1948
- .pipe(Stream.runCollect);
1949
- expect(completed[0]?._tag).toBe("AlreadyImported");
1950
- const wrongProvider = yield* scanner
1951
- .recentThreads(workspace, [{ ...imported.source, provider: "claudeAgent" }])
1952
- .pipe(Stream.runCollect);
1953
- expect(wrongProvider[0]?._tag).toBe("Importable");
2031
1955
- yield* fileSystem.open(filePath);
1956
- yield* fileSystem.remove(filePath);
1957
- yield* writeTranscript({
1958
- filePath,
1959
- contents: contents("replaced-session"),
1960
- mtimeMs: nowMs,
1961
- });
2032
+ yield* fixture.replace;
2033
const replaced = yield* scanner
2034
.recentThreads(workspace, [imported.source])
2035
.pipe(Stream.runCollect);
2036
expect(replaced[0]).toMatchObject({
2037
_tag: "Importable",
2038
thread: { providerSessionId: "replaced-session" },
1968
- source: { size: imported.source.size, mtimeMs: imported.source.mtimeMs },
2039
+ source: {
2040
+ ...imported.source,
2041
+ providerSessionId: "replaced-session",
2042
+ inode: "10000000000000000",
2043
+ },
2044
});
1970
- }).pipe(Effect.provide(layerScannerTest({ claudeHomePath, codexHomePath })));
1971
- }),
2045
+ expect(imported.source.inode).toBe("10000000000000001");
2046
+ if (replaced[0]?._tag !== "Importable") return;
2047
+
2048
+ const roundedInode = Number(imported.source.inode);
2049
+ expect(Number.isSafeInteger(roundedInode)).toBe(false);
2050
+ expect(String(roundedInode)).toBe(replaced[0].source.inode);
2051
+ for (const inode of [null, roundedInode]) {
2052
+ const legacy = decodeImportSource({ ...imported.source, inode });
2053
+ const opensBeforeBackfill = fixture.opens();
2054
+ const backfilled = yield* scanner
2055
+ .recentThreads(workspace, [legacy])
2056
+ .pipe(Stream.runCollect);
2057
+ expect(backfilled[0]).toMatchObject({
2058
+ _tag: "Importable",
2059
+ thread: { providerSessionId: "replaced-session" },
2060
+ source: replaced[0].source,
2061
+ });
2062
+ expect(fixture.opens()).toBe(opensBeforeBackfill + 1);
2063
+ if (backfilled[0]?._tag !== "Importable") return;
2064
+ const completed = yield* scanner
2065
+ .recentThreads(workspace, [backfilled[0].source])
2066
+ .pipe(Stream.runCollect);
2067
+ expect(completed[0]?._tag).toBe("AlreadyImported");
2068
+ expect(fixture.opens()).toBe(opensBeforeBackfill + 1);
2069
+ }
2070
+ }),
2071
);
2072
2073
it.effect("imports visible history from a transcript with an oversized tool record", () =>
apps/server/src/project/AgentSessionScanner.ts
+11
-2
@@ -566,7 +566,7 @@ function transcriptIdentity(filePath: string, stats: FileSystem.File.Info) {
566
size: Number(stats.size),
567
mtimeMs: Option.match(stats.mtime, { onNone: () => null, onSome: (date) => date.getTime() }),
568
device: Number(stats.dev),
569
- inode: Option.match(stats.ino, { onNone: () => null, onSome: Number }),
569
+ inode: Option.match(stats.ino, { onNone: () => null, onSome: String }),
570
birthtimeMs: Option.match(stats.birthtime, {
571
onNone: () => null,
572
onSome: (date) => date.getTime(),
@@ -579,13 +579,22 @@ type TranscriptIdentity = Pick<
579
"filePath" | "size" | "mtimeMs" | "device" | "inode" | "birthtimeMs"
580
>;
581
582
+const fileIdKey = (inode: TranscriptIdentity["inode"]) => (inode === null ? null : String(inode));
583
+
584
function sameTranscriptIdentity(left: TranscriptIdentity, right: TranscriptIdentity): boolean {
585
+ // Unsafe legacy numbers may already have lost file ID precision.
586
+ if (
587
+ (typeof left.inode === "number" && !Number.isSafeInteger(left.inode)) ||
588
+ (typeof right.inode === "number" && !Number.isSafeInteger(right.inode))
589
+ ) {
590
+ return false;
591
+ }
592
return (
593
left.filePath === right.filePath &&
594
left.size === right.size &&
595
left.mtimeMs === right.mtimeMs &&
596
left.device === right.device &&
588
- left.inode === right.inode &&
597
+ fileIdKey(left.inode) === fileIdKey(right.inode) &&
598
left.birthtimeMs === right.birthtimeMs
599
);
600
}