@@ -21,6 +21,7 @@ import { fileURLToPath } from "node:url";
2121const { source } = await import ( "@/lib/source" ) ;
2222const llms = await import ( "@/lib/llms" ) ;
2323const { getLLMText } = await import ( "@/lib/get-llm-text" ) ;
24+ const { protectFencedCodeBlocks, protectInlineCode } = await import ( "@/lib/llm-markdown" ) ;
2425const { withDocsBasePath } = await import ( "@/lib/urls" ) ;
2526const { agentSkillMarkdown } = await import ( "@/lib/agent-skill" ) ;
2627const { mcpDiscoveryDocument } = await import ( "@/lib/mcp-discovery" ) ;
@@ -368,6 +369,74 @@ if (mcpErrors.length > 0) {
368369 ) ;
369370}
370371
372+ // ── Check 10: placeholder protectors are collision-safe ──────────────────────
373+ // Regression coverage for the protect/restore pipeline shared by llm-markdown.ts
374+ // and get-llm-text.ts. Adversarial inputs embed text that LOOKS like the internal
375+ // placeholders inside inline code and fenced blocks, plus a normal body link. A
376+ // correct implementation restores every input byte-for-byte and rewrites ONLY the
377+ // body links that sit outside code. This guards against the old sequential-replace
378+ // corruption (a protected span reintroducing a later token; the fenced restore
379+ // consuming a sentinel reintroduced by an inline span).
380+ {
381+ // Mirror absolutizeInBodyLinks exactly: fences first, then inline spans, rewrite
382+ // the body links in between, then restore inline then fences.
383+ const runPipeline = ( markdown : string ) => {
384+ const fences = protectFencedCodeBlocks ( markdown ) ;
385+ const inline = protectInlineCode ( fences . markdown ) ;
386+ const rewritten = inline . markdown . replace ( / \] \( ( \/ [ ^ ) \s ] * ) \) / g, ( full , target : string ) => {
387+ if ( target . startsWith ( "//" ) ) return full ;
388+ return `](https://www.prisma.io${ target } )` ;
389+ } ) ;
390+ return fences . restore ( inline . restore ( rewritten ) ) ;
391+ } ;
392+
393+ const fence = "```" ;
394+ const cases : { name : string ; input : string ; expected : string } [ ] = [
395+ {
396+ name : "inline code containing old-format placeholder text is preserved" ,
397+ input : "Use `__LLM_INLINE_CODE_1__` and `__LLM_FENCED_CODE_BLOCK_0__` literally here." ,
398+ expected : "Use `__LLM_INLINE_CODE_1__` and `__LLM_FENCED_CODE_BLOCK_0__` literally here." ,
399+ } ,
400+ {
401+ name : "fenced block containing inline-sentinel-looking text + body link is preserved" ,
402+ input : `${ fence } \n__LLM_INLINE_CODE_0__ and [x](/orm/a)\n${ fence } ` ,
403+ expected : `${ fence } \n__LLM_INLINE_CODE_0__ and [x](/orm/a)\n${ fence } ` ,
404+ } ,
405+ {
406+ name : "link inside inline code is NOT rewritten" ,
407+ input : "See `[label](/orm/foo)` inline." ,
408+ expected : "See `[label](/orm/foo)` inline." ,
409+ } ,
410+ {
411+ name : "body link outside code IS rewritten" ,
412+ input : "See [label](/orm/foo) here." ,
413+ expected : "See [label](https://www.prisma.io/orm/foo) here." ,
414+ } ,
415+ {
416+ name : "mixed: body link rewrites while placeholder-looking code stays verbatim" ,
417+ input : `Body [a](/x) then \`__LLM_FENCED_CODE_BLOCK_0__\` then\n${ fence } \ncode [b](/y)\n${ fence } \nend.` ,
418+ expected : `Body [a](https://www.prisma.io/x) then \`__LLM_FENCED_CODE_BLOCK_0__\` then\n${ fence } \ncode [b](/y)\n${ fence } \nend.` ,
419+ } ,
420+ ] ;
421+
422+ const protectorFailures = cases . flatMap ( ( testCase ) => {
423+ const actual = runPipeline ( testCase . input ) ;
424+ if ( actual === testCase . expected ) return [ ] ;
425+ return [
426+ `${ testCase . name } \n expected: ${ JSON . stringify ( testCase . expected ) } \n actual: ${ JSON . stringify ( actual ) } ` ,
427+ ] ;
428+ } ) ;
429+
430+ if ( protectorFailures . length > 0 ) {
431+ fail (
432+ "Protector round-trip" ,
433+ `${ protectorFailures . length } of ${ cases . length } case(s) failed:\n ${ protectorFailures . join ( "\n " ) } ` ,
434+ ) ;
435+ } else {
436+ pass ( "Protector round-trip" , `all ${ cases . length } adversarial protect/restore cases pass` ) ;
437+ }
438+ }
439+
371440// ── Report ───────────────────────────────────────────────────────────────────
372441const icon : Record < Status , string > = { pass : "✓" , warn : "⚠" , fail : "✗" } ;
373442console . log ( "\nAgent-readiness checks\n" ) ;
0 commit comments