Skip to content

feat(mothership): format generated function code#5742

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

feat(mothership): format generated function code#5742
j15z wants to merge 5 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 or unverifiable 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.

Formatted output is accepted only when Sim references remain identical and ordered, JavaScript has an equivalent Babel syntax tree, and a second formatting pass is byte-stable. Parenthesized environment expressions retain grouping through raw resolver substitution, including comment-bearing expressions; otherwise the original Function source is kept.

The safety suite 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, real VariableResolver integration, JavaScript runtime equivalence, and independent CPython AST and runtime equivalence.

Type of Change

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

Testing

  • DATABASE_URL=postgres://postgres:postgres@localhost:5432/sim bunx vitest run lib/workflows/blocks/format-function-code.test.ts lib/workflows/blocks/format-function-code.safety.test.ts lib/workflows/blocks/format-function-code.validation.test.ts lib/workflows/blocks/format-function-code.integration.test.ts lib/copilot/tools/server/workflow/edit-workflow/function-code-formatting.test.ts from apps/sim - 110 tests passed.
  • DATABASE_URL=postgres://postgres:postgres@localhost:5432/sim bun test apps/sim/lib/copilot/tools/server/workflow/edit-workflow/operations.test.ts - 14 tests passed with 45 expectations; 124 relevant tests passed in total.
  • bunx biome check across the six formatter implementation/test files - passed.
  • bun run check:utils - passed with no banned patterns.
  • bun run check:api-validation - passed with all 964 API routes Zod-backed and no baseline drift.
  • bun build apps/sim/lib/workflows/blocks/format-function-code.ts --target=node --outdir /tmp/sim-function-formatter-bundle --external @wasm-fmt/ruff_fmt/node - passed with Prettier's Babel parser bundled.
  • bun run --cwd apps/sim type-check - the authored formatter files emitted no errors; the isolated worktree still surfaces unrelated linked-dependency drift for two audit constants, recordAuditBatch, and @google-cloud/storage. CI should validate from a fresh dependency install.

Reviewers should focus on parenthesized environment-expression protection, Sim-reference ordering, the JavaScript AST normalization boundary, the independent CPython oracle, and Ruff WebAssembly tracing for standalone Next deployments.

Post-deploy monitoring and validation: for 24 hours after deployment, the Workflow/Mothership owner should watch edit_workflow error rate and latency and search logs for Function code formatting failed open, grouped by language and block ID. Healthy behavior is stable edit latency/error rate with warnings limited to genuinely invalid source; a sustained warning increase, latency regression, or any behavior-changing formatted Function should trigger rollback of this change while fail-open keeps original source intact.

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 7:08am

Request Review

@cursor

cursor Bot commented Jul 17, 2026

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches persisted workflow code and adds formatter dependencies on the edit_workflow server path; mitigated by fail-open behavior, reference protection, and extensive tests, but wrong masking could still alter execution semantics.

Overview
Copilot edit_workflow now auto-formats JavaScript and Python Function block bodies after operations are applied, so mothership-generated code is persisted in readable form instead of dense single-line strings.

The workflow engine tracks block IDs whenever a code input is submitted (add, edit, nested nodes, subflow insert). After applyOperationsToWorkflowState, formatChangedFunctionCode runs only on those blocks, using the final language and resolved IDs. The tool response can include codeFormatting (changedBlockIds, failures) and a user-facing message when formatting fails open.

New formatFunctionCode (Prettier for JS, Ruff WASM for Python) masks Sim workflow references (<block.field>) and env vars ({{VAR}}) before formatting, then restores them with guards so comparisons/shifts are not mistaken for references. Invalid or unsafe output fails open (original code kept); errors are logged and surfaced without blocking the workflow save.

Dependencies: prettier, @wasm-fmt/ruff_fmt, with Next external/tracing updates for the WASM formatter. Broad unit, safety, integration, and edit-workflow tests cover reference preservation, idempotence, and tracking behavior.

Reviewed by Cursor Bugbot for commit 58209d5. 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