Skip to content

Honor custom agent model and reasoning-effort pins across harnesses - #343750

Closed
dknappettmsft wants to merge 22 commits into
microsoft:mainfrom
dknappettmsft:dknappettmsft/custom-agent-model-pin
Closed

dknappettmsft wants to merge 22 commits into
microsoft:mainfrom
dknappettmsft:dknappettmsft/custom-agent-model-pin

Conversation

@dknappettmsft

@dknappettmsft dknappettmsft commented Oct 23, 2026 •

Copy link
Copy Markdown

Fixes #338949. Supersedes #341716, whose changes are included and rebased onto current main (co-authored).

Selecting a custom agent whose .agent.md frontmatter pins model and/or reasoning-effort can leave the previously selected picker model and effort in place. The harness then sends that selection instead of the agent's pin.

Changes

Copilot agent-host harness (chat view)

  • Host discovery publishes pins: SessionCustomizationDiscovery reads each discovered workspace or user agent file. It publishes the frontmatter model list (first entry as the protocol model, the ordered list in _meta['vscode.models']) and reasoning-effort (_meta['vscode.reasoningEffort']). AgentCustomizationItemProvider maps these into ICustomAgent. Plugin agents fall back to the protocol model.

  • Model matching: agent-host models are published under the session-type vendor (agent-host-…), so ILanguageModelChatMetadata.matchesQualifiedName never matches values like Claude Sonnet 4.5 (copilot) or claude-sonnet-4.5. For each pinned value, ChatInputPart tries, in order:

    1. The existing qualified-name match.
    2. An exact metadata.id match among session-targeted models, so an advertised ID such as provider/custom:variant=fast isn't parsed as a configuration suffix.
    3. resolveAgentFileModelIdentifiers.

    Pins resolve against the incoming session's model pool (its session type), not the widget's outgoing session while binding. Local harness matching is unchanged.

  • Reasoning effort: reasoning-effort is parsed into PromptHeader → ICustomAgent → CustomChatMode. A dedicated autorun applies it to the current agent-host model as a read-time overlay in ChatModelConfigurationStore (setPresentedConfiguration), so it follows model changes and late registration.

    • It uses thinkingLevel in preference to reasoningEffort when a model advertises both, matching the picker and the runtime.
    • The overlay is validated on every read against the session-scoped schema with the same filter restores use, so host locks, narrowed choices and other schema constraints win.
    • It never reaches preferences or storage. Overlay values carry an agent source (vscode.modelConfiguration.<key>.source). The Copilot host preserves it in its configuration report, and the store never adopts it as a runtime value.
    • Captured configuration also carries the conversation's own value underneath (vscode.modelConfiguration.<key>.underlying / .underlyingSource). Restores put that value back in place of the agent-sourced one, even after the store is cleared or another conversation changed the shared bucket. The host ignores these keys.
    • An explicit choice for that key supersedes the overlay.

User overrides of pins

  • The input state persists agentPinOverrides ({ modeId, model?, configKeys? }). It's written by InputModel.toJSON and the session operation log, and validated on revival.
  • It's recorded when the user picks a model other than the pinned one, explicitly sets a pinned configuration key, or edits an earlier request that explicitly chose that key or ran on another model. A handoff's or prompt file's model that differs from the pin (requestModelByQualifiedName) is recorded too, immediately before it applies, while the pin's own automatic application records nothing.
  • _syncFromModel applies the incoming overrides before the mode sync, so deliberate choices survive navigation and reloads.
  • Overrides are kept through programmatic fallbacks while an agent registers late, and cleared when the user picks another agent.
  • Between clients: custom-agent drafts sync the overrides in _meta['vscode.agentPinOverrides'], set to null when there are none.
    • The synced value is authoritative. Peers adopt each other's model and effort overrides, a re-armed pin clears them, and adopted overrides persist with the input state.
    • For live drafts from clients that don't sync overrides, effort overrides are derived from the draft's own provenance, on top of the current overrides: explicit adds the key, agent removes it.
    • The first transferred draft uses only synced overrides, so it never replaces locally saved ones with a guess.
    • The synced-versus-local draft comparison ignores this key only when the synced draft doesn't carry it. A pending local edit to the overrides alone therefore still wins over a peer update.
    • Outgoing drafts are built in one place, and peer updates are compared against that same form. A fallback for the channel's own model keeps this client's agent-applied configuration, such as an effort-only agent's pin.
  • First send: when an untitled agent-host session materializes, or a late send re-targets it, its overrides travel with the transferred mode to the real session. Cleared overrides are transferred too. The mode is identified by its advertised id (IChatRequestModeInfo.modeId), not a remote agent's wrapped instruction URI.
  • Session restore: when an agent-host session restores from a transferred draft that didn't sync overrides, backfillRestoredPickerState recovers the locally saved ones for the same agent. An explicitly synced null is kept, so a peer's re-armed pins aren't undone by older saved overrides.

