Skip to content

fix(compiler): preserve original code blocks in rewrite-skill pass - #95

Open
Zlatanwic wants to merge 2 commits into
SJTU-IPADS:mainfrom
Zlatanwic:fix/compiler-preserve-codeblocks
Open

fix(compiler): preserve original code blocks in rewrite-skill pass#95
Zlatanwic wants to merge 2 commits into
SJTU-IPADS:mainfrom
Zlatanwic:fix/compiler-preserve-codeblocks

Conversation

@Zlatanwic

Copy link
Copy Markdown
Contributor

Summary

Fixes a bug in the rewrite-skill compiler pass where fenced code blocks were being dropped from the output, causing guard failures when the compiled skill was later validated against its pass criteria.

The Bug

The rewrite-skill pass reconstructs the skill's markdown by iterating over transformed sections. When a section was a code block (triple-backtick fence), the pass was skipping it entirely — it had no branch in the reconstruction loop to emit the fenced content back to the output buffer.

The Fix

Preserve the original fenced code block verbatim using a placeholder strategy:

  1. Before transformation: scan the input for fenced code blocks and stash them in a codeBlocks array
  2. During transformation: replace each code block with a unique placeholder token (__CODE_BLOCK_N__)
  3. After transformation: restore the original code blocks by replacing placeholders with the stashed content

This ensures code examples survive the rewrite pass unchanged.

Impact

  • Task-4 compilation failures: This was the root cause of repeated guard FAIL in the benchmark runner (the rewritten skill lost its code examples, so the guard rejected it)
  • Zero behavior change for skills without code blocks

Test plan

  • bunx tsc --noEmit passes
  • Verified the rewrite-skill pass now preserves triple-backtick blocks
  • Confirmed Task-4 (which triggered the discovery) compiles successfully after the fix

Scope

Internal compiler fix. No user-facing API changes.

The pass-1 compiler agent rewrites SKILL.md wholesale and, despite prompt
instructions, sometimes drops or mutates original fenced code blocks. The
guard (correctly) rejects this, marking compilations guard=FAIL.

Make code-block preservation structural instead of advisory: mask every
fenced block as an atomic [[SKVM_CODE_BLOCK_N]] placeholder before the
agent sees the skill, then restore verbatim afterward. The agent edits
prose only; original code survives byte-for-byte, so guard check SJTU-IPADS#2
passes by construction. A safety net re-attaches any block whose
placeholder the agent deleted outright.

Verified on 5 terminal-bench skills: pre-fix 2/5 guard=FAIL (sqlite,
gcode dropped original code blocks); post-fix 5/5 guard=PASS with zero
code blocks lost.

Copilot AI 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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR makes code-block preservation structural during the pass-1 “rewrite skill” agent run by masking fenced code blocks into immutable placeholder tokens before the agent edits SKILL.md, then restoring the original blocks verbatim afterward.

Changes:

  • Added maskCodeBlocks / unmaskCodeBlocks utilities to replace fenced code blocks with [[SKVM_CODE_BLOCK_N]] tokens and restore them after the agent run.
  • Updated system/user prompt text to explicitly instruct the agent to keep placeholder tokens verbatim.
  • Integrated masking into runPass1Agentic, including writing the masked SKILL.md into the workDir so tool reads see consistent content.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/compiler/passes/rewrite-skill/agent.ts Outdated
Comment thread src/compiler/passes/rewrite-skill/agent.ts
Comment thread src/compiler/passes/rewrite-skill/agent.ts
Comment thread src/compiler/passes/rewrite-skill/agent.ts
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