u-foo 4.0.0 → 4.0.2
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 +90 -30
- package/README.zh-CN.md +76 -26
- package/SKILLS/ufoo/SKILL.md +25 -1
- package/SKILLS/ufoo-bus/SKILL.md +25 -2
- package/bin/ucode.js +4 -0
- package/bin/ufoo.js +3 -3
- 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 +6 -6
- package/src/agents/activity/activityDetector.js +1 -1
- package/src/agents/capabilities/agentManagement/service.js +40 -13
- package/src/agents/capabilities/coordination/index.js +1 -1
- package/src/agents/capabilities/taskReports/index.js +1 -1
- package/src/agents/controller/controllerToolExecutor.js +36 -20
- package/src/agents/controller/index.js +1 -6
- package/src/agents/internal/internalRunner.js +90 -56
- package/src/agents/launch/index.js +0 -1
- package/src/agents/launch/launcher.js +38 -29
- package/src/agents/launch/launcherReadiness.js +35 -0
- package/src/agents/launch/notifier.js +2 -2
- package/src/agents/launch/ptyWrapper.js +5 -2
- package/src/agents/launch/readyDetector.js +2 -2
- package/src/agents/profiles/main.js +1 -1
- package/src/agents/prompts/cooperation.js +14 -0
- package/src/agents/prompts/defaultBootstrap.js +1 -1
- package/src/agents/prompts/groupBootstrap.js +23 -7
- package/src/agents/prompts/mainAgent.js +11 -2
- package/src/agents/prompts/native/system.js +4 -1
- package/src/agents/prompts/native/ufoo.js +11 -3
- package/src/agents/prompts/promptProfiles.js +2 -0
- package/src/agents/prompts/reporting.js +16 -0
- package/src/agents/providers/claudeEventTranslator.js +3 -0
- package/src/agents/providers/claudeThreadProvider.js +3 -2
- package/src/agents/providers/clientIdentity.js +75 -0
- package/src/agents/providers/codexEventTranslator.js +23 -2
- package/src/agents/providers/codexThreadProvider.js +15 -6
- 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 +58 -40
- package/src/agents/providers/runtimeConfig.js +10 -8
- package/src/agents/providers/thinkingLevels.js +10 -36
- package/src/agents/providers/transports/responsesProtocol.js +11 -6
- package/src/agents/providers/upstreamRuntime.js +135 -0
- package/src/agents/runtime/context/checkpointStore.js +38 -0
- package/src/agents/runtime/context/compaction.js +151 -0
- package/src/agents/runtime/context/contextBudget.js +93 -0
- package/src/agents/runtime/context/replayHistory.js +28 -0
- package/src/agents/runtime/context/runtimeStore.js +2 -1
- package/src/agents/runtime/core/agentLoop.js +99 -18
- package/src/agents/runtime/core/executionBudget.js +36 -0
- package/src/agents/runtime/createAgentRuntime.js +16 -4
- package/src/agents/runtime/prompts/responseLanguage.js +12 -0
- package/src/agents/runtime/protocol/runObservation.js +24 -0
- package/src/agents/runtime/protocol/traceContext.js +9 -0
- package/src/app/chat/ChatController.js +26 -53
- package/src/app/chat/agentDirectory.js +1 -16
- package/src/app/chat/bootstrap.js +1 -2
- package/src/app/chat/commandExecutor.js +93 -285
- package/src/app/chat/commands.js +32 -45
- package/src/app/chat/daemonMessageRouter.js +0 -45
- package/src/app/chat/historyStore.js +12 -48
- package/src/app/chat/inputSubmitHandler.js +7 -33
- package/src/app/chat/internalAgentLogHistory.js +19 -60
- package/src/app/chat/multiWindow/paneManager.js +10 -3
- package/src/app/chat/streamState.js +0 -4
- package/src/app/cli/features/init.js +5 -5
- package/src/app/cli/metricsCommands.js +68 -0
- package/src/app/cli/run.js +42 -3
- package/src/app/cli/traceCommands.js +36 -0
- package/src/code/agent.js +18 -13
- package/src/code/busConsumer.js +1 -3
- package/src/code/commands.js +1 -1
- package/src/code/context/assembler.js +7 -8
- package/src/code/context/stateCommit.js +2 -1
- package/src/code/internalThread.js +10 -3
- 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 +55 -17
- package/src/code/providers/modelsCatalog.js +1 -317
- package/src/code/repl.js +4 -11
- package/src/code/sessionStore.js +3 -3
- package/src/code/taskDecomposer.js +18 -0
- package/src/code/toolBudget.js +1 -17
- package/src/config.js +22 -72
- package/src/coordination/bus/index.js +12 -3
- package/src/coordination/bus/inject.js +2 -2
- package/src/coordination/bus/message.js +15 -1
- package/src/coordination/bus/nickname.js +1 -1
- package/src/coordination/bus/subscriber.js +3 -2
- package/src/coordination/bus/utils.js +1 -1
- package/src/coordination/history/agentPerformance.js +279 -0
- package/src/coordination/history/agentTrace.js +249 -0
- package/src/coordination/state/workspaceAccess.js +1 -1
- 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 +11 -2
- package/src/runtime/contracts/uiProtocol.js +18 -19
- package/src/runtime/daemon/agentHost.js +57 -25
- package/src/runtime/daemon/agentRecoveryGroups.js +70 -0
- package/src/runtime/daemon/busBridge.js +1 -1
- package/src/runtime/daemon/controlPlaneService.js +1 -1
- package/src/runtime/daemon/groupOrchestrator.js +20 -5
- package/src/runtime/daemon/groupStatus.js +20 -0
- package/src/runtime/daemon/index.js +19 -44
- package/src/runtime/daemon/mcpHttpServer.js +39 -10
- package/src/runtime/daemon/mcpServer.js +1 -1
- package/src/runtime/daemon/nicknameScope.js +1 -1
- package/src/runtime/daemon/ops.js +46 -23
- package/src/runtime/daemon/projectRuntimeGateway.js +2 -0
- package/src/runtime/daemon/promptRequest.js +92 -589
- package/src/runtime/daemon/status.js +5 -6
- package/src/tools/controllerSchemas.js +6 -2
- package/src/tools/handlers/tier2.js +4 -2
- package/src/tools/registry.js +4 -0
- package/src/tools/schemaFixtures.js +26 -2
- package/src/tools/tier0/readAgentMetrics.js +14 -0
- package/src/tools/tier0/readAgentTrace.js +12 -0
- package/src/ui/agentSettingsController.js +107 -0
- package/src/ui/agentSurface.js +1 -1
- package/src/ui/dashboardBridge.js +1 -1
- package/src/ui/format/banner.js +1 -4
- 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/rustChatHost.js +191 -206
- package/src/ui/rustMultiSession.js +77 -36
- package/src/ui/rustUcodeHost.js +6 -1
- package/src/ui/settingsBridge.js +33 -16
- 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 -336
- package/src/agents/controller/ufooAgent.js +0 -830
- package/src/agents/launch/ptyRunner.js +0 -1135
- package/src/agents/providers/controllerJsonTransport.js +0 -28
- package/src/agents/providers/upstreamTransport.js +0 -698
- package/src/app/chat/agentBar.js +0 -135
- package/src/app/chat/agentSockets.js +0 -225
- package/src/app/chat/dashboardView.js +0 -405
- package/src/app/chat/rawKeyMap.js +0 -42
- package/src/app/chat/settingsController.js +0 -111
- package/src/coordination/history/controllerSummary.js +0 -23
- 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/executionPaths.js +0 -30
- package/src/runtime/daemon/promptLoop.js +0 -136
- package/src/runtime/privacy/shadowDiff.js +0 -174
|
@@ -6,625 +6,128 @@ const {
|
|
|
6
6
|
consumeControllerInboxEntries,
|
|
7
7
|
} = require("../../coordination/report/store");
|
|
8
8
|
const { isGlobalControllerProjectRoot } = require("../projects");
|
|
9
|
-
const {
|
|
10
|
-
resolveGateRouterConfig,
|
|
11
|
-
shouldUseGateRouter,
|
|
12
|
-
} = require("../../orchestration/controller/gateRouter");
|
|
13
|
-
const {
|
|
14
|
-
CONTROLLER_MODES,
|
|
15
|
-
applyControllerModeForMessage,
|
|
16
|
-
resolveControllerMode,
|
|
17
|
-
} = require("../../orchestration/controller/flags");
|
|
18
|
-
const {
|
|
19
|
-
resolveLoopRuntimeOptions,
|
|
20
|
-
runPromptWithControllerLoop,
|
|
21
|
-
} = require("../../agents/controller/loopRuntime");
|
|
22
|
-
const {
|
|
23
|
-
appendShadowDiff,
|
|
24
|
-
createLoopObserver,
|
|
25
|
-
} = require("../../agents/controller/loopObservability");
|
|
26
|
-
const {
|
|
27
|
-
DEFAULT_SHADOW_SAMPLING_RATE,
|
|
28
|
-
createShadowBudgetBreaker,
|
|
29
|
-
createShadowGuard,
|
|
30
|
-
shouldSampleShadow,
|
|
31
|
-
} = require("../../orchestration/controller/shadowGuard");
|
|
32
9
|
|
|
33
10
|
function normalizeProjectRoute(route) {
|
|
34
11
|
if (!route || typeof route !== "object") return null;
|
|
35
|
-
const projectRoot = String(route.project_root ||
|
|
12
|
+
const projectRoot = String(route.project_root || "").trim();
|
|
36
13
|
if (!projectRoot) return null;
|
|
37
14
|
return {
|
|
38
15
|
project_root: projectRoot,
|
|
39
|
-
project_name: String(route.project_name ||
|
|
40
|
-
prompt: String(route.prompt ||
|
|
16
|
+
project_name: String(route.project_name || "").trim(),
|
|
17
|
+
prompt: String(route.prompt || "").trim(),
|
|
41
18
|
reason: String(route.reason || "").trim(),
|
|
42
19
|
};
|
|
43
20
|
}
|
|
44
21
|
|
|
45
|
-
function buildRoutedProjectPayload(
|
|
46
|
-
const payload = basePayload && typeof basePayload === "object" ? { ...basePayload } : {};
|
|
47
|
-
payload.routed_project = {
|
|
48
|
-
project_root: String(route.project_root || ""),
|
|
49
|
-
project_name: String(route.project_name || ""),
|
|
50
|
-
reason: String(route.reason || ""),
|
|
51
|
-
};
|
|
52
|
-
return payload;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function buildPromptWithPrivateReports(prompt = "", reports = [], requestMeta = {}) {
|
|
56
|
-
const meta = requestMeta && typeof requestMeta === "object" ? requestMeta : {};
|
|
57
|
-
const hasMeta = Object.keys(meta).length > 0;
|
|
58
|
-
if (!Array.isArray(reports) || reports.length === 0) {
|
|
59
|
-
if (!hasMeta) return prompt;
|
|
60
|
-
const lines = [];
|
|
61
|
-
lines.push(prompt || "");
|
|
62
|
-
lines.push("");
|
|
63
|
-
lines.push("Routing request metadata (JSON):");
|
|
64
|
-
lines.push(JSON.stringify(meta, null, 2));
|
|
65
|
-
lines.push("");
|
|
66
|
-
lines.push("Honor this metadata when choosing dispatch targets and injection_mode.");
|
|
67
|
-
return lines.join("\n");
|
|
68
|
-
}
|
|
69
|
-
const lines = [];
|
|
70
|
-
lines.push(prompt || "");
|
|
71
|
-
lines.push("");
|
|
72
|
-
if (hasMeta) {
|
|
73
|
-
lines.push("Routing request metadata (JSON):");
|
|
74
|
-
lines.push(JSON.stringify(meta, null, 2));
|
|
75
|
-
lines.push("");
|
|
76
|
-
lines.push("Honor this metadata when choosing dispatch targets and injection_mode.");
|
|
77
|
-
lines.push("");
|
|
78
|
-
}
|
|
79
|
-
lines.push("Private runtime reports for ufoo-agent (JSON):");
|
|
80
|
-
lines.push(JSON.stringify(reports, null, 2));
|
|
81
|
-
lines.push("");
|
|
82
|
-
lines.push("Use these runtime reports when deciding reply/dispatch/ops.");
|
|
83
|
-
lines.push("Treat them as control-plane observability, not automatic downstream delivery instructions.");
|
|
84
|
-
lines.push("If report.meta.handoff.status is \"delivered\" and report.meta.needs_dispatch is not true, do not dispatch that handoff again.");
|
|
85
|
-
lines.push("Only treat a report as a controller dispatch request when report.meta.needs_dispatch is true.");
|
|
86
|
-
return lines.join("\n");
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function normalizeMessageId(req = {}) {
|
|
90
|
-
return String(req.request_id || req.message_id || req.msg_id || req.id || "").trim();
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function summarizeShadowPayload(payload = {}) {
|
|
94
|
-
if (!payload || typeof payload !== "object") {
|
|
95
|
-
return {
|
|
96
|
-
reply_present: false,
|
|
97
|
-
dispatch_count: 0,
|
|
98
|
-
ops_count: 0,
|
|
99
|
-
terminal_reason: "",
|
|
100
|
-
};
|
|
101
|
-
}
|
|
22
|
+
function buildRoutedProjectPayload(payload = {}, route = {}) {
|
|
102
23
|
return {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
24
|
+
...payload,
|
|
25
|
+
routed_project: {
|
|
26
|
+
project_root: String(route.project_root || ""),
|
|
27
|
+
project_name: String(route.project_name || ""),
|
|
28
|
+
reason: String(route.reason || ""),
|
|
29
|
+
},
|
|
109
30
|
};
|
|
110
31
|
}
|
|
111
32
|
|
|
112
|
-
async function handlePromptRequest(
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
log = () => {},
|
|
134
|
-
} = options;
|
|
135
|
-
|
|
136
|
-
const req = originalReq && typeof originalReq === "object" ? { ...originalReq } : {};
|
|
137
|
-
if ((req.text == null || req.text === "") && req.prompt != null) {
|
|
138
|
-
req.text = req.prompt;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
log(`prompt ${String(req.text || "").slice(0, 200)}`);
|
|
142
|
-
const requestMeta = req.request_meta && typeof req.request_meta === "object" ? req.request_meta : {};
|
|
143
|
-
const messageId = normalizeMessageId(req);
|
|
144
|
-
const requestedControllerMode = String(
|
|
145
|
-
requestMeta.controller_mode || requestMeta.agent_execution_path || ""
|
|
146
|
-
).trim();
|
|
147
|
-
let controllerMode = resolveControllerMode({
|
|
148
|
-
projectRoot,
|
|
149
|
-
requestedMode: requestedControllerMode,
|
|
150
|
-
});
|
|
151
|
-
const isGlobalController = isGlobalControllerProjectRoot(projectRoot);
|
|
152
|
-
const forcedProjectRoot = String(requestMeta.force_project_root || "").trim();
|
|
153
|
-
const resolvedLoopRuntime = resolveLoopRuntimeOptions();
|
|
154
|
-
if (controllerMode === CONTROLLER_MODES.LEGACY && resolvedLoopRuntime.enabled) {
|
|
155
|
-
controllerMode = CONTROLLER_MODES.LOOP;
|
|
156
|
-
}
|
|
157
|
-
if (selectExecutionPath) {
|
|
158
|
-
try { controllerMode = selectExecutionPath({ sessionId: req.session_id || requestMeta.session_id || "main-default", requestedMode: requestedControllerMode, mode: controllerMode }); }
|
|
159
|
-
catch (error) { socket.write(`${JSON.stringify({ type: IPC_RESPONSE_TYPES.ERROR, error: error.message, code: error.code })}\n`); return false; }
|
|
160
|
-
}
|
|
161
|
-
const controllerObserver = createLoopObserver({
|
|
162
|
-
projectRoot,
|
|
163
|
-
enabled: true,
|
|
164
|
-
defaults: {
|
|
165
|
-
controller_mode: controllerMode,
|
|
166
|
-
},
|
|
167
|
-
});
|
|
168
|
-
const appliedControllerMode = applyControllerModeForMessage({
|
|
169
|
-
projectRoot,
|
|
170
|
-
nextMode: controllerMode,
|
|
171
|
-
messageId,
|
|
172
|
-
});
|
|
173
|
-
if (appliedControllerMode.transition) {
|
|
174
|
-
controllerObserver.emit("controller.flag.transition", appliedControllerMode.transition);
|
|
175
|
-
}
|
|
176
|
-
const loopRuntime = {
|
|
177
|
-
...resolvedLoopRuntime,
|
|
178
|
-
enabled: controllerMode === CONTROLLER_MODES.LOOP,
|
|
33
|
+
async function handlePromptRequest({
|
|
34
|
+
projectRoot,
|
|
35
|
+
req = {},
|
|
36
|
+
socket,
|
|
37
|
+
runMainAgent,
|
|
38
|
+
forwardProjectPrompt,
|
|
39
|
+
log = () => {},
|
|
40
|
+
} = {}) {
|
|
41
|
+
const text = String(req.text ?? req.prompt ?? "");
|
|
42
|
+
const requestMeta = req.request_meta && typeof req.request_meta === "object" && !Array.isArray(req.request_meta)
|
|
43
|
+
? req.request_meta : {};
|
|
44
|
+
const requestId = String(req.request_id || req.message_id || req.msg_id || req.id || "").trim() || undefined;
|
|
45
|
+
const sessionId = req.session_id || requestMeta.session_id || "main-default";
|
|
46
|
+
const global = isGlobalControllerProjectRoot(projectRoot);
|
|
47
|
+
const respond = (payload, opsResults = []) => {
|
|
48
|
+
socket.write(`${JSON.stringify({
|
|
49
|
+
type: IPC_RESPONSE_TYPES.RESPONSE,
|
|
50
|
+
data: payload,
|
|
51
|
+
opsResults,
|
|
52
|
+
})}\n`);
|
|
53
|
+
return true;
|
|
179
54
|
};
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
if (isGlobalController && forcedProjectRoot) {
|
|
184
|
-
try {
|
|
185
|
-
const routed = await Promise.resolve(forwardProjectPrompt({
|
|
186
|
-
targetProjectRoot: forcedProjectRoot,
|
|
187
|
-
targetProjectName: String(requestMeta.force_project_name || "").trim(),
|
|
188
|
-
prompt: req.text || "",
|
|
189
|
-
routeReason: "forced_project_root",
|
|
190
|
-
requestMeta,
|
|
191
|
-
}));
|
|
192
|
-
if (!routed || routed.ok !== true) {
|
|
193
|
-
const error = routed && routed.error ? routed.error : "project forward failed";
|
|
194
|
-
socket.write(
|
|
195
|
-
`${JSON.stringify({
|
|
196
|
-
type: IPC_RESPONSE_TYPES.ERROR,
|
|
197
|
-
error,
|
|
198
|
-
})}\n`,
|
|
199
|
-
);
|
|
200
|
-
return false;
|
|
201
|
-
}
|
|
202
|
-
const payload = buildRoutedProjectPayload(routed.payload || {}, {
|
|
203
|
-
project_root: routed.project_root || forcedProjectRoot,
|
|
204
|
-
project_name: routed.project_name || String(requestMeta.force_project_name || "").trim(),
|
|
205
|
-
reason: "forced_project_root",
|
|
206
|
-
});
|
|
207
|
-
socket.write(
|
|
208
|
-
`${JSON.stringify({
|
|
209
|
-
type: IPC_RESPONSE_TYPES.RESPONSE,
|
|
210
|
-
data: payload,
|
|
211
|
-
opsResults: routed.opsResults || [],
|
|
212
|
-
})}\n`,
|
|
213
|
-
);
|
|
214
|
-
return true;
|
|
215
|
-
} catch (err) {
|
|
216
|
-
socket.write(
|
|
217
|
-
`${JSON.stringify({
|
|
218
|
-
type: IPC_RESPONSE_TYPES.ERROR,
|
|
219
|
-
error: err.message || String(err),
|
|
220
|
-
})}\n`,
|
|
221
|
-
);
|
|
222
|
-
return false;
|
|
55
|
+
const forward = async (route) => {
|
|
56
|
+
if (typeof forwardProjectPrompt !== "function") {
|
|
57
|
+
throw new Error("project forwarding is unavailable");
|
|
223
58
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
let nextRequestMeta = requestMeta;
|
|
230
|
-
const hasExplicitRequestMeta = Object.keys(nextRequestMeta).length > 0;
|
|
231
|
-
if (hasExplicitRequestMeta && !Object.prototype.hasOwnProperty.call(nextRequestMeta, "agent_execution_path") && controllerMode !== CONTROLLER_MODES.LEGACY) {
|
|
232
|
-
nextRequestMeta = {
|
|
233
|
-
...nextRequestMeta,
|
|
234
|
-
agent_execution_path: controllerMode,
|
|
235
|
-
};
|
|
236
|
-
}
|
|
237
|
-
let forceMainRouterFallback = false;
|
|
238
|
-
|
|
239
|
-
const logGateRouterEvent = (event, details = {}) => {
|
|
240
|
-
controllerObserver.emit(event, details);
|
|
241
|
-
if (typeof log !== "function") return;
|
|
242
|
-
log(`event ${JSON.stringify({ event, ...details })}`);
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
const attachGateRouterMeta = (reason, detail = {}) => {
|
|
246
|
-
nextRequestMeta = {
|
|
247
|
-
...nextRequestMeta,
|
|
248
|
-
gate_router: {
|
|
249
|
-
attempted: true,
|
|
250
|
-
reason,
|
|
251
|
-
...detail,
|
|
252
|
-
},
|
|
253
|
-
};
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
controllerObserver.emit("controller.prompt_path_selected", {
|
|
257
|
-
applied_from_msg_id: messageId,
|
|
258
|
-
shadow_enabled: shadowEnabled,
|
|
259
|
-
loop_enabled: loopRuntime.enabled,
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
const gateRouterEligibility = shouldUseGateRouter({
|
|
263
|
-
projectRoot,
|
|
264
|
-
prompt: req.text || "",
|
|
265
|
-
requestMeta: nextRequestMeta,
|
|
266
|
-
});
|
|
267
|
-
const gateRouterConfig = gateRouterEligibility.enabled
|
|
268
|
-
? resolveGateRouterConfig({
|
|
269
|
-
projectRoot,
|
|
59
|
+
const routed = await forwardProjectPrompt({
|
|
60
|
+
targetProjectRoot: route.project_root,
|
|
61
|
+
targetProjectName: route.project_name,
|
|
62
|
+
prompt: route.prompt || text,
|
|
63
|
+
routeReason: route.reason,
|
|
270
64
|
requestMeta,
|
|
271
|
-
})
|
|
272
|
-
: null;
|
|
273
|
-
|
|
274
|
-
if (!useNativeMain && !useGlobalProjectRouter && gateRouterEligibility.enabled && typeof runUfooRouteAgent === "function") {
|
|
275
|
-
logGateRouterEvent("controller.gate_router_attempted", {
|
|
276
|
-
flag: gateRouterEligibility.executionPath,
|
|
277
|
-
intent_reason: gateRouterEligibility.intent.reason,
|
|
278
65
|
});
|
|
279
|
-
|
|
280
|
-
const routed = await runUfooRouteAgent({
|
|
281
|
-
projectRoot,
|
|
282
|
-
prompt: req.text || "",
|
|
283
|
-
provider: gateRouterConfig.provider,
|
|
284
|
-
model: gateRouterConfig.model,
|
|
285
|
-
timeoutMs: gateRouterConfig.timeoutMs,
|
|
286
|
-
});
|
|
287
|
-
|
|
288
66
|
if (!routed || routed.ok !== true) {
|
|
289
|
-
|
|
290
|
-
error: routed && routed.error ? routed.error : "route_agent_failed",
|
|
291
|
-
});
|
|
292
|
-
forceMainRouterFallback = true;
|
|
293
|
-
logGateRouterEvent("controller.gate_router_upgraded", {
|
|
294
|
-
reason: "provider_error",
|
|
295
|
-
fallback_used: "main_router",
|
|
296
|
-
});
|
|
297
|
-
} else {
|
|
298
|
-
const route = routed.route || {};
|
|
299
|
-
const canDispatch = route.decision === "direct_dispatch"
|
|
300
|
-
&& route.target && route.target !== "unknown"
|
|
301
|
-
&& route.confidence >= gateRouterConfig.confidenceThreshold;
|
|
302
|
-
|
|
303
|
-
if (!canDispatch) {
|
|
304
|
-
const upgradeReason = route.decision && route.decision !== "direct_dispatch"
|
|
305
|
-
? route.decision
|
|
306
|
-
: "low_confidence";
|
|
307
|
-
attachGateRouterMeta(upgradeReason, {
|
|
308
|
-
decision: route.decision || "",
|
|
309
|
-
target: route.target || "unknown",
|
|
310
|
-
confidence: Number(route.confidence || 0),
|
|
311
|
-
route_reason: route.reason || "",
|
|
312
|
-
});
|
|
313
|
-
forceMainRouterFallback = true;
|
|
314
|
-
logGateRouterEvent("controller.gate_router_upgraded", {
|
|
315
|
-
reason: upgradeReason,
|
|
316
|
-
decision: route.decision || "",
|
|
317
|
-
target: route.target || "unknown",
|
|
318
|
-
confidence: Number(route.confidence || 0),
|
|
319
|
-
fallback_used: "main_router",
|
|
320
|
-
});
|
|
321
|
-
} else {
|
|
322
|
-
const payload = {
|
|
323
|
-
reply: "",
|
|
324
|
-
dispatch: [{
|
|
325
|
-
target: route.target,
|
|
326
|
-
message: route.message || req.text || "",
|
|
327
|
-
injection_mode: route.injection_mode || "immediate",
|
|
328
|
-
source: "ufoo-agent-gate-router",
|
|
329
|
-
}],
|
|
330
|
-
ops: [],
|
|
331
|
-
};
|
|
332
|
-
try {
|
|
333
|
-
markPending(route.target);
|
|
334
|
-
await dispatchMessages(projectRoot, payload.dispatch);
|
|
335
|
-
consumeControllerInboxEntries(projectRoot, "ufoo-agent", privateReports);
|
|
336
|
-
logGateRouterEvent("controller.gate_router_completed", {
|
|
337
|
-
target: route.target,
|
|
338
|
-
confidence: Number(route.confidence || 0),
|
|
339
|
-
provider: routed.meta && routed.meta.provider ? routed.meta.provider : "",
|
|
340
|
-
model: routed.meta && routed.meta.model ? routed.meta.model : "",
|
|
341
|
-
fallback_used: "none",
|
|
342
|
-
});
|
|
343
|
-
socket.write(
|
|
344
|
-
`${JSON.stringify({
|
|
345
|
-
type: IPC_RESPONSE_TYPES.RESPONSE,
|
|
346
|
-
data: payload,
|
|
347
|
-
opsResults: [],
|
|
348
|
-
})}\n`,
|
|
349
|
-
);
|
|
350
|
-
return true;
|
|
351
|
-
} catch (err) {
|
|
352
|
-
if (err.code === "uncertain_effect") {
|
|
353
|
-
socket.write(`${JSON.stringify({ type: IPC_RESPONSE_TYPES.ERROR, error: err.message, code: err.code })}\n`);
|
|
354
|
-
return false;
|
|
355
|
-
}
|
|
356
|
-
attachGateRouterMeta("dispatch_failed", {
|
|
357
|
-
target: route.target,
|
|
358
|
-
confidence: Number(route.confidence || 0),
|
|
359
|
-
route_reason: route.reason || "",
|
|
360
|
-
error: err && err.message ? err.message : String(err),
|
|
361
|
-
});
|
|
362
|
-
forceMainRouterFallback = true;
|
|
363
|
-
logGateRouterEvent("controller.gate_router_upgraded", {
|
|
364
|
-
reason: "dispatch_failed",
|
|
365
|
-
target: route.target,
|
|
366
|
-
confidence: Number(route.confidence || 0),
|
|
367
|
-
fallback_used: "main_router",
|
|
368
|
-
});
|
|
369
|
-
}
|
|
370
|
-
}
|
|
67
|
+
throw new Error(routed?.error || "project forward failed");
|
|
371
68
|
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
69
|
+
return {
|
|
70
|
+
payload: buildRoutedProjectPayload(routed.payload || {}, {
|
|
71
|
+
project_root: routed.project_root || route.project_root,
|
|
72
|
+
project_name: routed.project_name || route.project_name,
|
|
73
|
+
reason: route.reason,
|
|
74
|
+
}),
|
|
75
|
+
opsResults: routed.opsResults || [],
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
log(`prompt ${text.slice(0, 200)}`);
|
|
381
79
|
try {
|
|
382
|
-
|
|
80
|
+
if (global && requestMeta.force_project_root) {
|
|
81
|
+
const routed = await forward({
|
|
82
|
+
project_root: String(requestMeta.force_project_root).trim(),
|
|
83
|
+
project_name: String(requestMeta.force_project_name || "").trim(),
|
|
84
|
+
prompt: text,
|
|
85
|
+
reason: "forced_project_root",
|
|
86
|
+
});
|
|
87
|
+
return respond(routed.payload, routed.opsResults);
|
|
88
|
+
}
|
|
89
|
+
if (typeof runMainAgent !== "function") {
|
|
90
|
+
throw new Error("main agent runtime is unavailable");
|
|
91
|
+
}
|
|
92
|
+
const reports = listControllerInboxEntries(projectRoot, "ufoo-agent", { num: 100 });
|
|
93
|
+
const handled = await runMainAgent({
|
|
383
94
|
projectRoot,
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
processManager,
|
|
388
|
-
runUfooAgent,
|
|
389
|
-
runPromptWithControllerLoop: injectedLoopRunner,
|
|
390
|
-
dispatchMessages,
|
|
391
|
-
handleOps,
|
|
392
|
-
ackBus,
|
|
393
|
-
markPending,
|
|
394
|
-
reportTaskStatus,
|
|
395
|
-
maxAssistantLoops: 2,
|
|
396
|
-
log,
|
|
397
|
-
ufooAgentOptions,
|
|
398
|
-
finalizeLocally: !useGlobalProjectRouter,
|
|
399
|
-
loopRuntime,
|
|
400
|
-
requestId: messageId || undefined,
|
|
401
|
-
sessionId: req.session_id || requestMeta.session_id || "main-default",
|
|
402
|
-
originalPrompt: req.text || "",
|
|
95
|
+
originalPrompt: text,
|
|
96
|
+
requestId,
|
|
97
|
+
sessionId,
|
|
403
98
|
requestMeta,
|
|
404
|
-
};
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
: await promptRunner(runnerOptions);
|
|
408
|
-
|
|
409
|
-
if (!handled.ok) {
|
|
410
|
-
log(`agent-fail ${handled.error || "agent failed"}`);
|
|
411
|
-
socket.write(
|
|
412
|
-
`${JSON.stringify({
|
|
413
|
-
type: IPC_RESPONSE_TYPES.ERROR,
|
|
414
|
-
error: handled.error || "agent failed",
|
|
415
|
-
})}\n`,
|
|
416
|
-
);
|
|
417
|
-
return false;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
let shadowResult = null;
|
|
421
|
-
if (shadowEnabled && !useGlobalProjectRouter && typeof injectedLoopRunner === "function") {
|
|
422
|
-
const shadowObserver = createLoopObserver({
|
|
423
|
-
projectRoot,
|
|
424
|
-
enabled: true,
|
|
425
|
-
defaults: {
|
|
426
|
-
controller_mode: controllerMode,
|
|
427
|
-
shadow_only: true,
|
|
428
|
-
},
|
|
429
|
-
});
|
|
430
|
-
|
|
431
|
-
const shadowSamplingRate = Number.isFinite(Number(requestMeta.shadow_sampling_rate))
|
|
432
|
-
? Math.max(0, Math.min(1, Number(requestMeta.shadow_sampling_rate)))
|
|
433
|
-
: DEFAULT_SHADOW_SAMPLING_RATE;
|
|
434
|
-
const sampling = shouldSampleShadow({ messageId, samplingRate: shadowSamplingRate });
|
|
435
|
-
const budgetBreaker = createShadowBudgetBreaker({ projectRoot });
|
|
436
|
-
const budgetStatus = budgetBreaker.check();
|
|
437
|
-
|
|
438
|
-
if (!sampling.sampled) {
|
|
439
|
-
shadowObserver.emit("controller.shadow.skipped", {
|
|
440
|
-
applied_from_msg_id: messageId,
|
|
441
|
-
reason: "sampling_excluded",
|
|
442
|
-
sampling_rate: sampling.rate,
|
|
443
|
-
});
|
|
444
|
-
} else if (!budgetStatus.allowed) {
|
|
445
|
-
shadowObserver.emit("controller.shadow.skipped", {
|
|
446
|
-
applied_from_msg_id: messageId,
|
|
447
|
-
reason: budgetStatus.reason,
|
|
448
|
-
sampling_rate: sampling.rate,
|
|
449
|
-
});
|
|
450
|
-
} else {
|
|
451
|
-
const shadowGuard = createShadowGuard({ projectRoot });
|
|
452
|
-
const noOpExecutors = shadowGuard.buildNoOpExecutors();
|
|
453
|
-
const beforeSnapshot = shadowGuard.takeSnapshot();
|
|
454
|
-
|
|
455
|
-
shadowObserver.emit("controller.shadow.started", {
|
|
456
|
-
applied_from_msg_id: messageId,
|
|
457
|
-
primary_mode: CONTROLLER_MODES.LEGACY,
|
|
458
|
-
candidate_mode: CONTROLLER_MODES.LOOP,
|
|
459
|
-
sampling_rate: sampling.rate,
|
|
460
|
-
});
|
|
461
|
-
|
|
462
|
-
try {
|
|
463
|
-
shadowResult = await injectedLoopRunner({
|
|
464
|
-
projectRoot,
|
|
465
|
-
prompt: promptText,
|
|
466
|
-
provider,
|
|
467
|
-
model,
|
|
468
|
-
processManager,
|
|
469
|
-
runUfooAgent,
|
|
470
|
-
dispatchMessages: noOpExecutors.dispatchMessages,
|
|
471
|
-
handleOps: noOpExecutors.handleOps,
|
|
472
|
-
ackBus: noOpExecutors.ackBus,
|
|
473
|
-
markPending: noOpExecutors.markPending,
|
|
474
|
-
reportTaskStatus,
|
|
475
|
-
maxAssistantLoops: 2,
|
|
476
|
-
log,
|
|
477
|
-
ufooAgentOptions,
|
|
478
|
-
finalizeLocally: false,
|
|
479
|
-
loopRuntime: {
|
|
480
|
-
...resolvedLoopRuntime,
|
|
481
|
-
enabled: true,
|
|
482
|
-
},
|
|
483
|
-
observer: shadowObserver,
|
|
484
|
-
observabilityDefaults: {
|
|
485
|
-
controller_mode: controllerMode,
|
|
486
|
-
shadow_only: true,
|
|
487
|
-
},
|
|
488
|
-
});
|
|
489
|
-
} catch (shadowErr) {
|
|
490
|
-
shadowResult = {
|
|
491
|
-
ok: false,
|
|
492
|
-
error: shadowErr && shadowErr.message ? shadowErr.message : String(shadowErr),
|
|
493
|
-
};
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
const assertion = shadowGuard.assertNoSideEffects(beforeSnapshot);
|
|
497
|
-
if (!assertion.ok) {
|
|
498
|
-
shadowObserver.emit("controller.shadow.violation", {
|
|
499
|
-
applied_from_msg_id: messageId,
|
|
500
|
-
violations: assertion.violations,
|
|
501
|
-
});
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
const loopSummary = shadowResult && shadowResult.payload && shadowResult.payload.loop
|
|
505
|
-
? shadowResult.payload.loop
|
|
506
|
-
: null;
|
|
507
|
-
const totalInputTokens = loopSummary && typeof loopSummary.total_tokens === "number"
|
|
508
|
-
? loopSummary.total_tokens
|
|
509
|
-
: 0;
|
|
510
|
-
budgetBreaker.record({ inputTokens: totalInputTokens });
|
|
511
|
-
|
|
512
|
-
const diffFile = appendShadowDiff(projectRoot, {
|
|
513
|
-
event: "controller.shadow.diff",
|
|
514
|
-
request_id: messageId,
|
|
515
|
-
primary_mode: CONTROLLER_MODES.LEGACY,
|
|
516
|
-
candidate_mode: CONTROLLER_MODES.LOOP,
|
|
517
|
-
sampling_rate: sampling.rate,
|
|
518
|
-
primary: summarizeShadowPayload(handled.payload),
|
|
519
|
-
shadow: shadowResult && shadowResult.ok
|
|
520
|
-
? summarizeShadowPayload(shadowResult.payload)
|
|
521
|
-
: {
|
|
522
|
-
ok: false,
|
|
523
|
-
error: shadowResult && shadowResult.error ? String(shadowResult.error) : "shadow_run_failed",
|
|
524
|
-
},
|
|
525
|
-
side_effects_ok: assertion.ok,
|
|
526
|
-
side_effect_violations: assertion.violations,
|
|
527
|
-
});
|
|
528
|
-
shadowObserver.emit("controller.shadow.completed", {
|
|
529
|
-
applied_from_msg_id: messageId,
|
|
530
|
-
ok: shadowResult && shadowResult.ok === true,
|
|
531
|
-
diff_file: diffFile || "",
|
|
532
|
-
side_effects_ok: assertion.ok,
|
|
533
|
-
terminal_reason: loopSummary ? String(loopSummary.terminal_reason || "") : "",
|
|
534
|
-
});
|
|
535
|
-
}
|
|
99
|
+
});
|
|
100
|
+
if (!handled || handled.ok !== true) {
|
|
101
|
+
throw Object.assign(new Error(handled?.error || "agent failed"), { code: handled?.code });
|
|
536
102
|
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
103
|
+
let payload = handled.payload && typeof handled.payload === "object" ? { ...handled.payload } : {};
|
|
104
|
+
let opsResults = handled.opsResults || [];
|
|
105
|
+
if (global) {
|
|
106
|
+
const route = normalizeProjectRoute(payload.project_route);
|
|
540
107
|
if (route) {
|
|
541
|
-
const routed = await
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
prompt: route.prompt || req.text || "",
|
|
545
|
-
routeReason: route.reason,
|
|
546
|
-
requestMeta,
|
|
547
|
-
}));
|
|
548
|
-
if (!routed || routed.ok !== true) {
|
|
549
|
-
log(`project-forward-fail ${route.project_root} ${(routed && routed.error) || "project forward failed"}`);
|
|
550
|
-
socket.write(
|
|
551
|
-
`${JSON.stringify({
|
|
552
|
-
type: IPC_RESPONSE_TYPES.ERROR,
|
|
553
|
-
error: (routed && routed.error) || "project forward failed",
|
|
554
|
-
})}\n`,
|
|
555
|
-
);
|
|
556
|
-
return false;
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
consumeControllerInboxEntries(projectRoot, "ufoo-agent", privateReports);
|
|
560
|
-
const routedPayload = buildRoutedProjectPayload(routed.payload || {}, {
|
|
561
|
-
project_root: routed.project_root || route.project_root,
|
|
562
|
-
project_name: routed.project_name || route.project_name,
|
|
563
|
-
reason: route.reason,
|
|
564
|
-
});
|
|
108
|
+
const routed = await forward(route);
|
|
109
|
+
payload = routed.payload;
|
|
110
|
+
opsResults = routed.opsResults;
|
|
565
111
|
log(`project-forward-ok ${route.project_root}`);
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
})}\n`,
|
|
572
|
-
);
|
|
573
|
-
return true;
|
|
112
|
+
} else {
|
|
113
|
+
delete payload.project_route;
|
|
114
|
+
delete payload.disambiguate;
|
|
115
|
+
payload.dispatch = [];
|
|
116
|
+
payload.ops = [];
|
|
574
117
|
}
|
|
575
118
|
}
|
|
576
|
-
|
|
577
|
-
consumeControllerInboxEntries(projectRoot, "ufoo-agent", privateReports);
|
|
578
|
-
|
|
579
|
-
const payload = handled.payload && typeof handled.payload === "object"
|
|
580
|
-
? { ...handled.payload }
|
|
581
|
-
: {};
|
|
582
|
-
if (useGlobalProjectRouter) {
|
|
583
|
-
delete payload.project_route;
|
|
584
|
-
delete payload.disambiguate;
|
|
585
|
-
payload.dispatch = [];
|
|
586
|
-
payload.ops = [];
|
|
587
|
-
}
|
|
588
|
-
const opsResults = handled.opsResults || [];
|
|
119
|
+
consumeControllerInboxEntries(projectRoot, "ufoo-agent", reports);
|
|
589
120
|
log(`ok reply=${Boolean(payload.reply)} dispatch=${(payload.dispatch || []).length} ops=${(payload.ops || []).length}`);
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
? String(payload.loop.terminal_reason || "").trim()
|
|
599
|
-
: "",
|
|
600
|
-
});
|
|
601
|
-
socket.write(
|
|
602
|
-
`${JSON.stringify({
|
|
603
|
-
type: IPC_RESPONSE_TYPES.RESPONSE,
|
|
604
|
-
data: payload,
|
|
605
|
-
opsResults,
|
|
606
|
-
})}\n`,
|
|
607
|
-
);
|
|
608
|
-
return true;
|
|
609
|
-
} catch (err) {
|
|
610
|
-
log(`error ${err.message || String(err)}`);
|
|
611
|
-
controllerObserver.emit("controller.prompt_completed", {
|
|
612
|
-
applied_from_msg_id: messageId,
|
|
613
|
-
ok: false,
|
|
614
|
-
error: err.message || String(err),
|
|
615
|
-
shadow_enabled: shadowEnabled,
|
|
616
|
-
});
|
|
617
|
-
socket.write(
|
|
618
|
-
`${JSON.stringify({
|
|
619
|
-
type: IPC_RESPONSE_TYPES.ERROR,
|
|
620
|
-
error: err.message || String(err),
|
|
621
|
-
})}\n`,
|
|
622
|
-
);
|
|
121
|
+
return respond(payload, opsResults);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
log(`error ${error.message || String(error)}`);
|
|
124
|
+
socket.write(`${JSON.stringify({
|
|
125
|
+
type: IPC_RESPONSE_TYPES.ERROR,
|
|
126
|
+
error: error.message || String(error),
|
|
127
|
+
...(error.code ? { code: error.code } : {}),
|
|
128
|
+
})}\n`);
|
|
623
129
|
return false;
|
|
624
130
|
}
|
|
625
131
|
}
|
|
626
132
|
|
|
627
|
-
module.exports = {
|
|
628
|
-
handlePromptRequest,
|
|
629
|
-
buildPromptWithPrivateReports,
|
|
630
|
-
};
|
|
133
|
+
module.exports = { handlePromptRequest };
|