chore: initial commit with Phase 0 setup

This commit is contained in:
OG T
2026-06-06 22:55:45 +08:00
commit 9e79e58f87
56 changed files with 16088 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { logAuditEvent } from "@/lib/audit";
import { TaskStatus } from "@agent-bounty/contracts";
// Optional: restrict to cron secret
// export const maxDuration = 60; // Next.js edge/serverless config
export async function GET(req: Request) {
// Find all claims that have expired, but the task is still EXECUTING
const now = new Date();
const expiredClaims = await prisma.claim.findMany({
where: {
status: TaskStatus.EXECUTING,
expires_at: { lt: now },
},
include: {
task: true,
}
});
const rolledBackIds: string[] = [];
for (const claim of expiredClaims) {
if (claim.task.status === TaskStatus.EXECUTING) {
await prisma.$transaction(async (tx) => {
// Rollback Task
await tx.task.update({
where: { id: claim.task_id },
data: {
status: TaskStatus.OPEN,
error_classification: "claim_timeout",
}
});
// Rollback Claim
await tx.claim.update({
where: { id: claim.id },
data: { status: "CANCELLED" }
});
await logAuditEvent(tx, {
actorType: "SYSTEM",
action: "CLAIM_TIMEOUT_REAPER",
entityType: "TASK",
entityId: claim.task_id,
beforeState: { status: TaskStatus.EXECUTING },
afterState: { status: TaskStatus.OPEN },
reason: `Claim ${claim.id} expired at ${claim.expires_at.toISOString()}`,
});
});
rolledBackIds.push(claim.task_id);
}
}
return NextResponse.json({
swept: rolledBackIds.length,
task_ids: rolledBackIds,
});
}

View File

