Skip to content

Commit bd026dd

Browse files
author
Vaclav Berny
committed
fix(pr): reuse idle media slots and isolate gallery failures
1 parent 052a2b1 commit bd026dd

4 files changed

Lines changed: 122 additions & 22 deletions

File tree

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

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ it.effect("rejects unsupported endpoints before invoking Azure CLI", () => {
117117
);
118118
});
119119

120-
it.effect("shares renewed capabilities and serves overflow without retaining transient files", () =>
120+
it.effect("shares renewed capabilities and reuses range downloads under cache pressure", () =>
121121
Effect.gen(function* () {
122122
const fs = yield* FileSystem.FileSystem;
123123
let calls = 0;
@@ -146,26 +146,57 @@ it.effect("shares renewed capabilities and serves overflow without retaining tra
146146
url: `${base}.gif`,
147147
expiresAt: 1000,
148148
};
149-
const first = yield* Effect.scoped(media.get(original));
149+
const first = yield* media.get(original);
150150
const renewedAsset = { ...original, expiresAt: 2000 };
151151
const renewed = yield* Effect.scoped(media.get(renewedAsset));
152152
expect(renewed.path).toBe(first.path);
153153
expect(calls).toBe(1);
154154
for (let index = 1; index < 20; index++) {
155155
const file = yield* Effect.scoped(
156156
Effect.gen(function* () {
157-
const file = yield* media.get({ cwd: "/repo", url: `${base}-${index}.gif` });
157+
const input = { cwd: "/repo", url: `${base}-${index}.mp4` };
158+
const file = yield* media.get(input);
158159
expect(yield* fs.readFileString(file.path)).toBe("GIF89a");
159160
return file;
160161
}),
161162
);
162-
expect(yield* fs.exists(file.path)).toBe(index < 16);
163+
yield* Effect.scoped(
164+
Effect.gen(function* () {
165+
const repeated = yield* media.get({ cwd: "/repo", url: `${base}-${index}.mp4` });
166+
expect(repeated.path).toBe(file.path);
167+
const ranged = HttpServerResponse.toWeb(
168+
yield* assetFileResponse(repeated, "bytes=1-3"),
169+
);
170+
expect(ranged.status).toBe(206);
171+
expect(yield* Effect.promise(() => ranged.text())).toBe("IF8");
172+
}),
173+
);
174+
expect(calls).toBe(index + 1);
175+
expect(yield* fs.exists(file.path)).toBe(true);
176+
const retained = yield* Effect.forEach(paths, (path) => fs.exists(path));
177+
expect(retained.filter(Boolean).length).toBeLessThanOrEqual(16);
163178
expect(yield* fs.readFileString(first.path)).toBe("GIF89a");
164179
}
165180
expect(calls).toBe(20);
166-
}).pipe(Effect.provide(AzureDevOpsMedia.layer.pipe(Layer.provide(cli))));
181+
yield* Effect.scoped(
182+
Effect.gen(function* () {
183+
for (let index = 0; index < 15; index++) {
184+
yield* media.get({ cwd: "/repo", url: `${base}-active-${index}.mp4` });
185+
}
186+
const transient = yield* Effect.scoped(
187+
Effect.gen(function* () {
188+
const file = yield* media.get({ cwd: "/repo", url: `${base}-overflow.mp4` });
189+
expect(yield* fs.readFileString(file.path)).toBe("GIF89a");
190+
return file;
191+
}),
192+
);
193+
expect(yield* fs.exists(transient.path)).toBe(false);
194+
expect(yield* fs.readFileString(first.path)).toBe("GIF89a");
195+
}),
196+
);
197+
}).pipe(Effect.scoped, Effect.provide(AzureDevOpsMedia.layer.pipe(Layer.provide(cli))));
167198
for (const path of paths) expect(yield* fs.exists(path)).toBe(false);
168-
}).pipe(Effect.provide(NodeServices.layer)),
199+
}).pipe(Effect.provide(Layer.merge(NodeServices.layer, NodeHttpPlatform.layer))),
169200
);
170201

