@evomap/evolver-proxy 2.0.0-beta.1 → 2.0.0-beta.4

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 (37) hide show
  1. package/dist/bin/evolver-proxy.d.ts +67 -6
  2. package/dist/bin/evolver-proxy.js +389 -75
  3. package/dist/bin/proxySettings.d.ts +2 -0
  4. package/dist/bin/proxySettings.js +8 -1
  5. package/dist/daemon/collaborationFacade.d.ts +56 -0
  6. package/dist/daemon/collaborationFacade.js +877 -0
  7. package/dist/daemon/proxyDaemon.d.ts +3 -0
  8. package/dist/daemon/proxyDaemon.js +111 -0
  9. package/dist/daemon/selectHub.js +17 -1
  10. package/dist/llm/traceControl.js +1 -1
  11. package/dist/private/adapterLoader.d.ts +6 -1
  12. package/dist/private/adapterLoader.js +14 -3
  13. package/dist/router/messagesRoute.d.ts +13 -0
  14. package/dist/router/messagesRoute.js +56 -0
  15. package/dist/selfUpdate/executor.d.ts +10 -5
  16. package/dist/selfUpdate/executor.js +81 -6
  17. package/dist/selfUpdate/failureCodes.d.ts +6 -0
  18. package/dist/selfUpdate/failureCodes.js +6 -0
  19. package/dist/selfUpdate/index.d.ts +4 -1
  20. package/dist/selfUpdate/index.js +4 -1
  21. package/dist/selfUpdate/lastUpdate.d.ts +3 -1
  22. package/dist/selfUpdate/lastUpdate.js +37 -6
  23. package/dist/selfUpdate/releaseBinary.d.ts +10 -0
  24. package/dist/selfUpdate/releaseBinary.js +43 -6
  25. package/dist/selfUpdate/transaction.d.ts +109 -0
  26. package/dist/selfUpdate/transaction.js +1174 -0
  27. package/dist/selfUpdate/unixController.d.ts +15 -0
  28. package/dist/selfUpdate/unixController.js +186 -0
  29. package/dist/selfUpdate/version.d.ts +6 -2
  30. package/dist/selfUpdate/version.js +5 -3
  31. package/dist/selfUpdate/windowsController.d.ts +23 -0
  32. package/dist/selfUpdate/windowsController.js +274 -0
  33. package/dist/selfUpdate/windowsUpdater.d.ts +79 -0
  34. package/dist/selfUpdate/windowsUpdater.js +715 -0
  35. package/dist/sync/engine.d.ts +6 -5
  36. package/dist/sync/engine.js +102 -58
  37. package/package.json +8 -3
@@ -1,12 +1,68 @@
1
1
  #!/usr/bin/env node
2
2
  import { mailbox } from '@evomap/evolver-core';
3
+ import { loadEnvFileFromEnv } from './envFile.js';
3
4
  import { type SelfUpdatePolicy } from '../selfUpdate/policy.js';
4
5
  import { type ReleaseBinaryOptions } from '../selfUpdate/releaseBinary.js';
6
+ import { rollbackDurableSelfUpdate, type SelfUpdateRecoveryOptions, type SelfUpdateRecoveryResult, type StagedBinaryProbe } from '../selfUpdate/transaction.js';
7
+ import { maybeRunWindowsUpdaterWorkerFromArgv } from '../selfUpdate/windowsUpdater.js';
8
+ import { maybeRunUnixRecoveryController } from '../selfUpdate/unixController.js';
9
+ import { maybeRunWindowsRecoveryController } from '../selfUpdate/windowsController.js';
5
10
  import type { AtpProxyClient, ProxyDaemonDeps, ProxyTickReport } from '../daemon/proxyDaemon.js';
6
11
  import type { HelloLifecycleMode, HelloResult, HeartbeatOptions, HeartbeatResult } from '../lifecycle/manager.js';
7
12
  import type { InboundResult } from '../sync/engine.js';
13
+ /** evolver-proxy 系统级 daemon 入口(M6-7). EVOMAP_HUB_MODE/URL/NODE_SECRET 选址. */
14
+ interface RunProxyMainOptions {
15
+ environmentPrepared?: boolean;
16
+ }
17
+ export declare function runProxyMain(options?: RunProxyMainOptions): Promise<void>;
18
+ export declare function recoverBoundDurableSelfUpdate(options: Omit<SelfUpdateRecoveryOptions, 'beforeJournalMutation'>): Promise<SelfUpdateRecoveryResult>;
19
+ export declare function loadProxyEnvFile(env: NodeJS.ProcessEnv): ReturnType<typeof loadEnvFileFromEnv>;
20
+ interface StartupClosableStore {
21
+ close(): void;
22
+ }
23
+ interface StartupStoppableDaemon {
24
+ stop(): Promise<void>;
25
+ }
26
+ interface RollbackPendingStartupOptions {
27
+ storePath?: string;
28
+ store?: StartupClosableStore;
29
+ daemon?: StartupStoppableDaemon;
30
+ startupError: unknown;
31
+ env?: NodeJS.ProcessEnv;
32
+ rollback?: typeof rollbackDurableSelfUpdate;
33
+ openTelemetryStore?: (storePath: string) => StartupClosableStore;
34
+ persistTelemetry?: (store: StartupClosableStore, recovery: SelfUpdateRecoveryResult) => void;
35
+ logger?: {
36
+ write(chunk: string): unknown;
37
+ };
38
+ processExecPath?: string;
39
+ }
40
+ export declare function rollbackPendingStartup(options: RollbackPendingStartupOptions): Promise<SelfUpdateRecoveryResult>;
41
+ export declare function startupRollbackExitCode(rollback: SelfUpdateRecoveryResult): 78;
8
42
  export declare function proxyUsage(): string;
