From 2d28ab6f251aa261cf11a2d041921f8e6cd80423 Mon Sep 17 00:00:00 2001 From: tomas Date: Fri, 13 Nov 2026 02:22:25 +0000 Subject: [PATCH 1/7] refactor(init-notebook): export the sibling init-notebook lookup and the Windows init-block swap The app preview's run() executes the init notebook outside an editor kernel, so it needs the same lookup and the same Windows replacement for Deepnote Cloud's %%bash init block. Both move out of DeepnoteInitNotebookRunner unchanged; the lookup takes the .deepnote file's URI instead of the notebook document. --- .../deepnoteInitNotebookRunner.node.ts | 102 +++++++++--------- ...epnoteInitNotebookRunner.node.unit.test.ts | 33 +++++- src/notebooks/deepnote/deepnoteTestHelpers.ts | 18 ++++ 3 files changed, 103 insertions(+), 50 deletions(-) diff --git a/src/notebooks/deepnote/deepnoteInitNotebookRunner.node.ts b/src/notebooks/deepnote/deepnoteInitNotebookRunner.node.ts index f624f20397..8545cd3c0d 100644 --- a/src/notebooks/deepnote/deepnoteInitNotebookRunner.node.ts +++ b/src/notebooks/deepnote/deepnoteInitNotebookRunner.node.ts @@ -51,6 +51,57 @@ if os.path.exists("requirements.txt"): else: print("There's no requirements.txt, so nothing to install.")`.trim(); +/** + * The content to run for an init block. On Windows, Deepnote Cloud's default `%%bash` block becomes a Python block + * that installs `requirements.txt` into the kernel's interpreter; any other content is returned unchanged. + */ +export function adaptInitBlockContent(content: string): string { + return process.platform === 'win32' && content.trim() === DEEPNOTE_CLOUD_INIT_NOTEBOOK_BLOCK_CONTENT + ? VSCODE_INIT_NOTEBOOK_BLOCK_CONTENT + : content; +} + +/** + * Scans the `.deepnote` files next to `deepnoteFileUri` (skipping snapshots) and returns the single notebook of the + * first valid init source (same `project.id`, one notebook matching `initNotebookId`), or undefined if none. + */ +export async function findSiblingInitNotebook( + deepnoteFileUri: Uri, + projectId: string, + initNotebookId: string +): Promise { + const dirUri = Uri.joinPath(deepnoteFileUri, '..'); + + let entries: [string, number][]; + try { + entries = await workspace.fs.readDirectory(dirUri); + } catch (error) { + logger.warn(`Failed to read directory ${getDisplayPath(dirUri)} while looking for init notebook:`, error); + return undefined; + } + + for (const [name] of entries) { + if (!name.endsWith(DEEPNOTE_FILE_EXTENSION) || name.endsWith(SNAPSHOT_FILE_SUFFIX)) { + continue; + } + + const candidateUri = Uri.joinPath(dirUri, name); + try { + const candidate: DeepnoteFile = await readDeepnoteProjectFile(candidateUri); + const validation = isValidSiblingInitCandidate(candidate, projectId, initNotebookId); + + if (validation.valid) { + return candidate.project.notebooks[0]; + } + } catch (error) { + // One unreadable/invalid file must not stop the scan of the rest. + logger.warn(`Failed to read candidate init file ${getDisplayPath(candidateUri)}:`, error); + } + } + + return undefined; +} + /** * Runs a project's init notebook (from its sibling `.deepnote` file, referenced by * `project.initNotebookId`) in the kernel on start, and again on restart. Tracked per kernel — not @@ -141,7 +192,7 @@ export class DeepnoteInitNotebookRunner implements IDeepnoteInitNotebookRunner, return; } - const initNotebook = await this.findSiblingInitNotebook(notebook, projectId, initNotebookId); + const initNotebook = await findSiblingInitNotebook(notebook.uri, projectId, initNotebookId); if (!initNotebook) { logger.warn( `No valid sibling init file found for project ${projectId} (initNotebookId ${initNotebookId}), skipping init` @@ -187,48 +238,6 @@ export class DeepnoteInitNotebookRunner implements IDeepnoteInitNotebookRunner, } } - /** - * Scans sibling `.deepnote` files (skipping snapshots) and returns the single notebook of the - * first valid init source (same `project.id`, one notebook matching `initNotebookId`), or - * undefined if none. - */ - private async findSiblingInitNotebook( - notebook: NotebookDocument, - projectId: string, - initNotebookId: string - ): Promise { - const dirUri = Uri.joinPath(notebook.uri, '..'); - - let entries: [string, number][]; - try { - entries = await workspace.fs.readDirectory(dirUri); - } catch (error) { - logger.warn(`Failed to read directory ${getDisplayPath(dirUri)} while looking for init notebook:`, error); - return undefined; - } - - for (const [name] of entries) { - if (!name.endsWith(DEEPNOTE_FILE_EXTENSION) || name.endsWith(SNAPSHOT_FILE_SUFFIX)) { - continue; - } - - const candidateUri = Uri.joinPath(dirUri, name); - try { - const candidate: DeepnoteFile = await readDeepnoteProjectFile(candidateUri); - const validation = isValidSiblingInitCandidate(candidate, projectId, initNotebookId); - - if (validation.valid) { - return candidate.project.notebooks[0]; - } - } catch (error) { - // One unreadable/invalid file must not stop the scan of the rest. - logger.warn(`Failed to read candidate init file ${getDisplayPath(candidateUri)}:`, error); - } - } - - return undefined; - } - /** * Executes the init notebook's code blocks in the kernel. * @param notebook The notebook document (for kernel context) @@ -371,11 +380,8 @@ export class DeepnoteInitNotebookRunner implements IDeepnoteInitNotebookRunner, logger.info(`Executing init notebook block ${i + 1}/${codeBlocks.length}`); try { - // Make sure the init notebook execution works cross-platform - let blockContent = block.content ?? ''; - const isWindows = process.platform === 'win32'; - if (isWindows && blockContent.trim() === DEEPNOTE_CLOUD_INIT_NOTEBOOK_BLOCK_CONTENT) { - blockContent = VSCODE_INIT_NOTEBOOK_BLOCK_CONTENT; + const blockContent = adaptInitBlockContent(block.content ?? ''); + if (blockContent !== (block.content ?? '')) { logger.info( `Replacing Deepnote Cloud init notebook block ${ i + 1 diff --git a/src/notebooks/deepnote/deepnoteInitNotebookRunner.node.unit.test.ts b/src/notebooks/deepnote/deepnoteInitNotebookRunner.node.unit.test.ts index f32c6b8b8e..e307e3cf43 100644 --- a/src/notebooks/deepnote/deepnoteInitNotebookRunner.node.unit.test.ts +++ b/src/notebooks/deepnote/deepnoteInitNotebookRunner.node.unit.test.ts @@ -8,12 +8,14 @@ import { IKernel, IKernelProvider, INotebookKernelExecution } from '../../kernel import { IDisposableRegistry } from '../../platform/common/types'; import { mockedVSCodeNamespaces, resetVSCodeMocks } from '../../test/vscode-mock'; import { IDeepnoteNotebookManager } from '../types'; -import { DeepnoteInitNotebookRunner } from './deepnoteInitNotebookRunner.node'; +import { adaptInitBlockContent, DeepnoteInitNotebookRunner } from './deepnoteInitNotebookRunner.node'; import { createDeepnoteBlock, createDeepnoteFile, createDeepnoteNotebook, - createDeepnoteProject + createDeepnoteProject, + DEEPNOTE_CLOUD_INIT_BLOCK, + WINDOWS_INIT_BLOCK } from './deepnoteTestHelpers'; const PROJECT_ID = '11111111-1111-1111-1111-111111111111'; @@ -426,3 +428,30 @@ suite('DeepnoteInitNotebookRunner', () => { assert.isTrue(resolved); }); }); + +suite('adaptInitBlockContent', () => { + teardown(() => { + sinon.restore(); + }); + + test("replaces Deepnote Cloud's init block with the Python block on Windows, whatever whitespace surrounds it", () => { + sinon.stub(process, 'platform').value('win32'); + + assert.strictEqual(adaptInitBlockContent(DEEPNOTE_CLOUD_INIT_BLOCK), WINDOWS_INIT_BLOCK); + assert.strictEqual(adaptInitBlockContent(`\n ${DEEPNOTE_CLOUD_INIT_BLOCK}\n\n`), WINDOWS_INIT_BLOCK); + }); + + test("keeps Deepnote Cloud's init block on Linux", () => { + sinon.stub(process, 'platform').value('linux'); + + assert.strictEqual(adaptInitBlockContent(DEEPNOTE_CLOUD_INIT_BLOCK), DEEPNOTE_CLOUD_INIT_BLOCK); + }); + + test('keeps any other block on Windows', () => { + sinon.stub(process, 'platform').value('win32'); + const edited = DEEPNOTE_CLOUD_INIT_BLOCK.replace('./requirements.txt', './requirements-dev.txt'); + + assert.strictEqual(adaptInitBlockContent(edited), edited); + assert.strictEqual(adaptInitBlockContent('%%bash\necho hi'), '%%bash\necho hi'); + }); +}); diff --git a/src/notebooks/deepnote/deepnoteTestHelpers.ts b/src/notebooks/deepnote/deepnoteTestHelpers.ts index 443627457f..28b17ba835 100644 --- a/src/notebooks/deepnote/deepnoteTestHelpers.ts +++ b/src/notebooks/deepnote/deepnoteTestHelpers.ts @@ -227,3 +227,21 @@ export function createWorkspaceFolder(uri: Uri, index = 0): WorkspaceFolder { export function serializeProjectFile(projectId: string): string { return serializeDeepnoteFile(createDeepnoteFile({ project: createDeepnoteProject({ id: projectId }) })); } + +/** The code block of Deepnote Cloud's default init notebook. */ +export const DEEPNOTE_CLOUD_INIT_BLOCK = `%%bash +# If your project has a 'requirements.txt' file, we'll install it here. +if test -f requirements.txt + then + pip install -r ./requirements.txt + else echo "There's no requirements.txt, so nothing to install." +fi`; + +/** The block that runs in place of {@link DEEPNOTE_CLOUD_INIT_BLOCK} on Windows. */ +export const WINDOWS_INIT_BLOCK = `import os, sys, subprocess + +if os.path.exists("requirements.txt"): + print("Installing requirements.txt") + subprocess.run([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"], check=False) +else: + print("There's no requirements.txt, so nothing to install.")`; From 7dd1bb9fa86aaa19f9194be1a93216c2142c17a4 Mon Sep 17 00:00:00 2001 From: tomas Date: Fri, 13 Nov 2026 02:22:59 +0000 Subject: [PATCH 2/7] feat(app-preview): run local notebooks for run() POST /__deepnote/v2/runs and GET /__deepnote/v2/runs/:runId now answer as Deepnote does instead of 501. A run executes the workspace notebook with that ID headlessly, through @deepnote/runtime-core, in a new kernel on the toolkit server the extension runs for its .deepnote file: the project's init notebook first with its saved inputs, then the notebook with the page's inputs. The poll returns Deepnote's run shape, so run() resolves and result() returns the notebook's JSON output. - The body, the notebook and the inputs are validated with Deepnote's 400 messages. - Blocks run in Deepnote's sorting-key order: the run's copy is re-keyed, because runtime-core sorts keys with localeCompare. - A failing block's error is its message, or its Python exception's name and value; a failing init block's error starts with "Init notebook failed: ". - A missing interpreter, a declined toolkit install, or a server or kernel that fails to start ends the run as internal_error. - Closing the preview cancels its runs that have not reached their first block. --- README.md | 3 +- .../deepnote/appPreview/appPreviewApi.node.ts | 162 +++- .../appPreviewApi.node.unit.test.ts | 249 ++++++- .../appPreviewCommandHandler.node.ts | 5 +- ...appPreviewCommandHandler.node.unit.test.ts | 9 +- .../appPreview/appPreviewRunner.node.ts | 236 ++++++ .../appPreviewRunner.node.unit.test.ts | 691 ++++++++++++++++++ .../appPreview/appPreviewSession.node.ts | 22 +- .../appPreviewSession.node.unit.test.ts | 49 +- src/notebooks/deepnote/appPreview/types.ts | 38 + src/notebooks/serviceRegistry.node.ts | 3 + src/platform/common/utils/localize.ts | 9 + src/test/mocks/deepnoteRuntimeCore.ts | 66 +- 13 files changed, 1509 insertions(+), 33 deletions(-) create mode 100644 src/notebooks/deepnote/appPreview/appPreviewRunner.node.ts create mode 100644 src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts create mode 100644 src/notebooks/deepnote/appPreview/types.ts diff --git a/README.md b/README.md index 74f977713d..050083b60d 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,8 @@ Run `Deepnote: Preview App` from the Command Palette, or right-click a folder in - The folder is served the way Deepnote serves a published app, from a local address. Edit a file and reload the page to see the change. - The page's script tag for Deepnote's client (`https://deepnote.com/static/app-client/v1.js`) loads a copy of that client bundled with the extension instead, so the preview works without network access. - `inputs()` returns the input blocks of the notebook with that ID in the workspace's `.deepnote` files, as saved on disk, so save the notebook and reload the page to see a change. An ID that no workspace notebook has rejects with `Notebook not found`. -- The page's other calls to Deepnote reach a local stand-in that does not support them yet: `appData()` and `run()` reject with a message saying so, and VS Code shows a warning the first time the app calls each of them. +- `run()` runs that notebook, as saved on disk, in a new kernel on the Deepnote server the extension runs for its `.deepnote` file: the project's init notebook first, then the notebook with the page's input values. It uses the notebook's Python interpreter, and its SQL blocks use the integrations configured in VS Code, except BigQuery integrations that use Google OAuth. Like a run in Deepnote, it is not read-only: what its code writes, such as files or stored dataframes, is written. Closing the preview cancels its runs whose blocks have not started. +- The page's other calls to Deepnote reach a local stand-in that does not support them yet: `appData()` rejects with a message saying so, and VS Code shows a warning the first time the app calls it. - Previewing needs a trusted workspace and the desktop app. ### Database integrations diff --git a/src/notebooks/deepnote/appPreview/appPreviewApi.node.ts b/src/notebooks/deepnote/appPreview/appPreviewApi.node.ts index 45bd7183b5..a8dc0b1f69 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewApi.node.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewApi.node.ts @@ -1,32 +1,89 @@ +import { getInputBlockValueOverrideValidationError, isInputBlock } from '@deepnote/blocks'; // TODO: Import from `@deepnote/cloud` once it exports the API's generated types. The mock package is only a // devDependency, which is fine for a type-only import because the build erases it. import type { operations } from '@deepnote/cloud-mock'; import { timingSafeEqual } from 'crypto'; import express, { type NextFunction, type Request, type Response, type Router } from 'express'; +import { CancellationTokenSource } from 'vscode'; +import { z } from 'zod'; +import { generateUuid } from '../../../platform/common/uuid'; +import type { DeepnoteNotebook } from '../../../platform/deepnote/deepnoteTypes'; import { logger } from '../../../platform/logging'; import { blocksInNotebookOrder, toNotebookInputs, type WorkspaceNotebook } from './appPreviewWorkspace.node'; +import type { AppPreviewInputValue, AppPreviewRunOutcome, AppPreviewRunRequest, IAppPreviewRunner } from './types'; -export type AppPreviewOperation = 'appData()' | 'run()' | 'dataframe()'; +export type AppPreviewOperation = 'appData()' | 'dataframe()'; export interface AppPreviewApiOptions { token: string; findNotebook(notebookId: string): Promise; onUnsupported(operation: AppPreviewOperation): void; + runner: IAppPreviewRunner; +} + +/** A run as Deepnote's `GET /v2/runs/:runId` answers it, without the snapshot fields a local run never has. */ +export interface PreviewRun { + completedAt: string | null; + createdAt: string; + error: string | null; + notebookId: string; + runId: string; + snapshotBlocks: AppPreviewRunOutcome['snapshotBlocks']; + snapshotStatus: 'pending' | 'available' | 'unavailable'; + status: 'pending' | 'running' | AppPreviewRunOutcome['status']; +} + +export interface AppPreviewApi { + router: Router; + /** The preview's runs by ID, kept for the preview's lifetime. */ + runs: Map; + /** Cancels every run that has not finished. */ + dispose(): void; } // Protocol strings: app code receives them, so they stay English like Deepnote's API messages. const UNAUTHORIZED_MESSAGE = 'Unauthorized'; const NOT_AVAILABLE_MESSAGE = 'This endpoint is not available to app viewer tokens'; const NOTEBOOK_NOT_FOUND_MESSAGE = 'Notebook not found'; +const RUN_NOT_FOUND_MESSAGE = 'Run not found'; const INTERNAL_ERROR_MESSAGE = 'Internal server error'; type AppViewerNotebook = operations['getNotebook']['responses'][200]['content']['application/json']['notebook']; +const createRunBodySchema = z.strictObject({ + notebookId: z.string().min(1), + inputs: z.record(z.string().min(1), z.union([z.string(), z.boolean(), z.array(z.string())])).optional(), + detachedRunStorageMode: z.enum(['read_write', 'readonly']).optional() +}); + function notSupportedMessage(operation: AppPreviewOperation): string { return `${operation} is not supported in the local preview yet.`; } +function errorMessage(error: unknown): string { + return error instanceof Error ? error.message : String(error); +} + +/** Deepnote's input check: every input names an input block of the notebook, and suits each block of that name. */ +function inputsError(notebook: DeepnoteNotebook, inputs: Record): string | undefined { + const inputBlocks = notebook.blocks.filter(isInputBlock); + for (const [name, value] of Object.entries(inputs)) { + const blocks = inputBlocks.filter((block) => block.metadata.deepnote_variable_name === name); + if (blocks.length === 0) { + return `Input "${name}" is not defined for this notebook`; + } + for (const block of blocks) { + const error = getInputBlockValueOverrideValidationError(block, value); + if (error) { + return `Input "${name}" ${error}`; + } + } + } + + return undefined; +} + function isAuthorized(authorizationHeader: string | undefined, token: string): boolean { if (authorizationHeader === undefined) { return false; @@ -38,6 +95,17 @@ function isAuthorized(authorizationHeader: string | undefined, token: string): b return actual.length === expected.length && timingSafeEqual(actual, expected); } +/** Deepnote's 400 message for a body that fails its schema: each issue's path and message, a root-level one as is. */ +function readableErrors(error: z.ZodError): string { + return error.issues + .map((issue) => + issue.path.length === 0 + ? issue.message + : `${issue.path.map(String).join('.')} ${issue.message.toLowerCase()}` + ) + .join('; '); +} + /** * The `notebook` of Deepnote's `GET /v2/notebooks/:notebookId` answer. Notebooks carry no timestamps in a `.deepnote` * file, so `createdAt` is the file's, and a local notebook has no Deepnote schedule or runs. @@ -60,17 +128,46 @@ function toAppViewerNotebook({ file, notebook }: WorkspaceNotebook): AppViewerNo /** * The local stand-in for Deepnote's app viewer API, mounted at `/v2` on the preview server. * Requests without the preview's bearer token get 401. `GET /notebooks/:notebookId` answers from the workspace's - * notebooks, the other routes Deepnote's client calls or is about to call get 501, and every other route gets 403, as - * Deepnote answers app viewer tokens. A handler that fails is logged and gets a JSON 500. + * notebooks, and `POST /runs` runs one with `runner`, which `GET /runs/:runId` then reports on. The other routes + * Deepnote's client calls or is about to call get 501, and every other route gets 403, as Deepnote answers app viewer + * tokens. A handler that fails is logged and gets a JSON 500. */ -export function createAppPreviewApi(options: AppPreviewApiOptions): Router { +export function createAppPreviewApi(options: AppPreviewApiOptions): AppPreviewApi { const router = express.Router(); + const runs = new Map(); + const unfinished = new Set(); const unsupported = (operation: AppPreviewOperation) => (_req: Request, res: Response) => { options.onUnsupported(operation); res.status(501).json({ message: notSupportedMessage(operation) }); }; + const startRun = (run: PreviewRun, request: AppPreviewRunRequest) => { + const cancellation = new CancellationTokenSource(); + unfinished.add(cancellation); + void options.runner + .run(request, cancellation.token, () => { + run.status = 'running'; + }) + .catch((error): AppPreviewRunOutcome => { + logger.error(`AppPreviewApi: Run ${run.runId} failed`, error); + + return { status: 'internal_error', error: errorMessage(error), snapshotBlocks: null }; + }) + .then((outcome) => { + run.status = outcome.status; + run.error = outcome.error; + run.snapshotBlocks = outcome.snapshotBlocks; + run.snapshotStatus = + outcome.status === 'success' || outcome.status === 'error' ? 'available' : 'unavailable'; + run.completedAt = new Date().toISOString(); + }) + .finally(() => { + unfinished.delete(cancellation); + cancellation.dispose(); + }); + }; + router.use((req: Request, res: Response, next: NextFunction) => { if (!isAuthorized(req.headers.authorization, options.token)) { res.status(401).json({ message: UNAUTHORIZED_MESSAGE }); @@ -90,8 +187,55 @@ export function createAppPreviewApi(options: AppPreviewApiOptions): Router { res.json({ notebook: toAppViewerNotebook(found) }); }); router.get('/notebooks/:notebookId/app-data', unsupported('appData()')); - router.post('/runs', unsupported('run()')); - router.get('/runs/:runId', unsupported('run()')); + router.post('/runs', express.json(), async (req: Request, res: Response) => { + const parsed = createRunBodySchema.safeParse(req.body, { + // Deepnote's error map, so a missing field reads `is required`. + error: (issue) => (issue.code === 'invalid_type' && issue.input === undefined ? 'is required' : undefined) + }); + if (!parsed.success) { + res.status(400).json({ message: readableErrors(parsed.error) }); + + return; + } + + const { notebookId, inputs = {} } = parsed.data; + const found = await options.findNotebook(notebookId); + // Unlike its GET routes, Deepnote answers 400 here. + if (!found) { + res.status(400).json({ message: NOTEBOOK_NOT_FOUND_MESSAGE }); + + return; + } + const inputError = inputsError(found.notebook, inputs); + if (inputError) { + res.status(400).json({ message: inputError }); + + return; + } + + const run: PreviewRun = { + runId: generateUuid(), + notebookId, + status: 'pending', + snapshotStatus: 'pending', + snapshotBlocks: null, + error: null, + createdAt: new Date().toISOString(), + completedAt: null + }; + runs.set(run.runId, run); + startRun(run, { fileUri: found.fileUri, notebookId, inputs }); + res.status(202).json({ runId: run.runId, status: run.status, createdAt: run.createdAt }); + }); + router.get('/runs/:runId', (req: Request<{ runId: string }>, res: Response) => { + const run = runs.get(req.params.runId); + if (!run) { + res.status(404).json({ message: RUN_NOT_FOUND_MESSAGE }); + + return; + } + res.json({ run: { ...run, snapshotExpiresAt: null, snapshotContent: null, snapshotDownloadUrl: null } }); + }); router.get('/projects/:projectId/dataframes/:name', unsupported('dataframe()')); router.use((_req: Request, res: Response) => { @@ -103,5 +247,9 @@ export function createAppPreviewApi(options: AppPreviewApiOptions): Router { res.status(500).json({ message: INTERNAL_ERROR_MESSAGE }); }); - return router; + return { + router, + runs, + dispose: () => unfinished.forEach((cancellation) => cancellation.cancel()) + }; } diff --git a/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts index cfb945c37f..a8afdf38c0 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts @@ -3,8 +3,9 @@ import express from 'express'; import * as http from 'http'; import type { AddressInfo } from 'net'; import * as sinon from 'sinon'; -import { Uri } from 'vscode'; +import { Uri, type CancellationToken } from 'vscode'; +import { isUUID } from '../../../platform/common/uuid'; import { logger } from '../../../platform/logging'; import { createDeepnoteBlock, @@ -12,10 +13,13 @@ import { createDeepnoteNotebook, createDeepnoteProject } from '../deepnoteTestHelpers'; -import { createAppPreviewApi, type AppPreviewOperation } from './appPreviewApi.node'; +import { createAppPreviewApi, type AppPreviewApi, type AppPreviewOperation } from './appPreviewApi.node'; import type { WorkspaceNotebook } from './appPreviewWorkspace.node'; +import type { AppPreviewRunOutcome, AppPreviewRunRequest, IAppPreviewRunner, SnapshotBlock } from './types'; const TOKEN = 'preview-token'; +const SALES_FILE_URI = Uri.file('/workspace/sales.deepnote'); +const NOW = '2026-01-02T03:04:05.000Z'; const SALES_FILE = createDeepnoteFile({ metadata: { createdAt: '2025-03-04T05:06:07.000Z' }, @@ -45,30 +49,77 @@ const SALES_FILE = createDeepnoteFile({ } } ] + }), + createDeepnoteNotebook({ + id: 'nb-limits', + name: 'Limits', + blocks: [ + { + id: 'limit', + type: 'input-text', + blockGroup: 'g', + sortingKey: 'a0', + metadata: { deepnote_variable_name: 'limit', deepnote_variable_value: '5' } + } + ] }) ] }) }); +interface RunnerCall { + request: AppPreviewRunRequest; + token: CancellationToken; + onRunning(): void; + finish(outcome: AppPreviewRunOutcome): void; + fail(error: Error): void; +} + +/** A runner whose runs stay unfinished until the test finishes or fails them. */ +class FakeRunner implements IAppPreviewRunner { + public readonly calls: RunnerCall[] = []; + + public run(request: AppPreviewRunRequest, token: CancellationToken, onRunning: () => void) { + return new Promise((finish, fail) => + this.calls.push({ request, token, onRunning, finish, fail }) + ); + } +} + +const CHART_SNAPSHOT: SnapshotBlock[] = [ + { + id: 'chart', + type: 'code', + outputs: [{ output_type: 'display_data', data: { 'application/json': { total: 3 } }, metadata: {} }], + metadata: {}, + outputHead: null, + outputTail: null + } +]; + suite('createAppPreviewApi', () => { let server: http.Server; let baseUrl: string; + let api: AppPreviewApi; + let runner: FakeRunner; let findNotebook: sinon.SinonStub<[string], Promise>; let onUnsupported: sinon.SinonStub<[AppPreviewOperation], void>; setup(async () => { + runner = new FakeRunner(); const notebooks = new Map( SALES_FILE.project.notebooks.map((notebook) => [ notebook.id, - { file: SALES_FILE, fileUri: Uri.file('/workspace/sales.deepnote'), notebook } + { file: SALES_FILE, fileUri: SALES_FILE_URI, notebook } ]) ); findNotebook = sinon .stub<[string], Promise>() .callsFake(async (notebookId) => notebooks.get(notebookId)); onUnsupported = sinon.stub<[AppPreviewOperation], void>(); + api = createAppPreviewApi({ token: TOKEN, findNotebook, onUnsupported, runner }); const app = express(); - app.use('/v2', createAppPreviewApi({ token: TOKEN, findNotebook, onUnsupported })); + app.use('/v2', api.router); server = http.createServer(app); await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); baseUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}/v2`; @@ -76,6 +127,7 @@ suite('createAppPreviewApi', () => { teardown(() => { sinon.restore(); + api.dispose(); server.closeAllConnections(); server.close(); }); @@ -84,6 +136,42 @@ suite('createAppPreviewApi', () => { return fetch(`${baseUrl}${path}`, { method, headers: authorization == null ? {} : { authorization } }); } + function createRun(body: unknown): Promise { + return fetch(`${baseUrl}/runs`, { + method: 'POST', + headers: { authorization: `Bearer ${TOKEN}`, 'content-type': 'application/json' }, + body: JSON.stringify(body) + }); + } + + async function getRun(runId: string): Promise> { + const response = await call('GET', `/runs/${runId}`); + assert.strictEqual(response.status, 200); + + return (await response.json()).run; + } + + /** Polls the run until its status is `status`, as the client does. */ + async function waitForStatus(runId: string, status: string): Promise> { + const deadline = Date.now() + 2_000; + for (;;) { + const run = await getRun(runId); + if (run.status === status || Date.now() > deadline) { + assert.strictEqual(run.status, status); + + return run; + } + await new Promise((resolve) => setTimeout(resolve, 5)); + } + } + + async function startRun(): Promise { + const response = await createRun({ notebookId: 'nb-1', inputs: { region: 'Asia' } }); + assert.strictEqual(response.status, 202); + + return (await response.json()).runId; + } + async function assertJson(response: Response, status: number, body: unknown): Promise { assert.strictEqual(response.status, status); assert.isNull(response.headers.get('access-control-allow-origin')); @@ -144,10 +232,159 @@ suite('createAppPreviewApi', () => { ); }); + test('POST /runs starts a run of the notebook with the inputs, and answers 202 with it pending', async () => { + sinon.useFakeTimers({ now: Date.parse(NOW), toFake: ['Date'] }); + + const response = await createRun({ + notebookId: 'nb-1', + inputs: { region: 'Asia' }, + detachedRunStorageMode: 'readonly' + }); + + assert.strictEqual(response.status, 202); + const body = await response.json(); + assert.isTrue(isUUID(body.runId), `${body.runId} is not a UUID`); + assert.deepStrictEqual(body, { runId: body.runId, status: 'pending', createdAt: NOW }); + assert.deepStrictEqual( + runner.calls.map((runCall) => runCall.request), + [{ fileUri: SALES_FILE_URI, notebookId: 'nb-1', inputs: { region: 'Asia' } }] + ); + sinon.assert.notCalled(onUnsupported); + }); + + test('POST /runs runs the notebook without inputs when the body has none', async () => { + assert.strictEqual((await createRun({ notebookId: 'nb-init' })).status, 202); + + assert.deepStrictEqual( + runner.calls.map((runCall) => runCall.request), + [{ fileUri: SALES_FILE_URI, notebookId: 'nb-init', inputs: {} }] + ); + }); + + test("GET /runs/:runId reports the run as pending, running, and then finished, in Deepnote's shape", async () => { + const clock = sinon.useFakeTimers({ now: Date.parse(NOW), toFake: ['Date'] }); + const runId = await startRun(); + const pending = { + runId, + notebookId: 'nb-1', + status: 'pending', + snapshotStatus: 'pending', + snapshotExpiresAt: null, + snapshotContent: null, + snapshotDownloadUrl: null, + snapshotBlocks: null, + error: null, + createdAt: NOW, + completedAt: null + }; + assert.deepStrictEqual(await getRun(runId), pending); + + runner.calls[0].onRunning(); + assert.deepStrictEqual(await getRun(runId), { ...pending, status: 'running' }); + + clock.tick(65_000); + runner.calls[0].finish({ status: 'success', error: null, snapshotBlocks: CHART_SNAPSHOT }); + assert.deepStrictEqual(await waitForStatus(runId, 'success'), { + ...pending, + status: 'success', + snapshotStatus: 'available', + snapshotBlocks: CHART_SNAPSHOT, + completedAt: '2026-01-02T03:05:10.000Z' + }); + sinon.assert.notCalled(onUnsupported); + }); + + const outcomes: [AppPreviewRunOutcome, string][] = [ + [{ status: 'error', error: 'ValueError: no data', snapshotBlocks: CHART_SNAPSHOT }, 'available'], + [{ status: 'internal_error', error: 'The kernel died', snapshotBlocks: null }, 'unavailable'], + [{ status: 'stopped', error: null, snapshotBlocks: null }, 'unavailable'] + ]; + for (const [outcome, snapshotStatus] of outcomes) { + test(`GET /runs/:runId reports a run that ended as ${outcome.status} with its snapshot ${snapshotStatus}`, async () => { + const runId = await startRun(); + + runner.calls[0].finish(outcome); + + const run = await waitForStatus(runId, outcome.status); + assert.deepInclude(run, { ...outcome, snapshotStatus }); + assert.isString(run.completedAt); + }); + } + + test('GET /runs/:runId reports a runner that rejects as an internal error, and logs it', async () => { + const logError = sinon.stub(logger, 'error'); + const runId = await startRun(); + const failure = new Error('runner bug'); + + runner.calls[0].fail(failure); + + assert.deepInclude(await waitForStatus(runId, 'internal_error'), { + error: 'runner bug', + snapshotStatus: 'unavailable', + snapshotBlocks: null + }); + assert.deepStrictEqual( + logError.getCalls().map((logged) => logged.args[1]), + [failure] + ); + }); + + test('GET /runs/:runId answers 404 for a run ID the preview has not started', async () => { + await assertJson(await call('GET', '/runs/run-1'), 404, { message: 'Run not found' }); + }); + + test('dispose() cancels the unfinished runs, and records how the runner ended them', async () => { + const stoppedRunId = await startRun(); + const finishedRunId = await startRun(); + runner.calls[1].finish({ status: 'success', error: null, snapshotBlocks: [] }); + await waitForStatus(finishedRunId, 'success'); + + api.dispose(); + + assert.deepStrictEqual( + runner.calls.map((runCall) => runCall.token.isCancellationRequested === true), + [true, false] + ); + runner.calls[0].finish({ status: 'stopped', error: null, snapshotBlocks: null }); + assert.deepInclude(await waitForStatus(stoppedRunId, 'stopped'), { snapshotStatus: 'unavailable' }); + }); + + const invalidBodies: [string, unknown, string][] = [ + ['without notebookId', { inputs: {} }, 'notebookId is required'], + [ + 'with an empty notebookId', + { notebookId: '' }, + 'notebookId too small: expected string to have >=1 characters' + ], + ['with an empty input name', { notebookId: 'nb-1', inputs: { '': 'x' } }, 'inputs. invalid key in record'], + ['with a number as an input value', { notebookId: 'nb-1', inputs: { a: 1 } }, 'inputs.a invalid input'], + ['with an unknown key', { notebookId: 'nb-1', x: 1 }, 'Unrecognized key: "x"'], + ['for an unknown notebook', { notebookId: 'nb-missing' }, 'Notebook not found'], + [ + 'with an input the notebook does not define', + { notebookId: 'nb-1', inputs: { unknown: 'x' } }, + 'Input "unknown" is not defined for this notebook' + ], + [ + 'with an input only another notebook of the file defines', + { notebookId: 'nb-1', inputs: { limit: '5' } }, + 'Input "limit" is not defined for this notebook' + ], + [ + 'with a value the input block cannot hold', + { notebookId: 'nb-1', inputs: { region: true } }, + 'Input "region" must be a string for input-text' + ] + ]; + for (const [condition, body, message] of invalidBodies) { + test(`POST /runs answers 400 to a body ${condition}, without starting a run`, async () => { + await assertJson(await createRun(body), 400, { message }); + assert.deepStrictEqual(runner.calls, []); + }); + } + const stubs: [string, string, AppPreviewOperation][] = [ ['GET', '/notebooks/nb-1/app-data', 'appData()'], - ['POST', '/runs', 'run()'], - ['GET', '/runs/run-1', 'run()'], ['GET', '/projects/p-1/dataframes/sales%20by%20month', 'dataframe()'] ]; for (const [method, path, operation] of stubs) { diff --git a/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.ts b/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.ts index 17d657ffa3..a463233faa 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.ts @@ -10,6 +10,7 @@ import { basename } from '../../../platform/vscode-path/resources'; import type { AppPreviewOperation } from './appPreviewApi.node'; import { AppPreviewSession } from './appPreviewSession.node'; import { resolvePreviewProject } from './appPreviewWorkspace.node'; +import { IAppPreviewRunner } from './types'; const INTEGRATED_BROWSER_OPEN_COMMAND = 'workbench.action.browser.open'; const SIMPLE_BROWSER_OPEN_COMMAND = 'simpleBrowser.api.open'; @@ -24,7 +25,8 @@ export class AppPreviewCommandHandler implements IExtensionSyncActivationService constructor( @inject(IExtensionContext) private readonly extensionContext: IExtensionContext, - @inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry + @inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry, + @inject(IAppPreviewRunner) private readonly runner: IAppPreviewRunner ) {} public activate(): void { @@ -136,6 +138,7 @@ export class AppPreviewCommandHandler implements IExtensionSyncActivationService const session = new AppPreviewSession({ folder, project: await resolvePreviewProject(folder), + runner: this.runner, // A copy of https://deepnote.com/static/app-client/v1.js, so that previews work offline. scripts: { appClient: Uri.joinPath(this.extensionContext.extensionUri, 'resources', 'appPreview', 'v1.js') diff --git a/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.unit.test.ts index d7142bafc9..b22e9f2e6a 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.unit.test.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewCommandHandler.node.unit.test.ts @@ -14,6 +14,7 @@ import { mockedVSCodeNamespaces, resetVSCodeMocks } from '../../../test/vscode-m import { serializeProjectFile } from '../deepnoteTestHelpers'; import { AppPreviewCommandHandler } from './appPreviewCommandHandler.node'; import { AppPreviewSession } from './appPreviewSession.node'; +import type { IAppPreviewRunner } from './types'; const HOSTED = 'https://deepnote.com/static/app-client/v1.js'; const SIMPLE_BROWSER_OPEN = 'simpleBrowser.api.open'; @@ -92,7 +93,7 @@ suite('AppPreviewCommandHandler', () => { const context = mock(); when(context.subscriptions).thenReturn([]); when(context.extensionUri).thenReturn(Uri.file(path.join(tmp, 'extension'))); - new AppPreviewCommandHandler(instance(context), disposables).activate(); + new AppPreviewCommandHandler(instance(context), disposables, instance(mock())).activate(); const [command, handler] = capture(commands().registerCommand).last(); assert.strictEqual(command, Commands.PreviewApp); previewApp = handler; @@ -286,14 +287,14 @@ suite('AppPreviewCommandHandler', () => { }); assert.strictEqual((await api('/notebooks/nb-1/app-data')).status, 501); - assert.strictEqual((await api('/runs', 'POST')).status, 501); - assert.strictEqual((await api('/runs/run-1')).status, 501); + assert.strictEqual((await api('/projects/project-1/dataframes/sales')).status, 501); + assert.strictEqual((await api('/projects/project-1/dataframes/sales')).status, 501); verify( win().showWarningMessage('The app in app called appData(), which the local preview does not support yet.') ).once(); verify( - win().showWarningMessage('The app in app called run(), which the local preview does not support yet.') + win().showWarningMessage('The app in app called dataframe(), which the local preview does not support yet.') ).once(); }); }); diff --git a/src/notebooks/deepnote/appPreview/appPreviewRunner.node.ts b/src/notebooks/deepnote/appPreview/appPreviewRunner.node.ts new file mode 100644 index 0000000000..20548fee2e --- /dev/null +++ b/src/notebooks/deepnote/appPreview/appPreviewRunner.node.ts @@ -0,0 +1,236 @@ +import { generateSortingKey, type DeepnoteFile } from '@deepnote/blocks'; +import { ExecutionEngine, type BlockExecutionResult, type IError, type IOutput } from '@deepnote/runtime-core'; +import { inject, injectable } from 'inversify'; +import { workspace, type CancellationToken, type Uri } from 'vscode'; + +import { + DeepnoteToolkitDependencyResponse, + IDeepnoteServerStarter, + IDeepnoteToolkitDependencyService +} from '../../../kernels/deepnote/types'; +import { Cancellation } from '../../../platform/common/cancellation'; +import { getDisplayPath, getFilePath } from '../../../platform/common/platform/fs-paths.node'; +import { DeepnoteAppPreview } from '../../../platform/common/utils/localize'; +import { readDeepnoteProjectFile } from '../../../platform/deepnote/deepnoteProjectFileReader'; +import type { DeepnoteNotebook } from '../../../platform/deepnote/deepnoteTypes'; +import { logger } from '../../../platform/logging'; +import * as path from '../../../platform/vscode-path/path'; +import { adaptInitBlockContent, findSiblingInitNotebook } from '../deepnoteInitNotebookRunner.node'; +import { IDeepnoteNotebookInterpreters } from '../deepnoteNotebookInterpreters'; +import { blocksInNotebookOrder } from './appPreviewWorkspace.node'; +import type { + AppPreviewInputValue, + AppPreviewRunOutcome, + AppPreviewRunRequest, + IAppPreviewRunner, + SnapshotBlock +} from './types'; + +// Deepnote's run error for a failing init block, so app code sees the same message as in Deepnote. +const INIT_NOTEBOOK_FAILED_PREFIX = 'Init notebook failed: '; + +const STOPPED: AppPreviewRunOutcome = { status: 'stopped', error: null, snapshotBlocks: null }; + +interface NotebookRun { + failure: BlockExecutionResult | undefined; + outputs: Map; +} + +function errorMessage(error: unknown): string { + return error instanceof Error ? error.message : String(error); +} + +/** runtime-core sets `error` only when running the block threw; a Python exception arrives as an `error` output. */ +function failureMessage(result: BlockExecutionResult): string { + if (result.error?.message) { + return result.error.message; + } + + const output = result.outputs.find((candidate): candidate is IError => candidate.output_type === 'error'); + const detail = output ? [output.ename, output.evalue].filter(Boolean).join(': ') : ''; + + return detail || DeepnoteAppPreview.blockFailed(result.blockId); +} + +async function findInitNotebook( + file: DeepnoteFile, + fileUri: Uri, + notebookId: string +): Promise { + const { id: projectId, initNotebookId, notebooks } = file.project; + if (!initNotebookId || initNotebookId === notebookId) { + return undefined; + } + + const init = + notebooks.find((notebook) => notebook.id === initNotebookId) ?? + (await findSiblingInitNotebook(fileUri, projectId, initNotebookId)); + if (!init) { + logger.warn(`AppPreviewRunner: No init notebook ${initNotebookId} found for ${getDisplayPath(fileUri)}`); + } + + return init; +} + +function internalError(message: string): AppPreviewRunOutcome { + return { status: 'internal_error', error: message, snapshotBlocks: null }; +} + +/** + * A copy of `file` that holds only a copy of `notebook`, with its blocks re-keyed `000000`, `000001`, … in Deepnote's + * order. runtime-core sorts blocks with `localeCompare`, which would run Deepnote's key `a0` before `Z0j`. + */ +function runCopy(file: DeepnoteFile, notebook: DeepnoteNotebook): DeepnoteFile { + const copy = structuredClone({ ...file, project: { ...file.project, notebooks: [notebook] } }); + const [copied] = copy.project.notebooks; + copied.blocks = blocksInNotebookOrder(copied); + copied.blocks.forEach((block, index) => { + block.sortingKey = generateSortingKey(index); + }); + + return copy; +} + +async function runNotebook( + engine: ExecutionEngine, + file: DeepnoteFile, + inputs?: Record +): Promise { + const outputs = new Map(); + let failure: BlockExecutionResult | undefined; + const summary = await engine.runProject(file, { + ...(inputs ? { inputs } : {}), + onBlockDone: (result) => { + outputs.set(result.blockId, result.outputs); + if (!result.success) { + failure = result; + } + } + }); + + // A failed code block an agent added does not stop the run, so only a run that stopped has failed. + return { failure: summary.failedBlocks > 0 ? failure : undefined, outputs }; +} + +/** The run's copy of the notebook, input overrides included, with the outputs of this run. */ +function snapshotBlocksOf(file: DeepnoteFile, outputs: Map): SnapshotBlock[] { + return file.project.notebooks[0].blocks.map((block) => ({ + id: block.id, + type: block.type, + outputs: outputs.get(block.id) ?? [], + metadata: block.metadata ?? {}, + outputHead: null, + outputTail: null + })); +} + +/** + * Runs a workspace notebook for the app preview's `run()` the way Deepnote runs one for an app: in a fresh kernel, the + * init notebook first with its saved inputs, then the notebook with the page's inputs. The kernel is a session of its + * own on the toolkit server the extension runs for the notebook's `.deepnote` file. + */ +@injectable() +export class AppPreviewRunner implements IAppPreviewRunner { + constructor( + @inject(IDeepnoteNotebookInterpreters) private readonly interpreters: IDeepnoteNotebookInterpreters, + @inject(IDeepnoteServerStarter) private readonly serverStarter: IDeepnoteServerStarter, + @inject(IDeepnoteToolkitDependencyService) private readonly toolkit: IDeepnoteToolkitDependencyService + ) {} + + public async run( + request: AppPreviewRunRequest, + token: CancellationToken, + onRunning: () => void + ): Promise { + let running = false; + try { + return await this.execute(request, token, () => { + running = true; + onRunning(); + }); + } catch (error) { + if (!running && token.isCancellationRequested) { + return STOPPED; + } + logger.error( + `AppPreviewRunner: Could not run the notebook ${request.notebookId} of ${getDisplayPath( + request.fileUri + )}`, + error + ); + + return internalError(errorMessage(error)); + } + } + + private async execute( + { fileUri, inputs, notebookId }: AppPreviewRunRequest, + token: CancellationToken, + onRunning: () => void + ): Promise { + const file = await readDeepnoteProjectFile(fileUri); + const target = file.project.notebooks.find((notebook) => notebook.id === notebookId); + if (!target) { + throw new Error(DeepnoteAppPreview.notebookNotInFile(notebookId)); + } + const init = await findInitNotebook(file, fileUri, notebookId); + const targetCopy = runCopy(file, target); + const initCopy = init ? runCopy(file, init) : undefined; + for (const block of initCopy?.project.notebooks[0].blocks ?? []) { + if (block.type === 'code') { + block.content = adaptInitBlockContent(block.content ?? ''); + } + } + + // The integration endpoint gives a kernel its SQL credentials only for notebooks open in VS Code. + await workspace.openNotebookDocument(fileUri); + Cancellation.throwIfCanceled(token); + + const interpreter = await this.interpreters.resolve(fileUri); + Cancellation.throwIfCanceled(token); + if (!interpreter) { + return internalError(DeepnoteAppPreview.noInterpreter); + } + + const dependency = await this.toolkit.ensureToolkitInstalled(interpreter, fileUri, token); + Cancellation.throwIfCanceled(token); + if (dependency !== DeepnoteToolkitDependencyResponse.ok) { + return internalError(DeepnoteAppPreview.toolkitNotInstalled); + } + + const server = await this.serverStarter.startServer(interpreter, fileUri, token); + Cancellation.throwIfCanceled(token); + + logger.info(`AppPreviewRunner: Running the notebook ${notebookId} of ${getDisplayPath(fileUri)}`); + const engine = new ExecutionEngine( + { pythonEnv: getFilePath(interpreter.uri), workingDirectory: path.dirname(fileUri.fsPath) }, + { server } + ); + try { + await engine.start(); + Cancellation.throwIfCanceled(token); + onRunning(); + + if (initCopy) { + const { failure } = await runNotebook(engine, initCopy); + if (failure) { + return { + status: 'error', + error: `${INIT_NOTEBOOK_FAILED_PREFIX}${failureMessage(failure)}`, + snapshotBlocks: snapshotBlocksOf(targetCopy, new Map()) + }; + } + } + + const { failure, outputs } = await runNotebook(engine, targetCopy, inputs); + + return { + status: failure ? 'error' : 'success', + error: failure ? failureMessage(failure) : null, + snapshotBlocks: snapshotBlocksOf(targetCopy, outputs) + }; + } finally { + await engine.stop(); + } + } +} diff --git a/src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts new file mode 100644 index 0000000000..80ac99fc2a --- /dev/null +++ b/src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts @@ -0,0 +1,691 @@ +import { isExecutableBlock, isInputBlock, type DeepnoteBlock, type DeepnoteFile } from '@deepnote/blocks'; +import type { BlockExecutionResult, ExecutionOptions, ExecutionSummary } from '@deepnote/runtime-core'; +import { assert } from 'chai'; +import * as fs from 'fs'; +import * as os from 'os'; +import * as sinon from 'sinon'; +import { anything, instance, mock, when } from 'ts-mockito'; +import { CancellationError, CancellationTokenSource, Uri } from 'vscode'; + +import { + DeepnoteToolkitDependencyResponse, + type DeepnoteServerInfo, + type IDeepnoteServerStarter, + type IDeepnoteToolkitDependencyService +} from '../../../kernels/deepnote/types'; +import type { DeepnoteNotebook } from '../../../platform/deepnote/deepnoteTypes'; +import type { PythonEnvironment } from '../../../platform/pythonEnvironments/info'; +import * as path from '../../../platform/vscode-path/path'; +import { + __getExecutionEngines, + __resetRuntimeCoreMock, + __setExecutionEngineBehavior +} from '../../../test/mocks/deepnoteRuntimeCore'; +import { stubWorkspaceFsOnDisk } from '../../../test/mocks/vscodeFs'; +import { mockedVSCodeNamespaces, resetVSCodeMocks } from '../../../test/vscode-mock'; +import type { IDeepnoteNotebookInterpreters } from '../deepnoteNotebookInterpreters'; +import { + createDeepnoteBlock, + createDeepnoteFile, + createDeepnoteNotebook, + createDeepnoteProject, + DEEPNOTE_CLOUD_INIT_BLOCK, + WINDOWS_INIT_BLOCK +} from '../deepnoteTestHelpers'; +import { AppPreviewRunner } from './appPreviewRunner.node'; +import type { AppPreviewRunOutcome, AppPreviewRunRequest } from './types'; + +const INTERPRETER: PythonEnvironment = { id: 'venv', uri: Uri.file('/venv/bin/python') }; +const SERVER = { url: 'http://127.0.0.1:8888', jupyterPort: 8888, lspPort: 8889 } as DeepnoteServerInfo; + +type BlockResult = Partial>; + +function textInput(id: string, name: string, value: string, sortingKey = 'a0'): DeepnoteBlock { + return { + id, + type: 'input-text', + blockGroup: 'g', + sortingKey, + metadata: { deepnote_variable_name: name, deepnote_variable_value: value } + }; +} + +function notebookOf(id: string, blocks: DeepnoteBlock[]): DeepnoteNotebook { + return createDeepnoteNotebook({ id, name: id, blocks }); +} + +function projectFile(notebooks: DeepnoteNotebook[], initNotebookId?: string): DeepnoteFile { + return createDeepnoteFile({ + project: createDeepnoteProject({ id: 'project-1', notebooks, ...(initNotebookId ? { initNotebookId } : {}) }) + }); +} + +/** runtime-core 0.8.0's order: each notebook's executable blocks, sorted with `localeCompare`. */ +function executionOrder(file: DeepnoteFile): DeepnoteBlock[] { + return file.project.notebooks.flatMap((notebook) => + [...notebook.blocks] + .sort((a, b) => a.sortingKey.localeCompare(b.sortingKey)) + .filter((block) => isExecutableBlock(block)) + ); +} + +/** + * Runs a file as runtime-core 0.8.0 does: it sets the inputs on the file's input blocks in place, runs the executable + * blocks in its order, reports each through `onBlockDone`, and stops at the first failure. Each block's result comes + * from `results`, and is a success without outputs by default. + */ +function runtimeCoreLike(results: Record = {}) { + return async (file: DeepnoteFile, options: ExecutionOptions): Promise => { + for (const block of file.project.notebooks.flatMap((notebook) => notebook.blocks)) { + const name = isInputBlock(block) ? block.metadata.deepnote_variable_name : undefined; + if (isInputBlock(block) && name !== undefined && options.inputs && name in options.inputs) { + block.metadata.deepnote_variable_value = options.inputs[name] as string; + } + } + + const blocks = executionOrder(file); + let executedBlocks = 0; + for (const block of blocks) { + const result: BlockExecutionResult = { + blockId: block.id, + blockType: block.type, + success: true, + outputs: [], + executionCount: null, + durationMs: 1, + ...results[block.id] + }; + await options.onBlockDone?.(result); + executedBlocks++; + if (!result.success) { + return { totalBlocks: blocks.length, executedBlocks, failedBlocks: 1, totalDurationMs: 1 }; + } + } + + return { totalBlocks: blocks.length, executedBlocks, failedBlocks: 0, totalDurationMs: 1 }; + }; +} + +function notebookIdsPerCall(): string[][] { + return engine().runProjectCalls.map((runCall) => runCall.file.project.notebooks.map((notebook) => notebook.id)); +} + +function engine() { + const engines = __getExecutionEngines(); + assert.strictEqual(engines.length, 1, 'the run should create exactly one engine'); + + return engines[0]; +} + +suite('AppPreviewRunner', () => { + let tmp: string; + let steps: string[]; + let interpreters: IDeepnoteNotebookInterpreters; + let toolkit: IDeepnoteToolkitDependencyService; + let serverStarter: IDeepnoteServerStarter; + let cancellation: CancellationTokenSource; + let runner: AppPreviewRunner; + + function write(name: string, file: DeepnoteFile): Uri { + const fileUri = Uri.file(path.join(tmp, name)); + // JSON is YAML, and keeps the sorting keys, which the `.deepnote` serializer would rewrite. + fs.writeFileSync(fileUri.fsPath, JSON.stringify(file)); + + return fileUri; + } + + function run(fileUri: Uri, notebookId: string, inputs: AppPreviewRunRequest['inputs'] = {}) { + return runner.run({ fileUri, notebookId, inputs }, cancellation.token, () => steps.push('running')); + } + + setup(() => { + resetVSCodeMocks(); + __resetRuntimeCoreMock(); + stubWorkspaceFsOnDisk(); + tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'deepnote-app-preview-runner-')); + steps = []; + cancellation = new CancellationTokenSource(); + + when(mockedVSCodeNamespaces.workspace.openNotebookDocument(anything())).thenCall(async (uri: Uri) => { + steps.push(`open ${path.basename(uri.fsPath)}`); + }); + interpreters = mock(); + when(interpreters.resolve(anything())).thenCall(async () => { + steps.push('resolve'); + + return INTERPRETER; + }); + toolkit = mock(); + when(toolkit.ensureToolkitInstalled(anything(), anything(), anything())).thenCall(async () => { + steps.push('ensureToolkit'); + + return DeepnoteToolkitDependencyResponse.ok; + }); + serverStarter = mock(); + when(serverStarter.startServer(anything(), anything(), anything())).thenCall(async () => { + steps.push('startServer'); + + return SERVER; + }); + __setExecutionEngineBehavior({ + start: async () => { + steps.push('start'); + }, + runProject: runtimeCoreLike() + }); + + runner = new AppPreviewRunner(instance(interpreters), instance(serverStarter), instance(toolkit)); + }); + + teardown(() => { + sinon.restore(); + cancellation.dispose(); + fs.rmSync(tmp, { recursive: true, force: true }); + }); + + suite('what it runs', () => { + test("opens the file's notebook, then starts a kernel on the file's toolkit server, then runs", async () => { + const fileUri = write('main.deepnote', projectFile([notebookOf('nb-1', [createDeepnoteBlock()])])); + + await run(fileUri, 'nb-1'); + + assert.deepStrictEqual(steps, [ + 'open main.deepnote', + 'resolve', + 'ensureToolkit', + 'startServer', + 'start', + 'running' + ]); + assert.deepStrictEqual(engine().config, { pythonEnv: '/venv/bin/python', workingDirectory: tmp }); + assert.strictEqual(engine().options?.server, SERVER); + }); + + test('runs the init notebook of the same file first, then the notebook, each on its own', async () => { + const fileUri = write( + 'main.deepnote', + projectFile( + [ + notebookOf('nb-other', [createDeepnoteBlock({ id: 'other' })]), + notebookOf('nb-1', [createDeepnoteBlock({ id: 'main' })]), + notebookOf('nb-init', [createDeepnoteBlock({ id: 'setup' })]) + ], + 'nb-init' + ) + ); + + await run(fileUri, 'nb-1'); + + assert.deepStrictEqual(notebookIdsPerCall(), [['nb-init'], ['nb-1']]); + }); + + test("runs the init notebook from a sibling file of the same project when the file doesn't have it", async () => { + const fileUri = write( + 'main.deepnote', + projectFile([notebookOf('nb-1', [createDeepnoteBlock()])], 'nb-init') + ); + write( + 'init.deepnote', + projectFile([notebookOf('nb-init', [createDeepnoteBlock({ id: 'setup', content: 'import os' })])]) + ); + + await run(fileUri, 'nb-1'); + + assert.deepStrictEqual(notebookIdsPerCall(), [['nb-init'], ['nb-1']]); + assert.deepInclude(engine().runProjectCalls[0].file.project.notebooks[0].blocks[0], { + id: 'setup', + content: 'import os' + }); + }); + + const withoutInit: [string, string | undefined][] = [ + ['the project has no init notebook', undefined], + ['the notebook is the init notebook', 'nb-1'], + ['no file has the init notebook', 'nb-init'] + ]; + for (const [condition, initNotebookId] of withoutInit) { + test(`runs only the notebook when ${condition}`, async () => { + const fileUri = write( + 'main.deepnote', + projectFile([notebookOf('nb-1', [createDeepnoteBlock()])], initNotebookId) + ); + + const outcome = await run(fileUri, 'nb-1'); + + assert.strictEqual(outcome.status, 'success'); + assert.deepStrictEqual(notebookIdsPerCall(), [['nb-1']]); + }); + } + + test("passes the inputs to the notebook's run only, so the init notebook keeps its saved values", async () => { + const file = projectFile( + [ + notebookOf('nb-init', [ + { + id: 'init-x', + type: 'input-checkbox', + blockGroup: 'g', + sortingKey: 'a0', + metadata: { deepnote_variable_name: 'x', deepnote_variable_value: false } + } + ]), + notebookOf('nb-1', [textInput('x', 'x', 'saved')]) + ], + 'nb-init' + ); + const fileUri = write('main.deepnote', file); + const onDisk = fs.readFileSync(fileUri.fsPath, 'utf8'); + + const outcome = await run(fileUri, 'nb-1', { x: 'hello' }); + + const [initCall, notebookCall] = engine().runProjectCalls; + assert.isUndefined(initCall.options.inputs); + assert.deepInclude(initCall.file.project.notebooks[0].blocks[0].metadata, { + deepnote_variable_value: false + }); + assert.deepStrictEqual(notebookCall.options.inputs, { x: 'hello' }); + assert.deepInclude(outcome.snapshotBlocks?.[0].metadata, { deepnote_variable_value: 'hello' }); + assert.strictEqual(fs.readFileSync(fileUri.fsPath, 'utf8'), onDisk); + }); + + test("runs the blocks in Deepnote's sorting key order, which runtime-core's localeCompare would not", async () => { + const fileUri = write( + 'main.deepnote', + projectFile([ + notebookOf('nb-1', [ + createDeepnoteBlock({ id: 'lower', sortingKey: 'a0' }), + createDeepnoteBlock({ id: 'upper', sortingKey: 'Z0j' }), + createDeepnoteBlock({ id: 'first', sortingKey: 'V' }) + ]) + ]) + ); + + const outcome = await run(fileUri, 'nb-1'); + + assert.deepStrictEqual( + executionOrder(engine().runProjectCalls[0].file).map((block) => block.id), + ['first', 'upper', 'lower'] + ); + assert.deepStrictEqual( + outcome.snapshotBlocks?.map((block) => block.id), + ['first', 'upper', 'lower'] + ); + }); + + test("runs Windows' replacement for Deepnote Cloud's init block on Windows, and leaves the file alone", async () => { + sinon.stub(process, 'platform').value('win32'); + const fileUri = write( + 'main.deepnote', + projectFile( + [ + notebookOf('nb-init', [ + createDeepnoteBlock({ id: 'setup', content: DEEPNOTE_CLOUD_INIT_BLOCK }) + ]), + notebookOf('nb-1', [createDeepnoteBlock({ id: 'main', content: DEEPNOTE_CLOUD_INIT_BLOCK })]) + ], + 'nb-init' + ) + ); + const onDisk = fs.readFileSync(fileUri.fsPath, 'utf8'); + + await run(fileUri, 'nb-1'); + + const contents = engine().runProjectCalls.map((runCall) => + runCall.file.project.notebooks[0].blocks.map((block) => (block.type === 'code' ? block.content : '')) + ); + assert.deepStrictEqual(contents, [[WINDOWS_INIT_BLOCK], [DEEPNOTE_CLOUD_INIT_BLOCK]]); + assert.strictEqual(fs.readFileSync(fileUri.fsPath, 'utf8'), onDisk); + }); + + test("runs Deepnote Cloud's init block unchanged on Linux", async () => { + sinon.stub(process, 'platform').value('linux'); + const fileUri = write( + 'main.deepnote', + projectFile( + [ + notebookOf('nb-init', [createDeepnoteBlock({ content: DEEPNOTE_CLOUD_INIT_BLOCK })]), + notebookOf('nb-1', []) + ], + 'nb-init' + ) + ); + + await run(fileUri, 'nb-1'); + + const [initBlock] = engine().runProjectCalls[0].file.project.notebooks[0].blocks; + assert.deepInclude(initBlock, { content: DEEPNOTE_CLOUD_INIT_BLOCK }); + }); + }); + + suite('outcomes', () => { + let fileUri: Uri; + + setup(() => { + fileUri = write( + 'main.deepnote', + projectFile([ + notebookOf('nb-1', [ + textInput('region', 'region', 'Europe', 'a0'), + createDeepnoteBlock({ id: 'chart', sortingKey: 'a1' }), + createDeepnoteBlock({ id: 'table', sortingKey: 'a2' }), + { id: 'notes', type: 'markdown', blockGroup: 'g', sortingKey: 'a3', content: '', metadata: {} } + ]) + ]) + ); + }); + + const chartOutput = { output_type: 'display_data', data: { 'application/json': { total: 3 } }, metadata: {} }; + const errorOutput = { output_type: 'error', ename: 'ValueError', evalue: 'no data', traceback: [] }; + + test('succeeds with every block of the notebook and its outputs, and stops the kernel', async () => { + __setExecutionEngineBehavior({ runProject: runtimeCoreLike({ chart: { outputs: [chartOutput] } }) }); + + const outcome = await run(fileUri, 'nb-1', { region: 'Asia' }); + + assert.deepStrictEqual(outcome, { + status: 'success', + error: null, + snapshotBlocks: [ + { + id: 'region', + type: 'input-text', + outputs: [], + metadata: { deepnote_variable_name: 'region', deepnote_variable_value: 'Asia' }, + outputHead: null, + outputTail: null + }, + { + id: 'chart', + type: 'code', + outputs: [chartOutput], + metadata: {}, + outputHead: null, + outputTail: null + }, + { id: 'table', type: 'code', outputs: [], metadata: {}, outputHead: null, outputTail: null }, + { id: 'notes', type: 'markdown', outputs: [], metadata: {}, outputHead: null, outputTail: null } + ] + }); + assert.strictEqual(engine().stopCalls, 1); + }); + + const failures: [string, BlockResult, string][] = [ + ['a Python exception', { success: false, outputs: [errorOutput] }, 'ValueError: no data'], + [ + 'an error running the block', + { success: false, outputs: [errorOutput], error: new Error('The kernel died') }, + 'The kernel died' + ], + ['no error', { success: false, outputs: [] }, 'Block table failed.'] + ]; + for (const [condition, tableResult, message] of failures) { + test(`fails with the block's message for ${condition}, keeping the outputs so far`, async () => { + __setExecutionEngineBehavior({ + runProject: runtimeCoreLike({ chart: { outputs: [chartOutput] }, table: tableResult }) + }); + + const outcome = await run(fileUri, 'nb-1'); + + assert.strictEqual(outcome.status, 'error'); + assert.strictEqual(outcome.error, message); + assert.deepStrictEqual( + outcome.snapshotBlocks?.map((block) => [block.id, block.outputs]), + [ + ['region', []], + ['chart', [chartOutput]], + ['table', tableResult.outputs], + ['notes', []] + ] + ); + assert.strictEqual(engine().stopCalls, 1); + }); + } + + test('succeeds when only a block an agent added failed, as runtime-core carries on then', async () => { + __setExecutionEngineBehavior({ + runProject: async (_file, options) => { + await options.onBlockDone?.({ + blockId: 'agent-added', + blockType: 'code', + success: false, + outputs: [errorOutput], + executionCount: null, + durationMs: 0 + }); + + return { totalBlocks: 1, executedBlocks: 1, failedBlocks: 0, totalDurationMs: 1 }; + } + }); + + const outcome = await run(fileUri, 'nb-1'); + + assert.strictEqual(outcome.status, 'success'); + assert.isNull(outcome.error); + }); + + test('fails with the init block\'s message after "Init notebook failed: ", without running the notebook', async () => { + const withInit = write( + 'with-init.deepnote', + projectFile( + [ + notebookOf('nb-init', [createDeepnoteBlock({ id: 'setup' })]), + notebookOf('nb-1', [createDeepnoteBlock({ id: 'chart' })]) + ], + 'nb-init' + ) + ); + __setExecutionEngineBehavior({ + runProject: runtimeCoreLike({ setup: { success: false, outputs: [errorOutput] } }) + }); + + const outcome = await run(withInit, 'nb-1'); + + assert.deepStrictEqual(outcome, { + status: 'error', + error: 'Init notebook failed: ValueError: no data', + snapshotBlocks: [ + { id: 'chart', type: 'code', outputs: [], metadata: {}, outputHead: null, outputTail: null } + ] + }); + assert.deepStrictEqual(notebookIdsPerCall(), [['nb-init']]); + assert.strictEqual(engine().stopCalls, 1); + }); + + test('is an internal error, without a kernel, when the notebook has no interpreter', async () => { + when(interpreters.resolve(anything())).thenResolve(undefined); + + const outcome = await run(fileUri, 'nb-1'); + + assert.deepStrictEqual(outcome, { + status: 'internal_error', + error: 'No Python interpreter is selected for the notebook. Select one in VS Code, then run the app again.', + snapshotBlocks: null + }); + assert.deepStrictEqual(__getExecutionEngines(), []); + }); + + const declined = [ + DeepnoteToolkitDependencyResponse.cancel, + DeepnoteToolkitDependencyResponse.selectDifferentInterpreter, + DeepnoteToolkitDependencyResponse.failed + ]; + for (const response of declined) { + test(`is an internal error, without a server, when the toolkit install answers ${DeepnoteToolkitDependencyResponse[response]}`, async () => { + when(toolkit.ensureToolkitInstalled(anything(), anything(), anything())).thenResolve(response); + + const outcome = await run(fileUri, 'nb-1'); + + assert.deepStrictEqual(outcome, { + status: 'internal_error', + error: "deepnote-toolkit is not installed in the notebook's Python interpreter. Install it from VS Code, then run the app again.", + snapshotBlocks: null + }); + assert.notInclude(steps, 'startServer'); + }); + } + + test('is an internal error with the message when the server fails to start', async () => { + when(serverStarter.startServer(anything(), anything(), anything())).thenReject( + new Error('The server exited') + ); + + const outcome = await run(fileUri, 'nb-1'); + + assert.deepStrictEqual(outcome, { + status: 'internal_error', + error: 'The server exited', + snapshotBlocks: null + }); + assert.deepStrictEqual(__getExecutionEngines(), []); + }); + + const engineFailures: [string, () => void][] = [ + [ + 'the kernel fails to start', + () => + __setExecutionEngineBehavior({ + start: async () => { + throw new Error('The kernel died'); + } + }) + ], + [ + 'running the notebook throws', + () => + __setExecutionEngineBehavior({ + runProject: async () => { + throw new Error('The kernel died'); + } + }) + ] + ]; + for (const [condition, arrange] of engineFailures) { + test(`is an internal error with the message, and stops the kernel, when ${condition}`, async () => { + arrange(); + + const outcome = await run(fileUri, 'nb-1'); + + assert.deepStrictEqual(outcome, { + status: 'internal_error', + error: 'The kernel died', + snapshotBlocks: null + }); + assert.strictEqual(engine().stopCalls, 1); + }); + } + }); + + suite('cancellation', () => { + let fileUri: Uri; + + setup(() => { + fileUri = write('main.deepnote', projectFile([notebookOf('nb-1', [createDeepnoteBlock()])])); + }); + + const stoppedSteps: [string, () => void, string[]][] = [ + [ + 'the notebook opens', + () => + when(mockedVSCodeNamespaces.workspace.openNotebookDocument(anything())).thenCall(async () => { + steps.push('open'); + cancellation.cancel(); + }), + ['open'] + ], + [ + 'the toolkit install is checked', + () => + when(toolkit.ensureToolkitInstalled(anything(), anything(), anything())).thenCall(async () => { + steps.push('ensureToolkit'); + cancellation.cancel(); + + return DeepnoteToolkitDependencyResponse.cancel; + }), + ['open main.deepnote', 'resolve', 'ensureToolkit'] + ], + [ + 'the server starts, which then throws', + () => + when(serverStarter.startServer(anything(), anything(), anything())).thenCall(async () => { + steps.push('startServer'); + cancellation.cancel(); + throw new CancellationError(); + }), + ['open main.deepnote', 'resolve', 'ensureToolkit', 'startServer'] + ] + ]; + for (const [step, arrange, expectedSteps] of stoppedSteps) { + test(`stops before the next step when cancelled while ${step}`, async () => { + arrange(); + + const outcome = await run(fileUri, 'nb-1'); + + assert.deepStrictEqual(outcome, { status: 'stopped', error: null, snapshotBlocks: null }); + assert.deepStrictEqual(steps, expectedSteps); + assert.deepStrictEqual(__getExecutionEngines(), []); + }); + } + + test('stops without running, and stops the kernel, when cancelled while the kernel starts', async () => { + let kernelStarted: () => void = () => undefined; + __setExecutionEngineBehavior({ + start: () => + new Promise((resolve) => { + kernelStarted = resolve; + }), + runProject: runtimeCoreLike() + }); + + const outcome = run(fileUri, 'nb-1'); + await waitFor(() => __getExecutionEngines()[0]?.startCalls === 1); + cancellation.cancel(); + kernelStarted(); + + assert.deepStrictEqual(await outcome, { status: 'stopped', error: null, snapshotBlocks: null }); + assert.deepStrictEqual(engine().runProjectCalls, []); + assert.strictEqual(engine().stopCalls, 1); + assert.notInclude(steps, 'running'); + }); + + test('runs to the end when cancelled once the kernel is running', async () => { + __setExecutionEngineBehavior({ + runProject: async (file, options) => { + cancellation.cancel(); + + return runtimeCoreLike()(file, options); + } + }); + + const outcome: AppPreviewRunOutcome = await run(fileUri, 'nb-1'); + + assert.strictEqual(outcome.status, 'success'); + }); + + test('is an internal error when running throws after a cancel once the kernel is running', async () => { + __setExecutionEngineBehavior({ + runProject: async () => { + cancellation.cancel(); + throw new Error('The kernel died'); + } + }); + + const outcome = await run(fileUri, 'nb-1'); + + assert.deepStrictEqual(outcome, { + status: 'internal_error', + error: 'The kernel died', + snapshotBlocks: null + }); + }); + }); +}); + +async function waitFor(condition: () => boolean): Promise { + const deadline = Date.now() + 2_000; + while (!condition()) { + if (Date.now() > deadline) { + throw new Error('waitFor timed out'); + } + await new Promise((resolve) => setTimeout(resolve, 5)); + } +} diff --git a/src/notebooks/deepnote/appPreview/appPreviewSession.node.ts b/src/notebooks/deepnote/appPreview/appPreviewSession.node.ts index 0670300015..d8a19d8a22 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewSession.node.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewSession.node.ts @@ -7,8 +7,9 @@ import { IDisposable } from '../../../platform/common/types'; import { generateUuid } from '../../../platform/common/uuid'; import { logger } from '../../../platform/logging'; import { extname, extUri } from '../../../platform/vscode-path/resources'; -import { createAppPreviewApi, type AppPreviewOperation } from './appPreviewApi.node'; +import { createAppPreviewApi, type AppPreviewApi, type AppPreviewOperation } from './appPreviewApi.node'; import { findNotebookById } from './appPreviewWorkspace.node'; +import type { IAppPreviewRunner } from './types'; export interface AppPreviewProject { id: string; @@ -18,6 +19,7 @@ export interface AppPreviewProject { export interface AppPreviewSessionOptions { folder: Uri; project: AppPreviewProject | undefined; + runner: IAppPreviewRunner; scripts: { appClient: Uri }; onUnsupported(operation: AppPreviewOperation): void; } @@ -121,6 +123,7 @@ function swapClientScriptTags(page: string): string { * Deepnote's client in place of the hosted one, the token endpoint the client asks first, and a local API at `/v2`. */ export class AppPreviewSession implements IDisposable { + private api: AppPreviewApi | undefined; private project: AppPreviewProject | undefined; private server: http.Server | undefined; private serverUri: Uri | undefined; @@ -155,7 +158,9 @@ export class AppPreviewSession implements IDisposable { return this.serverUri; } + /** Cancels the preview's unfinished runs, then stops its server. */ public dispose(): void { + this.api?.dispose(); const server = this.server; this.server = undefined; if (!server) { @@ -242,14 +247,13 @@ export class AppPreviewSession implements IDisposable { }); app.post('/__deepnote/api-token', (req, res) => this.sendToken(req, res)); app.use('/__deepnote', (_req, res) => notFound(res)); - app.use( - '/v2', - createAppPreviewApi({ - token: this.token, - findNotebook: (notebookId) => findNotebookById(notebookId, this.project?.folder), - onUnsupported: (operation) => this.options.onUnsupported(operation) - }) - ); + this.api = createAppPreviewApi({ + token: this.token, + findNotebook: (notebookId) => findNotebookById(notebookId, this.project?.folder), + onUnsupported: (operation) => this.options.onUnsupported(operation), + runner: this.options.runner + }); + app.use('/v2', this.api.router); app.use((req, res) => this.serveFolder(req, res)); return app; diff --git a/src/notebooks/deepnote/appPreview/appPreviewSession.node.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewSession.node.unit.test.ts index 7c3d449649..9260511293 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewSession.node.unit.test.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewSession.node.unit.test.ts @@ -4,7 +4,7 @@ import * as fs from 'fs'; import * as http from 'http'; import * as os from 'os'; import { anything, when } from 'ts-mockito'; -import { Uri } from 'vscode'; +import { Uri, type CancellationToken } from 'vscode'; import * as path from '../../../platform/vscode-path/path'; import { stubWorkspaceFsOnDisk } from '../../../test/mocks/vscodeFs'; @@ -12,6 +12,7 @@ import { mockedVSCodeNamespaces, resetVSCodeMocks } from '../../../test/vscode-m import { createDeepnoteFile, createDeepnoteNotebook, createDeepnoteProject } from '../deepnoteTestHelpers'; import type { AppPreviewOperation } from './appPreviewApi.node'; import { AppPreviewSession, type AppPreviewProject } from './appPreviewSession.node'; +import type { AppPreviewRunOutcome, IAppPreviewRunner } from './types'; const CSP = "default-src 'self' 'unsafe-inline' 'unsafe-eval' data: blob: https:"; const CLIENT_SCRIPT = '/* the bundled client */\n'; @@ -35,7 +36,7 @@ interface RawResponse { function request( session: AppPreviewSession, urlPath: string, - options: { method?: string; host?: string; headers?: http.OutgoingHttpHeaders } = {} + options: { method?: string; host?: string; headers?: http.OutgoingHttpHeaders; body?: string } = {} ): Promise { const port = Number(new URL(session.serverUrl.toString(true)).port); @@ -60,7 +61,7 @@ function request( } ); req.on('error', reject); - req.end(); + req.end(options.body); }); } @@ -91,6 +92,16 @@ suite('AppPreviewSession', () => { let clientScript: string; let sessions: AppPreviewSession[]; let unsupported: AppPreviewOperation[]; + let runTokens: CancellationToken[]; + + /** Runs that end only once their token is cancelled. */ + const runner: IAppPreviewRunner = { + run: (_request, token) => + new Promise((resolve) => { + runTokens.push(token); + token.onCancellationRequested(() => resolve({ status: 'stopped', error: null, snapshotBlocks: null })); + }) + }; async function startSession( options: { folder?: string; project?: AppPreviewProject | null } = {} @@ -99,6 +110,7 @@ suite('AppPreviewSession', () => { const session = new AppPreviewSession({ folder: Uri.file(options.folder ?? appFolder), project: project ?? undefined, + runner, scripts: { appClient: Uri.file(clientScript) }, onUnsupported: (operation) => unsupported.push(operation) }); @@ -117,6 +129,7 @@ suite('AppPreviewSession', () => { setup(() => { resetVSCodeMocks(); unsupported = []; + runTokens = []; sessions = []; tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'deepnote-app-preview-')); appFolder = path.join(tmp, 'app'); @@ -399,6 +412,36 @@ suite('AppPreviewSession', () => { assert.strictEqual(await notebookName(), 'Copy a'); }); + + test('cancels its unfinished runs when it is disposed', async () => { + const notebook = createDeepnoteNotebook({ id: 'nb-1' }); + const file = createDeepnoteFile({ project: createDeepnoteProject({ notebooks: [notebook] }) }); + write('main.deepnote', serializeDeepnoteFile(file), tmp); + stubWorkspaceFsOnDisk(); + when(mockedVSCodeNamespaces.workspace.findFiles('**/*.deepnote')).thenCall(async () => [ + Uri.file(path.join(tmp, 'main.deepnote')) + ]); + const session = await startSession(); + const { token } = await requestToken(session); + + const created = await request(session, '/v2/runs', { + method: 'POST', + headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' }, + body: JSON.stringify({ notebookId: 'nb-1' }) + }); + assert.strictEqual(created.status, 202); + assert.deepStrictEqual( + runTokens.map((runToken) => runToken.isCancellationRequested === true), + [false] + ); + + session.dispose(); + + assert.deepStrictEqual( + runTokens.map((runToken) => runToken.isCancellationRequested === true), + [true] + ); + }); }); suite('client script swap', () => { diff --git a/src/notebooks/deepnote/appPreview/types.ts b/src/notebooks/deepnote/appPreview/types.ts new file mode 100644 index 0000000000..d9872ab722 --- /dev/null +++ b/src/notebooks/deepnote/appPreview/types.ts @@ -0,0 +1,38 @@ +import type { IOutput } from '@deepnote/runtime-core'; +import type { CancellationToken, Uri } from 'vscode'; + +/** An input value a page can pass to `run()`, as Deepnote's `POST /v2/runs` accepts it. */ +export type AppPreviewInputValue = string | boolean | string[]; + +/** A block of a run's snapshot, as Deepnote's `GET /v2/runs/:runId` lists it in `snapshotBlocks`. */ +export interface SnapshotBlock { + id: string; + metadata: Record; + outputHead: null; + outputs: IOutput[]; + outputTail: null; + type: string; +} + +export interface AppPreviewRunRequest { + fileUri: Uri; + inputs: Record; + notebookId: string; +} + +export interface AppPreviewRunOutcome { + error: string | null; + snapshotBlocks: SnapshotBlock[] | null; + status: 'success' | 'error' | 'internal_error' | 'stopped'; +} + +export const IAppPreviewRunner = Symbol('IAppPreviewRunner'); +export interface IAppPreviewRunner { + /** + * Runs the notebook `notebookId` of the `.deepnote` file at `fileUri` in a new kernel on the toolkit server the + * extension runs for that file: the project's init notebook first, with its saved inputs, then the notebook with + * `inputs` applied. `onRunning` is called once the kernel is up, before the first block. Cancelling `token` + * stops the run only until then. Never rejects: failures are reported in the outcome. + */ + run(request: AppPreviewRunRequest, token: CancellationToken, onRunning: () => void): Promise; +} diff --git a/src/notebooks/serviceRegistry.node.ts b/src/notebooks/serviceRegistry.node.ts index 0b2b8cb6b3..e9a05b21d4 100644 --- a/src/notebooks/serviceRegistry.node.ts +++ b/src/notebooks/serviceRegistry.node.ts @@ -103,6 +103,8 @@ import { DeepnoteApiTokenService } from './deepnote/cloud/deepnoteApiTokenServic import { DeepnoteCloudOperationLock } from './deepnote/cloud/deepnoteCloudOperationLock'; import { PublishAppCommandHandler } from './deepnote/cloud/publishAppCommandHandler.node'; import { AppPreviewCommandHandler } from './deepnote/appPreview/appPreviewCommandHandler.node'; +import { AppPreviewRunner } from './deepnote/appPreview/appPreviewRunner.node'; +import { IAppPreviewRunner } from './deepnote/appPreview/types'; import { SyncWithDeepnoteCloudCommandHandler } from './deepnote/cloud/syncWithDeepnoteCloudCommandHandler.node'; import { IDeepnoteApiTokenService, IDeepnoteCloudOperationLock } from './deepnote/cloud/types'; import { OpenInDeepnoteHandler } from './deepnote/openInDeepnoteHandler.node'; @@ -251,6 +253,7 @@ export function registerTypes(serviceManager: IServiceManager, isDevMode: boolea IExtensionSyncActivationService, AppPreviewCommandHandler ); + serviceManager.addSingleton(IAppPreviewRunner, AppPreviewRunner); serviceManager.addSingleton( IExtensionSyncActivationService, IntegrationEnvRefreshHandler diff --git a/src/platform/common/utils/localize.ts b/src/platform/common/utils/localize.ts index c163f9f8dc..0d4e88719d 100644 --- a/src/platform/common/utils/localize.ts +++ b/src/platform/common/utils/localize.ts @@ -1419,6 +1419,15 @@ export namespace DeepnoteAppPreview { export const startFailed = (message: string) => l10n.t('Could not start the app preview: {0}', message); export const operationNotSupported = (operation: string, folderName: string) => l10n.t('The app in {1} called {0}, which the local preview does not support yet.', operation, folderName); + export const noInterpreter = l10n.t( + 'No Python interpreter is selected for the notebook. Select one in VS Code, then run the app again.' + ); + export const toolkitNotInstalled = l10n.t( + "deepnote-toolkit is not installed in the notebook's Python interpreter. Install it from VS Code, then run the app again." + ); + export const blockFailed = (blockId: string) => l10n.t('Block {0} failed.', blockId); + export const notebookNotInFile = (notebookId: string) => + l10n.t('The notebook {0} is no longer in its .deepnote file.', notebookId); } export namespace SelectInputSettings { diff --git a/src/test/mocks/deepnoteRuntimeCore.ts b/src/test/mocks/deepnoteRuntimeCore.ts index 1ed0920579..15cf6c5e32 100644 --- a/src/test/mocks/deepnoteRuntimeCore.ts +++ b/src/test/mocks/deepnoteRuntimeCore.ts @@ -1,5 +1,15 @@ -import type { AgentBlock } from '@deepnote/blocks'; -import type { AgentBlockContext, ServerExit, ServerInfo, ServerOptions } from '@deepnote/runtime-core'; +import type { AgentBlock, DeepnoteFile } from '@deepnote/blocks'; +import type { + AgentBlockContext, + ExecutionEngine as RealExecutionEngine, + ExecutionEngineOptions, + ExecutionOptions, + ExecutionSummary, + RuntimeConfig, + ServerExit, + ServerInfo, + ServerOptions +} from '@deepnote/runtime-core'; import type { ChildProcess } from 'child_process'; /** @@ -16,6 +26,15 @@ const stopServerCalls: ServerInfo[] = []; let nextServerId = 0; let startServerImpl: RuntimeCore['startServer'] | null = null; let stopServerImpl: RuntimeCore['stopServer'] | null = null; +const executionEngines: ExecutionEngine[] = []; +let executionEngineBehavior: ExecutionEngineBehavior = {}; + +/** What the fake engines do; each defaults to resolving at once, and `runProject` to an empty summary. */ +export interface ExecutionEngineBehavior { + runProject?(file: DeepnoteFile, options: ExecutionOptions): Promise; + start?(): Promise; + stop?(): Promise; +} function makeFakeProcess(id: number): ChildProcess { // Partial stand-in: only the members DeepnoteServerStarter touches. @@ -27,6 +46,39 @@ function makeFakeProcess(id: number): ChildProcess { } as unknown as ChildProcess; } +/** A fake engine that records what it was given and does what `__setExecutionEngineBehavior` says. */ +export class ExecutionEngine implements Pick { + /** Each `runProject` call, with a deep copy of the file as it was when the call started. */ + public readonly runProjectCalls: { file: DeepnoteFile; options: ExecutionOptions }[] = []; + public startCalls = 0; + public stopCalls = 0; + + constructor( + public readonly config: RuntimeConfig, + public readonly options?: ExecutionEngineOptions + ) { + executionEngines.push(this); + } + + public async runProject(file: DeepnoteFile, options: ExecutionOptions = {}): Promise { + this.runProjectCalls.push({ file: structuredClone(file), options }); + + return executionEngineBehavior.runProject + ? executionEngineBehavior.runProject(file, options) + : { totalBlocks: 0, executedBlocks: 0, failedBlocks: 0, totalDurationMs: 0 }; + } + + public async start(): Promise { + this.startCalls++; + await executionEngineBehavior.start?.(); + } + + public async stop(): Promise { + this.stopCalls++; + await executionEngineBehavior.stop?.(); + } +} + export const executeAgentBlock: RuntimeCore['executeAgentBlock'] = async (block, context) => { executeAgentBlockCalls.push({ block, context }); @@ -77,6 +129,14 @@ export const stopServer: RuntimeCore['stopServer'] = async (info) => { }; // Test-only helpers (prefixed with __ to signal they are not part of the real API). +export function __getExecutionEngines(): ExecutionEngine[] { + return executionEngines; +} + +export function __setExecutionEngineBehavior(behavior: ExecutionEngineBehavior): void { + executionEngineBehavior = behavior; +} + export function __getExecuteAgentBlockCalls(): { block: AgentBlock; context: AgentBlockContext }[] { return executeAgentBlockCalls; } @@ -109,4 +169,6 @@ export function __resetRuntimeCoreMock(): void { nextServerId = 0; startServerImpl = null; stopServerImpl = null; + executionEngines.length = 0; + executionEngineBehavior = {}; } From 3bfe60f3181b5da00173519817a301d4ed8c7f3f Mon Sep 17 00:00:00 2001 From: tomas Date: Fri, 13 Nov 2026 02:32:14 +0000 Subject: [PATCH 3/7] test(e2e): run a workspace notebook from a previewed page's run() The page runs a notebook whose init notebook, in a sibling file, has an input of the same name and another type, and shows each run's status and result(): an unknown input rejects, a run succeeds with the notebook's JSON and the init notebook's saved value, and a run whose last block raises ends as error with the exception and the JSON displayed before it. The notebook's sorting keys are Deepnote-written ones that localeCompare orders differently, so the second run's result also checks the block order. No run() warning appears. --- .../fixtures/app-preview-runs-init.deepnote | 28 ++++++++++ test/e2e/fixtures/app-preview-runs.deepnote | 40 +++++++++++++++ test/e2e/fixtures/app-preview-runs/app.js | 28 ++++++++++ test/e2e/fixtures/app-preview-runs/index.html | 19 +++++++ .../suite/appPreview/previewApp.e2e.test.ts | 51 ++++++++++++++++++- 5 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 test/e2e/fixtures/app-preview-runs-init.deepnote create mode 100644 test/e2e/fixtures/app-preview-runs.deepnote create mode 100644 test/e2e/fixtures/app-preview-runs/app.js create mode 100644 test/e2e/fixtures/app-preview-runs/index.html diff --git a/test/e2e/fixtures/app-preview-runs-init.deepnote b/test/e2e/fixtures/app-preview-runs-init.deepnote new file mode 100644 index 0000000000..024fd05fb5 --- /dev/null +++ b/test/e2e/fixtures/app-preview-runs-init.deepnote @@ -0,0 +1,28 @@ +version: '1.0.0' +metadata: + createdAt: '2025-01-01T00:00:00.000Z' +project: + id: e2e-app-preview-runs-project + name: E2E App Preview Runs + initNotebookId: e2e-runs-init + notebooks: + # Its input shares the run notebook's input name, with another type, and keeps its saved value in a run. + - id: e2e-runs-init + name: Init + blocks: + - id: e2e-runs-init-region + blockGroup: e2e-runs-init-region + type: input-checkbox + sortingKey: a0 + metadata: + deepnote_variable_name: region + deepnote_variable_value: false + - id: e2e-runs-init-code + blockGroup: e2e-runs-init-code + type: code + content: init_region = region + sortingKey: a1 + metadata: {} + executionMode: block + isModule: false + settings: {} diff --git a/test/e2e/fixtures/app-preview-runs.deepnote b/test/e2e/fixtures/app-preview-runs.deepnote new file mode 100644 index 0000000000..70708ff14c --- /dev/null +++ b/test/e2e/fixtures/app-preview-runs.deepnote @@ -0,0 +1,40 @@ +version: '1.0.0' +metadata: + createdAt: '2025-01-01T00:00:00.000Z' +project: + id: e2e-app-preview-runs-project + name: E2E App Preview Runs + initNotebookId: e2e-runs-init + notebooks: + # Sorting keys as Deepnote writes them: Deepnote runs V, Z0j, a0 in that order, while localeCompare would put a0 + # first. + - id: e2e-runs-notebook + name: Sales run + blocks: + - id: e2e-runs-check + blockGroup: e2e-runs-check + type: code + content: |- + if not region: + raise ValueError('Choose a region first') + sortingKey: a0 + metadata: {} + - id: e2e-runs-region + blockGroup: e2e-runs-region + type: input-text + sortingKey: V + metadata: + deepnote_variable_name: region + deepnote_variable_value: Europe + deepnote_input_label: Region + - id: e2e-runs-summary + blockGroup: e2e-runs-summary + type: code + content: |- + from IPython.display import display + display({'application/json': {'region': region, 'init_region': init_region}}, raw=True) + sortingKey: Z0j + metadata: {} + executionMode: block + isModule: false + settings: {} diff --git a/test/e2e/fixtures/app-preview-runs/app.js b/test/e2e/fixtures/app-preview-runs/app.js new file mode 100644 index 0000000000..d1a0c8746e --- /dev/null +++ b/test/e2e/fixtures/app-preview-runs/app.js @@ -0,0 +1,28 @@ +// Shows on the page what run() returned, so the E2E test and its screenshots can read it. +function show(id, text) { + document.getElementById(id).textContent = text; +} + +const notebook = window.Deepnote.connect().notebook('e2e-runs-notebook'); + +async function showRun(id, label, inputs) { + try { + const { run, result } = await notebook.run(inputs); + show(`${id}-status`, `${label}: ${run.status}${run.error ? `, ${run.error}` : ''}`); + show(`${id}-result`, `${label} result(): ${JSON.stringify(result())}`); + } catch (error) { + show(`${id}-status`, `${label} rejected: ${error.message}`); + show(`${id}-result`, `${label} result(): none`); + } +} + +(async () => { + try { + await notebook.run({ unknown: 'x' }); + show('unknown-input', "run({ unknown: 'x' }) resolved"); + } catch (error) { + show('unknown-input', `run({ unknown: 'x' }) rejected: ${error.message}`); + } + await showRun('asia', "run({ region: 'Asia' })", { region: 'Asia' }); + await showRun('empty', "run({ region: '' })", { region: '' }); +})(); diff --git a/test/e2e/fixtures/app-preview-runs/index.html b/test/e2e/fixtures/app-preview-runs/index.html new file mode 100644 index 0000000000..16c26679ca --- /dev/null +++ b/test/e2e/fixtures/app-preview-runs/index.html @@ -0,0 +1,19 @@ + + + + + Runs fixture + + + +

