Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(notebooks): name the hourly rate when a cell starts a sandbox (#91024)

Sinan Kucukkoselerf8228e2e58c3
diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts
index 0144432e7146..e1f449ee6ad5 100644
--- a/frontend/src/lib/api.ts
+++ b/frontend/src/lib/api.ts
@@ -4913,7 +4913,7 @@ const api = {
connection_id?: string | null
send_raw_query?: boolean
}
- ): Promise<{ run_id: string }> {
+ ): Promise<{ run_id: string; starts_sandbox?: boolean; sandbox_hourly_price?: number | null }> {
return await new ApiRequest().notebook(notebookId).withAction('sql_v2/run').create({ data })
},
async sqlV2RunInterrupt(
diff --git a/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.test.ts b/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.test.ts
index 9470490e6c25..4f4de4c759fa 100644
--- a/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.test.ts
+++ b/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.test.ts
@@ -217,25 +217,43 @@ describe('notebookNodeSQLV2Logic', () => {
})
})
- it('opens the kernel panel and notifies for a kernel-lane run, and not for a direct one', async () => {
+ it('opens the kernel panel for a kernel-lane run, and not for a direct one', async () => {
// Scenario B: a run that needs the sandbox must surface the provisioning wait;
- // a pure-SQL run must never pop the panel or toast (it needs no sandbox at all).
- const toastSpy = jest.spyOn(lemonToast, 'info')
+ // a pure-SQL run must never pop the panel (it needs no sandbox at all).
mount()
logic.actions.runQuery('select 1')
await expectLogic(logic).toFinishAllListeners()
expect(notebookSettingsLogic.findMounted()?.values.showKernelInfo).toBe(false)
expect(logic.values.pendingKernelStart).toBe(false)
- expect(toastSpy).not.toHaveBeenCalled()
logic.actions.runQuery('select * from new_events', { new_events: { node_id: 'py', kind: 'local' } })
await expectLogic(logic).toFinishAllListeners()
expect(notebookSettingsLogic.findMounted()?.values.showKernelInfo).toBe(true)
expect(logic.values.pendingKernelStart).toBe(true)
- expect(toastSpy).toHaveBeenCalledWith(expect.stringContaining('Starting a compute sandbox'))
})
})
+ // The run response is the only source that knows whether this run provisions, because the
+ // backend decides it at dispatch. A client that guesses from a kernel poll either bills a
+ // user twice for one sandbox or starts a paid one in silence.
+ test.each([
+ ['names the rate when the run starts a paid sandbox', true, 0.25, ['compute sandbox at $0.25 / h']],
+ // The unpriced branch is the only one that ends the sentence here, so matching it also
+ // proves no rate was quoted.
+ ['announces without a rate when the run reports no price', true, null, ['compute sandbox. The cell']],
+ ['stays quiet when the run reuses a running sandbox', false, null, []],
+ ])('%s', async (_name, startsSandbox, price, expected) => {
+ runSpy.mockResolvedValue({ run_id: 'r1', starts_sandbox: startsSandbox, sandbox_hourly_price: price })
+ const toastSpy = jest.spyOn(lemonToast, 'info')
+ mount()
+ logic.actions.runQuery('select * from new_events', { new_events: { node_id: 'py', kind: 'local' } })
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(toastSpy.mock.calls.map(([message]) => message)).toEqual(
+ expected.map((fragment) => expect.stringContaining(fragment))
+ )
+ })
+
it('rejects blank code before dispatching a run', async () => {
mount()
logic.actions.runQuery(' ')
diff --git a/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.ts b/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.ts
index 94a5fdfe7f29..f0e605d66998 100644
--- a/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.ts
+++ b/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.ts
@@ -223,6 +223,9 @@ export interface notebookNodeSQLV2LogicActions {
setShowKernelInfo: (showKernelInfo: boolean) => {
showKernelInfo: boolean
} // notebookSettingsLogic
+ announceSandboxStart: (hourlyPrice: number | null) => {
+ hourlyPrice: number | null
+ }
interruptRun: () => {
value: true
}
@@ -346,6 +349,7 @@ export const notebookNodeSQLV2Logic = kea<notebookNodeSQLV2LogicType>([
resetPaging: true,
setDirectRows: (directRows: NotebookNodeSQLV2DirectRows | null) => ({ directRows }),
setPendingKernelStart: (pendingKernelStart: boolean) => ({ pendingKernelStart }),
+ announceSandboxStart: (hourlyPrice: number | null) => ({ hourlyPrice }),
}),
reducers({
// Tracks the run being in progress; driven by the run's status, not a socket lifecycle.
@@ -552,7 +556,6 @@ export const notebookNodeSQLV2Logic = kea<notebookNodeSQLV2LogicType>([
if (!kernelKnownRunning) {
actions.setShowKernelInfo(true)
actions.setPendingKernelStart(true)
- lemonToast.info('Starting a compute sandbox. The cell will run once it’s ready.')
}
}
// Read from the notebook rather than the caller: every run path (Run button,
@@ -560,7 +563,7 @@ export const notebookNodeSQLV2Logic = kea<notebookNodeSQLV2LogicType>([
const variables = props.getVariables?.() ?? []
actions.startOperation(runOperation)
try {
- const { run_id } = await api.notebooks.sqlV2Run(props.notebookShortId, {
+ const runResponse = await api.notebooks.sqlV2Run(props.notebookShortId, {
node_id: props.nodeId,
code,
refs,
@@ -577,7 +580,14 @@ export const notebookNodeSQLV2Logic = kea<notebookNodeSQLV2LogicType>([
})
// Mark this as the active run so a still-in-flight poll from a previous run
// can't overwrite this result or stop this run's poller once it resolves.
+ const run_id = runResponse.run_id
cache.activeRunId = run_id
+ // The backend decides this at dispatch, so it is the only accurate source.
+ // A kernel poll is up to ten seconds old, which is long enough to miss a
+ // sandbox that stopped inside that window and start paid compute silently.
+ if (runResponse.starts_sandbox) {
+ actions.announceSandboxStart(runResponse.sandbox_hourly_price ?? null)
+ }
// Persisting nodeId pins the cell's identity: markdown-notebook cell ids are
// content fingerprints otherwise, so without the pin any later prop change
// would orphan this run's node_id and break refs to this cell.
@@ -595,6 +605,18 @@ export const notebookNodeSQLV2Logic = kea<notebookNodeSQLV2LogicType>([
actions.nodeRunFinished(props.nodeId, 'failed', null)
}
},
+ announceSandboxStart: ({ hourlyPrice }) => {
+ // The panel that shows the price is only just opening, so say the rate in the
+ // toast too. A user who runs a Python cell never opens the panel deliberately and
+ // would otherwise start paid compute without being told what it costs. The price
+ // is null for a local docker kernel, which is free.
+ lemonToast.info(
+ hourlyPrice == null
+ ? 'Starting a compute sandbox. The cell will run once it’s ready.'
+ : `Starting a compute sandbox at $${hourlyPrice.toFixed(2)} / h while it runs. ` +
+ 'Change its size in the kernel panel.'
+ )
+ },
runNode: ({ overrides }) => {
const content = props.getContent?.() ?? null
const self = content ? buildNotebookDependencyGraph(content).nodesById[props.nodeId] : null