mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-05 23:06:17 +01:00
fix: settle closed externally billed Native accounting without invented usage
This commit is contained in:
1 parent
e73ebcdb57
commit
a0b2ba8be3
2 files changed
+52
-5
No files matched your search
@@ -0,0 +1,41 @@
|
||||
import { sql } from "drizzle-orm";
|
||||
import { heartbeatRuns, nativeRunFinalizations, nativeRunResults } from "@paperclipai/db";
|
||||
|
||||
/** Provider-owned normalization of the existing server-authored accounting
|
||||
* contract. Runtime selection validates the explicit unmetered acknowledgement
|
||||
* before freezing v6/v8; the executor authors providerAccounting after execution.
|
||||
* Neither current agent configuration nor a provider's raw result grants this.
|
||||
* A matching accepted, committed result closes the operation even when its
|
||||
* disposition failed. No usage or price is manufactured by this closure. */
|
||||
export const externallyBilledNativeAccountingClosure = sql<boolean>`coalesce((
|
||||
${heartbeatRuns.runtimeMode} = 'native'
|
||||
and ${heartbeatRuns.status} in ('succeeded', 'failed', 'cancelled', 'timed_out', 'interrupted')
|
||||
and ${heartbeatRuns.usageJson}->>'accountingCaptureFailed' is distinct from 'true'
|
||||
and ${heartbeatRuns.resultJson}->'providerAccounting'->'externallyBilled' = 'true'::jsonb
|
||||
and ${heartbeatRuns.resultJson}->'providerAccounting'->'usage' = 'null'::jsonb
|
||||
and ${heartbeatRuns.resultJson}->'providerAccounting'->'cost' = 'null'::jsonb
|
||||
and ${heartbeatRuns.runnerProfileJson}->'nativeExecutionInput'->'binding'->>'companyId' = ${heartbeatRuns.companyId}::text
|
||||
and ${heartbeatRuns.runnerProfileJson}->'nativeExecutionInput'->'binding'->>'agentId' = ${heartbeatRuns.agentId}::text
|
||||
and ${heartbeatRuns.runnerProfileJson}->'nativeExecutionInput'->'binding'->>'runId' = ${heartbeatRuns.id}::text
|
||||
and ${heartbeatRuns.runnerProfileJson}->'nativeExecutionInput'->'binding'->>'issueId' = ${heartbeatRuns.nativeIssueId}::text
|
||||
and (
|
||||
(${heartbeatRuns.runnerProfileJson}->'nativeExecutionInput'->>'schema' = 'paperclip.native-execution-input.v6'
|
||||
and ((${heartbeatRuns.runnerProfileJson}->'nativeExecutionInput'->'provider'->>'kind' = 'openai_dot'
|
||||
and ${heartbeatRuns.runnerProfileJson}->'nativeExecutionInput'->'session'->>'driverKind' = 'openai_dot_mcp')
|
||||
or (${heartbeatRuns.runnerProfileJson}->'nativeExecutionInput'->'provider'->>'kind' = 'grok_bot'
|
||||
and ${heartbeatRuns.runnerProfileJson}->'nativeExecutionInput'->'session'->>'driverKind' = 'grok_bot_mcp')))
|
||||
or (${heartbeatRuns.runnerProfileJson}->'nativeExecutionInput'->>'schema' = 'paperclip.native-execution-input.v8'
|
||||
and ${heartbeatRuns.runnerProfileJson}->'nativeExecutionInput'->'provider'->>'kind' = 'muse'
|
||||
and ${heartbeatRuns.runnerProfileJson}->'nativeExecutionInput'->'session'->>'driverKind' = 'muse_external')
|
||||
)
|
||||
and exists (select 1 from ${nativeRunFinalizations} accounting_finalizer
|
||||
join ${nativeRunResults} accounting_result on accounting_result.id = accounting_finalizer.result_id
|
||||
and accounting_result.company_id = accounting_finalizer.company_id
|
||||
and accounting_result.run_id = accounting_finalizer.run_id
|
||||
and accounting_result.issue_id = accounting_finalizer.issue_id
|
||||
where accounting_finalizer.company_id = ${heartbeatRuns.companyId}
|
||||
and accounting_finalizer.run_id = ${heartbeatRuns.id}
|
||||
and accounting_finalizer.issue_id = ${heartbeatRuns.nativeIssueId}
|
||||
and accounting_finalizer.phase = 'committed'
|
||||
and accounting_result.schema_status = 'accepted')
|
||||
), false)`;
|
||||
@@ -20,6 +20,7 @@ import { priceAnthropicReceipt } from "./anthropic-pricing.js";
|
||||
import { priceCodexReceipt } from "./codex-pricing.js";
|
||||
import type { AdapterUsageCheckpoint } from "@paperclipai/adapter-utils";
|
||||
import { replayUsageReceipts, indexPendingUsageReceipts, readPendingRunUsageReceipts, persistUsageReceiptInTransaction, reconcileLateRunUsageReceiptInTransaction, type UsageReceiptIndex } from "./usage-receipts.js";
|
||||
import { externallyBilledNativeAccountingClosure } from "./native-runtime/external-provider-accounting.js";
|
||||
import { crashPoint } from "@paperclipai/shared/crash-points";
|
||||
|
||||
const terminalStatuses = ["succeeded", "failed", "timed_out", "cancelled", "interrupted"];
|
||||
@@ -32,6 +33,7 @@ const amount = (value: unknown) => typeof value === "number" && Number.isFinite(
|
||||
* rather than acknowledging evidence from an earlier spool snapshot. */
|
||||
async function readAccountingClosure(db: Db, runId: string) {
|
||||
const [closure] = await db.select({
|
||||
externallyBilled: externallyBilledNativeAccountingClosure,
|
||||
sourceId: sql<unknown>`${heartbeatRuns.usageJson}->'accountingReceiptSourceId'`,
|
||||
sequence: sql<unknown>`${heartbeatRuns.usageJson}->'accountingReceiptSequence'`,
|
||||
receiptId: sql<unknown>`${heartbeatRuns.usageJson}->'accountingReceiptId'`,
|
||||
@@ -325,7 +327,11 @@ export async function accountRunCost(db: Db, runId: string, hooks: BudgetService
|
||||
const [coordinator] = await tx.select().from(nativeRunFinalizations).where(eq(nativeRunFinalizations.runId, run.id));
|
||||
if (coordinator && !coordinator.resultId && coordinator.phase !== "terminal_failure") return false;
|
||||
}
|
||||
const receiptUnavailable = !preProviderFailure && (captureFailed || object(run.usageJson).accountingReceiptReady === false);
|
||||
// The provider contract explicitly reports unavailable usage and external
|
||||
// billing. This is a closed operation, not a lost receipt: preserve the
|
||||
// unpriced charge and ordinary unpriced-budget policy (no timeout exemption).
|
||||
const receiptUnavailable = !preProviderFailure && !closure.externallyBilled
|
||||
&& (captureFailed || object(run.usageJson).accountingReceiptReady === false);
|
||||
const receiptClosure = captureFailed ? "timed_out" as const
|
||||
: receiptUnavailable ? await readProviderReceiptClosure(tx, run.id) : null;
|
||||
if (receiptUnavailable && !receiptClosure) return false;
|
||||
@@ -359,7 +365,7 @@ export async function accountRunCost(db: Db, runId: string, hooks: BudgetService
|
||||
// projection keeps the price already observed. A receipt that will never
|
||||
// complete (lost, timed out, or a failed capture) settles at no less than
|
||||
// that observed money, so settlement never lowers spend.
|
||||
const observedCents = receiptUnavailable ? text(object(usage.budgetExposure).costCents) : null;
|
||||
const observedCents = receiptUnavailable || closure.externallyBilled ? text(object(usage.budgetExposure).costCents) : null;
|
||||
let observedCostUsd: string | null = null;
|
||||
if (observedCents) {
|
||||
try {
|
||||
@@ -375,11 +381,11 @@ export async function accountRunCost(db: Db, runId: string, hooks: BudgetService
|
||||
const billingType = text(usage.billingType) ?? "unknown";
|
||||
// A lost receipt that already carries a provider-reported cost settles at
|
||||
// that cost, as an estimate marked as a lower bound, not as unpriced.
|
||||
const reportedLowerBound = receiptUnavailable && knownCostUsd !== null && billingType !== "subscription_included"
|
||||
const reportedLowerBound = (receiptUnavailable || closure.externallyBilled) && knownCostUsd !== null && billingType !== "subscription_included"
|
||||
&& usdToUnits(knownCostUsd) > 0n;
|
||||
const lowerBoundProvenance = reportedLowerBound ? { source: "provider_reported", version: "accounting-receipt/v1",
|
||||
...object(usage.pricingProvenance),
|
||||
evidence: [text(object(usage.pricingProvenance).evidence), "final receipt lost; provider-reported cost of the observed usage is a lower bound"]
|
||||
evidence: [text(object(usage.pricingProvenance).evidence), closure.externallyBilled ? "externally billed provider reports no final price; previously observed cost is a lower bound" : "final receipt lost; provider-reported cost of the observed usage is a lower bound"]
|
||||
.filter(Boolean).join("; ") } : null;
|
||||
const costUnits = billingType === "subscription_included" ? 0n : usdToUnits(costUsd ?? 0);
|
||||
const costCents = unitsToCents(costUnits);
|
||||
@@ -482,7 +488,7 @@ function actionableRunAccounting(at: Date = new Date()) {
|
||||
and ${heartbeatRuns.runtimeMode} = 'native' and ${nativeRunFinalizations.resultId} is null
|
||||
and ${nativeRunFinalizations.phase} <> 'terminal_failure'))`,
|
||||
sql`(${heartbeatRuns.usageJson}->>'accountingReceiptReady' is distinct from 'false' or ${heartbeatRuns.resultJson}->'executionRecovery'->>'providerWorkStarted' = 'false' or ${heartbeatRuns.usageJson}->>'accountingProviderWorkStarted' = 'false'
|
||||
or (${closedNativeReceiptSource}) or (${closedLegacyReceiptSource}) or (${closureEvidenceTimedOut(at)}) or (${nativeProviderWorkNeverStarted}))`,
|
||||
or (${externallyBilledNativeAccountingClosure}) or (${closedNativeReceiptSource}) or (${closedLegacyReceiptSource}) or (${closureEvidenceTimedOut(at)}) or (${nativeProviderWorkNeverStarted}))`,
|
||||
)),
|
||||
)),
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user