* fix(claude): state why a permission prompt appeared ### Linked issue Closes # No open issue tracks this. The behavior appeared in a live session on a remote host. ### Type of change - [x] Bug fix - [ ] New feature - [ ] Enhancement - [ ] Refactor - [ ] Docs ### Reasoning A user selects Bypass and expects no prompts. A `permissions.ask` rule or a `PreToolUse` hook outranks the mode, so the agent asks anyway, and the card gives no cause. The user reads this as a broken mode. Claude explains each prompt in `canUseTool`'s `decisionReason`. Paseo dropped it. The card now shows that sentence, so a user can find the hook that owns the rule instead of guessing. Codex already puts its approval reason in `description`. Claude now matches, so no new field and no protocol change are needed. `description` was unset on every Claude permission request except `AskUserQuestion`, which keeps its own summary because that spread still runs after this line. ### Goals - The card shows the provider's reason when the provider sends one. Acceptance: a hook-forced prompt in Bypass shows the hook's `permissionDecisionReason`. - A card without a reason looks exactly as it does today. Acceptance: no new row, no layout shift. - Claude and Codex carry an approval reason the same way. Acceptance: one field, no provider-specific rendering. ### Non-goals - Changing the CLI's precedence. A hook that asks still asks. Paseo shows the cause; it does not overrule the hook. - Auto-approving hook-forced tools in Bypass. `f998ee169` did that and was reverted the same day. A blanket allow defeats a real safety hook. ### QA Real Claude, web (browser), one live session in `bypassPermissions`, driven through the app by Playwright. ``` npx playwright test --project=real-provider e2e/browser/permission-reason-card.real.spec.ts 1 passed (52.7s) ``` The run forces two prompts in the same session and captures both. Forced by a `permissions.ask` rule, which sends no reason. This is the card before this change:  Forced by a `PreToolUse` hook that returns a reason, same session:  The spec asserts both states, so the screenshots are not the only proof: the hook's sentence is absent from the first card and visible on the second. Both cards come from one session on this branch. The pair shows the two render paths, with a reason and without. It is not an old build beside a new one. A card without a reason renders exactly as it did before this change, which is why the first shot stands in for the old card. The spec lives at `packages/app/e2e/browser/permission-reason-card.real.spec.ts`. It is not part of this commit. Unit: ``` npx vitest run packages/server/src/server/agent/providers/claude/agent.test.ts Test Files 1 passed (1) Tests 75 passed (75) ``` Platforms: the daemon fills a field the card already rendered, so every platform gets it through the existing path. Web verified above with real Claude. ### Checklist - [x] One focused change - [x] `npm run typecheck` passes - [x] `npm run lint` passes - [x] `npm run format` passes - [x] QA evidence - [x] Tests added or updated where it made sense Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix: preserve context for Claude permission reasons --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: Mohamed Boudra <boudra.moha@gmail.com>
219 lines
6.0 KiB
TypeScript
219 lines
6.0 KiB
TypeScript
const NOTIFICATION_PREVIEW_LIMIT = 220;
|
|
|
|
export type AgentAttentionReason = "finished" | "error" | "permission";
|
|
|
|
export interface AgentAttentionNotificationData {
|
|
[key: string]: unknown;
|
|
serverId: string;
|
|
workspaceId?: string;
|
|
agentId: string;
|
|
reason: AgentAttentionReason;
|
|
}
|
|
|
|
export interface AgentAttentionNotificationPayload {
|
|
title: string;
|
|
body: string;
|
|
data: AgentAttentionNotificationData;
|
|
}
|
|
|
|
interface BuildAgentAttentionNotificationPayloadInput {
|
|
reason: AgentAttentionReason;
|
|
serverId: string;
|
|
workspaceId: string;
|
|
agentId: string;
|
|
assistantMessage?: string | null;
|
|
permissionRequest?: NotificationPermissionRequest | null;
|
|
}
|
|
|
|
export interface NotificationPermissionRequest {
|
|
id: string;
|
|
provider: string;
|
|
name: string;
|
|
kind: "tool" | "plan" | "question" | "mode" | "other";
|
|
title?: string;
|
|
description?: string;
|
|
input?: Record<string, unknown>;
|
|
metadata?: Record<string, unknown>;
|
|
}
|
|
|
|
export type AssistantTimelineItem =
|
|
| { type: "assistant_message"; text: string }
|
|
| { type: string; text?: string };
|
|
|
|
const normalizeNotificationText = (text: string): string => text.replace(/\s+/g, " ").trim();
|
|
|
|
const truncateNotificationText = (text: string, limit: number): string => {
|
|
if (text.length <= limit) {
|
|
return text;
|
|
}
|
|
const trimmed = text.slice(0, Math.max(0, limit - 3)).trimEnd();
|
|
return trimmed.length > 0 ? `${trimmed}...` : text.slice(0, limit);
|
|
};
|
|
|
|
const stripMarkdownToText = (markdown: string): string => {
|
|
let text = markdown.replace(/\r\n/g, "\n");
|
|
|
|
// Strip fenced code markers but keep the code content itself.
|
|
text = text.replace(/^\s*```[^\n]*$/gm, "");
|
|
text = text.replace(/^\s*~~~[^\n]*$/gm, "");
|
|
|
|
// Markdown links/images.
|
|
text = text.replace(/!\[([^\]]*)\]\((?:[^()\\]|\\.)*\)/g, "$1");
|
|
text = text.replace(/\[([^\]]+)\]\((?:[^()\\]|\\.)*\)/g, "$1");
|
|
|
|
// Structural prefixes.
|
|
text = text.replace(/^\s{0,3}#{1,6}\s+/gm, "");
|
|
text = text.replace(/^\s{0,3}>+\s?/gm, "");
|
|
text = text.replace(/^\s{0,3}(?:[*+-]|\d+\.)\s+/gm, "");
|
|
text = text.replace(/^\s{0,3}(?:[-*_]\s*){3,}$/gm, "");
|
|
|
|
// Inline markdown markers.
|
|
text = text.replace(/`([^`]+)`/g, "$1");
|
|
text = text.replace(/\*\*([^*]+)\*\*/g, "$1");
|
|
text = text.replace(/__([^_]+)__/g, "$1");
|
|
text = text.replace(/\*([^*\n]+)\*/g, "$1");
|
|
text = text.replace(/_([^_\n]+)_/g, "$1");
|
|
text = text.replace(/~~([^~]+)~~/g, "$1");
|
|
|
|
// Angle-bracketed URL autolinks.
|
|
text = text.replace(/<([^>\n]+)>/g, "$1");
|
|
|
|
return text;
|
|
};
|
|
|
|
const buildNotificationPreview = (text: string | null | undefined): string | null => {
|
|
if (!text) {
|
|
return null;
|
|
}
|
|
|
|
const normalized = normalizeNotificationText(stripMarkdownToText(text));
|
|
if (!normalized) {
|
|
return null;
|
|
}
|
|
|
|
return truncateNotificationText(normalized, NOTIFICATION_PREVIEW_LIMIT);
|
|
};
|
|
|
|
const safeStringify = (value: unknown): string | null => {
|
|
try {
|
|
return JSON.stringify(value);
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
const buildPermissionDetails = (
|
|
request: NotificationPermissionRequest | null | undefined,
|
|
): string | null => {
|
|
if (!request) {
|
|
return null;
|
|
}
|
|
|
|
const title = request.title?.trim();
|
|
const description = request.description?.trim();
|
|
const details: string[] = [];
|
|
|
|
if (title) {
|
|
details.push(title);
|
|
} else if (description && request.input) {
|
|
const inputPreview = safeStringify(request.input);
|
|
if (inputPreview) {
|
|
details.push(inputPreview);
|
|
}
|
|
}
|
|
if (description && description !== title) {
|
|
details.push(description);
|
|
}
|
|
if (details.length > 0) {
|
|
return details.join(" - ");
|
|
}
|
|
|
|
const inputPreview = request.input ? safeStringify(request.input) : null;
|
|
if (inputPreview) {
|
|
return inputPreview;
|
|
}
|
|
|
|
const metadataPreview = request.metadata ? safeStringify(request.metadata) : null;
|
|
if (metadataPreview) {
|
|
return metadataPreview;
|
|
}
|
|
|
|
return request.name?.trim() || request.kind;
|
|
};
|
|
|
|
export function findLatestAssistantMessageFromTimeline(
|
|
timeline: readonly AssistantTimelineItem[],
|
|
): string | null {
|
|
// Providers may stream assistant content in consecutive chunks.
|
|
const chunks: string[] = [];
|
|
for (let i = timeline.length - 1; i >= 0; i -= 1) {
|
|
const item = timeline[i];
|
|
if (item.type !== "assistant_message" || typeof item.text !== "string") {
|
|
if (chunks.length > 0) {
|
|
break;
|
|
}
|
|
continue;
|
|
}
|
|
chunks.push(item.text);
|
|
}
|
|
|
|
if (chunks.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
return chunks.toReversed().join("");
|
|
}
|
|
|
|
export function findLatestPermissionRequest(
|
|
pendingPermissions: ReadonlyMap<string, NotificationPermissionRequest>,
|
|
): NotificationPermissionRequest | null {
|
|
let latest: NotificationPermissionRequest | null = null;
|
|
for (const request of pendingPermissions.values()) {
|
|
latest = request;
|
|
}
|
|
return latest;
|
|
}
|
|
|
|
function resolveAgentAttentionTitle(reason: AgentAttentionReason): string {
|
|
if (reason === "permission") return "Agent needs permission";
|
|
if (reason === "error") return "Agent needs attention";
|
|
return "Agent finished";
|
|
}
|
|
|
|
function resolveAgentAttentionPreview(
|
|
input: BuildAgentAttentionNotificationPayloadInput,
|
|
): string | null {
|
|
if (input.reason === "finished") {
|
|
return buildNotificationPreview(input.assistantMessage);
|
|
}
|
|
if (input.reason === "permission") {
|
|
return buildNotificationPreview(buildPermissionDetails(input.permissionRequest));
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function resolveAgentAttentionFallbackBody(reason: AgentAttentionReason): string {
|
|
if (reason === "permission") return "Permission requested.";
|
|
if (reason === "error") return "Encountered an error.";
|
|
return "Finished working.";
|
|
}
|
|
|
|
export function buildAgentAttentionNotificationPayload(
|
|
input: BuildAgentAttentionNotificationPayloadInput,
|
|
): AgentAttentionNotificationPayload {
|
|
const title = resolveAgentAttentionTitle(input.reason);
|
|
const preview = resolveAgentAttentionPreview(input);
|
|
const body = preview ?? resolveAgentAttentionFallbackBody(input.reason);
|
|
|
|
return {
|
|
title,
|
|
body,
|
|
data: {
|
|
serverId: input.serverId,
|
|
workspaceId: input.workspaceId,
|
|
agentId: input.agentId,
|
|
reason: input.reason,
|
|
},
|
|
};
|
|
}
|