@evident-ai/cli 3.6.1-dev.a6e5986 → 3.6.1-dev.a90767a
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 +313 -72
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2455,8 +2455,11 @@ import chalk7 from "chalk";
|
|
|
2455
2455
|
import ora4 from "ora";
|
|
2456
2456
|
import { select as select4 } from "@inquirer/prompts";
|
|
2457
2457
|
|
|
2458
|
+
// src/lib/opencode-start-timeout.ts
|
|
2459
|
+
var OPENCODE_START_TIMEOUT_ENV = "EVIDENT_OPENCODE_START_TIMEOUT";
|
|
2460
|
+
|
|
2458
2461
|
// src/lib/telemetry.ts
|
|
2459
|
-
var CLI_VERSION = (true ? "3.6.1-dev.
|
|
2462
|
+
var CLI_VERSION = (true ? "3.6.1-dev.a90767a" : void 0) ?? process.env.npm_package_version ?? "unknown";
|
|
2460
2463
|
function getCliVersion() {
|
|
2461
2464
|
return CLI_VERSION;
|
|
2462
2465
|
}
|
|
@@ -2647,6 +2650,22 @@ function redactDiagnosticText(message) {
|
|
|
2647
2650
|
const redacted = message.replace(/esk_[A-Za-z0-9_-]+/g, "esk_***").replace(/ct_[A-Za-z0-9_-]+/g, "ct_***").replace(/(?<![A-Za-z0-9_-])sk-ant-[A-Za-z0-9_-]{20,}(?![A-Za-z0-9_-])/g, "sk-ant-***").replace(/(?<![A-Za-z0-9_-])sk-proj-[A-Za-z0-9_-]{20,}(?![A-Za-z0-9_-])/g, "sk-proj-***").replace(/(?<![A-Za-z0-9_-])sk-[A-Za-z0-9]{20,}(?![A-Za-z0-9_-])/g, "sk-***").replace(/(?<![A-Za-z0-9_-])(gh[oprsu])_[A-Za-z0-9]{20,}(?![A-Za-z0-9_-])/g, "$1_***").replace(/(?<![A-Za-z0-9_-])github_pat_[A-Za-z0-9_]{20,}(?![A-Za-z0-9_-])/g, "github_pat_***").replace(/https?:\/\/\S+/g, "<url>");
|
|
2648
2651
|
return redactHomeDirectory(redacted);
|
|
2649
2652
|
}
|
|
2653
|
+
function describeErrorWithCause(error2) {
|
|
2654
|
+
const base = error2 instanceof Error ? error2.message : String(error2);
|
|
2655
|
+
const cause = error2 instanceof Error ? error2.cause : void 0;
|
|
2656
|
+
let detail;
|
|
2657
|
+
if (cause instanceof Error) {
|
|
2658
|
+
const causeWithCode = cause;
|
|
2659
|
+
detail = cause.message || (typeof causeWithCode.code === "string" ? causeWithCode.code : void 0) || cause.name;
|
|
2660
|
+
} else if (cause !== null && typeof cause === "object") {
|
|
2661
|
+
const causeRecord = cause;
|
|
2662
|
+
detail = typeof causeRecord.message === "string" && causeRecord.message || typeof causeRecord.code === "string" && causeRecord.code || void 0;
|
|
2663
|
+
} else if (typeof cause === "string") {
|
|
2664
|
+
detail = cause;
|
|
2665
|
+
}
|
|
2666
|
+
if (!detail || base.includes(detail)) return base;
|
|
2667
|
+
return `${base} (${redactDiagnosticText(detail).slice(0, 200)})`;
|
|
2668
|
+
}
|
|
2650
2669
|
|
|
2651
2670
|
// src/lib/opencode/session-db-reclaim.ts
|
|
2652
2671
|
var SESSION_DB_RECLAIM_CHUNK_PAGES = 100;
|
|
@@ -5218,9 +5237,11 @@ async function checkOpenCode2Health(port, password) {
|
|
|
5218
5237
|
return { healthy: false, error: message };
|
|
5219
5238
|
}
|
|
5220
5239
|
}
|
|
5221
|
-
async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
|
|
5240
|
+
async function waitForOpenCodeHealth(port, timeoutMs = 3e4, stopEarly) {
|
|
5222
5241
|
const startTime = Date.now();
|
|
5223
5242
|
while (Date.now() - startTime < timeoutMs) {
|
|
5243
|
+
const earlyStopReason = stopEarly?.();
|
|
5244
|
+
if (earlyStopReason) return { healthy: false, error: earlyStopReason };
|
|
5224
5245
|
const health = await checkOpenCodeHealth(port);
|
|
5225
5246
|
if (health.healthy || health.authFailed) {
|
|
5226
5247
|
return health;
|
|
@@ -5229,9 +5250,11 @@ async function waitForOpenCodeHealth(port, timeoutMs = 3e4) {
|
|
|
5229
5250
|
}
|
|
5230
5251
|
return { healthy: false, error: "Timeout waiting for OpenCode to be healthy" };
|
|
5231
5252
|
}
|
|
5232
|
-
async function waitForOpenCode2Health(port, password, timeoutMs = 3e4) {
|
|
5253
|
+
async function waitForOpenCode2Health(port, password, timeoutMs = 3e4, stopEarly) {
|
|
5233
5254
|
const startTime = Date.now();
|
|
5234
5255
|
while (Date.now() - startTime < timeoutMs) {
|
|
5256
|
+
const earlyStopReason = stopEarly?.();
|
|
5257
|
+
if (earlyStopReason) return { healthy: false, error: earlyStopReason };
|
|
5235
5258
|
const health = await checkOpenCode2Health(port, password);
|
|
5236
5259
|
if (health.healthy || health.authFailed) {
|
|
5237
5260
|
return health;
|
|
@@ -8415,28 +8438,29 @@ function getProcessCwd(pid) {
|
|
|
8415
8438
|
}
|
|
8416
8439
|
return void 0;
|
|
8417
8440
|
}
|
|
8418
|
-
function
|
|
8419
|
-
|
|
8420
|
-
|
|
8421
|
-
|
|
8422
|
-
|
|
8423
|
-
|
|
8424
|
-
|
|
8425
|
-
|
|
8426
|
-
|
|
8441
|
+
function watchOpenCodeExit(child, log3) {
|
|
8442
|
+
let spawnError = null;
|
|
8443
|
+
child.once("error", (error2) => {
|
|
8444
|
+
spawnError = error2;
|
|
8445
|
+
const code = spawnError.code ?? "unknown error";
|
|
8446
|
+
log3(
|
|
8447
|
+
`[watchOpenCodeExit] OpenCode command ${child.spawnfile ?? "unknown"} could not be spawned: ${code}: ${spawnError.message}`,
|
|
8448
|
+
"error"
|
|
8449
|
+
);
|
|
8450
|
+
});
|
|
8451
|
+
return () => {
|
|
8452
|
+
if (spawnError) {
|
|
8453
|
+
const code = spawnError.code ?? "unknown error";
|
|
8454
|
+
return `it could not be spawned (${code}: ${spawnError.message})`;
|
|
8427
8455
|
}
|
|
8428
|
-
|
|
8429
|
-
|
|
8430
|
-
return false;
|
|
8431
|
-
}
|
|
8432
|
-
function findAvailablePort(startPort, maxAttempts = 10) {
|
|
8433
|
-
for (let i = 0; i < maxAttempts; i++) {
|
|
8434
|
-
const port = startPort + i;
|
|
8435
|
-
if (!isPortInUse(port)) {
|
|
8436
|
-
return port;
|
|
8456
|
+
if (child.signalCode !== null && child.signalCode !== void 0) {
|
|
8457
|
+
return `it was killed by ${child.signalCode} before it answered`;
|
|
8437
8458
|
}
|
|
8438
|
-
|
|
8439
|
-
|
|
8459
|
+
if (child.exitCode !== null && child.exitCode !== void 0) {
|
|
8460
|
+
return `it exited with status ${child.exitCode} before it answered`;
|
|
8461
|
+
}
|
|
8462
|
+
return null;
|
|
8463
|
+
};
|
|
8440
8464
|
}
|
|
8441
8465
|
function findProcessesByPattern(pgrepPattern, psPattern) {
|
|
8442
8466
|
const instances = [];
|
|
@@ -8626,6 +8650,144 @@ function stopOpenCodeAndWait(opencodeProcess, timeoutMs) {
|
|
|
8626
8650
|
);
|
|
8627
8651
|
}
|
|
8628
8652
|
|
|
8653
|
+
// src/lib/opencode/port.ts
|
|
8654
|
+
import { execFileSync as execFileSync2 } from "node:child_process";
|
|
8655
|
+
import { createServer as createServer2 } from "node:net";
|
|
8656
|
+
var HOLDER_LOOKUP_TIMEOUT_MS = 250;
|
|
8657
|
+
var RECLAIM_SIGNAL_WAIT_MS = 250;
|
|
8658
|
+
var RECLAIM_POLL_INTERVAL_MS = 25;
|
|
8659
|
+
function isPortInUse(port) {
|
|
8660
|
+
return new Promise((resolve6) => {
|
|
8661
|
+
const server = createServer2();
|
|
8662
|
+
server.once("error", () => resolve6(true));
|
|
8663
|
+
server.listen({ port, host: "127.0.0.1", exclusive: true }, () => {
|
|
8664
|
+
server.close(() => resolve6(false));
|
|
8665
|
+
});
|
|
8666
|
+
});
|
|
8667
|
+
}
|
|
8668
|
+
async function findAvailablePort(startPort, maxAttempts = 10) {
|
|
8669
|
+
for (let i = 0; i < maxAttempts; i++) {
|
|
8670
|
+
const port = startPort + i;
|
|
8671
|
+
if (!await isPortInUse(port)) return port;
|
|
8672
|
+
}
|
|
8673
|
+
return null;
|
|
8674
|
+
}
|
|
8675
|
+
function outputText(value) {
|
|
8676
|
+
if (typeof value === "string") return value;
|
|
8677
|
+
if (Buffer.isBuffer(value)) return value.toString("utf8");
|
|
8678
|
+
return value === void 0 || value === null ? "" : String(value);
|
|
8679
|
+
}
|
|
8680
|
+
function holderPids(stdout) {
|
|
8681
|
+
const tokens = stdout.trim() ? stdout.trim().split(/\s+/) : [];
|
|
8682
|
+
if (tokens.length === 0) return [];
|
|
8683
|
+
if (!tokens.every((token) => /^\d+$/.test(token))) return null;
|
|
8684
|
+
return tokens.map(Number);
|
|
8685
|
+
}
|
|
8686
|
+
function isExecTimeout(error2) {
|
|
8687
|
+
if (!error2 || typeof error2 !== "object") return false;
|
|
8688
|
+
const candidate = error2;
|
|
8689
|
+
return candidate.code === "ETIMEDOUT" || candidate.killed === true && candidate.signal === "SIGKILL";
|
|
8690
|
+
}
|
|
8691
|
+
function listLoopbackPortHolders(port) {
|
|
8692
|
+
try {
|
|
8693
|
+
const stdout = outputText(
|
|
8694
|
+
execFileSync2("fuser", ["-n", "tcp", String(port)], {
|
|
8695
|
+
encoding: "utf8",
|
|
8696
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
8697
|
+
// The resume path must not wait indefinitely on a broken /proc read.
|
|
8698
|
+
timeout: HOLDER_LOOKUP_TIMEOUT_MS,
|
|
8699
|
+
killSignal: "SIGKILL"
|
|
8700
|
+
})
|
|
8701
|
+
);
|
|
8702
|
+
const pids = holderPids(stdout);
|
|
8703
|
+
return pids !== null && pids.length > 0 ? pids : { error: "fuser failed to identify the port holder" };
|
|
8704
|
+
} catch (error2) {
|
|
8705
|
+
const candidate = error2;
|
|
8706
|
+
const stdout = outputText(candidate.stdout);
|
|
8707
|
+
const stderr = outputText(candidate.stderr);
|
|
8708
|
+
const pids = holderPids(stdout);
|
|
8709
|
+
if (pids === null) return { error: "fuser failed to identify the port holder" };
|
|
8710
|
+
if (pids.length > 0) return pids;
|
|
8711
|
+
if (candidate.code === "ENOENT") return { error: "fuser is unavailable" };
|
|
8712
|
+
if (isExecTimeout(error2)) return { error: "fuser timed out while identifying the port holder" };
|
|
8713
|
+
if (candidate.status === 1 && stderr.trim() === "") return [];
|
|
8714
|
+
return { error: "fuser failed to identify the port holder" };
|
|
8715
|
+
}
|
|
8716
|
+
}
|
|
8717
|
+
async function identifyHolders(listHolders) {
|
|
8718
|
+
let timer;
|
|
8719
|
+
const timeout = new Promise((resolve6) => {
|
|
8720
|
+
timer = setTimeout(
|
|
8721
|
+
() => resolve6({ error: "holder identification timed out" }),
|
|
8722
|
+
HOLDER_LOOKUP_TIMEOUT_MS
|
|
8723
|
+
);
|
|
8724
|
+
});
|
|
8725
|
+
try {
|
|
8726
|
+
return await Promise.race([Promise.resolve().then(listHolders), timeout]);
|
|
8727
|
+
} catch (error2) {
|
|
8728
|
+
return {
|
|
8729
|
+
error: `holder identification failed: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
8730
|
+
};
|
|
8731
|
+
} finally {
|
|
8732
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
8733
|
+
}
|
|
8734
|
+
}
|
|
8735
|
+
function isErrno(error2, code) {
|
|
8736
|
+
return error2 !== null && typeof error2 === "object" && "code" in error2 && error2.code === code;
|
|
8737
|
+
}
|
|
8738
|
+
function signalHolders(pids, signal) {
|
|
8739
|
+
let failure = null;
|
|
8740
|
+
for (const pid of pids) {
|
|
8741
|
+
try {
|
|
8742
|
+
process.kill(pid, signal);
|
|
8743
|
+
} catch (error2) {
|
|
8744
|
+
if (isErrno(error2, "ESRCH")) continue;
|
|
8745
|
+
failure ??= `could not send ${signal} to holder ${pid}: ${error2 instanceof Error ? error2.message : String(error2)}`;
|
|
8746
|
+
}
|
|
8747
|
+
}
|
|
8748
|
+
return failure;
|
|
8749
|
+
}
|
|
8750
|
+
async function waitForPortFree(port, waitMs) {
|
|
8751
|
+
const deadline = Date.now() + waitMs;
|
|
8752
|
+
let probeError;
|
|
8753
|
+
for (; ; ) {
|
|
8754
|
+
try {
|
|
8755
|
+
if (!await isPortInUse(port)) return { free: true };
|
|
8756
|
+
} catch (error2) {
|
|
8757
|
+
probeError = error2 instanceof Error ? error2.message : String(error2);
|
|
8758
|
+
}
|
|
8759
|
+
const remaining = deadline - Date.now();
|
|
8760
|
+
if (remaining <= 0) return { free: false, detail: probeError };
|
|
8761
|
+
await new Promise(
|
|
8762
|
+
(resolve6) => setTimeout(resolve6, Math.min(RECLAIM_POLL_INTERVAL_MS, remaining))
|
|
8763
|
+
);
|
|
8764
|
+
}
|
|
8765
|
+
}
|
|
8766
|
+
async function reclaimLoopbackPort(port, options = {}) {
|
|
8767
|
+
const holderResult = await identifyHolders(
|
|
8768
|
+
options.listHolders ?? (() => listLoopbackPortHolders(port))
|
|
8769
|
+
);
|
|
8770
|
+
if (!Array.isArray(holderResult)) {
|
|
8771
|
+
return { kind: "blocked", port, detail: holderResult.error };
|
|
8772
|
+
}
|
|
8773
|
+
if (holderResult.length === 0) return { kind: "no-holder", port };
|
|
8774
|
+
const termFailure = signalHolders(holderResult, "SIGTERM");
|
|
8775
|
+
const afterTerm = await waitForPortFree(port, RECLAIM_SIGNAL_WAIT_MS);
|
|
8776
|
+
if (afterTerm.free && termFailure === null) {
|
|
8777
|
+
return { kind: "freed", port, pids: holderResult, usedSigkill: false };
|
|
8778
|
+
}
|
|
8779
|
+
const killFailure = signalHolders(holderResult, "SIGKILL");
|
|
8780
|
+
const afterKill = await waitForPortFree(port, RECLAIM_SIGNAL_WAIT_MS);
|
|
8781
|
+
if (!afterKill.free) {
|
|
8782
|
+
return {
|
|
8783
|
+
kind: "blocked",
|
|
8784
|
+
port,
|
|
8785
|
+
detail: afterKill.detail ?? termFailure ?? killFailure ?? "the port remained in use after SIGTERM and SIGKILL"
|
|
8786
|
+
};
|
|
8787
|
+
}
|
|
8788
|
+
return { kind: "freed", port, pids: holderResult, usedSigkill: true };
|
|
8789
|
+
}
|
|
8790
|
+
|
|
8629
8791
|
// src/lib/opencode/install.ts
|
|
8630
8792
|
import { execSync as execSync2 } from "child_process";
|
|
8631
8793
|
import chalk4 from "chalk";
|
|
@@ -10823,6 +10985,12 @@ var ChannelTerminalError = class extends Error {
|
|
|
10823
10985
|
this.status = status2;
|
|
10824
10986
|
}
|
|
10825
10987
|
};
|
|
10988
|
+
var LocalOpenCodeError = class extends Error {
|
|
10989
|
+
constructor(message, options) {
|
|
10990
|
+
super(message, options);
|
|
10991
|
+
this.name = "LocalOpenCodeError";
|
|
10992
|
+
}
|
|
10993
|
+
};
|
|
10826
10994
|
function backoffDelay(attempt, policy) {
|
|
10827
10995
|
const exp = policy.baseDelayMs * Math.pow(2, attempt);
|
|
10828
10996
|
const capped = Math.min(policy.maxDelayMs, exp);
|
|
@@ -11254,7 +11422,14 @@ var ChannelDriver = class _ChannelDriver {
|
|
|
11254
11422
|
return this.isV2 ? isV2SessionOngoing(this.openCodeClient, sessionId) : isSessionOngoing(this.port, sessionId, this.openCodeClient);
|
|
11255
11423
|
}
|
|
11256
11424
|
async createOpenCodeSession(directory) {
|
|
11257
|
-
|
|
11425
|
+
try {
|
|
11426
|
+
return await (this.isV2 ? createV2Session(this.openCodeClient, directory) : createOpenCodeSession(this.port, directory, this.openCodeClient));
|
|
11427
|
+
} catch (err) {
|
|
11428
|
+
throw new LocalOpenCodeError(
|
|
11429
|
+
`creating an OpenCode session on port ${this.port} failed: ${describeErrorWithCause(err)}`,
|
|
11430
|
+
{ cause: err }
|
|
11431
|
+
);
|
|
11432
|
+
}
|
|
11258
11433
|
}
|
|
11259
11434
|
async hasAnyConfiguredProvider() {
|
|
11260
11435
|
return hasAnyConfiguredProvider(this.port, this.openCodeClient);
|
|
@@ -16613,12 +16788,19 @@ import chalk5 from "chalk";
|
|
|
16613
16788
|
import ora2 from "ora";
|
|
16614
16789
|
import { select as select2 } from "@inquirer/prompts";
|
|
16615
16790
|
var INTERACTIVE_START_TIMEOUT_MS = 3e4;
|
|
16616
|
-
function
|
|
16617
|
-
|
|
16791
|
+
async function reclaimNonInteractivePort(port, options) {
|
|
16792
|
+
const isPortInUseFn = options.isPortInUse ?? isPortInUse;
|
|
16793
|
+
if (!await isPortInUseFn(port)) return void 0;
|
|
16794
|
+
if (!options.imageManaged) {
|
|
16618
16795
|
throw new Error(
|
|
16619
16796
|
`Port ${port} is already in use by a non-OpenCode process. Free it or pass --port.`
|
|
16620
16797
|
);
|
|
16621
16798
|
}
|
|
16799
|
+
const outcome = await (options.reclaimLoopbackPort ?? reclaimLoopbackPort)(port);
|
|
16800
|
+
if (outcome.kind !== "blocked") return outcome;
|
|
16801
|
+
throw new Error(
|
|
16802
|
+
`Port ${port} is held by an unresponsive process. Evident tried to reclaim it but could not free it: ${outcome.detail}. Stop the holder manually or pass --port.`
|
|
16803
|
+
);
|
|
16622
16804
|
}
|
|
16623
16805
|
function unknownPasswordError(port) {
|
|
16624
16806
|
return new Error(
|
|
@@ -16675,24 +16857,29 @@ async function ensureOpenCodeRunning(ctx) {
|
|
|
16675
16857
|
}
|
|
16676
16858
|
}
|
|
16677
16859
|
if (!ctx.interactive) {
|
|
16678
|
-
|
|
16860
|
+
const reclaimOutcome = await reclaimNonInteractivePort(ctx.port, {
|
|
16861
|
+
imageManaged: ctx.inheritStdio === true
|
|
16862
|
+
});
|
|
16679
16863
|
ctx.log(`OpenCode is not running on port ${ctx.port}. Starting it automatically...`);
|
|
16680
16864
|
const proc = await startOpenCode(ctx.port, { inheritStdio: ctx.inheritStdio });
|
|
16681
|
-
const
|
|
16865
|
+
const stopEarly = watchOpenCodeExit(proc, ctx.log);
|
|
16866
|
+
const health = await waitForOpenCodeHealth(ctx.port, ctx.startTimeoutMs, stopEarly);
|
|
16682
16867
|
if (!health.healthy) {
|
|
16683
16868
|
if (health.authFailed) {
|
|
16684
16869
|
return {
|
|
16685
16870
|
port: ctx.port,
|
|
16686
16871
|
process: proc,
|
|
16687
16872
|
version: null,
|
|
16688
|
-
notReadyReason: "it is answering with an authentication challenge, likely because OPENCODE_SERVER_PASSWORD is set in the environment of the process that started it
|
|
16873
|
+
notReadyReason: "it is answering with an authentication challenge, likely because OPENCODE_SERVER_PASSWORD is set in the environment of the process that started it",
|
|
16874
|
+
...reclaimOutcome ? { reclaimOutcome } : {}
|
|
16689
16875
|
};
|
|
16690
16876
|
}
|
|
16691
16877
|
return {
|
|
16692
16878
|
port: ctx.port,
|
|
16693
16879
|
process: proc,
|
|
16694
16880
|
version: null,
|
|
16695
|
-
notReadyReason: `it did not become healthy within ${Math.round(ctx.startTimeoutMs / 1e3)}s
|
|
16881
|
+
notReadyReason: stopEarly() ?? `it did not become healthy within ${Math.round(ctx.startTimeoutMs / 1e3)}s; raise the wait with --opencode-start-timeout <seconds> (env ${OPENCODE_START_TIMEOUT_ENV})`,
|
|
16882
|
+
...reclaimOutcome ? { reclaimOutcome } : {}
|
|
16696
16883
|
};
|
|
16697
16884
|
}
|
|
16698
16885
|
ctx.log(`OpenCode started on port ${ctx.port}${health.version ? ` (v${health.version})` : ""}`);
|
|
@@ -16700,14 +16887,15 @@ async function ensureOpenCodeRunning(ctx) {
|
|
|
16700
16887
|
port: ctx.port,
|
|
16701
16888
|
process: proc,
|
|
16702
16889
|
version: health.version ?? null,
|
|
16703
|
-
notReadyReason: null
|
|
16890
|
+
notReadyReason: null,
|
|
16891
|
+
...reclaimOutcome ? { reclaimOutcome } : {}
|
|
16704
16892
|
};
|
|
16705
16893
|
}
|
|
16706
16894
|
let port = ctx.port;
|
|
16707
|
-
if (isPortInUse(port)) {
|
|
16895
|
+
if (await isPortInUse(port)) {
|
|
16708
16896
|
console.log(chalk5.yellow(`
|
|
16709
16897
|
Port ${port} is already in use.`));
|
|
16710
|
-
const alternativePort = findAvailablePort(port + 1);
|
|
16898
|
+
const alternativePort = await findAvailablePort(port + 1);
|
|
16711
16899
|
if (alternativePort) {
|
|
16712
16900
|
const useAlternative = await select2({
|
|
16713
16901
|
message: `Use port ${alternativePort} instead?`,
|
|
@@ -16754,13 +16942,15 @@ Port ${port} is already in use.`));
|
|
|
16754
16942
|
if (action === "start") {
|
|
16755
16943
|
const spinner = ora2("Starting OpenCode...").start();
|
|
16756
16944
|
const proc = await startOpenCode(port, { inheritStdio: ctx.inheritStdio });
|
|
16757
|
-
const
|
|
16945
|
+
const stopEarly = watchOpenCodeExit(proc, ctx.log);
|
|
16946
|
+
const health = await waitForOpenCodeHealth(port, INTERACTIVE_START_TIMEOUT_MS, stopEarly);
|
|
16758
16947
|
if (!health.healthy) {
|
|
16759
16948
|
spinner.fail("Failed to start OpenCode");
|
|
16760
16949
|
if (health.authFailed) {
|
|
16761
16950
|
throw unknownPasswordError(port);
|
|
16762
16951
|
}
|
|
16763
|
-
|
|
16952
|
+
const exitReason = stopEarly();
|
|
16953
|
+
throw new Error(`OpenCode failed to start${exitReason ? `: ${exitReason}` : ""}`);
|
|
16764
16954
|
}
|
|
16765
16955
|
spinner.stop();
|
|
16766
16956
|
return { port, process: proc, version: health.version ?? null, notReadyReason: null };
|
|
@@ -16799,40 +16989,24 @@ async function ensureOpenCode2Running(ctx) {
|
|
|
16799
16989
|
}
|
|
16800
16990
|
let port = ctx.port;
|
|
16801
16991
|
if (!ctx.interactive) {
|
|
16802
|
-
|
|
16803
|
-
|
|
16804
|
-
|
|
16805
|
-
Port ${port} is already in use.`));
|
|
16806
|
-
const alternativePort = findAvailablePort(port + 1);
|
|
16807
|
-
if (alternativePort) {
|
|
16808
|
-
const useAlternative = await select3({
|
|
16809
|
-
message: `Use port ${alternativePort} instead?`,
|
|
16810
|
-
choices: [
|
|
16811
|
-
{ name: `Yes, use port ${alternativePort}`, value: "yes" },
|
|
16812
|
-
{ name: "No, I will free the port manually", value: "no" }
|
|
16813
|
-
]
|
|
16814
|
-
});
|
|
16815
|
-
if (useAlternative === "yes") {
|
|
16816
|
-
port = alternativePort;
|
|
16817
|
-
} else {
|
|
16818
|
-
throw new Error(`Port ${ctx.port} is in use`);
|
|
16819
|
-
}
|
|
16820
|
-
}
|
|
16821
|
-
}
|
|
16822
|
-
if (!ctx.interactive) {
|
|
16992
|
+
const reclaimOutcome = await reclaimNonInteractivePort(port, {
|
|
16993
|
+
imageManaged: ctx.inheritStdio === true
|
|
16994
|
+
});
|
|
16823
16995
|
ctx.log(`OpenCode V2 is not running on port ${port}. Starting it automatically...`);
|
|
16824
16996
|
const { child: proc, password } = await startOpenCode2(port, {
|
|
16825
16997
|
inheritStdio: ctx.inheritStdio,
|
|
16826
16998
|
command: ctx.command
|
|
16827
16999
|
});
|
|
16828
|
-
const
|
|
17000
|
+
const stopEarly = watchOpenCodeExit(proc, ctx.log);
|
|
17001
|
+
const health = await waitForOpenCode2Health(port, password, ctx.startTimeoutMs, stopEarly);
|
|
16829
17002
|
if (!health.healthy) {
|
|
16830
17003
|
return {
|
|
16831
17004
|
port,
|
|
16832
17005
|
process: proc,
|
|
16833
17006
|
version: null,
|
|
16834
|
-
notReadyReason: `it did not become healthy within ${Math.round(ctx.startTimeoutMs / 1e3)}s`,
|
|
16835
|
-
password
|
|
17007
|
+
notReadyReason: stopEarly() ?? `it did not become healthy within ${Math.round(ctx.startTimeoutMs / 1e3)}s; raise the wait with --opencode-start-timeout <seconds> (env ${OPENCODE_START_TIMEOUT_ENV})`,
|
|
17008
|
+
password,
|
|
17009
|
+
...reclaimOutcome ? { reclaimOutcome } : {}
|
|
16836
17010
|
};
|
|
16837
17011
|
}
|
|
16838
17012
|
ctx.log(`OpenCode V2 started on port ${port}${health.version ? ` (v${health.version})` : ""}`);
|
|
@@ -16841,8 +17015,27 @@ Port ${port} is already in use.`));
|
|
|
16841
17015
|
process: proc,
|
|
16842
17016
|
version: health.version ?? null,
|
|
16843
17017
|
notReadyReason: null,
|
|
16844
|
-
password
|
|
17018
|
+
password,
|
|
17019
|
+
...reclaimOutcome ? { reclaimOutcome } : {}
|
|
16845
17020
|
};
|
|
17021
|
+
} else if (await isPortInUse(port)) {
|
|
17022
|
+
console.log(chalk6.yellow(`
|
|
17023
|
+
Port ${port} is already in use.`));
|
|
17024
|
+
const alternativePort = await findAvailablePort(port + 1);
|
|
17025
|
+
if (alternativePort) {
|
|
17026
|
+
const useAlternative = await select3({
|
|
17027
|
+
message: `Use port ${alternativePort} instead?`,
|
|
17028
|
+
choices: [
|
|
17029
|
+
{ name: `Yes, use port ${alternativePort}`, value: "yes" },
|
|
17030
|
+
{ name: "No, I will free the port manually", value: "no" }
|
|
17031
|
+
]
|
|
17032
|
+
});
|
|
17033
|
+
if (useAlternative === "yes") {
|
|
17034
|
+
port = alternativePort;
|
|
17035
|
+
} else {
|
|
17036
|
+
throw new Error(`Port ${ctx.port} is in use`);
|
|
17037
|
+
}
|
|
17038
|
+
}
|
|
16846
17039
|
}
|
|
16847
17040
|
const command = ctx.command ?? "opencode2";
|
|
16848
17041
|
const action = await select3({
|
|
@@ -16879,10 +17072,17 @@ Port ${port} is already in use.`));
|
|
|
16879
17072
|
inheritStdio: ctx.inheritStdio,
|
|
16880
17073
|
command: ctx.command
|
|
16881
17074
|
});
|
|
16882
|
-
const
|
|
17075
|
+
const stopEarly = watchOpenCodeExit(proc, ctx.log);
|
|
17076
|
+
const health = await waitForOpenCode2Health(
|
|
17077
|
+
port,
|
|
17078
|
+
password,
|
|
17079
|
+
INTERACTIVE_START_TIMEOUT_MS2,
|
|
17080
|
+
stopEarly
|
|
17081
|
+
);
|
|
16883
17082
|
if (!health.healthy) {
|
|
16884
17083
|
spinner.fail("Failed to start OpenCode V2");
|
|
16885
|
-
|
|
17084
|
+
const exitReason = stopEarly();
|
|
17085
|
+
throw new Error(`OpenCode V2 failed to start${exitReason ? `: ${exitReason}` : ""}`);
|
|
16886
17086
|
}
|
|
16887
17087
|
spinner.stop();
|
|
16888
17088
|
return {
|
|
@@ -17178,7 +17378,7 @@ async function configureGitHubAccess({ env, log: log3 }) {
|
|
|
17178
17378
|
}
|
|
17179
17379
|
|
|
17180
17380
|
// src/lib/opencode/config-overlay.ts
|
|
17181
|
-
import { execFileSync as
|
|
17381
|
+
import { execFileSync as execFileSync3 } from "node:child_process";
|
|
17182
17382
|
import { copyFileSync, existsSync as existsSync2, statSync as statSync6 } from "node:fs";
|
|
17183
17383
|
import { isAbsolute as isAbsolute5, join as join15, resolve as resolve5 } from "node:path";
|
|
17184
17384
|
function parseConfigContent(content) {
|
|
@@ -17220,7 +17420,7 @@ function applyRunnerOpenCodeConfig({
|
|
|
17220
17420
|
}
|
|
17221
17421
|
copyFileSync(source, join15(cwd, target));
|
|
17222
17422
|
try {
|
|
17223
|
-
|
|
17423
|
+
execFileSync3("git", ["-C", cwd, "update-index", "--skip-worktree", target], {
|
|
17224
17424
|
stdio: "ignore"
|
|
17225
17425
|
});
|
|
17226
17426
|
} catch (error2) {
|
|
@@ -17990,7 +18190,6 @@ function resolveFileSyncExactFiles(raw, homeDir) {
|
|
|
17990
18190
|
}
|
|
17991
18191
|
var DEFAULT_OPENCODE_START_TIMEOUT_SECONDS = 180;
|
|
17992
18192
|
var MAX_OPENCODE_START_TIMEOUT_SECONDS = 3600;
|
|
17993
|
-
var OPENCODE_START_TIMEOUT_ENV = "EVIDENT_OPENCODE_START_TIMEOUT";
|
|
17994
18193
|
var OPENCODE_VERSION_ENV = "EVIDENT_OPENCODE_VERSION";
|
|
17995
18194
|
function resolveOpenCodeVersion(options, env = process.env) {
|
|
17996
18195
|
let raw;
|
|
@@ -18404,6 +18603,8 @@ async function driveChannels(state, driver) {
|
|
|
18404
18603
|
let idleMs = 0;
|
|
18405
18604
|
let consecutiveDrainFailures = 0;
|
|
18406
18605
|
let unreachableMs = 0;
|
|
18606
|
+
let consecutiveOpenCodeFailures = 0;
|
|
18607
|
+
let openCodeFailureMs = 0;
|
|
18407
18608
|
let lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
|
|
18408
18609
|
let lastSeenAppliedFiles = driver.fileSyncActivity().appliedFiles;
|
|
18409
18610
|
let lastSeenClaudeApplies = driver.fileSyncActivity().claudeCredentialApplies;
|
|
@@ -18421,6 +18622,7 @@ async function driveChannels(state, driver) {
|
|
|
18421
18622
|
const cycleStartedAtMs = performance.now();
|
|
18422
18623
|
let idleThisCycle = false;
|
|
18423
18624
|
let unreachableThisCycle = false;
|
|
18625
|
+
let openCodeFailureThisCycle = false;
|
|
18424
18626
|
if (state.connection?.reconnecting && state.connection.reconnectPromise) {
|
|
18425
18627
|
logActivity(state, { type: "info", message: "Waiting for tunnel reconnection..." });
|
|
18426
18628
|
if (state.interactive) displayStatus(state);
|
|
@@ -18437,6 +18639,8 @@ async function driveChannels(state, driver) {
|
|
|
18437
18639
|
const processed = await driver.drainPending();
|
|
18438
18640
|
consecutiveDrainFailures = 0;
|
|
18439
18641
|
unreachableMs = 0;
|
|
18642
|
+
consecutiveOpenCodeFailures = 0;
|
|
18643
|
+
openCodeFailureMs = 0;
|
|
18440
18644
|
state.messageCount += processed;
|
|
18441
18645
|
if (driver.recycleRequested) {
|
|
18442
18646
|
await beginGracefulShutdown(state, "recycle");
|
|
@@ -18518,12 +18722,28 @@ async function driveChannels(state, driver) {
|
|
|
18518
18722
|
state.running = false;
|
|
18519
18723
|
break;
|
|
18520
18724
|
}
|
|
18521
|
-
const errorMessage5 =
|
|
18725
|
+
const errorMessage5 = describeErrorWithCause(error2);
|
|
18522
18726
|
logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage5}` });
|
|
18523
18727
|
if (state.interactive) displayStatus(state);
|
|
18524
18728
|
if (driver.hasInFlightWatchers()) {
|
|
18525
18729
|
consecutiveDrainFailures = 0;
|
|
18526
18730
|
unreachableMs = 0;
|
|
18731
|
+
consecutiveOpenCodeFailures = 0;
|
|
18732
|
+
openCodeFailureMs = 0;
|
|
18733
|
+
} else if (error2 instanceof LocalOpenCodeError) {
|
|
18734
|
+
consecutiveDrainFailures = 0;
|
|
18735
|
+
unreachableMs = 0;
|
|
18736
|
+
if (state.idleTimeout !== null) {
|
|
18737
|
+
consecutiveOpenCodeFailures++;
|
|
18738
|
+
openCodeFailureThisCycle = true;
|
|
18739
|
+
if (consecutiveOpenCodeFailures === 1) {
|
|
18740
|
+
logActivity(state, {
|
|
18741
|
+
type: "info",
|
|
18742
|
+
message: `Local OpenCode server on port ${state.port} is failing, will exit if this persists past the idle timeout (timeout: ${state.idleTimeout}s)...`
|
|
18743
|
+
});
|
|
18744
|
+
if (state.interactive) displayStatus(state);
|
|
18745
|
+
}
|
|
18746
|
+
}
|
|
18527
18747
|
} else if (state.idleTimeout !== null) {
|
|
18528
18748
|
consecutiveDrainFailures++;
|
|
18529
18749
|
unreachableThisCycle = true;
|
|
@@ -18583,6 +18803,7 @@ async function driveChannels(state, driver) {
|
|
|
18583
18803
|
const cycleMs = performance.now() - cycleStartedAtMs;
|
|
18584
18804
|
if (idleThisCycle) idleMs += cycleMs;
|
|
18585
18805
|
if (unreachableThisCycle) unreachableMs += cycleMs;
|
|
18806
|
+
if (openCodeFailureThisCycle) openCodeFailureMs += cycleMs;
|
|
18586
18807
|
if (state.idleTimeout !== null && consecutiveDrainFailures >= 2 && unreachableMs > state.idleTimeout * 1e3) {
|
|
18587
18808
|
logActivity(state, {
|
|
18588
18809
|
type: "info",
|
|
@@ -18592,6 +18813,15 @@ async function driveChannels(state, driver) {
|
|
|
18592
18813
|
if (state.interactive) displayStatus(state);
|
|
18593
18814
|
break;
|
|
18594
18815
|
}
|
|
18816
|
+
if (state.idleTimeout !== null && consecutiveOpenCodeFailures >= 2 && openCodeFailureMs > state.idleTimeout * 1e3) {
|
|
18817
|
+
logActivity(state, {
|
|
18818
|
+
type: "info",
|
|
18819
|
+
level: "warn",
|
|
18820
|
+
message: `Exiting: could not use the local OpenCode server on port ${state.port} for ${consecutiveOpenCodeFailures} consecutive polls (${Math.round(openCodeFailureMs / 1e3)}s)`
|
|
18821
|
+
});
|
|
18822
|
+
if (state.interactive) displayStatus(state);
|
|
18823
|
+
break;
|
|
18824
|
+
}
|
|
18595
18825
|
if (state.idleTimeout !== null && idlePolls >= 2 && idleMs > state.idleTimeout * 1e3 && !stayAwakeHolds()) {
|
|
18596
18826
|
logActivity(state, { type: "info", message: "Idle timeout reached" });
|
|
18597
18827
|
if (state.interactive) displayStatus(state);
|
|
@@ -20496,7 +20726,7 @@ async function run(options) {
|
|
|
20496
20726
|
port: state.port,
|
|
20497
20727
|
interactive: state.interactive,
|
|
20498
20728
|
agentId: state.agentId,
|
|
20499
|
-
log: (message) => log2(state, message),
|
|
20729
|
+
log: (message, level = "info") => log2(state, message, level),
|
|
20500
20730
|
startTimeoutMs: opencodeStartTimeoutMs,
|
|
20501
20731
|
inheritStdio: Boolean(options.opencodePidFile),
|
|
20502
20732
|
command: opencodeV2Command
|
|
@@ -20504,10 +20734,15 @@ async function run(options) {
|
|
|
20504
20734
|
port: state.port,
|
|
20505
20735
|
interactive: state.interactive,
|
|
20506
20736
|
agentId: state.agentId,
|
|
20507
|
-
log: (message) => log2(state, message),
|
|
20737
|
+
log: (message, level = "info") => log2(state, message, level),
|
|
20508
20738
|
startTimeoutMs: opencodeStartTimeoutMs,
|
|
20509
20739
|
inheritStdio: Boolean(options.opencodePidFile)
|
|
20510
20740
|
});
|
|
20741
|
+
if (oc.reclaimOutcome) {
|
|
20742
|
+
const outcome = oc.reclaimOutcome;
|
|
20743
|
+
const message = outcome.kind === "freed" ? `Reclaimed OpenCode port ${outcome.port}: stopped holder PID(s) ${outcome.pids.join(", ")} with ${outcome.usedSigkill ? "SIGTERM and SIGKILL" : "SIGTERM"} and started a fresh OpenCode.` : `Reclaimed OpenCode port ${outcome.port}: the port freed itself before a holder could be signalled, and a fresh OpenCode was started.`;
|
|
20744
|
+
logActivity(state, { type: "info", level: "warn", message });
|
|
20745
|
+
}
|
|
20511
20746
|
state.port = oc.port;
|
|
20512
20747
|
state.opencodeProcess = options.opencodePidFile ? null : oc.process;
|
|
20513
20748
|
state.opencodeVersion = oc.version;
|
|
@@ -20551,7 +20786,7 @@ async function run(options) {
|
|
|
20551
20786
|
password: state.opencodePassword
|
|
20552
20787
|
});
|
|
20553
20788
|
state.opencodeClient = openCodeClient;
|
|
20554
|
-
if (options.opencodePidFile && oc.process?.pid !== void 0) {
|
|
20789
|
+
if (options.opencodePidFile && oc.process?.pid !== void 0 && oc.process.exitCode === null && oc.process.signalCode === null) {
|
|
20555
20790
|
try {
|
|
20556
20791
|
writeFileSync9(options.opencodePidFile, `${oc.process.pid}
|
|
20557
20792
|
`, { mode: 384 });
|
|
@@ -20562,6 +20797,12 @@ async function run(options) {
|
|
|
20562
20797
|
error: `Failed to write OpenCode pid file ${options.opencodePidFile}: ${error2 instanceof Error ? error2.message : String(error2)}`
|
|
20563
20798
|
});
|
|
20564
20799
|
}
|
|
20800
|
+
} else if (options.opencodePidFile && oc.process?.pid !== void 0) {
|
|
20801
|
+
logActivity(state, {
|
|
20802
|
+
type: "info",
|
|
20803
|
+
level: "warn",
|
|
20804
|
+
message: `OpenCode pid file ${options.opencodePidFile} was left unchanged because the process spawned for this run had already exited.`
|
|
20805
|
+
});
|
|
20565
20806
|
}
|
|
20566
20807
|
if (state.opencodeVersion !== null) {
|
|
20567
20808
|
const provenance = checkSessionDbProvenance({
|
|
@@ -20608,7 +20849,7 @@ async function run(options) {
|
|
|
20608
20849
|
const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
|
|
20609
20850
|
ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
|
|
20610
20851
|
if (!state.interactive && oc.notReadyReason !== null) {
|
|
20611
|
-
const message = `OpenCode is not ready on port ${state.port}: ${oc.notReadyReason}. The runner will still come online, but messages will fail until opencode answers
|
|
20852
|
+
const message = `OpenCode is not ready on port ${state.port}: ${oc.notReadyReason}. The runner will still come online, but messages will fail until opencode answers.`;
|
|
20612
20853
|
logActivity(state, { type: "info", level: "warn", message });
|
|
20613
20854
|
} else {
|
|
20614
20855
|
const versionWarning = buildOpenCodeVersionWarning(
|
|
@@ -20854,7 +21095,7 @@ async function run(options) {
|
|
|
20854
21095
|
if (state.interactive) displayStatus(state);
|
|
20855
21096
|
}
|
|
20856
21097
|
}).catch((error2) => {
|
|
20857
|
-
const message =
|
|
21098
|
+
const message = describeErrorWithCause(error2);
|
|
20858
21099
|
logActivity(state, {
|
|
20859
21100
|
type: "error",
|
|
20860
21101
|
error: `Failed to drain queued messages on connect: ${message}`
|
|
@@ -20921,7 +21162,7 @@ async function run(options) {
|
|
|
20921
21162
|
if (state.interactive) displayStatus(state);
|
|
20922
21163
|
}
|
|
20923
21164
|
}).catch((error2) => {
|
|
20924
|
-
const message =
|
|
21165
|
+
const message = describeErrorWithCause(error2);
|
|
20925
21166
|
logActivity(state, {
|
|
20926
21167
|
type: "error",
|
|
20927
21168
|
error: `Failed to drain queued messages on ping: ${message}`
|