diff --git a/src/components/Home/RunSection/RunRow.tsx b/src/components/Home/RunSection/RunRow.tsx index bcbbde8433..5692d3e7dd 100644 --- a/src/components/Home/RunSection/RunRow.tsx +++ b/src/components/Home/RunSection/RunRow.tsx @@ -27,6 +27,7 @@ import { runAnnotationsQueryOptions } from "@/services/runAnnotations"; import { getAnnotationValue, getPipelineTagsFromAnnotations, + RUN_REQUESTED_BY_ANNOTATION, RUN_SOURCE_ANNOTATION, } from "@/utils/annotations"; import { formatDate } from "@/utils/date"; @@ -65,6 +66,10 @@ const RunRow = ({ const name = run.pipeline_name ?? "Unknown pipeline"; const tags = getPipelineTagsFromAnnotations(annotations); const source = getAnnotationValue(annotations, RUN_SOURCE_ANNOTATION); + const requestedBy = getAnnotationValue( + annotations, + RUN_REQUESTED_BY_ANNOTATION, + ); const createdBy = run.created_by ?? "Unknown user"; const truncatedCreatedBy = truncateMiddle(createdBy); @@ -185,7 +190,11 @@ const RunRow = ({ - + { ...runAnnotationsQueryOptions(runId, backendUrl), }); const runSource = getAnnotationValue(runAnnotations, RUN_SOURCE_ANNOTATION); + const requestedBy = getAnnotationValue( + runAnnotations, + RUN_REQUESTED_BY_ANNOTATION, + ); const handleCopyUrl = () => { copyToClipboard(window.location.href); @@ -127,7 +132,9 @@ export const RunDetails = () => { {metadata && ( } + titleAction={ + + } items={[ { label: "Run Id", value: metadata.id }, { label: "Execution Id", value: metadata.root_execution_id }, diff --git a/src/components/shared/RunSource.test.tsx b/src/components/shared/RunSource.test.tsx index eab0555164..aa4fe80405 100644 --- a/src/components/shared/RunSource.test.tsx +++ b/src/components/shared/RunSource.test.tsx @@ -28,6 +28,18 @@ describe("getRunSourceMessage", () => { expect(getRunSourceMessage("tangent")).toBe("Submitted by a Tangent agent"); }); + it.each(["tangent", "tangent-ui"])( + "names the person who requested a %s run", + (source) => { + expect(getRunSourceMessage(source, "person@example.com")).toBe( + "Submitted by person@example.com via Tangent", + ); + expect(getRunSourceMessage("web-app", "person@example.com")).toBe( + "Submitted via the Tangle web app", + ); + }, + ); + /** Anything may submit a run, so the map is a reader's vocabulary. */ it("falls back for a submitter it has never heard of", () => { expect(getRunSourceMessage("some-future-tool")).toBe( diff --git a/src/components/shared/RunSource.tsx b/src/components/shared/RunSource.tsx index bbf3065a7f..171766d954 100644 --- a/src/components/shared/RunSource.tsx +++ b/src/components/shared/RunSource.tsx @@ -61,10 +61,21 @@ export const hasRunSource = (source?: string | null): boolean => Boolean(source); /** Human-readable message describing how a run was submitted. */ -export const getRunSourceMessage = (source?: string | null): string => - getRunSourceConfig(source).message; +export const getRunSourceMessage = ( + source?: string | null, + requestedBy?: string, +): string => { + if ( + requestedBy && + (source === TANGENT_AGENT_RUN_SOURCE || source === TANGENT_UI_RUN_SOURCE) + ) { + return `Submitted by ${requestedBy} via Tangent`; + } + return getRunSourceConfig(source).message; +}; interface RunSourceIconProps { + requestedBy?: string; source?: string | null; size?: "xs" | "sm" | "md"; className?: string; @@ -72,15 +83,17 @@ interface RunSourceIconProps { export const RunSourceIcon = ({ source, + requestedBy, size = "sm", className, }: RunSourceIconProps) => { - const { icon, message } = getRunSourceConfig(source); + const { icon } = getRunSourceConfig(source); + const message = getRunSourceMessage(source, requestedBy); return ( - + diff --git a/src/routes/v2/pages/RunView/components/RunDetailsContent.test.tsx b/src/routes/v2/pages/RunView/components/RunDetailsContent.test.tsx index 165b5aed87..2f39ad7cdf 100644 --- a/src/routes/v2/pages/RunView/components/RunDetailsContent.test.tsx +++ b/src/routes/v2/pages/RunView/components/RunDetailsContent.test.tsx @@ -130,6 +130,26 @@ afterEach(() => { }); describe("", () => { + it.each<[string, string | undefined, string]>([ + [ + "tangent", + "matt@example.test", + "Submitted by matt@example.test via Tangent", + ], + ["tangent", undefined, "Submitted by a Tangent agent"], + ])( + "shows the Run Info byline for a %s run requested by %s", + async (source, requester, expected) => { + fetchRunAnnotations.mockResolvedValue({ + source, + requested_by: requester, + }); + renderPanel(); + expect(await screen.findByText(expected)).toBeVisible(); + expect(screen.getByRole("img", { name: expected })).toBeInTheDocument(); + }, + ); + it("shows run annotations", async () => { fetchRunAnnotations.mockResolvedValue({ experiment: "baseline", diff --git a/src/routes/v2/pages/RunView/components/RunDetailsContent.tsx b/src/routes/v2/pages/RunView/components/RunDetailsContent.tsx index 023c51d51a..9a96d06a9a 100644 --- a/src/routes/v2/pages/RunView/components/RunDetailsContent.tsx +++ b/src/routes/v2/pages/RunView/components/RunDetailsContent.tsx @@ -44,6 +44,7 @@ import { isSystemRunAnnotation, PIPELINE_NOTES_ANNOTATION, PIPELINE_TAGS_ANNOTATION, + RUN_REQUESTED_BY_ANNOTATION, RUN_SOURCE_ANNOTATION, SYSTEM_ANNOTATIONS, } from "@/utils/annotations"; @@ -297,6 +298,10 @@ function RunInfoSection({ metadata }: { metadata: PipelineRunResponse }) { const { data: runAnnotations } = useRunAnnotations(metadata.id); const runSource = getAnnotationValue(runAnnotations, RUN_SOURCE_ANNOTATION); + const requestedBy = getAnnotationValue( + runAnnotations, + RUN_REQUESTED_BY_ANNOTATION, + ); const hasKnownSource = hasRunSource(runSource); return ( @@ -317,9 +322,13 @@ function RunInfoSection({ metadata }: { metadata: PipelineRunResponse }) { {hasKnownSource && ( - + - {getRunSourceMessage(runSource)} + {getRunSourceMessage(runSource, requestedBy)} )} diff --git a/src/routes/v2/pages/Tangent/components/ProjectChatArea.tsx b/src/routes/v2/pages/Tangent/components/ProjectChatArea.tsx index 261d02f411..88448148be 100644 --- a/src/routes/v2/pages/Tangent/components/ProjectChatArea.tsx +++ b/src/routes/v2/pages/Tangent/components/ProjectChatArea.tsx @@ -26,7 +26,7 @@ export const ProjectChatArea = observer(function ProjectChatArea() { const references = useChatReferences(store.projectId); const { createdBy } = useProjectAuthor(); const recordContribution = useRecordProjectContribution(); - const commands = useChatCommands(store.projectId); + const commands = useChatCommands(store.projectId, createdBy); const refreshSessionResources = useRefreshSessionResources(); const activeSessionId = store.activeSessionId; diff --git a/src/routes/v2/pages/Tangent/hooks/useChatCommands.test.tsx b/src/routes/v2/pages/Tangent/hooks/useChatCommands.test.tsx index c52bf8c22f..002d613013 100644 --- a/src/routes/v2/pages/Tangent/hooks/useChatCommands.test.tsx +++ b/src/routes/v2/pages/Tangent/hooks/useChatCommands.test.tsx @@ -1,4 +1,3 @@ -import type { EmbedCommand } from "@tangent/embed-react"; import { renderHook } from "@testing-library/react"; import { beforeEach, describe, expect, it, vi } from "vitest"; @@ -11,7 +10,12 @@ const fakes = vi.hoisted(() => ({ listEntries: vi.fn(), listFiles: vi.fn(), resolvePipeline: vi.fn(), - submit: vi.fn(), + submit: + vi.fn< + ( + deps: BridgeDeps, + ) => Promise<{ success: boolean; runId?: string; error?: string }> + >(), recordSessionRun: vi.fn(), })); @@ -67,8 +71,10 @@ const TRAIN = { archived: false, }; -function commandNamed(name: string): EmbedCommand { - const { result } = renderHook(() => useChatCommands("project-1")); +function commandNamed(name: string, currentUserId = "first@example.com") { + const { result } = renderHook(() => + useChatCommands("project-1", currentUserId), + ); const command = result.current.find((candidate) => candidate.name === name); if (!command) throw new Error(`No /${name} command`); return command; @@ -121,16 +127,22 @@ describe("useChatCommands", () => { it("starts each pipeline it's given, puts it in the chat, and reports the rest", async () => { fakes.submit.mockResolvedValue({ success: true, runId: "r-9" }); - const text = await commandNamed("run").run("[p-1, nope]"); + const text = await commandNamed("run").run( + "[p-1, nope]", + "first@example.com", + ); expect(fakes.resolvePipeline).toHaveBeenCalledWith({ source: "remote", backendUrl: "https://backend.example", pipelineId: "p-1", }); - const [deps] = fakes.submit.mock.calls[0] as [BridgeDeps]; + const [deps] = fakes.submit.mock.calls[0]; expect(deps.getSpec()?.name).toBe("Train"); - expect(Object.values(deps.getRunAnnotations?.() ?? {})).toEqual(["true"]); + expect(deps.getRunAnnotations?.()).toEqual({ + "tangleml.com/project/id/project-1": "true", + requested_by: "first@example.com", + }); expect(fakes.recordSessionRun).toHaveBeenCalledWith( "chat-1", "r-9", @@ -141,13 +153,44 @@ describe("useChatCommands", () => { expect(text).toContain("- `nope`: not one of this project's pipelines"); }); + it.each([undefined, "guest@example.com"])( + "leaves the requester unset when the sender is %s", + async (authorId) => { + fakes.submit.mockResolvedValue({ success: true, runId: "r-1" }); + await commandNamed("run").run("p-1", authorId); + const [deps] = fakes.submit.mock.calls[0]; + expect(deps.getRunAnnotations?.()).not.toHaveProperty("requested_by"); + expect(recordContribution).not.toHaveBeenCalled(); + }, + ); + + it("keeps overlapping requests with the people who sent them", async () => { + const first = Promise.withResolvers<{ success: boolean; runId: string }>(); + fakes.submit + .mockReturnValueOnce(first.promise) + .mockResolvedValueOnce({ success: true, runId: "second" }); + const a = commandNamed("run").run("p-1", "first@example.com"); + await vi.waitFor(() => expect(fakes.submit).toHaveBeenCalledOnce()); + await commandNamed("run", "second@example.com").run( + "p-1", + "second@example.com", + ); + first.resolve({ success: true, runId: "first" }); + await a; + expect( + fakes.submit.mock.calls.map( + ([deps]) => deps.getRunAnnotations?.()?.requested_by, + ), + ).toEqual(["first@example.com", "second@example.com"]); + }); + it("reports why the backend didn't start a pipeline", async () => { fakes.submit.mockResolvedValue({ success: false, error: "A required input has no value.", }); - const text = await commandNamed("run").run("p-1"); + const text = await commandNamed("run").run("p-1", "first@example.com"); expect(text).toContain("`p-1` (Train): A required input has no value."); expect(fakes.recordSessionRun).not.toHaveBeenCalled(); diff --git a/src/routes/v2/pages/Tangent/hooks/useChatCommands.ts b/src/routes/v2/pages/Tangent/hooks/useChatCommands.ts index 45cdbecaaf..baa1a69243 100644 --- a/src/routes/v2/pages/Tangent/hooks/useChatCommands.ts +++ b/src/routes/v2/pages/Tangent/hooks/useChatCommands.ts @@ -43,6 +43,7 @@ import { import { useRecordProjectContribution } from "@/services/projects/useProjectContributors"; import { runExecutionStatsQuery } from "@/services/projects/useProjectRuns"; import { useTeamForSubmission } from "@/services/teams/useTeamForSubmission"; +import { RUN_REQUESTED_BY_ANNOTATION } from "@/utils/annotationKeys"; import { buildProjectRunAnnotationKey } from "@/utils/annotations"; import { getExecutionStatusLabel, @@ -56,7 +57,7 @@ import { PIPELINE_YAML_LOAD_OPTIONS } from "@/utils/yaml"; * project card does, and `/run` starts pipelines by id. None of them starts * the agent; it's sent their results with the next message. */ -export function useChatCommands(projectId: string): EmbedCommand[] { +export function useChatCommands(projectId: string, currentUserId?: string) { const store = useTangentProject(); const handlers = useProjectCardHandlers(); const storage = usePipelineStorage(); @@ -139,6 +140,7 @@ export function useChatCommands(projectId: string): EmbedCommand[] { id: string, pipelines: ProjectCardEntry[], sessionId: string | undefined, + requestedBy: string | undefined, ): Promise { const pipeline = pipelines.find(({ target }) => targetId(target) === id); if (!pipeline) return { id, error: "not one of this project's pipelines" }; @@ -153,13 +155,14 @@ export function useChatCommands(projectId: string): EmbedCommand[] { queryClient, getRunAnnotations: () => ({ [buildProjectRunAnnotationKey(projectId)]: "true", + ...(requestedBy && { [RUN_REQUESTED_BY_ANNOTATION]: requestedBy }), }), getRunTeam: () => runTeamRef.current, }).submitPipelineRun(); if (!result.success || !result.runId) { return { id, name: pipeline.name, error: result.error }; } - recordContribution(projectId); + if (requestedBy) recordContribution(projectId); if (sessionId) { // Best effort: the run has started whether or not the chat noted it. await recordSessionRun(sessionId, result.runId, pipeline.name).catch( @@ -212,7 +215,7 @@ export function useChatCommands(projectId: string): EmbedCommand[] { name: "run", description: "Start pipelines by id", argumentHint: "", - run: async (args) => { + run: async (args: string, authorId?: string) => { if (parsePipelineIds(args).length === 0) { throw new Error( "add pipeline ids separated by commas. /pipelines lists them.", @@ -220,6 +223,8 @@ export function useChatCommands(projectId: string): EmbedCommand[] { } // The person may open another chat while these start. const sessionId = store.activeSessionId; + const requestedBy = + authorId === currentUserId ? currentUserId : undefined; const { pipelines } = await handlers.listEntries(); const ids = parsePipelineIds( args, @@ -227,10 +232,10 @@ export function useChatCommands(projectId: string): EmbedCommand[] { ); const starts: RunStart[] = []; for (const id of ids) { - starts.push(await start(id, pipelines, sessionId)); + starts.push(await start(id, pipelines, sessionId, requestedBy)); } return runStartsResult(starts); }, }, - ]; + ] satisfies EmbedCommand[]; } diff --git a/src/utils/annotationKeys.ts b/src/utils/annotationKeys.ts index 26ffd52074..0f95d6554c 100644 --- a/src/utils/annotationKeys.ts +++ b/src/utils/annotationKeys.ts @@ -12,6 +12,7 @@ export const PIPELINE_TAGS_ANNOTATION = "tags"; export const PIPELINE_CANONICAL_NAME_ANNOTATION = "canonical-pipeline-name"; export const RUN_NAME_TEMPLATE_ANNOTATION = "run-name-template"; export const RUN_SOURCE_ANNOTATION = "source"; +export const RUN_REQUESTED_BY_ANNOTATION = "requested_by"; export const EDITOR_POSITION_ANNOTATION = "editor.position"; export const EDITOR_COLLAPSED_ANNOTATION = "editor.collapsed"; export const EDITOR_FLOW_DIRECTION_ANNOTATION = "editor.flow-direction";