fix: guard against undefined isDefaultForLocation in deserialized chat state (fixes #317793)#317802
Open
vs-code-engineering[bot] wants to merge 1 commit into
Conversation
…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>
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
🔧 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.isDefaultForLocationfield may beundefinedif the state was persisted before this field was introduced. WhensetCurrentLanguageModelthen accessesmodel.metadata.isDefaultForLocation[this.location](wherethis.locationis'panel'), it throws a TypeError.The root cause is the deserialization path in
ChatModelconstructor that copiesmetadatadirectly from serialized state without ensuring the newerisDefaultForLocationfield exists.Fixes #317793
Recommended reviewer:
@justschenCulprit Commit
The crash was introduced when
isDefaultForLocationwas added tosetCurrentLanguageModelto store whether the selected model is the default for the current location. The deserialization path inchatModel.tswas not updated to ensure this field exists on restored state, creating a mismatch between persisted data (old format withoutisDefaultForLocation) 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...c305abcfcommit 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']Affected Files
src/vs/workbench/contrib/chat/common/model/chatModel.tssrc/vs/workbench/contrib/chat/browser/widget/input/chatInputPart.tsRepro Steps
isDefaultForLocationwas added to the metadata schemaisDefaultForLocationto be an object_syncFromModelcallssetCurrentLanguageModelwith the deserialized modelmodel.metadata.isDefaultForLocationisundefined→ TypeError accessing['panel']How the Fix Works
Chosen approach (
src/vs/workbench/contrib/chat/common/model/chatModel.ts:2404-2406):Added
?? {}fallback forisDefaultForLocationduring 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 inemptyInputState.fromStorage(line 215-220 of chatInputPart.ts).Alternatives considered:
?.atmodel.metadata.isDefaultForLocation?.[this.location]insetCurrentLanguageModel— 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.