@1agh/maude 2.2.1 → 2.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/apps/studio/acp/agents/auth.ts +241 -0
- package/apps/studio/acp/agents/catalog.ts +308 -0
- package/apps/studio/acp/agents/chat-key.ts +64 -0
- package/apps/studio/acp/agents/conformance.ts +1853 -0
- package/apps/studio/acp/agents/consent.ts +92 -0
- package/apps/studio/acp/agents/descriptor.ts +188 -0
- package/apps/studio/acp/agents/discover.ts +491 -0
- package/apps/studio/acp/agents/e2e-model.ts +58 -0
- package/apps/studio/acp/agents/health.ts +46 -0
- package/apps/studio/acp/agents/link-scan.ts +230 -0
- package/apps/studio/acp/agents/spawn.ts +164 -0
- package/apps/studio/acp/agents/state.ts +132 -0
- package/apps/studio/acp/ask-queue.ts +13 -4
- package/apps/studio/acp/bootstrap-brief.ts +16 -3
- package/apps/studio/acp/bridge.ts +709 -99
- package/apps/studio/acp/env.ts +43 -1
- package/apps/studio/acp/index.ts +428 -43
- package/apps/studio/acp/may-scope.ts +244 -21
- package/apps/studio/acp/policy/agent-home.ts +202 -0
- package/apps/studio/acp/policy/bridge-gate.ts +68 -0
- package/apps/studio/acp/policy/codex-home.ts +228 -0
- package/apps/studio/acp/policy/enforce.ts +620 -0
- package/apps/studio/acp/policy/gate-types.ts +41 -0
- package/apps/studio/acp/policy/launch-codex.ts +351 -0
- package/apps/studio/acp/policy/launch-copilot.ts +119 -0
- package/apps/studio/acp/policy/launch-gemini.ts +120 -0
- package/apps/studio/acp/policy/launch-opencode.ts +131 -0
- package/apps/studio/acp/policy/opencode-user-config.ts +240 -0
- package/apps/studio/acp/probe.ts +78 -14
- package/apps/studio/acp/transcript.ts +14 -0
- package/apps/studio/acp/write-scope.ts +62 -2
- package/apps/studio/client/panels/ChatPanel.jsx +19 -4
- package/apps/studio/client/panels/PermissionPrompt.jsx +11 -6
- package/apps/studio/client/panels/acp-runtime.js +95 -7
- package/apps/studio/client/v2/ai/Advanced.jsx +4 -3
- package/apps/studio/client/v2/ai/AgentNotice.jsx +138 -0
- package/apps/studio/client/v2/ai/AgentOffer.jsx +110 -0
- package/apps/studio/client/v2/ai/AgentPicker.jsx +121 -0
- package/apps/studio/client/v2/ai/Composer.jsx +4 -0
- package/apps/studio/client/v2/ai/ConnectSheet.jsx +13 -0
- package/apps/studio/client/v2/ai/Feed.jsx +8 -0
- package/apps/studio/client/v2/ai/NeedsYouSlot.jsx +24 -1
- package/apps/studio/client/v2/ai/Suggest.jsx +5 -1
- package/apps/studio/client/v2/ai/advanced-model.js +17 -6
- package/apps/studio/client/v2/ai/agents-model.js +333 -0
- package/apps/studio/client/v2/ai/agents-store.js +223 -0
- package/apps/studio/client/v2/ai/ai.css +63 -0
- package/apps/studio/client/v2/ai/needs-you.js +1 -1
- package/apps/studio/client/v2/fold/fold-store.js +1 -0
- package/apps/studio/client/v2/settings/agent-keys.js +88 -0
- package/apps/studio/client/v2/settings/agents-card.jsx +408 -0
- package/apps/studio/client/v2/settings/connections-model.js +1 -1
- package/apps/studio/client/v2/settings/connections-section.jsx +4 -0
- package/apps/studio/client/v2/settings/settings-cards.js +2 -1
- package/apps/studio/client/v2/settings/settings-model.js +1 -0
- package/apps/studio/client/v2/settings/settings.css +7 -0
- package/apps/studio/client/v2/shell/served-routes.js +1 -0
- package/apps/studio/cloud-build.ts +5 -0
- package/apps/studio/dist/client.bundle.js +604 -604
- package/apps/studio/dist/styles.css +1 -1
- package/apps/studio/generation/keys.ts +197 -11
- package/apps/studio/outbox/policy.ts +8 -2
- package/apps/studio/outbox/types.ts +5 -0
- package/apps/studio/routes/agents.ts +479 -0
- package/apps/studio/routes/index.ts +3 -0
- package/apps/studio/scripts/acp-conformance-local.ts +95 -0
- package/apps/studio/scripts/acp-e2e-local.ts +1026 -0
- package/apps/studio/server.ts +9 -0
- package/apps/studio/sync/writer-registry.ts +12 -0
- package/apps/studio/whats-new.json +10 -0
- package/apps/studio/ws.ts +3 -0
- package/cli/commands/codex.mjs +36 -2
- package/cli/lib/harness/codex-runtime.mjs +98 -11
- package/package.json +9 -8
|
@@ -0,0 +1,620 @@
|
|
|
1
|
+
// acp/policy/enforce.ts — the bridge's permission gate for a bridge-enforced (non-Claude) ACP agent.
|
|
2
|
+
// Contract: `.ai/plans/notes/universal-acp-contract.md` § "Bridge-enforced rule set" (rules 1–13), DDR-252.
|
|
3
|
+
//
|
|
4
|
+
// The boundary itself is the agent's own launch policy (Codex's Seatbelt profile, opencode's permission
|
|
5
|
+
// engine — acp/policy/launch-*.ts). This gate is the SECOND gate: it answers every
|
|
6
|
+
// `session/request_permission` from ACP `kind` + `locations` + `rawInput`, never a tool name (only Claude
|
|
7
|
+
// has `_meta.claudeCode.toolName`). It is pure: no I/O beyond realpath, no bridge state but what the
|
|
8
|
+
// caller passes in `GateState`, so the replay tests can drive it with recorded frames.
|
|
9
|
+
//
|
|
10
|
+
// Rule map (contract numbering):
|
|
11
|
+
// 1 stopped session → cancelled `gatePermission`
|
|
12
|
+
// 2 normalise kind / targets / command `normaliseRequest`
|
|
13
|
+
// 3 credential refusal, no card may-scope.ts `credentialRefusalByKind`
|
|
14
|
+
// 4 "AI may" refusal, no card may-scope.ts `mayRefusalByKind`
|
|
15
|
+
// 5 writes: inside → once, outside → card, none → refuse write-scope.ts `writeVerdictForPaths`
|
|
16
|
+
// 6 reads: inside → once, outside → card, none → refuse
|
|
17
|
+
// 7 shell: os-sandbox → always card; command-text → auto verb / plain inside read → once, else card
|
|
18
|
+
// 8 anything else → refuse
|
|
19
|
+
// 9 never pick an "always" option; strip it from the card `cardOptions`
|
|
20
|
+
// 10 refused modes `isRefusedMode` (the pin itself is the bridge's)
|
|
21
|
+
// 11 fs/* and terminal/* → JSON-RPC error `isForbiddenClientMethod`
|
|
22
|
+
// 12 tripwire on completed tool updates `tripwire`
|
|
23
|
+
// 13 a refusal that ends the turn says why `refusalNotice`
|
|
24
|
+
//
|
|
25
|
+
// Every refusal picks the request's `reject_once` option, or `cancelled` when there is none. An allow
|
|
26
|
+
// is `allowOnce` + `audienceHold: true`: the bridge still runs its audience check (H1) before granting.
|
|
27
|
+
|
|
28
|
+
import { homedir } from 'node:os';
|
|
29
|
+
import { isAbsolute, resolve } from 'node:path';
|
|
30
|
+
|
|
31
|
+
import type {
|
|
32
|
+
PermissionOption,
|
|
33
|
+
RequestPermissionRequest,
|
|
34
|
+
RequestPermissionResponse,
|
|
35
|
+
} from '@agentclientprotocol/sdk';
|
|
36
|
+
|
|
37
|
+
import { MAUDE_VERBS, type VerbDef, verbTier } from '../../actions/verbs.ts';
|
|
38
|
+
import { agentPath } from '../agent-path.ts';
|
|
39
|
+
import type { AgentDescriptor, Enforcement, PolicyCtx } from '../agents/descriptor.ts';
|
|
40
|
+
import {
|
|
41
|
+
assetsRootsFor,
|
|
42
|
+
bashReadOnlyInside,
|
|
43
|
+
credentialRefusalByKind,
|
|
44
|
+
type KindRequest,
|
|
45
|
+
mayRefusalByKind,
|
|
46
|
+
plainWords,
|
|
47
|
+
} from '../may-scope.ts';
|
|
48
|
+
import { isInsideRoot, resolveRealPath, writeVerdictForPaths } from '../write-scope.ts';
|
|
49
|
+
|
|
50
|
+
// ---------------------------------------------------------------------------------------------------
|
|
51
|
+
// Types (the call-site interface; the lead reconciles with acp/policy/gate-types.ts at merge)
|
|
52
|
+
|
|
53
|
+
export type GateDecision = 'allow_once' | 'reject' | 'ask';
|
|
54
|
+
|
|
55
|
+
export type GateReason =
|
|
56
|
+
| 'stopped'
|
|
57
|
+
| 'not-enforced'
|
|
58
|
+
| 'no-tool-call'
|
|
59
|
+
| 'credentials'
|
|
60
|
+
| 'web'
|
|
61
|
+
| 'assets'
|
|
62
|
+
| 'write-inside'
|
|
63
|
+
| 'write-outside'
|
|
64
|
+
| 'write-no-target'
|
|
65
|
+
| 'read-inside'
|
|
66
|
+
| 'read-outside'
|
|
67
|
+
| 'read-no-target'
|
|
68
|
+
| 'shell-escalation'
|
|
69
|
+
| 'shell-auto-verb'
|
|
70
|
+
| 'shell-read-inside'
|
|
71
|
+
| 'shell-ask'
|
|
72
|
+
| 'shell-no-command'
|
|
73
|
+
| 'fetch'
|
|
74
|
+
| 'unknown-kind'
|
|
75
|
+
| 'no-allow-option';
|
|
76
|
+
|
|
77
|
+
export interface GateResult {
|
|
78
|
+
decision: GateDecision;
|
|
79
|
+
/** The option to answer with: allow → the `allow_once` option, reject → `reject_once` (absent → cancelled). */
|
|
80
|
+
optionId?: string;
|
|
81
|
+
reason: GateReason;
|
|
82
|
+
/** What the person's card may offer: only `allow_once` / `reject_once`, never a persistent option. */
|
|
83
|
+
strippedOptions: PermissionOption[];
|
|
84
|
+
/** An allow still goes through the bridge's audience check (H1) before it is granted. */
|
|
85
|
+
audienceHold: boolean;
|
|
86
|
+
/** What the card shows: the resolved paths (never the model's own spelling), the command, the cwd. */
|
|
87
|
+
shown: { paths: string[]; command?: string; cwd: string };
|
|
88
|
+
/** The request's toolCallId, when it had one. */
|
|
89
|
+
toolCallId?: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Per-request facts the bridge holds live; everything else is in `PolicyCtx` (per session). */
|
|
93
|
+
export interface GateState {
|
|
94
|
+
/** The session was stopped: every late request is `cancelled` (rule 1). */
|
|
95
|
+
stopped: boolean;
|
|
96
|
+
/** Folders the person let this chat read (ai/attach-grants.ts), read live: readable, never writable. */
|
|
97
|
+
grantRoots?: readonly string[];
|
|
98
|
+
/** A run's own write scope (S8.10 Handoff): every write target must be one of these. */
|
|
99
|
+
onlyPaths?: readonly string[] | null;
|
|
100
|
+
/** The home folder `~` expands to (default: $HOME). */
|
|
101
|
+
home?: string;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export type EnforcedDescriptor = AgentDescriptor & { enforcement: Enforcement };
|
|
105
|
+
|
|
106
|
+
/** A request reduced to what the rules judge (rule 2). */
|
|
107
|
+
export interface NormalisedRequest extends KindRequest {
|
|
108
|
+
hasToolCall: boolean;
|
|
109
|
+
toolCallId?: string;
|
|
110
|
+
/** Paths the request names WITHOUT its cwd: a cwd is a folder to resolve from, not a write target. */
|
|
111
|
+
targets: string[];
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ---------------------------------------------------------------------------------------------------
|
|
115
|
+
// Rule 2 — normalisation
|
|
116
|
+
|
|
117
|
+
const obj = (v: unknown): Record<string, unknown> | null =>
|
|
118
|
+
v && typeof v === 'object' && !Array.isArray(v) ? (v as Record<string, unknown>) : null;
|
|
119
|
+
|
|
120
|
+
function pathsOf(toolCall: Record<string, unknown>, targetKeys: readonly string[]): string[] {
|
|
121
|
+
const out: string[] = [];
|
|
122
|
+
if (Array.isArray(toolCall.locations)) {
|
|
123
|
+
for (const l of toolCall.locations) {
|
|
124
|
+
const p = obj(l)?.path;
|
|
125
|
+
if (typeof p === 'string' && p) out.push(p);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const raw = obj(toolCall.rawInput) ?? {};
|
|
129
|
+
for (const k of targetKeys) {
|
|
130
|
+
const v = raw[k];
|
|
131
|
+
if (typeof v === 'string' && v) out.push(v);
|
|
132
|
+
else if (Array.isArray(v)) for (const x of v) if (typeof x === 'string' && x) out.push(x);
|
|
133
|
+
}
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function commandOf(raw: Record<string, unknown>): string {
|
|
138
|
+
const c = raw.command;
|
|
139
|
+
if (typeof c === 'string') return c;
|
|
140
|
+
if (Array.isArray(c)) return c.filter((x) => typeof x === 'string').join(' ');
|
|
141
|
+
return '';
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* B8 finding 1 — Gemini sends an `execute` with no `rawInput`: the command is only in `title`, which
|
|
146
|
+
* gemini-cli's shell tool builds as `<command>` + ` [in <dir>]` or ` [current working directory
|
|
147
|
+
* <dir>]` + an optional ` (<description>)`. Returns the command and its directory, or null when the
|
|
148
|
+
* title is not exactly that shape (the caller then has no command: refused, fail closed).
|
|
149
|
+
*
|
|
150
|
+
* The split is the first ` [in ` / ` [current working directory ` that leaves a well-formed tail;
|
|
151
|
+
* a description that itself carries a directory bracket is refused, so a command spelled to look
|
|
152
|
+
* like a shorter one (`cat x [in y] (z …`) never parses. A bracket, paren or newline left in the
|
|
153
|
+
* command makes `plainWords` refuse it afterwards, so a mis-split can only reach the card.
|
|
154
|
+
*/
|
|
155
|
+
const TITLE_RE =
|
|
156
|
+
/^(?<command>[^\n\r]+?) \[(?:in (?<inDir>[^\]\n\r]+)|current working directory (?<cwdDir>[^\]\n\r]+))\](?: \((?<desc>[^\n\r]*)\))?$/;
|
|
157
|
+
|
|
158
|
+
export function commandFromTitle(title: unknown): { command: string; dir: string } | null {
|
|
159
|
+
if (typeof title !== 'string' || !title) return null;
|
|
160
|
+
const m = TITLE_RE.exec(title);
|
|
161
|
+
const g = m?.groups;
|
|
162
|
+
if (!g?.command) return null;
|
|
163
|
+
const dir = g.inDir ?? g.cwdDir;
|
|
164
|
+
if (!dir) return null;
|
|
165
|
+
if (g.desc !== undefined && /\[(?:in |current working directory )/.test(g.desc)) return null;
|
|
166
|
+
const command = g.command.trim();
|
|
167
|
+
return command ? { command, dir } : null;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Rule 2: `kind`; targets = `locations[].path` ∪ `rawInput[targetKeys]` (∪ `rawInput.cwd` in `paths`);
|
|
172
|
+
* command = `rawInput.command` (a string, or an argv joined with spaces); relative paths resolve from
|
|
173
|
+
* the request's own cwd (spelled through agent-path.ts), else from the session's scope root. A
|
|
174
|
+
* `command-text` agent's `execute` with no `rawInput.command` takes it from `title`
|
|
175
|
+
* (`commandFromTitle`, Gemini), with the title's directory as the cwd.
|
|
176
|
+
*/
|
|
177
|
+
export function normaliseRequest(
|
|
178
|
+
req: Pick<RequestPermissionRequest, 'toolCall'> | null | undefined,
|
|
179
|
+
enforcement: Pick<Enforcement, 'targetKeys'> & Partial<Pick<Enforcement, 'shellGate'>>,
|
|
180
|
+
ctx: Pick<PolicyCtx, 'scopeRoot'>,
|
|
181
|
+
home: string = process.env.HOME || homedir()
|
|
182
|
+
): NormalisedRequest {
|
|
183
|
+
const toolCall = obj(req?.toolCall);
|
|
184
|
+
if (!toolCall) {
|
|
185
|
+
return {
|
|
186
|
+
hasToolCall: false,
|
|
187
|
+
kind: undefined,
|
|
188
|
+
paths: [],
|
|
189
|
+
targets: [],
|
|
190
|
+
command: '',
|
|
191
|
+
cwd: ctx.scopeRoot,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
const raw = obj(toolCall.rawInput) ?? {};
|
|
195
|
+
const targets = pathsOf(toolCall, enforcement.targetKeys);
|
|
196
|
+
let command = commandOf(raw);
|
|
197
|
+
let rawCwd = typeof raw.cwd === 'string' && raw.cwd ? raw.cwd : null;
|
|
198
|
+
if (
|
|
199
|
+
!command &&
|
|
200
|
+
toolCall.kind === 'execute' &&
|
|
201
|
+
enforcement.shellGate === 'command-text' &&
|
|
202
|
+
!(typeof raw.command === 'string' || Array.isArray(raw.command))
|
|
203
|
+
) {
|
|
204
|
+
const fromTitle = commandFromTitle(toolCall.title);
|
|
205
|
+
if (fromTitle) {
|
|
206
|
+
command = fromTitle.command;
|
|
207
|
+
rawCwd = fromTitle.dir;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const spelledCwd = rawCwd ? agentPath(rawCwd, ctx.scopeRoot, home) : null;
|
|
211
|
+
const url = raw.url;
|
|
212
|
+
return {
|
|
213
|
+
hasToolCall: true,
|
|
214
|
+
toolCallId: typeof toolCall.toolCallId === 'string' ? toolCall.toolCallId : undefined,
|
|
215
|
+
kind: typeof toolCall.kind === 'string' ? toolCall.kind : undefined,
|
|
216
|
+
targets,
|
|
217
|
+
paths: rawCwd ? [...targets, rawCwd] : targets,
|
|
218
|
+
command,
|
|
219
|
+
cwd: spelledCwd?.abs ?? ctx.scopeRoot,
|
|
220
|
+
webShaped: typeof url === 'string' && /^\s*https?:/i.test(url),
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// ---------------------------------------------------------------------------------------------------
|
|
225
|
+
// Rule 9 — options
|
|
226
|
+
|
|
227
|
+
/** Option ids known to persist a rule or widen the session, whatever `kind` they claim. */
|
|
228
|
+
export const PERSISTENT_OPTION_IDS: ReadonlySet<string> = new Set([
|
|
229
|
+
'accept_execpolicy_amendment', // Codex: writes an exec-policy rule into CODEX_HOME
|
|
230
|
+
'allow_for_session', // Codex: "don't ask again for these files"
|
|
231
|
+
'always', // opencode: widens the session
|
|
232
|
+
'allow_always',
|
|
233
|
+
// S3 — codex-acp ApprovalOptionId: a permissions request's grants widen the turn or the session,
|
|
234
|
+
// a network amendment persists a rule, an MCP `allow_session` covers the session.
|
|
235
|
+
'allow_permissions_turn',
|
|
236
|
+
'allow_permissions_turn_strict_auto_review',
|
|
237
|
+
'allow_permissions_session',
|
|
238
|
+
'apply_network_policy_amendment',
|
|
239
|
+
'allow_session',
|
|
240
|
+
]);
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Rule 9 — a persistent id: one of PERSISTENT_OPTION_IDS, or any `allow_permissions_*` (Codex's
|
|
244
|
+
* permission-profile grants: a variant a newer codex-acp adds is refused until someone lists it).
|
|
245
|
+
*/
|
|
246
|
+
export function isPersistentOptionId(id: string): boolean {
|
|
247
|
+
return PERSISTENT_OPTION_IDS.has(id) || id.startsWith('allow_permissions_');
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** The options a card may show: `allow_once` and `reject_once` only, minus any persistent id. */
|
|
251
|
+
export function cardOptions(options: unknown): PermissionOption[] {
|
|
252
|
+
if (!Array.isArray(options)) return [];
|
|
253
|
+
return (options as PermissionOption[]).filter(
|
|
254
|
+
(o) =>
|
|
255
|
+
!!o &&
|
|
256
|
+
typeof o.optionId === 'string' &&
|
|
257
|
+
(o.kind === 'allow_once' || o.kind === 'reject_once') &&
|
|
258
|
+
!isPersistentOptionId(o.optionId)
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// ---------------------------------------------------------------------------------------------------
|
|
263
|
+
// Rule 7 helper — `maude design <auto-tier verb>`
|
|
264
|
+
|
|
265
|
+
const DESIGN_DEFS = new Map<string, VerbDef>(
|
|
266
|
+
MAUDE_VERBS.filter((v) => v.scope === 'design').map((v) => [v.verb, v])
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* True when `command` is exactly ONE plain `maude design <verb> …` call (no operator, redirect,
|
|
271
|
+
* expansion or glob — `plainWords`) of an auto-tier verb (actions/verbs.ts `verbTier`). A two-word
|
|
272
|
+
* entry (`ds-upgrade analyse`) is matched before its verb's own entry, as the registry says.
|
|
273
|
+
*/
|
|
274
|
+
export function isAutoDesignVerbCall(command: string): boolean {
|
|
275
|
+
const w = plainWords(command);
|
|
276
|
+
if (w?.[0] !== 'maude' || w[1] !== 'design' || !w[2]) return false;
|
|
277
|
+
const def = (w[3] && DESIGN_DEFS.get(`${w[2]} ${w[3]}`)) || DESIGN_DEFS.get(w[2]);
|
|
278
|
+
return !!def && verbTier(def) === 'auto';
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// ---------------------------------------------------------------------------------------------------
|
|
282
|
+
// Rule 6 helper — reads
|
|
283
|
+
|
|
284
|
+
const GLOB_RE = /[*?[{]/;
|
|
285
|
+
const climbs = (p: string) => p.split(/[\\/]/).includes('..');
|
|
286
|
+
|
|
287
|
+
/** Every target, resolved on disk from `cwd`, lies in `roots` (a glob by its literal prefix). */
|
|
288
|
+
function readsInside(
|
|
289
|
+
targets: readonly string[],
|
|
290
|
+
cwd: string,
|
|
291
|
+
roots: readonly string[],
|
|
292
|
+
home: string
|
|
293
|
+
): boolean {
|
|
294
|
+
return targets.every((t) => {
|
|
295
|
+
let p = t;
|
|
296
|
+
if (GLOB_RE.test(p)) {
|
|
297
|
+
if (climbs(p)) return false;
|
|
298
|
+
p = p.split(GLOB_RE)[0] || '.';
|
|
299
|
+
}
|
|
300
|
+
const a = agentPath(p, cwd, home);
|
|
301
|
+
if (!a || a.odd) return false;
|
|
302
|
+
const real = resolveRealPath(a.abs, cwd);
|
|
303
|
+
return roots.some((r) => r === real || isInsideRoot(real, r));
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const realRoots = (roots: readonly string[]) =>
|
|
308
|
+
roots.filter((r) => isAbsolute(r)).flatMap((r) => [resolve(r), resolveRealPath(r, r)]);
|
|
309
|
+
|
|
310
|
+
function resolvedForCard(paths: readonly string[], cwd: string, home: string): string[] {
|
|
311
|
+
const out = new Set<string>();
|
|
312
|
+
for (const p of paths) {
|
|
313
|
+
const a = agentPath(p, cwd, home);
|
|
314
|
+
if (a) out.add(resolveRealPath(a.abs, cwd));
|
|
315
|
+
}
|
|
316
|
+
return [...out];
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// ---------------------------------------------------------------------------------------------------
|
|
320
|
+
// The gate
|
|
321
|
+
|
|
322
|
+
const WRITE_KINDS = new Set(['edit', 'delete', 'move']);
|
|
323
|
+
const READ_KINDS = new Set(['read', 'search']);
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Rules 1–9 and 11–13 for one `session/request_permission` of a bridge-enforced agent. Pure: the
|
|
327
|
+
* bridge applies the result (answer now, or show `strippedOptions` and check the answer with
|
|
328
|
+
* `answerCard`).
|
|
329
|
+
*/
|
|
330
|
+
export function gatePermission(
|
|
331
|
+
req: Pick<RequestPermissionRequest, 'toolCall' | 'options'> | null | undefined,
|
|
332
|
+
d: AgentDescriptor,
|
|
333
|
+
ctx: PolicyCtx,
|
|
334
|
+
state: GateState
|
|
335
|
+
): GateResult {
|
|
336
|
+
const home = state.home ?? (process.env.HOME || homedir());
|
|
337
|
+
const strippedOptions = cardOptions(req?.options);
|
|
338
|
+
const rejectOpt = strippedOptions.find((o) => o.kind === 'reject_once');
|
|
339
|
+
const allowOpt = strippedOptions.find((o) => o.kind === 'allow_once');
|
|
340
|
+
const enforcement = d.policy === 'bridge-enforced' ? d.enforcement : undefined;
|
|
341
|
+
const n = normaliseRequest(req, enforcement ?? { targetKeys: [] }, ctx, home);
|
|
342
|
+
const shown = {
|
|
343
|
+
paths: resolvedForCard(n.paths, n.cwd, home),
|
|
344
|
+
command: n.command || undefined,
|
|
345
|
+
cwd: n.cwd,
|
|
346
|
+
};
|
|
347
|
+
const base = { strippedOptions, shown, toolCallId: n.toolCallId };
|
|
348
|
+
const reject = (reason: GateReason): GateResult => ({
|
|
349
|
+
...base,
|
|
350
|
+
decision: 'reject',
|
|
351
|
+
optionId: reason === 'stopped' ? undefined : rejectOpt?.optionId,
|
|
352
|
+
reason,
|
|
353
|
+
audienceHold: false,
|
|
354
|
+
});
|
|
355
|
+
const ask = (reason: GateReason): GateResult => ({
|
|
356
|
+
...base,
|
|
357
|
+
decision: 'ask',
|
|
358
|
+
reason,
|
|
359
|
+
audienceHold: false,
|
|
360
|
+
});
|
|
361
|
+
const allow = (reason: GateReason): GateResult =>
|
|
362
|
+
allowOpt
|
|
363
|
+
? { ...base, decision: 'allow_once', optionId: allowOpt.optionId, reason, audienceHold: true }
|
|
364
|
+
: ask('no-allow-option');
|
|
365
|
+
|
|
366
|
+
// 1
|
|
367
|
+
if (state.stopped) return reject('stopped');
|
|
368
|
+
if (!enforcement) return reject('not-enforced');
|
|
369
|
+
// 8 (a missing toolCall names nothing to judge)
|
|
370
|
+
if (!n.hasToolCall) return reject('no-tool-call');
|
|
371
|
+
// 3
|
|
372
|
+
if (credentialRefusalByKind(n, ctx.credentialPaths, home)) return reject('credentials');
|
|
373
|
+
// 4
|
|
374
|
+
const assetsRoots = ctx.designRoots.flatMap((dr) => assetsRootsFor(ctx.scopeRoot, dr));
|
|
375
|
+
const may = mayRefusalByKind(n, ctx.may, ctx.scopeRoot, assetsRoots, home);
|
|
376
|
+
if (may) return reject(may);
|
|
377
|
+
|
|
378
|
+
const kind = n.kind ?? '';
|
|
379
|
+
const readRoots = [...ctx.readRoots, ...realRoots(state.grantRoots ?? [])];
|
|
380
|
+
|
|
381
|
+
// 5
|
|
382
|
+
if (WRITE_KINDS.has(kind)) {
|
|
383
|
+
if (!n.targets.length) return reject('write-no-target');
|
|
384
|
+
const v = writeVerdictForPaths(
|
|
385
|
+
n.targets,
|
|
386
|
+
n.cwd,
|
|
387
|
+
ctx.writeRoots,
|
|
388
|
+
state.grantRoots ?? [],
|
|
389
|
+
state.onlyPaths ?? null,
|
|
390
|
+
ctx.designRoots
|
|
391
|
+
);
|
|
392
|
+
if (v.reason === 'no-target') return reject('write-no-target');
|
|
393
|
+
return v.inside ? allow('write-inside') : ask('write-outside');
|
|
394
|
+
}
|
|
395
|
+
// 6
|
|
396
|
+
if (READ_KINDS.has(kind)) {
|
|
397
|
+
if (!n.paths.length) return reject('read-no-target');
|
|
398
|
+
return readsInside(n.paths, n.cwd, readRoots, home)
|
|
399
|
+
? allow('read-inside')
|
|
400
|
+
: ask('read-outside');
|
|
401
|
+
}
|
|
402
|
+
// 7
|
|
403
|
+
if (kind === 'execute') {
|
|
404
|
+
if (!n.command.trim()) return reject('shell-no-command');
|
|
405
|
+
if (enforcement.shellGate === 'os-sandbox') return ask('shell-escalation');
|
|
406
|
+
if (isAutoDesignVerbCall(n.command)) return allow('shell-auto-verb');
|
|
407
|
+
if (bashReadOnlyInside(n.command, n.cwd, readRoots, home)) return allow('shell-read-inside');
|
|
408
|
+
return ask('shell-ask');
|
|
409
|
+
}
|
|
410
|
+
// web on (an off switch refused it at rule 4): the network boundary is the launch policy; the
|
|
411
|
+
// person decides each fetch, with no standing option.
|
|
412
|
+
if (kind === 'fetch') return ask('fetch');
|
|
413
|
+
// 8 — think, switch_mode, other, unknown, absent
|
|
414
|
+
return reject('unknown-kind');
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** The ACP answer for a decided (non-`ask`) result: the chosen option, or `cancelled`. */
|
|
418
|
+
export function toPermissionResponse(result: GateResult): RequestPermissionResponse {
|
|
419
|
+
if (result.decision === 'ask' || !result.optionId) return { outcome: { outcome: 'cancelled' } };
|
|
420
|
+
return { outcome: { outcome: 'selected', optionId: result.optionId } };
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* The person's answer to an `ask` card, checked against what the card offered: an option it did not
|
|
425
|
+
* show (an "always" one, a forged id) or no answer is `cancelled`.
|
|
426
|
+
*/
|
|
427
|
+
export function answerCard(
|
|
428
|
+
result: GateResult,
|
|
429
|
+
optionId: string | null | undefined
|
|
430
|
+
): RequestPermissionResponse {
|
|
431
|
+
if (result.decision !== 'ask') return toPermissionResponse(result);
|
|
432
|
+
const picked = result.strippedOptions.find((o) => o.optionId === optionId);
|
|
433
|
+
return picked
|
|
434
|
+
? { outcome: { outcome: 'selected', optionId: picked.optionId } }
|
|
435
|
+
: { outcome: { outcome: 'cancelled' } };
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// ---------------------------------------------------------------------------------------------------
|
|
439
|
+
// Rules 10, 11, 13
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Rule 10: a mode the bridge refuses to switch to (and the picker never lists). An allowlist (S1,
|
|
443
|
+
* attacker A6): for a pinned agent every mode outside `pinMode.allow` is refused, including one a
|
|
444
|
+
* newer adapter adds.
|
|
445
|
+
*/
|
|
446
|
+
export function isRefusedMode(d: AgentDescriptor, modeId: string): boolean {
|
|
447
|
+
const pin = d.enforcement?.pinMode;
|
|
448
|
+
return !!pin && !pin.allow.includes(modeId);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/** Rule 11: a client method a bridge-enforced agent must never call (never advertised). */
|
|
452
|
+
export function isForbiddenClientMethod(d: AgentDescriptor, method: string): boolean {
|
|
453
|
+
return (
|
|
454
|
+
d.policy === 'bridge-enforced' && (method.startsWith('fs/') || method.startsWith('terminal/'))
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** The JSON-RPC error rule 11 answers with. */
|
|
459
|
+
export const FORBIDDEN_METHOD_ERROR = Object.freeze({
|
|
460
|
+
code: -32601,
|
|
461
|
+
message: 'Not available: Maude does not offer file or terminal access to this agent',
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
const REFUSED_WHAT: Partial<Record<GateReason, string>> = {
|
|
465
|
+
credentials: 'a credential store',
|
|
466
|
+
web: 'web access ("Search the web" is off)',
|
|
467
|
+
assets: 'the assets folder ("Use Assets and footage" is off)',
|
|
468
|
+
'write-no-target': 'a write that names no file',
|
|
469
|
+
'read-no-target': 'a read that names no file',
|
|
470
|
+
'shell-no-command': 'a shell call with no command',
|
|
471
|
+
'no-tool-call': 'a request with no tool',
|
|
472
|
+
'unknown-kind': 'a tool it cannot judge',
|
|
473
|
+
'not-enforced': 'a tool from an agent it does not confine',
|
|
474
|
+
};
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* Rule 13: when a refusal ends the agent's turn (`rejectEndsTurn`, Codex), the line the panel shows
|
|
478
|
+
* instead of an error card. Null when the turn goes on, or the result is not a refusal Maude made.
|
|
479
|
+
*/
|
|
480
|
+
export function refusalNotice(d: AgentDescriptor, result: GateResult): string | null {
|
|
481
|
+
if (!d.enforcement?.rejectEndsTurn || result.decision !== 'reject' || result.reason === 'stopped')
|
|
482
|
+
return null;
|
|
483
|
+
return `${d.label} stopped: Maude refused ${REFUSED_WHAT[result.reason] ?? 'this tool call'}`;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/** Rule 13, the person's own "No" on a card: still not an error. */
|
|
487
|
+
export function cardRefusalNotice(d: AgentDescriptor): string | null {
|
|
488
|
+
return d.enforcement?.rejectEndsTurn ? `${d.label} stopped: you refused this step` : null;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// ---------------------------------------------------------------------------------------------------
|
|
492
|
+
// Rule 12 — tripwire
|
|
493
|
+
|
|
494
|
+
interface SeenCall {
|
|
495
|
+
kind?: string;
|
|
496
|
+
paths: string[];
|
|
497
|
+
cwd?: string;
|
|
498
|
+
command?: string;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/** Per-session memory: a `tool_call_update` usually carries only its status, the first frame the rest. */
|
|
502
|
+
export interface TripwireMemory {
|
|
503
|
+
calls: Map<string, SeenCall>;
|
|
504
|
+
/** toolCallIds the person approved on a card: their targets were the person's call. */
|
|
505
|
+
approved: Set<string>;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
export function newTripwireMemory(): TripwireMemory {
|
|
509
|
+
return { calls: new Map(), approved: new Set() };
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
export interface TripwireVerdict {
|
|
513
|
+
violated: boolean;
|
|
514
|
+
reason: string;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Rule 12: a COMPLETED tool call whose targets break the launch policy. Evidence, never a gate: a
|
|
519
|
+
* missing frame proves nothing (Codex reports none for a sandbox-denied command).
|
|
520
|
+
* • any bridge-enforced agent: a completed call that touched a credential store;
|
|
521
|
+
* • `readGate:'os-sandbox'`: a completed read/search outside the read roots, or a completed
|
|
522
|
+
* edit/delete/move outside the write roots, that the person did not approve on a card.
|
|
523
|
+
* Pass the session's `memory` so a status-only `tool_call_update` is judged with what its
|
|
524
|
+
* `tool_call` said; without it, the update alone is judged.
|
|
525
|
+
*/
|
|
526
|
+
export function tripwire(
|
|
527
|
+
update: unknown,
|
|
528
|
+
d: AgentDescriptor,
|
|
529
|
+
ctx: PolicyCtx,
|
|
530
|
+
memory?: TripwireMemory,
|
|
531
|
+
home: string = process.env.HOME || homedir()
|
|
532
|
+
): TripwireVerdict {
|
|
533
|
+
const none = { violated: false, reason: '' };
|
|
534
|
+
const u = obj(update);
|
|
535
|
+
const enforcement = d.policy === 'bridge-enforced' ? d.enforcement : undefined;
|
|
536
|
+
if (!u || !enforcement) return none;
|
|
537
|
+
if (u.sessionUpdate !== 'tool_call' && u.sessionUpdate !== 'tool_call_update') return none;
|
|
538
|
+
const id = typeof u.toolCallId === 'string' ? u.toolCallId : undefined;
|
|
539
|
+
const seen: SeenCall = (id && memory?.calls.get(id)) || { paths: [] };
|
|
540
|
+
if (typeof u.kind === 'string') seen.kind = u.kind;
|
|
541
|
+
const raw = obj(u.rawInput);
|
|
542
|
+
if (raw && typeof raw.cwd === 'string' && raw.cwd) seen.cwd = raw.cwd;
|
|
543
|
+
// Security review 2026-11-12 (A1): the shell command is judged too — a completed `cat` of a key
|
|
544
|
+
// names it only in its command words.
|
|
545
|
+
if (raw) {
|
|
546
|
+
const command = commandOf(raw);
|
|
547
|
+
if (command) seen.command = command;
|
|
548
|
+
}
|
|
549
|
+
// B8 — Gemini names its shell command only in the title
|
|
550
|
+
if (!seen.command && enforcement.shellGate === 'command-text') {
|
|
551
|
+
const fromTitle = commandFromTitle(u.title);
|
|
552
|
+
if (fromTitle) seen.command = fromTitle.command;
|
|
553
|
+
}
|
|
554
|
+
for (const p of pathsOf(u, enforcement.targetKeys))
|
|
555
|
+
if (!seen.paths.includes(p)) seen.paths.push(p);
|
|
556
|
+
if (id && memory) memory.calls.set(id, seen);
|
|
557
|
+
if (u.status !== 'completed') return none;
|
|
558
|
+
if (id && memory) memory.calls.delete(id);
|
|
559
|
+
|
|
560
|
+
const cwd = seen.cwd
|
|
561
|
+
? (agentPath(seen.cwd, ctx.scopeRoot, home)?.abs ?? ctx.scopeRoot)
|
|
562
|
+
: ctx.scopeRoot;
|
|
563
|
+
const paths = seen.cwd ? [...seen.paths, seen.cwd] : seen.paths;
|
|
564
|
+
const req: KindRequest = { kind: seen.kind, paths, command: seen.command ?? '', cwd };
|
|
565
|
+
if (credentialRefusalByKind(req, ctx.credentialPaths, home))
|
|
566
|
+
return {
|
|
567
|
+
violated: true,
|
|
568
|
+
reason: `completed ${seen.kind ?? 'tool'} touched a credential store`,
|
|
569
|
+
};
|
|
570
|
+
if (enforcement.readGate !== 'os-sandbox' || (id && memory?.approved.has(id))) return none;
|
|
571
|
+
const kind = seen.kind ?? '';
|
|
572
|
+
if (
|
|
573
|
+
READ_KINDS.has(kind) &&
|
|
574
|
+
seen.paths.length &&
|
|
575
|
+
!readsInside(seen.paths, cwd, ctx.readRoots, home)
|
|
576
|
+
)
|
|
577
|
+
return { violated: true, reason: `completed ${kind} outside the read roots` };
|
|
578
|
+
if (WRITE_KINDS.has(kind) && seen.paths.length) {
|
|
579
|
+
const v = writeVerdictForPaths(seen.paths, cwd, ctx.writeRoots);
|
|
580
|
+
if (v.reason === 'outside' || v.reason === 'no-target')
|
|
581
|
+
return { violated: true, reason: `completed ${kind} outside the write roots` };
|
|
582
|
+
}
|
|
583
|
+
return none;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// ---------------------------------------------------------------------------------------------------
|
|
587
|
+
// The adapter the bridge calls
|
|
588
|
+
|
|
589
|
+
export interface PermissionGate {
|
|
590
|
+
/** Rules 1–9: the decision for one request. */
|
|
591
|
+
decide(req: RequestPermissionRequest, state: GateState): GateResult;
|
|
592
|
+
/** The person's card answer, checked against what was shown; an allow is remembered for rule 12. */
|
|
593
|
+
answer(result: GateResult, optionId: string | null | undefined): RequestPermissionResponse;
|
|
594
|
+
/** Rule 12, per streamed update. */
|
|
595
|
+
observe(update: unknown): TripwireVerdict;
|
|
596
|
+
isRefusedMode(modeId: string): boolean;
|
|
597
|
+
isForbiddenClientMethod(method: string): boolean;
|
|
598
|
+
refusalNotice(result: GateResult): string | null;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/** One gate per session: `ctx` is pinned at session start, the tripwire memory lives here. */
|
|
602
|
+
export function createPermissionGate(d: EnforcedDescriptor, ctx: PolicyCtx): PermissionGate {
|
|
603
|
+
const memory = newTripwireMemory();
|
|
604
|
+
return {
|
|
605
|
+
decide: (req, state) => gatePermission(req, d, ctx, state),
|
|
606
|
+
answer: (result, optionId) => {
|
|
607
|
+
const res = answerCard(result, optionId);
|
|
608
|
+
const picked = result.strippedOptions.find(
|
|
609
|
+
(o) => res.outcome.outcome === 'selected' && o.optionId === res.outcome.optionId
|
|
610
|
+
);
|
|
611
|
+
if (result.toolCallId && picked?.kind === 'allow_once')
|
|
612
|
+
memory.approved.add(result.toolCallId);
|
|
613
|
+
return res;
|
|
614
|
+
},
|
|
615
|
+
observe: (update) => tripwire(update, d, ctx, memory),
|
|
616
|
+
isRefusedMode: (modeId) => isRefusedMode(d, modeId),
|
|
617
|
+
isForbiddenClientMethod: (method) => isForbiddenClientMethod(d, method),
|
|
618
|
+
refusalNotice: (result) => refusalNotice(d, result),
|
|
619
|
+
};
|
|
620
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// The permission gate the bridge holds for an agent that is not Claude (universal ACP, B3).
|
|
2
|
+
//
|
|
3
|
+
// F3 left a call site (one function → select / refuse / ask) and F5 built the rules
|
|
4
|
+
// (acp/policy/enforce.ts: decide · answer · observe · forbidden methods · refusal notices). This is
|
|
5
|
+
// the ONE interface between them (B3 wiring): F5's semantics, with the scope read live per call.
|
|
6
|
+
// • decide — rules 1–9 for one `session/request_permission`. An allow carries `audienceHold`: the
|
|
7
|
+
// bridge runs its audience check (H1) before granting. An ask carries `strippedOptions`: the card
|
|
8
|
+
// shows only `allow_once` / `reject_once` (rule 9).
|
|
9
|
+
// • answer — the person's card answer, checked against what the card showed; an allow is
|
|
10
|
+
// remembered so the tripwire does not fire on a call the person approved.
|
|
11
|
+
// • observe — rule 12, per streamed `session/update`.
|
|
12
|
+
// • isForbiddenClientMethod — rule 11: `fs/*` and `terminal/*` from any non-Claude agent.
|
|
13
|
+
// • refusalNotice / cardRefusalNotice — rule 13, the line the panel shows when a refusal ends the
|
|
14
|
+
// agent's turn (`rejectEndsTurn`).
|
|
15
|
+
// Implemented by acp/policy/bridge-gate.ts `createBridgeGate`; the bridge never calls enforce.ts
|
|
16
|
+
// directly. A non-Claude agent with no gate wired is refused everything (fail closed).
|
|
17
|
+
|
|
18
|
+
import type { RequestPermissionRequest, RequestPermissionResponse } from '@agentclientprotocol/sdk';
|
|
19
|
+
|
|
20
|
+
import type { AgentDescriptor, PolicyCtx } from '../agents/descriptor.ts';
|
|
21
|
+
import type { GateResult, GateState, TripwireVerdict } from './enforce.ts';
|
|
22
|
+
|
|
23
|
+
export type { GateResult, GateState, TripwireVerdict } from './enforce.ts';
|
|
24
|
+
|
|
25
|
+
export interface PermissionGateInput {
|
|
26
|
+
req: RequestPermissionRequest;
|
|
27
|
+
descriptor: AgentDescriptor;
|
|
28
|
+
/** The same scope the launch policy was built from, read live (the "AI may" switches can change). */
|
|
29
|
+
ctx: PolicyCtx;
|
|
30
|
+
/** Per-request facts the bridge holds live: stopped, this chat's read grants, a run's write scope. */
|
|
31
|
+
state: GateState;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface PermissionGate {
|
|
35
|
+
decide(input: PermissionGateInput): GateResult;
|
|
36
|
+
answer(result: GateResult, optionId: string | null | undefined): RequestPermissionResponse;
|
|
37
|
+
observe(update: unknown, ctx: PolicyCtx): TripwireVerdict;
|
|
38
|
+
isForbiddenClientMethod(method: string): boolean;
|
|
39
|
+
refusalNotice(result: GateResult): string | null;
|
|
40
|
+
cardRefusalNotice(): string | null;
|
|
41
|
+
}
|