/** * Generated by Veycera from an investigation of "Approved refund is issued correctly". * Evidence level at generation: diagnosis supported. * Historical attribution is unverified; sandbox reproduction does not prove the original execution. * Contract snapshot: ctr_refund_executed_yc-recording-v1, version 1. * Mechanism (sandbox-observed): When the customer has several orders the agent acts on a different one than the ticket quotes; with a single order it acts on the right one. * * Target: YOUR agent, reached over HTTP at VEYCERA_AGENT_URL. The test seeds * the failing conditions in a disposable Veycera sandbox, sends the synthetic * ticket to that endpoint and asserts the outcome contract against the * sandbox's business records. It fails until the defect is fixed, and it * refuses to run against anything but an explicitly configured target. * Runs inside the Veycera platform package (it uses its sandbox harness); * it does not import or inspect your agent's code. * Required: VEYCERA_REGRESSION_SQLITE_PATH is an absolute path to a dedicated, * disposable SQLite harness shared with the intended agent. Configure that * agent's VEYCERA_SQLITE_PATH to the same file. A private in-memory database * cannot be read by an HTTP agent. Never point this harness at production. * SIM_API_TOKEN is forwarded only to the explicitly configured agent target. */ import { describe, expect, it } from "vitest"; import { isAbsolute } from "node:path"; import { createCausalRuntime } from "@/lib/causal/runtime"; import { SandboxLab } from "@/lib/causal/rca/sandbox"; import { experimentFactorsSchema, type ExperimentFactors } from "@/lib/causal/rca/types"; import { outcomeContractSchema } from "@/lib/causal/contracts/schema"; const CONTRACT = outcomeContractSchema.parse({"id":"ctr_refund_executed_yc-recording-v1","tenantId":"yc-recording-v1","name":"Approved refund is issued correctly","description":"When the support agent approves a refund request and resolves the ticket, the payment for the order named in the ticket must carry a succeeded refund for exactly the requested amount, in the requested currency, within two minutes. Refunds on other payments of the same customer do not count.","version":1,"trigger":{"object":"ticket","categories":["refund"],"status":"resolved","requiresAgentApproval":true},"identifiers":{"order":{"source":"ticket.orderRef","required":true}},"conditions":[{"path":"refunds.succeededMinor","op":"eq","ref":"ticket.requestedAmountMinor","id":"refund_amount_correct","description":"Succeeded refunds on the named payment equal the requested amount","applicableWhen":[{"path":"charge.exists","op":"eq","value":true},{"path":"ticket.requestedAmountMinor","op":"neq","value":null}]},{"path":"refunds.succeededMinor","op":"eq","ref":"charge.amountMinor","id":"refund_full_when_unspecified","description":"Without a requested amount, the payment is refunded in full","applicableWhen":[{"path":"charge.exists","op":"eq","value":true},{"path":"ticket.requestedAmountMinor","op":"eq","value":null}]},{"path":"charge.currency","op":"eq","ref":"ticket.requestedCurrency","id":"refund_currency_matches","description":"The payment's currency matches the requested currency","applicableWhen":[{"path":"charge.exists","op":"eq","value":true},{"path":"ticket.requestedCurrency","op":"neq","value":null}]},{"path":"refunds.count","op":"lte","value":1,"id":"refund_single","description":"At most one refund was issued against the payment","applicableWhen":[{"path":"charge.exists","op":"eq","value":true}]},{"path":"siblingRefunds.sinceTicketMinor","op":"eq","value":0,"id":"no_refund_on_other_payments","description":"No other payment of the customer was refunded for this request","applicableWhen":[]}],"instance":{"eventTypes":["ticket.resolved"],"initialRetrySeconds":30,"maxRetrySeconds":600,"maxAgeSeconds":86400},"deadlineSeconds":120,"severity":"high","approved":true,"approvedAt":"2026-10-30T14:32:32.938Z","enabled":true,"draftedBy":"template","provenance":{"sources":[{"kind":"customer_request","ref":"onboarding instruction","excerpt":"Approved refunds must land on the named payment for exactly the requested amount."},{"kind":"policy_document","ref":"Approved refund workflow","excerpt":"issues one refund for the requested amount (minor units, requested currency) against that charge"},{"kind":"api_schema","ref":"Support API (requestedAmountMinor, requestedCurrency), Payments API"}],"assumptions":["A request without a structured amount means a full refund of the payment.","Split refunds are not allowed: at most one refund per payment per request.","Tickets resolved without agent approval (declined or escalated requests) are not monitored."]},"createdAt":"2026-10-30T14:32:32.938Z","updatedAt":"2026-10-30T14:32:32.938Z"}); const FAILING_CONDITIONS = ["refund_amount_correct","no_refund_on_other_payments"]; const AGENT_URL = process.env.VEYCERA_AGENT_URL; const HARNESS_PATH = process.env.VEYCERA_REGRESSION_SQLITE_PATH; const EXPECTED_AGENT_VERSION = null; async function runWorkflow(factors: ExperimentFactors) { if (!AGENT_URL) throw new Error("VEYCERA_AGENT_URL is not set. This regression test must target your agent's endpoint; it will not fall back to Veycera's example agent."); if (!HARNESS_PATH || HARNESS_PATH === ":memory:" || !isAbsolute(HARNESS_PATH)) throw new Error("VEYCERA_REGRESSION_SQLITE_PATH must be an absolute path to a dedicated disposable SQLite file shared with the intended agent. Configure the agent's VEYCERA_SQLITE_PATH to the same file; private in-memory and production configuration fallbacks are refused."); if (EXPECTED_AGENT_VERSION && process.env.VEYCERA_AGENT_VERSION !== EXPECTED_AGENT_VERSION) throw new Error("VEYCERA_AGENT_VERSION must identify the intended tested target version."); const f = experimentFactorsSchema.parse(factors); const rt = await createCausalRuntime({ sqlitePath: HARNESS_PATH, agentUrl: AGENT_URL, access: { mode: "deterministic", reason: "regression test" }, env: { SIM_API_TOKEN: process.env.SIM_API_TOKEN, VEYCERA_AGENT_VERSION: process.env.VEYCERA_AGENT_VERSION } as NodeJS.ProcessEnv }); const lab = new SandboxLab(rt.sql, rt.envs, rt.tenantId, rt.agentFor); try { const contract = CONTRACT; // immutable evaluated policy, independent of today's stored contract const env = await lab.create("regression", f); const fixture = await lab.seed(env.id, f); await lab.act(env.id, f, fixture); const { evaluation } = await lab.read(env.id, contract, fixture); return evaluation; } finally { try { await lab.disposeAll(); } finally { await rt.close(); } } } describe("regression: Approved refund is issued correctly", () => { it("Approved refund is issued correctly: wrong object selected with customer agent, refund of 4500 minor units, charge 4500 usd; prior refunds 0, 2 order(s) (quoted order is the oldest), order id quoted", async () => { const evaluation = await runWorkflow({"workflow":"refund","requestedAmountMinor":4500,"chargeAmountMinor":4500,"currency":"usd","priorRefunds":[],"minutesBeforeDispatch":null,"ordersForCustomer":2,"quotedOrderPosition":"oldest","ticketQuotesOrderId":true,"actor":"customer_agent","fulfillmentAccess":"granted","providerAvailability":"up"}); for (const id of FAILING_CONDITIONS) expect(evaluation.conditions.find((c) => c.id === id)?.status, id).toBe("satisfied"); expect(evaluation.status).toBe("verified"); }); it("passing control: customer agent, refund of 4500 minor units, charge 4500 usd; prior refunds 0, 1 order(s), order id quoted (the one-factor intervention that removed the failure)", async () => { const evaluation = await runWorkflow({"workflow":"refund","requestedAmountMinor":4500,"chargeAmountMinor":4500,"currency":"usd","priorRefunds":[],"minutesBeforeDispatch":null,"ordersForCustomer":1,"quotedOrderPosition":"oldest","ticketQuotesOrderId":true,"actor":"customer_agent","fulfillmentAccess":"granted","providerAvailability":"up"}); expect(evaluation.status).toBe("verified"); }); });