@@ -0,0 +1,245 @@
import { NextRequest, NextResponse } from "next/server";
import {
ListOpenTasksRequestSchema,
ClaimTaskRequestSchema,
SubmitSolutionRequestSchema,
TaskStatus,
JudgeOverallResult
} from "@agent-bounty/contracts";
import { prisma } from "@/lib/prisma";
import { runSubmissionInSandbox } from "@/lib/sandbox";
import { logAuditEvent } from "@/lib/audit";
import { redis } from "@/lib/redis";
import crypto from "crypto";
export async function POST(request: NextRequest, props: { params: Promise<{ tool: string }> }) {
const params = await props.params;
const tool = params.tool;
const authHeader = request.headers.get("Authorization");
if (!authHeader || !authHeader.startsWith("Bearer ")) {
return NextResponse.json({ error: "Unauthorized: Missing Bearer token" }, { status: 401 });
}
const token = authHeader.split(" ")[1];
if (process.env.API_KEY && token !== process.env.API_KEY) {
return NextResponse.json({ error: "Forbidden: Invalid API Key" }, { status: 403 });
}
try {
const body = await request.json();
switch (tool) {
case "list_open_tasks": {
ListOpenTasksRequestSchema.parse(body);
const tasks = await prisma.task.findMany({
where: { status: TaskStatus.OPEN },
});
const formattedTasks = tasks.map((t) => ({
task_id: t.id,
title: t.title,
status: t.status,
difficulty: t.difficulty,
reward: {
amount: t.reward_amount,
currency: t.reward_currency,
display_amount: `$${(t.reward_amount / 100).toFixed(2)}`
},
required_stack: t.required_stack,
scope_clarity_score: t.scope_clarity_score,
created_at: t.created_at.toISOString(),
description_preview: t.description.substring(0, 100) + (t.description.length > 100 ? "..." : ""),
}));
return NextResponse.json({
tasks: formattedTasks,
total_open: formattedTasks.length,
stockout_warning: formattedTasks.length === 0,
});
}
case "claim_task": {
const parsed = ClaimTaskRequestSchema.parse(body);
const claim = await prisma.$transaction(async (tx) => {
const updated = await tx.task.updateMany({
where: { id: parsed.task_id, status: TaskStatus.OPEN },
data: { status: TaskStatus.EXECUTING }
});
if (updated.count === 0) {
throw new Error("Task is not OPEN or does not exist");
}
const task = await tx.task.findUniqueOrThrow({ where: { id: parsed.task_id } });
const newClaim = await tx.claim.create({
data: {
task_id: task.id,
developer_wallet: parsed.developer_wallet,
status: TaskStatus.EXECUTING,
claim_token: crypto.randomUUID(),
held_amount: task.reward_amount,
held_currency: task.reward_currency,
expires_at: new Date(Date.now() + 3600000)
}
});
await logAuditEvent(tx, {
actorType: "AGENT",
actorId: parsed.developer_wallet,
action: "CLAIM_TASK",
entityType: "TASK",
entityId: task.id,
beforeState: { status: TaskStatus.OPEN },
afterState: { status: TaskStatus.EXECUTING }
});
return newClaim;
});
// Set Redis TTL key (3600 seconds)
await redis.set(`vw:task:${claim.task_id}:executing`, claim.claim_token, "EX", 3600);
return NextResponse.json({
task_id: claim.task_id,
status: claim.status,
held_amount: claim.held_amount,
held_currency: claim.held_currency,
expires_at: claim.expires_at.toISOString(),
claim_token: claim.claim_token,
});
}
case "submit_solution": {
const parsed = SubmitSolutionRequestSchema.parse(body);
const submission = await prisma.$transaction(async (tx) => {
const claim = await tx.claim.findUnique({ where: { claim_token: parsed.claim_token } });
if (!claim || claim.task_id !== parsed.task_id || claim.status !== TaskStatus.EXECUTING) {
throw new Error("Invalid claim token or claim is not EXECUTING");
}
const updatedTask = await tx.task.updateMany({
where: { id: parsed.task_id, status: TaskStatus.EXECUTING },
data: { status: TaskStatus.VERIFYING }
});
if (updatedTask.count === 0) {
throw new Error("Task is not EXECUTING");
}
await tx.claim.update({
where: { id: claim.id },
data: { status: TaskStatus.VERIFYING }
});
const newSubmission = await tx.submission.create({
data: {
task_id: parsed.task_id,
claim_id: claim.id,
status: TaskStatus.VERIFYING,
deliverables: parsed.deliverables as any,
estimated_judge_complete_at: new Date(Date.now() + 300000)
}
});
await logAuditEvent(tx, {
actorType: "AGENT",
action: "SUBMIT_SOLUTION",
entityType: "TASK",
entityId: parsed.task_id,
beforeState: { status: TaskStatus.EXECUTING },
afterState: { status: TaskStatus.VERIFYING }
});
return newSubmission;
});
// Async trigger E2B Sandbox evaluation
const taskObj = await prisma.task.findUnique({ where: { id: submission.task_id }});
if (taskObj && typeof taskObj.acceptance_criteria === "object" && taskObj.acceptance_criteria !== null) {
const criteria = taskObj.acceptance_criteria as any;
if (criteria.test_file_content) {
// Fire and forget
runSubmissionInSandbox(
submission.id,
parsed.deliverables as Record<string, string>,
criteria.test_file_content
).then(async (result) => {
// Update submission
await prisma.submission.update({
where: { id: submission.id },
data: { status: "JUDGED" }
});
// Create JudgeResult
await prisma.judgeResult.create({
data: {
submission_id: submission.id,
overall_result: result.overall_result,
tests: result.tests,
artifacts: result.artifacts,
error_classification: result.error_classification,
resource_usage: result.resource_usage
}
});
// Update Task & Claim Status
const newTaskStatus = result.overall_result === JudgeOverallResult.PASS
? TaskStatus.COMPLETED
: TaskStatus.FAILED_RETRYABLE;
await prisma.task.update({
where: { id: submission.task_id },
data: { status: newTaskStatus }
});
await prisma.claim.update({
where: { id: submission.claim_id },
data: { status: newTaskStatus }
});
// @ts-ignore prisma transaction client vs prisma client
await logAuditEvent(prisma, {
actorType: "SYSTEM",
action: "JUDGE_COMPLETE",
entityType: "TASK",
entityId: submission.task_id,
beforeState: { status: TaskStatus.VERIFYING },
afterState: { status: newTaskStatus },
metadata: { overall_result: result.overall_result, error_classification: result.error_classification }
});
}).catch(console.error);
}
}
return NextResponse.json({
task_id: submission.task_id,
submission_id: submission.id,
status: submission.status,
estimated_judge_complete_at: submission.estimated_judge_complete_at?.toISOString() ?? new Date().toISOString(),
});
}
case "check_payout_status": {
// Mocked for now until Settlement phase is implemented
return NextResponse.json({
task_id: body.task_id,
phase: "PAYOUT_READY",
amount: 100,
currency: "USD",
updated_at: new Date().toISOString(),
});
}
default:
return NextResponse.json({ error: `Unknown tool: ${tool}` }, { status: 404 });
}
} catch (error: any) {
console.error(`[API Gateway] Error handling ${tool}:`, error);
return NextResponse.json({ error: error.message || String(error) }, { status: 400 });
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

View File

@@ -0,0 +1,26 @@
@import "tailwindcss";
:root {
--background: #ffffff;
--foreground: #171717;
}
@theme inline {
--color-background: var(--background);
--color-foreground: var(--foreground);
--font-sans: var(--font-geist-sans);
--font-mono: var(--font-geist-mono);
}
@media (prefers-color-scheme: dark) {
:root {
--background: #0a0a0a;
--foreground: #ededed;
}
}
body {
background: var(--background);
color: var(--foreground);
font-family: Arial, Helvetica, sans-serif;
}

View File

@@ -0,0 +1,33 @@
import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
import "./globals.css";
const geistSans = Geist({
variable: "--font-geist-sans",
subsets: ["latin"],
});
const geistMono = Geist_Mono({
variable: "--font-geist-mono",
subsets: ["latin"],
});
export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
};
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html
lang="en"
className={`${geistSans.variable} ${geistMono.variable} h-full antialiased`}
>
<body className="min-h-full flex flex-col">{children}</body>
</html>
);
}

