Skip to content

fix: unstick the anti-pattern "Why?" explanation and use Copilot Auto - #301

Merged
Sanjay Singh (san360) merged 5 commits into
mainfrom
san360-fix-why-button-hang
Oct 30, 2026
Merged

Sanjay Singh (san360) merged 5 commits into
mainfrom
san360-fix-why-button-hang

Conversation

@san360

@san360 Sanjay Singh (san360) commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes the anti-pattern Why? button that stays on "Asking AI for an explanation…" forever (#278). It also makes every AI feature use GitHub Copilot's Auto model, fail fast with a clear message, and appear only when GitHub Copilot is available.

Code changes

File Change
src/core/copilot-model.ts (new) Shared { vendor: 'copilot', id: 'auto' } selector, the consent-dialog justification text, and the 120s request budget (COPILOT_REQUEST_BUDGET_MS).
src/webview/panel-llm-model.ts (new) Model access: selectModel() (Copilot Auto only; at most 15s and never past the request's deadline), isCopilotModelAvailable() probe, withTimeout(), and classification of errors that must not be retried.
src/webview/panel-llm.ts Removed the hard-coded model families and old selection code. callLlm gets a 120s budget that includes model selection and can be shared across calls (llmDeadline() and a deadline option), no retries for non-recoverable errors, empty reply treated as failure, and the justification. callLlmJson only gains the justification.
src/webview/panel-rpc.ts generateRule passes one deadline to all of its model calls (up to three) and keeps the latest draft if a follow-up runs out of time. Its retry loop is now the testable generateRuleMarkdown.
src/webview/shared.ts Generate SKILL.md (generateSkillContent) gets the 300s webview timeout used for Copilot-backed requests, like Why? and Generate rule. The list is type-checked against the RPC method names.
src/core/rule-compiler.ts { family: 'gpt-4.1' } replaced by the shared Auto selector, plus the justification.
src/webview/occurrence-explain.ts (new) Why? flow moved out of the page module. Every state is rendered through Preact.
src/webview/page-antipatterns.ts Inline explainOccurrence removed; the page uses the new module. The Why? button is always rendered and marked llm-only.
src/webview/panel.ts getCapabilities actually probes Copilot. The panel pushes capabilities messages on lm.onDidChangeChatModels.
src/core/types/rpc-types.ts New HostCapabilities and HostCapabilitiesMessage types.
src/webview/capabilities.ts Applies pushed capabilities and reflects them as an llm-unavailable class on the root element, so CSS shows or hides AI controls without rebuilding the page. Adds a host-aware llmUnavailableNote() (replaces LLM_UNAVAILABLE_NOTE).
src/webview/dashboard-shell.ts, page-dashboard.ts, page-config.ts, styles.css AI controls are always rendered and marked llm-only: the Skill Finder and Level Up nav entries, the Dashboard Skill Finder card, and Review Context Files with its workspace count. A disabled Review Context Files carrying the sign-in hint is marked llm-fallback. Two CSS rules keyed on the root class show or hide them.
Tests New panel-llm-call.test.ts, occurrence-explain.test.ts, capabilities.test.ts; extended panel.test.ts; new shared-rpc.test.ts; Playwright cases in antipatterns.spec.ts and the new copilot-availability.spec.ts (harness.html mirrors the nav markers); a packaged-VSIX desktop test in tests/vscode/suite/populated.spec.ts, with new expectHidden and expectDisabled helpers in tests/vscode/dashboard.ts.
Docs README privacy note (states exactly what is hidden or disabled without Copilot), CHANGELOG.

Impacted functionality: before → after

Functionality (where in the UI) Before After
Why? result box, Anti-Patterns → finding → occurrences The loading text stayed after the answer arrived (<answer>Asking AI for an explanation…). An empty answer left only the loading text. Asking again after hiding the answer stuck on "Asking AI for an explanation…" forever, with no error. The loading text is replaced by the answer. Asking again shows the new answer. An empty answer shows "The AI returned an empty explanation. Try again." Host errors appear in the box.
Model used by every AI feature: Why?, Rule Editor Generate rule, Skill Finder (Analyze, catalog picks, Generate SKILL.md), Learning Center (quiz, Slop or Not, Did you know, resources), Context Health Review Context Files Tried gpt-5.4-mini → gpt-5-mini → gpt-4.1-mini → gpt-4.1, then the first model from any vendor. A model could be selectable but disabled for the user's plan, which produced empty replies (#91). Only Copilot Auto, which routes to a model enabled for the user's plan and org. There is no fallback. If Auto is unavailable: "GitHub Copilot isn't available. Sign in to GitHub Copilot in VS Code to use AI features."
Wait time and retries for plain-text AI calls: Why?, Generate rule, Generate SKILL.md Model selection had no time limit. Every failure except cancellation was retried 3 times with a 90s timeout each, so a hung request took up to 270s. Denied access and quota errors were retried too. Empty replies counted as success. Generate SKILL.md gave up on the webview side after 120s and dropped a late reply. The whole request, model selection included, is limited to 120s, and selection to 15s. Generate rule's up to three model calls share that one budget and keep the latest draft if time runs out. Generate SKILL.md now waits up to 300s on the webview side, so the reply or the error always arrives. Timeouts, denied access (NoPermissions), quota or rate limits (Blocked) and NotFound fail immediately with an actionable message (for example "Run 'Chat: Manage Language Model Access'…"). Transient errors and empty replies are retried within the budget.
JSON-based AI features: Skill Finder, Learning Center, Context Health review Hard-coded families with any-vendor fallback; no consent justification. Copilot Auto with the justification. Retry and JSON-repair logic are unchanged; that area is handled in #253 and #277.
Rule Playground: natural-language rule compile { family: 'gpt-4.1' } only, falling back to the heuristic template when that model wasn't available. Copilot Auto; the heuristic fallback is unchanged.
Language-model consent dialog, first AI use No reason given. Shows "AI Engineer Coach uses GitHub Copilot to explain anti-pattern detections, review context files, and generate skills, rules, and quizzes when you ask."
AI feature visibility in VS Code getCapabilities always returned llm: true. Why?, Skill Finder (nav entry and dashboard card), Level Up and Review Context Files were always shown, even without Copilot, and then failed or hung when used. The dashboard probes Copilot Auto at load (up to 3s; treated as available if the probe is slow or errors). Without Copilot, Why? buttons, the Skill Finder card, and the Skill Finder and Level Up nav entries are hidden. Opening those pages directly redirects to Dashboard. Review Context Files is disabled with a hint. The Rule Editor's Generate rule stays available and, as before, falls back to a starter template.
Copilot sign-in or sign-out while the dashboard is open Not tracked; AI features always looked available. The panel listens to lm.onDidChangeChatModels and pushes the change. AI controls appear or disappear in place, with no need to reopen. The page isn't rebuilt, so an open Rule Editor and its unsaved draft, expanded findings, and loaded results are kept. If you're on Skill Finder or Level Up when Copilot becomes unavailable, you stay on the page and AI actions show the sign-in message.
Hint next to unavailable AI features Only the canvas text existed: "Open in VS Code with the local Copilot agent to use this." In VS Code: "Sign in to GitHub Copilot in VS Code to use this." The canvas text is unchanged.
Bring-your-own-key or third-party-only setups (no Copilot) AI features could run on any registered model through the any-vendor fallback. Not supported: AI features are hidden unless Copilot Auto is available. This is an intentional scope decision.
Copilot app canvas mode AI features hidden (llm: false). Unchanged.
Diagnostics, AI Engineer Coach Output channel Only call-failed from JSON calls. Local [panel-llm] lines: model-selected (vendor, id, family), model-unavailable, call-failed (with a retryable flag), empty-response, probe-inconclusive, plus [panel] capabilities-changed. Prompts and keys are never logged.

Not changed:

  • prompts and secret redaction;
  • callLlmJson retry and JSON-repair logic;
  • the @aicoach chat participant, which uses the model selected in chat;
  • MCP tools;
  • log parsing and analytics, and every page driven by logs;
  • the canvas host.

Root cause (reproduced)

explainOccurrence wrote the loading text with textContent and then called Preact render() on the same element. Preact never removes DOM nodes it didn't create, and after a textContent wipe it keeps patching its old, detached nodes.

The new tests reproduce the bug against the original code:

  • jsdom, first answer: "The session ran long.Start a new chat per task.Asking AI for an explanation..."
  • jsdom, asking again: "Asking AI for an explanation..." forever
  • real Chromium (Playwright): "First explanation.Asking AI for an explanation..."

Not adopted from the issue

  • contributes.languageModels: VS Code has no such contribution point. The only one is languageModelChatProviders, which is for providing models. Calling vscode.lm needs no contribution; consent is an auth dialog shown on the first sendRequest.
  • gpt-4o / o1 / o3-mini family list: these models are retired too. Copilot Auto replaces hard-coded names.
  • Warm-cache pendingMessages flush: already fixed on main by publishReady() (68ceef6) and covered by an existing panel.test.ts case.

Validation

The branch is up to date with main (merged at ea0163e). The only conflict was CHANGELOG.md, resolved by keeping both entries. The PR's src/ changes are identical before and after the merge. Results below are for b9a5780.

Command Result
npm run check (CI verify) ✅ passed on b9a5780: npm run check, build, package, check-size
Packaged VS Code E2E (CI vscode-compatibility) ✅ all 6 jobs passed: Windows, macOS and Ubuntu × VS Code minimum (1.125.0) and latest. The new Copilot-unavailable test ran in each job and passed.
npm run check (local) typecheck ✅ · lint ✅ 0 errors (431 warnings, identical to main) · knip ✅ · lockfile-lint ✅ · tests ✅ 1736 passed, 5 skipped · spellcheck ✅ through the cspell API (282 files, 0 issues). The cspell CLI refuses this machine's prerelease Node (v27.0.0-alpha) before checking anything.
npm run test:vscode (packaged VSIX, VS Code 1.125.0) ✅ empty data 1/1, populated data 7/7, including the new Copilot-unavailable test
npm run test:vscode -- --vscode-version latest (VS Code 1.144.0) ✅ empty data 1/1, populated data 7/7
npm run test:e2e 129 passed, 7 skipped (Burndown/Token usage feature flags), 1 failed. The failure is curiosity.spec.ts:353 "reads and scrolls long escaped prompts from language previews at 1280x800", which is flaky on main too: in alternating batches of repeated runs it failed 9 of 40 times on main and 4 of 40 on this branch. This PR doesn't touch the Curiosity page.
npm run check-size ✅ extension 1.64 MB, VSIX 1.50 MB, within the 2 MB budget

New tests:

  • panel-llm-call.test.ts (18): selection, probe, retry, timeout and budget behaviour, including model selection counting toward the budget and deadlines shared across calls.
  • shared-rpc.test.ts (4): every Copilot-backed RPC waits longer than the host budget plus a 60s delivery margin; other RPCs keep 120s.
  • panel-rpc.test.ts (+5): generateRuleMarkdown retry feedback and keeping the latest draft.
  • occurrence-explain.test.ts (6) and capabilities.test.ts (5): jsdom.
  • 4 host-capability tests in panel.test.ts.
  • 4 Playwright tests in antipatterns.spec.ts, including a regression test that an unsaved Rule Editor draft survives a capability change.
  • 2 Playwright tests in copilot-availability.spec.ts: the Dashboard Skill Finder card and the Context Health fallback button.
  • 1 packaged-VSIX desktop test in tests/vscode/suite/populated.spec.ts, run in real VS Code with Copilot unavailable: the Skill Finder card, the Skill Finder and Level Up nav entries, and every Why? button are rendered but hidden, and Review Context Files shows the disabled fallback with the sign-in hint.

Automated run in real VS Code (Copilot signed out)

Besides the desktop test, VS Code 1.144.0 itself was driven with Playwright's Electron automation. It ran in an isolated profile with the synthetic session logs from tests/vscode, clicking through the real UI and reading the Output channel. GitHub Copilot was signed out, so no Copilot model was available.

Scenario Result
Signed out ✅ The root element has llm-unavailable. The Skill Finder and Level Up nav entries and the Dashboard Skill Finder card are hidden. All 90 Why? buttons are rendered but hidden. Review Context Files is replaced by a disabled button with "Sign in to GitHub Copilot in VS Code to use this."
chat.allowAnonymousAccess: true without signing in, at runtime and after a reload ✅ Copilot offers no model until GitHub's terms are accepted in Chat, so the AI controls stay hidden.
Copilot becomes available and then unavailable while the dashboard is open (the host's capabilities message replayed inside the real webview) ✅ Controls appear and disappear in place: visible Why? buttons go 0 → 90 → 0. The open Rule Editor keeps the same element and its unsaved draft, and the expanded occurrence list stays open.
Why? with no model available (button forced visible) ✅ Actionable error in 54 ms; asking again errors in 57 ms. The Output channel shows [panel-llm] model-unavailable | selector=copilot/auto.
Skill Finder Analyze with no model available ✅ Actionable error in 62 ms.
Rule Editor Generate rule with no model available ✅ Starter template in 102 ms. The status still says "Rule generated…"; that text predates this PR (see follow-ups).

The automation doesn't sign in to GitHub or accept GitHub's Copilot terms on anyone's behalf, so the signed-in checks below still need a person.

Manual verification with real Copilot (still to do)

Launch an isolated dev host from the repo root with code --user-data-dir "$env:TEMP\aiec-test" --extensionDevelopmentPath "$PWD". It starts with fresh GitHub sign-in and consent state and still reads your real session logs. Watch View → Output → AI Engineer Coach.

  • Signed in: Why? returns an explanation, and the log shows a [panel-llm] model-selected line with vendor=copilot id=auto. Hide it, then ask again: a new explanation appears.
  • Consent: on the first use the dialog shows the justification. Deny gives an immediate actionable error; after allowing access, Why? works.
  • Signed out: the Why? buttons, the Skill Finder card, and the Skill Finder and Level Up nav entries are hidden, and Review Context Files is disabled with a hint. Verified by the automated run and the desktop test.
  • Sign in: those controls come back without reopening the dashboard. Verified with a replayed host message; a real sign-in is still to do.
  • With the Rule Editor open and a draft typed, sign out of Copilot and back in: the editor and the draft stay. Verified with a replayed host message; a real sign-out is still to do.
  • Offline: you get a bounded error, never an endless spinner. With no model the error comes back in under 0.1 s; losing the network while signed in is still to do.
  • Skill Finder scan and the Level Up quiz still work through Copilot Auto.

Follow-ups (not in this PR)

  • The Rule Editor's Generate rule says "Rule generated…" even when it fell back to the starter template (page-antipatterns-editor.ts).
  • cleanRuleMarkdown (panel-rpc.ts) removes the closing fence of a trailing detect block when the model's reply isn't wrapped in a fence. Validation then skips the detect checks without reporting anything.
  • curiosity.spec.ts:353 (language previews at 1280x800) is flaky on main when run with 6 workers.

Related Issues

Resolves #278. Related: #91 (empty responses from selectable-but-disabled models). This PR supersedes the model-selection part of #253, while #253's callLlmJson empty-response handling is still complementary. It is also compatible with #276 / #277.

Checklist

  • npm run check passes (typecheck + lint + spellcheck + knip + tests). Green in CI verify on b9a5780; see Validation for local notes.
  • Changes are covered by tests (if applicable)
  • Documentation updated (if applicable): README privacy note and CHANGELOG

The Why? explainer wrote its loading text with textContent and then
called Preact render() on the same container. Preact never removes nodes
it did not create, so "Asking AI for an explanation..." stayed after the
answer, and a repeat request patched detached nodes and never appeared.
Every explanation state now renders through Preact, and empty answers
surface as errors.

AI features and the natural-language rule compiler now use only GitHub
Copilot's Auto model ({ vendor: 'copilot', id: 'auto' }) instead of
hard-coded model families. callLlm no longer retries timeouts, denied
access, quota or not-found errors, treats empty replies as failures, and
keeps each call within a 120s budget.

getCapabilities now probes Copilot availability (bounded, optimistic on
timeout) instead of always reporting llm: true, and the panel pushes
capability changes on lm.onDidChangeChatModels so Why?, Skill Finder,
Level Up and Context Review follow Copilot sign-in and sign-out.

The contributes.languageModels entry proposed in the issue is not added:
VS Code has no such contribution point (languageModelChatProviders is for
providing models) and consuming vscode.lm needs no contribution.

Resolves #278

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 21, 2026 20:15
@github-actions

Copy link
Copy Markdown

⚠️ Deprecation Warning: The deny-licenses option is deprecated for possible removal in the next major release. For more information, see issue 997.

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Capability-triggered page replacement silently discards unsaved Rule Editor drafts.

1 open issue
What changed in this PR

Fixes stuck anti-pattern explanations and standardizes optional AI features on GitHub Copilot Auto.

Changes:

  • Renders explanation states consistently through Preact.
  • Adds bounded model selection and improved plain-text request error handling.
  • Updates AI feature visibility when Copilot availability changes, with regression coverage.
File Description
tests/​e2e/​harness.html Adds AI navigation markers.
tests/​e2e/​antipatterns.spec.ts Tests explanations and capability changes.
src/​webview/​styles.css Hides unavailable AI navigation items.
src/​webview/​panel.ts Probes and pushes Copilot availability.
src/​webview/​panel.test.ts Tests host capability responses and updates.
src/​webview/​panel-llm.ts Updates model access, retries, and timeouts.
src/​webview/​panel-llm-model.ts Centralizes model selection and failure classification.
src/​webview/​panel-llm-call.test.ts Tests model access and request failures.
src/​webview/​page-config.ts Uses host-specific availability guidance.
src/​webview/​page-antipatterns.ts Delegates explanation rendering.
src/​webview/​occurrence-explain.ts Manages explanation states through Preact.
src/​webview/​occurrence-explain.test.ts Tests explanation rendering and recovery.
src/​webview/​dashboard-shell.ts Marks AI-dependent navigation items.
src/​webview/​capabilities.ts Handles capability updates and subscriptions.
src/​webview/​capabilities.test.ts Tests capability state and notifications.
src/​webview/​app.ts Refreshes navigation and pages on capability changes.
src/​core/​types/​rpc-types.ts Defines shared capability message types.
src/​core/​rule-compiler.ts Uses Copilot Auto and consent justification.
src/​core/​copilot-model.ts Defines shared selector and justification.
README.md Documents Copilot requirements and availability.
CHANGELOG.md Records explanation and model-access changes.

Review effort: Balanced

Comment thread src/webview/app.ts Resolved
Addresses review feedback on #301. Re-rendering the current page after a
capability push cleared #content, which closed the Rule Editor modal and
discarded unsaved drafts, along with other in-page state such as expanded
findings and loaded Context Health review results.

Capability changes no longer rebuild the page. capabilities.ts toggles an
llm-unavailable class on the root element, and AI controls are always
rendered with llm-only or llm-fallback markers that CSS shows or hides:
Why? buttons, the Dashboard Skill Finder card, the Skill Finder and Level
Up nav entries, and the Context Health review controls.

Refs #278

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 22, 2026 05:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Host and webview request timeouts remain misaligned, and the planned real-Copilot integration verification is still pending.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread src/webview/panel-llm.ts Resolved
Addresses the high-severity review finding on #301. callLlm started its
120s budget after model selection, which can take up to 15s, so a request
could run for about 135s. Generate SKILL.md was not in the webview's
LLM_METHODS, so its RPC gave up at 120s and discarded the late reply or
the host's actionable error.

- The 120s budget (COPILOT_REQUEST_BUDGET_MS, now in core/copilot-model)
  starts before model selection, and selection is capped at the smaller
  of 15s and the remaining budget.
- callLlm accepts a shared deadline. generateRule makes up to three model
  calls for one request, so it now passes one deadline to all of them and
  keeps the latest draft if a follow-up runs out of time. Its retry loop
  moved into generateRuleMarkdown so it can be unit tested.
- generateSkillContent uses the 300s LLM RPC timeout like explainOccurrence
  and generateRule. The list is type-checked against the RPC method names.

Refs #278

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 22, 2026 17:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Real Copilot Auto routing, consent, sign-in transitions, and offline behavior still require the manual integration checks identified in the PR.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Sanjay Singh (san360) and others added 2 commits October 30, 2026 11:08
Resolve the CHANGELOG conflict by keeping both Unreleased entries.

Co-authored-by: Copilot App <[email protected]>
…suite

Add a packaged-VSIX desktop test that runs with GitHub Copilot unavailable
(the harness sets chat.disableAIFeatures) and checks, in real VS Code, that:
- the Dashboard Skill Finder card and the Skill Finder and Level Up nav
  entries are rendered but hidden,
- every anti-pattern Why? button is rendered but hidden,
- Review Context Files is hidden and its disabled fallback carries the
  "Sign in to GitHub Copilot in VS Code to use this." hint.

The test lives in its own describe block so the existing populated suite
stays within the max-lines-per-function budget, and the Dashboard page
object gains expectHidden and expectDisabled helpers.

Also correct the README privacy note and CHANGELOG entry: they now list
exactly which features are hidden or disabled without Copilot, include
Level Up among the AI features, and note that rule generation falls back
to a starter template instead of being hidden.

Refs #278

Co-authored-by: Copilot App <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Authenticated Copilot validation remains pending for Auto routing, first-use consent/denial, and real sign-in/out model events, which mocks, signed-out tests, and replayed messages do not verify.

No open issues 🎉

Review effort: Balanced

@san360

Copy link
Copy Markdown
Contributor Author

Aymen (@aymenfurter), I'm re-requesting your review: the branch rules require approval of the latest push, so your approval of 8e8b860 no longer counts.

Changes since you approved:

  • Merged main (ea0163e). The only conflict was CHANGELOG.md, and both sides' entries are kept. This PR's own src/ changes are identical to what you approved.
  • b9a5780 adds a packaged-VSIX desktop test (in the suite from test: add packaged VS Code E2E smoke coverage #302) that checks Copilot-only features are hidden while Copilot is unavailable. It also corrects the README and CHANGELOG wording about what is hidden or disabled.

All CI checks pass, including the new test on Windows, macOS and Ubuntu with VS Code 1.125.0 and the latest release. Copilot's re-review of b9a5780 found no issues. The signed-in Copilot checks listed in the description are still to do.

@san360
Sanjay Singh (san360) merged commit e6cedb7 into main Oct 30, 2026
13 checks passed
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.

fix: "Why?" button hangs indefinitely — missing languageModels permission + stale model family names

3 participants