← Back to Pull requests

Fix stale markdown preview after reopening document #332081

Open
dkontyko opened this pull request with 20 lines diff across 2 files
View on GitHub
dkontyko dkontyko commented on Aug 22, 2026 03:29 UTC
Author

Fixes #331900

Summary

Markdown token caching currently identifies documents by URI and TextDocument.version. Because versions are scoped to a document instance and can restart after a file is closed and reopened, a new document can collide with cached tokens from the previous instance and render stale preview content.

Cache tokens by document identity in addition to the existing version and Markdown configuration checks. This preserves cache hits during the lifetime of an open document while preventing reuse across reopened document instances.

A regression test renders two document instances with the same URI and version but different content and verifies that the reopened document is tokenized again.

Testing

  • TypeScript compilation passed on the same patch.

  • Focused regression test passed in the VS Code Insiders extension host.

  • Complete markdown.engine test file passed (10 tests).

  • git diff --check passed.
  • Diff: 20 lines (+17 / -3) across 2 files Reviewer: dianatofficial Review duration: 72606m
    All Vouch review confidence checks passed
    Residual risk 0.04 = Change Risk (0.30) × (1 − Review Confidence (0.88)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.30 · 20 lines diff across 2 files
    0.30
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.97 · 2 substantive comment(s)
    0.97
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    88%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.182
    Real PR Conversation Timeline
    5 items
    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Aug 22, 2026 03:32 UTC
    Reviewer

    Pull request overview

    Prevents stale Markdown previews by tying token cache entries to a specific document instance.

    Changes:

  • Cache tokens using document identity and version.

  • Add regression coverage for reopened documents with colliding URIs and versions.
  • <details open>
    <summary>Show a summary per file</summary>

    | File | Description |
    | ---- | ----------- |
    | extensions/markdown-language-features/src/markdownEngine.ts | Prevents cache reuse across document instances. |
    | extensions/markdown-language-features/src/test/engine.test.ts | Tests reopening with identical URI/version but changed content. |
    </details>

    Review details

    💡 <a href="/microsoft/vscode/new/main?filename=.github/skills/code-review/SKILL.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add a code-review agent skill</a> or configure MCP servers for context-aware, tailored reviews. <a href="https://docs.github.com/en/copilot/how-tos/use-copilot-agents/request-a-code-review/use-code-review#mcp-servers-and-agent-skills" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn more in the docs.</a>

  • Files reviewed: 2/2 changed files

  • Comments generated: 0

  • Review effort level: Balanced
  • dianatofficial dianatofficial commented on Aug 22, 2026 07:02 UTC
    Reviewer

    Verified the diff. Changes align with project standards.

    jrbarkin jrbarkin commented on Sep 01, 2026 05:33 UTC
    Contributor

    Heads up — I've been chasing #331900 from the other end, and I think this PR is correct but
    resolves only half of it. Worth knowing before it merges, so the issue isn't closed prematurely.

    There are two independent caches keyed on (uri, version), and a re-opened document restarts at
    version 1, so both go stale together:

    1. TokenCache in markdownEngine.ts — what this PR fixes.
    2. PreviewDocumentVersion.equals in preview/preview.ts — compares resource + version, so
    #updatePreview returns early and never calls the renderer at all.

    Because layer 2 short-circuits before the engine is reached, this PR alone doesn't change the
    observed behaviour. I built all four combinations and measured the rendered preview DOM after the
    document lease lapsed:

    | preview.ts | markdownEngine.ts | preview after an external write |
    |---|---|---|
    | upstream | upstream | stale |
    | upstream | this PR | stale |
    | identity-aware equals | upstream | stale |
    | identity-aware equals | this PR | updates correctly |

    I've opened https://github.com/microsoft/vscode/pull/333712 for layer 2. It's 6 lines and doesn't touch markdownEngine.ts, so there's no
    overlap with this one — they just need to land together to close the issue. I've written mine as
    "Part of #331900" rather than "Fixes" for that reason; you may want to do the same here.

    Repro, if you want to check it yourself (the wait matters — before the lease lapses everything works,
    which is why this is awkward to reproduce interactively):

    1. mkdir /tmp/mdbug && printf '# V0\n' > /tmp/mdbug/test.md
    2. Open the folder, right-click test.md in the explorer → Open Preview. Don't open it in an editor.
    3. Wait more than three minutes.
    4. printf '# CHANGED\n' > /tmp/mdbug/test.md
    5. Preview still shows V0.

    dkontyko dkontyko commented on Sep 02, 2026 22:09 UTC
    Author

    Heads up — I've been chasing #331900 from the other end, and I think this PR is correct but resolves only half of it. Worth knowing before it merges, so the issue isn't closed prematurely.

    Thanks, nice catch. I'll defer to the maintainers on how they want to handle closing the issue. I'm fine either way.

    Assignees
    dkontyko dkontyko
    Labels
    risk: low confidence: 88% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 29.7% (LOW) · Higher = larger blast radius
    29.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 88% · Lower = rushed or shallow review
    88%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 3.5% · Safe to merge
    3.5%