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
|
@@ -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,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
|
-
};
|