@evomap/evolver 1.90.0 → 2.0.0-beta.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/README.md +27 -564
- package/bin/evolver-llm-proxy.js +3 -0
- package/bin/evolver-mcp.js +2 -0
- package/bin/evolver-proxy.js +3 -0
- package/bin/evolver.js +4 -0
- package/index.js +1 -3670
- package/package.json +32 -62
- package/CONTRIBUTING.md +0 -19
- package/LICENSE +0 -641
- package/README.ja-JP.md +0 -521
- package/README.ko-KR.md +0 -520
- package/README.zh-CN.md +0 -531
- package/SKILL.md +0 -365
- package/assets/cover.png +0 -0
- package/assets/gep/genes.seed.json +0 -496
- package/conformance/savings-core/constants.json +0 -30
- package/conformance/savings-core/golden-vectors.json +0 -333
- package/scripts/a2a_export.js +0 -63
- package/scripts/a2a_ingest.js +0 -79
- package/scripts/a2a_promote.js +0 -118
- package/scripts/analyze_by_skill.js +0 -121
- package/scripts/build_binaries.js +0 -479
- package/scripts/check-changelog.js +0 -166
- package/scripts/extract_log.js +0 -85
- package/scripts/generate_history.js +0 -75
- package/scripts/gep_append_event.js +0 -96
- package/scripts/gep_personality_report.js +0 -234
- package/scripts/human_report.js +0 -147
- package/scripts/recall-verify-report.js +0 -234
- package/scripts/recover_loop.js +0 -61
- package/scripts/refresh_stars_badge.js +0 -168
- package/scripts/seed-merchants.js +0 -91
- package/scripts/skill2recipes.js +0 -118
- package/scripts/suggest_version.js +0 -89
- package/scripts/validate-modules.js +0 -38
- package/scripts/validate-suite.js +0 -78
- package/src/adapters/claudeCode.js +0 -194
- package/src/adapters/codex.js +0 -216
- package/src/adapters/cursor.js +0 -91
- package/src/adapters/hookAdapter.js +0 -469
- package/src/adapters/kiro.js +0 -195
- package/src/adapters/opencode.js +0 -326
- package/src/adapters/scripts/_lockPaths.js +0 -74
- package/src/adapters/scripts/_memoryFiltering.js +0 -35
- package/src/adapters/scripts/_runtimePaths.js +0 -440
- package/src/adapters/scripts/evolver-session-end.js +0 -321
- package/src/adapters/scripts/evolver-session-start.js +0 -587
- package/src/adapters/scripts/evolver-signal-detect.js +0 -98
- package/src/adapters/scripts/evolver-task-recall.js +0 -173
- package/src/atp/atpExecute.js +0 -283
- package/src/atp/atpTaskPickup.js +0 -233
- package/src/atp/autoBuyer.js +0 -382
- package/src/atp/autoDeliver.js +0 -215
- package/src/atp/cli.js +0 -354
- package/src/atp/cliAutobuyPrompt.js +0 -154
- package/src/atp/consumerAgent.js +0 -157
- package/src/atp/defaultHandler.js +0 -69
- package/src/atp/heartbeatSignalsHandler.js +0 -254
- package/src/atp/hubClient.js +0 -317
- package/src/atp/index.js +0 -38
- package/src/atp/merchantAgent.js +0 -118
- package/src/atp/protocol.js +0 -41
- package/src/atp/questionComposer.js +0 -133
- package/src/atp/serviceHelper.js +0 -92
- package/src/canary.js +0 -13
- package/src/config.js +0 -351
- package/src/evolve/guards.js +0 -1
- package/src/evolve/pipeline/collect.js +0 -1
- package/src/evolve/pipeline/dispatch.js +0 -1
- package/src/evolve/pipeline/enrich.js +0 -1
- package/src/evolve/pipeline/hub.js +0 -1
- package/src/evolve/pipeline/select.js +0 -1
- package/src/evolve/pipeline/signals.js +0 -1
- package/src/evolve/utils.js +0 -1
- package/src/evolve.js +0 -1
- package/src/experiment/agentRunner.js +0 -229
- package/src/experiment/cli.js +0 -159
- package/src/experiment/comparison.js +0 -233
- package/src/experiment/metrics.js +0 -75
- package/src/forceUpdate.js +0 -991
- package/src/gep/a2a.js +0 -173
- package/src/gep/a2aProtocol.js +0 -1
- package/src/gep/analyzer.js +0 -35
- package/src/gep/antiAbuseTelemetry.js +0 -1
- package/src/gep/assetCallLog.js +0 -197
- package/src/gep/assetStore.js +0 -723
- package/src/gep/assets.js +0 -36
- package/src/gep/autoDistillConv.js +0 -1
- package/src/gep/autoDistillLlm.js +0 -1
- package/src/gep/bridge.js +0 -138
- package/src/gep/candidateEval.js +0 -1
- package/src/gep/candidates.js +0 -1
- package/src/gep/claimNudge.js +0 -121
- package/src/gep/cliContracts.js +0 -1190
- package/src/gep/contentHash.js +0 -1
- package/src/gep/conversationDistiller.js +0 -1
- package/src/gep/conversationSniffer.js +0 -1
- package/src/gep/crypto.js +0 -1
- package/src/gep/curriculum.js +0 -1
- package/src/gep/deviceId.js +0 -1
- package/src/gep/directoryClient.js +0 -115
- package/src/gep/envFingerprint.js +0 -1
- package/src/gep/epigenetics.js +0 -1
- package/src/gep/execBridge.js +0 -1
- package/src/gep/executionTrace.js +0 -291
- package/src/gep/explore.js +0 -1
- package/src/gep/featureFlags.js +0 -121
- package/src/gep/gitOps.js +0 -265
- package/src/gep/hash.js +0 -1
- package/src/gep/hostErrorClassifier.js +0 -34
- package/src/gep/hubFetch.js +0 -1
- package/src/gep/hubReview.js +0 -1
- package/src/gep/hubSearch.js +0 -1
- package/src/gep/hubVerify.js +0 -1
- package/src/gep/idleScheduler.js +0 -400
- package/src/gep/issueReporter.js +0 -416
- package/src/gep/learningSignals.js +0 -1
- package/src/gep/llmReview.js +0 -92
- package/src/gep/localStateAwareness.js +0 -243
- package/src/gep/mailboxTransport.js +0 -119
- package/src/gep/memoryGraph.js +0 -1
- package/src/gep/memoryGraphAdapter.js +0 -1
- package/src/gep/mutation.js +0 -1
- package/src/gep/narrativeMemory.js +0 -1
- package/src/gep/oauthLogin.js +0 -181
- package/src/gep/openPRRegistry.js +0 -1
- package/src/gep/paths.js +0 -522
- package/src/gep/personality.js +0 -1
- package/src/gep/policyCheck.js +0 -1
- package/src/gep/portable.js +0 -103
- package/src/gep/privacyClient.js +0 -235
- package/src/gep/prompt.js +0 -1
- package/src/gep/questionGenerator.js +0 -518
- package/src/gep/recallInject.js +0 -1
- package/src/gep/recallVerifier.js +0 -1
- package/src/gep/reflection.js +0 -1
- package/src/gep/sanitize.js +0 -264
- package/src/gep/savingsCore.js +0 -1
- package/src/gep/schemas/capsule.js +0 -170
- package/src/gep/schemas/gene.js +0 -154
- package/src/gep/schemas/index.js +0 -8
- package/src/gep/schemas/protocol.js +0 -51
- package/src/gep/schemas/task.js +0 -74
- package/src/gep/selector.js +0 -1
- package/src/gep/selfPR.js +0 -469
- package/src/gep/signals.js +0 -776
- package/src/gep/skill2gep.js +0 -1056
- package/src/gep/skill2gepAudit.js +0 -303
- package/src/gep/skill2recipes.js +0 -511
- package/src/gep/skillDistiller.js +0 -1
- package/src/gep/skillPublisher.js +0 -358
- package/src/gep/solidify.js +0 -1
- package/src/gep/strategy.js +0 -1
- package/src/gep/taskReceiver.js +0 -575
- package/src/gep/tokenSavings.js +0 -1
- package/src/gep/trajectoryExport.js +0 -1
- package/src/gep/validationReport.js +0 -55
- package/src/gep/validator/index.js +0 -411
- package/src/gep/validator/reporter.js +0 -210
- package/src/gep/validator/sandboxExecutor.js +0 -480
- package/src/gep/validator/stakeBootstrap.js +0 -357
- package/src/gep/workspaceKeychain.js +0 -1
- package/src/ops/cleanup.js +0 -80
- package/src/ops/commentary.js +0 -60
- package/src/ops/health_check.js +0 -104
- package/src/ops/index.js +0 -11
- package/src/ops/innovation.js +0 -67
- package/src/ops/lifecycle.js +0 -798
- package/src/ops/self_repair.js +0 -76
- package/src/ops/skills_monitor.js +0 -147
- package/src/ops/trigger.js +0 -33
- package/src/proxy/clientSettings.js +0 -405
- package/src/proxy/envelope.js +0 -59
- package/src/proxy/extensions/dmHandler.js +0 -45
- package/src/proxy/extensions/sessionHandler.js +0 -141
- package/src/proxy/extensions/skillUpdater.js +0 -64
- package/src/proxy/extensions/traceControl.js +0 -1
- package/src/proxy/index.js +0 -1395
- package/src/proxy/inject.js +0 -1
- package/src/proxy/lifecycle/manager.js +0 -1568
- package/src/proxy/mailbox/state.js +0 -207
- package/src/proxy/mailbox/store.js +0 -602
- package/src/proxy/router/cache_passthrough.js +0 -26
- package/src/proxy/router/features.js +0 -84
- package/src/proxy/router/gemini_route.js +0 -154
- package/src/proxy/router/messages_route.js +0 -535
- package/src/proxy/router/model_router.js +0 -113
- package/src/proxy/router/models_route.js +0 -52
- package/src/proxy/router/ollama_route.js +0 -103
- package/src/proxy/router/responses_route.js +0 -170
- package/src/proxy/router/vertex_route.js +0 -110
- package/src/proxy/server/http.js +0 -363
- package/src/proxy/server/routes.js +0 -558
- package/src/proxy/server/settings.js +0 -115
- package/src/proxy/sync/engine.js +0 -179
- package/src/proxy/sync/inbound.js +0 -211
- package/src/proxy/sync/outbound.js +0 -361
- package/src/proxy/task/monitor.js +0 -131
- package/src/proxy/trace/extractor.js +0 -1
- package/src/proxy/trace/usage.js +0 -1
- package/src/solo/breaker.js +0 -25
- package/src/solo/gitGuard.js +0 -65
- package/src/webui/client/clientJs/assets.js +0 -111
- package/src/webui/client/clientJs/bootstrap.js +0 -92
- package/src/webui/client/clientJs/common.js +0 -77
- package/src/webui/client/clientJs/i18n.js +0 -366
- package/src/webui/client/clientJs/index.js +0 -35
- package/src/webui/client/clientJs/interactions.js +0 -351
- package/src/webui/client/clientJs/overview.js +0 -152
- package/src/webui/client/clientJs/personality.js +0 -285
- package/src/webui/client/clientJs/pipelines.js +0 -330
- package/src/webui/client/indexHtml.js +0 -221
- package/src/webui/client/static.js +0 -23
- package/src/webui/client/stylesCss.js +0 -639
- package/src/webui/client/vendor/README.md +0 -15
- package/src/webui/client/vendor/echarts.min.js +0 -45
- package/src/webui/index.js +0 -14
- package/src/webui/observer/assets.js +0 -146
- package/src/webui/observer/index.js +0 -37
- package/src/webui/observer/interactions.js +0 -127
- package/src/webui/observer/jsonl.js +0 -75
- package/src/webui/observer/paths.js +0 -46
- package/src/webui/observer/personality.js +0 -43
- package/src/webui/observer/pipelineEvents.js +0 -58
- package/src/webui/observer/redact.js +0 -63
- package/src/webui/observer/runs.js +0 -356
- package/src/webui/observer/safety.js +0 -57
- package/src/webui/observer/skills.js +0 -70
- package/src/webui/observer/status.js +0 -71
- package/src/webui/server/http.js +0 -138
- package/src/webui/server/routes.js +0 -41
|
@@ -1,361 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { PROXY_PROTOCOL_VERSION } = require('../mailbox/store');
|
|
4
|
-
const { AuthError } = require('../lifecycle/manager');
|
|
5
|
-
const { isProxyTraceUploadPayloadAllowed, resolveTraceMode } = require('../trace/extractor');
|
|
6
|
-
const {
|
|
7
|
-
hubFetch,
|
|
8
|
-
hubUnreachableBackoffMs,
|
|
9
|
-
isHubUnreachableError,
|
|
10
|
-
readHubResponseJson,
|
|
11
|
-
readHubResponseText,
|
|
12
|
-
sanitizeHubResponseForLog,
|
|
13
|
-
throwIfHubUnreachableResponse,
|
|
14
|
-
} = require('../../gep/hubFetch');
|
|
15
|
-
const { redactString } = require('../../gep/sanitize');
|
|
16
|
-
|
|
17
|
-
const MAX_BATCH = 50;
|
|
18
|
-
const MAX_RETRIES = 10;
|
|
19
|
-
const DEFAULT_MAX_OUTBOUND_BODY_BYTES = 4 * 1024 * 1024;
|
|
20
|
-
const MIN_ADAPTIVE_OUTBOUND_BODY_BYTES = 16 * 1024;
|
|
21
|
-
const OUTBOUND_BODY_BYTES_STATE_KEY = 'outbound_sync_max_body_bytes';
|
|
22
|
-
|
|
23
|
-
function sanitizeHubErrorMessage(value) {
|
|
24
|
-
if (typeof sanitizeHubResponseForLog === 'function') {
|
|
25
|
-
return sanitizeHubResponseForLog(value);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
let text;
|
|
29
|
-
if (typeof value === 'string') {
|
|
30
|
-
text = value;
|
|
31
|
-
} else if (value && typeof value.message === 'string') {
|
|
32
|
-
text = value.message;
|
|
33
|
-
} else {
|
|
34
|
-
try { text = JSON.stringify(value); } catch { text = String(value || ''); }
|
|
35
|
-
}
|
|
36
|
-
return redactString(String(text || ''));
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function resolveHardOutboundBodyBytes(env = process.env) {
|
|
40
|
-
const raw = Number(env.EVOMAP_OUTBOUND_SYNC_MAX_BODY_BYTES || env.EVOMAP_MAILBOX_OUTBOUND_MAX_BODY_BYTES);
|
|
41
|
-
const max = Number.isFinite(raw) && raw > 0
|
|
42
|
-
? Math.floor(raw)
|
|
43
|
-
: DEFAULT_MAX_OUTBOUND_BODY_BYTES;
|
|
44
|
-
return Math.max(1, max);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function resolveMaxOutboundBodyBytes(env = process.env, store = null) {
|
|
48
|
-
let max = resolveHardOutboundBodyBytes(env);
|
|
49
|
-
try {
|
|
50
|
-
const adaptive = Number(store && typeof store.getState === 'function'
|
|
51
|
-
? store.getState(OUTBOUND_BODY_BYTES_STATE_KEY)
|
|
52
|
-
: null);
|
|
53
|
-
if (Number.isFinite(adaptive) && adaptive > 0) max = Math.min(max, Math.floor(adaptive));
|
|
54
|
-
} catch { /* adaptive state is best-effort */ }
|
|
55
|
-
return Math.max(1, max);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function hubMessage(m) {
|
|
59
|
-
return {
|
|
60
|
-
id: m.id,
|
|
61
|
-
type: m.type,
|
|
62
|
-
payload: m.payload,
|
|
63
|
-
priority: m.priority,
|
|
64
|
-
ref_id: m.ref_id,
|
|
65
|
-
created_at: m.created_at,
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function serializeOutboundBody(senderId, messages) {
|
|
70
|
-
return JSON.stringify({
|
|
71
|
-
sender_id: senderId,
|
|
72
|
-
proxy_protocol_version: PROXY_PROTOCOL_VERSION,
|
|
73
|
-
messages: messages.map(hubMessage),
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function outboundEndpoint(hubUrl, senderId) {
|
|
78
|
-
const endpoint = new URL(`${String(hubUrl || '').replace(/\/+$/, '')}/a2a/mailbox/outbound`);
|
|
79
|
-
if (senderId) endpoint.searchParams.set('sender_id', senderId);
|
|
80
|
-
return endpoint.toString();
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function outboundBodyBytes(senderId, messages) {
|
|
84
|
-
return Buffer.byteLength(serializeOutboundBody(senderId, messages), 'utf8');
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function buildSizedOutboundBatch(senderId, messages, maxBodyBytes, hardMaxBodyBytes = maxBodyBytes) {
|
|
88
|
-
const selected = [];
|
|
89
|
-
const rejected = [];
|
|
90
|
-
let body = serializeOutboundBody(senderId, selected);
|
|
91
|
-
let bodyBytes = Buffer.byteLength(body, 'utf8');
|
|
92
|
-
|
|
93
|
-
for (const m of messages) {
|
|
94
|
-
const singleBytes = outboundBodyBytes(senderId, [m]);
|
|
95
|
-
if (singleBytes > hardMaxBodyBytes) {
|
|
96
|
-
rejected.push({
|
|
97
|
-
id: m.id,
|
|
98
|
-
status: 'rejected',
|
|
99
|
-
error: `outbound message exceeds max body bytes (${singleBytes} > ${hardMaxBodyBytes})`,
|
|
100
|
-
});
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
if (selected.length === 0 && singleBytes > maxBodyBytes) {
|
|
104
|
-
selected.push(m);
|
|
105
|
-
body = serializeOutboundBody(senderId, selected);
|
|
106
|
-
bodyBytes = Buffer.byteLength(body, 'utf8');
|
|
107
|
-
break;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const candidate = selected.concat(m);
|
|
111
|
-
const candidateBody = serializeOutboundBody(senderId, candidate);
|
|
112
|
-
const candidateBytes = Buffer.byteLength(candidateBody, 'utf8');
|
|
113
|
-
if (candidateBytes > maxBodyBytes) break;
|
|
114
|
-
|
|
115
|
-
selected.push(m);
|
|
116
|
-
body = candidateBody;
|
|
117
|
-
bodyBytes = candidateBytes;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return { selected, rejected, body, bodyBytes, maxBodyBytes };
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function numberField(row, keys) {
|
|
124
|
-
for (const key of keys) {
|
|
125
|
-
if (!row || row[key] === undefined || row[key] === null || row[key] === '') continue;
|
|
126
|
-
const n = Number(row[key]);
|
|
127
|
-
if (Number.isFinite(n)) return n;
|
|
128
|
-
}
|
|
129
|
-
return null;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function resultRetryAfterMs(row) {
|
|
133
|
-
const ms = numberField(row, ['retryAfterMs', 'retry_after_ms']);
|
|
134
|
-
if (ms !== null) return Math.max(1_000, ms);
|
|
135
|
-
const seconds = numberField(row, ['retryAfter', 'retry_after']);
|
|
136
|
-
return seconds !== null ? Math.max(1_000, seconds * 1000) : null;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function resultIsRetryable(row) {
|
|
140
|
-
return row && (row.retryable === true || row.retryable === 'true' || row.terminal === false || row.terminal === 'false');
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function resultIsTerminal(row) {
|
|
144
|
-
return row && (row.terminal === true || row.terminal === 'true');
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
class OutboundSync {
|
|
148
|
-
constructor({ store, hubUrl, getHeaders, logger }) {
|
|
149
|
-
this.store = store;
|
|
150
|
-
this.hubUrl = hubUrl;
|
|
151
|
-
this.logger = logger || console;
|
|
152
|
-
this.getHeaders = getHeaders;
|
|
153
|
-
this._hubUnreachableFailures = 0;
|
|
154
|
-
this._hubUnreachableUntil = 0;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
_hubUnreachableWaitMs() {
|
|
158
|
-
return Math.max(0, this._hubUnreachableUntil - Date.now());
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
_recordHubReachable() {
|
|
162
|
-
this._hubUnreachableFailures = 0;
|
|
163
|
-
this._hubUnreachableUntil = 0;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
_recordHubUnreachable(err) {
|
|
167
|
-
this._hubUnreachableFailures += 1;
|
|
168
|
-
const retryAfterMs = hubUnreachableBackoffMs(this._hubUnreachableFailures);
|
|
169
|
-
this._hubUnreachableUntil = Date.now() + retryAfterMs;
|
|
170
|
-
this.logger.warn?.(
|
|
171
|
-
`[outbound] Hub unreachable; backing off for ${Math.ceil(retryAfterMs / 1000)}s: ` +
|
|
172
|
-
`${err && err.message || err}`
|
|
173
|
-
);
|
|
174
|
-
return retryAfterMs;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
async flush(channel = 'evomap-hub') {
|
|
178
|
-
const waitMs = this._hubUnreachableWaitMs();
|
|
179
|
-
if (waitMs > 0) {
|
|
180
|
-
return {
|
|
181
|
-
sent: 0,
|
|
182
|
-
error: 'hub_unreachable_backoff',
|
|
183
|
-
hubUnreachable: true,
|
|
184
|
-
retryAfterMs: waitMs,
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
const pendingBatch = this.store.pollOutbound({ channel, limit: MAX_BATCH });
|
|
189
|
-
if (pendingBatch.length === 0) return { sent: 0 };
|
|
190
|
-
|
|
191
|
-
let pending = pendingBatch;
|
|
192
|
-
const rejectedTraceUploads = [];
|
|
193
|
-
const traceUploadEnabled = resolveTraceMode(process.env, { store: this.store });
|
|
194
|
-
for (const m of pendingBatch) {
|
|
195
|
-
if (m.type !== 'proxy_trace') continue;
|
|
196
|
-
if (!traceUploadEnabled) {
|
|
197
|
-
rejectedTraceUploads.push({ id: m.id, error: 'proxy trace upload disabled' });
|
|
198
|
-
} else if (!isProxyTraceUploadPayloadAllowed(m.payload, process.env, { store: this.store })) {
|
|
199
|
-
rejectedTraceUploads.push({ id: m.id, error: 'proxy trace payload rejected' });
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
if (rejectedTraceUploads.length > 0) {
|
|
203
|
-
this.store.updateStatusBatch(rejectedTraceUploads.map(m => ({
|
|
204
|
-
id: m.id,
|
|
205
|
-
status: 'rejected',
|
|
206
|
-
error: m.error,
|
|
207
|
-
})));
|
|
208
|
-
const rejectedIds = new Set(rejectedTraceUploads.map(m => m.id));
|
|
209
|
-
pending = pendingBatch.filter(m => !rejectedIds.has(m.id));
|
|
210
|
-
if (pending.length === 0) return { sent: 0, dropped: rejectedTraceUploads.length };
|
|
211
|
-
}
|
|
212
|
-
let dropped = rejectedTraceUploads.length;
|
|
213
|
-
|
|
214
|
-
try {
|
|
215
|
-
const senderId = this.store.getState('node_id');
|
|
216
|
-
const endpoint = outboundEndpoint(this.hubUrl, senderId);
|
|
217
|
-
const prepared = buildSizedOutboundBatch(
|
|
218
|
-
senderId,
|
|
219
|
-
pending,
|
|
220
|
-
resolveMaxOutboundBodyBytes(process.env, this.store),
|
|
221
|
-
resolveHardOutboundBodyBytes(process.env)
|
|
222
|
-
);
|
|
223
|
-
if (prepared.rejected.length > 0) {
|
|
224
|
-
this.store.updateStatusBatch(prepared.rejected);
|
|
225
|
-
dropped += prepared.rejected.length;
|
|
226
|
-
}
|
|
227
|
-
pending = prepared.selected;
|
|
228
|
-
if (pending.length === 0) return { sent: 0, dropped };
|
|
229
|
-
|
|
230
|
-
const res = await hubFetch(endpoint, {
|
|
231
|
-
method: 'POST',
|
|
232
|
-
headers: this.getHeaders(),
|
|
233
|
-
body: prepared.body,
|
|
234
|
-
signal: AbortSignal.timeout(30_000),
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
if (res.status === 413) {
|
|
238
|
-
const errText = sanitizeHubErrorMessage(
|
|
239
|
-
await readHubResponseText(res).catch(() => 'request entity too large')
|
|
240
|
-
);
|
|
241
|
-
const error = `Hub 413 outbound payload too large: ${errText}`;
|
|
242
|
-
if (pending.length <= 1) {
|
|
243
|
-
this.store.updateStatusBatch(pending.map(m => ({
|
|
244
|
-
id: m.id,
|
|
245
|
-
status: 'rejected',
|
|
246
|
-
error,
|
|
247
|
-
})));
|
|
248
|
-
const result = {
|
|
249
|
-
sent: 0,
|
|
250
|
-
error: 'hub_payload_too_large',
|
|
251
|
-
payloadTooLarge: true,
|
|
252
|
-
dropped: dropped + pending.length,
|
|
253
|
-
};
|
|
254
|
-
return result;
|
|
255
|
-
}
|
|
256
|
-
const currentCeiling = Math.min(prepared.maxBodyBytes, Math.max(1, prepared.bodyBytes - 1));
|
|
257
|
-
const nextMax = currentCeiling > MIN_ADAPTIVE_OUTBOUND_BODY_BYTES
|
|
258
|
-
? Math.max(MIN_ADAPTIVE_OUTBOUND_BODY_BYTES, Math.floor(currentCeiling / 2))
|
|
259
|
-
: Math.max(1, Math.floor(currentCeiling / 2));
|
|
260
|
-
try { this.store.setState(OUTBOUND_BODY_BYTES_STATE_KEY, nextMax); } catch { /* best effort */ }
|
|
261
|
-
this.logger.warn?.(
|
|
262
|
-
`[outbound] Hub 413 for ${pending.length} messages (${prepared.bodyBytes} bytes); ` +
|
|
263
|
-
`reducing outbound batch budget to ${nextMax} bytes`
|
|
264
|
-
);
|
|
265
|
-
const result = { sent: 0, error: 'hub_payload_too_large', payloadTooLarge: true };
|
|
266
|
-
if (dropped > 0) result.dropped = dropped;
|
|
267
|
-
return result;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
await throwIfHubUnreachableResponse(res, 'outbound flush');
|
|
271
|
-
this._recordHubReachable();
|
|
272
|
-
|
|
273
|
-
if (res.status === 403 || res.status === 401) {
|
|
274
|
-
const errText = sanitizeHubErrorMessage(await readHubResponseText(res).catch(() => 'unknown'));
|
|
275
|
-
throw new AuthError(`Hub ${res.status}: ${errText}`, res.status);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
if (!res.ok) {
|
|
279
|
-
const errText = sanitizeHubErrorMessage(await readHubResponseText(res).catch(() => 'unknown'));
|
|
280
|
-
throw new Error(`Hub returned ${res.status}: ${errText}`);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
const data = await readHubResponseJson(res);
|
|
284
|
-
const results = data.results || [];
|
|
285
|
-
|
|
286
|
-
const updates = [];
|
|
287
|
-
const inboundMessages = [];
|
|
288
|
-
let deferred = 0;
|
|
289
|
-
|
|
290
|
-
for (const r of results) {
|
|
291
|
-
if (r.status === 'accepted' || r.status === 'ok') {
|
|
292
|
-
updates.push({ id: r.id, status: 'synced' });
|
|
293
|
-
} else if (r.status === 'failed' || r.status === 'rejected') {
|
|
294
|
-
const msg = pending.find(m => m.id === r.id);
|
|
295
|
-
const error = sanitizeHubErrorMessage(r.error || r.reason || 'rejected by hub');
|
|
296
|
-
const retryAfterMs = resultRetryAfterMs(r);
|
|
297
|
-
// A `terminal: true` hint from the Hub means "final, do not retry"
|
|
298
|
-
// and must win even when the same result also carries retryAfterMs
|
|
299
|
-
// or retryable (a contradictory but possible payload). Guard the
|
|
300
|
-
// defer branch with !resultIsTerminal so a terminal message is
|
|
301
|
-
// finalized as failed instead of being parked for retry and resent.
|
|
302
|
-
if (msg && !resultIsTerminal(r) && (retryAfterMs !== null || resultIsRetryable(r))) {
|
|
303
|
-
this.store.deferRetry(r.id, error, retryAfterMs || 60_000);
|
|
304
|
-
deferred++;
|
|
305
|
-
} else if (msg && !resultIsTerminal(r) && msg.retry_count < MAX_RETRIES) {
|
|
306
|
-
this.store.incrementRetry(r.id, error);
|
|
307
|
-
} else {
|
|
308
|
-
// Reuse the already-sanitized `error` (r.error / r.reason, else
|
|
309
|
-
// 'rejected by hub'). Only label 'max retries' for the genuine
|
|
310
|
-
// retry-exhaustion path (non-terminal, no explicit Hub error) so a
|
|
311
|
-
// terminal rejection without an error/reason is not mislabeled as
|
|
312
|
-
// retry exhaustion (Bugbot PR #301 Low).
|
|
313
|
-
const exhausted = !resultIsTerminal(r) && !(r.error || r.reason);
|
|
314
|
-
updates.push({ id: r.id, status: 'failed', error: exhausted ? sanitizeHubErrorMessage('max retries') : error });
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
if (r.response) {
|
|
319
|
-
inboundMessages.push({
|
|
320
|
-
type: `${r.original_type || 'unknown'}_result`,
|
|
321
|
-
payload: r.response,
|
|
322
|
-
refId: r.id,
|
|
323
|
-
channel,
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
if (updates.length > 0) this.store.updateStatusBatch(updates);
|
|
329
|
-
if (inboundMessages.length > 0) this.store.writeInboundBatch(inboundMessages);
|
|
330
|
-
|
|
331
|
-
this.store.setState('last_sync_at', new Date().toISOString());
|
|
332
|
-
const result = { sent: pending.length, synced: updates.length, responses: inboundMessages.length };
|
|
333
|
-
if (deferred > 0) result.deferred = deferred;
|
|
334
|
-
if (dropped > 0) result.dropped = dropped;
|
|
335
|
-
return result;
|
|
336
|
-
} catch (err) {
|
|
337
|
-
if (err instanceof AuthError) throw err;
|
|
338
|
-
if (isHubUnreachableError(err)) {
|
|
339
|
-
const retryAfterMs = this._recordHubUnreachable(err);
|
|
340
|
-
const result = {
|
|
341
|
-
sent: 0,
|
|
342
|
-
error: 'hub_unreachable',
|
|
343
|
-
hubUnreachable: true,
|
|
344
|
-
retryAfterMs,
|
|
345
|
-
};
|
|
346
|
-
if (dropped > 0) result.dropped = dropped;
|
|
347
|
-
return result;
|
|
348
|
-
}
|
|
349
|
-
const errMessage = sanitizeHubErrorMessage(err);
|
|
350
|
-
this.logger.error(`[outbound] flush failed: ${errMessage}`);
|
|
351
|
-
for (const m of pending) {
|
|
352
|
-
this.store.incrementRetry(m.id, errMessage);
|
|
353
|
-
}
|
|
354
|
-
const result = { sent: 0, error: errMessage };
|
|
355
|
-
if (dropped > 0) result.dropped = dropped;
|
|
356
|
-
return result;
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
module.exports = { OutboundSync, MAX_BATCH, MAX_RETRIES };
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
class TaskMonitor {
|
|
4
|
-
constructor({ store, logger } = {}) {
|
|
5
|
-
this.store = store;
|
|
6
|
-
this.logger = logger || console;
|
|
7
|
-
this._stats = {
|
|
8
|
-
tasks_received: 0,
|
|
9
|
-
tasks_claimed: 0,
|
|
10
|
-
tasks_completed: 0,
|
|
11
|
-
tasks_failed: 0,
|
|
12
|
-
last_claim_at: null,
|
|
13
|
-
last_complete_at: null,
|
|
14
|
-
avg_completion_ms: 0,
|
|
15
|
-
_completion_times: [],
|
|
16
|
-
};
|
|
17
|
-
this._restoreStats();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
_restoreStats() {
|
|
21
|
-
const raw = this.store.getState('task_monitor_stats');
|
|
22
|
-
if (!raw) return;
|
|
23
|
-
try {
|
|
24
|
-
const saved = typeof raw === 'string' ? JSON.parse(raw) : raw;
|
|
25
|
-
if (saved.tasks_claimed) this._stats.tasks_claimed = saved.tasks_claimed;
|
|
26
|
-
if (saved.tasks_completed) this._stats.tasks_completed = saved.tasks_completed;
|
|
27
|
-
if (saved.tasks_failed) this._stats.tasks_failed = saved.tasks_failed;
|
|
28
|
-
if (saved.tasks_received) this._stats.tasks_received = saved.tasks_received;
|
|
29
|
-
if (saved.last_claim_at) this._stats.last_claim_at = saved.last_claim_at;
|
|
30
|
-
if (saved.last_complete_at) this._stats.last_complete_at = saved.last_complete_at;
|
|
31
|
-
if (saved.avg_completion_ms) this._stats.avg_completion_ms = saved.avg_completion_ms;
|
|
32
|
-
} catch { /* ignore corrupt state */ }
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
get subscribed() {
|
|
36
|
-
const raw = this.store.getState('task_subscription');
|
|
37
|
-
if (!raw) return false;
|
|
38
|
-
try {
|
|
39
|
-
const parsed = typeof raw === 'string' ? JSON.parse(raw) : raw;
|
|
40
|
-
return !!parsed.enabled;
|
|
41
|
-
} catch {
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
subscribe(filters = []) {
|
|
47
|
-
this.store.setState('task_subscription', JSON.stringify({
|
|
48
|
-
enabled: true,
|
|
49
|
-
filters,
|
|
50
|
-
subscribed_at: new Date().toISOString(),
|
|
51
|
-
}));
|
|
52
|
-
const result = this.store.send({
|
|
53
|
-
type: 'task_subscribe',
|
|
54
|
-
payload: { capability_filter: filters },
|
|
55
|
-
});
|
|
56
|
-
return result;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
unsubscribe() {
|
|
60
|
-
this.store.setState('task_subscription', JSON.stringify({
|
|
61
|
-
enabled: false,
|
|
62
|
-
unsubscribed_at: new Date().toISOString(),
|
|
63
|
-
}));
|
|
64
|
-
const result = this.store.send({
|
|
65
|
-
type: 'task_unsubscribe',
|
|
66
|
-
payload: {},
|
|
67
|
-
});
|
|
68
|
-
return result;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
recordClaim(taskId) {
|
|
72
|
-
this._stats.tasks_claimed++;
|
|
73
|
-
this._stats.last_claim_at = Date.now();
|
|
74
|
-
this.store.setState('task_monitor_stats', JSON.stringify(this.getMetrics()));
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
recordComplete(taskId, startedAt) {
|
|
78
|
-
this._stats.tasks_completed++;
|
|
79
|
-
this._stats.last_complete_at = Date.now();
|
|
80
|
-
if (startedAt) {
|
|
81
|
-
const duration = Date.now() - startedAt;
|
|
82
|
-
this._stats._completion_times.push(duration);
|
|
83
|
-
if (this._stats._completion_times.length > 100) {
|
|
84
|
-
this._stats._completion_times.shift();
|
|
85
|
-
}
|
|
86
|
-
const sum = this._stats._completion_times.reduce((a, b) => a + b, 0);
|
|
87
|
-
this._stats.avg_completion_ms = Math.round(sum / this._stats._completion_times.length);
|
|
88
|
-
}
|
|
89
|
-
this.store.setState('task_monitor_stats', JSON.stringify(this.getMetrics()));
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
recordFailed(taskId) {
|
|
93
|
-
this._stats.tasks_failed++;
|
|
94
|
-
this.store.setState('task_monitor_stats', JSON.stringify(this.getMetrics()));
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
recordTaskReceived(count = 1) {
|
|
98
|
-
this._stats.tasks_received += count;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
getMetrics() {
|
|
102
|
-
const pendingTasks = this.store.countPending({ direction: 'inbound' });
|
|
103
|
-
return {
|
|
104
|
-
subscribed: this.subscribed,
|
|
105
|
-
tasks_received: this._stats.tasks_received,
|
|
106
|
-
tasks_claimed: this._stats.tasks_claimed,
|
|
107
|
-
tasks_completed: this._stats.tasks_completed,
|
|
108
|
-
tasks_failed: this._stats.tasks_failed,
|
|
109
|
-
tasks_pending: pendingTasks,
|
|
110
|
-
last_claim_at: this._stats.last_claim_at,
|
|
111
|
-
last_complete_at: this._stats.last_complete_at,
|
|
112
|
-
avg_completion_ms: this._stats.avg_completion_ms,
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
getHeartbeatMeta() {
|
|
117
|
-
const pendingTasks = this.store.countPending({ direction: 'inbound' });
|
|
118
|
-
return {
|
|
119
|
-
task_subscription: this.subscribed,
|
|
120
|
-
task_metrics: {
|
|
121
|
-
pending: pendingTasks,
|
|
122
|
-
claimed: this._stats.tasks_claimed,
|
|
123
|
-
completed: this._stats.tasks_completed,
|
|
124
|
-
failed: this._stats.tasks_failed,
|
|
125
|
-
avg_completion_ms: this._stats.avg_completion_ms,
|
|
126
|
-
},
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
module.exports = { TaskMonitor };
|