@lobehub/cli 0.0.94 → 0.0.95
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/acceptance-report.html +2 -2
- package/dist/index.js +489 -29
- package/man/man1/lh.1 +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -46559,7 +46559,7 @@ const ACCOUNT_KINDS = [
|
|
|
46559
46559
|
"wallet",
|
|
46560
46560
|
"service"
|
|
46561
46561
|
];
|
|
46562
|
-
const parseKind = (value) => {
|
|
46562
|
+
const parseKind$1 = (value) => {
|
|
46563
46563
|
if (!ACCOUNT_KINDS.includes(value)) throw new Error(`Unknown account kind: ${value}. Valid values: ${ACCOUNT_KINDS.join(", ")}.`);
|
|
46564
46564
|
return value;
|
|
46565
46565
|
};
|
|
@@ -46622,7 +46622,7 @@ const fenceUntrusted = (body) => [
|
|
|
46622
46622
|
].join("\n");
|
|
46623
46623
|
function registerAgentAccountCommand(agent) {
|
|
46624
46624
|
const account = agent.command("account").description("Manage the addresses an agent owns (its mail inbox, mounted logins)");
|
|
46625
|
-
account.command("list [agentId]").description("List an agent's accounts (or every account in scope with --all)").option("-s, --slug <slug>", "Agent slug").option("--all", "List accounts of every agent in scope").option("-k, --kind <kind>", `Filter by kind (${ACCOUNT_KINDS.join(", ")})`, parseKind).option("-p, --provider <provider>", "Filter by provider (e.g. agent-mail)").option("--json [fields]", "Output JSON, optionally specify fields (comma-separated)").action(async (agentIdArg, options) => {
|
|
46625
|
+
account.command("list [agentId]").description("List an agent's accounts (or every account in scope with --all)").option("-s, --slug <slug>", "Agent slug").option("--all", "List accounts of every agent in scope").option("-k, --kind <kind>", `Filter by kind (${ACCOUNT_KINDS.join(", ")})`, parseKind$1).option("-p, --provider <provider>", "Filter by provider (e.g. agent-mail)").option("--json [fields]", "Output JSON, optionally specify fields (comma-separated)").action(async (agentIdArg, options) => {
|
|
46626
46626
|
const client = await getTrpcClient();
|
|
46627
46627
|
const agentId = options.all ? void 0 : await resolveAgentId$1(client, {
|
|
46628
46628
|
agentId: agentIdArg,
|
|
@@ -46702,7 +46702,7 @@ function registerAgentAccountCommand(agent) {
|
|
|
46702
46702
|
}
|
|
46703
46703
|
console.log(`${import_picocolors.default.green("✓")} Issued ${import_picocolors.default.bold(item.identifier)} ${import_picocolors.default.dim(item.id)}`);
|
|
46704
46704
|
});
|
|
46705
|
-
account.command("mount [agentId]").description("Attach an account the user already holds (e.g. a login). Provider-issued accounts go through `provision`.").option("-s, --slug <slug>", "Agent slug").requiredOption("-i, --identifier <identifier>", "Handle the outside world knows it by").requiredOption("-k, --kind <kind>", `Account kind (${ACCOUNT_KINDS.join(", ")})`, parseKind).option("-p, --provider <provider>", "Provider name", "user").option("-c, --capabilities <list>", "Comma-separated capabilities: receive, send, login, sign", parseCapabilities).option("-n, --display-name <name>", "Display name").option("--metadata-file <path>", "JSON object of non-secret metadata (\"-\" for stdin)").option("--json", "Output JSON").action(async (agentIdArg, options) => {
|
|
46705
|
+
account.command("mount [agentId]").description("Attach an account the user already holds (e.g. a login). Provider-issued accounts go through `provision`.").option("-s, --slug <slug>", "Agent slug").requiredOption("-i, --identifier <identifier>", "Handle the outside world knows it by").requiredOption("-k, --kind <kind>", `Account kind (${ACCOUNT_KINDS.join(", ")})`, parseKind$1).option("-p, --provider <provider>", "Provider name", "user").option("-c, --capabilities <list>", "Comma-separated capabilities: receive, send, login, sign", parseCapabilities).option("-n, --display-name <name>", "Display name").option("--metadata-file <path>", "JSON object of non-secret metadata (\"-\" for stdin)").option("--json", "Output JSON").action(async (agentIdArg, options) => {
|
|
46706
46706
|
const client = await getTrpcClient();
|
|
46707
46707
|
const agentId = await resolveAgentId$1(client, {
|
|
46708
46708
|
agentId: agentIdArg,
|
|
@@ -559529,6 +559529,71 @@ function registerGoalGraphEditCommands(goal) {
|
|
|
559529
559529
|
});
|
|
559530
559530
|
}
|
|
559531
559531
|
|
|
559532
|
+
//#endregion
|
|
559533
|
+
//#region src/commands/goal/graphGlyphs.ts
|
|
559534
|
+
/** The glyph `lh goal show` prints before each node of the graph. */
|
|
559535
|
+
const nodeIcon = {
|
|
559536
|
+
batch: "▦",
|
|
559537
|
+
dead_end: "✕",
|
|
559538
|
+
decision: "◆",
|
|
559539
|
+
experiment: "⚗",
|
|
559540
|
+
finding: "●",
|
|
559541
|
+
hazard: "⚠",
|
|
559542
|
+
pattern: "↻",
|
|
559543
|
+
problem: "◇",
|
|
559544
|
+
task: "▣",
|
|
559545
|
+
template: "▤",
|
|
559546
|
+
tool: "⚒"
|
|
559547
|
+
};
|
|
559548
|
+
/**
|
|
559549
|
+
* Every edge kind reads `source <kind> target`, so a row listing its INCOMING
|
|
559550
|
+
* edges by kind states the relationship backwards: an incoming `depends_on`
|
|
559551
|
+
* means the other node depends on THIS one, not the reverse. Render the inverse
|
|
559552
|
+
* verb instead, so each entry is a true sentence about the row it sits on.
|
|
559553
|
+
*/
|
|
559554
|
+
const inverseEdgeLabel = {
|
|
559555
|
+
answers: "answered by",
|
|
559556
|
+
confirms: "confirmed by",
|
|
559557
|
+
contains: "inside",
|
|
559558
|
+
contradicts: "contradicted by",
|
|
559559
|
+
decomposes: "part of",
|
|
559560
|
+
depends_on: "blocks",
|
|
559561
|
+
derived_from: "ancestor of",
|
|
559562
|
+
investigates: "investigated by",
|
|
559563
|
+
leads_to: "follows",
|
|
559564
|
+
produces: "produced by",
|
|
559565
|
+
refutes: "refuted by",
|
|
559566
|
+
supports: "supported by"
|
|
559567
|
+
};
|
|
559568
|
+
|
|
559569
|
+
//#endregion
|
|
559570
|
+
//#region ../../packages/const/src/goal.ts
|
|
559571
|
+
/**
|
|
559572
|
+
* The batch pattern-break gate's option ids. Shared by the server that opens the
|
|
559573
|
+
* gate and the client that localizes it, the same way `GOAL_CLARIFICATION_OPTION`
|
|
559574
|
+
* is; the human-facing labels live in the locale files, never in the payload.
|
|
559575
|
+
*/
|
|
559576
|
+
const GOAL_BATCH_BREAK_OPTION = {
|
|
559577
|
+
newClass: "new_class",
|
|
559578
|
+
revise: "revise"
|
|
559579
|
+
};
|
|
559580
|
+
/**
|
|
559581
|
+
* The release-approval gate's option ids. `revise` shares the pattern break's
|
|
559582
|
+
* id, but here the correction goes to the main Agent, whose rewritten plan
|
|
559583
|
+
* returns to the same review; the probes are not run again.
|
|
559584
|
+
*/
|
|
559585
|
+
const GOAL_BATCH_RELEASE_OPTION = {
|
|
559586
|
+
release: "release",
|
|
559587
|
+
revise: GOAL_BATCH_BREAK_OPTION.revise
|
|
559588
|
+
};
|
|
559589
|
+
/** Node kinds that are marks: Goal-wide, left by one Task for every later one. */
|
|
559590
|
+
const GOAL_MARK_KINDS = [
|
|
559591
|
+
"pattern",
|
|
559592
|
+
"tool",
|
|
559593
|
+
"hazard",
|
|
559594
|
+
"dead_end"
|
|
559595
|
+
];
|
|
559596
|
+
|
|
559532
559597
|
//#endregion
|
|
559533
559598
|
//#region src/commands/goal/reads.ts
|
|
559534
559599
|
/**
|
|
@@ -559571,6 +559636,86 @@ const goalReads = async () => {
|
|
|
559571
559636
|
};
|
|
559572
559637
|
};
|
|
559573
559638
|
|
|
559639
|
+
//#endregion
|
|
559640
|
+
//#region src/commands/goal/mark.ts
|
|
559641
|
+
const KINDS_HELP = `Kinds: ${GOAL_MARK_KINDS.join(", ")}.`;
|
|
559642
|
+
const parseKind = (kind) => {
|
|
559643
|
+
if (!GOAL_MARK_KINDS.includes(kind)) throw new Error(`Unknown mark kind "${kind}". ${KINDS_HELP}`);
|
|
559644
|
+
return kind;
|
|
559645
|
+
};
|
|
559646
|
+
const describeMark = (mark) => {
|
|
559647
|
+
const record = mark.confirms || mark.refutes ? `+${mark.confirms}/-${mark.refutes}` : import_picocolors.default.dim("unconfirmed");
|
|
559648
|
+
const retired = mark.status === "retired" ? import_picocolors.default.dim(" (retired)") : "";
|
|
559649
|
+
const lines = [`${import_picocolors.default.bold(`[${mark.kind}]`)} ${mark.title} ${record}${retired}`, import_picocolors.default.dim(` id: ${mark.id}`)];
|
|
559650
|
+
if (mark.description) lines.push(` ${mark.description.replaceAll("\n", "\n ")}`);
|
|
559651
|
+
return lines.join("\n");
|
|
559652
|
+
};
|
|
559653
|
+
/** Outside a Goal Task run there is no run to stand for, so the goal must be named. */
|
|
559654
|
+
const requireGoalId$1 = (goalId, command) => {
|
|
559655
|
+
if (!goalId) throw new Error(`Pass the goal id: \`lh goal ${command}\` outside a Goal Task run cannot tell which goal you mean.`);
|
|
559656
|
+
return goalId;
|
|
559657
|
+
};
|
|
559658
|
+
const RUN_ONLY = "Only a Goal Task run judges a mark: it records that this run relied on the mark. Run it from inside the Task run.";
|
|
559659
|
+
/**
|
|
559660
|
+
* Marks: the notes runs of one Goal leave for each other.
|
|
559661
|
+
*
|
|
559662
|
+
* Inside a Goal Task run (an operation token), every command acts for that
|
|
559663
|
+
* run's Goal and Task, so no ids are needed. With a person's login, `list`,
|
|
559664
|
+
* `add` and `retire` take the goal id; judging is for runs only.
|
|
559665
|
+
*/
|
|
559666
|
+
function registerGoalMarkCommands(goal) {
|
|
559667
|
+
const mark = goal.command("mark").summary("Notes runs of a goal leave for each other").description([
|
|
559668
|
+
"Marks are short notes one run of a goal leaves for every later run: a fix for a pitfall",
|
|
559669
|
+
"(pattern), a reusable script (tool), something not to touch (hazard), or an approach",
|
|
559670
|
+
"that failed (dead_end). Every Task run of the goal is handed the trusted ones.",
|
|
559671
|
+
KINDS_HELP
|
|
559672
|
+
].join("\n"));
|
|
559673
|
+
mark.command("add <kind> <title>").description(`Leave a mark for later runs. ${KINDS_HELP}`).option("-d, --description <text>", "The details: exact error, cause, fix, path, usage").option("--goal <id>", "The goal, when not run from inside one of its Task runs").action(async (kind, title, options) => {
|
|
559674
|
+
const input = {
|
|
559675
|
+
description: options.description,
|
|
559676
|
+
kind: parseKind(kind),
|
|
559677
|
+
title
|
|
559678
|
+
};
|
|
559679
|
+
const client = await getTrpcClient();
|
|
559680
|
+
const result = hasOperationToken() ? await client.goal.addMarkOperation.mutate(input) : await client.goal.addMark.mutate({
|
|
559681
|
+
...input,
|
|
559682
|
+
id: requireGoalId$1(options.goal, "mark add --goal <id>")
|
|
559683
|
+
});
|
|
559684
|
+
log$13.info(`Left ${input.kind} mark ${result.data.id}`);
|
|
559685
|
+
});
|
|
559686
|
+
mark.command("list [goal-id]").description("List the marks of a goal, newest first").option("--json", "Output JSON").action(async (goalId, options) => {
|
|
559687
|
+
const client = await getTrpcClient();
|
|
559688
|
+
const marks = hasOperationToken() ? (await client.goal.marksOperation.query()).data : (await client.goal.marks.query({ id: requireGoalId$1(goalId, "mark list <goal-id>") })).data;
|
|
559689
|
+
if (options.json) return outputJson(marks);
|
|
559690
|
+
if (marks.length === 0) return log$13.info("No marks yet.");
|
|
559691
|
+
console.log(marks.map(describeMark).join("\n\n"));
|
|
559692
|
+
});
|
|
559693
|
+
for (const [command, verdict, summary] of [[
|
|
559694
|
+
"confirm",
|
|
559695
|
+
"confirms",
|
|
559696
|
+
"Record that a mark this run relied on held"
|
|
559697
|
+
], [
|
|
559698
|
+
"refute",
|
|
559699
|
+
"refutes",
|
|
559700
|
+
"Record that a mark this run relied on was wrong"
|
|
559701
|
+
]]) mark.command(`${command} <mark-id>`).description(summary).action(async (markId) => {
|
|
559702
|
+
if (!hasOperationToken()) throw new Error(RUN_ONLY);
|
|
559703
|
+
const result = await (await getTrpcClient()).goal.judgeMarkOperation.mutate({
|
|
559704
|
+
markId,
|
|
559705
|
+
verdict
|
|
559706
|
+
});
|
|
559707
|
+
log$13.info(`Recorded: ${verdict} ${markId} (now +${result.data.confirms}/-${result.data.refutes})`);
|
|
559708
|
+
});
|
|
559709
|
+
mark.command("retire <goal-id> <mark-id>").description("Take a mark out of every later run; it stays on the goal graph").option("--reason <text>", "Why, kept on the audit trail").action(async (goalId, markId, options) => {
|
|
559710
|
+
const result = await (await getTrpcClient()).goal.retireMark.mutate({
|
|
559711
|
+
id: goalId,
|
|
559712
|
+
markId,
|
|
559713
|
+
reason: options.reason
|
|
559714
|
+
});
|
|
559715
|
+
log$13.info(result.message);
|
|
559716
|
+
});
|
|
559717
|
+
}
|
|
559718
|
+
|
|
559574
559719
|
//#endregion
|
|
559575
559720
|
//#region src/commands/goal/metric.ts
|
|
559576
559721
|
const comparisons = [
|
|
@@ -559665,6 +559810,345 @@ function registerGoalMetricCommands(goal) {
|
|
|
559665
559810
|
});
|
|
559666
559811
|
}
|
|
559667
559812
|
|
|
559813
|
+
//#endregion
|
|
559814
|
+
//#region ../../packages/utils/src/goalObjective.ts
|
|
559815
|
+
const isFiniteNumber = (value) => typeof value === "number" && Number.isFinite(value);
|
|
559816
|
+
const numericMetrics = (value) => {
|
|
559817
|
+
if (!value || typeof value !== "object") return void 0;
|
|
559818
|
+
const entries = Object.entries(value).filter(([, v]) => isFiniteNumber(v));
|
|
559819
|
+
return entries.length > 0 ? Object.fromEntries(entries) : void 0;
|
|
559820
|
+
};
|
|
559821
|
+
const fromJson = (text) => {
|
|
559822
|
+
const candidates = text.match(/\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}/g) ?? [];
|
|
559823
|
+
for (const raw of candidates.reverse()) try {
|
|
559824
|
+
const data = JSON.parse(raw);
|
|
559825
|
+
const score = isFiniteNumber(data.score) ? data.score : typeof data.score === "string" && data.score.trim() !== "" && Number.isFinite(Number(data.score)) ? Number(data.score) : void 0;
|
|
559826
|
+
if (score === void 0) continue;
|
|
559827
|
+
return {
|
|
559828
|
+
metrics: numericMetrics(data.metrics),
|
|
559829
|
+
score,
|
|
559830
|
+
summary: typeof data.summary === "string" && data.summary ? data.summary : void 0,
|
|
559831
|
+
valid: data.valid !== false
|
|
559832
|
+
};
|
|
559833
|
+
} catch {
|
|
559834
|
+
continue;
|
|
559835
|
+
}
|
|
559836
|
+
};
|
|
559837
|
+
const NUMBER = String.raw`[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?`;
|
|
559838
|
+
/**
|
|
559839
|
+
* The score in an evaluator's output, or undefined when there is none.
|
|
559840
|
+
*
|
|
559841
|
+
* Accepted, most specific first: a JSON object with a numeric `score`
|
|
559842
|
+
* (optional `valid`, `summary`, `metrics`); a `TOTAL_SCORE <n>` line; a
|
|
559843
|
+
* `score: <n>` / `score = <n>` line; a last line that is a bare number. The
|
|
559844
|
+
* last match of a form wins, so a final result beats earlier progress output.
|
|
559845
|
+
*/
|
|
559846
|
+
const parseEvaluatorOutput = (output) => {
|
|
559847
|
+
const json = fromJson(output);
|
|
559848
|
+
if (json) return json;
|
|
559849
|
+
for (const pattern of [new RegExp(String.raw`^\s*TOTAL_SCORE\s+(${NUMBER})\s*$`, "gim"), new RegExp(String.raw`^\s*(?:final\s+)?score\s*[:=]\s*(${NUMBER})\s*$`, "gim")]) {
|
|
559850
|
+
const matches = [...output.matchAll(pattern)];
|
|
559851
|
+
if (matches.length > 0) return {
|
|
559852
|
+
score: Number(matches.at(-1)[1]),
|
|
559853
|
+
valid: true
|
|
559854
|
+
};
|
|
559855
|
+
}
|
|
559856
|
+
const last = output.trim().split("\n").at(-1)?.trim() ?? "";
|
|
559857
|
+
if (new RegExp(`^${NUMBER}$`).test(last)) return {
|
|
559858
|
+
score: Number(last),
|
|
559859
|
+
valid: true
|
|
559860
|
+
};
|
|
559861
|
+
};
|
|
559862
|
+
|
|
559863
|
+
//#endregion
|
|
559864
|
+
//#region src/commands/goal/objective.ts
|
|
559865
|
+
const DEFAULT_TIMEOUT_SECONDS = 1800;
|
|
559866
|
+
const OUTPUT_TAIL_LINES = 60;
|
|
559867
|
+
const LOCK_POLL_MS = 2e3;
|
|
559868
|
+
/** Only for locks written before a holder's deadline was recorded next to its pid. */
|
|
559869
|
+
const LOCK_STALE_GRACE_MS = 300;
|
|
559870
|
+
const formatScore = (value) => String(Number(value.toFixed(4)));
|
|
559871
|
+
const printLeaderboard = (board) => {
|
|
559872
|
+
const { direction, evaluator, title } = board.objective;
|
|
559873
|
+
console.log(`${import_picocolors.default.bold(title || "Objective")} — ${direction === "max" ? "higher" : "lower"} is better · evaluator: ${import_picocolors.default.dim(evaluator)}`);
|
|
559874
|
+
if (board.lanes.length === 0) return console.log(import_picocolors.default.dim(" No evaluation recorded yet."));
|
|
559875
|
+
board.lanes.forEach((lane, index) => {
|
|
559876
|
+
const you = lane.lane === board.you ? import_picocolors.default.cyan(" (your lane)") : "";
|
|
559877
|
+
console.log(` ${index + 1}. ${lane.title}${you} best ${import_picocolors.default.bold(formatScore(lane.best))} · latest ${formatScore(lane.latest)} · ${lane.evaluations} eval(s)`);
|
|
559878
|
+
});
|
|
559879
|
+
};
|
|
559880
|
+
/** Outside a Goal Task run there is no run to stand for, so the goal must be named. */
|
|
559881
|
+
const requireGoalId = (goalId, command) => {
|
|
559882
|
+
if (!goalId) throw new Error(`Pass the goal id: \`lh goal ${command}\` outside a Goal Task run cannot tell which goal you mean.`);
|
|
559883
|
+
return goalId;
|
|
559884
|
+
};
|
|
559885
|
+
const readLeaderboard = async (goalId) => {
|
|
559886
|
+
const client = await getTrpcClient();
|
|
559887
|
+
return hasOperationToken() ? (await client.goal.objectiveOperation.query()).data : (await client.goal.objective.query({ id: requireGoalId(goalId, "objective <goal-id>") })).data;
|
|
559888
|
+
};
|
|
559889
|
+
/**
|
|
559890
|
+
* One evaluation at a time per machine: evaluators are often heavy (a judge
|
|
559891
|
+
* that trains or replays a model), and lanes of one goal on the same host
|
|
559892
|
+
* would otherwise starve each other. A directory is the lock because `mkdir`
|
|
559893
|
+
* is atomic everywhere; a holder that died is detected by its pid or age.
|
|
559894
|
+
*/
|
|
559895
|
+
const withMachineLock = async (timeoutSeconds, run, { dir = path.join(tmpdir(), "lobehub-goal-evaluate.lock"), onWait = () => {}, pollMs = LOCK_POLL_MS } = {}) => {
|
|
559896
|
+
const owner = `${process.pid}-${Date.now().toString(36)}`;
|
|
559897
|
+
const staged = `${dir}.staging-${owner}`;
|
|
559898
|
+
let waited = false;
|
|
559899
|
+
for (;;) {
|
|
559900
|
+
await mkdir(staged, { recursive: true });
|
|
559901
|
+
await writeFile(path.join(staged, "pid"), String(process.pid));
|
|
559902
|
+
await writeFile(path.join(staged, "owner"), owner);
|
|
559903
|
+
await writeFile(path.join(staged, "deadline"), String(Date.now() + timeoutSeconds * 1e3));
|
|
559904
|
+
try {
|
|
559905
|
+
await rename(staged, dir);
|
|
559906
|
+
break;
|
|
559907
|
+
} catch (error) {
|
|
559908
|
+
await rm(staged, {
|
|
559909
|
+
force: true,
|
|
559910
|
+
recursive: true
|
|
559911
|
+
});
|
|
559912
|
+
const code = error.code;
|
|
559913
|
+
if (code !== "EEXIST" && code !== "ENOTEMPTY" && code !== "ENOTDIR") throw error;
|
|
559914
|
+
const holder = Number(await readFile(path.join(dir, "pid"), "utf8").catch(() => "0"));
|
|
559915
|
+
const deadline = Number(await readFile(path.join(dir, "deadline"), "utf8").catch(() => "0"));
|
|
559916
|
+
const age = Date.now() - (await stat(dir).catch(() => ({ mtimeMs: Date.now() }))).mtimeMs;
|
|
559917
|
+
const alive = holder > 0 && (() => {
|
|
559918
|
+
try {
|
|
559919
|
+
process.kill(holder, 0);
|
|
559920
|
+
return true;
|
|
559921
|
+
} catch {
|
|
559922
|
+
return false;
|
|
559923
|
+
}
|
|
559924
|
+
})();
|
|
559925
|
+
const expired = deadline > 0 ? Date.now() > deadline : age > LOCK_STALE_GRACE_MS * 1e3;
|
|
559926
|
+
if (!alive || expired) {
|
|
559927
|
+
await rm(dir, {
|
|
559928
|
+
force: true,
|
|
559929
|
+
recursive: true
|
|
559930
|
+
});
|
|
559931
|
+
continue;
|
|
559932
|
+
}
|
|
559933
|
+
if (!waited) onWait();
|
|
559934
|
+
waited = true;
|
|
559935
|
+
await new Promise((resolve) => setTimeout(resolve, pollMs));
|
|
559936
|
+
}
|
|
559937
|
+
}
|
|
559938
|
+
try {
|
|
559939
|
+
return await run();
|
|
559940
|
+
} finally {
|
|
559941
|
+
if (await readFile(path.join(dir, "owner"), "utf8").catch(() => "") === owner) await rm(dir, {
|
|
559942
|
+
force: true,
|
|
559943
|
+
recursive: true
|
|
559944
|
+
});
|
|
559945
|
+
}
|
|
559946
|
+
};
|
|
559947
|
+
/** Hash of the working tree's committed state plus its uncommitted changes, in a git repo. */
|
|
559948
|
+
const workingTreeFingerprint = (cwd) => {
|
|
559949
|
+
const git = (...args) => spawnSync("git", args, {
|
|
559950
|
+
cwd,
|
|
559951
|
+
encoding: "utf8",
|
|
559952
|
+
maxBuffer: 64 * 1024 * 1024
|
|
559953
|
+
});
|
|
559954
|
+
const head = git("rev-parse", "HEAD");
|
|
559955
|
+
if (head.status !== 0) return void 0;
|
|
559956
|
+
const diff = git("diff", "HEAD");
|
|
559957
|
+
const untracked = git("ls-files", "--others", "--exclude-standard", "-z");
|
|
559958
|
+
return createHash("sha256").update(head.stdout).update(diff.stdout ?? "").update(untracked.stdout ?? "").digest("hex").slice(0, 16);
|
|
559959
|
+
};
|
|
559960
|
+
/**
|
|
559961
|
+
* Splits an evaluator command into an executable and its arguments.
|
|
559962
|
+
*
|
|
559963
|
+
* The evaluator string is stored on the goal and read back from the server, so other lane members
|
|
559964
|
+
* can write it. It is never handed to a shell: metacharacters stay literal, and only quoting (so
|
|
559965
|
+
* one argument can contain a space) keeps its meaning.
|
|
559966
|
+
*/
|
|
559967
|
+
const splitEvaluatorCommand = (command) => {
|
|
559968
|
+
const tokens = [];
|
|
559969
|
+
let current = "";
|
|
559970
|
+
let quoted;
|
|
559971
|
+
let started = false;
|
|
559972
|
+
for (const char of command) {
|
|
559973
|
+
if (quoted) {
|
|
559974
|
+
if (char === quoted) quoted = void 0;
|
|
559975
|
+
else current += char;
|
|
559976
|
+
continue;
|
|
559977
|
+
}
|
|
559978
|
+
if (char === "\"" || char === "'") {
|
|
559979
|
+
quoted = char;
|
|
559980
|
+
started = true;
|
|
559981
|
+
continue;
|
|
559982
|
+
}
|
|
559983
|
+
if (/\s/.test(char)) {
|
|
559984
|
+
if (started) tokens.push(current);
|
|
559985
|
+
current = "";
|
|
559986
|
+
started = false;
|
|
559987
|
+
continue;
|
|
559988
|
+
}
|
|
559989
|
+
current += char;
|
|
559990
|
+
started = true;
|
|
559991
|
+
}
|
|
559992
|
+
if (quoted) throw new Error(`Unbalanced ${quoted} in the evaluator command: ${command}`);
|
|
559993
|
+
if (started) tokens.push(current);
|
|
559994
|
+
return tokens;
|
|
559995
|
+
};
|
|
559996
|
+
/** Kills the evaluator and everything it started, not just the process we spawned. */
|
|
559997
|
+
const killEvaluatorTree = (child) => {
|
|
559998
|
+
const { pid } = child;
|
|
559999
|
+
if (pid && process.platform !== "win32") try {
|
|
560000
|
+
process.kill(-pid, "SIGKILL");
|
|
560001
|
+
return;
|
|
560002
|
+
} catch {}
|
|
560003
|
+
if (pid && process.platform === "win32") {
|
|
560004
|
+
if (spawnSync("taskkill", [
|
|
560005
|
+
"/pid",
|
|
560006
|
+
String(pid),
|
|
560007
|
+
"/T",
|
|
560008
|
+
"/F"
|
|
560009
|
+
]).status === 0) return;
|
|
560010
|
+
}
|
|
560011
|
+
try {
|
|
560012
|
+
child.kill("SIGKILL");
|
|
560013
|
+
} catch {}
|
|
560014
|
+
};
|
|
560015
|
+
/**
|
|
560016
|
+
* Run the evaluator in `cwd`; resolves with its combined output and whether it timed out.
|
|
560017
|
+
*
|
|
560018
|
+
* The timeout settles on its own: a helper that inherited stdout/stderr can keep the pipes open
|
|
560019
|
+
* long after the evaluator is gone, and waiting for `close` would hold the machine lock past the
|
|
560020
|
+
* deadline the user asked for.
|
|
560021
|
+
*/
|
|
560022
|
+
const runEvaluator = (command, cwd, timeoutSeconds) => new Promise((resolve) => {
|
|
560023
|
+
const [executable, ...args] = splitEvaluatorCommand(command);
|
|
560024
|
+
if (!executable) {
|
|
560025
|
+
resolve({
|
|
560026
|
+
code: null,
|
|
560027
|
+
output: "",
|
|
560028
|
+
timedOut: false
|
|
560029
|
+
});
|
|
560030
|
+
return;
|
|
560031
|
+
}
|
|
560032
|
+
const child = spawn(executable, args, {
|
|
560033
|
+
cwd,
|
|
560034
|
+
detached: process.platform !== "win32",
|
|
560035
|
+
shell: false,
|
|
560036
|
+
stdio: [
|
|
560037
|
+
"ignore",
|
|
560038
|
+
"pipe",
|
|
560039
|
+
"pipe"
|
|
560040
|
+
]
|
|
560041
|
+
});
|
|
560042
|
+
let output = "";
|
|
560043
|
+
let timedOut = false;
|
|
560044
|
+
let settled = false;
|
|
560045
|
+
const finish = (code) => {
|
|
560046
|
+
if (settled) return;
|
|
560047
|
+
settled = true;
|
|
560048
|
+
clearTimeout(timer);
|
|
560049
|
+
child.stdout.destroy();
|
|
560050
|
+
child.stderr.destroy();
|
|
560051
|
+
resolve({
|
|
560052
|
+
code,
|
|
560053
|
+
output,
|
|
560054
|
+
timedOut
|
|
560055
|
+
});
|
|
560056
|
+
};
|
|
560057
|
+
child.stdout.on("data", (chunk) => output += chunk);
|
|
560058
|
+
child.stderr.on("data", (chunk) => output += chunk);
|
|
560059
|
+
const timer = setTimeout(() => {
|
|
560060
|
+
timedOut = true;
|
|
560061
|
+
killEvaluatorTree(child);
|
|
560062
|
+
finish(null);
|
|
560063
|
+
}, timeoutSeconds * 1e3);
|
|
560064
|
+
child.on("error", (error) => {
|
|
560065
|
+
output += `${error.message}\n`;
|
|
560066
|
+
finish(null);
|
|
560067
|
+
});
|
|
560068
|
+
child.on("close", (code) => finish(code));
|
|
560069
|
+
});
|
|
560070
|
+
const tail = (text, lines) => text.trimEnd().split("\n").slice(-lines).join("\n");
|
|
560071
|
+
const printResult = (result) => {
|
|
560072
|
+
console.log("========================================");
|
|
560073
|
+
if (!result.valid) console.log(` Valid: ${import_picocolors.default.yellow("no (this evaluation does not count)")}`);
|
|
560074
|
+
console.log(` Score: ${import_picocolors.default.bold(formatScore(result.score))}`);
|
|
560075
|
+
if (result.summary) console.log(` Summary: ${result.summary}`);
|
|
560076
|
+
if (result.metrics) {
|
|
560077
|
+
console.log(" Metrics:");
|
|
560078
|
+
for (const [key, value] of Object.entries(result.metrics)) console.log(` ${key}: ${value}`);
|
|
560079
|
+
}
|
|
560080
|
+
console.log("========================================");
|
|
560081
|
+
};
|
|
560082
|
+
/**
|
|
560083
|
+
* Objectives: the score a goal optimises and the command that measures it.
|
|
560084
|
+
*
|
|
560085
|
+
* Inside a Goal Task run (an operation token) `evaluate` and `objective` act
|
|
560086
|
+
* for that run's goal and lane. With a person's login they take the goal id,
|
|
560087
|
+
* and an evaluation is recorded in the `manual` lane.
|
|
560088
|
+
*/
|
|
560089
|
+
function registerGoalObjectiveCommands(goal) {
|
|
560090
|
+
goal.command("set-objective <goal-id>").description("Set the score this goal optimises and the command that measures it").option("--evaluator <command>", "Command that prints the score, run in the working directory (no shell; quote arguments with spaces)").option("--direction <max|min>", "Whether higher or lower is better", "max").option("--key <key>", "Metric series key (default: objective)").option("--title <title>", "Name of the score").option("--timeout <seconds>", "Seconds before an evaluation is abandoned").option("--clear", "Remove the objective").action(async (goalId, options) => {
|
|
560091
|
+
if (!options.clear && !options.evaluator) throw new Error("Pass --evaluator <command>, or --clear to remove the objective.");
|
|
560092
|
+
if (options.direction !== "max" && options.direction !== "min") throw new Error("--direction must be max or min");
|
|
560093
|
+
const result = await (await getTrpcClient()).goal.setObjective.mutate({
|
|
560094
|
+
id: goalId,
|
|
560095
|
+
objective: options.clear ? null : {
|
|
560096
|
+
direction: options.direction,
|
|
560097
|
+
evaluator: options.evaluator,
|
|
560098
|
+
key: options.key,
|
|
560099
|
+
timeoutSeconds: options.timeout ? Number.parseInt(options.timeout, 10) : void 0,
|
|
560100
|
+
title: options.title
|
|
560101
|
+
}
|
|
560102
|
+
});
|
|
560103
|
+
log$13.info(result.message);
|
|
560104
|
+
});
|
|
560105
|
+
goal.command("objective [goal-id]").description("Show a goal's objective and the leaderboard of its lanes").option("--json", "Output JSON").action(async (goalId, options) => {
|
|
560106
|
+
const board = await readLeaderboard(goalId);
|
|
560107
|
+
if (options.json) return outputJson(board);
|
|
560108
|
+
if (!board) return log$13.info("This goal has no objective.");
|
|
560109
|
+
printLeaderboard(board);
|
|
560110
|
+
});
|
|
560111
|
+
goal.command("evaluate").description("Run the goal's evaluator in the current directory, record the score for your lane, and print it").option("-d, --details", "Also print the tail of the evaluator output").option("--goal <id>", "The goal, when not run from inside one of its Task runs").option("--timeout <seconds>", "Override the objective's evaluation timeout").action(async (options) => {
|
|
560112
|
+
const board = await readLeaderboard(options.goal);
|
|
560113
|
+
if (!board) throw new Error("This goal has no objective; set one with `lh goal set-objective`.");
|
|
560114
|
+
const timeoutSeconds = options.timeout ? Number.parseInt(options.timeout, 10) : board.objective.timeoutSeconds ?? DEFAULT_TIMEOUT_SECONDS;
|
|
560115
|
+
const cwd = process.cwd();
|
|
560116
|
+
const started = Date.now();
|
|
560117
|
+
const run = await withMachineLock(timeoutSeconds, () => runEvaluator(board.objective.evaluator, cwd, timeoutSeconds), { onWait: () => log$13.info("Waiting for another evaluation on this machine to finish...") });
|
|
560118
|
+
const durationMs = Date.now() - started;
|
|
560119
|
+
const result = run.timedOut ? void 0 : parseEvaluatorOutput(run.output);
|
|
560120
|
+
if (!result) {
|
|
560121
|
+
console.log(run.timedOut ? `Evaluator timed out after ${timeoutSeconds}s.` : `Evaluator printed no score (exit code ${run.code}).`);
|
|
560122
|
+
console.log(tail(run.output, OUTPUT_TAIL_LINES));
|
|
560123
|
+
process.exitCode = 1;
|
|
560124
|
+
return;
|
|
560125
|
+
}
|
|
560126
|
+
printResult(result);
|
|
560127
|
+
if (options.details) console.log(import_picocolors.default.dim(tail(run.output, OUTPUT_TAIL_LINES)));
|
|
560128
|
+
const input = {
|
|
560129
|
+
durationMs,
|
|
560130
|
+
fingerprint: workingTreeFingerprint(cwd),
|
|
560131
|
+
metrics: result.metrics,
|
|
560132
|
+
score: result.score,
|
|
560133
|
+
summary: result.summary,
|
|
560134
|
+
valid: result.valid
|
|
560135
|
+
};
|
|
560136
|
+
const client = await getTrpcClient();
|
|
560137
|
+
const recorded = hasOperationToken() ? (await client.goal.recordEvaluationOperation.mutate(input)).data : (await client.goal.recordEvaluation.mutate({
|
|
560138
|
+
...input,
|
|
560139
|
+
id: options.goal
|
|
560140
|
+
})).data;
|
|
560141
|
+
if (recorded.recorded) {
|
|
560142
|
+
if (recorded.newBest) log$13.info(import_picocolors.default.green("New best score for this goal."));
|
|
560143
|
+
else if (recorded.newLaneBest) log$13.info(import_picocolors.default.green("New best score for your lane."));
|
|
560144
|
+
}
|
|
560145
|
+
if (recorded.leaderboard) printLeaderboard({
|
|
560146
|
+
...recorded.leaderboard,
|
|
560147
|
+
you: recorded.lane
|
|
560148
|
+
});
|
|
560149
|
+
});
|
|
560150
|
+
}
|
|
560151
|
+
|
|
559668
560152
|
//#endregion
|
|
559669
560153
|
//#region src/commands/goal/report.ts
|
|
559670
560154
|
/** `lh goal report`: the wrap-up run submits the Goal report it wrote. */
|
|
@@ -559734,32 +560218,6 @@ const parseVariant = (raw) => {
|
|
|
559734
560218
|
values
|
|
559735
560219
|
};
|
|
559736
560220
|
};
|
|
559737
|
-
const nodeIcon = {
|
|
559738
|
-
batch: "▦",
|
|
559739
|
-
decision: "◆",
|
|
559740
|
-
experiment: "⚗",
|
|
559741
|
-
finding: "●",
|
|
559742
|
-
problem: "◇",
|
|
559743
|
-
task: "▣"
|
|
559744
|
-
};
|
|
559745
|
-
/**
|
|
559746
|
-
* Every edge kind reads `source <kind> target`, so a row listing its INCOMING
|
|
559747
|
-
* edges by kind states the relationship backwards: an incoming `depends_on`
|
|
559748
|
-
* means the other node depends on THIS one, not the reverse. Render the inverse
|
|
559749
|
-
* verb instead, so each entry is a true sentence about the row it sits on.
|
|
559750
|
-
*/
|
|
559751
|
-
const inverseEdgeLabel = {
|
|
559752
|
-
answers: "answered by",
|
|
559753
|
-
contains: "inside",
|
|
559754
|
-
contradicts: "contradicted by",
|
|
559755
|
-
decomposes: "part of",
|
|
559756
|
-
depends_on: "blocks",
|
|
559757
|
-
derived_from: "ancestor of",
|
|
559758
|
-
investigates: "investigated by",
|
|
559759
|
-
leads_to: "follows",
|
|
559760
|
-
produces: "produced by",
|
|
559761
|
-
supports: "supported by"
|
|
559762
|
-
};
|
|
559763
560221
|
const terminalOutcomes = new Set([
|
|
559764
560222
|
"achieved",
|
|
559765
560223
|
"waiting_human",
|
|
@@ -560409,7 +560867,9 @@ function registerGoalCommand$1(program) {
|
|
|
560409
560867
|
log$13.info(result.message);
|
|
560410
560868
|
});
|
|
560411
560869
|
registerGoalGraphEditCommands(goal);
|
|
560870
|
+
registerGoalMarkCommands(goal);
|
|
560412
560871
|
registerGoalMetricCommands(goal);
|
|
560872
|
+
registerGoalObjectiveCommands(goal);
|
|
560413
560873
|
}
|
|
560414
560874
|
|
|
560415
560875
|
//#endregion
|
package/man/man1/lh.1
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
.\" Code generated by `npm run man:generate`; DO NOT EDIT.
|
|
2
2
|
.\" Manual command details come from the Commander command tree.
|
|
3
|
-
.TH LH 1 "" "@lobehub/cli 0.0.
|
|
3
|
+
.TH LH 1 "" "@lobehub/cli 0.0.95" "User Commands"
|
|
4
4
|
.SH NAME
|
|
5
5
|
lh \- LobeHub CLI \- manage and connect to LobeHub services
|
|
6
6
|
.SH SYNOPSIS
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lobehub/cli",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.95",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/lobehub/lobehub",
|
|
@@ -83,5 +83,5 @@
|
|
|
83
83
|
"access": "public",
|
|
84
84
|
"registry": "https://registry.npmjs.org"
|
|
85
85
|
},
|
|
86
|
-
"gitHead": "
|
|
86
|
+
"gitHead": "1a49740448ebdc0c947dfe7715a3fcd459a372fa"
|
|
87
87
|
}
|