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
|
@@ -1,280 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const crypto = require("crypto");
|
|
4
|
-
const fs = require("fs");
|
|
5
|
-
const path = require("path");
|
|
6
|
-
|
|
7
|
-
const { getUfooPaths } = require("../../coordination/state/paths");
|
|
8
|
-
|
|
9
|
-
const DEFAULT_SHADOW_SAMPLING_RATE = 0.1;
|
|
10
|
-
const DEFAULT_SHADOW_DAILY_INPUT_TOKEN_LIMIT = 50000;
|
|
11
|
-
const DEFAULT_SHADOW_DAILY_OUTPUT_TOKEN_LIMIT = 10000;
|
|
12
|
-
|
|
13
|
-
const TIER2_MOCK_TOOL_NAMES = Object.freeze([
|
|
14
|
-
"dispatch_message",
|
|
15
|
-
"launch_agent",
|
|
16
|
-
"rename_agent",
|
|
17
|
-
"close_agent",
|
|
18
|
-
"manage_cron",
|
|
19
|
-
]);
|
|
20
|
-
|
|
21
|
-
function hashMessageId(messageId) {
|
|
22
|
-
const raw = String(messageId || "").trim();
|
|
23
|
-
if (!raw) return 0;
|
|
24
|
-
const digest = crypto.createHash("sha1").update(raw).digest();
|
|
25
|
-
return digest.readUInt32BE(0);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function shouldSampleShadow({ messageId = "", samplingRate = DEFAULT_SHADOW_SAMPLING_RATE } = {}) {
|
|
29
|
-
const rate = Number(samplingRate);
|
|
30
|
-
if (!Number.isFinite(rate) || rate <= 0) return { sampled: false, rate: 0 };
|
|
31
|
-
if (rate >= 1) return { sampled: true, rate: 1 };
|
|
32
|
-
const bucket = hashMessageId(messageId) / 0xffffffff;
|
|
33
|
-
return { sampled: bucket < rate, rate };
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function getShadowBudgetPath(projectRoot, now = new Date()) {
|
|
37
|
-
const { ufooDir } = getUfooPaths(projectRoot);
|
|
38
|
-
const stamp = now.toISOString().slice(0, 10);
|
|
39
|
-
return path.join(ufooDir, "shadow", `budget-${stamp}.json`);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function readShadowBudgetState(file) {
|
|
43
|
-
try {
|
|
44
|
-
const raw = fs.readFileSync(file, "utf8");
|
|
45
|
-
const parsed = JSON.parse(raw);
|
|
46
|
-
return {
|
|
47
|
-
input_tokens_used: Number(parsed.input_tokens_used) || 0,
|
|
48
|
-
output_tokens_used: Number(parsed.output_tokens_used) || 0,
|
|
49
|
-
tripped: Boolean(parsed.tripped),
|
|
50
|
-
tripped_reason: String(parsed.tripped_reason || ""),
|
|
51
|
-
};
|
|
52
|
-
} catch {
|
|
53
|
-
return { input_tokens_used: 0, output_tokens_used: 0, tripped: false, tripped_reason: "" };
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function writeShadowBudgetState(file, state) {
|
|
58
|
-
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
59
|
-
fs.writeFileSync(file, JSON.stringify(state, null, 2));
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function createShadowBudgetBreaker({
|
|
63
|
-
projectRoot,
|
|
64
|
-
inputLimit = DEFAULT_SHADOW_DAILY_INPUT_TOKEN_LIMIT,
|
|
65
|
-
outputLimit = DEFAULT_SHADOW_DAILY_OUTPUT_TOKEN_LIMIT,
|
|
66
|
-
now = () => new Date(),
|
|
67
|
-
} = {}) {
|
|
68
|
-
const resolveFile = () => getShadowBudgetPath(projectRoot, now());
|
|
69
|
-
|
|
70
|
-
return {
|
|
71
|
-
check() {
|
|
72
|
-
const file = resolveFile();
|
|
73
|
-
const state = readShadowBudgetState(file);
|
|
74
|
-
if (state.tripped) {
|
|
75
|
-
return { allowed: false, reason: state.tripped_reason || "shadow_budget_tripped", state };
|
|
76
|
-
}
|
|
77
|
-
if (state.input_tokens_used >= inputLimit) {
|
|
78
|
-
return { allowed: false, reason: "shadow_input_budget_exceeded", state };
|
|
79
|
-
}
|
|
80
|
-
if (state.output_tokens_used >= outputLimit) {
|
|
81
|
-
return { allowed: false, reason: "shadow_output_budget_exceeded", state };
|
|
82
|
-
}
|
|
83
|
-
return { allowed: true, state };
|
|
84
|
-
},
|
|
85
|
-
record({ inputTokens = 0, outputTokens = 0, tripped = false, trippedReason = "" } = {}) {
|
|
86
|
-
const file = resolveFile();
|
|
87
|
-
const state = readShadowBudgetState(file);
|
|
88
|
-
state.input_tokens_used += Math.max(0, Number(inputTokens) || 0);
|
|
89
|
-
state.output_tokens_used += Math.max(0, Number(outputTokens) || 0);
|
|
90
|
-
if (tripped) {
|
|
91
|
-
state.tripped = true;
|
|
92
|
-
state.tripped_reason = String(trippedReason || "shadow_budget_tripped");
|
|
93
|
-
} else if (state.input_tokens_used >= inputLimit) {
|
|
94
|
-
state.tripped = true;
|
|
95
|
-
state.tripped_reason = "shadow_input_budget_exceeded";
|
|
96
|
-
} else if (state.output_tokens_used >= outputLimit) {
|
|
97
|
-
state.tripped = true;
|
|
98
|
-
state.tripped_reason = "shadow_output_budget_exceeded";
|
|
99
|
-
}
|
|
100
|
-
writeShadowBudgetState(file, state);
|
|
101
|
-
return state;
|
|
102
|
-
},
|
|
103
|
-
tripForProviderRateLimit(reason = "shadow_provider_rate_limit") {
|
|
104
|
-
const file = resolveFile();
|
|
105
|
-
const state = readShadowBudgetState(file);
|
|
106
|
-
state.tripped = true;
|
|
107
|
-
state.tripped_reason = reason;
|
|
108
|
-
writeShadowBudgetState(file, state);
|
|
109
|
-
return state;
|
|
110
|
-
},
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function snapshotDirectory(rootDir) {
|
|
115
|
-
const entries = [];
|
|
116
|
-
if (!rootDir || !fs.existsSync(rootDir)) {
|
|
117
|
-
return { root: rootDir, entries };
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const walk = (dir) => {
|
|
121
|
-
let list;
|
|
122
|
-
try {
|
|
123
|
-
list = fs.readdirSync(dir, { withFileTypes: true });
|
|
124
|
-
} catch {
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
for (const entry of list) {
|
|
128
|
-
const abs = path.join(dir, entry.name);
|
|
129
|
-
if (entry.isDirectory()) {
|
|
130
|
-
walk(abs);
|
|
131
|
-
continue;
|
|
132
|
-
}
|
|
133
|
-
try {
|
|
134
|
-
const stat = fs.statSync(abs);
|
|
135
|
-
entries.push({
|
|
136
|
-
path: abs,
|
|
137
|
-
size: stat.size,
|
|
138
|
-
ino: stat.ino,
|
|
139
|
-
mtime_ms: Math.floor(stat.mtimeMs),
|
|
140
|
-
});
|
|
141
|
-
} catch {
|
|
142
|
-
// ignore transient errors
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
walk(rootDir);
|
|
148
|
-
entries.sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));
|
|
149
|
-
return { root: rootDir, entries };
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function diffSnapshots(before = { entries: [] }, after = { entries: [] }) {
|
|
153
|
-
const diffs = [];
|
|
154
|
-
const beforeMap = new Map();
|
|
155
|
-
for (const item of before.entries || []) beforeMap.set(item.path, item);
|
|
156
|
-
const seen = new Set();
|
|
157
|
-
|
|
158
|
-
for (const item of after.entries || []) {
|
|
159
|
-
seen.add(item.path);
|
|
160
|
-
const prior = beforeMap.get(item.path);
|
|
161
|
-
if (!prior) {
|
|
162
|
-
diffs.push({ path: item.path, kind: "added" });
|
|
163
|
-
continue;
|
|
164
|
-
}
|
|
165
|
-
if (prior.size !== item.size || prior.ino !== item.ino) {
|
|
166
|
-
diffs.push({ path: item.path, kind: "modified" });
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
for (const item of before.entries || []) {
|
|
171
|
-
if (!seen.has(item.path)) {
|
|
172
|
-
diffs.push({ path: item.path, kind: "removed" });
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return diffs;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function resolveBusQueueRoot(projectRoot) {
|
|
180
|
-
const { ufooDir } = getUfooPaths(projectRoot);
|
|
181
|
-
return path.join(ufooDir, "bus", "queues");
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
function resolveMemoryRoot(projectRoot) {
|
|
185
|
-
const { ufooDir } = getUfooPaths(projectRoot);
|
|
186
|
-
return path.join(ufooDir, "memory");
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
function buildTier2ToolExecutor(ctx = {}) {
|
|
190
|
-
const invocations = [];
|
|
191
|
-
return {
|
|
192
|
-
invocations,
|
|
193
|
-
async execute(toolCall = {}) {
|
|
194
|
-
const name = String(toolCall.name || "").trim();
|
|
195
|
-
invocations.push({ name, arguments: toolCall.arguments || {} });
|
|
196
|
-
if (TIER2_MOCK_TOOL_NAMES.includes(name)) {
|
|
197
|
-
return {
|
|
198
|
-
ok: true,
|
|
199
|
-
mocked: true,
|
|
200
|
-
shadow_only: true,
|
|
201
|
-
name,
|
|
202
|
-
result: { mocked: true, shadow_only: true },
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
if (typeof ctx.fallback === "function") {
|
|
206
|
-
return ctx.fallback(toolCall);
|
|
207
|
-
}
|
|
208
|
-
return {
|
|
209
|
-
ok: false,
|
|
210
|
-
error: { code: "shadow_only_tool_unavailable", message: `tool ${name} disabled in shadow mode` },
|
|
211
|
-
};
|
|
212
|
-
},
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
function createShadowGuard({ projectRoot, now = () => new Date() } = {}) {
|
|
217
|
-
const busQueueRoot = resolveBusQueueRoot(projectRoot);
|
|
218
|
-
const memoryRoot = resolveMemoryRoot(projectRoot);
|
|
219
|
-
const tier2 = buildTier2ToolExecutor();
|
|
220
|
-
|
|
221
|
-
function takeSnapshot() {
|
|
222
|
-
return {
|
|
223
|
-
ts: now().toISOString(),
|
|
224
|
-
bus_queue: snapshotDirectory(busQueueRoot),
|
|
225
|
-
memory: snapshotDirectory(memoryRoot),
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
function assertNoSideEffects(beforeSnapshot, afterSnapshot = takeSnapshot()) {
|
|
230
|
-
const busDiff = diffSnapshots(beforeSnapshot.bus_queue, afterSnapshot.bus_queue);
|
|
231
|
-
const memoryDiff = diffSnapshots(beforeSnapshot.memory, afterSnapshot.memory);
|
|
232
|
-
const violations = [
|
|
233
|
-
...busDiff.map((d) => ({ scope: "bus_queue", ...d })),
|
|
234
|
-
...memoryDiff.map((d) => ({ scope: "memory", ...d })),
|
|
235
|
-
];
|
|
236
|
-
return {
|
|
237
|
-
ok: violations.length === 0,
|
|
238
|
-
violations,
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
function buildNoOpExecutors() {
|
|
243
|
-
return {
|
|
244
|
-
dispatchMessages: async () => undefined,
|
|
245
|
-
handleOps: async () => [],
|
|
246
|
-
ackBus: async () => 0,
|
|
247
|
-
markPending: () => {},
|
|
248
|
-
tier2ToolExecutor: tier2,
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
return {
|
|
253
|
-
projectRoot,
|
|
254
|
-
busQueueRoot,
|
|
255
|
-
memoryRoot,
|
|
256
|
-
takeSnapshot,
|
|
257
|
-
assertNoSideEffects,
|
|
258
|
-
buildNoOpExecutors,
|
|
259
|
-
tier2ToolInvocations: () => tier2.invocations.slice(),
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
module.exports = {
|
|
264
|
-
DEFAULT_SHADOW_DAILY_INPUT_TOKEN_LIMIT,
|
|
265
|
-
DEFAULT_SHADOW_DAILY_OUTPUT_TOKEN_LIMIT,
|
|
266
|
-
DEFAULT_SHADOW_SAMPLING_RATE,
|
|
267
|
-
TIER2_MOCK_TOOL_NAMES,
|
|
268
|
-
buildTier2ToolExecutor,
|
|
269
|
-
createShadowBudgetBreaker,
|
|
270
|
-
createShadowGuard,
|
|
271
|
-
diffSnapshots,
|
|
272
|
-
getShadowBudgetPath,
|
|
273
|
-
hashMessageId,
|
|
274
|
-
readShadowBudgetState,
|
|
275
|
-
resolveBusQueueRoot,
|
|
276
|
-
resolveMemoryRoot,
|
|
277
|
-
shouldSampleShadow,
|
|
278
|
-
snapshotDirectory,
|
|
279
|
-
writeShadowBudgetState,
|
|
280
|
-
};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const fs = require("fs");
|
|
4
|
-
const path = require("path");
|
|
5
|
-
const { createRuntimeStore } = require("../../agents/runtime");
|
|
6
|
-
|
|
7
|
-
/** Persist one executor per conversation. Old controller transcripts stay on their path. */
|
|
8
|
-
function createExecutionPathSelector(projectRoot) {
|
|
9
|
-
const store = createRuntimeStore({ workspaceRoot: projectRoot, namespace: "controller-path", sessionId: "paths" });
|
|
10
|
-
return function select({ sessionId = "main-default", requestedMode, mode }) {
|
|
11
|
-
if (!/^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,100}$/.test(sessionId)) throw new Error("invalid session identity");
|
|
12
|
-
let selected;
|
|
13
|
-
store.transaction((state) => {
|
|
14
|
-
const value = state.capabilityState["execution-path"]?.value || { sessions: {} };
|
|
15
|
-
selected = Object.hasOwn(value.sessions, sessionId) ? value.sessions[sessionId] : "";
|
|
16
|
-
if (selected) {
|
|
17
|
-
if (requestedMode && requestedMode !== selected) throw Object.assign(new Error(`session uses ${selected}; use /session new before changing its executor`), { code: "session_path_pinned" });
|
|
18
|
-
return [];
|
|
19
|
-
}
|
|
20
|
-
const oldHistory = path.join(projectRoot, ".ufoo", "agent", "ufoo-agent.history.jsonl");
|
|
21
|
-
const newHistory = path.join(projectRoot, ".ufoo", "agent", "main", "runtimes", sessionId, "events.jsonl");
|
|
22
|
-
selected = sessionId === "main-default" && fs.existsSync(oldHistory) && !fs.existsSync(newHistory) ? "legacy" : mode;
|
|
23
|
-
value.sessions[sessionId] = selected;
|
|
24
|
-
return [{ type: "capability.state_committed", capabilityId: "execution-path", version: 1, value }];
|
|
25
|
-
});
|
|
26
|
-
return selected;
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
module.exports = { createExecutionPathSelector };
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
const { finalizeRouterPayload } = require("../../orchestration/controller/routerFinalize");
|
|
2
|
-
|
|
3
|
-
function normalizePayload(payload) {
|
|
4
|
-
if (!payload || typeof payload !== "object") {
|
|
5
|
-
return { reply: "", dispatch: [], ops: [] };
|
|
6
|
-
}
|
|
7
|
-
return {
|
|
8
|
-
...payload,
|
|
9
|
-
reply: typeof payload.reply === "string" ? payload.reply : "",
|
|
10
|
-
dispatch: Array.isArray(payload.dispatch) ? payload.dispatch : [],
|
|
11
|
-
ops: Array.isArray(payload.ops) ? payload.ops : [],
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function stripAssistantCall(payload) {
|
|
16
|
-
if (!payload || typeof payload !== "object") {
|
|
17
|
-
return { reply: "", dispatch: [], ops: [] };
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const ops = Array.isArray(payload.ops) ? payload.ops : [];
|
|
21
|
-
const normalOps = [];
|
|
22
|
-
|
|
23
|
-
for (const op of ops) {
|
|
24
|
-
if (op && op.action === "assistant_call") {
|
|
25
|
-
continue;
|
|
26
|
-
}
|
|
27
|
-
if (op) normalOps.push(op);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const nextPayload = {
|
|
31
|
-
...normalizePayload(payload),
|
|
32
|
-
ops: normalOps,
|
|
33
|
-
};
|
|
34
|
-
delete nextPayload.assistant_call;
|
|
35
|
-
return nextPayload;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async function finalizePromptRun({
|
|
39
|
-
projectRoot,
|
|
40
|
-
payload,
|
|
41
|
-
prompt,
|
|
42
|
-
processManager,
|
|
43
|
-
dispatchMessages,
|
|
44
|
-
handleOps,
|
|
45
|
-
markPending,
|
|
46
|
-
finalizeLocally = true,
|
|
47
|
-
}) {
|
|
48
|
-
return finalizeRouterPayload({
|
|
49
|
-
projectRoot,
|
|
50
|
-
payload,
|
|
51
|
-
prompt,
|
|
52
|
-
processManager,
|
|
53
|
-
dispatchMessages,
|
|
54
|
-
handleOps,
|
|
55
|
-
markPending,
|
|
56
|
-
finalizeLocally,
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async function runPromptWithAssistant({
|
|
61
|
-
projectRoot,
|
|
62
|
-
prompt,
|
|
63
|
-
provider,
|
|
64
|
-
model,
|
|
65
|
-
processManager = null,
|
|
66
|
-
runUfooAgent,
|
|
67
|
-
runPromptWithControllerLoop = null,
|
|
68
|
-
dispatchMessages,
|
|
69
|
-
handleOps,
|
|
70
|
-
markPending = () => {},
|
|
71
|
-
ufooAgentOptions = {},
|
|
72
|
-
finalizeLocally = true,
|
|
73
|
-
loopRuntime = null,
|
|
74
|
-
}) {
|
|
75
|
-
const agentOptions = {
|
|
76
|
-
...(ufooAgentOptions && typeof ufooAgentOptions === "object" ? ufooAgentOptions : {}),
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const firstResult = await runUfooAgent({
|
|
80
|
-
projectRoot,
|
|
81
|
-
prompt: prompt || "",
|
|
82
|
-
provider,
|
|
83
|
-
model,
|
|
84
|
-
...agentOptions,
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
if (!firstResult || !firstResult.ok) {
|
|
88
|
-
return {
|
|
89
|
-
ok: false,
|
|
90
|
-
error: (firstResult && firstResult.error) || "agent failed",
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const firstPayload = stripAssistantCall(firstResult.payload);
|
|
95
|
-
const shouldUpgradeToLoop = Boolean(
|
|
96
|
-
loopRuntime
|
|
97
|
-
&& loopRuntime.enabled
|
|
98
|
-
&& firstPayload
|
|
99
|
-
&& firstPayload.upgrade_to_loop_router === true
|
|
100
|
-
&& typeof runPromptWithControllerLoop === "function"
|
|
101
|
-
);
|
|
102
|
-
|
|
103
|
-
if (shouldUpgradeToLoop) {
|
|
104
|
-
return runPromptWithControllerLoop({
|
|
105
|
-
projectRoot,
|
|
106
|
-
prompt: prompt || "",
|
|
107
|
-
provider,
|
|
108
|
-
model,
|
|
109
|
-
processManager,
|
|
110
|
-
runUfooAgent,
|
|
111
|
-
dispatchMessages,
|
|
112
|
-
handleOps,
|
|
113
|
-
markPending,
|
|
114
|
-
ufooAgentOptions,
|
|
115
|
-
finalizeLocally,
|
|
116
|
-
loopRuntime,
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return finalizePromptRun({
|
|
121
|
-
projectRoot,
|
|
122
|
-
payload: firstPayload,
|
|
123
|
-
prompt: prompt || "",
|
|
124
|
-
processManager,
|
|
125
|
-
dispatchMessages,
|
|
126
|
-
handleOps,
|
|
127
|
-
markPending,
|
|
128
|
-
finalizeLocally,
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
module.exports = {
|
|
133
|
-
runPromptWithAssistant,
|
|
134
|
-
normalizePayload,
|
|
135
|
-
stripAssistantCall,
|
|
136
|
-
};
|
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
// §11.6 Phase 1 shadow diff metrics: model text BLEU ≥ 0.85 and
|
|
4
|
-
// tool-call sequence consistency ≥ 95%. This module provides the deterministic
|
|
5
|
-
// helpers the shadow harness feeds paired (legacy, api-backed) samples into.
|
|
6
|
-
|
|
7
|
-
const PHASE1_DEFAULT_BLEU_THRESHOLD = 0.85;
|
|
8
|
-
const PHASE1_DEFAULT_TOOLCALL_THRESHOLD = 0.95;
|
|
9
|
-
|
|
10
|
-
function tokenize(text) {
|
|
11
|
-
return String(text || "")
|
|
12
|
-
.toLowerCase()
|
|
13
|
-
.replace(/[^\p{L}\p{N}\s]/gu, " ")
|
|
14
|
-
.split(/\s+/)
|
|
15
|
-
.filter(Boolean);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function ngrams(tokens, n) {
|
|
19
|
-
if (!Array.isArray(tokens) || tokens.length < n) return [];
|
|
20
|
-
const out = [];
|
|
21
|
-
for (let i = 0; i <= tokens.length - n; i += 1) {
|
|
22
|
-
out.push(tokens.slice(i, i + n).join("\u0001"));
|
|
23
|
-
}
|
|
24
|
-
return out;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function countMap(list) {
|
|
28
|
-
const map = new Map();
|
|
29
|
-
for (const item of list) {
|
|
30
|
-
map.set(item, (map.get(item) || 0) + 1);
|
|
31
|
-
}
|
|
32
|
-
return map;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function clippedPrecision(candidateNgrams, referenceNgrams) {
|
|
36
|
-
if (candidateNgrams.length === 0) return 0;
|
|
37
|
-
const candidateCounts = countMap(candidateNgrams);
|
|
38
|
-
const referenceCounts = countMap(referenceNgrams);
|
|
39
|
-
let clipped = 0;
|
|
40
|
-
for (const [gram, count] of candidateCounts.entries()) {
|
|
41
|
-
const refCount = referenceCounts.get(gram) || 0;
|
|
42
|
-
clipped += Math.min(count, refCount);
|
|
43
|
-
}
|
|
44
|
-
return clipped / candidateNgrams.length;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function brevityPenalty(candidateLen, referenceLen) {
|
|
48
|
-
if (candidateLen === 0) return 0;
|
|
49
|
-
if (candidateLen >= referenceLen) return 1;
|
|
50
|
-
return Math.exp(1 - referenceLen / candidateLen);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function computeBleu(referenceText, candidateText, options = {}) {
|
|
54
|
-
const maxN = Number.isFinite(options.maxN) ? Math.max(1, Math.min(4, options.maxN)) : 4;
|
|
55
|
-
const weight = 1 / maxN;
|
|
56
|
-
const ref = tokenize(referenceText);
|
|
57
|
-
const cand = tokenize(candidateText);
|
|
58
|
-
if (cand.length === 0) return 0;
|
|
59
|
-
if (ref.length === 0) return 0;
|
|
60
|
-
|
|
61
|
-
let logSum = 0;
|
|
62
|
-
for (let n = 1; n <= maxN; n += 1) {
|
|
63
|
-
const precision = clippedPrecision(ngrams(cand, n), ngrams(ref, n));
|
|
64
|
-
if (precision <= 0) return 0;
|
|
65
|
-
logSum += weight * Math.log(precision);
|
|
66
|
-
}
|
|
67
|
-
const bp = brevityPenalty(cand.length, ref.length);
|
|
68
|
-
return bp * Math.exp(logSum);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function extractToolCallNames(events = []) {
|
|
72
|
-
return (Array.isArray(events) ? events : [])
|
|
73
|
-
.filter((event) => event && event.type === "tool_call" && event.name)
|
|
74
|
-
.map((event) => String(event.name).trim())
|
|
75
|
-
.filter(Boolean);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function computeToolCallSequenceConsistency(referenceSeq = [], candidateSeq = []) {
|
|
79
|
-
const ref = Array.isArray(referenceSeq) ? referenceSeq : [];
|
|
80
|
-
const cand = Array.isArray(candidateSeq) ? candidateSeq : [];
|
|
81
|
-
if (ref.length === 0 && cand.length === 0) return 1;
|
|
82
|
-
|
|
83
|
-
const m = ref.length;
|
|
84
|
-
const n = cand.length;
|
|
85
|
-
const dp = Array.from({ length: m + 1 }, () => new Array(n + 1).fill(0));
|
|
86
|
-
for (let i = 1; i <= m; i += 1) {
|
|
87
|
-
for (let j = 1; j <= n; j += 1) {
|
|
88
|
-
if (ref[i - 1] === cand[j - 1]) {
|
|
89
|
-
dp[i][j] = dp[i - 1][j - 1] + 1;
|
|
90
|
-
} else {
|
|
91
|
-
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
const lcs = dp[m][n];
|
|
96
|
-
const denom = Math.max(m, n);
|
|
97
|
-
if (denom === 0) return 1;
|
|
98
|
-
return lcs / denom;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function buildPhase1ShadowDiffSample({ legacy = {}, api = {} } = {}) {
|
|
102
|
-
const bleu = computeBleu(legacy.text || "", api.text || "");
|
|
103
|
-
const toolSeqConsistency = computeToolCallSequenceConsistency(
|
|
104
|
-
extractToolCallNames(legacy.events || legacy.toolCalls || []),
|
|
105
|
-
extractToolCallNames(api.events || api.toolCalls || [])
|
|
106
|
-
);
|
|
107
|
-
return {
|
|
108
|
-
bleu,
|
|
109
|
-
toolSeqConsistency,
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function summarizePhase1ShadowDiff(samples = [], options = {}) {
|
|
114
|
-
const bleuThreshold = Number.isFinite(options.bleuThreshold)
|
|
115
|
-
? options.bleuThreshold
|
|
116
|
-
: PHASE1_DEFAULT_BLEU_THRESHOLD;
|
|
117
|
-
const toolCallThreshold = Number.isFinite(options.toolCallThreshold)
|
|
118
|
-
? options.toolCallThreshold
|
|
119
|
-
: PHASE1_DEFAULT_TOOLCALL_THRESHOLD;
|
|
120
|
-
|
|
121
|
-
const list = Array.isArray(samples) ? samples : [];
|
|
122
|
-
if (list.length === 0) {
|
|
123
|
-
return {
|
|
124
|
-
sampleCount: 0,
|
|
125
|
-
meanBleu: 0,
|
|
126
|
-
meanToolSeqConsistency: 0,
|
|
127
|
-
bleuPass: false,
|
|
128
|
-
toolCallPass: false,
|
|
129
|
-
overallPass: false,
|
|
130
|
-
bleuThreshold,
|
|
131
|
-
toolCallThreshold,
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
let totalBleu = 0;
|
|
136
|
-
let totalTool = 0;
|
|
137
|
-
let toolCallPassCount = 0;
|
|
138
|
-
for (const sample of list) {
|
|
139
|
-
totalBleu += Number(sample.bleu || 0);
|
|
140
|
-
const consistency = Number(sample.toolSeqConsistency || 0);
|
|
141
|
-
totalTool += consistency;
|
|
142
|
-
if (consistency >= toolCallThreshold) toolCallPassCount += 1;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const meanBleu = totalBleu / list.length;
|
|
146
|
-
const meanToolSeqConsistency = totalTool / list.length;
|
|
147
|
-
const bleuPass = meanBleu >= bleuThreshold;
|
|
148
|
-
const toolCallPassRate = toolCallPassCount / list.length;
|
|
149
|
-
const toolCallPass = toolCallPassRate >= toolCallThreshold;
|
|
150
|
-
|
|
151
|
-
return {
|
|
152
|
-
sampleCount: list.length,
|
|
153
|
-
meanBleu,
|
|
154
|
-
meanToolSeqConsistency,
|
|
155
|
-
toolCallPassRate,
|
|
156
|
-
bleuPass,
|
|
157
|
-
toolCallPass,
|
|
158
|
-
overallPass: bleuPass && toolCallPass,
|
|
159
|
-
bleuThreshold,
|
|
160
|
-
toolCallThreshold,
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
module.exports = {
|
|
165
|
-
PHASE1_DEFAULT_BLEU_THRESHOLD,
|
|
166
|
-
PHASE1_DEFAULT_TOOLCALL_THRESHOLD,
|
|
167
|
-
tokenize,
|
|
168
|
-
ngrams,
|
|
169
|
-
computeBleu,
|
|
170
|
-
extractToolCallNames,
|
|
171
|
-
computeToolCallSequenceConsistency,
|
|
172
|
-
buildPhase1ShadowDiffSample,
|
|
173
|
-
summarizePhase1ShadowDiff,
|
|
174
|
-
};
|