ticket-runner 0.6.2 → 0.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/templates.ts CHANGED
@@ -4,6 +4,13 @@
4
4
  */
5
5
 
6
6
  import { UNCHECKED_BOX } from "./acceptance-criteria.js";
7
+ import {
8
+ type Billing,
9
+ CREDENTIAL_WORDS,
10
+ type Credential,
11
+ type StrayStage,
12
+ emptyBilling,
13
+ } from "./credential.js";
7
14
  import type { GuardReason } from "./guards.js";
8
15
  import type { HostKind } from "./host.js";
9
16
  import type { FailurePoint } from "./lifecycle.js";
@@ -513,7 +520,15 @@ export interface RunSummary {
513
520
  version: string;
514
521
  runId: string;
515
522
  durationMs: number;
523
+ /** What the Run's Stages were billed to, which its header names. */
524
+ credential: Credential;
516
525
  outcomes: TicketOutcome[];
526
+ /**
527
+ * What the Run's Stages said they were billed: the Spend or extra usage its
528
+ * header gives, and every Stage that said it ran on something else, each
529
+ * named in a row after its own Ticket's.
530
+ */
531
+ billing?: Billing;
517
532
  /**
518
533
  * The line saying a newer Version is out, where one is. Above the header
519
534
  * rather than below it: the Run already said this hours ago, at the top of a
@@ -526,34 +541,52 @@ export interface RunSummary {
526
541
 
527
542
  /**
528
543
  * What a Release is put down to, in the words the Run summary and the log
529
- * share: the subscription rate limit, a transient API failure, or a Base
530
- * branch that fails the Check the Ticket's branch failed.
544
+ * share: the rate limit, a transient API failure, an API key out of credit or
545
+ * past its spend limit, or a Base branch that fails the Check the Ticket's
546
+ * branch failed.
531
547
  */
532
548
  export const RELEASE_WORDS: Record<ReleaseCause, string> = {
533
549
  "rate-limited": "rate limit",
534
550
  "api-error": "API failure",
551
+ "out-of-budget": "out of API budget",
535
552
  "failing-base": "failing Base branch",
536
553
  };
537
554
 
555
+ /**
556
+ * The same words, for a Run on `credential`. A rate limit on an `--api-key`
557
+ * Run is the API's, not the subscription's, and is named so: what the human
558
+ * looks at about it is the Console, not their plan.
559
+ */
560
+ export function releaseWords(cause: ReleaseCause, credential: Credential): string {
561
+ return cause === "rate-limited" && credential === "api-key"
562
+ ? "API rate limit"
563
+ : RELEASE_WORDS[cause];
564
+ }
565
+
538
566
  /** Every summary row is `<verb> #<n> <detail>`, so the numbers line up. */
539
567
  const VERB_WIDTH = 9;
540
568
 
541
569
  /**
542
570
  * What a Run prints when it ends. One line per Ticket, then why the Run
543
571
  * stopped — the Frontier is empty, everything left on it is blocked, the rate
544
- * limit, a transient API failure or a failing Base branch released a Ticket and
545
- * the Run went no further, or a human stopped it.
572
+ * limit, a transient API failure, the API key's budget or a failing Base branch
573
+ * released a Ticket and the Run went no further, or a human stopped it.
546
574
  */
547
575
  export function runSummary({
548
576
  version,
549
577
  runId,
550
578
  durationMs,
579
+ credential,
551
580
  outcomes,
581
+ billing = emptyBilling(),
552
582
  newer,
553
583
  stop,
554
584
  }: RunSummary): string {
555
585
  const rows = [
556
- ...outcomes.flatMap(ticketRows),
586
+ ...outcomes.flatMap((outcome) => [
587
+ ...ticketRows(outcome, credential),
588
+ ...billing.strays.filter((stray) => stray.ticket === outcome.ticket).map(billedRow),
589
+ ]),
557
590
  // Only a Run that reached the end of the Frontier can name what was held
558
591
  // back all Run, so only that stop carries candidates to skip.
559
592
  ...(stop.reason === "frontier"
@@ -563,24 +596,60 @@ export function runSummary({
563
596
 
564
597
  return [
565
598
  ...(newer === undefined ? [] : [newer]),
566
- `ticket-runner ${version} run ${runId} · ${Math.round(durationMs / 60_000)}m`,
599
+ `ticket-runner ${version} run ${runId} · ${Math.round(durationMs / 60_000)}m · ${billedTo(credential, billing)}`,
567
600
  "",
568
601
  ...(rows.length === 0 ? [" nothing to do"] : rows),
569
602
  "",
570
- lastLine(stop),
603
+ lastLine(stop, credential),
571
604
  "",
572
605
  ].join("\n");
573
606
  }
574
607
 
608
+ /**
609
+ * What the header says the Run's Stages were billed to. An `--api-key` Run
610
+ * adds its Spend, ending `+` where a Stage on the key reported no figure, so it
611
+ * is at least that much. A subscription Run adds no amount, since its figure
612
+ * is an estimate at API prices and not money spent, only whether a Stage ran
613
+ * on the plan's extra usage.
614
+ */
615
+ function billedTo(credential: Credential, billing: Billing): string {
616
+ const words = CREDENTIAL_WORDS[credential];
617
+ if (credential === "api-key") {
618
+ return `${words} · ${dollars(billing.spendUsd)}${billing.spendUnreported ? "+" : ""}`;
619
+ }
620
+ return billing.extraUsage ? `${words} · extra usage` : words;
621
+ }
622
+
623
+ /**
624
+ * A Stage billed somewhere the Run did not choose, with its own figure where it
625
+ * gave one, ending `+` where that is only part of its session's.
626
+ */
627
+ function billedRow(stray: StrayStage): string {
628
+ const cost =
629
+ stray.costUsd === undefined
630
+ ? ""
631
+ : ` · ${dollars(stray.costUsd)}${stray.costIncomplete === true ? "+" : ""}`;
632
+ return row("billed", stray.ticket, `${stray.stage} · apiKeySource ${stray.apiKeySource}${cost}`);
633
+ }
634
+
635
+ /** An amount of dollars as the Run summary prints one: `$12.34`. */
636
+ function dollars(usd: number): string {
637
+ return `$${usd.toFixed(2)}`;
638
+ }
639
+
575
640
  /** The one line that says why the Run stopped. */
576
- function lastLine(stop: RunStop): string {
641
+ function lastLine(stop: RunStop, credential: Credential): string {
577
642
  switch (stop.reason) {
578
- case "rate-limited":
643
+ case "rate-limited": {
644
+ const limited = credential === "api-key" ? "API rate limited" : "Rate limited";
579
645
  return stop.resetsAt === undefined
580
- ? "Rate limited."
581
- : `Rate limited · resets ${utcMinute(stop.resetsAt)}.`;
646
+ ? `${limited}.`
647
+ : `${limited} · resets ${utcMinute(stop.resetsAt)}.`;
648
+ }
582
649
  case "api-error":
583
650
  return "API failure.";
651
+ case "out-of-budget":
652
+ return "Out of API budget.";
584
653
  case "failing-base":
585
654
  return `Base branch \`${stop.baseBranch}\` fails \`${stop.command}\` at ${stop.commit.slice(0, 7)}.`;
586
655
  case "stopped":
@@ -618,9 +687,9 @@ function holding(busy: number[]): string {
618
687
  * The Notes follow their own Ticket rather than being gathered at the end, so a
619
688
  * Run that took six Tickets still says which one noticed what.
620
689
  */
621
- function ticketRows(outcome: TicketOutcome): string[] {
690
+ function ticketRows(outcome: TicketOutcome, credential: Credential): string[] {
622
691
  return [
623
- ticketRow(outcome),
692
+ ticketRow(outcome, credential),
624
693
  ...(outcome.outcome === "skipped" ? [] : outcome.notes.map(noteRow)),
625
694
  ];
626
695
  }
@@ -653,7 +722,7 @@ function noteRow({ origin, stage, issue, opened, summary }: RoutedNote): string
653
722
  }
654
723
 
655
724
  /** One Ticket's line in a summary: what happened to it, and where to look. */
656
- function ticketRow(outcome: TicketOutcome): string {
725
+ function ticketRow(outcome: TicketOutcome, credential: Credential): string {
657
726
  switch (outcome.outcome) {
658
727
  case "merged":
659
728
  return row("merged", outcome.ticket, `${outcome.title} (PR #${outcome.pullRequest})`);
@@ -669,7 +738,7 @@ function ticketRow(outcome: TicketOutcome): string {
669
738
  return row(
670
739
  "released",
671
740
  outcome.ticket,
672
- `${outcome.title} · ${RELEASE_WORDS[outcome.cause]} at ${outcome.stage}`,
741
+ `${outcome.title} · ${releaseWords(outcome.cause, credential)} at ${outcome.stage}`,
673
742
  );
674
743
  case "skipped":
675
744
  // The reason is the guard's own word for it, which is also the marker on
package/src/timer.ts ADDED
@@ -0,0 +1,25 @@
1
+ /**
2
+ * A wait the caller can call off before it elapses.
3
+ *
4
+ * Called off rather than left to run out, because a pending timer keeps Node's
5
+ * process alive: a Run that ended while one was still counting down would
6
+ * hold the terminal until it did.
7
+ */
8
+ export interface Timer {
9
+ /** Resolves once the wait has elapsed, and never if it was called off first. */
10
+ elapsed: Promise<void>;
11
+ /** Call the wait off. Calling it off again, or after it elapsed, does nothing. */
12
+ cancel(): void;
13
+ }
14
+
15
+ /** Start a wait of `ms` milliseconds. */
16
+ export type StartTimer = (ms: number) => Timer;
17
+
18
+ /** A wait on the real clock. */
19
+ export const startTimer: StartTimer = (ms) => {
20
+ let handle: ReturnType<typeof setTimeout> | undefined;
21
+ const elapsed = new Promise<void>((resolve) => {
22
+ handle = setTimeout(resolve, ms);
23
+ });
24
+ return { elapsed, cancel: () => clearTimeout(handle) };
25
+ };
package/src/title.ts CHANGED
@@ -22,7 +22,7 @@ export const BRANCH_TITLE =
22
22
  /** Any `<type>(<scope>): <summary>`; the repo's own types and scopes are CONTRIBUTING.md's business. */
23
23
  const CONVENTIONAL_SUBJECT = /^[a-z]+(\([a-z0-9._-]+\))?: \S/;
24
24
 
25
- /** The `(#<n>)` a branch commit carries, which the squash commit's `Closes #<n>` replaces. */
25
+ /** The `(#<n>)` a commit may carry, which the squash commit's `Closes #<n>` replaces. */
26
26
  const TICKET_REFERENCE = /\s*\(#\d+\)$/;
27
27
 
28
28
  /**