feat(chat): per-chat permission modes for Claude, Codex and OpenCode

This commit is contained in:
2026-10-24 18:16:10 +01:00
parent ef575898f2
commit 6d430948a6
29 changed files with 1512 additions and 3 deletions
+82
View File
@@ -0,0 +1,82 @@
// Switches an open chat's permission mode in a real browser against a loopback
// fixture, never operator data.
// PROBE_WIDTH=1280 PROBE_HOST=material|cupertino node tool/chat_mode_probe.mjs <web-build> <out>
import assert from 'node:assert/strict';
import { mkdir, mkdtemp, writeFile } from 'node:fs/promises';
import { once } from 'node:events';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { startFixture } from './runs_probe_server.mjs';
import { launchChrome, waitForPort, waitForPage, connect, sleep } from './pipelines_probe_cdp_lib.mjs';
import { axNodes, enableSemantics, waitFor, capture } from './pipelines_probe_cdp_input.mjs';
const width = Number(process.env.PROBE_WIDTH ?? 1280);
const host = process.env.PROBE_HOST ?? 'material';
const output = resolve(process.argv[3] ?? 'build/chat-mode-probe');
await mkdir(output, { recursive: true });
const messages = [
{ id: 'u0', role: 'user', parts: [{ id: 'p0', type: 'text', text: 'Switch this chat to auto mode.' }] },
{ id: 'a0', role: 'assistant', parts: [{ id: 'p1', type: 'text', text: 'Recorded fixture reply.' }] },
];
const fixture = await startFixture({ root: resolve(process.argv[2]), chatMessages: messages });
const profile = await mkdtemp(join(tmpdir(), 'chat-mode-'));
const chrome = launchChrome('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', profile, 'about:blank');
const log = { width, host, checks: [], captures: [] };
let c;
const named = async (pattern) => (await axNodes(c)).filter((n) => pattern.test(n.name?.value ?? ''));
const press = async (node) => {
const { model } = await c.send('DOM.getBoxModel', { backendNodeId: node.backendDOMNodeId });
const [x1, y1, , , x3, y3] = model.content;
for (const type of ['mousePressed', 'mouseReleased']) {
await c.send('Input.dispatchMouseEvent', { type, x: (x1 + x3) / 2, y: (y1 + y3) / 2, button: 'left', clickCount: 1 });
}
};
const first = async (pattern, what) => {
let found;
assert.ok(await waitFor(async () => (found = (await named(pattern)).find((n) => n.backendDOMNodeId)), 30000), what);
return found;
};
try {
c = await connect((await waitForPage(await waitForPort(join(profile, 'DevToolsActivePort')))).webSocketDebuggerUrl);
await c.send('Page.enable');
if (host === 'cupertino') {
await c.send('Emulation.setUserAgentOverride', {
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Safari/605.1.15', platform: 'MacIntel',
});
} else {
await c.send('Emulation.setUserAgentOverride', {
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/155.0.0.0 Safari/537.36', platform: 'Win32',
});
}
await c.send('Emulation.setDeviceMetricsOverride', { width, height: 800, deviceScaleFactor: 1, mobile: false });
await c.send('Page.navigate', { url: fixture.url + '/chat/ses_probechat' });
assert.ok(await waitFor(() => c.evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))"), 30000));
await enableSemantics(c);
await first(/Change mode\b.*Ask for permission/, 'mode control shows the row mode');
await sleep(800);
await capture(c, output, log, `${host}-mode-default`);
log.checks.push('bar shows the stored/default mode before any chat_modes request');
await press(await first(/Change mode\b/, 'mode control'));
await first(/^Auto/, 'Auto option');
await sleep(600);
await capture(c, output, log, `${host}-mode-picker`);
await press(await first(/^Auto/, 'Auto option'));
await first(/Change mode\b.*Auto/, 'bar shows Auto');
await sleep(600);
await capture(c, output, log, `${host}-mode-auto`);
const state = await (await fetch(fixture.url + '/fixture/state')).json();
assert.deepEqual(state.chatModeRequests, [{ session_id: 'ses_probechat', mode: 'auto' }]);
log.checks.push('choosing Auto sent chat_mode and the bar shows Auto');
console.log(JSON.stringify(log, null, 2));
} catch (error) {
if (c) {
await capture(c, output, log, 'failure');
await writeFile(join(output, 'failure-ax.json'), JSON.stringify(await axNodes(c), null, 2));
}
throw error;
} finally {
await writeFile(join(output, `${host}-result.json`), JSON.stringify(log, null, 2));
c?.close(); const exited = once(chrome, 'exit'); chrome.kill(); await exited;
await fixture.close();
}
+29
View File
@@ -337,7 +337,18 @@ export async function startFixture({ root, runs = RUNS, runDetail, files = {}, c
kind: 'operator',
directory: '/srv/probe',
updated_at: '2026-10-21T12:00:00Z',
backend: 'claude',
mode: 'default',
mode_name: 'Ask for permission',
};
const CHAT_MODES = [
{ id: 'default', name: 'Ask for permission', description: 'Ask before edits and commands.' },
{ id: 'acceptEdits', name: 'Accept edits', description: 'Make file edits without asking; ask for other actions.' },
{ id: 'plan', name: 'Plan', description: 'Explore and plan without changing anything.' },
{ id: 'auto', name: 'Auto', description: "Act without asking, with Claude Code's safety checks." },
{ id: 'bypassPermissions', name: 'Bypass permissions', description: 'Never ask. Everything runs without checks.' },
];
const chatModeRequests = [];
const dropConnections = () => {
for (const connection of connections) connection.close();
@@ -394,6 +405,7 @@ export async function startFixture({ root, runs = RUNS, runDetail, files = {}, c
.writeHead(200, { 'content-type': 'application/json' })
.end(JSON.stringify({
subscribed,
chatModeRequests,
live,
connections: connections.size,
refuse,
@@ -550,6 +562,23 @@ export async function startFixture({ root, runs = RUNS, runDetail, files = {}, c
setTimeout(() => client.send({ id: request.id, type: 'chat_prompt', chat_queued: chatRunning }), 300);
return;
}
if (request.type === 'chat_modes') {
client.send({ id: request.id, type: 'chat_modes', chat_modes: CHAT_MODES,
chat_mode: { session_id: CHAT_SESSION.id, mode: CHAT_SESSION.mode } });
return;
}
if (request.type === 'chat_mode') {
const mode = CHAT_MODES.find((entry) => entry.id === request.chat_mode?.mode);
chatModeRequests.push(request.chat_mode);
if (!mode) {
client.send({ id: request.id, type: 'error', error: 'unknown Claude permission mode' });
return;
}
CHAT_SESSION.mode = mode.id;
CHAT_SESSION.mode_name = mode.name;
client.send({ id: request.id, type: 'chat_mode', chat_mode: { session_id: CHAT_SESSION.id, mode: mode.id } });
return;
}
if (request.type === 'chat_shells') {
client.send({ id: request.id, type: 'chat_shells', chat_shells: [] });
return;