Skip to content

Commit b86f2ff

Browse files
committed
fix(chat): preserve question attachments through replay and draft cleanup
1 parent fc41d19 commit b86f2ff

14 files changed

Lines changed: 225 additions & 53 deletions

‎apps/mobile/src/features/threads/QuestionAttachments.tsx‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from "@t3tools/contracts";
1010
import { useAtomValue } from "@effect/atom-react";
1111
import { Alert, View } from "react-native";
12+
import { useEffect, useRef } from "react";
1213
import { ComposerAttachmentButton } from "../../components/ComposerAttachmentButton";
1314
import { ComposerAttachmentStrip } from "../../components/ComposerAttachmentStrip";
1415
import { pickComposerFiles, pickComposerMedia } from "../../lib/composerImages";
@@ -39,6 +40,20 @@ export function QuestionAttachments(props: {
3940
const { selectedThread } = useThreadSelection();
4041
const configs = useServerConfigs();
4142
const drafts = useAtomValue(composerDraftsAtom);
43+
const scopeKey = JSON.stringify([
44+
selectedThread?.environmentId,
45+
selectedThread?.id,
46+
props.requestId,
47+
props.question.id,
48+
]);
49+
const pickerScope = useRef<{ key: string; active: boolean } | null>(null);
50+
useEffect(() => {
51+
const scope = { key: scopeKey, active: true };
52+
pickerScope.current = scope;
53+
return () => {
54+
scope.active = false;
55+
};
56+
}, [scopeKey]);
4257
const append = (
4358
key: string,
4459
attachments: Parameters<typeof appendComposerDraftAttachments>[1],
@@ -59,6 +74,7 @@ export function QuestionAttachments(props: {
5974
});
6075
};
6176
const paste = useNativePaste((uris) => {
77+
const scope = pickerScope.current;
6278
if (
6379
!selectedThread ||
6480
props.disabled ||
@@ -77,9 +93,13 @@ export function QuestionAttachments(props: {
7793
existingCount: appAtomRegistry.get(composerDraftsAtom)[key]?.attachments.length ?? 0,
7894
})
7995
.then(async (images) => {
80-
if ((appAtomRegistry.get(questionAttachmentPreparationAtom)[key] ?? 0) > 0)
81-
append(key, images);
82-
else await releaseUnusedComposerAttachmentFiles(images);
96+
if (
97+
scope?.active &&
98+
(appAtomRegistry.get(questionAttachmentPreparationAtom)[key] ?? 0) > 0
99+
) {
100+
if (append(key, images) > 0)
101+
Alert.alert("Could not paste image", "Too many attachments.");
102+
} else await releaseUnusedComposerAttachmentFiles(images);
83103
})
84104
.catch((error) =>
85105
Alert.alert("Could not paste image", error instanceof Error ? error.message : "Try again."),
@@ -98,6 +118,7 @@ export function QuestionAttachments(props: {
98118
);
99119
const attachments = drafts[key]?.attachments ?? [];
100120
const pick = async (kind: "media" | "files") => {
121+
const scope = pickerScope.current;
101122
changeQuestionAttachmentPreparation(key, 1);
102123
try {
103124
const existingCount = appAtomRegistry.get(composerDraftsAtom)[key]?.attachments.length ?? 0;
@@ -113,7 +134,10 @@ export function QuestionAttachments(props: {
113134
});
114135
const picked = "files" in result ? result.files : result.attachments;
115136
// Resolution on another client clears the reservation while the picker is open.
116-
if ((appAtomRegistry.get(questionAttachmentPreparationAtom)[key] ?? 0) === 0) {
137+
if (
138+
!scope?.active ||
139+
(appAtomRegistry.get(questionAttachmentPreparationAtom)[key] ?? 0) === 0
140+
) {
117141
await releaseUnusedComposerAttachmentFiles(picked);
118142
return;
119143
}

‎apps/mobile/src/state/question-attachments.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function questionAttachmentDraftPrefix(
66
environmentId: EnvironmentId,
77
threadId: ThreadId,
88
): string {
9-
return `${environmentId}:question-${encodeURIComponent(threadId)}-`;
9+
return `${environmentId}:question-${encodeURIComponent(threadId)}:`;
1010
}
1111
export function questionAttachmentDraftKey(
1212
environmentId: EnvironmentId,

‎apps/mobile/src/state/use-selected-thread-requests.ts‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,10 @@ export function useSelectedThreadRequests() {
248248
"",
249249
);
250250
if (userInputResponsesInFlight.current.has(responseKey)) return;
251-
const attachmentsByQuestionId: Record<
251+
const attachmentsByQuestionId = new Map<
252252
string,
253253
import("@t3tools/contracts").UserInputAttachments[string]
254-
> = {};
254+
>();
255255
for (const question of activePendingUserInput.questions) {
256256
const key = questionAttachmentDraftKey(
257257
selectedThreadShell.environmentId,
@@ -275,13 +275,16 @@ export function useSelectedThreadRequests() {
275275
);
276276
return;
277277
}
278-
attachmentsByQuestionId[question.id] = attachments.map((attachment) => ({
279-
type: attachment.type,
280-
id: attachment.uploadedAttachmentId!,
281-
name: attachment.name,
282-
mimeType: attachment.mimeType,
283-
sizeBytes: attachment.sizeBytes,
284-
}));
278+
attachmentsByQuestionId.set(
279+
question.id,
280+
attachments.map((attachment) => ({
281+
type: attachment.type,
282+
id: attachment.uploadedAttachmentId!,
283+
name: attachment.name,
284+
mimeType: attachment.mimeType,
285+
sizeBytes: attachment.sizeBytes,
286+
})),
287+
);
285288
}
286289
userInputResponsesInFlight.current.add(responseKey);
287290
setRespondingUserInputId(activePendingUserInput.requestId);
@@ -291,7 +294,9 @@ export function useSelectedThreadRequests() {
291294
threadId: selectedThreadShell.id,
292295
requestId: activePendingUserInput.requestId,
293296
answers: activePendingUserInputAnswers,
294-
...(Object.keys(attachmentsByQuestionId).length > 0 ? { attachmentsByQuestionId } : {}),
297+
...(attachmentsByQuestionId.size > 0
298+
? { attachmentsByQuestionId: Object.fromEntries(attachmentsByQuestionId) }
299+
: {}),
295300
},
296301
});
297302
userInputResponsesInFlight.current.delete(responseKey);

‎apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,23 @@ it.layer(
12681268
assert.isFalse(yield* exists(removePath));
12691269
assert.isTrue(yield* exists(laterPath));
12701270
assert.isTrue(yield* exists(otherThreadPath));
1271+
1272+
// Replay message and activity history from different cursors, as during a projection rebuild.
1273+
yield* sql`DELETE FROM projection_thread_messages WHERE thread_id = ${threadId}`;
1274+
yield* sql`DELETE FROM projection_thread_activities WHERE thread_id = ${threadId}`;
1275+
yield* sql`UPDATE projection_state SET last_applied_sequence = 0
1276+
WHERE projector IN ('projection.thread-messages', 'projection.thread-activities')`;
1277+
yield* fileSystem.writeFileString(removePath, "remove");
1278+
yield* fileSystem.writeFileString(
1279+
path.join(attachmentsDir, `${answerRemoveId}.txt`),
1280+
"answer",
1281+
);
1282+
yield* projectionPipeline.bootstrap;
1283+
assert.isTrue(yield* exists(keepPath));
1284+
assert.isTrue(yield* exists(laterPath));
1285+
assert.isTrue(yield* exists(path.join(attachmentsDir, `${answerKeepId}.txt`)));
1286+
assert.isFalse(yield* exists(removePath));
1287+
assert.isFalse(yield* exists(path.join(attachmentsDir, `${answerRemoveId}.txt`)));
12711288
}),
12721289
);
12731290
});

‎apps/server/src/orchestration/Layers/ProjectionPipeline.ts‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,17 +1927,25 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
19271927
const runProjectorForEvent = Effect.fn("runProjectorForEvent")(function* (
19281928
projector: ProjectorDefinition,
19291929
event: OrchestrationEvent,
1930+
pendingPrunes: Map<string, OrchestrationEvent>,
19301931
) {
19311932
const attachmentSideEffects: AttachmentSideEffects = {
19321933
deletedThreadIds: new Set<string>(),
19331934
prunedThreadRelativePaths: new Map<string, Set<string>>(),
19341935
};
19351936

19361937
yield* sql.withTransaction(applyProjectorForEvent(projector, event, attachmentSideEffects));
1938+
for (const threadId of attachmentSideEffects.prunedThreadRelativePaths.keys()) {
1939+
pendingPrunes.set(threadId, event);
1940+
}
1941+
attachmentSideEffects.prunedThreadRelativePaths.clear();
19371942
yield* applyAttachmentSideEffects(event, attachmentSideEffects);
19381943
});
19391944

1940-
const bootstrapProjector = (projector: ProjectorDefinition) =>
1945+
const bootstrapProjector = (
1946+
projector: ProjectorDefinition,
1947+
pendingPrunes: Map<string, OrchestrationEvent>,
1948+
) =>
19411949
projectionStateRepository
19421950
.getByProjector({
19431951
projector: projector.name,
@@ -1949,7 +1957,7 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
19491957
Option.isSome(stateRow) ? stateRow.value.lastAppliedSequence : 0,
19501958
Number.MAX_SAFE_INTEGER,
19511959
),
1952-
(event) => runProjectorForEvent(projector, event),
1960+
(event) => runProjectorForEvent(projector, event, pendingPrunes),
19531961
),
19541962
),
19551963
);
@@ -1997,11 +2005,21 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
19972005
yield* cleanup;
19982006
});
19992007

2000-
const bootstrap: OrchestrationProjectionPipelineShape["bootstrap"] = Effect.forEach(
2001-
projectors,
2002-
bootstrapProjector,
2003-
{ concurrency: 1 },
2004-
).pipe(
2008+
const bootstrap: OrchestrationProjectionPipelineShape["bootstrap"] = Effect.gen(function* () {
2009+
const pendingPrunes = new Map<string, OrchestrationEvent>();
2010+
yield* Effect.forEach(
2011+
projectors,
2012+
(projector) => bootstrapProjector(projector, pendingPrunes),
2013+
{ concurrency: 1, discard: true },
2014+
);
2015+
// Both message and activity references must finish replaying before files are pruned.
2016+
for (const [threadId, event] of pendingPrunes) {
2017+
yield* applyAttachmentSideEffects(event, {
2018+
deletedThreadIds: new Set(),
2019+
prunedThreadRelativePaths: new Map([[threadId, new Set()]]),
2020+
});
2021+
}
2022+
}).pipe(
20052023
Effect.provideService(FileSystem.FileSystem, fileSystem),
20062024
Effect.provideService(Path.Path, path),
20072025
Effect.provideService(ServerConfig, serverConfig),

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import * as NodePath from "node:path";
55
import * as NodeServices from "@effect/platform-node/NodeServices";
66
import { describe, expect, it } from "@effect/vitest";
77
import {
8-
type ClientOrchestrationCommand,
8+
ClientOrchestrationCommand,
99
CommandId,
1010
ApprovalRequestId,
1111
MessageId,
1212
ThreadId,
1313
} from "@t3tools/contracts";
1414
import * as Effect from "effect/Effect";
1515
import * as Layer from "effect/Layer";
16+
import * as Schema from "effect/Schema";
1617

1718
import * as ServerConfig from "../config.ts";
1819
import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
@@ -24,6 +25,7 @@ const testLayer = Layer.mergeAll(
2425
).pipe(Layer.provideMerge(NodeServices.layer));
2526

2627
const attachmentUuid = "00000000-0000-4000-8000-0000000000aa";
28+
const isClientCommand = Schema.is(ClientOrchestrationCommand);
2729

2830
function turnStartCommand(input: {
2931
readonly threadId?: string;
@@ -361,6 +363,58 @@ describe("normalizeDispatchCommand attachments", () => {
361363
});
362364

363365
describe("question attachments", () => {
366+
it("requires uploaded metadata for question images, including pasted images", () => {
367+
expect(
368+
isClientCommand({
369+
type: "thread.user-input.respond",
370+
commandId: "answer",
371+
threadId: "thread-1",
372+
requestId: "request",
373+
answers: { q: "" },
374+
createdAt: "2026-08-01T00:00:00.000Z",
375+
attachmentsByQuestionId: {
376+
q: [
377+
{
378+
type: "image",
379+
name: "image.png",
380+
mimeType: "image/png",
381+
sizeBytes: 6,
382+
dataUrl: "data:image/png;base64,cGl4ZWxz",
383+
},
384+
],
385+
},
386+
}),
387+
).toBe(false);
388+
});
389+
390+
it.effect("preserves a __proto__ question key and cleans up its claimed files", () =>
391+
Effect.gen(function* () {
392+
const config = yield* ServerConfig.ServerConfig;
393+
const id = `pending-${attachmentUuid}`;
394+
NodeFS.writeFileSync(NodePath.join(config.attachmentsDir, `${id}.png`), "pixels");
395+
const command: ClientOrchestrationCommand = {
396+
type: "thread.user-input.respond",
397+
commandId: CommandId.make("answer"),
398+
threadId: ThreadId.make("thread-1"),
399+
requestId: ApprovalRequestId.make("request"),
400+
answers: { ["__proto__"]: "" },
401+
createdAt: "2026-08-01T00:00:00.000Z",
402+
attachmentsByQuestionId: {
403+
["__proto__"]: [
404+
{ type: "image", id, name: "image.png", mimeType: "image/png", sizeBytes: 6 },
405+
],
406+
},
407+
};
408+
const normalized = yield* normalizeDispatchCommand(command);
409+
if (normalized.type !== "thread.user-input.respond") throw new Error("Wrong command");
410+
expect(Object.keys(normalized.attachmentsByQuestionId!)).toEqual(["__proto__"]);
411+
const attachment = normalized.attachmentsByQuestionId!["__proto__"]![0]!;
412+
const claimedPath = NodePath.join(config.attachmentsDir, `${attachment.id}.png`);
413+
expect(NodeFS.existsSync(claimedPath)).toBe(true);
414+
yield* cleanupFailedUploadedAttachments(command, normalized);
415+
expect(NodeFS.existsSync(claimedPath)).toBe(false);
416+
}).pipe(Effect.provide(testLayer)),
417+
);
364418
it.effect(
365419
"claims images and files by question, preserves answers, and cleans up failed dispatches",
366420
() =>

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,18 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) =>
278278

279279
if (canonicalCommand.type === "thread.user-input.respond") {
280280
let index = 0;
281-
const attachmentsByQuestionId: Record<string, UserInputAttachments[string]> = {};
282-
for (const [questionId, original] of Object.entries(
283-
canonicalCommand.attachmentsByQuestionId ?? {},
284-
)) {
285-
attachmentsByQuestionId[questionId] = normalizedAttachments.slice(
286-
index,
287-
index + original.length,
288-
) as UserInputAttachments[string];
289-
index += original.length;
290-
}
281+
const attachmentsByQuestionId = Object.fromEntries(
282+
Object.entries(canonicalCommand.attachmentsByQuestionId ?? {}).map(
283+
([questionId, original]) => {
284+
const claimed = normalizedAttachments.slice(
285+
index,
286+
index + original.length,
287+
) as UserInputAttachments[string];
288+
index += original.length;
289+
return [questionId, claimed];
290+
},
291+
),
292+
);
291293
return {
292294
...canonicalCommand,
293295
...(attachments.length > 0 ? { attachmentsByQuestionId } : {}),

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
10731073
});
10741074
const request = userInputActivity;
10751075
const attachments = Object.values(command.attachmentsByQuestionId ?? {}).flat();
1076-
const questionTextById: Record<string, string> = {};
1076+
let questionTextById: Record<string, string> = {};
10771077
if (attachments.length > 0) {
10781078
const payload =
10791079
request?.kind === "user-input.requested"
@@ -1088,8 +1088,9 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
10881088
: "This question is no longer pending.",
10891089
});
10901090
}
1091-
for (const question of payload.value.questions)
1092-
questionTextById[question.id] = question.question;
1091+
questionTextById = Object.fromEntries(
1092+
payload.value.questions.map((question) => [question.id, question.question]),
1093+
);
10931094
for (const questionId of Object.keys(command.attachmentsByQuestionId ?? {})) {
10941095
const question = payload.value.questions.find((question) => question.id === questionId);
10951096
if (!question || question.allowCustomAnswer === false) {

‎apps/server/src/provider/userInputAttachments.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ describe("question answer paths", () => {
3838
expect(answers.other).toBe("No file");
3939
expect(original.q).toEqual(["First", "Second"]);
4040
expect(NodeFS.readFileSync(path, "utf8")).toBe("spec");
41+
const specialKey = yield* appendUserInputAttachmentPaths({
42+
answers: {},
43+
attachmentsDir: config.attachmentsDir,
44+
attachmentsByQuestionId: { ["__proto__"]: [attachment] },
45+
});
46+
expect(Object.keys(specialKey)).toEqual(["__proto__"]);
47+
expect(specialKey["__proto__"]).toContain(path);
4148
}).pipe(Effect.provide(layer)),
4249
);
4350
it.effect("does not send an unavailable attachment path", () =>

0 commit comments

Comments
 (0)