Skip to content

Commit 97b1934

Browse files
[developer] Auto-commit: Implement Core Feature
1 parent 6fbcca9 commit 97b1934

1 file changed

Lines changed: 8 additions & 125 deletions

File tree

src/integrations/ralph/swarm/swarm-coordinator.ts

Lines changed: 8 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ export class SwarmCoordinator {
4545
private gitWorkflowManager: GitWorkflowManager;
4646
private registeredSwarmId?: string;
4747

48+
get swarmId(): string | undefined {
49+
return this.registeredSwarmId;
50+
}
51+
52+
get agents(): Agent[] {
53+
return Array.from(this.activeAgents.values());
54+
}
55+
4856
constructor(config?: Partial<SwarmCoordinatorConfig>) {
4957
this.config = {
5058
maxAgents: 10,
@@ -1146,131 +1154,6 @@ You are a PROJECT COORDINATOR. Your role is to:
11461154
activeCount > 0 ? this.swarmState.completedTaskCount / activeCount : 0;
11471155
}
11481156

1149-
/**
1150-
* Stop the swarm and clean up all resources
1151-
*/
1152-
async stopSwarm(): Promise<void> {
1153-
logger.info('Stopping swarm coordinator', { swarmId: this.swarmState.id });
1154-
1155-
try {
1156-
// 1. Update swarm state
1157-
const previousStatus = this.swarmState.status;
1158-
this.swarmState.status = 'stopping';
1159-
1160-
// 2. Stop coordination timer
1161-
if (this.coordinationTimer) {
1162-
clearInterval(this.coordinationTimer);
1163-
this.coordinationTimer = undefined;
1164-
logger.debug('Stopped coordination timer');
1165-
}
1166-
1167-
// 3. Stop all active agents
1168-
const stopPromises: Promise<void>[] = [];
1169-
for (const [agentId, agent] of this.activeAgents) {
1170-
if (agent.status === 'active') {
1171-
logger.info(`Stopping agent ${agent.role} (${agentId})`);
1172-
agent.status = 'stopping';
1173-
1174-
// Commit any pending work
1175-
if (agent.currentTask && this.swarmState.tasks) {
1176-
const task = this.swarmState.tasks.find(
1177-
(t) => t.id === agent.currentTask
1178-
);
1179-
if (task) {
1180-
stopPromises.push(
1181-
this.gitWorkflowManager
1182-
.commitAgentWork(
1183-
agent,
1184-
task,
1185-
'[Agent] Final commit before shutdown'
1186-
)
1187-
.catch((err) =>
1188-
logger.error(
1189-
`Failed to commit agent ${agent.role} work:`,
1190-
err
1191-
)
1192-
)
1193-
);
1194-
}
1195-
}
1196-
}
1197-
}
1198-
1199-
// Wait for all agent commits
1200-
await Promise.allSettled(stopPromises);
1201-
1202-
// 4. Merge all agent work if possible
1203-
if (this.config.enableDynamicPlanning && previousStatus === 'active') {
1204-
try {
1205-
await this.gitWorkflowManager.coordinateMerges(
1206-
Array.from(this.activeAgents.values())
1207-
);
1208-
logger.info('Successfully merged all agent work before shutdown');
1209-
} catch (error) {
1210-
logger.warn('Could not merge all agent work during shutdown:', error);
1211-
}
1212-
}
1213-
1214-
// 5. Clear planner wakeup queue
1215-
this.plannerWakeupQueue.clear();
1216-
1217-
// 6. Save final swarm state
1218-
if (this.frameManager && this.swarmState.project) {
1219-
try {
1220-
await this.frameManager.pushFrame({
1221-
type: 'task' as any,
1222-
name: `swarm-shutdown-${this.swarmState.id}`,
1223-
inputs: {
1224-
swarmId: this.swarmState.id,
1225-
project: this.swarmState.project,
1226-
completedTasks: this.swarmState.completedTaskCount,
1227-
activeTasks: this.swarmState.activeTaskCount,
1228-
},
1229-
outputs: {
1230-
status: 'shutdown',
1231-
performance: this.swarmState.performance,
1232-
},
1233-
digest_json: {
1234-
type: 'swarm_shutdown',
1235-
agents: Array.from(this.activeAgents.values()).map((a) => ({
1236-
id: a.id,
1237-
role: a.role,
1238-
status: a.status,
1239-
performance: a.performance,
1240-
})),
1241-
},
1242-
} as any);
1243-
} catch (error) {
1244-
logger.error('Failed to save shutdown frame:', error);
1245-
}
1246-
}
1247-
1248-
// 7. Unregister from global registry
1249-
if (this.registeredSwarmId) {
1250-
const registry = SwarmRegistry.getInstance();
1251-
registry.unregisterSwarm(this.registeredSwarmId);
1252-
this.registeredSwarmId = undefined;
1253-
}
1254-
1255-
// 8. Clear all agents
1256-
this.activeAgents.clear();
1257-
1258-
// 9. Update final state
1259-
this.swarmState.status = 'stopped';
1260-
this.swarmState.endTime = Date.now();
1261-
1262-
logger.info('Swarm coordinator stopped successfully', {
1263-
swarmId: this.swarmState.id,
1264-
duration: this.swarmState.endTime - this.swarmState.startTime,
1265-
tasksCompleted: this.swarmState.completedTaskCount,
1266-
});
1267-
} catch (error) {
1268-
logger.error('Error during swarm shutdown:', error);
1269-
this.swarmState.status = 'error';
1270-
throw error;
1271-
}
1272-
}
1273-
12741157
[Symbol.toStringTag] = 'SwarmCoordinator';
12751158
}
12761159

0 commit comments

Comments
 (0)