@getgavel/cli 0.1.9 → 0.1.10

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 (3) hide show
  1. package/README.md +13 -1
  2. package/dist/gavel.js +346 -10
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -41,14 +41,26 @@ gavel act --help # one command's usage
41
41
  ## For your person
42
42
 
43
43
  Once your person lets you, their personal agent, do what they can in Gavel (Settings › Agents,
44
- "Can do what you can in Gavel"), you act on their decisions as them:
44
+ "Can do what you can in Gavel"), you act as them: on their decisions, settings, trusts, agents and
45
+ Blockers.
45
46
 
46
47
  ```sh
47
48
  gavel my-queue # their queue
48
49
  gavel decide-mine D-12 --option <id> --their-words "ship it" # only on their word, from your chat
49
50
  gavel boomerang D-12 # forward-mine, research-more, ask-someone, defer, undo…
51
+ gavel set-my-day --day-start 09:00 --day-end 18:00 # their day, alerts, reports and trusts
52
+ gavel move-agent-mine <agent id> --under <agent id> # their agents: edit, pause, archive…
53
+ gavel my-blockers # what's stuck; nudge-mine, reassign
54
+ gavel call GET /v1/me --for-person true # any route, as you or as them
50
55
  ```
51
56
 
57
+ ## In GitHub Actions
58
+
59
+ A job with `permissions: id-token: write` needs no pasted token: the command trades GitHub's token
60
+ for the run's own, once a person has let the repository's runs ask under one of their agents
61
+ (Settings › Agents). `gavel actions-env` writes it, masked, as `GAVEL_TOKEN` for the job's later
62
+ steps, for an agent there that speaks MCP.
63
+
52
64
  ## Your Claude Code or Codex chat, in Gavel
53
65
 
54
66
  If your agent works in Claude Code or Codex on your laptop, run this in its folder, in a terminal
package/dist/gavel.js CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
  // The `gavel` command-line tool: the MCP server's operations over the HTTP API, for agents that
3
3
  // prefer a shell. GAVEL_TOKEN (an agent's token) and GAVEL_API_URL (default https://api.getgavel.ai);
4
- // on a fleet token, the chat's name with --as or GAVEL_AGENT (sent as X-Gavel-Agent).
4
+ // on a fleet token, the chat's name with --as or GAVEL_AGENT (sent as X-Gavel-Agent). In a GitHub
5
+ // Actions job with id-token: write and no GAVEL_TOKEN, the run's own token, traded for GitHub's.
5
6
  // Prints the API's JSON; on an error, prints it to stderr and exits 1.
6
7
  import { execFileSync } from "node:child_process";
7
8
  import { createHash, randomUUID } from "node:crypto";
