@vanillagreen/pi-claude-bridge 1.8.0 → 1.9.0
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/README.md +60 -10
- package/bundle/connector-inventory.js +137 -0
- package/bundle/index.js +8682 -8287
- package/package.json +6 -2
- package/src/agents-md.ts +5 -7
- package/src/assistant-stream.ts +307 -0
- package/src/bridge-state.ts +136 -0
- package/src/claude-executable.ts +264 -0
- package/src/config.ts +13 -7
- package/src/connector-inventory.ts +281 -0
- package/src/connectors.ts +359 -0
- package/src/debug.ts +80 -0
- package/src/index.ts +109 -1579
- package/src/models.ts +22 -1
- package/src/query-state.ts +42 -0
- package/src/rate-limit.ts +63 -0
- package/src/session-persistence.ts +329 -0
- package/src/stream-idle-watchdog.ts +134 -0
- package/src/tool-mapping.ts +53 -0
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
import { type HookCallback, type query } from "@anthropic-ai/claude-agent-sdk";
|
|
2
|
+
import { normalizeConnectorWriteMode, type Config, type ConnectorWriteMode } from "./config.js";
|
|
3
|
+
import { MCP_SERVER_NAME } from "./skills.js";
|
|
4
|
+
|
|
5
|
+
// Disable Claude Code built-ins in the provider path. Pi owns tool execution;
|
|
6
|
+
// Claude reaches Pi tools through the bridged MCP server instead.
|
|
7
|
+
//
|
|
8
|
+
// `allowedTools` is a permission auto-allow list in the Claude Agent SDK, not a
|
|
9
|
+
// visibility allowlist. Use `tools: []` to remove the built-in tool set, and keep
|
|
10
|
+
// this disallow list as a belt-and-suspenders guard for SDK/CLI built-ins that may
|
|
11
|
+
// otherwise leak into the model context (e.g. TodoWrite, CronList, SendMessage).
|
|
12
|
+
export const DISALLOWED_BUILTIN_TOOLS = [
|
|
13
|
+
"Read", "Write", "Edit", "MultiEdit", "Glob", "Grep", "Bash", "Agent", "Task",
|
|
14
|
+
"NotebookEdit", "EnterWorktree", "ExitWorktree",
|
|
15
|
+
"CronList", "CronCreate", "CronDelete", "TeamCreate", "TeamDelete",
|
|
16
|
+
"TaskOutput", "TaskStop", "SendMessage", "Skill",
|
|
17
|
+
"TodoRead", "TodoWrite",
|
|
18
|
+
"ListMcpResources", "ReadMcpResource",
|
|
19
|
+
"WebFetch", "WebSearch",
|
|
20
|
+
"AskUserQuestion", "EnterPlanMode", "ExitPlanMode",
|
|
21
|
+
"ToolSearch", "ScheduleWakeup",
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
export const CLAUDE_BRIDGE_TOOL_ISOLATION = {
|
|
25
|
+
tools: [] as string[],
|
|
26
|
+
disallowedTools: DISALLOWED_BUILTIN_TOOLS,
|
|
27
|
+
allowedTools: [`mcp__${MCP_SERVER_NAME}__*`],
|
|
28
|
+
} satisfies Pick<NonNullable<Parameters<typeof query>[0]["options"]>, "tools" | "allowedTools" | "disallowedTools">;
|
|
29
|
+
|
|
30
|
+
// --- Claude account cloud MCP connectors (Gmail / Calendar / Drive) ---
|
|
31
|
+
//
|
|
32
|
+
// By default the bridge suppresses claude.ai cloud MCP servers (see the
|
|
33
|
+
// ENABLE_CLAUDEAI_MCP_SERVERS="0" note near the query builder) so Pi owns tool
|
|
34
|
+
// execution and tokens stay lean. This opt-in flag lets the authenticated
|
|
35
|
+
// Claude account's authorized Google connectors flow through to the model,
|
|
36
|
+
// exposing Gmail/Calendar/Drive tools the account has connected. Gated so the
|
|
37
|
+
// default behavior is unchanged. See
|
|
38
|
+
// docs/plans/claude-bridge-google-connectors.md.
|
|
39
|
+
export function connectorsEnabledFromEnv(): boolean {
|
|
40
|
+
const v = (process.env.CLAUDE_BRIDGE_ENABLE_CONNECTORS ?? "").trim().toLowerCase();
|
|
41
|
+
return v === "1" || v === "true" || v === "yes" || v === "on";
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Connectors are enabled if EITHER the env var is truthy OR the resolved bridge
|
|
45
|
+
// config sets `provider.enableConnectors`. Env is the simplest per-process knob
|
|
46
|
+
// (one sidecar per Claude account sets it in its child env); config lets a host
|
|
47
|
+
// app enable it declaratively via its written settings.json.
|
|
48
|
+
export function connectorsEnabledFor(config?: Config): boolean {
|
|
49
|
+
return connectorsEnabledFromEnv() || config?.provider?.enableConnectors === true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Cloud MCP connector tool namespaces auto-allowed when connectors are enabled.
|
|
53
|
+
// Names match Claude Code's claude.ai connector servers.
|
|
54
|
+
// Whole-server globs (the only glob shape the CLI matcher honors). Deny rules
|
|
55
|
+
// take precedence, so listing a server here never exposes its writes — the
|
|
56
|
+
// write ids above and the PreToolUse hook still remove them.
|
|
57
|
+
export const CLAUDE_AI_CONNECTOR_TOOL_PATTERNS = [
|
|
58
|
+
"mcp__claude_ai_Gmail__*",
|
|
59
|
+
"mcp__claude_ai_Google_Calendar__*",
|
|
60
|
+
"mcp__claude_ai_Google_Drive__*",
|
|
61
|
+
"mcp__claude_ai_Slack__*",
|
|
62
|
+
"mcp__claude_ai_Atlassian__*",
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
// Claude Code registers a Claude account's cloud connectors as DEFERRED tools
|
|
66
|
+
// that the model must load via ToolSearch (and enumerate via the MCP-resource
|
|
67
|
+
// tools). The default bridge isolation disallows all three so Pi owns tool
|
|
68
|
+
// discovery — but that hides the connectors from the model entirely. When
|
|
69
|
+
// connectors are enabled we must let these through so Gmail/Calendar/Drive are
|
|
70
|
+
// discoverable. Verified: disallowing ToolSearch reliably yields NO_CONNECTORS.
|
|
71
|
+
export const CONNECTOR_DISCOVERY_TOOLS = ["ToolSearch", "ListMcpResources", "ReadMcpResource"];
|
|
72
|
+
|
|
73
|
+
// --- Connector WRITE tool control (read-inline / write-by-approval) ---
|
|
74
|
+
//
|
|
75
|
+
// Connector tools execute INSIDE claude via the bridge, so Memsira's Pi-level
|
|
76
|
+
// ConsentGate never sees them. To keep every connector WRITE explicit + gated,
|
|
77
|
+
// connector chat sessions run read-only (writes denied); the model performs a
|
|
78
|
+
// write only through a gated Pi custom-tool whose app-side dispatcher runs a
|
|
79
|
+
// ONE-SHOT write-enabled bridge query. This block is the bridge lever for that:
|
|
80
|
+
// deny connector write tools by default, allow them only for that executor.
|
|
81
|
+
//
|
|
82
|
+
// Cloud connector server namespaces (the `mcp__<server>__` prefix). Every
|
|
83
|
+
// claude.ai connector server lives under CONNECTOR_NS_PREFIX, so that prefix —
|
|
84
|
+
// not the named trio — is what marks a tool as connector-owned.
|
|
85
|
+
const CONNECTOR_NS_PREFIX = "mcp__claude_ai_";
|
|
86
|
+
const CONNECTOR_NS_GMAIL = `${CONNECTOR_NS_PREFIX}Gmail__`;
|
|
87
|
+
const CONNECTOR_NS_CALENDAR = `${CONNECTOR_NS_PREFIX}Google_Calendar__`;
|
|
88
|
+
const CONNECTOR_NS_DRIVE = `${CONNECTOR_NS_PREFIX}Google_Drive__`;
|
|
89
|
+
const CONNECTOR_NS_SLACK = `${CONNECTOR_NS_PREFIX}Slack__`;
|
|
90
|
+
const CONNECTOR_NS_ATLASSIAN = `${CONNECTOR_NS_PREFIX}Atlassian__`;
|
|
91
|
+
|
|
92
|
+
// Read verbs: a connector tool whose tool segment BEGINS with one of these
|
|
93
|
+
// words is a non-mutating READ and stays available. Everything else on a
|
|
94
|
+
// connector namespace is treated as a WRITE. Matching is on WORDS, not on a
|
|
95
|
+
// literal `verb_` prefix, because connector servers do not share a naming
|
|
96
|
+
// convention — verified live against a Claude account with Slack + Atlassian
|
|
97
|
+
// attached:
|
|
98
|
+
//
|
|
99
|
+
// Gmail search_threads, get_message (snake_case)
|
|
100
|
+
// Slack slack_read_channel, slack_search_public
|
|
101
|
+
// (snake_case, server-prefixed)
|
|
102
|
+
// Atlassian getJiraIssue, searchJiraIssuesUsingJql, getConfluencePage
|
|
103
|
+
// (camelCase)
|
|
104
|
+
//
|
|
105
|
+
// A literal-prefix test only matched the Gmail shape, so EVERY Slack and
|
|
106
|
+
// Atlassian tool — reads included — classified as a write and was denied at
|
|
107
|
+
// runtime, making those connectors unusable in a read-only session.
|
|
108
|
+
const CONNECTOR_READ_VERBS = new Set([
|
|
109
|
+
"list", "search", "get", "read", "fetch", "find",
|
|
110
|
+
"download", "describe", "query", "count", "view", "lookup", "whoami",
|
|
111
|
+
]);
|
|
112
|
+
|
|
113
|
+
// Mutating words. Two jobs, both on the deny side:
|
|
114
|
+
//
|
|
115
|
+
// 1. A name that begins with a read verb but also names a mutation
|
|
116
|
+
// (`fetchAndLock`, `get_incident_and_acknowledge`) is a WRITE — deny wins
|
|
117
|
+
// over the read exemption, so a compound name cannot earn read treatment.
|
|
118
|
+
// 2. A leading word that repeats the server name is NOT skipped when it is a
|
|
119
|
+
// mutation word, so a connector whose SERVER is verb-shaped
|
|
120
|
+
// (`Delete__delete_get_thing`) keeps its real verb.
|
|
121
|
+
//
|
|
122
|
+
// This list is deliberately broad and errs toward over-denial: a read wrongly
|
|
123
|
+
// called a write only blocks a read, whereas the reverse runs an ungated
|
|
124
|
+
// mutation. It is a backstop, not the primary protection — that is the leading
|
|
125
|
+
// verb, which real connector tools put first (`createJiraIssue`,
|
|
126
|
+
// `slack_send_message`, `delete_file`). Nouns that collide with real read names
|
|
127
|
+
// are deliberately excluded (`comment`/`tag`/`flag`/`run`, since
|
|
128
|
+
// `getComment`, `searchByTag`, `getFeatureFlag`, `getWorkflowRun` are reads).
|
|
129
|
+
const CONNECTOR_MUTATION_WORDS = new Set([
|
|
130
|
+
"create", "update", "delete", "remove", "add", "edit", "send", "post",
|
|
131
|
+
"write", "upload", "publish", "schedule", "transition", "archive", "move",
|
|
132
|
+
"copy", "revoke", "assign", "invite", "share", "rename", "replace", "set",
|
|
133
|
+
"merge", "resolve", "lock", "unlock", "acknowledge", "ack", "book",
|
|
134
|
+
"start", "stop", "terminate", "restart", "join", "leave", "star", "unstar",
|
|
135
|
+
"forward", "sync", "approve", "reject", "close", "reopen", "cancel",
|
|
136
|
+
"enable", "disable", "grant", "trigger", "execute", "apply", "submit",
|
|
137
|
+
"pin", "unpin", "mute", "unmute", "subscribe", "unsubscribe", "follow",
|
|
138
|
+
"unfollow", "clear", "purge", "reset", "rotate", "deploy", "install",
|
|
139
|
+
"uninstall", "save", "store", "put", "patch", "insert", "append",
|
|
140
|
+
"prepend", "duplicate", "restore", "revert", "import", "export", "upsert",
|
|
141
|
+
"sign", "complete", "claim", "release", "promote", "demote", "escalate",
|
|
142
|
+
"resend", "retry", "react", "vote",
|
|
143
|
+
]);
|
|
144
|
+
|
|
145
|
+
// Splits a tool (or server) segment into lowercase words, handling snake_case,
|
|
146
|
+
// camelCase, PascalCase, and acronym runs (`getHTTPResponse` → get, http,
|
|
147
|
+
// response). Punctuation-only input yields an empty list, which callers treat
|
|
148
|
+
// as unparseable → write.
|
|
149
|
+
function connectorNameWords(segment: string): string[] {
|
|
150
|
+
return segment
|
|
151
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
152
|
+
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
|
|
153
|
+
.split(/[^A-Za-z0-9]+/)
|
|
154
|
+
.filter(Boolean)
|
|
155
|
+
.map((word) => word.toLowerCase());
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Explicit known write tool names (current claude.ai connectors). Passed to the
|
|
159
|
+
// SDK disallowedTools so today's writes are removed from the model's context by
|
|
160
|
+
// exact tool id (the CLI matcher only supports exact ids or a whole-server glob).
|
|
161
|
+
export const CONNECTOR_WRITE_TOOLS = [
|
|
162
|
+
`${CONNECTOR_NS_GMAIL}create_draft`,
|
|
163
|
+
`${CONNECTOR_NS_GMAIL}create_label`,
|
|
164
|
+
`${CONNECTOR_NS_GMAIL}label_message`,
|
|
165
|
+
`${CONNECTOR_NS_GMAIL}label_thread`,
|
|
166
|
+
`${CONNECTOR_NS_GMAIL}unlabel_message`,
|
|
167
|
+
`${CONNECTOR_NS_GMAIL}unlabel_thread`,
|
|
168
|
+
`${CONNECTOR_NS_GMAIL}apply_sensitive_label`,
|
|
169
|
+
`${CONNECTOR_NS_GMAIL}remove_sensitive_label`,
|
|
170
|
+
`${CONNECTOR_NS_CALENDAR}create_event`,
|
|
171
|
+
`${CONNECTOR_NS_CALENDAR}update_event`,
|
|
172
|
+
`${CONNECTOR_NS_CALENDAR}delete_event`,
|
|
173
|
+
`${CONNECTOR_NS_CALENDAR}respond_to_event`,
|
|
174
|
+
`${CONNECTOR_NS_DRIVE}create_file`,
|
|
175
|
+
`${CONNECTOR_NS_DRIVE}copy_file`,
|
|
176
|
+
// Slack + Atlassian writes, taken from a live enumeration of an account with
|
|
177
|
+
// both connectors attached. The PreToolUse hook already denies these by verb;
|
|
178
|
+
// listing them by id also removes them from the model's context in a
|
|
179
|
+
// read-only session (the CLI matcher needs exact ids). Additive only — an id
|
|
180
|
+
// missing here is still denied at call time.
|
|
181
|
+
`${CONNECTOR_NS_SLACK}slack_send_message`,
|
|
182
|
+
`${CONNECTOR_NS_SLACK}slack_send_message_draft`,
|
|
183
|
+
`${CONNECTOR_NS_SLACK}slack_schedule_message`,
|
|
184
|
+
`${CONNECTOR_NS_SLACK}slack_create_canvas`,
|
|
185
|
+
`${CONNECTOR_NS_SLACK}slack_update_canvas`,
|
|
186
|
+
`${CONNECTOR_NS_ATLASSIAN}createJiraIssue`,
|
|
187
|
+
`${CONNECTOR_NS_ATLASSIAN}editJiraIssue`,
|
|
188
|
+
`${CONNECTOR_NS_ATLASSIAN}transitionJiraIssue`,
|
|
189
|
+
`${CONNECTOR_NS_ATLASSIAN}addCommentToJiraIssue`,
|
|
190
|
+
`${CONNECTOR_NS_ATLASSIAN}addWorklogToJiraIssue`,
|
|
191
|
+
`${CONNECTOR_NS_ATLASSIAN}createIssueLink`,
|
|
192
|
+
`${CONNECTOR_NS_ATLASSIAN}createConfluencePage`,
|
|
193
|
+
`${CONNECTOR_NS_ATLASSIAN}updateConfluencePage`,
|
|
194
|
+
`${CONNECTOR_NS_ATLASSIAN}createConfluenceFooterComment`,
|
|
195
|
+
`${CONNECTOR_NS_ATLASSIAN}createConfluenceInlineComment`,
|
|
196
|
+
`${CONNECTOR_NS_ATLASSIAN}createCompassComponent`,
|
|
197
|
+
`${CONNECTOR_NS_ATLASSIAN}createCompassComponentRelationship`,
|
|
198
|
+
`${CONNECTOR_NS_ATLASSIAN}createCompassCustomFieldDefinition`,
|
|
199
|
+
];
|
|
200
|
+
|
|
201
|
+
// Classify a connector tool name as a WRITE (mutating) tool. FAIL CLOSED, twice:
|
|
202
|
+
//
|
|
203
|
+
// 1. Namespace: the whole `mcp__claude_ai_<Server>__` space counts, not just the
|
|
204
|
+
// known Gmail/Calendar/Drive trio. Connectors attach account-wide, so ANY
|
|
205
|
+
// other connector on the account (Slack, Atlassian, Figma, org-custom) shows
|
|
206
|
+
// up in a bridge session — and the connector path deliberately omits
|
|
207
|
+
// `tools: []` (see toolIsolationForQuery), so those tools are discoverable
|
|
208
|
+
// and callable. Keying on the trio meant e.g. a Slack send_message ran
|
|
209
|
+
// ungated inside claude, invisible to Pi's ConsentGate.
|
|
210
|
+
// 2. Verb: a tool on that space is a write UNLESS its tool segment BEGINS with a
|
|
211
|
+
// known read verb, so not-yet-known write tools (e.g. Gmail send_message,
|
|
212
|
+
// Drive delete_file, Calendar add_attendee) are blocked in a read-only
|
|
213
|
+
// session. A name under the connector prefix with no parseable `__<tool>`
|
|
214
|
+
// segment is also a write: it is a connector by construction, so deny wins.
|
|
215
|
+
// The verb is matched as a WORD across snake_case and camelCase, and a
|
|
216
|
+
// leading word that merely repeats the server name is skipped first
|
|
217
|
+
// (`Slack__slack_read_channel` reads as `read channel`), because connector
|
|
218
|
+
// servers name their tools differently from one another.
|
|
219
|
+
//
|
|
220
|
+
// Non-connector tools (Pi custom-tools, ToolSearch, MCP-resource tools, other MCP
|
|
221
|
+
// servers) are never connector writes → false. Used by connectorWriteDenyHook and
|
|
222
|
+
// by callers (e.g. the one-shot write executor) that enumerate live connector tools.
|
|
223
|
+
export function isConnectorWriteTool(name: string): boolean {
|
|
224
|
+
if (!name.startsWith(CONNECTOR_NS_PREFIX)) return false;
|
|
225
|
+
// First `__` after the prefix ends the server segment. First (not last) so a
|
|
226
|
+
// server name containing `__` leaves the extra segment in `tool`, which then
|
|
227
|
+
// fails the read-prefix test — ambiguity resolves to write.
|
|
228
|
+
const sep = name.indexOf("__", CONNECTOR_NS_PREFIX.length);
|
|
229
|
+
// No separator (sep < 0) OR an EMPTY server segment (sep at the prefix, e.g.
|
|
230
|
+
// `mcp__claude_ai___search_messages`) means the name doesn't parse as
|
|
231
|
+
// <server>__<tool> — it never earns the read-prefix exemption.
|
|
232
|
+
if (sep <= CONNECTOR_NS_PREFIX.length) return true;
|
|
233
|
+
const server = name.slice(CONNECTOR_NS_PREFIX.length, sep);
|
|
234
|
+
const words = connectorNameWords(name.slice(sep + "__".length));
|
|
235
|
+
// Skip a leading run of words that just repeats the server name, so a
|
|
236
|
+
// server-prefixed tool (`Slack__slack_read_channel`) is judged on its real
|
|
237
|
+
// verb. Only an exact leading match is skipped — an unrelated first word
|
|
238
|
+
// (`Weird__Server__list_things`) stays and fails the read test. A mutation
|
|
239
|
+
// word is never skipped, so a verb-shaped server name
|
|
240
|
+
// (`Delete__delete_get_thing`, `Sync__sync_get_status`) cannot launder its
|
|
241
|
+
// own tool's verb away.
|
|
242
|
+
const serverWords = connectorNameWords(server);
|
|
243
|
+
let skipped = 0;
|
|
244
|
+
while (
|
|
245
|
+
skipped < serverWords.length
|
|
246
|
+
&& words[skipped] === serverWords[skipped]
|
|
247
|
+
&& !CONNECTOR_MUTATION_WORDS.has(words[skipped])
|
|
248
|
+
) skipped++;
|
|
249
|
+
const rest = words.slice(skipped);
|
|
250
|
+
// Nothing parseable left (empty/punctuation-only tool segment, or a tool
|
|
251
|
+
// named exactly after its server) → write.
|
|
252
|
+
if (rest.length === 0) return true;
|
|
253
|
+
if (!CONNECTOR_READ_VERBS.has(rest[0])) return true;
|
|
254
|
+
// Begins as a read but also names a mutation → deny wins.
|
|
255
|
+
return rest.some((word) => CONNECTOR_MUTATION_WORDS.has(word));
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Connector write mode from the env override. `allow` exposes connector write
|
|
259
|
+
// tools; `deny` hides them. Returns undefined when unset so config can decide.
|
|
260
|
+
export function connectorWriteModeFromEnv(): ConnectorWriteMode | undefined {
|
|
261
|
+
const v = (process.env.CLAUDE_BRIDGE_CONNECTOR_WRITE ?? "").trim().toLowerCase();
|
|
262
|
+
if (v === "allow") return "allow";
|
|
263
|
+
if (v === "deny") return "deny";
|
|
264
|
+
return undefined;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Resolve the connector write mode: env wins over config, default `deny`
|
|
268
|
+
// (mirrors connectorsEnabledFor's env-first precedence). Only meaningful when
|
|
269
|
+
// connectors are enabled; connector chat sessions keep the default deny and the
|
|
270
|
+
// one-shot approved-write executor sets allow (env or config).
|
|
271
|
+
//
|
|
272
|
+
// FAIL CLOSED: writes are enabled ONLY by an explicit, validated `allow`. The
|
|
273
|
+
// config value is re-normalized here (defense in depth over normalizeProviderConfig)
|
|
274
|
+
// so a raw legacy-config value like "Deny"/"read-only"/true can never be treated
|
|
275
|
+
// as a truthy non-deny and silently open writes — anything but exact allow → deny.
|
|
276
|
+
export function connectorWriteModeFor(config?: Config): ConnectorWriteMode {
|
|
277
|
+
const resolved = connectorWriteModeFromEnv() ?? normalizeConnectorWriteMode(config?.provider?.connectorWriteMode);
|
|
278
|
+
return resolved === "allow" ? "allow" : "deny";
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// PreToolUse hook that hard-blocks connector WRITE tools at call time. Hooks run
|
|
282
|
+
// regardless of permissionMode (we use bypassPermissions), so this — not the
|
|
283
|
+
// static deny lists — is the real prefix-based runtime enforcement of
|
|
284
|
+
// isConnectorWriteTool. disallowedTools removes today's KNOWN writes from model
|
|
285
|
+
// context, but it lists exact ids on the known trio only, so a future write tool
|
|
286
|
+
// (e.g. mcp__claude_ai_Gmail__send_message, ..._Drive__delete_file) or any tool
|
|
287
|
+
// on another connector the account has attached (mcp__claude_ai_Slack__send_message)
|
|
288
|
+
// would otherwise be callable in a read-only session; this hook denies it by prefix.
|
|
289
|
+
export function connectorWriteDenyHook(): HookCallback {
|
|
290
|
+
return async (input) => {
|
|
291
|
+
// The CLI treats a hook error/timeout as an EMPTY hook output and lets
|
|
292
|
+
// the tool call proceed (fail OPEN) — so any exception in this body
|
|
293
|
+
// must convert to a deny, never an allow. Today's body is pure string
|
|
294
|
+
// checks on schema-validated input; the catch pins that invariant for
|
|
295
|
+
// whatever gets added here later.
|
|
296
|
+
try {
|
|
297
|
+
if (input.hook_event_name !== "PreToolUse") return { continue: true };
|
|
298
|
+
if (!isConnectorWriteTool(input.tool_name)) return { continue: true };
|
|
299
|
+
return connectorWriteDenyOutput(String(input.tool_name));
|
|
300
|
+
} catch {
|
|
301
|
+
const toolName = typeof (input as { tool_name?: unknown })?.tool_name === "string"
|
|
302
|
+
? (input as { tool_name: string }).tool_name
|
|
303
|
+
: "<unknown>";
|
|
304
|
+
return connectorWriteDenyOutput(toolName);
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function connectorWriteDenyOutput(toolName: string) {
|
|
310
|
+
return {
|
|
311
|
+
hookSpecificOutput: {
|
|
312
|
+
hookEventName: "PreToolUse" as const,
|
|
313
|
+
permissionDecision: "deny" as const,
|
|
314
|
+
permissionDecisionReason:
|
|
315
|
+
`Connector write tool "${toolName}" is blocked in read-only connector mode. ` +
|
|
316
|
+
`Connector writes must go through Memsira's gated approval flow.`,
|
|
317
|
+
},
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Connector query-option fragment: tool isolation (allow/deny lists) plus, when
|
|
322
|
+
// connectors are enabled and writes are denied, the runtime PreToolUse write
|
|
323
|
+
// hook. Spread into the SDK query options; continuation queries inherit it via
|
|
324
|
+
// `{ ...queryOptions }`. Exported so the wiring is unit-testable end to end.
|
|
325
|
+
export function connectorQueryOptions(connectorsEnabled: boolean, writeMode: ConnectorWriteMode = "deny"): Partial<Pick<NonNullable<Parameters<typeof query>[0]["options"]>, "tools" | "allowedTools" | "disallowedTools" | "hooks">> {
|
|
326
|
+
const isolation = toolIsolationForQuery(connectorsEnabled, writeMode);
|
|
327
|
+
// Only enforce (and only meaningful) when connectors are on and writes denied.
|
|
328
|
+
if (!connectorsEnabled || writeMode === "allow") return isolation;
|
|
329
|
+
return { ...isolation, hooks: { PreToolUse: [{ hooks: [connectorWriteDenyHook()] }] } };
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Tool isolation for a query. When connectors are enabled we still remove
|
|
333
|
+
// Claude Code's filesystem/shell built-ins (via disallowedTools; Pi owns those)
|
|
334
|
+
// and auto-allow the cloud connector tool namespaces so the model can call
|
|
335
|
+
// Gmail/Calendar/Drive.
|
|
336
|
+
//
|
|
337
|
+
// Critically, we must OMIT `tools: []` in the connector path: an empty --tools
|
|
338
|
+
// allowlist strips the claude.ai cloud MCP connector tools from the model's
|
|
339
|
+
// view (verified — Pi's SDK-injected custom-tools survive it, but connectors do
|
|
340
|
+
// not). Dropping `tools` leaves the connectors visible; disallowedTools still
|
|
341
|
+
// hard-denies the built-ins so Pi keeps ownership of file/shell/web tools.
|
|
342
|
+
export function toolIsolationForQuery(connectorsEnabled: boolean, writeMode: ConnectorWriteMode = "deny"): Partial<Pick<NonNullable<Parameters<typeof query>[0]["options"]>, "tools" | "allowedTools" | "disallowedTools">> {
|
|
343
|
+
if (!connectorsEnabled) return CLAUDE_BRIDGE_TOOL_ISOLATION;
|
|
344
|
+
// Keep ToolSearch + MCP-resource tools available so the model can discover the
|
|
345
|
+
// deferred cloud connector tools; still block file/shell/web built-ins.
|
|
346
|
+
const disallowedTools = DISALLOWED_BUILTIN_TOOLS.filter((t) => !CONNECTOR_DISCOVERY_TOOLS.includes(t));
|
|
347
|
+
// Deny connector WRITE tools unless writes are explicitly allowed (fail
|
|
348
|
+
// closed: any mode but exact "allow" is treated as read-only). This removes
|
|
349
|
+
// today's KNOWN writes from the model's context by exact id; deny rules take
|
|
350
|
+
// precedence over the CLAUDE_AI_CONNECTOR_TOOL_PATTERNS allow rules below, so
|
|
351
|
+
// reads stay available. Runtime enforcement covering future write tools and
|
|
352
|
+
// connector namespaces we don't enumerate here (Slack, Atlassian, org-custom)
|
|
353
|
+
// is done by connectorWriteDenyHook — see connectorQueryOptions.
|
|
354
|
+
if (writeMode !== "allow") disallowedTools.push(...CONNECTOR_WRITE_TOOLS);
|
|
355
|
+
return {
|
|
356
|
+
disallowedTools,
|
|
357
|
+
allowedTools: [...CLAUDE_BRIDGE_TOOL_ISOLATION.allowedTools, ...CLAUDE_AI_CONNECTOR_TOOL_PATTERNS],
|
|
358
|
+
};
|
|
359
|
+
}
|
package/src/debug.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { appendFileSync, chmodSync, mkdirSync } from "fs";
|
|
2
|
+
import { dirname, join } from "path";
|
|
3
|
+
import { piUserDir } from "./config.js";
|
|
4
|
+
|
|
5
|
+
// --- Debug logging ---
|
|
6
|
+
// CLAUDE_BRIDGE_DEBUG=1 enables debug logging to <piUserDir>/claude-bridge.log
|
|
7
|
+
// (~/.pi/agent/claude-bridge.log unless PI_CODING_AGENT_DIR points elsewhere).
|
|
8
|
+
|
|
9
|
+
export const DEBUG = process.env.CLAUDE_BRIDGE_DEBUG === "1";
|
|
10
|
+
export const DEBUG_LOG_PATH = process.env.CLAUDE_BRIDGE_DEBUG_PATH || join(piUserDir(), "claude-bridge.log");
|
|
11
|
+
|
|
12
|
+
export function diagLogPath(): string {
|
|
13
|
+
return process.env.CLAUDE_BRIDGE_DIAG_PATH || join(piUserDir(), "claude-bridge-diag.log");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Ensure log directories exist when debug is enabled
|
|
17
|
+
if (DEBUG) {
|
|
18
|
+
try {
|
|
19
|
+
mkdirSync(dirname(DEBUG_LOG_PATH), { recursive: true });
|
|
20
|
+
mkdirSync(dirname(diagLogPath()), { recursive: true, mode: 0o700 });
|
|
21
|
+
} catch {
|
|
22
|
+
// If directory creation fails, debug functions will throw on first use
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Unique per module evaluation — confirms whether subagents share module state
|
|
27
|
+
export const moduleInstanceId = Math.random().toString(36).slice(2, 8);
|
|
28
|
+
|
|
29
|
+
export function debug(...args: unknown[]) {
|
|
30
|
+
if (!DEBUG) return;
|
|
31
|
+
const ts = new Date().toISOString();
|
|
32
|
+
const fmt = (a: unknown): string => {
|
|
33
|
+
if (typeof a === "string") return a;
|
|
34
|
+
if (a instanceof Error) return `${a.name}: ${a.message}${a.stack ? "\n" + a.stack : ""}`;
|
|
35
|
+
return JSON.stringify(a);
|
|
36
|
+
};
|
|
37
|
+
const msg = args.map(fmt).join(" ");
|
|
38
|
+
try { appendFileSync(DEBUG_LOG_PATH, `[${ts}] [${moduleInstanceId}] ${msg}\n`); } catch { /* debug is best effort */ }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Per-query CLI debug capture. When CLAUDE_BRIDGE_DEBUG=1, ask the Claude Code
|
|
42
|
+
// CLI subprocess to write its own debug log to a file we choose, and also
|
|
43
|
+
// forward its stderr into our debug stream. Drops straight into the real SDK's
|
|
44
|
+
// Options — see @anthropic-ai/claude-agent-sdk sdk.d.ts:1245 (debug, debugFile,
|
|
45
|
+
// stderr). Without this, CC's internal view of the world is invisible to us
|
|
46
|
+
// and "No conversation found" / empty-error reports are unactionable.
|
|
47
|
+
let nextCliDebugSeq = 1;
|
|
48
|
+
export function makeCliDebugOptions(tag: string): { debug?: boolean; debugFile?: string; stderr?: (data: string) => void } {
|
|
49
|
+
if (!DEBUG) return {};
|
|
50
|
+
const seq = nextCliDebugSeq++;
|
|
51
|
+
const ts = new Date().toISOString().replace(/[:.]/g, "-");
|
|
52
|
+
const logDir = join(dirname(DEBUG_LOG_PATH), "cc-cli-logs");
|
|
53
|
+
try { mkdirSync(logDir, { recursive: true }); } catch { /* ignore */ }
|
|
54
|
+
const debugFile = join(logDir, `${ts}-${tag}-${seq}.log`);
|
|
55
|
+
debug(`cli-debug: ${tag} #${seq} → ${debugFile}`);
|
|
56
|
+
return {
|
|
57
|
+
debug: true,
|
|
58
|
+
debugFile,
|
|
59
|
+
stderr: (data: string) => {
|
|
60
|
+
for (const line of data.split(/\r?\n/)) {
|
|
61
|
+
if (line) debug(`[cli-stderr ${tag}#${seq}] ${line}`);
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Unconditional diagnostic dump — for "should never happen" paths */
|
|
68
|
+
export function diagDump(label: string, data: Record<string, unknown>) {
|
|
69
|
+
try {
|
|
70
|
+
const ts = new Date().toISOString();
|
|
71
|
+
const entry = { ts, moduleInstanceId, label, ...data };
|
|
72
|
+
const path = diagLogPath();
|
|
73
|
+
try { mkdirSync(dirname(path), { recursive: true, mode: 0o700 }); } catch { /* best effort */ }
|
|
74
|
+
appendFileSync(path, JSON.stringify(entry) + "\n", { mode: 0o600 });
|
|
75
|
+
try { chmodSync(path, 0o600); } catch { /* best effort */ }
|
|
76
|
+
debug(`DIAG: ${label} (see ${path})`);
|
|
77
|
+
} catch (error) {
|
|
78
|
+
debug(`DIAG FAILED: ${label}`, error);
|
|
79
|
+
}
|
|
80
|
+
}
|