-
Notifications
You must be signed in to change notification settings - Fork 9
feat(tasks): thread scheduled task titles onto GET /api/tasks/runs list runs #764
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
Open
sweetmantech
wants to merge
1
commit into
main
Choose a base branch
from
feat/task-runs-titles
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,77 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
|
|
||
| import { attachRunTitles } from "../attachRunTitles"; | ||
| import { buildRunTitleMap } from "../buildRunTitleMap"; | ||
| import { selectScheduledActions } from "@/lib/supabase/scheduled_actions/selectScheduledActions"; | ||
| import type { TriggerRun } from "@/lib/trigger/fetchTriggerRuns"; | ||
| import type { Tables } from "@/types/database.types"; | ||
|
|
||
| vi.mock("@/lib/supabase/scheduled_actions/selectScheduledActions", () => ({ | ||
| selectScheduledActions: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("../buildRunTitleMap", () => ({ | ||
| buildRunTitleMap: vi.fn(), | ||
| })); | ||
|
|
||
| const makeRun = (id: string): TriggerRun => ({ | ||
| id, | ||
| status: "COMPLETED", | ||
| createdAt: "2026-07-07T00:00:00.000Z", | ||
| startedAt: null, | ||
| finishedAt: null, | ||
| durationMs: 1000, | ||
| }); | ||
|
|
||
| const mockAction = { | ||
| id: "action_1", | ||
| title: "Weekly valuation + streams report", | ||
| trigger_schedule_id: "sched_1", | ||
| } as Tables<"scheduled_actions">; | ||
|
|
||
| describe("attachRunTitles", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("annotates runs with the resolved title, null when unmapped", async () => { | ||
| vi.mocked(selectScheduledActions).mockResolvedValue([mockAction]); | ||
| vi.mocked(buildRunTitleMap).mockResolvedValue( | ||
| new Map([["run_a", "Weekly valuation + streams report"]]), | ||
| ); | ||
|
|
||
| const result = await attachRunTitles([makeRun("run_a"), makeRun("run_b")], "acc_1", 20); | ||
|
|
||
| expect(selectScheduledActions).toHaveBeenCalledWith({ account_id: "acc_1" }); | ||
| expect(buildRunTitleMap).toHaveBeenCalledWith([mockAction], 20); | ||
| expect(result[0].title).toBe("Weekly valuation + streams report"); | ||
| expect(result[1].title).toBeNull(); | ||
| }); | ||
|
|
||
| it("preserves all existing run fields", async () => { | ||
| vi.mocked(selectScheduledActions).mockResolvedValue([mockAction]); | ||
| vi.mocked(buildRunTitleMap).mockResolvedValue(new Map()); | ||
|
|
||
| const run = { ...makeRun("run_a"), tags: ["account:acc_1"], costInCents: 0.5 }; | ||
| const result = await attachRunTitles([run], "acc_1", 20); | ||
|
|
||
| expect(result[0]).toEqual({ ...run, title: null }); | ||
| }); | ||
|
|
||
| it("returns an empty array without querying when there are no runs", async () => { | ||
| const result = await attachRunTitles([], "acc_1", 20); | ||
|
|
||
| expect(result).toEqual([]); | ||
| expect(selectScheduledActions).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("fails open with null titles when scheduled_actions lookup throws", async () => { | ||
| vi.mocked(selectScheduledActions).mockRejectedValue(new Error("db down")); | ||
|
|
||
| const result = await attachRunTitles([makeRun("run_a")], "acc_1", 20); | ||
|
|
||
| expect(result).toHaveLength(1); | ||
| expect(result[0].id).toBe("run_a"); | ||
| expect(result[0].title).toBeNull(); | ||
| }); | ||
| }); |
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,87 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
|
|
||
| import { buildRunTitleMap } from "../buildRunTitleMap"; | ||
| import { fetchTriggerRuns } from "@/lib/trigger/fetchTriggerRuns"; | ||
|
|
||
| vi.mock("@/lib/trigger/fetchTriggerRuns", () => ({ | ||
| fetchTriggerRuns: vi.fn(), | ||
| })); | ||
|
|
||
| const baseRun = { | ||
| status: "COMPLETED", | ||
| createdAt: "2026-07-07T00:00:00.000Z", | ||
| startedAt: null, | ||
| finishedAt: null, | ||
| durationMs: 1000, | ||
| }; | ||
|
|
||
| describe("buildRunTitleMap", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("maps run ids from schedule-filtered runs to the action's title", async () => { | ||
| vi.mocked(fetchTriggerRuns).mockResolvedValueOnce([ | ||
| { ...baseRun, id: "run_a" }, | ||
| { ...baseRun, id: "run_b" }, | ||
| ]); | ||
|
|
||
| const map = await buildRunTitleMap( | ||
| [{ title: "Weekly valuation + streams report", trigger_schedule_id: "sched_1" }], | ||
| 20, | ||
| ); | ||
|
|
||
| expect(fetchTriggerRuns).toHaveBeenCalledWith({ "filter[schedule]": "sched_1" }, 20); | ||
| expect(map.get("run_a")).toBe("Weekly valuation + streams report"); | ||
| expect(map.get("run_b")).toBe("Weekly valuation + streams report"); | ||
| }); | ||
|
|
||
| it("fetches once per schedule and merges entries across actions", async () => { | ||
| vi.mocked(fetchTriggerRuns) | ||
| .mockResolvedValueOnce([{ ...baseRun, id: "run_a" }]) | ||
| .mockResolvedValueOnce([{ ...baseRun, id: "run_b" }]); | ||
|
|
||
| const map = await buildRunTitleMap( | ||
| [ | ||
| { title: "Task One", trigger_schedule_id: "sched_1" }, | ||
| { title: "Task Two", trigger_schedule_id: "sched_2" }, | ||
| ], | ||
| 5, | ||
| ); | ||
|
|
||
| expect(fetchTriggerRuns).toHaveBeenCalledTimes(2); | ||
| expect(map.get("run_a")).toBe("Task One"); | ||
| expect(map.get("run_b")).toBe("Task Two"); | ||
| }); | ||
|
|
||
| it("skips actions without a trigger_schedule_id", async () => { | ||
| const map = await buildRunTitleMap([{ title: "Unlinked task", trigger_schedule_id: null }], 20); | ||
|
|
||
| expect(fetchTriggerRuns).not.toHaveBeenCalled(); | ||
| expect(map.size).toBe(0); | ||
| }); | ||
|
|
||
| it("fails open per action when a Trigger.dev fetch throws", async () => { | ||
| vi.mocked(fetchTriggerRuns) | ||
| .mockRejectedValueOnce(new Error("Trigger.dev API error: 500")) | ||
| .mockResolvedValueOnce([{ ...baseRun, id: "run_b" }]); | ||
|
|
||
| const map = await buildRunTitleMap( | ||
| [ | ||
| { title: "Broken schedule", trigger_schedule_id: "sched_1" }, | ||
| { title: "Working schedule", trigger_schedule_id: "sched_2" }, | ||
| ], | ||
| 20, | ||
| ); | ||
|
|
||
| expect(map.size).toBe(1); | ||
| expect(map.get("run_b")).toBe("Working schedule"); | ||
| }); | ||
|
|
||
| it("returns an empty map for an empty actions list", async () => { | ||
| const map = await buildRunTitleMap([], 20); | ||
|
|
||
| expect(fetchTriggerRuns).not.toHaveBeenCalled(); | ||
| expect(map.size).toBe(0); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { selectScheduledActions } from "@/lib/supabase/scheduled_actions/selectScheduledActions"; | ||
| import type { TriggerRun } from "@/lib/trigger/fetchTriggerRuns"; | ||
| import { buildRunTitleMap } from "./buildRunTitleMap"; | ||
|
|
||
| export type TaskRunWithTitle = TriggerRun & { title: string | null }; | ||
|
|
||
| /** | ||
| * Annotates Trigger.dev runs with the title of the scheduled action that | ||
| * triggered them. Titles are resolved by matching run ids against each of | ||
| * the account's schedules (see buildRunTitleMap); runs that cannot be | ||
| * mapped (e.g. non-scheduled runs) get title null. | ||
| * | ||
| * Fails open: if the scheduled_actions lookup or title resolution fails, | ||
| * every run is returned with title null so the runs list never breaks. | ||
| * | ||
| * @param runs - Runs from the account-tag list fetch | ||
| * @param accountId - The account whose scheduled actions to resolve against | ||
| * @param limit - Runs to fetch per schedule (match the list page size) | ||
| * @returns The same runs, each with a title field (null when unresolvable) | ||
| */ | ||
| export async function attachRunTitles( | ||
| runs: TriggerRun[], | ||
| accountId: string, | ||
| limit: number, | ||
| ): Promise<TaskRunWithTitle[]> { | ||
| if (runs.length === 0) { | ||
| return []; | ||
| } | ||
|
|
||
| try { | ||
| const actions = await selectScheduledActions({ account_id: accountId }); | ||
| const titleByRunId = await buildRunTitleMap(actions, limit); | ||
| return runs.map(run => ({ ...run, title: titleByRunId.get(run.id) ?? null })); | ||
| } catch (error) { | ||
| console.error("Error resolving task run titles:", error); | ||
| return runs.map(run => ({ ...run, title: null })); | ||
| } | ||
| } |
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,43 @@ | ||
| import { fetchTriggerRuns } from "@/lib/trigger/fetchTriggerRuns"; | ||
|
|
||
| export type RunTitleSource = { | ||
| title: string; | ||
| trigger_schedule_id: string | null; | ||
| }; | ||
|
|
||
| /** | ||
| * Builds a run-id → task-title map by fetching each scheduled action's | ||
| * recent Trigger.dev runs via the schedule filter. Trigger.dev list runs | ||
| * carry no schedule reference, so this reverse lookup is how a run is | ||
| * linked back to its originating scheduled action. | ||
| * | ||
| * Fails open per action: a failed Trigger.dev fetch contributes no | ||
| * entries instead of breaking the whole map. | ||
| * | ||
| * @param actions - Scheduled actions (title + trigger_schedule_id) | ||
| * @param limit - Runs to fetch per schedule (match the list page size) | ||
| * @returns Map of Trigger.dev run id to the originating task's title | ||
| */ | ||
| export async function buildRunTitleMap( | ||
| actions: RunTitleSource[], | ||
| limit: number, | ||
| ): Promise<Map<string, string>> { | ||
| const entriesPerAction = await Promise.all( | ||
| actions | ||
| .filter(action => action.trigger_schedule_id) | ||
| .map(async (action): Promise<(readonly [string, string])[]> => { | ||
| try { | ||
| const runs = await fetchTriggerRuns( | ||
| { "filter[schedule]": action.trigger_schedule_id as string }, | ||
| limit, | ||
| ); | ||
| return runs.map(run => [run.id, action.title] as const); | ||
| } catch { | ||
| // Trigger.dev fetch failed for this schedule — skip its titles | ||
| return []; | ||
| } | ||
| }), | ||
| ); | ||
|
|
||
| return new Map(entriesPerAction.flat()); | ||
| } |
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.
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.
P2: List-mode requests now scale with the account's total scheduled task count, not the requested run limit, because
attachRunTitlesfetches every scheduled action and then one Trigger.dev run list per schedule. Consider bounding or batching this lookup so a large account cannot makeGET /api/tasks/runsslow or trip Trigger.dev rate limits.Prompt for AI agents