1
0
Fork 0
polaris-task-force/src/lib/operations/allocationSweep.ts
Z8MB1E 67c993ca10 feat(operations): mission reservations and deterministic settlement
Reserve personnel, vehicles, cargo, and budget with idempotent keys and exactly-once settlement; extraction effects preflight before any write so validated HQ deposits and locker credits are final and survive mission failure. Failure sweeps touch only unextracted reservations.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-09-20 02:43:47 -04:00

92 lines
2.6 KiB
TypeScript

import type { Payload, PayloadRequest } from "payload";
import type { ReservationRow, SettlementConsumed } from "./allocationTypes";
import { settleOperationReservation } from "./allocation";
/**
* Operation-wide batch settlement: mission failure and expiry sweeps. Each
* row still settles through `settleOperationReservation`, so the exactly-once
* guard applies per row and re-running a sweep is a no-op.
*/
/**
* Mission-failure settlement: every still-reserved reservation for the
* operation settles with reason `mission-failed`, consuming only what the
* caller evidences and returning the rest. Rows already settled (including
* extraction-driven settlements) are terminal and therefore untouched, and
* no operation-effects rows are written or reversed here: a recorded
* extraction deposit stays final even when the mission fails.
*/
export async function failOperationReservations(
payload: Payload,
operationId: string,
opts?: {
effectId?: number;
actorId?: number;
consumedByKey?: Record<string, SettlementConsumed>;
},
req?: PayloadRequest,
): Promise<number> {
const active = await payload.find({
collection: "operation-reservations",
where: {
and: [{ operationId: { equals: operationId } }, { status: { equals: "reserved" } }],
},
limit: 1000,
depth: 0,
overrideAccess: true,
req,
});
let settled = 0;
for (const doc of active.docs) {
const row = doc as unknown as ReservationRow;
await settleOperationReservation(
payload,
{ reservationId: row.id },
{
outcome: "settled",
reason: "mission-failed",
consumed: opts?.consumedByKey?.[row.reservationKey],
effectId: opts?.effectId,
actorId: opts?.actorId,
},
req,
);
settled += 1;
}
return settled;
}
/**
* Expiry sweeper: reservations past `expiresAt` while still reserved settle
* as `expired` with everything returned. Idempotent via the settle guard.
*/
export async function expireOperationReservations(
payload: Payload,
now: Date = new Date(),
req?: PayloadRequest,
): Promise<number> {
const due = await payload.find({
collection: "operation-reservations",
where: {
and: [{ status: { equals: "reserved" } }, { expiresAt: { less_than: now.toISOString() } }],
},
limit: 1000,
depth: 0,
overrideAccess: true,
req,
});
let settled = 0;
for (const doc of due.docs) {
const row = doc as unknown as ReservationRow;
await settleOperationReservation(
payload,
{ reservationId: row.id },
{ outcome: "expired" },
req,
);
settled += 1;
}
return settled;
}