Skip to content

fix: fetch newest workflow runs in ui query#1140

Merged
adityachoudhari26 merged 1 commit into
mainfrom
fetch-newest-workflow-runs
May 18, 2026
Merged

fix: fetch newest workflow runs in ui query#1140
adityachoudhari26 merged 1 commit into
mainfrom
fetch-newest-workflow-runs

Conversation

@adityachoudhari26
Copy link
Copy Markdown
Member

@adityachoudhari26 adityachoudhari26 commented May 18, 2026

fixes #1128

Summary by CodeRabbit

  • Refactor
    • Increased default pagination limit for workflow runs list from 100 to 500 results per page.

Review Change Stack

Copilot AI review requested due to automatic review settings May 18, 2026 15:38
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 18, 2026

📝 Walkthrough

Walkthrough

The workflowsRouter.runs.list endpoint pagination is restructured to correctly handle large result sets. The default pagination limit is increased from 100 to 500, and the query now applies limit/offset at the workflow run ID selection level instead of the final joined result set. A new newestRunIds subquery selects and orders runs before joining job data.

Changes

Workflow Runs Listing

Layer / File(s) Summary
Pagination restructuring and limit increase
packages/trpc/src/routes/workflows.ts
Input schema default limit increased to 500. New newestRunIds subquery selects workflow run IDs ordered by minimum job creation time with pagination applied at the run-ID level. Main query restructured to join from this subquery through schema.workflowRun, schema.workflowJob, and schema.job, replacing direct filtering and pagination on the joined result set.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A hundred runs too few, the queries would sigh,
Now five hundred flow, and pagination's the key—
A subquery sorts runs before they unite,
With jobs they all join in the proper light!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: fetch newest workflow runs in ui query' accurately describes the main change of restructuring the query to properly fetch and display newest workflow runs beyond the first 100.
Linked Issues check ✅ Passed The code changes directly address issue #1128 by increasing the default pagination limit from 100 to 500 and restructuring the query to properly fetch newest runs using a subquery approach for correct ordering and selection.
Out of Scope Changes check ✅ Passed All changes are scoped to the workflowsRouter.runs.list endpoint pagination logic, directly related to the linked issue objective of displaying workflow runs beyond the first 100.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fetch-newest-workflow-runs

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes the workflows UI showing only the first 100 runs by changing the runs.list tRPC query to first pick the newest run IDs (ordered by the earliest job creation time) and then fetch their job details, instead of joining first and applying LIMIT against the row-multiplied join. The default page size is also bumped from 100 to 500.