64
apps/web/src/app/page.tsx Normal file
View File

@@ -0,0 +1,64 @@
import { prisma } from "@/lib/prisma";
import Link from "next/link";
export default async function Home() {
const tasks = await prisma.task.findMany({
orderBy: { created_at: "desc" }
});
return (
<div className="min-h-screen bg-gray-950 text-gray-100 p-8 font-sans">
<div className="max-w-5xl mx-auto">
<div className="flex justify-between items-center mb-10">
<h1 className="text-4xl font-extrabold bg-clip-text text-transparent bg-gradient-to-r from-blue-400 to-purple-500">
Agent Bounty Protocol
</h1>
<Link href="/tasks/create" className="bg-blue-600 hover:bg-blue-500 text-white font-medium py-2 px-6 rounded-full transition-all duration-300 shadow-lg shadow-blue-500/30">
+ Post Bounty
</Link>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
{tasks.length === 0 ? (
<div className="col-span-full text-center py-20 text-gray-500">
No tasks available. Be the first to post a bounty!
</div>
) : (
tasks.map((task) => (
<Link href={`/tasks/${task.id}`} key={task.id} className="block group">
<div className="bg-gray-900 border border-gray-800 rounded-2xl p-6 h-full transition-all duration-300 hover:border-blue-500 hover:shadow-[0_0_20px_rgba(59,130,246,0.15)] relative overflow-hidden">
<div className="absolute top-0 left-0 w-full h-1 bg-gradient-to-r from-blue-500 to-purple-500 opacity-0 group-hover:opacity-100 transition-opacity"></div>
<div className="flex justify-between items-start mb-4">
<span className={`px-3 py-1 text-xs font-semibold rounded-full ${
task.status === "OPEN" ? "bg-green-500/10 text-green-400 border border-green-500/20" :
task.status === "EXECUTING" ? "bg-yellow-500/10 text-yellow-400 border border-yellow-500/20" :
task.status === "COMPLETED" ? "bg-blue-500/10 text-blue-400 border border-blue-500/20" :
"bg-gray-800 text-gray-400 border border-gray-700"
}`}>
{task.status}
</span>
<span className="text-lg font-bold text-gray-200">
${(task.reward_amount / 100).toFixed(2)}
</span>
</div>
<h2 className="text-xl font-bold text-white mb-2 line-clamp-1">{task.title}</h2>
<p className="text-gray-400 text-sm mb-6 line-clamp-2">{task.description}</p>
<div className="flex flex-wrap gap-2 mt-auto">
{task.required_stack.map((tech) => (
<span key={tech} className="text-xs bg-gray-800 text-gray-300 px-2 py-1 rounded">
{tech}
</span>
))}
</div>
</div>
</Link>
))
)}
</div>
</div>
</div>
);
}

