feat: agents ask the user for a secret through a private card (#15907)

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
Julius MarmingeandClaude Opus 5.5 authored and GitHub committed 2026-10-05 16:14:12 -07:00
1 parent 25d5c7cacb
commit 9f61ba6741
49 files changed
+2250 -27

No files matched your search

@@ -0,0 +1,75 @@
import { SecretRequestError, ThreadId, TurnItemId } from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";
import {
secretRequestAnswerInput,
secretRequestDisplay,
secretRequestFailureMessage,
type SecretRequestItem,
} from "./secretRequest.ts";
const item = {
id: TurnItemId.make("turn-item:secret-request:1"),
threadId: ThreadId.make("thread-1"),
};
describe("secretRequestDisplay", () => {
it("shows the form only while pending and maps every answer to its outcome copy", () => {
const display = (secretStatus: SecretRequestItem["secretStatus"]) =>
secretRequestDisplay({ secretStatus }, "local");
expect(display("pending")).toEqual({ kind: "pending" });
expect(display("saved")).toEqual({
kind: "answered",
outcome: "saved",
label: "Saved securely and kept private",
});
expect(display("declined")).toEqual({
kind: "answered",
outcome: "declined",
label: "Declined",
});
expect(display("cancelled")).toEqual({
kind: "answered",
outcome: "ended",
label: "Request ended",
});
});
it("never offers the form for a request inherited from another thread", () => {
expect(secretRequestDisplay({ secretStatus: "pending" }, "inherited")).toEqual({
kind: "pending-elsewhere",
label: "Waiting for an answer in the original thread",
});
expect(secretRequestDisplay({ secretStatus: "saved" }, "inherited")).toMatchObject({
outcome: "saved",
});
});
});
describe("secretRequestAnswerInput", () => {
it("refuses a blank save and trims the value the server will store", () => {
expect(secretRequestAnswerInput(item, { type: "save", secret: " " })).toBeNull();
expect(secretRequestAnswerInput(item, { type: "save", secret: " whsec_1 " })).toEqual({
threadId: item.threadId,
turnItemId: item.id,
answer: { type: "save", secret: "whsec_1" },
});
expect(secretRequestAnswerInput(item, { type: "decline" })).toEqual({
threadId: item.threadId,
turnItemId: item.id,
answer: { type: "decline" },
});
});
});
describe("secretRequestFailureMessage", () => {
it("passes through server errors but hides anything that could echo the payload", () => {
expect(
secretRequestFailureMessage(new SecretRequestError({ reason: "already_answered" })),
).toBe("This secret request was already answered.");
expect(secretRequestFailureMessage(new Error('Expected string, got "whsec_1"'))).toBe(
"Could not answer the request. Try again.",
);
expect(secretRequestFailureMessage(undefined)).toBe("Could not answer the request. Try again.");
});
});
@@ -0,0 +1,101 @@
import type { OrchestrationV2TurnItem, SecretRequestAnswerInput } from "@t3tools/contracts";
export type SecretRequestItem = Extract<
OrchestrationV2TurnItem,
{ readonly type: "secret_request" }
>;
/** Shown under the field: the one promise the card makes about the value. */
export const SECRET_REQUEST_PRIVACY_NOTE = "Stored securely, never shown to the agent";
export const SECRET_REQUEST_DEFAULT_PLACEHOLDER = "Paste the secret";
/** What a secret request card shows: the form while pending, otherwise a one-line outcome. */
export type SecretRequestDisplay =
| { readonly kind: "pending" }
| { readonly kind: "pending-elsewhere"; readonly label: string }
| {
readonly kind: "answered";
readonly outcome: "saved" | "declined" | "ended";
readonly label: string;
};
const SAVED_DISPLAY: SecretRequestDisplay = {
kind: "answered",
outcome: "saved",
label: "Saved securely and kept private",
};
const DECLINED_DISPLAY: SecretRequestDisplay = {
kind: "answered",
outcome: "declined",
label: "Declined",
};
const ENDED_DISPLAY: SecretRequestDisplay = {
kind: "answered",
outcome: "ended",
label: "Request ended",
};
const PENDING_DISPLAY: SecretRequestDisplay = { kind: "pending" };
const PENDING_ELSEWHERE_DISPLAY: SecretRequestDisplay = {
kind: "pending-elsewhere",
label: "Waiting for an answer in the original thread",
};
/**
* `visibility` is the projected row's: a request inherited from another
* thread (a fork) can only be answered where it was asked.
*/
export function secretRequestDisplay(
item: Pick<SecretRequestItem, "secretStatus">,
visibility: "local" | "inherited" | "synthetic",
): SecretRequestDisplay {
switch (item.secretStatus) {
case "pending":
return visibility === "local" ? PENDING_DISPLAY : PENDING_ELSEWHERE_DISPLAY;
case "saved":
return SAVED_DISPLAY;
case "declined":
return DECLINED_DISPLAY;
case "cancelled":
return ENDED_DISPLAY;
}
}
/**
* Builds the RPC payload for an answer. A save with a blank value returns null,
* since the server rejects it; callers keep Save disabled instead.
*/
export function secretRequestAnswerInput(
item: Pick<SecretRequestItem, "id" | "threadId">,
answer: { readonly type: "save"; readonly secret: string } | { readonly type: "decline" },
): SecretRequestAnswerInput | null {
if (answer.type === "decline") {
return { threadId: item.threadId, turnItemId: item.id, answer: { type: "decline" } };
}
const secret = answer.secret.trim();
if (secret.length === 0) return null;
return { threadId: item.threadId, turnItemId: item.id, answer: { type: "save", secret } };
}
/** Failures whose message is written for the user and never echoes the request payload. */
const USER_FACING_FAILURE_TAGS = new Set(["SecretRequestError", "EnvironmentAuthorizationError"]);
/**
* Inline error copy for a failed answer. Only known server errors pass their
* message through: anything else (transport or encoding failures) gets the
* generic copy, so the typed value can never surface in the UI.
*/
export function secretRequestFailureMessage(failure: unknown): string {
if (
typeof failure === "object" &&
failure !== null &&
"_tag" in failure &&
typeof failure._tag === "string" &&
USER_FACING_FAILURE_TAGS.has(failure._tag) &&
"message" in failure &&
typeof failure.message === "string" &&
failure.message.trim().length > 0
) {
return failure.message;
}
return "Could not answer the request. Try again.";
}
@@ -1299,6 +1299,17 @@ export function createServerEnvironmentAtoms<R, E>(
scheduler: configScheduler,
concurrency: configConcurrency,
}),
// Off the config lane: answering a card must not queue behind settings
// edits. One answer per card at a time.
answerSecretRequest: createEnvironmentRpcCommand(runtime, {
label: "environment-data:server:secrets:answer-request",
tag: WS_METHODS.secretsAnswerRequest,
concurrency: {
mode: "singleFlight",
key: ({ environmentId, input }) =>
JSON.stringify([environmentId, input.threadId, input.turnItemId]),
},
}),
refreshUsageRates: createEnvironmentRpcCommand(runtime, {
label: "environment-data:server:refresh-usage-rates",
tag: WS_METHODS.serverRefreshUsageRates,
@@ -285,6 +285,9 @@ export function summarizeT3ToolCalls(
quantity(countEntities(entityIds("requestId")), "pending question request"),
);
break;
case "secret-request":
label = phrase("Asked for", "ask for", quantity(selected.length, "secret"));
break;
case "worktree-handoff":
label = phrase(
"Handed off to",