@ai-sdk/harness-opencode 1.0.152 → 1.0.154

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.
@@ -8,12 +8,19 @@ import { mkdirSync, realpathSync, writeFileSync } from 'node:fs';
8
8
  import path from 'node:path';
9
9
  import { argv, env as procEnv } from 'node:process';
10
10
  import { isDeepStrictEqual } from 'node:util';
11
- import type { StartMessage } from '../opencode-bridge-protocol';
11
+ import type {
12
+ OpenCodeBuiltinToolName,
13
+ StartMessage,
14
+ } from '../opencode-bridge-protocol';
12
15
 
13
16
  import {
14
17
  createOpencodeClient,
15
18
  createOpencodeServer,
16
19
  } from '@opencode-ai/sdk/v2';
20
+ import {
21
+ createOpenCodeHostInputTracker,
22
+ type OpenCodeHostInputTracker,
23
+ } from './opencode-host-input-tracker';
17
24
  import {
18
25
  createTranslationState,
19
26
  emitOpenCodeStreamStart,
@@ -29,7 +36,7 @@ import {
29
36
  } from './opencode-context-fallback';
30
37
  import { createEmitStreamEvent, stringValue } from './create-emit-stream-event';
31
38
  import { mapOpenCodeFinishReason } from './opencode-finish-step';
32
- import { prependOpenCodeBinToPath } from './opencode-path';
39
+ import { configureOpenCodePath } from './opencode-path';
33
40
  import { configureOpenCodeServerAuth } from './opencode-server-auth';
34
41
  import {
35
42
  addUsage,
@@ -64,6 +71,7 @@ type RuntimeState = {
64
71
  sessionId?: string;
65
72
  relay?: ToolRelay;
66
73
  openCodeConfig?: Record<string, unknown>;
74
+ inactiveBuiltinToolNames?: StartMessage['inactiveBuiltinToolNames'];
67
75
  toolNames: Set<string>;
68
76
  mcpToolPrefixes: Set<string>;
69
77
  };
@@ -98,9 +106,23 @@ const OPENCODE_TO_WIRE: Readonly<Record<string, string>> = {
98
106
  subtask: 'agent',
99
107
  };
100
108
 
101
- const PUBLIC_TO_NATIVE: Readonly<Record<string, string>> = {
102
- read: 'view',
103
- write: 'write',
109
+ type OpenCodePermissionName =
110
+ | 'question'
111
+ | 'read'
112
+ | 'edit'
113
+ | 'bash'
114
+ | 'glob'
115
+ | 'grep'
116
+ | 'list'
117
+ | 'webfetch'
118
+ | 'skill'
119
+ | 'todowrite'
120
+ | 'task';
121
+
122
+ const BUILTIN_TOOL_PERMISSION = {
123
+ askUserQuestions: 'question',
124
+ read: 'read',
125
+ write: 'edit',
104
126
  edit: 'edit',
105
127
  bash: 'bash',
106
128
  glob: 'glob',
@@ -109,8 +131,8 @@ const PUBLIC_TO_NATIVE: Readonly<Record<string, string>> = {
109
131
  webfetch: 'webfetch',
110
132
  skill: 'skill',
111
133
  todowrite: 'todowrite',
112
- agent: 'agent',
113
- };
134
+ agent: 'task',
135
+ } as const satisfies Record<OpenCodeBuiltinToolName, OpenCodePermissionName>;
114
136
 
115
137
  const TOOL_KIND: Readonly<Record<string, 'readonly' | 'edit' | 'bash'>> = {
116
138
  read: 'readonly',
@@ -137,7 +159,13 @@ const runtime: RuntimeState = {
137
159
  toolNames: new Set(),
138
160
  mcpToolPrefixes: new Set(),
139
161
  };
140
- prependOpenCodeBinToPath({ bootstrapDir, env: procEnv });
162
+ /*
163
+ * The tool relay outlives a single turn, so it reports host tool waits to the
164
+ * tracker of the turn that is currently running.
165
+ */
166
+ let currentHostInputTracker: OpenCodeHostInputTracker | undefined;
167
+ let currentTurn: BridgeTurn | undefined;
168
+ configureOpenCodePath({ bootstrapDir, env: procEnv });
141
169
 
142
170
  await runBridge<StartMessage>({
143
171
  bridgeType: 'opencode',
@@ -148,10 +176,11 @@ await runBridge<StartMessage>({
148
176
  });
149
177
 
150
178
  async function runTurn(start: StartMessage, turn: BridgeTurn): Promise<void> {
179
+ currentTurn = turn;
151
180
  const emit: Emit = msg => turn.emit(msg as BridgeEvent);
152
181
  let totalUsage: HarnessUsage | undefined;
153
182
  try {
154
- await ensureRuntime({ start, turn, emit });
183
+ await ensureRuntime({ start });
155
184
  const client = runtime.client!;
156
185
  if (start.skillsChanged) {
157
186
  await client.instance.dispose({ directory: workdir });
@@ -199,16 +228,16 @@ async function switchSessionModel({
199
228
 
200
229
  async function ensureRuntime({
201
230
  start,
202
- turn,
203
- emit,
204
231
  }: {
205
232
  start: StartMessage;
206
- turn: BridgeTurn;
207
- emit: Emit;
208
233
  }): Promise<void> {
209
234
  if (
210
235
  runtime.client &&
211
- isDeepStrictEqual(runtime.openCodeConfig, start.openCodeConfig)
236
+ isDeepStrictEqual(runtime.openCodeConfig, start.openCodeConfig) &&
237
+ isDeepStrictEqual(
238
+ runtime.inactiveBuiltinToolNames,
239
+ start.inactiveBuiltinToolNames,
240
+ )
212
241
  ) {
213
242
  return;
214
243
  }
@@ -220,8 +249,6 @@ async function ensureRuntime({
220
249
  runtime.toolNames = new Set(start.tools.map(tool => tool.name));
221
250
  runtime.relay = await startToolRelay({
222
251
  tools: start.tools,
223
- emit,
224
- requestToolResult: turn.requestToolResult,
225
252
  });
226
253
  }
227
254
  const toolSchemasPath =
@@ -258,6 +285,9 @@ async function ensureRuntime({
258
285
  .map(([serverName]) => `${sanitizeMcpToolName(serverName)}_`),
259
286
  );
260
287
  runtime.openCodeConfig = structuredClone(start.openCodeConfig);
288
+ runtime.inactiveBuiltinToolNames = structuredClone(
289
+ start.inactiveBuiltinToolNames,
290
+ );
261
291
  } catch (error) {
262
292
  closeRuntime();
263
293
  throw error;
@@ -271,6 +301,7 @@ function closeRuntime(): void {
271
301
  runtime.client = undefined;
272
302
  runtime.relay = undefined;
273
303
  runtime.openCodeConfig = undefined;
304
+ runtime.inactiveBuiltinToolNames = undefined;
274
305
  runtime.toolNames = new Set();
275
306
  runtime.mcpToolPrefixes = new Set();
276
307
  }
@@ -304,17 +335,9 @@ function buildOpenCodeConfig({
304
335
  };
305
336
  if (start.model) config.model = start.model;
306
337
  if (skillsDir) config.skills = { paths: [skillsDir] };
307
- const inactiveToolNames = resolveInactiveBuiltinToolNames(start);
308
338
  const permission = config.permission as Record<string, unknown>;
309
- for (const toolName of inactiveToolNames) {
310
- const permissionName = toPermissionToolName(
311
- PUBLIC_TO_NATIVE[toolName] ?? toolName,
312
- );
313
- if (permissionName === 'ls') {
314
- permission.list = 'ask';
315
- } else {
316
- permission[permissionName] = 'ask';
317
- }
339
+ for (const toolName of start.inactiveBuiltinToolNames ?? []) {
340
+ permission[BUILTIN_TOOL_PERMISSION[toolName]] = 'deny';
318
341
  }
319
342
  const provider = buildProviderConfig(start);
320
343
  if (provider) config.provider = provider;
@@ -715,6 +738,8 @@ async function runPrompt({
715
738
  let terminalError: string | undefined;
716
739
  let submittingUserMessage = false;
717
740
  const state = createTranslationState();
741
+ const hostInputTracker = createOpenCodeHostInputTracker({ emit });
742
+ currentHostInputTracker = hostInputTracker;
718
743
  const initialSessionTokens = await readSessionTokens({
719
744
  client,
720
745
  sessionId,
@@ -729,14 +754,16 @@ async function runPrompt({
729
754
  client,
730
755
  sessionId,
731
756
  permissionMode: start.permissionMode,
732
- builtinToolFiltering: start.builtinToolFiltering,
757
+ inactiveBuiltinToolNames: start.inactiveBuiltinToolNames,
733
758
  turn,
734
759
  state,
760
+ hostInputTracker,
735
761
  emit: msg => {
736
762
  if (msg.type === 'text-delta' || msg.type === 'reasoning-delta') {
737
763
  sawContent = true;
738
764
  }
739
765
  if (msg.type === 'finish-step') {
766
+ hostInputTracker.observeFinishStep();
740
767
  sawFinishStep = true;
741
768
  stepUsage = addUsage({
742
769
  left: stepUsage,
@@ -957,7 +984,7 @@ async function runCompaction({
957
984
  client,
958
985
  sessionId,
959
986
  permissionMode: start.permissionMode,
960
- builtinToolFiltering: start.builtinToolFiltering,
987
+ inactiveBuiltinToolNames: start.inactiveBuiltinToolNames,
961
988
  turn,
962
989
  state: createTranslationState(),
963
990
  emit: msg => {
@@ -1021,9 +1048,10 @@ async function consumeEvents({
1021
1048
  client,
1022
1049
  sessionId,
1023
1050
  permissionMode,
1024
- builtinToolFiltering,
1051
+ inactiveBuiltinToolNames,
1025
1052
  turn,
1026
1053
  state,
1054
+ hostInputTracker,
1027
1055
  emit,
1028
1056
  signal,
1029
1057
  onSubscribed,
@@ -1032,9 +1060,10 @@ async function consumeEvents({
1032
1060
  client: OpenCodeClient;
1033
1061
  sessionId: string;
1034
1062
  permissionMode: StartMessage['permissionMode'];
1035
- builtinToolFiltering: StartMessage['builtinToolFiltering'];
1063
+ inactiveBuiltinToolNames: StartMessage['inactiveBuiltinToolNames'];
1036
1064
  turn: BridgeTurn;
1037
1065
  state: TranslationState;
1066
+ hostInputTracker?: OpenCodeHostInputTracker;
1038
1067
  emit: Emit;
1039
1068
  signal: AbortSignal;
1040
1069
  onSubscribed?: () => void;
@@ -1069,6 +1098,7 @@ async function consumeEvents({
1069
1098
  getHostToolName,
1070
1099
  authorizeHostToolCall: input => authorizeHostToolCall({ ...input, state }),
1071
1100
  onSubagentSession: registerSubagentSession(sessionId),
1101
+ onToolCallStatus: input => hostInputTracker?.observeToolCall(input),
1072
1102
  isMcpToolName: toolName =>
1073
1103
  [...runtime.mcpToolPrefixes].some(prefix => toolName.startsWith(prefix)),
1074
1104
  stripWorkDir,
@@ -1141,56 +1171,141 @@ async function consumeEvents({
1141
1171
  }
1142
1172
  processEvent(event);
1143
1173
  };
1144
- for await (const rawEvent of stream) {
1145
- if (signal.aborted || turn.abortSignal.aborted) break;
1174
+ /*
1175
+ * Permission requests and questions are answered without blocking the event
1176
+ * loop, so every request of a step reaches the host before it pauses. A
1177
+ * failed handler still ends the event loop with its error.
1178
+ */
1179
+ let rejectHandlerFailure!: (error: unknown) => void;
1180
+ const handlerFailure = new Promise<never>((_, reject) => {
1181
+ rejectHandlerFailure = reject;
1182
+ });
1183
+ handlerFailure.catch(() => {});
1184
+ const runHandler = (handler: Promise<void>) => {
1185
+ handler.catch(error => rejectHandlerFailure(error));
1186
+ };
1187
+ const hostInputWait = ({
1188
+ toolCallId,
1189
+ isDescendant,
1190
+ settlesToolCall,
1191
+ }: {
1192
+ toolCallId: string;
1193
+ isDescendant: boolean;
1194
+ settlesToolCall: boolean;
1195
+ }): HostInputWait | undefined => {
1196
+ if (hostInputTracker == null) return undefined;
1197
+ if (isDescendant) {
1198
+ const id = toolCallId;
1199
+ return {
1200
+ markWaiting: () =>
1201
+ hostInputTracker.markGroupWaiting({ group: 'subagent', id }),
1202
+ clearWaiting: () =>
1203
+ hostInputTracker.clearGroupWaiting({ group: 'subagent', id }),
1204
+ };
1205
+ }
1206
+ return {
1207
+ markWaiting: () => hostInputTracker.markWaiting({ toolCallId }),
1208
+ clearWaiting: () =>
1209
+ hostInputTracker.clearWaiting({
1210
+ toolCallId,
1211
+ settled: settlesToolCall,
1212
+ }),
1213
+ };
1214
+ };
1215
+ const iterator = stream[Symbol.asyncIterator]();
1216
+ try {
1217
+ for (;;) {
1218
+ const next = await Promise.race([iterator.next(), handlerFailure]);
1219
+ if (next.done) break;
1220
+ if (consumeEvent(next.value)) break;
1221
+ }
1222
+ } finally {
1223
+ void iterator.return?.();
1224
+ }
1225
+
1226
+ function consumeEvent(rawEvent: unknown): boolean {
1227
+ if (signal.aborted || turn.abortSignal.aborted) return true;
1146
1228
  const event = unwrapOpenCodeEvent(rawEvent);
1147
1229
  const eventSessionId = event ? getOpenCodeEventSessionId(event) : undefined;
1148
- if (!event) continue;
1230
+ if (!event) return false;
1149
1231
  const scopedSessionId =
1150
1232
  !eventSessionId || eventSessionId === sessionId
1151
1233
  ? sessionId
1152
1234
  : taskSessionIds.has(eventSessionId)
1153
1235
  ? eventSessionId
1154
1236
  : undefined;
1155
- if (!scopedSessionId) continue;
1237
+ if (!scopedSessionId) return false;
1156
1238
  const isDescendant = scopedSessionId !== sessionId;
1157
- if (event.type === 'question.asked') {
1158
- await handleQuestion({
1159
- client,
1160
- turn,
1161
- emit,
1162
- event,
1239
+ const waitFor = ({
1240
+ toolCallId,
1241
+ settlesToolCall,
1242
+ }: {
1243
+ toolCallId: string;
1244
+ settlesToolCall: boolean;
1245
+ }) =>
1246
+ hostInputWait({
1247
+ toolCallId: isDescendant
1248
+ ? `${scopedSessionId}:${toolCallId}`
1249
+ : toolCallId,
1250
+ isDescendant,
1251
+ settlesToolCall,
1163
1252
  });
1253
+ if (event.type === 'question.asked') {
1254
+ runHandler(
1255
+ handleQuestion({
1256
+ client,
1257
+ turn,
1258
+ emit,
1259
+ event,
1260
+ waitFor,
1261
+ }),
1262
+ );
1164
1263
  } else if (event.type === 'permission.v2.asked') {
1165
- await handlePermissionV2({
1166
- client,
1167
- sessionId: scopedSessionId,
1168
- permissionMode,
1169
- builtinToolFiltering,
1170
- turn,
1171
- emit,
1172
- event,
1173
- });
1264
+ runHandler(
1265
+ handlePermissionV2({
1266
+ client,
1267
+ sessionId: scopedSessionId,
1268
+ permissionMode,
1269
+ inactiveBuiltinToolNames,
1270
+ turn,
1271
+ emit,
1272
+ event,
1273
+ waitFor,
1274
+ }),
1275
+ );
1174
1276
  } else if (event.type === 'permission.asked') {
1175
- await handlePermission({
1176
- client,
1177
- sessionId: scopedSessionId,
1178
- permissionMode,
1179
- builtinToolFiltering,
1180
- turn,
1181
- emit,
1182
- event,
1183
- });
1277
+ runHandler(
1278
+ handlePermission({
1279
+ client,
1280
+ sessionId: scopedSessionId,
1281
+ permissionMode,
1282
+ inactiveBuiltinToolNames,
1283
+ turn,
1284
+ emit,
1285
+ event,
1286
+ waitFor,
1287
+ }),
1288
+ );
1184
1289
  } else if (isDescendant) {
1185
1290
  processDescendantEvent(scopedSessionId, event);
1186
1291
  } else {
1187
1292
  emitStreamEvent(event);
1188
1293
  }
1189
- if (isDescendant) continue;
1190
- if (onEvent?.(event)) break;
1294
+ if (isDescendant) return false;
1295
+ return onEvent?.(event) === true;
1191
1296
  }
1192
1297
  }
1193
1298
 
1299
+ type HostInputWait = {
1300
+ markWaiting(): void;
1301
+ clearWaiting(): void;
1302
+ };
1303
+
1304
+ type WaitForHostInput = (input: {
1305
+ toolCallId: string;
1306
+ settlesToolCall: boolean;
1307
+ }) => HostInputWait | undefined;
1308
+
1194
1309
  function getSubagentStepId(event: OpenCodeEvent | undefined) {
1195
1310
  if (event?.type === 'message.part.updated') {
1196
1311
  const part = asOpenCodeObject(event.properties?.part);
@@ -1206,11 +1321,13 @@ async function handleQuestion({
1206
1321
  turn,
1207
1322
  emit,
1208
1323
  event,
1324
+ waitFor,
1209
1325
  }: {
1210
1326
  client: OpenCodeClient;
1211
1327
  turn: BridgeTurn;
1212
1328
  emit: Emit;
1213
1329
  event: OpenCodeEvent;
1330
+ waitFor?: WaitForHostInput;
1214
1331
  }): Promise<void> {
1215
1332
  const nativeRequest = event.properties as OpenCodeQuestionRequest | undefined;
1216
1333
  if (
@@ -1238,17 +1355,21 @@ async function handleQuestion({
1238
1355
  });
1239
1356
 
1240
1357
  const questionKey = openCodeQuestionKey(nativeRequest);
1241
- const result = await turn.requestToolResult({
1242
- toolCallId,
1243
- matches: candidate => {
1244
- const continuedRequest = candidate.toolResult?.providerOptions?.opencode
1245
- ?.nativeRequest as OpenCodeQuestionRequest | undefined;
1246
- return (
1247
- continuedRequest != null &&
1248
- openCodeQuestionKey(continuedRequest) === questionKey
1249
- );
1250
- },
1251
- });
1358
+ const wait = waitFor?.({ toolCallId, settlesToolCall: true });
1359
+ wait?.markWaiting();
1360
+ const result = await turn
1361
+ .requestToolResult({
1362
+ toolCallId,
1363
+ matches: candidate => {
1364
+ const continuedRequest = candidate.toolResult?.providerOptions?.opencode
1365
+ ?.nativeRequest as OpenCodeQuestionRequest | undefined;
1366
+ return (
1367
+ continuedRequest != null &&
1368
+ openCodeQuestionKey(continuedRequest) === questionKey
1369
+ );
1370
+ },
1371
+ })
1372
+ .finally(() => wait?.clearWaiting());
1252
1373
  const nativeResponse = toOpenCodeQuestionResponse({
1253
1374
  nativeRequest,
1254
1375
  output: result.output as Parameters<
@@ -1282,18 +1403,20 @@ async function handlePermissionV2({
1282
1403
  client,
1283
1404
  sessionId,
1284
1405
  permissionMode,
1285
- builtinToolFiltering,
1406
+ inactiveBuiltinToolNames,
1286
1407
  turn,
1287
1408
  emit,
1288
1409
  event,
1410
+ waitFor,
1289
1411
  }: {
1290
1412
  client: OpenCodeClient;
1291
1413
  sessionId: string;
1292
1414
  permissionMode: StartMessage['permissionMode'];
1293
- builtinToolFiltering: StartMessage['builtinToolFiltering'];
1415
+ inactiveBuiltinToolNames: StartMessage['inactiveBuiltinToolNames'];
1294
1416
  turn: BridgeTurn;
1295
1417
  emit: Emit;
1296
1418
  event: OpenCodeEvent;
1419
+ waitFor?: WaitForHostInput;
1297
1420
  }): Promise<void> {
1298
1421
  const props = event.properties ?? {};
1299
1422
  const requestID = String(props.id ?? '');
@@ -1306,9 +1429,10 @@ async function handlePermissionV2({
1306
1429
  requestID,
1307
1430
  toolCallId: String(props.source?.callID ?? requestID),
1308
1431
  permissionMode,
1309
- builtinToolFiltering,
1432
+ inactiveBuiltinToolNames,
1310
1433
  turn,
1311
1434
  emit,
1435
+ waitFor,
1312
1436
  });
1313
1437
  await client.v2.session.permission.reply({
1314
1438
  sessionID: sessionId,
@@ -1322,18 +1446,20 @@ async function handlePermission({
1322
1446
  client,
1323
1447
  sessionId,
1324
1448
  permissionMode,
1325
- builtinToolFiltering,
1449
+ inactiveBuiltinToolNames,
1326
1450
  turn,
1327
1451
  emit,
1328
1452
  event,
1453
+ waitFor,
1329
1454
  }: {
1330
1455
  client: OpenCodeClient;
1331
1456
  sessionId: string;
1332
1457
  permissionMode: StartMessage['permissionMode'];
1333
- builtinToolFiltering: StartMessage['builtinToolFiltering'];
1458
+ inactiveBuiltinToolNames: StartMessage['inactiveBuiltinToolNames'];
1334
1459
  turn: BridgeTurn;
1335
1460
  emit: Emit;
1336
1461
  event: OpenCodeEvent;
1462
+ waitFor?: WaitForHostInput;
1337
1463
  }): Promise<void> {
1338
1464
  const props = event.properties ?? {};
1339
1465
  const requestID = String(props.id ?? '');
@@ -1345,9 +1471,10 @@ async function handlePermission({
1345
1471
  requestID,
1346
1472
  toolCallId: String(tool?.callID ?? requestID),
1347
1473
  permissionMode,
1348
- builtinToolFiltering,
1474
+ inactiveBuiltinToolNames,
1349
1475
  turn,
1350
1476
  emit,
1477
+ waitFor,
1351
1478
  });
1352
1479
  await client.permission.reply({
1353
1480
  requestID,
@@ -1364,29 +1491,36 @@ async function selectPermissionReply({
1364
1491
  requestID,
1365
1492
  toolCallId,
1366
1493
  permissionMode,
1367
- builtinToolFiltering,
1494
+ inactiveBuiltinToolNames,
1368
1495
  turn,
1369
1496
  emit,
1497
+ waitFor,
1370
1498
  }: {
1371
1499
  action: string;
1372
1500
  resources: string[];
1373
1501
  requestID: string;
1374
1502
  toolCallId: string;
1375
1503
  permissionMode: StartMessage['permissionMode'];
1376
- builtinToolFiltering: StartMessage['builtinToolFiltering'];
1504
+ inactiveBuiltinToolNames: StartMessage['inactiveBuiltinToolNames'];
1377
1505
  turn: BridgeTurn;
1378
1506
  emit: Emit;
1507
+ waitFor?: WaitForHostInput;
1379
1508
  }): Promise<{ reply: 'once' | 'always' | 'reject'; message?: string }> {
1509
+ const requestApproval = async () => {
1510
+ const wait = waitFor?.({ toolCallId, settlesToolCall: false });
1511
+ wait?.markWaiting();
1512
+ return turn
1513
+ .requestToolApproval(requestID)
1514
+ .finally(() => wait?.clearWaiting());
1515
+ };
1380
1516
  const toolName = toPermissionToolName(action);
1381
- if (
1382
- isBuiltinToolInactive({ toolName, toolFiltering: builtinToolFiltering })
1383
- ) {
1517
+ if (isBuiltinToolInactive({ toolName, inactiveBuiltinToolNames })) {
1384
1518
  emit({
1385
1519
  type: 'tool-approval-request',
1386
1520
  approvalId: requestID,
1387
1521
  toolCallId,
1388
1522
  });
1389
- const decision = await turn.requestToolApproval(requestID);
1523
+ const decision = await requestApproval();
1390
1524
  return decision.approved
1391
1525
  ? { reply: 'once' }
1392
1526
  : {
@@ -1412,7 +1546,7 @@ async function selectPermissionReply({
1412
1546
  approvalId: requestID,
1413
1547
  toolCallId,
1414
1548
  });
1415
- const decision = await turn.requestToolApproval(requestID);
1549
+ const decision = await requestApproval();
1416
1550
  return decision.approved
1417
1551
  ? { reply: 'once' }
1418
1552
  : {
@@ -1437,26 +1571,15 @@ function toPermissionToolName(action: string): string {
1437
1571
  return toWireToolName(normalized);
1438
1572
  }
1439
1573
 
1440
- function resolveInactiveBuiltinToolNames(
1441
- start: StartMessage,
1442
- ): ReadonlyArray<string> {
1443
- const toolFiltering = start.builtinToolFiltering;
1444
- if (toolFiltering == null) return [];
1445
- return toolFiltering.mode === 'allow'
1446
- ? Object.keys(PUBLIC_TO_NATIVE).filter(
1447
- name => !toolFiltering.toolNames.includes(name),
1448
- )
1449
- : toolFiltering.toolNames;
1450
- }
1451
-
1452
1574
  function isBuiltinToolInactive(input: {
1453
1575
  toolName: string;
1454
- toolFiltering: StartMessage['builtinToolFiltering'];
1576
+ inactiveBuiltinToolNames: StartMessage['inactiveBuiltinToolNames'];
1455
1577
  }): boolean {
1456
- if (input.toolFiltering == null) return false;
1457
- return input.toolFiltering.mode === 'allow'
1458
- ? !input.toolFiltering.toolNames.includes(input.toolName)
1459
- : input.toolFiltering.toolNames.includes(input.toolName);
1578
+ return (
1579
+ input.inactiveBuiltinToolNames?.some(
1580
+ toolName => toolName === input.toolName,
1581
+ ) ?? false
1582
+ );
1460
1583
  }
1461
1584
 
1462
1585
  function isExternalPath(resource: string): boolean {
@@ -1718,16 +1841,32 @@ function emitAssistantContentPart(part: unknown, emit: Emit): void {
1718
1841
 
1719
1842
  async function startToolRelay({
1720
1843
  tools,
1721
- emit,
1722
- requestToolResult,
1723
1844
  }: {
1724
1845
  tools: ReadonlyArray<{ name: string }>;
1725
- emit: Emit;
1726
- requestToolResult: (
1727
- toolCallId: string,
1728
- ) => Promise<{ output: unknown; isError?: boolean }>;
1729
1846
  }): Promise<ToolRelay> {
1730
- return startAuthorizedToolRelay({ tools, emit, requestToolResult });
1847
+ return startAuthorizedToolRelay({
1848
+ tools,
1849
+ emit: message => {
1850
+ currentTurn?.emit(message as BridgeEvent);
1851
+ },
1852
+ requestToolResult: toolCallId => {
1853
+ if (!currentTurn) throw new Error('No active turn in OpenCode bridge');
1854
+ return currentTurn.requestToolResult(toolCallId);
1855
+ },
1856
+ onWaitingChange: ({ requestId, waiting }) => {
1857
+ if (waiting) {
1858
+ currentHostInputTracker?.markGroupWaiting({
1859
+ group: 'host-tool',
1860
+ id: requestId,
1861
+ });
1862
+ } else {
1863
+ currentHostInputTracker?.clearGroupWaiting({
1864
+ group: 'host-tool',
1865
+ id: requestId,
1866
+ });
1867
+ }
1868
+ },
1869
+ });
1731
1870
  }
1732
1871
 
1733
1872
  function createDeferred<T>(): {