Sales runs

+
    +
  • run({ unknown: 'x' }): pending
  • +
  • run({ region: 'Asia' }): pending
  • +
  • run({ region: 'Asia' }) result(): pending
  • +
  • run({ region: '' }): pending
  • +
  • run({ region: '' }) result(): pending
  • +
+ + + diff --git a/test/e2e/suite/appPreview/previewApp.e2e.test.ts b/test/e2e/suite/appPreview/previewApp.e2e.test.ts index 23f6a88ea3..1f5e35f16f 100644 --- a/test/e2e/suite/appPreview/previewApp.e2e.test.ts +++ b/test/e2e/suite/appPreview/previewApp.e2e.test.ts @@ -20,6 +20,7 @@ import { } from 'vscode-extension-tester'; import { + KERNEL_CONNECT_TIMEOUT, QUICK_PICK_TIMEOUT, SUITE_TIMEOUT, WORKBENCH_TIMEOUT, @@ -32,15 +33,19 @@ import { } from '../../helpers'; const FIXTURE_APP = path.resolve(process.cwd(), 'test', 'e2e', 'fixtures', 'app-preview'); +const RUNS_FIXTURE_APP = path.resolve(process.cwd(), 'test', 'e2e', 'fixtures', 'app-preview-runs'); const INPUTS_NOTEBOOK_FIXTURE = 'app-preview-inputs.deepnote'; +const RUNS_NOTEBOOK_FIXTURES = ['app-preview-runs.deepnote', 'app-preview-runs-init.deepnote']; const SYNCED_APP = path.join('Sales', '.files', '_deepnote_static'); const SYNCED_TITLE = 'Synced fixture'; const RELOAD_TITLE = 'Reload fixture'; const REUSE_TITLE = 'Reuse fixture'; const INPUTS_TITLE = 'Inputs fixture'; +const RUNS_TITLE = 'Runs fixture'; const PREVIEW_URL = /^http:\/\/127\.0\.0\.1:\d+\//; const NOT_SUPPORTED = 'which the local preview does not support yet.'; const INPUTS_RESOLVED = 'inputs() resolved: '; +const RUN_RESULT = ' result(): '; const NOTEBOOK_INPUTS = [ { blockId: 'e2e-region-input', name: 'region', type: 'input-text', value: 'Europe', label: 'Region' }, @@ -89,7 +94,7 @@ function appDataWarning(folderName: string): string { return `The app in ${folderName} called appData(), which the local preview does not support yet.`; } -async function readReport(): Promise { +async function readReport(timeout = WORKBENCH_TIMEOUT): Promise { const driver = VSBrowser.instance.driver; let report: string[] = []; try { @@ -103,7 +108,7 @@ async function readReport(): Promise { return report.length > 0 && report.every((line) => !line.endsWith('pending')); }, - WORKBENCH_TIMEOUT, + timeout, 'the preview page did not finish its client calls' ); } catch (error) { @@ -282,6 +287,7 @@ describe('Deepnote: Preview App — serve a data app folder in the built-in brow // Kept out of every app folder and the folders above them, so no app resolves to this project. fs.mkdirSync(path.join(workspaceDir, 'notebooks')); inputsNotebookFile = copyFixtureIntoDir(path.join(workspaceDir, 'notebooks'), INPUTS_NOTEBOOK_FIXTURE); + RUNS_NOTEBOOK_FIXTURES.forEach((fixture) => copyFixtureIntoDir(path.join(workspaceDir, 'notebooks'), fixture)); await VSBrowser.instance.waitForWorkbench(WORKBENCH_TIMEOUT); await openFolderViaDialog(workspaceDir); @@ -384,6 +390,47 @@ describe('Deepnote: Preview App — serve a data app folder in the built-in brow } }); + it('runs the workspace notebook for run(), after its init notebook, and reports its result or its error', async function () { + const screenshot = createScreenshotter(this); + await clearNotifications(); + const appDir = fs.mkdtempSync(path.join(workspaceDir, 'runs-app-')); + fs.cpSync(RUNS_FIXTURE_APP, appDir, { recursive: true }); + + await selectDeepnoteContextMenu(await findRootExplorerItem(path.basename(appDir)), 'Preview App'); + + // The first run starts the notebook file's toolkit server and a kernel. + const report = await onIntegratedBrowserPage(RUNS_TITLE, async () => { + const read = await readReport(KERNEL_CONNECT_TIMEOUT); + await screenshot('run-results'); + + return read; + }); + expect( + report.map((line) => { + const text = String(line); + const at = text.indexOf(RUN_RESULT); + + return at === -1 ? text : [text.slice(0, at), JSON.parse(text.slice(at + RUN_RESULT.length))]; + }) + ).to.deep.equal([ + 'Sales runs', + 'run({ unknown: \'x\' }) rejected: Input "unknown" is not defined for this notebook', + "run({ region: 'Asia' }): success", + ["run({ region: 'Asia' })", { region: 'Asia', init_region: false }], + "run({ region: '' }): error, ValueError: Choose a region first", + // The notebook displays its result before the block that raises, in Deepnote's block order. + ["run({ region: '' })", { region: '', init_region: false }] + ]); + + const center = await new Workbench().openNotificationsCenter(); + const messages = await Promise.all( + (await center.getNotifications(NotificationType.Warning)).map((notification) => notification.getMessage()) + ); + expect(messages.filter((message) => message.endsWith(NOT_SUPPORTED))).to.deep.equal([]); + await screenshot('no-run-warning'); + await center.close(); + }); + it('switches to the open tab and reloads it when the same folder is previewed again', async function () { const screenshot = createScreenshotter(this); const driver = VSBrowser.instance.driver; From 969411723e326111d7fb17136b892f5ab3fd3967 Mon Sep 17 00:00:00 2001 From: tomas Date: Fri, 13 Nov 2026 02:38:05 +0000 Subject: [PATCH 4/7] refactor(app-preview): trim comments to the why --- .../deepnote/appPreview/appPreviewApi.node.ts | 6 +++--- .../appPreview/appPreviewApi.node.unit.test.ts | 2 -- .../deepnote/appPreview/appPreviewRunner.node.ts | 11 +++-------- .../appPreview/appPreviewRunner.node.unit.test.ts | 7 +++---- .../deepnote/appPreview/appPreviewSession.node.ts | 1 - .../appPreview/appPreviewSession.node.unit.test.ts | 1 - src/test/mocks/deepnoteRuntimeCore.ts | 1 - 7 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/notebooks/deepnote/appPreview/appPreviewApi.node.ts b/src/notebooks/deepnote/appPreview/appPreviewApi.node.ts index a8dc0b1f69..99cfb3eb0f 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewApi.node.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewApi.node.ts @@ -38,7 +38,7 @@ export interface AppPreviewApi { router: Router; /** The preview's runs by ID, kept for the preview's lifetime. */ runs: Map; - /** Cancels every run that has not finished. */ + /** Cancels the token of every unfinished run, which stops only runs whose blocks have not started. */ dispose(): void; } @@ -65,7 +65,7 @@ function errorMessage(error: unknown): string { return error instanceof Error ? error.message : String(error); } -/** Deepnote's input check: every input names an input block of the notebook, and suits each block of that name. */ +/** The input check Deepnote's `POST /v2/runs` makes before it accepts a run. */ function inputsError(notebook: DeepnoteNotebook, inputs: Record): string | undefined { const inputBlocks = notebook.blocks.filter(isInputBlock); for (const [name, value] of Object.entries(inputs)) { @@ -95,7 +95,7 @@ function isAuthorized(authorizationHeader: string | undefined, token: string): b return actual.length === expected.length && timingSafeEqual(actual, expected); } -/** Deepnote's 400 message for a body that fails its schema: each issue's path and message, a root-level one as is. */ +/** Deepnote's 400 message for a body that fails its schema. */ function readableErrors(error: z.ZodError): string { return error.issues .map((issue) => diff --git a/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts index a8afdf38c0..64f93b6e11 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts @@ -75,7 +75,6 @@ interface RunnerCall { fail(error: Error): void; } -/** A runner whose runs stay unfinished until the test finishes or fails them. */ class FakeRunner implements IAppPreviewRunner { public readonly calls: RunnerCall[] = []; @@ -151,7 +150,6 @@ suite('createAppPreviewApi', () => { return (await response.json()).run; } - /** Polls the run until its status is `status`, as the client does. */ async function waitForStatus(runId: string, status: string): Promise> { const deadline = Date.now() + 2_000; for (;;) { diff --git a/src/notebooks/deepnote/appPreview/appPreviewRunner.node.ts b/src/notebooks/deepnote/appPreview/appPreviewRunner.node.ts index 20548fee2e..68cb20d616 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewRunner.node.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewRunner.node.ts @@ -77,8 +77,8 @@ function internalError(message: string): AppPreviewRunOutcome { } /** - * A copy of `file` that holds only a copy of `notebook`, with its blocks re-keyed `000000`, `000001`, … in Deepnote's - * order. runtime-core sorts blocks with `localeCompare`, which would run Deepnote's key `a0` before `Z0j`. + * A copy of `file` holding only `notebook`, its blocks re-keyed in Deepnote's order: runtime-core sorts blocks with + * `localeCompare`, which would run Deepnote's key `a0` before `Z0j`. */ function runCopy(file: DeepnoteFile, notebook: DeepnoteNotebook): DeepnoteFile { const copy = structuredClone({ ...file, project: { ...file.project, notebooks: [notebook] } }); @@ -112,7 +112,7 @@ async function runNotebook( return { failure: summary.failedBlocks > 0 ? failure : undefined, outputs }; } -/** The run's copy of the notebook, input overrides included, with the outputs of this run. */ +/** After a run, `file`'s input blocks hold the run's input values: runtime-core writes them in place. */ function snapshotBlocksOf(file: DeepnoteFile, outputs: Map): SnapshotBlock[] { return file.project.notebooks[0].blocks.map((block) => ({ id: block.id, @@ -124,11 +124,6 @@ function snapshotBlocksOf(file: DeepnoteFile, outputs: Map): })); } -/** - * Runs a workspace notebook for the app preview's `run()` the way Deepnote runs one for an app: in a fresh kernel, the - * init notebook first with its saved inputs, then the notebook with the page's inputs. The kernel is a session of its - * own on the toolkit server the extension runs for the notebook's `.deepnote` file. - */ @injectable() export class AppPreviewRunner implements IAppPreviewRunner { constructor( diff --git a/src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts index 80ac99fc2a..4fc9cea51c 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts @@ -60,7 +60,7 @@ function projectFile(notebooks: DeepnoteNotebook[], initNotebookId?: string): De }); } -/** runtime-core 0.8.0's order: each notebook's executable blocks, sorted with `localeCompare`. */ +/** The order runtime-core 0.8.0 runs a file's blocks in. */ function executionOrder(file: DeepnoteFile): DeepnoteBlock[] { return file.project.notebooks.flatMap((notebook) => [...notebook.blocks] @@ -70,9 +70,8 @@ function executionOrder(file: DeepnoteFile): DeepnoteBlock[] { } /** - * Runs a file as runtime-core 0.8.0 does: it sets the inputs on the file's input blocks in place, runs the executable - * blocks in its order, reports each through `onBlockDone`, and stops at the first failure. Each block's result comes - * from `results`, and is a success without outputs by default. + * A `runProject` that behaves like runtime-core 0.8.0's: it writes the inputs into the file's input blocks in place + * and stops at the first failure. */ function runtimeCoreLike(results: Record = {}) { return async (file: DeepnoteFile, options: ExecutionOptions): Promise => { diff --git a/src/notebooks/deepnote/appPreview/appPreviewSession.node.ts b/src/notebooks/deepnote/appPreview/appPreviewSession.node.ts index d8a19d8a22..4f6017c9b7 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewSession.node.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewSession.node.ts @@ -158,7 +158,6 @@ export class AppPreviewSession implements IDisposable { return this.serverUri; } - /** Cancels the preview's unfinished runs, then stops its server. */ public dispose(): void { this.api?.dispose(); const server = this.server; diff --git a/src/notebooks/deepnote/appPreview/appPreviewSession.node.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewSession.node.unit.test.ts index 9260511293..38fa447d17 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewSession.node.unit.test.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewSession.node.unit.test.ts @@ -94,7 +94,6 @@ suite('AppPreviewSession', () => { let unsupported: AppPreviewOperation[]; let runTokens: CancellationToken[]; - /** Runs that end only once their token is cancelled. */ const runner: IAppPreviewRunner = { run: (_request, token) => new Promise((resolve) => { diff --git a/src/test/mocks/deepnoteRuntimeCore.ts b/src/test/mocks/deepnoteRuntimeCore.ts index 15cf6c5e32..07d76971f4 100644 --- a/src/test/mocks/deepnoteRuntimeCore.ts +++ b/src/test/mocks/deepnoteRuntimeCore.ts @@ -46,7 +46,6 @@ function makeFakeProcess(id: number): ChildProcess { } as unknown as ChildProcess; } -/** A fake engine that records what it was given and does what `__setExecutionEngineBehavior` says. */ export class ExecutionEngine implements Pick { /** Each `runProject` call, with a deep copy of the file as it was when the call started. */ public readonly runProjectCalls: { file: DeepnoteFile; options: ExecutionOptions }[] = []; From d65ab8e4f0dc173d984b816ebebe8b599cff7f63 Mon Sep 17 00:00:00 2001 From: tomas Date: Fri, 13 Nov 2026 03:11:29 +0000 Subject: [PATCH 5/7] test(app-preview): tighten the run tests --- .../appPreviewApi.node.unit.test.ts | 5 -- .../appPreviewRunner.node.unit.test.ts | 47 ++++++++++--------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts index 64f93b6e11..d46bae2d52 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts @@ -358,11 +358,6 @@ suite('createAppPreviewApi', () => { ['with a number as an input value', { notebookId: 'nb-1', inputs: { a: 1 } }, 'inputs.a invalid input'], ['with an unknown key', { notebookId: 'nb-1', x: 1 }, 'Unrecognized key: "x"'], ['for an unknown notebook', { notebookId: 'nb-missing' }, 'Notebook not found'], - [ - 'with an input the notebook does not define', - { notebookId: 'nb-1', inputs: { unknown: 'x' } }, - 'Input "unknown" is not defined for this notebook' - ], [ 'with an input only another notebook of the file defines', { notebookId: 'nb-1', inputs: { limit: '5' } }, diff --git a/src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts index 4fc9cea51c..c150a1b1bc 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts @@ -237,8 +237,7 @@ suite('AppPreviewRunner', () => { }); }); - const withoutInit: [string, string | undefined][] = [ - ['the project has no init notebook', undefined], + const withoutInit: [string, string][] = [ ['the notebook is the init notebook', 'nb-1'], ['no file has the init notebook', 'nb-init'] ]; @@ -275,7 +274,7 @@ suite('AppPreviewRunner', () => { const fileUri = write('main.deepnote', file); const onDisk = fs.readFileSync(fileUri.fsPath, 'utf8'); - const outcome = await run(fileUri, 'nb-1', { x: 'hello' }); + await run(fileUri, 'nb-1', { x: 'hello' }); const [initCall, notebookCall] = engine().runProjectCalls; assert.isUndefined(initCall.options.inputs); @@ -283,7 +282,6 @@ suite('AppPreviewRunner', () => { deepnote_variable_value: false }); assert.deepStrictEqual(notebookCall.options.inputs, { x: 'hello' }); - assert.deepInclude(outcome.snapshotBlocks?.[0].metadata, { deepnote_variable_value: 'hello' }); assert.strictEqual(fs.readFileSync(fileUri.fsPath, 'utf8'), onDisk); }); @@ -335,25 +333,6 @@ suite('AppPreviewRunner', () => { assert.deepStrictEqual(contents, [[WINDOWS_INIT_BLOCK], [DEEPNOTE_CLOUD_INIT_BLOCK]]); assert.strictEqual(fs.readFileSync(fileUri.fsPath, 'utf8'), onDisk); }); - - test("runs Deepnote Cloud's init block unchanged on Linux", async () => { - sinon.stub(process, 'platform').value('linux'); - const fileUri = write( - 'main.deepnote', - projectFile( - [ - notebookOf('nb-init', [createDeepnoteBlock({ content: DEEPNOTE_CLOUD_INIT_BLOCK })]), - notebookOf('nb-1', []) - ], - 'nb-init' - ) - ); - - await run(fileUri, 'nb-1'); - - const [initBlock] = engine().runProjectCalls[0].file.project.notebooks[0].blocks; - assert.deepInclude(initBlock, { content: DEEPNOTE_CLOUD_INIT_BLOCK }); - }); }); suite('outcomes', () => { @@ -591,6 +570,17 @@ suite('AppPreviewRunner', () => { }), ['open'] ], + [ + 'the interpreter resolves', + () => + when(interpreters.resolve(anything())).thenCall(async () => { + steps.push('resolve'); + cancellation.cancel(); + + return INTERPRETER; + }), + ['open main.deepnote', 'resolve'] + ], [ 'the toolkit install is checked', () => @@ -602,6 +592,17 @@ suite('AppPreviewRunner', () => { }), ['open main.deepnote', 'resolve', 'ensureToolkit'] ], + [ + 'the server starts', + () => + when(serverStarter.startServer(anything(), anything(), anything())).thenCall(async () => { + steps.push('startServer'); + cancellation.cancel(); + + return SERVER; + }), + ['open main.deepnote', 'resolve', 'ensureToolkit', 'startServer'] + ], [ 'the server starts, which then throws', () => From 9b51a2c30977d96fe834515e71d246dc7eb9ea55 Mon Sep 17 00:00:00 2001 From: tomas Date: Fri, 13 Nov 2026 03:38:21 +0000 Subject: [PATCH 6/7] fix(app-preview): address review findings - Cancelling a run once its kernel is up now stops the kernel: runtime-core's stop() fails the executing block and shuts the session down, so the run ends as stopped instead of keeping the kernel until the window closes. Overlapping stop() calls throw, so every caller shares one. - Runs pass the project's integrations to runtime-core, as the editor's agent cells do. - A body express.json() rejects, such as malformed JSON or one over the size limit, gets its 4xx and message instead of a logged 500. - The run store drops the oldest finished runs beyond 20, keeping unfinished runs and each notebook's newest successful run. - The README says that agent blocks need the provider's key in VS Code's environment, that a run outlives the closed preview until the window reloads or closes, and that a legacy multi-notebook file shows its split prompt on the first run. --- README.md | 7 +- .../deepnote/appPreview/appPreviewApi.node.ts | 46 +++++++++- .../appPreviewApi.node.unit.test.ts | 46 ++++++++++ .../appPreview/appPreviewRunner.node.ts | 28 ++++-- .../appPreviewRunner.node.unit.test.ts | 89 +++++++++++++++---- src/notebooks/deepnote/appPreview/types.ts | 5 +- src/test/mocks/deepnoteRuntimeCore.ts | 15 +++- 7 files changed, 204 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 050083b60d..6e8ba859f4 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,12 @@ Run `Deepnote: Preview App` from the Command Palette, or right-click a folder in - The folder is served the way Deepnote serves a published app, from a local address. Edit a file and reload the page to see the change. - The page's script tag for Deepnote's client (`https://deepnote.com/static/app-client/v1.js`) loads a copy of that client bundled with the extension instead, so the preview works without network access. - `inputs()` returns the input blocks of the notebook with that ID in the workspace's `.deepnote` files, as saved on disk, so save the notebook and reload the page to see a change. An ID that no workspace notebook has rejects with `Notebook not found`. -- `run()` runs that notebook, as saved on disk, in a new kernel on the Deepnote server the extension runs for its `.deepnote` file: the project's init notebook first, then the notebook with the page's input values. It uses the notebook's Python interpreter, and its SQL blocks use the integrations configured in VS Code, except BigQuery integrations that use Google OAuth. Like a run in Deepnote, it is not read-only: what its code writes, such as files or stored dataframes, is written. Closing the preview cancels its runs whose blocks have not started. +- `run()` runs that notebook, as saved on disk, in a new kernel on the Deepnote server the extension runs for its `.deepnote` file: the project's init notebook first, then the notebook with the page's input values. + - It uses the notebook's Python interpreter. Its SQL blocks use the integrations configured in VS Code, except BigQuery integrations that use Google OAuth. + - Agent blocks run only when the model provider's API key, such as `OPENAI_API_KEY`, is set in VS Code's own environment. The key saved for agent cells is not used. + - Like a run in Deepnote, it is not read-only: what its code writes, such as files or stored dataframes, is written. + - A run keeps going until its notebook finishes, even after you close the preview. Reloading or closing the VS Code window stops it. + - For a legacy `.deepnote` file that holds several notebooks, the first run shows the prompt to split the file, as opening the file does. - The page's other calls to Deepnote reach a local stand-in that does not support them yet: `appData()` rejects with a message saying so, and VS Code shows a warning the first time the app calls it. - Previewing needs a trusted workspace and the desktop app. diff --git a/src/notebooks/deepnote/appPreview/appPreviewApi.node.ts b/src/notebooks/deepnote/appPreview/appPreviewApi.node.ts index 99cfb3eb0f..ac0234456f 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewApi.node.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewApi.node.ts @@ -36,9 +36,12 @@ export interface PreviewRun { export interface AppPreviewApi { router: Router; - /** The preview's runs by ID, kept for the preview's lifetime. */ + /** + * The preview's runs by ID, in the order they started. Besides unfinished runs and each notebook's newest + * successful run, it keeps only the newest {@link MAX_FINISHED_RUNS} finished runs. + */ runs: Map; - /** Cancels the token of every unfinished run, which stops only runs whose blocks have not started. */ + /** Cancels every unfinished run, which stops its kernel once it has one. */ dispose(): void; } @@ -51,6 +54,9 @@ const INTERNAL_ERROR_MESSAGE = 'Internal server error'; type AppViewerNotebook = operations['getNotebook']['responses'][200]['content']['application/json']['notebook']; +// Each finished run holds all its outputs, and a page may start one on every reload. +const MAX_FINISHED_RUNS = 20; + const createRunBodySchema = z.strictObject({ notebookId: z.string().min(1), inputs: z.record(z.string().min(1), z.union([z.string(), z.boolean(), z.array(z.string())])).optional(), @@ -61,10 +67,36 @@ function notSupportedMessage(operation: AppPreviewOperation): string { return `${operation} is not supported in the local preview yet.`; } +/** The 4xx status of an error body-parser raises for the request itself, such as malformed JSON. */ +function clientErrorStatus(error: unknown): number | undefined { + if (typeof error !== 'object' || error === null || !('status' in error) || !('expose' in error)) { + return undefined; + } + + return typeof error.status === 'number' && error.status < 500 && error.expose === true ? error.status : undefined; +} + function errorMessage(error: unknown): string { return error instanceof Error ? error.message : String(error); } +/** + * Drops the oldest finished runs beyond {@link MAX_FINISHED_RUNS}. Unfinished runs stay, and so does each notebook's + * newest successful run, which `appData()` answers from. + */ +function evictFinishedRuns(runs: Map): void { + const newestSuccess = new Map(); + for (const run of runs.values()) { + if (run.status === 'success') { + newestSuccess.set(run.notebookId, run.runId); + } + } + const removable = [...runs.values()].filter( + (run) => run.completedAt !== null && newestSuccess.get(run.notebookId) !== run.runId + ); + removable.slice(0, Math.max(0, removable.length - MAX_FINISHED_RUNS)).forEach((run) => runs.delete(run.runId)); +} + /** The input check Deepnote's `POST /v2/runs` makes before it accepts a run. */ function inputsError(notebook: DeepnoteNotebook, inputs: Record): string | undefined { const inputBlocks = notebook.blocks.filter(isInputBlock); @@ -130,7 +162,8 @@ function toAppViewerNotebook({ file, notebook }: WorkspaceNotebook): AppViewerNo * Requests without the preview's bearer token get 401. `GET /notebooks/:notebookId` answers from the workspace's * notebooks, and `POST /runs` runs one with `runner`, which `GET /runs/:runId` then reports on. The other routes * Deepnote's client calls or is about to call get 501, and every other route gets 403, as Deepnote answers app viewer - * tokens. A handler that fails is logged and gets a JSON 500. + * tokens. A body that cannot be read, such as malformed JSON, gets its 4xx; any other failure is logged and gets a JSON + * 500. */ export function createAppPreviewApi(options: AppPreviewApiOptions): AppPreviewApi { const router = express.Router(); @@ -161,6 +194,7 @@ export function createAppPreviewApi(options: AppPreviewApiOptions): AppPreviewAp run.snapshotStatus = outcome.status === 'success' || outcome.status === 'error' ? 'available' : 'unavailable'; run.completedAt = new Date().toISOString(); + evictFinishedRuns(runs); }) .finally(() => { unfinished.delete(cancellation); @@ -243,6 +277,12 @@ export function createAppPreviewApi(options: AppPreviewApiOptions): AppPreviewAp }); // Express's own handler would answer an HTML page that holds the stack, and log it outside the extension's logger. router.use((error: unknown, req: Request, res: Response, _next: NextFunction) => { + const status = clientErrorStatus(error); + if (status !== undefined) { + res.status(status).json({ message: errorMessage(error) }); + + return; + } logger.error(`AppPreviewApi: ${req.method} ${req.path} failed`, error); res.status(500).json({ message: INTERNAL_ERROR_MESSAGE }); }); diff --git a/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts index d46bae2d52..369dca4fe1 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts @@ -376,6 +376,52 @@ suite('createAppPreviewApi', () => { }); } + const unreadableBodies: [string, string, number, string][] = [ + ['that is not JSON', '{"notebookId":', 400, 'Unexpected end of JSON input'], + [ + 'over the JSON size limit', + JSON.stringify({ notebookId: 'nb-1', inputs: { region: 'x'.repeat(200_000) } }), + 413, + 'request entity too large' + ] + ]; + for (const [condition, body, status, message] of unreadableBodies) { + test(`POST /runs answers ${status} to a body ${condition}, without starting a run or logging an error`, async () => { + const logError = sinon.stub(logger, 'error'); + + const response = await fetch(`${baseUrl}/runs`, { + method: 'POST', + headers: { authorization: `Bearer ${TOKEN}`, 'content-type': 'application/json' }, + body + }); + + await assertJson(response, status, { message }); + assert.deepStrictEqual(runner.calls, []); + sinon.assert.notCalled(logError); + }); + } + + test("forgets the oldest finished runs beyond 20, but never an unfinished run or a notebook's newest success", async () => { + const unfinished = await startRun(); + const finishRun = async (status: 'success' | 'error') => { + const runId = await startRun(); + runner.calls[runner.calls.length - 1].finish({ status, error: null, snapshotBlocks: [] }); + await waitForStatus(runId, status); + + return runId; + }; + const olderSuccess = await finishRun('success'); + const newestSuccess = await finishRun('success'); + const failed: string[] = []; + for (let index = 0; index < 21; index++) { + failed.push(await finishRun('error')); + } + + assert.deepStrictEqual([...api.runs.keys()], [unfinished, newestSuccess, ...failed.slice(1)]); + await assertJson(await call('GET', `/runs/${olderSuccess}`), 404, { message: 'Run not found' }); + assert.strictEqual((await getRun(newestSuccess)).status, 'success'); + }); + const stubs: [string, string, AppPreviewOperation][] = [ ['GET', '/notebooks/nb-1/app-data', 'appData()'], ['GET', '/projects/p-1/dataframes/sales%20by%20month', 'dataframe()'] diff --git a/src/notebooks/deepnote/appPreview/appPreviewRunner.node.ts b/src/notebooks/deepnote/appPreview/appPreviewRunner.node.ts index 68cb20d616..e8913a40e1 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewRunner.node.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewRunner.node.ts @@ -1,7 +1,7 @@ import { generateSortingKey, type DeepnoteFile } from '@deepnote/blocks'; import { ExecutionEngine, type BlockExecutionResult, type IError, type IOutput } from '@deepnote/runtime-core'; import { inject, injectable } from 'inversify'; -import { workspace, type CancellationToken, type Uri } from 'vscode'; +import { workspace, type CancellationToken, type Disposable, type Uri } from 'vscode'; import { DeepnoteToolkitDependencyResponse, @@ -100,6 +100,7 @@ async function runNotebook( let failure: BlockExecutionResult | undefined; const summary = await engine.runProject(file, { ...(inputs ? { inputs } : {}), + integrations: file.project.integrations ?? [], onBlockDone: (result) => { outputs.set(result.blockId, result.outputs); if (!result.success) { @@ -137,14 +138,10 @@ export class AppPreviewRunner implements IAppPreviewRunner { token: CancellationToken, onRunning: () => void ): Promise { - let running = false; try { - return await this.execute(request, token, () => { - running = true; - onRunning(); - }); + return await this.execute(request, token, onRunning); } catch (error) { - if (!running && token.isCancellationRequested) { + if (token.isCancellationRequested) { return STOPPED; } logger.error( @@ -201,13 +198,24 @@ export class AppPreviewRunner implements IAppPreviewRunner { { pythonEnv: getFilePath(interpreter.uri), workingDirectory: path.dirname(fileUri.fsPath) }, { server } ); + let stopping: Promise | undefined; + // Overlapping stop() calls make runtime-core throw. + const stop = () => (stopping ??= engine.stop()); + let onCancel: Disposable | undefined; try { await engine.start(); Cancellation.throwIfCanceled(token); + // stop() also fails the block that is executing and shuts the kernel down, so runProject then resolves. + onCancel = token.onCancellationRequested(() => { + stop().catch((error) => logger.warn('AppPreviewRunner: Could not stop a cancelled run', error)); + }); onRunning(); if (initCopy) { const { failure } = await runNotebook(engine, initCopy); + if (token.isCancellationRequested) { + return STOPPED; + } if (failure) { return { status: 'error', @@ -218,6 +226,9 @@ export class AppPreviewRunner implements IAppPreviewRunner { } const { failure, outputs } = await runNotebook(engine, targetCopy, inputs); + if (token.isCancellationRequested) { + return STOPPED; + } return { status: failure ? 'error' : 'success', @@ -225,7 +236,8 @@ export class AppPreviewRunner implements IAppPreviewRunner { snapshotBlocks: snapshotBlocksOf(targetCopy, outputs) }; } finally { - await engine.stop(); + onCancel?.dispose(); + await stop(); } } } diff --git a/src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts index c150a1b1bc..0069187d3b 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewRunner.node.unit.test.ts @@ -33,7 +33,7 @@ import { WINDOWS_INIT_BLOCK } from '../deepnoteTestHelpers'; import { AppPreviewRunner } from './appPreviewRunner.node'; -import type { AppPreviewRunOutcome, AppPreviewRunRequest } from './types'; +import type { AppPreviewRunRequest } from './types'; const INTERPRETER: PythonEnvironment = { id: 'venv', uri: Uri.file('/venv/bin/python') }; const SERVER = { url: 'http://127.0.0.1:8888', jupyterPort: 8888, lspPort: 8889 } as DeepnoteServerInfo; @@ -105,6 +105,28 @@ function runtimeCoreLike(results: Record = {}) { }; } +/** A run whose block executes until the engine is stopped, then fails as runtime-core reports it. */ +function failWhenStopped(blockId: string) { + return async ( + _file: DeepnoteFile, + options: ExecutionOptions, + stopped: Promise + ): Promise => { + await stopped; + await options.onBlockDone?.({ + blockId, + blockType: 'code', + success: false, + outputs: [], + executionCount: null, + durationMs: 1, + error: new Error('Kernel client disconnected while an execution was in flight.') + }); + + return { totalBlocks: 1, executedBlocks: 1, failedBlocks: 1, totalDurationMs: 1 }; + }; +} + function notebookIdsPerCall(): string[][] { return engine().runProjectCalls.map((runCall) => runCall.file.project.notebooks.map((notebook) => notebook.id)); } @@ -218,6 +240,22 @@ suite('AppPreviewRunner', () => { assert.deepStrictEqual(notebookIdsPerCall(), [['nb-init'], ['nb-1']]); }); + test("gives the project's integrations to the init notebook's run and the notebook's run", async () => { + const integrations = [{ id: 'pg', name: 'Postgres', type: 'pgsql' }]; + const file = projectFile( + [notebookOf('nb-1', [createDeepnoteBlock()]), notebookOf('nb-init', [createDeepnoteBlock()])], + 'nb-init' + ); + const fileUri = write('main.deepnote', { ...file, project: { ...file.project, integrations } }); + + await run(fileUri, 'nb-1'); + + assert.deepStrictEqual( + engine().runProjectCalls.map((runCall) => runCall.options.integrations), + [integrations, integrations] + ); + }); + test("runs the init notebook from a sibling file of the same project when the file doesn't have it", async () => { const fileUri = write( 'main.deepnote', @@ -647,35 +685,56 @@ suite('AppPreviewRunner', () => { assert.notInclude(steps, 'running'); }); - test('runs to the end when cancelled once the kernel is running', async () => { - __setExecutionEngineBehavior({ - runProject: async (file, options) => { - cancellation.cancel(); + const cancelCounts: [string, number][] = [ + ['once', 1], + ['twice', 2] + ]; + for (const [times, count] of cancelCounts) { + test(`stops the kernel once, and ends stopped, when cancelled ${times} while a block runs`, async () => { + __setExecutionEngineBehavior({ runProject: failWhenStopped('block-1') }); - return runtimeCoreLike()(file, options); + const outcome = run(fileUri, 'nb-1'); + await waitFor(() => __getExecutionEngines()[0]?.runProjectCalls.length === 1); + for (let cancelled = 0; cancelled < count; cancelled++) { + cancellation.cancel(); } + await waitFor(() => engine().stopCalls > 0); + + assert.deepStrictEqual(await outcome, { status: 'stopped', error: null, snapshotBlocks: null }); + assert.strictEqual(engine().stopCalls, 1); }); + } + + test('ends stopped, without running the notebook, when cancelled while the init notebook runs', async () => { + const withInit = write( + 'with-init.deepnote', + projectFile( + [notebookOf('nb-init', [createDeepnoteBlock({ id: 'setup' })]), notebookOf('nb-1', [])], + 'nb-init' + ) + ); + __setExecutionEngineBehavior({ runProject: failWhenStopped('setup') }); - const outcome: AppPreviewRunOutcome = await run(fileUri, 'nb-1'); + const outcome = run(withInit, 'nb-1'); + await waitFor(() => __getExecutionEngines()[0]?.runProjectCalls.length === 1); + cancellation.cancel(); + await waitFor(() => engine().stopCalls > 0); - assert.strictEqual(outcome.status, 'success'); + assert.deepStrictEqual(await outcome, { status: 'stopped', error: null, snapshotBlocks: null }); + assert.deepStrictEqual(notebookIdsPerCall(), [['nb-init']]); }); - test('is an internal error when running throws after a cancel once the kernel is running', async () => { + test('ends stopped when running throws after a cancel', async () => { __setExecutionEngineBehavior({ runProject: async () => { cancellation.cancel(); - throw new Error('The kernel died'); + throw new Error('Kernel client disconnected while an execution was in flight.'); } }); const outcome = await run(fileUri, 'nb-1'); - assert.deepStrictEqual(outcome, { - status: 'internal_error', - error: 'The kernel died', - snapshotBlocks: null - }); + assert.deepStrictEqual(outcome, { status: 'stopped', error: null, snapshotBlocks: null }); }); }); }); diff --git a/src/notebooks/deepnote/appPreview/types.ts b/src/notebooks/deepnote/appPreview/types.ts index d9872ab722..add98c69b6 100644 --- a/src/notebooks/deepnote/appPreview/types.ts +++ b/src/notebooks/deepnote/appPreview/types.ts @@ -31,8 +31,9 @@ export interface IAppPreviewRunner { /** * Runs the notebook `notebookId` of the `.deepnote` file at `fileUri` in a new kernel on the toolkit server the * extension runs for that file: the project's init notebook first, with its saved inputs, then the notebook with - * `inputs` applied. `onRunning` is called once the kernel is up, before the first block. Cancelling `token` - * stops the run only until then. Never rejects: failures are reported in the outcome. + * `inputs` applied. `onRunning` is called once the kernel is up, before the first block. Cancelling `token` ends + * the run as `stopped`, and once the kernel is up also stops the block that is executing and shuts the kernel down. + * Never rejects: failures are reported in the outcome. */ run(request: AppPreviewRunRequest, token: CancellationToken, onRunning: () => void): Promise; } diff --git a/src/test/mocks/deepnoteRuntimeCore.ts b/src/test/mocks/deepnoteRuntimeCore.ts index 07d76971f4..0b9c3c9803 100644 --- a/src/test/mocks/deepnoteRuntimeCore.ts +++ b/src/test/mocks/deepnoteRuntimeCore.ts @@ -29,9 +29,13 @@ let stopServerImpl: RuntimeCore['stopServer'] | null = null; const executionEngines: ExecutionEngine[] = []; let executionEngineBehavior: ExecutionEngineBehavior = {}; -/** What the fake engines do; each defaults to resolving at once, and `runProject` to an empty summary. */ +/** + * What the fake engines do; each defaults to resolving at once, and `runProject` to an empty summary. `stopped` + * settles once the engine's `stop()` is called: the real `stop()` fails the block that is executing, and + * `runProject` then resolves. + */ export interface ExecutionEngineBehavior { - runProject?(file: DeepnoteFile, options: ExecutionOptions): Promise; + runProject?(file: DeepnoteFile, options: ExecutionOptions, stopped: Promise): Promise; start?(): Promise; stop?(): Promise; } @@ -51,6 +55,10 @@ export class ExecutionEngine implements Pick void = () => undefined; + private readonly stopped = new Promise((resolve) => { + this.markStopped = resolve; + }); constructor( public readonly config: RuntimeConfig, @@ -63,7 +71,7 @@ export class ExecutionEngine implements Pick { this.stopCalls++; + this.markStopped(); await executionEngineBehavior.stop?.(); } } From 8fb12920ec1768415064158b80e617c3b4a9893c Mon Sep 17 00:00:00 2001 From: tomas Date: Wed, 18 Nov 2026 14:37:56 +0000 Subject: [PATCH 7/7] test(app-preview): stub the runner with ts-mockito Replace the hand-written FakeRunner and its recording array in the API tests with a ts-mockito mock of IAppPreviewRunner, as the notebook lookup and the unsupported-operation callback already are. startRun() stubs run() with a deferred outcome of its own, so a test settles the run it started instead of indexing into the runner's calls. --- .../appPreviewApi.node.unit.test.ts | 108 ++++++++---------- 1 file changed, 50 insertions(+), 58 deletions(-) diff --git a/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts b/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts index 369dca4fe1..12e4b6cd8a 100644 --- a/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts +++ b/src/notebooks/deepnote/appPreview/appPreviewApi.node.unit.test.ts @@ -3,8 +3,10 @@ import express from 'express'; import * as http from 'http'; import type { AddressInfo } from 'net'; import * as sinon from 'sinon'; -import { Uri, type CancellationToken } from 'vscode'; +import { anything, capture, instance, mock, verify, when } from 'ts-mockito'; +import { Uri } from 'vscode'; +import { createDeferred, type Deferred } from '../../../platform/common/utils/async'; import { isUUID } from '../../../platform/common/uuid'; import { logger } from '../../../platform/logging'; import { @@ -15,7 +17,7 @@ import { } from '../deepnoteTestHelpers'; import { createAppPreviewApi, type AppPreviewApi, type AppPreviewOperation } from './appPreviewApi.node'; import type { WorkspaceNotebook } from './appPreviewWorkspace.node'; -import type { AppPreviewRunOutcome, AppPreviewRunRequest, IAppPreviewRunner, SnapshotBlock } from './types'; +import type { AppPreviewRunOutcome, IAppPreviewRunner, SnapshotBlock } from './types'; const TOKEN = 'preview-token'; const SALES_FILE_URI = Uri.file('/workspace/sales.deepnote'); @@ -67,24 +69,6 @@ const SALES_FILE = createDeepnoteFile({ }) }); -interface RunnerCall { - request: AppPreviewRunRequest; - token: CancellationToken; - onRunning(): void; - finish(outcome: AppPreviewRunOutcome): void; - fail(error: Error): void; -} - -class FakeRunner implements IAppPreviewRunner { - public readonly calls: RunnerCall[] = []; - - public run(request: AppPreviewRunRequest, token: CancellationToken, onRunning: () => void) { - return new Promise((finish, fail) => - this.calls.push({ request, token, onRunning, finish, fail }) - ); - } -} - const CHART_SNAPSHOT: SnapshotBlock[] = [ { id: 'chart', @@ -100,12 +84,13 @@ suite('createAppPreviewApi', () => { let server: http.Server; let baseUrl: string; let api: AppPreviewApi; - let runner: FakeRunner; + let runner: IAppPreviewRunner; let findNotebook: sinon.SinonStub<[string], Promise>; let onUnsupported: sinon.SinonStub<[AppPreviewOperation], void>; setup(async () => { - runner = new FakeRunner(); + runner = mock(); + when(runner.run(anything(), anything(), anything())).thenReturn(createDeferred().promise); const notebooks = new Map( SALES_FILE.project.notebooks.map((notebook) => [ notebook.id, @@ -116,7 +101,7 @@ suite('createAppPreviewApi', () => { .stub<[string], Promise>() .callsFake(async (notebookId) => notebooks.get(notebookId)); onUnsupported = sinon.stub<[AppPreviewOperation], void>(); - api = createAppPreviewApi({ token: TOKEN, findNotebook, onUnsupported, runner }); + api = createAppPreviewApi({ token: TOKEN, findNotebook, onUnsupported, runner: instance(runner) }); const app = express(); app.use('/v2', api.router); server = http.createServer(app); @@ -163,11 +148,13 @@ suite('createAppPreviewApi', () => { } } - async function startRun(): Promise { + async function startRun(): Promise<{ runId: string; outcome: Deferred }> { + const outcome = createDeferred(); + when(runner.run(anything(), anything(), anything())).thenReturn(outcome.promise); const response = await createRun({ notebookId: 'nb-1', inputs: { region: 'Asia' } }); assert.strictEqual(response.status, 202); - return (await response.json()).runId; + return { runId: (await response.json()).runId, outcome }; } async function assertJson(response: Response, status: number, body: unknown): Promise { @@ -243,25 +230,29 @@ suite('createAppPreviewApi', () => { const body = await response.json(); assert.isTrue(isUUID(body.runId), `${body.runId} is not a UUID`); assert.deepStrictEqual(body, { runId: body.runId, status: 'pending', createdAt: NOW }); - assert.deepStrictEqual( - runner.calls.map((runCall) => runCall.request), - [{ fileUri: SALES_FILE_URI, notebookId: 'nb-1', inputs: { region: 'Asia' } }] - ); + verify(runner.run(anything(), anything(), anything())).once(); + assert.deepStrictEqual(capture(runner.run).last()[0], { + fileUri: SALES_FILE_URI, + notebookId: 'nb-1', + inputs: { region: 'Asia' } + }); sinon.assert.notCalled(onUnsupported); }); test('POST /runs runs the notebook without inputs when the body has none', async () => { assert.strictEqual((await createRun({ notebookId: 'nb-init' })).status, 202); - assert.deepStrictEqual( - runner.calls.map((runCall) => runCall.request), - [{ fileUri: SALES_FILE_URI, notebookId: 'nb-init', inputs: {} }] - ); + verify(runner.run(anything(), anything(), anything())).once(); + assert.deepStrictEqual(capture(runner.run).last()[0], { + fileUri: SALES_FILE_URI, + notebookId: 'nb-init', + inputs: {} + }); }); test("GET /runs/:runId reports the run as pending, running, and then finished, in Deepnote's shape", async () => { const clock = sinon.useFakeTimers({ now: Date.parse(NOW), toFake: ['Date'] }); - const runId = await startRun(); + const { runId, outcome } = await startRun(); const pending = { runId, notebookId: 'nb-1', @@ -277,11 +268,12 @@ suite('createAppPreviewApi', () => { }; assert.deepStrictEqual(await getRun(runId), pending); - runner.calls[0].onRunning(); + const [, , onRunning] = capture(runner.run).last(); + onRunning(); assert.deepStrictEqual(await getRun(runId), { ...pending, status: 'running' }); clock.tick(65_000); - runner.calls[0].finish({ status: 'success', error: null, snapshotBlocks: CHART_SNAPSHOT }); + outcome.resolve({ status: 'success', error: null, snapshotBlocks: CHART_SNAPSHOT }); assert.deepStrictEqual(await waitForStatus(runId, 'success'), { ...pending, status: 'success', @@ -299,22 +291,22 @@ suite('createAppPreviewApi', () => { ]; for (const [outcome, snapshotStatus] of outcomes) { test(`GET /runs/:runId reports a run that ended as ${outcome.status} with its snapshot ${snapshotStatus}`, async () => { - const runId = await startRun(); + const run = await startRun(); - runner.calls[0].finish(outcome); + run.outcome.resolve(outcome); - const run = await waitForStatus(runId, outcome.status); - assert.deepInclude(run, { ...outcome, snapshotStatus }); - assert.isString(run.completedAt); + const finished = await waitForStatus(run.runId, outcome.status); + assert.deepInclude(finished, { ...outcome, snapshotStatus }); + assert.isString(finished.completedAt); }); } test('GET /runs/:runId reports a runner that rejects as an internal error, and logs it', async () => { const logError = sinon.stub(logger, 'error'); - const runId = await startRun(); + const { runId, outcome } = await startRun(); const failure = new Error('runner bug'); - runner.calls[0].fail(failure); + outcome.reject(failure); assert.deepInclude(await waitForStatus(runId, 'internal_error'), { error: 'runner bug', @@ -332,19 +324,19 @@ suite('createAppPreviewApi', () => { }); test('dispose() cancels the unfinished runs, and records how the runner ended them', async () => { - const stoppedRunId = await startRun(); - const finishedRunId = await startRun(); - runner.calls[1].finish({ status: 'success', error: null, snapshotBlocks: [] }); - await waitForStatus(finishedRunId, 'success'); + const stopped = await startRun(); + const finished = await startRun(); + finished.outcome.resolve({ status: 'success', error: null, snapshotBlocks: [] }); + await waitForStatus(finished.runId, 'success'); api.dispose(); - assert.deepStrictEqual( - runner.calls.map((runCall) => runCall.token.isCancellationRequested === true), - [true, false] - ); - runner.calls[0].finish({ status: 'stopped', error: null, snapshotBlocks: null }); - assert.deepInclude(await waitForStatus(stoppedRunId, 'stopped'), { snapshotStatus: 'unavailable' }); + const [, stoppedToken] = capture(runner.run).first(); + const [, finishedToken] = capture(runner.run).second(); + assert.isTrue(stoppedToken.isCancellationRequested); + assert.isNotTrue(finishedToken.isCancellationRequested); + stopped.outcome.resolve({ status: 'stopped', error: null, snapshotBlocks: null }); + assert.deepInclude(await waitForStatus(stopped.runId, 'stopped'), { snapshotStatus: 'unavailable' }); }); const invalidBodies: [string, unknown, string][] = [ @@ -372,7 +364,7 @@ suite('createAppPreviewApi', () => { for (const [condition, body, message] of invalidBodies) { test(`POST /runs answers 400 to a body ${condition}, without starting a run`, async () => { await assertJson(await createRun(body), 400, { message }); - assert.deepStrictEqual(runner.calls, []); + verify(runner.run(anything(), anything(), anything())).never(); }); } @@ -396,16 +388,16 @@ suite('createAppPreviewApi', () => { }); await assertJson(response, status, { message }); - assert.deepStrictEqual(runner.calls, []); + verify(runner.run(anything(), anything(), anything())).never(); sinon.assert.notCalled(logError); }); } test("forgets the oldest finished runs beyond 20, but never an unfinished run or a notebook's newest success", async () => { - const unfinished = await startRun(); + const { runId: unfinished } = await startRun(); const finishRun = async (status: 'success' | 'error') => { - const runId = await startRun(); - runner.calls[runner.calls.length - 1].finish({ status, error: null, snapshotBlocks: [] }); + const { runId, outcome } = await startRun(); + outcome.resolve({ status, error: null, snapshotBlocks: [] }); await waitForStatus(runId, status); return runId;