Repository navigation
Conversation
Accept an optional order_id, a canonical lowercase UUIDv4 generated by the client, on POST /api/v1/vm and POST /api/v1/vm/custom-template. The first request for an account and ID claims it in the new vm_order_identity table and creates the order in the same transaction. An exact repeat returns the original VM and subscription, a changed order or the other create route returns 409, and a repeat after the VM is gone returns 404. NIP-98 requests with an order_id must carry a payload tag. Requests without an order_id are unchanged. On hourly plans the keyed path runs the existing credit quote before commit and leaves nothing behind when it refuses. Report honours_order_id on GET /api/v1/vm/templates and send the catalogue with Cache-Control: no-store. Needs migration 20261201000300_vm_order_identity.sql.
douglaz
marked this pull request as ready for review
November 28, 2026 23:03
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A client that sends
POST /api/v1/vmorPOST /api/v1/vm/custom-templateand loses the reply (a dropped connection, a timeout, a proxy error) cannot tell whether a machine was ordered. Today the only way to find out is to list the account's VMs and guess which one is new, and a plain retry orders a second machine with its own subscription, which the client may then go on to pay for.What this changes
order_idin the JSON body. The client generates it, a canonical lowercase UUIDv4, and keeps it with the order it is about to send.order_idexcluded), so JSON key order and whitespace do not matter. On the custom route the CPU selectors are normalised the way placement reads them, and the feature list is compared as a set.GET /api/v1/vm/templatesreportshonours_order_id, so a client can check before relying on the ID, and the catalogue reply is now sent withCache-Control: no-store.order_idrun the existing handlers unchanged.API_DOCUMENTATION.mdandAPI_CHANGELOG.mddescribe the field, the replies and the catalogue flag.API
Request field, on
CreateVmRequestandCustomVmOrder:Any other form (upper case, no hyphens, another UUID version,
null) is rejected when the body is parsed, before any order work.Authentication: with NIP-98, a request carrying
order_idmust include thepayloadtag, which the existing middleware checks against the body. Without the tag the request gets 401 "A signed payload is required for order_id". A retry uses a fresh auth event over the same body. Session and API token bearer authentication are unchanged.Replies, for the same authenticated account and
order_id:VmStatus)An expired VM that still exists on the account is returned with its current status. IDs are scoped per account: two accounts can use the same value independently.
Catalogue,
GET /api/v1/vm/templates:truemeans both create routes honour the contract above. A missing field orfalsemeans they do not, which is what clients see from older versions. This version always reportstrue; the value is a constant in the catalogue handler, not detected at run time. It is only correct while every process that can receive create requests runs this version, so during a rolling upgrade the catalogue should not be served by upgraded processes while create requests can still reach older ones. An older process ignoresorder_idand creates a second order. The reply carriesCache-Control: no-storeso that a savedtrueis not reused; clients are asked to read the flag fresh before the first send and again before any repeat, and to repeat only when both reads returnedtrue.Compatibility
order_idsees no change on either create route.no-storefor every client, so caches in front of the API stop caching it.Database
New migration
lnvps_db/migrations/20261201000300_vm_order_identity.sql, additive only:No foreign keys and no backfill; existing orders have no rows. The primary key decides between concurrent claims of the same ID: a second claim waits on the first transaction, then reads its committed result, or takes the claim itself if the first rolled back. The migration's version follows the three
20261201…migrations already on the base.Each API process also opens a second, lazily connected MySQL pool of up to 8 connections. It is used only for keyed orders: the claim transaction, held from the claim to the commit, and the locked read that renders a keyed reply. Those cannot starve the main pool, but they share these 8 connections (see Known limits).
On hourly plans the keyed path runs the existing credit quote before commit, with the staged rows visible to the quote's own reads, and refuses an unfunded order with the existing error message without committing anything. The same ID can be used again after the account is funded. The unkeyed paths keep their current behaviour.
Testing
Workspace test suite, all crates except the end-to-end crate, single-threaded, against a throwaway MariaDB 12.3.3 with
LNVPS_TEST_DATABASE_URLset: 2,972 passed, 0 failed, 25 ignored across 56 test harness results. This run was made before the catalogue flag was added.After the catalogue flag was added, the three tests in
api::routes::tests::orderswere run against the same kind of database: 3 passed, 0 failed. The rest of the suite was not run again after that.The new tests go through the production router and signed request middleware on both routes. They cover replay after a discarded reply through a fresh router, 409 for changed fields and for the other route, the same ID under two accounts, independent orders without an ID, rollback of an unfunded hourly order on both routes followed by a funded retry and a replay, and the catalogue flag with its cache header.
An earlier signed HTTP run (before the tests and the catalogue flag were added) against two API processes sharing one migrated MariaDB, on loopback with no workers and no payment or hypervisor backends. Two processes received the same keyed order at the same time, and both returned the same VM after the second claim was observed waiting on the first. The run also covered a sequential retry with an equivalent CPU architecture spelling, 409s, a reply discarded after commit and replayed after restarting both processes, a second account using the same ID, 401 for a signature over changed body bytes, and two unkeyed orders on each route creating two VMs. It added no payments.
A second signed HTTP run on the finished change, through the production routes on an isolated setup with no external connections. On both routes the catalogue reported
truebefore the create and again before the repeat, the create reply was discarded unread after the order was committed, and the repeat returned the same VM and subscription, leaving one order per account. Unkeyed orders on both routes created separate orders. It added no payments.On a private instance running these changes, a client's first create reply was lost and the client sent the same create again with the same order ID and body. Both requests were answered with the same machine, and the order had one invoice request and one settled payment.
The branch was then rebased onto the current
masterwithout conflicts and compile-checked (thelnvps_apilibrary, binaries and tests). The tests were not run again after the rebase.Known limits
LNVPS_TEST_DATABASE_URLis not set, as the existing database tests do. Without a database they check nothing.honours_order_idis a constant. It cannot see a mixed-version deployment, and a database backend other than MySQL (the in-process mock, for example) refuses keyed orders while the catalogue still reportstrue.