Skip to content

Commit 4fbd2a7

Browse files
CrazyBoyMclaude
andcommitted
feat(pool): add graceful shutdown support
## New Features ### AgentPool Graceful Shutdown - `gracefulShutdown()`: Safely stop all agents, wait for working ones to complete - `resumeFromShutdown()`: Resume agents from a previous graceful shutdown - `registerShutdownHandlers()`: Convenience method for SIGTERM/SIGINT handling ### New Types - `GracefulShutdownOptions`: Configuration for shutdown behavior - `ShutdownResult`: Detailed result of shutdown operation ## How It Works 1. Categorizes agents into READY and WORKING states 2. Immediately persists READY agents 3. Waits for WORKING agents with configurable timeout 4. Optionally interrupts agents that don't complete in time 5. Saves running agents list for recovery after restart ## Usage ```typescript const pool = new AgentPool({ dependencies }); // Option 1: Register signal handlers pool.registerShutdownHandlers(); // Option 2: Manual shutdown const result = await pool.gracefulShutdown({ timeout: 30000, saveRunningList: true, forceInterrupt: true, }); // Resume after restart await pool.resumeFromShutdown(configFactory, { strategy: 'crash' }); ``` Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 19e3a22 commit 4fbd2a7

13 files changed

Lines changed: 4277 additions & 420 deletions

README.md

Lines changed: 198 additions & 413 deletions
Large diffs are not rendered by default.

docs/ARCHITECTURE.md

Lines changed: 572 additions & 0 deletions
Large diffs are not rendered by default.

docs/DEPLOYMENT.md

Lines changed: 638 additions & 0 deletions
Large diffs are not rendered by default.

