Skip to content

fix: guard against undefined isDefaultForLocation in deserialized chat state (fixes #317793)#317802

Open
vs-code-engineering[bot] wants to merge 1 commit into
mainfrom
fix/chat-isDefaultForLocation-undefined-317793-274a3fd1432a8f23
Open

fix: guard against undefined isDefaultForLocation in deserialized chat state (fixes #317793)#317802
vs-code-engineering[bot] wants to merge 1 commit into
mainfrom
fix/chat-isDefaultForLocation-undefined-317793-274a3fd1432a8f23

Conversation

@vs-code-engineering
Copy link
Copy Markdown
Contributor

🔧 Error Fix

Summary

Error: TypeError: Cannot read properties of undefined (reading 'panel')

When chat input state is restored from storage (e.g., on session switch or window reload), the selectedModel.metadata.isDefaultForLocation field may be undefined if the state was persisted before this field was introduced. When setCurrentLanguageModel then accesses model.metadata.isDefaultForLocation[this.location] (where this.location is 'panel'), it throws a TypeError.

The root cause is the deserialization path in ChatModel constructor that copies metadata directly from serialized state without ensuring the newer isDefaultForLocation field exists.

Fixes #317793
Recommended reviewer: @justschen

Culprit Commit

The crash was introduced when isDefaultForLocation was added to setCurrentLanguageModel to store whether the selected model is the default for the current location. The deserialization path in chatModel.ts was not updated to ensure this field exists on restored state, creating a mismatch between persisted data (old format without isDefaultForLocation) and runtime expectations (field must be an object for property access).

Exact culprit commit not identifiable from shallow clone, but the regression appeared in the 0958016b...c305abcf commit range (1.121.0-insider).

Code Flow

sequenceDiagram
    participant Storage as Storage (old format)
    participant CM as ChatModel constructor
    participant IM as InputModel
    participant Auto as autorun
    participant Sync as _syncFromModel
    participant Set as setCurrentLanguageModel

    Storage->>CM: serialized inputState (no isDefaultForLocation)
    CM->>IM: new InputModel(state)
    Note over IM: metadata.isDefaultForLocation = undefined
    Auto->>Sync: model.state.read(reader)
    Sync->>Set: setCurrentLanguageModel(state.selectedModel)
    Set->>Set: model.metadata.isDefaultForLocation[this.location]
    Note over Set: TypeError: undefined['panel']
Loading

Affected Files

File Role
src/vs/workbench/contrib/chat/common/model/chatModel.ts Producer — deserializes selectedModel.metadata without ensuring isDefaultForLocation exists
src/vs/workbench/contrib/chat/browser/widget/input/chatInputPart.ts Crash site — accesses isDefaultForLocation[location] in setCurrentLanguageModel (line 1241)

Repro Steps

  1. User has chat sessions persisted from a VS Code version before isDefaultForLocation was added to the metadata schema
  2. User upgrades to 1.121.0 which expects isDefaultForLocation to be an object
  3. On session restore/switch, _syncFromModel calls setCurrentLanguageModel with the deserialized model
  4. model.metadata.isDefaultForLocation is undefined → TypeError accessing ['panel']

How the Fix Works

Chosen approach (src/vs/workbench/contrib/chat/common/model/chatModel.ts:2404-2406):

Added ?? {} fallback for isDefaultForLocation during deserialization of the input model state. This ensures the field is always a valid object when the model is constructed from serialized storage, fixing the data at the producer (deserialization site) rather than adding a guard at the crash site (the data-producer principle). This matches the existing migration pattern already present in emptyInputState.fromStorage (line 215-220 of chatInputPart.ts).

Alternatives considered:

  • Adding ?. at model.metadata.isDefaultForLocation?.[this.location] in setCurrentLanguageModel — rejected because it patches the crash site rather than fixing the producer of invalid data.

Recommended Owner

@justschen — most recent contributor to input state sync logic in chatInputPart.ts and model state management.

Generated by errors-fix · ● 68.3M ·

…t model state (fixes #317793)

When chat input state is restored from storage, the selectedModel.metadata
may lack isDefaultForLocation if it was persisted before this field existed.
This causes a TypeError in setCurrentLanguageModel when accessing
model.metadata.isDefaultForLocation[this.location].

Fix by defaulting isDefaultForLocation to {} during deserialization,
matching the migration already present in emptyInputState.fromStorage.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 21, 2026 16:38
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@vs-code-engineering vs-code-engineering Bot marked this pull request as ready for review May 21, 2026 16:45
@vs-code-engineering vs-code-engineering Bot enabled auto-merge (squash) May 21, 2026 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Error] unhandlederror-Cannot read properties of undefined (reading 'panel')

2 participants