View File

@@ -0,0 +1,71 @@
import { prisma } from "@/lib/prisma";
import { notFound } from "next/navigation";
import Link from "next/link";
export default async function TaskDetails({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
const task = await prisma.task.findUnique({
where: { id },
include: { submissions: true }
});
if (!task) return notFound();
return (
<div className="min-h-screen bg-gray-950 text-gray-100 p-8 font-sans">
<div className="max-w-4xl mx-auto">
<Link href="/" className="text-blue-400 hover:text-blue-300 mb-8 inline-flex items-center gap-2">
Back to Tasks
</Link>
<div className="bg-gray-900 border border-gray-800 rounded-3xl p-10 relative overflow-hidden shadow-2xl">
<div className="absolute top-0 right-0 w-64 h-64 bg-blue-500/10 rounded-full blur-3xl -translate-y-1/2 translate-x-1/2"></div>
<div className="flex justify-between items-start mb-6">
<h1 className="text-4xl font-extrabold text-white">{task.title}</h1>
<div className="text-right">
<div className="text-3xl font-black text-transparent bg-clip-text bg-gradient-to-r from-green-400 to-emerald-600">
${(task.reward_amount / 100).toFixed(2)}
</div>
<div className="text-sm text-gray-500">{task.reward_currency}</div>
</div>
</div>
<div className="flex gap-4 mb-8">
<span className="px-4 py-2 bg-gray-800 rounded-lg text-sm font-semibold border border-gray-700">
Status: <span className="text-blue-400">{task.status}</span>
</span>
<span className="px-4 py-2 bg-gray-800 rounded-lg text-sm font-semibold border border-gray-700">
Difficulty: <span className="text-purple-400">{task.difficulty}</span>
</span>
</div>
<div className="prose prose-invert max-w-none mb-10">
<h3 className="text-xl font-bold mb-4 text-gray-200 border-b border-gray-800 pb-2">Description</h3>
<p className="text-gray-400 leading-relaxed whitespace-pre-wrap">{task.description}</p>
</div>
<div className="mb-10">
<h3 className="text-xl font-bold mb-4 text-gray-200 border-b border-gray-800 pb-2">Required Stack</h3>
<div className="flex flex-wrap gap-2">
{task.required_stack.map((tech) => (
<span key={tech} className="bg-blue-900/30 text-blue-300 px-3 py-1.5 rounded-md text-sm font-medium border border-blue-800/50">
{tech}
</span>
))}
</div>
</div>
<div className="bg-gray-950 rounded-xl p-6 border border-gray-800">
<h3 className="text-lg font-bold mb-4 text-gray-300">Acceptance Criteria (Test File)</h3>
<pre className="text-xs text-gray-400 overflow-x-auto p-4 bg-black rounded-lg border border-gray-800">
{typeof task.acceptance_criteria === "object" && task.acceptance_criteria !== null
? (task.acceptance_criteria as any).test_file_content || "No test file specified."
: "N/A"}
</pre>
</div>
</div>
</div>
</div>
);
}

View File

@@ -0,0 +1,32 @@
"use server";
import { prisma } from "@/lib/prisma";
import { TaskStatus, TaskDifficulty } from "@agent-bounty/contracts";
import { redirect } from "next/navigation";
export async function createTask(formData: FormData) {
const title = formData.get("title") as string;
const description = formData.get("description") as string;
const rewardAmount = parseInt(formData.get("rewardAmount") as string, 10) * 100; // to cents
const requiredStack = (formData.get("requiredStack") as string).split(",").map(s => s.trim());
const testFileContent = formData.get("testFileContent") as string;
const task = await prisma.task.create({
data: {
title,
description,
status: TaskStatus.OPEN,
difficulty: TaskDifficulty.COMPONENT,
scope_clarity_score: 1.0,
reward_amount: rewardAmount,
reward_currency: "USD",
required_stack: requiredStack,
acceptance_criteria: {
validation_mode: "VITEST_UNIT",
test_file_content: testFileContent
}
}
});
redirect(`/tasks/${task.id}`);
}

View File

@@ -0,0 +1,96 @@
"use client";
import { useTransition } from "react";
import { createTask } from "./actions";
import Link from "next/link";
export default function CreateTaskPage() {
const [isPending, startTransition] = useTransition();
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
startTransition(() => {
createTask(formData);
});
};
return (
<div className="min-h-screen bg-gray-950 text-gray-100 p-8 font-sans">
<div className="max-w-2xl mx-auto">
<Link href="/" className="text-blue-400 hover:text-blue-300 mb-8 inline-flex items-center gap-2">
Back to Tasks
</Link>
<h1 className="text-3xl font-extrabold text-white mb-8">Post a New Bounty</h1>
<form onSubmit={handleSubmit} className="bg-gray-900 border border-gray-800 rounded-3xl p-8 shadow-2xl space-y-6">
<div>
<label className="block text-sm font-semibold text-gray-300 mb-2">Title</label>
<input
name="title"
required
className="w-full bg-black border border-gray-700 rounded-lg px-4 py-3 text-white focus:outline-none focus:border-blue-500 focus:ring-1 focus:ring-blue-500"
placeholder="e.g. Build a React Button Component"
/>
</div>
<div>
<label className="block text-sm font-semibold text-gray-300 mb-2">Reward Amount (USD)</label>
<input
name="rewardAmount"
type="number"
min="1"
required
className="w-full bg-black border border-gray-700 rounded-lg px-4 py-3 text-white focus:outline-none focus:border-blue-500 focus:ring-1 focus:ring-blue-500"
placeholder="10"
/>
</div>
<div>
<label className="block text-sm font-semibold text-gray-300 mb-2">Required Stack (comma separated)</label>
<input
name="requiredStack"
required
className="w-full bg-black border border-gray-700 rounded-lg px-4 py-3 text-white focus:outline-none focus:border-blue-500 focus:ring-1 focus:ring-blue-500"
placeholder="React, Tailwind, TypeScript"
/>
</div>
<div>
<label className="block text-sm font-semibold text-gray-300 mb-2">Description</label>
<textarea
name="description"
required
rows={4}
className="w-full bg-black border border-gray-700 rounded-lg px-4 py-3 text-white focus:outline-none focus:border-blue-500 focus:ring-1 focus:ring-blue-500"
placeholder="Describe the task details here..."
/>
</div>
<div>
<label className="block text-sm font-semibold text-gray-300 mb-2">Acceptance Criteria (Vitest Code)</label>
<textarea
name="testFileContent"
required
rows={6}
className="w-full bg-black border border-gray-700 rounded-lg px-4 py-3 text-white font-mono text-sm focus:outline-none focus:border-blue-500 focus:ring-1 focus:ring-blue-500"
placeholder="import { expect, test } from 'vitest';&#10;&#10;test('component renders', () => { ... });"
/>
<p className="text-xs text-gray-500 mt-2">Provide the exact test file content that the agent's solution must pass.</p>
</div>
<button
type="submit"
disabled={isPending}
className="w-full bg-blue-600 hover:bg-blue-500 text-white font-bold py-4 rounded-xl transition-all duration-300 shadow-lg shadow-blue-500/30 disabled:opacity-50 disabled:cursor-not-allowed mt-4"
>
{isPending ? "Posting Bounty..." : "Post Bounty to Network"}
</button>
</form>
</div>
</div>
);
}

