Skip to content

Commit 949d208

Browse files
authored
fix(state): harden maintenance findings
Fix sparse state query scanning, align LangGraph checkpoint inheritance, and move recurring maintenance notes into durable core memory.
1 parent 47530c0 commit 949d208

14 files changed

Lines changed: 498 additions & 504 deletions

File tree

AGENTS.md

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
# AgentState
2+
3+
Conversation history database-as-a-service for AI agents.
4+
5+
## Project Structure
6+
7+
```
8+
packages/
9+
api/ Hono API on Cloudflare Workers + D1
10+
shared/ Shared TypeScript types (public API contract)
11+
dashboard/ Next.js + Clerk + shadcn/ui (served as static assets by API Worker)
12+
docs/ Integration guides
13+
scripts/ Setup and deployment scripts
14+
```
15+
16+
See `docs/knowledge/core-memory.md` for durable maintenance notes. Do not create dated AI review reports; fold recurring findings into core memory and keep `docs/INDEX.md` updated.
17+
18+
**Architecture**: Single Cloudflare Worker serves both the REST API (`/api/v1/*`) and dashboard static assets.
19+
20+
## Dev Commands
21+
22+
```bash
23+
# Install
24+
bun install
25+
26+
# Local dev — API (port 8787)
27+
cd packages/api
28+
bunx wrangler dev
29+
30+
# Local dev — Dashboard (port 3000, separate dev server)
31+
cd packages/dashboard
32+
bun run dev
33+
34+
# Database
35+
bunx drizzle-kit generate # Generate migrations
36+
bunx wrangler d1 migrations apply agentstate-db --local # Apply locally
37+
bunx wrangler d1 execute agentstate-db --local --file=scripts/seed.sql # Seed
38+
39+
# Lint + Format
40+
bunx biome check packages/api/src/ # Check
41+
bunx biome check --write packages/api/src/ # Auto-fix
42+
43+
# Type check
44+
bunx tsc --noEmit -p packages/api/tsconfig.json
45+
46+
# Test
47+
cd packages/api && bunx vitest run
48+
49+
# Deploy
50+
bunx wrangler deploy -c packages/api/wrangler.jsonc
51+
```
52+
53+
## Architecture
54+
55+
- **Package manager**: Bun
56+
- **Linter/Formatter**: Biome
57+
- **API Framework**: Hono (ultrafast, Workers-native)
58+
- **ORM**: Drizzle ORM (type-safe, D1/SQLite)
59+
- **Database**: Cloudflare D1 (SQLite at edge)
60+
- **Auth (API)**: Bearer token with SHA-256 hashed API keys
61+
- **Auth (Dashboard)**: Clerk (client-side keyless mode for dev; prod keys in `.env.production`)
62+
- **AI**: Workers AI for title generation and follow-up questions
63+
- **Deployment**: Single Cloudflare Worker serves both API and dashboard static assets
64+
65+
## Conventions
66+
67+
- **IDs**: nanoid (21 chars), auto-generated via Drizzle `$defaultFn`
68+
- **Timestamps**: Unix milliseconds (Date.now()), stored as INTEGER in SQLite
69+
- **API responses**: snake_case field names
70+
- **API keys**: Format `as_live_` + 40 base62 chars. Only SHA-256 hash stored.
71+
- **Metadata**: JSON serialized as TEXT column, parsed on read
72+
- **Pagination**: Cursor-based (never offset-based)
73+
- **Error format**: `{ error: { code: "MACHINE_CODE", message: "Human message" } }`
74+
75+
## Key Files
76+
77+
- `packages/api/src/db/schema.ts` — Single source of truth for DB schema
78+
- `packages/api/src/middleware/auth.ts` — API key auth (critical security path)
79+
- `packages/api/src/routes/conversations/` — Core CRUD operations
80+
- `packages/api/src/routes/projects.ts` — Project and key management routes
81+
- `packages/api/drizzle/` — Generated SQL migrations (committed to git)
82+
83+
## Testing
84+
85+
```bash
86+
# Run all tests
87+
cd packages/api && bunx vitest run
88+
89+
# Local test API key (from seed.sql):
90+
as_live_TEST_KEY_FOR_LOCAL_DEV_ONLY_1234567890ab
91+
92+
# Health check
93+
curl http://localhost:8787/
94+
95+
# Create conversation
96+
curl -X POST http://localhost:8787/v1/conversations \
97+
-H "Authorization: Bearer as_live_TEST_KEY_FOR_LOCAL_DEV_ONLY_1234567890ab" \
98+
-H "Content-Type: application/json" \
99+
-d '{"messages":[{"role":"user","content":"Hello"}]}'
100+
```
101+
102+
## Git Conventions
103+
104+
- **Semantic commits**: `feat:`, `fix:`, `chore:`, `docs:`, `refactor:`, `test:`
105+
- **Co-authors** (always include both):
106+
107+
```
108+
Co-Authored-By: Duyet Le <me@duyet.net>
109+
Co-Authored-By: duyetbot <bot@duyet.net>
110+
```
111+
112+
## Autonomous Maintenance
113+
114+
See **[PLAN.md](PLAN.md)** for the full maintenance playbook, quality benchmarks, scenario tables, and backlog. Keep PLAN.md up to date after every iteration.
115+
116+
### Continuous Improvement Loop (every 15 min via cron)
117+
118+
When the cron fires, run this full cycle:
119+
120+
#### Phase 0 — Benchmark
121+
Run all quality checks in parallel. If any regress, fix before proceeding.
122+
```bash
123+
bunx biome check packages/api/src/
124+
bunx tsc --noEmit -p packages/api/tsconfig.json
125+
cd packages/api && bunx vitest run
126+
cd packages/dashboard && bunx tsc --noEmit
127+
git status --short
128+
```
129+
130+
#### Phase 1 — Plan & Brainstorm
131+
1. **Read current state**: Check PLAN.md backlog, memory files, recent git log
132+
2. **Brainstorm**: What has the biggest impact right now?
133+
- Next unchecked backlog item ready to build
134+
- Quality gaps (test coverage, type safety, accessibility)
135+
- Architectural improvements that unlock future work
136+
- Dashboard UX gaps users would expect
137+
- Developer experience — docs accuracy, SDK completeness
138+
3. **Plan**: For non-trivial items, use Agent (Plan) to design approach. Write plan to memory if it spans multiple iterations.
139+
4. **Prioritize**: Pick 2-4 concrete tasks for parallel execution without file conflicts. Prefer high-impact items.
140+
141+
#### Phase 2 — Execute (batch parallel agents)
142+
Spawn 2-4 parallel agents in a single message (`run_in_background: true`):
143+
- Use PLAN.md scenario tables as menu
144+
- Mix categories: feature + quality + dashboard + docs
145+
- Agents touch **different files** — no conflicts
146+
- senior-engineer for features/refactors, junior-engineer for lint/format/dead-code, code-reviewer for audits
147+
148+
#### Phase 3 — Collect & Verify
149+
1. Review agent outputs — reject anything that breaks existing behavior
150+
2. Full quality suite: lint → typecheck → test
151+
3. If anything fails, fix before continuing
152+
153+
#### Phase 4 — PR, Monitor, Merge & Deploy
154+
1. Create feature branch: `git checkout -b claude/improvement-<timestamp>`
155+
2. Commit each logical change separately with semantic messages + co-authors
156+
3. Push branch and create PR: `gh pr create --title "..." --body "..."`
157+
4. Monitor CI: poll `gh pr checks <pr-number>` until all checks pass or fail
158+
5. If CI passes → merge: `gh pr merge <pr-number> --squash --delete-branch`
159+
6. If CI fails → fix issues, push again, re-check
160+
7. After merge → deploy: `bunx wrangler deploy -c packages/api/wrangler.jsonc`
161+
8. Verify: `curl -s -o /dev/null -w '%{http_code}' https://agentstate.app/api`
162+
9. Update memory benchmark scores + PLAN.md
163+
164+
#### Phase 5 — Reflect
165+
- What was accomplished?
166+
- What should the NEXT iteration focus on?
167+
- Any blockers or decisions needing user input?
168+
- Save insights to memory for continuity
169+
170+
#### Rules
171+
- Always use PR workflow — never push directly to main
172+
- One commit per logical change
173+
- Skip iteration if working tree is dirty from user work
174+
- Save progress to memory after each run
175+
- Don't repeat work from previous iterations — check memory first
176+
- If a brainstormed idea is too large, break into phases and save plan to memory
177+
178+
## Deployment
179+
180+
```bash
181+
# 1. Fill in credentials
182+
cp .env.example .env.local
183+
# Edit .env.local with your Cloudflare credentials
184+
185+
# 2. Set GitHub secrets
186+
./scripts/setup-secrets.sh
187+
188+
# 3. Create D1 database
189+
bunx wrangler d1 create agentstate-db
190+
# Update database_id in packages/api/wrangler.jsonc
191+
192+
# 4. Push to main → auto-deploys via GitHub Actions
193+
git push origin main
194+
```

