Skip to content

Commit dd094a3

Browse files
committed
Project Scaffold
1 parent 2e0f575 commit dd094a3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+5490
-1
lines changed

.github/workflows/ci.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
lint-and-build:
11+
name: Lint & Build
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- uses: pnpm/action-setup@v4
17+
with:
18+
version: 9
19+
20+
- uses: actions/setup-node@v4
21+
with:
22+
node-version: 20
23+
cache: 'pnpm'
24+
25+
- name: Install dependencies
26+
run: pnpm install --frozen-lockfile
27+
28+
- name: Build shared package
29+
run: pnpm --filter @agent-safe/shared build
30+
31+
- name: Build backend
32+
run: pnpm --filter @agent-safe/backend build
33+
34+
- name: Build frontend
35+
run: pnpm --filter @agent-safe/web build
36+
37+
- name: Lint
38+
run: pnpm lint
39+
40+
contracts:
41+
name: Foundry Tests
42+
runs-on: ubuntu-latest
43+
defaults:
44+
run:
45+
working-directory: packages/contracts
46+
steps:
47+
- uses: actions/checkout@v4
48+
with:
49+
submodules: recursive
50+
51+
- name: Install Foundry
52+
uses: foundry-rs/foundry-toolchain@v1
53+
54+
- name: Install contract dependencies
55+
run: forge install
56+
57+
- name: Build contracts
58+
run: forge build
59+
60+
- name: Run tests
61+
run: forge test -vvv

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
node_modules/
2+
dist/
3+
.next/
4+
out/
5+
.turbo/
6+
.env
7+
.env.local
8+
*.log
9+
coverage/
10+
.DS_Store
11+
12+
# Foundry
13+
packages/contracts/cache/
14+
packages/contracts/out/
15+
packages/contracts/broadcast/

.prettierrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"semi": true,
3+
"singleQuote": true,
4+
"trailingComma": "all",
5+
"printWidth": 100,
6+
"tabWidth": 2
7+
}

Makefile

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
.PHONY: install dev build clean test lint format docker-up docker-down forge-build forge-test
2+
3+
# ─── Setup ────────────────────────────────────────────────
4+
install:
5+
pnpm install
6+
7+
# ─── Development ──────────────────────────────────────────
8+
dev:
9+
pnpm dev
10+
11+
build:
12+
pnpm build
13+
14+
clean:
15+
pnpm clean
16+
17+
lint:
18+
pnpm lint
19+
20+
format:
21+
pnpm format
22+
23+
# ─── Contracts (Foundry) ─────────────────────────────────
24+
forge-build:
25+
cd packages/contracts && forge build
26+
27+
forge-test:
28+
cd packages/contracts && forge test -vvv
29+
30+
forge-deploy-local:
31+
cd packages/contracts && forge script script/Deploy.s.sol --fork-url http://localhost:8545 --broadcast
32+
33+
forge-deploy-base-sepolia:
34+
cd packages/contracts && forge script script/Deploy.s.sol --rpc-url base_sepolia --broadcast --verify
35+
36+
# ─── Docker ───────────────────────────────────────────────
37+
docker-up:
38+
docker-compose up -d
39+
40+
docker-down:
41+
docker-compose down
42+
43+
docker-logs:
44+
docker-compose logs -f
45+
46+
# ─── Database ─────────────────────────────────────────────
47+
db-reset:
48+
docker-compose down -v && docker-compose up -d postgres
49+
sleep 2
50+
psql $(DATABASE_URL) -f db/schema.sql

README.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,60 @@
1-
# Agent-Safe
1+
# AgentSafe
2+
3+
> An ERC-4337 smart wallet on Base powered by **SwarmGuard** (multi-agent AI defense) and **GovernanceSafe** (proposal analysis + safe auto-voting with veto).
4+
5+
## Monorepo Structure
6+
7+
```
8+
apps/
9+
web/ → Next.js frontend dashboard
10+
backend/ → Node.js + Express agent orchestrator API
11+
packages/
12+
contracts/ → Foundry Solidity smart contracts
13+
shared/ → Shared TypeScript types, Zod schemas, constants
14+
```
15+
16+
## Prerequisites
17+
18+
- Node.js ≥ 20
19+
- pnpm ≥ 9
20+
- Foundry (for contracts)
21+
22+
## Quick Start
23+
24+
```bash
25+
# 1. Install dependencies
26+
pnpm install
27+
28+
# 2. Copy environment variables
29+
cp .env.example .env
30+
# Fill in your keys
31+
32+
# 3. Run everything in dev mode
33+
pnpm dev
34+
35+
# Frontend → http://localhost:3000
36+
# Backend → http://localhost:4000
37+
```
38+
39+
## Useful Commands
40+
41+
```bash
42+
pnpm build # Build all packages
43+
pnpm lint # Lint all packages
44+
pnpm format # Format all files with Prettier
45+
46+
# Contracts (from packages/contracts)
47+
forge build
48+
forge test
49+
```
50+
51+
## Architecture
52+
53+
- **AgentSafe Wallet** – ERC-4337 account abstraction wallet on Base
54+
- **SwarmGuard** – Multi-agent defense system (Sentinel, MEV Watcher, Liquidation Predictor, Scam Detector, Coordinator, Defender)
55+
- **GovernanceSafe** – Proposal parser, risk analysis, vote recommendation, execution with human veto
56+
- **Policy Engine** – On-chain deterministic guardrails that AI cannot override
57+
58+
## License
59+
60+
MIT

