fix: address review findings on upload-on-attach

Real bugs from Macroscope and Bugbot, all verified against source:

- awaitAttachmentUploads consulted only live jobs, so an upload that
  settled before the await lost its result and a ready image could drop
  from a pick-and-send message. Terminal states are now kept in a
  settled map that cancel/release clean up.
- Stashing cancelled in-flight uploads before the localStorage write was
  confirmed; a quota failure stranded chips in uploading forever.
  Cancellation now happens only after the write lands.
- Retargeting a draft to another environment overwrote a reload-restored
  attachment's ready state with a dead failed state. The mismatch is now
  derived (never written), so switching back recovers the attachment,
  and re-uploads release the old environment's bytes first.
- The turn-start failure path restored chips from the pre-await
  snapshot, resurrecting uploading states no job would ever advance.
- The "images were not attached" toast could fire when no message was
  sent; it now waits for the turn to actually start.
- Removing a preview annotation or discarding a draft leaked the upload
  and its server-side bytes; both now release like chip removal.
- Mobile paste drops now surface the needs-an-update banner instead of
  a console.warn.
- Normalizer error mappings keep their PlatformError causes; the dead
  unstructured AttachmentUploadRequestError is deleted; the sweep test
  no longer reads the real clock (effect-diagnostics CI failure).

Also folds the rebase onto main: the new moveComposerPromptAndImages
draft-carry now merges the unified image list, and in-flight uploads are
retargeted to the destination draft so moved chips keep their progress.

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Theo BrowneandClaude Fable 5 committed 2026-08-13 18:03:23 -07:00
1 parent f695b6f5bf
commit 03196ef7ad
18 files changed
+299 -135

No files matched your search

+12 -7
View File
@@ -48,9 +48,19 @@ const UploadClaimsSchema = Schema.Struct({
export type AttachmentUploadClaims = typeof UploadClaimsSchema.Type;
const UploadClaimsJson = Schema.fromJsonString(UploadClaimsSchema);
const decodeUploadClaims = Schema.decodeUnknownOption(UploadClaimsJson);
const decodeUploadClaimsOption = Schema.decodeUnknownOption(UploadClaimsJson);
const encodeUploadClaims = Schema.encodeSync(UploadClaimsJson);
// Plain function (not Effect) so the base64/JSON failure modes stay a simple
// null, mirroring AssetAccess.decodeClaims.
function decodeUploadClaims(encodedPayload: string): AttachmentUploadClaims | null {
try {
return Option.getOrNull(decodeUploadClaimsOption(base64UrlDecodeUtf8(encodedPayload)));
} catch {
return null;
}
}
const loadSigningSecret = Effect.gen(function* () {
const secretStore = yield* ServerSecretStore.ServerSecretStore;
return yield* secretStore.getOrCreateRandom(SIGNING_SECRET_NAME, 32);
@@ -104,12 +114,7 @@ export const validateAttachmentUploadToken = Effect.fn("AttachmentUpload.validat
if (!secret) return null;
if (!timingSafeEqualBase64Url(signature, signPayload(encodedPayload, secret))) return null;
let claims: AttachmentUploadClaims | null;
try {
claims = Option.getOrNull(decodeUploadClaims(base64UrlDecodeUtf8(encodedPayload)));
} catch {
claims = null;
}
const claims = decodeUploadClaims(encodedPayload);
if (!claims || claims.expiresAt <= (yield* Clock.currentTimeMillis)) return null;
return claims;
});
+7 -2
View File
@@ -191,17 +191,22 @@ describe("attachmentStore", () => {
attachmentsDir,
"pending-00000000-0000-4000-8000-000000000004.png.part",
);
// Fixed epoch so the test never touches the real clock: files are
// stamped relative to `nowMs` below, not to wall time.
const nowMs = 1_800_000_000_000;
const freshMs = nowMs - 60_000;
const staleMs = nowMs - PENDING_ATTACHMENT_MAX_AGE_MS - 60_000;
for (const filePath of [stalePending, freshPending, scoped, stalePart]) {
NodeFS.writeFileSync(filePath, Buffer.from("x"));
}
const staleMs = Date.now() - PENDING_ATTACHMENT_MAX_AGE_MS - 60_000;
NodeFS.utimesSync(freshPending, freshMs / 1000, freshMs / 1000);
NodeFS.utimesSync(stalePending, staleMs / 1000, staleMs / 1000);
NodeFS.utimesSync(scoped, staleMs / 1000, staleMs / 1000);
NodeFS.utimesSync(stalePart, staleMs / 1000, staleMs / 1000);
const swept = sweepStalePendingAttachments({
attachmentsDir,
nowMs: Date.now(),
nowMs,
});
expect(swept.deleted).toBe(2);
expect(NodeFS.existsSync(stalePending)).toBe(false);
+4 -2
View File
@@ -121,9 +121,10 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) =>
const stats = yield* fileSystem.stat(claimPlan.currentPath).pipe(
Effect.mapError(
() =>
(cause) =>
new OrchestrationDispatchCommandError({
message: `Attachment '${attachment.name}' cannot be sent: attachment not found (removed or expired).`,
cause,
}),
),
);
@@ -136,9 +137,10 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) =>
if (!claimPlan.alreadyScoped) {
yield* fileSystem.rename(claimPlan.currentPath, claimPlan.finalPath).pipe(
Effect.mapError(
() =>
(cause) =>
new OrchestrationDispatchCommandError({
message: `Failed to claim attachment '${attachment.name}' for this thread.`,
cause,
}),
),
);