9
- export declare function runProxyCli(): void;
43
+ export interface RunProxyCliOptions {
44
+ argv?: readonly string[];
45
+ env?: NodeJS.ProcessEnv;
46
+ platform?: NodeJS.Platform;
47
+ processExecPath?: string;
48
+ runMain?: () => Promise<void>;
49
+ runUnixRecoveryController?: typeof maybeRunUnixRecoveryController;
50
+ runWindowsRecoveryController?: typeof maybeRunWindowsRecoveryController;
51
+ runWindowsUpdaterWorker?: typeof maybeRunWindowsUpdaterWorkerFromArgv;
52
+ }
53
+ export interface ProxyCliPathOptions {
54
+ home?: string;
55
+ store?: string;
56
+ settings?: string;
57
+ envFile?: string;
58
+ help: boolean;
59
+ }
60
+ export declare function parseProxyCliPathOptions(argv: readonly string[]): ProxyCliPathOptions;
61
+ export declare function prepareProxyCliEnvironment(argv: readonly string[], env: NodeJS.ProcessEnv): {
62
+ options: ProxyCliPathOptions;
63
+ envFile: ReturnType<typeof loadEnvFileFromEnv>;
64
+ };
65
+ export declare function runProxyCli(options?: RunProxyCliOptions): Promise<number>;
10
66
  type PrivateAdapterImporter = (specifier: string) => Promise<unknown>;