Local chat (from #341716, adjusted)

  • Re-apply the pin after input-state restoration, unless the user overrode it, the conversation's in-session selection reason is UserSelection/RestoredChoice, or the state came from a remote peer. This runs outside the sync guard so the result is written back.
  • Retry an unresolved pin when the model list changes. An explicit or restored choice cancels the retry, and it waits while an authored model request (a handoff or prompt file) is pending. Mode switches and restores still apply the pin.
  • Keep the hostModelSelection guard so the Agents window's host-owned selection isn't bypassed.

Cloud delegation (from #341716, unchanged)

  • Omit the picker-model override when the selected remote agent pins a model.

How to test

  1. Create .github/agents/pinned.agent.md with model: [claude-sonnet-4.5, Claude Sonnet 4.5 (copilot)] and reasoning-effort: high.
  2. In the chat view with the Copilot harness, select a different model and effort in the picker. Then select the pinned agent.
  3. The picker switches to Claude Sonnet 4.5 / High. The session's events.jsonl shows the agent's model and effort on assistant.message.
  4. Change the model and the effort manually, switch to a conversation in another mode and back, then reload the window. Your choices are kept.
  5. Switch to a mode without reasoning-effort. The effort returns to the conversation's own setting, and new sessions don't inherit High.
  6. Create an agent with only reasoning-effort: high. Its effort applies to whichever model is selected, including after you switch models.
  7. With the same agent-host session open in two windows, override the model or effort in one. The other window keeps that choice instead of re-applying the pin. Re-selecting the agent in the first window re-arms the pin in both, including for a window that was closed at the time and reopened later.
  8. Repeat step 2 in Local chat with model: Claude Haiku 4.5 (copilot).

Validation

  • Targeted tsc type-check of all changed src files and tests: passed for workbench files. In agent-host files, the only errors are existing ones caused by the missing @github/copilot-sdk types in the minimal toolchain (confirmed against an unmodified baseline). Ran with a minimal toolchain, because a full npm ci wasn't possible locally.
  • Unit tests (chatInputPart.test.ts, chatModelConfigurationStore.test.ts, chatModel.test.ts, chatService.test.ts, agentHostChatContribution.test.ts, agentCustomizationItemProvider.test.ts, sessionCustomizationDiscovery.test.ts, copilotManagedModelConfiguration.test.ts, promptFileParser.test.ts, copilotCloudSessionsProvider.spec.ts): not run locally; relying on CI.
    • CI on 935af2f ran the PR's tests at the time. Its only failures were one PR test, fixed in 754c964, and two SessionsList tests broken on main.
    • CI for the latest commit (fb4d73b) is pending.
  • Known unrelated failure: main currently fails Compile & Hygiene and two Sessions - SessionsList tests (QUICK_CHATS_SECTION_ID used without an import in sessionsList.test.ts). CI merges this PR with main, so those checks fail here until main is fixed.
  • Manual verification in a dev build: not done yet.

Selecting a custom agent whose frontmatter pins `model` and/or
`reasoning-effort` could leave the previously selected picker model and
effort in place, which the harness then sent instead of the agent's pin.

- Copilot agent-host harness: resolve agent-file model values (display
  name or model ID) against session-targeted models, which are published
  under an `agent-host-*` vendor that qualified-name matching never hits.
- Plumb `reasoning-effort` from agent frontmatter into the chat mode and
  present it, session-scoped, on the selected agent-host model.
- Local chat: re-apply the pin after input-state restoration (unless the
  user deliberately picked a model in that mode) and when a pinned model
  becomes available, writing the result back to the input model.
- Cloud delegation: omit the picker-model override when the selected
  remote agent pins a model.
- Keep the host-owned model selection guard for the Agents window.

Fixes microsoft#338949
Supersedes microsoft#341716

Co-authored-by: aeschli <6461412+aeschli@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 23, 2026 09:32

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

Production agent-host metadata still omits the pins, and selection restoration and configuration handling can overwrite user preferences.

5 open issues
What changed in this PR

This PR addresses #338949 by making custom-agent model and reasoning-effort pins take precedence over existing picker selections across chat harnesses and cloud delegation.

Changes:

  • Carries reasoning-effort metadata through prompt parsing, custom agents, and cached chat modes.
  • Adds harness-aware model resolution, pin restoration, delayed-resolution retries, and session-scoped effort presentation.
  • Omits cloud picker-model overrides for pinned agents and adds regression tests.
File Description
src/​vs/​workbench/​contrib/​chat/​test/​common/​promptSyntax/​service/​promptFileParser.test.ts Tests reasoning-effort frontmatter parsing.
src/​vs/​workbench/​contrib/​chat/​test/​browser/​widget/​input/​chatInputPart.test.ts Tests pin precedence, restoration, and harness model matching.
src/​vs/​workbench/​contrib/​chat/​common/​promptSyntax/​service/​promptsServiceImpl.ts Copies parsed effort into custom-agent metadata.
src/​vs/​workbench/​contrib/​chat/​common/​promptSyntax/​service/​promptsService.ts Adds the custom-agent reasoning-effort field.
src/​vs/​workbench/​contrib/​chat/​common/​promptSyntax/​promptFileParser.ts Exposes the reasoning-effort header value.
src/​vs/​workbench/​contrib/​chat/​common/​chatModes.ts Carries effort through mode observables and caching.
src/​vs/​workbench/​contrib/​chat/​browser/​widget/​input/​chatInputPart.ts Implements pin resolution, restoration, retries, and effort presentation.
extensions/​copilot/​src/​extension/​chatSessions/​vscode-node/​test/​copilotCloudSessionsProvider.spec.ts Tests cloud model-pin precedence and updates mocks.
extensions/​copilot/​src/​extension/​chatSessions/​vscode-node/​copilotCloudSessionsProvider.ts Suppresses picker-model overrides for pinned cloud agents.

Review effort: Balanced

dknappettmsft and others added 2 commits October 23, 2026 10:59
- Agent host: publish each discovered agent's frontmatter `model` list
  and `reasoning-effort` (protocol `model` plus `_meta`), and map them in
  AgentCustomizationItemProvider so agent-host modes actually carry pins.
- Cancel a pending pin retry when the user explicitly picks a model.
- Apply effort-only pins to the current harness model.
- Keep presented reasoning effort as a read-time overlay in
  ChatModelConfigurationStore so it never becomes a durable preference
  (unrelated edits, restores and host session reports don't absorb it;
  an explicit choice supersedes it).
- Judge restore intent from the incoming conversation alone: keep
  UserSelection and RestoredChoice models across navigation and repeated
  restores, and cancel any pending pin retry.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

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.

- Namespace agent customization metadata as `vscode.models` and
  `vscode.reasoningEffort` so other hosts' `_meta` isn't read as pins.
- Validate the presented effort on every read against the effective
  session schema (read-only keys, offered choices, disabled items).
- Tag presented values with an `agent` source so restores recognise
  them even after the store is cleared or recreated; the Copilot host
  keeps that provenance in its configuration report and the store never
  adopts agent-sourced runtime values.
- Persist which pins the user overrode (`agentPinOverrides`) in the
  input state and session operation log, so deliberate model and effort
  choices survive navigation and reloads. Overrides are set before the
  mode sync, kept through programmatic fallbacks while an agent
  registers, and cleared when the user picks another agent.
- Reconcile the reasoning-effort overlay in its own autorun that follows
  the current model, covering effort-only pins and late model
  registration.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
dknappettmsft and others added 2 commits October 23, 2026 12:39
Passing `undefined` for the selected model fell back to the parameter
default, so the test began with a model already selected.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

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.

- Prefer an exact advertised harness model id over agent-file alias
  parsing, which reads `:key=value` as a configuration suffix.
- Derive per-key overrides from a peer's draft in the agent host session
  handler, where the configuration is known to be the peer's: an
  explicitly sourced effort is the peer's choice; its overlay is not.
- Recover saved `agentPinOverrides` when an agent-host session restores
  from a transferred draft that doesn't carry them.
- Carry the conversation's own value underneath a presented overlay in
  captured configuration, so restoring it after another conversation
  changed the shared bucket recovers that value rather than the bucket's.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

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

Peer-draft restoration and override reconciliation can still lose deliberate model or effort choices and retain stale overrides.

2 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Persist peer model overrides through local edits and reloads

src/​vs/​workbench/​contrib/​chat/​browser/​widget/​input/​chatInputPart.ts:2094

[P2] Preserve a peer's model override beyond this remote-origin sync. When a peer selects a different, non-pinned model, this guard initially keeps it, but _applyRemoteDraft() records only effort-key overrides. With no matching local intended choice, the selection controller records SessionRestore; a local input update clears origin, and serialization does not retain it either. A later restore can therefore reapply the agent's model pin instead of the peer's choice. Persist model-override intent when adopting a peer selection that differs from the resolved pin, and cover peer selection → local edit → navigation/reload.

4 resolved since last review

Review effort: Balanced

- Custom-agent drafts carry the user's pin overrides in `_meta`
  (`vscode.agentPinOverrides`, `null` when none), so peers adopt model
  and effort overrides, re-armed pins clear them, and adopted overrides
  persist with the input state through local edits and reloads.
- The first transferred draft uses only synced intent, so it never
  replaces locally saved overrides with a guess; live drafts from peers
  that don't sync intent still derive effort overrides from provenance,
  layered on the current overrides (explicit adds, `agent` removes).
- Synced-versus-local draft comparison ignores the overrides key, so
  derived overrides aren't mistaken for a local edit.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dknappettmsft

Copy link
Copy Markdown
Author

Addressed the previously missed finding ("Persist peer model overrides through local edits and reloads", chatInputPart.ts) in ee7611e: a peer that picks a model other than the agent's pin records a model override, which now syncs in the draft's _meta['vscode.agentPinOverrides']. The receiving client adopts it into agentPinOverrides in its input state, so it survives local edits (the merged state keeps it after origin clears) and is serialized for navigation and reloads, rather than relying on the transient selectedModelReason/origin. Covered by the handler test that adopts a synced model override, publishes it back in drafts, and clears it when the peer re-arms the pin.

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

Restoration can resurrect cleared overrides, and peer synchronization can discard pending metadata-only user choices.

2 open issues
2 resolved since last review

Review effort: Balanced

- Compare synced and local drafts including the overrides key when the
  synced draft syncs it, so a pending local edit that changes only the
  overrides isn't replaced by a peer update.
- Preserve a transferred draft's explicitly synced `null` overrides
  through conversion and `backfillRestoredPickerState`, so a peer's
  re-armed pins aren't replaced by locally saved overrides on reload.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dknappettmsft
dknappettmsft requested a balanced review from Copilot October 23, 2026 14:24

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

First-send state transfer and restoration of authored model choices still leave paths where agent pins can replace deliberate selections.

2 open issues
2 resolved since last review

Review effort: Balanced

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

Unresolved selection defects and cross-client restoration changes need fixes, passing targeted tests, and human validation.

3 open issues
2 resolved since last review

Review effort: Balanced

…ts, and restore edited requests' effort

- Apply a custom agent's effort to `thinkingLevel` before
  `reasoningEffort` when a model advertises both, matching the picker and
  runtime, so the pin takes effect and picker edits supersede it.
- Skip the model-list pin retry while an authored model request is
  pending, so a pinned model registering first doesn't cancel it. Mode
  switches and restores still apply the pin and replace stale requests.
- When editing an earlier request, record its explicitly chosen effort as
  an override for the conversation, so the agent's effort overlay doesn't
  hide the restored value.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

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

Identifier-based request edits still lose model override intent on restoration, and the new mode dependency breaks an existing restoration test fixture.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Preserve model override when restoring edited requests

src/​vs/​workbench/​contrib/​chat/​browser/​widget/​input/​chatInputPart.ts:1634

[P2] Preserve the edited request's model override as well as its effort. With chat.editRequests: input, editing an earlier request restores its model through ChatWidget → requestModelByIdentifier(). If that request used model B and the current agent's pin to A has been re-armed, this selects B with ProgrammaticSelection but records no model override. Navigating away and back or reloading then lets _syncFromModel() reapply A. Record override intent in the identifier resolver before applying B, and sync the changed intent even if B was already selected, as the qualified-name path does. Add an edit → restore regression under a model-pinned agent.

3 resolved since last review

Review effort: Balanced

…n fixture

- Editing an earlier request restores its model by identifier; record a
  model override when it differs from the agent's resolved pin, in the
  resolver before it applies, and sync the changed intent, as authored
  requests do, so later restores and reloads keep it.
- Give the existing request-restoration fixture in
  chatModelConfigurationStore.test.ts a current (built-in) mode, which
  `requestModelByIdentifier` now reads.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dknappettmsft

Copy link
Copy Markdown
Author

Addressed the previously missed finding (Preserve the edited request's model override as well as its effort, chatInputPart.ts) in 942ad59: when editing an earlier request restores its model through requestModelByIdentifier(), a model override is now recorded in the resolver before the model applies if it differs from the current agent's resolved pin, and the changed intent is synced even when that model was already selected, as the qualified-name path does. Navigating away and back or reloading therefore keeps the request's model instead of re-applying the pin. Added an edit → restore regression under a model-pinned agent.

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

Interacting restoration and cross-client synchronization paths need human review, passing targeted tests, and manual multi-window verification before approval.

1 open issue
1 resolved since last review

Review effort: Balanced

Capture an explicitly chosen `null` underneath the agent's overlay, as
nullable configuration schemas accept it, so restoring the conversation
recovers `null` rather than another conversation's shared preference.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

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

Cross-client persistence and restoration need current-commit test results and multi-window verification, and an overlay schema-validation issue remains unresolved.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Validate overlay values with the full schema filter

src/​vs/​workbench/​contrib/​chat/​browser/​widget/​input/​chatModelConfigurationStore.ts:166

[P2] Validate presented values with the existing schema filter, not just the enum. For example, a host can advertise reasoningEffort with enum: ['low', 'high'] and const: 'low'. A High pin passes this check and is returned in the picker/request configuration, even though filterConfigurationToSchema rejects it. Pattern and length constraints are also skipped. Reuse that filter before adding overlay provenance, retaining the read-only check, and add a regression for an enum value excluded by const. This also keeps overlays consistent with the filter's rejection of unsupported composite schemas.

1 resolved since last review

Review effort: Balanced

dknappettmsft and others added 2 commits October 23, 2026 17:50
Check the agent's effort overlay with `filterConfigurationToSchema`
(enum including disabled items, const, type, pattern, length and
unsupported composites) plus the read-only check, so a value the schema
rejects never reaches the picker or request configuration.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dknappettmsft

Copy link
Copy Markdown
Author

Addressed the previously missed finding (Validate overlay values with the full schema filter, chatModelConfigurationStore.ts) in fb4d73b: presented overlay values are now checked with filterConfigurationToSchema, the same filter restores use, which covers the enum including disabled items, const, type, pattern and length constraints, and rejects unsupported composite schemas. The read-only check is kept. The check runs before the overlay's provenance is added, so a value the schema rejects never reaches the picker or request configuration. Added a regression where const excludes an advertised enum value.

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

Cross-client synchronization and restoration changes need passing latest targeted CI and manual multi-window validation before human approval.

No open issues 🎉

Review effort: Balanced

@dknappettmsft
dknappettmsft marked this pull request as ready for review October 23, 2026 17:47
@aeschli

Copy link
Copy Markdown
Contributor

replaced by #345671

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.

Custom agent model pin is overridden by the model selected in Copilot chat or the GitHub cloud window

3 participants