Skip to content

Refactor Copilot managed-settings for maintainability - #322439

Merged
joshspicer merged 8 commits into
mainfrom
joshspicer-managed-settings-review
Jun 23, 2026
Merged

joshspicer merged 8 commits into
mainfrom
joshspicer-managed-settings-review

Conversation

@joshspicer

Copy link
Copy Markdown
Contributor

Why

The Copilot enterprise managed-settings code (native MDM plus the GitHub server channel, both feeding one canonical IPolicyData.managedSettings bag) 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

  • Single descriptor table for structured settings. adaptManagedSettings now drives object/array encoding from one STRUCTURED_MANAGED_SETTINGS table (key + responseField + encode) instead of three bespoke branches. Adding a structured key is one row.
  • Consolidated equality. Replaced the two bespoke areManagedSettings*Equal helpers with the shared deep equals (the records are flat, so it is exactly equivalent).
  • Shared helpers. Added hasManagedSettingsDefinitions (replacing a private duplicate in accountPolicyService) and managedSettingValue(key) for the common "lock to the managed value, else fall through" policy callback.
  • Docs. Updated the add-policy skill doc to match.

Maintainability review (3-model council)

Folded in findings from a GPT-5.5 / Gemini 3.1 Pro / Sonnet 4.6 review:

  • Behavior-preservation fix (the important one): the scalar remainder in adaptManagedSettings is now built with { ...response } + delete rather than for..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.
  • managedSettingValue is memoized per key, so the policy-definition reference identity that isSamePolicyDefinition relies on is a real guarantee rather than an accident of where the helper is called.
  • Corrected JSDoc and skill wording that claimed responseField is compiler-checked against the response interface; the response's index signature defeats keyof, 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-client is clean and all four managed-settings suites pass (50 tests, including the new __proto__ and memoization cases):

src/vs/platform/policy/test/common/copilotManagedSettings.test.ts
src/vs/platform/policy/test/node/copilotManagedSettingsService.test.ts
src/vs/workbench/services/policies/test/browser/accountPolicyService.test.ts
src/vs/workbench/services/accounts/test/browser/managedSettings.test.ts

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]>
Copilot AI review requested due to automatic review settings June 22, 2026 20:16

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.

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_SETTINGS descriptor table in adaptManagedSettings.
  • Replaces bespoke managed-settings equality helpers with shared deep equals, and adds shared helpers hasManagedSettingsDefinitions + memoized managedSettingValue(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
joshspicer marked this pull request as ready for review June 22, 2026 20:26
@joshspicer
joshspicer marked this pull request as draft June 22, 2026 20:26
joshspicer and others added 5 commits June 22, 2026 13:36
…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
joshspicer marked this pull request as ready for review June 22, 2026 22:27
@joshspicer
joshspicer merged commit a4ce08f into main Jun 23, 2026
28 checks passed
@joshspicer
joshspicer deleted the joshspicer-managed-settings-review branch June 23, 2026 00:42
@vs-code-engineering vs-code-engineering Bot added this to the 1.127.0 milestone Jun 23, 2026
@vs-code-engineering vs-code-engineering Bot locked and limited conversation to collaborators Aug 7, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants