u-foo 3.1.7 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/OBSERVABILITY.md +65 -0
- package/README.md +238 -21
- package/README.zh-CN.md +165 -20
- package/SKILLS/ufoo/SKILL.md +40 -4
- package/SKILLS/ufoo-bus/SKILL.md +46 -3
- package/bin/uclaude.js +4 -0
- package/bin/ucodex.js +4 -0
- package/bin/ufoo.js +6 -4
- package/dist/tui/darwin-arm64/ufoo-tui +0 -0
- package/dist/tui/darwin-x64/ufoo-tui +0 -0
- package/dist/tui/linux-arm64/ufoo-tui +0 -0
- package/dist/tui/linux-x64/ufoo-tui +0 -0
- package/package.json +14 -7
- package/scripts/postinstall-skills.js +53 -0
- package/scripts/postinstall.js +11 -16
- package/scripts/smoke-native-messages.js +214 -0
- package/src/agents/activity/activityDetector.js +1 -1
- package/src/agents/activity/activityStateWriter.js +37 -38
- package/src/agents/capabilities/agentManagement/index.js +4 -0
- package/src/agents/capabilities/agentManagement/service.js +140 -0
- package/src/agents/capabilities/agentRouting/index.js +4 -0
- package/src/agents/capabilities/coding/index.js +32 -0
- package/src/agents/capabilities/coordination/index.js +37 -0
- package/src/agents/capabilities/globalRouting/index.js +5 -0
- package/src/agents/capabilities/groups/index.js +3 -0
- package/src/agents/capabilities/index.js +25 -0
- package/src/agents/capabilities/planning/index.js +123 -0
- package/src/agents/capabilities/planning/main.js +28 -0
- package/src/agents/capabilities/projectDiscovery/index.js +3 -0
- package/src/agents/capabilities/scheduling/index.js +3 -0
- package/src/agents/capabilities/sharedMemory/index.js +4 -0
- package/src/agents/capabilities/skills/index.js +12 -0
- package/src/agents/capabilities/taskExecution/index.js +3 -0
- package/src/agents/capabilities/taskReports/index.js +6 -0
- package/src/agents/controller/controllerToolExecutor.js +40 -22
- package/src/agents/controller/index.js +1 -6
- package/src/agents/internal/internalRunner.js +181 -76
- package/src/agents/launch/index.js +0 -1
- package/src/agents/launch/launcher.js +96 -14
- package/src/agents/launch/nativeCapabilities.js +40 -0
- package/src/agents/launch/nativeMessages.js +362 -0
- package/src/agents/launch/notifier.js +15 -20
- package/src/agents/launch/readyDetector.js +28 -14
- package/src/agents/profiles/coding.js +6 -0
- package/src/agents/profiles/globalRouter.js +2 -0
- package/src/agents/profiles/main.js +5 -0
- package/src/agents/prompts/cooperation.js +14 -0
- package/src/agents/prompts/defaultBootstrap.js +1 -1
- package/src/agents/prompts/groupBootstrap.js +32 -6
- package/src/agents/prompts/mainAgent.js +48 -0
- package/src/agents/prompts/native/system.js +4 -1
- package/src/agents/prompts/native/tasks.js +2 -2
- package/src/agents/prompts/native/ufoo.js +14 -3
- package/src/agents/prompts/promptProfiles.js +2 -0
- package/src/agents/prompts/reporting.js +16 -0
- package/src/agents/providers/claudeEventTranslator.js +9 -1
- package/src/agents/providers/claudeThreadProvider.js +47 -4
- package/src/agents/providers/clientIdentity.js +75 -0
- package/src/agents/providers/codexEventTranslator.js +68 -7
- package/src/agents/providers/codexThreadProvider.js +18 -8
- package/src/agents/providers/credentials/claude.js +10 -3
- package/src/agents/providers/credentials/index.js +2 -0
- package/src/agents/providers/directAuthStatus.js +1 -1
- package/src/agents/providers/index.js +1 -1
- package/src/agents/providers/modelSettings.js +34 -0
- package/src/agents/providers/modelsCatalog.js +379 -0
- package/src/agents/providers/nativeTransport.js +1146 -0
- package/src/agents/providers/runtimeConfig.js +188 -0
- package/src/agents/providers/thinkingLevels.js +106 -0
- package/src/agents/providers/transports/anthropicMessagesTransport.js +121 -0
- package/src/agents/providers/transports/index.js +10 -0
- package/src/agents/providers/transports/openaiChatTransport.js +117 -0
- package/src/agents/providers/transports/openaiResponsesTransport.js +120 -0
- package/src/agents/providers/transports/responsesProtocol.js +504 -0
- package/src/agents/providers/transports/systemBlocks.js +16 -0
- package/src/agents/providers/transports/transportContract.js +50 -0
- package/src/agents/providers/transports/visionBlocks.js +151 -0
- package/src/agents/providers/upstreamRuntime.js +135 -0
- package/src/agents/runtime/composeCapabilities.js +89 -0
- package/src/agents/runtime/context/checkpointStore.js +38 -0
- package/src/agents/runtime/context/commandStore.js +43 -0
- package/src/agents/runtime/context/compaction.js +151 -0
- package/src/agents/runtime/context/contextBudget.js +93 -0
- package/src/agents/runtime/context/contextWindow.js +119 -0
- package/src/agents/runtime/context/fileLock.js +36 -0
- package/src/agents/runtime/context/replayHistory.js +28 -0
- package/src/agents/runtime/context/runtimeStore.js +127 -0
- package/src/agents/runtime/context/sessionJournal.js +360 -0
- package/src/agents/runtime/context/sessionStore.js +57 -0
- package/src/agents/runtime/core/agentLoop.js +351 -0
- package/src/agents/runtime/core/executionBudget.js +36 -0
- package/src/agents/runtime/core/stableJson.js +29 -0
- package/src/agents/runtime/core/toolLoopGuard.js +55 -0
- package/src/agents/runtime/createAgentRuntime.js +259 -0
- package/src/agents/runtime/index.js +14 -0
- package/src/agents/runtime/prompts/responseLanguage.js +12 -0
- package/src/agents/runtime/protocol/faultHarness.js +90 -0
- package/src/agents/runtime/protocol/materialize.js +107 -0
- package/src/agents/runtime/protocol/runObservation.js +24 -0
- package/src/agents/runtime/protocol/toolCallLedger.js +221 -0
- package/src/agents/runtime/protocol/traceContext.js +9 -0
- package/src/agents/runtime/tasks/resourceLease.js +66 -0
- package/src/agents/runtime/tasks/scheduler.js +73 -0
- package/src/agents/runtime/tools/registry.js +57 -0
- package/src/app/chat/ChatController.js +29 -45
- package/src/app/chat/agentDirectory.js +9 -18
- package/src/app/chat/bootstrap.js +1 -2
- package/src/app/chat/commandExecutor.js +111 -313
- package/src/app/chat/commands.js +39 -45
- package/src/app/chat/daemonConnection.js +58 -5
- package/src/app/chat/daemonMessageRouter.js +61 -48
- package/src/app/chat/historyStore.js +12 -48
- package/src/app/chat/inputSubmitHandler.js +21 -35
- package/src/app/chat/internalAgentLogHistory.js +19 -60
- package/src/app/chat/launchRequestContext.js +42 -0
- package/src/app/chat/multiWindow/paneManager.js +91 -164
- package/src/app/chat/streamState.js +3 -7
- package/src/app/chat/transport.js +1 -1
- package/src/app/cli/busCoreCommands.js +9 -0
- package/src/app/cli/features/doctor.js +33 -0
- package/src/app/cli/features/init.js +5 -5
- package/src/app/cli/features/skills.js +16 -4
- package/src/app/cli/metricsCommands.js +68 -0
- package/src/app/cli/reportCoreCommands.js +37 -0
- package/src/app/cli/run.js +82 -42
- package/src/app/cli/traceCommands.js +36 -0
- package/src/code/agent.js +34 -17
- package/src/code/busConsumer.js +1 -3
- package/src/code/commands.js +1 -1
- package/src/code/context/artifacts.js +12 -11
- package/src/code/context/assembler.js +9 -9
- package/src/code/context/promptLayers.js +1 -13
- package/src/code/context/stableJson.js +2 -27
- package/src/code/context/stateCommit.js +2 -1
- package/src/code/contextWindow.js +2 -117
- package/src/code/conversation/sessionJournal.js +3 -349
- package/src/code/internalThread.js +107 -0
- package/src/code/launcher/ucode.js +1 -1
- package/src/code/launcher/ucodeBootstrap.js +1 -1
- package/src/code/modelCommand.js +11 -35
- package/src/code/nativeRunner.js +104 -2697
- package/src/code/protocol/faultHarness.js +2 -88
- package/src/code/protocol/materialize.js +2 -105
- package/src/code/protocol/toolCallLedger.js +5 -215
- package/src/code/providers/anthropicMessagesTransport.js +2 -118
- package/src/code/providers/modelsCatalog.js +1 -317
- package/src/code/providers/openaiChatTransport.js +2 -114
- package/src/code/providers/openaiResponsesTransport.js +2 -117
- package/src/code/providers/responsesProtocol.js +2 -497
- package/src/code/providers/transportContract.js +2 -48
- package/src/code/providers/visionBlocks.js +2 -149
- package/src/code/repl.js +2 -11
- package/src/code/sessionStore.js +27 -99
- package/src/code/taskDecomposer.js +18 -0
- package/src/code/thinkingLevels.js +1 -130
- package/src/code/toolBudget.js +1 -17
- package/src/code/toolLoopGuard.js +3 -49
- package/src/code/tools/artifactRead.js +1 -1
- package/src/code/tools/executor.js +373 -0
- package/src/code/tools/specs.js +312 -0
- package/src/config.js +95 -131
- package/src/coordination/bus/deliveryQueue.js +16 -2
- package/src/coordination/bus/index.js +14 -9
- package/src/coordination/bus/inject.js +15 -7
- package/src/coordination/bus/message.js +15 -1
- package/src/coordination/bus/nativeReceipts.js +100 -0
- package/src/coordination/bus/nickname.js +1 -1
- package/src/coordination/bus/queue.js +1 -3
- package/src/coordination/bus/subscriber.js +3 -2
- package/src/coordination/bus/utils.js +8 -4
- package/src/coordination/history/agentPerformance.js +279 -0
- package/src/coordination/history/agentSurface.js +142 -0
- package/src/coordination/history/agentTrace.js +249 -0
- package/src/coordination/memory/index.js +5 -21
- package/src/coordination/report/store.js +103 -83
- package/src/coordination/state/agentsStore.js +57 -3
- package/src/coordination/state/fileLock.js +2 -0
- package/src/coordination/state/workspaceAccess.js +36 -0
- package/src/orchestration/controller/index.js +1 -8
- package/src/orchestration/controller/launchRouting.js +1 -1
- package/src/orchestration/solo/commands.js +3 -1
- package/src/runtime/contracts/agentMode.js +26 -0
- package/src/runtime/contracts/eventContract.js +3 -0
- package/src/runtime/contracts/uiProtocol.js +20 -17
- package/src/runtime/daemon/agentHost.js +407 -0
- package/src/runtime/daemon/agentRecoveryGroups.js +70 -0
- package/src/runtime/daemon/busBridge.js +396 -0
- package/src/runtime/daemon/claudeChannel.js +159 -0
- package/src/runtime/daemon/controlPlaneService.js +28 -18
- package/src/runtime/daemon/cronOps.js +25 -17
- package/src/runtime/daemon/deliveryScheduler.js +32 -5
- package/src/runtime/daemon/globalDaemon.js +28 -1
- package/src/runtime/daemon/groupOrchestrator.js +21 -5
- package/src/runtime/daemon/groupStatus.js +20 -0
- package/src/runtime/daemon/index.js +120 -375
- package/src/runtime/daemon/ipcServer.js +32 -10
- package/src/runtime/daemon/mcpConfigure.js +113 -20
- package/src/runtime/daemon/mcpHttpServer.js +27 -5
- package/src/runtime/daemon/mcpServer.js +8 -5
- package/src/runtime/daemon/mcpStdioProxy.js +127 -10
- package/src/runtime/daemon/nicknameScope.js +1 -1
- package/src/runtime/daemon/ops.js +68 -40
- package/src/runtime/daemon/projectRuntimeGateway.js +2 -0
- package/src/runtime/daemon/promptRequest.js +92 -570
- package/src/runtime/daemon/providerSessions.js +67 -33
- package/src/runtime/daemon/reportControlBus.js +59 -25
- package/src/runtime/daemon/status.js +9 -6
- package/src/runtime/privacy/redactor.js +2 -2
- package/src/runtime/projects/identity.js +2 -1
- package/src/runtime/projects/registry.js +98 -38
- package/src/tools/controllerSchemas.js +28 -0
- package/src/tools/handlers/dispatchMessage.js +5 -2
- package/src/tools/handlers/listAgents.js +3 -1
- package/src/tools/handlers/readBusSummary.js +3 -1
- package/src/tools/handlers/readProjectRegistry.js +29 -2
- package/src/tools/handlers/routeAgent.js +35 -0
- package/src/tools/handlers/tier2.js +4 -2
- package/src/tools/index.js +1 -0
- package/src/tools/registry.js +6 -0
- package/src/tools/schemaFixtures.js +42 -5
- package/src/tools/tier0/readAgentMetrics.js +14 -0
- package/src/tools/tier0/readAgentTrace.js +12 -0
- package/src/tools/tier1/routeAgent.js +2 -2
- package/src/tools/tier2/runtimeCoordination.js +38 -0
- package/src/tools/types.js +2 -0
- package/src/ui/agentPresentation.js +109 -0
- package/src/ui/agentSettingsController.js +107 -0
- package/src/ui/agentSurface.js +270 -0
- package/src/ui/dashboardBridge.js +1 -1
- package/src/ui/format/banner.js +9 -15
- package/src/ui/format/index.js +14 -10
- package/src/ui/format/loopSummary.js +50 -0
- package/src/ui/format/markdownRenderer.js +3 -4
- package/src/ui/format/startupBanner.js +50 -0
- package/src/ui/multiPaneBusMirror.js +29 -3
- package/src/ui/rustChatHost.js +316 -599
- package/src/ui/rustMultiSession.js +288 -73
- package/src/ui/rustUcodeHost.js +21 -88
- package/src/ui/settingsBridge.js +34 -14
- package/src/ui/toolMergeBridge.js +0 -4
- package/src/ui/uiHostServer.js +5 -8
- package/src/agents/controller/loopObservability.js +0 -200
- package/src/agents/controller/loopRuntime.js +0 -472
- package/src/agents/controller/ufooAgent.js +0 -829
- package/src/agents/launch/ptyRunner.js +0 -1135
- package/src/agents/providers/upstreamTransport.js +0 -689
- package/src/app/chat/agentBar.js +0 -135
- package/src/app/chat/agentEnter.js +0 -70
- package/src/app/chat/agentSockets.js +0 -225
- package/src/app/chat/dashboardView.js +0 -405
- package/src/app/chat/multiWindow/index.js +0 -310
- package/src/app/chat/multiWindow/paneLayout.js +0 -84
- package/src/app/chat/multiWindow/renderer.js +0 -420
- package/src/app/chat/multiWindow/virtualTerminal.js +0 -327
- package/src/app/chat/multiWindow/vtFrame.js +0 -93
- package/src/app/chat/rawKeyMap.js +0 -42
- package/src/app/chat/settingsController.js +0 -111
- package/src/orchestration/controller/flags.js +0 -167
- package/src/orchestration/controller/gateRouter.js +0 -211
- package/src/orchestration/controller/routerFastPath.js +0 -22
- package/src/orchestration/controller/routerFinalize.js +0 -222
- package/src/orchestration/controller/shadowGuard.js +0 -280
- package/src/runtime/daemon/promptLoop.js +0 -136
- package/src/runtime/privacy/shadowDiff.js +0 -174
- package/src/ui/multiWindowHandoff.js +0 -232
- package/src/ui/ptyHandoff.js +0 -23
|
@@ -2,17 +2,17 @@ const fs = require("fs");
|
|
|
2
2
|
const path = require("path");
|
|
3
3
|
const net = require("net");
|
|
4
4
|
const { spawn, spawnSync } = require("child_process");
|
|
5
|
-
const { runUfooAgent, runUfooRouteAgent } = require("../../agents/controller/ufooAgent");
|
|
6
5
|
const { launchAgent, closeAgent, getRecoverableAgents, resumeAgents } = require("./ops");
|
|
7
6
|
const { buildStatus } = require("./status");
|
|
8
7
|
const EventBus = require("../../coordination/bus");
|
|
9
8
|
const { AgentProcessManager } = require("./agentProcessManager");
|
|
10
9
|
const NicknameManager = require("../../coordination/bus/nickname");
|
|
11
|
-
const {
|
|
12
|
-
const { generateInstanceId, subscriberToSafeName } = require("../../coordination/bus/utils");
|
|
10
|
+
const { generateInstanceId } = require("../../coordination/bus/utils");
|
|
13
11
|
const { createDaemonIpcServer } = require("./ipcServer");
|
|
14
12
|
const { IPC_REQUEST_TYPES, IPC_RESPONSE_TYPES, BUS_STATUS_PHASES } = require("../contracts/eventContract");
|
|
15
13
|
const { getUfooPaths } = require("../../coordination/state/paths");
|
|
14
|
+
const { loadAgentsData } = require("../../coordination/state/agentsStore");
|
|
15
|
+
const { isInternalAgentMeta } = require("../contracts/agentMode");
|
|
16
16
|
const {
|
|
17
17
|
upsertProjectRuntime,
|
|
18
18
|
markProjectDormant,
|
|
@@ -24,14 +24,10 @@ const { createDaemonCronController } = require("./cronOps");
|
|
|
24
24
|
const { createGroupOrchestrator } = require("./groupOrchestrator");
|
|
25
25
|
const { DeliveryScheduler } = require("./deliveryScheduler");
|
|
26
26
|
const { normalizeFormat, renderGroupDiagramFromTemplate, renderGroupDiagramFromRuntime } = require("../../orchestration/groups/diagram");
|
|
27
|
-
const { runPromptWithAssistant } = require("./promptLoop");
|
|
28
27
|
const { handlePromptRequest } = require("./promptRequest");
|
|
29
28
|
const { recordAgentReport } = require("./reporting");
|
|
30
|
-
const {
|
|
31
|
-
|
|
32
|
-
extractAgentReportControl,
|
|
33
|
-
takeReportControlEvents,
|
|
34
|
-
} = require("./reportControlBus");
|
|
29
|
+
const { startBusBridge } = require("./busBridge");
|
|
30
|
+
const { createAgentHost } = require("./agentHost");
|
|
35
31
|
const { isGlobalControllerProjectRoot } = require("../projects");
|
|
36
32
|
const {
|
|
37
33
|
assignSoloRoleToExistingAgent,
|
|
@@ -79,7 +75,7 @@ function normalizeBusAgentType(agentType = "") {
|
|
|
79
75
|
if (value === "agy" || value === "antigravity") return "agy";
|
|
80
76
|
if (value === "grok" || value === "ugrok" || value === "grok-build" || value === "xai") return "grok";
|
|
81
77
|
if (value === "kimi" || value === "kimi-cli" || value === "kimi-code") return "kimi";
|
|
82
|
-
if (value === "
|
|
78
|
+
if (value === "ucode") return "ucode";
|
|
83
79
|
return value;
|
|
84
80
|
}
|
|
85
81
|
|
|
@@ -90,7 +86,7 @@ function normalizeLaunchAgent(agent = "") {
|
|
|
90
86
|
if (value === "agy" || value === "antigravity") return "agy";
|
|
91
87
|
if (value === "grok" || value === "ugrok" || value === "grok-build" || value === "xai") return "grok";
|
|
92
88
|
if (value === "kimi" || value === "kimi-cli" || value === "kimi-code") return "kimi";
|
|
93
|
-
if (value === "
|
|
89
|
+
if (value === "ucode") return "ucode";
|
|
94
90
|
return "";
|
|
95
91
|
}
|
|
96
92
|
|
|
@@ -572,6 +568,13 @@ async function handleOps(projectRoot, ops = [], processManager = null, runtimeSe
|
|
|
572
568
|
// Check for existing agent with same nickname
|
|
573
569
|
const { existing, cleaned } = checkAndCleanupNickname(projectRoot, nickname, { scopedNickname, agentType: agent });
|
|
574
570
|
if (existing) {
|
|
571
|
+
if (op.internal_only || runtimeServices.internalOnly) {
|
|
572
|
+
const meta = loadAgentsData(getUfooPaths(projectRoot).agentsFile).agents[existing];
|
|
573
|
+
if (!isInternalAgentMeta(meta)) {
|
|
574
|
+
results.push({ action: "launch", ok: false, agent, error: "nickname belongs to a non-internal agent; choose another nickname" });
|
|
575
|
+
continue;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
575
578
|
// Agent with this nickname already exists and is active
|
|
576
579
|
results.push({
|
|
577
580
|
action: "launch",
|
|
@@ -588,6 +591,7 @@ async function handleOps(projectRoot, ops = [], processManager = null, runtimeSe
|
|
|
588
591
|
}
|
|
589
592
|
// eslint-disable-next-line no-await-in-loop
|
|
590
593
|
const launchResult = await launchAgent(projectRoot, agent, count, nickname, processManager, {
|
|
594
|
+
internalOnly: op.internal_only === true || runtimeServices.internalOnly === true,
|
|
591
595
|
scopedNickname,
|
|
592
596
|
launchScope: op.launch_scope || "",
|
|
593
597
|
terminalApp: op.terminal_app || "",
|
|
@@ -802,8 +806,9 @@ async function handleOps(projectRoot, ops = [], processManager = null, runtimeSe
|
|
|
802
806
|
return results;
|
|
803
807
|
}
|
|
804
808
|
|
|
805
|
-
async function dispatchMessages(projectRoot, dispatch = []) {
|
|
809
|
+
async function dispatchMessages(projectRoot, dispatch = [], { strict = false } = {}) {
|
|
806
810
|
const eventBus = new EventBus(projectRoot);
|
|
811
|
+
const receipts = [];
|
|
807
812
|
// Always use "ufoo-agent" as the publisher for daemon messages
|
|
808
813
|
const defaultPublisher = "ufoo-agent";
|
|
809
814
|
const resolveDispatchTarget = (target) => {
|
|
@@ -849,345 +854,24 @@ async function dispatchMessages(projectRoot, dispatch = []) {
|
|
|
849
854
|
const sendOptions = {
|
|
850
855
|
injectionMode: item.injection_mode,
|
|
851
856
|
source: item.source,
|
|
857
|
+
...(item.task_id ? { taskId: item.task_id } : {}),
|
|
858
|
+
...(item.runtime_context ? { data: { runtime_context: item.runtime_context } } : {}),
|
|
852
859
|
};
|
|
853
860
|
try {
|
|
854
861
|
if (target === "broadcast") {
|
|
855
|
-
await eventBus.broadcast(item.message, pub, sendOptions);
|
|
862
|
+
receipts.push({ ok: true, ...await eventBus.broadcast(item.message, pub, sendOptions) });
|
|
856
863
|
} else {
|
|
857
|
-
await eventBus.send(target, item.message, pub, sendOptions);
|
|
864
|
+
receipts.push({ ok: true, ...await eventBus.send(target, item.message, pub, sendOptions) });
|
|
858
865
|
}
|
|
859
866
|
} catch (err) {
|
|
860
867
|
appendControlLog(
|
|
861
868
|
projectRoot,
|
|
862
869
|
`dispatch failed target=${JSON.stringify(item.target)} resolved=${JSON.stringify(target)} error=${err && err.message ? err.message : String(err)}`
|
|
863
870
|
);
|
|
871
|
+
if (strict) { err.receipts = receipts; throw err; }
|
|
864
872
|
}
|
|
865
873
|
}
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
function startBusBridge(
|
|
869
|
-
projectRoot,
|
|
870
|
-
provider,
|
|
871
|
-
onEvent,
|
|
872
|
-
onStatus,
|
|
873
|
-
shouldDrain,
|
|
874
|
-
onReport,
|
|
875
|
-
options = {}
|
|
876
|
-
) {
|
|
877
|
-
const state = {
|
|
878
|
-
subscriber: null,
|
|
879
|
-
queueFile: null,
|
|
880
|
-
pending: new Set(),
|
|
881
|
-
watchedAgents: new Set(),
|
|
882
|
-
lastEventSeq: 0,
|
|
883
|
-
emittedEventKeys: [],
|
|
884
|
-
emittedEventKeySet: new Set(),
|
|
885
|
-
};
|
|
886
|
-
const eventBus = options.eventBus || new EventBus(projectRoot);
|
|
887
|
-
const reportJoinError = typeof options.onJoinError === "function"
|
|
888
|
-
? options.onJoinError
|
|
889
|
-
: () => {};
|
|
890
|
-
let joinInProgress = null;
|
|
891
|
-
let lastJoinError = "";
|
|
892
|
-
let stopped = false;
|
|
893
|
-
let polling = false;
|
|
894
|
-
|
|
895
|
-
function getAgentNickname(agentId) {
|
|
896
|
-
if (!agentId) return agentId;
|
|
897
|
-
try {
|
|
898
|
-
const busPath = getUfooPaths(projectRoot).agentsFile;
|
|
899
|
-
const bus = JSON.parse(fs.readFileSync(busPath, "utf8"));
|
|
900
|
-
const meta = bus.agents && bus.agents[agentId];
|
|
901
|
-
if (meta && meta.nickname) {
|
|
902
|
-
return meta.nickname;
|
|
903
|
-
}
|
|
904
|
-
} catch {
|
|
905
|
-
// Ignore errors, return original ID
|
|
906
|
-
}
|
|
907
|
-
return agentId;
|
|
908
|
-
}
|
|
909
|
-
|
|
910
|
-
function getEventDedupeKey(evt) {
|
|
911
|
-
if (!evt || typeof evt !== "object") return "";
|
|
912
|
-
const seq = Number(evt.seq);
|
|
913
|
-
if (Number.isFinite(seq) && seq > 0) return `seq:${seq}`;
|
|
914
|
-
return [
|
|
915
|
-
"event",
|
|
916
|
-
evt.timestamp || evt.ts || "",
|
|
917
|
-
evt.event || "",
|
|
918
|
-
evt.publisher || "",
|
|
919
|
-
evt.target || "",
|
|
920
|
-
JSON.stringify(evt.data || {}),
|
|
921
|
-
].join(":");
|
|
922
|
-
}
|
|
923
|
-
|
|
924
|
-
function rememberEmittedEvent(evt) {
|
|
925
|
-
const key = getEventDedupeKey(evt);
|
|
926
|
-
if (!key) return false;
|
|
927
|
-
if (state.emittedEventKeySet.has(key)) return true;
|
|
928
|
-
state.emittedEventKeySet.add(key);
|
|
929
|
-
state.emittedEventKeys.push(key);
|
|
930
|
-
if (state.emittedEventKeys.length > 500) {
|
|
931
|
-
const removed = state.emittedEventKeys.splice(0, state.emittedEventKeys.length - 500);
|
|
932
|
-
for (const item of removed) state.emittedEventKeySet.delete(item);
|
|
933
|
-
}
|
|
934
|
-
return false;
|
|
935
|
-
}
|
|
936
|
-
|
|
937
|
-
function hasPositiveSeq(seq) {
|
|
938
|
-
const value = Number(seq);
|
|
939
|
-
return Number.isFinite(value) && value > 0;
|
|
940
|
-
}
|
|
941
|
-
|
|
942
|
-
function toBridgeEvent(evt) {
|
|
943
|
-
const data = evt.data && typeof evt.data === "object" ? evt.data : {};
|
|
944
|
-
return {
|
|
945
|
-
seq: evt.seq,
|
|
946
|
-
event: evt.event,
|
|
947
|
-
publisher: evt.publisher,
|
|
948
|
-
target: evt.target,
|
|
949
|
-
data,
|
|
950
|
-
message: data.message || "",
|
|
951
|
-
state: data.state || "",
|
|
952
|
-
previous: data.previous || "",
|
|
953
|
-
subscriber: data.subscriber || "",
|
|
954
|
-
source: data.source || "",
|
|
955
|
-
injection_mode: data.injection_mode || "",
|
|
956
|
-
ts: evt.timestamp || evt.ts,
|
|
957
|
-
};
|
|
958
|
-
}
|
|
959
|
-
|
|
960
|
-
function emitBusEvent(evt) {
|
|
961
|
-
if (!evt || !onEvent) return;
|
|
962
|
-
if (rememberEmittedEvent(evt)) return;
|
|
963
|
-
onEvent(toBridgeEvent(evt));
|
|
964
|
-
}
|
|
965
|
-
|
|
966
|
-
function readAgentsData() {
|
|
967
|
-
try {
|
|
968
|
-
const busPath = getUfooPaths(projectRoot).agentsFile;
|
|
969
|
-
return JSON.parse(fs.readFileSync(busPath, "utf8"));
|
|
970
|
-
} catch {
|
|
971
|
-
return {};
|
|
972
|
-
}
|
|
973
|
-
}
|
|
974
|
-
|
|
975
|
-
function buildWatchedAliases() {
|
|
976
|
-
const aliases = new Set();
|
|
977
|
-
const bus = readAgentsData();
|
|
978
|
-
for (const agentId of state.watchedAgents) {
|
|
979
|
-
aliases.add(agentId);
|
|
980
|
-
const meta = bus.agents && bus.agents[agentId];
|
|
981
|
-
if (!meta) continue;
|
|
982
|
-
if (meta.nickname) aliases.add(meta.nickname);
|
|
983
|
-
if (meta.scoped_nickname) aliases.add(meta.scoped_nickname);
|
|
984
|
-
if (meta.display_nickname) aliases.add(meta.display_nickname);
|
|
985
|
-
}
|
|
986
|
-
return aliases;
|
|
987
|
-
}
|
|
988
|
-
|
|
989
|
-
function isWatchedEvent(evt, aliases = buildWatchedAliases()) {
|
|
990
|
-
if (!evt || (evt.event !== "message" && evt.event !== "activity_state_changed")) return false;
|
|
991
|
-
const publisher = String(evt.publisher || "");
|
|
992
|
-
const target = String(evt.target || "");
|
|
993
|
-
const subscriber = evt.data && evt.data.subscriber ? String(evt.data.subscriber) : "";
|
|
994
|
-
return aliases.has(publisher) || aliases.has(target) || aliases.has(subscriber);
|
|
995
|
-
}
|
|
996
|
-
|
|
997
|
-
function getEventFiles() {
|
|
998
|
-
try {
|
|
999
|
-
const dir = getUfooPaths(projectRoot).busEventsDir;
|
|
1000
|
-
return fs.readdirSync(dir)
|
|
1001
|
-
.filter((name) => name.endsWith(".jsonl"))
|
|
1002
|
-
.sort()
|
|
1003
|
-
.map((name) => path.join(dir, name));
|
|
1004
|
-
} catch {
|
|
1005
|
-
return [];
|
|
1006
|
-
}
|
|
1007
|
-
}
|
|
1008
|
-
|
|
1009
|
-
function readCurrentSeq() {
|
|
1010
|
-
try {
|
|
1011
|
-
const raw = fs.readFileSync(path.join(getUfooPaths(projectRoot).busDir, "seq.counter"), "utf8").trim();
|
|
1012
|
-
const seq = Number(raw);
|
|
1013
|
-
return Number.isFinite(seq) ? seq : 0;
|
|
1014
|
-
} catch {
|
|
1015
|
-
return 0;
|
|
1016
|
-
}
|
|
1017
|
-
}
|
|
1018
|
-
|
|
1019
|
-
function readEventFile(file) {
|
|
1020
|
-
try {
|
|
1021
|
-
return fs.readFileSync(file, "utf8")
|
|
1022
|
-
.split(/\r?\n/)
|
|
1023
|
-
.filter(Boolean)
|
|
1024
|
-
.map((line) => {
|
|
1025
|
-
try {
|
|
1026
|
-
return JSON.parse(line);
|
|
1027
|
-
} catch {
|
|
1028
|
-
return null;
|
|
1029
|
-
}
|
|
1030
|
-
})
|
|
1031
|
-
.filter(Boolean);
|
|
1032
|
-
} catch {
|
|
1033
|
-
return [];
|
|
1034
|
-
}
|
|
1035
|
-
}
|
|
1036
|
-
|
|
1037
|
-
function pollWatchedEvents() {
|
|
1038
|
-
if (state.watchedAgents.size === 0) {
|
|
1039
|
-
state.lastEventSeq = readCurrentSeq();
|
|
1040
|
-
return;
|
|
1041
|
-
}
|
|
1042
|
-
const aliases = buildWatchedAliases();
|
|
1043
|
-
let maxSeq = state.lastEventSeq;
|
|
1044
|
-
for (const file of getEventFiles().slice(-2)) {
|
|
1045
|
-
for (const evt of readEventFile(file)) {
|
|
1046
|
-
const seq = Number(evt.seq);
|
|
1047
|
-
if (hasPositiveSeq(seq)) {
|
|
1048
|
-
if (seq <= state.lastEventSeq) continue;
|
|
1049
|
-
if (seq > maxSeq) maxSeq = seq;
|
|
1050
|
-
}
|
|
1051
|
-
if (isWatchedEvent(evt, aliases)) emitBusEvent(evt);
|
|
1052
|
-
}
|
|
1053
|
-
}
|
|
1054
|
-
state.lastEventSeq = Math.max(state.lastEventSeq, maxSeq);
|
|
1055
|
-
}
|
|
1056
|
-
|
|
1057
|
-
async function ensureSubscriber() {
|
|
1058
|
-
if (stopped || state.subscriber) return state.subscriber;
|
|
1059
|
-
if (joinInProgress) return joinInProgress;
|
|
1060
|
-
joinInProgress = (async () => {
|
|
1061
|
-
try {
|
|
1062
|
-
// Determine agent type based on provider configuration
|
|
1063
|
-
const agentType = provider === "codex-cli" ? "codex" : (provider === "ucode" ? "ufoo-code" : "claude-code");
|
|
1064
|
-
// Use fixed ID "ufoo-agent" for daemon's bus identity with explicit nickname
|
|
1065
|
-
const sub = await eventBus.join("ufoo-agent", agentType, "ufoo-agent");
|
|
1066
|
-
if (!sub || stopped) return null;
|
|
1067
|
-
state.subscriber = sub;
|
|
1068
|
-
const safe = subscriberToSafeName(sub);
|
|
1069
|
-
state.queueFile = path.join(getUfooPaths(projectRoot).busQueuesDir, safe, "pending.jsonl");
|
|
1070
|
-
lastJoinError = "";
|
|
1071
|
-
return sub;
|
|
1072
|
-
} catch (err) {
|
|
1073
|
-
const detail = err && err.message ? err.message : String(err || "unknown join error");
|
|
1074
|
-
if (detail !== lastJoinError) {
|
|
1075
|
-
lastJoinError = detail;
|
|
1076
|
-
try {
|
|
1077
|
-
reportJoinError(err);
|
|
1078
|
-
} catch {
|
|
1079
|
-
// Diagnostics must never turn a recoverable join failure into an
|
|
1080
|
-
// unhandled rejection in the process-wide global daemon.
|
|
1081
|
-
}
|
|
1082
|
-
}
|
|
1083
|
-
return null;
|
|
1084
|
-
} finally {
|
|
1085
|
-
joinInProgress = null;
|
|
1086
|
-
}
|
|
1087
|
-
})();
|
|
1088
|
-
return joinInProgress;
|
|
1089
|
-
}
|
|
1090
|
-
|
|
1091
|
-
async function handleReportControlEvent(evt) {
|
|
1092
|
-
if (!isAgentReportControlEvent(evt)) return false;
|
|
1093
|
-
if (typeof onReport !== "function") return false;
|
|
1094
|
-
const control = extractAgentReportControl(evt);
|
|
1095
|
-
if (!control) return false;
|
|
1096
|
-
await onReport(control.report, {
|
|
1097
|
-
event: evt,
|
|
1098
|
-
requestId: control.request_id,
|
|
1099
|
-
queuedAt: control.queued_at,
|
|
1100
|
-
});
|
|
1101
|
-
return true;
|
|
1102
|
-
}
|
|
1103
|
-
|
|
1104
|
-
async function pollReportControlQueue() {
|
|
1105
|
-
const events = takeReportControlEvents(projectRoot);
|
|
1106
|
-
if (!events.length) return;
|
|
1107
|
-
for (const evt of events) {
|
|
1108
|
-
if (await handleReportControlEvent(evt)) continue;
|
|
1109
|
-
}
|
|
1110
|
-
}
|
|
1111
|
-
|
|
1112
|
-
function pollQueue() {
|
|
1113
|
-
if (!state.queueFile) return;
|
|
1114
|
-
const queue = new DeliveryQueue(state.queueFile);
|
|
1115
|
-
queue.recover();
|
|
1116
|
-
while (true) {
|
|
1117
|
-
const claim = queue.claimNext();
|
|
1118
|
-
if (!claim) break;
|
|
1119
|
-
const evt = claim.event;
|
|
1120
|
-
if (!evt) {
|
|
1121
|
-
queue.completeClaim(claim);
|
|
1122
|
-
continue;
|
|
1123
|
-
}
|
|
1124
|
-
try {
|
|
1125
|
-
emitBusEvent(evt);
|
|
1126
|
-
queue.completeClaim(claim);
|
|
1127
|
-
} catch {
|
|
1128
|
-
queue.restoreClaim(claim);
|
|
1129
|
-
continue;
|
|
1130
|
-
}
|
|
1131
|
-
if (evt.publisher && state.pending.has(evt.publisher)) {
|
|
1132
|
-
state.pending.delete(evt.publisher);
|
|
1133
|
-
if (onStatus) {
|
|
1134
|
-
const displayName = getAgentNickname(evt.publisher);
|
|
1135
|
-
onStatus({ phase: BUS_STATUS_PHASES.DONE, text: `${displayName} done`, key: evt.publisher });
|
|
1136
|
-
}
|
|
1137
|
-
}
|
|
1138
|
-
}
|
|
1139
|
-
}
|
|
1140
|
-
|
|
1141
|
-
async function poll() {
|
|
1142
|
-
if (polling) return;
|
|
1143
|
-
polling = true;
|
|
1144
|
-
try {
|
|
1145
|
-
await ensureSubscriber();
|
|
1146
|
-
await pollReportControlQueue();
|
|
1147
|
-
if (typeof shouldDrain === "function" && !shouldDrain()) return;
|
|
1148
|
-
pollQueue();
|
|
1149
|
-
pollWatchedEvents();
|
|
1150
|
-
} finally {
|
|
1151
|
-
polling = false;
|
|
1152
|
-
}
|
|
1153
|
-
}
|
|
1154
|
-
|
|
1155
|
-
const interval = setInterval(() => {
|
|
1156
|
-
poll().catch(() => {});
|
|
1157
|
-
}, 1000);
|
|
1158
|
-
return {
|
|
1159
|
-
markPending(target) {
|
|
1160
|
-
if (!target) return;
|
|
1161
|
-
state.pending.add(target);
|
|
1162
|
-
if (onStatus) {
|
|
1163
|
-
const displayName = getAgentNickname(target);
|
|
1164
|
-
onStatus({ phase: BUS_STATUS_PHASES.START, text: `${displayName} processing`, key: target });
|
|
1165
|
-
}
|
|
1166
|
-
},
|
|
1167
|
-
getSubscriber() {
|
|
1168
|
-
void ensureSubscriber();
|
|
1169
|
-
return state.subscriber;
|
|
1170
|
-
},
|
|
1171
|
-
refresh() {
|
|
1172
|
-
return poll();
|
|
1173
|
-
},
|
|
1174
|
-
watchAgent(agentId, enabled = true) {
|
|
1175
|
-
if (!agentId) return;
|
|
1176
|
-
if (enabled) {
|
|
1177
|
-
state.watchedAgents.add(agentId);
|
|
1178
|
-
state.lastEventSeq = Math.max(state.lastEventSeq, readCurrentSeq());
|
|
1179
|
-
} else {
|
|
1180
|
-
state.watchedAgents.delete(agentId);
|
|
1181
|
-
if (state.watchedAgents.size === 0) {
|
|
1182
|
-
state.lastEventSeq = readCurrentSeq();
|
|
1183
|
-
}
|
|
1184
|
-
}
|
|
1185
|
-
},
|
|
1186
|
-
stop() {
|
|
1187
|
-
stopped = true;
|
|
1188
|
-
clearInterval(interval);
|
|
1189
|
-
},
|
|
1190
|
-
};
|
|
874
|
+
return receipts;
|
|
1191
875
|
}
|
|
1192
876
|
|
|
1193
877
|
function startDaemon({
|
|
@@ -1341,12 +1025,23 @@ function startDaemon({
|
|
|
1341
1025
|
cronController: null,
|
|
1342
1026
|
groupOrchestrator: null,
|
|
1343
1027
|
};
|
|
1344
|
-
const handleRuntimeOps = (root, ops, manager = processManager) =>
|
|
1345
|
-
handleOps(root, ops, manager, runtimeServices);
|
|
1028
|
+
const handleRuntimeOps = async (root, ops, manager = processManager, leaseContext = {}) => {
|
|
1029
|
+
if (!ops.some((op) => op.action === "launch") || !runtimeServices.workspaceAccess) return handleOps(root, ops, manager, runtimeServices);
|
|
1030
|
+
const result = await runtimeServices.workspaceAccess.leases.run({ key: fs.realpathSync(root), ownerId: leaseContext.leaseOwnerId || `ops-${require("crypto").randomUUID()}` },
|
|
1031
|
+
() => handleOps(root, ops, manager, runtimeServices));
|
|
1032
|
+
if (result?.code === "workspace_busy") throw Object.assign(new Error("workspace writer is active; inspect or cancel its task before launching workers"), { code: "workspace_busy" });
|
|
1033
|
+
return result;
|
|
1034
|
+
};
|
|
1346
1035
|
const daemonCronController = projectRuntime.own("cronController", createDaemonCronController({
|
|
1347
1036
|
projectRoot,
|
|
1348
|
-
dispatch: async ({ taskId, target, message }) => {
|
|
1349
|
-
|
|
1037
|
+
dispatch: async ({ taskId, occurrenceId, target, message }) => {
|
|
1038
|
+
if (target === "ufoo-agent") {
|
|
1039
|
+
mainAgentHost.submit({ requestId: `cron-${require("crypto").createHash("sha256").update(occurrenceId).digest("hex").slice(0, 32)}`,
|
|
1040
|
+
text: message, requestMeta: { source: "cron", scheduleId: taskId, occurrenceId } }, "main-schedules");
|
|
1041
|
+
return;
|
|
1042
|
+
}
|
|
1043
|
+
await mainAgentHost.management.commands.execute({ commandId: `cron-${require("crypto").createHash("sha256").update(`${occurrenceId}:${target}`).digest("hex").slice(0, 32)}`,
|
|
1044
|
+
kind: "cron-delivery", args: { taskId, target, message } }, () => dispatchMessages(projectRoot, [{ target, message }], { strict: true }));
|
|
1350
1045
|
log(`cron:${taskId} -> ${target}`);
|
|
1351
1046
|
},
|
|
1352
1047
|
log,
|
|
@@ -1364,6 +1059,7 @@ function startDaemon({
|
|
|
1364
1059
|
cronTasks: daemonCronController ? daemonCronController.listTasks() : [],
|
|
1365
1060
|
}),
|
|
1366
1061
|
runtime: projectRuntime.status(),
|
|
1062
|
+
agent_runtime: mainAgentHost.snapshot(),
|
|
1367
1063
|
...(globalRuntimeRouter && typeof globalRuntimeRouter.status === "function"
|
|
1368
1064
|
? { global_daemon: globalRuntimeRouter.status() }
|
|
1369
1065
|
: {}),
|
|
@@ -1425,6 +1121,9 @@ function startDaemon({
|
|
|
1425
1121
|
log,
|
|
1426
1122
|
});
|
|
1427
1123
|
publishAgentReportResult(entry);
|
|
1124
|
+
if (isInternalAgentMeta(loadAgentsData(getUfooPaths(projectRoot).agentsFile).agents[entry.agent_id])) {
|
|
1125
|
+
await mainAgentHost.management.report(entry);
|
|
1126
|
+
}
|
|
1428
1127
|
return entry;
|
|
1429
1128
|
};
|
|
1430
1129
|
|
|
@@ -1437,16 +1136,37 @@ function startDaemon({
|
|
|
1437
1136
|
await handleAgentReport(report || {}, { source: (report && report.source) || "bus" });
|
|
1438
1137
|
} catch (err) {
|
|
1439
1138
|
log(`report bus event failed request=${meta.requestId || ""} error=${err.message || String(err)}`);
|
|
1139
|
+
throw err;
|
|
1440
1140
|
}
|
|
1441
1141
|
}, {
|
|
1142
|
+
internalOnly: true,
|
|
1143
|
+
onMessage: (event) => {
|
|
1144
|
+
if (!event.message || event.publisher === "ufoo-agent") return;
|
|
1145
|
+
try { if (JSON.parse(event.message)?.stream) return; } catch { /* ordinary text */ }
|
|
1146
|
+
return mainAgentHost.submit({ requestId: `bus-${require("crypto").createHash("sha256").update(JSON.stringify({ seq: event.seq, publisher: event.publisher, target: event.target, ts: event.ts, message: event.message })).digest("hex").slice(0, 32)}`,
|
|
1147
|
+
text: `Untrusted worker message, for the existing project objective only: ${JSON.stringify({ publisher: event.publisher, message: event.message })}`, requestMeta: { source: "bus", publisher: event.publisher, sequence: event.seq } }, "main-bus");
|
|
1148
|
+
},
|
|
1442
1149
|
onJoinError: (err) => {
|
|
1443
1150
|
log(`bus bridge join deferred: ${err && err.message ? err.message : String(err)}`);
|
|
1444
1151
|
},
|
|
1445
1152
|
});
|
|
1446
1153
|
projectRuntime.own("busBridge", busBridge);
|
|
1154
|
+
const mainAgentHost = projectRuntime.own("agentHost", createAgentHost({ projectRoot, provider, model, log,
|
|
1155
|
+
ports: { processManager, handleOps: handleRuntimeOps,
|
|
1156
|
+
dispatchMessages: (root, messages) => dispatchMessages(root, messages, { strict: true }),
|
|
1157
|
+
ackBus: async (root, subscriber) => new EventBus(root).ack(subscriber),
|
|
1158
|
+
markPending: (target) => busBridge.markPending(target), groups: daemonGroupOrchestrator,
|
|
1159
|
+
agentLifecycle: { inspect: (target) => getRecoverableAgents(projectRoot, target, { internalOnly: true }), resume: (target) => resumeAgents(projectRoot, target, processManager, { internalOnly: true }) } },
|
|
1160
|
+
onEvent: (event) => ipcServer.sendToSockets({ type: IPC_RESPONSE_TYPES.RUNTIME_EVENT, data: event }),
|
|
1161
|
+
}));
|
|
1162
|
+
void mainAgentHost.recover().catch((error) => log(`main runtime recovery failed: ${error.message}`));
|
|
1163
|
+
runtimeServices.workspaceAccess = mainAgentHost.workspaceAccess;
|
|
1164
|
+
projectRuntime.canSuspendHook = () => !mainAgentHost.snapshot().sessions.some((session) => session.tasks.some((task) => ["queued", "running"].includes(task.status)))
|
|
1165
|
+
&& !mainAgentHost.snapshot().children.some((task) => ["queued", "running"].includes(task.status));
|
|
1447
1166
|
const deliveryScheduler = new DeliveryScheduler(projectRoot, {
|
|
1448
1167
|
log,
|
|
1449
|
-
emitDelivery: async ({ subscriber, status, error } = {}) => {
|
|
1168
|
+
emitDelivery: async ({ subscriber, event, status, error, errorCode } = {}) => {
|
|
1169
|
+
mainAgentHost.management.delivery({ subscriber, event, status, errorCode });
|
|
1450
1170
|
if (status === "error") {
|
|
1451
1171
|
log(`delivery failed subscriber=${subscriber || "unknown"} error=${error || "unknown"}`);
|
|
1452
1172
|
}
|
|
@@ -1524,7 +1244,34 @@ function startDaemon({
|
|
|
1524
1244
|
await globalRuntimeRouter.handleRequest(routedProjectRoot, req, socket);
|
|
1525
1245
|
return;
|
|
1526
1246
|
}
|
|
1247
|
+
if (globalRuntimeRouter && sameProjectRoot(projectRoot, globalRuntimeRouter.controllerRoot)) {
|
|
1248
|
+
globalRuntimeRouter.bindControllerClient?.(socket);
|
|
1249
|
+
}
|
|
1527
1250
|
if (await runtimeControlPlane.handleRequest(req, socket)) return;
|
|
1251
|
+
if (req.type === IPC_REQUEST_TYPES.AGENT_RUNTIME) {
|
|
1252
|
+
try {
|
|
1253
|
+
const session = ["cancel", "resume"].includes(req.operation)
|
|
1254
|
+
? mainAgentHost.findRuntime({ sessionId: req.session_id, taskRunId: req.task_run_id, interactionId: req.interaction_id })
|
|
1255
|
+
: ["submit", "events"].includes(req.operation) ? mainAgentHost.resolveRuntime(req.session_id || "main-default") : null;
|
|
1256
|
+
let result;
|
|
1257
|
+
if (req.operation === "submit") result = session.submit({ requestId: req.request_id, text: req.text, attachments: req.attachments || [] });
|
|
1258
|
+
else if (req.operation === "cancel") result = session.cancel({ taskRunId: req.task_run_id, reason: req.reason });
|
|
1259
|
+
else if (req.operation === "resume") result = await session.resume({ interactionId: req.interaction_id, answer: req.answer });
|
|
1260
|
+
else if (req.operation === "events") {
|
|
1261
|
+
const events = session.events({ after: Number(req.after_sequence) || 0 });
|
|
1262
|
+
result = { events: events.slice(0, 1000).map(mainAgentHost.publicEvent), has_more: events.length > 1000,
|
|
1263
|
+
next_sequence: events[Math.min(events.length, 1000) - 1]?.sequence || Number(req.after_sequence) || 0 };
|
|
1264
|
+
}
|
|
1265
|
+
else if (req.operation === "tasks") result = await mainAgentHost.taskPort.execute(req.arguments || {}, { sessionId: req.session_id || "main-default", requestId: req.request_id });
|
|
1266
|
+
else if (req.operation === "status") result = mainAgentHost.snapshot();
|
|
1267
|
+
else if (req.operation === "trace") result = require("../../coordination/history/agentTrace").readAgentTrace(projectRoot, req.arguments || {});
|
|
1268
|
+
else throw new Error("unknown agent_runtime operation");
|
|
1269
|
+
if (!socket.destroyed) socket.write(`${JSON.stringify({ type: IPC_RESPONSE_TYPES.RUNTIME_RESULT, request_id: req.request_id, data: result })}\n`);
|
|
1270
|
+
} catch (error) {
|
|
1271
|
+
if (!socket.destroyed) socket.write(`${JSON.stringify({ type: IPC_RESPONSE_TYPES.ERROR, request_id: req.request_id, error: error.message, code: error.code || "runtime_error" })}\n`);
|
|
1272
|
+
}
|
|
1273
|
+
return;
|
|
1274
|
+
}
|
|
1528
1275
|
if (req.type === IPC_REQUEST_TYPES.MCP_STATUS || req.type === IPC_REQUEST_TYPES.MCP_RESTART) {
|
|
1529
1276
|
if (!isGlobalControllerProjectRoot(projectRoot)) {
|
|
1530
1277
|
socket.write(`${JSON.stringify({
|
|
@@ -1568,32 +1315,18 @@ function startDaemon({
|
|
|
1568
1315
|
return;
|
|
1569
1316
|
}
|
|
1570
1317
|
if (req.type === IPC_REQUEST_TYPES.PROMPT) {
|
|
1318
|
+
req.request_id = req.request_id || require("crypto").randomUUID();
|
|
1319
|
+
const forwardEffect = async (args, invoke) => {
|
|
1320
|
+
const commandId = `project-forward-${require("crypto").createHash("sha256").update(req.request_id).digest("hex").slice(0, 32)}`;
|
|
1321
|
+
const result = await mainAgentHost.management.commands.execute({ commandId, kind: "project-forward", args }, invoke);
|
|
1322
|
+
if (result?.code === "uncertain_effect") throw Object.assign(new Error("project handoff outcome is unknown; inspect its receipt before retrying"), { code: "uncertain_effect" });
|
|
1323
|
+
return result;
|
|
1324
|
+
};
|
|
1571
1325
|
await handlePromptRequest({
|
|
1572
1326
|
projectRoot,
|
|
1573
1327
|
req,
|
|
1574
1328
|
socket,
|
|
1575
|
-
|
|
1576
|
-
model,
|
|
1577
|
-
processManager,
|
|
1578
|
-
runPromptWithAssistant,
|
|
1579
|
-
runUfooAgent,
|
|
1580
|
-
runUfooRouteAgent,
|
|
1581
|
-
dispatchMessages,
|
|
1582
|
-
handleOps: handleRuntimeOps,
|
|
1583
|
-
markPending: (target) => busBridge.markPending(target),
|
|
1584
|
-
reportTaskStatus: async (report) => {
|
|
1585
|
-
await recordAgentReport({
|
|
1586
|
-
projectRoot,
|
|
1587
|
-
report,
|
|
1588
|
-
onStatus: (status) => {
|
|
1589
|
-
ipcServer.sendToSockets({
|
|
1590
|
-
type: IPC_RESPONSE_TYPES.STATUS,
|
|
1591
|
-
data: status,
|
|
1592
|
-
});
|
|
1593
|
-
},
|
|
1594
|
-
log,
|
|
1595
|
-
});
|
|
1596
|
-
},
|
|
1329
|
+
runMainAgent: ({ originalPrompt, requestId, sessionId, requestMeta }) => mainAgentHost.runPrompt({ prompt: originalPrompt, requestId, sessionId, requestMeta }),
|
|
1597
1330
|
forwardProjectPrompt: async ({
|
|
1598
1331
|
targetProjectRoot,
|
|
1599
1332
|
targetProjectName,
|
|
@@ -1605,6 +1338,7 @@ function startDaemon({
|
|
|
1605
1338
|
if (!root) {
|
|
1606
1339
|
return { ok: false, error: "target project root is required" };
|
|
1607
1340
|
}
|
|
1341
|
+
return forwardEffect({ root, prompt, requestMeta }, async () => {
|
|
1608
1342
|
if (!fs.existsSync(root)) {
|
|
1609
1343
|
return { ok: false, error: `target project not found: ${root}` };
|
|
1610
1344
|
}
|
|
@@ -1642,11 +1376,14 @@ function startDaemon({
|
|
|
1642
1376
|
type: IPC_REQUEST_TYPES.PROMPT,
|
|
1643
1377
|
text: String(prompt || ""),
|
|
1644
1378
|
request_meta: nextMeta,
|
|
1379
|
+
request_id: req.request_id || req.message_id || req.id,
|
|
1380
|
+
session_id: req.session_id,
|
|
1645
1381
|
};
|
|
1646
1382
|
if (globalRuntimeRouter && typeof globalRuntimeRouter.request === "function") {
|
|
1647
1383
|
return globalRuntimeRouter.request(root, routedRequest, { timeoutMs: 12000 });
|
|
1648
1384
|
}
|
|
1649
1385
|
return sendPromptRequestToProject(root, routedRequest);
|
|
1386
|
+
});
|
|
1650
1387
|
},
|
|
1651
1388
|
log,
|
|
1652
1389
|
});
|
|
@@ -1685,6 +1422,7 @@ function startDaemon({
|
|
|
1685
1422
|
`${JSON.stringify({
|
|
1686
1423
|
type: IPC_RESPONSE_TYPES.ERROR,
|
|
1687
1424
|
error: "bus_send requires target and message",
|
|
1425
|
+
...(req.request_id ? { request_id: req.request_id } : {}),
|
|
1688
1426
|
})}
|
|
1689
1427
|
`,
|
|
1690
1428
|
);
|
|
@@ -1709,6 +1447,7 @@ function startDaemon({
|
|
|
1709
1447
|
socket.write(
|
|
1710
1448
|
`${JSON.stringify({
|
|
1711
1449
|
type: IPC_RESPONSE_TYPES.BUS_SEND_OK,
|
|
1450
|
+
...(req.request_id ? { request_id: req.request_id } : {}),
|
|
1712
1451
|
})}
|
|
1713
1452
|
`,
|
|
1714
1453
|
);
|
|
@@ -1718,6 +1457,7 @@ function startDaemon({
|
|
|
1718
1457
|
`${JSON.stringify({
|
|
1719
1458
|
type: IPC_RESPONSE_TYPES.ERROR,
|
|
1720
1459
|
error: err.message || "bus_send failed",
|
|
1460
|
+
...(req.request_id ? { request_id: req.request_id } : {}),
|
|
1721
1461
|
})}
|
|
1722
1462
|
`,
|
|
1723
1463
|
);
|
|
@@ -1885,6 +1625,7 @@ function startDaemon({
|
|
|
1885
1625
|
}
|
|
1886
1626
|
const op = {
|
|
1887
1627
|
action: "launch",
|
|
1628
|
+
internal_only: req.internal_only === true,
|
|
1888
1629
|
agent: normalizedAgent,
|
|
1889
1630
|
count: finalCount,
|
|
1890
1631
|
nickname: explicitNickname,
|
|
@@ -1903,9 +1644,9 @@ function startDaemon({
|
|
|
1903
1644
|
: null,
|
|
1904
1645
|
};
|
|
1905
1646
|
let soloLaunchBootstrap = null;
|
|
1906
|
-
if (requestedProfile && (normalizedAgent === "
|
|
1907
|
-
const agentTypeMap = {
|
|
1908
|
-
const defaultNickMap = {
|
|
1647
|
+
if (requestedProfile && (normalizedAgent === "ucode" || normalizedAgent === "claude" || normalizedAgent === "codex" || normalizedAgent === "agy" || normalizedAgent === "grok" || normalizedAgent === "kimi")) {
|
|
1648
|
+
const agentTypeMap = { ucode: "ucode", claude: "claude-code", codex: "codex", agy: "agy", grok: "grok", kimi: "kimi" };
|
|
1649
|
+
const defaultNickMap = { ucode: "ucode", claude: "claude", codex: "codex", agy: "agy", grok: "grok", kimi: "kimi" };
|
|
1909
1650
|
const agentTypeForBootstrap = agentTypeMap[normalizedAgent];
|
|
1910
1651
|
const soloNickname = explicitNickname || defaultNickMap[normalizedAgent];
|
|
1911
1652
|
const profileResult = resolveSoloPromptProfile(projectRoot, requestedProfile);
|
|
@@ -1927,7 +1668,7 @@ function startDaemon({
|
|
|
1927
1668
|
});
|
|
1928
1669
|
if (built.required) {
|
|
1929
1670
|
try {
|
|
1930
|
-
if (normalizedAgent === "
|
|
1671
|
+
if (normalizedAgent === "ucode") {
|
|
1931
1672
|
// ucode: bootstrap via env var pointing to file
|
|
1932
1673
|
const prepared = prepareSoloUcodeBootstrap(projectRoot, soloNickname, built.promptText);
|
|
1933
1674
|
op.extra_env = {
|
|
@@ -2169,6 +1910,7 @@ function startDaemon({
|
|
|
2169
1910
|
const result = await daemonGroupOrchestrator.runGroup({
|
|
2170
1911
|
alias,
|
|
2171
1912
|
instance,
|
|
1913
|
+
internal_only: req.internal_only === true,
|
|
2172
1914
|
dry_run: dryRun,
|
|
2173
1915
|
host_inject_sock: hostInjectSock,
|
|
2174
1916
|
host_daemon_sock: hostDaemonSock,
|
|
@@ -2486,7 +2228,7 @@ function startDaemon({
|
|
|
2486
2228
|
if (req.type === IPC_REQUEST_TYPES.RESUME_AGENTS) {
|
|
2487
2229
|
const target = req.target || "";
|
|
2488
2230
|
try {
|
|
2489
|
-
const result = await resumeAgents(projectRoot, target, processManager);
|
|
2231
|
+
const result = await resumeAgents(projectRoot, target, processManager, { internalOnly: req.internal_only === true });
|
|
2490
2232
|
const resumedCount = result.resumed.length;
|
|
2491
2233
|
const skippedCount = result.skipped.length;
|
|
2492
2234
|
const reply = resumedCount > 0
|
|
@@ -2516,7 +2258,7 @@ function startDaemon({
|
|
|
2516
2258
|
if (req.type === IPC_REQUEST_TYPES.LIST_RECOVERABLE_AGENTS) {
|
|
2517
2259
|
const target = req.target || "";
|
|
2518
2260
|
try {
|
|
2519
|
-
const result = getRecoverableAgents(projectRoot, target);
|
|
2261
|
+
const result = getRecoverableAgents(projectRoot, target, { internalOnly: req.internal_only === true });
|
|
2520
2262
|
const count = result.recoverable.length;
|
|
2521
2263
|
const reply = count > 0 ? `Found ${count} recoverable agent(s)` : "No recoverable agents";
|
|
2522
2264
|
socket.write(
|
|
@@ -2606,6 +2348,7 @@ function startDaemon({
|
|
|
2606
2348
|
type: IPC_RESPONSE_TYPES.REGISTER_OK,
|
|
2607
2349
|
subscriberId,
|
|
2608
2350
|
nickname: resolvedNickname,
|
|
2351
|
+
agentHandle: result.agent_handle,
|
|
2609
2352
|
})}
|
|
2610
2353
|
`,
|
|
2611
2354
|
);
|
|
@@ -2641,11 +2384,12 @@ function startDaemon({
|
|
|
2641
2384
|
const resolved = resolveSessionFromFile(agentType, {
|
|
2642
2385
|
pid: parsedAgentPid,
|
|
2643
2386
|
cwd: projectRoot,
|
|
2387
|
+
projectRoot,
|
|
2388
|
+
subscriberId,
|
|
2644
2389
|
});
|
|
2645
|
-
if (resolved && resolved.sessionId) {
|
|
2390
|
+
if (resolved && resolved.sessionId && persistProviderSession(projectRoot, subscriberId, resolved)) {
|
|
2646
2391
|
const attemptNote = attempt > 1 ? ` (attempt ${attempt})` : "";
|
|
2647
2392
|
log(`agent_ready session resolved from file for ${subscriberId}: ${resolved.sessionId}${attemptNote}`);
|
|
2648
|
-
persistProviderSession(projectRoot, subscriberId, resolved);
|
|
2649
2393
|
if (providerSessions) {
|
|
2650
2394
|
providerSessions.set(subscriberId, {
|
|
2651
2395
|
sessionId: resolved.sessionId,
|
|
@@ -2719,7 +2463,7 @@ function startDaemon({
|
|
|
2719
2463
|
const runnerProcesses = [];
|
|
2720
2464
|
|
|
2721
2465
|
for (const line of lines) {
|
|
2722
|
-
if (line.includes("agent-
|
|
2466
|
+
if (line.includes("agent-runner")) {
|
|
2723
2467
|
const parts = line.trim().split(/\s+/);
|
|
2724
2468
|
if (parts.length >= 2) {
|
|
2725
2469
|
const pid = parseInt(parts[1], 10);
|
|
@@ -2837,6 +2581,7 @@ function startDaemon({
|
|
|
2837
2581
|
if (daemonCronController) {
|
|
2838
2582
|
daemonCronController.stopAll();
|
|
2839
2583
|
}
|
|
2584
|
+
void mainAgentHost.close().catch((error) => writeLog(`main runtime shutdown failed: ${error.message}`));
|
|
2840
2585
|
|
|
2841
2586
|
runtimeControlPlane.stop();
|
|
2842
2587
|
if (mcpHttpServer) {
|