Skip to content

fix(wand): stop markdown code fences landing in generated code - #6289

Merged
waleedlatif1 merged 6 commits into
stagingfrom
fix/wand-markdown-code-fences
Aug 5, 2026
Merged

fix(wand): stop markdown code fences landing in generated code#6289
waleedlatif1 merged 6 commits into
stagingfrom
fix/wand-markdown-code-fences

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Strip markdown code fences from wand output for raw-value generation types. Only applies when the response opens with a fence, so code that merely contains a backtick run in a template literal or docstring is never touched
  • Reset the wand conversation history when a Function block switches language. maintainHistory was replaying the previous language's assistant turn, which kept steering generations back to it
  • Feed the sanitized text into history so one fenced reply can't become the in-context example for every later turn
  • Give PYTHON_AI_PROMPT the worked example the JavaScript prompt already had, and align its import rule with the sandbox package list
  • Reinforce the no-markdown instruction server-side for javascript-function-body, matching what cron-expression and json-object already do

Two known non-goals: a response that starts with prose before the fence, and mid-stream display (the editor may briefly show a fence before the final value replaces it). Both are deliberate — per-chunk stripping would mean guessing whether a trailing backtick run opens a fence or is part of the code.

Type of Change

  • Bug fix

Testing

15 unit tests on the sanitizer covering full wrap, missing closing fence, untagged fence, indentation preservation, multi-fence, idempotence, and the must-not-touch cases. Verified the tests fail when the util is stubbed out. Full suite green (18,597 passing), typecheck, biome, and check:api-validation all clean.

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)

@vercel

vercel Bot commented Aug 5, 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 Aug 5, 2026 6:11pm

Request Review

@cursor

cursor Bot commented Aug 5, 2026

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes wand output handling and multi-turn history for workflow code generation; logic is guarded by generation type and covered by unit tests, but incorrect stripping could still corrupt user code in edge cases.

Overview
Fixes wand (AI code generation) so markdown fences and stale chat history no longer corrupt function bodies and other raw-value outputs.

Fence stripping: New stripCodeFences / shouldStripCodeFences run after the stream completes for machine-value generation types (function bodies, JSON, cron, SQL, etc.), not free-form system-prompt. Stripping only when the response opens with a fence avoids breaking code that legitimately contains backticks in template literals or docstrings. Sanitized text is what gets written to the editor and stored in wand history so one fenced reply cannot anchor later turns.

History reset: useWand accepts historyResetKey (the Function block passes language). Changing it clears conversation history and uses an epoch so in-flight requests from the old context cannot append after a reset—fixing generations that kept reverting to the previous language when maintainHistory was on.

Prompts: The wand API adds server-side “raw function body only, no markdown” reinforcement for javascript-function-body. The Python AI prompt gains sandbox-aware import rules, a worked example, and explicit “no prose” output rules aligned with JavaScript.

Removed: onGenerationComplete from useWand (unused after sanitization moved into the hook).

Reviewed by Cursor Bugbot for commit 68e1192. Configure here.

@greptile-apps

greptile-apps Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR sanitizes markdown-fenced Wand responses, resets generation history when Function blocks switch languages, and strengthens raw-code prompting.

  • Adds final-response fence stripping for machine-readable generation types.
  • Prevents retired language contexts from being appended to conversation history.
  • Expands the Python generation prompt and reinforces raw function-body output server-side.

Confidence Score: 4/5

The PR does not appear safe to merge until the outstanding sanitizer path that removes a valid trailing embedded fence is resolved.

The current sanitizer still unconditionally removes a final fence-like line, so a response truncated immediately after a delimiter embedded in generated code is persisted in corrupted form.

Files Needing Attention: apps/sim/lib/wand/strip-code-fences.ts and apps/sim/lib/wand/strip-code-fences.test.ts

Important Files Changed

Filename Overview
apps/sim/lib/wand/strip-code-fences.ts Introduces conservative outer-fence removal while retaining the previously reported corruption when a truncated embedded delimiter is the final line.
apps/sim/lib/wand/strip-code-fences.test.ts Covers wrapped, truncated, nested, multi-block, and untouched output cases, including the acknowledged trailing-delimiter behavior.
apps/sim/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-wand.ts Sanitizes completed output and uses an epoch guard to keep retired language turns out of conversation history.
apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/code/code.tsx Keys Wand history resets to Function block language changes and expands Python generation guidance.
apps/sim/app/api/wand/route.ts Adds language-neutral server-side instructions requiring raw function-body output.

Reviews (6): Last reviewed commit: "test(wand): record the trailing-fence am..." | Re-trigger Greptile

Comment thread apps/sim/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-wand.ts Outdated
Comment thread apps/sim/lib/wand/strip-code-fences.ts Outdated
@waleedlatif1
waleedlatif1 force-pushed the fix/wand-markdown-code-fences branch from 3742f1b to 2a70483 Compare August 5, 2026 17:44
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/lib/wand/strip-code-fences.ts Outdated
Strip fences from wand output for raw-value generation types, reset the
conversation history when a Function block switches language, and give the
Python prompt the worked example the JavaScript one already had.
Slice only the outermost fence delimiters so a fenced body containing
line-leading backticks keeps every interior line, and skip the history
append when a language reset retired the request mid-flight.
The trimStart check made the -1 branch dead and stated "opens with a
fence" twice. Derive it once from the first fence line's position.
@waleedlatif1
waleedlatif1 force-pushed the fix/wand-markdown-code-fences branch from 2a70483 to fb8337a Compare August 5, 2026 17:54
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/lib/wand/strip-code-fences.ts Outdated

@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.

✅ Bugbot reviewed your changes and found no new issues!

2 issues from previous reviews remain unresolved.

Fix All in Cursor

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit fb8337a. Configure here.

No call site ever passed it, so the branch never ran. The props interface
makes the removal compile-time verified.
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

@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.

✅ Bugbot reviewed your changes and found no new issues!

2 issues from previous reviews remain unresolved.

Fix All in Cursor

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit c89adb2. Configure here.

A truncated response whose body embeds line-leading backticks lost every
line after the first embedded delimiter. Only the opening line and a
final fence line are removed now. Sync the history epoch in a layout
effect so a request settling before the passive flush cannot append to
already-reset history.
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

@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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 5985357. Configure here.

Comment thread apps/sim/lib/wand/strip-code-fences.ts Outdated
A bare fence on the last line closes the wrapper in every well-formed
response and is content only when generation stopped exactly on an
embedded delimiter. Nothing separates the two, so assert the chosen
behavior instead of leaving it implicit.
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

@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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 68e1192. Configure here.

@waleedlatif1
waleedlatif1 merged commit 2d90c72 into staging Aug 5, 2026
30 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/wand-markdown-code-fences branch August 5, 2026 18:29
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