@evident-ai/cli 3.6.1-dev.d1c8cf0 → 3.6.1-dev.dac6a7f

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/dist/index.js CHANGED
@@ -100,6 +100,26 @@ function clearCredentials() {
100
100
  credentials.set("byEndpoint", byEndpoint);
101
101
  hardenCredentialsPermissions();
102
102
  }
103
+ function getManagedInferenceCredential(runnerId) {
104
+ hardenCredentialsPermissions();
105
+ return credentials.get("managedInferenceByEndpoint")?.[credentialsKey()]?.[runnerId];
106
+ }
107
+ function setManagedInferenceCredential(runnerId, credential) {
108
+ const byEndpoint = credentials.get("managedInferenceByEndpoint") ?? {};
109
+ byEndpoint[credentialsKey()] ??= {};
110
+ byEndpoint[credentialsKey()][runnerId] = credential;
111
+ credentials.set("managedInferenceByEndpoint", byEndpoint);
112
+ hardenCredentialsPermissions();
113
+ }
114
+ function clearManagedInferenceCredential(runnerId) {
115
+ const byEndpoint = credentials.get("managedInferenceByEndpoint") ?? {};
116
+ const runners = byEndpoint[credentialsKey()];
117
+ if (!runners || !(runnerId in runners)) return;
118
+ delete runners[runnerId];
119
+ if (Object.keys(runners).length === 0) delete byEndpoint[credentialsKey()];
120
+ credentials.set("managedInferenceByEndpoint", byEndpoint);
121
+ hardenCredentialsPermissions();
122
+ }
103
123
  function clearAllCredentials() {
104
124
  credentials.clear();
105
125
  hardenCredentialsPermissions();
@@ -119,13 +139,13 @@ function getCliName() {
119
139
  // src/lib/api.ts
120
140
  async function request(path, method, body, headers = {}) {
121
141
  const url = `${getApiUrlConfig()}${path}`;
122
- const requestHeaders = {
142
+ const requestHeaders2 = {
123
143
  "Content-Type": "application/json",
124
144
  ...headers
125
145
  };
126
146
  const response = await fetch(url, {
127
147
  method,
128
- headers: requestHeaders,
148
+ headers: requestHeaders2,
129
149
  body: body ? JSON.stringify(body) : void 0
130
150
  });
131
151
  if (!response.ok) {
@@ -1287,6 +1307,102 @@ var TUNNEL_USAGE_REARM_PING_PATH = "/__evident/usage-rearm";
1287
1307
  var MAX_FILE_PUSH_BYTES = 64 * 1024;
1288
1308
  var MAX_FILE_SYNC_DIRECTORIES = 16;
1289
1309
 
1310
+ // ../../packages/types/src/credential-proxy-native-config.ts
1311
+ var EVIDENT_PROXY_TOKEN_REFERENCE = "{env:EVIDENT_PROXY_TOKEN}";
1312
+ var CLAUDE_PROVIDER = {
1313
+ namespace: "evident-claude",
1314
+ name: "Evident Claude credential proxy",
1315
+ npm: "@ai-sdk/anthropic"
1316
+ };
1317
+ var NATIVE_PROVIDERS = {
1318
+ claude: CLAUDE_PROVIDER,
1319
+ anthropic: CLAUDE_PROVIDER,
1320
+ codex: {
1321
+ namespace: "evident-codex",
1322
+ name: "Evident Codex credential proxy",
1323
+ npm: "@ai-sdk/openai"
1324
+ },
1325
+ openai: {
1326
+ namespace: "evident-openai",
1327
+ name: "Evident OpenAI credential proxy",
1328
+ npm: "@ai-sdk/openai"
1329
+ },
1330
+ kimi: {
1331
+ namespace: "evident-kimi",
1332
+ name: "Evident Kimi credential proxy",
1333
+ npm: "@ai-sdk/openai-compatible",
1334
+ sdkName: "evident-kimi"
1335
+ },
1336
+ antigravity: {
1337
+ namespace: "evident-antigravity",
1338
+ name: "Evident Antigravity credential proxy",
1339
+ npm: "@ai-sdk/openai-compatible",
1340
+ sdkName: "evident-antigravity"
1341
+ },
1342
+ xai: {
1343
+ namespace: "evident-xai",
1344
+ name: "Evident xAI credential proxy",
1345
+ npm: "@ai-sdk/openai-compatible",
1346
+ sdkName: "evident-xai"
1347
+ },
1348
+ mistral: {
1349
+ namespace: "evident-mistral",
1350
+ name: "Evident Mistral credential proxy",
1351
+ npm: "@ai-sdk/openai-compatible",
1352
+ sdkName: "evident-mistral"
1353
+ }
1354
+ };
1355
+ var CREDENTIAL_PROXY_NATIVE_NAMESPACES = [
1356
+ ...new Set(Object.values(NATIVE_PROVIDERS).map(({ namespace }) => namespace))
1357
+ ];
1358
+ function credentialProxyInferenceUrl(baseUrl) {
1359
+ const trimmed = baseUrl.trim().replace(/\/+$/, "");
1360
+ return /\/v1$/i.test(trimmed) ? trimmed : `${trimmed}/v1`;
1361
+ }
1362
+ function buildCredentialProxyNativeConfig({
1363
+ inferenceBaseUrl,
1364
+ modelDescriptors
1365
+ }) {
1366
+ const baseURL = credentialProxyInferenceUrl(inferenceBaseUrl);
1367
+ const providers = {};
1368
+ const unsupportedProviders = /* @__PURE__ */ new Set();
1369
+ for (const descriptor of modelDescriptors) {
1370
+ const definition = NATIVE_PROVIDERS[descriptor.provider.trim().toLowerCase()];
1371
+ if (!definition) {
1372
+ unsupportedProviders.add(descriptor.provider.trim() || descriptor.provider);
1373
+ continue;
1374
+ }
1375
+ providers[definition.namespace] ??= definition.npm === "@ai-sdk/anthropic" ? {
1376
+ name: definition.name,
1377
+ npm: definition.npm,
1378
+ options: { baseURL, authToken: EVIDENT_PROXY_TOKEN_REFERENCE },
1379
+ models: {}
1380
+ } : definition.npm === "@ai-sdk/openai" ? {
1381
+ name: definition.name,
1382
+ npm: definition.npm,
1383
+ options: { baseURL, apiKey: EVIDENT_PROXY_TOKEN_REFERENCE },
1384
+ models: {}
1385
+ } : {
1386
+ name: definition.name,
1387
+ npm: definition.npm,
1388
+ options: {
1389
+ name: definition.sdkName,
1390
+ baseURL,
1391
+ apiKey: EVIDENT_PROXY_TOKEN_REFERENCE
1392
+ },
1393
+ models: {}
1394
+ };
1395
+ providers[definition.namespace].models[descriptor.id] = { name: descriptor.id };
1396
+ }
1397
+ return {
1398
+ unsupportedProviders: [...unsupportedProviders],
1399
+ config: {
1400
+ $schema: "https://opencode.ai/config.json",
1401
+ provider: providers
1402
+ }
1403
+ };
1404
+ }
1405
+
1290
1406
  // ../../packages/types/src/logging/index.ts
1291
1407
  var CORRELATION_ID_HEADER = "x-evident-correlation-id";
1292
1408
  var FORWARD_FAILURE_REASON_HEADER = "X-Evident-Failure-Reason";
@@ -1326,7 +1442,7 @@ import ora4 from "ora";
1326
1442
  import { select as select4 } from "@inquirer/prompts";
1327
1443
 
1328
1444
  // src/lib/telemetry.ts
1329
- var CLI_VERSION = (true ? "3.6.1-dev.d1c8cf0" : void 0) ?? process.env.npm_package_version ?? "unknown";
1445
+ var CLI_VERSION = (true ? "3.6.1-dev.dac6a7f" : void 0) ?? process.env.npm_package_version ?? "unknown";
1330
1446
  function getCliVersion() {
1331
1447
  return CLI_VERSION;
1332
1448
  }
@@ -1407,7 +1523,7 @@ async function flushEvents() {
1407
1523
  const controller = new AbortController();
1408
1524
  const timeout = setTimeout(() => controller.abort(), FLUSH_TIMEOUT_MS);
1409
1525
  try {
1410
- const request2 = {
1526
+ const request3 = {
1411
1527
  events,
1412
1528
  client_type: "cli",
1413
1529
  client_version: CLI_VERSION
@@ -1418,7 +1534,7 @@ async function flushEvents() {
1418
1534
  "Content-Type": "application/json",
1419
1535
  Authorization: authHeader
1420
1536
  },
1421
- body: JSON.stringify(request2),
1537
+ body: JSON.stringify(request3),
1422
1538
  signal: controller.signal
1423
1539
  });
1424
1540
  if (!response.ok) {
@@ -4681,11 +4797,11 @@ function failureMessage(status2) {
4681
4797
  function isRetryableStatus(status2) {
4682
4798
  return CONNECT_KEY_RETRY_STATUSES.includes(status2);
4683
4799
  }
4684
- async function importOneCredential(request2, credential, sleepForRetry, log3) {
4800
+ async function importOneCredential(request3, credential, sleepForRetry, log3) {
4685
4801
  const path = `/api/integration/${encodeURIComponent(credential.integrationId)}/connect/key`;
4686
4802
  for (let attempt = 1; attempt <= CONNECT_KEY_MAX_ATTEMPTS; attempt += 1) {
4687
4803
  try {
4688
- const response = await request2(
4804
+ const response = await request3(
4689
4805
  path,
4690
4806
  {
4691
4807
  method: "POST",
@@ -4720,7 +4836,7 @@ async function importOneCredential(request2, credential, sleepForRetry, log3) {
4720
4836
  throw new Error("OpenCode credential import retry loop did not return");
4721
4837
  }
4722
4838
  async function importOpenCodeCredentials({
4723
- request: request2,
4839
+ request: request3,
4724
4840
  classified,
4725
4841
  connectedIntegrationIds,
4726
4842
  sleepForRetry = sleep2,
@@ -4732,7 +4848,7 @@ async function importOpenCodeCredentials({
4732
4848
  result.alreadyConnected.push(credential.integrationId);
4733
4849
  continue;
4734
4850
  }
4735
- const outcome = await importOneCredential(request2, credential, sleepForRetry, log3);
4851
+ const outcome = await importOneCredential(request3, credential, sleepForRetry, log3);
4736
4852
  if (outcome.ok) result.imported.push(credential.integrationId);
4737
4853
  else result.failed.push(outcome.failure);
4738
4854
  }
@@ -4750,7 +4866,7 @@ function parseIntegrationList(value) {
4750
4866
  return { integrationIds, hasData: value.data.length > 0 };
4751
4867
  }
4752
4868
  async function getConnectedOpenCodeIntegrationIds({
4753
- request: request2,
4869
+ request: request3,
4754
4870
  directory,
4755
4871
  sleepForRetry = sleep2,
4756
4872
  log: log3
@@ -4758,7 +4874,7 @@ async function getConnectedOpenCodeIntegrationIds({
4758
4874
  const path = `/api/integration?location%5Bdirectory%5D=${encodeURIComponent(directory)}`;
4759
4875
  for (let attempt = 1; attempt <= INTEGRATION_LIST_MAX_READS; attempt += 1) {
4760
4876
  try {
4761
- const response = await request2(path);
4877
+ const response = await request3(path);
4762
4878
  const parsed = parseIntegrationList(await response.json());
4763
4879
  if (parsed === null) {
4764
4880
  log3(
@@ -4787,7 +4903,7 @@ async function getConnectedOpenCodeIntegrationIds({
4787
4903
  return null;
4788
4904
  }
4789
4905
  async function importOpenCodeV2Credentials({
4790
- request: request2,
4906
+ request: request3,
4791
4907
  homeDir,
4792
4908
  directory,
4793
4909
  sleepForRetry = sleep2,
@@ -4805,7 +4921,7 @@ async function importOpenCodeV2Credentials({
4805
4921
  const classified = classifyOpenCodeCredentials(credentials2);
4806
4922
  if (classified.importable.length === 0) return emptyResult(classified.unsupported);
4807
4923
  const connectedIntegrationIds = await getConnectedOpenCodeIntegrationIds({
4808
- request: request2,
4924
+ request: request3,
4809
4925
  directory,
4810
4926
  sleepForRetry,
4811
4927
  log: log3
@@ -4814,7 +4930,7 @@ async function importOpenCodeV2Credentials({
4814
4930
  return emptyResult(classified.unsupported, "connection-list-unavailable");
4815
4931
  }
4816
4932
  return importOpenCodeCredentials({
4817
- request: request2,
4933
+ request: request3,
4818
4934
  classified,
4819
4935
  connectedIntegrationIds,
4820
4936
  sleepForRetry,
@@ -5505,7 +5621,11 @@ function isSessionDbProvenanceRecord(value) {
5505
5621
  var OPENCODE2_PACKAGE_SPEC = "@opencode-ai/cli@0.0.0-beta-17823";
5506
5622
 
5507
5623
  // src/lib/opencode/opencode-version-gate.ts
5508
- var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
5624
+ var QUEUE_VALIDATED_OPENCODE_VERSIONS = [
5625
+ "1.17.11",
5626
+ "1.18.3",
5627
+ "1.18.35"
5628
+ ];
5509
5629
  function isQueueValidatedVersion(version2) {
5510
5630
  if (!version2) return false;
5511
5631
  return QUEUE_VALIDATED_OPENCODE_VERSIONS.includes(version2);
@@ -6550,6 +6670,7 @@ function connectTunnel(options) {
6550
6670
  }, 3e4);
6551
6671
  let relaySilenceTimeout;
6552
6672
  let relaySilenceTimedOut = false;
6673
+ let relayConnectionId;
6553
6674
  const clearRelaySilenceTimeout = () => {
6554
6675
  if (relaySilenceTimeout === void 0) return;
6555
6676
  clearTimeout(relaySilenceTimeout);
@@ -6635,7 +6756,8 @@ function connectTunnel(options) {
6635
6756
  armRelaySilenceTimeout();
6636
6757
  forwarder.setBinaryFramesSupported(message.binary_frames === true);
6637
6758
  const connectedAgentId = message.agent_id ?? agentId;
6638
- onConnected?.(connectedAgentId);
6759
+ relayConnectionId = message.connection_id;
6760
+ onConnected?.(connectedAgentId, relayConnectionId);
6639
6761
  resolve5({
6640
6762
  ws,
6641
6763
  close: () => ws.close(1e3, "CLI shutdown")
@@ -6672,7 +6794,7 @@ function connectTunnel(options) {
6672
6794
  clearRelaySilenceTimeout();
6673
6795
  const reasonStr = (relaySilenceTimedOut ? "no relay heartbeat for 90s \u2014 reconnecting" : void 0) || reason.toString() || upgradeRejection || (code === 1006 ? "abnormal closure" : "No reason provided");
6674
6796
  forwarder.abortAll();
6675
- onDisconnected?.(code, reasonStr);
6797
+ onDisconnected?.(code, reasonStr, relayConnectionId);
6676
6798
  });
6677
6799
  });
6678
6800
  }
@@ -6735,7 +6857,7 @@ var RunnerConnection = class _RunnerConnection {
6735
6857
  authHeader: this.opts.getAuthHeader(),
6736
6858
  port: this.opts.port,
6737
6859
  openCodePassword: this.opts.openCodePassword,
6738
- onConnected: (agentId) => {
6860
+ onConnected: (agentId, relayConnectionId) => {
6739
6861
  const attempt = this.reconnectAttempt + 1;
6740
6862
  this.reconnectAttempt = 0;
6741
6863
  this.reconnecting = false;
@@ -6745,13 +6867,17 @@ var RunnerConnection = class _RunnerConnection {
6745
6867
  this.disconnectedAtMs = null;
6746
6868
  events.onConnected(agentId, isReconnect, {
6747
6869
  generation: this.generation,
6870
+ relayConnectionId: relayConnectionId ?? null,
6748
6871
  attempt,
6749
6872
  downtimeMs
6750
6873
  });
6751
6874
  },
6752
- onDisconnected: (code, reason) => {
6875
+ onDisconnected: (code, reason, relayConnectionId) => {
6753
6876
  this.disconnectedAtMs = this.now();
6754
- events.onDisconnected(code, reason, { generation: this.generation });
6877
+ events.onDisconnected(code, reason, {
6878
+ generation: this.generation,
6879
+ relayConnectionId: relayConnectionId ?? null
6880
+ });
6755
6881
  const relayRequestedReconnect = code === 1e3 && _RunnerConnection.RELAY_RECONNECT_CLOSE_REASONS.has(reason);
6756
6882
  if (this.opts.isRunning() && (code !== 1e3 || relayRequestedReconnect) && !this.reconnecting) {
6757
6883
  this.reconnectPromise = this.connectWithRetry(true).catch((err) => {
@@ -7199,8 +7325,8 @@ import { chmod, mkdir, open as open3, realpath, rename, unlink } from "node:fs/p
7199
7325
  import { basename as basename2, dirname as dirname6, isAbsolute as isAbsolute2, join as join9, relative as relative2, resolve as resolve3, sep as sep2 } from "node:path";
7200
7326
  var FILE_MODE = 384;
7201
7327
  var DIRECTORY_MODE = 448;
7202
- async function writePushedFile(request2) {
7203
- const { requestedPath, content, allowedDirectories, homeDir } = request2;
7328
+ async function writePushedFile(request3) {
7329
+ const { requestedPath, content, allowedDirectories, homeDir } = request3;
7204
7330
  const bytes = content.byteLength;
7205
7331
  if (allowedDirectories.length === 0) {
7206
7332
  return refuse("file_sync_disabled", "File sync is not enabled on this runner.", {
@@ -13778,6 +13904,38 @@ async function configureGitHubAccess({ env, log: log3 }) {
13778
13904
  import { execFileSync as execFileSync2 } from "node:child_process";
13779
13905
  import { copyFileSync, existsSync as existsSync2, statSync as statSync6 } from "node:fs";
13780
13906
  import { isAbsolute as isAbsolute3, join as join11, resolve as resolve4 } from "node:path";
13907
+ function parseConfigContent(content) {
13908
+ if (!content?.trim()) return {};
13909
+ const parsed = JSON.parse(content);
13910
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
13911
+ throw new Error("OPENCODE_CONFIG_CONTENT must contain a JSON object");
13912
+ }
13913
+ return parsed;
13914
+ }
13915
+ function mergeManagedOpenCodeConfigContent(content, managedProviders) {
13916
+ const config = parseConfigContent(content);
13917
+ const provider = typeof config.provider === "object" && config.provider !== null && !Array.isArray(config.provider) ? { ...config.provider } : {};
13918
+ for (const namespace of CREDENTIAL_PROXY_NATIVE_NAMESPACES) {
13919
+ delete provider[namespace];
13920
+ }
13921
+ return JSON.stringify({ ...config, provider: { ...provider, ...managedProviders } });
13922
+ }
13923
+ function removeManagedOpenCodeConfigContent(content) {
13924
+ if (!content?.trim()) return content;
13925
+ const config = parseConfigContent(content);
13926
+ if (typeof config.provider !== "object" || config.provider === null || Array.isArray(config.provider)) {
13927
+ return content;
13928
+ }
13929
+ const provider = { ...config.provider };
13930
+ let changed = false;
13931
+ for (const namespace of CREDENTIAL_PROXY_NATIVE_NAMESPACES) {
13932
+ if (namespace in provider) {
13933
+ delete provider[namespace];
13934
+ changed = true;
13935
+ }
13936
+ }
13937
+ return changed ? JSON.stringify({ ...config, provider }) : content;
13938
+ }
13781
13939
  function isFile(filePath) {
13782
13940
  return existsSync2(filePath) && statSync6(filePath).isFile();
13783
13941
  }
@@ -14199,6 +14357,230 @@ function createCredentialSync({
14199
14357
  };
14200
14358
  }
14201
14359
 
14360
+ // src/lib/managed-inference-setup.ts
14361
+ var defaultStore = {
14362
+ get: getManagedInferenceCredential,
14363
+ set: setManagedInferenceCredential,
14364
+ clear: clearManagedInferenceCredential
14365
+ };
14366
+ function endpoint(options, operation) {
14367
+ return `${options.apiUrl}/runners/${options.agentId}/managed-credentials/${operation}`;
14368
+ }
14369
+ function requestHeaders(options) {
14370
+ return { Authorization: options.getAuthHeader(), "Content-Type": "application/json" };
14371
+ }
14372
+ function request2(options) {
14373
+ return withRequestTimeout(options.fetchImpl ?? fetch, REQUEST_TIMEOUT_MS);
14374
+ }
14375
+ function safeFailure(operation, status2) {
14376
+ return status2 === void 0 ? `${operation} could not reach the managed credential service` : `${operation} returned HTTP ${status2}`;
14377
+ }
14378
+ function logTransportFailure(options, operation, error2, authorization) {
14379
+ let detail = error2 instanceof Error ? error2.message : String(error2);
14380
+ const credential = authorization.split(/\s+/, 2)[1];
14381
+ for (const secret of [authorization, credential]) {
14382
+ if (secret) detail = detail.replaceAll(secret, "[redacted]");
14383
+ }
14384
+ options.log(
14385
+ `MANAGED-INFERENCE-TRANSPORT-FAILED: operation=${operation} detail=${redactDiagnosticText(detail)}`,
14386
+ "debug"
14387
+ );
14388
+ }
14389
+ async function responseJson(response) {
14390
+ try {
14391
+ const value = await response.json();
14392
+ return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
14393
+ } catch (error2) {
14394
+ return null;
14395
+ }
14396
+ }
14397
+ function readSetup(value) {
14398
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
14399
+ const setup = value;
14400
+ if (typeof setup.runner_id !== "string" || typeof setup.generation !== "string" || typeof setup.status !== "string" || setup.token_id !== null && typeof setup.token_id !== "string") {
14401
+ return null;
14402
+ }
14403
+ return setup;
14404
+ }
14405
+ function clearLocalSetup(options) {
14406
+ const env = options.env ?? process.env;
14407
+ (options.store ?? defaultStore).clear(options.agentId);
14408
+ delete env.EVIDENT_PROXY_TOKEN;
14409
+ try {
14410
+ const content = removeManagedOpenCodeConfigContent(env.OPENCODE_CONFIG_CONTENT);
14411
+ if (content === void 0) delete env.OPENCODE_CONFIG_CONTENT;
14412
+ else env.OPENCODE_CONFIG_CONTENT = content;
14413
+ } catch (error2) {
14414
+ options.log(
14415
+ "MANAGED-INFERENCE-CLEANUP-PARTIAL: stored credentials were removed, but malformed OPENCODE_CONFIG_CONTENT could not be inspected",
14416
+ "warn"
14417
+ );
14418
+ }
14419
+ }
14420
+ async function discoverManagedInferenceSetup(options) {
14421
+ const headers = requestHeaders(options);
14422
+ let response;
14423
+ try {
14424
+ response = await request2(options)(endpoint(options, "current"), {
14425
+ headers
14426
+ });
14427
+ } catch (error2) {
14428
+ logTransportFailure(options, "discovery", error2, headers.Authorization);
14429
+ return { kind: "failed", reason: safeFailure("Managed setup discovery") };
14430
+ }
14431
+ if (response.status === 404) {
14432
+ clearLocalSetup(options);
14433
+ return { kind: "none" };
14434
+ }
14435
+ if (!response.ok) {
14436
+ return { kind: "failed", reason: safeFailure("Managed setup discovery", response.status) };
14437
+ }
14438
+ const body = await responseJson(response);
14439
+ const setup = readSetup(body?.setup);
14440
+ return setup ? { kind: "setup", setup } : { kind: "failed", reason: "Managed setup discovery returned an invalid response" };
14441
+ }
14442
+ async function post(options, operation, body) {
14443
+ const headers = requestHeaders(options);
14444
+ try {
14445
+ return await request2(options)(endpoint(options, operation), {
14446
+ method: "POST",
14447
+ headers,
14448
+ body: JSON.stringify(body)
14449
+ });
14450
+ } catch (error2) {
14451
+ logTransportFailure(options, operation, error2, headers.Authorization);
14452
+ return null;
14453
+ }
14454
+ }
14455
+ function applyCredential(options, credential) {
14456
+ const { config, unsupportedProviders } = buildCredentialProxyNativeConfig({
14457
+ inferenceBaseUrl: credential.inferenceBaseUrl,
14458
+ modelDescriptors: credential.modelDescriptors
14459
+ });
14460
+ if (unsupportedProviders.length > 0 || Object.keys(config.provider).length === 0) {
14461
+ return "Managed setup contains unsupported provider descriptors";
14462
+ }
14463
+ const env = options.env ?? process.env;
14464
+ let merged;
14465
+ try {
14466
+ merged = mergeManagedOpenCodeConfigContent(env.OPENCODE_CONFIG_CONTENT, config.provider);
14467
+ } catch (error2) {
14468
+ return "OPENCODE_CONFIG_CONTENT is malformed; managed providers were not installed";
14469
+ }
14470
+ try {
14471
+ (options.store ?? defaultStore).set(options.agentId, credential);
14472
+ } catch (error2) {
14473
+ options.log(
14474
+ `MANAGED-INFERENCE-STORE-FAILED: runner=${options.agentId} generation=${credential.generation}`,
14475
+ "warn"
14476
+ );
14477
+ return "Managed credentials could not be written to the protected store";
14478
+ }
14479
+ env.EVIDENT_PROXY_TOKEN = credential.token;
14480
+ env.OPENCODE_CONFIG_CONTENT = merged;
14481
+ return null;
14482
+ }
14483
+ async function reportManagedInferenceApplyFailure(options, failure) {
14484
+ const response = await post(options, "report-failure", {
14485
+ generation: failure.generation,
14486
+ token_id: failure.tokenId
14487
+ });
14488
+ if (!response?.ok) {
14489
+ options.log(
14490
+ `MANAGED-INFERENCE-REPORT-FAILED: runner=${options.agentId} generation=${failure.generation}${response ? ` status=${response.status}` : ""}`,
14491
+ "warn"
14492
+ );
14493
+ }
14494
+ }
14495
+ async function reaffirmStored(options, credential) {
14496
+ const response = await post(options, "reaffirm", {
14497
+ generation: credential.generation,
14498
+ token_id: credential.tokenId
14499
+ });
14500
+ if (!response?.ok) {
14501
+ return {
14502
+ kind: "failed",
14503
+ reason: response?.status === 409 ? "Stored managed credential no longer matches this setup; restart required" : response ? safeFailure("Managed credential reaffirmation", response.status) : safeFailure("Managed credential reaffirmation")
14504
+ };
14505
+ }
14506
+ const body = await responseJson(response);
14507
+ const setup = readSetup(body?.setup);
14508
+ if (!setup || setup.generation !== credential.generation || setup.token_id !== credential.tokenId) {
14509
+ return { kind: "failed", reason: "Managed credential reaffirmation returned a mismatch" };
14510
+ }
14511
+ const failure = applyCredential(options, credential);
14512
+ return failure ? { kind: "failed", reason: failure } : { kind: "ready", generation: credential.generation, tokenId: credential.tokenId };
14513
+ }
14514
+ async function prepareManagedInferenceSetup(options, discovery, health) {
14515
+ if (health.authFailed) {
14516
+ return {
14517
+ kind: "failed",
14518
+ reason: "OpenCode authentication failed; managed credentials were not rotated"
14519
+ };
14520
+ }
14521
+ const stored = (options.store ?? defaultStore).get(options.agentId);
14522
+ if (health.healthy) {
14523
+ if (!stored) {
14524
+ return {
14525
+ kind: "failed",
14526
+ reason: "Managed credentials are missing for the serving OpenCode process; restart required"
14527
+ };
14528
+ }
14529
+ return reaffirmStored(options, stored);
14530
+ }
14531
+ if (discovery.setup.status === "acknowledged" && stored?.generation === discovery.setup.generation && stored.tokenId === discovery.setup.token_id) {
14532
+ return reaffirmStored(options, stored);
14533
+ }
14534
+ const response = await post(options, "bootstrap", { generation: discovery.setup.generation });
14535
+ if (!response?.ok) {
14536
+ return {
14537
+ kind: "failed",
14538
+ reason: response ? safeFailure("Managed credential bootstrap", response.status) : safeFailure("Managed credential bootstrap")
14539
+ };
14540
+ }
14541
+ const body = await responseJson(response);
14542
+ const setup = readSetup(body?.setup);
14543
+ const token = body?.inference_token;
14544
+ const inferenceBaseUrl = body?.inference_base_url;
14545
+ const modelDescriptors = body?.model_descriptors;
14546
+ if (!setup || setup.generation !== discovery.setup.generation || typeof setup.token_id !== "string" || typeof token !== "string" || typeof inferenceBaseUrl !== "string" || !Array.isArray(modelDescriptors) || !modelDescriptors.every(
14547
+ (descriptor) => typeof descriptor === "object" && descriptor !== null && typeof descriptor.id === "string" && typeof descriptor.provider === "string"
14548
+ )) {
14549
+ return { kind: "failed", reason: "Managed credential bootstrap returned an invalid response" };
14550
+ }
14551
+ const credential = {
14552
+ generation: setup.generation,
14553
+ tokenId: setup.token_id,
14554
+ token,
14555
+ inferenceBaseUrl,
14556
+ modelDescriptors
14557
+ };
14558
+ const failure = applyCredential(options, credential);
14559
+ if (failure) {
14560
+ await reportManagedInferenceApplyFailure(options, credential);
14561
+ return { kind: "failed", reason: failure };
14562
+ }
14563
+ return { kind: "ready", generation: credential.generation, tokenId: credential.tokenId };
14564
+ }
14565
+ async function acknowledgeManagedInferenceSetup(options, preparation) {
14566
+ const response = await post(options, "acknowledge", {
14567
+ generation: preparation.generation,
14568
+ token_id: preparation.tokenId,
14569
+ config_validated: true
14570
+ });
14571
+ if (!response?.ok) {
14572
+ throw new Error(
14573
+ response ? safeFailure("Managed credential acknowledgment", response.status) : safeFailure("Managed credential acknowledgment")
14574
+ );
14575
+ }
14576
+ const body = await responseJson(response);
14577
+ const setup = readSetup(body?.setup);
14578
+ if (!setup || setup.generation !== preparation.generation || setup.token_id !== preparation.tokenId) {
14579
+ throw new Error("Managed credential acknowledgment returned a mismatch");
14580
+ }
14581
+ return response.status === 200 && setup.status === "acknowledged" ? "acknowledged" : "pending";
14582
+ }
14583
+
14202
14584
  // src/commands/run.ts
14203
14585
  var MAX_ACTIVITY_LOG_ENTRIES = 10;
14204
14586
  var MAX_DISCONNECT_REASON_LENGTH = 200;
@@ -15983,6 +16365,40 @@ async function run(options) {
15983
16365
  for (const warning2 of opencodeVersionWarnings) {
15984
16366
  logActivity(state, { type: "info", level: "warn", message: warning2 });
15985
16367
  }
16368
+ const managedSetupOptions = {
16369
+ agentId: state.agentId,
16370
+ apiUrl: getApiUrlConfig(),
16371
+ getAuthHeader: () => state.authHeader,
16372
+ log: (message, level = "info") => logActivity(state, {
16373
+ type: level === "error" ? "error" : "info",
16374
+ level,
16375
+ ...level === "error" ? { error: message } : { message }
16376
+ })
16377
+ };
16378
+ const managedDiscovery = await discoverManagedInferenceSetup(managedSetupOptions);
16379
+ let managedPreparation = { kind: "none" };
16380
+ if (managedDiscovery.kind === "failed") {
16381
+ managedPreparation = managedDiscovery;
16382
+ managedSetupOptions.log(`MANAGED-INFERENCE-NOT-READY: ${managedDiscovery.reason}`, "warn");
16383
+ } else if (managedDiscovery.kind === "setup") {
16384
+ const managedHealth = opencodeVersion === "v2" ? await checkOpenCode2Health(state.port) : await checkOpenCodeHealth(state.port);
16385
+ managedPreparation = await prepareManagedInferenceSetup(
16386
+ managedSetupOptions,
16387
+ managedDiscovery,
16388
+ managedHealth
16389
+ );
16390
+ if (managedPreparation.kind === "failed") {
16391
+ managedSetupOptions.log(
16392
+ `MANAGED-INFERENCE-NOT-READY: ${managedPreparation.reason}`,
16393
+ "warn"
16394
+ );
16395
+ } else if (managedPreparation.kind === "ready") {
16396
+ managedSetupOptions.log(
16397
+ `MANAGED-INFERENCE-PREPARED: runner=${state.agentId} generation=${managedPreparation.generation}`,
16398
+ "info"
16399
+ );
16400
+ }
16401
+ }
15986
16402
  const { value: maxActiveSessions, warnings: maxActiveSessionsWarnings } = resolveMaxActiveSessions(options, process.env);
15987
16403
  for (const warning2 of maxActiveSessionsWarnings) {
15988
16404
  logActivity(state, { type: "info", level: "warn", message: warning2 });
@@ -16077,6 +16493,30 @@ async function run(options) {
16077
16493
  }
16078
16494
  }
16079
16495
  state.opencodeConnected = oc.notReadyReason === null;
16496
+ if (managedPreparation.kind === "ready" && oc.notReadyReason === null) {
16497
+ try {
16498
+ const acknowledgement = await acknowledgeManagedInferenceSetup(
16499
+ managedSetupOptions,
16500
+ managedPreparation
16501
+ );
16502
+ if (acknowledgement === "acknowledged") {
16503
+ managedSetupOptions.log(
16504
+ `MANAGED-INFERENCE-ACKNOWLEDGED: runner=${state.agentId} generation=${managedPreparation.generation}`,
16505
+ "info"
16506
+ );
16507
+ } else {
16508
+ managedSetupOptions.log(
16509
+ `MANAGED-INFERENCE-NOT-READY: policy refresh is pending for runner=${state.agentId} generation=${managedPreparation.generation}`,
16510
+ "warn"
16511
+ );
16512
+ }
16513
+ } catch (error2) {
16514
+ managedSetupOptions.log(
16515
+ `MANAGED-INFERENCE-NOT-READY: ${error2 instanceof Error ? error2.message : String(error2)}`,
16516
+ "warn"
16517
+ );
16518
+ }
16519
+ }
16080
16520
  const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
16081
16521
  ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
16082
16522
  if (!state.interactive && oc.notReadyReason !== null) {
@@ -16259,7 +16699,7 @@ async function run(options) {
16259
16699
  onConnected: (agentId, isReconnect, transition) => {
16260
16700
  state.connected = true;
16261
16701
  state.agentId = agentId;
16262
- const connectionMessage = isReconnect ? `Tunnel reconnected (runner: ${agentId}, generation: ${transition.generation}, attempt: ${transition.attempt}, offline for ${transition.downtimeMs}ms)` : `Tunnel connected (runner: ${agentId}, generation: ${transition.generation})`;
16702
+ const connectionMessage = isReconnect ? `Tunnel reconnected (runner: ${agentId}, generation: ${transition.generation}, relay connection: ${transition.relayConnectionId ?? "unknown"}, attempt: ${transition.attempt}, offline for ${transition.downtimeMs}ms)` : `Tunnel connected (runner: ${agentId}, generation: ${transition.generation}, relay connection: ${transition.relayConnectionId ?? "unknown"})`;
16263
16703
  logActivity(state, {
16264
16704
  type: "info",
16265
16705
  message: connectionMessage
@@ -16292,6 +16732,7 @@ async function run(options) {
16292
16732
  connected: state.opencodeConnected
16293
16733
  }),
16294
16734
  connection_generation: transition.generation,
16735
+ ...transition.relayConnectionId !== null ? { relay_connection_id: transition.relayConnectionId } : {},
16295
16736
  ...isReconnect && transition.downtimeMs !== null ? {
16296
16737
  reconnect_attempt: transition.attempt,
16297
16738
  downtime_ms: transition.downtimeMs
@@ -16322,12 +16763,13 @@ async function run(options) {
16322
16763
  const safeReason2 = redactDiagnosticText(reason).slice(0, MAX_DISCONNECT_REASON_LENGTH);
16323
16764
  logActivity(state, {
16324
16765
  type: "info",
16325
- message: `Tunnel disconnected (generation: ${transition.generation}, code: ${code}, reason: ${safeReason2})`
16766
+ message: `Tunnel disconnected (generation: ${transition.generation}, relay connection: ${transition.relayConnectionId ?? "unknown"}, code: ${code}, reason: ${safeReason2})`
16326
16767
  });
16327
16768
  emitAgentDisconnected(state.agentId, {
16328
16769
  code,
16329
16770
  reason: safeReason2,
16330
- connection_generation: transition.generation
16771
+ connection_generation: transition.generation,
16772
+ ...transition.relayConnectionId !== null ? { relay_connection_id: transition.relayConnectionId } : {}
16331
16773
  });
16332
16774
  if (state.interactive) displayStatus(state);
16333
16775
  },
@@ -16478,9 +16920,9 @@ program.name("evident").description("Run OpenCode locally and connect it to Evid
16478
16920
  "--endpoint <url>",
16479
16921
  "Evident API base URL (default: production; e.g. http://localhost:3001)"
16480
16922
  ).option("--tunnel <url>", "Tunnel WebSocket URL (default: production; e.g. ws://localhost:8787)").hook("preAction", (thisCommand) => {
16481
- const { endpoint, tunnel } = thisCommand.opts();
16482
- if (endpoint) {
16483
- setEndpoint(endpoint);
16923
+ const { endpoint: endpoint2, tunnel } = thisCommand.opts();
16924
+ if (endpoint2) {
16925
+ setEndpoint(endpoint2);
16484
16926
  }
16485
16927
  if (tunnel) {
16486
16928
  setTunnelUrl(tunnel);