Skip to content

perf(workflows): reuse saved steps when writing dependencies - #581

Merged
artempartos merged 2 commits into
AixleHQ:developfrom
Hitin:feature/workflow-step-sync-n-plus-one
Oct 28, 2026
Merged

artempartos merged 2 commits into
AixleHQ:developfrom
Hitin:feature/workflow-step-sync-n-plus-one

Conversation

@Hitin

@Hitin Hitin commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Summary

PUT …/workflows/:id/aggregate (AggregatesController#update → WorkflowAggregateSave#apply! → WorkflowStepSync#sync!) re-read every step with dependencies via Step.find(id) in write_dependencies, even though write_steps had just saved those records. That's the N+1 Sentry reported (SELECT "steps".* FROM "steps" WHERE "steps"."id" = $1 ORDER BY "steps"."position" ASC LIMIT $2).

  • write_steps now returns key → Step. sync! builds key_map (key → id) from it, so the return value of sync! and the rewrite_draft_references / retire_others contracts stay the same.
  • write_dependencies calls update!(depends_on_step_ids:) on the in-memory record. This drops the Step.find and also the workflow lookup the validations made on a freshly found record: 2 fewer queries per step with dependencies.
  • Only depends_on_step_ids is written there. The in-memory instructions can still hold draft keys ({{step:new-1}}) that rewrite_draft_references already replaced via update_all. A partial update! doesn't touch them, and instruction_references_belong_to_project only runs when instructions change. The existing test "a reference to a step created in the same save…" covers this.

Out of scope: the pluck of sibling steps in Step#depends_on_step_ids_valid still runs once per step with dependencies. It's needed for the cycle and reference check, so changing it is a separate task.

The same path is used by workflow version revert (Versions::Snapshots::Workflow).

Related issues

Sentry PALAD-AI-RAILS-4C. Board task #23.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Refactor / chore
  • Breaking change

How was this tested?

New integration test in test/integration/api/v1/projects/workflows/aggregates_test.rb: it saves a dependency chain (Build ← new-1 ← Test) inside assert_no_queries_match(/FROM "steps" WHERE "steps"\."id" = .* LIMIT /) and checks the stored depends_on_step_ids. The pattern requires LIMIT, so it matches Step.find and does not match the update_all subquery or the validation pluck. On the old code it should fail on the two Step.find calls.

Tests were not run locally (no Ruby in this environment). Verification is left to CI.

Checklist

  • I read CONTRIBUTING.md.
  • All my commits are signed off for the DCO (git commit -s).
  • I have signed (or will sign) the CLA when the bot prompts me — or my
    organization has executed the Corporate CLA.
  • make check_all passes locally (tests, rubocop, brakeman, eslint, typescript). CI only.
  • I added/updated tests where it makes sense.
  • I updated documentation where it makes sense. Not needed.
  • User-visible changes have an entry under [Unreleased] in CHANGELOG.md.
  • My commits use clear, descriptive messages.

WorkflowStepSync#write_dependencies re-found every step with dependencies via
Step.find, although write_steps had just saved those records. write_steps now
returns key -> Step and the dependencies are updated on those records.

Fixes Sentry PALAD-AI-RAILS-4C.

Signed-off-by: aixle-developer-agent <[email protected]>
@Hitin
Hitin marked this pull request as ready for review October 27, 2026 07:19
* origin/develop:
  docs(workflows): describe workflows as sessions made of steps (AixleHQ#595)
  feat(workflows): open archived workflows read-only in the builder (AixleHQ#594)
  feat(task-comments): let authors delete their own recent comments (AixleHQ#593)
  feat(mcp): log the JSON-RPC method and tool name of each MCP call (AixleHQ#592)
  fix(board): show a run waiting for a session slot as Queued on its card (AixleHQ#591)
  feat(brand): use the new Aixle Flow lockup and route fonts through tokens (AixleHQ#590)
  feat(overview): show tokens used and session time instead of token cost (AixleHQ#589)
  fix(agents): pre-acknowledge Claude Code's cost-threshold notice (AixleHQ#587)
  test(playwright): QA helpers batch 2 (appRequest, readInertiaPage, shot, probeRoutes, signOut) (AixleHQ#586)
  test(playwright): plain-JS QA helpers for browser_run_code on staging (AixleHQ#584)
  feat(runners): add the runners table and the Runner model (AixleHQ#583)
  refactor(feature-flags): plain cached reads, no usage tooling (AixleHQ#579)

# Conflicts:
#	CHANGELOG.md
@artempartos

Copy link
Copy Markdown
Contributor

Finishing this in place before merge:

  • Merged the latest develop (merge commit, so no force-push and your commits keep their sign-off).
  • Dropped the CHANGELOG entry. Two fewer queries per dependent step is an internal performance fix with nothing a user would notice. CLAUDE.md gives those no entry, and fix(admin): preload company session limits on the companies index #523 skipped one for the same reason.

The code change and the test are unchanged.

@artempartos
artempartos merged commit 601b141 into AixleHQ:develop Oct 28, 2026
16 checks passed
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.

2 participants