Skip to content

Commit 4fec23e

Browse files
ScottN-PVclaude
andcommitted
fix(web): name the remote host and checked path when media is not found
Media chips resolve on the environment's host, so on a remote environment a path that exists on this device can still fail with "Media file was not found." without saying where the lookup ran. The not-found error now carries the absolute path the host checked (after expanding ~ and resolving against the workspace), and when the environment is not on this device the toast names it and that path. Servers that predate the field fall back to the chip's own path; this device keeps the old message. The desktop app retries an absolute path on this device when the remote host misses it. If that retry fails too, the remote host's error is now the one reported, so the toast never pairs this device's failure with the remote host's name. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 09c5e7a commit 4fec23e

10 files changed

Lines changed: 209 additions & 16 deletions

File tree

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,41 @@ describe("AssetAccess", () => {
386386
}).pipe(Effect.provide(layerTest)),
387387
);
388388

389+
it.effect.each([
390+
{ form: "absolute", requestedPath: undefined, checked: ["absolute.mp4"] },
391+
{
392+
form: "workspace-relative",
393+
requestedPath: "clips/relative.mp4",
394+
checked: ["workspace", "clips", "relative.mp4"],
395+
},
396+
{
397+
form: "home-relative",
398+
requestedPath: "~/Downloads/home.mp4",
399+
checked: ["home", "Downloads", "home.mp4"],
400+
},
401+
])("reports the absolute path it checked for a missing $form media path", (scenario) =>
402+
Effect.gen(function* () {
403+
const fs = yield* FileSystem.FileSystem;
404+
const path = yield* Path.Path;
405+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-media-missing-" });
406+
const root = path.join(directory, "workspace");
407+
yield* fs.makeDirectory(root);
408+
const checkedPath = path.join(directory, ...scenario.checked);
409+
const error = yield* issueAssetUrl({
410+
resource: {
411+
_tag: "media-file",
412+
threadId: ThreadId.make("thread-1"),
413+
path: scenario.requestedPath ?? checkedPath,
414+
},
415+
workspaceRoot: root,
416+
}).pipe(
417+
Effect.flip,
418+
Effect.provideService(HostProcess.HomeDirectory, path.join(directory, "home")),
419+
);
420+
expect(error).toMatchObject({ _tag: "AssetWorkspaceAssetNotFoundError", checkedPath });
421+
}).pipe(Effect.provide(layerTest)),
422+
);
423+
389424
it.effect.skipIf(!symlinksSupported)(
390425
"rejects non-previewable files, disguised targets, and directories",
391426
() =>

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,10 @@ const finalizeAbsoluteMediaFileAsset = Effect.fn("AssetAccess.finalizeAbsoluteMe
359359
),
360360
);
361361
if (!canonicalFile) {
362-
return yield* new AssetWorkspaceAssetNotFoundError({ resource: input.resource });
362+
return yield* new AssetWorkspaceAssetNotFoundError({
363+
resource: input.resource,
364+
checkedPath: input.requestedPath,
365+
});
363366
}
364367
if (hostPreviewMimeTypeFromExtension(path.extname(canonicalFile)) === null) {
365368
return yield* new AssetPreviewTypeValidationError({ resource: input.resource });
@@ -385,7 +388,10 @@ const finalizeAbsoluteMediaFileAsset = Effect.fn("AssetAccess.finalizeAbsoluteMe
385388
),
386389
);
387390
if (!opened) {
388-
return yield* new AssetWorkspaceAssetNotFoundError({ resource: input.resource });
391+
return yield* new AssetWorkspaceAssetNotFoundError({
392+
resource: input.resource,
393+
checkedPath: input.requestedPath,
394+
});
389395
}
390396
return {
391397
claims: {

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ import {
114114
} from "@t3tools/shared/codexMarkdownDirectives";
115115
import { renderSkillInlineMarkdownChildren } from "./chat/SkillInlineText";
116116
import {
117+
describeMediaUnavailable,
117118
resolveMarkdownMediaPreview,
118119
type ExpandedImagePreview,
119120
} from "./chat/ExpandedImagePreview";
@@ -184,6 +185,7 @@ import { readThreadShell, useProjects } from "../state/entities";
184185
import { serverEnvironment } from "../state/server";
185186
import { shellEnvironment } from "../state/shell";
186187
import { assetEnvironment } from "../state/assets";
188+
import { readEnvironmentLabel } from "../state/environments";
187189
import { readEnvironmentScope, usePreparedConnection, useEnvironmentScope } from "../state/session";
188190
import { previewEnvironment } from "../state/preview";
189191
import { useAtomCommand } from "../state/use-atom-command";
@@ -2277,6 +2279,8 @@ function useChatMarkdownState({
22772279
canOperateHost &&
22782280
canUseMarkdownFileShellActions(environmentId, remoteOpen.state.mode, remoteOpen.isResolved);
22792281
const preparedConnection = usePreparedConnection(environmentId);
2282+
const remoteEnvironmentId =
2283+
remoteOpen.isResolved && remoteOpen.state.mode !== "local-exec" ? environmentId : null;
22802284
const openMarkdownMedia = useCallback(
22812285
(source: string, resolvedFilePath?: string, clickedImage?: HTMLImageElement | null) => {
22822286
const requestId = ++mediaRequestId.current;
@@ -2308,16 +2312,16 @@ function useChatMarkdownState({
23082312
stackedThreadToast({
23092313
type: "error",
23102314
title: "Media unavailable",
2311-
description:
2312-
error instanceof Error
2313-
? error.message
2314-
: "The file could not be loaded. It may have been moved or deleted.",
2315+
description: describeMediaUnavailable(
2316+
error,
2317+
remoteEnvironmentId === null ? null : readEnvironmentLabel(remoteEnvironmentId),
2318+
),
23152319
}),
23162320
);
23172321
},
23182322
);
23192323
},
2320-
[createAssetUrl, cwd, expandMedia, preparedConnection, threadRef],
2324+
[createAssetUrl, cwd, expandMedia, preparedConnection, remoteEnvironmentId, threadRef],
23212325
);
23222326
const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId));
23232327
const projects = useProjects();

‎apps/web/src/components/chat/ExpandedImagePreview.test.ts‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2-
import { EnvironmentId } from "@t3tools/contracts";
2+
import {
3+
AssetPreviewTypeValidationError,
4+
AssetWorkspaceAssetNotFoundError,
5+
EnvironmentId,
6+
ThreadId,
7+
} from "@t3tools/contracts";
38

49
import type { ComposerFileAttachment } from "../../composerDraftStore";
510
import {
611
wrapExpandedImageIndex,
712
attachVideoThumbnail,
813
buildAttachmentVideoPreview,
914
buildExpandedImagePreview,
15+
describeMediaUnavailable,
1016
resolveMarkdownMediaPreview,
1117
} from "./ExpandedImagePreview";
1218

@@ -44,6 +50,47 @@ describe("resolveMarkdownMediaPreview", () => {
4450
);
4551
});
4652

