feat(core): a box is a row, a session is the billing unit

Migration 1 of 0048, and the first of the seven weeks — nothing about a live
feed works without these two tables, so it is not a cleanup during them.

  box      a VM someone owns: an id that is also its DNS label, an editable
           label, an owning user, the machine it sits on, a tier and a state.
           Owned by a person and placed on a team's hardware, which are two
           different relationships, hence both userId and machineId.
  session  one run of one box by one linked Steam account, and what costs
           money. Separate from box because the ticket changes after bind as
           addresses are discovered — the vsock contract calls it "a stream,
           not one value" — so it is a column a client polls, not a value it
           is handed once.

Box states are neslet's own three and no more. `starting` and `stopping` are
the obvious additions and both are omitted because nothing would ever write
them; a failed box is `stopped` with stopClean false, which is how neslet
models it too.

The generated migration would have failed on live rows in three ways, so it
is hand-written and tested against a database seeded at the old schema:

  - machine.team_id becomes notNull, and *every existing row is null* because
    the old registration path passed null. Personal teams are backfilled for
    machine owners first, reusing a team they already own rather than minting
    a second, with the owner membership row repaired where missing.
  - game_download.host_id becomes a foreign key. It held free-form strings,
    so unattributable rows are deleted before the cast — the only destructive
    statement here, and a considered loss: it is a progress report neslet
    re-derives from disk.
  - Team.createPersonal was written and documented in packages/core/CLAUDE.md
    as part of the login flow and never actually called, so no user has a
    team. ensurePersonal is idempotent and now runs on every login, which is
    what backfills accounts the migration does not reach.

Verified on a seeded legacy database: three null-team machines backfilled, an
existing team reused rather than duplicated, a blank display name handled, and
both unattributable download rows dropped while the attributable one survived.

Also fixes two things this work ran into rather than caused:

  - Database.client() built a new postgres pool on every call, and use()
    called it twice per invocation — pools of ten connections held for a 30s
    idle timeout. Invisible in a Worker where requests are short; the suite
    crossed 100 connections and Postgres said "sorry, too many clients
    already" in whichever file ran last, which reads as a flaky test rather
    than a leak. Now one pool per connection string.
  - download.test.ts asserted against `hst_…` host ids, which is exactly the
    unattributable row the new foreign key exists to refuse.

There is no "no team" any more: PATCH /machine/:id took teamId null to mean
"mine alone" and now requires a team, because the personal team is the one to
name. Its test is updated to the new contract rather than deleted.

113 → 128 tests, 0 fail.
This commit is contained in:
Wanjohi
2026-09-03 21:39:27 +03:00
parent 1bfdfcf3cf
commit 6c1d407985
23 changed files with 3681 additions and 44 deletions

View File

@@ -6,8 +6,10 @@ import { CloudflareStorage } from '@nestri/auth/storage/cloudflare';
import { subjects } from '@nestri/core/auth/subjects';
import { Database } from '@nestri/core/db/index';
import { Env } from '@nestri/core/env';
import { Actor } from '@nestri/core/actor';
import { Identifier } from '@nestri/core/id';
import { Steam } from '@nestri/core/steam/index';
import { Team } from '@nestri/core/team/index';
import { User } from '@nestri/core/user/index';
import { LinkedAccount } from '@nestri/core/user/linked-account';
@@ -81,6 +83,19 @@ export default {
return { userID: newUserID, linkedAccountID: newLinkedAccountID };
});
// Every user needs a personal team, because `machine.teamId` is
// notNull since 0048 and registering a host has nowhere to put
// it otherwise. `packages/core/CLAUDE.md` documented this call
// as part of the login flow and it was never actually made, so
// no user in the database has one.
//
// Run on every login rather than only on creation: that is what
// backfills the accounts made before this existed, and
// `ensurePersonal` is idempotent precisely so it can be.
await Actor.with({ type: 'user', properties: { userID, linkedAccountID } }, () =>
Team.ensurePersonal({ displayName: personaname })
);
return context.subject('user', {
userID,
linkedAccountID
@@ -96,6 +111,16 @@ export default {
profile
});
// Same reason as the Steam branch above. The SSH path creates
// users too, so leaving it out would give a host registered
// from `nessh` nowhere to live.
await Actor.with({ type: 'user', properties: { userID, linkedAccountID } }, () =>
// `username` is optional on the SSH path — a key can arrive
// before a persona does. The slug only has to be derivable,
// not pretty, and a rename is a later problem.
Team.ensurePersonal({ displayName: username ?? 'Player' })
);
return context.subject('user', {
userID,
linkedAccountID,