docs/ROADMAP.md

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
# KODE SDK Roadmap
2+
3+
> This document outlines the development roadmap for KODE SDK, based on actual current capabilities and planned enhancements.
4+
5+
---
6+
7+
## Current State (v2.7.0)
8+
9+
### What Works Well
10+
11+
| Feature | Status | Notes |
12+
|---------|--------|-------|
13+
| Agent State Machine | Stable | 7-stage breakpoint system |
14+
| JSONStore | Stable | WAL-protected file persistence |
15+
| Event System | Stable | 3 channels (Progress/Control/Monitor) |
16+
| Fork/Resume | Stable | Safe fork points, crash recovery |
17+
| Multi-provider | Stable | Anthropic, OpenAI, Gemini, DeepSeek, Qwen, GLM... |
18+
| Tool System | Stable | Built-in + MCP protocol |
19+
| AgentPool | Stable | Up to 50 agents per process |
20+
| Checkpointer | Stable | Memory, File, Redis implementations |
21+
| Context Compression | Stable | Automatic history management |
22+
| Hook System | Stable | Pre/post model and tool hooks |
23+
24+
### Current Limitations
25+
26+
| Limitation | Impact | Workaround |
27+
|------------|--------|------------|
28+
| JSONStore only | No database persistence | Implement custom Store |
29+
| Single-process pool | No distributed scaling | Build orchestration layer |
30+
| 5-min processing timeout | Not configurable | Fork SDK if needed |
31+
| No stateless mode | Serverless challenges | Request-scoped pattern |
32+
| No distributed locking | Multi-instance conflicts | External coordination |
33+
34+
---
35+
36+
## Short-term: v2.8 - v2.9 (Q1-Q2 2025)
37+
38+
### v2.8: Store Improvements
39+
40+
**Goal**: Make custom Store implementation easier and more robust.
41+
42+
| Feature | Priority | Description |
43+
|---------|----------|-------------|
44+
| Store interface documentation | P0 | Comprehensive guide for implementing custom stores |
45+
| Store validation utilities | P1 | Test helpers to verify Store implementations |
46+
| Incremental message API | P1 | `appendMessage()` in addition to `saveMessages()` |
47+
| Store migration utilities | P2 | Tools for migrating data between Store implementations |
48+
49+
**New APIs:**
50+
```typescript
51+
// Optional incremental methods (backwards compatible)
52+
interface Store {
53+
// Existing methods...
54+
55+
// NEW: Incremental append (optional, for performance)
56+
appendMessage?(agentId: string, message: Message): Promise<void>;
57+
58+
// NEW: Paginated loading (optional, for large histories)
59+
loadMessagesPaginated?(agentId: string, opts: {
60+
offset: number;
61+
limit: number;
62+
}): Promise<Message[]>;
63+
}
64+
```
65+
66+
### v2.9: Configurable Limits
67+
68+
**Goal**: Remove hard-coded limits, improve serverless compatibility.
69+
70+
| Feature | Priority | Description |
71+
|---------|----------|-------------|
72+
| Configurable processing timeout | P0 | Currently hard-coded to 5 minutes |
73+
| Configurable tool buffer size | P1 | Currently hard-coded to 10 MB |
74+
| Pool size validation | P2 | Better error messages when exceeding limits |
75+
76+
**New APIs:**
77+
```typescript
78+
// Agent configuration
79+
const agent = await Agent.create({
80+
agentId: 'my-agent',
81+
templateId: 'default',
82+
// NEW: Runtime limits
83+
limits: {
84+
processingTimeout: 30_000, // 30 seconds for serverless
85+
toolBufferSize: 5 * 1024 * 1024, // 5 MB
86+
},
87+
}, dependencies);
88+
```
89+
90+
---
91+
92+
## Mid-term: v3.0 (Q3 2025)
93+
94+
### v3.0: Official Store Implementations
95+
96+
**Goal**: Provide production-ready Store implementations for common databases.
97+
98+
| Store | Priority | Dependencies |
99+
|-------|----------|--------------|
100+
| `@kode-sdk/store-postgres` | P0 | `pg` |
101+
| `@kode-sdk/store-redis` | P0 | `ioredis` |
102+
| `@kode-sdk/store-supabase` | P1 | `@supabase/supabase-js` |
103+
| `@kode-sdk/store-dynamodb` | P2 | `@aws-sdk/client-dynamodb` |
104+
105+
**Package Structure:**
106+
```
107+
@kode-sdk/store-postgres
108+
├── src/
109+
│ ├── index.ts
110+
│ ├── postgres-store.ts
111+
│ └── schema.sql
112+
├── README.md
113+
└── package.json
114+
```
115+
116+
**Features:**
117+
- Complete Store interface implementation
118+
- Schema migration utilities
119+
- Connection pooling
120+
- Retry logic for transient failures
121+
- Distributed locking support
122+
123+
### v3.0: Stateless Execution Mode
124+
125+
**Goal**: Native support for serverless environments.
126+
127+
```typescript
128+
// NEW: Request-scoped execution
129+
import { StatelessAgent } from '@shareai-lab/kode-sdk';
130+
131+
export async function POST(req: Request) {
132+
const { agentId, message } = await req.json();
133+
134+
// Automatically handles: load → execute → persist
135+
const result = await StatelessAgent.run(agentId, message, {
136+
store: postgresStore,
137+
templateId: 'default',
138+
timeout: 25_000,
139+
});
140+
141+
return Response.json(result);
142+
}
143+
```
144+
145+
**Key Features:**
146+
- Automatic state loading and persisting
147+
- Timeout handling with graceful shutdown
148+
- No in-memory pool required
149+
- Optimized for cold starts
150+
151+
---
152+
153+
## Long-term: v4.0+ (2026)
154+
155+
### v4.0: Distributed Infrastructure (Optional Package)
156+
157+
**Goal**: Official distributed coordination package for high-scale deployments.
158+
159+
```
160+
@kode-sdk/distributed
161+
├── scheduler/ # Agent scheduling across workers
162+
├── discovery/ # Agent location discovery
163+
├── locking/ # Distributed locking
164+
└── migration/ # Agent migration between nodes
165+
```
166+
167+
**Features:**
168+
```typescript
169+
import { DistributedPool } from '@kode-sdk/distributed';
170+
171+
const pool = new DistributedPool({
172+
store: postgresStore,
173+
redis: redisClient,
174+
workerId: process.env.WORKER_ID,
175+
maxLocalAgents: 50,
176+
});
177+
178+
// Agent automatically migrates between workers
179+
const agent = await pool.acquire(agentId);
180+
await agent.send(message);
181+
await agent.complete();
182+
await pool.release(agentId);
183+
```
184+
185+
### v4.0: Observability Integration
186+
187+
**Goal**: First-class observability support.
188+
189+
```typescript
190+
import { OpenTelemetryPlugin } from '@kode-sdk/observability';
191+
192+
const agent = await Agent.create({
193+
agentId: 'my-agent',
194+
templateId: 'default',
195+
plugins: [
196+
new OpenTelemetryPlugin({
197+
serviceName: 'my-agent-service',
198+
tracing: true,
199+
metrics: true,
200+
}),
201+
],
202+
}, dependencies);
203+
```
204+
205+
**Metrics:**
206+
- `agent.step.duration` - Time per agent step
207+
- `agent.tool.duration` - Time per tool execution
208+
- `agent.model.tokens` - Token usage
209+
- `agent.errors` - Error count by type
210+
211+
### v4.x: Additional Sandboxes
212+
213+
| Sandbox | Status | Description |
214+
|---------|--------|-------------|
215+
| `DockerSandbox` | Planned | Run tools in Docker containers |
216+
| `K8sSandbox` | Planned | Run tools in Kubernetes pods |
217+
| `E2BSandbox` | Planned | Integration with E2B.dev |
218+
| `FirecrackerSandbox` | Exploring | MicroVM isolation |
219+
220+
---
221+
222+
## Community Contributions Welcome
223+
224+
### High-Impact Contributions
225+
226+
1. **Store Implementations**
227+
- MongoDB Store
228+
- SQLite Store (for embedded use)
229+
- Turso/LibSQL Store
230+
231+
2. **Sandbox Implementations**
232+
- Docker Sandbox
233+
- WebContainer Sandbox (browser)
234+
235+
3. **Tool Integrations**
236+
- Browser automation (Playwright)
237+
- Database clients
238+
- Cloud service SDKs
239+
240+
### Contribution Guidelines
241+
242+
See [CONTRIBUTING.md](./CONTRIBUTING.md) for:
243+
- Code style and testing requirements
244+
- Pull request process
245+
- Interface implementation guidelines
246+
247+
---
248+
249+
## Version Support Policy
250+
251+
| Version | Status | Support Until |
252+
|---------|--------|---------------|
253+
| v2.7.x | Current | Active development |
254+
| v2.6.x | Maintenance | 6 months after v2.8 |
255+
| v2.5.x | End of Life | No longer supported |
256+
257+
**Semver Policy:**
258+
- Major (v3, v4): Breaking changes to core APIs
259+
- Minor (v2.8, v2.9): New features, backwards compatible
260+
- Patch (v2.7.1): Bug fixes only
261+
262+
---
263+
264+
## Feedback & Prioritization
265+
266+
Roadmap priorities are influenced by:
267+
268+
1. **GitHub Issues**: Feature requests with most reactions
269+
2. **Community Discussions**: Patterns emerging from usage
270+
3. **Production Feedback**: Real-world deployment challenges
271+
272+
To influence the roadmap:
273+
- Open or upvote GitHub Issues
274+
- Share your use case in Discussions
275+
- Contribute implementations for planned features
276+
277+
---
278+
279+
## Timeline Summary
280+
281+
```
282+
2025 Q1-Q2: v2.8-v2.9
283+
├── Store interface improvements
284+
├── Configurable limits
285+
└── Better serverless support
286+
287+
2025 Q3: v3.0
288+
├── Official Store packages (Postgres, Redis, Supabase)
289+
├── Stateless execution mode
290+
└── Improved documentation
291+
292+
2026: v4.0+
293+
├── Distributed infrastructure package
294+
├── Observability integration
295+
└── Additional sandbox implementations
296+
```
297+
298+
The roadmap focuses on:
299+
1. **Making extension easier** (v2.8-2.9)
300+
2. **Providing official implementations** (v3.0)
301+
3. **Scaling infrastructure** (v4.0+)
302+
303+
Core philosophy remains: **KODE SDK is a runtime kernel, not a platform.** Official packages extend capabilities without bloating the core.

0 commit comments

Comments
 (0)