Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

feat: implement sub-agent spawning and lifecycle event tracking for background task monitoring

Salman Qureshi23373eb35dc7
diff --git a/src/core/agent.ts b/src/core/agent.ts
index 094aa0ee..fe56aa40 100644
--- a/src/core/agent.ts
+++ b/src/core/agent.ts
@@ -308,6 +308,23 @@ export class Agent {
await channel.send(message, channelId).catch(() => {});
}
});
+ supervisor.setLifecycleCallback((event) => {
+ const bgTask = this.backgroundTasks.getByAgentId(event.agentId);
+ if (!bgTask) return;
+
+ if (event.type === 'progress' && event.progress) {
+ this.backgroundTasks.updateAgentProgress(bgTask.id, event.progress);
+ this.syncBgTasksToTui();
+ return;
+ }
+
+ if (event.type === 'complete' && event.result) {
+ const result = event.result;
+ const status = result.status === 'completed' ? 'completed' : result.status === 'halted' ? 'cancelled' : 'failed';
+ this.backgroundTasks.completeAgentTask(bgTask.id, status === 'completed' ? 0 : 1, status, result.output);
+ this.syncBgTasksToTui();
+ }
+ });
}
private enqueueMessage(msg: ChannelMessage): void {
@@ -2039,6 +2056,29 @@ Always specify owner and repo parameters on GitHub tools. The user's GitHub user
return true;
}
+ if (rawArgs.startsWith('agent ') || rawArgs.startsWith('delegate ')) {
+ if (!this.supervisor) {
+ await channel.send('Sub-agents are not enabled in this environment.', channelId);
+ return true;
+ }
+ const taskDescription = rawArgs.replace(/^(agent|delegate)\s+/, '').trim();
+ if (!taskDescription) {
+ await channel.send('Usage: `/code agent <task>`', channelId);
+ return true;
+ }
+ const cwd = this.capabilities.getCwd();
+ const agentId = await this.supervisor.spawn({
+ task: taskDescription,
+ sourceChannelId: channelId,
+ sourceChannelType: channelType as any,
+ workingDirectory: cwd,
+ });
+ const bgId = this.backgroundTasks.spawnAgent(taskDescription, cwd, agentId);
+ this.syncBgTasksToTui();
+ await channel.send(`Started coding sub-agent ${agentId} in background task ${bgId}. Use /bg ${bgId} for progress.`, channelId);
+ return true;
+ }
+
if (rawArgs === 'off' || rawArgs === 'exit') {
this.programmingMode.setOff();
if (cliChannel) cliChannel.setProgrammingStatus(this.programmingMode.getState(), this.programmingMode.getProjectContext());
@@ -2054,7 +2094,7 @@ Always specify owner and repo parameters on GitHub tools. The user's GitHub user
return true;
}
- await channel.send('Unknown /code command. Available: /code, /code plan, /code execute, /code build, /code workspace, /code off, /code toggle', channelId);
+ await channel.send('Unknown /code command. Available: /code, /code plan, /code execute, /code build, /code workspace, /code agent <task>, /code off, /code toggle', channelId);
return true;
}
diff --git a/src/core/supervisor.ts b/src/core/supervisor.ts
index 4e7aad72..a587c117 100644
--- a/src/core/supervisor.ts
+++ b/src/core/supervisor.ts
@@ -14,6 +14,7 @@ import { ResourceManager } from './resource-manager.js';
import { logger } from '../utils/logger.js';
export type NotifyCallback = (channelType: string, channelId: string, message: string) => Promise<void>;
+export type AgentLifecycleCallback = (event: { type: 'progress' | 'complete'; agentId: string; progress?: string; result?: SubAgentResult }) => void;
export class SubAgentSupervisor {
private activeAgents: Map<string, SubAgent> = new Map();
@@ -34,6 +35,7 @@ export class SubAgentSupervisor {
private channels: ChannelRegistry;
private notifyCallback?: NotifyCallback;
+ private lifecycleCallback?: AgentLifecycleCallback;
private pausedAgents: Set<string> = new Set();
private pauseResolvers: Map<string, () => void> = new Map();
@@ -72,6 +74,10 @@ export class SubAgentSupervisor {
this.notifyCallback = cb;
}
+ setLifecycleCallback(cb: AgentLifecycleCallback): void {
+ this.lifecycleCallback = cb;
+ }
+
private async notify(channelType: string, channelId: string, message: string): Promise<void> {
if (this.notifyCallback) {
await this.notifyCallback(channelType, channelId, message);
@@ -153,6 +159,7 @@ export class SubAgentSupervisor {
subAgent.setProgressCallback((agentId, progress) => {
this.taskBoard.update(agentId, { progress });
+ this.lifecycleCallback?.({ type: 'progress', agentId, progress });
const entry = this.taskBoard.get(agentId);
if (entry) {
@@ -187,6 +194,7 @@ export class SubAgentSupervisor {
this.pausedAgents.delete(agentId);
logger.info({ agentId, status: result.status, duration: result.duration }, 'Sub-agent completed');
+ this.lifecycleCallback?.({ type: 'complete', agentId, result });
const entry = this.taskBoard.get(agentId);
if (entry) {
@@ -349,4 +357,4 @@ export class SubAgentSupervisor {
}
return count;
}
-}
\ No newline at end of file
+}