{"record":{"id":"6856f5d042b96542","repo":"can1357/oh-my-pi","slug":"invalid-notebook-editable-representation-for-dis","errorCode":null,"errorMessage":"Invalid notebook editable representation for ${displayPath}: expected first line to be \"# %% [code] cell:0\", \"# %% [markdown] cell:0\", or \"# %% [raw] cell:0\".","messagePattern":"Invalid notebook editable representation for (.+?): expected first line to be \"# %% \\[code\\] cell:0\", \"# %% \\[markdown\\] cell:0\", or \"# %% \\[raw\\] cell:0\"\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/edit/notebook.ts","lineNumber":175,"sourceCode":"\n\tconst flush = () => {\n\t\tif (!current) return;\n\t\tcells.push({\n\t\t\tcellType: current.cellType,\n\t\t\tcellIndex: current.cellIndex,\n\t\t\tsource: linesToSourceText(current.lines),\n\t\t});\n\t};\n\n\tfor (const line of lines) {\n\t\tconst marker = parseVirtualCellMarker(line);\n\t\tif (marker) {\n\t\t\tflush();\n\t\t\tcurrent = { ...marker, lines: [] };\n\t\t\tcontinue;\n\t\t}\n\t\tif (!current) {\n\t\t\tthrow new Error(\n\t\t\t\t`Invalid notebook editable representation for ${displayPath}: expected first line to be \"# %% [code] cell:0\", \"# %% [markdown] cell:0\", or \"# %% [raw] cell:0\".`,\n\t\t\t);\n\t\t}\n\t\tcurrent.lines.push(unescapeMarkerLikeLine(line));\n\t}\n\tflush();\n\treturn cells;\n}\n\nexport function applyNotebookEditableText(\n\tnotebook: NotebookDocument,\n\ttext: string,\n\tdisplayPath: string,\n): NotebookDocument {\n\tconst parsedCells = parseNotebookEditableText(text, displayPath);\n\tconst usedOriginalCells = new Set<number>();\n\tconst nextNotebook = structuredClone(notebook);\n\tnextNotebook.cells = parsedCells.map(parsedCell => {","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/edit/notebook.ts#L157-L193","documentation":"parseNotebookEditableText converts an editable virtual-text representation of a Jupyter notebook (cells prefixed with '# %% [code|markdown|raw] cell:N' markers) back into cells. The very first line of the payload must be a cell marker; if any line appears before any marker has been seen, the payload does not start with a cell header and cannot be mapped onto notebook cells.","triggerScenarios":"Calling applyNotebookEditableText (via the edit/write tool on a .ipynb path) with text whose first line is not a cell marker — e.g. raw cell source pasted without the '# %% [code] cell:0' header, a truncated payload whose marker line was lost, or a payload starting with a blank line or comment.","commonSituations":"An LLM or script writing notebook edits omits the marker header and writes cell content directly; the notebook was manually hand-edited in the virtual view and the first marker was deleted; a diff/patch dropped the first line.","solutions":["Prefix the payload with a valid first-cell marker such as '# %% [code] cell:0' before the first content line","Re-read the notebook via readEditFileText to get a canonical editable representation and re-apply the edit on top of it","Verify the payload was not truncated or line-shifted (e.g. by a patch tool) and regenerate it"],"exampleFix":"// before\n\"print('hello')\"\n// after\n\"# %% [code] cell:0\\nprint('hello')\"","handlingStrategy":"validation","validationCode":"const firstLine = text.split('\\n', 1)[0];\nif (!/^# %% \\[(code|markdown|raw)\\]( cell:\\d+)?$/.test(firstLine.trim())) {\n  throw new Error(`payload must start with a cell marker, got: ${firstLine.slice(0, 60)}`);\n}","typeGuard":"function startsWithCellMarker(text: string): boolean {\n  return /^# %% \\[(code|markdown|raw)\\] cell:\\d+/.test(text.split('\\n', 1)[0]);\n}","tryCatchPattern":"try {\n  await editNotebook(path, payload);\n} catch (err) {\n  if (err instanceof Error && err.message.includes('Invalid notebook editable representation')) {\n    payload = `# %% [code] cell:0\\n${payload}`;\n    await editNotebook(path, payload);\n  } else throw err;\n}","preventionTips":["Always emit the '# %% [type] cell:N' marker as the literal first line of notebook edit payloads","Re-read the notebook's editable text before editing rather than constructing payloads from raw .ipynb JSON","Validate payload first line before submitting; never let diff tools strip the leading marker line"],"tags":["notebook","format-validation","payload"],"backgroundTag":"invalid-edit-payload-format","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}