Skip to content

Commit 68d0fc5

Browse files
Merge remote-tracking branch 'origin/main' into fix/issue-26796
2 parents 2160ee1 + eb565e1 commit 68d0fc5

8 files changed

Lines changed: 336 additions & 35 deletions

File tree

‎apps/mobile/src/lib/composerFiles.test.ts‎

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,28 +124,35 @@ describe("composer file attachments", () => {
124124
width: 4032,
125125
height: 3024,
126126
};
127+
type Size = { width: number; height: number };
127128
const native = {
128-
size: { width: 1, height: 1 },
129-
resizes: [] as Array<{ width?: number | null; height?: number | null }>,
129+
size: { width: 1, height: 1 } as Size,
130+
resizes: [] as Size[],
130131
saved: rendered as { uri: string; base64?: string },
132+
encode: null as ((size: Size, format: string) => { uri: string; base64?: string }) | null,
131133
};
132134

133135
beforeEach(() => {
134136
native.size = { width: 1, height: 1 };
135137
native.resizes = [];
136138
native.saved = rendered;
139+
native.encode = null;
137140
mocks.manipulate.mockReset();
138141
mocks.release.mockReset();
139-
mocks.manipulate.mockImplementation(() => {
142+
mocks.manipulate.mockImplementation((source: string | Size) => {
143+
let size = typeof source === "string" ? native.size : source;
140144
const context = {
141-
resize(size: { width?: number | null; height?: number | null }) {
142-
native.resizes.push(size);
145+
resize(next: Size) {
146+
native.resizes.push(next);
147+
size = next;
143148
return context;
144149
},
145150
renderAsync: async () => ({
146-
...native.size,
151+
width: size.width,
152+
height: size.height,
147153
release: mocks.release,
148-
saveAsync: async () => native.saved,
154+
saveAsync: async (options: { format: string }) =>
155+
native.encode?.(size, options.format) ?? native.saved,
149156
}),
150157
};
151158
return context;
@@ -210,6 +217,45 @@ describe("composer file attachments", () => {
210217
]);
211218
});
212219

220+
it("shrinks a fitted PNG further when re-encoding it exceeds the image limit", async () => {
221+
const header = Buffer.alloc(24);
222+
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]).copy(header);
223+
header.write("IHDR", 12);
224+
header.writeUInt32BE(1668, 16);
225+
header.writeUInt32BE(2388, 20);
226+
native.size = { width: 1668, height: 2388 };
227+
// Native PNG encoding of the 2000px resample is larger than the 7 MB original.
228+
native.encode = (size, format) => ({
229+
uri: `file:///cache/fitted-${size.width}x${size.height}.${format}`,
230+
base64: Buffer.alloc(size.width * size.height * 4).toString("base64"),
231+
});
232+
mocks.size.mockReturnValue(7 * 1024 * 1024);
233+
mocks.readBase64.mockResolvedValue(header.toString("base64"));
234+
mocks.pickMedia.mockResolvedValue({
235+
canceled: false,
236+
assets: [{ ...photo, fileName: "IMG_9280.png", mimeType: "image/png" }],
237+
});
238+
239+
const result = await pickComposerImages({ existingCount: 0 });
240+
241+
expect(result.error).toBeNull();
242+
const [image] = result.images;
243+
const last = native.resizes.at(-1)!;
244+
expect(native.resizes[0]).toEqual({ width: 1397, height: 2000 });
245+
expect(Math.max(last.width, last.height)).toBeLessThan(2000);
246+
expect(image).toEqual(
247+
expect.objectContaining({
248+
name: "IMG_9280.png",
249+
mimeType: "image/png",
250+
sizeBytes: last.width * last.height * 4,
251+
previewUri: `file:///cache/fitted-${last.width}x${last.height}.png`,
252+
}),
253+
);
254+
expect(image!.sizeBytes).toBeLessThanOrEqual(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES);
255+
expect(mocks.delete).toHaveBeenCalledWith("file:///cache/fitted-1397x2000.png");
256+
expect(mocks.delete).not.toHaveBeenCalledWith(image!.previewUri);
257+
});
258+
213259
it("measures the picker file instead of trusting the reported size", async () => {
214260
mocks.pickMedia.mockResolvedValue({
215261
canceled: false,

‎apps/mobile/src/lib/composerImageFit.ts‎

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PROVIDER_SEND_TURN_MAX_IMAGE_BYTES } from "@t3tools/contracts";
12
import {
23
downscalableImageMimeType,
34
fitImageAttachmentSize,
@@ -6,8 +7,24 @@ import {
67
IMAGE_DIMENSIONS_HEADER_BYTES,
78
readImageDimensions,
89
} from "@t3tools/shared/imageDimensions";
10+
import { estimateBase64ByteSize } from "./base64";
911

1012
const PHOTO_JPEG_QUALITY = 0.85;
13+
const MAX_BYTE_LIMIT_SHRINKS = 3;
14+
15+
function shrinkToByteLimit(
16+
size: { readonly width: number; readonly height: number },
17+
bytes: number,
18+
): { width: number; height: number } | null {
19+
if (bytes <= PROVIDER_SEND_TURN_MAX_IMAGE_BYTES) return null;
20+
// Encoded size tracks pixel count, so scale each edge by the square root with some headroom.
21+
const scale = Math.sqrt(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES / bytes) * 0.9;
22+
const next = {
23+
width: Math.max(1, Math.floor(size.width * scale)),
24+
height: Math.max(1, Math.floor(size.height * scale)),
25+
};
26+
return next.width < size.width || next.height < size.height ? next : null;
27+
}
1128

1229
export async function renderPhoto(
1330
uri: string,
@@ -16,21 +33,35 @@ export async function renderPhoto(
1633
const { ImageManipulator, SaveFormat } = await import("expo-image-manipulator");
1734
let image = await ImageManipulator.manipulate(uri).renderAsync();
1835
try {
19-
const fittedSize = fitImageAttachmentSize(image);
20-
if (fittedSize !== null) {
21-
const resized = await ImageManipulator.manipulate(image).resize(fittedSize).renderAsync();
22-
image.release();
23-
image = resized;
24-
}
25-
const saved = await image.saveAsync({
26-
format: format === "png" ? SaveFormat.PNG : SaveFormat.JPEG,
27-
compress: PHOTO_JPEG_QUALITY,
28-
base64: true,
29-
});
30-
if (!saved.base64) {
31-
throw new Error("The rendered photo has no bytes.");
36+
let size = fitImageAttachmentSize(image);
37+
for (let shrinks = 0; ; shrinks += 1) {
38+
if (size !== null) {
39+
const resized = await ImageManipulator.manipulate(image).resize(size).renderAsync();
40+
image.release();
41+
image = resized;
42+
}
43+
const saved = await image.saveAsync({
44+
format: format === "png" ? SaveFormat.PNG : SaveFormat.JPEG,
45+
compress: PHOTO_JPEG_QUALITY,
46+
base64: true,
47+
});
48+
if (!saved.base64) {
49+
throw new Error("The rendered photo has no bytes.");
50+
}
51+
// PNG ignores compress, so a resampled PNG can encode larger than its original.
52+
size =
53+
shrinks < MAX_BYTE_LIMIT_SHRINKS
54+
? shrinkToByteLimit(image, estimateBase64ByteSize(saved.base64))
55+
: null;
56+
if (size === null) return { base64: saved.base64, uri: saved.uri };
57+
try {
58+
const { File } = await import("expo-file-system");
59+
const discarded = new File(saved.uri);
60+
if (discarded.exists) discarded.delete();
61+
} catch (error) {
62+
console.warn("[composer-images] could not remove discarded render", error);
63+
}
3264
}
33-
return { base64: saved.base64, uri: saved.uri };
3465
} finally {
3566
image.release();
3667
}

‎apps/server/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"@t3tools/source-control-github": "workspace:*",
5959
"@t3tools/source-control-gitlab": "workspace:*",
6060
"character-entities": "2.0.2",
61+
"diff": "8.0.3",
6162
"effect": "catalog:",
6263
"jose": "catalog:",
6364
"node-pty": "^1.2.0-beta.15",

‎apps/server/src/guest/GuestShareService.test.ts‎

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,6 @@ it.effect("serves no render for an item outside the shared thread", () =>
411411
}).pipe(Effect.provide(layerTest)),
412412
);
413413

414-
/** A message from the owner, which opens a run for the command below. */
415414
const ownerRun = Effect.gen(function* () {
416415
const orchestrator = yield* Orchestrator.OrchestratorV2;
417416
yield* orchestrator.dispatch({
@@ -430,12 +429,17 @@ const ownerRun = Effect.gen(function* () {
430429
return runs[0]!.id;
431430
});
432431

433-
const writeCommand = (
432+
const writeItem = (
434433
id: string,
435-
fields: Pick<
436-
Extract<OrchestrationV2TurnItem, { type: "command_execution" }>,
437-
"status" | "output"
438-
>,
434+
fields:
435+
| Pick<
436+
Extract<OrchestrationV2TurnItem, { type: "command_execution" }>,
437+
"type" | "status" | "input" | "output"
438+
>
439+
| Pick<
440+
Extract<OrchestrationV2TurnItem, { type: "file_change" }>,
441+
"type" | "status" | "fileName" | "diffStr" | "oldStr" | "newStr"
442+
>,
439443
) =>
440444
Effect.gen(function* () {
441445
const runId = yield* ownerRun;
@@ -464,8 +468,6 @@ const writeCommand = (
464468
startedAt: now,
465469
completedAt: fields.status === "running" ? null : now,
466470
updatedAt: now,
467-
type: "command_execution",
468-
input: "ls /work/src",
469471
...fields,
470472
},
471473
},
@@ -474,17 +476,40 @@ const writeCommand = (
474476
return TurnItemId.make(id);
475477
});
476478

477-
it.effect("gives the guest a command's detail and output, as the owner's row loads them", () =>
479+
it.effect("gives the guest a command's detail and output, but no file diffs", () =>
478480
Effect.gen(function* () {
479481
const { guestShares, credential } = yield* shareThread;
480482
const { token: session } = yield* guestShares.redeem({ credential, name: "Sam" });
481-
const command = yield* writeCommand("item:command", {
483+
const command = yield* writeItem("item:command", {
484+
type: "command_execution",
482485
status: "completed",
486+
input: "ls /work/src",
483487
output: "/work/src/auth.ts\n",
484488
});
485489

486490
const detail = (yield* guestShares.item(session, command)).item;
487491
assert.deepInclude(detail, { input: "ls /work/src", output: "/work/src/auth.ts\n" });
492+
const edit = yield* writeItem("item:edit", {
493+
type: "file_change",
494+
status: "completed",
495+
fileName: "src/auth.ts",
496+
diffStr: "-const token = 1;\n+const token = 2;\n",
497+
});
498+
const write = yield* writeItem("item:write", {
499+
type: "file_change",
500+
status: "completed",
501+
fileName: "src/config.ts",
502+
oldStr: "",
503+
newStr: "export const key = 1;\n",
504+
});
505+
for (const [id, fileName] of [
506+
[edit, "src/auth.ts"],
507+
[write, "src/config.ts"],
508+
] as const) {
509+
const change = (yield* guestShares.item(session, id)).item;
510+
assert.deepInclude(change, { type: "file_change", fileName });
511+
assert.doesNotHaveAnyKeys(change, ["diffStr", "oldStr", "newStr"]);
512+
}
488513
const frames = Array.from(
489514
yield* Stream.runCollect(yield* guestShares.commandOutput(session, command)),
490515
);
@@ -508,7 +533,11 @@ it.effect("ends a running command's output once the owner stops sharing", () =>
508533
Effect.gen(function* () {
509534
const { orchestrator, guestShares, credential } = yield* shareThread;
510535
const { token: session } = yield* guestShares.redeem({ credential, name: "Sam" });
511-
const command = yield* writeCommand("item:running", { status: "running" });
536+
const command = yield* writeItem("item:running", {
537+
type: "command_execution",
538+
status: "running",
539+
input: "ls /work/src",
540+
});
512541
const collecting = yield* Stream.runCollect(
513542
yield* guestShares.commandOutput(session, command),
514543
).pipe(Effect.forkChild);

‎apps/server/src/guest/GuestShareService.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import { buildBoundedThreadStreamSnapshot } from "../orchestration-v2/ThreadStre
5757
import {
5858
projectDomainEventForWire,
5959
projectTurnItemForDetail,
60+
projectTurnItemForWire,
6061
} from "../orchestration-v2/WireProjection.ts";
6162
import * as ProviderRegistry from "../provider/ProviderRegistry.ts";
6263
import * as ServerSettings from "../serverSettings.ts";
@@ -179,7 +180,7 @@ export class GuestShareService extends Context.Service<
179180
session: string,
180181
itemId: string,
181182
) => Effect.Effect<{ readonly path: string; readonly title: string }, GuestShareAccessError>;
182-
/** One item of the shared thread, as the owner's expanded row loads it. */
183+
/** One item of the shared thread, as the owner's expanded row loads it, without file diffs. */
183184
readonly item: (
184185
session: string,
185186
itemId: string,
@@ -658,7 +659,10 @@ const make = Effect.gen(function* () {
658659

659660
const item = Effect.fn("GuestShareService.item")(function* (session: string, itemId: string) {
660661
const { item } = yield* threadItem(session, itemId);
661-
return { item: projectTurnItemForDetail(item) };
662+
return {
663+
item:
664+
item.type === "file_change" ? projectTurnItemForWire(item) : projectTurnItemForDetail(item),
665+
};
662666
});
663667

664668
const commandOutput = Effect.fn("GuestShareService.commandOutput")(function* (

0 commit comments

Comments
 (0)