apps/backend/Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM node:20-alpine AS base
2+
RUN corepack enable && corepack prepare pnpm@9.15.0 --activate
3+
WORKDIR /app
4+
5+
# Copy workspace config
6+
COPY package.json pnpm-workspace.yaml pnpm-lock.yaml* turbo.json tsconfig.base.json ./
7+
8+
# Copy package sources
9+
COPY packages/shared ./packages/shared
10+
COPY apps/backend ./apps/backend
11+
12+
# Install dependencies
13+
RUN pnpm install --frozen-lockfile || pnpm install
14+
15+
# Build shared package first
16+
RUN pnpm --filter @agent-safe/shared build
17+
18+
# Build backend
19+
RUN pnpm --filter @agent-safe/backend build
20+
21+
EXPOSE 4000
22+
CMD ["node", "apps/backend/dist/index.js"]

apps/backend/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# @agent-safe/backend
2+
3+
Node.js + Express backend API for the AgentSafe swarm agent orchestrator.
4+
5+
## Endpoints
6+
7+
| Method | Path | Description |
8+
|--------|------|-------------|
9+
| GET | `/health` | Health check |
10+
| POST | `/api/swarm/evaluate-tx` | Evaluate a transaction through SwarmGuard |
11+
| GET | `/api/swarm/logs` | Fetch audit log of swarm decisions |
12+
| GET | `/api/governance/proposals` | Fetch governance proposals (Snapshot stub) |
13+
| POST | `/api/governance/recommend` | Analyse a proposal and return recommendation |
14+
15+
## Agents
16+
17+
| Agent | File | Purpose |
18+
|-------|------|---------|
19+
| Sentinel | `agents/sentinel.ts` | Approval & activity monitoring |
20+
| MEV Watcher | `agents/mevWatcher.ts` | Sandwich attack detection |
21+
| Liquidation Predictor | `agents/liquidationPredictor.ts` | Health factor tracking |
22+
| Scam Detector | `agents/scamDetector.ts` | Contract reputation checks |
23+
| Coordinator | `agents/coordinator.ts` | Consensus aggregation |
24+
| Defender | `agents/defender.ts` | Defensive action execution |
25+
26+
## Run
27+
28+
```bash
29+
pnpm dev # starts on http://localhost:4000
30+
```

apps/backend/package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "@agent-safe/backend",
3+
"version": "0.1.0",
4+
"private": true,
5+
"type": "module",
6+
"scripts": {
7+
"dev": "tsx watch src/index.ts",
8+
"build": "tsc",
9+
"start": "node dist/index.js",
10+
"clean": "rm -rf dist",
11+
"lint": "echo 'lint: ok'"
12+
},
13+
"dependencies": {
14+
"@agent-safe/shared": "workspace:*",
15+
"express": "^4.21.0",
16+
"cors": "^2.8.5",
17+
"dotenv": "^16.4.0",
18+
"zod": "^3.23.0"
19+
},
20+
"devDependencies": {
21+
"@types/express": "^5.0.0",
22+
"@types/cors": "^2.8.17",
23+
"@types/node": "^22.0.0",
24+
"tsx": "^4.19.0",
25+
"typescript": "^5.5.0"
26+
}
27+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { AgentRiskReport, SwarmConsensusDecision } from '@agent-safe/shared';
2+
import { CONSENSUS_THRESHOLD } from '@agent-safe/shared';
3+
4+
/**
5+
* Coordinator Agent – aggregates individual agent reports and produces consensus.
6+
* TODO: Implement weighted voting, configurable thresholds.
7+
*/
8+
export async function runCoordinatorAgent(
9+
reports: AgentRiskReport[],
10+
): Promise<SwarmConsensusDecision> {
11+
// TODO: Implement weighted voting based on agent confidence
12+
// TODO: Configurable consensus threshold
13+
// TODO: Handle conflicting agent outputs
14+
15+
const highRiskCount = reports.filter(
16+
(r) => r.risk_level === 'HIGH' || r.risk_level === 'CRITICAL',
17+
).length;
18+
19+
const riskScore = Math.round(
20+
(reports.reduce((sum, r) => {
21+
const levelScore = { LOW: 10, MEDIUM: 40, HIGH: 75, CRITICAL: 95 }[r.risk_level];
22+
return sum + levelScore * r.confidence;
23+
}, 0) /
24+
reports.length) *
25+
1,
26+
);
27+
28+
const consensusMet = highRiskCount >= CONSENSUS_THRESHOLD;
29+
30+
return {
31+
final_decision: consensusMet ? 'BLOCK' : 'ALLOW',
32+
risk_score: riskScore,
33+
consensus: `${highRiskCount}/${reports.length} agents`,
34+
summary: consensusMet
35+
? 'Multiple agents flagged high risk – transaction blocked.'
36+
: 'Risk level acceptable – transaction allowed.',
37+
actions: consensusMet ? ['BLOCK_TX'] : ['ALLOW'],
38+
agent_reports: reports,
39+
timestamp: new Date().toISOString(),
40+
};
41+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { SwarmConsensusDecision } from '@agent-safe/shared';
2+
3+
/**
4+
* Defender Agent – executes defensive actions when allowed.
5+
* TODO: Implement approval revocation, swap prevention, repayment triggers.
6+
*/
7+
export async function runDefenderAgent(
8+
decision: SwarmConsensusDecision,
9+
): Promise<{ executed: boolean; action: string }> {
10+
// TODO: Revoke approvals via UserOp
11+
// TODO: Prevent swap execution
12+
// TODO: Trigger repayment on lending positions (stretch)
13+
14+
if (decision.final_decision === 'EXECUTE_DEFENSE') {
15+
console.log('[DefenderAgent] Would execute defensive action:', decision.actions);
16+
return {
17+
executed: false, // Stub – no real execution
18+
action: 'STUB_DEFENSE_ACTION',
19+
};
20+
}
21+
22+
return {
23+
executed: false,
24+
action: 'NO_ACTION_NEEDED',
25+
};
26+
}

0 commit comments

Comments
 (0)