171202
it.effect("retries after a failed authenticated download", () => {

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,23 @@ const make = Effect.gen(function* () {
9393
get: Effect.fn("AzureDevOpsMedia.get")(function* (input) {
9494
const key = new MediaKey({ cwd: input.cwd, url: input.url });
9595
return yield* RcMap.get(downloads, key).pipe(
96-
// Overflow belongs to the request scope instead of displacing an active reader.
97-
Effect.catchTags({ ExceededCapacityError: () => download(key) }),
96+
Effect.catchTags({
97+
ExceededCapacityError: () =>
98+
Effect.gen(function* () {
99+
// Reclaim an idle slot; never displace a file held by a response stream.
100+
const idle =
101+
downloads.state._tag === "Open"
102+
? Array.from(downloads.state.map).find(([, entry]) => entry.refCount === 0)
103+
: undefined;
104+
if (idle) {
105+
yield* RcMap.invalidate(downloads, idle[0]);
106+
return yield* RcMap.get(downloads, key).pipe(
107+
Effect.catchTags({ ExceededCapacityError: () => download(key) }),
108+
);
109+
}
110+
return yield* download(key);
111+
}),
112+
}),
98113
Effect.tapError(() => RcMap.invalidate(downloads, key)),
99114
Effect.mapError((cause) =>
100115
isAzureDevOpsMediaFetchError(cause) ? cause : new AzureDevOpsMediaFetchError({ cause }),

‎apps/web/src/components/chat/markdownImageGallery.browser.test.ts‎

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,60 @@ import { resolveMarkdownMediaPreview, wrapExpandedImageIndex } from "./ExpandedI
1010

1111
afterEach(() => document.body.replaceChildren());
1212

13-
it.each([true, false])(
14-
"keeps linked private images authenticated when navigating from a linked=%s image",
15-
async (linked) => {
13+
it("keeps the selected preview and other images available when a neighboring asset fails", async () => {
14+
const base =
15+
"https://dev.azure.com/org/project/_apis/git/repositories/repo/pullRequests/42/attachments";
16+
const environmentId = EnvironmentId.make("remote");
17+
const selected = { src: "https://remote.example/selected.gif", name: "Selected" };
18+
const failed = {
19+
src: "https://remote.example/thumbnail.gif",
20+
name: "Unavailable target",
21+
originalUrl: `${base}/failed.gif`,
22+
};
23+
const available = {
24+
src: `${base}/available.gif`,
25+
name: "Available",
26+
originalUrl: `${base}/available.gif`,
27+
};
28+
const createAssetUrl = vi.fn<Parameters<typeof resolveMarkdownMediaPreview>[0]["createAssetUrl"]>(
29+
async ({ input }) => {
30+
if ("url" in input.resource && input.resource.url.endsWith("failed.gif"))
31+
throw new Error("Asset unavailable");
32+
return AsyncResult.success({
33+
relativeUrl: "/api/assets/signed/available.gif",
34+
expiresAt: 2000,
35+
});
36+
},
37+
);
38+
const resolved = await resolveMarkdownImageGallery(
39+
{ images: [selected, failed, available], index: 0 },
40+
{
41+
cwd: "/repo",
42+
environmentId,
43+
pullRequestMedia: true,
44+
httpBaseUrl: "https://remote.example",
45+
createAssetUrl,
46+
},
47+
);
48+
expect(resolved.index).toBe(0);
49+
expect(resolved.images[0]).toBe(selected);
50+
expect(resolved.images[1]).toBe(failed);
51+
expect(resolved.images[2]?.src).toBe("https://remote.example/api/assets/signed/available.gif");
52+
expect(createAssetUrl).toHaveBeenCalledTimes(2);
53+
});
54+
55+
it.each([
56+
{ linked: true, protocolRelative: false },
57+
{ linked: false, protocolRelative: false },
58+
{ linked: true, protocolRelative: true },
59+
])(
60+
"keeps private gallery images authenticated ($linked, $protocolRelative)",
61+
async ({ linked, protocolRelative }) => {
1662
const base =
1763
"https://dev.azure.com/org/project/_apis/git/repositories/repo/pullRequests/42/attachments";
18-
const urls = [`${base}/first.gif#preview`, `${base}/second.gif#preview`];
64+
const urls = [`${base}/first.gif#preview`, `${base}/second.gif#preview`].map((url) =>
65+
protocolRelative ? url.replace("https:", "") : url,
66+
);
1967
const container = document.createElement("div");
2068
container.setAttribute("data-image-gallery", "");
2169
document.body.append(container);
@@ -64,7 +112,10 @@ it.each([true, false])(
64112
url: `${base}/second.gif`,
65113
},
66114
});
67-
expect(gallery.images[gallery.index]).toBe(selected);
115+
if (!protocolRelative) expect(gallery.images[gallery.index]).toBe(selected);
116+
expect(gallery.images[gallery.index]?.src).toBe(
117+
"https://remote.example/api/assets/signed/first.gif#preview",
118+
);
68119
expect(createAssetUrl).toHaveBeenCalledTimes(2);
69120
},
70121
);

‎apps/web/src/components/chat/markdownImageGallery.ts‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ export async function resolveMarkdownImageGallery(
1717
): Promise<ExpandedImagePreview> {
1818
const images = await Promise.all(
1919
gallery.images.map(async (item) => {
20-
if (
21-
item.actionsSource?.asset ||
22-
!item.originalUrl ||
23-
!input.pullRequestMedia ||
24-
!input.cwd ||
25-
!pullRequestMediaResource(item.originalUrl, input.cwd)
26-
)
20+
if (item.actionsSource?.asset || !item.originalUrl || !input.pullRequestMedia || !input.cwd)
2721
return item;
28-
const resolved = await resolveMarkdownMediaPreview({ ...input, source: item.originalUrl });
29-
return resolved?.images[0] ?? item;
22+
// Authenticated attachment hosts require HTTPS even when the client runs on local HTTP.
23+
const source = item.originalUrl.startsWith("//")
24+
? `https:${item.originalUrl}`
25+
: item.originalUrl;
26+
if (!pullRequestMediaResource(source, input.cwd)) return item;
27+
try {
28+
const resolved = await resolveMarkdownMediaPreview({ ...input, source });
29+
return resolved?.images[0] ?? item;
30+
} catch {
31+
return item;
32+
}
3033
}),
3134
);
3235
return { ...gallery, images };

0 commit comments

Comments
 (0)