@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.
Files changed (74) hide show
  1. package/apps/studio/acp/agents/auth.ts +241 -0
  2. package/apps/studio/acp/agents/catalog.ts +308 -0
  3. package/apps/studio/acp/agents/chat-key.ts +64 -0
  4. package/apps/studio/acp/agents/conformance.ts +1853 -0
  5. package/apps/studio/acp/agents/consent.ts +92 -0
  6. package/apps/studio/acp/agents/descriptor.ts +188 -0
  7. package/apps/studio/acp/agents/discover.ts +491 -0
  8. package/apps/studio/acp/agents/e2e-model.ts +58 -0
  9. package/apps/studio/acp/agents/health.ts +46 -0
  10. package/apps/studio/acp/agents/link-scan.ts +230 -0
  11. package/apps/studio/acp/agents/spawn.ts +164 -0
  12. package/apps/studio/acp/agents/state.ts +132 -0
  13. package/apps/studio/acp/ask-queue.ts +13 -4
  14. package/apps/studio/acp/bootstrap-brief.ts +16 -3
  15. package/apps/studio/acp/bridge.ts +709 -99
  16. package/apps/studio/acp/env.ts +43 -1
  17. package/apps/studio/acp/index.ts +428 -43
  18. package/apps/studio/acp/may-scope.ts +244 -21
  19. package/apps/studio/acp/policy/agent-home.ts +202 -0
  20. package/apps/studio/acp/policy/bridge-gate.ts +68 -0
  21. package/apps/studio/acp/policy/codex-home.ts +228 -0
  22. package/apps/studio/acp/policy/enforce.ts +620 -0
  23. package/apps/studio/acp/policy/gate-types.ts +41 -0
  24. package/apps/studio/acp/policy/launch-codex.ts +351 -0
  25. package/apps/studio/acp/policy/launch-copilot.ts +119 -0
  26. package/apps/studio/acp/policy/launch-gemini.ts +120 -0
  27. package/apps/studio/acp/policy/launch-opencode.ts +131 -0
  28. package/apps/studio/acp/policy/opencode-user-config.ts +240 -0
  29. package/apps/studio/acp/probe.ts +78 -14
  30. package/apps/studio/acp/transcript.ts +14 -0
  31. package/apps/studio/acp/write-scope.ts +62 -2
  32. package/apps/studio/client/panels/ChatPanel.jsx +19 -4
  33. package/apps/studio/client/panels/PermissionPrompt.jsx +11 -6
  34. package/apps/studio/client/panels/acp-runtime.js +95 -7
  35. package/apps/studio/client/v2/ai/Advanced.jsx +4 -3
  36. package/apps/studio/client/v2/ai/AgentNotice.jsx +138 -0
  37. package/apps/studio/client/v2/ai/AgentOffer.jsx +110 -0
  38. package/apps/studio/client/v2/ai/AgentPicker.jsx +121 -0
  39. package/apps/studio/client/v2/ai/Composer.jsx +4 -0
  40. package/apps/studio/client/v2/ai/ConnectSheet.jsx +13 -0
  41. package/apps/studio/client/v2/ai/Feed.jsx +8 -0
  42. package/apps/studio/client/v2/ai/NeedsYouSlot.jsx +24 -1
  43. package/apps/studio/client/v2/ai/Suggest.jsx +5 -1
  44. package/apps/studio/client/v2/ai/advanced-model.js +17 -6
  45. package/apps/studio/client/v2/ai/agents-model.js +333 -0
  46. package/apps/studio/client/v2/ai/agents-store.js +223 -0
  47. package/apps/studio/client/v2/ai/ai.css +63 -0
  48. package/apps/studio/client/v2/ai/needs-you.js +1 -1
  49. package/apps/studio/client/v2/fold/fold-store.js +1 -0
  50. package/apps/studio/client/v2/settings/agent-keys.js +88 -0
  51. package/apps/studio/client/v2/settings/agents-card.jsx +408 -0
  52. package/apps/studio/client/v2/settings/connections-model.js +1 -1
  53. package/apps/studio/client/v2/settings/connections-section.jsx +4 -0
  54. package/apps/studio/client/v2/settings/settings-cards.js +2 -1
  55. package/apps/studio/client/v2/settings/settings-model.js +1 -0
  56. package/apps/studio/client/v2/settings/settings.css +7 -0
  57. package/apps/studio/client/v2/shell/served-routes.js +1 -0
  58. package/apps/studio/cloud-build.ts +5 -0
  59. package/apps/studio/dist/client.bundle.js +604 -604
  60. package/apps/studio/dist/styles.css +1 -1
  61. package/apps/studio/generation/keys.ts +197 -11
  62. package/apps/studio/outbox/policy.ts +8 -2
  63. package/apps/studio/outbox/types.ts +5 -0
  64. package/apps/studio/routes/agents.ts +479 -0
  65. package/apps/studio/routes/index.ts +3 -0
  66. package/apps/studio/scripts/acp-conformance-local.ts +95 -0
  67. package/apps/studio/scripts/acp-e2e-local.ts +1026 -0
  68. package/apps/studio/server.ts +9 -0
  69. package/apps/studio/sync/writer-registry.ts +12 -0
  70. package/apps/studio/whats-new.json +10 -0
  71. package/apps/studio/ws.ts +3 -0
  72. package/cli/commands/codex.mjs +36 -2
  73. package/cli/lib/harness/codex-runtime.mjs +98 -11
  74. package/package.json +9 -8
