Skip to content

Fence JSON examples in skill guidance so they seed as code-block nodes (closes #1965) - #1972

Merged
malibio merged 1 commit into
mainfrom
issue-1965-skill-guidance-fences
Aug 4, 2026
Merged

malibio merged 1 commit into
mainfrom
issue-1965-skill-guidance-fences

Conversation

@malibio

@malibio malibio commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Closes #1965.

What

Skill guidance is seeded through a genuine markdown import (prepare_nodes_from_template → prepare_nodes_from_markdown), which turns ``` fences into code-block nodes. The guidance source text never used fences, so its worked JSON examples were absorbed into prose text nodes.

The gap was worse than "flattened into prose"

Confirmed by dumping the rendered guidance before and after. Because an unfenced example lands in a text paragraph — whose continuation lines are trim_start()ed — the JSON came back from the round-trip with all indentation stripped, every line flushed to column 0:

{
"node_type": "widget",
"content": "Shipment 24",
"properties": {
"label": "Shipment 24",

With fences, the block round-trips verbatim (flatten_subtree_content re-emits each node's content as stored):

{
  "node_type": "widget",
  "content": "Shipment 24",
  "properties": {
    "label": "Shipment 24",

So AC #3 ("no regression in prompt rendering") is met with room to spare: same content, same tokens, correctly indented, plus explicit example boundaries.

Changes

  • Fenced the Node Creation call-shape example and both Schema Creation schema examples as ```json.
  • Inline code spans for short inline call shapes (search_nodes(...), enum and relationship fragments) — matching what the sibling prose variants of the same skill_rules constants already do.
  • Widened seed_skill_children_are_real_markdown_types_not_prompt to admit code-block (it previously whitelisted only header | text and would have failed).
  • Added seed_skill_json_examples_produce_code_block_children, asserting the expected code-block children appear, keep their ```json fence verbatim, and parse as valid JSON via serde_json.

Verification

  • cargo test -p nodespace-agent --lib — 467 passed.
  • cargo test -p nodespace-core --lib — 1133 passed.
  • gen_skill_md --check — SKILL.md up to date. It renders the prose variants; only imperative variants were touched, so there is no drift.
  • Full pre-push gate (test:all + cargo build + e2e) passed.
  • Baseline was 180 files / 4301 frontend tests passing; unchanged. architecture-benchmarks.test.ts and navigation-service.test.ts flaked once under full-suite parallel load and both pass in isolation — this change is Rust-only and cannot affect them.

Acceptance criteria

  • All JSON worked examples across the guidance functions use proper ```json fences.
  • Test confirms code-block nodes are produced where expected.
  • No regression in prompt rendering — verified by before/after diff of the rendered text.

🤖 Generated with Claude Code

closes #1965)

Skill guidance is seeded through a real markdown import
(prepare_nodes_from_template -> prepare_nodes_from_markdown), which turns
``` fences into code-block nodes. The guidance source never used fences, so
its worked JSON examples were absorbed into prose text nodes instead.

That was not only a structural loss. Because an unfenced example lands in a
text paragraph, whose continuation lines are trim_start()ed, the JSON came
back from the round trip with all indentation stripped and every line
flushed to column 0. Fencing restores the examples verbatim, indentation
included, so the model-facing text is unchanged in content and strictly
better formatted.

- Fence the Node Creation call-shape example and both Schema Creation
  schema examples as ```json.
- Use inline code spans for short inline call shapes (search_nodes(...),
  enum/relationship fragments), matching what the sibling `prose` variants
  of the same rules already do.
- Widen seed_skill_children_are_real_markdown_types_not_prompt to admit
  code-block, and add a test asserting the expected code-block children
  appear, keep their ```json fence verbatim, and parse as valid JSON.

SKILL.md is unaffected: gen_skill_md renders the `prose` variants, and
`--check` reports it up to date.

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review: PR #1972 — Fence JSON examples in skill guidance

Review Type: Initial review (no prior reviews/comments on this PR)
Scope reviewed: commit e89034ab only — origin/main has advanced past this branch's base (#1971), so the ai-chat/OCC changes in origin/main...HEAD are explicitly excluded.
Files: packages/agent/src/skill_pipeline.rs, packages/agent/src/skill_rules.rs (+76 / −11)


Summary

This is a tightly scoped, well-diagnosed fix. The issue framed it as a cosmetic markdown-hygiene problem; the author found the real defect underneath — unfenced JSON was not merely "unstructured", it was being corrupted on round-trip. I independently verified that claim rather than taking it on faith, and it holds (evidence below). The change is a net improvement to the model-facing prompt with no meaning change, verified test coverage, and no SKILL.md drift.

Verdict: APPROVE. No blocking findings. Two minor observations below, neither worth holding the merge.


Requirements Check (issue #1965)

AC 1 — "All JSON worked examples across the six guidance functions use proper ```json fences." — ✅ MET

I audited every *_guidance() function plus both inline seed_skill_nodes() blocks named in the issue. Multi-line JSON exists in exactly two places, and both are now fenced:

Source Multi-line JSON? Status
schema_creation_guidance() (📁 skill_pipeline.rs:36) Yes — Invoice + Project examples ✅ both fenced
graph_editing_guidance() (📁 :132) No n/a
relationship_management_guidance() (📁 :162) No n/a
node_deletion_guidance() (📁 :181) No n/a
bulk_import_guidance() (📁 :200) No n/a
organization_guidance() (📁 :219) No n/a
seed_skill_nodes() Research & Search block (📁 :~255) No — single-line call shapes only ✅ inline code spans
seed_skill_nodes() Node Creation block (📁 :~329) Yes — call-shape example ✅ fenced

Nothing was missed. The four guidance functions with no JSON genuinely have none — they are prose-plus-interpolated-rules only. The single-line call shapes in Research & Search correctly got inline code spans rather than fences, which is the right call: a fenced block per bullet would have fragmented a list into eight code-block siblings for no benefit.

The issue also asked to check that header-based visual grouping isn't accidentally converted into spurious child splits. The diff touches no #/## lines, so that risk is not introduced here.

AC 2 — "seed_skill_children_are_real_markdown_types_not_prompt (or an extended version) confirms code-block nodes are produced." — ✅ MET, and exceeded

The author did both halves properly: widened the existing test to admit the new legitimate type, and added a dedicated positive assertion (seed_skill_json_examples_produce_code_block_children, 📁 skill_pipeline.rs:733). See the test-quality section — I mutation-tested it.

AC 3 — "No regression in how the guidance renders as LLM prompt text." — ✅ MET (verified empirically, not assumed)

This is the AC that actually mattered, since this repo has hard-won memory that skill guidance wording is load-bearing and that prompt changes have measurably moved agent behavior. I did not take the commit message's claim on trust. I built a throwaway probe that drives the real prepare_nodes_from_template and reconstructs what render_skill_instructions → flatten_subtree_content (📁 packages/core/src/services/node_service/hierarchy.rs:959) would emit, for both the fenced source and a programmatically de-fenced reconstruction of the old source, then diffed them. Result:

Old (unfenced) — Node Creation, as the model actually received it:

{
"node_type": "widget",
"content": "Shipment 24",
"properties": {
"label": "Shipment 24",
...
}
}

New (fenced):

{
  "node_type": "widget",
  "content": "Shipment 24",
  "properties": {
    "label": "Shipment 24",
    ...
  }
}

The empirical claim is confirmed. Every line was previously flushed to column 0 — the paragraph parser trim_start()s continuation lines, so an unfenced example loses all nesting structure before the model ever sees it. Same for both Schema Creation examples. So the change is not neutral-at-best: it is a strict improvement in the prompt, restoring indentation that was silently being destroyed. Token content is otherwise byte-identical — no wording, ordering, or semantic change. The only additions to the prompt stream are the fence markers themselves and backtick pairs, both of which are conventional signals an LLM reads as "this is code", which is exactly the intended reading.

Worth recording for future prompt-eval work: this is a case where the rendered prompt changed even though nobody edited the prose. Anyone re-baselining agent eval numbers against pre-#1965 runs should know the Node/Schema Creation prompts materially improved here.


Code Review Findings

🟢 Brace escaping is correct — verified by execution, not inspection

The trap flagged in this area is real: schema_creation_guidance() is a format! over a raw string where every literal brace must be doubled ({{/}}), while the Node Creation block is a plain r#""# literal where braces are literal. Getting this wrong yields either a compile error or, worse, silently malformed JSON in the prompt.

📁 skill_pipeline.rs:66-113 and :332-344 — no escaping mistakes were introduced. I confirmed this the strong way rather than by eye: the new test parses each emitted fenced block through serde_json::from_str::<serde_json::Value> after stripping the fence, and it passes. That means the post-format!, post-markdown-round-trip JSON is genuinely well-formed — which also transitively proves the {{→{ unescaping produced valid output. Good test design; this is the assertion that makes the whole class of brace bugs unable to land silently.

🟢 New test is meaningful, not a restatement of the implementation — mutation-verified

📁 skill_pipeline.rs:733-775. I checked the obvious failure mode for a test like this (does it merely echo what the code does?) by mutating the source: I removed the Node Creation fence and re-ran it. It failed, with a diagnostic that names the actual root cause:

Skill 'Node Creation' should produce 1 'code-block' child(ren) from its fenced
JSON example(s), got 0. Unfenced JSON is parsed as prose instead.

That is a genuinely useful failure message — a future contributor who reflows this guidance and drops a fence gets told what broke and why it matters, not just a count mismatch. The test asserts three independent properties (count, fence preserved verbatim, body parses as JSON), each of which fails independently. This is real coverage.

On the hardcoded counts (Node Creation: 1, Schema Creation: 2): I'd call these an appropriate pin rather than a maintenance hazard. The alternative — asserting "at least one code-block" — would not catch the realistic regression, which is losing one of the two Schema Creation fences while the other still passes. The counts are two small integers adjacent to a message explaining what they mean, and the cost of updating them is one line if someone deliberately adds a third example. Pinning exact counts is the right trade here.

🟢 Widening seed_skill_children_are_real_markdown_types_not_prompt does not weaken its guard

📁 skill_pipeline.rs:710-717. The concern would be that adding a third accepted variant erodes the test's purpose. It doesn't: that test's stated job is proving children are real markdown types, not the retired prompt type, and code-block is precisely such a real markdown type — the same parser produces it. prompt, and any other unexpected type, still fails the matches!. The guard's discriminating power against the regression it was written for is unchanged. Crucially, the author didn't just relax the constraint and walk away — the newly-admitted type is now positively asserted by the companion test, so code-block moved from "unchecked" to "checked more strictly than before". That's the correct way to widen an allowlist.

🟢 Inline code spans align the two rule variants

📁 skill_rules.rs:101 (ENUM_FORMAT) and 📁 skill_rules.rs:113 (TARGET_TYPE_MUST_EXIST). The imperative variants now use backticks for inline JSON fragments, matching what the sibling prose variants already did. This reduces the gratuitous divergence between two strings that are supposed to say the same thing in different registers — a small DRY/consistency win on a pair that is explicitly maintained in lockstep.

🟢 SKILL.md correctly needed no regeneration

Verified rather than assumed: cargo run -p nodespace-agent --bin gen_skill_md -- --check exits 0 with "is up to date". gen_skill_md renders the prose variants, and this PR only edited imperative variants plus skill_pipeline string literals that gen_skill_md doesn't consume. The commit message's claim is accurate. No companion-file drift.

🟡 Nit: inconsistent blank line after the closing fence

📁 skill_pipeline.rs:344 (Node Creation) puts a blank line after the closing ``` before "Never reuse...", while 📁 skill_pipeline.rs:113 (Schema Creation, Project example) ends the raw string immediately at ``` with no trailing newline. Both round-trip correctly — I confirmed the flattened output is well-formed in both cases, since flatten_subtree_content joins with \n\n regardless — so this is purely source-file consistency, not a behavior difference. Not worth a commit on its own.

🟡 Nit / observation for a future issue (pre-existing, not introduced here)

My round-trip probe surfaced something adjacent that this PR does not cause and should not be asked to fix: markdown list bullets are also lossy through the same path. - "list all tasks" → ... comes back as "list all tasks" → ... with the - marker stripped, and each bullet becomes its own node joined by a blank line, so a tight list renders to the model as loose paragraphs. Same root cause family as the indentation loss this PR fixes.

This is out of scope for #1965 and I would not expand the PR. But it's the natural follow-up if anyone pursues #1958's structural-sync work, and it's worth capturing as its own issue while the diagnosis is fresh — the list-flattening is arguably a bigger prompt-fidelity loss than the indentation one, since it affects the Research & Search bullets that this PR just touched.


Verification Performed

Check Result
cargo test -p nodespace-agent (full crate) ✅ 467 passed, 0 failed
cargo test -p nodespace-agent --lib skill ✅ 40 passed, incl. both changed/new tests
Mutation test: remove a fence → new test fails? ✅ fails with a clear, causal message
Round-trip prompt diff, fenced vs unfenced ✅ indentation-loss claim confirmed; no content change
Fenced JSON validity post-format! ✅ serde_json parses both blocks
gen_skill_md -- --check ✅ up to date
cargo fmt --check / cargo clippy --all-targets ✅ clean, no new warnings, no lint suppression
Primary checkout + worktree git status ✅ both clean (probe removed)

Not attributed to this PR: the pty::manager and frontend architecture-benchmarks / navigation-service failures are known load-sensitive flakes in this repo and cannot be affected by a Rust-only change to two files in packages/agent.


Recommendation: ✅ APPROVE

Atomic, single-purpose, correctly diagnosed, and verified end-to-end. The author went past the issue's stated ask — which was essentially "make the markdown tidy" — and identified that the untidy markdown was actively corrupting the model-facing prompt, then proved it with a test that fails for the right reason. Both nits are optional and neither should block. Ship it.

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Verdict: APPROVE (posted via --comment because GitHub blocks self-approval on your own PR.)

Full review: #1972 (comment)

All 3 acceptance criteria from #1965 are met. Completeness audited across all six *_guidance() functions plus both inline seed_skill_nodes() blocks — multi-line JSON exists in exactly two places and both are fenced; the other four functions genuinely contain no JSON.

AC #3 was verified empirically rather than assumed: I reconstructed the render_skill_instructions → flatten_subtree_content round trip for both the fenced and de-fenced sources and diffed the model-facing text. The indentation-loss finding is confirmed — unfenced JSON was previously reaching the model with every line flushed to column 0. This change is a strict improvement to the prompt, with no wording or semantic drift.

Also verified: brace escaping is correct in the format! block (proven by the new test's serde_json parse, not by inspection); the new test is mutation-sensitive (removing a fence fails it with a causal message); widening the not_prompt test does not weaken its guard, since the newly-admitted code-block type is now positively asserted elsewhere; and gen_skill_md --check confirms SKILL.md needed no regeneration.

cargo test -p nodespace-agent — 467 passed, 0 failed. fmt + clippy clean.

Two 🟡 nits only, neither blocking: a cosmetic blank-line inconsistency after one closing fence, and a pre-existing (not introduced here) list-bullet flattening loss in the same round-trip path that's worth its own follow-up issue rather than scope creep on this PR.

@malibio

malibio commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator Author

Address Review Summary

Review verdict was APPROVE with zero 🔴 Critical and zero blocking findings — five 🟢 confirmations and two 🟡 nits, both explicitly marked optional by the reviewer. Ran /address-review anyway per project workflow rather than pre-judging that there was nothing to do.

Result: no code changes. 0 commits. Both nits resolved as no-op, one by filing a follow-up issue.

⏭️ Skipped: 🟡 Nit — inconsistent blank line after closing fence

Skipped: the premise is incorrect — there is no inconsistency. The finding compares two fences that are in structurally different positions:

  • skill_pipeline.rs:344 (Node Creation) — blank line after the fence is required, because prose follows it (Never reuse "widget"...). Without it the paragraph would fuse to the fence.
  • skill_pipeline.rs:113 (Schema Creation, Project) — the fence is the final content of the raw string (```"#,). Nothing follows it. Adding a blank line would append a stray trailing newline to the guidance for no reason.

Each is already correct for its own context; they differ because the contexts differ, not because one is wrong. "Making them consistent" would mean making one of them worse. The reviewer reached the same practical conclusion ("purely source-file consistency... not worth a commit on its own") — I'm additionally noting the stated inconsistency isn't real.

⏭️ Skipped here → ✅ filed as #1974: 🟡 List bullets are lossy through the same round-trip

Verified the claim before acting on it, rather than taking it on faith. Confirmed:

  • Source: - Use 'collection' to narrow search...
  • Rendered to model: Use 'collection' to narrow search... — - marker stripped, and each bullet joined by \n\n, so tight lists become loose paragraphs.

This is real and is the same root-cause family as the indentation loss this PR fixes: flatten_subtree_content re-emits node.content verbatim, and bullet markers (unlike code fences) are not preserved inside content.

It is pre-existing, not introduced by this PR, affects every guidance block rather than anything this diff touched, and fixing it would change the model-facing prompt for all skills — which needs measurement, not a drive-by edit. Both the reviewer and I agree it should not expand this PR.

Captured with full diagnosis as #1974, cross-referenced to #1958.

Verification

No code changed, so the pre-push gate result from the reviewed commit e89034ab still stands: agent lib 467 passed, core lib 1133 passed, gen_skill_md --check up to date, fmt + clippy clean. Working tree clean.

Re-Review Decision

NO RE-REVIEW NEEDED — zero lines of code changed since the review. The only action taken was filing a follow-up issue for an out-of-scope, pre-existing defect. There is no new diff for a reviewer to examine.

⚠️ Note for the human merger: the APPROVE on this PR was produced by an automated reviewer on a PR authored by the same automated session, posted via --comment because GitHub blocks literal self-approval. It has not had independent human review. Please give it your own eyes before merging.

@malibio
malibio merged commit fd8c292 into main Aug 4, 2026
2 checks passed
@malibio
malibio deleted the issue-1965-skill-guidance-fences branch August 4, 2026 22:03
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.

Skill seed guidance text should use proper markdown (fenced code blocks) for JSON examples

1 participant