Reference
Code Examples
Common workflows and code examples for Jetpack.
Basic Workflow
Simple Task Execution
# Start Jetpack
jetpack start --agents 2
# Create a simple task
jetpack task \
--title "Add logging to API endpoints" \
--priority medium \
--skills typescript,backend
# Monitor progress
jetpack statusFeature Development with Dependencies
import { createLibSQLLocalDataLayer } from '@jetpack-agent/data';
import { SwarmCoordinator } from '@jetpack-agent/coordinator';
async function developFeature() {
const dataLayer = await createLibSQLLocalDataLayer('.jetpack/jetpack.db');
const swarm = new SwarmCoordinator(dataLayer, {
workDir: process.cwd(),
claimStrategy: 'best-fit',
});
await swarm.start();
// Step 1: Design & Planning
const designTask = await dataLayer.tasks.create({
title: 'Design user profile feature',
priority: 'high',
requiredSkills: ['documentation'],
dependencies: [],
});
// Step 2: Database Schema (depends on design)
const schemaTask = await dataLayer.tasks.create({
title: 'Create user profile database schema',
priority: 'high',
requiredSkills: ['database'],
dependencies: [designTask.id],
});
// Step 3: Backend API (depends on schema)
const apiTask = await dataLayer.tasks.create({
title: 'Implement user profile API endpoints',
priority: 'high',
requiredSkills: ['backend', 'typescript'],
dependencies: [schemaTask.id],
});
// Step 4: Frontend Component (depends on design)
const uiTask = await dataLayer.tasks.create({
title: 'Build user profile UI component',
priority: 'medium',
requiredSkills: ['frontend', 'react'],
dependencies: [designTask.id],
});
// Step 5: Integration (depends on API and UI)
const integrationTask = await dataLayer.tasks.create({
title: 'Integrate profile UI with API',
priority: 'medium',
requiredSkills: ['frontend', 'backend'],
dependencies: [apiTask.id, uiTask.id],
});
// Step 6: Testing (depends on integration)
const testTask = await dataLayer.tasks.create({
title: 'Write tests for user profile feature',
priority: 'high',
requiredSkills: ['testing'],
dependencies: [integrationTask.id],
});
console.log('Feature development pipeline created!');
console.log('6 tasks with proper dependencies');
// Monitor until complete
while (true) {
const stats = await swarm.getStats();
if (stats.tasks.completed === 6) {
console.log('Feature development complete!');
break;
}
console.log(`Progress: ${stats.tasks.completed}/6 tasks completed`);
await new Promise(resolve => setTimeout(resolve, 5000));
}
await swarm.stop();
}Advisory File Reservations
import { createLibSQLLocalDataLayer } from '@jetpack-agent/data';
async function reservationExample() {
const dataLayer = await createLibSQLLocalDataLayer('.jetpack/jetpack.db');
// Agent 1 reserves a file (always succeeds)
const grant1 = await dataLayer.reservations.reserve({
agentId: 'agent-1',
taskId: 'task-1',
filePath: 'src/auth.ts',
reservationType: 'edit',
});
console.log('Agent 1 reserved:', grant1.granted); // true
console.log('Conflicts:', grant1.conflicts); // []
// Agent 2 reserves the SAME file (also succeeds — advisory!)
const grant2 = await dataLayer.reservations.reserve({
agentId: 'agent-2',
taskId: 'task-2',
filePath: 'src/auth.ts',
reservationType: 'edit',
});
console.log('Agent 2 reserved:', grant2.granted); // true
console.log('Conflicts:', grant2.conflicts); // [{ agentId: 'agent-1', ... }]
// Agent 2 sees the conflict and can coordinate
if (grant2.conflicts.length > 0) {
console.log('Another agent is working on this file!');
}
// Release when done
await dataLayer.reservations.releaseAll('agent-1');
await dataLayer.reservations.releaseAll('agent-2');
}Using the Memory System
import { createLibSQLLocalDataLayer } from '@jetpack-agent/data';
async function memoryExample() {
const dataLayer = await createLibSQLLocalDataLayer('.jetpack/jetpack.db');
// Store knowledge about the codebase
await dataLayer.memories.store({
agentId: 'agent-1',
taskId: 'task-1',
content: 'The authentication system uses JWT tokens with 24-hour expiry',
memoryType: 'codebase_knowledge',
importance: 0.9,
tags: ['auth', 'jwt'],
});
await dataLayer.memories.store({
agentId: 'agent-1',
taskId: 'task-1',
content: 'All API endpoints follow RESTful conventions with /api/v1 prefix',
memoryType: 'solution_pattern',
importance: 0.8,
tags: ['api-design', 'rest'],
});
// Retrieve memories for a specific task context
const taskMemories = await dataLayer.memories.getTaskContext('task-1');
console.log('Task memories:', taskMemories);
}