@evomap/evolver 1.89.20 → 2.0.0-beta.0

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.
Files changed (231) hide show
  1. package/README.md +27 -564
  2. package/bin/evolver-llm-proxy.js +3 -0
  3. package/bin/evolver-mcp.js +2 -0
  4. package/bin/evolver-proxy.js +3 -0
  5. package/bin/evolver.js +4 -0
  6. package/index.js +1 -3586
  7. package/package.json +32 -62
  8. package/CONTRIBUTING.md +0 -19
  9. package/LICENSE +0 -641
  10. package/README.ja-JP.md +0 -521
  11. package/README.ko-KR.md +0 -520
  12. package/README.zh-CN.md +0 -531
  13. package/SKILL.md +0 -365
  14. package/assets/cover.png +0 -0
  15. package/assets/gep/genes.seed.json +0 -496
  16. package/conformance/savings-core/constants.json +0 -30
  17. package/conformance/savings-core/golden-vectors.json +0 -333
  18. package/scripts/a2a_export.js +0 -63
  19. package/scripts/a2a_ingest.js +0 -79
  20. package/scripts/a2a_promote.js +0 -118
  21. package/scripts/analyze_by_skill.js +0 -121
  22. package/scripts/build_binaries.js +0 -479
  23. package/scripts/check-changelog.js +0 -166
  24. package/scripts/extract_log.js +0 -85
  25. package/scripts/generate_history.js +0 -75
  26. package/scripts/gep_append_event.js +0 -96
  27. package/scripts/gep_personality_report.js +0 -234
  28. package/scripts/human_report.js +0 -147
  29. package/scripts/recall-verify-report.js +0 -234
  30. package/scripts/recover_loop.js +0 -61
  31. package/scripts/refresh_stars_badge.js +0 -168
  32. package/scripts/seed-merchants.js +0 -91
  33. package/scripts/skill2recipes.js +0 -118
  34. package/scripts/suggest_version.js +0 -89
  35. package/scripts/validate-modules.js +0 -38
  36. package/scripts/validate-suite.js +0 -78
  37. package/skills/_meta/SKILL.md +0 -41
  38. package/skills/index.json +0 -14
  39. package/src/adapters/claudeCode.js +0 -194
  40. package/src/adapters/codex.js +0 -216
  41. package/src/adapters/cursor.js +0 -91
  42. package/src/adapters/hookAdapter.js +0 -469
  43. package/src/adapters/kiro.js +0 -195
  44. package/src/adapters/opencode.js +0 -326
  45. package/src/adapters/scripts/_lockPaths.js +0 -74
  46. package/src/adapters/scripts/_memoryFiltering.js +0 -35
  47. package/src/adapters/scripts/_runtimePaths.js +0 -440
  48. package/src/adapters/scripts/evolver-session-end.js +0 -321
  49. package/src/adapters/scripts/evolver-session-start.js +0 -587
  50. package/src/adapters/scripts/evolver-signal-detect.js +0 -98
  51. package/src/adapters/scripts/evolver-task-recall.js +0 -173
  52. package/src/atp/atpExecute.js +0 -283
  53. package/src/atp/atpTaskPickup.js +0 -233
  54. package/src/atp/autoBuyer.js +0 -382
  55. package/src/atp/autoDeliver.js +0 -215
  56. package/src/atp/cli.js +0 -354
  57. package/src/atp/cliAutobuyPrompt.js +0 -154
  58. package/src/atp/consumerAgent.js +0 -157
  59. package/src/atp/defaultHandler.js +0 -69
  60. package/src/atp/heartbeatSignalsHandler.js +0 -254
  61. package/src/atp/hubClient.js +0 -317
  62. package/src/atp/index.js +0 -38
  63. package/src/atp/merchantAgent.js +0 -118
  64. package/src/atp/protocol.js +0 -41
  65. package/src/atp/questionComposer.js +0 -133
  66. package/src/atp/serviceHelper.js +0 -92
  67. package/src/canary.js +0 -13
  68. package/src/config.js +0 -351
  69. package/src/evolve/guards.js +0 -1
  70. package/src/evolve/pipeline/collect.js +0 -1
  71. package/src/evolve/pipeline/dispatch.js +0 -1
  72. package/src/evolve/pipeline/enrich.js +0 -1
  73. package/src/evolve/pipeline/hub.js +0 -1
  74. package/src/evolve/pipeline/select.js +0 -1
  75. package/src/evolve/pipeline/signals.js +0 -1
  76. package/src/evolve/utils.js +0 -1
  77. package/src/evolve.js +0 -1
  78. package/src/experiment/agentRunner.js +0 -229
  79. package/src/experiment/cli.js +0 -159
  80. package/src/experiment/comparison.js +0 -233
  81. package/src/experiment/metrics.js +0 -75
  82. package/src/forceUpdate.js +0 -991
  83. package/src/gep/a2a.js +0 -173
  84. package/src/gep/a2aProtocol.js +0 -1
  85. package/src/gep/analyzer.js +0 -35
  86. package/src/gep/antiAbuseTelemetry.js +0 -1
  87. package/src/gep/assetCallLog.js +0 -197
  88. package/src/gep/assetStore.js +0 -723
  89. package/src/gep/assets.js +0 -36
  90. package/src/gep/autoDistillConv.js +0 -1
  91. package/src/gep/autoDistillLlm.js +0 -1
  92. package/src/gep/bridge.js +0 -138
  93. package/src/gep/candidateEval.js +0 -1
  94. package/src/gep/candidates.js +0 -1
  95. package/src/gep/claimNudge.js +0 -121
  96. package/src/gep/cliContracts.js +0 -1190
  97. package/src/gep/contentHash.js +0 -1
  98. package/src/gep/conversationDistiller.js +0 -1
  99. package/src/gep/conversationSniffer.js +0 -1
  100. package/src/gep/crypto.js +0 -1
  101. package/src/gep/curriculum.js +0 -1
  102. package/src/gep/deviceId.js +0 -1
  103. package/src/gep/directoryClient.js +0 -115
  104. package/src/gep/envFingerprint.js +0 -1
  105. package/src/gep/epigenetics.js +0 -1
  106. package/src/gep/execBridge.js +0 -1
  107. package/src/gep/executionTrace.js +0 -291
  108. package/src/gep/explore.js +0 -1
  109. package/src/gep/featureFlags.js +0 -121
  110. package/src/gep/gitOps.js +0 -265
  111. package/src/gep/hash.js +0 -1
  112. package/src/gep/hostErrorClassifier.js +0 -34
  113. package/src/gep/hubFetch.js +0 -1
  114. package/src/gep/hubReview.js +0 -1
  115. package/src/gep/hubSearch.js +0 -1
  116. package/src/gep/hubVerify.js +0 -1
  117. package/src/gep/idleScheduler.js +0 -400
  118. package/src/gep/issueReporter.js +0 -416
  119. package/src/gep/learningSignals.js +0 -1
  120. package/src/gep/llmReview.js +0 -92
  121. package/src/gep/localStateAwareness.js +0 -243
  122. package/src/gep/mailboxTransport.js +0 -119
  123. package/src/gep/memoryGraph.js +0 -1
  124. package/src/gep/memoryGraphAdapter.js +0 -1
  125. package/src/gep/mutation.js +0 -1
  126. package/src/gep/narrativeMemory.js +0 -1
  127. package/src/gep/oauthLogin.js +0 -181
  128. package/src/gep/openPRRegistry.js +0 -1
  129. package/src/gep/paths.js +0 -522
  130. package/src/gep/personality.js +0 -1
  131. package/src/gep/policyCheck.js +0 -1
  132. package/src/gep/portable.js +0 -103
  133. package/src/gep/privacyClient.js +0 -235
  134. package/src/gep/prompt.js +0 -1
  135. package/src/gep/questionGenerator.js +0 -518
  136. package/src/gep/recallInject.js +0 -1
  137. package/src/gep/recallVerifier.js +0 -1
  138. package/src/gep/reflection.js +0 -1
  139. package/src/gep/sanitize.js +0 -264
  140. package/src/gep/savingsCore.js +0 -1
  141. package/src/gep/schemas/capsule.js +0 -170
  142. package/src/gep/schemas/gene.js +0 -154
  143. package/src/gep/schemas/index.js +0 -8
  144. package/src/gep/schemas/protocol.js +0 -51
  145. package/src/gep/schemas/task.js +0 -74
  146. package/src/gep/selector.js +0 -1
  147. package/src/gep/selfPR.js +0 -469
  148. package/src/gep/signals.js +0 -776
  149. package/src/gep/skill2gep.js +0 -1056
  150. package/src/gep/skill2gepAudit.js +0 -303
  151. package/src/gep/skill2recipes.js +0 -511
  152. package/src/gep/skillDistiller.js +0 -1
  153. package/src/gep/skillPublisher.js +0 -358
  154. package/src/gep/solidify.js +0 -1
  155. package/src/gep/strategy.js +0 -1
  156. package/src/gep/taskReceiver.js +0 -575
  157. package/src/gep/tokenSavings.js +0 -1
  158. package/src/gep/trajectoryExport.js +0 -1
  159. package/src/gep/validationReport.js +0 -55
  160. package/src/gep/validator/index.js +0 -411
  161. package/src/gep/validator/reporter.js +0 -210
  162. package/src/gep/validator/sandboxExecutor.js +0 -480
  163. package/src/gep/validator/stakeBootstrap.js +0 -357
  164. package/src/gep/workspaceKeychain.js +0 -1
  165. package/src/ops/cleanup.js +0 -80
  166. package/src/ops/commentary.js +0 -60
  167. package/src/ops/health_check.js +0 -104
  168. package/src/ops/index.js +0 -11
  169. package/src/ops/innovation.js +0 -67
  170. package/src/ops/lifecycle.js +0 -798
  171. package/src/ops/self_repair.js +0 -76
  172. package/src/ops/skills_monitor.js +0 -147
  173. package/src/ops/trigger.js +0 -33
  174. package/src/proxy/clientSettings.js +0 -405
  175. package/src/proxy/envelope.js +0 -59
  176. package/src/proxy/extensions/dmHandler.js +0 -45
  177. package/src/proxy/extensions/sessionHandler.js +0 -141
  178. package/src/proxy/extensions/skillUpdater.js +0 -64
  179. package/src/proxy/extensions/traceControl.js +0 -1
  180. package/src/proxy/index.js +0 -1395
  181. package/src/proxy/inject.js +0 -1
  182. package/src/proxy/lifecycle/manager.js +0 -1568
  183. package/src/proxy/mailbox/state.js +0 -207
  184. package/src/proxy/mailbox/store.js +0 -590
  185. package/src/proxy/router/cache_passthrough.js +0 -26
  186. package/src/proxy/router/features.js +0 -84
  187. package/src/proxy/router/gemini_route.js +0 -154
  188. package/src/proxy/router/messages_route.js +0 -535
  189. package/src/proxy/router/model_router.js +0 -113
  190. package/src/proxy/router/models_route.js +0 -52
  191. package/src/proxy/router/ollama_route.js +0 -103
  192. package/src/proxy/router/responses_route.js +0 -170
  193. package/src/proxy/router/vertex_route.js +0 -110
  194. package/src/proxy/server/http.js +0 -363
  195. package/src/proxy/server/routes.js +0 -558
  196. package/src/proxy/server/settings.js +0 -115
  197. package/src/proxy/sync/engine.js +0 -179
  198. package/src/proxy/sync/inbound.js +0 -211
  199. package/src/proxy/sync/outbound.js +0 -320
  200. package/src/proxy/task/monitor.js +0 -131
  201. package/src/proxy/trace/extractor.js +0 -1
  202. package/src/proxy/trace/usage.js +0 -1
  203. package/src/webui/client/clientJs/assets.js +0 -111
  204. package/src/webui/client/clientJs/bootstrap.js +0 -92
  205. package/src/webui/client/clientJs/common.js +0 -77
  206. package/src/webui/client/clientJs/i18n.js +0 -366
  207. package/src/webui/client/clientJs/index.js +0 -35
  208. package/src/webui/client/clientJs/interactions.js +0 -351
  209. package/src/webui/client/clientJs/overview.js +0 -152
  210. package/src/webui/client/clientJs/personality.js +0 -285
  211. package/src/webui/client/clientJs/pipelines.js +0 -330
  212. package/src/webui/client/indexHtml.js +0 -221
  213. package/src/webui/client/static.js +0 -23
  214. package/src/webui/client/stylesCss.js +0 -639
  215. package/src/webui/client/vendor/README.md +0 -15
  216. package/src/webui/client/vendor/echarts.min.js +0 -45
  217. package/src/webui/index.js +0 -14
  218. package/src/webui/observer/assets.js +0 -146
  219. package/src/webui/observer/index.js +0 -37
  220. package/src/webui/observer/interactions.js +0 -127
  221. package/src/webui/observer/jsonl.js +0 -75
  222. package/src/webui/observer/paths.js +0 -46
  223. package/src/webui/observer/personality.js +0 -43
  224. package/src/webui/observer/pipelineEvents.js +0 -58
  225. package/src/webui/observer/redact.js +0 -63
  226. package/src/webui/observer/runs.js +0 -356
  227. package/src/webui/observer/safety.js +0 -57
  228. package/src/webui/observer/skills.js +0 -70
  229. package/src/webui/observer/status.js +0 -71
  230. package/src/webui/server/http.js +0 -138
  231. package/src/webui/server/routes.js +0 -41
