agental 0.22.0 → 0.24.0

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.
Files changed (2) hide show
  1. package/dist/cli.js +125 -17
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -7411,7 +7411,7 @@ import { parseArgs } from "node:util";
7411
7411
 
7412
7412
  // ../src/cli-build.ts
7413
7413
  var PUBLISHED = true;
7414
- var CLI_VERSION = true ? "0.22.0" : "dev";
7414
+ var CLI_VERSION = true ? "0.24.0" : "dev";
7415
7415
  var DEFAULT_API_URL = true ? "https://agental.io" : "http://127.0.0.1:8741";
7416
7416
  var PACKAGE_NAME = "agental";
7417
7417
  var USER_AGENT = `agental-cli/${CLI_VERSION}`;
@@ -9363,6 +9363,9 @@ var COMMANDS = [
9363
9363
  ["--input <NAME>=<default>", "add, update: a variable a run is given, with its default; NAME alone: a run must give it"],
9364
9364
  ["--secret-input <NAME>[=<default>]", "add, update: an input sealed where it is kept, \u2022\u2022\u2022\u2022 in output"],
9365
9365
  ["--input <NAME>=<value>", "run: a value for one of its inputs, over the default"],
9366
+ ["--after-job <job>[:succeeded|failed]", "add, update: run it when a run of that job ends (any end, or that one)"],
9367
+ ["--on-mail <sandbox>", "add, update: run it when mail arrives at that sandbox's address"],
9368
+ ["--no-events", "update: take away every event that starts it"],
9366
9369
  ["--name <name>", "update: a new name"],
9367
9370
  ["--wait[=<seconds>]", "run, again: wait for it to end, print its output, exit with its exit code"],
9368
9371
  ["--prune", "apply: delete the jobs the file does not name"],
@@ -10345,6 +10348,27 @@ function freshAfterOf(job2) {
10345
10348
  const keep = job2.keepFailedFor ?? "24h";
10346
10349
  return keep === "0" ? "deletes the sandbox, whether the command exits 0 or not" : `deletes the sandbox if the command exits 0; else keeps it stopped for ${keep}`;
10347
10350
  }
10351
+ function monthlySpendOf(given) {
10352
+ if (given === void 0) {
10353
+ return void 0;
10354
+ }
10355
+ const spelled = given.trim().replace(/^\$/, "");
10356
+ if (spelled === "none") {
10357
+ return null;
10358
+ }
10359
+ if (!/^[0-9]+(\.[0-9]{1,2})?$/.test(spelled) || Number(spelled) < 0.01 || Number(spelled) > 1e5) {
10360
+ throw new Error(`--max-monthly-spend is the most a job's runs may cost in a month, in dollars from 0.01 to 100000, as 25 or 25.50, or none: not ${given}.`);
10361
+ }
10362
+ return Math.round(Number(spelled) * 100) * 1e4;
10363
+ }
10364
+ function spendLimitOf(job2) {
10365
+ const limit = job2.maxMonthlySpendMicros ?? null;
10366
+ if (limit === null) {
10367
+ return "no limit";
10368
+ }
10369
+ const cents = (micros) => `$${(Math.ceil(micros / 1e4) / 100).toFixed(2)}`;
10370
+ return `${cents(job2.spentThisMonthMicros ?? 0)} of ${cents(limit)} this month`;
10371
+ }
10348
10372
  function retriesOf(job2) {
10349
10373
  const retries = job2.retries ?? 0;
10350
10374
  return retries === 0 ? "none: a run that fails is not tried again" : `${retries}, after ${RETRY_WAITS.slice(0, retries).join(", ")}`;
@@ -10421,7 +10445,7 @@ function sandboxDoneOf(run2) {
10421
10445
  }
10422
10446
  return run2.stoppedSandbox === null ? "started, left running" : `started, stopped (${run2.stoppedSandbox})`;
10423
10447
  }
10424
- var BY = { timer: "timer", run_now: "by hand", api: "api", http: "http" };
10448
+ var BY = { timer: "timer", run_now: "by hand", api: "api", http: "http", event: "event" };
10425
10449
  function commandLine(words2) {
10426
10450
  if (words2.length === 1) {
10427
10451
  return words2[0] ?? "";
@@ -10522,6 +10546,37 @@ function runInputsOf(options) {
10522
10546
  }
10523
10547
  return inputs;
10524
10548
  }
10549
+ function eventsOf(options) {
10550
+ const afterJob = options["after-job"] ?? [];
10551
+ const onMail = options["on-mail"] ?? [];
10552
+ if (options["no-events"]) {
10553
+ if (afterJob.length > 0 || onMail.length > 0) {
10554
+ throw new Error("--no-events takes away every event that starts the job: give it without --after-job and --on-mail.");
10555
+ }
10556
+ return [];
10557
+ }
10558
+ if (afterJob.length === 0 && onMail.length === 0) {
10559
+ return void 0;
10560
+ }
10561
+ return [
10562
+ ...afterJob.map((given) => {
10563
+ const colon = given.lastIndexOf(":");
10564
+ const [job2, outcome] = colon < 0 ? [given, "any"] : [given.slice(0, colon), given.slice(colon + 1)];
10565
+ if (job2 === "" || !["succeeded", "failed", "any"].includes(outcome)) {
10566
+ throw new Error(`--after-job is a job, by its name or id, and :succeeded or :failed for one end of its runs alone, as --after-job build:succeeded: ${given} is not.`);
10567
+ }
10568
+ return { on: "job.run_finished", job: job2, outcome };
10569
+ }),
10570
+ ...onMail.map((sandbox) => ({ on: "email.received", sandbox }))
10571
+ ];
10572
+ }
10573
+ function eventsOfJob(job2) {
10574
+ const events = job2.events ?? [];
10575
+ if (events.length === 0) {
10576
+ return null;
10577
+ }
10578
+ return events.map((event) => event.on === "job.run_finished" ? `after ${event.jobName ?? event.job}${event.outcome === "any" ? "" : ` (${event.outcome})`}` : `mail to ${event.sandboxName ?? event.sandbox}`).join(", ");
10579
+ }
10525
10580
  function waitSecondsOf(options) {
10526
10581
  const given = options["wait-seconds"];
10527
10582
  if (given === void 0) {
@@ -10552,8 +10607,10 @@ function fieldsOf(options) {
10552
10607
  if (options.alerts !== void 0 && !ALERTS.includes(options.alerts)) {
10553
10608
  throw new Error(`--alerts is email, webhook, both or none, not ${options.alerts}.`);
10554
10609
  }
10610
+ const spend = monthlySpendOf(options["max-monthly-spend"]);
10555
10611
  const target = targetOf(options);
10556
10612
  const inputs = jobInputsOf(options);
10613
+ const events = eventsOf(options);
10557
10614
  return {
10558
10615
  ...target === void 0 ? {} : { target },
10559
10616
  ...options.every === void 0 ? {} : { every: options.every },
@@ -10567,7 +10624,9 @@ function fieldsOf(options) {
10567
10624
  ...options["keep-failed-for"] === void 0 ? {} : { keepFailedFor: options["keep-failed-for"] },
10568
10625
  ...options.retries === void 0 ? {} : { retries: Number(options.retries.trim()) },
10569
10626
  ...options.alerts === void 0 ? {} : { alerts: options.alerts },
10570
- ...inputs === void 0 ? {} : { inputs }
10627
+ ...spend === void 0 ? {} : { maxMonthlySpendMicros: spend },
10628
+ ...inputs === void 0 ? {} : { inputs },
10629
+ ...events === void 0 ? {} : { events }
10571
10630
  };
10572
10631
  }
10573
10632
  function inputsOfJob(job2) {
@@ -10598,7 +10657,9 @@ function print3(job2, asJson) {
10598
10657
  ["timeout", `${Math.round(job2.timeoutMs / 1e3)}s`],
10599
10658
  ["retries", retriesOf(job2)],
10600
10659
  ["alerts", alertsOf(job2)],
10660
+ ["spending", spendLimitOf(job2)],
10601
10661
  ...inputsOfJob(job2) === null ? [] : [["inputs", inputsOfJob(job2)]],
10662
+ ...eventsOfJob(job2) === null ? [] : [["events", eventsOfJob(job2)]],
10602
10663
  ["next", nextOf(job2, false)],
10603
10664
  ...job2.failingSince ? [["failing", `since ${at2(job2.failingSince)}`]] : [],
10604
10665
  ["last run", lastOf(job2.lastRun)],
@@ -10703,6 +10764,9 @@ async function job(client, positionals, command2, options) {
10703
10764
  if ((options.input !== void 0 || options["secret-input"] !== void 0) && !["add", "update", "run"].includes(what)) {
10704
10765
  throw new Error("--input goes with job add, update or run, and --secret-input with add or update.");
10705
10766
  }
10767
+ if ((options["after-job"] !== void 0 || options["on-mail"] !== void 0 || options["no-events"]) && !["add", "update"].includes(what)) {
10768
+ throw new Error("--after-job, --on-mail and --no-events go with job add or update.");
10769
+ }
10706
10770
  if (rest.length > (what === "logs" || what === "keep" || what === "again" ? 1 : 0)) {
10707
10771
  throw new Error(what === "add" ? "job add takes its name, then its command after --: agental job add nightly --sandbox <sandbox> --every 1h -- npm test" : USAGE4);
10708
10772
  }
@@ -10736,7 +10800,7 @@ async function job(client, positionals, command2, options) {
10736
10800
  const changes = { ...fieldsOf(options), ...options.name === void 0 ? {} : { name: options.name }, ...command2 !== null && command2.length > 0 ? { command: commandLine(command2) } : {} };
10737
10801
  if (Object.keys(changes).length === 0) {
10738
10802
  throw new Error(
10739
- "job update needs something to change: --sandbox, --template (and --size), --keep-failed-for, --every, --cron, --at, --tz, --timeout, --overlap, --if-stopped, --keep-memory, --delete-after or --disk-only, --retries, --alerts, --input or --secret-input, --name for a new name, or a new command after --."
10803
+ "job update needs something to change: --sandbox, --template (and --size), --keep-failed-for, --every, --cron, --at, --tz, --timeout, --overlap, --if-stopped, --keep-memory, --delete-after or --disk-only, --retries, --alerts, --max-monthly-spend, --input or --secret-input, --after-job, --on-mail or --no-events, --name for a new name, or a new command after --."
10740
10804
  );
10741
10805
  }
10742
10806
  print3(await ask6(client, "PATCH", path5, changes), options.json);
@@ -11051,7 +11115,7 @@ async function refusedCalls(client, name, options) {
11051
11115
  }
11052
11116
  table2([["AT", "STATUS", "TRIGGER", "FROM", "WHY"], ...calls.map((call3) => [past(call3.at), String(call3.status), call3.triggerId, call3.from ?? "-", call3.reason])]);
11053
11117
  }
11054
- var FILE_KEYS = ["name", "sandbox", "template", "size", "command", "every", "cron", "at", "timeZone", "timeoutMs", "overlap", "ifStopped", "after", "keepFailedFor", "retries", "alerts", "inputs", "enabled"];
11118
+ var FILE_KEYS = ["name", "sandbox", "template", "size", "command", "every", "cron", "at", "timeZone", "timeoutMs", "overlap", "ifStopped", "after", "keepFailedFor", "retries", "alerts", "maxMonthlySpendMicros", "inputs", "events", "enabled"];
11055
11119
  function fileJobOf(item, index) {
11056
11120
  const where = `jobs[${index}]`;
11057
11121
  if (item === null || typeof item !== "object" || Array.isArray(item)) {
@@ -11083,6 +11147,31 @@ function fileJobOf(item, index) {
11083
11147
  const target = template2 === void 0 ? { sandbox } : { template: template2, ...size2 === void 0 ? {} : { size: size2 } };
11084
11148
  return { name, body: { target, ...fields } };
11085
11149
  }
11150
+ function byEvents(jobs2) {
11151
+ const named = new Map(jobs2.map((each) => [each.name, each]));
11152
+ const done2 = /* @__PURE__ */ new Set();
11153
+ const ordered2 = [];
11154
+ const visit = (each, path5) => {
11155
+ if (done2.has(each.name) || path5.has(each.name)) {
11156
+ return;
11157
+ }
11158
+ path5.add(each.name);
11159
+ const events = Array.isArray(each.body["events"]) ? each.body["events"] : [];
11160
+ for (const event of events) {
11161
+ const job2 = event !== null && typeof event === "object" ? event["job"] : void 0;
11162
+ const before = typeof job2 === "string" ? named.get(job2) : void 0;
11163
+ if (before !== void 0) {
11164
+ visit(before, path5);
11165
+ }
11166
+ }
11167
+ done2.add(each.name);
11168
+ ordered2.push(each);
11169
+ };
11170
+ for (const each of jobs2) {
11171
+ visit(each, /* @__PURE__ */ new Set());
11172
+ }
11173
+ return ordered2;
11174
+ }
11086
11175
  function fileJobsOf(file) {
11087
11176
  let text;
11088
11177
  try {
@@ -11148,7 +11237,7 @@ async function apply(client, file, options) {
11148
11237
  return;
11149
11238
  }
11150
11239
  let applied;
11151
- for (const [index, each] of jobs2.entries()) {
11240
+ for (const [index, each] of byEvents(jobs2).entries()) {
11152
11241
  const response = await client.request(jobPath(each.name), { method: "PUT", body: JSON.stringify(each.body) });
11153
11242
  let body = null;
11154
11243
  try {
@@ -11287,7 +11376,7 @@ function sentOf(events) {
11287
11376
  function answerOf(delivery) {
11288
11377
  return delivery.httpStatus === null ? "none" : String(delivery.httpStatus);
11289
11378
  }
11290
- function eventsOf(deliveries) {
11379
+ function eventsOf2(deliveries) {
11291
11380
  const byEvent = /* @__PURE__ */ new Map();
11292
11381
  for (const delivery of deliveries) {
11293
11382
  const key2 = delivery.eventId ?? delivery.id;
@@ -11358,7 +11447,7 @@ function describe3(webhook2) {
11358
11447
  `url: ${webhook2.url}`,
11359
11448
  `sent: ${sentOf(webhook2.events)}`,
11360
11449
  `added: ${at3(webhook2.createdAt)}`,
11361
- `last post: ${standingOf(eventsOf(webhook2.deliveries))}`,
11450
+ `last post: ${standingOf(eventsOf2(webhook2.deliveries))}`,
11362
11451
  ""
11363
11452
  ].join("\n");
11364
11453
  }
@@ -11387,7 +11476,7 @@ async function listWebhooks(client, asJson) {
11387
11476
  for (const webhook2 of webhooks) {
11388
11477
  const listed = webhook2.events.join(",");
11389
11478
  const sent = webhook2.events.includes(EVERY) ? "every event" : listed.length <= 33 ? listed : `${webhook2.events.length} kinds`;
11390
- process.stdout.write(`${[pad3(webhook2.id, 22), pad3(nameOf(webhook2), 18), pad3(sent, 34), standingOf(eventsOf(webhook2.deliveries))].join(" ")}
11479
+ process.stdout.write(`${[pad3(webhook2.id, 22), pad3(nameOf(webhook2), 18), pad3(sent, 34), standingOf(eventsOf2(webhook2.deliveries))].join(" ")}
11391
11480
  `);
11392
11481
  }
11393
11482
  }
@@ -11426,7 +11515,7 @@ async function webhook(client, positionals, options) {
11426
11515
  return;
11427
11516
  }
11428
11517
  process.stdout.write(describe3(found));
11429
- const slow = eventsOf(found.deliveries).filter((one) => one.tries.length > 1 || one.outcome !== "taken");
11518
+ const slow = eventsOf2(found.deliveries).filter((one) => one.tries.length > 1 || one.outcome !== "taken");
11430
11519
  if (slow.length > 0) {
11431
11520
  process.stdout.write("\nEvents not taken at the first try, newest first:\n");
11432
11521
  for (const one of slow.slice(0, 10)) {
@@ -12423,12 +12512,18 @@ async function main() {
12423
12512
  // agental job add and update: how many times a failed run is tried again, and who is told when one fails for good.
12424
12513
  retries: { type: "string" },
12425
12514
  alerts: { type: "string" },
12515
+ // agental job add and update: the most a job's runs may cost in a month, in dollars, or none.
12516
+ "max-monthly-spend": { type: "string" },
12426
12517
  wait: { type: "boolean", default: false },
12427
12518
  // job run and again: --wait 30 or --wait=30, as waitSecondsSpelled gives it.
12428
12519
  "wait-seconds": { type: "string" },
12429
12520
  // agental job add and update: the inputs a run may be given, and those that are secret; job run: the values a run is given.
12430
12521
  input: { type: "string", multiple: true },
12431
12522
  "secret-input": { type: "string", multiple: true },
12523
+ // agental job add and update: the events that start a job, another job's run ending and mail to a sandbox; or none.
12524
+ "after-job": { type: "string", multiple: true },
12525
+ "on-mail": { type: "string", multiple: true },
12526
+ "no-events": { type: "boolean", default: false },
12432
12527
  // agental job apply: delete the jobs the file does not name, or say what it would do and change nothing.
12433
12528
  prune: { type: "boolean", default: false },
12434
12529
  "dry-run": { type: "boolean", default: false },
@@ -15306,16 +15401,19 @@ Usage
15306
15401
  agental jobs [--sandbox <sandbox-id> | --template <template>] [--json]
15307
15402
  agental job add [<name>] --sandbox <sandbox-id> [--every 1m|15m|1h|1d | --cron "<m h dom mon dow>" [--tz <zone>] | --at <date-time>] [--timeout <time>]
15308
15403
  [--if-stopped start|skip] [--keep-memory | --delete-after] [--overlap skip|replace|parallel] [--retries <0-5>] [--alerts email|webhook|both|none]
15309
- [--input <NAME>[=<default>]]... [--secret-input <NAME>[=<default>]]... [--json] -- <command> [args...]
15404
+ [--max-monthly-spend <dollars>]
15405
+ [--input <NAME>[=<default>]]... [--secret-input <NAME>[=<default>]]... [--after-job <job>[:succeeded|failed]]... [--on-mail <sandbox>]...
15406
+ [--json] -- <command> [args...]
15310
15407
  agental job add [<name>] --template <template> [--size tiny|small|medium|large] [--keep-failed-for 0|30m|24h|7d] [--every 1m|15m|1h|1d | --cron "<m h dom mon dow>" [--tz <zone>] | --at <date-time>]
15311
- [--timeout <time>] [--overlap skip|replace|parallel] [--retries <0-5>] [--alerts email|webhook|both|none]
15312
- [--input <NAME>[=<default>]]... [--secret-input <NAME>[=<default>]]... [--json] -- <command> [args...] a fresh sandbox for each run
15408
+ [--timeout <time>] [--overlap skip|replace|parallel] [--retries <0-5>] [--alerts email|webhook|both|none] [--max-monthly-spend <dollars>]
15409
+ [--input <NAME>[=<default>]]... [--secret-input <NAME>[=<default>]]... [--after-job <job>[:succeeded|failed]]... [--on-mail <sandbox>]...
15410
+ [--json] -- <command> [args...] a fresh sandbox for each run
15313
15411
  agental job list [--sandbox <sandbox-id> | --template <template>] [--json]
15314
15412
  agental job get|delete|pause|resume <job> [--json]
15315
15413
  agental job update <job> [--sandbox <sandbox-id> | --template <template> [--size <size>]] [--keep-failed-for <time>] [--every <time> | --cron "<expr>" | --at <date-time>] [--tz <zone>]
15316
15414
  [--name <name>] [--timeout <time>] [--if-stopped start|skip] [--keep-memory | --delete-after | --disk-only] [--overlap skip|replace|parallel]
15317
- [--retries <0-5>] [--alerts email|webhook|both|none] [--input <NAME>[=<default>]]... [--secret-input <NAME>[=<default>]]...
15318
- [--json] [-- <command> [args...]]
15415
+ [--retries <0-5>] [--alerts email|webhook|both|none] [--max-monthly-spend <dollars>|none] [--input <NAME>[=<default>]]... [--secret-input <NAME>[=<default>]]...
15416
+ [--after-job <job>[:succeeded|failed]]... [--on-mail <sandbox>]... [--no-events] [--json] [-- <command> [args...]]
15319
15417
  agental job run <job> [--input <NAME>=<value>]... [--wait[=<seconds>]] [--json]
15320
15418
  agental job again <job> <run-id> [--wait[=<seconds>]] [--json] run it again with what that run was given
15321
15419
  agental job runs <job> [--limit <n>] [--json]
@@ -15510,14 +15608,24 @@ is kept and masked as \u2022\u2022\u2022\u2022 in output. A name is upper-case l
15510
15608
  to update, they replace the ones the job had. A run by the clock is given the defaults; job run
15511
15609
  --input NAME=VALUE gives a run a value over its default, and again runs a job once more with what
15512
15610
  one of its runs was given, secret values too.
15611
+ Events start a job too: add and update take --after-job JOB to run it when a run of JOB ends,
15612
+ --after-job JOB:succeeded or JOB:failed for one end alone (failed: for good, after its retries),
15613
+ and --on-mail SANDBOX to run it when mail arrives at that sandbox's address. Its command is told
15614
+ what started it: AGENTAL_TRIGGER_JOB, AGENTAL_TRIGGER_RUN and AGENTAL_TRIGGER_OUTCOME, or
15615
+ AGENTAL_TRIGGER_SANDBOX and AGENTAL_TRIGGER_EMAIL, the message's id. Given to update, they replace
15616
+ the events the job had; --no-events takes them all away. A chain that comes back to the job is
15617
+ refused, and events that start 60 runs of a job in a minute pause it.
15618
+ --max-monthly-spend 25 gives a job a spending limit in dollars: once its runs this calendar month
15619
+ (UTC) cost that much, at the list price of the running time they caused, it pauses itself, says
15620
+ so, and mails as its alerts say; a run under way goes on to its end. none takes the limit away.
15513
15621
  job apply puts the jobs a file lists, each by its name: one not there is made, and one that is
15514
15622
  is changed to exactly what the file says, whatever it leaves out going back to its default; each
15515
15623
  is said to be made, changed or unchanged. --prune deletes the jobs the file does not name;
15516
15624
  --dry-run changes nothing, and says which would be made, put or deleted. The file is YAML, or
15517
15625
  JSON, a map whose jobs: lists each job: its name, command, sandbox: for a sandbox you keep or
15518
15626
  template: (and size:) for a fresh sandbox at each run, and any of every, cron, at, timeZone,
15519
- timeoutMs, overlap, ifStopped, after, keepFailedFor, retries, alerts, inputs and enabled, as
15520
- POST /v1/jobs takes them:
15627
+ timeoutMs, overlap, ifStopped, after, keepFailedFor, retries, alerts, maxMonthlySpendMicros, inputs, events and enabled,
15628
+ as POST /v1/jobs takes them; a job named in another's events is listed before it:
15521
15629
  jobs:
15522
15630
  - name: nightly
15523
15631
  sandbox: build
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agental",
3
- "version": "0.22.0",
3
+ "version": "0.24.0",
4
4
  "description": "The agental command: Linux sandboxes for your agents, from the terminal. npx agental@latest signs you in and installs the agent skills.",
5
5
  "license": "MIT",
6
6
  "author": "VERTIXER LTD",