diff --git a/src/routes/v2/pages/Tangent/components/TangentProjectAgentProvider.tsx b/src/routes/v2/pages/Tangent/components/TangentProjectAgentProvider.tsx index 42ca3d9962..508e16082d 100644 --- a/src/routes/v2/pages/Tangent/components/TangentProjectAgentProvider.tsx +++ b/src/routes/v2/pages/Tangent/components/TangentProjectAgentProvider.tsx @@ -283,7 +283,10 @@ export function TangentProjectAgentProvider({ attach: (input) => createProjectResource(store.projectId, input), }); await refreshProjectResources(); - return { pipelineName: file.storageKey, fileId: file.id }; + return { + pipelineName: file.storageKey, + target: pipelineFileTarget(file), + }; }, readSessionResource: async (uri) => { const read = readSessionResourceRef.current; diff --git a/src/routes/v2/pages/Tangent/services/createWorkareaRemoteTools.test.ts b/src/routes/v2/pages/Tangent/services/createWorkareaRemoteTools.test.ts index f5320b6cb3..e1b421ae3b 100644 --- a/src/routes/v2/pages/Tangent/services/createWorkareaRemoteTools.test.ts +++ b/src/routes/v2/pages/Tangent/services/createWorkareaRemoteTools.test.ts @@ -399,10 +399,15 @@ describe("createWorkareaRemoteTools", () => { }); it("import_pipeline imports the YAML, opens it, and reports the tab it can spawn into", async () => { - const importPipeline = vi - .fn() - .mockResolvedValue({ pipelineName: "Imported DAG", fileId: "file-7" }); - const tab = { ...pipelineTab("tab-imp", "file-7"), title: "Imported DAG" }; + const importPipeline = vi.fn().mockResolvedValue({ + pipelineName: "Imported DAG", + target: { type: "pipeline", identity: "remote/file-7" }, + }); + const tab = { + ...pipelineTab("tab-imp"), + target: { type: "pipeline", identity: "remote/file-7" } as const, + title: "Imported DAG", + }; const openTarget = vi.fn().mockResolvedValue(tab); const tools = createWorkareaRemoteTools(() => makeDeps({ @@ -424,7 +429,7 @@ describe("createWorkareaRemoteTools", () => { "Imported DAG", ); expect(openTarget).toHaveBeenCalledWith( - { type: "pipeline", identity: "id/file-7" }, + { type: "pipeline", identity: "remote/file-7" }, "Imported DAG", ); expect(result).toEqual({ @@ -432,7 +437,7 @@ describe("createWorkareaRemoteTools", () => { kind: "pipeline", name: "Imported DAG", title: "Imported DAG", - target: "pipeline://id/file-7", + target: "pipeline://remote/file-7", active: true, environmentId: "env-7", ready: true, @@ -443,9 +448,10 @@ describe("createWorkareaRemoteTools", () => { const yaml = "name: From Resource\nimplementation:\n graph:\n tasks: {}\n"; const readSessionResource = vi.fn().mockResolvedValue(yaml); - const importPipeline = vi - .fn() - .mockResolvedValue({ pipelineName: "From Resource", fileId: "file-r" }); + const importPipeline = vi.fn().mockResolvedValue({ + pipelineName: "From Resource", + target: { type: "pipeline", identity: idIdentity("file-r") }, + }); const tab = { ...pipelineTab("tab-r", "file-r"), title: "From Resource" }; const tools = createWorkareaRemoteTools(() => makeDeps({ diff --git a/src/routes/v2/pages/Tangent/services/createWorkareaRemoteTools.ts b/src/routes/v2/pages/Tangent/services/createWorkareaRemoteTools.ts index 8e9126b916..b78b48f825 100644 --- a/src/routes/v2/pages/Tangent/services/createWorkareaRemoteTools.ts +++ b/src/routes/v2/pages/Tangent/services/createWorkareaRemoteTools.ts @@ -53,7 +53,7 @@ export interface WorkareaToolDeps { importPipeline: ( yaml: string, name?: string, - ) => Promise<{ pipelineName: string; fileId: string }>; + ) => Promise<{ pipelineName: string; target: WorkareaTarget }>; readSessionResource: (uri: string) => Promise; clonePipeline: ( runId: string, @@ -331,10 +331,10 @@ export function createWorkareaRemoteTools( "handle of a Tangent session resource holding the pipeline YAML, which " + "is how you bring in a pipeline another agent contributed — or inline " + "`yaml`. The content must be a graph pipeline spec. `name` is optional " + - "and is made unique if it is already taken; otherwise the name from the " + - "YAML is used. The pipeline is attached to the project automatically " + + "and overrides the name in the YAML. " + + "The pipeline is attached to the project automatically " + "(it shows up in Resources). Returns the tab summary plus the pipeline " + - "`name` and its `pipeline://id/` `target`, including the " + + "`name` and its `target`, including the " + "`environmentId` and `ready` flag for that tab's sub-agent host, so a " + "spawn can follow straight on.", inputSchema: { @@ -374,14 +374,10 @@ export function createWorkareaRemoteTools( "Provide a `resourceUri` to a session resource, or inline `yaml`.", ); } - const { pipelineName, fileId } = await deps.importPipeline( + const { pipelineName, target } = await deps.importPipeline( pipelineYaml, optionalName(args), ); - const target: WorkareaTarget = { - type: "pipeline", - identity: idIdentity(fileId), - }; const tab = await openAndSummarize(getDeps, target, pipelineName); return { ...tab, name: pipelineName }; }, diff --git a/src/services/pipelineStorage/PipelineStorageService.test.ts b/src/services/pipelineStorage/PipelineStorageService.test.ts index 596e6f9195..d57e5b694c 100644 --- a/src/services/pipelineStorage/PipelineStorageService.test.ts +++ b/src/services/pipelineStorage/PipelineStorageService.test.ts @@ -8,7 +8,10 @@ import { LibraryDB } from "@/providers/ComponentLibraryProvider/libraries/storag import { TourPipelineStorageService } from "@/providers/TourProvider/tourPipelineStorage/TourPipelineStorageService"; import type { SavedPipelineDocument } from "@/services/pipelineSpecService"; import { migrateLocalPipelineOnOpen } from "@/services/pipelineStorage/migrateLocalPipelineOnOpen"; -import { createAndAttachPipeline } from "@/services/projects/createAndAttachPipeline"; +import { + createAndAttachPipeline, + importAndAttachPipeline, +} from "@/services/projects/createAndAttachPipeline"; import { pipelineFileTarget } from "@/services/projects/pipelineResource"; import { createProjectResource, @@ -751,6 +754,33 @@ describe("remote drafts and project attachment", () => { }, ); + it("attaches an imported graph using the saved backend pipeline ID", async () => { + const attach = vi.fn().mockResolvedValue(undefined); + const localWrite = vi.spyOn(storage.rootFolder, "addFile"); + const file = await importAndAttachPipeline({ + storage, + yaml: content, + attach, + }); + expect(attach).toHaveBeenCalledWith({ + entity: "pipeline", + entityId: ref.pipelineId, + name: editedSpec.name, + }); + expect(pipelineFileTarget(file).identity).toBe(`remote/${ref.pipelineId}`); + expect(localWrite).not.toHaveBeenCalled(); + }); + + it("keeps the saved import when project attachment cannot be confirmed", async () => { + const remove = vi.spyOn(PipelineFile.prototype, "deleteFile"); + const attach = vi.fn().mockRejectedValue(new Error("Response lost")); + await expect( + importAndAttachPipeline({ storage, yaml: content, attach }), + ).rejects.toThrow(`Pipeline ${ref.pipelineId} was saved`); + expect(client.put).toHaveBeenCalledOnce(); + expect(remove).not.toHaveBeenCalled(); + }); + it("rolls back a local pipeline when attachment is refused", async () => { vi.stubEnv("VITE_REMOTE_PIPELINES", "false"); const deleteFile = vi.fn().mockResolvedValue(undefined); diff --git a/src/services/projects/createAndAttachPipeline.ts b/src/services/projects/createAndAttachPipeline.ts index 3e04bfeaf3..5449a12407 100644 --- a/src/services/projects/createAndAttachPipeline.ts +++ b/src/services/projects/createAndAttachPipeline.ts @@ -1,6 +1,9 @@ import { createNewPipeline } from "@/routes/v2/pages/Editor/components/EditorMenuBar/components/fileMenu.actions"; import { availablePipelineName } from "@/services/localPipelines/localPipelinesService"; -import { getPipelineCreationBackendUrl } from "@/services/pipelineStorage/pipelineCreation"; +import { + createPipelineFromContent, + getPipelineCreationBackendUrl, +} from "@/services/pipelineStorage/pipelineCreation"; import type { PipelineFile } from "@/services/pipelineStorage/PipelineFile"; import type { PipelineStorageService } from "@/services/pipelineStorage/PipelineStorageService"; import { isGraphImplementation } from "@/utils/componentSpec"; @@ -39,6 +42,12 @@ async function attachOrCleanup( try { await attach(pipelineFileResourceInput(file)); } catch (error) { + if (file.remote) { + throw new Error( + `Pipeline ${file.id} was saved, but its project attachment could not be confirmed. Check the project before importing again.`, + { cause: error }, + ); + } await file .deleteFile() .catch((cleanupError: unknown) => @@ -90,14 +99,18 @@ export async function importAndAttachPipeline({ ); } - const storageKey = await availablePipelineName( - name ?? componentSpec.name ?? "Imported Pipeline", - ); + const backendUrl = await getPipelineCreationBackendUrl(); + const requestedName = name ?? componentSpec.name ?? "Imported Pipeline"; + const storageKey = backendUrl + ? requestedName + : await availablePipelineName(requestedName); componentSpec.name = storageKey; - const file = await storage.rootFolder.addFile( + const file = await createPipelineFromContent( + storage, storageKey, componentSpecToYaml(componentSpec), + { backendUrl }, ); return attachOrCleanup(file, attach);