Repository navigation
Refactor Copilot managed-settings for maintainability - #322439
Merged
Merged
Conversation
Centralize structured (object/array) managed-setting handling behind a
single descriptor table so adding a key touches one place, consolidate the
duplicated equality helpers onto `equals`, and add shared
`hasManagedSettingsDefinitions` and `managedSettingValue` helpers. Strictly
behavior-preserving.
Incorporates a 3-model maintainability review:
- `adaptManagedSettings` builds the scalar remainder via `{ ...response }`
plus delete (CopyDataProperties) instead of for..in + assignment, so a
server-sent own `__proto__` key cannot trigger the inherited setter. This
matches the original `...rest` semantics; adds a regression test.
- `managedSettingValue` is memoized per key so its policy-definition
reference identity is real rather than incidental to the call site.
- Corrected JSDoc and skill docs that overstated `responseField` as
compiler-checked; it is a hand-maintained union backstopped by tests.
Co-authored-by: Copilot <[email protected]>
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors Copilot enterprise managed-settings plumbing (native MDM + GitHub server channel feeding IPolicyData.managedSettings) to reduce per-key duplication and centralize common helpers, aiming to keep behavior unchanged while making new managed-settings keys easier to add.
Changes:
- Centralizes structured (object/array) managed-settings adaptation behind a single
STRUCTURED_MANAGED_SETTINGSdescriptor table inadaptManagedSettings. - Replaces bespoke managed-settings equality helpers with shared deep
equals, and adds shared helpershasManagedSettingsDefinitions+ memoizedmanagedSettingValue(key). - Updates tests and the add-policy managed-settings skill doc to reflect the refactor and lock in resilience behavior.
Show a summary per file
| File | Description |
|---|---|
| src/vs/workbench/services/policies/common/accountPolicyService.ts | Uses shared hasManagedSettingsDefinitions helper instead of a local duplicate. |
| src/vs/workbench/services/accounts/test/browser/managedSettings.test.ts | Adds regression coverage for server-sent own __proto__ handling in adaptation. |
| src/vs/workbench/services/accounts/browser/managedSettings.ts | Refactors server response adaptation: structured settings via descriptor table; scalar remainder handled via spread+delete. |
| src/vs/workbench/contrib/chat/browser/chat.shared.contribution.ts | Switches common managed-settings policy.value callbacks to managedSettingValue(KEY). |
| src/vs/platform/policy/test/common/copilotManagedSettings.test.ts | Adds tests for hasManagedSettingsDefinitions and memoized managedSettingValue. |
| src/vs/platform/policy/node/copilotManagedSettingsService.ts | Uses equals for watched-definition comparisons; removes bespoke equality helper; clarifies native boundary projection docs. |
| src/vs/platform/policy/common/copilotManagedSettingsIpc.ts | Uses equals for managed-settings change detection; removes bespoke equality helper. |
| src/vs/platform/policy/common/copilotManagedSettings.ts | Adds managedSettingValue + hasManagedSettingsDefinitions helpers. |
| .github/skills/add-policy/github-managed-settings.md | Updates documentation to reflect new helpers and structured-key descriptor table approach. |
Copilot's findings
- Files reviewed: 9/9 changed files
- Comments generated: 2
The bag-carrying `type` is load-bearing, not cosmetic: `projectManagedSettings` gates each value with `typeof value === type` and drops mismatches, and the native MDM watcher reads the registry/plist value as that type. Spell out that omitting it (or declaring the object/array type) makes a structured key fail projection and silently never apply. Co-authored-by: Copilot <[email protected]>
joshspicer
marked this pull request as ready for review
June 22, 2026 20:26
joshspicer
marked this pull request as draft
June 22, 2026 20:26
…ccuracy
- hasManagedSettingsDefinitions: reuse the allocation-free isEmptyObject
helper instead of Object.keys(...).length (the bot's only valid nit).
- Add a primitive `__proto__` regression test proving a server-sent
`{"__proto__": true}` scalar is dropped, never pollutes the result
(disproves the reviewer's prototype-pollution concern).
- Fix github-managed-settings.md: omitting `type` or declaring
`'object'`/`'array'` is a compile error (the field is required and
constrained to `'string' | 'number' | 'boolean'`), not a runtime drop;
only `'number'`/`'boolean'` compile-but-drop-at-runtime.
Co-authored-by: Copilot <[email protected]>
Centralize the server-over-MDM precedence into a shared selectManagedSettings helper (plus a ManagedSettingsSource union) and reuse it in both AccountPolicyService and the Policy Diagnostics report, so the report can never drift from the source that policy evaluation actually applies. Rewrite the diagnostics "Managed Settings" section to: - show the Active source (GitHub Server API / Native MDM / None) - break down each channel (server fetch status + raw response, native MDM bag) - label the raw response as the last *successful* fetch, so a later failed fetch (e.g. a 404) no longer looks like it contradicts an empty effective bag - compute the true effective bag via the shared projectManagedSettings Co-authored-by: Copilot <[email protected]>
The "Generate example" button filled the editor and the localStorage draft but never called debouncedSave(), so the generated body was never POSTed to /api/state and the endpoint kept serving the empty preset. Add the missing debouncedSave() to match applyPreset() and the editor input handler. Co-authored-by: Copilot <[email protected]>
…e errors The Developer: Policy Diagnostics "Managed Settings" section now renders data only (tables and JSON blocks, no explanatory paragraphs). It also collects non-fatal parsing/normalization warnings from every stage of the managed-settings pipeline, jsonc-style (accumulate, never throw), and surfaces them in a new "Parse Errors" section: - adapt: re-runs adaptManagedSettings on the raw server response - project: re-runs projectManagedSettings against the declared policy keys - parse: re-parses JSON-payload string values with the jsonc parser This explains why a key is silently dropped. For example a server extraKnownMarketplaces entry with source "github" but no "repo" now shows the "requires \"repo\"" warning instead of just vanishing from the bag. Adds a focused test for that github-without-repo normalization case. Co-authored-by: Copilot <[email protected]>
Code-quality pass on the managed-settings diagnostics section:
- Extract a jsonBlock() helper for the repeated fenced-JSON rendering
(4 call sites collapsed).
- Parse only the known JSON-payload keys (enabledPlugins,
strictKnownMarketplaces, extraKnownMarketplaces) instead of a
leading-brace heuristic. This mirrors what PolicyConfiguration actually
parses, avoids mis-sniffing scalar values, and catches malformed payloads
that don't start with a brace.
- Unify the raw-response guard on isObject() so the printed raw response and
the adapt-stage warning harvest use one predicate.
- Drop the defensive object copy in projectManagedSettings(); it is read-only,
so normalize undefined with `?? {}` instead of spreading.
Co-authored-by: Copilot <[email protected]>
The diagnostics report showed "Native MDM | Available | no (desktop only)" even on desktop. ICopilotManagedSettingsService was registered only in the electron-main process and hand-plumbed into AccountPolicyService, but never placed in the renderer service collection, so the report's accessor.get(ICopilotManagedSettingsService) always threw and mislabeled the channel as unavailable. Register the CopilotManagedSettingsChannelClient (the renderer's handle to the main-process service) in the service collection in both desktop.main.ts and sessions.main.ts. The diagnostics now resolves it on desktop and Agents windows and reports real native MDM availability and values; web still has no native channel and correctly reports unavailable. Also tidy the report builder: extract a PROPERTY_VALUE_TABLE_HEADER constant for the five repeated two-column table headers, and drop the now-misleading "(desktop only)" annotation on the availability row. Co-authored-by: Copilot <[email protected]>
joshspicer
marked this pull request as ready for review
June 22, 2026 22:27
roblourens
approved these changes
Jun 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
The Copilot enterprise managed-settings code (native MDM plus the GitHub server channel, both feeding one canonical
IPolicyData.managedSettingsbag) had accumulated per-key duplication: each structured object/array setting carried its own hand-written branch in the server adapter, there were two near-identical deep-equality helpers, and a private "does any policy declare a managed setting" check was duplicated across files. A few doc comments also overstated compile-time guarantees. This refactor consolidates that surface so adding a managed-settings key touches one place, with no behavior change.What
adaptManagedSettingsnow drives object/array encoding from oneSTRUCTURED_MANAGED_SETTINGStable (key+responseField+encode) instead of three bespoke branches. Adding a structured key is one row.areManagedSettings*Equalhelpers with the shared deepequals(the records are flat, so it is exactly equivalent).hasManagedSettingsDefinitions(replacing a private duplicate inaccountPolicyService) andmanagedSettingValue(key)for the common "lock to the managed value, else fall through" policy callback.add-policyskill doc to match.Maintainability review (3-model council)
Folded in findings from a GPT-5.5 / Gemini 3.1 Pro / Sonnet 4.6 review:
adaptManagedSettingsis now built with{ ...response }+deleterather thanfor..in+ assignment. The latter would route a server-sent own__proto__key through the inherited__proto__setter, diverging from the original destructuring...rest. Added a regression test that locks this in.managedSettingValueis memoized per key, so the policy-definition reference identity thatisSamePolicyDefinitionrelies on is a real guarantee rather than an accident of where the helper is called.responseFieldis compiler-checked against the response interface; the response's index signature defeatskeyof, so the union is hand-maintained and the adapter tests are the drift backstop.Risk and testing
Strictly behavior-preserving. This is security-sensitive enterprise-lockdown code, so no functional change is intended.
npm run typecheck-clientis clean and all four managed-settings suites pass (50 tests, including the new__proto__and memoization cases):