@omniralph/agent 2.3.3 → 2.3.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.
- package/dist/cli.js +487 -130
- package/dist/cli.js.map +1 -1
- package/dist/ui/assets/{index-DFSS3IQz.css → index-DRDub3v5.css} +1 -1
- package/dist/ui/assets/index-DizYqPj0.js +1451 -0
- package/dist/ui/index.html +2 -2
- package/package.json +3 -3
- package/dist/ui/assets/index-DUnFI74o.js +0 -1451
package/dist/cli.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// src/main.ts
|
|
4
4
|
import { stat as stat10 } from "node:fs/promises";
|
|
5
5
|
import { hostname, homedir as homedir3, userInfo } from "node:os";
|
|
6
|
-
import
|
|
6
|
+
import path26 from "node:path";
|
|
7
7
|
import { parseArgs } from "node:util";
|
|
8
8
|
|
|
9
9
|
// ../../packages/shared/src/finding.ts
|
|
@@ -153,12 +153,12 @@ var InstallableVersion = z5.string().max(64).refine(
|
|
|
153
153
|
(v) => SEMVER_PATTERN.test(v) || /^[a-z][a-z0-9._-]{0,63}$/i.test(v),
|
|
154
154
|
"Expected a semver version or a dist-tag"
|
|
155
155
|
);
|
|
156
|
-
function normalizePackagePath(
|
|
157
|
-
if (
|
|
156
|
+
function normalizePackagePath(path27) {
|
|
157
|
+
if (path27 === "" || path27.startsWith("/") || path27.includes("\\") || path27.includes("\0")) {
|
|
158
158
|
return null;
|
|
159
159
|
}
|
|
160
160
|
const segments = [];
|
|
161
|
-
for (const segment of
|
|
161
|
+
for (const segment of path27.split("/")) {
|
|
162
162
|
if (segment === "" || segment === ".") continue;
|
|
163
163
|
if (segment === "..") {
|
|
164
164
|
if (segments.length === 0) return null;
|
|
@@ -169,8 +169,8 @@ function normalizePackagePath(path26) {
|
|
|
169
169
|
}
|
|
170
170
|
return segments.length > 0 ? segments.join("/") : null;
|
|
171
171
|
}
|
|
172
|
-
var PackagePath = z5.string().transform((
|
|
173
|
-
const normalized = normalizePackagePath(
|
|
172
|
+
var PackagePath = z5.string().transform((path27, ctx) => {
|
|
173
|
+
const normalized = normalizePackagePath(path27);
|
|
174
174
|
if (normalized === null) {
|
|
175
175
|
ctx.addIssue({
|
|
176
176
|
code: "custom",
|
|
@@ -269,10 +269,10 @@ function resolveInstallablePaths(packageDir, manifest) {
|
|
|
269
269
|
throw new Error(`Package directory ${packageDir} is not an absolute path`);
|
|
270
270
|
}
|
|
271
271
|
const root = packageDir.replace(/\/+$/, "");
|
|
272
|
-
const resolve3 = (
|
|
273
|
-
const normalized = normalizePackagePath(
|
|
272
|
+
const resolve3 = (path27) => {
|
|
273
|
+
const normalized = normalizePackagePath(path27);
|
|
274
274
|
if (normalized === null) {
|
|
275
|
-
throw new Error(`Path ${
|
|
275
|
+
throw new Error(`Path ${path27} leaves the package directory ${packageDir}`);
|
|
276
276
|
}
|
|
277
277
|
return `${root}/${normalized}`;
|
|
278
278
|
};
|
|
@@ -1805,8 +1805,8 @@ function resolve(scopes, name) {
|
|
|
1805
1805
|
}
|
|
1806
1806
|
return { value: void 0, path: joinPath(top.path, name) };
|
|
1807
1807
|
}
|
|
1808
|
-
function joinPath(
|
|
1809
|
-
return
|
|
1808
|
+
function joinPath(path27, name) {
|
|
1809
|
+
return path27 ? `${path27}.${name}` : name;
|
|
1810
1810
|
}
|
|
1811
1811
|
function isFalsy(value) {
|
|
1812
1812
|
return value === void 0 || value === null || value === false || value === "" || Array.isArray(value) && value.length === 0;
|
|
@@ -1831,13 +1831,13 @@ function renderTemplate(template, context, catalog = VARIABLES, limits = {}) {
|
|
|
1831
1831
|
out += node.text;
|
|
1832
1832
|
continue;
|
|
1833
1833
|
}
|
|
1834
|
-
const { value, path:
|
|
1834
|
+
const { value, path: path27 } = resolve(scopes, node.name);
|
|
1835
1835
|
if (node.type === "variable") {
|
|
1836
|
-
const entry = entries.get(
|
|
1836
|
+
const entry = entries.get(path27);
|
|
1837
1837
|
const quoted = entry?.trust === "untrusted" ? entry.quote : void 0;
|
|
1838
1838
|
let text3 = stringify(value);
|
|
1839
1839
|
if (text3 === "" && quoted === void 0) continue;
|
|
1840
|
-
const limit = limits[
|
|
1840
|
+
const limit = limits[path27];
|
|
1841
1841
|
if (limit !== void 0 && text3.length > limit) {
|
|
1842
1842
|
text3 = `${text3.slice(0, limit)}${TRUNCATION_MARKER}`;
|
|
1843
1843
|
}
|
|
@@ -1852,7 +1852,7 @@ function renderTemplate(template, context, catalog = VARIABLES, limits = {}) {
|
|
|
1852
1852
|
}
|
|
1853
1853
|
if (isFalsy(value)) continue;
|
|
1854
1854
|
const items = Array.isArray(value) ? value : [value];
|
|
1855
|
-
for (const item of items) out += render(node.children, [...scopes, { value: item, path:
|
|
1855
|
+
for (const item of items) out += render(node.children, [...scopes, { value: item, path: path27 }]);
|
|
1856
1856
|
}
|
|
1857
1857
|
return out;
|
|
1858
1858
|
};
|
|
@@ -6676,7 +6676,7 @@ var TokenRefreshReport = z12.object({
|
|
|
6676
6676
|
/** The signed refresh token's `exp`; null for a legacy opaque token. */
|
|
6677
6677
|
refreshTokenExpiresAt: z12.iso.datetime().nullable()
|
|
6678
6678
|
});
|
|
6679
|
-
var HostToolPath = z12.string().max(4096).refine((
|
|
6679
|
+
var HostToolPath = z12.string().max(4096).refine((path27) => path27.startsWith("/"), "must be an absolute path");
|
|
6680
6680
|
var HOST_TOOL_SOURCES = ["configured", "discovered"];
|
|
6681
6681
|
var HOST_TOOL_PROBLEMS = ["missing", "not_executable", "blocked"];
|
|
6682
6682
|
var HostToolReport = z12.object({
|
|
@@ -7087,8 +7087,8 @@ var DocumentationUpdate = z14.object({
|
|
|
7087
7087
|
suggestion: z14.string().trim().min(1).max(MAX_DOCUMENTATION_SUGGESTION_LENGTH).optional()
|
|
7088
7088
|
});
|
|
7089
7089
|
var DocumentationUpdateList = z14.array(DocumentationUpdate).max(MAX_DOCUMENTATION_UPDATES3);
|
|
7090
|
-
function filePathKey(
|
|
7091
|
-
return
|
|
7090
|
+
function filePathKey(path27) {
|
|
7091
|
+
return path27.trim().replace(/^(\.\/|\/)+/, "");
|
|
7092
7092
|
}
|
|
7093
7093
|
function documentationUpdatesByFile(updates) {
|
|
7094
7094
|
const byPath = /* @__PURE__ */ new Map();
|
|
@@ -9052,7 +9052,7 @@ async function readResponse(res, method, url, schema) {
|
|
|
9052
9052
|
json = void 0;
|
|
9053
9053
|
}
|
|
9054
9054
|
if (res.status === 204) json = null;
|
|
9055
|
-
const
|
|
9055
|
+
const path27 = new URL(url).pathname;
|
|
9056
9056
|
if (!res.ok) {
|
|
9057
9057
|
const code5 = json && typeof json === "object" && "error" in json && typeof json.error === "string" ? json.error : void 0;
|
|
9058
9058
|
const detail = json && typeof json === "object" && "message" in json && typeof json.message === "string" ? json.message : void 0;
|
|
@@ -9060,7 +9060,7 @@ async function readResponse(res, method, url, schema) {
|
|
|
9060
9060
|
throw new ApiError(
|
|
9061
9061
|
res.status,
|
|
9062
9062
|
code5,
|
|
9063
|
-
`${method} ${
|
|
9063
|
+
`${method} ${path27} failed with ${res.status}${code5 ? ` (${code5})` : ""}${detail ? `: ${detail}` : ""}`,
|
|
9064
9064
|
Number.isFinite(retryAfter3) && retryAfter3 > 0 ? retryAfter3 : void 0
|
|
9065
9065
|
);
|
|
9066
9066
|
}
|
|
@@ -9069,7 +9069,7 @@ async function readResponse(res, method, url, schema) {
|
|
|
9069
9069
|
throw new ApiError(
|
|
9070
9070
|
res.status,
|
|
9071
9071
|
"invalid_response",
|
|
9072
|
-
`${method} ${
|
|
9072
|
+
`${method} ${path27} returned an invalid body`
|
|
9073
9073
|
);
|
|
9074
9074
|
}
|
|
9075
9075
|
return parsed.data;
|
|
@@ -9093,10 +9093,10 @@ var HttpControlPanelApi = class {
|
|
|
9093
9093
|
server;
|
|
9094
9094
|
accessToken;
|
|
9095
9095
|
fetchFn;
|
|
9096
|
-
async post(
|
|
9096
|
+
async post(path27, body, schema, options) {
|
|
9097
9097
|
return postJson(
|
|
9098
9098
|
this.fetchFn,
|
|
9099
|
-
`${this.server}${
|
|
9099
|
+
`${this.server}${path27}`,
|
|
9100
9100
|
body,
|
|
9101
9101
|
schema,
|
|
9102
9102
|
await this.accessToken(),
|
|
@@ -9561,13 +9561,13 @@ function buildFile(b) {
|
|
|
9561
9561
|
const isNew = b.isNew || b.minusPath === DEV_NULL;
|
|
9562
9562
|
const isDeleted = b.isDeleted || b.plusPath === DEV_NULL;
|
|
9563
9563
|
let oldPath = b.renameFrom ?? b.copyFrom ?? minus ?? b.headerOld ?? null;
|
|
9564
|
-
let
|
|
9564
|
+
let path27 = b.renameTo ?? b.copyTo ?? plus ?? b.headerNew ?? oldPath ?? "";
|
|
9565
9565
|
if (b.binary && !minus && !plus) {
|
|
9566
9566
|
const binaryLine = b.lines.find((l) => l.startsWith("Binary files "));
|
|
9567
9567
|
const m = binaryLine ? /^Binary files (.+) and (.+) differ$/.exec(binaryLine) : null;
|
|
9568
9568
|
if (m && m[1] && m[2]) {
|
|
9569
9569
|
if (m[1] !== DEV_NULL && !b.renameFrom) oldPath = stripPrefix(unquotePath(m[1]), "a/");
|
|
9570
|
-
if (m[2] !== DEV_NULL && !b.renameTo)
|
|
9570
|
+
if (m[2] !== DEV_NULL && !b.renameTo) path27 = stripPrefix(unquotePath(m[2]), "b/");
|
|
9571
9571
|
}
|
|
9572
9572
|
}
|
|
9573
9573
|
let status = "modified";
|
|
@@ -9576,9 +9576,9 @@ function buildFile(b) {
|
|
|
9576
9576
|
else if (b.renameFrom !== void 0) status = "renamed";
|
|
9577
9577
|
else if (b.copyFrom !== void 0) status = "copied";
|
|
9578
9578
|
if (status === "added") oldPath = null;
|
|
9579
|
-
if (status === "deleted")
|
|
9579
|
+
if (status === "deleted") path27 = oldPath ?? path27;
|
|
9580
9580
|
const file = {
|
|
9581
|
-
path:
|
|
9581
|
+
path: path27,
|
|
9582
9582
|
oldPath,
|
|
9583
9583
|
status,
|
|
9584
9584
|
binary: b.binary,
|
|
@@ -9592,8 +9592,8 @@ function buildFile(b) {
|
|
|
9592
9592
|
if (b.similarity !== void 0) file.similarity = b.similarity;
|
|
9593
9593
|
return file;
|
|
9594
9594
|
}
|
|
9595
|
-
function stripPrefix(
|
|
9596
|
-
return
|
|
9595
|
+
function stripPrefix(path27, prefix) {
|
|
9596
|
+
return path27.startsWith(prefix) ? path27.slice(prefix.length) : path27;
|
|
9597
9597
|
}
|
|
9598
9598
|
function parseMarkerPath(raw, prefix) {
|
|
9599
9599
|
let value = raw;
|
|
@@ -9734,18 +9734,18 @@ var LANGUAGE_BY_FILENAME = {
|
|
|
9734
9734
|
Rakefile: "ruby",
|
|
9735
9735
|
Gemfile: "ruby"
|
|
9736
9736
|
};
|
|
9737
|
-
function isCiWorkflowPath(
|
|
9738
|
-
return /^\.github\/workflows\/[^/]+\.ya?ml$/.test(
|
|
9737
|
+
function isCiWorkflowPath(path27) {
|
|
9738
|
+
return /^\.github\/workflows\/[^/]+\.ya?ml$/.test(path27) || /(^|\/)\.gitlab-ci\.ya?ml$/.test(path27);
|
|
9739
9739
|
}
|
|
9740
|
-
function languageForPath(
|
|
9741
|
-
if (isCiWorkflowPath(
|
|
9742
|
-
const name =
|
|
9740
|
+
function languageForPath(path27) {
|
|
9741
|
+
if (isCiWorkflowPath(path27)) return "ci";
|
|
9742
|
+
const name = path27.slice(path27.lastIndexOf("/") + 1);
|
|
9743
9743
|
return LANGUAGE_BY_FILENAME[name] ?? LANGUAGE_BY_EXTENSION[extname(name).toLowerCase()];
|
|
9744
9744
|
}
|
|
9745
9745
|
function languagesForPaths(paths) {
|
|
9746
9746
|
const present2 = /* @__PURE__ */ new Set();
|
|
9747
|
-
for (const
|
|
9748
|
-
const language = languageForPath(
|
|
9747
|
+
for (const path27 of paths) {
|
|
9748
|
+
const language = languageForPath(path27);
|
|
9749
9749
|
if (language) present2.add(language);
|
|
9750
9750
|
}
|
|
9751
9751
|
return RUBRIC_LANGUAGES.filter((language) => present2.has(language));
|
|
@@ -9822,8 +9822,8 @@ var FILE_CATEGORIES = [
|
|
|
9822
9822
|
}
|
|
9823
9823
|
];
|
|
9824
9824
|
var DEFAULT_RISK_WEIGHT = 2;
|
|
9825
|
-
function categorizeFile(
|
|
9826
|
-
const lower =
|
|
9825
|
+
function categorizeFile(path27) {
|
|
9826
|
+
const lower = path27.toLowerCase();
|
|
9827
9827
|
for (const rule of FILE_CATEGORIES) {
|
|
9828
9828
|
if (rule.patterns.some((p) => p.test(lower)))
|
|
9829
9829
|
return { category: rule.category, weight: rule.weight };
|
|
@@ -10150,8 +10150,8 @@ function selectFiles(files, profile, options) {
|
|
|
10150
10150
|
total2 -= candidate.tokens;
|
|
10151
10151
|
}
|
|
10152
10152
|
for (const candidate of byDropOrder) {
|
|
10153
|
-
const
|
|
10154
|
-
if (
|
|
10153
|
+
const reason2 = dropped.get(candidate);
|
|
10154
|
+
if (reason2) skipped.push({ path: candidate.file.path, reason: reason2 });
|
|
10155
10155
|
}
|
|
10156
10156
|
return {
|
|
10157
10157
|
included: candidates.filter((c) => !dropped.has(c)),
|
|
@@ -10197,16 +10197,16 @@ var TEST_FILENAME = [
|
|
|
10197
10197
|
/[a-z0-9](Test|Tests|Spec|IT)\.(java|kt|kts|cs|swift|scala|php)$/
|
|
10198
10198
|
// FooTest.java, FooTests.cs
|
|
10199
10199
|
];
|
|
10200
|
-
function isTestPath(
|
|
10201
|
-
return TEST_DIRECTORY.test(
|
|
10200
|
+
function isTestPath(path27) {
|
|
10201
|
+
return TEST_DIRECTORY.test(path27) || TEST_FILENAME.some((pattern) => pattern.test(path27));
|
|
10202
10202
|
}
|
|
10203
|
-
function checkCovers(type,
|
|
10204
|
-
if (languageForPath(
|
|
10205
|
-
return type === "performance" ? !isTestPath(
|
|
10203
|
+
function checkCovers(type, path27) {
|
|
10204
|
+
if (languageForPath(path27) === void 0) return false;
|
|
10205
|
+
return type === "performance" ? !isTestPath(path27) : true;
|
|
10206
10206
|
}
|
|
10207
|
-
function directoryOf(
|
|
10208
|
-
const slash =
|
|
10209
|
-
return slash === -1 ? "" :
|
|
10207
|
+
function directoryOf(path27) {
|
|
10208
|
+
const slash = path27.lastIndexOf("/");
|
|
10209
|
+
return slash === -1 ? "" : path27.slice(0, slash);
|
|
10210
10210
|
}
|
|
10211
10211
|
function groupFilesByDirectory(files, max = MAX_FILES_PER_CHECK_GROUP) {
|
|
10212
10212
|
const size = Math.max(1, Math.floor(max));
|
|
@@ -10815,16 +10815,16 @@ var UsageTracker = class {
|
|
|
10815
10815
|
function sum(usage) {
|
|
10816
10816
|
return usage.inputTokens + usage.outputTokens;
|
|
10817
10817
|
}
|
|
10818
|
-
function realpathOrResolve(
|
|
10818
|
+
function realpathOrResolve(path27) {
|
|
10819
10819
|
try {
|
|
10820
|
-
return realpathSync(
|
|
10820
|
+
return realpathSync(path27);
|
|
10821
10821
|
} catch {
|
|
10822
|
-
return resolve2(
|
|
10822
|
+
return resolve2(path27);
|
|
10823
10823
|
}
|
|
10824
10824
|
}
|
|
10825
|
-
function isInsideWorktree(worktree,
|
|
10825
|
+
function isInsideWorktree(worktree, path27) {
|
|
10826
10826
|
const root = realpathOrResolve(worktree);
|
|
10827
|
-
const target = realpathOrResolve(resolve2(worktree,
|
|
10827
|
+
const target = realpathOrResolve(resolve2(worktree, path27));
|
|
10828
10828
|
const rel = relative(root, target);
|
|
10829
10829
|
return rel === "" || !rel.startsWith("..") && !isAbsolute(rel);
|
|
10830
10830
|
}
|
|
@@ -11572,15 +11572,15 @@ function diff(cwd, range, env, paths = []) {
|
|
|
11572
11572
|
env
|
|
11573
11573
|
);
|
|
11574
11574
|
}
|
|
11575
|
-
async function showFile(cwd, commit,
|
|
11575
|
+
async function showFile(cwd, commit, path27, env) {
|
|
11576
11576
|
try {
|
|
11577
|
-
const type = (await git(cwd, ["cat-file", "-t", `${commit}:${
|
|
11577
|
+
const type = (await git(cwd, ["cat-file", "-t", `${commit}:${path27}`], env)).trim();
|
|
11578
11578
|
if (type !== "blob") return void 0;
|
|
11579
11579
|
} catch (error) {
|
|
11580
11580
|
if (error instanceof GitError) return void 0;
|
|
11581
11581
|
throw error;
|
|
11582
11582
|
}
|
|
11583
|
-
return git(cwd, ["cat-file", "blob", `${commit}:${
|
|
11583
|
+
return git(cwd, ["cat-file", "blob", `${commit}:${path27}`], env);
|
|
11584
11584
|
}
|
|
11585
11585
|
|
|
11586
11586
|
// ../../packages/engine/src/runReview.ts
|
|
@@ -11599,11 +11599,11 @@ function scopeRange(scope) {
|
|
|
11599
11599
|
var location = (f) => ({ path: f.path, startLine: f.startLine, endLine: f.endLine });
|
|
11600
11600
|
async function makeSelfContained(findings, diffFiles, input, logger) {
|
|
11601
11601
|
const files = /* @__PURE__ */ new Map();
|
|
11602
|
-
const headFile = (
|
|
11603
|
-
let file = files.get(
|
|
11602
|
+
const headFile = (path27) => {
|
|
11603
|
+
let file = files.get(path27);
|
|
11604
11604
|
if (!file) {
|
|
11605
|
-
file = showFile(input.cwd, input.headSha,
|
|
11606
|
-
files.set(
|
|
11605
|
+
file = showFile(input.cwd, input.headSha, path27, input.gitEnv);
|
|
11606
|
+
files.set(path27, file);
|
|
11607
11607
|
}
|
|
11608
11608
|
return file;
|
|
11609
11609
|
};
|
|
@@ -11784,19 +11784,19 @@ function extractFileReferences(answer, limit = 100) {
|
|
|
11784
11784
|
const seen = /* @__PURE__ */ new Set();
|
|
11785
11785
|
const references = [];
|
|
11786
11786
|
for (const match of answer.matchAll(pattern)) {
|
|
11787
|
-
const [,
|
|
11788
|
-
if (!
|
|
11787
|
+
const [, path27, start, end] = match;
|
|
11788
|
+
if (!path27 || path27.includes("..")) continue;
|
|
11789
11789
|
const startLine = start ? Number(start) : void 0;
|
|
11790
11790
|
let endLine = end ? Number(end) : void 0;
|
|
11791
11791
|
if (startLine !== void 0 && startLine < 1) continue;
|
|
11792
11792
|
if (startLine === void 0 || endLine !== void 0 && endLine < startLine) {
|
|
11793
11793
|
endLine = void 0;
|
|
11794
11794
|
}
|
|
11795
|
-
const key2 = `${
|
|
11795
|
+
const key2 = `${path27}:${startLine ?? ""}-${endLine ?? ""}`;
|
|
11796
11796
|
if (seen.has(key2)) continue;
|
|
11797
11797
|
seen.add(key2);
|
|
11798
11798
|
references.push({
|
|
11799
|
-
path:
|
|
11799
|
+
path: path27,
|
|
11800
11800
|
...startLine !== void 0 && { startLine },
|
|
11801
11801
|
...endLine !== void 0 && { endLine }
|
|
11802
11802
|
});
|
|
@@ -12210,12 +12210,12 @@ async function runQuery(query, request, seen) {
|
|
|
12210
12210
|
let allowed = false;
|
|
12211
12211
|
try {
|
|
12212
12212
|
const rule = options.matchedAskRule;
|
|
12213
|
-
const
|
|
12213
|
+
const reason2 = [
|
|
12214
12214
|
options.title ?? options.decisionReason,
|
|
12215
12215
|
rule && `It matches \`${rule.toolName}${rule.ruleContent ? `(${rule.ruleContent})` : ""}\` in the ${rule.source} settings.`
|
|
12216
12216
|
].filter(Boolean).join("\n");
|
|
12217
12217
|
allowed = await askPermission(
|
|
12218
|
-
{ tool: toolName, input: toolInput, ...
|
|
12218
|
+
{ tool: toolName, input: toolInput, ...reason2 && { reason: reason2 } },
|
|
12219
12219
|
options.signal
|
|
12220
12220
|
);
|
|
12221
12221
|
} catch {
|
|
@@ -13537,12 +13537,12 @@ function readStoryOutcome(structured) {
|
|
|
13537
13537
|
for (const f of parsed.data.followUps) {
|
|
13538
13538
|
const title = f.title.replace(/\s+/g, " ").trim();
|
|
13539
13539
|
if (!title || followUps.some((x) => x.title.toLowerCase() === title.toLowerCase())) continue;
|
|
13540
|
-
const
|
|
13540
|
+
const reason2 = f.reason?.trim();
|
|
13541
13541
|
followUps.push({
|
|
13542
13542
|
title,
|
|
13543
13543
|
description: f.description.trim(),
|
|
13544
13544
|
...f.manual && { manual: true },
|
|
13545
|
-
...f.after === "epic" && { after: "epic", ...
|
|
13545
|
+
...f.after === "epic" && { after: "epic", ...reason2 && { reason: reason2 } }
|
|
13546
13546
|
});
|
|
13547
13547
|
}
|
|
13548
13548
|
const recommendations = [];
|
|
@@ -13927,9 +13927,9 @@ function failureReason(outcome, timeoutMs) {
|
|
|
13927
13927
|
return `it exited with code ${outcome.exitCode}`;
|
|
13928
13928
|
}
|
|
13929
13929
|
function failureDetail(outcome, timeoutMs) {
|
|
13930
|
-
const
|
|
13931
|
-
if (
|
|
13932
|
-
return outcome.stderr ? `${
|
|
13930
|
+
const reason2 = failureReason(outcome, timeoutMs);
|
|
13931
|
+
if (reason2 === null) return null;
|
|
13932
|
+
return outcome.stderr ? `${reason2}: ${outcome.stderr}` : reason2;
|
|
13933
13933
|
}
|
|
13934
13934
|
async function runSetupCommands(commands, options) {
|
|
13935
13935
|
const setup = {};
|
|
@@ -13950,8 +13950,8 @@ async function runSetupCommands(commands, options) {
|
|
|
13950
13950
|
exitCode: outcome.exitCode,
|
|
13951
13951
|
durationMs: Date.now() - started
|
|
13952
13952
|
};
|
|
13953
|
-
const
|
|
13954
|
-
if (
|
|
13953
|
+
const reason2 = failureReason(outcome, timeoutMs);
|
|
13954
|
+
if (reason2 === null) {
|
|
13955
13955
|
options.logger.info(
|
|
13956
13956
|
{ ...log, truncated: outcome.truncated, stderr: outcome.stderr },
|
|
13957
13957
|
"setup command finished"
|
|
@@ -13964,10 +13964,10 @@ async function runSetupCommands(commands, options) {
|
|
|
13964
13964
|
{ ...log, stderr: outcome.stderr },
|
|
13965
13965
|
"setup command failed; failing the run"
|
|
13966
13966
|
);
|
|
13967
|
-
throw new SetupCommandError(command.name, failureDetail(outcome, timeoutMs) ??
|
|
13967
|
+
throw new SetupCommandError(command.name, failureDetail(outcome, timeoutMs) ?? reason2);
|
|
13968
13968
|
}
|
|
13969
13969
|
options.logger.info({ ...log, stderr: outcome.stderr }, "optional setup command failed");
|
|
13970
|
-
setup[command.name] = `(The setup command ${command.name} failed: ${
|
|
13970
|
+
setup[command.name] = `(The setup command ${command.name} failed: ${reason2}.)`;
|
|
13971
13971
|
}
|
|
13972
13972
|
return setup;
|
|
13973
13973
|
}
|
|
@@ -14593,10 +14593,14 @@ var ClaudeStatusReporter = class extends EventEmitter2 {
|
|
|
14593
14593
|
* Checks again at once ("Check again"): a read already running is shared,
|
|
14594
14594
|
* and one that started less than `checkMinIntervalMs` ago answers instead.
|
|
14595
14595
|
*/
|
|
14596
|
-
async check() {
|
|
14597
|
-
if (this.reading !== null)
|
|
14596
|
+
async check(options = {}) {
|
|
14597
|
+
if (this.reading !== null) {
|
|
14598
|
+
if (!options.force) return this.reading;
|
|
14599
|
+
await this.reading.catch(() => void 0);
|
|
14600
|
+
return this.start();
|
|
14601
|
+
}
|
|
14598
14602
|
const minInterval = this.options.checkMinIntervalMs ?? CLAUDE_STATUS_CHECK_MIN_INTERVAL_MS;
|
|
14599
|
-
if (this.last !== null && this.readAt !== null && this.now() - this.readAt < minInterval) {
|
|
14603
|
+
if (!options.force && this.last !== null && this.readAt !== null && this.now() - this.readAt < minInterval) {
|
|
14600
14604
|
return this.last;
|
|
14601
14605
|
}
|
|
14602
14606
|
return this.start();
|
|
@@ -14650,6 +14654,19 @@ var ClaudeStatusReporter = class extends EventEmitter2 {
|
|
|
14650
14654
|
models: reported,
|
|
14651
14655
|
checkedAt
|
|
14652
14656
|
};
|
|
14657
|
+
if (status.status === "signed_in" && status.source === "login") {
|
|
14658
|
+
const problem = await this.options.checkStoredLogin?.() ?? null;
|
|
14659
|
+
if (problem !== null) {
|
|
14660
|
+
status = {
|
|
14661
|
+
status: "signed_out",
|
|
14662
|
+
source: null,
|
|
14663
|
+
models: [],
|
|
14664
|
+
checkedAt,
|
|
14665
|
+
error: problem,
|
|
14666
|
+
expired: true
|
|
14667
|
+
};
|
|
14668
|
+
}
|
|
14669
|
+
}
|
|
14653
14670
|
} catch (error) {
|
|
14654
14671
|
const message = error instanceof Error ? error.message : String(error);
|
|
14655
14672
|
status = {
|
|
@@ -14722,8 +14739,8 @@ function controlPanelFetcher(options) {
|
|
|
14722
14739
|
const contentType = (res.headers.get("content-type") ?? "").split(";")[0]?.trim() ?? "";
|
|
14723
14740
|
if (!res.ok) {
|
|
14724
14741
|
const body = await res.json().catch(() => void 0);
|
|
14725
|
-
const
|
|
14726
|
-
return { ok: false, contentType, error:
|
|
14742
|
+
const reason2 = typeof body?.reason === "string" ? body.reason : `HTTP ${res.status}${typeof body?.error === "string" ? ` (${body.error})` : ""}`;
|
|
14743
|
+
return { ok: false, contentType, error: reason2 };
|
|
14727
14744
|
}
|
|
14728
14745
|
if (contentType === "text/html") {
|
|
14729
14746
|
await res.body?.cancel().catch(() => void 0);
|
|
@@ -15587,8 +15604,8 @@ async function runQueryTask(spec, ctx, options = {}) {
|
|
|
15587
15604
|
);
|
|
15588
15605
|
}
|
|
15589
15606
|
case "aborted": {
|
|
15590
|
-
const
|
|
15591
|
-
throw
|
|
15607
|
+
const reason2 = ctx.signal.reason;
|
|
15608
|
+
throw reason2 instanceof Error ? reason2 : new Error("the run was aborted");
|
|
15592
15609
|
}
|
|
15593
15610
|
case "error":
|
|
15594
15611
|
case "idle":
|
|
@@ -15685,8 +15702,8 @@ async function downloadRunAttachments(options) {
|
|
|
15685
15702
|
const files = [];
|
|
15686
15703
|
try {
|
|
15687
15704
|
for (const attachment of attachments) {
|
|
15688
|
-
const fail = (
|
|
15689
|
-
`Couldn't download the attachment ${labelOf(attachment)}: ${
|
|
15705
|
+
const fail = (reason2) => new RunAttachmentDownloadError(
|
|
15706
|
+
`Couldn't download the attachment ${labelOf(attachment)}: ${reason2}`
|
|
15690
15707
|
);
|
|
15691
15708
|
if (Date.parse(attachment.expiresAt) <= now().getTime()) {
|
|
15692
15709
|
throw fail("its download link expired before the run started");
|
|
@@ -15797,7 +15814,7 @@ async function runRequestAttempt(args) {
|
|
|
15797
15814
|
});
|
|
15798
15815
|
const baseRunner = options.runSession ?? createSessionRunner();
|
|
15799
15816
|
const runSession = ctx.sessions?.runner(baseRunner) ?? baseRunner;
|
|
15800
|
-
const label = (
|
|
15817
|
+
const label = (reason2) => `${requestStashPrefix(payload)}${reason2}`;
|
|
15801
15818
|
let files;
|
|
15802
15819
|
try {
|
|
15803
15820
|
files = await downloadRunAttachments({
|
|
@@ -16280,7 +16297,7 @@ async function runStoryAttempt(args) {
|
|
|
16280
16297
|
const plainEnv = withoutJobEnv(ctx.env());
|
|
16281
16298
|
const git2 = await jobGitAccess(ctx, { refreshMs: options.tokenRefreshMs });
|
|
16282
16299
|
const prefix = stashPrefix(payload);
|
|
16283
|
-
const label = (
|
|
16300
|
+
const label = (reason2) => `${prefix}${reason2} (attempt ${payload.attempt})`;
|
|
16284
16301
|
let files;
|
|
16285
16302
|
try {
|
|
16286
16303
|
files = await downloadRunAttachments({
|
|
@@ -16387,8 +16404,8 @@ async function runStoryAttempt(args) {
|
|
|
16387
16404
|
usage: session.usage,
|
|
16388
16405
|
durationMs: Date.now() - started
|
|
16389
16406
|
});
|
|
16390
|
-
const notDone = async (status, feedback,
|
|
16391
|
-
await stashQuietly2(worktree, label(
|
|
16407
|
+
const notDone = async (status, feedback, reason2) => {
|
|
16408
|
+
await stashQuietly2(worktree, label(reason2), log);
|
|
16392
16409
|
return result(status, { feedback });
|
|
16393
16410
|
};
|
|
16394
16411
|
if (!outcome) {
|
|
@@ -19126,9 +19143,9 @@ var AgentDaemon = class {
|
|
|
19126
19143
|
if ([...this.pendingAcks.values()].some((ack) => ack.jobId === job.id)) {
|
|
19127
19144
|
await this.heartbeat();
|
|
19128
19145
|
}
|
|
19129
|
-
const
|
|
19146
|
+
const reason2 = running.controller.signal.reason;
|
|
19130
19147
|
const history = this.options.history;
|
|
19131
|
-
if (failure3 !== void 0 &&
|
|
19148
|
+
if (failure3 !== void 0 && reason2 instanceof ShutdownAbort) {
|
|
19132
19149
|
jobLog.info("job interrupted by shutdown");
|
|
19133
19150
|
history?.finish(job.id, "canceled", "The agent stopped before the attempt finished");
|
|
19134
19151
|
return;
|
|
@@ -19143,8 +19160,8 @@ var AgentDaemon = class {
|
|
|
19143
19160
|
);
|
|
19144
19161
|
return;
|
|
19145
19162
|
}
|
|
19146
|
-
const body = failureBody(failure3,
|
|
19147
|
-
if (
|
|
19163
|
+
const body = failureBody(failure3, reason2);
|
|
19164
|
+
if (reason2 instanceof JobCanceled) history?.finish(job.id, "canceled");
|
|
19148
19165
|
else history?.finish(job.id, "failed", body.error);
|
|
19149
19166
|
jobLog.warn({ err: failure3, retryable: body.retryable }, "job failed");
|
|
19150
19167
|
await this.report(job.id, jobLog, () => this.options.api.submitFailure(job.id, body));
|
|
@@ -20616,17 +20633,301 @@ function createLogger(options = {}) {
|
|
|
20616
20633
|
return { logger, ring, file, close: () => file?.close() };
|
|
20617
20634
|
}
|
|
20618
20635
|
|
|
20636
|
+
// src/claude-credentials.ts
|
|
20637
|
+
import { createHash as createHash4, randomBytes as randomBytes2 } from "node:crypto";
|
|
20638
|
+
import { readFile as readFile12 } from "node:fs/promises";
|
|
20639
|
+
import os from "node:os";
|
|
20640
|
+
import path20 from "node:path";
|
|
20641
|
+
var CLAUDE_OAUTH_CLIENT_ID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e";
|
|
20642
|
+
var CLAUDE_OAUTH_AUTHORIZE_URL = "https://claude.com/cai/oauth/authorize";
|
|
20643
|
+
var CLAUDE_OAUTH_TOKEN_URL = "https://platform.claude.com/v1/oauth/token";
|
|
20644
|
+
var CLAUDE_OAUTH_REDIRECT_URI = "https://platform.claude.com/oauth/code/callback";
|
|
20645
|
+
var CLAUDE_OAUTH_PROFILE_URL = "https://api.anthropic.com/api/oauth/profile";
|
|
20646
|
+
var CLAUDE_OAUTH_SCOPES = [
|
|
20647
|
+
"user:profile",
|
|
20648
|
+
"user:inference",
|
|
20649
|
+
"user:sessions:claude_code",
|
|
20650
|
+
"user:mcp_servers",
|
|
20651
|
+
"user:file_upload",
|
|
20652
|
+
"user:plugins"
|
|
20653
|
+
];
|
|
20654
|
+
var CLAUDE_TOKEN_EXCHANGE_TIMEOUT_MS = 12e4;
|
|
20655
|
+
var CLAUDE_SIGN_IN_TTL_MS = 15 * 6e4;
|
|
20656
|
+
function claudeLoginPaths(env = process.env, homeDir = os.homedir()) {
|
|
20657
|
+
const configDir = env.CLAUDE_CONFIG_DIR;
|
|
20658
|
+
return configDir ? {
|
|
20659
|
+
credentials: path20.join(configDir, ".credentials.json"),
|
|
20660
|
+
globalConfig: path20.join(configDir, ".claude.json")
|
|
20661
|
+
} : {
|
|
20662
|
+
credentials: path20.join(homeDir, ".claude", ".credentials.json"),
|
|
20663
|
+
globalConfig: path20.join(homeDir, ".claude.json")
|
|
20664
|
+
};
|
|
20665
|
+
}
|
|
20666
|
+
var nodeClaudeCredentialsFs = {
|
|
20667
|
+
readFile: (file) => readFile12(file, "utf8"),
|
|
20668
|
+
writePrivateJson
|
|
20669
|
+
};
|
|
20670
|
+
async function readJsonObject(fs, file) {
|
|
20671
|
+
let text3;
|
|
20672
|
+
try {
|
|
20673
|
+
text3 = await fs.readFile(file);
|
|
20674
|
+
} catch (error) {
|
|
20675
|
+
if (error.code === "ENOENT") return null;
|
|
20676
|
+
throw error;
|
|
20677
|
+
}
|
|
20678
|
+
const value = JSON.parse(text3);
|
|
20679
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
20680
|
+
throw new Error("not a JSON object");
|
|
20681
|
+
}
|
|
20682
|
+
return value;
|
|
20683
|
+
}
|
|
20684
|
+
function reason(error) {
|
|
20685
|
+
return error instanceof Error ? error.message : String(error);
|
|
20686
|
+
}
|
|
20687
|
+
async function readStoredClaudeOauth(paths, fs = nodeClaudeCredentialsFs) {
|
|
20688
|
+
const file = await readJsonObject(fs, paths.credentials);
|
|
20689
|
+
const oauth = file?.claudeAiOauth;
|
|
20690
|
+
return oauth !== null && typeof oauth === "object" ? oauth : null;
|
|
20691
|
+
}
|
|
20692
|
+
function storedLoginProblem(oauth, now) {
|
|
20693
|
+
const hasAccess = typeof oauth.accessToken === "string" && oauth.accessToken.length > 0;
|
|
20694
|
+
const hasRefresh = typeof oauth.refreshToken === "string" && oauth.refreshToken.length > 0;
|
|
20695
|
+
if (!hasAccess && !hasRefresh) {
|
|
20696
|
+
return "Claude's sign-in expired and couldn't be refreshed";
|
|
20697
|
+
}
|
|
20698
|
+
if (hasRefresh && typeof oauth.refreshTokenExpiresAt === "number" && oauth.refreshTokenExpiresAt <= now) {
|
|
20699
|
+
return "Claude's sign-in expired";
|
|
20700
|
+
}
|
|
20701
|
+
if (!hasRefresh && (typeof oauth.expiresAt !== "number" || oauth.expiresAt <= now)) {
|
|
20702
|
+
return "Claude's sign-in expired";
|
|
20703
|
+
}
|
|
20704
|
+
return null;
|
|
20705
|
+
}
|
|
20706
|
+
function storedLoginChecker(options) {
|
|
20707
|
+
return async () => {
|
|
20708
|
+
let oauth;
|
|
20709
|
+
try {
|
|
20710
|
+
oauth = await readStoredClaudeOauth(options.paths, options.fs);
|
|
20711
|
+
} catch {
|
|
20712
|
+
return null;
|
|
20713
|
+
}
|
|
20714
|
+
if (oauth === null) return null;
|
|
20715
|
+
return storedLoginProblem(oauth, (options.now ?? Date.now)());
|
|
20716
|
+
};
|
|
20717
|
+
}
|
|
20718
|
+
var ClaudeSignInError = class extends Error {
|
|
20719
|
+
constructor(status, message) {
|
|
20720
|
+
super(message);
|
|
20721
|
+
this.status = status;
|
|
20722
|
+
}
|
|
20723
|
+
status;
|
|
20724
|
+
};
|
|
20725
|
+
function base64url(bytes) {
|
|
20726
|
+
return bytes.toString("base64url");
|
|
20727
|
+
}
|
|
20728
|
+
var SUBSCRIPTION_TYPES = {
|
|
20729
|
+
claude_max: "max",
|
|
20730
|
+
claude_pro: "pro",
|
|
20731
|
+
claude_enterprise: "enterprise",
|
|
20732
|
+
claude_team: "team"
|
|
20733
|
+
};
|
|
20734
|
+
var ClaudeSignIn = class {
|
|
20735
|
+
constructor(options) {
|
|
20736
|
+
this.options = options;
|
|
20737
|
+
}
|
|
20738
|
+
options;
|
|
20739
|
+
pending = null;
|
|
20740
|
+
/** Why "Refresh sign-in" can't help here; null when it can. */
|
|
20741
|
+
unavailable() {
|
|
20742
|
+
const platform = this.options.platform ?? process.platform;
|
|
20743
|
+
if (platform === "darwin") {
|
|
20744
|
+
return "On macOS Claude keeps its login in the keychain: sign in with `claude` instead";
|
|
20745
|
+
}
|
|
20746
|
+
const env = this.options.env ?? process.env;
|
|
20747
|
+
if (env.ANTHROPIC_API_KEY) {
|
|
20748
|
+
return "ANTHROPIC_API_KEY is set, and it wins over a sign-in: remove it from the agent's environment first";
|
|
20749
|
+
}
|
|
20750
|
+
if (env.CLAUDE_CODE_OAUTH_TOKEN) {
|
|
20751
|
+
return "CLAUDE_CODE_OAUTH_TOKEN is set, and it wins over a sign-in: remove it from the agent's environment first";
|
|
20752
|
+
}
|
|
20753
|
+
return null;
|
|
20754
|
+
}
|
|
20755
|
+
now() {
|
|
20756
|
+
return (this.options.now ?? Date.now)();
|
|
20757
|
+
}
|
|
20758
|
+
start() {
|
|
20759
|
+
const problem = this.unavailable();
|
|
20760
|
+
if (problem !== null) throw new ClaudeSignInError(409, problem);
|
|
20761
|
+
const verifier = base64url(randomBytes2(32));
|
|
20762
|
+
const challenge = base64url(createHash4("sha256").update(verifier).digest());
|
|
20763
|
+
const state = base64url(randomBytes2(32));
|
|
20764
|
+
const expiresAt = this.now() + CLAUDE_SIGN_IN_TTL_MS;
|
|
20765
|
+
this.pending = { verifier, state, expiresAt };
|
|
20766
|
+
const url = new URL(CLAUDE_OAUTH_AUTHORIZE_URL);
|
|
20767
|
+
url.searchParams.set("code", "true");
|
|
20768
|
+
url.searchParams.set("client_id", CLAUDE_OAUTH_CLIENT_ID);
|
|
20769
|
+
url.searchParams.set("response_type", "code");
|
|
20770
|
+
url.searchParams.set("redirect_uri", CLAUDE_OAUTH_REDIRECT_URI);
|
|
20771
|
+
url.searchParams.set("scope", CLAUDE_OAUTH_SCOPES.join(" "));
|
|
20772
|
+
url.searchParams.set("code_challenge", challenge);
|
|
20773
|
+
url.searchParams.set("code_challenge_method", "S256");
|
|
20774
|
+
url.searchParams.set("state", state);
|
|
20775
|
+
return { authorizeUrl: url.toString(), expiresAt: new Date(expiresAt).toISOString() };
|
|
20776
|
+
}
|
|
20777
|
+
async complete(input) {
|
|
20778
|
+
const pending = this.pending;
|
|
20779
|
+
if (pending === null || pending.expiresAt <= this.now()) {
|
|
20780
|
+
this.pending = null;
|
|
20781
|
+
throw new ClaudeSignInError(409, "This sign-in expired: start again");
|
|
20782
|
+
}
|
|
20783
|
+
const [code5, state] = input.trim().split("#");
|
|
20784
|
+
if (!code5) throw new ClaudeSignInError(400, "Paste the code the sign-in page shows");
|
|
20785
|
+
if (state !== void 0 && state !== pending.state) {
|
|
20786
|
+
throw new ClaudeSignInError(400, "That code is from another sign-in: start again");
|
|
20787
|
+
}
|
|
20788
|
+
const fs = this.options.fs ?? nodeClaudeCredentialsFs;
|
|
20789
|
+
const { credentials, globalConfig } = this.options.paths;
|
|
20790
|
+
let stored;
|
|
20791
|
+
try {
|
|
20792
|
+
stored = await readJsonObject(fs, credentials) ?? {};
|
|
20793
|
+
} catch (error) {
|
|
20794
|
+
throw new ClaudeSignInError(
|
|
20795
|
+
409,
|
|
20796
|
+
`Can't read ${credentials} (${reason(error)}): fix or move it, then save again`
|
|
20797
|
+
);
|
|
20798
|
+
}
|
|
20799
|
+
const tokens2 = await this.exchange(code5, pending);
|
|
20800
|
+
this.pending = null;
|
|
20801
|
+
const profile = await this.profile(tokens2.access_token);
|
|
20802
|
+
const orgType = profile?.organization?.organization_type;
|
|
20803
|
+
const subscriptionType = orgType ? SUBSCRIPTION_TYPES[orgType] ?? null : null;
|
|
20804
|
+
const rateLimitTier = profile?.organization?.rate_limit_tier ?? null;
|
|
20805
|
+
const now = this.now();
|
|
20806
|
+
stored = await readJsonObject(fs, credentials).catch(() => stored) ?? {};
|
|
20807
|
+
await fs.writePrivateJson(credentials, {
|
|
20808
|
+
...stored,
|
|
20809
|
+
claudeAiOauth: {
|
|
20810
|
+
accessToken: tokens2.access_token,
|
|
20811
|
+
refreshToken: tokens2.refresh_token ?? "",
|
|
20812
|
+
expiresAt: now + tokens2.expires_in * 1e3,
|
|
20813
|
+
...tokens2.refresh_token_expires_in !== void 0 && {
|
|
20814
|
+
refreshTokenExpiresAt: now + tokens2.refresh_token_expires_in * 1e3
|
|
20815
|
+
},
|
|
20816
|
+
scopes: (tokens2.scope ?? CLAUDE_OAUTH_SCOPES.join(" ")).split(" ").filter(Boolean),
|
|
20817
|
+
subscriptionType,
|
|
20818
|
+
rateLimitTier,
|
|
20819
|
+
clientId: CLAUDE_OAUTH_CLIENT_ID
|
|
20820
|
+
}
|
|
20821
|
+
});
|
|
20822
|
+
const email = tokens2.account?.email_address ?? profile?.account?.email ?? null;
|
|
20823
|
+
const accountUuid = tokens2.account?.uuid ?? profile?.account?.uuid;
|
|
20824
|
+
const organizationUuid = tokens2.organization?.uuid ?? profile?.organization?.uuid;
|
|
20825
|
+
let config;
|
|
20826
|
+
try {
|
|
20827
|
+
config = await readJsonObject(fs, globalConfig);
|
|
20828
|
+
} catch (error) {
|
|
20829
|
+
return {
|
|
20830
|
+
email,
|
|
20831
|
+
subscriptionType,
|
|
20832
|
+
accountNotUpdated: `Can't read ${globalConfig} (${reason(error)})`
|
|
20833
|
+
};
|
|
20834
|
+
}
|
|
20835
|
+
if (config !== null || accountUuid !== void 0) {
|
|
20836
|
+
const previous = config?.oauthAccount !== null && typeof config?.oauthAccount === "object" ? config.oauthAccount : {};
|
|
20837
|
+
const account = {
|
|
20838
|
+
...config,
|
|
20839
|
+
hasCompletedOnboarding: true,
|
|
20840
|
+
oauthAccount: {
|
|
20841
|
+
...previous,
|
|
20842
|
+
...accountUuid !== void 0 && { accountUuid },
|
|
20843
|
+
...email !== null && { emailAddress: email },
|
|
20844
|
+
...organizationUuid !== void 0 && { organizationUuid },
|
|
20845
|
+
...tokens2.organization?.name !== void 0 && {
|
|
20846
|
+
organizationName: tokens2.organization.name
|
|
20847
|
+
}
|
|
20848
|
+
}
|
|
20849
|
+
};
|
|
20850
|
+
try {
|
|
20851
|
+
await fs.writePrivateJson(globalConfig, account);
|
|
20852
|
+
} catch (error) {
|
|
20853
|
+
return {
|
|
20854
|
+
email,
|
|
20855
|
+
subscriptionType,
|
|
20856
|
+
accountNotUpdated: `Can't write ${globalConfig} (${reason(error)})`
|
|
20857
|
+
};
|
|
20858
|
+
}
|
|
20859
|
+
}
|
|
20860
|
+
return { email, subscriptionType };
|
|
20861
|
+
}
|
|
20862
|
+
async exchange(code5, pending) {
|
|
20863
|
+
const fetchFn = this.options.fetch ?? fetch;
|
|
20864
|
+
let response;
|
|
20865
|
+
try {
|
|
20866
|
+
response = await fetchFn(CLAUDE_OAUTH_TOKEN_URL, {
|
|
20867
|
+
method: "POST",
|
|
20868
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
20869
|
+
body: new URLSearchParams({
|
|
20870
|
+
grant_type: "authorization_code",
|
|
20871
|
+
code: code5,
|
|
20872
|
+
redirect_uri: CLAUDE_OAUTH_REDIRECT_URI,
|
|
20873
|
+
client_id: CLAUDE_OAUTH_CLIENT_ID,
|
|
20874
|
+
code_verifier: pending.verifier,
|
|
20875
|
+
state: pending.state
|
|
20876
|
+
}).toString(),
|
|
20877
|
+
signal: AbortSignal.timeout(
|
|
20878
|
+
this.options.exchangeTimeoutMs ?? CLAUDE_TOKEN_EXCHANGE_TIMEOUT_MS
|
|
20879
|
+
)
|
|
20880
|
+
});
|
|
20881
|
+
} catch (error) {
|
|
20882
|
+
const timedOut = error instanceof Error && error.name === "TimeoutError";
|
|
20883
|
+
throw new ClaudeSignInError(
|
|
20884
|
+
502,
|
|
20885
|
+
timedOut ? "Anthropic didn't answer the sign-in in 2 minutes: try again" : `Can't reach Anthropic: ${error instanceof Error ? error.message : String(error)}`
|
|
20886
|
+
);
|
|
20887
|
+
}
|
|
20888
|
+
if (!response.ok) {
|
|
20889
|
+
const text3 = (await response.text().catch(() => "")).slice(0, 300);
|
|
20890
|
+
if (response.status === 400 || response.status === 401) {
|
|
20891
|
+
this.pending = null;
|
|
20892
|
+
throw new ClaudeSignInError(
|
|
20893
|
+
400,
|
|
20894
|
+
`Anthropic didn't accept that code (${response.status}): start again${text3 ? ` (${text3})` : ""}`
|
|
20895
|
+
);
|
|
20896
|
+
}
|
|
20897
|
+
throw new ClaudeSignInError(502, `Anthropic's sign-in failed (${response.status})`);
|
|
20898
|
+
}
|
|
20899
|
+
const body = await response.json();
|
|
20900
|
+
if (typeof body.access_token !== "string" || typeof body.expires_in !== "number") {
|
|
20901
|
+
throw new ClaudeSignInError(502, "Anthropic's sign-in answered without a token");
|
|
20902
|
+
}
|
|
20903
|
+
return body;
|
|
20904
|
+
}
|
|
20905
|
+
/** The plan and account; null when the profile can't be read (the tokens still work). */
|
|
20906
|
+
async profile(accessToken) {
|
|
20907
|
+
const fetchFn = this.options.fetch ?? fetch;
|
|
20908
|
+
try {
|
|
20909
|
+
const response = await fetchFn(CLAUDE_OAUTH_PROFILE_URL, {
|
|
20910
|
+
headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": "application/json" },
|
|
20911
|
+
signal: AbortSignal.timeout(1e4)
|
|
20912
|
+
});
|
|
20913
|
+
return response.ok ? await response.json() : null;
|
|
20914
|
+
} catch {
|
|
20915
|
+
return null;
|
|
20916
|
+
}
|
|
20917
|
+
}
|
|
20918
|
+
};
|
|
20919
|
+
|
|
20619
20920
|
// src/packages/history.ts
|
|
20620
20921
|
import { randomUUID as randomUUID6 } from "node:crypto";
|
|
20621
20922
|
import { EventEmitter as EventEmitter5 } from "node:events";
|
|
20622
|
-
import { readFile as
|
|
20623
|
-
import
|
|
20923
|
+
import { readFile as readFile13 } from "node:fs/promises";
|
|
20924
|
+
import path21 from "node:path";
|
|
20624
20925
|
import { z as z41 } from "zod";
|
|
20625
20926
|
var INSTALL_HISTORY_SIZE = 50;
|
|
20626
20927
|
var INSTALL_OUTPUT_MAX_BYTES = 64 * 1024;
|
|
20627
20928
|
var MAX_ERROR2 = 4e3;
|
|
20628
20929
|
function installsPath(dataDir2) {
|
|
20629
|
-
return
|
|
20930
|
+
return path21.join(dataDir2, "state", "installs.json");
|
|
20630
20931
|
}
|
|
20631
20932
|
var INSTALL_ACTIONS = ["install", "remove"];
|
|
20632
20933
|
var INSTALL_SOURCES = ["local", "fleet"];
|
|
@@ -20694,7 +20995,7 @@ var InstallHistory = class extends EventEmitter5 {
|
|
|
20694
20995
|
if (!file) return;
|
|
20695
20996
|
let json;
|
|
20696
20997
|
try {
|
|
20697
|
-
json = JSON.parse(await
|
|
20998
|
+
json = JSON.parse(await readFile13(file, "utf8"));
|
|
20698
20999
|
} catch (error) {
|
|
20699
21000
|
if (error.code !== "ENOENT") {
|
|
20700
21001
|
logger.warn({ err: error, file }, "couldn't read the install history; starting a new one");
|
|
@@ -20962,7 +21263,7 @@ var PackageManager = class {
|
|
|
20962
21263
|
// src/storage.ts
|
|
20963
21264
|
import { EventEmitter as EventEmitter6 } from "node:events";
|
|
20964
21265
|
import { lstat as lstat2, readdir as readdir4, stat as stat7, statfs } from "node:fs/promises";
|
|
20965
|
-
import
|
|
21266
|
+
import path22 from "node:path";
|
|
20966
21267
|
var STORAGE_CACHE_MS = 10 * 6e4;
|
|
20967
21268
|
var WALK_YIELD_EVERY = 1e3;
|
|
20968
21269
|
var MAX_WALK_DEPTH = 64;
|
|
@@ -20978,15 +21279,15 @@ var STORAGE_ITEMS = [
|
|
|
20978
21279
|
];
|
|
20979
21280
|
var defaultYield = () => new Promise((resolve3) => setImmediate(resolve3));
|
|
20980
21281
|
function isWithin(child, parent) {
|
|
20981
|
-
const relative2 =
|
|
20982
|
-
return relative2 === "" || !relative2.startsWith("..") && !
|
|
21282
|
+
const relative2 = path22.relative(parent, child);
|
|
21283
|
+
return relative2 === "" || !relative2.startsWith("..") && !path22.isAbsolute(relative2);
|
|
20983
21284
|
}
|
|
20984
21285
|
var AgentStorage = class extends EventEmitter6 {
|
|
20985
21286
|
constructor(options) {
|
|
20986
21287
|
super();
|
|
20987
21288
|
this.options = options;
|
|
20988
|
-
this.workspaceRoot =
|
|
20989
|
-
this.dataDir =
|
|
21289
|
+
this.workspaceRoot = path22.resolve(options.workspaceRoot);
|
|
21290
|
+
this.dataDir = path22.resolve(options.dataDir);
|
|
20990
21291
|
this.cacheMs = options.cacheMs ?? STORAGE_CACHE_MS;
|
|
20991
21292
|
this.now = options.now ?? (() => /* @__PURE__ */ new Date());
|
|
20992
21293
|
this.yieldToLoop = options.yieldToLoop ?? defaultYield;
|
|
@@ -21013,10 +21314,10 @@ var AgentStorage = class extends EventEmitter6 {
|
|
|
21013
21314
|
itemPaths() {
|
|
21014
21315
|
return {
|
|
21015
21316
|
repo_cache: [this.workspaceRoot],
|
|
21016
|
-
worktrees: [
|
|
21017
|
-
branch_worktrees: [
|
|
21018
|
-
packages: [
|
|
21019
|
-
caches: [
|
|
21317
|
+
worktrees: [path22.join(this.workspaceRoot, WORKTREES_DIR)],
|
|
21318
|
+
branch_worktrees: [path22.join(this.workspaceRoot, BRANCHES_DIR)],
|
|
21319
|
+
packages: [path22.join(this.dataDir, INSTALLABLES_DIR)],
|
|
21320
|
+
caches: [path22.join(this.dataDir, "cache"), path22.join(this.dataDir, "pr-cache")],
|
|
21020
21321
|
logs: [logDirectory(this.dataDir)]
|
|
21021
21322
|
};
|
|
21022
21323
|
}
|
|
@@ -21049,7 +21350,7 @@ var AgentStorage = class extends EventEmitter6 {
|
|
|
21049
21350
|
const syncs = this.options.history?.recent().filter((a) => a.type === REPO_SYNC_JOB_TYPE) ?? [];
|
|
21050
21351
|
const result = [];
|
|
21051
21352
|
for (const repo of repos) {
|
|
21052
|
-
const fetchHead = await lstat2(
|
|
21353
|
+
const fetchHead = await lstat2(path22.join(repo.dir, "FETCH_HEAD")).catch(() => null);
|
|
21053
21354
|
const sync = syncs.find(
|
|
21054
21355
|
(a) => a.repo?.provider === repo.provider && a.repo.fullPath === repo.fullPath
|
|
21055
21356
|
);
|
|
@@ -21139,7 +21440,7 @@ var AgentStorage = class extends EventEmitter6 {
|
|
|
21139
21440
|
});
|
|
21140
21441
|
for (const entry of entries) {
|
|
21141
21442
|
if (++visited % WALK_YIELD_EVERY === 0) await this.yieldToLoop();
|
|
21142
|
-
const full =
|
|
21443
|
+
const full = path22.join(dir, entry.name);
|
|
21143
21444
|
if (exclude.some((p) => isWithin(full, p))) continue;
|
|
21144
21445
|
if (entry.isDirectory()) {
|
|
21145
21446
|
if (depth < MAX_WALK_DEPTH) stack.push({ dir: full, depth: depth + 1 });
|
|
@@ -21164,7 +21465,7 @@ var AgentStorage = class extends EventEmitter6 {
|
|
|
21164
21465
|
id = String(await device(existing));
|
|
21165
21466
|
break;
|
|
21166
21467
|
} catch {
|
|
21167
|
-
const parent =
|
|
21468
|
+
const parent = path22.dirname(existing);
|
|
21168
21469
|
if (parent === existing) break;
|
|
21169
21470
|
existing = parent;
|
|
21170
21471
|
}
|
|
@@ -21320,18 +21621,18 @@ function heartbeatPublisher(hub, daemon) {
|
|
|
21320
21621
|
|
|
21321
21622
|
// src/ui/link.ts
|
|
21322
21623
|
import { spawn as spawn4 } from "node:child_process";
|
|
21323
|
-
import { readFile as
|
|
21324
|
-
import
|
|
21624
|
+
import { readFile as readFile14, rm as rm10 } from "node:fs/promises";
|
|
21625
|
+
import path24 from "node:path";
|
|
21325
21626
|
import { z as z42 } from "zod";
|
|
21326
21627
|
|
|
21327
21628
|
// src/ui/server.ts
|
|
21328
|
-
import { createHash as
|
|
21629
|
+
import { createHash as createHash5, randomBytes as randomBytes3, timingSafeEqual } from "node:crypto";
|
|
21329
21630
|
import { createReadStream } from "node:fs";
|
|
21330
21631
|
import { realpath as realpath2, stat as stat8 } from "node:fs/promises";
|
|
21331
21632
|
import {
|
|
21332
21633
|
createServer as createServer2
|
|
21333
21634
|
} from "node:http";
|
|
21334
|
-
import
|
|
21635
|
+
import path23 from "node:path";
|
|
21335
21636
|
import { fileURLToPath } from "node:url";
|
|
21336
21637
|
var UI_HOST = "127.0.0.1";
|
|
21337
21638
|
var UI_MAX_BODY_BYTES = 1024 * 1024;
|
|
@@ -21362,13 +21663,13 @@ function defaultBundleDir() {
|
|
|
21362
21663
|
return fileURLToPath(new URL("./ui/", import.meta.url));
|
|
21363
21664
|
}
|
|
21364
21665
|
function newUiToken() {
|
|
21365
|
-
return
|
|
21666
|
+
return randomBytes3(32).toString("base64url");
|
|
21366
21667
|
}
|
|
21367
21668
|
function uiCookieName(port) {
|
|
21368
21669
|
return `omniralph_agent_${port}`;
|
|
21369
21670
|
}
|
|
21370
21671
|
function digest(value) {
|
|
21371
|
-
return
|
|
21672
|
+
return createHash5("sha256").update(value).digest();
|
|
21372
21673
|
}
|
|
21373
21674
|
function tokensEqual(a, b) {
|
|
21374
21675
|
return timingSafeEqual(digest(a), digest(b));
|
|
@@ -21445,12 +21746,12 @@ function safeSegments(rawPath) {
|
|
|
21445
21746
|
return segments;
|
|
21446
21747
|
}
|
|
21447
21748
|
function isWithin2(child, parent) {
|
|
21448
|
-
const relative2 =
|
|
21449
|
-
return relative2 === "" || !relative2.startsWith("..") && !
|
|
21749
|
+
const relative2 = path23.relative(parent, child);
|
|
21750
|
+
return relative2 === "" || !relative2.startsWith("..") && !path23.isAbsolute(relative2);
|
|
21450
21751
|
}
|
|
21451
21752
|
async function startUiServer(options) {
|
|
21452
21753
|
const token = options.token ?? newUiToken();
|
|
21453
|
-
const bundleDir =
|
|
21754
|
+
const bundleDir = path23.resolve(options.bundleDir ?? defaultBundleDir());
|
|
21454
21755
|
const allowedHosts = options.allowedHosts ?? [];
|
|
21455
21756
|
const routes = [
|
|
21456
21757
|
{
|
|
@@ -21554,7 +21855,7 @@ async function startUiServer(options) {
|
|
|
21554
21855
|
};
|
|
21555
21856
|
const serveFile = (req, res, file, cacheControl) => {
|
|
21556
21857
|
res.writeHead(200, {
|
|
21557
|
-
"content-type": CONTENT_TYPES[
|
|
21858
|
+
"content-type": CONTENT_TYPES[path23.extname(file).toLowerCase()] ?? "application/octet-stream",
|
|
21558
21859
|
"cache-control": cacheControl
|
|
21559
21860
|
});
|
|
21560
21861
|
if (req.method === "HEAD") {
|
|
@@ -21585,14 +21886,14 @@ async function startUiServer(options) {
|
|
|
21585
21886
|
let root;
|
|
21586
21887
|
try {
|
|
21587
21888
|
root = await realpath2(bundleDir);
|
|
21588
|
-
if (!(await stat8(
|
|
21889
|
+
if (!(await stat8(path23.join(root, "index.html"))).isFile()) throw new Error("not a file");
|
|
21589
21890
|
} catch {
|
|
21590
21891
|
sendHtml(req, res, 503, NOT_BUILT_PAGE);
|
|
21591
21892
|
return;
|
|
21592
21893
|
}
|
|
21593
|
-
const index =
|
|
21894
|
+
const index = path23.join(root, "index.html");
|
|
21594
21895
|
if (segments.length > 0) {
|
|
21595
|
-
const candidate =
|
|
21896
|
+
const candidate = path23.join(root, ...segments);
|
|
21596
21897
|
const real = await realpath2(candidate).catch(() => null);
|
|
21597
21898
|
const info = real && isWithin2(real, root) ? await stat8(real).catch(() => null) : null;
|
|
21598
21899
|
if (real && info?.isFile()) {
|
|
@@ -21703,7 +22004,7 @@ var UiLink = z42.object({
|
|
|
21703
22004
|
token: z42.string().min(1)
|
|
21704
22005
|
});
|
|
21705
22006
|
function uiLinkPath(configPath) {
|
|
21706
|
-
return
|
|
22007
|
+
return path24.join(path24.dirname(configPath), "ui.json");
|
|
21707
22008
|
}
|
|
21708
22009
|
async function writeUiLink(file, link) {
|
|
21709
22010
|
await writePrivateJson(file, link);
|
|
@@ -21711,7 +22012,7 @@ async function writeUiLink(file, link) {
|
|
|
21711
22012
|
async function readUiLink(file) {
|
|
21712
22013
|
let text3;
|
|
21713
22014
|
try {
|
|
21714
|
-
text3 = await
|
|
22015
|
+
text3 = await readFile14(file, "utf8");
|
|
21715
22016
|
} catch (error) {
|
|
21716
22017
|
if (error.code === "ENOENT") return null;
|
|
21717
22018
|
throw error;
|
|
@@ -22046,7 +22347,7 @@ function attemptRoutes(options) {
|
|
|
22046
22347
|
// src/ui/log-routes.ts
|
|
22047
22348
|
import { createReadStream as createReadStream2 } from "node:fs";
|
|
22048
22349
|
import { stat as stat9 } from "node:fs/promises";
|
|
22049
|
-
import
|
|
22350
|
+
import path25 from "node:path";
|
|
22050
22351
|
var LOG_PAGE_LINES = 200;
|
|
22051
22352
|
var LOG_PAGE_LEVELS = ["all", "warn"];
|
|
22052
22353
|
var NO_LOG_FILE = "This agent hasn't written a log file.";
|
|
@@ -22093,7 +22394,7 @@ function logRoutes(options) {
|
|
|
22093
22394
|
method: "GET",
|
|
22094
22395
|
path: "/api/logs/download",
|
|
22095
22396
|
handle: async ({ req, res }) => {
|
|
22096
|
-
const file = log.dir === void 0 ? void 0 :
|
|
22397
|
+
const file = log.dir === void 0 ? void 0 : path25.join(log.dir, LOG_FILE_NAME);
|
|
22097
22398
|
const info = file ? await stat9(file).catch(() => null) : null;
|
|
22098
22399
|
if (!file || !info?.isFile()) throw new UiHttpError(404, NO_LOG_FILE);
|
|
22099
22400
|
res.writeHead(200, {
|
|
@@ -22119,8 +22420,13 @@ function logRoutes(options) {
|
|
|
22119
22420
|
}
|
|
22120
22421
|
|
|
22121
22422
|
// src/ui/machine-routes.ts
|
|
22122
|
-
function
|
|
22123
|
-
|
|
22423
|
+
function signInError(error) {
|
|
22424
|
+
if (error instanceof ClaudeSignInError) throw new UiHttpError(error.status, error.message);
|
|
22425
|
+
throw error;
|
|
22426
|
+
}
|
|
22427
|
+
function claudeRoutes(claude, options = {}) {
|
|
22428
|
+
const { signIn, heartbeatSoon, logger } = options;
|
|
22429
|
+
const routes = [
|
|
22124
22430
|
{
|
|
22125
22431
|
method: "POST",
|
|
22126
22432
|
path: "/api/claude/check",
|
|
@@ -22130,6 +22436,47 @@ function claudeRoutes(claude) {
|
|
|
22130
22436
|
})
|
|
22131
22437
|
}
|
|
22132
22438
|
];
|
|
22439
|
+
if (!signIn) return routes;
|
|
22440
|
+
routes.push(
|
|
22441
|
+
{
|
|
22442
|
+
method: "POST",
|
|
22443
|
+
path: "/api/claude/sign-in",
|
|
22444
|
+
handle: () => {
|
|
22445
|
+
try {
|
|
22446
|
+
return { status: 200, body: signIn.start() };
|
|
22447
|
+
} catch (error) {
|
|
22448
|
+
signInError(error);
|
|
22449
|
+
}
|
|
22450
|
+
}
|
|
22451
|
+
},
|
|
22452
|
+
{
|
|
22453
|
+
method: "POST",
|
|
22454
|
+
path: "/api/claude/sign-in/complete",
|
|
22455
|
+
handle: async (request) => {
|
|
22456
|
+
const body = await request.json();
|
|
22457
|
+
const code5 = body !== null && typeof body === "object" ? body.code : void 0;
|
|
22458
|
+
if (typeof code5 !== "string" || code5.trim().length === 0 || code5.length > 4096) {
|
|
22459
|
+
throw new UiHttpError(400, "Paste the code the sign-in page shows");
|
|
22460
|
+
}
|
|
22461
|
+
try {
|
|
22462
|
+
const result = await signIn.complete(code5);
|
|
22463
|
+
logger?.info({ subscriptionType: result.subscriptionType }, "Claude signed in again");
|
|
22464
|
+
if (result.accountNotUpdated) {
|
|
22465
|
+
logger?.warn(
|
|
22466
|
+
{ reason: result.accountNotUpdated },
|
|
22467
|
+
"Claude's tokens are stored, but its account in ~/.claude.json wasn't updated"
|
|
22468
|
+
);
|
|
22469
|
+
}
|
|
22470
|
+
} catch (error) {
|
|
22471
|
+
signInError(error);
|
|
22472
|
+
}
|
|
22473
|
+
const status = await claude.check({ force: true });
|
|
22474
|
+
heartbeatSoon?.();
|
|
22475
|
+
return { status: 200, body: { claude: status } };
|
|
22476
|
+
}
|
|
22477
|
+
}
|
|
22478
|
+
);
|
|
22479
|
+
return routes;
|
|
22133
22480
|
}
|
|
22134
22481
|
function storageRoutes(options) {
|
|
22135
22482
|
const { storage, logger } = options;
|
|
@@ -22457,7 +22804,7 @@ ${USAGE}`);
|
|
|
22457
22804
|
}
|
|
22458
22805
|
}
|
|
22459
22806
|
function expandHome(dir, home = homedir3()) {
|
|
22460
|
-
return
|
|
22807
|
+
return path26.resolve(dir === "~" || dir.startsWith("~/") ? path26.join(home, dir.slice(1)) : dir);
|
|
22461
22808
|
}
|
|
22462
22809
|
async function runEnroll(values, configPath, deps) {
|
|
22463
22810
|
if (!values.server || !values.token) {
|
|
@@ -22631,7 +22978,13 @@ async function runAgent(config, configPath, agentDataDir, healthzPort, log, deps
|
|
|
22631
22978
|
history: installs,
|
|
22632
22979
|
logger
|
|
22633
22980
|
});
|
|
22634
|
-
const
|
|
22981
|
+
const claudePaths = claudeLoginPaths(deps.env, deps.homeDir ?? homedir3());
|
|
22982
|
+
const claude = new ClaudeStatusReporter({
|
|
22983
|
+
read: deps.readClaude ?? sdkReadClaude(),
|
|
22984
|
+
checkStoredLogin: storedLoginChecker({ paths: claudePaths }),
|
|
22985
|
+
logger
|
|
22986
|
+
});
|
|
22987
|
+
const claudeSignIn = new ClaudeSignIn({ paths: claudePaths, fetch: deps.fetch, env: deps.env });
|
|
22635
22988
|
const hub = new UiEventHub();
|
|
22636
22989
|
let onHeartbeat = () => {
|
|
22637
22990
|
};
|
|
@@ -22690,7 +23043,11 @@ async function runAgent(config, configPath, agentDataDir, healthzPort, log, deps
|
|
|
22690
23043
|
...settingsRoutes(settings),
|
|
22691
23044
|
...packageRoutes({ packages, history: installs, logger }),
|
|
22692
23045
|
...updateRoutes({ updater, daemon }),
|
|
22693
|
-
...claudeRoutes(claude
|
|
23046
|
+
...claudeRoutes(claude, {
|
|
23047
|
+
signIn: claudeSignIn,
|
|
23048
|
+
heartbeatSoon: () => daemon.heartbeatSoon(),
|
|
23049
|
+
logger
|
|
23050
|
+
}),
|
|
22694
23051
|
...storageRoutes({ storage, logger }),
|
|
22695
23052
|
...attemptRoutes({ history, log: logSources, logger }),
|
|
22696
23053
|
...logRoutes({ log: logSources, logger })
|