31
apps/web/src/lib/audit.ts Normal file
View File

@@ -0,0 +1,31 @@
import { prisma } from "./prisma";
import { Prisma } from "@prisma/client";
export async function logAuditEvent(
tx: Prisma.TransactionClient,
params: {
actorType: "SYSTEM" | "AGENT" | "USER";
actorId?: string;
action: string;
entityType: "TASK" | "CLAIM" | "SUBMISSION" | "JUDGE_RESULT";
entityId: string;
beforeState?: any;
afterState?: any;
reason?: string;
metadata?: any;
}
) {
return tx.auditEvent.create({
data: {
actorType: params.actorType,
actorId: params.actorId,
action: params.action,
entityType: params.entityType,
entityId: params.entityId,
beforeState: params.beforeState ?? Prisma.JsonNull,
afterState: params.afterState ?? Prisma.JsonNull,
reason: params.reason,
metadata: params.metadata ?? Prisma.JsonNull,
},
});
}

View File

@@ -0,0 +1,7 @@
import { PrismaClient } from "@prisma/client";
const globalForPrisma = global as unknown as { prisma: PrismaClient };
export const prisma = globalForPrisma.prisma || new PrismaClient();
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;

View File

@@ -0,0 +1,9 @@
import Redis from "ioredis";
const globalForRedis = global as unknown as { redis: Redis };
export const redis =
globalForRedis.redis ||
new Redis(process.env.REDIS_URL || "redis://localhost:6379");
if (process.env.NODE_ENV !== "production") globalForRedis.redis = redis;