Changes:

  • Add a subquery that selects the newest workflowRun.ids for a workflow, ordered by min(job.createdAt) DESC NULLS LAST, with LIMIT/OFFSET applied there.
  • Drive the main job query off that subquery via an innerJoin, removing the previous where/limit/offset from the row-multiplied join.
  • Increase the default limit from 100 to 500.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/trpc/src/routes/workflows.ts`:
- Around line 165-178: The ordering uses min(schema.job.createdAt) so workflow
runs without jobs sort last; add a createdAt timestamp to the workflowRun schema
and migration, then update the newestRunIds query to order using COALESCE
between job.createdAt and workflowRun.createdAt (e.g.
orderBy(sql`coalesce(min(${schema.job.createdAt}),
${schema.workflowRun.createdAt}) desc nulls last`)) or simply order by
schema.workflowRun.createdAt desc; update the workflowRun schema (createdAt),
migration, and replace the orderBy in the newestRunIds query accordingly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 5f4a66fb-0fba-4075-a3bc-159cb8a4077a

📥 Commits

Reviewing files that changed from the base of the PR and between 96dded9 and 03493b0.

📒 Files selected for processing (1)
  • packages/trpc/src/routes/workflows.ts

Comment on lines +165 to +178
const newestRunIds = ctx.db
.select({ id: schema.workflowRun.id })
.from(schema.workflowRun)
.leftJoin(
schema.workflowJob,
eq(schema.workflowJob.workflowRunId, schema.workflowRun.id),
)
.leftJoin(schema.job, eq(schema.job.id, schema.workflowJob.jobId))
.where(eq(schema.workflowRun.workflowId, input.workflowId))
.groupBy(schema.workflowRun.id)
.orderBy(sql`min(${schema.job.createdAt}) desc nulls last`)
.limit(input.limit)
.offset(input.offset)
.as("newest_runs");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | 🏗️ Heavy lift

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Check schema.workflowRun for timestamp fields

# Search for workflowRun schema definition and look for timestamp fields
ast-grep --pattern $'workflowRun = $$$({
  $$$
})'

Repository: ctrlplanedev/ctrlplane

Length of output: 48


🏁 Script executed:

#!/bin/bash
# Description: Check if workflow runs can be created without jobs

# Look for workflowRun creation logic to see if jobs are always created
rg -nP --type=ts -C5 'workflowRun.*insert|insert.*workflowRun'

Repository: ctrlplanedev/ctrlplane

Length of output: 48


🏁 Script executed:

# Find schema-related files
fd -e ts -e js -e sql | grep -i schema | head -20

Repository: ctrlplanedev/ctrlplane

Length of output: 955


🏁 Script executed:

# Look for drizzle or database schema definitions
find . -type f \( -name "*schema*" -o -name "*migration*" \) | grep -E "\.(ts|js|sql)$" | head -20

Repository: ctrlplanedev/ctrlplane

Length of output: 248


🏁 Script executed:

# Search for workflowRun definition in the codebase
rg -l "workflowRun" --type=ts | head -10

Repository: ctrlplanedev/ctrlplane

Length of output: 325


🏁 Script executed:

# Check the workflow schema definition
cat -n packages/db/src/schema/workflow.ts

Repository: ctrlplanedev/ctrlplane

Length of output: 1761


🏁 Script executed:

# Look at the context around lines 165-178 in workflows.ts
sed -n '150,190p' packages/trpc/src/routes/workflows.ts

Repository: ctrlplanedev/ctrlplane

Length of output: 1453


🏁 Script executed:

# Check where workflowRun records are created
rg -n "workflowRun" packages/trpc/src/routes/workflows.ts -B5 -A5

Repository: ctrlplanedev/ctrlplane

Length of output: 3345


🏁 Script executed:

# Look for where workflowRun is inserted/created
rg -n "insert.*workflowRun|workflowRun.*insert" packages/trpc/src -B3 -A3

Repository: ctrlplanedev/ctrlplane

Length of output: 48


🏁 Script executed:

# Check if there's any creation logic that always creates jobs
rg -n "create.*workflow|workflow.*create" packages/trpc/src/routes/workflows.ts -B5 -A5

Repository: ctrlplanedev/ctrlplane

Length of output: 48


🏁 Script executed:

# Search for where workflowRun is created - look for .insert calls
rg -n "\.insert\(" packages/trpc/src/routes/workflows.ts -B3 -A3

Repository: ctrlplanedev/ctrlplane

Length of output: 48


🏁 Script executed:

# Look at the job schema to confirm it has createdAt
sed -n '1,100p' packages/db/src/schema/job.ts | head -50

Repository: ctrlplanedev/ctrlplane

Length of output: 1180


🏁 Script executed:

# Get more of the job schema to see the createdAt field
sed -n '45,150p' packages/db/src/schema/job.ts

Repository: ctrlplanedev/ctrlplane

Length of output: 3115


🏁 Script executed:

# Search for where workflowRun is created - broader search
rg -n "workflowRun" packages/trpc/src -B2 -A2 | grep -E "\.insert|\.values|create|add"

Repository: ctrlplanedev/ctrlplane

Length of output: 178


🏁 Script executed:

# Look for create run mutations
rg -n "createRun|create.*run|run.*create" packages/trpc/src/routes/workflows.ts -B3 -A10

Repository: ctrlplanedev/ctrlplane

Length of output: 48


🏁 Script executed:

# Check if there's any logic that always creates jobs when creating a run
rg -n "workflowRun.*insert|insert.*workflowRun" packages/trpc -B5 -A5

Repository: ctrlplanedev/ctrlplane

Length of output: 48


🏁 Script executed:

# Look at all procedures in workflows.ts to find run creation
sed -n '1,300p' packages/trpc/src/routes/workflows.ts | grep -E "\.mutation|\.insert|workflowRun" -B2 -A2

Repository: ctrlplanedev/ctrlplane

Length of output: 2329


Add createdAt field to workflowRun schema or reconsider ordering strategy.

The query orders by min(schema.job.createdAt) desc nulls last, which means workflow runs without any jobs will appear at the end of the list (because their min job creation time is NULL). Since the workflowRun table has no timestamp field (confirmed from schema), and runs can exist without jobs (indicated by the LEFT JOIN strategy), this ordering produces incorrect results for newly created runs that haven't spawned jobs yet.

Either add a createdAt timestamp to the workflowRun schema and order by that, or implement an alternative ordering strategy that correctly handles runs without jobs.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/trpc/src/routes/workflows.ts` around lines 165 - 178, The ordering
uses min(schema.job.createdAt) so workflow runs without jobs sort last; add a
createdAt timestamp to the workflowRun schema and migration, then update the
newestRunIds query to order using COALESCE between job.createdAt and
workflowRun.createdAt (e.g. orderBy(sql`coalesce(min(${schema.job.createdAt}),
${schema.workflowRun.createdAt}) desc nulls last`)) or simply order by
schema.workflowRun.createdAt desc; update the workflowRun schema (createdAt),
migration, and replace the orderBy in the newestRunIds query accordingly.

@adityachoudhari26 adityachoudhari26 merged commit 96a87ca into main May 18, 2026
16 checks passed
@adityachoudhari26 adityachoudhari26 deleted the fetch-newest-workflow-runs branch May 18, 2026 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: workflows ui only shows the first 100 runs

2 participants