@parcha/agentrun 0.4.29 → 0.4.31
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/lib/cli.mjs +5 -4
- package/lib/commands.generated.mjs +3 -3
- package/lib/local.mjs +2 -2
- package/lib/login.mjs +117 -15
- package/lib/share.mjs +214 -14
- package/lib/target.mjs +10 -0
- package/package.json +2 -2
- package/skills/agentrun-cli/references/commands.md +16 -6
package/lib/cli.mjs
CHANGED
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
// (the harness its owner installs: goose) is harness.mjs's, and service
|
|
7
7
|
// (AgentRun as a launchd or systemd user service) and stop (the AgentRun the installer started in the background) are
|
|
8
8
|
// service.mjs's, doctor (how this AgentRun is set up, read-only) is doctor.mjs's, export (an agent written as a template
|
|
9
|
-
// package folder) is export.mjs's, share (
|
|
10
|
-
// deploy (an agent's package sent to another AgentRun, with its lock, rollback and
|
|
11
|
-
// word runs agentrun-WORD from PATH, as git runs git-WORD (externalOf, below).
|
|
9
|
+
// package folder) is export.mjs's, share (a run's bundle published to a share host with --to, or shown with --dry-run)
|
|
10
|
+
// and share delete are share.mjs's, and deploy (an agent's package sent to another AgentRun, with its lock, rollback and
|
|
11
|
+
// delete) is deploy.mjs's. Any other word runs agentrun-WORD from PATH, as git runs git-WORD (externalOf, below).
|
|
12
12
|
import { spawn } from "node:child_process";
|
|
13
13
|
import { accessSync, closeSync, constants, fstatSync, openSync, readFileSync, statSync } from "node:fs";
|
|
14
14
|
import { homedir, constants as osConstants } from "node:os";
|
|
@@ -75,7 +75,8 @@ const HELP = () => {
|
|
|
75
75
|
"with its token there; AGENTRUN_TOKEN goes to no other address); it is never a flag and never printed. agentrun login",
|
|
76
76
|
"--base-url URL checks a token read from stdin and writes the config file (0600), or on a terminal signs in with a",
|
|
77
77
|
"device code when the server offers one; agentrun login --to URL (or cloud) signs in with a device code when the",
|
|
78
|
-
"server offers one, else reads the token from stdin; agentrun logout removes it.",
|
|
78
|
+
"server offers one, else reads the token from stdin; agentrun logout removes it. agentrun login --share URL keeps a",
|
|
79
|
+
"share host's token in its own entry of the same file, which agentrun share --to and share delete alone read.",
|
|
79
80
|
"Keys and other secrets are set in the console, signed in: the token can't write one, so no command does.",
|
|
80
81
|
"Output: the response's JSON on stdout, a file's bytes as they are; an error's on stderr. Exit codes: 0 ok, 1 the",
|
|
81
82
|
"server refused, 2 bad usage (a bad base URL among it), 3 the server cannot be reached, 4 no token, the token was",
|
|
@@ -3098,7 +3098,7 @@ export const COMMANDS = [
|
|
|
3098
3098
|
"name": "model_key",
|
|
3099
3099
|
"type": "string",
|
|
3100
3100
|
"required": false,
|
|
3101
|
-
"description": "The run's own model key: a key for the model gateway at AGENT_MODEL_BASE_URL, such as a LiteLLM virtual key minted for this run, which every model request tied to the run carries in place of the server's own key (its turns, its subagents, its learned workflows' nodes, a workflow's model nodes, its end-of-run compile turn). AgentRun never mints, lists or revokes it. Sealed into the run's launch bound to the gateway's origin, read at each claim (a reclaim after a crash among them), erased from the store by the write that ends the run's turn;
|
|
3101
|
+
"description": "The run's own model key: a key for the model gateway at AGENT_MODEL_BASE_URL, such as a LiteLLM virtual key minted for this run, which every model request tied to the run carries in place of the server's own key (its turns, its subagents, its learned workflows' nodes, a workflow's model nodes, its end-of-run compile turn, and the learning work it starts: a learned workflow's check replay runs and an audit's shadow run). AgentRun never mints, lists or revokes it. Sealed into the run's launch bound to the gateway's origin, read at each claim (a reclaim after a crash among them), erased from the store by the write that ends the run's turn. A replay or shadow run the run starts while its claim holds the key takes a copy sealed for that run alone, erased by the write that ends that run (or its purge): such a run starts at the run's end, capped at its share of what the run's cap leaves then (none left: it is dropped, nothing spent), and it is listed in the run's `learning_work` until it ends; revoke the key once the run is terminal and `learning_work.state` is not `running`. Never in its saved body, its timeline, its view, a log or an Idempotency-Key's fingerprint (a retry with a new key answers the first request's run, its first key kept). The harness, its tools and its sandbox never see it. 422 `invalid` at `model_key` for a key under 8 characters (no cut could hold it) or one with a character that is not visible ASCII (a space, a control character, anything past ASCII); 422 at `model_key` when the run's model never goes to that gateway (no AGENT_MODEL_BASE_URL here, a native run, or a model routed elsewhere). Absent, the run spends on the server's key for its route; where the server holds none (no AGENT_MODEL_API_KEY, none saved in Settings), a start without it is 422 `model_key_required`, never a failure at its first model call."
|
|
3102
3102
|
},
|
|
3103
3103
|
{
|
|
3104
3104
|
"name": "workflow_first",
|
|
@@ -3578,7 +3578,7 @@ export const COMMANDS = [
|
|
|
3578
3578
|
"name": "model_key",
|
|
3579
3579
|
"type": "string",
|
|
3580
3580
|
"required": false,
|
|
3581
|
-
"description": "The run's own model key: a key for the model gateway at AGENT_MODEL_BASE_URL, such as a LiteLLM virtual key minted for this run, which every model request tied to the run carries in place of the server's own key (its turns, its subagents, its learned workflows' nodes, a workflow's model nodes, its end-of-run compile turn). AgentRun never mints, lists or revokes it. Sealed into the run's launch bound to the gateway's origin, read at each claim (a reclaim after a crash among them), erased from the store by the write that ends the run's turn;
|
|
3581
|
+
"description": "The run's own model key: a key for the model gateway at AGENT_MODEL_BASE_URL, such as a LiteLLM virtual key minted for this run, which every model request tied to the run carries in place of the server's own key (its turns, its subagents, its learned workflows' nodes, a workflow's model nodes, its end-of-run compile turn, and the learning work it starts: a learned workflow's check replay runs and an audit's shadow run). AgentRun never mints, lists or revokes it. Sealed into the run's launch bound to the gateway's origin, read at each claim (a reclaim after a crash among them), erased from the store by the write that ends the run's turn. A replay or shadow run the run starts while its claim holds the key takes a copy sealed for that run alone, erased by the write that ends that run (or its purge): such a run starts at the run's end, capped at its share of what the run's cap leaves then (none left: it is dropped, nothing spent), and it is listed in the run's `learning_work` until it ends; revoke the key once the run is terminal and `learning_work.state` is not `running`. Never in its saved body, its timeline, its view, a log or an Idempotency-Key's fingerprint (a retry with a new key answers the first request's run, its first key kept). The harness, its tools and its sandbox never see it. 422 `invalid` at `model_key` for a key under 8 characters (no cut could hold it) or one with a character that is not visible ASCII (a space, a control character, anything past ASCII); 422 at `model_key` when the run's model never goes to that gateway (no AGENT_MODEL_BASE_URL here, a native run, or a model routed elsewhere). Absent, the run spends on the server's key for its route; where the server holds none (no AGENT_MODEL_API_KEY, none saved in Settings), a start without it is 422 `model_key_required`, never a failure at its first model call. On a message it replaces the run's key from the run's next model request on, a turn still going included, and it is the first key of a run that started on the server's (mint one per turn); a message without one keeps the key the run holds, which is none once its last turn ended: then, where the server holds no key for the run's model, the message is 422 `model_key_required` and nothing is written."
|
|
3582
3582
|
}
|
|
3583
3583
|
],
|
|
3584
3584
|
"hasBody": true,
|
|
@@ -3762,7 +3762,7 @@ export const COMMANDS = [
|
|
|
3762
3762
|
"name": "model_key",
|
|
3763
3763
|
"type": "string",
|
|
3764
3764
|
"required": false,
|
|
3765
|
-
"description": "The run's own model key: a key for the model gateway at AGENT_MODEL_BASE_URL, such as a LiteLLM virtual key minted for this run, which every model request tied to the run carries in place of the server's own key (its turns, its subagents, its learned workflows' nodes, a workflow's model nodes, its end-of-run compile turn). AgentRun never mints, lists or revokes it. Sealed into the run's launch bound to the gateway's origin, read at each claim (a reclaim after a crash among them), erased from the store by the write that ends the run's turn;
|
|
3765
|
+
"description": "The run's own model key: a key for the model gateway at AGENT_MODEL_BASE_URL, such as a LiteLLM virtual key minted for this run, which every model request tied to the run carries in place of the server's own key (its turns, its subagents, its learned workflows' nodes, a workflow's model nodes, its end-of-run compile turn, and the learning work it starts: a learned workflow's check replay runs and an audit's shadow run). AgentRun never mints, lists or revokes it. Sealed into the run's launch bound to the gateway's origin, read at each claim (a reclaim after a crash among them), erased from the store by the write that ends the run's turn. A replay or shadow run the run starts while its claim holds the key takes a copy sealed for that run alone, erased by the write that ends that run (or its purge): such a run starts at the run's end, capped at its share of what the run's cap leaves then (none left: it is dropped, nothing spent), and it is listed in the run's `learning_work` until it ends; revoke the key once the run is terminal and `learning_work.state` is not `running`. Never in its saved body, its timeline, its view, a log or an Idempotency-Key's fingerprint (a retry with a new key answers the first request's run, its first key kept). The harness, its tools and its sandbox never see it. 422 `invalid` at `model_key` for a key under 8 characters (no cut could hold it) or one with a character that is not visible ASCII (a space, a control character, anything past ASCII); 422 at `model_key` when the run's model never goes to that gateway (no AGENT_MODEL_BASE_URL here, a native run, or a model routed elsewhere). Absent, the run spends on the server's key for its route; where the server holds none (no AGENT_MODEL_API_KEY, none saved in Settings), a start without it is 422 `model_key_required`, never a failure at its first model call."
|
|
3766
3766
|
},
|
|
3767
3767
|
{
|
|
3768
3768
|
"name": "workflow_first",
|
package/lib/local.mjs
CHANGED
|
@@ -6,9 +6,9 @@ import { DOCTOR } from "./doctor.mjs";
|
|
|
6
6
|
import { EXPORT } from "./export.mjs";
|
|
7
7
|
import { HARNESS_CHECK, HARNESS_INSTALL, HARNESS_KEYS, HARNESS_SIGN_IN } from "./harness.mjs";
|
|
8
8
|
import { SECRETS } from "./login.mjs";
|
|
9
|
-
import { SHARE } from "./share.mjs";
|
|
9
|
+
import { SHARE, SHARE_DELETE } from "./share.mjs";
|
|
10
10
|
import { OPEN, SERVICE, STOP } from "./service.mjs";
|
|
11
11
|
import { SKILL_INSTALL } from "./skill.mjs";
|
|
12
12
|
import { TEMPLATE_EVAL, TEMPLATE_INSTALL, TEMPLATE_VALIDATE } from "./template.mjs";
|
|
13
13
|
|
|
14
|
-
export const LOCAL = [...SECRETS, SKILL_INSTALL, TEMPLATE_VALIDATE, TEMPLATE_EVAL, TEMPLATE_INSTALL, EXPORT, SHARE, HARNESS_INSTALL, HARNESS_SIGN_IN, HARNESS_KEYS, HARNESS_CHECK, ...SERVICE, STOP, OPEN, DOCTOR, DEPLOY, DEPLOY_ROLLBACK, DEPLOY_DELETE];
|
|
14
|
+
export const LOCAL = [...SECRETS, SKILL_INSTALL, TEMPLATE_VALIDATE, TEMPLATE_EVAL, TEMPLATE_INSTALL, EXPORT, SHARE, SHARE_DELETE, HARNESS_INSTALL, HARNESS_SIGN_IN, HARNESS_KEYS, HARNESS_CHECK, ...SERVICE, STOP, OPEN, DOCTOR, DEPLOY, DEPLOY_ROLLBACK, DEPLOY_DELETE];
|
package/lib/login.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// The commands that take a secret, from stdin and never from the command line: `login` (the token for a server, checked
|
|
2
|
-
// and kept in the config file, or one a device sign-in ends with: device.mjs
|
|
2
|
+
// and kept in the config file, or one a device sign-in ends with: device.mjs; with --share, the token for a share host,
|
|
3
|
+
// kept in the file's own `share` entry, which no other command reads) and `logout`. No command writes a key or any other secret to the server: those are set in
|
|
3
4
|
// the console, signed in, and the server refuses the token on them (secret-writes.ts). A secret is never in argv, so
|
|
4
5
|
// never in shell history or `ps`, and never in any output or error. Each command is `{ command, pathParams, summary,
|
|
5
6
|
// route?, help, options, run }`; cli.mjs runs it with `{ values, args, env, io, cli }`, where `cli` is its own
|
|
@@ -9,7 +10,7 @@ import { chmodSync, closeSync, constants, existsSync, fsyncSync, mkdirSync, open
|
|
|
9
10
|
import { homedir } from "node:os";
|
|
10
11
|
import { dirname, join } from "node:path";
|
|
11
12
|
import { SignInError, capabilitiesOf, deviceOffer, deviceSignIn } from "./device.mjs";
|
|
12
|
-
import { CLOUD_URL, toUrl } from "./target.mjs";
|
|
13
|
+
import { CLOUD_URL, shareBase, toUrl } from "./target.mjs";
|
|
13
14
|
|
|
14
15
|
/** The config file `login` writes and every command reads (cli.mjs's targetOf, which refuses it unless it is a private
|
|
15
16
|
* file of this user's): $XDG_CONFIG_HOME/agentrun/config.json, else
|
|
@@ -127,13 +128,50 @@ function syncFolder(dir) {
|
|
|
127
128
|
|
|
128
129
|
const print = (io, value) => io.stdout.write(`${io.stdout.isTTY ? JSON.stringify(value, null, 2) : JSON.stringify(value)}\n`);
|
|
129
130
|
|
|
130
|
-
/**
|
|
131
|
-
*
|
|
132
|
-
|
|
131
|
+
/** The config file's object as it is, for a login to write its own entry into and keep the rest: `{}` when there is none,
|
|
132
|
+
* or one that is not a private file of this user's or not a JSON object (no command takes such a file: a login writes
|
|
133
|
+
* it whole, as it always has). */
|
|
134
|
+
function configNow(file, cli) {
|
|
135
|
+
try {
|
|
136
|
+
const value = JSON.parse(cli.privateFile(file) ?? "{}");
|
|
137
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
138
|
+
} catch {
|
|
139
|
+
return {};
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** The share host's entry of the config file (`agentrun login --share`): `{ base_url, api_key }` under `share`, beside
|
|
144
|
+
* the server's base_url and api_key. Only share.mjs reads it, and its token goes only to the address it was kept with,
|
|
145
|
+
* so a share host's login never repoints another command and a server's token never reaches a share host. */
|
|
146
|
+
const SHARE_ENTRY = "share";
|
|
147
|
+
|
|
148
|
+
/** The share host's target for `base` (target.mjs shareBase): its API, `<base>/r/v2`, and the token the share entry keeps
|
|
149
|
+
* for that same address. Refused, exit 4, with the command that signs in, when the entry is for another address or
|
|
150
|
+
* there is none; `nothing` ends the refusal (what was not done). */
|
|
151
|
+
export function shareTargetOf(base, env, cli, nothing) {
|
|
133
152
|
const file = configFileOf(env);
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
153
|
+
const how = `agentrun login --share ${base} < TOKEN_FILE`;
|
|
154
|
+
const text = cli.privateFile(file);
|
|
155
|
+
let config;
|
|
156
|
+
try {
|
|
157
|
+
config = text === undefined ? {} : JSON.parse(text);
|
|
158
|
+
} catch {
|
|
159
|
+
throw cli.fail(4, `${file} is not valid JSON; refusing it. ${nothing}`);
|
|
160
|
+
}
|
|
161
|
+
const entry = config?.[SHARE_ENTRY];
|
|
162
|
+
if (entry === null || typeof entry !== "object" || typeof entry.base_url !== "string" || shareBase(entry.base_url) !== base) {
|
|
163
|
+
throw cli.fail(4, `No share host login for ${base}: sign in to it once with \`${how}\` (its token goes in the config file's own share entry, which no other command reads). ${nothing}`);
|
|
164
|
+
}
|
|
165
|
+
if (typeof entry.api_key !== "string" || !entry.api_key || cli.BAD_HEADER.test(entry.api_key) || /\s/.test(entry.api_key)) {
|
|
166
|
+
throw cli.fail(4, `The share entry in ${file} holds no usable token; sign in again with \`${how}\`. ${nothing}`);
|
|
167
|
+
}
|
|
168
|
+
return { base: `${base}/r/v2`, token: entry.api_key };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** `config` written as the config file, whole: beside it under a name no one could have planted (random, opened only if
|
|
172
|
+
* it is new, through no link), then moved over it, so a reader never sees half a file, and it is never wider than
|
|
173
|
+
* 0600. Any failure is said plainly, its code alone, and leaves nothing behind. */
|
|
174
|
+
function writeConfig(file, config, cli) {
|
|
137
175
|
const temp = join(dirname(file), `.config.json.${randomBytes(8).toString("hex")}.tmp`);
|
|
138
176
|
let made = false;
|
|
139
177
|
try {
|
|
@@ -144,7 +182,7 @@ function keep(base, token, env, io, cli) {
|
|
|
144
182
|
try {
|
|
145
183
|
// Every byte, however few one write takes (a full disk, a quota), and on the disk before it takes the old one's
|
|
146
184
|
// place: the rename never puts half a config where a whole one was.
|
|
147
|
-
const bytes = Buffer.from(`${JSON.stringify(
|
|
185
|
+
const bytes = Buffer.from(`${JSON.stringify(config, null, 2)}\n`);
|
|
148
186
|
for (let at = 0; at < bytes.length; ) {
|
|
149
187
|
const wrote = writeSync(fd, bytes, at, bytes.length - at);
|
|
150
188
|
if (!(wrote > 0)) throw Object.assign(new Error("a write wrote nothing"), { code: "EIO" });
|
|
@@ -162,10 +200,47 @@ function keep(base, token, env, io, cli) {
|
|
|
162
200
|
} finally {
|
|
163
201
|
if (made) rmSync(temp, { force: true });
|
|
164
202
|
}
|
|
165
|
-
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** `{ base_url, api_key }` in the config file (under `entry`, the share host's, when given), every other entry of it
|
|
206
|
+
* kept, then its path printed: what every login ends with, whichever way the token came. */
|
|
207
|
+
function keep(base, token, env, io, cli, entry) {
|
|
208
|
+
const file = configFileOf(env);
|
|
209
|
+
const login = { base_url: base, api_key: token };
|
|
210
|
+
writeConfig(file, { ...configNow(file, cli), ...(entry ? { [entry]: login } : login) }, cli);
|
|
211
|
+
print(io, entry ? { [entry]: { base_url: base }, config_file: file } : { base_url: base, config_file: file });
|
|
166
212
|
return 0;
|
|
167
213
|
}
|
|
168
214
|
|
|
215
|
+
/** `agentrun login --share URL < FILE`: a share host's token, read from stdin (a share host offers no device sign-in),
|
|
216
|
+
* checked against its own list of the token's shares (GET <URL>/r/v2/shares), and kept in the share entry. */
|
|
217
|
+
async function shareLogin(given, env, io, cli) {
|
|
218
|
+
const base = shareBase(given);
|
|
219
|
+
if (base === undefined) throw cli.fail(2, "--share must be a share host's http or https address with no query, fragment, user or password (an agentrun-serve --share-host).");
|
|
220
|
+
const token = (await readSecret(io, cli.fail, "The share host's token", `agentrun login --share ${base} < FILE`)).trim();
|
|
221
|
+
if (!token) throw cli.fail(2, "stdin holds no token: one line, the token alone.");
|
|
222
|
+
if (cli.BAD_HEADER.test(token) || /\s/.test(token)) throw cli.fail(4, "The token is not a valid header value (it holds a space, a line break, a control character or a character outside Latin-1); nothing was written.");
|
|
223
|
+
const client = secretClient(cli.clientOf, { base: `${base}/r/v2`, token }, io, [token]);
|
|
224
|
+
client.unreachable = (error) => cli.fail(3, `Cannot reach the share host at ${base} (${error?.cause?.code ?? error?.name}); nothing was written.`);
|
|
225
|
+
const checked = await cli.request(client, { method: "GET", path: "/shares", headers: {} });
|
|
226
|
+
if (checked.code !== undefined) {
|
|
227
|
+
client.io.stderr.write(`${checked.code === 4 ? `The share host at ${base} refused the token` : `The share host at ${base} did not take the check`}; nothing was written.\n`);
|
|
228
|
+
return checked.code;
|
|
229
|
+
}
|
|
230
|
+
// The answer must be the token's own list of shares: a 200 from another route or another kind of server checks nothing.
|
|
231
|
+
let list;
|
|
232
|
+
try {
|
|
233
|
+
list = JSON.parse(checked.bytes.toString("utf8"));
|
|
234
|
+
} catch {
|
|
235
|
+
list = undefined;
|
|
236
|
+
}
|
|
237
|
+
if (!Array.isArray(list?.shares) || list.quota === null || typeof list.quota !== "object") {
|
|
238
|
+
client.io.stderr.write(`${base} answered the check with ${checked.response.status}, not a share host's list of shares; nothing was written.\n`);
|
|
239
|
+
return 1;
|
|
240
|
+
}
|
|
241
|
+
return keep(base, token, env, io, cli, SHARE_ENTRY);
|
|
242
|
+
}
|
|
243
|
+
|
|
169
244
|
export const LOGIN = {
|
|
170
245
|
command: ["login"],
|
|
171
246
|
pathParams: [],
|
|
@@ -174,10 +249,15 @@ export const LOGIN = {
|
|
|
174
249
|
help: [
|
|
175
250
|
["--to URL|cloud", `the server, /v2 added when its address's path holds none; cloud is AgentRun cloud (AGENTRUN_CLOUD_URL, else ${CLOUD_URL}). When its capabilities offer a device sign-in, prints an address and a code to approve there, else reads the token from stdin`],
|
|
176
251
|
["--base-url URL", "the server, /v2 added as for --to (default AGENTRUN_BASE_URL); the token from stdin, or on a terminal a device sign-in when the server offers one"],
|
|
252
|
+
["--share URL", "a share host (agentrun share RUN --to URL publishes there): its token from stdin, checked at URL/r/v2/shares and kept in the config file's own share entry; the server's address and token stay as they are"],
|
|
177
253
|
["(stdin)", "the token: agentrun login --base-url URL < FILE"],
|
|
178
254
|
],
|
|
179
|
-
options: { to: { type: "string" } },
|
|
255
|
+
options: { to: { type: "string" }, share: { type: "string" } },
|
|
180
256
|
async run({ values, env, io, cli }) {
|
|
257
|
+
if (values.share !== undefined) {
|
|
258
|
+
if (values.to !== undefined || values["base-url"] !== undefined) throw cli.fail(2, "agentrun login --share signs in to a share host alone: --share, or --to / --base-url for a server, not both.");
|
|
259
|
+
return shareLogin(values.share, env, io, cli);
|
|
260
|
+
}
|
|
181
261
|
if (values.to !== undefined && values["base-url"] !== undefined) throw cli.fail(2, "agentrun login takes the server once: --to or --base-url, not both.");
|
|
182
262
|
const named = values.to !== undefined;
|
|
183
263
|
const flagged = values["base-url"] !== undefined;
|
|
@@ -222,11 +302,33 @@ export const LOGIN = {
|
|
|
222
302
|
export const LOGOUT = {
|
|
223
303
|
command: ["logout"],
|
|
224
304
|
pathParams: [],
|
|
225
|
-
summary: "Remove the config file login wrote, its address and token",
|
|
226
|
-
help: [],
|
|
227
|
-
options: {},
|
|
228
|
-
async run({ env, io }) {
|
|
305
|
+
summary: "Remove the config file login wrote, its address and token (with --share, the share host's entry alone)",
|
|
306
|
+
help: [["--share", "remove the share host's entry alone (agentrun login --share); the server's address and token stay"]],
|
|
307
|
+
options: { share: { type: "boolean" } },
|
|
308
|
+
async run({ values, env, io, cli }) {
|
|
229
309
|
const file = configFileOf(env);
|
|
310
|
+
if (values.share) {
|
|
311
|
+
// Read strictly: a file refused or unreadable as a config may still hold the share token, so it is said, never
|
|
312
|
+
// taken for an empty one.
|
|
313
|
+
const text = cli.privateFile(file);
|
|
314
|
+
let config;
|
|
315
|
+
try {
|
|
316
|
+
config = JSON.parse(text ?? "{}");
|
|
317
|
+
} catch {
|
|
318
|
+
config = undefined;
|
|
319
|
+
}
|
|
320
|
+
if (config === null || typeof config !== "object" || Array.isArray(config)) throw cli.fail(4, `${file} is not a config file login writes (a JSON object); refusing it. Nothing was removed.`);
|
|
321
|
+
const removed = Object.hasOwn(config, SHARE_ENTRY);
|
|
322
|
+
if (removed) {
|
|
323
|
+
delete config[SHARE_ENTRY];
|
|
324
|
+
// The rest written back as a login writes it; nothing left, no file.
|
|
325
|
+
const { base_url: base, api_key: token, ...rest } = config;
|
|
326
|
+
if (base === undefined && token === undefined && !Object.keys(rest).length) rmSync(file, { force: true });
|
|
327
|
+
else writeConfig(file, config, cli);
|
|
328
|
+
}
|
|
329
|
+
print(io, { config_file: file, share: { removed } });
|
|
330
|
+
return 0;
|
|
331
|
+
}
|
|
230
332
|
const removed = existsSync(file);
|
|
231
333
|
rmSync(file, { force: true });
|
|
232
334
|
print(io, { config_file: file, removed });
|
package/lib/share.mjs
CHANGED
|
@@ -8,26 +8,39 @@
|
|
|
8
8
|
// the bundle already cut, no more.
|
|
9
9
|
// `--out FILE` writes the bundle's bytes as the server sent them (0600, never over a file); `--restore` asks the server to
|
|
10
10
|
// keep restorable cuts in place (a stateless re-export); `--expect SHA` refuses a bundle whose sha256 is another, the
|
|
11
|
-
// guard the page's publish command carries.
|
|
12
|
-
//
|
|
11
|
+
// guard the page's publish command carries.
|
|
12
|
+
// `--to URL` publishes: the bundle, exported again with those restores and checked against --expect before anything is
|
|
13
|
+
// sent, is posted byte for byte to the share host at URL (POST <URL>/r/v2/shares, agentrun-serve --share-host) with the
|
|
14
|
+
// token `agentrun login --share URL` kept for that address alone, and the share's URL (<URL>/r/<id>) printed. A host's
|
|
15
|
+
// refusal is said in this CLI's words, with its counts by rule, never its message or a value. `agentrun share delete
|
|
16
|
+
// URL` takes a share down with the same token; its page then answers 410. Without --to, --dry-run or --out, share exits
|
|
17
|
+
// 2 and sends nothing.
|
|
13
18
|
import { createHash } from "node:crypto";
|
|
14
19
|
import { closeSync, existsSync, openSync, unlinkSync, writeSync } from "node:fs";
|
|
15
20
|
import { homedir } from "node:os";
|
|
16
21
|
import { join, resolve } from "node:path";
|
|
22
|
+
import { shareTargetOf } from "./login.mjs";
|
|
17
23
|
import { canOpen, launchOpener } from "./service.mjs";
|
|
24
|
+
import { shareBase } from "./target.mjs";
|
|
18
25
|
|
|
19
26
|
/** start.mjs START_LOG, named again here: start.mjs imports cli.mjs, which imports this module (share.test.mjs writes
|
|
20
27
|
* the start log at start.mjs's name, so the two can't part). */
|
|
21
28
|
const START_LOG = "start.log";
|
|
22
29
|
|
|
23
|
-
/** Exits: 0 shown
|
|
24
|
-
* what it names (or not the one --expect names)
|
|
25
|
-
* restore it refuses, a bundle over its cap, an --out file that exists,
|
|
30
|
+
/** Exits: 0 shown, written or published; 1 the server failed the export, refused it as withheld, or sent a bundle that
|
|
31
|
+
* is not what it names (or not the one --expect names), or the share host refused the bundle; 2 bad usage, a run the
|
|
32
|
+
* server does not have or that has not ended, a restore it refuses, a bundle over its cap, an --out file that exists,
|
|
33
|
+
* or none of --to, --dry-run and --out; 3 the share host not reached; 4 no share login for the host, or its token
|
|
34
|
+
* refused. */
|
|
26
35
|
const REFUSED = 2;
|
|
27
36
|
const FORMAT = "agentrun.share/1";
|
|
28
37
|
const CUT_ID = /^c_[0-9a-f]{16}$/;
|
|
29
38
|
const SHA256 = /^[0-9a-f]{64}$/;
|
|
30
39
|
const NOTHING = "Nothing was written or opened.";
|
|
40
|
+
const UNSENT = "Nothing was sent.";
|
|
41
|
+
const UNSTORED = "Nothing was stored.";
|
|
42
|
+
/** A share's id on a share host (share-host.ts SHARE_ID): 128 random bits in base32. */
|
|
43
|
+
const SHARE_ID = /^[a-z2-7]{26}$/;
|
|
31
44
|
/** The order kinds are summed up in: those a preview may restore, then those it never does. */
|
|
32
45
|
const KINDS = ["label", "personal", "jev", "format", "held", "dump"];
|
|
33
46
|
|
|
@@ -126,26 +139,207 @@ function writeNew(path, bytes, files) {
|
|
|
126
139
|
}
|
|
127
140
|
}
|
|
128
141
|
|
|
142
|
+
/** A share host's answer's error object, or {}: read for its code and details alone, never its message. */
|
|
143
|
+
function hostErrorOf(bytes) {
|
|
144
|
+
try {
|
|
145
|
+
const error = JSON.parse(bytes.toString("utf8"))?.error;
|
|
146
|
+
return error !== null && typeof error === "object" && !Array.isArray(error) ? error : {};
|
|
147
|
+
} catch {
|
|
148
|
+
return {};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const detailsOf = (error) => (error.details !== null && typeof error.details === "object" && !Array.isArray(error.details) ? error.details : {});
|
|
152
|
+
/** An error's code when it is one (lowercase letters and _), else undefined. */
|
|
153
|
+
const codeOf = (error) => (typeof error.code === "string" && /^[a-z_]{1,40}$/.test(error.code) ? error.code : undefined);
|
|
154
|
+
const whole = (n) => (Number.isSafeInteger(n) && n >= 0 ? n : undefined);
|
|
155
|
+
|
|
156
|
+
/** The rule names a share host's counts are printed with: those the bundle's own records name (its cuts' and restores',
|
|
157
|
+
* the scrubber's ids as the server that cut it knows them). Any other name in a host's answer is counted as unknown: a
|
|
158
|
+
* name is printed only when this side knew it already, so no answer of a host's prints a value. */
|
|
159
|
+
const rulesOf = (bundle) => new Set([...(Array.isArray(bundle.cuts) ? bundle.cuts : []), ...(Array.isArray(bundle.restored) ? bundle.restored : [])].map((record) => record?.rule).filter((rule) => typeof rule === "string"));
|
|
160
|
+
|
|
161
|
+
/** Counts by rule from a host's answer, `rule n, ...`, in its order with unknown last; undefined when it holds none. */
|
|
162
|
+
function countsLine(value, known) {
|
|
163
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return undefined;
|
|
164
|
+
const counts = new Map();
|
|
165
|
+
let unknown = 0;
|
|
166
|
+
for (const [rule, n] of Object.entries(value)) {
|
|
167
|
+
if (!whole(n)) continue;
|
|
168
|
+
if (known.has(rule)) counts.set(rule, (counts.get(rule) ?? 0) + n);
|
|
169
|
+
else unknown += n;
|
|
170
|
+
}
|
|
171
|
+
if (unknown) counts.set("unknown", unknown);
|
|
172
|
+
return counts.size ? [...counts].map(([rule, n]) => `${rule} ${n}`).join(", ") : undefined;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** What a 422 from a share host says, by its reason. */
|
|
176
|
+
const SCREENED = {
|
|
177
|
+
scrub_found: "its scrub found values no restore declares",
|
|
178
|
+
restored_unmatched: "restored records of it name values its scrub does not find there (a stale bundle, or a scrubber of another version)",
|
|
179
|
+
scrub_failed: "its scrub of it failed, and the scrub fails closed",
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/** A share host's refusal of an upload: its exit, and this CLI's words, with counts and numbers from its details and its
|
|
183
|
+
* code alone, never its message or a value of it. */
|
|
184
|
+
function uploadRefusal(response, bytes, { host, known, run }) {
|
|
185
|
+
const error = hostErrorOf(bytes);
|
|
186
|
+
const details = detailsOf(error);
|
|
187
|
+
const code = codeOf(error);
|
|
188
|
+
const login = `agentrun login --share ${host} < TOKEN_FILE`;
|
|
189
|
+
switch (response.status) {
|
|
190
|
+
case 401:
|
|
191
|
+
return [4, `The share host at ${host} refused the token (401). Sign in to it again: ${login}. ${UNSTORED}`];
|
|
192
|
+
case 403: {
|
|
193
|
+
if (details.reason !== "share_quota") return [4, `The share host at ${host} refused the token (403${code ? ` ${code}` : ""}). Sign in to it again: ${login}. ${UNSTORED}`];
|
|
194
|
+
const [used, limit] = [whole(details.used), whole(details.limit)];
|
|
195
|
+
return [1, `The share host at ${host}: this token keeps ${used ?? "its most"} shares${limit === undefined ? "" : `, its most (${limit})`}. Take one down to make room: agentrun share delete URL. ${UNSTORED}`];
|
|
196
|
+
}
|
|
197
|
+
case 422: {
|
|
198
|
+
const reason = Object.hasOwn(SCREENED, details.reason) ? details.reason : undefined;
|
|
199
|
+
const found = countsLine(details.counts, known);
|
|
200
|
+
const unmatched = countsLine(details.unmatched, known);
|
|
201
|
+
const said = [reason ? SCREENED[reason] : "it refused what the bundle holds", ...(found ? [`by rule: ${found}`] : [])].join(", ");
|
|
202
|
+
const page = `agentrun share ${run} --dry-run`;
|
|
203
|
+
const advice = reason === "scrub_found" ? ` A restore keeps a label, personal or jev cut, never a key format, a held value or a dump: open the page again (${page}) and publish with those cuts in place.` : reason === "restored_unmatched" ? ` Open the page again (${page}) and use the command it shows now.` : "";
|
|
204
|
+
return [1, `The share host refused the bundle (422 ${reason ?? code ?? "input_invalid"}): ${said}${unmatched ? `; restored records it could not match, by rule: ${unmatched}` : ""}.${advice} Its answer is not shown. ${UNSTORED}`];
|
|
205
|
+
}
|
|
206
|
+
case 400:
|
|
207
|
+
return [1, `The share host refused the bundle (400): it does not take this bundle's format or shape (an AgentRun of another version on one side). Its answer is not shown. ${UNSTORED}`];
|
|
208
|
+
case 413: {
|
|
209
|
+
const [strings, limit] = [whole(details.strings), whole(details.limit)];
|
|
210
|
+
if (details.reason === "share_strings" && strings !== undefined && limit !== undefined) return [1, `The share host refused the bundle (413): it holds ${strings} distinct strings, over the ${limit} the host scrubs one by one. ${UNSTORED}`];
|
|
211
|
+
return [1, `The share host refused the bundle (413): it is over the host's size cap. ${UNSTORED}`];
|
|
212
|
+
}
|
|
213
|
+
case 404:
|
|
214
|
+
return [1, `${host} answered 404 at /r/v2/shares: no share host there. --to takes a share host's address (an agentrun-serve --share-host). ${UNSTORED}`];
|
|
215
|
+
default:
|
|
216
|
+
return [response.status === 501 ? 5 : 1, `The share host refused it: ${response.status}${code ? ` ${code}` : ""}. Its answer is not shown. ${UNSTORED}`];
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** A client of the share host's API at `target` (login.mjs shareTargetOf), its refusals said by `refusal`. */
|
|
221
|
+
function hostClientOf(cli, io, host, target, refusal) {
|
|
222
|
+
const client = cli.clientOf(target, io);
|
|
223
|
+
client.unreachable = (error) => cli.fail(3, `Cannot reach the share host at ${host} (${error?.cause?.code ?? error?.name}).`);
|
|
224
|
+
client.refused = (response, bytes) => {
|
|
225
|
+
const [exit, said] = refusal(response, bytes);
|
|
226
|
+
if (said) io.stderr.write(`${said}\n`);
|
|
227
|
+
return exit;
|
|
228
|
+
};
|
|
229
|
+
return client;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** `share RUN --to`: the checked bundle's bytes posted to the share host, and the share's URL printed. */
|
|
233
|
+
async function publishTo(cli, io, { host, target, bytes, bundle, summary }) {
|
|
234
|
+
const known = rulesOf(bundle);
|
|
235
|
+
const client = hostClientOf(cli, io, host, target, (response, body) => uploadRefusal(response, body, { host, known, run: summary.run }));
|
|
236
|
+
// Who can open it, said before anything is sent: an upload that goes through is public at once.
|
|
237
|
+
io.stderr.write(`Publishing to ${host}: a share is public, anyone with the link can open it.\n`);
|
|
238
|
+
const got = await cli.request(client, { method: "POST", path: "/shares", headers: {}, body: bytes, type: "application/json" });
|
|
239
|
+
if (got.code !== undefined) return got.code;
|
|
240
|
+
let answer;
|
|
241
|
+
try {
|
|
242
|
+
answer = JSON.parse(got.bytes.toString("utf8"));
|
|
243
|
+
} catch {
|
|
244
|
+
answer = undefined;
|
|
245
|
+
}
|
|
246
|
+
if (typeof answer?.id !== "string" || !SHARE_ID.test(answer.id)) {
|
|
247
|
+
io.stderr.write(`The share host at ${host} answered ${got.response.status} with no share id; whether it stored the bundle is unknown.\n`);
|
|
248
|
+
return 1;
|
|
249
|
+
}
|
|
250
|
+
const { id } = answer;
|
|
251
|
+
// The host's URL for it (on its public address), when it is one for this id; else the one its layout makes.
|
|
252
|
+
const url = typeof answer.url === "string" && /^https?:\/\//.test(answer.url) && URL.canParse(answer.url) && new URL(answer.url).pathname.endsWith(`/r/${id}`) && !/[?#]/.test(answer.url) ? answer.url : `${host}/r/${id}`;
|
|
253
|
+
// Readers get the host's public link; the delete goes to the address that took the upload, which the share login
|
|
254
|
+
// is kept for (a host's public address may be another, and shareTargetOf takes the login's own address alone).
|
|
255
|
+
const takeDown = `agentrun share delete ${host}/r/${id}`;
|
|
256
|
+
const lines = linesOf(summary);
|
|
257
|
+
lines.push(`Shared: ${url}`, `Take it down with: ${takeDown}`);
|
|
258
|
+
const unchecked = countsLine(answer.restored_unchecked, known);
|
|
259
|
+
if (unchecked) lines.push(`Restores of Jev's cuts the host took unchecked (it runs no Jev), by rule: ${unchecked}.`);
|
|
260
|
+
if (answer.sha256 !== summary.sha256) {
|
|
261
|
+
lines.push(`The share host names another sha256 for it than the ${summary.sha256} sent: take it down (${takeDown}) and ask the host's operator.`);
|
|
262
|
+
io.stderr.write(`${lines.join("\n")}\n`);
|
|
263
|
+
client.print({ ...summary, url, id });
|
|
264
|
+
return 1;
|
|
265
|
+
}
|
|
266
|
+
io.stderr.write(`${lines.join("\n")}\n`);
|
|
267
|
+
client.print({ ...summary, url, id });
|
|
268
|
+
return 0;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** A share's URL, `<host>/r/<id>`: its host's address (target.mjs shareBase) and its id; undefined for any other. */
|
|
272
|
+
function shareUrlOf(given) {
|
|
273
|
+
const url = typeof given === "string" && URL.canParse(given) ? new URL(given) : undefined;
|
|
274
|
+
if (!url || !/^https?:$/.test(url.protocol) || /[?#]/.test(given) || url.username || url.password) return undefined;
|
|
275
|
+
const found = /^(.*)\/r\/([a-z2-7]{26})\/?$/.exec(url.pathname);
|
|
276
|
+
const host = found && shareBase(`${url.origin}${found[1]}`);
|
|
277
|
+
return host ? { host, id: found[2] } : undefined;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** `agentrun share delete URL` (cli.mjs LOCAL): a share taken down, with the share host's login. */
|
|
281
|
+
export const SHARE_DELETE = {
|
|
282
|
+
command: ["share", "delete"],
|
|
283
|
+
pathParams: ["url"],
|
|
284
|
+
route: "DELETE <host>/r/v2/shares/{id}, on the share host the URL is on",
|
|
285
|
+
summary: "Take a published share down with the share host's login: its page and bundle then answer 410",
|
|
286
|
+
help: [["URL", "the share's URL, as share --to printed it: <share host>/r/<id>"]],
|
|
287
|
+
options: {},
|
|
288
|
+
async run({ args, env, io, cli }) {
|
|
289
|
+
const parsed = shareUrlOf(args[0]);
|
|
290
|
+
if (!parsed) throw cli.fail(REFUSED, "agentrun share delete takes a share's URL, as agentrun share --to printed it: <share host>/r/<id> (http or https, with no query, fragment, user or password).");
|
|
291
|
+
const { host, id } = parsed;
|
|
292
|
+
const url = `${host}/r/${id}`;
|
|
293
|
+
const UNDELETED = "Nothing was deleted.";
|
|
294
|
+
const target = shareTargetOf(host, env, cli, UNDELETED);
|
|
295
|
+
let already = false;
|
|
296
|
+
const client = hostClientOf(cli, io, host, target, (response, bytes) => {
|
|
297
|
+
const code = codeOf(hostErrorOf(bytes));
|
|
298
|
+
switch (response.status) {
|
|
299
|
+
case 410:
|
|
300
|
+
already = true;
|
|
301
|
+
return [0];
|
|
302
|
+
case 404:
|
|
303
|
+
return [1, `The share host at ${host} has no share ${id}. ${UNDELETED}`];
|
|
304
|
+
case 403:
|
|
305
|
+
return [4, `The share ${id} was made with another token; only that token deletes it. ${UNDELETED}`];
|
|
306
|
+
case 401:
|
|
307
|
+
return [4, `The share host at ${host} refused the token (401). Sign in to it again: agentrun login --share ${host} < TOKEN_FILE. ${UNDELETED}`];
|
|
308
|
+
default:
|
|
309
|
+
return [response.status === 501 ? 5 : 1, `The share host refused it: ${response.status}${code ? ` ${code}` : ""}. Its answer is not shown. ${UNDELETED}`];
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
const got = await cli.request(client, { method: "DELETE", path: `/shares/${id}`, headers: {} });
|
|
313
|
+
if (got.code !== undefined && !already) return got.code;
|
|
314
|
+
io.stderr.write(already ? `${url} was already deleted: its page answers 410 Gone.\n` : `Deleted ${url}: its page now answers 410 Gone.\n`);
|
|
315
|
+
client.print({ url, id, deleted: true });
|
|
316
|
+
return 0;
|
|
317
|
+
},
|
|
318
|
+
};
|
|
319
|
+
|
|
129
320
|
/** The command, given what opens a browser here and the fs calls `--out` makes (a test's seam); SHARE (below) binds it
|
|
130
321
|
* to this machine. */
|
|
131
322
|
export function shareOf({ launch = launchOpener, platform = process.platform, files = { openSync, writeSync, closeSync, unlinkSync } } = {}) {
|
|
132
323
|
return {
|
|
133
324
|
command: ["share"],
|
|
134
325
|
pathParams: ["run"],
|
|
135
|
-
route: "GET /runs/{run_id}/export; the page
|
|
136
|
-
summary: "
|
|
326
|
+
route: "GET /runs/{run_id}/export; with --to, then POST <host>/r/v2/shares on the share host; the page --dry-run opens is <origin>/share/{run_id}",
|
|
327
|
+
summary: "Share an ended run: publish its bundle to a share host (--to), or show what that would send (--dry-run: its bundle checked and summed up, its page opened here) or write it (--out)",
|
|
137
328
|
help: [
|
|
138
329
|
["RUN", "the run's id: an ended run"],
|
|
330
|
+
["--to URL", "publish: the bundle, exported again with --restore and checked against --expect before anything is sent, posted to the share host at URL with the token agentrun login --share URL kept; prints the share's URL"],
|
|
139
331
|
["--dry-run", "fetch the run's share bundle, print its summary and open its preview page; nothing is sent anywhere"],
|
|
140
332
|
["--out FILE", "write the bundle as the server sent it, byte for byte (0600, never over a file); alone, it opens nothing"],
|
|
141
333
|
["--restore IDS", "cut ids to keep in place, comma-separated (restorable ones only: a label, personal or jev cut)"],
|
|
142
334
|
["--expect SHA", "refuse a bundle whose sha256 is another (the page's publish command carries it)"],
|
|
143
335
|
["--no-open", "open no browser; print the page's link"],
|
|
144
336
|
],
|
|
145
|
-
options: { "dry-run": { type: "boolean" }, out: { type: "string" }, restore: { type: "string" }, expect: { type: "string" }, open: { type: "boolean" } },
|
|
337
|
+
options: { "dry-run": { type: "boolean" }, out: { type: "string" }, to: { type: "string" }, restore: { type: "string" }, expect: { type: "string" }, open: { type: "boolean" } },
|
|
146
338
|
async run({ values, args, env, io, cli }) {
|
|
147
339
|
const [run] = args;
|
|
148
|
-
|
|
340
|
+
const publish = values.to !== undefined;
|
|
341
|
+
if (!publish && !values["dry-run"] && values.out === undefined) throw cli.fail(REFUSED, `agentrun share RUN takes --to URL (publish it to a share host), --dry-run (preview its page) or --out FILE (write its bundle). ${UNSENT}`);
|
|
342
|
+
if (publish && (values["dry-run"] || values.out !== undefined)) throw cli.fail(REFUSED, `--to publishes the bundle; --dry-run and --out show or write it: one at a time. ${UNSENT}`);
|
|
149
343
|
if (["", ".", ".."].includes(run)) throw cli.fail(REFUSED, "RUN may not be empty, . or ..");
|
|
150
344
|
// Ids only, checked here: a value pasted in their place is never sent, and never echoed.
|
|
151
345
|
const restore = values.restore === undefined ? [] : values.restore.split(",").map((id) => id.trim()).filter(Boolean);
|
|
@@ -154,6 +348,11 @@ export function shareOf({ launch = launchOpener, platform = process.platform, fi
|
|
|
154
348
|
if (expect !== undefined && !SHA256.test(expect)) throw cli.fail(REFUSED, "--expect takes a sha256: 64 hex digits, as the page shows it.");
|
|
155
349
|
const out = values.out === undefined ? undefined : resolve(values.out);
|
|
156
350
|
if (out !== undefined && existsSync(out)) throw cli.fail(REFUSED, `${out} already exists: --out writes a new file. Nothing was asked of the server.`);
|
|
351
|
+
// The share host and its login before the export: a publish that can't be sent asks nothing of the server.
|
|
352
|
+
const host = publish ? shareBase(values.to) : undefined;
|
|
353
|
+
if (publish && host === undefined) throw cli.fail(REFUSED, `--to must be a share host's http or https address with no query, fragment, user or password (an agentrun-serve --share-host). ${UNSENT}`);
|
|
354
|
+
const hostTarget = publish ? shareTargetOf(host, env, cli, UNSENT) : undefined;
|
|
355
|
+
const nothing = publish ? UNSENT : NOTHING;
|
|
157
356
|
|
|
158
357
|
const target = cli.targetOf(values, env);
|
|
159
358
|
const client = cli.clientOf(target, io);
|
|
@@ -166,23 +365,24 @@ export function shareOf({ launch = launchOpener, platform = process.platform, fi
|
|
|
166
365
|
};
|
|
167
366
|
const query = restore.length ? `?${new URLSearchParams({ restore: restore.join(",") })}` : "";
|
|
168
367
|
const { bytes, code } = await cli.request(client, { method: "GET", path: `/runs/${encodeURIComponent(run)}/export${query}` });
|
|
169
|
-
if (code !== undefined) return io.stderr.write(`${
|
|
368
|
+
if (code !== undefined) return io.stderr.write(`${nothing}\n`), code;
|
|
170
369
|
|
|
171
370
|
// The bundle is checked before anything of it is used: its format, and the sha256 it names over its own content.
|
|
172
371
|
let bundle;
|
|
173
372
|
try {
|
|
174
373
|
bundle = JSON.parse(bytes.toString("utf8"));
|
|
175
374
|
} catch {
|
|
176
|
-
throw cli.fail(1, `The server's answer is not JSON. ${
|
|
375
|
+
throw cli.fail(1, `The server's answer is not JSON. ${nothing}`);
|
|
177
376
|
}
|
|
178
|
-
if (bundle === null || typeof bundle !== "object" || Array.isArray(bundle) || bundle.format !== FORMAT || typeof bundle.sha256 !== "string") throw cli.fail(1, `The server's answer is not an ${FORMAT} bundle. ${
|
|
377
|
+
if (bundle === null || typeof bundle !== "object" || Array.isArray(bundle) || bundle.format !== FORMAT || typeof bundle.sha256 !== "string") throw cli.fail(1, `The server's answer is not an ${FORMAT} bundle. ${nothing}`);
|
|
179
378
|
const { sha256: named, ...content } = bundle;
|
|
180
|
-
if (createHash("sha256").update(stable(content)).digest("hex") !== named) throw cli.fail(1, `The server's bundle does not hash to the sha256 it names. ${
|
|
379
|
+
if (createHash("sha256").update(stable(content)).digest("hex") !== named) throw cli.fail(1, `The server's bundle does not hash to the sha256 it names. ${nothing}`);
|
|
181
380
|
if (expect !== undefined && named !== expect) {
|
|
182
|
-
throw cli.fail(1, `The bundle's sha256 is ${named}, not the expected ${expect}: the run's export changed since the page showed it (its learning, a restore chosen there). Open the page again (agentrun share ${run} --dry-run) and use the command it shows now. ${
|
|
381
|
+
throw cli.fail(1, `The bundle's sha256 is ${named}, not the expected ${expect}: the run's export changed since the page showed it (its learning, a restore chosen there). Open the page again (agentrun share ${run} --dry-run) and use the command it shows now. ${nothing}`);
|
|
183
382
|
}
|
|
184
383
|
|
|
185
384
|
const summary = summaryOf(run, bundle, bytes);
|
|
385
|
+
if (publish) return publishTo(cli, io, { host, target: hostTarget, bytes, bundle, summary });
|
|
186
386
|
const origin = new URL(target.base).origin;
|
|
187
387
|
// The page opens with the same restores, so its first read is this bundle.
|
|
188
388
|
const page = `${origin}/share/${encodeURIComponent(run)}${query}`;
|
package/lib/target.mjs
CHANGED
|
@@ -31,3 +31,13 @@ export function checkedBase(given) {
|
|
|
31
31
|
export function toUrl(given, env) {
|
|
32
32
|
return apiBase(given === "cloud" ? env.AGENTRUN_CLOUD_URL || CLOUD_URL : given);
|
|
33
33
|
}
|
|
34
|
+
|
|
35
|
+
/** A share host's address (`agentrun login --share`, `agentrun share --to`): http or https with no query, fragment, user
|
|
36
|
+
* or password, as checkedBase takes a server's, written as its origin and path without trailing slashes. A link under
|
|
37
|
+
* its /r/ (or its API's /r/v2) names the same host: a share host serves everything under /r/, so the address is what
|
|
38
|
+
* comes before it. Undefined for any other; the caller refuses it without echoing it. */
|
|
39
|
+
export function shareBase(given) {
|
|
40
|
+
const url = typeof given === "string" && URL.canParse(given) ? new URL(given) : undefined;
|
|
41
|
+
if (!url || !/^https?:$/.test(url.protocol) || /[?#]/.test(given) || url.username || url.password) return undefined;
|
|
42
|
+
return `${url.origin}${url.pathname.replace(/\/+$/, "").replace(/\/r(?:\/v2)?$/, "")}`;
|
|
43
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcha/agentrun",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.31",
|
|
4
4
|
"description": "AgentRun, the open-source platform that supercharges your agents (self-improvement, durable execution and a control plane on top of your favorite agent harness), on this machine with one command: npx @parcha/agentrun",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"NOTICE"
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@parcha/agentrun-serve": "0.1.0-beta.
|
|
20
|
+
"@parcha/agentrun-serve": "0.1.0-beta.63"
|
|
21
21
|
},
|
|
22
22
|
"scripts": {
|
|
23
23
|
"test": "node --test 'test/*.test.mjs'"
|
|
@@ -873,15 +873,18 @@ Route: `GET /capabilities, then its device sign-in; else GET /agents?limit=1, to
|
|
|
873
873
|
|
|
874
874
|
- `--to URL|cloud` (string, this CLI's own): the server, /v2 added when its address's path holds none; cloud is AgentRun cloud (AGENTRUN_CLOUD_URL, else https://api.agentrun.ai). When its capabilities offer a device sign-in, prints an address and a code to approve there, else reads the token from stdin
|
|
875
875
|
- `--base-url URL` (string, this CLI's own): the server, /v2 added as for --to (default AGENTRUN_BASE_URL); the token from stdin, or on a terminal a device sign-in when the server offers one
|
|
876
|
+
- `--share URL` (string, this CLI's own): a share host (agentrun share RUN --to URL publishes there): its token from stdin, checked at URL/r/v2/shares and kept in the config file's own share entry; the server's address and token stay as they are
|
|
876
877
|
|
|
877
878
|
## logout
|
|
878
879
|
|
|
879
880
|
### `agentrun logout`
|
|
880
881
|
|
|
881
|
-
Remove the config file login wrote, its address and token.
|
|
882
|
+
Remove the config file login wrote, its address and token (with --share, the share host's entry alone).
|
|
882
883
|
|
|
883
884
|
Route: none, it runs on this machine.
|
|
884
885
|
|
|
886
|
+
- `--share` (boolean, this CLI's own): remove the share host's entry alone (agentrun login --share); the server's address and token stay
|
|
887
|
+
|
|
885
888
|
## mcp-servers
|
|
886
889
|
|
|
887
890
|
### `agentrun mcp-servers delete NAME`
|
|
@@ -1014,7 +1017,7 @@ Route: `POST /runs`.
|
|
|
1014
1017
|
- `--effort low|medium|high` (string, one of these): How hard a run works. It sets the model's thinking level, and adds a short note on how to work right after the agent's instructions, on every harness: low answers directly, medium plans briefly and checks once, high plans each part, fills the gaps and reviews. A run started with none thinks at its agent revision's own thinking level, else the host's default, and gets no note.
|
|
1015
1018
|
- `--max-cost-usd NUMBER` (number): This run's cap, for every turn of it. Default the definition's `max_cost_usd`, else the effort default.
|
|
1016
1019
|
- `--quote-id STRING` (string): Run at a quote's terms. A different `effort` or `max_cost_usd` than the quote's is `409 quote_invalid`.
|
|
1017
|
-
- `--model-key STRING` (string): The run's own model key: a key for the model gateway at AGENT_MODEL_BASE_URL, such as a LiteLLM virtual key minted for this run, which every model request tied to the run carries in place of the server's own key (its turns, its subagents, its learned workflows' nodes, a workflow's model nodes, its end-of-run compile turn). AgentRun never mints, lists or revokes it. Sealed into the run's launch bound to the gateway's origin, read at each claim (a reclaim after a crash among them), erased from the store by the write that ends the run's turn;
|
|
1020
|
+
- `--model-key STRING` (string): The run's own model key: a key for the model gateway at AGENT_MODEL_BASE_URL, such as a LiteLLM virtual key minted for this run, which every model request tied to the run carries in place of the server's own key (its turns, its subagents, its learned workflows' nodes, a workflow's model nodes, its end-of-run compile turn, and the learning work it starts: a learned workflow's check replay runs and an audit's shadow run). AgentRun never mints, lists or revokes it. Sealed into the run's launch bound to the gateway's origin, read at each claim (a reclaim after a crash among them), erased from the store by the write that ends the run's turn. A replay or shadow run the run starts while its claim holds the key takes a copy sealed for that run alone, erased by the write that ends that run (or its purge): such a run starts at the run's end, capped at its share of what the run's cap leaves then (none left: it is dropped, nothing spent), and it is listed in the run's `learning_work` until it ends; revoke the key once the run is terminal and `learning_work.state` is not `running`. Never in its saved body, its timeline, its view, a log or an Idempotency-Key's fingerprint (a retry with a new key answers the first request's run, its first key kept). The harness, its tools and its sandbox never see it. 422 `invalid` at `model_key` for a key under 8 characters (no cut could hold it) or one with a character that is not visible ASCII (a space, a control character, anything past ASCII); 422 at `model_key` when the run's model never goes to that gateway (no AGENT_MODEL_BASE_URL here, a native run, or a model routed elsewhere). Absent, the run spends on the server's key for its route; where the server holds none (no AGENT_MODEL_API_KEY, none saved in Settings), a start without it is 422 `model_key_required`, never a failure at its first model call.
|
|
1018
1021
|
- `--workflow-first` (boolean): Whether a run of an agent that learns may start on one of its learned workflows. On (absent or true), a run whose input fits exactly one active learned workflow (its input schema has a required field, and the run's input passes it) runs that workflow first, with no turn of the agent's model: its answer is the run's answer, and if it hands the job back (it escalates, a step fails), the same run goes on with the agent, given what the workflow found. The run keeps which path it took: `workflow`, `handed_back` or `agent`. false: the agent takes the run from its first turn.
|
|
1019
1022
|
- `--learning use` (string, one of these): `use`: the run starts from what its agent learned (its corrections and learned workflows, as a run that learns is given them), whatever its agent's `learning` says, and keeps nothing: no compile turn, no correction and no merge, at its end or after a follow-up. Its `learning` reads `seeded` while it runs, then `dropped` with reason `use`. So many runs of one agent read one learned state, frozen (a held-out evaluation). On a server whose learning is off it is given nothing, as every run there. Absent: the agent's own (`learning: true` learns). 422 not_an_option for anything else.
|
|
1020
1023
|
- `--repo STRING` (string): The GitHub repository (`owner/name`) this run reaches: the one its GitHub App token is made for, so its github MCP calls are scoped to it. It must be one the agent is bound to (a repository its GitHub triggers' `accept` names on `/repository/full_name`); another is 422 at `repo` in words naming the repositories it is bound to. Absent: the agent's repository when it is bound to exactly one and the first message names none; otherwise the repository a `https://github.com/<owner>/<repo>` URL in the first message names. A run a trigger starts takes its repository from its event, and `repo` there is 422.
|
|
@@ -1128,7 +1131,7 @@ Route: `POST /runs/{run_id}/messages`.
|
|
|
1128
1131
|
|
|
1129
1132
|
- `--idempotency-key STRING` (string): Makes a create safe to retry. The same key and body within `limits.idempotency_ttl_s` replays the first answer with `Idempotent-Replayed: true`; another body is `409 idempotency_mismatch`; a retry while the first is in flight is `409 idempotency_in_progress`. Keys are scoped to the credential's account and the route.
|
|
1130
1133
|
- `--content STRING` (string, required): At most `limits.max_message_chars`. Not only whitespace.
|
|
1131
|
-
- `--model-key STRING` (string): The run's own model key: a key for the model gateway at AGENT_MODEL_BASE_URL, such as a LiteLLM virtual key minted for this run, which every model request tied to the run carries in place of the server's own key (its turns, its subagents, its learned workflows' nodes, a workflow's model nodes, its end-of-run compile turn). AgentRun never mints, lists or revokes it. Sealed into the run's launch bound to the gateway's origin, read at each claim (a reclaim after a crash among them), erased from the store by the write that ends the run's turn;
|
|
1134
|
+
- `--model-key STRING` (string): The run's own model key: a key for the model gateway at AGENT_MODEL_BASE_URL, such as a LiteLLM virtual key minted for this run, which every model request tied to the run carries in place of the server's own key (its turns, its subagents, its learned workflows' nodes, a workflow's model nodes, its end-of-run compile turn, and the learning work it starts: a learned workflow's check replay runs and an audit's shadow run). AgentRun never mints, lists or revokes it. Sealed into the run's launch bound to the gateway's origin, read at each claim (a reclaim after a crash among them), erased from the store by the write that ends the run's turn. A replay or shadow run the run starts while its claim holds the key takes a copy sealed for that run alone, erased by the write that ends that run (or its purge): such a run starts at the run's end, capped at its share of what the run's cap leaves then (none left: it is dropped, nothing spent), and it is listed in the run's `learning_work` until it ends; revoke the key once the run is terminal and `learning_work.state` is not `running`. Never in its saved body, its timeline, its view, a log or an Idempotency-Key's fingerprint (a retry with a new key answers the first request's run, its first key kept). The harness, its tools and its sandbox never see it. 422 `invalid` at `model_key` for a key under 8 characters (no cut could hold it) or one with a character that is not visible ASCII (a space, a control character, anything past ASCII); 422 at `model_key` when the run's model never goes to that gateway (no AGENT_MODEL_BASE_URL here, a native run, or a model routed elsewhere). Absent, the run spends on the server's key for its route; where the server holds none (no AGENT_MODEL_API_KEY, none saved in Settings), a start without it is 422 `model_key_required`, never a failure at its first model call. On a message it replaces the run's key from the run's next model request on, a turn still going included, and it is the first key of a run that started on the server's (mint one per turn); a message without one keeps the key the run holds, which is none once its last turn ended: then, where the server holds no key for the run's model, the message is 422 `model_key_required` and nothing is written.
|
|
1132
1135
|
- `--body JSON|@FILE|-` (application/json, required): The whole body: inline, from a file or from stdin. The flags above win over a JSON body's fields.
|
|
1133
1136
|
- `--body-type TYPE` (string): The body's type, one of application/json; any other than JSON is sent as given.
|
|
1134
1137
|
|
|
@@ -1174,7 +1177,7 @@ Route: `POST /agents/{agent_id}/runs`.
|
|
|
1174
1177
|
- `--effort low|medium|high` (string, one of these): How hard a run works. It sets the model's thinking level, and adds a short note on how to work right after the agent's instructions, on every harness: low answers directly, medium plans briefly and checks once, high plans each part, fills the gaps and reviews. A run started with none thinks at its agent revision's own thinking level, else the host's default, and gets no note.
|
|
1175
1178
|
- `--max-cost-usd NUMBER` (number): This run's cap, for every turn of it. Default the definition's `max_cost_usd`, else the effort default.
|
|
1176
1179
|
- `--quote-id STRING` (string): Run at a quote's terms. A different `effort` or `max_cost_usd` than the quote's is `409 quote_invalid`.
|
|
1177
|
-
- `--model-key STRING` (string): The run's own model key: a key for the model gateway at AGENT_MODEL_BASE_URL, such as a LiteLLM virtual key minted for this run, which every model request tied to the run carries in place of the server's own key (its turns, its subagents, its learned workflows' nodes, a workflow's model nodes, its end-of-run compile turn). AgentRun never mints, lists or revokes it. Sealed into the run's launch bound to the gateway's origin, read at each claim (a reclaim after a crash among them), erased from the store by the write that ends the run's turn;
|
|
1180
|
+
- `--model-key STRING` (string): The run's own model key: a key for the model gateway at AGENT_MODEL_BASE_URL, such as a LiteLLM virtual key minted for this run, which every model request tied to the run carries in place of the server's own key (its turns, its subagents, its learned workflows' nodes, a workflow's model nodes, its end-of-run compile turn, and the learning work it starts: a learned workflow's check replay runs and an audit's shadow run). AgentRun never mints, lists or revokes it. Sealed into the run's launch bound to the gateway's origin, read at each claim (a reclaim after a crash among them), erased from the store by the write that ends the run's turn. A replay or shadow run the run starts while its claim holds the key takes a copy sealed for that run alone, erased by the write that ends that run (or its purge): such a run starts at the run's end, capped at its share of what the run's cap leaves then (none left: it is dropped, nothing spent), and it is listed in the run's `learning_work` until it ends; revoke the key once the run is terminal and `learning_work.state` is not `running`. Never in its saved body, its timeline, its view, a log or an Idempotency-Key's fingerprint (a retry with a new key answers the first request's run, its first key kept). The harness, its tools and its sandbox never see it. 422 `invalid` at `model_key` for a key under 8 characters (no cut could hold it) or one with a character that is not visible ASCII (a space, a control character, anything past ASCII); 422 at `model_key` when the run's model never goes to that gateway (no AGENT_MODEL_BASE_URL here, a native run, or a model routed elsewhere). Absent, the run spends on the server's key for its route; where the server holds none (no AGENT_MODEL_API_KEY, none saved in Settings), a start without it is 422 `model_key_required`, never a failure at its first model call.
|
|
1178
1181
|
- `--workflow-first` (boolean): Whether a run of an agent that learns may start on one of its learned workflows. On (absent or true), a run whose input fits exactly one active learned workflow (its input schema has a required field, and the run's input passes it) runs that workflow first, with no turn of the agent's model: its answer is the run's answer, and if it hands the job back (it escalates, a step fails), the same run goes on with the agent, given what the workflow found. The run keeps which path it took: `workflow`, `handed_back` or `agent`. false: the agent takes the run from its first turn.
|
|
1179
1182
|
- `--learning use` (string, one of these): `use`: the run starts from what its agent learned (its corrections and learned workflows, as a run that learns is given them), whatever its agent's `learning` says, and keeps nothing: no compile turn, no correction and no merge, at its end or after a follow-up. Its `learning` reads `seeded` while it runs, then `dropped` with reason `use`. So many runs of one agent read one learned state, frozen (a held-out evaluation). On a server whose learning is off it is given nothing, as every run there. Absent: the agent's own (`learning: true` learns). 422 not_an_option for anything else.
|
|
1180
1183
|
- `--repo STRING` (string): The GitHub repository (`owner/name`) this run reaches: the one its GitHub App token is made for, so its github MCP calls are scoped to it. It must be one the agent is bound to (a repository its GitHub triggers' `accept` names on `/repository/full_name`); another is 422 at `repo` in words naming the repositories it is bound to. Absent: the agent's repository when it is bound to exactly one and the first message names none; otherwise the repository a `https://github.com/<owner>/<repo>` URL in the first message names. A run a trigger starts takes its repository from its event, and `repo` there is 422.
|
|
@@ -1397,16 +1400,23 @@ Route: none, it runs on this machine.
|
|
|
1397
1400
|
|
|
1398
1401
|
### `agentrun share RUN`
|
|
1399
1402
|
|
|
1400
|
-
|
|
1403
|
+
Share an ended run: publish its bundle to a share host (--to), or show what that would send (--dry-run: its bundle checked and summed up, its page opened here) or write it (--out).
|
|
1401
1404
|
|
|
1402
|
-
Route: `GET /runs/{run_id}/export; the page
|
|
1405
|
+
Route: `GET /runs/{run_id}/export; with --to, then POST <host>/r/v2/shares on the share host; the page --dry-run opens is <origin>/share/{run_id}`.
|
|
1403
1406
|
|
|
1407
|
+
- `--to URL` (string, this CLI's own): publish: the bundle, exported again with --restore and checked against --expect before anything is sent, posted to the share host at URL with the token agentrun login --share URL kept; prints the share's URL
|
|
1404
1408
|
- `--dry-run` (boolean, this CLI's own): fetch the run's share bundle, print its summary and open its preview page; nothing is sent anywhere
|
|
1405
1409
|
- `--out FILE` (string, this CLI's own): write the bundle as the server sent it, byte for byte (0600, never over a file); alone, it opens nothing
|
|
1406
1410
|
- `--restore IDS` (string, this CLI's own): cut ids to keep in place, comma-separated (restorable ones only: a label, personal or jev cut)
|
|
1407
1411
|
- `--expect SHA` (string, this CLI's own): refuse a bundle whose sha256 is another (the page's publish command carries it)
|
|
1408
1412
|
- `--no-open` (boolean, this CLI's own): open no browser; print the page's link
|
|
1409
1413
|
|
|
1414
|
+
### `agentrun share delete URL`
|
|
1415
|
+
|
|
1416
|
+
Take a published share down with the share host's login: its page and bundle then answer 410.
|
|
1417
|
+
|
|
1418
|
+
Route: `DELETE <host>/r/v2/shares/{id}, on the share host the URL is on`.
|
|
1419
|
+
|
|
1410
1420
|
## skill
|
|
1411
1421
|
|
|
1412
1422
|
### `agentrun skill install`
|