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 25, 11:00 UTC
Fix attachment cleanup to match exact thread segments
- Parse thread segments from attachment IDs using a UUID-aware pattern - Prevent revert/delete cleanup from removing files belonging to similarly prefixed threads - Add tests for prefix-collision cases in attachment store and projection pipeline
Julius Marminge committed
Mar 1, 2026 at 19:01 UTC
d58af33fba73936659158ba2167a00475889d1e5
4 files changed
+72
-9
apps/server/src/attachmentStore.test.ts
+14
-2
@@ -4,7 +4,11 @@ import path from "node:path";
4
5
import { describe, expect, it } from "vitest";
6
7
-import { createAttachmentId, resolveAttachmentPathById } from "./attachmentStore.ts";
7
+import {
8
+ createAttachmentId,
9
+ parseThreadSegmentFromAttachmentId,
10
+ resolveAttachmentPathById,
11
+} from "./attachmentStore.ts";
12
13
describe("attachmentStore", () => {
14
it("sanitizes thread ids when creating attachment ids", () => {
@@ -14,7 +18,7 @@ describe("attachmentStore", () => {
18
return;
19
}
20
17
- const [threadSegment] = attachmentId.split("-", 1);
21
+ const threadSegment = parseThreadSegmentFromAttachmentId(attachmentId);
22
expect(threadSegment).toBeTruthy();
23
expect(threadSegment).toMatch(/^[a-z0-9_-]+$/i);
24
expect(threadSegment).not.toContain(".");
@@ -22,6 +26,14 @@ describe("attachmentStore", () => {
26
expect(threadSegment).not.toContain("/");
27
});
28
29
+ it("parses exact thread segments from attachment ids without prefix collisions", () => {
30
+ const fooId = "foo-00000000-0000-4000-8000-000000000001";
31
+ const fooBarId = "foo-bar-00000000-0000-4000-8000-000000000002";
32
+
33
+ expect(parseThreadSegmentFromAttachmentId(fooId)).toBe("foo");
34
+ expect(parseThreadSegmentFromAttachmentId(fooBarId)).toBe("foo-bar");
35
+ });
36
+
37
it("resolves attachment path by id using the extension that exists on disk", () => {
38
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "t3code-attachment-store-"));
39
try {
apps/server/src/attachmentStore.ts
+19
@@ -12,6 +12,13 @@ import { inferImageExtension, SAFE_IMAGE_FILE_EXTENSIONS } from "./imageMime.ts"
12
13
const ATTACHMENT_FILENAME_EXTENSIONS = [...SAFE_IMAGE_FILE_EXTENSIONS, ".bin"];
14
const ATTACHMENT_ID_THREAD_SEGMENT_MAX_CHARS = 80;
15
+const ATTACHMENT_ID_THREAD_SEGMENT_PATTERN = "[a-z0-9_]+(?:-[a-z0-9_]+)*";
16
+const ATTACHMENT_ID_UUID_PATTERN =
17
+ "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
18
+const ATTACHMENT_ID_PATTERN = new RegExp(
19
+ `^(${ATTACHMENT_ID_THREAD_SEGMENT_PATTERN})-(${ATTACHMENT_ID_UUID_PATTERN})$`,
20
+ "i",
21
+);
22
23
export function toSafeThreadAttachmentSegment(threadId: string): string | null {
24
const segment = threadId
@@ -35,6 +42,18 @@ export function createAttachmentId(threadId: string): string | null {
42
return `${threadSegment}-${randomUUID()}`;
43
}
44
45
+export function parseThreadSegmentFromAttachmentId(attachmentId: string): string | null {
46
+ const normalizedId = normalizeAttachmentRelativePath(attachmentId);
47
+ if (!normalizedId || normalizedId.includes("/") || normalizedId.includes(".")) {
48
+ return null;
49
+ }
50
+ const match = normalizedId.match(ATTACHMENT_ID_PATTERN);
51
+ if (!match) {
52
+ return null;
53
+ }
54
+ return match[1]?.toLowerCase() ?? null;
55
+}
56
+
57
export function attachmentRelativePath(attachment: ChatAttachment): string {
58
switch (attachment.type) {
59
case "image": {
apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts
+19
-3
@@ -734,8 +734,10 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
734
const eventStore = yield* OrchestrationEventStore;
735
const now = new Date().toISOString();
736
const threadId = ThreadId.makeUnsafe("thread revert.files");
737
- const keepAttachmentId = "thread-revert-files-keep";
738
- const removeAttachmentId = "thread-revert-files-remove";
737
+ const keepAttachmentId = "thread-revert-files-00000000-0000-4000-8000-000000000001";
738
+ const removeAttachmentId = "thread-revert-files-00000000-0000-4000-8000-000000000002";
739
+ const otherThreadAttachmentId =
740
+ "thread-revert-files-extra-00000000-0000-4000-8000-000000000003";
741
742
const appendAndProject = (event: Parameters<typeof eventStore.append>[0]) =>
743
eventStore
@@ -904,8 +906,11 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
906
fs.mkdirSync(path.join(stateDir, "attachments"), { recursive: true });
907
fs.writeFileSync(keepPath, Buffer.from("keep"));
908
fs.writeFileSync(removePath, Buffer.from("remove"));
909
+ const otherThreadPath = path.join(stateDir, "attachments", `${otherThreadAttachmentId}.png`);
910
+ fs.writeFileSync(otherThreadPath, Buffer.from("other"));
911
assert.equal(fs.existsSync(keepPath), true);
912
assert.equal(fs.existsSync(removePath), true);
913
+ assert.equal(fs.existsSync(otherThreadPath), true);
914
915
yield* appendAndProject({
916
type: "thread.reverted",
@@ -925,6 +930,7 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
930
931
assert.equal(fs.existsSync(keepPath), true);
932
assert.equal(fs.existsSync(removePath), false);
933
+ assert.equal(fs.existsSync(otherThreadPath), true);
934
}).pipe(
935
(effect) => runWithProjectionPipelineLayer(stateDir, effect),
936
Effect.ensuring(Effect.sync(() => fs.rmSync(stateDir, { recursive: true, force: true }))),
@@ -943,7 +949,9 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
949
const eventStore = yield* OrchestrationEventStore;
950
const now = new Date().toISOString();
951
const threadId = ThreadId.makeUnsafe("thread delete.files");
946
- const attachmentId = "thread-delete-files-att-1";
952
+ const attachmentId = "thread-delete-files-00000000-0000-4000-8000-000000000001";
953
+ const otherThreadAttachmentId =
954
+ "thread-delete-files-extra-00000000-0000-4000-8000-000000000002";
955
956
const appendAndProject = (event: Parameters<typeof eventStore.append>[0]) =>
957
eventStore
@@ -1025,9 +1033,16 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
1033
});
1034
1035
const threadAttachmentPath = path.join(stateDir, "attachments", `${attachmentId}.png`);
1036
+ const otherThreadAttachmentPath = path.join(
1037
+ stateDir,
1038
+ "attachments",
1039
+ `${otherThreadAttachmentId}.png`,
1040
+ );
1041
fs.mkdirSync(path.join(stateDir, "attachments"), { recursive: true });
1042
fs.writeFileSync(threadAttachmentPath, Buffer.from("delete"));
1043
+ fs.writeFileSync(otherThreadAttachmentPath, Buffer.from("other-thread"));
1044
assert.equal(fs.existsSync(threadAttachmentPath), true);
1045
+ assert.equal(fs.existsSync(otherThreadAttachmentPath), true);
1046
1047
yield* appendAndProject({
1048
type: "thread.deleted",
@@ -1046,6 +1061,7 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
1061
});
1062
1063
assert.equal(fs.existsSync(threadAttachmentPath), false);
1064
+ assert.equal(fs.existsSync(otherThreadAttachmentPath), true);
1065
}).pipe(
1066
(effect) => runWithProjectionPipelineLayer(stateDir, effect),
1067
Effect.ensuring(Effect.sync(() => fs.rmSync(stateDir, { recursive: true, force: true }))),
apps/server/src/orchestration/Layers/ProjectionPipeline.ts
+20
-4
@@ -37,7 +37,12 @@ import {
37
OrchestrationProjectionPipeline,
38
type OrchestrationProjectionPipelineShape,
39
} from "../Services/ProjectionPipeline.ts";
40
-import { attachmentRelativePath, toSafeThreadAttachmentSegment } from "../../attachmentStore.ts";
40
+import {
41
+ attachmentRelativePath,
42
+ parseAttachmentIdFromRelativePath,
43
+ parseThreadSegmentFromAttachmentId,
44
+ toSafeThreadAttachmentSegment,
45
+} from "../../attachmentStore.ts";
46
47
export const ORCHESTRATION_PROJECTOR_NAMES = {
48
projects: "projection.projects",
@@ -198,7 +203,8 @@ function collectThreadAttachmentRelativePaths(
203
if (attachment.type !== "image") {
204
continue;
205
}
201
- if (!attachment.id.startsWith(`${threadSegment}-`)) {
206
+ const attachmentThreadSegment = parseThreadSegmentFromAttachmentId(attachment.id);
207
+ if (!attachmentThreadSegment || attachmentThreadSegment !== threadSegment) {
208
continue;
209
}
210
relativePaths.add(attachmentRelativePath(attachment));
@@ -236,7 +242,12 @@ const runAttachmentSideEffects = Effect.fn(function* (sideEffects: AttachmentSid
242
if (normalizedEntry.length === 0 || normalizedEntry.includes("/")) {
243
return;
244
}
239
- if (!normalizedEntry.startsWith(`${threadSegment}-`)) {
245
+ const attachmentId = parseAttachmentIdFromRelativePath(normalizedEntry);
246
+ if (!attachmentId) {
247
+ return;
248
+ }
249
+ const attachmentThreadSegment = parseThreadSegmentFromAttachmentId(attachmentId);
250
+ if (!attachmentThreadSegment || attachmentThreadSegment !== threadSegment) {
251
return;
252
}
253
yield* fileSystem.remove(path.join(attachmentsRootDir, normalizedEntry), {
@@ -272,7 +283,12 @@ const runAttachmentSideEffects = Effect.fn(function* (sideEffects: AttachmentSid
283
if (relativePath.length === 0 || relativePath.includes("/")) {
284
return;
285
}
275
- if (!relativePath.startsWith(`${threadSegment}-`)) {
286
+ const attachmentId = parseAttachmentIdFromRelativePath(relativePath);
287
+ if (!attachmentId) {
288
+ return;
289
+ }
290
+ const attachmentThreadSegment = parseThreadSegmentFromAttachmentId(attachmentId);
291
+ if (!attachmentThreadSegment || attachmentThreadSegment !== threadSegment) {
292
return;
293
}
294