Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ sentence under it is the summary shown on the

## Unreleased

### Fixed

- `vapi task fund` can request a new USDC authorization after vAPI or confirmed chain state proves an earlier authorization expired unused, including when a pool was already created or the signer was interrupted. The replacement counts once against spend caps.

## 0.17.4 - 2026-12-02

Pools with claims work in the CLI: `vapi task claim`, `show`, `cancel … pool` and `board` read them again.
Expand Down
51 changes: 51 additions & 0 deletions packages/core/src/spend-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,57 @@ export async function releaseSpend(
});
}

type TaskFundingReservationLookup = {
ledgerPath: string;
wallet: WalletName;
taskKey: string;
reservationId: string;
now: Date;
};

function matchesTaskFundingReservation(
entry: z.infer<typeof spendReservationSchema>,
input: TaskFundingReservationLookup,
) {
return (
entry.id === input.reservationId &&
entry.kind === "escrow-funding" &&
(entry.taskKey === undefined || entry.taskKey === input.taskKey)
);
}

/** Whether this preparation has reserved funding spend on any stored day. */
export async function hasTaskFundingReservation(input: TaskFundingReservationLookup) {
const rows = await readStoredSpendLedgerRows(input.ledgerPath, input.now, true);
return rows.some(
(row) =>
row.wallet === input.wallet &&
row.reservations?.some((entry) => matchesTaskFundingReservation(entry, input)),
);
}

/** Releases the exact reservation after its funding authorization is proven unusable. */
export async function releaseTaskFundingReservation(
input: TaskFundingReservationLookup,
): Promise<void> {
await withLedgerLock(input.ledgerPath, async () => {
const rows = await readStoredSpendLedgerRows(input.ledgerPath, input.now, true);
let released = false;
for (const row of rows) {
if (row.wallet !== input.wallet) continue;
const reservation = row.reservations?.find((entry) =>
matchesTaskFundingReservation(entry, input),
);
if (!reservation) continue;
const remaining = BigInt(row.spentAtomic) - BigInt(reservation.amountAtomic);
row.spentAtomic = (remaining > 0n ? remaining : 0n).toString();
row.reservations = row.reservations!.filter((entry) => entry !== reservation);
released = true;
}
if (released) await writeLedgerAtomically(input.ledgerPath, rows);
});
}

function parseAtomicCap(value: string, label: string): bigint {
if (!/^\d+$/.test(value)) {
throw new Error(`Invalid ${label} spend cap ${JSON.stringify(value)}.`);
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/tasks/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ export type TasksRequestOptions = { signal?: AbortSignal };
/** Retain the same key when retrying the same mutation. The client never retries automatically. */
export type TasksMutationOptions = TasksRequestOptions & { idempotencyKey: string };

type TaskV2OperationBody = {
step: string;
transactionHash?: string;
proof?: "expired-authorization";
};

const tasksClientErrorBrand = Symbol.for("@vapi-network/core/TasksClientError");

export class TasksClientError extends Error {
Expand Down Expand Up @@ -381,7 +387,7 @@ export function createTasksClient(options: TasksClientOptions): TasksClientV2 {
operationV2(
id: string,
mode: "transaction" | "reconcile" | "recover" | "abandon",
body: { step: string; transactionHash?: string },
body: TaskV2OperationBody,
call: TasksMutationOptions,
) {
input(taskV2StepSchema, body.step);
Expand Down Expand Up @@ -912,7 +918,7 @@ export type TasksClient = {
operationV2?(
id: string,
mode: "transaction" | "reconcile" | "recover" | "abandon",
body: { step: string; transactionHash?: string },
body: TaskV2OperationBody,
call: TasksMutationOptions,
): Promise<
| z.infer<typeof taskV2OperationResponseSchema>
Expand Down
Loading
Loading