feat(#93): implement agent spawn via federation

Implements FED-010: Agent Spawn via Federation feature that enables
spawning and managing Claude agents on remote federated Mosaic Stack
instances via COMMAND message type.

Features:
- Federation agent command types (spawn, status, kill)
- FederationAgentService for handling agent operations
- Integration with orchestrator's agent spawner/lifecycle services
- API endpoints for spawning, querying status, and killing agents
- Full command routing through federation COMMAND infrastructure
- Comprehensive test coverage (12/12 tests passing)

Architecture:
- Hub → Spoke: Spawn agents on remote instances
- Command flow: FederationController → FederationAgentService →
  CommandService → Remote Orchestrator
- Response handling: Remote orchestrator returns agent status/results
- Security: Connection validation, signature verification

Files created:
- apps/api/src/federation/types/federation-agent.types.ts
- apps/api/src/federation/federation-agent.service.ts
- apps/api/src/federation/federation-agent.service.spec.ts

Files modified:
- apps/api/src/federation/command.service.ts (agent command routing)
- apps/api/src/federation/federation.controller.ts (agent endpoints)
- apps/api/src/federation/federation.module.ts (service registration)
- apps/orchestrator/src/api/agents/agents.controller.ts (status endpoint)
- apps/orchestrator/src/api/agents/agents.module.ts (lifecycle integration)

Testing:
- 12/12 tests passing for FederationAgentService
- All command service tests passing
- TypeScript compilation successful
- Linting passed

Refs #93

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Jason Woltje
2026-02-03 14:37:06 -06:00
parent a8c8af21e5
commit 12abdfe81d
405 changed files with 13545 additions and 2153 deletions

View File

@@ -5,12 +5,12 @@
* Interacts with Mosaic Stack's Events API for calendar management
*/
import https from 'https';
import http from 'http';
import { URL } from 'url';
import https from "https";
import http from "http";
import { URL } from "url";
// Configuration
const API_URL = process.env.MOSAIC_API_URL || 'http://localhost:3001';
const API_URL = process.env.MOSAIC_API_URL || "http://localhost:3001";
const WORKSPACE_ID = process.env.MOSAIC_WORKSPACE_ID;
const API_TOKEN = process.env.MOSAIC_API_TOKEN;
@@ -20,7 +20,7 @@ const API_TOKEN = process.env.MOSAIC_API_TOKEN;
function apiRequest(method, path, body = null) {
return new Promise((resolve, reject) => {
const url = new URL(path, API_URL);
const isHttps = url.protocol === 'https:';
const isHttps = url.protocol === "https:";
const client = isHttps ? https : http;
const options = {
@@ -29,16 +29,16 @@ function apiRequest(method, path, body = null) {
port: url.port || (isHttps ? 443 : 80),
path: url.pathname + url.search,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_TOKEN}`,
'X-Workspace-ID': WORKSPACE_ID
}
"Content-Type": "application/json",
Authorization: `Bearer ${API_TOKEN}`,
"X-Workspace-ID": WORKSPACE_ID,
},
};
const req = client.request(options, (res) => {
let data = '';
res.on('data', (chunk) => data += chunk);
res.on('end', () => {
let data = "";
res.on("data", (chunk) => (data += chunk));
res.on("end", () => {
try {
const parsed = data ? JSON.parse(data) : {};
if (res.statusCode >= 200 && res.statusCode < 300) {
@@ -46,17 +46,17 @@ function apiRequest(method, path, body = null) {
} else {
// Provide helpful error messages based on status code
let errorMsg = `HTTP ${res.statusCode}: ${parsed.message || data}`;
if (res.statusCode === 401) {
errorMsg += '\n → Check MOSAIC_API_TOKEN is valid';
errorMsg += "\n → Check MOSAIC_API_TOKEN is valid";
} else if (res.statusCode === 403) {
errorMsg += '\n → Verify workspace permissions and MOSAIC_WORKSPACE_ID';
errorMsg += "\n → Verify workspace permissions and MOSAIC_WORKSPACE_ID";
} else if (res.statusCode === 404) {
errorMsg += '\n → Resource not found. Check the event ID';
errorMsg += "\n → Resource not found. Check the event ID";
} else if (res.statusCode === 400) {
errorMsg += '\n → Invalid request. Check date formats and required fields';
errorMsg += "\n → Invalid request. Check date formats and required fields";
}
reject(new Error(errorMsg));
}
} catch (err) {
@@ -65,7 +65,7 @@ function apiRequest(method, path, body = null) {
});
});
req.on('error', reject);
req.on("error", reject);
if (body) {
req.write(JSON.stringify(body));
@@ -82,10 +82,10 @@ function parseArgs(args) {
const parsed = { _: [] };
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg.startsWith('--')) {
if (arg.startsWith("--")) {
const key = arg.slice(2);
const value = args[i + 1];
if (value && !value.startsWith('--')) {
if (value && !value.startsWith("--")) {
parsed[key] = value;
i++;
} else {
@@ -103,7 +103,7 @@ function parseArgs(args) {
*/
async function createEvent(args) {
if (!args.title || !args.start) {
throw new Error('Required: --title and --start (ISO 8601 date)');
throw new Error("Required: --title and --start (ISO 8601 date)");
}
const body = {
@@ -114,11 +114,11 @@ async function createEvent(args) {
if (args.end) body.endTime = args.end;
if (args.description) body.description = args.description;
if (args.location) body.location = args.location;
if (args['all-day'] !== undefined) body.allDay = args['all-day'] === 'true';
if (args['project-id']) body.projectId = args['project-id'];
if (args["all-day"] !== undefined) body.allDay = args["all-day"] === "true";
if (args["project-id"]) body.projectId = args["project-id"];
if (args.metadata) body.metadata = JSON.parse(args.metadata);
const result = await apiRequest('POST', '/events', body);
const result = await apiRequest("POST", "/events", body);
return result;
}
@@ -128,17 +128,17 @@ async function createEvent(args) {
async function listEvents(args) {
const params = new URLSearchParams();
if (args.from) params.append('startFrom', args.from);
if (args.to) params.append('startTo', args.to);
if (args['project-id']) params.append('projectId', args['project-id']);
if (args['all-day'] !== undefined) params.append('allDay', args['all-day']);
if (args.page) params.append('page', args.page);
if (args.limit) params.append('limit', args.limit);
if (args.from) params.append("startFrom", args.from);
if (args.to) params.append("startTo", args.to);
if (args["project-id"]) params.append("projectId", args["project-id"]);
if (args["all-day"] !== undefined) params.append("allDay", args["all-day"]);
if (args.page) params.append("page", args.page);
if (args.limit) params.append("limit", args.limit);
const query = params.toString();
const path = `/events${query ? '?' + query : ''}`;
const path = `/events${query ? "?" + query : ""}`;
const result = await apiRequest('GET', path);
const result = await apiRequest("GET", path);
return result;
}
@@ -147,10 +147,10 @@ async function listEvents(args) {
*/
async function getEvent(eventId) {
if (!eventId) {
throw new Error('Event ID required');
throw new Error("Event ID required");
}
const result = await apiRequest('GET', `/events/${eventId}`);
const result = await apiRequest("GET", `/events/${eventId}`);
return result;
}
@@ -159,7 +159,7 @@ async function getEvent(eventId) {
*/
async function updateEvent(eventId, args) {
if (!eventId) {
throw new Error('Event ID required');
throw new Error("Event ID required");
}
const body = {};
@@ -169,11 +169,11 @@ async function updateEvent(eventId, args) {
if (args.end) body.endTime = args.end;
if (args.description) body.description = args.description;
if (args.location) body.location = args.location;
if (args['all-day'] !== undefined) body.allDay = args['all-day'] === 'true';
if (args['project-id']) body.projectId = args['project-id'];
if (args["all-day"] !== undefined) body.allDay = args["all-day"] === "true";
if (args["project-id"]) body.projectId = args["project-id"];
if (args.metadata) body.metadata = JSON.parse(args.metadata);
const result = await apiRequest('PATCH', `/events/${eventId}`, body);
const result = await apiRequest("PATCH", `/events/${eventId}`, body);
return result;
}
@@ -182,10 +182,10 @@ async function updateEvent(eventId, args) {
*/
async function deleteEvent(eventId) {
if (!eventId) {
throw new Error('Event ID required');
throw new Error("Event ID required");
}
const result = await apiRequest('DELETE', `/events/${eventId}`);
const result = await apiRequest("DELETE", `/events/${eventId}`);
return result;
}
@@ -198,12 +198,12 @@ async function main() {
// Validate environment
if (!WORKSPACE_ID) {
console.error('Error: MOSAIC_WORKSPACE_ID environment variable required');
console.error("Error: MOSAIC_WORKSPACE_ID environment variable required");
process.exit(1);
}
if (!API_TOKEN) {
console.error('Error: MOSAIC_API_TOKEN environment variable required');
console.error("Error: MOSAIC_API_TOKEN environment variable required");
process.exit(1);
}
@@ -211,32 +211,32 @@ async function main() {
let result;
switch (command) {
case 'create':
case "create":
result = await createEvent(args);
console.log(JSON.stringify(result, null, 2));
break;
case 'list':
case "list":
result = await listEvents(args);
console.log(JSON.stringify(result, null, 2));
break;
case 'get':
case "get":
result = await getEvent(args._[1]);
console.log(JSON.stringify(result, null, 2));
break;
case 'update':
case "update":
result = await updateEvent(args._[1], args);
console.log(JSON.stringify(result, null, 2));
break;
case 'delete':
case "delete":
result = await deleteEvent(args._[1]);
console.log(JSON.stringify(result, null, 2));
break;
case 'help':
case "help":
default:
console.log(`
Mosaic Calendar CLI
@@ -287,7 +287,7 @@ Environment:
break;
}
} catch (error) {
console.error('Error:', error.message);
console.error("Error:", error.message);
process.exit(1);
}
}
@@ -295,11 +295,4 @@ Environment:
// Run if called directly
main();
export {
createEvent,
listEvents,
getEvent,
updateEvent,
deleteEvent,
apiRequest
};
export { createEvent, listEvents, getEvent, updateEvent, deleteEvent, apiRequest };