@@ -23,6 +24,9 @@ const USAGE = `gavel: Gavel from the command line (https://getgavel.ai/agents)
23
24
  the words it would send from the newest session here; sends
24
25
  nothing
25
26
  gavel bridge wake [--server <name>] the bridge's wake hook, which Claude Code runs (not by hand)
27
+ gavel actions-env in a GitHub Actions job with id-token: write, GAVEL_TOKEN
28
+ for its next steps: the run's own token, traded for
29
+ GitHub's and masked (never printed)
26
30
  gavel search (<words> | --query <words>) [--area <slug>] [--status open|decided|any] [--limit <n>]
27
31
  gavel ask --file decision.json the decision, as MCP ask's input
28
32
  gavel revise <D-12> --file decision.json a draft the editor sent back, fixed (the whole ask again)
@@ -100,6 +104,12 @@ const USAGE = `gavel: Gavel from the command line (https://getgavel.ai/agents)
100
104
  your person's own machine (My Machines), --agent starts an
101
105
  agent under you, or yourself, as itself (no --name), its
102
106
  old tokens ended
107
+ gavel move-agent <agent id> --under <agent id>
108
+ an agent below you, with what's under it, under you or
109
+ another agent below you
110
+ gavel call <GET|POST|PUT|PATCH|DELETE> </v1/path> [--query <name=value>] [--body <file.json>]
111
+ [--for-person true] any route of the API as you, or as your person with
112
+ --for-person: MCP's do_action, the answer as it comes
103
113
  gavel me
104
114
  gavel my-queue [--view mine|asked|decided] [--before <time>] [--limit <n>]
105
115
  your person's queue, what they asked, or their Decided
@@ -124,22 +134,62 @@ const USAGE = `gavel: Gavel from the command line (https://getgavel.ai/agents)
124
134
  gavel overrule <D-12> [--text <words> --their-words <words>] [--note <words>]
125
135
  what one of their agents decided: their own decision
126
136
  instead, or reopened in their queue
137
+ gavel my-settings your person's day, alerts, weekly report and Slack messages
138
+ gavel set-my-day [--timezone <zone>] [--day-start <HH:MM>] [--day-end <HH:MM>] [--morning <HH:MM>]
139
+ [--later-every <minutes>] when Now alerts may reach them, their morning, Later's pace
140
+ gavel set-my-alerts [--on-call true|false] [--sounds all|now|off] [--sound <name>]
141
+ [--later-like-now true|false] [--muted true|false] [--workspace-sound <name>|none]
142
+ [--snooze-until <time>|none]
143
+ gavel set-my-reports [--weekly-report true|false] [--slack-messages true|false]
144
+ gavel my-notifications [--limit <n>] what went out to them, and how many are unread
145
+ gavel mark-notification-read <id>
146
+ gavel my-weekly-report
147
+ gavel my-trusts what their agents decide without asking them
148
+ gavel add-trust --agent <id> --words <plain words> [--area <id>]
149
+ for one of their own agents
150
+ gavel edit-trust <trust id> [--words <plain words>] [--area <id>|none]
151
+ gavel revoke-trust <trust id>
152
+ gavel trust-decisions <trust id>
153
+ gavel trust-suggestions <D-12> Defer to my agent's ready-made trusts for a decision
154
+ gavel my-agents your person's agents and their sub-agents
155
+ gavel edit-agent <agent id> [--name <name>] [--title <title>|none] [--description <words>]
156
+ gavel show-agent <agent id> --visible true|false
157
+ shown to the workspace, or hidden, with what's under it
158
+ gavel pause-agent <agent id> [--paused true|false]
159
+ paused (the default), or resumed with --paused false
160
+ gavel set-agent-permissions <agent id> [--permissions <ask,answer>] [--may-invite true|false]
161
+ [--may-ask-person true|false]
162
+ gavel add-sub-agent <parent agent id> --name <name> [--description <words>] [--permissions <ask,answer>]
163
+ no token: your person pairs it by hand
164
+ gavel move-agent-mine <agent id> --under <agent id>
165
+ one of theirs, with what's under it, under another of theirs
166
+ gavel archive-agent <agent id> a sub-agent, back as it was on its next call
167
+ gavel remove-agent <agent id> --their-words <words>
168
+ an agent and everything under it, for good: only on
169
+ their word
170
+ gavel my-blockers what's stuck, as Blockers shows your person
171
+ gavel nudge-mine <D-12> its decider, for them: once a day, when it blocks them
172
+ gavel reassign <D-12> (--to-person <email> | --to-agent <id>) [--note <words>]
173
+ a stuck decision to someone else, for them: an Admin's
127
174
 
128
175
  --as <name> on a fleet token, which chat you are (the name you go by,
129
176
  e.g. worker 35 or the task you're on; keep using the same one)
130
177
 
131
178
  gavel <command> --help shows one command's usage. A command refuses a flag or a word it doesn't
132
- take, and a flag given twice (but --option), with its usage.
133
- The commands from my-queue to overrule act for your person, as them, once they've let you
179
+ take, and a flag given twice (but --option, --code, --label and --query), with its usage.
180
+ The commands from my-queue to reassign act for your person, as them, once they've let you
134
181
  (Settings › Agents); each goes with X-Gavel-For-Person.
135
182
  A text argument or --file of "-" is read from stdin.
136
- Environment: GAVEL_TOKEN (required), GAVEL_API_URL (default https://api.getgavel.ai),
137
- GAVEL_AGENT (instead of --as).`;
183
+ Environment: GAVEL_TOKEN (required, but a GitHub Actions job with id-token: write trades GitHub's
184
+ token for the run's own, named by --as or GAVEL_AGENT), GAVEL_API_URL (default
185
+ https://api.getgavel.ai), GAVEL_AGENT (instead of --as), GAVEL_WORKSPACE (a run's workspace, only
186
+ when its repository asks in more than one).`;
138
187
  // What each command takes besides its words, as its usage above names them: anything else is
139
188
  // refused with that usage, never dropped.
140
189
  const FLAGS = {
141
190
  pair: ["harness", "machine"],
142
191
  bridge: ["harness", "server"],
192
+ "actions-env": [],
143
193
  search: ["query", "area", "status", "limit"],
144
194
  ask: ["file"],
145
195
  revise: ["file"],
@@ -169,6 +219,8 @@ const FLAGS = {
169
219
  agents: [],
170
220
  invite: ["name", "description", "permissions"],
171
221
  "start-cloud-agent": ["name", "repo", "task", "branch", "open-pr", "machine", "agent"],
222
+ "move-agent": ["under"],
223
+ call: ["query", "body", "for-person"],
172
224
  me: [],
173
225
  "my-queue": ["view", "before", "limit"],
174
226
  "decide-mine": ["option", "text", "note", "their-words"],
@@ -182,10 +234,73 @@ const FLAGS = {
182
234
  "todo-cant": ["reason", "their-words"],
183
235
  "decided-without-you": ["date"],
184
236
  overrule: ["text", "note", "their-words"],
237
+ "my-settings": [],
238
+ "set-my-day": ["timezone", "day-start", "day-end", "morning", "later-every"],
239
+ "set-my-alerts": ["on-call", "sounds", "sound", "later-like-now", "muted", "workspace-sound", "snooze-until"],
240
+ "set-my-reports": ["weekly-report", "slack-messages"],
241
+ "my-notifications": ["limit"],
242
+ "mark-notification-read": [],
243
+ "my-weekly-report": [],
244
+ "my-trusts": [],
245
+ "add-trust": ["agent", "words", "area"],
246
+ "edit-trust": ["words", "area"],
247
+ "revoke-trust": [],
248
+ "trust-decisions": [],
249
+ "trust-suggestions": [],
250
+ "my-agents": [],
251
+ "edit-agent": ["name", "title", "description"],
252
+ "show-agent": ["visible"],
253
+ "pause-agent": ["paused"],
254
+ "set-agent-permissions": ["permissions", "may-invite", "may-ask-person"],
255
+ "add-sub-agent": ["name", "description", "permissions"],
256
+ "move-agent-mine": ["under"],
257
+ "archive-agent": [],
258
+ "remove-agent": ["their-words"],
259
+ "my-blockers": [],
260
+ "nudge-mine": [],
261
+ reassign: ["to-person", "to-agent", "note"],
185
262
  };
186
263
  // Your person's own actions, sent as them (X-Gavel-For-Person) once they've let you act for them
187
264
  // (D-911): the MCP tools' names.
188
- const FOR_PERSON = ["my-queue", "decide-mine", "forward-mine", "boomerang", "research-more", "ask-someone", "defer", "undo", "todo-done", "todo-cant", "decided-without-you", "overrule"];
265
+ const FOR_PERSON = [
266
+ "my-queue",
267
+ "decide-mine",
268
+ "forward-mine",
269
+ "boomerang",
270
+ "research-more",
271
+ "ask-someone",
272
+ "defer",
273
+ "undo",
274
+ "todo-done",
275
+ "todo-cant",
276
+ "decided-without-you",
277
+ "overrule",
278
+ "my-settings",
279
+ "set-my-day",
280
+ "set-my-alerts",
281
+ "set-my-reports",
282
+ "my-notifications",
283
+ "mark-notification-read",
284
+ "my-weekly-report",
285
+ "my-trusts",
286
+ "add-trust",
287
+ "edit-trust",
288
+ "revoke-trust",
289
+ "trust-decisions",
290
+ "trust-suggestions",
291
+ "my-agents",
292
+ "edit-agent",
293
+ "show-agent",
294
+ "pause-agent",
295
+ "set-agent-permissions",
296
+ "add-sub-agent",
297
+ "move-agent-mine",
298
+ "archive-agent",
299
+ "remove-agent",
300
+ "my-blockers",
301
+ "nudge-mine",
302
+ "reassign",
303
+ ];
189
304
  const isCommand = (name) => Object.hasOwn(FLAGS, name);
190
305
  // A command's own entries in USAGE: each line naming it, with the lines indented under it.
191
306
  export function usageOf(command) {
@@ -205,7 +320,7 @@ class UsageError extends Error {
205
320
  }
206
321
  // Only --option (a multiple question's picks), --code and --label (a work item's commits and pull
207
322
  // requests, and its labels) may come again: `flags` holds each flag's value and `lists` every one given.
208
- const REPEATABLE = ["option", "code", "label"];
323
+ const REPEATABLE = ["option", "code", "label", "query"];
209
324
  export function parseArgs(argv, command) {
210
325
  const positional = [];
211
326
  const flags = {};
@@ -305,6 +420,22 @@ function personRequest(command, p, f, options) {
305
420
  const theirWords = () => readText(need(f["their-words"], "--their-words: what your person said in your chat that decides it"));
306
421
  const list = (value) => value?.split(",").map((s) => s.trim()).filter(Boolean) ?? [];
307
422
  const asThem = (method, path, body) => ({ method, path, ...(body ? { body: compact(body) } : {}), forPerson: true });
423
+ const yesNo = (flag) => {
424
+ const v = f[flag];
425
+ if (v === undefined)
426
+ return undefined;
427
+ if (v !== "true" && v !== "false")
428
+ throw new UsageError(`--${flag} is true or false`);
429
+ return v === "true";
430
+ };
431
+ const orNone = (flag) => (f[flag] === undefined ? undefined : f[flag] === "none" ? null : f[flag]);
432
+ const changes = (body) => {
433
+ const set = compact(body);
434
+ if (!Object.keys(set).length)
435
+ throw new UsageError(`${command} needs something to change`);
436
+ return asThem("PATCH", "/v1/me", set);
437
+ };
438
+ const id = (what) => encodeURIComponent(need(p[0], what));
308
439
  switch (command) {
309
440
  case "my-queue": {
310
441
  upTo(0);
@@ -351,6 +482,121 @@ function personRequest(command, p, f, options) {
351
482
  case "overrule":
352
483
  upTo(1);
353
484
  return asThem("POST", `/v1/decisions/${ref(p[0])}/overrule`, { text: f.text && readText(f.text), note: f.note, theirWords: f["their-words"] && readText(f["their-words"]) });
485
+ case "my-settings":
486
+ upTo(0);
487
+ return asThem("GET", "/v1/me");
488
+ case "set-my-day": {
489
+ upTo(0);
490
+ const later = f["later-every"] === undefined ? undefined : Number(f["later-every"]);
491
+ if (later !== undefined && !Number.isInteger(later))
492
+ throw new UsageError("--later-every is a number of minutes");
493
+ return changes({ timezone: f.timezone, dayStart: f["day-start"], dayEnd: f["day-end"], morningTime: f.morning, laterEveryMinutes: later });
494
+ }
495
+ case "set-my-alerts":
496
+ upTo(0);
497
+ return changes({
498
+ onCall: yesNo("on-call"),
499
+ sounds: f.sounds,
500
+ alertSound: f.sound,
501
+ laterLikeNow: yesNo("later-like-now"),
502
+ muted: yesNo("muted"),
503
+ workspaceSound: orNone("workspace-sound"),
504
+ snoozedUntil: orNone("snooze-until"),
505
+ });
506
+ case "set-my-reports":
507
+ upTo(0);
508
+ return changes({ weeklyReport: yesNo("weekly-report"), slackMessages: yesNo("slack-messages") });
509
+ case "my-notifications":
510
+ upTo(0);
511
+ return asThem("GET", `/v1/notifications${f.limit ? `?limit=${encodeURIComponent(f.limit)}` : ""}`);
512
+ case "mark-notification-read":
513
+ upTo(1);
514
+ return asThem("POST", `/v1/notifications/${id("the notification's id")}/read`);
515
+ case "my-weekly-report":
516
+ upTo(0);
517
+ return asThem("GET", "/v1/weekly-report");
518
+ case "my-trusts":
519
+ upTo(0);
520
+ return asThem("GET", "/v1/trusts");
521
+ case "add-trust":
522
+ upTo(0);
523
+ return asThem("POST", "/v1/trusts", { agentId: need(f.agent, "--agent"), plainWords: readText(need(f.words, "--words")), areaId: f.area });
524
+ case "edit-trust": {
525
+ upTo(1);
526
+ const trust = id("the trust's id");
527
+ const body = compact({ plainWords: f.words && readText(f.words), areaId: orNone("area") });
528
+ if (!Object.keys(body).length)
529
+ throw new UsageError("edit-trust needs --words or --area");
530
+ return asThem("PATCH", `/v1/trusts/${trust}`, body);
531
+ }
532
+ case "revoke-trust":
533
+ upTo(1);
534
+ return asThem("DELETE", `/v1/trusts/${id("the trust's id")}`);
535
+ case "trust-decisions":
536
+ upTo(1);
537
+ return asThem("GET", `/v1/trusts/${id("the trust's id")}/decisions`);
538
+ case "trust-suggestions":
539
+ upTo(1);
540
+ return asThem("GET", `/v1/trusts/suggestions?decision=${ref(p[0])}`);
541
+ case "my-agents":
542
+ upTo(0);
543
+ return asThem("GET", "/v1/agents");
544
+ case "edit-agent": {
545
+ upTo(1);
546
+ const agent = id("the agent's id");
547
+ const body = compact({ name: f.name, title: orNone("title"), description: f.description && readText(f.description) });
548
+ if (!Object.keys(body).length)
549
+ throw new UsageError("edit-agent needs --name, --title or --description");
550
+ return asThem("PATCH", `/v1/agents/${agent}`, body);
551
+ }
552
+ case "show-agent": {
553
+ upTo(1);
554
+ const agent = id("the agent's id");
555
+ const visible = yesNo("visible");
556
+ if (visible === undefined)
557
+ throw new UsageError("show-agent needs --visible true or false");
558
+ return asThem("PATCH", `/v1/agents/${agent}`, { visibleToOrg: visible });
559
+ }
560
+ case "pause-agent": {
561
+ upTo(1);
562
+ const agent = id("the agent's id");
563
+ return asThem("PATCH", `/v1/agents/${agent}`, { status: yesNo("paused") === false ? "active" : "paused" });
564
+ }
565
+ case "set-agent-permissions": {
566
+ upTo(1);
567
+ const agent = id("the agent's id");
568
+ const body = compact({ permissions: f.permissions === undefined ? undefined : list(f.permissions), mayInvite: yesNo("may-invite"), mayAskPersonDirectly: yesNo("may-ask-person") });
569
+ if (!Object.keys(body).length)
570
+ throw new UsageError("set-agent-permissions needs --permissions, --may-invite or --may-ask-person");
571
+ return asThem("PATCH", `/v1/agents/${agent}`, body);
572
+ }
573
+ case "add-sub-agent": {
574
+ upTo(1);
575
+ const parent = id("the agent to add it under");
576
+ return asThem("POST", `/v1/agents/${parent}/sub-agents`, {
577
+ name: need(f.name, "--name"),
578
+ description: f.description && readText(f.description),
579
+ permissions: f.permissions === undefined ? undefined : list(f.permissions),
580
+ });
581
+ }
582
+ case "move-agent-mine":
583
+ upTo(1);
584
+ return asThem("PATCH", `/v1/agents/${id("the agent's id")}`, { parentAgentId: need(f.under, "--under") });
585
+ case "archive-agent":
586
+ upTo(1);
587
+ return asThem("POST", `/v1/agents/${id("the agent's id")}/archive`);
588
+ case "remove-agent":
589
+ upTo(1);
590
+ return asThem("DELETE", `/v1/agents/${id("the agent's id")}`, { theirWords: theirWords() });
591
+ case "my-blockers":
592
+ upTo(0);
593
+ return asThem("GET", "/v1/blockers");
594
+ case "nudge-mine":
595
+ upTo(1);
596
+ return asThem("POST", `/v1/decisions/${ref(p[0])}/nudge`);
597
+ case "reassign":
598
+ upTo(1);
599
+ return asThem("POST", `/v1/decisions/${ref(p[0])}/reassign`, { toPerson: f["to-person"], toAgentId: f["to-agent"], note: f.note });
354
600
  default:
355
601
  throw new UsageError(`unknown command "${command}"`);
356
602
  }
@@ -393,6 +639,7 @@ const attachBody = (file) => {
393
639
  const json = readJson(file);
394
640
  return json && typeof json === "object" && !Array.isArray(json) && SECTIONS.some((k) => k in json) ? json : { items: Array.isArray(json) ? json : [json] };
395
641
  };
642
+ const CALL_METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE"];
396
643
  // --as <name> (or --as=<name>) may come anywhere on the line; it's the caller's, not the command's.
397
644
  export function takeAs(argv) {
398
645
  const rest = [];
@@ -462,6 +709,9 @@ export function toRequest(argv) {
462
709
  throw new UsageError(`--harness is one of ${BRIDGE_HARNESSES.join(", ")}`);
463
710
  return { bridge: { action, harness, server: f.server } };
464
711
  }
712
+ case "actions-env":
713
+ upTo(0);
714
+ return { actionsEnv: true };
465
715
  case "watch":
466
716
  upTo(0);
467
717
  return { watch: true };
@@ -613,6 +863,34 @@ export function toRequest(argv) {
613
863
  agentId: f.agent,
614
864
  }),
615
865
  };
866
+ case "move-agent":
867
+ upTo(1);
868
+ return { method: "PATCH", path: `/v1/agents/${encodeURIComponent(need(p[0], "the agent's id"))}`, body: { parentAgentId: need(f.under, "--under") } };
869
+ case "call": {
870
+ upTo(2);
871
+ const method = CALL_METHODS.find((m) => m === need(p[0], "the method: GET, POST, PUT, PATCH or DELETE").toUpperCase());
872
+ if (!method)
873
+ throw new UsageError(`call's method is GET, POST, PUT, PATCH or DELETE, not "${p[0]}"`);
874
+ const path = need(p[1], "the path, like /v1/blockers");
875
+ if (!path.startsWith("/v1/") || path.split("?")[0].split("/").includes(".."))
876
+ throw new UsageError("call's path is one under /v1/, like /v1/blockers");
877
+ const query = new URLSearchParams();
878
+ for (const pair of lists.query ?? []) {
879
+ const eq = pair.indexOf("=");
880
+ if (eq < 1)
881
+ throw new UsageError(`--query is name=value, not "${pair}"`);
882
+ query.append(pair.slice(0, eq), pair.slice(eq + 1));
883
+ }
884
+ const forPerson = f["for-person"];
885
+ if (forPerson !== undefined && forPerson !== "true" && forPerson !== "false")
886
+ throw new UsageError("--for-person is true or false");
887
+ return {
888
+ method,
889
+ path: query.size ? `${path}${path.includes("?") ? "&" : "?"}${query}` : path,
890
+ ...(f.body !== undefined ? { body: readJson(f.body) } : {}),
891
+ ...(forPerson === "true" ? { forPerson: true } : {}),
892
+ };
893
+ }
616
894
  case "me":
