@ai-sdk/harness-opencode 1.0.176 → 1.0.177
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/CHANGELOG.md +21 -0
- package/dist/bridge/host-tool-mcp.mjs.map +1 -1
- package/dist/bridge/index.mjs +279 -37
- package/dist/bridge/index.mjs.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +22 -7
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/bridge/index.ts +231 -33
- package/src/bridge/opencode-project-isolation.ts +71 -0
- package/src/bridge/permission-paths.ts +168 -0
- package/src/opencode-auth.ts +25 -7
- package/src/opencode-bridge-protocol.ts +1 -0
- package/src/opencode-harness.ts +6 -0
package/dist/bridge/index.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { appendFile, mkdir, writeFile } from "node:fs/promises";
|
|
2
|
-
import { existsSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
|
|
1
|
+
import { appendFile, lstat, mkdir, realpath, writeFile } from "node:fs/promises";
|
|
2
|
+
import { existsSync, globSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { randomBytes, randomUUID } from "node:crypto";
|
|
4
4
|
import { argv, env, pid, stdout } from "node:process";
|
|
5
5
|
import { WebSocketServer } from "ws";
|
|
6
|
+
import { homedir } from "node:os";
|
|
6
7
|
import path from "node:path";
|
|
7
8
|
import { isDeepStrictEqual } from "node:util";
|
|
8
9
|
import { createOpencodeClient, createOpencodeServer } from "@opencode-ai/sdk/v2";
|
|
@@ -157,6 +158,7 @@ async function runBridge(options) {
|
|
|
157
158
|
* between turns. A new `start` fences on this so turns never overlap.
|
|
158
159
|
*/
|
|
159
160
|
let activeTurn;
|
|
161
|
+
let lastStartId;
|
|
160
162
|
let debugConfig;
|
|
161
163
|
let consoleCaptureInstalled = false;
|
|
162
164
|
const envDebugEnabled = ENV_TRUTHY.has((env.HARNESS_DEBUG ?? "").toLowerCase());
|
|
@@ -300,6 +302,11 @@ async function runBridge(options) {
|
|
|
300
302
|
const handleInbound = async (msg, ws) => {
|
|
301
303
|
switch (msg.type) {
|
|
302
304
|
case "start": {
|
|
305
|
+
const startId = msg.startId;
|
|
306
|
+
if (startId != null) {
|
|
307
|
+
if (startId === lastStartId) return;
|
|
308
|
+
lastStartId = startId;
|
|
309
|
+
}
|
|
303
310
|
for (;;) {
|
|
304
311
|
const pendingTurn = activeTurn;
|
|
305
312
|
if (pendingTurn == null) break;
|
|
@@ -517,7 +524,10 @@ async function runBridge(options) {
|
|
|
517
524
|
type: "bridge-hello",
|
|
518
525
|
state: currentTurnState,
|
|
519
526
|
lastSeq: seqCounter,
|
|
520
|
-
capabilities: {
|
|
527
|
+
capabilities: {
|
|
528
|
+
idempotentStart: true,
|
|
529
|
+
experimental_userMessageResponses: true
|
|
530
|
+
}
|
|
521
531
|
});
|
|
522
532
|
ws.on("message", (raw) => {
|
|
523
533
|
let parsed;
|
|
@@ -1573,6 +1583,30 @@ function configureOpenCodePath({ bootstrapDir, env }) {
|
|
|
1573
1583
|
].join(path.delimiter);
|
|
1574
1584
|
}
|
|
1575
1585
|
//#endregion
|
|
1586
|
+
//#region src/bridge/opencode-project-isolation.ts
|
|
1587
|
+
const GLOB_CHARACTERS = /[*?[\]{}]/;
|
|
1588
|
+
function resolveOpenCodeInstructions({ workdir, hostInstructions }) {
|
|
1589
|
+
if (hostInstructions !== void 0 && !(Array.isArray(hostInstructions) && hostInstructions.every((entry) => typeof entry === "string"))) return;
|
|
1590
|
+
const directory = resolveRealPath(workdir);
|
|
1591
|
+
return [path.join(directory, "AGENTS.md"), ...(hostInstructions ?? []).flatMap((entry) => {
|
|
1592
|
+
if (entry.startsWith("http://") || entry.startsWith("https://") || entry.startsWith("~/") || path.isAbsolute(entry)) return [entry];
|
|
1593
|
+
if (!GLOB_CHARACTERS.test(path.dirname(entry))) return [path.join(directory, entry)];
|
|
1594
|
+
return globSync(entry, {
|
|
1595
|
+
cwd: directory,
|
|
1596
|
+
withFileTypes: true
|
|
1597
|
+
}).filter((match) => match.isFile()).map((match) => path.join(match.parentPath, match.name)).sort();
|
|
1598
|
+
})];
|
|
1599
|
+
}
|
|
1600
|
+
function resolveRealPath(file) {
|
|
1601
|
+
const resolved = path.resolve(file);
|
|
1602
|
+
try {
|
|
1603
|
+
return realpathSync(resolved);
|
|
1604
|
+
} catch (error) {
|
|
1605
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT") return resolved;
|
|
1606
|
+
throw error;
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
//#endregion
|
|
1576
1610
|
//#region src/bridge/opencode-server-auth.ts
|
|
1577
1611
|
function configureOpenCodeServerAuth({ env }) {
|
|
1578
1612
|
const username = env.OPENCODE_SERVER_USERNAME ?? "opencode";
|
|
@@ -1799,6 +1833,94 @@ function toOpenCodePromptParts(prompt) {
|
|
|
1799
1833
|
});
|
|
1800
1834
|
}
|
|
1801
1835
|
//#endregion
|
|
1836
|
+
//#region src/bridge/permission-paths.ts
|
|
1837
|
+
async function areOpenCodePermissionPathsTrusted(options) {
|
|
1838
|
+
const args = options.toolCall?.input;
|
|
1839
|
+
const nativeName = options.toolCall?.nativeName;
|
|
1840
|
+
const access = ["edit", "write"].includes(options.action) || [
|
|
1841
|
+
"write",
|
|
1842
|
+
"edit",
|
|
1843
|
+
"apply_patch"
|
|
1844
|
+
].includes(nativeName ?? "") ? "write" : "read";
|
|
1845
|
+
let targets;
|
|
1846
|
+
if (Array.isArray(options.metadata?.files)) {
|
|
1847
|
+
if (options.metadata.files.length === 0) return false;
|
|
1848
|
+
targets = options.metadata.files.flatMap((value) => {
|
|
1849
|
+
if (value == null || typeof value !== "object") return [void 0];
|
|
1850
|
+
const file = value;
|
|
1851
|
+
return file.type === "move" || file.movePath != null ? [file.filePath, file.movePath] : [file.filePath];
|
|
1852
|
+
});
|
|
1853
|
+
} else if (args && [
|
|
1854
|
+
"read",
|
|
1855
|
+
"view",
|
|
1856
|
+
"write",
|
|
1857
|
+
"edit"
|
|
1858
|
+
].includes(nativeName ?? "")) targets = [args.filePath];
|
|
1859
|
+
else if (args && [
|
|
1860
|
+
"glob",
|
|
1861
|
+
"grep",
|
|
1862
|
+
"list",
|
|
1863
|
+
"ls"
|
|
1864
|
+
].includes(nativeName ?? "")) targets = [args.path ?? options.workDir];
|
|
1865
|
+
else if (typeof options.metadata?.filepath === "string" && options.action !== "edit") targets = [options.metadata.filepath];
|
|
1866
|
+
else if (options.action === "edit" && typeof options.metadata?.filepath === "string" && path.posix.isAbsolute(options.metadata.filepath)) targets = [options.metadata.filepath];
|
|
1867
|
+
else if (options.action === "read" && options.resources.length > 0 && options.resources.every((value) => path.posix.isAbsolute(value))) targets = options.resources;
|
|
1868
|
+
else return false;
|
|
1869
|
+
if (!targets.every((value) => typeof value === "string")) return false;
|
|
1870
|
+
return areFileToolPathsTrusted({
|
|
1871
|
+
workDir: options.workDir,
|
|
1872
|
+
cwd: options.workDir,
|
|
1873
|
+
paths: targets,
|
|
1874
|
+
access,
|
|
1875
|
+
readableSkillRoots: options.readableSkillRoots
|
|
1876
|
+
});
|
|
1877
|
+
}
|
|
1878
|
+
async function areFileToolPathsTrusted(options) {
|
|
1879
|
+
if (options.paths.length === 0) return false;
|
|
1880
|
+
try {
|
|
1881
|
+
const roots = [await realpath(options.workDir)];
|
|
1882
|
+
if (options.access === "read") for (const root of options.readableSkillRoots) try {
|
|
1883
|
+
roots.push(await realpath(root));
|
|
1884
|
+
} catch {
|
|
1885
|
+
continue;
|
|
1886
|
+
}
|
|
1887
|
+
for (const filePath of options.paths) {
|
|
1888
|
+
if (typeof filePath !== "string" || filePath.trim().length === 0) return false;
|
|
1889
|
+
if (!path.posix.isAbsolute(filePath) && !path.posix.isAbsolute(options.cwd)) return false;
|
|
1890
|
+
const absolutePath = path.posix.isAbsolute(filePath) ? filePath : `${options.cwd}/${filePath}`;
|
|
1891
|
+
for (const candidatePath of [absolutePath, path.posix.normalize(absolutePath)]) {
|
|
1892
|
+
const candidate = await resolvePermissionPath({ filePath: candidatePath });
|
|
1893
|
+
if (!roots.some((root) => {
|
|
1894
|
+
const relative = path.posix.relative(root, candidate);
|
|
1895
|
+
return relative === "" || relative !== ".." && !relative.startsWith("../") && !path.posix.isAbsolute(relative);
|
|
1896
|
+
})) return false;
|
|
1897
|
+
}
|
|
1898
|
+
}
|
|
1899
|
+
return true;
|
|
1900
|
+
} catch {
|
|
1901
|
+
return false;
|
|
1902
|
+
}
|
|
1903
|
+
}
|
|
1904
|
+
async function resolvePermissionPath(options) {
|
|
1905
|
+
let existingPath = options.filePath;
|
|
1906
|
+
const missing = [];
|
|
1907
|
+
for (;;) {
|
|
1908
|
+
let exists = true;
|
|
1909
|
+
try {
|
|
1910
|
+
await lstat(existingPath);
|
|
1911
|
+
} catch (error) {
|
|
1912
|
+
if (error == null || typeof error !== "object" || !("code" in error) || error.code !== "ENOENT") throw error;
|
|
1913
|
+
exists = false;
|
|
1914
|
+
}
|
|
1915
|
+
if (exists) return path.posix.join(await realpath(existingPath), ...missing);
|
|
1916
|
+
const parent = path.posix.dirname(existingPath);
|
|
1917
|
+
const segment = path.posix.basename(existingPath);
|
|
1918
|
+
if (parent === existingPath || segment === "." || segment === "..") throw new Error("Unable to resolve permission path.");
|
|
1919
|
+
missing.unshift(segment);
|
|
1920
|
+
existingPath = parent;
|
|
1921
|
+
}
|
|
1922
|
+
}
|
|
1923
|
+
//#endregion
|
|
1802
1924
|
//#region src/bridge/index.ts
|
|
1803
1925
|
const NATIVE_TO_COMMON = {
|
|
1804
1926
|
view: "read",
|
|
@@ -1863,6 +1985,7 @@ configureOpenCodePath({
|
|
|
1863
1985
|
bootstrapDir,
|
|
1864
1986
|
env
|
|
1865
1987
|
});
|
|
1988
|
+
env.OPENCODE_DISABLE_PROJECT_CONFIG = "1";
|
|
1866
1989
|
await runBridge({
|
|
1867
1990
|
bridgeType: "opencode",
|
|
1868
1991
|
bridgeStateDir,
|
|
@@ -1934,6 +2057,8 @@ async function ensureRuntime({ start }) {
|
|
|
1934
2057
|
if (runtime.client && isDeepStrictEqual(runtime.openCodeConfig, start.openCodeConfig) && isDeepStrictEqual(runtime.inactiveBuiltinToolNames, start.inactiveBuiltinToolNames)) return;
|
|
1935
2058
|
closeRuntime();
|
|
1936
2059
|
try {
|
|
2060
|
+
const homeDirectory = homedir();
|
|
2061
|
+
for (const protectedPath of [homeDirectory, path.join(env.XDG_CONFIG_HOME || path.join(homeDirectory, ".config"), "opencode")]) if (isPathInsideOrEqual(protectedPath, workdir)) throw new Error(`OpenCode session working directory "${workdir}" must not be or contain the sandbox home directory or OpenCode's global config directory, because OpenCode always loads configuration from those locations.`);
|
|
1937
2062
|
if (start.tools && start.tools.length > 0) {
|
|
1938
2063
|
runtime.toolNames = new Set(start.tools.map((tool) => tool.name));
|
|
1939
2064
|
runtime.relay = await startToolRelay({ tools: start.tools });
|
|
@@ -1982,10 +2107,10 @@ function buildOpenCodeConfig({ start, relayPort, toolSchemasPath }) {
|
|
|
1982
2107
|
share: "disabled",
|
|
1983
2108
|
autoupdate: false,
|
|
1984
2109
|
permission: {
|
|
1985
|
-
read: "allow",
|
|
1986
|
-
glob: "allow",
|
|
1987
|
-
grep: "allow",
|
|
1988
|
-
list: "allow",
|
|
2110
|
+
read: start.permissionMode && start.permissionMode !== "allow-all" ? "ask" : "allow",
|
|
2111
|
+
glob: start.permissionMode && start.permissionMode !== "allow-all" ? "ask" : "allow",
|
|
2112
|
+
grep: start.permissionMode && start.permissionMode !== "allow-all" ? "ask" : "allow",
|
|
2113
|
+
list: start.permissionMode && start.permissionMode !== "allow-all" ? "ask" : "allow",
|
|
1989
2114
|
edit: "ask",
|
|
1990
2115
|
bash: "ask",
|
|
1991
2116
|
external_directory: "ask",
|
|
@@ -1995,6 +2120,11 @@ function buildOpenCodeConfig({ start, relayPort, toolSchemasPath }) {
|
|
|
1995
2120
|
question: "allow"
|
|
1996
2121
|
}
|
|
1997
2122
|
};
|
|
2123
|
+
const instructions = resolveOpenCodeInstructions({
|
|
2124
|
+
workdir,
|
|
2125
|
+
hostInstructions: start.openCodeConfig?.instructions
|
|
2126
|
+
});
|
|
2127
|
+
if (instructions) config.instructions = instructions;
|
|
1998
2128
|
if (start.model) config.model = start.model;
|
|
1999
2129
|
if (skillsDir) config.skills = { paths: [skillsDir] };
|
|
2000
2130
|
const permission = config.permission;
|
|
@@ -2236,6 +2366,7 @@ async function runPrompt({ client, sessionId, start, turn, emit }) {
|
|
|
2236
2366
|
const eventLoop = consumeEvents({
|
|
2237
2367
|
client,
|
|
2238
2368
|
sessionId,
|
|
2369
|
+
readableSkillRoots: start.readableSkillRoots,
|
|
2239
2370
|
permissionMode: start.permissionMode,
|
|
2240
2371
|
inactiveBuiltinToolNames: start.inactiveBuiltinToolNames,
|
|
2241
2372
|
turn,
|
|
@@ -2420,6 +2551,7 @@ async function runCompaction({ client, sessionId, start, turn, emit }) {
|
|
|
2420
2551
|
const eventLoop = consumeEvents({
|
|
2421
2552
|
client,
|
|
2422
2553
|
sessionId,
|
|
2554
|
+
readableSkillRoots: start.readableSkillRoots,
|
|
2423
2555
|
permissionMode: start.permissionMode,
|
|
2424
2556
|
inactiveBuiltinToolNames: start.inactiveBuiltinToolNames,
|
|
2425
2557
|
turn,
|
|
@@ -2470,7 +2602,7 @@ async function runCompaction({ client, sessionId, start, turn, emit }) {
|
|
|
2470
2602
|
harnessMetadata: { opencode: { missingSummary: true } }
|
|
2471
2603
|
});
|
|
2472
2604
|
}
|
|
2473
|
-
async function consumeEvents({ client, sessionId, permissionMode, inactiveBuiltinToolNames, turn, state, hostInputTracker, emit, signal, onSubscribed, onEvent }) {
|
|
2605
|
+
async function consumeEvents({ client, sessionId, permissionMode, readableSkillRoots = [], inactiveBuiltinToolNames, turn, state, hostInputTracker, emit, signal, onSubscribed, onEvent }) {
|
|
2474
2606
|
const stream = await subscribeLegacyEvents({
|
|
2475
2607
|
client,
|
|
2476
2608
|
signal
|
|
@@ -2478,6 +2610,55 @@ async function consumeEvents({ client, sessionId, permissionMode, inactiveBuilti
|
|
|
2478
2610
|
onSubscribed?.();
|
|
2479
2611
|
if (!stream) return;
|
|
2480
2612
|
const taskSessionIds = /* @__PURE__ */ new Set([sessionId]);
|
|
2613
|
+
const observedCalls = /* @__PURE__ */ new Map();
|
|
2614
|
+
const permissionContext = {
|
|
2615
|
+
readableSkillRoots,
|
|
2616
|
+
decisions: /* @__PURE__ */ new Map(),
|
|
2617
|
+
getToolCall: async ({ event, sessionId: sourceSessionId }) => {
|
|
2618
|
+
const props = event.properties ?? {};
|
|
2619
|
+
const source = asOpenCodeObject(props.tool) ?? asOpenCodeObject(props.source);
|
|
2620
|
+
const callID = stringValue(source?.callID);
|
|
2621
|
+
if (!callID) return void 0;
|
|
2622
|
+
const key = JSON.stringify([sourceSessionId, callID]);
|
|
2623
|
+
const observed = observedCalls.get(key);
|
|
2624
|
+
if (observed) return observed;
|
|
2625
|
+
const messageID = stringValue(source?.messageID);
|
|
2626
|
+
if (messageID && typeof client.session.message === "function") try {
|
|
2627
|
+
const parts = asOpenCodeObject((await client.session.message({
|
|
2628
|
+
sessionID: sourceSessionId,
|
|
2629
|
+
messageID,
|
|
2630
|
+
directory: workdir
|
|
2631
|
+
})).data)?.parts;
|
|
2632
|
+
if (Array.isArray(parts)) {
|
|
2633
|
+
const snapshot = permissionToolCallFromPart(parts.map(asOpenCodeObject).find((value) => value?.callID === callID));
|
|
2634
|
+
if (snapshot) {
|
|
2635
|
+
observedCalls.set(key, snapshot);
|
|
2636
|
+
return snapshot;
|
|
2637
|
+
}
|
|
2638
|
+
}
|
|
2639
|
+
} catch {
|
|
2640
|
+
return observedCalls.get(key);
|
|
2641
|
+
}
|
|
2642
|
+
return observedCalls.get(key);
|
|
2643
|
+
},
|
|
2644
|
+
emitToolCall: ({ toolCallId, toolCall }) => {
|
|
2645
|
+
if (!toolCall || sentToolCallIds.has(toolCallId)) return;
|
|
2646
|
+
const nativeName = toolCall.nativeName;
|
|
2647
|
+
const toolName = toWireToolName(nativeName);
|
|
2648
|
+
state.toolCallsEmitted.add(toolCallId);
|
|
2649
|
+
emitToHost({
|
|
2650
|
+
type: "tool-call",
|
|
2651
|
+
toolCallId,
|
|
2652
|
+
toolName,
|
|
2653
|
+
input: JSON.stringify(toolCall.input),
|
|
2654
|
+
providerExecuted: true,
|
|
2655
|
+
...nativeNameField({
|
|
2656
|
+
nativeName,
|
|
2657
|
+
toolName
|
|
2658
|
+
})
|
|
2659
|
+
});
|
|
2660
|
+
}
|
|
2661
|
+
};
|
|
2481
2662
|
const registerSubagentSession = (sourceSessionId) => function register({ parentSessionId, sessionId: subagentSessionId }) {
|
|
2482
2663
|
if (parentSessionId === sourceSessionId && taskSessionIds.has(sourceSessionId)) taskSessionIds.add(subagentSessionId);
|
|
2483
2664
|
};
|
|
@@ -2630,6 +2811,19 @@ async function consumeEvents({ client, sessionId, permissionMode, inactiveBuilti
|
|
|
2630
2811
|
const scopedSessionId = !eventSessionId || eventSessionId === sessionId ? sessionId : taskSessionIds.has(eventSessionId) ? eventSessionId : void 0;
|
|
2631
2812
|
if (!scopedSessionId) return false;
|
|
2632
2813
|
const isDescendant = scopedSessionId !== sessionId;
|
|
2814
|
+
const part = asOpenCodeObject(event.properties?.part);
|
|
2815
|
+
const snapshot = permissionToolCallFromPart(part);
|
|
2816
|
+
if (snapshot && typeof part?.callID === "string") observedCalls.set(JSON.stringify([scopedSessionId, part.callID]), snapshot);
|
|
2817
|
+
if (event.type === "session.next.tool.started") {
|
|
2818
|
+
const props = event.properties ?? {};
|
|
2819
|
+
const input = asOpenCodeObject(props.input);
|
|
2820
|
+
const nativeName = stringValue(props.toolName ?? props.tool);
|
|
2821
|
+
const callID = stringValue(props.callID);
|
|
2822
|
+
if (input && nativeName && callID) observedCalls.set(JSON.stringify([scopedSessionId, callID]), {
|
|
2823
|
+
nativeName,
|
|
2824
|
+
input
|
|
2825
|
+
});
|
|
2826
|
+
}
|
|
2633
2827
|
const waitFor = ({ toolCallId, settlesToolCall }) => hostInputWait({
|
|
2634
2828
|
toolCallId: isDescendant ? `${scopedSessionId}:${toolCallId}` : toolCallId,
|
|
2635
2829
|
isDescendant,
|
|
@@ -2645,6 +2839,7 @@ async function consumeEvents({ client, sessionId, permissionMode, inactiveBuilti
|
|
|
2645
2839
|
else if (event.type === "permission.v2.asked") runHandler(handlePermissionV2({
|
|
2646
2840
|
client,
|
|
2647
2841
|
sessionId: scopedSessionId,
|
|
2842
|
+
context: permissionContext,
|
|
2648
2843
|
permissionMode,
|
|
2649
2844
|
inactiveBuiltinToolNames,
|
|
2650
2845
|
turn,
|
|
@@ -2655,6 +2850,7 @@ async function consumeEvents({ client, sessionId, permissionMode, inactiveBuilti
|
|
|
2655
2850
|
else if (event.type === "permission.asked") runHandler(handlePermission({
|
|
2656
2851
|
client,
|
|
2657
2852
|
sessionId: scopedSessionId,
|
|
2853
|
+
context: permissionContext,
|
|
2658
2854
|
permissionMode,
|
|
2659
2855
|
inactiveBuiltinToolNames,
|
|
2660
2856
|
turn,
|
|
@@ -2719,11 +2915,18 @@ async function handleQuestion({ client, turn, emit, event, waitFor }) {
|
|
|
2719
2915
|
function sanitizeMcpToolName(value) {
|
|
2720
2916
|
return value.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
2721
2917
|
}
|
|
2722
|
-
async function handlePermissionV2({ client, sessionId, permissionMode, inactiveBuiltinToolNames, turn, emit, event, waitFor }) {
|
|
2918
|
+
async function handlePermissionV2({ client, sessionId, context, permissionMode, inactiveBuiltinToolNames, turn, emit, event, waitFor }) {
|
|
2723
2919
|
const props = event.properties ?? {};
|
|
2724
2920
|
const requestID = String(props.id ?? "");
|
|
2725
2921
|
if (!requestID) return;
|
|
2726
2922
|
const reply = await selectPermissionReply({
|
|
2923
|
+
context,
|
|
2924
|
+
sessionId,
|
|
2925
|
+
toolCall: await context.getToolCall({
|
|
2926
|
+
event,
|
|
2927
|
+
sessionId
|
|
2928
|
+
}),
|
|
2929
|
+
metadata: asOpenCodeObject(props.metadata),
|
|
2727
2930
|
action: String(props.action ?? ""),
|
|
2728
2931
|
resources: Array.isArray(props.resources) ? props.resources.map(String) : [],
|
|
2729
2932
|
requestID,
|
|
@@ -2741,12 +2944,19 @@ async function handlePermissionV2({ client, sessionId, permissionMode, inactiveB
|
|
|
2741
2944
|
...reply.message ? { message: reply.message } : {}
|
|
2742
2945
|
});
|
|
2743
2946
|
}
|
|
2744
|
-
async function handlePermission({ client, sessionId, permissionMode, inactiveBuiltinToolNames, turn, emit, event, waitFor }) {
|
|
2947
|
+
async function handlePermission({ client, sessionId, context, permissionMode, inactiveBuiltinToolNames, turn, emit, event, waitFor }) {
|
|
2745
2948
|
const props = event.properties ?? {};
|
|
2746
2949
|
const requestID = String(props.id ?? "");
|
|
2747
2950
|
if (!requestID) return;
|
|
2748
2951
|
const tool = asOpenCodeObject(props.tool);
|
|
2749
2952
|
const reply = await selectPermissionReply({
|
|
2953
|
+
context,
|
|
2954
|
+
sessionId,
|
|
2955
|
+
toolCall: await context.getToolCall({
|
|
2956
|
+
event,
|
|
2957
|
+
sessionId
|
|
2958
|
+
}),
|
|
2959
|
+
metadata: asOpenCodeObject(props.metadata),
|
|
2750
2960
|
action: String(props.permission ?? ""),
|
|
2751
2961
|
resources: Array.isArray(props.patterns) ? props.patterns.map(String) : [],
|
|
2752
2962
|
requestID,
|
|
@@ -2764,25 +2974,40 @@ async function handlePermission({ client, sessionId, permissionMode, inactiveBui
|
|
|
2764
2974
|
...reply.message ? { message: reply.message } : {}
|
|
2765
2975
|
});
|
|
2766
2976
|
}
|
|
2767
|
-
async function selectPermissionReply({ action, resources, requestID, toolCallId, permissionMode, inactiveBuiltinToolNames, turn, emit, waitFor }) {
|
|
2768
|
-
const requestApproval =
|
|
2769
|
-
const
|
|
2977
|
+
async function selectPermissionReply({ context, sessionId, toolCall, metadata, action, resources, requestID, toolCallId, permissionMode, inactiveBuiltinToolNames, turn, emit, waitFor }) {
|
|
2978
|
+
const requestApproval = () => {
|
|
2979
|
+
const key = toolCall ? JSON.stringify([
|
|
2980
|
+
sessionId,
|
|
2770
2981
|
toolCallId,
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2982
|
+
toolCall
|
|
2983
|
+
]) : void 0;
|
|
2984
|
+
const existing = key ? context.decisions.get(key) : void 0;
|
|
2985
|
+
if (existing) return existing;
|
|
2986
|
+
const decision = (async () => {
|
|
2987
|
+
context.emitToolCall({
|
|
2988
|
+
toolCallId,
|
|
2989
|
+
toolCall
|
|
2990
|
+
});
|
|
2991
|
+
await emit({
|
|
2992
|
+
type: "tool-approval-request",
|
|
2993
|
+
approvalId: requestID,
|
|
2994
|
+
toolCallId
|
|
2995
|
+
});
|
|
2996
|
+
const wait = waitFor?.({
|
|
2997
|
+
toolCallId,
|
|
2998
|
+
settlesToolCall: false
|
|
2999
|
+
});
|
|
3000
|
+
wait?.markWaiting();
|
|
3001
|
+
return turn.requestToolApproval(requestID).finally(() => wait?.clearWaiting());
|
|
3002
|
+
})();
|
|
3003
|
+
if (key) context.decisions.set(key, decision);
|
|
3004
|
+
return decision;
|
|
2775
3005
|
};
|
|
2776
3006
|
const toolName = toPermissionToolName(action);
|
|
2777
3007
|
if (isBuiltinToolInactive({
|
|
2778
3008
|
toolName,
|
|
2779
3009
|
inactiveBuiltinToolNames
|
|
2780
3010
|
})) {
|
|
2781
|
-
await emit({
|
|
2782
|
-
type: "tool-approval-request",
|
|
2783
|
-
approvalId: requestID,
|
|
2784
|
-
toolCallId
|
|
2785
|
-
});
|
|
2786
3011
|
const decision = await requestApproval();
|
|
2787
3012
|
return decision.approved ? { reply: "once" } : {
|
|
2788
3013
|
reply: "reject",
|
|
@@ -2790,17 +3015,24 @@ async function selectPermissionReply({ action, resources, requestID, toolCallId,
|
|
|
2790
3015
|
};
|
|
2791
3016
|
}
|
|
2792
3017
|
if (!permissionMode || permissionMode === "allow-all") return { reply: "always" };
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
3018
|
+
const nativeName = toolCall?.nativeName;
|
|
3019
|
+
const filePermission = action === "external_directory" || [
|
|
3020
|
+
"read",
|
|
3021
|
+
"write",
|
|
3022
|
+
"edit",
|
|
3023
|
+
"glob",
|
|
3024
|
+
"grep",
|
|
3025
|
+
"ls"
|
|
3026
|
+
].includes(toolName);
|
|
3027
|
+
const kind = action === "external_directory" && nativeName ? nativeName === "apply_patch" ? "edit" : TOOL_KIND[toWireToolName(nativeName)] ?? "bash" : TOOL_KIND[toolName] ?? "bash";
|
|
3028
|
+
if ((permissionMode === "allow-edits" ? kind === "readonly" || kind === "edit" : kind === "readonly") && (!filePermission || await areOpenCodePermissionPathsTrusted({
|
|
3029
|
+
action,
|
|
3030
|
+
resources,
|
|
3031
|
+
metadata,
|
|
3032
|
+
toolCall,
|
|
3033
|
+
workDir: workdir,
|
|
3034
|
+
readableSkillRoots: context.readableSkillRoots
|
|
3035
|
+
}))) return { reply: "once" };
|
|
2804
3036
|
const decision = await requestApproval();
|
|
2805
3037
|
return decision.approved ? { reply: "once" } : {
|
|
2806
3038
|
reply: "reject",
|
|
@@ -2823,10 +3055,6 @@ function toPermissionToolName(action) {
|
|
|
2823
3055
|
function isBuiltinToolInactive(input) {
|
|
2824
3056
|
return input.inactiveBuiltinToolNames?.some((toolName) => toolName === input.toolName) ?? false;
|
|
2825
3057
|
}
|
|
2826
|
-
function isExternalPath(resource) {
|
|
2827
|
-
if (!path.isAbsolute(resource)) return false;
|
|
2828
|
-
return !isPathInsideOrEqual(resource, workdir) && (!skillsDir || !isPathInsideOrEqual(resource, skillsDir));
|
|
2829
|
-
}
|
|
2830
3058
|
function isPathInsideOrEqual(file, root) {
|
|
2831
3059
|
const relative = path.relative(canonicalizeForContainment(root), canonicalizeForContainment(file));
|
|
2832
3060
|
return relative === "" || relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative);
|
|
@@ -2840,6 +3068,20 @@ function canonicalizeForContainment(inputPath) {
|
|
|
2840
3068
|
return parent === normalized ? normalized : path.join(canonicalizeForContainment(parent), path.basename(normalized));
|
|
2841
3069
|
}
|
|
2842
3070
|
}
|
|
3071
|
+
function permissionToolCallFromPart(value) {
|
|
3072
|
+
const part = asOpenCodeObject(value);
|
|
3073
|
+
const state = asOpenCodeObject(part?.state);
|
|
3074
|
+
const input = asOpenCodeObject(state?.input);
|
|
3075
|
+
if (part?.type !== "tool" || typeof part.tool !== "string" || !input || ![
|
|
3076
|
+
"running",
|
|
3077
|
+
"completed",
|
|
3078
|
+
"error"
|
|
3079
|
+
].includes(String(state?.status))) return void 0;
|
|
3080
|
+
return {
|
|
3081
|
+
nativeName: part.tool,
|
|
3082
|
+
input
|
|
3083
|
+
};
|
|
3084
|
+
}
|
|
2843
3085
|
function toWireToolName(nativeName) {
|
|
2844
3086
|
return NATIVE_TO_COMMON[nativeName] ?? OPENCODE_TO_WIRE[nativeName] ?? nativeName;
|
|
2845
3087
|
}
|