fix(cli): tolerate malformed notebook cells

This commit is contained in:
marius-kilocode
2026-05-29 15:58:52 +02:00
parent 4967c22861
commit 470e59fd52
2 changed files with 26 additions and 4 deletions
@@ -28,16 +28,17 @@ const render = (kind: "markdown" | "code", text: string) => {
export async function open(filepath: string): Promise<Readable | undefined> {
if (path.extname(filepath).toLowerCase() !== ".ipynb") return undefined
const data = parse((await Encoding.read(filepath)).text)
if (!object(data) || !Array.isArray(data.cells)) return undefined
const raw = (await Encoding.read(filepath)).text
const data = parse(raw)
if (!object(data) || !Array.isArray(data.cells)) return Readable.from([raw])
const cells: string[] = []
for (const cell of data.cells) {
if (!object(cell)) return undefined
if (!object(cell)) continue
if (cell.cell_type !== "markdown" && cell.cell_type !== "code") continue
const text = source(cell.source)
if (text === undefined) return undefined
if (text === undefined) continue
cells.push(render(cell.cell_type, text))
}
@@ -93,6 +93,27 @@ describe("kilocode notebook reads", () => {
}),
)
it.live("skips invalid cells without exposing raw notebook payloads", () =>
Effect.gen(function* () {
const dir = yield* tmpdirScoped()
const filepath = path.join(dir, "partial.ipynb")
const content = JSON.stringify({
cells: [
null,
{ cell_type: "code", source: null, outputs: ["INVALID_OUTPUT_SHOULD_NOT_APPEAR"] },
{ cell_type: "markdown", source: ["Readable cell"], metadata: { marker: "CELL_METADATA_SHOULD_NOT_APPEAR" } },
],
})
yield* put(filepath, content)
const result = yield* run(dir, { filePath: filepath })
expect(result.output).toContain("Readable cell")
expect(result.output).not.toContain("INVALID_OUTPUT_SHOULD_NOT_APPEAR")
expect(result.output).not.toContain("CELL_METADATA_SHOULD_NOT_APPEAR")
}),
)
it.live("applies read pagination to extracted cell text", () =>
Effect.gen(function* () {
const dir = yield* tmpdirScoped()