Skip to content

Commit 1065097

Browse files
Vikhyath Mondreticlaude
andcommitted
fix(billing): charge a Pi session only when it completed
Aligns Pi with the outcome policy the Function path already applies rather than keeping the divergence the previous commit introduced. A session that ends by throwing — a provider crash, a lifetime limit, a cancellation — is absorbed, because a charge nobody can tie to delivered work is not one worth defending, and consistency across the two sandbox paths is worth more than recovering the cost of runs that failed. A command exiting non-zero is still billed: the callback returns normally there and the agent produced its answer, which is the same reason the Function path bills its own non-zero exits. The window still closes at teardown, so a completed session is charged for the whole time the provider held its sandbox. Verified on both providers, including that the new case fails when the charge is applied unconditionally. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 8b1e69f commit 1065097

2 files changed

Lines changed: 41 additions & 11 deletions

File tree

‎apps/sim/lib/execution/remote-sandbox/index.ts‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,21 +1142,31 @@ export async function withPiSandbox<T>(
11421142
writeFile: (path, content) => sandbox.writeFile(path, content),
11431143
}
11441144

1145+
let sessionCompleted = false
11451146
try {
1146-
return await fn(runner)
1147+
const result = await fn(runner)
1148+
sessionCompleted = true
1149+
return result
11471150
} finally {
11481151
/*
1149-
* Charged on creation rather than on a successful session, which is where
1150-
* this departs from the Function path — and deliberately. A Function run is
1151-
* seconds long, so absorbing one that the provider failed to deliver costs
1152-
* little and reads as fair. A Pi session holds its sandbox for tens of
1153-
* minutes, and that compute is consumed whether the agent finished, errored,
1154-
* or was cancelled. Billing only the clean endings would mean paying for
1155-
* every other one. A create that throws never reaches here, so the one case
1156-
* where nothing was provisioned is still free.
1152+
* Charged only for a session that ran to completion, which is the same rule
1153+
* the Function path applies to its own outcomes: a run whose sandbox never
1154+
* delivered is not billed, because a charge nobody can tie to delivered work
1155+
* is not one worth defending. A session that ends by throwing — a provider
1156+
* crash, a lifetime limit, a cancellation — is absorbed, and a create that
1157+
* throws never reaches here at all.
1158+
*
1159+
* A command exiting non-zero is not a failure by this rule. `fn` returns
1160+
* normally there, the agent produced its answer, and the Function path bills
1161+
* its own non-zero exits for the same reason.
1162+
*
1163+
* Measured up to teardown rather than to the last command, so the window
1164+
* covers the whole time the provider held the sandbox.
11571165
*/
1158-
const cost = calculateSandboxCost(created, Date.now())
1159-
if (cost && options.cost) options.cost.total += cost.total
1166+
if (sessionCompleted) {
1167+
const cost = calculateSandboxCost(created, Date.now())
1168+
if (cost && options.cost) options.cost.total += cost.total
1169+
}
11601170
try {
11611171
await sandbox.kill()
11621172
} catch {

‎apps/sim/lib/execution/remote-sandbox/pi-sandbox-billing.smoke.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@ describe.skipIf(!smokeEnabled)('pi sandbox billing smoke', () => {
5959
CASE_TIMEOUT_MS
6060
)
6161

62+
it(
63+
'bills nothing for a session that ended by throwing',
64+
async () => {
65+
// Mirrors the Function path: a sandbox that never delivered is absorbed
66+
// rather than charged. Covers a provider crash, a lifetime limit, and a
67+
// cancellation alike, since all three reach here the same way.
68+
const sandboxCost: SandboxCostSink = { total: 0 }
69+
70+
await expect(
71+
withPiSandbox({ lifetimeMs: LIFETIME_MS, cost: sandboxCost }, async (runner) => {
72+
await runner.run('echo started', { envs: {}, timeoutMs: CASE_TIMEOUT_MS })
73+
throw new Error('session failed after the sandbox was provisioned')
74+
})
75+
).rejects.toThrow('session failed after the sandbox was provisioned')
76+
77+
expect(sandboxCost.total).toBe(0)
78+
},
79+
CASE_TIMEOUT_MS
80+
)
81+
6282
it(
6383
'bills nothing when no sink is supplied',
6484
async () => {

0 commit comments

Comments
 (0)