View File

@@ -0,0 +1,76 @@
import { Sandbox } from "@e2b/code-interpreter";
import { JudgeOverallResult, JudgeErrorClassification } from "@agent-bounty/contracts";
export async function runSubmissionInSandbox(
submissionId: string,
deliverables: Record<string, string>,
testFileContent: string
) {
let sandbox;
try {
sandbox = await Sandbox.create();
// Setup: Initialize a simple project
await sandbox.commands.run("npm init -y");
await sandbox.commands.run("npm install vitest react react-dom @types/react @types/react-dom");
// Write deliverables
for (const [filepath, content] of Object.entries(deliverables)) {
// In a real implementation we would ensure directories exist
// For MVP we assume flat files or e2b handles basic paths
await sandbox.files.write(filepath, content);
}
// Write test file
await sandbox.files.write("test.spec.tsx", testFileContent);
// Run tests
const result = await sandbox.commands.run("npx vitest run test.spec.tsx --reporter json", {
timeout: 120000 // 2 minutes
});
let overall = JudgeOverallResult.FAIL;
let errorClass = result.exitCode === 0 ? null : JudgeErrorClassification.TEST_FAIL;
let parsedTests = [];
if (result.exitCode === 0) {
overall = JudgeOverallResult.PASS;
}
try {
if (result.stdout) {
const jsonResult = JSON.parse(result.stdout);
parsedTests = jsonResult.testResults || [];
}
} catch (e) {
console.error("Failed to parse vitest JSON output", e);
}
return {
overall_result: overall,
error_classification: errorClass,
tests: parsedTests,
artifacts: {
logs: result.stdout + "\n" + result.stderr
},
resource_usage: {
cpu_ms: 0,
mem_peak_mb: 0,
io_bytes: 0
}
};
} catch (error: any) {
console.error("Sandbox evaluation failed", error);
return {
overall_result: JudgeOverallResult.FAIL,
error_classification: JudgeErrorClassification.ENVIRONMENT_ERROR,
tests: [],
artifacts: { logs: error.message },
resource_usage: { cpu_ms: 0, mem_peak_mb: 0, io_bytes: 0 }
};
} finally {
if (sandbox) {
await sandbox.close();
}
}
}