@dench.com/cli 3.11.50-staging.923 → 3.11.52-staging.960

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/cron.ts CHANGED
@@ -502,13 +502,24 @@ async function runCreate(ctx: CronCliContext): Promise<void> {
502
502
  })) as { jobId?: string };
503
503
 
504
504
  if (isWebhook && created.jobId) {
505
+ // A webhook created --disabled answers every request 403; its note must
506
+ // never read as if the URL works yet (DEN-512).
507
+ const jobId = created.jobId;
508
+ const noteFor = (note: string) =>
509
+ disabled
510
+ ? `Saved turned off (--disabled): the URL answers every request 403 until \`dench cron enable ${jobId}\`. After that: ${note}`
511
+ : note;
512
+ const state = disabled ? { enabled: false } : {};
505
513
  if (unsigned) {
506
514
  out(ctx, {
507
515
  ...created,
516
+ ...state,
508
517
  webhookUrl: automationWebhookUrl(ctx, created.jobId),
509
518
  webhookAccess: "open",
510
519
  warning: UNSIGNED_WEBHOOK_WARNING,
511
- note: "Plain POSTs to webhookUrl fire this automation. `dench cron secret <jobId>` turns signing back on.",
520
+ note: noteFor(
521
+ "Plain POSTs to webhookUrl fire this automation. `dench cron secret <jobId>` turns signing back on.",
522
+ ),
512
523
  });
513
524
  return;
514
525
  }
@@ -519,6 +530,7 @@ async function runCreate(ctx: CronCliContext): Promise<void> {
519
530
  // Created, signing required, no secret yet: the URL accepts nothing.
520
531
  out(ctx, {
521
532
  ...created,
533
+ ...state,
522
534
  webhookUrl: automationWebhookUrl(ctx, created.jobId),
523
535
  webhookAccess: "off",
524
536
  note: `Created with signing on, but its secret could not be made (${error instanceof Error ? error.message : String(error)}). The URL accepts no requests until you run \`dench cron secret ${created.jobId}\`.`,
@@ -527,8 +539,11 @@ async function runCreate(ctx: CronCliContext): Promise<void> {
527
539
  }
528
540
  out(ctx, {
529
541
  ...created,
542
+ ...state,
530
543
  ...secret,
531
- note: "Signed: every POST to webhookUrl must carry a valid HMAC signature. The secret is shown once; store it now (`dench cron secret <jobId>` rotates it).",
544
+ note: noteFor(
545
+ "Signed: every POST to webhookUrl must carry a valid HMAC signature. The secret is shown once; store it now (`dench cron secret <jobId>` rotates it).",
546
+ ),
532
547
  });
533
548
  return;
534
549
  }
package/dench.ts CHANGED
@@ -27,6 +27,7 @@ import {
27
27
  resolveHostFromArgs,
28
28
  STAGING_HOST,
29
29
  } from "./host";
30
+ import { reportCliApiKeyUse } from "./lib/api-key-usage";
30
31
  import { CliArgError, isHelpRequest } from "./lib/cli-args";
31
32
  import { cliVersion } from "./lib/cli-version";
32
33
  import { assertDeclaredCommandFlags } from "./lib/command-args";
@@ -2574,6 +2575,8 @@ function missingDevEnvError() {
2574
2575
  });
2575
2576
  }
2576
2577
 
2578
+ let apiKeyUseReported = false;
2579
+
2577
2580
  async function getRuntime() {
2578
2581
  const scope = resolveSessionScope({ args: filteredArgs });
2579
2582
  if (!hasFlag("--dev")) {
@@ -2634,11 +2637,18 @@ async function getRuntime() {
2634
2637
  const apiKey = process.env.DENCH_API_KEY?.trim();
2635
2638
  if (apiKey && sandboxConvexUrl) {
2636
2639
  const apiHost = resolveApiHost(host);
2640
+ const client = createCliConvexClient(sandboxConvexUrl);
2641
+ // DEN-481: show this key's use as "Last used" in Settings → API keys.
2642
+ // Runs alongside the command, at most once per key per window.
2643
+ if (!apiKeyUseReported) {
2644
+ apiKeyUseReported = true;
2645
+ void reportCliApiKeyUse({ client, apiKey, configDir: CONFIG_DIR });
2646
+ }
2637
2647
  return {
2638
2648
  mode: "apiKey" as const,
2639
2649
  host: apiHost,
2640
2650
  convexUrl: sandboxConvexUrl,
2641
- client: createCliConvexClient(sandboxConvexUrl),
2651
+ client,
2642
2652
  sessionToken: apiKey,
2643
2653
  organization: sandboxOrganization,
2644
2654
  };
@@ -0,0 +1,104 @@
1
+ import { createHmac } from "node:crypto";
2
+ import { mkdir, readFile, writeFile } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+ import type { ConvexHttpClient } from "convex/browser";
5
+ import { makeFunctionReference } from "convex/server";
6
+
7
+ /**
8
+ * DEN-481: a command run with `DENCH_API_KEY` shows up as "Last used" in
9
+ * Settings → API keys. Most CLI reads resolve the key in Convex queries,
10
+ * which cannot write, so the CLI reports the use once through
11
+ * `functions/apiKeyUsage:recordUse`, which throttles per key on the server.
12
+ *
13
+ * This side remembers when it last reported each key (by an id derived from
14
+ * the key, never the key) in the CLI config directory and skips the call
15
+ * within the window, so a script running hundreds of commands makes one call
16
+ * per window, not one per command. The report never waits behind, or holds
17
+ * up, the command's own mutations, and nothing here ever fails a command.
18
+ */
19
+
20
+ const recordUse = makeFunctionReference<
21
+ "mutation",
22
+ { apiKey: string },
23
+ boolean
24
+ >("functions/apiKeyUsage:recordUse");
25
+
26
+ /** Matches `API_KEY_LAST_USED_THROTTLE_MS` in convex/lib/denchApiKey.ts. */
27
+ export const API_KEY_USE_REPORT_INTERVAL_MS = 5 * 60_000;
28
+ const STATE_FILE = "api-key-usage.json";
29
+ const MAX_REMEMBERED_KEYS = 50;
30
+
31
+ type ReportState = Record<string, number>;
32
+
33
+ const KEY_ID_LABEL = "dench-cli-api-key-usage-v1";
34
+
35
+ /**
36
+ * A stable, non-reversible id for the key: an HMAC keyed by the key over a
37
+ * fixed label. Unlike a plain sha256 it never equals the server's
38
+ * `keyHash`, so the local state file cannot be matched against that column.
39
+ */
40
+ function keyId(apiKey: string) {
41
+ return createHmac("sha256", apiKey)
42
+ .update(KEY_ID_LABEL)
43
+ .digest("hex")
44
+ .slice(0, 32);
45
+ }
46
+
47
+ async function readState(configDir: string): Promise<ReportState> {
48
+ try {
49
+ const parsed = JSON.parse(
50
+ await readFile(join(configDir, STATE_FILE), "utf8"),
51
+ ) as unknown;
52
+ return parsed && typeof parsed === "object" && !Array.isArray(parsed)
53
+ ? (parsed as ReportState)
54
+ : {};
55
+ } catch {
56
+ return {};
57
+ }
58
+ }
59
+
60
+ async function writeState(configDir: string, state: ReportState) {
61
+ const newest = Object.entries(state)
62
+ .filter(([, at]) => typeof at === "number")
63
+ .sort(([, a], [, b]) => b - a)
64
+ .slice(0, MAX_REMEMBERED_KEYS);
65
+ await mkdir(configDir, { recursive: true });
66
+ await writeFile(
67
+ join(configDir, STATE_FILE),
68
+ `${JSON.stringify(Object.fromEntries(newest))}\n`,
69
+ { mode: 0o600 },
70
+ );
71
+ }
72
+
73
+ /**
74
+ * Reports that `apiKey` was used, unless this machine already did within the
75
+ * window. Resolves to whether a report was sent; never rejects.
76
+ */
77
+ export async function reportCliApiKeyUse(input: {
78
+ client: Pick<ConvexHttpClient, "mutation">;
79
+ apiKey: string;
80
+ configDir: string;
81
+ now?: number;
82
+ }): Promise<boolean> {
83
+ try {
84
+ const apiKey = input.apiKey.trim();
85
+ if (!apiKey || apiKey.startsWith("dch_agent_")) return false;
86
+ const now = input.now ?? Date.now();
87
+ const id = keyId(apiKey);
88
+ const state = await readState(input.configDir);
89
+ const last = state[id];
90
+ if (
91
+ typeof last === "number" &&
92
+ now - last < API_KEY_USE_REPORT_INTERVAL_MS
93
+ ) {
94
+ return false;
95
+ }
96
+ // `skipQueue`: the client queues mutations one at a time, so a queued
97
+ // report would hold the command's own first mutation for a round trip.
98
+ await input.client.mutation(recordUse, { apiKey }, { skipQueue: true });
99
+ await writeState(input.configDir, { ...state, [id]: now }).catch(() => {});
100
+ return true;
101
+ } catch {
102
+ return false;
103
+ }
104
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dench.com/cli",
3
- "version": "3.11.50-staging.923",
3
+ "version": "3.11.52-staging.960",
4
4
  "description": "Dench agent workspace CLI.",
5
5
  "type": "module",
6
6
  "bin": {