mirror of
https://github.com/nestriness/nestri.git
synced 2026-12-04 04:56:21 +02:00
feat(core,api): record burn as rate segments, and refuse the next run when spent
The counters this fills are the ones the windows already knew how to read. What was missing was anything that put a number in them. Burn is recorded as segments: one stretch of one run at one unchanging rate, opened when the rate becomes true and closed when it stops being. Not a row per session, because a session's rate does not survive its own lifetime — a second run changes what the account spends per second while the first is still going, and a rate that applied from that moment must not be backdated over the time before it. Not a row per event either, because burn accrues against an envelope that is held rather than per thing consumed. Closing a segment is what moves burn into the counters, so a long run lands incrementally instead of all at the end. Burn that only arrives when a session stops is burn that cannot refuse the next one, and a bar that does not move while something is running is a bar nobody believes. The counters are written with the staleness rule as a single statement: add to the total if its stamp is still inside the window, otherwise start again from this amount. Reading and then deciding would be two statements with a gap, and the gap is where a concurrent tick doubles or vanishes. The first tick for a team and the thousandth are the same call, for the same reason. The gate sits at the one moment it is allowed to speak — before a run starts, never again. A limit refuses the next run and never interrupts one already going; someone losing a session mid-game to a meter does not come back. Every window is checked rather than the shortest, because they protect different things over different spans. The answer comes back with the created run rather than being thrown away: the response carries where each window stands, what the account spends per second now, and what one more run would cost. Every surface that can start a run has to show that before the click, and a second call for it is a call nobody makes. Asking twice would also let the number shown and the number billed disagree. Accrual is wired to the run's own state transition, in the same transaction that moves it. A session that went live without its meter starting is free hardware; one that ended without its meter stopping bills forever. Both are silent, so neither may be a second write that might not happen.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { and, desc, eq, inArray, isNull, sql } from 'drizzle-orm';
|
||||
import z from 'zod';
|
||||
|
||||
import { Burn } from '../billing/burn.js';
|
||||
import { BoxTable, BoxTier } from '../box/box.sql.js';
|
||||
import { Box } from '../box/index.js';
|
||||
import { Database } from '../db/index.js';
|
||||
@@ -8,6 +9,7 @@ import { ErrorCodes, VisibleError } from '../error.js';
|
||||
import { Examples } from '../examples.js';
|
||||
import { fn } from '../fn.js';
|
||||
import { GameTable } from '../game/game.sql.js';
|
||||
import { Machine } from '../machine/index.js';
|
||||
import { SessionState, SessionTable } from './session.sql.js';
|
||||
|
||||
/**
|
||||
@@ -650,6 +652,27 @@ export namespace Session {
|
||||
await Box.setState({ id: moved.boxId, ...box });
|
||||
}
|
||||
|
||||
// Burn follows the run's state, in the same transaction that
|
||||
// moved it. A session that went live without its meter starting
|
||||
// is free hardware; one that ended without its meter stopping
|
||||
// bills forever. Both are silent, so neither may be a second
|
||||
// write that might not happen.
|
||||
//
|
||||
// `live` is where it starts rather than `starting`, because what
|
||||
// is billed is an envelope actually held — a box that never
|
||||
// finished coming up held nothing.
|
||||
const machine = await Machine.fromID(input.machineId);
|
||||
if (machine?.teamId) {
|
||||
if (current.state !== 'live' && moved.state === 'live') {
|
||||
await Burn.start({ teamId: machine.teamId, sessionId: moved.id });
|
||||
} else if (
|
||||
ACCRUING.includes(current.state as (typeof ACCRUING)[number]) &&
|
||||
!ACCRUING.includes(moved.state as (typeof ACCRUING)[number])
|
||||
) {
|
||||
await Burn.stop({ teamId: machine.teamId, sessionId: moved.id });
|
||||
}
|
||||
}
|
||||
|
||||
return { outcome: 'moved', session: moved };
|
||||
});
|
||||
}
|
||||
@@ -671,6 +694,16 @@ export namespace Session {
|
||||
*/
|
||||
const ADDRESSABLE = ['starting', 'live'] as const;
|
||||
|
||||
/**
|
||||
* The states in which a run is spending.
|
||||
*
|
||||
* Only `live`. A run that is being brought up holds nothing yet, and the
|
||||
* terminal states hold nothing any more — so this is a list of one, written
|
||||
* as a list because the question it answers is "is this run costing
|
||||
* anything?" and that will not always have one answer.
|
||||
*/
|
||||
const ACCRUING = ['live'] as const;
|
||||
|
||||
/**
|
||||
* Publish a ticket for a run, on behalf of the host it is placed on.
|
||||
*
|
||||
|
||||
@@ -47,7 +47,14 @@ async function scene(label: string, steamAppId: number) {
|
||||
afterAll(async () => {
|
||||
if (createdUserIds.length > 0) {
|
||||
// session cascades from box; box has to precede the machine, which
|
||||
// cascades from the user.
|
||||
// cascades from the user. `burn_segment` holds a session with
|
||||
// `restrict` — deleting a run must not erase what it cost — so what the
|
||||
// runs cost goes before the runs do.
|
||||
await sql`delete from "burn_segment" where session_id in (
|
||||
select s.id from "session" s
|
||||
join "box" b on b.id = s.box_id
|
||||
where b.user_id in ${sql(createdUserIds)}
|
||||
)`;
|
||||
await sql`delete from "box" where user_id in ${sql(createdUserIds)}`;
|
||||
await sql`delete from "user" where id in ${sql(createdUserIds)}`;
|
||||
createdUserIds.length = 0;
|
||||
|
||||
Reference in New Issue
Block a user