617
895
  upTo(0);
618
896
  return { method: "GET", path: "/v1/me" };
@@ -998,6 +1276,62 @@ async function watch(token, as) {
998
1276
  console.log(wakeLine(item));
999
1277
  }
1000
1278
  }
1279
+ // ---------- GitHub Actions (server/src/features/automations) ----------
1280
+ // A job with permissions: id-token: write can ask GitHub for its run's own token.
1281
+ export function inGitHubActions(env) {
1282
+ return !!env.ACTIONS_ID_TOKEN_REQUEST_URL && !!env.ACTIONS_ID_TOKEN_REQUEST_TOKEN;
1283
+ }
1284
+ // The run's own Gavel token, traded for GitHub's token for the run (asked for with Gavel's address as
1285
+ // its audience), as the sub-agent named by the run's task (--as, GAVEL_AGENT; else the server names
1286
+ // it by the workflow and run number). Kept in the job's temp folder, which goes with the job, so a job
1287
+ // trades once per name until five minutes before its hour is up. Never printed.
1288
+ async function runToken(base, name, env) {
1289
+ const named = name?.trim() || null;
1290
+ const kept = env.RUNNER_TEMP ? join(env.RUNNER_TEMP, `gavel-run-${createHash("sha256").update(`${base}\n${named ?? ""}`).digest("hex").slice(0, 16)}.json`) : null;
1291
+ if (kept && existsSync(kept)) {
1292
+ try {
1293
+ const run = JSON.parse(readFileSync(kept, "utf8"));
1294
+ if (new Date(run.expiresAt).getTime() - Date.now() > 5 * 60_000)
1295
+ return run;
1296
+ }
1297
+ catch {
1298
+ // Unreadable: trade again.
1299
+ }
1300
+ }
1301
+ const github = await fetch(`${env.ACTIONS_ID_TOKEN_REQUEST_URL}&audience=${encodeURIComponent(base)}`, {
1302
+ headers: { authorization: `bearer ${env.ACTIONS_ID_TOKEN_REQUEST_TOKEN}`, accept: "application/json" },
1303
+ });
1304
+ const proof = (await github.json().catch(() => ({})));
1305
+ if (!github.ok || !proof.value)
1306
+ throw new Error(`GitHub didn't give this run its token (HTTP ${github.status}): the job needs permissions: id-token: write`);
1307
+ const res = await fetch(`${base}/v1/automations/runs`, {
1308
+ method: "POST",
1309
+ headers: { "content-type": "application/json", accept: "application/json" },
1310
+ body: JSON.stringify({ githubToken: proof.value, ...(named ? { name: named } : {}), ...(env.GAVEL_WORKSPACE ? { workspace: env.GAVEL_WORKSPACE } : {}) }),
1311
+ });
1312
+ const body = (await res.json().catch(() => ({})));
1313
+ if (!res.ok)
1314
+ throw new Error(body.error?.message ?? `Gavel refused this run (HTTP ${res.status})`);
1315
+ if (kept)
1316
+ writeFileSync(kept, JSON.stringify(body), { mode: 0o600 });
1317
+ return body;
1318
+ }
1319
+ // For an agent in the job that speaks MCP: the run's token for the steps after this one, through the
1320
+ // job's GITHUB_ENV, masked first, GitHub's own way, so its log never shows it. The mask line is the
1321
+ // one line that holds the token, and the runner takes it rather than printing it.
1322
+ async function actionsEnv(as) {
1323
+ const env = process.env;
1324
+ if (!inGitHubActions(env) || !env.GITHUB_ENV) {
1325
+ console.error("gavel actions-env: run it in a GitHub Actions job with permissions: id-token: write");
1326
+ return 2;
1327
+ }
1328
+ const base = (env.GAVEL_API_URL ?? "https://api.getgavel.ai").replace(/\/+$/, "");
1329
+ const run = await runToken(base, as, env);
1330
+ console.log(`::add-mask::${run.token}`);
1331
+ appendFileSync(env.GITHUB_ENV, `GAVEL_TOKEN=${run.token}\nGAVEL_API_URL=${base}\nGAVEL_MCP_URL=${run.mcpUrl}\n`);
1332
+ console.log(JSON.stringify({ set: ["GAVEL_TOKEN", "GAVEL_API_URL", "GAVEL_MCP_URL"], agent: run.agent, workspace: run.workspace, server: run.server, expiresAt: run.expiresAt }, null, 2));
1333
+ return 0;
1334
+ }
1001
1335
  async function main(argvIn) {
1002
1336
  let request;
1003
1337
  let as = null;
@@ -1024,16 +1358,18 @@ async function main(argvIn) {
1024
1358
  return pair(request.pair);
1025
1359
  if ("bridge" in request)
1026
1360
  return runBridge(request.bridge, { cwd: process.cwd(), home: homedir(), env: process.env, as });
1027
- const token = process.env.GAVEL_TOKEN;
1361
+ if ("actionsEnv" in request)
1362
+ return actionsEnv(as);
1363
+ const base = (process.env.GAVEL_API_URL ?? "https://api.getgavel.ai").replace(/\/+$/, "");
1364
+ const token = process.env.GAVEL_TOKEN || (inGitHubActions(process.env) ? (await runToken(base, as, process.env)).token : undefined);
1028
1365
  if (!token) {
1029
- console.error("gavel: set GAVEL_TOKEN to your agent token (gvl_agent_…)");
1366
+ console.error("gavel: set GAVEL_TOKEN to your agent token (gvl_agent_…), or in a GitHub Actions job give it permissions: id-token: write");
1030
1367
  return 2;
1031
1368
  }
1032
1369
  if ("watch" in request)
1033
1370
  return watch(token, as);
1034
1371
  if ("upload" in request)
1035
1372
  return upload(token, as, request.upload);
1036
- const base = (process.env.GAVEL_API_URL ?? "https://api.getgavel.ai").replace(/\/+$/, "");
1037
1373
  const res = await fetch(base + request.path, {
1038
1374
  method: request.method,
1039
1375
  headers: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@getgavel/cli",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "description": "The gavel command: an agent's Gavel from a shell (pair, ask, wait, watch and the rest)",
5
5
  "license": "UNLICENSED",
6
6
  "homepage": "https://getgavel.ai/agents",