Skip to content

save finalState for runs#4529

Closed
taylordowns2000 wants to merge 5 commits intomainfrom
4485-flexible-cron-input
Closed

save finalState for runs#4529
taylordowns2000 wants to merge 5 commits intomainfrom
4485-flexible-cron-input

Conversation

@taylordowns2000
Copy link
Member

@taylordowns2000 taylordowns2000 commented Mar 13, 2026

This PR saves finalState for a run, as a precursor to #4485.

When the worker sends run:complete, we now persist the run's final state by setting final_dataclip_id on the runs table. Previously final_state was only used for webhook response broadcasting, then discarded. This is a prerequisite for #4485 (using the run's final output as input for the next cron execution).

The handler accepts two mutually exclusive fields from the worker:

  • final_dataclip_id — reuse an existing step output dataclip (single-leaf workflows, no duplication)
  • final_state — a new map to persist as a separate dataclip (multi-leaf workflows with merged output)

Neither field is required — backward compatible with the current worker.

Changes

  • Migration: Adds nullable final_dataclip_id FK on runs → dataclips
  • Run schema: New belongs_to :final_dataclip; complete/2 casts the new field
  • CompleteRun handler: resolve_final_dataclip/2 either reuses an existing ID or inserts a new dataclip. Respects save_dataclips: false.
  • Run channel: Injects project_id into the run:complete payload so new dataclips can be associated with the correct project

Performance considerations

  • Single-leaf workflows (most common): Worker sends final_dataclip_id pointing to the last step's output dataclip. Zero additional storage, zero additional writes — just sets the FK.
  • Multi-leaf workflows: One extra dataclip INSERT per run. The merged body doesn't exist elsewhere, so this is net-new data.
  • Transaction scope: CompleteRun now wraps in Repo.transact. For the ID-reuse path this adds no extra queries. For the final_state path it's one INSERT + one UPDATE in a short transaction.

How to test

  1. Run any workflow — final_dataclip_id will be null until the worker is updated (backward compatible)
  2. After worker change: run a single-leaf workflow, verify final_dataclip_id equals the last step's output_dataclip_id (no duplicate)
  3. After worker change: run a branching workflow, verify final_dataclip_id points to a new dataclip with the merged output
  4. mix test test/integration/web_and_worker_test.exs

AI Usage

Please disclose whether you've used AI anywhere in this PR (it's cool, we just
want to know!):

  • I have used Claude Code
  • I have used another model
  • I have not used AI

You can read more details in our
Responsible AI Policy

Pre-submission checklist

  • I have performed an AI review of my code (we recommend using /review
    with Claude Code)
  • I have implemented and tested all related authorization policies.
    (e.g., :owner, :admin, :editor, :viewer)
  • I have updated the changelog.
  • I have ticked a box in "AI usage" in this PR

@github-project-automation github-project-automation bot moved this to New Issues in Core Mar 13, 2026
@codecov
Copy link

codecov bot commented Mar 13, 2026

Codecov Report

❌ Patch coverage is 78.57143% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.49%. Comparing base (5d4762c) to head (53fab9b).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
lib/lightning/runs/handlers.ex 76.92% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4529      +/-   ##
==========================================
- Coverage   89.55%   89.49%   -0.06%     
==========================================
  Files         425      425              
  Lines       20307    20318      +11     
==========================================
- Hits        18185    18184       -1     
- Misses       2122     2134      +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@taylordowns2000 taylordowns2000 marked this pull request as ready for review March 14, 2026 15:19
@taylordowns2000 taylordowns2000 changed the title All flexible "next cron input" for workflows save finalState for runs Mar 15, 2026
@taylordowns2000
Copy link
Member Author

@stuartc , easier to review this all as a single PR. closing in favor of #4531

@github-project-automation github-project-automation bot moved this from New Issues to Done in Core Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant