Skip to content

Commit 2718e15

Browse files
committed
fix: clean up abandoned uploads and track environment support
1 parent c5b11a8 commit 2718e15

9 files changed

Lines changed: 122 additions & 11 deletions

File tree

‎apps/server/src/assets/AttachmentUpload.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ describe("AttachmentUpload", () => {
6969
}).pipe(Effect.provide(testLayer)),
7070
);
7171

72+
it.effect("removes expired pending uploads while issuing a new upload URL", () =>
73+
Effect.gen(function* () {
74+
const config = yield* ServerConfig.ServerConfig;
75+
const staleId = "pending-00000000-0000-4000-8000-0000000000cc";
76+
const stalePath = NodePath.join(config.attachmentsDir, `${staleId}.png`);
77+
NodeFS.writeFileSync(stalePath, Buffer.from("pixels"));
78+
NodeFS.utimesSync(stalePath, 0, 0);
79+
80+
yield* TestClock.adjust("25 hours");
81+
yield* issueAttachmentUploadUrl(uploadInput);
82+
83+
expect(NodeFS.existsSync(stalePath)).toBe(false);
84+
}).pipe(Effect.provide(testLayer)),
85+
);
86+
7287
it.effect("stores the expected bytes without leaving temporary files", () =>
7388
Effect.gen(function* () {
7489
const config = yield* ServerConfig.ServerConfig;

‎apps/server/src/assets/AttachmentUpload.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
parseThreadSegmentFromAttachmentId,
1919
PENDING_ATTACHMENT_THREAD_SEGMENT,
2020
resolveAttachmentPathById,
21+
sweepStalePendingAttachments,
2122
} from "../attachmentStore.ts";
2223
import { resolveAttachmentRelativePath } from "../attachmentPaths.ts";
2324
import {
@@ -32,7 +33,10 @@ import { inferImageExtension } from "../imageMime.ts";
3233

3334
export const ATTACHMENT_UPLOAD_ROUTE_PREFIX = "/api/attachments/upload";
3435

36+
// Asset download tokens share this key, but their signed claim kind is different.
3537
const SIGNING_SECRET_NAME = "asset-access-signing-key";
38+
const PENDING_ATTACHMENT_SWEEP_INTERVAL_MS = 15 * 60_000;
39+
const lastPendingSweepByDirectory = new Map<string, number>();
3640

3741
const AttachmentUploadClaims = Schema.Struct({
3842
version: Schema.Literal(1),
@@ -68,8 +72,25 @@ export const issueAttachmentUploadUrl = Effect.fn("AttachmentUpload.issueUrl")(f
6872
const secret = yield* loadSigningSecret.pipe(
6973
Effect.mapError((cause) => new AttachmentUploadSigningKeyError({ cause })),
7074
);
75+
const config = yield* ServerConfig.ServerConfig;
76+
const nowMs = yield* Clock.currentTimeMillis;
77+
const previousSweep = lastPendingSweepByDirectory.get(config.attachmentsDir);
78+
if (
79+
previousSweep === undefined ||
80+
nowMs - previousSweep >= PENDING_ATTACHMENT_SWEEP_INTERVAL_MS
81+
) {
82+
lastPendingSweepByDirectory.set(config.attachmentsDir, nowMs);
83+
const swept = sweepStalePendingAttachments({
84+
attachmentsDir: config.attachmentsDir,
85+
nowMs,
86+
});
87+
if (swept.deleted > 0) {
88+
yield* Effect.logInfo("Removed expired attachment uploads.", { deleted: swept.deleted });
89+
}
90+
}
91+
7192
const attachmentId = createPendingAttachmentId();
72-
const expiresAt = (yield* Clock.currentTimeMillis) + ATTACHMENT_UPLOAD_URL_TTL_MS;
93+
const expiresAt = nowMs + ATTACHMENT_UPLOAD_URL_TTL_MS;
7394
const encodedPayload = base64UrlEncode(
7495
encodeAttachmentUploadClaims({
7596
version: 1,

‎apps/server/src/orchestration/Normalizer.attachments.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,31 @@ describe("normalizeDispatchCommand attachments", () => {
190190
}).pipe(Effect.provide(testLayer)),
191191
);
192192

193+
it.effect("removes a failed claimed copy after its pending original was removed", () =>
194+
Effect.gen(function* () {
195+
const config = yield* ServerConfig.ServerConfig;
196+
const pendingPath = NodePath.join(config.attachmentsDir, `pending-${attachmentUuid}.png`);
197+
NodeFS.writeFileSync(pendingPath, Buffer.from("pixels"));
198+
const command = turnStartCommand({
199+
attachments: [{ id: `pending-${attachmentUuid}`, sizeBytes: 6 }],
200+
});
201+
const normalized = yield* normalizeDispatchCommand(command);
202+
if (normalized.type !== "thread.turn.start") {
203+
throw new Error("Expected a thread.turn.start command.");
204+
}
205+
206+
const claimedPath = NodePath.join(
207+
config.attachmentsDir,
208+
`${normalized.message.attachments[0]!.id}.png`,
209+
);
210+
NodeFS.rmSync(pendingPath);
211+
212+
yield* cleanupFailedUploadedAttachments(command, normalized);
213+
214+
expect(NodeFS.existsSync(claimedPath)).toBe(false);
215+
}).pipe(Effect.provide(testLayer)),
216+
);
217+
193218
it.effect("keeps concurrent claims independent when one dispatch fails", () =>
194219
Effect.gen(function* () {
195220
const config = yield* ServerConfig.ServerConfig;

‎apps/server/src/orchestration/Normalizer.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
PENDING_ATTACHMENT_THREAD_SEGMENT,
1717
parseThreadSegmentFromAttachmentId,
1818
resolveAttachmentPath,
19-
resolveAttachmentPathById,
2019
} from "../attachmentStore.ts";
2120
import { ServerConfig } from "../config.ts";
2221
import { parseBase64DataUrl } from "../imageMime.ts";
@@ -286,15 +285,11 @@ export const cleanupFailedUploadedAttachments = Effect.fn(
286285
continue;
287286
}
288287

289-
const pendingPath = resolveAttachmentPathById({
290-
attachmentsDir: serverConfig.attachmentsDir,
291-
attachmentId: original.id,
292-
});
293288
const claimedPath = resolveAttachmentPath({
294289
attachmentsDir: serverConfig.attachmentsDir,
295290
attachment,
296291
});
297-
if (pendingPath && claimedPath && pendingPath !== claimedPath) {
292+
if (claimedPath) {
298293
claimedPaths.push(claimedPath);
299294
}
300295
}

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2100,8 +2100,10 @@ function ChatViewContent(props: ChatViewProps) {
21002100
: (primaryEnvironment?.serverConfig ?? null);
21012101
const pullRequestsCapabilityKnown = serverConfig !== null;
21022102
const supportsPullRequests = serverConfig?.environment.capabilities.pullRequests === true;
2103+
const attachmentEnvironmentConfig = environmentById.get(environmentId)?.serverConfig ?? null;
2104+
const attachmentUploadsCapabilityKnown = attachmentEnvironmentConfig !== null;
21032105
const supportsAttachmentUploads =
2104-
serverConfig?.environment.capabilities.attachmentUploads === true;
2106+
attachmentEnvironmentConfig?.environment.capabilities.attachmentUploads === true;
21052107
const versionMismatch = resolveServerConfigVersionMismatch(serverConfig);
21062108
const versionMismatchDismissKey =
21072109
versionMismatch && activeThread
@@ -6806,6 +6808,7 @@ function ChatViewContent(props: ChatViewProps) {
68066808
composerRef={composerRef}
68076809
composerDraftTarget={composerDraftTarget}
68086810
environmentId={environmentId}
6811+
attachmentUploadsCapabilityKnown={attachmentUploadsCapabilityKnown}
68096812
supportsAttachmentUploads={supportsAttachmentUploads}
68106813
routeKind={routeKind}
68116814
routeThreadRef={routeThreadRef}

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ export interface ChatComposerHandle {
544544
export interface ChatComposerProps {
545545
composerDraftTarget: ScopedThreadRef | DraftId;
546546
environmentId: EnvironmentId;
547+
attachmentUploadsCapabilityKnown: boolean;
547548
supportsAttachmentUploads: boolean;
548549
routeKind: "server" | "draft";
549550
routeThreadRef: ScopedThreadRef;
@@ -659,6 +660,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
659660
const {
660661
composerDraftTarget,
661662
environmentId,
663+
attachmentUploadsCapabilityKnown,
662664
supportsAttachmentUploads,
663665
routeKind,
664666
routeThreadRef,
@@ -781,6 +783,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
781783
const getComposerDraft = useComposerDraftStore((store) => store.getComposerDraft);
782784

783785
useEffect(() => {
786+
if (!attachmentUploadsCapabilityKnown) {
787+
return;
788+
}
784789
if (!supportsAttachmentUploads) {
785790
for (const image of composerImages) {
786791
releaseAttachmentUpload(image.id);
@@ -790,7 +795,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
790795
for (const image of composerImages) {
791796
startAttachmentUpload({ environmentId, image });
792797
}
793-
}, [composerImages, environmentId, supportsAttachmentUploads]);
798+
}, [attachmentUploadsCapabilityKnown, composerImages, environmentId, supportsAttachmentUploads]);
794799

795800
// ------------------------------------------------------------------
796801
// Model state

‎apps/web/src/lib/attachmentUploadQueue.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,50 @@ describe("attachmentUploadQueue", () => {
211211
expect(readAttachmentUpload(image.id)).toMatchObject({ status: "ready" });
212212
});
213213

214+
it("releases an upload URL that resolves after its image was removed", async () => {
215+
const image = makeImage("image-cancelled");
216+
const minted = {
217+
_tag: "Success" as const,
218+
value: {
219+
attachmentId: "pending-environment-1-image-cancelled.png",
220+
relativeUrl: "/api/attachments/upload/cancelled",
221+
expiresAt: 1,
222+
},
223+
};
224+
let resolveMint: (result: typeof minted) => void = () => {};
225+
const pendingMint = new Promise<typeof minted>((resolve) => {
226+
resolveMint = resolve;
227+
});
228+
let resolveDelete: () => void = () => {};
229+
const deleted = new Promise<void>((resolve) => {
230+
resolveDelete = resolve;
231+
});
232+
mocks.runAtomCommand.mockImplementation((_registry: unknown, command: unknown) => {
233+
if (command === mocks.createUploadUrl) {
234+
return pendingMint;
235+
}
236+
resolveDelete();
237+
return Promise.resolve({ _tag: "Success", value: undefined });
238+
});
239+
240+
startAttachmentUpload({ environmentId: firstEnvironment, image });
241+
releaseAttachmentUpload(image.id);
242+
resolveMint(minted);
243+
await deleted;
244+
245+
expect(TestXmlHttpRequest.requests).toEqual([]);
246+
expect(readAttachmentUpload(image.id)).toBeUndefined();
247+
expect(mocks.runAtomCommand).toHaveBeenCalledWith(
248+
expect.anything(),
249+
mocks.removeUpload,
250+
{
251+
environmentId: firstEnvironment,
252+
input: { attachmentId: minted.value.attachmentId },
253+
},
254+
expect.anything(),
255+
);
256+
});
257+
214258
it("restores the previous environment after a replacement upload fails", async () => {
215259
const image = makeImage("image-move");
216260
startAttachmentUpload({ environmentId: firstEnvironment, image });

‎apps/web/src/lib/attachmentUploadQueue.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ async function runUpload(job: UploadJob): Promise<void> {
128128
{ reportFailure: false },
129129
);
130130
if (job.cancelled) {
131+
if (minted._tag === "Success") {
132+
deletePendingUpload(job.environmentId, minted.value.attachmentId);
133+
}
131134
return;
132135
}
133136
if (minted._tag !== "Success") {

‎docs/user/composer.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ Messages can contain up to 120,000 characters. If a draft is longer, T3 Code kee
44
composer and shows how many characters need to be removed. Shorten the draft or split it into
55
multiple messages, then send again in the same thread.
66

7-
Image attachments upload as soon as you add them when your server supports direct uploads. Wait for
8-
each upload to finish before you send the message. If an upload fails, retry it or remove the image.
7+
On servers that support direct uploads, images upload as soon as you add them. The send button
8+
becomes available after every upload finishes. Failed uploads can be retried or removed.
99

1010
## Commands and skills
1111

0 commit comments

Comments
 (0)