@@ -1,179 +0,0 @@
1
- 'use strict';
2
-
3
- const { OutboundSync } = require('./outbound');
4
- const { InboundSync, DEFAULT_POLL_INTERVAL_ACTIVE, DEFAULT_POLL_INTERVAL_IDLE } = require('./inbound');
5
- const { AuthError } = require('../lifecycle/manager');
6
-
7
- const DEFAULT_OUTBOUND_INTERVAL = 5_000;
8
- const IDLE_THRESHOLD = 5 * 60_000;
9
-
10
- class SyncEngine {
11
- constructor({ store, hubUrl, getHeaders, logger, onInboundReceived, onAuthError, onOutboundFlushed }) {
12
- this.store = store;
13
- this.hubUrl = hubUrl;
14
- this.logger = logger || console;
15
- this.getHeaders = getHeaders;
16
- this.onInboundReceived = onInboundReceived || null;
17
- this.onAuthError = onAuthError || null;
18
- this.onOutboundFlushed = onOutboundFlushed || null;
19
-
20
- this.outbound = new OutboundSync({ store, hubUrl, getHeaders, logger });
21
- this.inbound = new InboundSync({ store, hubUrl, getHeaders, logger });
22
-
23
- this._outTimer = null;
24
- this._inTimer = null;
25
- this._running = false;
26
- this._lastActivity = Date.now();
27
- }
28
-
29
- start() {
30
- if (this._running) return;
31
- this._running = true;
32
- this._lastActivity = Date.now();
33
- this._scheduleOutbound(500);
34
- this._scheduleInbound(1_000);
35
- this.logger.log('[sync] engine started');
36
- }
37
-
38
- stop() {
39
- this._running = false;
40
- if (this._outTimer) { clearTimeout(this._outTimer); this._outTimer = null; }
41
- if (this._inTimer) { clearTimeout(this._inTimer); this._inTimer = null; }
42
- this.logger.log('[sync] engine stopped');
43
- }
44
-
45
- notifyNewOutbound() {
46
- this._lastActivity = Date.now();
47
- if (this._running && !this._outPending) {
48
- if (this._outTimer) clearTimeout(this._outTimer);
49
- this._scheduleOutbound(100);
50
- }
51
- }
52
-
53
- _isIdle() {
54
- return (Date.now() - this._lastActivity) > IDLE_THRESHOLD;
55
- }
56
-
57
- async _handleAuthError(source) {
58
- this.logger.error(`[sync] auth error from ${source}, triggering re-authentication`);
59
- if (typeof this.onAuthError === 'function') {
60
- try { await this.onAuthError(); } catch (e) {
61
- this.logger.error(`[sync] onAuthError callback failed: ${e.message}`);
62
- }
63
- }
64
- }
65
-
66
- _scheduleOutbound(delayMs) {
67
- if (!this._running) return;
68
- this._outTimer = setTimeout(async () => {
69
- // Defence-in-depth: a throw from this.outbound.flush(),
70
- // this.store.countPending(), or any post-flush bookkeeping used
71
- // to escape the setTimeout callback. Node logs the unhandled
72
- // rejection and the next setTimeout was never armed — the
73
- // outbound sync loop silently died until the process restarted,
74
- // while `_running` stayed true (no signal to the caller).
75
- //
76
- // Mirrors the heartbeat-loop fix in PR #147 (issue #544): wrap
77
- // the whole tick, schedule the next iteration in `finally` so a
78
- // surprise throw cannot park the loop.
79
- let nextDelay = DEFAULT_OUTBOUND_INTERVAL;
80
- let hubRetryAfterMs = 0;
81
- try {
82
- if (!this._running) return;
83
- this._outPending = true;
84
- try {
85
- const result = await this.outbound.flush();
86
- if (result.sent > 0) this._lastActivity = Date.now();
87
- if (result.retryAfterMs > 0) hubRetryAfterMs = result.retryAfterMs;
88
- if ((result.sent > 0 || result.dropped > 0) && typeof this.onOutboundFlushed === 'function') {
89
- try { this.onOutboundFlushed(result); } catch (e) {
90
- this.logger.warn?.('[sync] onOutboundFlushed callback failed:', e.message);
91
- }
92
- }
93
- } catch (err) {
94
- if (err instanceof AuthError) {
95
- await this._handleAuthError('outbound');
96
- } else {
97
- this.logger.error(`[sync] outbound error: ${err.message}`);
98
- }
99
- }
100
- this._outPending = false;
101
- if (hubRetryAfterMs > 0) {
102
- nextDelay = Math.max(1_000, hubRetryAfterMs);
103
- } else {
104
- try {
105
- const pending = this.store.countPending({ direction: 'outbound' });
106
- if (pending > 0) nextDelay = 1_000;
107
- } catch (err) {
108
- // countPending threw (corrupt store, FS hiccup): keep the
109
- // default cadence rather than parking the loop.
110
- this.logger.error(`[sync] countPending threw (non-fatal): ${err && err.message}`);
111
- }
112
- }
113
- } catch (err) {
114
- // Anything that escaped the inner blocks above. Log and let
115
- // finally re-arm the timer.
116
- this.logger.error(`[sync] outbound tick threw (non-fatal): ${err && err.message}`);
117
- this._outPending = false;
118
- } finally {
119
- if (this._running) this._scheduleOutbound(nextDelay);
120
- }
121
- }, delayMs);
122
- if (this._outTimer.unref) this._outTimer.unref();
123
- }
124
-
125
- _scheduleInbound(delayMs) {
126
- if (!this._running) return;
127
- this._inTimer = setTimeout(async () => {
128
- // Same defence-in-depth pattern as _scheduleOutbound: a throw
129
- // from inbound.pull / ackDelivered / _isIdle used to escape the
130
- // setTimeout callback and silently park the inbound loop.
131
- let nextDelay = DEFAULT_POLL_INTERVAL_ACTIVE;
132
- let hubRetryAfterMs = 0;
133
- try {
134
- if (!this._running) return;
135
- try {
136
- const result = await this.inbound.pull();
137
- if (result.retryAfterMs > 0) {
138
- hubRetryAfterMs = result.retryAfterMs;
139
- } else if (result.received > 0) {
140
- this._lastActivity = Date.now();
141
- if (typeof this.onInboundReceived === 'function') {
142
- try { this.onInboundReceived(result.received); } catch (e) {
143
- this.logger.warn?.('[sync] onInboundReceived callback failed:', e.message);
144
- }
145
- }
146
- }
147
- if (!hubRetryAfterMs) {
148
- const ack = await this.inbound.ackDelivered();
149
- if (ack.retryAfterMs > 0) hubRetryAfterMs = ack.retryAfterMs;
150
- }
151
- } catch (err) {
152
- if (err instanceof AuthError) {
153
- await this._handleAuthError('inbound');
154
- } else {
155
- this.logger.error(`[sync] inbound error: ${err.message}`);
156
- }
157
- }
158
- if (hubRetryAfterMs > 0) {
159
- nextDelay = Math.max(1_000, hubRetryAfterMs);
160
- } else {
161
- try {
162
- nextDelay = this._isIdle()
163
- ? DEFAULT_POLL_INTERVAL_IDLE
164
- : DEFAULT_POLL_INTERVAL_ACTIVE;
165
- } catch (err) {
166
- this.logger.error(`[sync] _isIdle threw (non-fatal): ${err && err.message}`);
167
- }
168
- }
169
- } catch (err) {
170
- this.logger.error(`[sync] inbound tick threw (non-fatal): ${err && err.message}`);
171
- } finally {
172
- if (this._running) this._scheduleInbound(nextDelay);
173
- }
174
- }, delayMs);
175
- if (this._inTimer.unref) this._inTimer.unref();
176
- }
177
- }
178
-
179
- module.exports = { SyncEngine, DEFAULT_OUTBOUND_INTERVAL };
@@ -1,211 +0,0 @@
1
- 'use strict';
2
-
3
- const { PROXY_PROTOCOL_VERSION } = require('../mailbox/store');
4
- const { AuthError } = require('../lifecycle/manager');
5
- const {
6
- drainHubResponse,
7
- hubFetch,
8
- hubUnreachableBackoffMs,
9
- isHubUnreachableError,
10
- readHubResponseJson,
11
- readHubResponseText,
12
- sanitizeHubResponseForLog,
13
- throwIfHubUnreachableResponse,
14
- } = require('../../gep/hubFetch');
15
-
16
- const DEFAULT_POLL_INTERVAL_ACTIVE = 10_000;
17
- const DEFAULT_POLL_INTERVAL_IDLE = 60_000;
18
-
19
- function toInboundStoreMessage(m, channel) {
20
- return {
21
- id: m.id,
22
- type: m.type,
23
- payload: m.payload,
24
- channel,
25
- priority: m.priority || 'normal',
26
- refId: m.ref_id,
27
- expiresAt: m.expires_at,
28
- };
29
- }
30
-
31
- class InboundSync {
32
- constructor({ store, hubUrl, getHeaders, logger }) {
33
- this.store = store;
34
- this.hubUrl = hubUrl;
35
- this.logger = logger || console;
36
- this.getHeaders = getHeaders;
37
- this._hubUnreachableFailures = 0;
38
- this._hubUnreachableUntil = 0;
39
- }
40
-
41
- _hubUnreachableWaitMs() {
42
- return Math.max(0, this._hubUnreachableUntil - Date.now());
43
- }
44
-
45
- _recordHubReachable() {
46
- this._hubUnreachableFailures = 0;
47
- this._hubUnreachableUntil = 0;
48
- }
49
-
50
- _recordHubUnreachable(err) {
51
- this._hubUnreachableFailures += 1;
52
- const retryAfterMs = hubUnreachableBackoffMs(this._hubUnreachableFailures);
53
- this._hubUnreachableUntil = Date.now() + retryAfterMs;
54
- this.logger.warn?.(
55
- `[inbound] Hub unreachable; backing off for ${Math.ceil(retryAfterMs / 1000)}s: ` +
56
- `${err && err.message || err}`
57
- );
58
- return retryAfterMs;
59
- }
60
-
61
- async pull(channel = 'evomap-hub', limit = 50) {
62
- const waitMs = this._hubUnreachableWaitMs();
63
- if (waitMs > 0) {
64
- return {
65
- received: 0,
66
- error: 'hub_unreachable_backoff',
67
- hubUnreachable: true,
68
- retryAfterMs: waitMs,
69
- };
70
- }
71
-
72
- const cursorKey = `${channel}:inbound_cursor`;
73
- const cursor = this.store.getCursor(cursorKey);
74
-
75
- const endpoint = `${this.hubUrl}/a2a/mailbox/inbound`;
76
-
77
- try {
78
- const senderId = this.store.getState('node_id');
79
- const res = await hubFetch(endpoint, {
80
- method: 'POST',
81
- headers: this.getHeaders(),
82
- body: JSON.stringify({ sender_id: senderId, proxy_protocol_version: PROXY_PROTOCOL_VERSION, cursor, limit }),
83
- signal: AbortSignal.timeout(35_000),
84
- });
85
-
86
- await throwIfHubUnreachableResponse(res, 'inbound pull');
87
- this._recordHubReachable();
88
-
89
- if (res.status === 403 || res.status === 401) {
90
- const errText = await readHubResponseText(res).catch(() => 'unknown');
91
- throw new AuthError(`Hub ${res.status}: ${sanitizeHubResponseForLog(errText)}`, res.status);
92
- }
93
-
94
- if (!res.ok) {
95
- const errText = await readHubResponseText(res).catch(() => 'unknown');
96
- throw new Error(`Hub returned ${res.status}: ${sanitizeHubResponseForLog(errText)}`);
97
- }
98
-
99
- const data = await readHubResponseJson(res);
100
- const messages = data.messages || [];
101
- let stored = 0;
102
- let dropped = 0;
103
-
104
- if (messages.length > 0) {
105
- for (const m of messages) {
106
- try {
107
- this.store.writeInbound(toInboundStoreMessage(m, channel));
108
- stored++;
109
- } catch (err) {
110
- if (err && err.code === 'MAILBOX_JSONL_LINE_TOO_LARGE') {
111
- dropped++;
112
- const msgId = m && m.id ? String(m.id) : '(missing id)';
113
- this.logger.warn?.(`[inbound] dropped oversized inbound message ${msgId}: ${err.message}`);
114
- continue;
115
- }
116
- throw err;
117
- }
118
- }
119
- }
120
-
121
- if (data.next_cursor) {
122
- this.store.setCursor(cursorKey, data.next_cursor);
123
- }
124
-
125
- const result = { received: stored, cursor: data.next_cursor || cursor };
126
- if (dropped > 0) result.dropped = dropped;
127
- return result;
128
- } catch (err) {
129
- if (err instanceof AuthError) throw err;
130
- if (isHubUnreachableError(err)) {
131
- const retryAfterMs = this._recordHubUnreachable(err);
132
- return {
133
- received: 0,
134
- error: 'hub_unreachable',
135
- hubUnreachable: true,
136
- retryAfterMs,
137
- };
138
- }
139
- this.logger.error(`[inbound] pull failed: ${err.message}`);
140
- return { received: 0, error: err.message };
141
- }
142
- }
143
-
144
- async ackDelivered(channel = 'evomap-hub') {
145
- const waitMs = this._hubUnreachableWaitMs();
146
- if (waitMs > 0) {
147
- return {
148
- acked: 0,
149
- error: 'hub_unreachable_backoff',
150
- hubUnreachable: true,
151
- retryAfterMs: waitMs,
152
- };
153
- }
154
-
155
- const delivered = this.store.list({
156
- type: '%',
157
- direction: 'inbound',
158
- status: 'delivered',
159
- limit: 100,
160
- }).filter(m => m.channel === channel);
161
-
162
- if (delivered.length === 0) return { acked: 0 };
163
-
164
- const endpoint = `${this.hubUrl}/a2a/mailbox/ack`;
165
-
166
- try {
167
- const senderId = this.store.getState('node_id');
168
- // Round-8 (§21.5): drain the response body so the undici long-poll
169
- // dispatcher pool is not leaked one socket per ack. ackDelivered
170
- // is called every inbound poll cycle (default 1-10s); the
171
- // pre-round-8 code captured no reference to res and never called
172
- // .json()/.text()/body.cancel(), so each ack pinned a socket
173
- // until GC. After a few minutes of activity the strict-pool was
174
- // exhausted and proxy-mode heartbeats hung on next acquire --
175
- // matches the "alive once then dead" user symptom in proxy mode.
176
- const res = await hubFetch(endpoint, {
177
- method: 'POST',
178
- headers: this.getHeaders(),
179
- body: JSON.stringify({ sender_id: senderId, message_ids: delivered.map(m => m.id) }),
180
- signal: AbortSignal.timeout(10_000),
181
- });
182
- await throwIfHubUnreachableResponse(res, 'inbound ack');
183
- this._recordHubReachable();
184
- if (res.status === 403 || res.status === 401) {
185
- const errText = await readHubResponseText(res).catch(() => 'unknown');
186
- throw new AuthError(`Hub ${res.status}: ${sanitizeHubResponseForLog(errText)}`, res.status);
187
- }
188
- if (!res.ok) {
189
- const errText = await readHubResponseText(res).catch(() => 'unknown');
190
- throw new Error(`Hub returned ${res.status}: ${sanitizeHubResponseForLog(errText)}`);
191
- }
192
- await drainHubResponse(res);
193
- return { acked: delivered.length };
194
- } catch (err) {
195
- if (err instanceof AuthError) throw err;
196
- if (isHubUnreachableError(err)) {
197
- const retryAfterMs = this._recordHubUnreachable(err);
198
- return {
199
- acked: 0,
200
- error: 'hub_unreachable',
201
- hubUnreachable: true,
202
- retryAfterMs,
203
- };
204
- }
205
- this.logger.error(`[inbound] ack failed: ${err.message}`);
206
- return { acked: 0, error: err.message };
207
- }
208
- }
209
- }
210
-
211
- module.exports = { InboundSync, DEFAULT_POLL_INTERVAL_ACTIVE, DEFAULT_POLL_INTERVAL_IDLE };
@@ -1,320 +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
- class OutboundSync {
124
- constructor({ store, hubUrl, getHeaders, logger }) {
125
- this.store = store;
126
- this.hubUrl = hubUrl;
127
- this.logger = logger || console;
128
- this.getHeaders = getHeaders;
129
- this._hubUnreachableFailures = 0;
130
- this._hubUnreachableUntil = 0;
131
- }
132
-
133
- _hubUnreachableWaitMs() {
134
- return Math.max(0, this._hubUnreachableUntil - Date.now());
135
- }
136
-
137
- _recordHubReachable() {
138
- this._hubUnreachableFailures = 0;
139
- this._hubUnreachableUntil = 0;
140
- }
141
-
142
- _recordHubUnreachable(err) {
143
- this._hubUnreachableFailures += 1;
144
- const retryAfterMs = hubUnreachableBackoffMs(this._hubUnreachableFailures);
145
- this._hubUnreachableUntil = Date.now() + retryAfterMs;
146
- this.logger.warn?.(
147
- `[outbound] Hub unreachable; backing off for ${Math.ceil(retryAfterMs / 1000)}s: ` +
148
- `${err && err.message || err}`
149
- );
150
- return retryAfterMs;
151
- }
152
-
153
- async flush(channel = 'evomap-hub') {
154
- const waitMs = this._hubUnreachableWaitMs();
155
- if (waitMs > 0) {
156
- return {
157
- sent: 0,
158
- error: 'hub_unreachable_backoff',
159
- hubUnreachable: true,
160
- retryAfterMs: waitMs,
161
- };
162
- }
163
-
164
- const pendingBatch = this.store.pollOutbound({ channel, limit: MAX_BATCH });
165
- if (pendingBatch.length === 0) return { sent: 0 };
166
-
167
- let pending = pendingBatch;
168
- const rejectedTraceUploads = [];
169
- const traceUploadEnabled = resolveTraceMode(process.env, { store: this.store });
170
- for (const m of pendingBatch) {
171
- if (m.type !== 'proxy_trace') continue;
172
- if (!traceUploadEnabled) {
173
- rejectedTraceUploads.push({ id: m.id, error: 'proxy trace upload disabled' });
174
- } else if (!isProxyTraceUploadPayloadAllowed(m.payload, process.env, { store: this.store })) {
175
- rejectedTraceUploads.push({ id: m.id, error: 'proxy trace payload rejected' });
176
- }
177
- }
178
- if (rejectedTraceUploads.length > 0) {
179
- this.store.updateStatusBatch(rejectedTraceUploads.map(m => ({
180
- id: m.id,
181
- status: 'rejected',
182
- error: m.error,
183
- })));
184
- const rejectedIds = new Set(rejectedTraceUploads.map(m => m.id));
185
- pending = pendingBatch.filter(m => !rejectedIds.has(m.id));
186
- if (pending.length === 0) return { sent: 0, dropped: rejectedTraceUploads.length };
187
- }
188
- let dropped = rejectedTraceUploads.length;
189
-
190
- try {
191
- const senderId = this.store.getState('node_id');
192
- const endpoint = outboundEndpoint(this.hubUrl, senderId);
193
- const prepared = buildSizedOutboundBatch(
194
- senderId,
195
- pending,
196
- resolveMaxOutboundBodyBytes(process.env, this.store),
197
- resolveHardOutboundBodyBytes(process.env)
198
- );
199
- if (prepared.rejected.length > 0) {
200
- this.store.updateStatusBatch(prepared.rejected);
201
- dropped += prepared.rejected.length;
202
- }
203
- pending = prepared.selected;
204
- if (pending.length === 0) return { sent: 0, dropped };
205
-
206
- const res = await hubFetch(endpoint, {
207
- method: 'POST',
208
- headers: this.getHeaders(),
209
- body: prepared.body,
210
- signal: AbortSignal.timeout(30_000),
211
- });
212
-
213
- if (res.status === 413) {
214
- const errText = sanitizeHubErrorMessage(
215
- await readHubResponseText(res).catch(() => 'request entity too large')
216
- );
217
- const error = `Hub 413 outbound payload too large: ${errText}`;
218
- if (pending.length <= 1) {
219
- this.store.updateStatusBatch(pending.map(m => ({
220
- id: m.id,
221
- status: 'rejected',
222
- error,
223
- })));
224
- const result = {
225
- sent: 0,
226
- error: 'hub_payload_too_large',
227
- payloadTooLarge: true,
228
- dropped: dropped + pending.length,
229
- };
230
- return result;
231
- }
232
- const currentCeiling = Math.min(prepared.maxBodyBytes, Math.max(1, prepared.bodyBytes - 1));
233
- const nextMax = currentCeiling > MIN_ADAPTIVE_OUTBOUND_BODY_BYTES
234
- ? Math.max(MIN_ADAPTIVE_OUTBOUND_BODY_BYTES, Math.floor(currentCeiling / 2))
235
- : Math.max(1, Math.floor(currentCeiling / 2));
236
- try { this.store.setState(OUTBOUND_BODY_BYTES_STATE_KEY, nextMax); } catch { /* best effort */ }
237
- this.logger.warn?.(
238
- `[outbound] Hub 413 for ${pending.length} messages (${prepared.bodyBytes} bytes); ` +
239
- `reducing outbound batch budget to ${nextMax} bytes`
240
- );
241
- const result = { sent: 0, error: 'hub_payload_too_large', payloadTooLarge: true };
242
- if (dropped > 0) result.dropped = dropped;
243
- return result;
244
- }
245
-
246
- await throwIfHubUnreachableResponse(res, 'outbound flush');
247
- this._recordHubReachable();
248
-
249
- if (res.status === 403 || res.status === 401) {
250
- const errText = sanitizeHubErrorMessage(await readHubResponseText(res).catch(() => 'unknown'));
251
- throw new AuthError(`Hub ${res.status}: ${errText}`, res.status);
252
- }
253
-
254
- if (!res.ok) {
255
- const errText = sanitizeHubErrorMessage(await readHubResponseText(res).catch(() => 'unknown'));
256
- throw new Error(`Hub returned ${res.status}: ${errText}`);
257
- }
258
-
259
- const data = await readHubResponseJson(res);
260
- const results = data.results || [];
261
-
262
- const updates = [];
263
- const inboundMessages = [];
264
-
265
- for (const r of results) {
266
- if (r.status === 'accepted' || r.status === 'ok') {
267
- updates.push({ id: r.id, status: 'synced' });
268
- } else if (r.status === 'failed' || r.status === 'rejected') {
269
- const msg = pending.find(m => m.id === r.id);
270
- const error = sanitizeHubErrorMessage(r.error || 'rejected by hub');
271
- if (msg && msg.retry_count < MAX_RETRIES) {
272
- this.store.incrementRetry(r.id, error);
273
- } else {
274
- updates.push({ id: r.id, status: 'failed', error: sanitizeHubErrorMessage(r.error || 'max retries') });
275
- }
276
- }
277
-
278
- if (r.response) {
279
- inboundMessages.push({
280
- type: `${r.original_type || 'unknown'}_result`,
281
- payload: r.response,
282
- refId: r.id,
283
- channel,
284
- });
285
- }
286
- }
287
-
288
- if (updates.length > 0) this.store.updateStatusBatch(updates);
289
- if (inboundMessages.length > 0) this.store.writeInboundBatch(inboundMessages);
290
-
291
- this.store.setState('last_sync_at', new Date().toISOString());
292
- const result = { sent: pending.length, synced: updates.length, responses: inboundMessages.length };
293
- if (dropped > 0) result.dropped = dropped;
294
- return result;
295
- } catch (err) {
296
- if (err instanceof AuthError) throw err;
297
- if (isHubUnreachableError(err)) {
298
- const retryAfterMs = this._recordHubUnreachable(err);
299
- const result = {
300
- sent: 0,
301
- error: 'hub_unreachable',
302
- hubUnreachable: true,
303
- retryAfterMs,
304
- };
305
- if (dropped > 0) result.dropped = dropped;
306
- return result;
307
- }
308
- const errMessage = sanitizeHubErrorMessage(err);
309
- this.logger.error(`[outbound] flush failed: ${errMessage}`);
310
- for (const m of pending) {
311
- this.store.incrementRetry(m.id, errMessage);
312
- }
313
- const result = { sent: 0, error: errMessage };
314
- if (dropped > 0) result.dropped = dropped;
315
- return result;
316
- }
317
- }
318
- }
319
-
320
- module.exports = { OutboundSync, MAX_BATCH, MAX_RETRIES };