Repository navigation
feat(attachments): attachments go where the workspace lives (whole attachments plan; SUPER-3014, SUPER-2658, SUPER-2874) #9064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
harshithmullapudi
merged 21 commits into
main
from
fix/super-3014-local-chat-attachments
Nov 2, 2026
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
30d8796
fix(desktop,host-service): send attachments through storage, not host…
saddlepaddle 10a2b74
fix(api): scope attachment.resolve to visible cloud workspaces and re…
harshithmullapudi 05165d4
Merge main into #8704 to resolve the cloud-workspace.ts import conflict
harshithmullapudi 6efd15d
fix(desktop): copy local chat attachments instead of applying remote …
harshithmullapudi 9130771
Merge #8704 (send attachments through storage) into SUPER-3014 fix
harshithmullapudi d2ba203
style: format attachments test
harshithmullapudi 32cfbb7
fix(desktop): keep every local-workspace attachment on the machine
harshithmullapudi f745e33
feat(desktop): show attachments by the name the user attached, as fil…
harshithmullapudi 30bf515
fix(desktop): keep attachments and say why when a chat send fails
harshithmullapudi 71b7e89
Merge origin/main into fix/super-3014-local-chat-attachments
harshithmullapudi ae52b5f
Merge origin/main into fix/super-3014-local-chat-attachments
harshithmullapudi 9c81d7e
feat(host-service): sweep the attachment store by last use, and keep …
harshithmullapudi f68cfaa
feat(chat-runtime,host-service): every adapter hands the agent its at…
harshithmullapudi d091944
fix(chat-runtime): drop an empty text block from a Claude prompt with…
harshithmullapudi 233498e
feat(shared): hostReadsAttachmentParts, the client side of the parts …
harshithmullapudi cef20a0
feat(desktop): attachments go where the workspace lives, as chat parts
harshithmullapudi 5a646e8
docs(sandbox): terminal paste and chat attachments go where the works…
harshithmullapudi f32821b
feat(mobile): attachments go where the workspace lives; render cloud …
harshithmullapudi cb1e6f2
docs(sandbox): a box's host-service is as new as its last boot
harshithmullapudi 635f21d
fix(attachments): finish folding the attachments plan into #9064
harshithmullapudi 4444ea9
Merge remote-tracking branch 'origin/main' into consolidate/attachmen…
harshithmullapudi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
apps/desktop/src/lib/trpc/routers/attachments/attachments.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import { afterEach, describe, expect, it } from "bun:test"; | ||
| import { | ||
| chmodSync, | ||
| existsSync, | ||
| mkdirSync, | ||
| mkdtempSync, | ||
| readFileSync, | ||
| rmSync, | ||
| writeFileSync, | ||
| } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { WORKSPACE_ATTACHMENTS_DIR } from "@superset/shared/workspace-attachments"; | ||
| import { writeAttachmentsIntoWorktree } from "./attachments"; | ||
|
|
||
| const roots: string[] = []; | ||
|
|
||
| function setup() { | ||
| const root = mkdtempSync(join(tmpdir(), "attachments-")); | ||
| roots.push(root); | ||
| const source = join(root, "build.dmg"); | ||
| writeFileSync(source, "first"); | ||
| const worktreePath = join(root, "worktree"); | ||
| mkdirSync(worktreePath); | ||
| return { | ||
| root, | ||
| source, | ||
| worktreePath, | ||
| directory: join(worktreePath, WORKSPACE_ATTACHMENTS_DIR), | ||
| }; | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| for (const root of roots.splice(0)) { | ||
| rmSync(root, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| describe("writeAttachmentsIntoWorktree", () => { | ||
| it("copies paths and writes bytes in order, into a self-ignoring dir", async () => { | ||
| const { source, worktreePath, directory } = setup(); | ||
|
|
||
| const paths = await writeAttachmentsIntoWorktree({ | ||
| worktreePath, | ||
| items: [ | ||
| { kind: "path", sourcePath: source }, | ||
| { | ||
| kind: "base64", | ||
| name: "image.png", | ||
| data: Buffer.from("png").toString("base64"), | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| expect(paths).toEqual({ | ||
| ok: true, | ||
| paths: [ | ||
| `${WORKSPACE_ATTACHMENTS_DIR}/build.dmg`, | ||
| `${WORKSPACE_ATTACHMENTS_DIR}/image.png`, | ||
| ], | ||
| }); | ||
| expect(readFileSync(join(directory, "build.dmg"), "utf8")).toBe("first"); | ||
| expect(readFileSync(join(directory, "image.png"), "utf8")).toBe("png"); | ||
| expect(readFileSync(join(directory, ".gitignore"), "utf8")).toBe("*\n"); | ||
| }); | ||
|
|
||
| it("never overwrites an earlier attachment", async () => { | ||
| const { source, worktreePath, directory } = setup(); | ||
| const items = [{ kind: "path" as const, sourcePath: source }]; | ||
|
|
||
| await writeAttachmentsIntoWorktree({ worktreePath, items }); | ||
| writeFileSync(source, "second"); | ||
| expect(await writeAttachmentsIntoWorktree({ worktreePath, items })).toEqual( | ||
| { ok: true, paths: [`${WORKSPACE_ATTACHMENTS_DIR}/build_1.dmg`] }, | ||
| ); | ||
| expect(readFileSync(join(directory, "build.dmg"), "utf8")).toBe("first"); | ||
| }); | ||
|
|
||
| it("reports which source is missing, a folder or unreadable, before writing anything", async () => { | ||
| const { root, source, worktreePath, directory } = setup(); | ||
| const unreadable = join(root, "locked.txt"); | ||
| writeFileSync(unreadable, "secret"); | ||
| chmodSync(unreadable, 0o000); | ||
|
|
||
| for (const [sourcePath, problem] of [ | ||
| [join(root, "moved.dmg"), "missing"], | ||
| [root, "not-a-file"], | ||
| [unreadable, "unreadable"], | ||
| ] as const) { | ||
| expect( | ||
| await writeAttachmentsIntoWorktree({ | ||
| worktreePath, | ||
| items: [ | ||
| { kind: "path", sourcePath: source }, | ||
| { kind: "path", sourcePath }, | ||
| ], | ||
| }), | ||
| ).toEqual({ ok: false, index: 1, problem }); | ||
| } | ||
| expect(existsSync(directory)).toBe(false); | ||
| }); | ||
|
|
||
| it("refuses a UNC source path", async () => { | ||
| const { worktreePath } = setup(); | ||
|
|
||
| await expect( | ||
| writeAttachmentsIntoWorktree({ | ||
| worktreePath, | ||
| items: [{ kind: "path", sourcePath: "//server/share/x.png" }], | ||
| }), | ||
| ).rejects.toThrow("Not a local absolute path"); | ||
| }); | ||
| }); | ||
145 changes: 145 additions & 0 deletions
145
apps/desktop/src/lib/trpc/routers/attachments/attachments.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import { constants } from "node:fs"; | ||
| import { copyFile, open, stat, writeFile } from "node:fs/promises"; | ||
| import { basename, isAbsolute, join } from "node:path"; | ||
| import { | ||
| MAX_ATTACHMENT_BYTES, | ||
| MAX_ATTACHMENTS, | ||
| } from "@superset/shared/attachment-limits"; | ||
| import { | ||
| attachmentFallbackName, | ||
| attachmentNameWithSuffix, | ||
| sanitizeAttachmentFileName, | ||
| WORKSPACE_ATTACHMENTS_DIR, | ||
| } from "@superset/shared/workspace-attachments"; | ||
| import { ensureWorkspaceAttachmentsDir } from "@superset/shared/workspace-attachments-dir"; | ||
| import { TRPCError } from "@trpc/server"; | ||
| import { z } from "zod"; | ||
| import { publicProcedure, router } from "../.."; | ||
|
|
||
| const MAX_NAME_ATTEMPTS = 50; | ||
|
|
||
| const itemSchema = z.discriminatedUnion("kind", [ | ||
| z.object({ kind: z.literal("path"), sourcePath: z.string().min(1) }), | ||
| z.object({ | ||
| kind: z.literal("base64"), | ||
| name: z.string(), | ||
| data: z.string().max(Math.ceil(MAX_ATTACHMENT_BYTES / 3) * 4), | ||
| }), | ||
| ]); | ||
|
|
||
| export type LocalAttachmentItem = z.infer<typeof itemSchema>; | ||
|
|
||
| export type AttachmentSourceProblem = "missing" | "not-a-file" | "unreadable"; | ||
|
|
||
| export type WriteAttachmentsResult = | ||
| | { ok: true; paths: string[] } | ||
| | { ok: false; index: number; problem: AttachmentSourceProblem }; | ||
|
|
||
| function badRequest(message: string): TRPCError { | ||
| return new TRPCError({ code: "BAD_REQUEST", message }); | ||
| } | ||
|
|
||
| // Windows opens an SMB connection for a UNC path, which can leak the user's NTLM hash. | ||
| function assertLocalAbsolute(path: string): void { | ||
| if (!isAbsolute(path) || /^[\\/]{2}/.test(path)) { | ||
| throw badRequest(`Not a local absolute path: ${path}`); | ||
| } | ||
| } | ||
|
|
||
| function errnoOf(error: unknown): string | undefined { | ||
| return (error as NodeJS.ErrnoException | null)?.code; | ||
| } | ||
|
|
||
| async function sourceProblem( | ||
| sourcePath: string, | ||
| ): Promise<AttachmentSourceProblem | null> { | ||
| try { | ||
| if (!(await stat(sourcePath)).isFile()) return "not-a-file"; | ||
| await (await open(sourcePath, "r")).close(); | ||
| return null; | ||
| } catch (error) { | ||
| const code = errnoOf(error); | ||
| if (code === "ENOENT" || code === "ENOTDIR") return "missing"; | ||
| if (code === "EACCES" || code === "EPERM") return "unreadable"; | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| async function assertWorktree(worktreePath: string): Promise<void> { | ||
| assertLocalAbsolute(worktreePath); | ||
| if (!(await stat(worktreePath)).isDirectory()) { | ||
| throw badRequest(`Not a directory: ${worktreePath}`); | ||
| } | ||
| } | ||
|
|
||
| function baseNameOf(item: LocalAttachmentItem, index: number): string { | ||
| const raw = item.kind === "path" ? basename(item.sourcePath) : item.name; | ||
| return sanitizeAttachmentFileName(raw) ?? attachmentFallbackName(index); | ||
| } | ||
|
|
||
| async function writeExclusive( | ||
| item: LocalAttachmentItem, | ||
| destination: string, | ||
| ): Promise<void> { | ||
| if (item.kind === "path") { | ||
| await copyFile(item.sourcePath, destination, constants.COPYFILE_EXCL); | ||
| return; | ||
| } | ||
| await writeFile(destination, Buffer.from(item.data, "base64"), { | ||
| flag: "wx", | ||
| }); | ||
| } | ||
|
|
||
| async function writeUnique( | ||
| item: LocalAttachmentItem, | ||
| directory: string, | ||
| base: string, | ||
| ): Promise<string> { | ||
| for (let attempt = 0; attempt < MAX_NAME_ATTEMPTS; attempt++) { | ||
| const fileName = attachmentNameWithSuffix(base, attempt); | ||
| try { | ||
| await writeExclusive(item, join(directory, fileName)); | ||
| return fileName; | ||
| } catch (error) { | ||
| if (errnoOf(error) !== "EEXIST") throw error; | ||
| } | ||
| } | ||
| throw new Error(`Could not find a free name for ${base}`); | ||
| } | ||
|
|
||
| export async function writeAttachmentsIntoWorktree(input: { | ||
| worktreePath: string; | ||
| items: LocalAttachmentItem[]; | ||
| }): Promise<WriteAttachmentsResult> { | ||
| await assertWorktree(input.worktreePath); | ||
| for (const [index, item] of input.items.entries()) { | ||
| if (item.kind !== "path") continue; | ||
| assertLocalAbsolute(item.sourcePath); | ||
| const problem = await sourceProblem(item.sourcePath); | ||
| if (problem) return { ok: false, index, problem }; | ||
| } | ||
| const directory = ensureWorkspaceAttachmentsDir(input.worktreePath); | ||
| const paths: string[] = []; | ||
| for (const [index, item] of input.items.entries()) { | ||
| const fileName = await writeUnique( | ||
| item, | ||
| directory, | ||
| baseNameOf(item, index), | ||
| ); | ||
| paths.push(`${WORKSPACE_ATTACHMENTS_DIR}/${fileName}`); | ||
| } | ||
| return { ok: true, paths }; | ||
| } | ||
|
|
||
| export const createAttachmentsRouter = () => { | ||
| return router({ | ||
| writeIntoWorktree: publicProcedure | ||
| .input( | ||
| z.object({ | ||
| worktreePath: z.string().min(1), | ||
| items: z.array(itemSchema).min(1).max(MAX_ATTACHMENTS), | ||
| }), | ||
| ) | ||
| .mutation(({ input }) => writeAttachmentsIntoWorktree(input)), | ||
| }); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { createAttachmentsRouter } from "./attachments"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: ParadevAI/Paradev
Length of output: 2952
Script executed:
Repository: ParadevAI/Paradev
Length of output: 14247
Skip only the unreadable-source case when read denial is unavailable.
chmodSync(..., 0o000)does not denyopen(..., "r")for root on POSIX or on Windows. Since the test combines three cases, skipping it removes missing-file and folder coverage. Keep those cases in the current test and skip only the unreadable-source test.🧪 Suggested fix