11
67
  export interface RuntimeDeps {
12
68
  mode: 'public' | 'private';
@@ -61,14 +117,19 @@ interface CreateProxyDaemonDepsOptions {
61
117
  evolverVersion: string;
62
118
  selfUpdatePolicy: SelfUpdatePolicy;
63
119
  env?: NodeJS.ProcessEnv;
64
- selfUpdateOverrides?: ReleaseBinaryOptions & {
65
- restart?: () => void;
120
+ selfUpdateOverrides?: SelfUpdateRuntimeOverrides;
121
+ }
122
+ interface SelfUpdateRuntimeOverrides extends ReleaseBinaryOptions {
123
+ restart?: () => void;
124
+ stagedBinaryProbe?: StagedBinaryProbe;
125
+ /** Test-only attestation seam. Production callers must use the real process executable. */
126
+ supervisorAttested?: {
127
+ processExecPath: string;
66
128
  };
67
129
  }
68
130
  export declare function createProxyDaemonDeps(options: CreateProxyDaemonDepsOptions): ProxyDaemonDeps;
69
- export declare function createSelfUpdateDeps(policy: SelfUpdatePolicy, currentVersion: string, env?: NodeJS.ProcessEnv, overrides?: ReleaseBinaryOptions & {
70
- restart?: () => void;
71
- }): ProxyDaemonDeps['selfUpdate'];
131
+ export declare function createSelfUpdateDeps(policy: SelfUpdatePolicy, currentVersion: string, env?: NodeJS.ProcessEnv, overrides?: SelfUpdateRuntimeOverrides): ProxyDaemonDeps['selfUpdate'];
132
+ export declare function assertSelfUpdateProcessTargetBound(options: ReleaseBinaryOptions, allowUnresolvedTarget?: boolean): void;
72
133
  export declare function resolvePublicNodeSecret(deps: RuntimeDeps): PublicNodeSecretSelection;
73
134
  /**
74
135
  * Hub rejected the cached node_secret as diverged (v1 a2aProtocol.js L1983-2017). Clear every
@@ -2,7 +2,7 @@
2
2
  import { randomBytes } from 'node:crypto';
3
3
  import { readFileSync, realpathSync, rmSync } from 'node:fs';
4
4
  import { homedir } from 'node:os';
5
- import { join } from 'node:path';
5
+ import { join, resolve, win32 } from 'node:path';
6
6
  import { fileURLToPath } from 'node:url';
7
7
  import { daemon, hub as hubNs, mailbox } from '@evomap/evolver-core';
8
8
  import { AtpHubClient, connectPublicHub, globalFetchLike, isNodeSecret, parseNodeSecretVersion } from '@evomap/evolver-adapter-public';
@@ -14,88 +14,240 @@ import { connectPrivateProxyHub } from '../private/adapterLoader.js';
14
14
  import { createAtpOrderConsentGate } from '../daemon/atpConsent.js';
15
15
  import { resolveProxyStorePath } from './proxyStorePath.js';
16
16
  import { publishProxySettings } from './proxySettings.js';
17
- import { loadEnvFileFromEnv } from './envFile.js';
17
+ import { expandHomePath, loadEnvFileFromEnv } from './envFile.js';
18
18
  import { resolveProxyNodeId } from '../lifecycle/legacyNodeId.js';
19
19
  import { getCurrentVersion } from '../selfUpdate/version.js';
20
20
  import { resolveSelfUpdatePolicy } from '../selfUpdate/policy.js';
21
- import { atomicReplaceExecutable, downloadGithubReleaseArtifact, resolveGithubReleaseManifest, } from '../selfUpdate/releaseBinary.js';
22
- /** evolver-proxy 系统级 daemon 入口(M6-7). EVOMAP_HUB_MODE/URL/NODE_SECRET 选址. */
23
- async function main() {
21
+ import { atomicReplaceExecutable, downloadGithubReleaseArtifact, resolveGithubReleaseManifest, resolveSelfUpdateTarget, } from '../selfUpdate/releaseBinary.js';
22
+ import { beginDurableSelfUpdate, confirmDurableSelfUpdate, recoverDurableSelfUpdate, rollbackDurableSelfUpdate, } from '../selfUpdate/transaction.js';
23
+ import { SELF_UPDATE_FAILURE_CODES } from '../selfUpdate/failureCodes.js';
24
+ import { maybeRunWindowsUpdaterWorkerFromArgv } from '../selfUpdate/windowsUpdater.js';
25
+ import { maybeRunUnixRecoveryController } from '../selfUpdate/unixController.js';
26
+ import { maybeRunWindowsRecoveryController } from '../selfUpdate/windowsController.js';
27
+ import { finalizeSelfUpdateRecoveryLastUpdate } from '../selfUpdate/lastUpdate.js';
28
+ export async function runProxyMain(options = {}) {
24
29
  if (process.argv.includes('--help') || process.argv.includes('-h')) {
25
30
  process.stdout.write(proxyUsage());
26
31
  return;
27
32
  }
28
- const envFile = loadEnvFileFromEnv(process.env);
29
- if (envFile.error)
30
- process.stderr.write(`[evolver-proxy] failed to load EVOLVER_ENV_FILE: ${safeLoopMessage(envFile.error)}\n`);
31
- const mode = resolveHubMode(process.env);
32
- const hubUrl = resolveHubUrl(process.env);
33
- const storePath = resolveProxyStorePath(process.env);
34
- // `?.trim() ||` not `??`: an EMPTY/whitespace EVOLVER_IPC_TOKEN (e.g. a blank `.env` entry or `export
35
- // EVOLVER_IPC_TOKEN=`) must be treated as unset and get a strong random token, never fall through as `''` —
36
- // an empty token would authenticate any `Authorization: Bearer ` request and defeat the loopback IPC auth.
37
- const ipcToken = process.env['EVOLVER_IPC_TOKEN']?.trim() || randomBytes(24).toString('hex');
38
- const ipcPort = resolveIpcPort(process.env);
39
- const store = new mailbox.MailboxStore({ path: storePath });
40
- // Trim + treat blank as unset, preferring the first NON-EMPTY override so an
41
- // empty `EVOMAP_NODE_ID=` (k8s configmap / `$(cat missing)`) neither shadows a
42
- // valid A2A_NODE_ID nor suppresses the legacy recovery below. v1 parity:
43
- // a2aProtocol trimmed the env id before use.
44
- const configuredNodeId = (process.env['EVOMAP_NODE_ID']?.trim() || process.env['A2A_NODE_ID']?.trim()) || undefined;
45
- // store node_id → env override → legacy ~/.evomap/node_id (PORT v1 #117): when
46
- // the store is unprimed AND no env override is set, recover the id the legacy
47
- // GEP path persisted before letting hello() mint a fresh A2ANode under the
48
- // same owner. See lifecycle/legacyNodeId.ts for the duplicate-node rationale.
49
- const senderId = () => resolveProxyNodeId({ storedNodeId: store.getState('node_id'), configuredNodeId });
50
- // Read our own version for hello/heartbeat reporting and self-update decisions. EVOLVER_SELF_UPDATE defaults
51
- // to off; prompt/auto opt into the daemon path, but the release download/replace seams still fail closed until
52
- // a verified release implementation is wired.
53
- const evolverVersion = getCurrentVersion();
54
- const selfUpdatePolicy = resolveSelfUpdatePolicy(process.env);
55
- const runtime = await connectHubRuntime({ mode, hubUrl, senderId, store });
56
- const proxyStartedAt = new Date().toISOString();
33
+ if (!options.environmentPrepared) {
34
+ const envFile = loadProxyEnvFile(process.env);
35
+ if (envFile.error)
36
+ process.stderr.write(`[evolver-proxy] failed to load EVOLVER_ENV_FILE: ${safeLoopMessage(envFile.error)}\n`);
37
+ }
38
+ // Recovery must run before any hub/store/runtime initialization. In particular,
39
+ // a broken store must not prevent a pending-health update from restoring the old binary.
40
+ const recovery = await recoverBoundDurableSelfUpdate({ env: process.env, processExecPath: process.execPath });
41
+ if (recovery.outcome === 'blocked') {
42
+ throw new Error(`self_update_recovery_blocked:${recovery.failureCode ?? 'unknown'}`);
43
+ }
44
+ if (recovery.restartRequired)
45
+ process.exit(78);
46
+ let storePath;
47
+ let store;
48
+ let proxyDaemon;
49
+ let confirmation;
50
+ let mode;
51
+ let hubUrl;
52
+ let port;
53
+ let evolverVersion;
54
+ let selfUpdatePolicy;
57
55
  const proxySettingsState = {};
58
- const publishLocalProxySettings = () => {
59
- if (!proxySettingsState.url)
60
- return;
61
- publishProxySettings({
62
- record: {
63
- url: proxySettingsState.url,
64
- token: ipcToken,
65
- pid: process.pid,
66
- started_at: proxyStartedAt,
67
- version: evolverVersion,
56
+ let publishLocalProxySettings = () => { };
57
+ try {
58
+ mode = resolveHubMode(process.env);
59
+ hubUrl = resolveHubUrl(process.env);
60
+ storePath = resolveProxyStorePath(process.env);
61
+ store = new mailbox.MailboxStore({ path: storePath });
62
+ finalizeRecoveryTelemetry(store, recovery);
63
+ // `?.trim() ||` not `??`: an EMPTY/whitespace EVOLVER_IPC_TOKEN (e.g. a blank `.env` entry or `export
64
+ // EVOLVER_IPC_TOKEN=`) must be treated as unset and get a strong random token, never fall through as `''` —
65
+ // an empty token would authenticate any `Authorization: Bearer ` request and defeat the loopback IPC auth.
66
+ const ipcToken = process.env['EVOLVER_IPC_TOKEN']?.trim() || randomBytes(24).toString('hex');
67
+ const ipcPort = resolveIpcPort(process.env);
68
+ // Trim + treat blank as unset, preferring the first NON-EMPTY override so an
69
+ // empty `EVOMAP_NODE_ID=` (k8s configmap / `$(cat missing)`) neither shadows a
70
+ // valid A2A_NODE_ID nor suppresses the legacy recovery below. v1 parity:
71
+ // a2aProtocol trimmed the env id before use.
72
+ const configuredNodeId = (process.env['EVOMAP_NODE_ID']?.trim() || process.env['A2A_NODE_ID']?.trim()) || undefined;
73
+ // store node_id → env override → legacy ~/.evomap/node_id (PORT v1 #117): when
74
+ // the store is unprimed AND no env override is set, recover the id the legacy
75
+ // GEP path persisted before letting hello() mint a fresh A2ANode under the
76
+ // same owner. See lifecycle/legacyNodeId.ts for the duplicate-node rationale.
77
+ const senderId = () => resolveProxyNodeId({ storedNodeId: store.getState('node_id'), configuredNodeId });
78
+ evolverVersion = getCurrentVersion();
79
+ selfUpdatePolicy = resolveSelfUpdatePolicy(process.env);
80
+ const proxyStartedAt = new Date().toISOString();
81
+ publishLocalProxySettings = () => {
82
+ if (!proxySettingsState.url)
83
+ return;
84
+ publishProxySettings({
85
+ env: process.env,
86
+ record: {
87
+ url: proxySettingsState.url,
88
+ token: ipcToken,
89
+ pid: process.pid,
90
+ started_at: proxyStartedAt,
91
+ version: evolverVersion,
92
+ },
93
+ });
94
+ };
95
+ const runtime = await connectHubRuntime({ mode, hubUrl, senderId, store });
96
+ proxyDaemon = new ProxyDaemon({
97
+ ...createProxyDaemonDeps({
98
+ runtime,
99
+ store,
100
+ ipcToken,
101
+ ...(ipcPort !== undefined ? { ipcPort } : {}),
102
+ evolverVersion,
103
+ selfUpdatePolicy,
104
+ env: process.env,
105
+ }),
106
+ onIpcListen: (listeningPort) => {
107
+ proxySettingsState.url = `http://127.0.0.1:${listeningPort}`;
108
+ publishLocalProxySettings();
68
109
  },
110
+ onIpcAuthFailure: publishLocalProxySettings,
69
111
  });
70
- };
71
- const daemon = new ProxyDaemon({
72
- ...createProxyDaemonDeps({
73
- runtime,
74
- store,
75
- ipcToken,
76
- ...(ipcPort !== undefined ? { ipcPort } : {}),
77
- evolverVersion,
78
- selfUpdatePolicy,
79
- env: process.env,
80
- }),
81
- onIpcListen: (port) => {
82
- proxySettingsState.url = `http://127.0.0.1:${port}`;
83
- publishLocalProxySettings();
84
- },
85
- onIpcAuthFailure: publishLocalProxySettings,
86
- });
87
- const port = await daemon.start();
112
+ port = await proxyDaemon.start();
113
+ if (recovery.outcome === 'pending_health') {
114
+ assertSelfUpdateProcessTargetBound({ env: process.env, processExecPath: process.execPath });
115
+ confirmation = await confirmDurableSelfUpdate({ env: process.env, processExecPath: process.execPath });
116
+ if (confirmation.outcome !== 'confirmed') {
117
+ throw new Error(`self_update_confirmation_failed:${confirmation.outcome}`);
118
+ }
119
+ }
120
+ }
121
+ catch (error) {
122
+ if (recovery.outcome === 'pending_health') {
123
+ const rollback = await rollbackPendingStartup({
124
+ ...(storePath ? { storePath } : {}),
125
+ ...(store ? { store } : {}),
126
+ ...(proxyDaemon ? { daemon: proxyDaemon } : {}),
127
+ startupError: error,
128
+ env: process.env,
129
+ });
130
+ process.stderr.write(`[evolver-proxy] self-update startup health check failed; ${rollback.outcome}: ${safeLoopErrorMessage(error)}\n`);
131
+ process.exit(startupRollbackExitCode(rollback));
132
+ }
133
+ await closeStartupResources({ store, daemon: proxyDaemon });
134
+ throw error;
135
+ }
136
+ if (confirmation)
137
+ finalizeRecoveryTelemetry(store, confirmation);
88
138
  proxySettingsState.url = `http://127.0.0.1:${port}`;
89
139
  publishLocalProxySettings();
90
140
  process.stdout.write(`[evolver-proxy] mode=${mode} hub=${hubUrl} ipc=127.0.0.1:${port} v=${evolverVersion} self-update=${selfUpdatePolicy}\n`);
91
- await runProxyLoop(daemon, { logger: process.stderr });
141
+ await runProxyLoop(proxyDaemon, { logger: process.stderr });
142
+ }
143
+ export async function recoverBoundDurableSelfUpdate(options) {
144
+ return recoverDurableSelfUpdate({
145
+ ...options,
146
+ beforeJournalMutation: () => {
147
+ assertSelfUpdateProcessTargetBound(options);
148
+ },
149
+ });
150
+ }
151
+ export function loadProxyEnvFile(env) {
152
+ const supervisor = env['EVOLVER_SELF_UPDATE_SUPERVISOR'];
153
+ const stateDir = env['EVOLVER_SELF_UPDATE_STATE_DIR'];
154
+ const targetPath = env['EVOLVER_SELF_UPDATE_TARGET_PATH'];
155
+ const result = loadEnvFileFromEnv(env);
156
+ if (supervisor === undefined) {
157
+ delete env['EVOLVER_SELF_UPDATE_SUPERVISOR'];
158
+ }
159
+ else {
160
+ env['EVOLVER_SELF_UPDATE_SUPERVISOR'] = supervisor;
161
+ if (stateDir !== undefined)
162
+ env['EVOLVER_SELF_UPDATE_STATE_DIR'] = stateDir;
163
+ if (targetPath !== undefined)
164
+ env['EVOLVER_SELF_UPDATE_TARGET_PATH'] = targetPath;
165
+ }
166
+ return result;
167
+ }
168
+ function finalizeRecoveryTelemetry(store, recovery) {
169
+ try {
170
+ finalizeSelfUpdateRecoveryLastUpdate(store, recovery);
171
+ }
172
+ catch (error) {
173
+ process.stderr.write(`[evolver-proxy] failed to persist self-update recovery telemetry: ${safeLoopErrorMessage(error)}\n`);
174
+ }
175
+ }
176
+ export async function rollbackPendingStartup(options) {
177
+ await closeStartupResources(options);
178
+ let rollback;
179
+ try {
180
+ const processExecPath = options.processExecPath ?? process.execPath;
181
+ assertSelfUpdateProcessTargetBound({ env: options.env ?? process.env, processExecPath }, true);
182
+ rollback = await (options.rollback ?? rollbackDurableSelfUpdate)({ env: options.env ?? process.env, processExecPath }, SELF_UPDATE_FAILURE_CODES.RESTART_FAILED);
183
+ }
184
+ catch (error) {
185
+ throw new Error(`self_update_recovery_blocked:${safeLoopErrorMessage(error)}`, {
186
+ cause: options.startupError,
187
+ });
188
+ }
189
+ if (options.storePath) {
190
+ let telemetryStore;
191
+ try {
192
+ telemetryStore = (options.openTelemetryStore ?? ((storePath) => (new mailbox.MailboxStore({ path: storePath }))))(options.storePath);
193
+ (options.persistTelemetry ?? ((openedStore, recovery) => {
194
+ finalizeRecoveryTelemetry(openedStore, recovery);
195
+ }))(telemetryStore, rollback);
196
+ }
197
+ catch {
198
+ (options.logger ?? process.stderr).write('[evolver-proxy] failed to reopen self-update telemetry store\n');
199
+ }
200
+ finally {
201
+ try {
202
+ telemetryStore?.close();
203
+ }
204
+ catch { /* rollback already completed; telemetry cleanup is best-effort */ }
205
+ }
206
+ }
207
+ return rollback;
208
+ }
209
+ async function closeStartupResources(options) {
210
+ let daemonStopped = false;
211
+ if (options.daemon) {
212
+ try {
213
+ await options.daemon.stop();
214
+ daemonStopped = true;
215
+ }
216
+ catch {
217
+ // Continue closing the directly-created store before rollback.
218
+ }
219
+ }
220
+ if (options.store && !daemonStopped) {
221
+ try {
222
+ options.store.close();
223
+ }
224
+ catch { /* rollback must not be blocked by resource cleanup */ }
225
+ }
226
+ }
227
+ export function startupRollbackExitCode(rollback) {
228
+ if (rollback.outcome === 'blocked') {
229
+ throw new Error(`self_update_recovery_blocked:${rollback.failureCode ?? 'unknown'}`);
230
+ }
231
+ if (rollback.outcome !== 'rollback_pending'
232
+ && rollback.outcome !== 'rolled_back'
233
+ && rollback.outcome !== 'confirmed') {
234
+ throw new Error(`self_update_recovery_blocked:unexpected_${rollback.outcome}`);
235
+ }
236
+ return 78;
92
237
  }
93
238
  export function proxyUsage() {
94
239
  return [
95
- 'usage: evolver-proxy',
240
+ 'usage: evolver-proxy [options]',
96
241
  '',
97
242
  'Starts the local Evolver proxy daemon.',
98
243
  '',
244
+ 'Options (CLI overrides environment variables):',
245
+ ' --home <dir> Root for assets, store, settings, and traces',
246
+ ' --store <path> Mailbox store path (EVOLVER_PROXY_STORE)',
247
+ ' --settings <path> Proxy settings file (EVOLVER_PROXY_SETTINGS_FILE)',
248
+ ' --env-file <path> Environment file (EVOLVER_ENV_FILE)',
249
+ ' -h, --help Show this help',
250
+ '',
99
251
  'Required for public mode:',
100
252
  ' EVOMAP_NODE_SECRET or A2A_NODE_SECRET',
101
253
  '',
@@ -104,22 +256,118 @@ export function proxyUsage() {
104
256
  ' EVOMAP_HUB_URL=<private hub url>',
105
257
  ' EVOMAP_ENTERPRISE_TOKEN=<token>',
106
258
  '',
259
+ 'Hub URL precedence:',
260
+ ' A2A_HUB_URL -> EVOMAP_HUB_URL -> EVOLVER_DEFAULT_HUB_URL -> https://evomap.ai',
261
+ '',
107
262
  'Useful options are configured through env or EVOLVER_ENV_FILE:',
108
263
  ' EVOLVER_IPC_PORT, EVOLVER_IPC_TOKEN, EVOLVER_PROXY_SETTINGS_FILE',
109
264
  ' EVOLVER_SELF_UPDATE, EVOLVER_LLM_TRACE_CAPTURE_BODIES',
110
265
  '',
111
266
  ].join('\n');
112
267
  }
113
- export function runProxyCli() {
268
+ const PROXY_PATH_FLAGS = new Map([
269
+ ['--home', 'home'],
270
+ ['--store', 'store'],
271
+ ['--settings', 'settings'],
272
+ ['--env-file', 'envFile'],
273
+ ]);
274
+ export function parseProxyCliPathOptions(argv) {
275
+ const options = { help: false };
276
+ for (let index = 0; index < argv.length; index += 1) {
277
+ const arg = argv[index];
278
+ if (arg === '--help' || arg === '-h') {
279
+ options.help = true;
280
+ continue;
281
+ }
282
+ const equalsIndex = arg.indexOf('=');
283
+ const flag = equalsIndex >= 0 ? arg.slice(0, equalsIndex) : arg;
284
+ const key = PROXY_PATH_FLAGS.get(flag);
285
+ if (key) {
286
+ const value = equalsIndex >= 0 ? arg.slice(equalsIndex + 1) : argv[++index];
287
+ if (!value?.trim() || (equalsIndex < 0 && value.startsWith('-'))) {
288
+ throw new Error(`${flag} requires a path`);
289
+ }
290
+ options[key] = resolve(expandHomePath(value.trim()));
291
+ continue;
292
+ }
293
+ if (arg.startsWith('-'))
294
+ throw new Error(`unknown option: ${arg}`);
295
+ }
296
+ return options;
297
+ }
298
+ export function prepareProxyCliEnvironment(argv, env) {
299
+ const options = parseProxyCliPathOptions(argv);
300
+ if (options.envFile)
301
+ env['EVOLVER_ENV_FILE'] = options.envFile;
302
+ const envFile = loadProxyEnvFile(env);
303
+ if (options.home) {
304
+ env['EVOMAP_DIR'] = options.home;
305
+ env['EVOLVER_HOME'] = options.home;
306
+ env['EVOMAP_HOME'] = options.home;
307
+ env['EVOLVER_SETTINGS_DIR'] = options.home;
308
+ env['EVOLVER_PROXY_STORE'] = join(options.home, 'proxy', 'mailbox.db');
309
+ env['EVOLVER_PROXY_SETTINGS_FILE'] = join(options.home, 'settings.json');
310
+ env['EVOLVER_LLM_TRACE_DIR'] = join(options.home, 'proxy', 'traces');
311
+ }
312
+ if (options.store)
313
+ env['EVOLVER_PROXY_STORE'] = options.store;
314
+ if (options.settings)
315
+ env['EVOLVER_PROXY_SETTINGS_FILE'] = options.settings;
316
+ return { options, envFile };
317
+ }
318
+ export async function runProxyCli(options = {}) {
319
+ const argv = options.argv ?? process.argv.slice(2);
320
+ const env = options.env ?? process.env;
321
+ const unixControllerExitCode = await (options.runUnixRecoveryController ?? maybeRunUnixRecoveryController)({
322
+ argv,
323
+ env,
324
+ platform: options.platform ?? process.platform,
325
+ processExecPath: options.processExecPath ?? process.execPath,
326
+ });
327
+ if (unixControllerExitCode !== undefined)
328
+ return unixControllerExitCode;
329
+ const windowsControllerExitCode = await (options.runWindowsRecoveryController ?? maybeRunWindowsRecoveryController)({
330
+ argv,
331
+ env,
332
+ platform: options.platform ?? process.platform,
333
+ processExecPath: options.processExecPath ?? process.execPath,
334
+ });
335
+ if (windowsControllerExitCode !== undefined)
336
+ return windowsControllerExitCode;
337
+ const workerExitCode = await (options.runWindowsUpdaterWorker ?? maybeRunWindowsUpdaterWorkerFromArgv)({
338
+ argv,
339
+ env,
340
+ platform: options.platform ?? process.platform,
341
+ processExecPath: options.processExecPath ?? process.execPath,
342
+ });
343
+ if (workerExitCode !== undefined)
344
+ return workerExitCode;
114
345
  const uninstallUnhandledRejectionGuard = daemon.installUnhandledRejectionWindow();
115
- main().catch((e) => {
346
+ try {
347
+ const cliOptions = parseProxyCliPathOptions(argv);
348
+ if (cliOptions.help) {
349
+ process.stdout.write(proxyUsage());
350
+ return 0;
351
+ }
352
+ const prepared = prepareProxyCliEnvironment(argv, env);
353
+ if (prepared.envFile.error) {
354
+ process.stderr.write(`[evolver-proxy] failed to load EVOLVER_ENV_FILE: ${safeLoopMessage(prepared.envFile.error)}\n`);
355
+ }
356
+ await (options.runMain ?? (() => runProxyMain({ environmentPrepared: true })))();
357
+ return 0;
358
+ }
359
+ catch (error) {
360
+ process.stderr.write(`[evolver-proxy] fatal: ${safeLoopErrorMessage(error)}\n`);
361
+ return 1;
362
+ }
363
+ finally {
116
364
  uninstallUnhandledRejectionGuard();
117
- process.stderr.write(`[evolver-proxy] fatal: ${safeLoopErrorMessage(e)}\n`);
118
- process.exit(1);
119
- });
365
+ }
120
366
  }
121
367
  if (isDirectRun(import.meta.url, process.argv[1])) {
122
- runProxyCli();
368
+ void runProxyCli().then((exitCode) => {
369
+ process.exitCode = exitCode;
370
+ });
123
371
  }
124
372
  export async function runProxyLoop(daemon, options = {}) {
125
373
  const minDelayMs = options.minDelayMs ?? 1_000;
@@ -335,24 +583,90 @@ function errorMessage(err) {
335
583
  export function createSelfUpdateDeps(policy, currentVersion, env = process.env, overrides = {}) {
336
584
  if (policy !== 'auto')
337
585
  return undefined;
586
+ const { supervisorAttested, restart, stagedBinaryProbe, processExecPath: _ignoredProcessExecPath, ...binaryOverrides } = overrides;
587
+ if (!supervisorAttested && !selfUpdateSupervisorAttested(env)) {
588
+ throw new Error('self_update_supervisor_required');
589
+ }
338
590
  const publicKey = env['EVOLVER_SELF_UPDATE_PUBLIC_KEY']?.trim();
339
591
  if (!publicKey)
340
592
  throw new Error('self_update_public_key_required');
341
593
  const releaseOpts = {
342
594
  env,
343
- ...overrides,
595
+ ...binaryOverrides,
596
+ processExecPath: supervisorAttested?.processExecPath ?? process.execPath,
344
597
  requireSignedManifest: true,
345
598
  };
599
+ const assertBound = () => assertSelfUpdateProcessTargetBound(releaseOpts);
600
+ assertBound();
346
601
  return {
347
602
  policy,
348
603
  currentVersion,
349
- resolveManifest: (directive) => resolveGithubReleaseManifest(directive, releaseOpts),
350
- download: (targetVersion, directive) => downloadGithubReleaseArtifact(targetVersion, directive, releaseOpts),
351
- atomicReplace: (stagedPath) => atomicReplaceExecutable(stagedPath, releaseOpts),
352
- restart: overrides.restart ?? (() => { process.exit(78); }),
604
+ resolveManifest: (directive) => {
605
+ assertBound();
606
+ return resolveGithubReleaseManifest(directive, releaseOpts);
607
+ },
608
+ download: (targetVersion, directive) => {
609
+ assertBound();
610
+ return downloadGithubReleaseArtifact(targetVersion, directive, releaseOpts);
611
+ },
612
+ atomicReplace: (stagedPath) => {
613
+ assertBound();
614
+ return atomicReplaceExecutable(stagedPath, releaseOpts);
615
+ },
616
+ beginTransaction: async (targetVersion) => {
617
+ assertBound();
618
+ const transaction = await beginDurableSelfUpdate(targetVersion, {
619
+ ...releaseOpts,
620
+ currentVersion,
621
+ ...(stagedBinaryProbe ? { stagedBinaryProbe } : {}),
622
+ });
623
+ return {
624
+ ...transaction,
625
+ install: async () => {
626
+ assertBound();
627
+ await transaction.install();
628
+ },
629
+ };
630
+ },
631
+ restart: restart ?? (() => { process.exit(78); }),
353
632
  publicKey,
354
633
  };
355
634
  }
635
+ export function assertSelfUpdateProcessTargetBound(options, allowUnresolvedTarget = false) {
636
+ let targetPath;
637
+ try {
638
+ targetPath = resolveSelfUpdateTarget(options).path;
639
+ }
640
+ catch {
641
+ if (allowUnresolvedTarget)
642
+ return;
643
+ throw new Error('self_update_process_target_mismatch');
644
+ }
645
+ const processExecPath = options.processExecPath ?? process.execPath;
646
+ try {
647
+ if (canonicalExecutablePath(processExecPath) !== canonicalExecutablePath(targetPath)) {
648
+ throw new Error('self_update_process_target_mismatch');
649
+ }
650
+ }
651
+ catch {
652
+ throw new Error('self_update_process_target_mismatch');
653
+ }
654
+ }
655
+ function canonicalExecutablePath(path) {
656
+ const canonical = realpathSync.native(path);
657
+ if (process.platform !== 'win32')
658
+ return resolve(canonical);
659
+ const withoutNamespace = canonical
660
+ .replace(/^\\\\\?\\UNC\\/i, '\\\\')
661
+ .replace(/^\\\\\?\\/i, '');
662
+ return win32.normalize(withoutNamespace).toLowerCase();
663
+ }
664
+ function selfUpdateSupervisorAttested(env) {
665
+ const supervisor = env['EVOLVER_SELF_UPDATE_SUPERVISOR']?.trim();
666
+ return supervisor === 'systemd'
667
+ || supervisor === 'launchd'
668
+ || supervisor === 'windows-scheduled-task';
669
+ }
356
670
  export function resolvePublicNodeSecret(deps) {
357
671
  const envNodeSecret = process.env['EVOMAP_NODE_SECRET'] ?? process.env['A2A_NODE_SECRET'];
358
672
  // version env precedence MUST mirror the node_secret precedence above (EVOMAP-first) so an operator
@@ -8,8 +8,10 @@ export interface ProxySettingsRecord {
8
8
  export interface PublishProxySettingsOptions {
9
9
  settingsPath?: string;
10
10
  homeDir?: string;
11
+ env?: Record<string, string | undefined>;
11
12
  record: ProxySettingsRecord;
12
13
  }
13
14
  export declare function defaultProxySettingsPath(homeDir?: string): string;
15
+ export declare function resolveProxySettingsPath(env?: Record<string, string | undefined>, homeDir?: string): string;
14
16
  export declare function publishProxySettings(options: PublishProxySettingsOptions): boolean;
15
17
  export declare function proxySettingsMatch(settingsPath: string, record: ProxySettingsRecord): boolean;
@@ -5,8 +5,15 @@ import { dirname, join } from 'node:path';
5
5
  export function defaultProxySettingsPath(homeDir = homedir()) {
6
6
  return join(homeDir, '.evolver', 'settings.json');
7
7
  }
8
+ export function resolveProxySettingsPath(env = {}, homeDir = homedir()) {
9
+ const explicit = env['EVOLVER_PROXY_SETTINGS_FILE']?.trim();
10
+ if (explicit)
11
+ return explicit;
12
+ const settingsDir = env['EVOLVER_SETTINGS_DIR']?.trim() || join(homeDir, '.evolver');
13
+ return join(settingsDir, 'settings.json');
14
+ }
8
15
  export function publishProxySettings(options) {
9
- const settingsPath = options.settingsPath ?? defaultProxySettingsPath(options.homeDir);
16
+ const settingsPath = options.settingsPath ?? resolveProxySettingsPath(options.env, options.homeDir);
10
17
  const record = options.record;
11
18
  if (!record.token.trim())
12
19
  return false;