release(api): machines keep the GPUs their host reports (#376)

- The host report accepts `gpus` (model, VRAM in MiB, PCI id); the
machine keeps them and `/machine` returns them. One new nullable column.
- Must reach prod before host agent 0.2.10, which sends the field.
This commit is contained in:
Wanjohi
2026-10-09 08:45:57 +00:00
committed by GitHub
7 changed files with 3933 additions and 6 deletions
+10 -3
View File
@@ -515,12 +515,16 @@ export namespace MachineApi {
}),
boxes: z.array(Box.Reported).meta({
description: 'Every box the host holds. A full snapshot, never a delta'
}),
gpus: z.array(Machine.Gpu).max(64).optional().meta({
description:
'The cards in this host. The first measured part of its capacity; left off when none could be read'
})
})
// Strict, so a field this cannot act on is a validation error a
// host operator sees rather than one quietly dropped. Capacity
// belongs here eventually and it has no honest fields yet;
// refusing the ones nobody measures is how it stays that way.
// belongs here, one measured field at a time: `gpus` is the
// first, and refusing the ones nobody measures keeps it honest.
.strict()
),
async (c) => {
@@ -536,8 +540,11 @@ export namespace MachineApi {
);
}
const { boxes } = c.req.valid('json');
const { boxes, gpus } = c.req.valid('json');
const outcome = await Box.applyHostReport({ machineId: Actor.machineID, boxes });
if (gpus && gpus.length > 0) {
await Machine.setGpus({ id: Actor.machineID, gpus });
}
// `agentPid`, `boxesKnown` and `boxesRunning` are read and not
// stored. They are a summary of the list that follows them, and a
+20
View File
@@ -87,6 +87,26 @@ describe('POST /machine/report', () => {
expect((await Box.fromID(running.id))!.state).toBe('running');
});
test('the cards a host reports are kept on its machine', async () => {
const host = await registeredHost('report-gpus');
const gpus = [{ model: 'AMD Radeon RX 9060 XT', vramMib: 16304, pciId: '1002:7590' }];
const res = await report(host, {
agentPid: 1,
boxesKnown: 0,
boxesRunning: 0,
boxes: [],
gpus
});
expect(res.status).toBe(200);
expect((await Machine.fromID(host.id))!.gpus).toEqual(gpus);
// A report without them leaves what is known alone.
await report(host, { agentPid: 1, boxesKnown: 0, boxesRunning: 0, boxes: [] });
expect((await Machine.fromID(host.id))!.gpus).toEqual(gpus);
});
test('a request is not wrapped in an envelope, but the response is', async () => {
const host = await registeredHost('report-envelope');
@@ -0,0 +1 @@
ALTER TABLE "machine" ADD COLUMN "gpus" jsonb;
File diff suppressed because it is too large Load Diff
@@ -148,6 +148,13 @@
"when": 1791359465283,
"tag": "0020_steam_link_request",
"breakpoints": true
},
{
"idx": 21,
"version": "7",
"when": 1791534844848,
"tag": "0021_machine_gpus",
"breakpoints": true
}
]
}
+47 -1
View File
@@ -39,6 +39,26 @@ export namespace Machine {
message: 'An endpoint id is 64 lowercase hex characters'
});
/** One card in a host, as the host reports it. */
export const Gpu = z
.object({
model: z.string().min(1).max(128).meta({
description: 'What the card is called, as its driver names it',
example: 'AMD Radeon RX 9060 XT'
}),
vramMib: z.number().int().nonnegative().optional().meta({
description: 'The card’s own memory in MiB. Absent when the driver does not say',
example: 16304
}),
pciId: z
.string()
.regex(/^[0-9a-f]{4}:[0-9a-f]{4}$/)
.meta({ description: 'vendor:device, the ids the model was looked up by', example: '1002:7590' })
})
.meta({ ref: 'Gpu' });
export type Gpu = z.infer<typeof Gpu>;
export const Info = z
.object({
id: z.string().meta({
@@ -77,6 +97,10 @@ export namespace Machine {
description:
'Where this host can be reached, as its own endpoint id. Null until the host has reported one — it holds the secret half of this identity, so it is the only thing that can say what the public half is',
example: Examples.Machine.endpointId
}),
gpus: z.array(Gpu).nullable().optional().meta({
description:
'The cards in this host, as its last report listed them. Null until a host has reported them'
})
})
.meta({
@@ -450,6 +474,27 @@ export namespace Machine {
}
);
/**
* Record the cards a host reported. Written only when they differ, so a report every
* few minutes does not rewrite a row that has not changed.
*/
export const setGpus = fn(
z.object({ id: z.string(), gpus: z.array(Gpu).max(64) }),
async (input) => {
await Database.use(async (tx) => {
await tx
.update(MachineTable)
.set({ gpus: input.gpus })
.where(
and(
eq(MachineTable.id, input.id),
sql`${MachineTable.gpus} is distinct from ${JSON.stringify(input.gpus)}::jsonb`
)
);
});
}
);
/** Every host an organisation owns outright — its fleet. */
export const listByOrganisation = fn(z.string(), async (organisationId) => {
return Database.use(async (tx) => {
@@ -493,7 +538,8 @@ export namespace Machine {
label: input.label,
slug: input.slug,
lastSeen: input.lastSeen?.toISOString() ?? null,
endpointId: input.endpointId
endpointId: input.endpointId,
gpus: input.gpus ?? null
};
}
}
+8 -2
View File
@@ -1,5 +1,5 @@
import { sql } from 'drizzle-orm';
import { check, index, pgTable, text, uniqueIndex } from 'drizzle-orm/pg-core';
import { check, index, jsonb, pgTable, text, uniqueIndex } from 'drizzle-orm/pg-core';
import { id, timestamps, ulid, utc } from '../db/types.js';
import { OrganisationTable } from '../organisation/organisation.sql.js';
@@ -95,7 +95,13 @@ export const MachineTable = pgTable(
// The secret itself is returned exactly once, at registration, and never
// stored: a leaked database must not yield working box credentials.
secretHash: text('secret_hash').notNull(),
lastSeen: utc('last_seen')
lastSeen: utc('last_seen'),
// The cards in this host, as its last report listed them: model, VRAM in
// MiB when the driver says, and the PCI id it was named by. What a person
// calls the machine ("AMD Radeon RX 9060 XT"), and the first measured part
// of the capacity a scheduler will place by. Null until a report carries
// them; never filled in by guessing.
gpus: jsonb('gpus').$type<{ model: string; vramMib?: number; pciId: string }[]>()
},
(t) => [
uniqueIndex('machine_secret_hash_unique').on(t.secretHash),