@devries-cameron20 / t3code-1 / commits / a56418f8bc
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Reuse attachment thread segment sanitizer in projection pipeline

- export and reuse `toSafeThreadAttachmentSegment` from `attachmentStore` - align projection attachment path logic with store-safe thread IDs - update projection pipeline tests to cover thread IDs with spaces

Julius Marminge committed Mar 1, 2026 at 18:49 UTC a56418f8bc9866ab114c85b4e73a7f95987c3469
3 files changed +36 -47
apps/server/src/attachmentStore.ts
+1 -1
@@ -13,7 +13,7 @@ import { inferImageExtension, SAFE_IMAGE_FILE_EXTENSIONS } from "./imageMime.ts"
13 const ATTACHMENT_FILENAME_EXTENSIONS = [...SAFE_IMAGE_FILE_EXTENSIONS, ".bin"];
14 const ATTACHMENT_ID_THREAD_SEGMENT_MAX_CHARS = 80;
15
16 -function toSafeThreadAttachmentSegment(threadId: string): string | null {
16 +export function toSafeThreadAttachmentSegment(threadId: string): string | null {
17 const segment = threadId
18 .trim()
19 .replace(/[^a-z0-9_-]+/gi, "-")
apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts
+29 -24
@@ -733,6 +733,9 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
733 const projectionPipeline = yield* OrchestrationProjectionPipeline;
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";
739
740 const appendAndProject = (event: Parameters<typeof eventStore.append>[0]) =>
741 eventStore
@@ -764,14 +767,14 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
767 type: "thread.created",
768 eventId: EventId.makeUnsafe("evt-revert-files-2"),
769 aggregateKind: "thread",
767 - aggregateId: ThreadId.makeUnsafe("thread-revert-files"),
770 + aggregateId: threadId,
771 occurredAt: now,
772 commandId: CommandId.makeUnsafe("cmd-revert-files-2"),
773 causationEventId: null,
774 correlationId: CorrelationId.makeUnsafe("cmd-revert-files-2"),
775 metadata: {},
776 payload: {
774 - threadId: ThreadId.makeUnsafe("thread-revert-files"),
777 + threadId,
778 projectId: ProjectId.makeUnsafe("project-revert-files"),
779 title: "Thread Revert Files",
780 model: "gpt-5-codex",
@@ -786,14 +789,14 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
789 type: "thread.turn-diff-completed",
790 eventId: EventId.makeUnsafe("evt-revert-files-3"),
791 aggregateKind: "thread",
789 - aggregateId: ThreadId.makeUnsafe("thread-revert-files"),
792 + aggregateId: threadId,
793 occurredAt: now,
794 commandId: CommandId.makeUnsafe("cmd-revert-files-3"),
795 causationEventId: null,
796 correlationId: CorrelationId.makeUnsafe("cmd-revert-files-3"),
797 metadata: {},
798 payload: {
796 - threadId: ThreadId.makeUnsafe("thread-revert-files"),
799 + threadId,
800 turnId: TurnId.makeUnsafe("turn-keep"),
801 checkpointTurnCount: 1,
802 checkpointRef: CheckpointRef.makeUnsafe("refs/t3/checkpoints/thread-revert-files/turn/1"),
@@ -808,21 +811,21 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
811 type: "thread.message-sent",
812 eventId: EventId.makeUnsafe("evt-revert-files-4"),
813 aggregateKind: "thread",
811 - aggregateId: ThreadId.makeUnsafe("thread-revert-files"),
814 + aggregateId: threadId,
815 occurredAt: now,
816 commandId: CommandId.makeUnsafe("cmd-revert-files-4"),
817 causationEventId: null,
818 correlationId: CorrelationId.makeUnsafe("cmd-revert-files-4"),
819 metadata: {},
820 payload: {
818 - threadId: ThreadId.makeUnsafe("thread-revert-files"),
821 + threadId,
822 messageId: MessageId.makeUnsafe("message-keep"),
823 role: "assistant",
824 text: "Keep",
825 attachments: [
826 {
827 type: "image",
825 - id: "thread-revert-files-keep",
828 + id: keepAttachmentId,
829 name: "keep.png",
830 mimeType: "image/png",
831 sizeBytes: 5,
@@ -839,14 +842,14 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
842 type: "thread.turn-diff-completed",
843 eventId: EventId.makeUnsafe("evt-revert-files-5"),
844 aggregateKind: "thread",
842 - aggregateId: ThreadId.makeUnsafe("thread-revert-files"),
845 + aggregateId: threadId,
846 occurredAt: now,
847 commandId: CommandId.makeUnsafe("cmd-revert-files-5"),
848 causationEventId: null,
849 correlationId: CorrelationId.makeUnsafe("cmd-revert-files-5"),
850 metadata: {},
851 payload: {
849 - threadId: ThreadId.makeUnsafe("thread-revert-files"),
852 + threadId,
853 turnId: TurnId.makeUnsafe("turn-remove"),
854 checkpointTurnCount: 2,
855 checkpointRef: CheckpointRef.makeUnsafe("refs/t3/checkpoints/thread-revert-files/turn/2"),
@@ -861,21 +864,21 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
864 type: "thread.message-sent",
865 eventId: EventId.makeUnsafe("evt-revert-files-6"),
866 aggregateKind: "thread",
864 - aggregateId: ThreadId.makeUnsafe("thread-revert-files"),
867 + aggregateId: threadId,
868 occurredAt: now,
869 commandId: CommandId.makeUnsafe("cmd-revert-files-6"),
870 causationEventId: null,
871 correlationId: CorrelationId.makeUnsafe("cmd-revert-files-6"),
872 metadata: {},
873 payload: {
871 - threadId: ThreadId.makeUnsafe("thread-revert-files"),
874 + threadId,
875 messageId: MessageId.makeUnsafe("message-remove"),
876 role: "assistant",
877 text: "Remove",
878 attachments: [
879 {
880 type: "image",
878 - id: "thread-revert-files-remove",
881 + id: removeAttachmentId,
882 name: "remove.png",
883 mimeType: "image/png",
884 sizeBytes: 5,
@@ -891,12 +894,12 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
894 const keepPath = path.join(
895 stateDir,
896 "attachments",
894 - "thread-revert-files-keep.png",
897 + `${keepAttachmentId}.png`,
898 );
899 const removePath = path.join(
900 stateDir,
901 "attachments",
899 - "thread-revert-files-remove.png",
902 + `${removeAttachmentId}.png`,
903 );
904 fs.mkdirSync(path.join(stateDir, "attachments"), { recursive: true });
905 fs.writeFileSync(keepPath, Buffer.from("keep"));
@@ -908,14 +911,14 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
911 type: "thread.reverted",
912 eventId: EventId.makeUnsafe("evt-revert-files-7"),
913 aggregateKind: "thread",
911 - aggregateId: ThreadId.makeUnsafe("thread-revert-files"),
914 + aggregateId: threadId,
915 occurredAt: now,
916 commandId: CommandId.makeUnsafe("cmd-revert-files-7"),
917 causationEventId: null,
918 correlationId: CorrelationId.makeUnsafe("cmd-revert-files-7"),
919 metadata: {},
920 payload: {
918 - threadId: ThreadId.makeUnsafe("thread-revert-files"),
921 + threadId,
922 turnCount: 1,
923 },
924 });
@@ -939,6 +942,8 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
942 const projectionPipeline = yield* OrchestrationProjectionPipeline;
943 const eventStore = yield* OrchestrationEventStore;
944 const now = new Date().toISOString();
945 + const threadId = ThreadId.makeUnsafe("thread delete.files");
946 + const attachmentId = "thread-delete-files-att-1";
947
948 const appendAndProject = (event: Parameters<typeof eventStore.append>[0]) =>
949 eventStore
@@ -970,14 +975,14 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
975 type: "thread.created",
976 eventId: EventId.makeUnsafe("evt-delete-files-2"),
977 aggregateKind: "thread",
973 - aggregateId: ThreadId.makeUnsafe("thread-delete-files"),
978 + aggregateId: threadId,
979 occurredAt: now,
980 commandId: CommandId.makeUnsafe("cmd-delete-files-2"),
981 causationEventId: null,
982 correlationId: CorrelationId.makeUnsafe("cmd-delete-files-2"),
983 metadata: {},
984 payload: {
980 - threadId: ThreadId.makeUnsafe("thread-delete-files"),
985 + threadId,
986 projectId: ProjectId.makeUnsafe("project-delete-files"),
987 title: "Thread Delete Files",
988 model: "gpt-5-codex",
@@ -992,21 +997,21 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
997 type: "thread.message-sent",
998 eventId: EventId.makeUnsafe("evt-delete-files-3"),
999 aggregateKind: "thread",
995 - aggregateId: ThreadId.makeUnsafe("thread-delete-files"),
1000 + aggregateId: threadId,
1001 occurredAt: now,
1002 commandId: CommandId.makeUnsafe("cmd-delete-files-3"),
1003 causationEventId: null,
1004 correlationId: CorrelationId.makeUnsafe("cmd-delete-files-3"),
1005 metadata: {},
1006 payload: {
1002 - threadId: ThreadId.makeUnsafe("thread-delete-files"),
1007 + threadId,
1008 messageId: MessageId.makeUnsafe("message-delete-files"),
1009 role: "user",
1010 text: "Delete",
1011 attachments: [
1012 {
1013 type: "image",
1009 - id: "thread-delete-files-att-1",
1014 + id: attachmentId,
1015 name: "delete.png",
1016 mimeType: "image/png",
1017 sizeBytes: 5,
@@ -1019,7 +1024,7 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
1024 },
1025 });
1026
1022 - const threadAttachmentPath = path.join(stateDir, "attachments", "thread-delete-files-att-1.png");
1027 + const threadAttachmentPath = path.join(stateDir, "attachments", `${attachmentId}.png`);
1028 fs.mkdirSync(path.join(stateDir, "attachments"), { recursive: true });
1029 fs.writeFileSync(threadAttachmentPath, Buffer.from("delete"));
1030 assert.equal(fs.existsSync(threadAttachmentPath), true);
@@ -1028,14 +1033,14 @@ projectionLayer("OrchestrationProjectionPipeline", (it) => {
1033 type: "thread.deleted",
1034 eventId: EventId.makeUnsafe("evt-delete-files-4"),
1035 aggregateKind: "thread",
1031 - aggregateId: ThreadId.makeUnsafe("thread-delete-files"),
1036 + aggregateId: threadId,
1037 occurredAt: now,
1038 commandId: CommandId.makeUnsafe("cmd-delete-files-4"),
1039 causationEventId: null,
1040 correlationId: CorrelationId.makeUnsafe("cmd-delete-files-4"),
1041 metadata: {},
1042 payload: {
1038 - threadId: ThreadId.makeUnsafe("thread-delete-files"),
1043 + threadId,
1044 deletedAt: now,
1045 },
1046 });
apps/server/src/orchestration/Layers/ProjectionPipeline.ts
+6 -22
@@ -37,7 +37,7 @@ import {
37 OrchestrationProjectionPipeline,
38 type OrchestrationProjectionPipelineShape,
39 } from "../Services/ProjectionPipeline.ts";
40 -import { attachmentRelativePath } from "../../attachmentStore.ts";
40 +import { attachmentRelativePath, toSafeThreadAttachmentSegment } from "../../attachmentStore.ts";
41
42 export const ORCHESTRATION_PROJECTOR_NAMES = {
43 projects: "projection.projects",
@@ -66,27 +66,11 @@ interface AttachmentSideEffects {
66 readonly prunedThreadRelativePaths: Map<string, Set<string>>;
67 }
68
69 -function toSafeThreadAttachmentSegment(threadId: string): string | null {
70 - const segment = encodeURIComponent(threadId);
71 - if (
72 - segment.length === 0 ||
73 - segment === "." ||
74 - segment === ".." ||
75 - segment.includes("/") ||
76 - segment.includes("\\") ||
77 - segment.includes("\0")
78 - ) {
79 - return null;
80 - }
81 - return segment;
82 -}
83 -
84 -const materializeAttachmentsForProjection = Effect.fn(function* (input: {
85 - readonly attachments: ReadonlyArray<ChatAttachment>;
86 -}) {
87 - if (input.attachments.length === 0) return [];
88 - return input.attachments;
89 -});
69 +const materializeAttachmentsForProjection = Effect.fn(
70 + (input: {
71 + readonly attachments: ReadonlyArray<ChatAttachment>;
72 + }) => Effect.succeed(input.attachments.length === 0 ? [] : input.attachments),
73 +);
74
75 function extractActivityRequestId(payload: unknown): ApprovalRequestId | null {
76 if (typeof payload !== "object" || payload === null) {