web: approval cards for chat permission requests (C10)

Completes the item the pipeline could not land: the C10 diff its own
implementer and probe assertions already verified is committed as-is.
The resource block is a padding-free six-line clamp inside a padded frame
(the app's maxLines = 6 + ellipsis) and the primary control is filled.
This commit is contained in:
2026-10-19 11:08:53 +01:00
parent 27a1605127
commit 5eb13968e3
9 changed files with 1148 additions and 15 deletions
+156
View File
@@ -12,6 +12,21 @@ import {
COMPOSER_SEND,
COMPOSER_STOP,
ChatTurn,
APPROVAL_ANSWERED,
APPROVAL_HEADLINE_RESOLVED,
APPROVAL_PENDING,
APPROVAL_RESOLVED,
APPROVAL_STATUS_ANSWERED,
APPROVAL_STATUS_PENDING,
APPROVAL_STATUS_RESOLVED,
DECISION_ALWAYS,
DECISION_LABEL_ALWAYS,
DECISION_LABEL_ONCE,
DECISION_LABEL_REJECT,
DECISION_LABEL_RESOLVED,
DECISION_ONCE,
DECISION_REJECT,
MAX_APPROVAL_RESOURCE_LINES,
MAX_ATTACHMENT_BYTES,
MAX_RENDERED_DIFF_LINES,
TOOL_DETAIL_MAX_LENGTH,
@@ -21,6 +36,10 @@ import {
TURN_PHASE_WAITING,
TURN_THINKING_WINDOW_MILLIS,
applyChatEvent,
applyChatPermission,
approvalCard,
approvalResources,
approvalResourceText,
attachmentDisplayName,
attachmentError,
attachmentFiles,
@@ -41,6 +60,7 @@ import {
composerCanSend,
composerSendEnabled,
composerSubmit,
decisionLabel,
deltaPartId,
directoryLabel,
filterSessions,
@@ -53,6 +73,7 @@ import {
newChatThread,
parseChatInput,
partRendering,
permissionRiskText,
prettyToolInput,
queuedAfterFrame,
reasoningLabel,
@@ -1168,3 +1189,138 @@ test("the running indicator's elapsed and per-tool timing use the thread's own s
assert.deepEqual(runningToolStamps({ t1: 42 }, thread.messages, 9999), { t1: 42 });
assert.deepEqual(runningToolStamps({ t1: 42 }, [], 9999), {});
});
// ---- Permission approvals (C10) -------------------------------------------
/** A `chat_permission` frame (internal/protocol/chat.go ChatPermissionFrame). */
const permissionFrame = (extra = {}) => ({
session_id: "s1",
request_id: "req-1",
action: "bash",
...extra,
});
test("approvalCard names the action while pending, a blank action falling back to tool call", () => {
const card = approvalCard(permissionFrame(), "");
assert.equal(card.state, APPROVAL_PENDING);
assert.equal(card.actionable, true);
assert.equal(card.showRisk, true);
assert.equal(card.headline, "Approve bash");
assert.equal(card.statusLine, APPROVAL_STATUS_PENDING);
assert.equal(approvalCard(permissionFrame({ action: " " }), "").headline, "Approve tool call");
assert.equal(approvalCard(permissionFrame({ resources: ["r1", "r2"] }), "").resourceText, "r1\nr2");
// No request to show.
assert.equal(approvalCard(null, ""), null);
assert.equal(approvalCard({ request_id: " " }, ""), null);
});
test("an answered card reads its decision label and waits for the agent", () => {
assert.equal(approvalCard(permissionFrame(), DECISION_ONCE).headline, DECISION_LABEL_ONCE);
assert.equal(approvalCard(permissionFrame(), DECISION_ALWAYS).headline, DECISION_LABEL_ALWAYS);
const rejected = approvalCard(permissionFrame(), DECISION_REJECT);
assert.equal(rejected.headline, DECISION_LABEL_REJECT);
assert.equal(rejected.statusLine, APPROVAL_STATUS_ANSWERED);
assert.equal(rejected.actionable, false);
// Risk box: the app shows it while pending and after a deny, not for an accept.
assert.equal(rejected.showRisk, true);
assert.equal(approvalCard(permissionFrame(), DECISION_ONCE).showRisk, false);
// An unknown decision still settles to the blank-safe label.
assert.equal(approvalCard(permissionFrame(), "whatever").headline, DECISION_LABEL_RESOLVED);
});
test("a server-resolved card uses its decision label and note, else Already resolved", () => {
const resolvedWith = approvalCard(
permissionFrame({ resolved: true, decision: DECISION_ALWAYS, message: "Another client allowed this." }),
DECISION_ONCE,
);
assert.equal(resolvedWith.state, APPROVAL_RESOLVED);
assert.equal(resolvedWith.headline, DECISION_LABEL_ALWAYS);
assert.equal(resolvedWith.statusLine, "Another client allowed this.");
assert.equal(resolvedWith.actionable, false);
assert.equal(resolvedWith.showRisk, false);
// A non-blank decision alone counts as resolved; a blank one reads Already resolved.
const bare = approvalCard(permissionFrame({ decision: DECISION_REJECT }), "");
assert.equal(bare.state, APPROVAL_RESOLVED);
assert.equal(bare.headline, DECISION_LABEL_REJECT);
assert.equal(bare.statusLine, APPROVAL_STATUS_RESOLVED);
const noDecision = approvalCard(permissionFrame({ resolved: true }), DECISION_ONCE);
assert.equal(noDecision.headline, APPROVAL_HEADLINE_RESOLVED);
assert.equal(noDecision.statusLine, APPROVAL_STATUS_RESOLVED);
// The server resolution outranks the local answer.
assert.equal(noDecision.decision, "");
});
test("decisionLabel is blank-safe", () => {
assert.equal(decisionLabel(DECISION_ONCE), DECISION_LABEL_ONCE);
assert.equal(decisionLabel(DECISION_ALWAYS), DECISION_LABEL_ALWAYS);
assert.equal(decisionLabel(DECISION_REJECT), DECISION_LABEL_REJECT);
assert.equal(decisionLabel(""), DECISION_LABEL_RESOLVED);
assert.equal(decisionLabel(undefined), DECISION_LABEL_RESOLVED);
});
test("permissionRiskText prefers the server message, else names the action, else a tool call", () => {
assert.equal(
permissionRiskText(permissionFrame({ message: "Runs a shell as root." })),
"Runs a shell as root.",
);
assert.equal(
permissionRiskText(permissionFrame()),
"The agent is asking to run bash; review the request before allowing.",
);
assert.equal(
permissionRiskText(permissionFrame({ action: " " })),
"The agent is asking to run a tool call; review the request before allowing.",
);
});
test("approvalResources drops blanks but keeps every resource entry", () => {
const shown = approvalResources({ resources: ["r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", " "] });
assert.deepEqual(shown, ["r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8"]);
assert.deepEqual(approvalResources({}), []);
});
test("approvalResourceText joins the resources and caps the visible lines at six", () => {
// A blank resource is no line; an embedded newline is its own line.
assert.equal(approvalResourceText({ resources: ["a", " ", "b\nc"] }), "a\nb\nc");
// Eight resources cap to six lines, the sixth carrying the ellipsis so the
// elided remainder is not silent (Android's maxLines = 6, Ellipsis).
assert.equal(
approvalResourceText({ resources: ["r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8"] }),
"r1\nr2\nr3\nr4\nr5\nr6\u2026",
);
// A resource carrying embedded newlines counts toward the cap too: six lines
// from one entry plus one more entry still yields six lines.
const capped = approvalResourceText({ resources: ["a\nb\nc\nd\ne\nf", "g"] });
assert.equal(capped.split("\n").length, MAX_APPROVAL_RESOURCE_LINES);
assert.equal(capped.split("\n")[MAX_APPROVAL_RESOURCE_LINES - 1], "f\u2026");
assert.equal(approvalResourceText({}), "");
});
test("applyChatPermission keeps the answer for the same request and clears it for a new one", () => {
let thread = newChatThread("s1");
thread = applyChatPermission(thread, permissionFrame());
assert.equal(thread.permission.request_id, "req-1");
assert.equal(thread.permissionAnswer, "");
thread = { ...thread, permissionAnswer: DECISION_ONCE };
// The same request re-pushed keeps the operator's local answer.
thread = applyChatPermission(thread, permissionFrame());
assert.equal(thread.permissionAnswer, DECISION_ONCE);
// A resolved re-push of the same request applies the outcome (which wins) and
// still keeps the stored answer.
thread = applyChatPermission(
thread,
permissionFrame({ resolved: true, decision: DECISION_ALWAYS, message: "done elsewhere" }),
);
assert.equal(thread.permissionAnswer, DECISION_ONCE);
const card = approvalCard(thread.permission, thread.permissionAnswer);
assert.equal(card.state, APPROVAL_RESOLVED);
assert.equal(card.headline, DECISION_LABEL_ALWAYS);
assert.equal(card.statusLine, "done elsewhere");
// A new request replaces the settled card and clears the answer.
thread = applyChatPermission(thread, permissionFrame({ request_id: "req-2" }));
assert.equal(thread.permission.request_id, "req-2");
assert.equal(thread.permissionAnswer, "");
});