feat(cli): feedback says whether a person or an agent wrote it, with the email an app attaches (#4984)

* feat(cli): feedback says whether a person or an agent wrote it, with the email an app attaches

* refactor(cli): split the .env loader so each part stays simple

* fix(cli): a broken .env loader fails loudly; only a missing .env is ignored
This commit is contained in:
Miguel Ángel
2026-10-04 05:07:49 +00:00
committed by GitHub
parent de9bf487d4
commit 0ca69472d9
11 changed files with 171 additions and 35 deletions
+16
View File
@@ -0,0 +1,16 @@
import { describe, expect, it } from "vitest";
import { applyDotEnv } from "./dotEnv.js";
describe("applyDotEnv", () => {
it("copies a project's keys without overwriting what is already set", () => {
const env: NodeJS.ProcessEnv = { KEPT: "outer" };
applyDotEnv('export GEMINI_KEY="abc" # note\nKEPT=inner\n# comment\nPLAIN=x #c', env);
expect(env).toEqual({ KEPT: "outer", GEMINI_KEY: "abc", PLAIN: "x" });
});
it("never takes the feedback email from a project file", () => {
const env: NodeJS.ProcessEnv = {};
applyDotEnv("HYPERFRAMES_FEEDBACK_EMAIL=someone@example.com\nOTHER=1", env);
expect(env).toEqual({ OTHER: "1" });
});
});
+31
View File
@@ -0,0 +1,31 @@
import { FEEDBACK_EMAIL_ENV } from "./feedbackSource.js";
// A quoted value ends at its closing quote; unquoted, `#` starts a comment only after whitespace, so `pass#word` stays.
function valueOf(raw: string): string {
const quote = raw.charAt(0);
if (quote === '"' || quote === "'") {
const end = raw.indexOf(quote, 1);
return end > 0 ? raw.slice(1, end) : raw.slice(1);
}
const comment = raw.match(/\s+#/);
return comment?.index === undefined ? raw : raw.slice(0, comment.index).trim();
}
function entryOf(rawLine: string): [string, string] | null {
let line = rawLine.trim();
if (!line || line.startsWith("#")) return null;
if (line.startsWith("export ")) line = line.slice(7).trim();
const eq = line.indexOf("=");
if (eq < 1) return null;
return [line.slice(0, eq).trim(), valueOf(line.slice(eq + 1).trim())];
}
// Never over a key already set, and never the feedback email: only the launching app may attach it, and an agent can
// write a project `.env`.
export function applyDotEnv(content: string, env: NodeJS.ProcessEnv): void {
for (const entry of content.split("\n").map(entryOf)) {
if (!entry) continue;
const [key, value] = entry;
if (key && key !== FEEDBACK_EMAIL_ENV && !(key in env)) env[key] = value;
}
}
@@ -0,0 +1,22 @@
import { describe, expect, it } from "vitest";
import { feedbackEmail, parseFeedbackSource } from "./feedbackSource.js";
describe("parseFeedbackSource", () => {
it.each([
[undefined, undefined],
["person", "person"],
[" agent ", "agent"],
["Person", null],
["", null],
])("%j reads as %j", (raw, expected) => {
expect(parseFeedbackSource(raw)).toBe(expected);
});
});
describe("feedbackEmail", () => {
it("reads only HYPERFRAMES_FEEDBACK_EMAIL, trimmed, and nothing when blank", () => {
expect(feedbackEmail({ HYPERFRAMES_FEEDBACK_EMAIL: " a@b.co " })).toBe("a@b.co");
expect(feedbackEmail({ HYPERFRAMES_FEEDBACK_EMAIL: " " })).toBeUndefined();
expect(feedbackEmail({ EMAIL: "a@b.co" })).toBeUndefined();
});
});
+17
View File
@@ -0,0 +1,17 @@
export type FeedbackSource = "person" | "agent";
const SOURCES: readonly string[] = ["person", "agent"];
/** The `--source` value, undefined when absent, null when it is neither. */
export function parseFeedbackSource(raw: string | undefined): FeedbackSource | null | undefined {
if (raw === undefined) return undefined;
const source = raw.trim();
return SOURCES.includes(source) ? (source as FeedbackSource) : null;
}
export const FEEDBACK_EMAIL_ENV = "HYPERFRAMES_FEEDBACK_EMAIL";
/** Set only by the launching app (never a flag or a project `.env`); unverified, so never proof of identity. */
export function feedbackEmail(env: NodeJS.ProcessEnv = process.env): string | undefined {
return env[FEEDBACK_EMAIL_ENV]?.trim() || undefined;
}
@@ -48,6 +48,17 @@ describe("submitFeedback", () => {
);
});
it("sends who wrote it and the attached email, the email capped like the backend's", async () => {
const fetchMock = vi.fn<typeof fetch>(async () => new Response(null, { status: 202 }));
vi.stubGlobal("fetch", fetchMock);
await submitFeedback({ rating: 5, cliVersion: "1", source: "person", email: "e".repeat(300) });
const body = JSON.parse(fetchMock.mock.calls[0]?.[1]?.body as string);
expect(body.source).toBe("person");
expect(body.email).toHaveLength(254);
});
it.each([0, 10])("serializes the NPS boundary %i without changing it", async (rating) => {
const fetchMock = vi.fn<typeof fetch>(async () => new Response(null, { status: 202 }));
vi.stubGlobal("fetch", fetchMock);
+5
View File
@@ -7,6 +7,7 @@ import { FEEDBACK_RATING_SCALE } from "./feedbackRating.js";
const MAX_COMMENT = 2000;
const MAX_CLI_VERSION = 100;
const MAX_ENV = 500;
const MAX_EMAIL = 254;
function cap(value: string | undefined, max: number): string | undefined {
if (value === undefined) return undefined;
@@ -18,6 +19,8 @@ export async function submitFeedback(input: {
comment?: string;
cliVersion: string;
env?: string;
source?: "person" | "agent";
email?: string;
}): Promise<void> {
try {
const apiBaseUrl = getPublishApiBaseUrl();
@@ -29,6 +32,8 @@ export async function submitFeedback(input: {
comment: cap(input.comment, MAX_COMMENT),
cli_version: cap(input.cliVersion, MAX_CLI_VERSION),
env: cap(input.env, MAX_ENV),
source: input.source,
email: cap(input.email, MAX_EMAIL),
}),
headers: {
"content-type": "application/json",