Skip to content

Agent Spawning

This guide demonstrates how to spawn a single Cursor Background Agent and monitor its progress until completion.

  • @jbcom/agentic installed globally
  • CURSOR_API_KEY environment variable set
  • GITHUB_TOKEN with repo access
Terminal window
agentic fleet spawn "https://github.com/my-org/my-repo" \
"Review the README and add a Quick Start section"
Terminal window
agentic fleet spawn "https://github.com/my-org/my-repo" \
"Fix the failing GitHub Actions workflow" \
--auto-pr
Terminal window
agentic fleet spawn "https://github.com/my-org/my-repo" \
"Update dependencies to latest versions" \
--ref feature-branch \
--branch agent/update-deps \
--auto-pr
import { Fleet } from '@jbcom/agentic';
async function spawnAgent() {
// Initialize Fleet manager
const fleet = new Fleet();
// Check if API is available
if (!fleet.isApiAvailable()) {
console.error('❌ Cursor API not available. Set CURSOR_API_KEY.');
process.exit(1);
}
// Spawn the agent
const result = await fleet.spawn({
repository: 'https://github.com/my-org/my-repo',
task: `
Review the codebase and create a summary document.
Steps:
1. Read the README.md and package.json
2. Identify the main entry points
3. List the key dependencies
4. Create a SUMMARY.md with your findings
`,
ref: 'main',
target: {
autoCreatePr: true,
branchName: 'agent/codebase-review',
},
});
if (!result.success || !result.data) {
console.error(`❌ Failed to spawn agent: ${result.error}`);
process.exit(1);
}
console.log(`✅ Agent spawned: ${result.data.id}`);
return result.data;
}
import { Fleet } from '@jbcom/agentic';
async function spawnAndMonitor() {
const fleet = new Fleet();
// Spawn agent
const spawnResult = await fleet.spawn({
repository: 'https://github.com/my-org/my-repo',
task: 'Fix the CI workflow',
target: { autoCreatePr: true },
});
if (!spawnResult.success || !spawnResult.data) {
throw new Error(spawnResult.error);
}
const agent = spawnResult.data;
console.log(`Agent ${agent.id} spawned, monitoring...`);
// Wait for completion with timeout
const finalResult = await fleet.waitFor(agent.id, {
timeout: 600000, // 10 minutes
pollInterval: 15000, // Check every 15 seconds
});
if (!finalResult.success || !finalResult.data) {
throw new Error(finalResult.error);
}
const finalAgent = finalResult.data;
console.log(`Agent completed with status: ${finalAgent.status}`);
if (finalAgent.target?.prUrl) {
console.log(`PR created: ${finalAgent.target.prUrl}`);
}
return finalAgent;
}

This is a complete example from the control repository:

import { Fleet } from '@jbcom/agentic';
async function main(): Promise<void> {
console.log('🚀 Agent Spawning Example\n');
const fleet = new Fleet();
if (!fleet.isApiAvailable()) {
console.error('❌ Cursor API not available. Set CURSOR_API_KEY.');
process.exit(1);
}
const spawnOptions = {
repository: 'https://github.com/your-org/your-repo',
task: `
Review the codebase and create a summary document.
Steps:
1. Read the README.md and package.json
2. Identify the main entry points
3. List the key dependencies
4. Create a SUMMARY.md with your findings
`,
ref: 'main',
target: {
autoCreatePr: true,
branchName: 'agent/codebase-review',
},
};
console.log('📋 Spawn Configuration:');
console.log(` Repository: ${spawnOptions.repository}`);
console.log(` Branch: ${spawnOptions.ref}`);
console.log(` Auto-create PR: ${spawnOptions.target.autoCreatePr}`);
// Spawn the agent
console.log('\n⏳ Spawning agent...');
const result = await fleet.spawn(spawnOptions);
if (!result.success || !result.data) {
console.error(`❌ Failed to spawn agent: ${result.error}`);
process.exit(1);
}
const agent = result.data;
console.log(`✅ Agent spawned successfully!`);
console.log(` Agent ID: ${agent.id}`);
console.log(` Status: ${agent.status}`);
// Monitor progress
console.log('\n📊 Monitoring agent progress...');
console.log(' (Press Ctrl+C to stop monitoring)\n');
const finalResult = await fleet.waitFor(agent.id, {
timeout: 600000,
pollInterval: 15000,
});
if (!finalResult.success || !finalResult.data) {
console.error(`❌ Error waiting for agent: ${finalResult.error}`);
process.exit(1);
}
const finalAgent = finalResult.data;
console.log('\n🏁 Agent completed!');
console.log(` Final Status: ${finalAgent.status}`);
if (finalAgent.target?.prUrl) {
console.log(` PR URL: ${finalAgent.target.prUrl}`);
}
// Archive the conversation
console.log('\n📁 Archiving conversation...');
const archiveResult = await fleet.archive(agent.id);
if (archiveResult.success) {
console.log(` Saved to: ${archiveResult.data}`);
}
console.log('\n✨ Done!');
}
main().catch((error) => {
console.error('Fatal error:', error);
process.exit(1);
});
OptionTypeDescription
repositorystringFull GitHub repository URL
taskstringThe task description for the agent
refstringBranch, tag, or commit to work from
target.autoCreatePrbooleanCreate PR on completion
target.branchNamestringCustom branch name for changes
target.openAsCursorGithubAppbooleanOpen PR as Cursor app

Be specific about what you want the agent to do:

// ❌ Too vague
task: 'Fix the bug'
// ✅ Clear and actionable
task: `
Fix the authentication bug in src/auth/login.ts.
The issue: Users with special characters in passwords cannot log in.
Steps:
1. Find the password validation function
2. Update the regex to allow special characters
3. Add unit tests for edge cases
4. Update the changelog
`

Set timeouts based on task complexity:

// Quick fixes: 5 minutes
await fleet.waitFor(id, { timeout: 300000 });
// Feature development: 30 minutes
await fleet.waitFor(id, { timeout: 1800000 });
// Complex refactoring: 1 hour
await fleet.waitFor(id, { timeout: 3600000 });
const result = await fleet.spawn(options);
if (!result.success) {
if (result.error?.includes('rate limit')) {
console.log('Rate limited, retrying in 60s...');
await sleep(60000);
return fleet.spawn(options);
}
throw new Error(result.error);
}