## TL;DR
Concatenating two notebooks' JSON by hand duplicates `metadata.kernelspec` and `language_info` (and cell ids), leaving a file that confuses Jupyter about which kernel it needs. Merge structurally: take cells from notebook B into a copy of notebook A with nbformat, keep A's metadata (or reconcile deliberately), and regenerate duplicate cell ids. Validate the merged result before writing.

```text
agent merged two notebooks and duplicated the kernel metadata, execution failed
```

## Use this when
- A merged notebook has doubled kernelspec entries or conflicting metadata.
- The merged notebook opens with the wrong kernel or fails to execute.
- The agent merges by concatenating JSON.

## Not for this skill when
- Merging by hand in the UI. Copy cells between open notebooks instead.
- The merge loses cells. That is a different bug.

## Steps
1. Load both with nbformat. Verify: both parse and validate.
2. Create the merged notebook from A's object; extend its cells with B's cells. Verify: one metadata block survives.
3. Reconcile kernelspec deliberately: keep A's, or set the intended one explicitly. Verify: exactly one kernelspec.
4. Regenerate any duplicate cell ids. Verify: all ids are unique.
5. Validate and write: `nbformat.validate(merged)` then write. Verify: it opens and executes.

## Variant phrasings
### merge notebooks duplicate kernelspec
Short form. One metadata block, deliberate choice.

### agent merge ipynb metadata
The agent phrasing. Structural merge, not JSON concat.

### combined notebook wrong kernel
The symptom phrasing. Reconcile the kernelspec.

Compatibility: nbformat 5.x. Cell ids exist in v4.5+; regenerate on merge.

## Why it happens
A notebook file has one metadata block describing the whole document, but a naive merge concatenates two documents' structures, producing doubled or conflicting metadata. Jupyter reads the first kernelspec it finds, which may be the wrong one, and the kernel launch then fails or uses the wrong environment. Structural merging keeps the document model intact: cells are a list, metadata is singular.

## Edge cases / pitfalls
- Cells referencing variables defined in the other notebook may not run standalone. Order the merged cells deliberately.
- Duplicate cell ids break some tools silently. Always regenerate on merge.
- Execution counts from both notebooks will collide. Clear them on merge.
- If the notebooks used different kernels, pick one and note the choice. Do not leave it ambiguous.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_6KpFK3IOuIOPGltpJNWqKw