53+
describe("describeMediaUnavailable", () => {
54+
const resource = {
55+
_tag: "media-file" as const,
56+
threadId: ThreadId.make("thread-1"),
57+
path: "~/Downloads/clip.mp4",
58+
};
59+
60+
it("names the remote environment and the path its host checked", () => {
61+
const error = new AssetWorkspaceAssetNotFoundError({
62+
resource,
63+
checkedPath: "/Users/graham/Downloads/clip.mp4",
64+
});
65+
66+
expect(describeMediaUnavailable(error, "mac-studio")).toBe(
67+
"The file was not found on mac-studio at /Users/graham/Downloads/clip.mp4.",
68+
);
69+
});
70+
71+
it("falls back to the chip's path when an older server sends no checked path", () => {
72+
const error = new AssetWorkspaceAssetNotFoundError({ resource });
73+
74+
expect(describeMediaUnavailable(error, "mac-studio")).toBe(
75+
"The file was not found on mac-studio at ~/Downloads/clip.mp4.",
76+
);
77+
});
78+
79+
it("keeps the server's message on this device and for other failures", () => {
80+
const notFound = new AssetWorkspaceAssetNotFoundError({
81+
resource,
82+
checkedPath: "/home/me/Downloads/clip.mp4",
83+
});
84+
const unsupported = new AssetPreviewTypeValidationError({ resource });
85+
86+
expect(describeMediaUnavailable(notFound, null)).toBe("Media file was not found.");
87+
expect(describeMediaUnavailable(unsupported, "mac-studio")).toBe(unsupported.message);
88+
expect(describeMediaUnavailable("offline", "mac-studio")).toBe(
89+
"The file could not be loaded. It may have been moved or deleted.",
90+
);
91+
});
92+
});
93+
4794
describe("buildExpandedImagePreview", () => {
4895
it("builds a signed-asset preview for a persisted video attachment", () => {
4996
const preview = buildAttachmentVideoPreview(EnvironmentId.make("environment-1"), {

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { SnapShotSource } from "@t3tools/contracts";
1+
import { AssetWorkspaceAssetNotFoundError, type SnapShotSource } from "@t3tools/contracts";
22

33
import type { ComposerFileAttachment } from "../../composerDraftStore";
44
import { type ChatFileAttachment, type ChatImageAttachment, isVideoAttachment } from "../../types";
@@ -9,6 +9,7 @@ import type {
99
ScopedThreadRef,
1010
} from "@t3tools/contracts";
1111
import { videoMimeType } from "@t3tools/shared/video";
12+
import * as Schema from "effect/Schema";
1213
import { resolveMediaSource } from "@t3tools/client-runtime/media-source";
1314
import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets";
1415
import {
@@ -104,6 +105,31 @@ export async function resolveMarkdownMediaPreview(input: {
104105
};
105106
}
106107

108+
const isAssetNotFoundError = Schema.is(AssetWorkspaceAssetNotFoundError);
109+
110+
/**
111+
* Text for the "Media unavailable" toast. Media resolves on the environment's host, so when that
112+
* host is not this device a missing file names it and the path it checked.
113+
*/
114+
export function describeMediaUnavailable(
115+
error: unknown,
116+
remoteEnvironmentLabel: string | null,
117+
): string {
118+
if (!(error instanceof Error)) {
119+
return "The file could not be loaded. It may have been moved or deleted.";
120+
}
121+
if (
122+
remoteEnvironmentLabel !== null &&
123+
isAssetNotFoundError(error) &&
124+
error.resource._tag === "media-file"
125+
) {
126+
// Older servers do not report the checked path; the chip's own path is the next best.
127+
const path = error.checkedPath ?? error.resource.path;
128+
return `The file was not found on ${remoteEnvironmentLabel} at ${path}.`;
129+
}
130+
return error.message;
131+
}
132+
107133
export function buildAttachmentVideoAsset(
108134
environmentId: EnvironmentId,
109135
attachment: ChatFileAttachment,

‎apps/web/src/state/environments.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as Option from "effect/Option";
1010
import { useMemo } from "react";
1111

1212
import { environmentCatalog } from "../connection/catalog";
13+
import { appAtomRegistry } from "../rpc/atomRegistry";
1314
import {
1415
environmentPresentations,
1516
environmentSummaries,
@@ -77,6 +78,14 @@ export function useEnvironment(
7778
);
7879
}
7980

81+
/** A one-off read for event handlers that need the label only when something fails. */
82+
export function readEnvironmentLabel(environmentId: EnvironmentId): string | null {
83+
return (
84+
appAtomRegistry.get(environmentPresentations.presentationAtom(environmentId))?.entry.target
85+
.label ?? null
86+
);
87+
}
88+
8089
export function usePrimaryEnvironment(): EnvironmentPresentation | null {
8190
return useEnvironment(usePrimaryEnvironmentId());
8291
}

‎packages/client-runtime/src/state/assets.test.ts‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ describe("createAssetEnvironmentAtoms", () => {
6666
{ name: "windows path", path: "C:\\Users\\demo\\clip.mp4", fallback: true },
6767
{ name: "inspection failure", path: "/tmp/clip.mp4", error: "inspection", fallback: true },
6868
{ name: "foreign thread", path: "/tmp/clip.mp4", error: "context", fallback: true },
69+
{ name: "missing on both hosts", path: "/tmp/clip.mp4", localFails: true },
70+
{
71+
name: "unreadable remotely, missing here",
72+
path: "/tmp/clip.mp4",
73+
error: "inspection",
74+
localFails: true,
75+
},
76+
{
77+
name: "a defect in the local retry",
78+
path: "/tmp/clip.mp4",
79+
localFails: true,
80+
localDefect: true,
81+
},
6982
{ name: "remote success", path: "/tmp/clip.mp4", success: true },
7083
{ name: "relative path", path: "clip.mp4" },
7184
{ name: "no primary", path: "/tmp/clip.mp4", primary: "none" },
@@ -92,7 +105,7 @@ describe("createAssetEnvironmentAtoms", () => {
92105
? new AssetWorkspaceAssetInspectionError({ resource, cause: new Error("unreadable") })
93106
: scenario.error === "context"
94107
? new AssetWorkspaceContextNotFoundError({ resource })
95-
: new AssetWorkspaceAssetNotFoundError({ resource });
108+
: new AssetWorkspaceAssetNotFoundError({ resource, checkedPath: "/tmp/clip.mp4" });
96109
const calls: EnvironmentId[] = [];
97110
const supervisors = new Map<
98111
EnvironmentId,
@@ -102,6 +115,14 @@ describe("createAssetEnvironmentAtoms", () => {
102115
const client = {
103116
[WS_METHODS.assetsCreateUrl]: () => {
104117
calls.push(environmentId);
118+
if (environmentId === localId && scenario.localFails) {
119+
const localError = Effect.fail(
120+
new AssetWorkspaceAssetNotFoundError({ resource, checkedPath: "/local/clip.mp4" }),
121+
);
122+
return scenario.localDefect
123+
? localError.pipe(Effect.ensuring(Effect.die("cleanup failed")))
124+
: localError;
125+
}
105126
return environmentId === remoteId && !scenario.success
106127
? Effect.fail(error)
107128
: Effect.succeed({
@@ -167,10 +188,16 @@ describe("createAssetEnvironmentAtoms", () => {
167188
? "https://local.test/api/assets/local/media"
168189
: "/api/assets/remote/media",
169190
);
191+
} else if (scenario.localDefect) {
192+
const cause = yield* Effect.sandbox(result).pipe(Effect.flip);
193+
expect(Cause.findErrorOption(cause)).toEqual(Option.some(error));
194+
expect(Cause.hasDies(cause)).toBe(true);
170195
} else {
171196
expect(yield* Effect.flip(result)).toEqual(error);
172197
}
173-
expect(calls).toEqual(scenario.fallback ? [remoteId, localId] : [remoteId]);
198+
expect(calls).toEqual(
199+
scenario.fallback || scenario.localFails ? [remoteId, localId] : [remoteId],
200+
);
174201
if (scenario.primary === "reconnecting") {
175202
registry.set(localEnvironment, localTarget);
176203
expect((yield* result).relativeUrl).toBe("https://local.test/api/assets/local/media");

‎packages/client-runtime/src/state/assets.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
getProjectFaviconResourceKey,
1414
isProjectFaviconFallbackUrl,
1515
} from "@t3tools/shared/projectFavicon";
16+
import * as Cause from "effect/Cause";
1617
import * as Effect from "effect/Effect";
1718
import * as Option from "effect/Option";
1819
import * as Result from "effect/Result";
@@ -156,10 +157,10 @@ export function createAssetEnvironmentAtoms<R, E>(
156157
)
157158
return yield* error;
158159
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
159-
const asset = yield* registry.run(
160-
local.environmentId,
161-
request(WS_METHODS.assetsCreateUrl, input),
162-
);
160+
// When the local retry fails too, report the thread's host's failure; defects stay.
161+
const asset = yield* registry
162+
.run(local.environmentId, request(WS_METHODS.assetsCreateUrl, input))
163+
.pipe(Effect.catchCause((cause) => Effect.failCause(Cause.map(cause, () => error))));
163164
return { ...asset, relativeUrl: new URL(asset.relativeUrl, local.httpBaseUrl).href };
164165
});
165166
const createUrl = createEnvironmentQueryAtomFamily(runtime, {

‎packages/contracts/src/assets.test.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import * as Schema from "effect/Schema";
22
import { describe, expect, it } from "vite-plus/test";
33

4-
import { AttachmentCreateUploadUrlInput } from "./assets.ts";
4+
import {
5+
AssetAccessError,
6+
AssetWorkspaceAssetNotFoundError,
7+
AttachmentCreateUploadUrlInput,
8+
} from "./assets.ts";
9+
import { ThreadId } from "./baseSchemas.ts";
510
import {
611
PROVIDER_SEND_TURN_MAX_FILE_BYTES,
712
PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
813
} from "./chatAttachment.ts";
914

1015
const isUploadInput = Schema.is(AttachmentCreateUploadUrlInput);
16+
const encodeAccessError = Schema.encodeUnknownSync(AssetAccessError);
17+
const decodeAccessError = Schema.decodeUnknownSync(AssetAccessError);
1118

1219
const uploadInput = {
1320
name: "screenshot.png",
@@ -58,3 +65,31 @@ describe("AttachmentCreateUploadUrlInput", () => {
5865
).toBe(false);
5966
});
6067
});
68+
69+
describe("AssetWorkspaceAssetNotFoundError", () => {
70+
const resource = {
71+
_tag: "media-file" as const,
72+
threadId: ThreadId.make("thread-1"),
73+
path: "~/Downloads/clip.mp4",
74+
};
75+
76+
it("carries the checked path across the wire", () => {
77+
const encoded = encodeAccessError(
78+
new AssetWorkspaceAssetNotFoundError({
79+
resource,
80+
checkedPath: "/Users/graham/Downloads/clip.mp4",
81+
}),
82+
);
83+
const error = decodeAccessError(JSON.parse(JSON.stringify(encoded)));
84+
85+
expect(error).toBeInstanceOf(AssetWorkspaceAssetNotFoundError);
86+
expect(error).toMatchObject({ checkedPath: "/Users/graham/Downloads/clip.mp4" });
87+
});
88+
89+
it("decodes an error from a server that sends no checked path", () => {
90+
const error = decodeAccessError({ _tag: "AssetWorkspaceAssetNotFoundError", resource });
91+
92+
expect(error).toBeInstanceOf(AssetWorkspaceAssetNotFoundError);
93+
expect(error).not.toHaveProperty("checkedPath");
94+
});
95+
});

‎packages/contracts/src/assets.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ export class AssetWorkspaceAssetNotFoundError extends Schema.TaggedError<AssetWo
207207
"AssetWorkspaceAssetNotFoundError",
208208
{
209209
resource: AssetResource,
210+
/** The absolute path the host checked for a media file, after expanding
211+
`~` and resolving against the workspace. Absent from older servers. */
212+
checkedPath: Schema.optionalKey(Schema.String),
210213
},
211214
) {
212215
override get message(): string {

0 commit comments

Comments
 (0)