@@ -17,7 +17,16 @@
17
17
  // `{ configured: true }`, mirroring `github_is_signed_in`). `isConfigured` backs
18
18
  // the GET status probe — it reveals only presence, never the value.
19
19
 
20
- import { chmodSync, existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
20
+ import {
21
+ chmodSync,
22
+ existsSync,
23
+ mkdirSync,
24
+ readFileSync,
25
+ renameSync,
26
+ rmSync,
27
+ statSync,
28
+ writeFileSync,
29
+ } from 'node:fs';
21
30
  import { homedir, platform } from 'node:os';
22
31
  import { dirname, join } from 'node:path';
23
32
 
@@ -34,6 +43,19 @@ export function keysConfigPath(): string {
34
43
 
35
44
  interface KeysFile {
36
45
  keys: Record<string, string>;
46
+ /**
47
+ * B7 (DDR-252) — the LEGACY chat scope, before keys were per profile: one key per ACP agent id.
48
+ * Read as the `default` profile's only (security review 2026-11-12, M3); the first chat-key write
49
+ * moves it into `chatByProfile.default`.
50
+ */
51
+ chat?: Record<string, string>;
52
+ /** The chat scope per profile: profile id → agent id (`codex`, `gemini`, …) → key, never Claude's. */
53
+ chatByProfile?: Record<string, Record<string, string>>;
54
+ /**
55
+ * B7 — Claude's Anthropic key, per profile (DDR-252 amends DDR-123): present only after the
56
+ * person turned on "Use an Anthropic API key for Claude" for that profile. `optIn` must be true.
57
+ */
58
+ claudeApiKey?: Record<string, { key: string; optIn: true; at: number }>;
37
59
  }
38
60
 
39
61
  let _modeWarnedFor: string | null = null;
@@ -66,19 +88,23 @@ function readKeysFile(): KeysFile {
66
88
  }
67
89
  }
68
90
 
69
- /** Write keys.json atomically-ish with a hard 0600 mode (create + chmod). */
91
+ /**
92
+ * Write keys.json atomically with a hard 0600 mode: a 0600 temp file beside it, then rename (B7 —
93
+ * the file now holds chat keys too, so a torn write must never leave a half file or a wider mode).
94
+ */
70
95
  function writeKeysFile(data: KeysFile): void {
71
96
  const path = keysConfigPath();
72
97
  mkdirSync(dirname(path), { recursive: true });
73
- // Write then chmod — writeFileSync's mode is masked by umask, so we assert
74
- // 0600 explicitly afterwards (same guarantee cli/lib/hubs-config.mjs makes).
75
- writeFileSync(path, `${JSON.stringify(data, null, 2)}\n`, { mode: 0o600 });
76
- if (platform() !== 'win32') {
77
- try {
78
- chmodSync(path, 0o600);
79
- } catch {
80
- /* best-effort — the warn-on-read path is the backstop */
81
- }
98
+ const tmp = `${path}.${process.pid}.${Date.now()}.tmp`;
99
+ try {
100
+ // `wx`: never through a planted file. writeFileSync's mode is masked by umask, so chmod 0600
101
+ // explicitly before the rename (same guarantee cli/lib/hubs-config.mjs makes).
102
+ writeFileSync(tmp, `${JSON.stringify(data, null, 2)}\n`, { mode: 0o600, flag: 'wx' });
103
+ if (platform() !== 'win32') chmodSync(tmp, 0o600);
104
+ renameSync(tmp, path);
105
+ } catch (err) {
106
+ rmSync(tmp, { force: true });
107
+ throw err;
82
108
  }
83
109
  }
84
110
 
@@ -160,3 +186,163 @@ export function configuredProviders(): string[] {
160
186
  .filter(([, v]) => typeof v === 'string' && v.length > 0)
161
187
  .map(([id]) => id);
162
188
  }
189
+
190
+ // ---------------------------------------------------------------- chat scope (B7, DDR-252)
191
+ //
192
+ // One key per ACP agent, injected by acp/agents/chat-key.ts into THAT agent's env only (after the
193
+ // shared scrub, spawn.ts). Nothing here hands a key to a route: routes read `chatKeyStatus` —
194
+ // presence and the last four characters — and only the spawn path reads the value (`getChatKey`).
195
+
196
+ /** An agent id a chat key may be stored under: a catalog slug. `claude` has its own slot below. */
197
+ const CHAT_AGENT_ID_RE = /^[a-z][a-z0-9-]{0,31}$/;
198
+ /** sidecar::profile_env's id shape (profiles.rs); anything else is the default profile. */
199
+ const PROFILE_ID_RE = /^[a-z0-9-]{1,32}$/;
200
+
201
+ /** A key as typed: trimmed, non-empty, at most 512 characters, no whitespace or control chars. */
202
+ export function normalizeChatKey(value: unknown): string | null {
203
+ if (typeof value !== 'string') return null;
204
+ const key = value.trim();
205
+ if (key.length === 0 || key.length > 512) return null;
206
+ if (/[\s\p{Cc}]/u.test(key)) return null;
207
+ return key;
208
+ }
209
+
210
+ function chatAgentId(agentId: string): string {
211
+ if (agentId === 'claude' || !CHAT_AGENT_ID_RE.test(agentId)) throw new Error('invalid agent id');
212
+ return agentId;
213
+ }
214
+
215
+ /** The profile this server runs as — the server learns it only from env (`sidecar::profile_env`). */
216
+ export function currentProfileId(env: Record<string, string | undefined> = process.env): string {
217
+ const id = env.MAUDE_PROFILE_ID;
218
+ return id && PROFILE_ID_RE.test(id) ? id : 'default';
219
+ }
220
+
221
+ export interface ChatKeyStatus {
222
+ set: boolean;
223
+ /** The last four characters, only when set (Settings shows ••••last4). */
224
+ last4: string | null;
225
+ }
226
+
227
+ const statusOf = (key: string | null | undefined): ChatKeyStatus =>
228
+ typeof key === 'string' && key.length > 0
229
+ ? { set: true, last4: key.slice(-4) }
230
+ : { set: false, last4: null };
231
+
232
+ /**
233
+ * Security review 2026-11-12 (defender M3) — chat keys are per profile, like Claude's opt-in key:
234
+ * a work profile's chat never runs on the personal profile's key. The pre-profile `chat` map is the
235
+ * `default` profile's (the legacy install IS the default profile, profiles.rs) and no other's.
236
+ */
237
+ function chatKeysOf(file: KeysFile, profileId: string): Record<string, string> {
238
+ const legacy = profileId === 'default' ? (file.chat ?? {}) : {};
239
+ return { ...legacy, ...(file.chatByProfile?.[profileId] ?? {}) };
240
+ }
241
+
242
+ /** Move the legacy `chat` map into `chatByProfile.default` (before any chat-key write). */
243
+ function migrateLegacyChat(file: KeysFile): void {
244
+ if (!file.chat) return;
245
+ const byProfile = { ...(file.chatByProfile ?? {}) };
246
+ byProfile.default = { ...file.chat, ...(byProfile.default ?? {}) };
247
+ file.chatByProfile = byProfile;
248
+ delete file.chat;
249
+ }
250
+
251
+ /** Store an agent's chat key for this profile (Settings › AI agents). Throws on a bad id or key. */
252
+ export function setChatKey(
253
+ agentId: string,
254
+ value: unknown,
255
+ profileId: string = currentProfileId()
256
+ ): ChatKeyStatus {
257
+ const id = chatAgentId(agentId);
258
+ const key = normalizeChatKey(value);
259
+ if (!key) throw new Error('invalid key');
260
+ if (!PROFILE_ID_RE.test(profileId)) throw new Error('invalid profile');
261
+ const file = readKeysFile();
262
+ migrateLegacyChat(file);
263
+ file.chatByProfile = {
264
+ ...(file.chatByProfile ?? {}),
265
+ [profileId]: { ...(file.chatByProfile?.[profileId] ?? {}), [id]: key },
266
+ };
267
+ writeKeysFile(file);
268
+ return statusOf(key);
269
+ }
270
+
271
+ /** Remove this profile's chat key for an agent. No-op when absent. */
272
+ export function clearChatKey(agentId: string, profileId: string = currentProfileId()): void {
273
+ const id = chatAgentId(agentId);
274
+ const file = readKeysFile();
275
+ if (!(id in chatKeysOf(file, profileId))) return;
276
+ migrateLegacyChat(file);
277
+ const mine = file.chatByProfile?.[profileId];
278
+ if (mine && id in mine) delete mine[id];
279
+ writeKeysFile(file);
280
+ }
281
+
282
+ /** Presence + last four characters — never the key. Claude's reads this profile's opt-in slot. */
283
+ export function chatKeyStatus(agentId: string): ChatKeyStatus {
284
+ if (agentId === 'claude') return claudeApiKeyStatus();
285
+ if (!CHAT_AGENT_ID_RE.test(agentId)) return statusOf(null);
286
+ return statusOf(normalizeChatKey(chatKeysOf(readKeysFile(), currentProfileId())[agentId]));
287
+ }
288
+
289
+ export function hasChatKey(agentId: string): boolean {
290
+ return chatKeyStatus(agentId).set;
291
+ }
292
+
293
+ /**
294
+ * The key itself — for the spawn path ONLY (acp/agents/chat-key.ts), into the owning agent's env.
295
+ * Never returned by a route. Claude's answers only when this profile opted in.
296
+ */
297
+ export function getChatKey(agentId: string): string | null {
298
+ if (agentId === 'claude') return claudeApiKeyFor(currentProfileId());
299
+ if (!CHAT_AGENT_ID_RE.test(agentId)) return null;
300
+ return normalizeChatKey(chatKeysOf(readKeysFile(), currentProfileId())[agentId]);
301
+ }
302
+
303
+ /**
304
+ * Turn on "Use an Anthropic API key for Claude instead of your Claude account" for this profile
305
+ * (DDR-252 amends DDR-123). `optIn` must be literally `true`: the confirm card is the person's
306
+ * choice, and nothing stores a Claude key without it.
307
+ */
308
+ export function setClaudeApiKey(
309
+ value: unknown,
310
+ optIn: unknown,
311
+ profileId: string = currentProfileId()
312
+ ): ChatKeyStatus {
313
+ if (optIn !== true) throw new Error('opt-in required');
314
+ if (!PROFILE_ID_RE.test(profileId)) throw new Error('invalid profile');
315
+ const key = normalizeChatKey(value);
316
+ if (!key) throw new Error('invalid key');
317
+ const file = readKeysFile();
318
+ file.claudeApiKey = {
319
+ ...(file.claudeApiKey ?? {}),
320
+ [profileId]: { key, optIn: true, at: Date.now() },
321
+ };
322
+ writeKeysFile(file);
323
+ return statusOf(key);
324
+ }
325
+
326
+ /** Turn the opt-in off for this profile: the key goes with it and Claude is back on the account. */
327
+ export function clearClaudeApiKey(profileId: string = currentProfileId()): void {
328
+ const file = readKeysFile();
329
+ if (file.claudeApiKey && profileId in file.claudeApiKey) {
330
+ delete file.claudeApiKey[profileId];
331
+ writeKeysFile(file);
332
+ }
333
+ }
334
+
335
+ function claudeApiKeyFor(profileId: string): string | null {
336
+ const rec = readKeysFile().claudeApiKey?.[profileId];
337
+ if (!rec || typeof rec !== 'object' || rec.optIn !== true) return null;
338
+ return normalizeChatKey(rec.key);
339
+ }
340
+
341
+ export function claudeApiKeyStatus(profileId: string = currentProfileId()): ChatKeyStatus {
342
+ return statusOf(claudeApiKeyFor(profileId));
343
+ }
344
+
345
+ /** Whether this profile turned the Claude API-key opt-in on (with a key stored). */
346
+ export function claudeApiKeyOptedIn(profileId: string = currentProfileId()): boolean {
347
+ return claudeApiKeyFor(profileId) !== null;
348
+ }
@@ -110,7 +110,9 @@ const NEEDS_SIGN_IN: readonly IntentKind[] = [
110
110
  * content-lane entry without a final answer (O4: the `after` gate).
111
111
  */
112
112
  export function closedGates(
113
- record: Pick<IntentRecord, 'kind' | 'project' | 'watermark'>,
113
+ record: Pick<IntentRecord, 'kind' | 'project' | 'watermark'> & {
114
+ payload?: { kind: string; agent?: unknown };
115
+ },
114
116
  facts: GateFacts,
115
117
  contentPending: readonly number[],
116
118
  now: number
@@ -122,7 +124,11 @@ export function closedGates(
122
124
  // S8.10 — a Handoff to production is an AI run that writes the code: it waits on the AI gates too
123
125
  // ("Connect your Claude account to let AI draft this."; after Connect, the waiting handoff runs).
124
126
  if (record.kind === 'ai.prompt' || record.kind === 'export.handoff') {
125
- if (!facts.aiConnected) closed.add('ai-connected');
127
+ // E2E finding (2026-11-12): `aiConnected` is CLAUDE's sign-in. A chat on another agent (Codex,
128
+ // opencode — its connection is checked at attach) is not held by it (plan Decision 1).
129
+ const agent = record.payload?.kind === 'ai.prompt' ? record.payload.agent : null;
130
+ const otherAgent = typeof agent === 'string' && agent !== 'claude';
131
+ if (!facts.aiConnected && !otherAgent) closed.add('ai-connected');
126
132
  if (!facts.aiReady) closed.add('ai-ready');
127
133
  if (typeof facts.allowanceResetsAt === 'number' && now < facts.allowanceResetsAt)
128
134
  closed.add('ai-allowance');
@@ -85,6 +85,11 @@ export type IntentPayload =
85
85
  attachments: string[];
86
86
  /** 'field' = 03's "waits in the field" (setting up, used up) */
87
87
  presentation: 'sent' | 'field';
88
+ /**
89
+ * B3 (universal ACP) — the chat's agent when it is not Claude. Claude's sign-in gate
90
+ * (`ai-connected`) does not hold it: that agent's connection is checked at attach.
91
+ */
92
+ agent?: string;
88
93
  }
89
94
  | { kind: 'share.invite'; email: string; role: Role; scope: ShareScope; message?: string }
90
95
  | { kind: 'share.ask-to-edit'; scope: ShareScope; note?: string }