Skip to content
Merged
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
134 changes: 134 additions & 0 deletions packages/core/src/tasks/server-parity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { prepareDeliveryManifest } from "./delivery-manifest.js";
import { parseFeeBp, taskMoney } from "./money.js";
import { missingTasksChain } from "./chain-port.js";
import { postTask, searchTasks, submitTask } from "./operations.js";
import { proofRequirementSchema } from "./v2.js";
import {
createOrderInputSchema,
deliverEscrowInputSchema,
Expand Down Expand Up @@ -244,6 +245,139 @@ describe("merged server response contracts", () => {
},
);

it("parses a pool order whose proposals carry pool claims, as the server returns them", async () => {
// Server view schema (vapi-app order-view.ts workProposalPayloadViewSchema): kind
// proposal|submission|claim, a claim carries preset proof fills and an optional inviteCode.
const claim = (value: string, extra: Record<string, unknown> = {}) => ({
...proposal,
signedPayload: {
...proposal.signedPayload,
kind: "claim",
proof: [{ preset: "text", value }],
...extra,
},
});
const parsed = tasksResponseSchemas.getOrder.parse({
workOrder: {
...privateOrder,
state: "open",
proposals: [claim("dada"), claim("invited-user", { inviteCode: "ABCD2345" })],
},
});
expect(parsed.workOrder).toMatchObject({
proposals: [
{ signedPayload: { kind: "claim", proof: [{ preset: "text", value: "dada" }] } },
{ signedPayload: { kind: "claim", inviteCode: "ABCD2345" } },
],
});
// The transport path `vapi task claim|show|cancel` uses parses the same order.
const fetch = vi.fn(async () => Response.json(parsed));
const client = createTasksClient({ baseUrl: "https://tasks.example", fetch });
expect(await client.getOrder(ORDER)).toEqual(parsed);
const order = (proposals: unknown[]) =>
tasksResponseSchemas.getOrder.safeParse({ workOrder: { ...privateOrder, proposals } });
// Signed proof bytes come back unchanged, never normalized.
const signedLink = "https://example.org/a\tb";
expect(
order([
{
...proposal,
signedPayload: {
...proposal.signedPayload,
kind: "claim",
proof: [{ preset: "link", value: signedLink }],
},
},
]),
).toMatchObject({
success: true,
data: { workOrder: { proposals: [{ signedPayload: { proof: [{ value: signedLink }] } }] } },
});
// Values are still validated: a bogus file hash or pr link fails, as on the server.
expect(
order([
{
...proposal,
signedPayload: {
...proposal.signedPayload,
kind: "submission",
proof: [{ kind: "file", value: "bogus" }],
},
},
]).success,
).toBe(false);
expect(
order([
{
...proposal,
signedPayload: {
...proposal.signedPayload,
kind: "claim",
proof: [{ preset: "pr", value: "bogus" }],
},
},
]).success,
).toBe(false);
// Server rules: a malformed invite code, empty proof, and proof on a plain proposal fail.
expect(order([claim("x", { inviteCode: "ABCD-1234" })]).success).toBe(false);
expect(
order([
{ ...proposal, signedPayload: { ...proposal.signedPayload, kind: "claim", proof: [] } },
]).success,
).toBe(false);
expect(
order([
{
...proposal,
signedPayload: {
...proposal.signedPayload,
proof: [{ kind: "url", value: "https://example.org/work" }],
},
},
]).success,
).toBe(false);
// A claim without proof stays invalid, as on the server.
expect(
tasksResponseSchemas.getOrder.safeParse({
workOrder: {
...privateOrder,
proposals: [{ ...proposal, signedPayload: { ...proposal.signedPayload, kind: "claim" } }],
},
}).success,
).toBe(false);
});

it("parses public pool cards and repository-bound pull request requirements", () => {
expect(
tasksResponseSchemas.publicTask.parse({
...card,
shape: "pool",
briefFull: card.brief,
children: [],
})?.shape,
).toBe("pool");
expect(
proofRequirementSchema.parse({
preset: "pr",
repository: "vAPI-Network/vapi-network",
merged: true,
challenge: true,
}),
).toEqual({
preset: "pr",
repository: "vAPI-Network/vapi-network",
merged: true,
challenge: true,
});
expect(
proofRequirementSchema.safeParse({ preset: "pr", repository: "vAPI-Network/vapi-network" })
.success,
).toBe(false);
expect(
proofRequirementSchema.safeParse({ preset: "pr", repository: "not a repo" }).success,
).toBe(false);
});

it("allows 600-second terms in requests and nested proposal and scope responses", () => {
expect(milestoneTermsSchema.parse(terms).workDurationSeconds).toBe(600);
expect(milestoneTermsSchema.safeParse({ ...terms, workDurationSeconds: 599 }).success).toBe(
Expand Down
79 changes: 77 additions & 2 deletions packages/core/src/tasks/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { z } from "zod";
import { taskTermsFields, taskViewFields } from "./v2.js";
import { taskV2ProofSchema } from "./v2-write.js";

import {
milestoneTermsSchema,
Expand Down Expand Up @@ -341,12 +342,86 @@ export const feedQuerySchema = z.object({
limit: z.number().int().positive().optional(),
});

// Mirrors the server's order-view payload (vapi-app workProposalPayloadViewSchema): a
// pool order lists its pool claims as proposals with kind "claim" and preset proof fills.
// Response proof is validated with the request validators but returned unchanged, so the
// parsed payload keeps the exact signed bytes.
const proofLabelViewSchema = z.string().optional();
const legacyProofViewSchema = z
.array(
z.discriminatedUnion("kind", [
z.strictObject({ kind: z.literal("url"), value: z.string(), label: proofLabelViewSchema }),
z.strictObject({ kind: z.literal("file"), value: z.string(), label: proofLabelViewSchema }),
]),
)
.min(1)
.max(20)
.refine((proof) => z.array(submissionProofSchema).safeParse(proof).success, "Invalid proof");
const presetProofViewSchema = z
.array(
z.strictObject({
preset: z.enum(["link", "file", "x-post", "onchain-tx", "pr", "text", "media"]),
value: z.string(),
label: proofLabelViewSchema,
}),
)
.min(1)
.max(20)
.refine((proof) => taskV2ProofSchema.safeParse(proof).success, "Invalid proof");
const proposalPayloadViewSchema = z
.looseObject({
version: z.literal("work-proposal-v1"),
workOrderId: uuidSchema,
providerAddress: addressSchema,
pricingModel: z.literal("fixed"),
milestones: z.array(milestoneTermsBoundarySchema).length(1),
kind: z.enum(["proposal", "submission", "claim"]).optional(),
inviteCode: z
.string()
.regex(/^[A-Z2-7]{8}$/u)
.optional(),
proof: z.union([legacyProofViewSchema, presetProofViewSchema]).optional(),
})
.superRefine((payload, context) => {
if (
payload.kind !== "claim" &&
payload.proof !== undefined &&
!legacyProofViewSchema.safeParse(payload.proof).success
) {
context.addIssue({
code: "custom",
path: ["proof"],
message: "Preset proof fills require a pool claim",
});
}
if (
(payload.kind === "submission" || payload.kind === "claim") &&
payload.proof === undefined
) {
context.addIssue({
code: "custom",
path: ["proof"],
message: "Submission proof is required",
});
}
if (payload.inviteCode !== undefined && payload.kind !== "claim") {
context.addIssue({
code: "custom",
path: ["inviteCode"],
message: "Invite codes are only accepted with a pool claim",
});
}
if (payload.kind !== "submission" && payload.kind !== "claim" && payload.proof !== undefined) {
context.addIssue({ code: "custom", path: ["proof"], message: "Proposal proof is forbidden" });
}
});

const proposalResponseSchema = z.looseObject({
id: uuidSchema,
workOrderId: uuidSchema,
providerAddress: addressSchema,
state: z.enum(["pending", "accepted", "rejected", "withdrawn"]),
signedPayload: proposalSigningPayloadSchema.loose(),
signedPayload: proposalPayloadViewSchema,
signature: signatureSchema,
signatureHash: bytes32Schema,
proposedMilestones: z.array(milestoneTermsBoundarySchema).length(1),
Expand Down Expand Up @@ -846,7 +921,7 @@ export const publicTaskCardSchema = z.looseObject({
id: z.string(),
title: z.string(),
brief: z.string(),
shape: z.enum(["task", "open-bounty"]),
shape: z.enum(["task", "open-bounty", "pool"]),
amount: publicAmountSchema.nullable(),
deadlineAt: z.string().nullable(),
durationSeconds: z.number().nullable(),
Expand Down
20 changes: 19 additions & 1 deletion packages/core/src/tasks/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,25 @@ export const proofRequirementSchema = z.discriminatedUnion("preset", [
...label,
chain: z.enum(["base", "base-sepolia", "solana"]).optional(),
}),
z.strictObject({ preset: z.literal("pr"), ...label, ...contains }),
z
.strictObject({
preset: z.literal("pr"),
...label,
...contains,
repository: z
.string()
.regex(/^[A-Za-z0-9_-]+\/[A-Za-z0-9_.-]+$/u)
.optional(),
merged: z.literal(true).optional(),
challenge: z.literal(true).optional(),
})
.refine(
// Server rule: a repository binding needs all three fields, or none.
(pr) =>
[pr.repository, pr.merged, pr.challenge].every((v) => v === undefined) ||
[pr.repository, pr.merged, pr.challenge].every((v) => v !== undefined),
"repository, merged and challenge go together",
),
z.strictObject({
preset: z.literal("text"),
...label,
Expand Down
Loading