CLAUDE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ docs/ Integration guides
1313
scripts/ Setup and deployment scripts
1414
```
1515

16+
See `docs/knowledge/core-memory.md` for durable maintenance notes. Do not create dated AI review reports; fold recurring findings into core memory and keep `docs/INDEX.md` updated.
17+
1618
**Architecture**: Single Cloudflare Worker serves both the REST API (`/api/v1/*`) and dashboard static assets.
1719

1820
## Dev Commands

PLAN.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Pick scenarios from the tables below. Spawn agents that touch **different files*
111111
3. Deploy: `cd packages/dashboard && bun run build && cd ../api && bunx wrangler deploy`
112112
4. Commit + push with semantic message + co-authors
113113
5. Verify: `curl -s https://agentstate.app/api`
114-
6. Update memory: `~/.claude/projects/-Users-duet-project-agentdb/memory/`
114+
6. Update memory: `docs/knowledge/core-memory.md`
115115
7. Update PLAN.md backlog status
116116

117117
## Rules
@@ -121,6 +121,7 @@ Pick scenarios from the tables below. Spawn agents that touch **different files*
121121
- Build dashboard before deploying
122122
- Commit + push after every meaningful change
123123
- Save progress to memory after each iteration
124+
- Use `docs/knowledge/core-memory.md` for durable maintenance notes; do not add dated AI review reports.
124125
- One commit per logical change, not one giant commit
125126
- Co-authors on every commit:
126127
```

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,15 @@ See [CLAUDE.md](CLAUDE.md) for full dev commands and conventions.
140140

141141
| Guide | Description |
142142
|-------|-------------|
143+
| [Documentation Index](docs/INDEX.md) | Complete docs index |
143144
| [Getting Started](docs/getting-started.md) | Zero to working in 2 minutes |
144145
| [API Reference](docs/api-reference.md) | Complete REST API documentation |
145146
| [V2 Migration Guide](docs/v2-migration.md) | Migrate from V1 to V2 API |
146147
| [TypeScript SDK](docs/sdk.md) | SDK installation, methods, and examples |
147148
| [Python SDK](https://pypi.org/project/agentstate/) | Python package for AgentState |
148149
| [Framework Integration](docs/integration.md) | Vercel AI SDK, Cloudflare Workers AI, LangGraph |
149150
| [Environment Variables](docs/environment-variables.md) | All env vars and Cloudflare bindings |
151+
| [Core Memory](docs/knowledge/core-memory.md) | Durable maintenance notes for future agents |
150152

151153
See [CLAUDE.md](CLAUDE.md) for development guide and conventions.
152154

bun.lock

Lines changed: 27 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)