Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

feat(hub): add singleton pattern and subagent spawning

yushen5b6a1c695337
diff --git a/src/hub/hub-singleton.ts b/src/hub/hub-singleton.ts
new file mode 100644
index 0000000000..5d04d0f7a8
--- /dev/null
+++ b/src/hub/hub-singleton.ts
@@ -0,0 +1,28 @@
+/**
+ * Global Hub singleton for cross-module access.
+ *
+ * Used by subagent tools and announce flow to interact with the Hub
+ * without threading references through the entire call chain.
+ */
+
+import type { Hub } from "./hub.js";
+
+let _hub: Hub | undefined;
+
+/** Set the global Hub instance. Called once during Hub construction. */
+export function setHub(hub: Hub): void {
+ _hub = hub;
+}
+
+/** Get the global Hub instance. Throws if not yet initialized. */
+export function getHub(): Hub {
+ if (!_hub) {
+ throw new Error("[Hub] Hub singleton not initialized. Ensure Hub is constructed before accessing.");
+ }
+ return _hub;
+}
+
+/** Check if the Hub singleton has been initialized. */
+export function isHubInitialized(): boolean {
+ return _hub !== undefined;
+}
diff --git a/src/hub/hub.ts b/src/hub/hub.ts
index 10236ff3de..bc86bfdb2d 100644
--- a/src/hub/hub.ts
+++ b/src/hub/hub.ts
@@ -9,7 +9,9 @@ import {
type ResponseErrorPayload,
} from "@multica/sdk";
import { AsyncAgent } from "../agent/async-agent.js";
+import type { AgentOptions } from "../agent/types.js";
import { getHubId } from "./hub-identity.js";
+import { setHub } from "./hub-singleton.js";
import { loadAgentRecords, addAgentRecord, removeAgentRecord } from "./agent-store.js";
import { RpcDispatcher, RpcError } from "./rpc/dispatcher.js";
import { createGetAgentMessagesHandler } from "./rpc/handlers/get-agent-messages.js";
@@ -48,6 +50,9 @@ export class Hub {
this.rpc.register("deleteAgent", createDeleteAgentHandler(this));
this.rpc.register("updateGateway", createUpdateGatewayHandler(this));
+ // Register as global singleton for cross-module access (subagent tools, announce flow)
+ setHub(this);
+
this.client = this.createClient(this.url);
this.client.connect();
this.restoreAgents();
@@ -243,6 +248,27 @@ export class Hub {
}
}
+ /** Create a subagent with specific options (isSubagent, systemPrompt, model) */
+ createSubagent(sessionId: string, options: Omit<AgentOptions, "sessionId"> = {}): AsyncAgent {
+ const existing = this.agents.get(sessionId);
+ if (existing && !existing.closed) {
+ return existing;
+ }
+
+ const agent = new AsyncAgent({
+ ...options,
+ sessionId,
+ isSubagent: true,
+ });
+ this.agents.set(agent.sessionId, agent);
+
+ // Subagents are ephemeral — don't persist to agent store
+ void this.consumeAgent(agent);
+
+ console.log(`[Hub] Subagent created: ${agent.sessionId}`);
+ return agent;
+ }
+
getAgent(id: string): AsyncAgent | undefined {
return this.agents.get(id);
}