Skip to content

feat(mothership): format generated function code#5742

Open
j15z wants to merge 4 commits into
stagingfrom
feat/function-code-formatting
Open

feat(mothership): format generated function code#5742
j15z wants to merge 4 commits into
stagingfrom
feat/function-code-formatting

Conversation

@j15z

@j15z j15z commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

Summary

Mothership-created JavaScript and Python Function blocks now arrive formatted and readable instead of being persisted as dense single-line source. Formatting runs after workflow operations resolve the final block identity and language, preserves Sim workflow and environment references, and fails open so invalid source remains unchanged while the mothership receives a sanitized formatting result.

This uses Prettier for JavaScript and Ruff's WebAssembly formatter for Python, keeping formatting deterministic and independent of how the mothership writes its tool-call strings.

The safety suite now exercises broad JavaScript and Python syntax corpora, formatting idempotence, exact workflow/environment-reference restoration, real placeholder-token collisions, high-cardinality reference sets, invalid-source fail-open behavior, and synchronous and asynchronous JavaScript runtime equivalence.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Other: ___________

Testing

  • DATABASE_URL=postgres://postgres:postgres@localhost:5432/sim bun test apps/sim/lib/workflows/blocks/format-function-code.test.ts apps/sim/lib/workflows/blocks/format-function-code.safety.test.ts apps/sim/lib/copilot/tools/server/workflow/edit-workflow/function-code-formatting.test.ts apps/sim/lib/copilot/tools/server/workflow/edit-workflow/operations.test.ts - 103 tests passed with 552 assertions in the clean staging-based worktree.
  • bunx biome check apps/sim/lib/workflows/blocks/format-function-code.test.ts apps/sim/lib/workflows/blocks/format-function-code.safety.test.ts apps/sim/lib/workflows/blocks/format-function-code.ts - passed.
  • bun run check:api-validation - passed with all 964 API routes Zod-backed and no baseline drift.
  • bun run --cwd apps/sim type-check - passed in the authored checkout. The isolated worktree reuses dependencies from a divergent checkout, so its full-project type-check surfaces unrelated missing audit exports and @google-cloud/storage; CI should validate this again from a fresh install.

Reviewers should focus on Sim-reference protection around comparison/shift syntax, the token-collision fixture, and Ruff WebAssembly tracing for standalone Next deployments.

No additional post-deploy monitoring is required for the safety-test follow-up because it changes no production runtime behavior.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

Screenshots/Videos

Not applicable - this changes server-side workflow code normalization and has no new visual surface.

@j15z
j15z requested a review from a team as a code owner July 17, 2026 20:15
@vercel

vercel Bot commented Jul 17, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Jul 18, 2026 2:19am

Request Review

@cursor

cursor Bot commented Jul 17, 2026

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes persisted workflow Function source via formatters; incorrect reference protection could alter executable code, though extensive tests and fail-open parsing mitigate this.

Overview
Copilot-generated Function blocks are normalized to readable JavaScript/Python after edit_workflow applies operations, instead of staying as dense one-liners.

The workflow engine tracks block IDs whenever a code input is submitted (add/edit, subflows, nested nodes), then runs formatChangedFunctionCode on that set using the final block language. Formatting uses Prettier (JS) and Ruff WASM (Python), with placeholder protection for <block.field> and {{ENV}} syntax so comparisons/shifts are not mistaken for references. Invalid source is left unchanged; failures are logged and returned as codeFormatting / codeFormattingMessage on the tool result without blocking the save.

Adds formatFunctionCode plus broad unit/safety tests, prettier and @wasm-fmt/ruff_fmt dependencies, and Next external/tracing config for the Python formatter.

Reviewed by Cursor Bugbot for commit b625977. Bugbot is set up for automated code reviews on this repo. Configure here.

@greptile-apps

greptile-apps Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR formats Function block code after workflow operations resolve the final block state. The main changes are:

  • Tracks Function blocks whose code was submitted by workflow operations.
  • Formats JavaScript with Prettier and Python with Ruff WASM.
  • Preserves workflow and environment references during formatting.
  • Reports formatting failures without blocking workflow persistence.
  • Adds formatter dependencies and standalone tracing configuration.

Confidence Score: 4/5

The Python async formatting path and server packaging for Prettier need fixes before merging.

  • Async Python bodies are rejected by the synthetic synchronous wrapper.
  • Some Next server builds can fail to load bundled Prettier, leaving JavaScript code unformatted.
  • Both failures preserve the original source, so workflow code is not corrupted.

apps/sim/lib/workflows/blocks/format-function-code.ts and apps/sim/next.config.ts

Important Files Changed

Filename Overview
apps/sim/lib/workflows/blocks/format-function-code.ts Adds reference-safe JavaScript and Python formatting, but the synchronous Python wrapper rejects async-only syntax.
apps/sim/lib/copilot/tools/server/workflow/edit-workflow/function-code-formatting.ts Formats tracked Function blocks from the final workflow state and preserves source when formatting fails.
apps/sim/lib/copilot/tools/server/workflow/edit-workflow/operations.ts Records submitted code across add, edit, nested-node, and subflow insertion operations.
apps/sim/lib/copilot/tools/server/workflow/edit-workflow/index.ts Runs formatting before validation and persistence and returns sanitized formatting results.
apps/sim/next.config.ts Adds Ruff externalization and asset tracing, but leaves the dynamically imported Prettier package on the bundled server path.

Reviews (1): Last reviewed commit: "feat(mothership): format generated funct..." | Re-trigger Greptile

Comment thread apps/sim/lib/workflows/blocks/format-function-code.ts
Comment thread apps/sim/next.config.ts

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want higher recall? High effort reviews run extra passes and find more bugs. A team admin can switch effort levels in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit a5e1c3d. Configure here.

Comment thread apps/sim/lib/workflows/blocks/format-function-code.ts Outdated
- Preserve Sim references adjacent to right-shift operators
- Add regression coverage for signed and unsigned shifts

Note: pre-existing type-check failures in audit exports and linked workspace dependencies were not addressed by this PR.
@j15z

j15z commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator Author

The Python async formatting path and server packaging for Prettier need fixes before merging.

Not addressing these two summary-level concerns because they do not reproduce against the current head:

  • The safety suite formats and stabilizes Python await, async for, and async with function bodies successfully. The synthetic wrapper is used only to give Ruff a function-body parse context; it is not the Python execution wrapper.
  • Prettier is deliberately listed in transpilePackages, so Next bundles it with the server code. Ruff is externalized and traced separately because its WebAssembly package carries runtime assets that must be copied into standalone output.

The formatter remains fail-open in either path, and the current focused suite passes all 103 tests.

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.

1 participant