Repository navigation
Do not add default metadata to cells when opening the Property Inspector - #20160
Conversation
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The form fills in the schema defaults when it is built and passes them to updateMetadata, which added them to the cell metadata. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks for making a pull request to jupyterlab! |
|
@meeseeksdev tag bug |
|
I submitted issue #19995, and I'd really like to help fix JupyterLab's metadata behavior, so with assistance from Codex (GPT-6 Astra), I tested The additional tests found a conditional-schema issue in For example, suppose the inactive There is also an initialization case: if the branches have different defaults and Could the lookup use the schema resolved for the current form data, consistent with RJSF, rather than the first default found across branches? Attached are four additional tests: two passing controls and the two failing cases above. They render real RJSF forms; the explicit-edit case calls |
Co-authored-by: dturaev <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lects Evaluate each `if` with the form's validator, as the form does, instead of taking the first default found in either branch. The validator loading moves to a module shared by the form and the widget. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thank you for testing this, and for the tests. You're right: scanning both branches made an explicit value disappear when it matched the inactive branch's default, and the active
I added your four cases to |
|
(Update: see the next comment for a simpler approach that replaces patch 2.) Thanks for the update. With help from Claude and Codex, I re-tested Common Tools behaves as intended in the cases I tested: opening it and moving between code, markdown and raw cells without the relevant metadata does not add defaults or dirty the notebook. Existing The attached follow-up addresses two remaining problems. 1. Preserve existing values of conditional fields
Patch 1 loads all 2. Compare with the values RJSF actually fills inDuring initialization, the form supplies the stored metadata to
Patch 2 computes the initialization baseline with RJSF’s Remaining limitation: when switching the condition makes RJSF fill in a field that only the new branch has, that field’s default is saved. On 32eb77e, the same switch instead saves the old branch’s value that RJSF carries over; main saves both. Fixing this needs additional handling and regression coverage; this series doesn’t attempt it. Validation and integration
The six baseline failures are five added cases and the strengthened initialization assertion. The other added case provides passing coverage of an explicit edit. The two commits apply to For #20002, the constructor conflict can be resolved by retaining both its Forms used in the browser tests: Metadata Form user settings{
"metadataforms": [
{
"id": "conditionalTestSection",
"label": "Conditional test",
"metadataSchema": {
"type": "object",
"properties": {
"/condition": { "title": "Condition", "type": "boolean" }
},
"allOf": [
{
"if": {
"properties": { "/condition": { "const": true } },
"required": ["/condition"]
},
"then": {
"properties": {
"/choice": {
"title": "Choice",
"type": "string",
"default": "then-default"
}
}
},
"else": {
"properties": {
"/choice": {
"title": "Choice",
"type": "string",
"default": "else-default"
}
}
}
}
]
}
},
{
"id": "conditionDefaultTestSection",
"label": "Condition default test",
"metadataSchema": {
"type": "object",
"properties": {
"/cond2": {
"title": "Condition 2",
"type": "boolean",
"default": true
}
},
"allOf": [
{
"if": {
"properties": { "/cond2": { "const": true } },
"required": ["/cond2"]
},
"then": {
"properties": {
"/choice2": {
"title": "Choice 2",
"type": "string",
"default": "then-default"
}
}
},
"else": {
"properties": {
"/choice2": {
"title": "Choice 2",
"type": "string",
"default": "else-default"
}
}
}
}
]
}
}
]
}For the empty-choice case, remove |
The form data was built from the top-level properties only. The form showed the default of a conditional key that has a value, and its first change wrote that default over the value. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01UVgHJTXiavHWWyVC9dv2vF
Compare a value of missing metadata with the value that the form fills in for it, computed from the current metadata with RJSF's getDefaultFormState, instead of selecting the conditional branches separately. RJSF selects the branch of the defaults with the metadata, while it renders the branch that the filled-in data selects, so a condition with a default made the two differ: opening the form wrote the default of the other branch, and a value that the form does not show was skipped. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01UVgHJTXiavHWWyVC9dv2vF
|
Thanks a lot for the careful re-test and for the follow-up patches. I applied both commits as they are, keeping you as the author, and pushed them to this PR. With the new tests on the Comparing with what One small thing: I agree that the remaining case (switching the condition and saving the default of a field that only the new branch has) is better handled separately. Thanks also for the notes on combining this with #20002. |
|
Thank you for adding the changes. Below is also a suggestion for an alternative approach, which may be the better long-term design. It writes only the metadata key of the field the user changed, instead of comparing with the values the form fills in. |
|
Alternative to the fill comparison: explicit field-key tracking The attached patch replaces the comparison with the filled values (9631fba) by tracking the metadata key of each change explicitly. Each property of the root field, i.e. each metadata key, records its key for each change that it passes on, and the form writes only that key. Defaults that the form fills in for other metadata keys (when the form is created, or for a branch that a change selects) are not written. A changed key is written with its whole value, e.g. an object including the defaults filled in it. The patch is also smaller. Compared with 9631fba, it removes 143 lines of source and adds 82: the fill comparison in This is another implementation option. The Common Tools interactions tested produced the same visible behavior with either approach. Differences remain for custom conditional forms, explicit writes equal to a missing key's default, and stale initialization (discussed below). The trade-off is that this hooks into RJSF more deeply: a wrapper around its Known limitations of the fill comparison in 9631fba. If you keep the fill comparison, a short note on these in the PR description would help the maintainers:
Why not take the key from the field id: ids join names with Jest results for
Combined with #20002 (f0dd6b4) and the test changes below, all 45 tests pass. In the browser (dev build), opening the Property Inspector and visiting cells writes nothing; edits in Common Tools and in two conditional forms from the user settings are saved; and a typed value is kept after leaving the cell and coming back. A minor exception, the same on main and in 9631fba: RJSF sets a missing UI tests, needed with either version: the UI tests in Interaction with #20002: whichever PR is merged second should keep #20002's guarded closure for both
Removing the guard fails the callback test, writing the initialization fails the unmount test, and removing only About #19995: I ran #20002's unmount scenario without its guard. With 9631fba, the stale form still overwrites the other cell ( Related: #17164 also ran into the Common Tools defaults while trying to remove metadata. The classic notebook stopped storing default Patch: I prepared this with Claude and Codex. |
RJSF calls onChange both when the user changes a field and when it fills in values itself, e.g. the defaults when the form is created, so writing the whole form data writes these defaults too. Track the metadata key of each change explicitly instead: the field of each metadata key, i.e. each property of the root field, records its key for each change that it passes on, and the form writes only that key. The values that the form fills in for other keys come without such a record. A changed key is written with its whole value, e.g. an object with the defaults filled in it. This replaces skipping the values of missing metadata that equal a default, which also skips such a value when the user enters it. The field id alone does not identify the key: it joins the names of the key and of the nested fields with `_`, which keys may contain too. It only pairs each recorded key with the report of its change. A field may pass on several changes before the form reports them, so the keys wait in a queue per id. A null field sets a missing value to null from an effect, which is recorded like a change. The cell model ignores null for a missing key, but a null field nested in an object still writes the object, as before. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01UVgHJTXiavHWWyVC9dv2vF
Co-Authored-By: Claude Opus 5.5 <[email protected]>
RJSF calls onChange with the id of the changed field as a second argument, and the metadata form relies on it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks @dturaev, this is much nicer than the fill comparison. Tracking the key per field avoids guessing, and it also saves a default that the user types on purpose, which the galata test expects anyway. I applied the patch locally and got the same numbers: 12 failing on main, 3 on 9631fba, 0 with your patch. I've pushed it to this PR with your commit as is, plus two small commits on top:
On #20002: if this one is merged second, I'll keep its guarded closure for both |
|
Thanks for taking it! One remaining documentation test appears to need updating: “Right Sidebar” in |
Opening Common Tools no longer writes the default metadata into the new raw cell, so its metadata has no tags, and the line of the added tag is no longer the eighth line of the metadata editor. Click that line by its content instead. The screenshot of the advanced tools changes as well. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01UVgHJTXiavHWWyVC9dv2vF
|
Thanks for catching that! I've added your commit on top. I can't run galata locally either, so let's see what CI says, and then the advanced tools screenshot can be regenerated. |
|
The only failure left is |
|
bot please open PR to update snapshots |
There was a problem hiding this comment.
🔵 Needs a closer look
Conditional metadata loading can throw, and persistence now depends on callback ordering that still needs runtime validation.
1 open issue
What changed in this PR
This PR prevents the Property Inspector from adding default cell metadata merely when opened or when an unrelated field changes, addressing #17562.
Changes:
- Track field changes and write only the corresponding metadata key.
- Load existing conditional metadata values and extract lazy validator loading.
- Add regression coverage and update browser tests to expect absent defaults.
| File | Description |
|---|---|
yarn.lock |
Record the metadataform utilities dependency. |
packages/ui-components/src/components/form.tsx |
Declare the optional change-event field ID. |
packages/metadataform/test/metadataFormWidget.spec.ts |
Cover defaults, conditional fields, and targeted writes. |
packages/metadataform/src/validator.ts |
Extract lazy validator loading. |
packages/metadataform/src/metadataform.ts |
Load existing values for conditional keys. |
packages/metadataform/src/form.tsx |
Track changed keys and restrict metadata writes. |
packages/metadataform/package.json |
Add the RJSF utilities dependency. |
galata/test/jupyterlab/metadataform.test.ts |
Expect defaults to remain unwritten on opening. |
galata/test/documentation/general.test.ts |
Update metadata expectations and editor targeting. |
Review effort: Balanced
| )) { | ||
| // Include the keys of the conditional branches, so that the form shows | ||
| // their current values instead of their defaults. | ||
| for (let metadataKey of this.metadataKeys) { |
There was a problem hiding this comment.
A fix for this, with tests, is attached to this comment. @b84330808, would you like to include it here, or should I open a separate issue and PR for it once this one is merged?
|
I created the PR https://github.com/b84330808/jupyterlab/pull/1 with snapshots from run 39636560786. |
|
Copilot’s point is valid. The underlying traversal bug predates this PR: ordinary keys such as Writing and removing nested keys have related weaknesses. Removal can even delete a stored primitive because the empty-object cleanup treats it as an empty object. The attached patch treats a non-object parent as a missing path when loading, replaces it with an object when writing a nested value, and preserves it when removing a nonexistent nested value. Eleven tests cover these cases; ten fail without the change, and the eleventh is a control checking that empty objects are still pruned. The focused suite, including #20002’s regressions, passes all 56 tests. Regarding callback ordering: rendered React tests under Jest/jsdom exercise two changes reported in one interaction, colliding field IDs, and a @b84330808, could you add the patch to this PR if it looks good to you? Prepared with help from Claude and Codex. |
Update galata snapshots for PR #20160
The snapshot bot changed it, but this PR doesn't touch the context menu. Co-Authored-By: Claude Opus 5.5 <[email protected]>
They moved to validator.ts so that metadataform.ts could use the validator as well. Since the form writes only the metadata key of the changed field, metadataform.ts no longer uses it, so keep the helpers in form.tsx as on main. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01UVgHJTXiavHWWyVC9dv2vF
#20305) ## References Fixes #20304 The doubled entry also showed up in a documentation snapshot in #20160. The entries were added in 4.5.0 (#17848), so 4.6.x is affected as well. ## Code changes The `create-new-language-file` plugin rebuilds its entries at startup and on `specsChanged`. When the kernel specs arrive after the plugin has activated, a second rebuild starts while the first one is still awaiting `getAvailableKernelFileTypes`. Each rebuild disposed of the previous entries before that await, so both kept their entries. The rebuild now disposes of the previous entries after the await, as the file editor's `updateKernelFileTypeComponents` does, so the last rebuild replaces the entries of the earlier ones. The test is a short Galata test, because the extension package has no Jest setup. It holds back the kernel specs response until the plugin has activated, which makes the race happen on every run. It is in its own commit before the fix. ## User-facing changes "New Python File", and likewise "New R File" and "New Julia File", no longer appear twice in the file browser context menu when the kernel specs load slowly. Kernel specs response held back by 3 seconds: | Before | After | | --- | --- | | <img width="426" height="226" alt="language-entries-before" src="https://github.com/user-attachments/assets/f1c9b46b-3499-433f-9ebb-d9075db8680f" /> | <img width="426" height="202" alt="language-entries-after" src="https://github.com/user-attachments/assets/1223fa87-9dc1-483f-bf5a-fbad63f75230" /> | ## Backwards-incompatible changes None. ## AI usage - **YES**: Some or all of the content of this PR was generated by AI. - **Yes**: The human author has carefully reviewed this PR and run this code - AI tools and models used: Claude (Opus 5.5, max effort)
|
I’m not sure how often users will encounter this, but a custom conditional form can hit it when an inactive branch defines Maybe we should check that intermediate values are non-null objects before using |
|
@MUFFANUJ Thanks, that’s the case Copilot flagged too. The patch attached to this comment does what you suggest: it treats a non-object parent as a missing path when loading. Your example confirms it: on main the form opens, on this PR it throws |
Thanks, I missed that. Verified the patch locally, and it looks fine to include in this PR. |
The metadata form navigated the path of a nested key, such as `/slideshow/slide_type`, with `in` and `Object.keys`. These throw for null and strings, and treat other values that are not objects, such as numbers, as empty objects that are pruned. Treat such a value as missing when the form loads the metadata, replace it by an object when the form writes a value to the nested key, and keep it when the form removes the nested key. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01UVgHJTXiavHWWyVC9dv2vF
When merging main, take form.tsx from main, where the form writes only the metadata key of the changed field (jupyterlab#20160), and route those writes and the custom-field updates through the guarded updateMetadata again. Since jupyterlab#20160, the form no longer writes during its initialization. The tests now observe the stale initialization where RJSF reports it and change fields through the form; a test for an obsolete form of the same cell is added. Also fix the lint errors in the tests. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01UVgHJTXiavHWWyVC9dv2vF
## References Fixes #19995. ## Code changes FormWidget captures the cell it was built for. Updates are skipped when that cell is no longer active, or when the form has been replaced or its owner disposed. Both field edits and custom-field updates use the same guard. This version incorporates main after #20160. It preserves that change's field-key-only writes and avoidance of initialization/default writes. Regression tests cover a form built for a different cell, an obsolete form for the same cell, and a collapsed form that mounts again after the active cell changed, as it does when another notebook gets the focus. The follow-up adapts and includes dturaev's supplied patch and regression test, prepared with AI assistance. > **Added by a maintainer** > > - Rebuilds the form when the notebook tools are attached while visible, and drops it when they are attached while hidden, so that a stale form is not mounted again. > - Holds the model of the source cell through a `WeakRef`, so that a form that is not rebuilt does not keep a closed notebook in memory. ## User-facing changes Edits in a stale metadata form cannot overwrite another cell's metadata or newer metadata from its replacement form. > **Added by a maintainer** > > The Property Inspector shows the tags of the active cell after another document had the focus, so a click on a tag no longer copies the tags of the previous notebook's cell. | Before | After | | --- | --- | | <img width="250" height="170" alt="pr20002-e89df782e1-before" src="https://github.com/user-attachments/assets/8b936f21-abae-4a8e-89f0-9a0f7bda532a" /> | <img width="250" height="170" alt="pr20002-e89df782e1-after" src="https://github.com/user-attachments/assets/639b6b50-8302-470c-b526-ec453c2083f9" /> ## Backwards-incompatible changes None. ## AI usage - **YES**: Some or all of the content of this PR was generated by AI. - **NOT CONFIRMED**: The human author has carefully reviewed this PR and run this code. This requirement has not yet been confirmed; the PR remains a draft pending confirmation. - AI assistance was used for implementation and testing. Automated test runs are not a substitute for the required human review and execution. Claude Code (Opus 5.5, max effort) was used for the maintainer changes. --------- Co-authored-by: dturaev <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: krassowski <[email protected]>
… built for another cell) (#20417) Backport PR #20002 on branch 4.6.x (Do not write metadata from a form built for another cell) #20160 is not on 4.6.x, so the guarded `onChange` still writes the whole form data, and the spec adds the `typeInto` helper that main got from #20160. Co-authored-by: Mrunmayee Daware <[email protected]>

References
Fixes #17562.
#20002 also changes
MetadataFormWidget.updateMetadata, for #19995 (a form writing to a cell it was not built for). This pull request also stops the write captured in #19995, because the form no longer writes the values that it fills in when it is created. #20002 is still needed for edits in a form built for another cell and for custom fields. One of them will need a merge after the other.Code changes
When the form is built, react-jsonschema-form fills in the schema defaults and calls
onChangewith them, and the form wrote every one of them to the cell. Changing one field later also wrote the defaults of all the other fields.The form now writes only the metadata key whose field was changed. The field of each metadata key (each property of the root field) wraps RJSF's
SchemaFieldand passes on every change with its key as the id.onChangewrites only that key, with its whole value. Values that the form fills in by itself come without an id, so they are not written. The key comes from the field'snamerather than from the id that RJSF gives the field, because ids join nested names with_and keys may contain_too.IFormComponentProps.onChangenow declares the id argument that RJSF already passes.The form also loads the current values of conditional keys, so it no longer shows their defaults over existing values. A nested key whose parent is not an object made the form throw, for example
/slideshow/slide_typewith"slideshow": null. Loading the conditional keys also made the form throw for the keys of an inactive branch, such as/options/enabledwith"options": false. The form now treats such a parent as missing when it loads the metadata, replaces it with an object when it writes the nested key, and keeps it when it removes the nested key.updateMetadata()still writes a value that the user changes back to its default, unlesswriteDefaultisfalse. The galata tests that expected defaults right after the form opens now expect them to be missing.@dturaev wrote the tests for conditional schemas and the commits f509456, 9631fba, 93f15be, 17e385b, a339e0e and 74c6522. 93f15be replaced the earlier comparison with the filled-in defaults by the field-key tracking above.
User-facing changes
Expanding Common Tools in the Property Inspector, or changing one field in it, no longer adds
editable: true,deletable: true,slideshow: {"slide_type": ""},tags: []or other default values to the cell metadata.I tested this change in the browser.
Choosing Slide as the Slide Type in Common Tools for a cell whose metadata has
"slideshow": nullnow sets"slideshow": {"slide_type": "slide"}. In 4.6.4 it emptied the Common Tools section and left the metadata unchanged.Backwards-incompatible changes
None.
AI usage