ycodium-acp-executor 0.2.5912 → 0.2.5921
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/dist/adapter-support.d.ts.map +1 -1
- package/dist/adapter-support.js +12 -3
- package/dist/adapter-support.js.map +1 -1
- package/dist/approvals.d.ts +1 -0
- package/dist/approvals.d.ts.map +1 -1
- package/dist/approvals.js.map +1 -1
- package/dist/events.d.ts +18 -0
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +226 -40
- package/dist/events.js.map +1 -1
- package/dist/executor/requests.d.ts +12 -1
- package/dist/executor/requests.d.ts.map +1 -1
- package/dist/executor/requests.js +341 -45
- package/dist/executor/requests.js.map +1 -1
- package/dist/executor/updates.d.ts +14 -2
- package/dist/executor/updates.d.ts.map +1 -1
- package/dist/executor/updates.js +65 -10
- package/dist/executor/updates.js.map +1 -1
- package/dist/executor.d.ts.map +1 -1
- package/dist/executor.js +16 -13
- package/dist/executor.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/runtime-model.d.ts.map +1 -1
- package/dist/runtime-model.js +54 -10
- package/dist/runtime-model.js.map +1 -1
- package/dist/source/adapter-support.ts +15 -3
- package/dist/source/approvals.ts +1 -0
- package/dist/source/events.ts +277 -41
- package/dist/source/executor/requests.ts +372 -45
- package/dist/source/executor/updates.ts +98 -12
- package/dist/source/executor.ts +25 -20
- package/dist/source/index.ts +4 -1
- package/dist/source/runtime-model.ts +75 -10
- package/dist/source/tool-call-state.ts +171 -4
- package/dist/source/types.ts +6 -1
- package/dist/source/vendor-hooks.ts +19 -0
- package/dist/tool-call-state.d.ts +27 -0
- package/dist/tool-call-state.d.ts.map +1 -1
- package/dist/tool-call-state.js +113 -4
- package/dist/tool-call-state.js.map +1 -1
- package/dist/types.d.ts +6 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/vendor-hooks.d.ts +18 -0
- package/dist/vendor-hooks.d.ts.map +1 -1
- package/dist/vendor-hooks.js.map +1 -1
- package/package.json +4 -4
|
@@ -17,10 +17,17 @@ import {
|
|
|
17
17
|
fileChangesFromAcpPermission,
|
|
18
18
|
makeAcpRequestOpenedEvent,
|
|
19
19
|
makeAcpRequestResolvedEvent,
|
|
20
|
+
toolCallTrackingKey,
|
|
21
|
+
type AcpToolCallTrackedState,
|
|
20
22
|
} from "../events.ts";
|
|
21
23
|
import { CLIENT_METHODS, RequestError } from "../protocol.ts";
|
|
22
|
-
import { parsePermissionRequest } from "../runtime-model.ts";
|
|
24
|
+
import { mergeToolCallState, parsePermissionRequest } from "../runtime-model.ts";
|
|
23
25
|
import { defaultPermissionStatement } from "../permission-statement.ts";
|
|
26
|
+
import {
|
|
27
|
+
readToolCallIdentityFields,
|
|
28
|
+
toolCallIdentityChanged,
|
|
29
|
+
type AcpToolCallState,
|
|
30
|
+
} from "../tool-call-state.ts";
|
|
24
31
|
import type { AcpApprovalDecision, AcpRequestPermissionParams } from "../types.ts";
|
|
25
32
|
import { userInputAnswerOptionId, type PendingUserInput } from "../user-input-pending.ts";
|
|
26
33
|
import { isRecord } from "../util.ts";
|
|
@@ -67,6 +74,127 @@ export function createExecutorRequests({
|
|
|
67
74
|
);
|
|
68
75
|
};
|
|
69
76
|
|
|
77
|
+
type PermissionMutationEntry = {
|
|
78
|
+
readonly requestId: string;
|
|
79
|
+
readonly trackingKey: string;
|
|
80
|
+
readonly hasOwnMutation: boolean;
|
|
81
|
+
hadPrevious: boolean;
|
|
82
|
+
previousTracked: AcpToolCallTrackedState | undefined;
|
|
83
|
+
hadApplied: boolean;
|
|
84
|
+
appliedTracked: AcpToolCallTrackedState | undefined;
|
|
85
|
+
};
|
|
86
|
+
const permissionMutationsByKey = new Map<string, PermissionMutationEntry[]>();
|
|
87
|
+
const trackingKeyByRequestId = new Map<string, string>();
|
|
88
|
+
let nextSettlementSeq = 0;
|
|
89
|
+
|
|
90
|
+
const readCommittedBaseline = (trackingKey: string): AcpToolCallTrackedState | undefined => {
|
|
91
|
+
const pendingList = permissionMutationsByKey.get(trackingKey);
|
|
92
|
+
if (pendingList !== undefined && pendingList.length > 0) {
|
|
93
|
+
return pendingList[0]!.previousTracked;
|
|
94
|
+
}
|
|
95
|
+
return toolCalls.get(trackingKey);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const recordPermissionMutation = (entry: PermissionMutationEntry): void => {
|
|
99
|
+
const list = permissionMutationsByKey.get(entry.trackingKey);
|
|
100
|
+
if (list !== undefined) list.push(entry);
|
|
101
|
+
else permissionMutationsByKey.set(entry.trackingKey, [entry]);
|
|
102
|
+
trackingKeyByRequestId.set(entry.requestId, entry.trackingKey);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const supersedePermissionTrackedState = (
|
|
106
|
+
trackingKey: string,
|
|
107
|
+
supersedeOptions?: { readonly restoreCommitted?: boolean },
|
|
108
|
+
): void => {
|
|
109
|
+
const list = permissionMutationsByKey.get(trackingKey);
|
|
110
|
+
if (list === undefined) return;
|
|
111
|
+
if (supersedeOptions?.restoreCommitted && list.length > 0) {
|
|
112
|
+
restoreTrackedState(trackingKey, list[0]!.hadPrevious, list[0]!.previousTracked);
|
|
113
|
+
}
|
|
114
|
+
for (const item of list) trackingKeyByRequestId.delete(item.requestId);
|
|
115
|
+
permissionMutationsByKey.delete(trackingKey);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const restoreTrackedState = (
|
|
119
|
+
trackingKey: string,
|
|
120
|
+
hadPrevious: boolean,
|
|
121
|
+
previousTracked: AcpToolCallTrackedState | undefined,
|
|
122
|
+
): void => {
|
|
123
|
+
if (hadPrevious && previousTracked !== undefined) {
|
|
124
|
+
toolCalls.set(trackingKey, previousTracked);
|
|
125
|
+
} else {
|
|
126
|
+
toolCalls.delete(trackingKey);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const runWithCommittedPermissionBaseline = <T>(
|
|
131
|
+
trackingKey: string,
|
|
132
|
+
applyUpdate: () => { readonly nextToolCall: AcpToolCallState; readonly reset: boolean } & T,
|
|
133
|
+
passiveNextState: AcpToolCallState,
|
|
134
|
+
rawTitle?: unknown,
|
|
135
|
+
): { readonly nextToolCall: AcpToolCallState; readonly reset: boolean } & T => {
|
|
136
|
+
const list = permissionMutationsByKey.get(trackingKey);
|
|
137
|
+
if (list === undefined || list.length === 0) {
|
|
138
|
+
return applyUpdate();
|
|
139
|
+
}
|
|
140
|
+
const baseEntry = list[0]!;
|
|
141
|
+
restoreTrackedState(trackingKey, baseEntry.hadPrevious, baseEntry.previousTracked);
|
|
142
|
+
const result = applyUpdate();
|
|
143
|
+
if (result.reset) {
|
|
144
|
+
for (const item of list) trackingKeyByRequestId.delete(item.requestId);
|
|
145
|
+
permissionMutationsByKey.delete(trackingKey);
|
|
146
|
+
return result;
|
|
147
|
+
}
|
|
148
|
+
baseEntry.hadPrevious = toolCalls.has(trackingKey);
|
|
149
|
+
baseEntry.previousTracked = toolCalls.get(trackingKey);
|
|
150
|
+
for (const item of list) {
|
|
151
|
+
if (!item.hadApplied || item.appliedTracked === undefined) continue;
|
|
152
|
+
const mergedApplied = mergeToolCallState(item.appliedTracked.state, passiveNextState);
|
|
153
|
+
item.appliedTracked = {
|
|
154
|
+
...item.appliedTracked,
|
|
155
|
+
state: mergedApplied,
|
|
156
|
+
...(typeof rawTitle === "string" ? { rawTitle } : {}),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
const tail = list[list.length - 1]!;
|
|
160
|
+
restoreTrackedState(trackingKey, tail.hadApplied, tail.appliedTracked);
|
|
161
|
+
return result;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const releasePermissionTrackedState = (requestId: string, accepted: boolean): void => {
|
|
165
|
+
const trackingKey = trackingKeyByRequestId.get(requestId);
|
|
166
|
+
trackingKeyByRequestId.delete(requestId);
|
|
167
|
+
if (trackingKey === undefined) return;
|
|
168
|
+
const list = permissionMutationsByKey.get(trackingKey);
|
|
169
|
+
if (list === undefined) return;
|
|
170
|
+
const idx = list.findIndex((item) => item.requestId === requestId);
|
|
171
|
+
if (idx < 0) return;
|
|
172
|
+
if (!accepted || !list[idx]!.hasOwnMutation) {
|
|
173
|
+
const [removed] = list.splice(idx, 1);
|
|
174
|
+
if (removed === undefined) return;
|
|
175
|
+
if (idx < list.length) {
|
|
176
|
+
list[idx]!.hadPrevious = removed.hadPrevious;
|
|
177
|
+
list[idx]!.previousTracked = removed.previousTracked;
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (list.length === 0) {
|
|
181
|
+
permissionMutationsByKey.delete(trackingKey);
|
|
182
|
+
}
|
|
183
|
+
restoreTrackedState(trackingKey, removed.hadPrevious, removed.previousTracked);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const acceptedEntry = list[idx]!;
|
|
187
|
+
const committed = list.splice(0, idx + 1);
|
|
188
|
+
for (const item of committed) trackingKeyByRequestId.delete(item.requestId);
|
|
189
|
+
if (list.length === 0) {
|
|
190
|
+
permissionMutationsByKey.delete(trackingKey);
|
|
191
|
+
restoreTrackedState(trackingKey, acceptedEntry.hadApplied, acceptedEntry.appliedTracked);
|
|
192
|
+
} else {
|
|
193
|
+
list[0]!.hadPrevious = acceptedEntry.hadApplied;
|
|
194
|
+
list[0]!.previousTracked = acceptedEntry.appliedTracked;
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
|
|
70
198
|
const settleApproval = (
|
|
71
199
|
pending: PendingApproval,
|
|
72
200
|
outcome: ReturnType<typeof approvalOutcomeForDecision>,
|
|
@@ -76,12 +204,29 @@ export function createExecutorRequests({
|
|
|
76
204
|
if (!approvals.settle(pending, outcome)) {
|
|
77
205
|
return false;
|
|
78
206
|
}
|
|
207
|
+
const accepted =
|
|
208
|
+
outcome.outcome === "selected" &&
|
|
209
|
+
(decision === "accept" || decision === "acceptForSession" || decision === "acceptAlways");
|
|
210
|
+
releasePermissionTrackedState(pending.requestId, accepted);
|
|
211
|
+
hooks.onPermissionRequestSettled?.({
|
|
212
|
+
sessionId: pending.request.sessionId,
|
|
213
|
+
toolCallId: pending.request.toolCall.toolCallId,
|
|
214
|
+
accepted,
|
|
215
|
+
...(pending.settlementId === undefined ? {} : { requestId: pending.settlementId }),
|
|
216
|
+
});
|
|
79
217
|
emitResolved(pending, decision, reason);
|
|
80
218
|
return true;
|
|
81
219
|
};
|
|
82
220
|
|
|
83
221
|
const settleAllApprovals = (decision: AcpApprovalDecision): void => {
|
|
84
222
|
for (const entry of approvals.settleAll({ outcome: "cancelled" })) {
|
|
223
|
+
releasePermissionTrackedState(entry.requestId, false);
|
|
224
|
+
hooks.onPermissionRequestSettled?.({
|
|
225
|
+
sessionId: entry.request.sessionId,
|
|
226
|
+
toolCallId: entry.request.toolCall.toolCallId,
|
|
227
|
+
accepted: false,
|
|
228
|
+
...(entry.settlementId === undefined ? {} : { requestId: entry.settlementId }),
|
|
229
|
+
});
|
|
85
230
|
emitResolved(entry, decision);
|
|
86
231
|
}
|
|
87
232
|
};
|
|
@@ -189,52 +334,178 @@ export function createExecutorRequests({
|
|
|
189
334
|
};
|
|
190
335
|
};
|
|
191
336
|
|
|
192
|
-
const handlePermissionRequest = async (
|
|
193
|
-
if (!isAcpRequestPermissionParams(
|
|
337
|
+
const handlePermissionRequest = async (rawParams: unknown): Promise<unknown> => {
|
|
338
|
+
if (!isAcpRequestPermissionParams(rawParams)) {
|
|
194
339
|
throw RequestError.invalidParams({ detail: "Malformed session/request_permission" });
|
|
195
340
|
}
|
|
196
|
-
const userInputOutcome = await handlePermissionAsUserInput(
|
|
341
|
+
const userInputOutcome = await handlePermissionAsUserInput(rawParams);
|
|
197
342
|
if (userInputOutcome !== undefined) return userInputOutcome;
|
|
198
|
-
|
|
199
|
-
const
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
return { outcome: { outcome: "selected", optionId } };
|
|
212
|
-
}
|
|
343
|
+
failIfDisposed();
|
|
344
|
+
const settlementId = `perm-${++nextSettlementSeq}`;
|
|
345
|
+
const transformed = hooks.transformPermissionRequest
|
|
346
|
+
? hooks.transformPermissionRequest(rawParams, { requestId: settlementId })
|
|
347
|
+
: rawParams;
|
|
348
|
+
if (!isAcpRequestPermissionParams(transformed)) {
|
|
349
|
+
hooks.onPermissionRequestSettled?.({
|
|
350
|
+
sessionId: rawParams.sessionId,
|
|
351
|
+
toolCallId: rawParams.toolCall.toolCallId,
|
|
352
|
+
accepted: false,
|
|
353
|
+
requestId: settlementId,
|
|
354
|
+
});
|
|
355
|
+
throw RequestError.invalidParams({ detail: "Malformed session/request_permission" });
|
|
213
356
|
}
|
|
214
|
-
const
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
const
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
const
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
357
|
+
const params = transformed;
|
|
358
|
+
const trackingKey = toolCallTrackingKey(params.sessionId, params.toolCall.toolCallId);
|
|
359
|
+
const immediateHadPrevious = toolCalls.has(trackingKey);
|
|
360
|
+
const immediatePrevious = toolCalls.get(trackingKey);
|
|
361
|
+
let mutatedToolCall = false;
|
|
362
|
+
const hasTextField = (record: Record<string, unknown>, key: string): boolean =>
|
|
363
|
+
typeof record[key] === "string" && record[key].trim().length > 0;
|
|
364
|
+
const rawToolCallRecord = rawParams.toolCall as unknown as Record<string, unknown>;
|
|
365
|
+
const rawReqMeta = rawToolCallRecord["_meta"];
|
|
366
|
+
const rawParentMeta = (rawParams as unknown as Record<string, unknown>)["_meta"];
|
|
367
|
+
const transformedToolCallRecord = params.toolCall as unknown as Record<string, unknown>;
|
|
368
|
+
const transformedReqMeta = transformedToolCallRecord["_meta"];
|
|
369
|
+
const transformedParentMeta = (params as unknown as Record<string, unknown>)["_meta"];
|
|
370
|
+
const hasExplicitNegativeMeta =
|
|
371
|
+
(isRecord(rawReqMeta) && rawReqMeta["is_mcp_tool_call"] === false) ||
|
|
372
|
+
(isRecord(rawParentMeta) && rawParentMeta["is_mcp_tool_call"] === false) ||
|
|
373
|
+
(isRecord(transformedReqMeta) && transformedReqMeta["is_mcp_tool_call"] === false) ||
|
|
374
|
+
(isRecord(transformedParentMeta) && transformedParentMeta["is_mcp_tool_call"] === false);
|
|
375
|
+
let hasOwnMutation = false;
|
|
376
|
+
const prepared = (() => {
|
|
377
|
+
try {
|
|
378
|
+
const existingPendingList = permissionMutationsByKey.get(trackingKey);
|
|
379
|
+
const hasPendingSibling =
|
|
380
|
+
existingPendingList !== undefined && existingPendingList.length > 0;
|
|
381
|
+
const tracked = readCommittedBaseline(trackingKey);
|
|
382
|
+
const requestFields = params.toolCall as {
|
|
383
|
+
readonly name?: unknown;
|
|
384
|
+
readonly mcpServer?: unknown;
|
|
385
|
+
readonly mcpTool?: unknown;
|
|
386
|
+
};
|
|
387
|
+
const identityChanged =
|
|
388
|
+
tracked !== undefined &&
|
|
389
|
+
toolCallIdentityChanged(
|
|
390
|
+
{
|
|
391
|
+
name: tracked.state.data["name"],
|
|
392
|
+
kind: tracked.state.kind,
|
|
393
|
+
mcpServer: tracked.state.data["mcpServer"],
|
|
394
|
+
mcpTool: tracked.state.data["mcpTool"],
|
|
395
|
+
},
|
|
396
|
+
{
|
|
397
|
+
name: requestFields.name,
|
|
398
|
+
kind: params.toolCall.kind,
|
|
399
|
+
mcpServer: requestFields.mcpServer,
|
|
400
|
+
mcpTool: requestFields.mcpTool,
|
|
401
|
+
},
|
|
402
|
+
);
|
|
403
|
+
const effectiveTracked = identityChanged ? undefined : tracked;
|
|
404
|
+
const parsed = parsePermissionRequest(params, effectiveTracked);
|
|
405
|
+
const trackedNegativeMcp =
|
|
406
|
+
tracked !== undefined && readToolCallIdentityFields(tracked.state).negativeMcp;
|
|
407
|
+
const parsedNegativeMcp =
|
|
408
|
+
parsed.toolCall !== undefined && readToolCallIdentityFields(parsed.toolCall).negativeMcp;
|
|
409
|
+
const introducedTransformedIdentity =
|
|
410
|
+
(typeof requestFields.name === "string" &&
|
|
411
|
+
requestFields.name.trim().length > 0 &&
|
|
412
|
+
requestFields.name !== tracked?.state.data["name"]) ||
|
|
413
|
+
(typeof requestFields.mcpServer === "string" &&
|
|
414
|
+
requestFields.mcpServer.trim().length > 0 &&
|
|
415
|
+
requestFields.mcpServer !== tracked?.state.data["mcpServer"]) ||
|
|
416
|
+
(typeof requestFields.mcpTool === "string" &&
|
|
417
|
+
requestFields.mcpTool.trim().length > 0 &&
|
|
418
|
+
requestFields.mcpTool !== tracked?.state.data["mcpTool"]);
|
|
419
|
+
hasOwnMutation =
|
|
420
|
+
identityChanged ||
|
|
421
|
+
hasExplicitNegativeMeta ||
|
|
422
|
+
trackedNegativeMcp !== parsedNegativeMcp ||
|
|
423
|
+
introducedTransformedIdentity ||
|
|
424
|
+
hasTextField(rawToolCallRecord, "name") ||
|
|
425
|
+
rawToolCallRecord["name"] === null ||
|
|
426
|
+
requestFields.name === null ||
|
|
427
|
+
hasTextField(rawToolCallRecord, "mcpServer") ||
|
|
428
|
+
rawToolCallRecord["mcpServer"] === null ||
|
|
429
|
+
requestFields.mcpServer === null ||
|
|
430
|
+
hasTextField(rawToolCallRecord, "mcpTool") ||
|
|
431
|
+
rawToolCallRecord["mcpTool"] === null ||
|
|
432
|
+
requestFields.mcpTool === null ||
|
|
433
|
+
hasTextField(rawToolCallRecord, "title") ||
|
|
434
|
+
(hasTextField(rawToolCallRecord, "kind") &&
|
|
435
|
+
(rawToolCallRecord["kind"] as string).trim() !== "other") ||
|
|
436
|
+
(rawToolCallRecord["status"] !== undefined && rawToolCallRecord["status"] !== null) ||
|
|
437
|
+
rawToolCallRecord["rawInput"] !== undefined ||
|
|
438
|
+
rawToolCallRecord["rawOutput"] !== undefined ||
|
|
439
|
+
rawToolCallRecord["content"] !== undefined ||
|
|
440
|
+
rawToolCallRecord["locations"] !== undefined;
|
|
441
|
+
const retainedRawTitle =
|
|
442
|
+
typeof params.toolCall.title === "string"
|
|
443
|
+
? params.toolCall.title
|
|
444
|
+
: parsedNegativeMcp &&
|
|
445
|
+
effectiveTracked?.rawTitle !== undefined &&
|
|
446
|
+
/^(?:(?:Running|Run)\s+)?mcp_/u.test(effectiveTracked.rawTitle.trim())
|
|
447
|
+
? undefined
|
|
448
|
+
: effectiveTracked?.rawTitle;
|
|
449
|
+
if (hasOwnMutation || !hasPendingSibling) {
|
|
450
|
+
if (parsed.toolCall === undefined) {
|
|
451
|
+
if (identityChanged && toolCalls.delete(trackingKey)) {
|
|
452
|
+
mutatedToolCall = true;
|
|
453
|
+
}
|
|
454
|
+
} else {
|
|
455
|
+
if (parsed.toolCall.status === "completed" || parsed.toolCall.status === "failed") {
|
|
456
|
+
if (toolCalls.delete(trackingKey)) {
|
|
457
|
+
mutatedToolCall = true;
|
|
458
|
+
}
|
|
459
|
+
} else {
|
|
460
|
+
mutatedToolCall = true;
|
|
461
|
+
toolCalls.set(trackingKey, {
|
|
462
|
+
state: parsed.toolCall,
|
|
463
|
+
lastEmittedDetailLength: effectiveTracked?.lastEmittedDetailLength,
|
|
464
|
+
skippedSinceEmit: effectiveTracked?.skippedSinceEmit ?? 0,
|
|
465
|
+
sessionId: params.sessionId,
|
|
466
|
+
turnId: effectiveTracked?.turnId ?? shared.activeTurnId,
|
|
467
|
+
startedAtMs: effectiveTracked?.startedAtMs,
|
|
468
|
+
rawTitle: retainedRawTitle,
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
const overriddenKind = hooks.permissionRequestKind?.({
|
|
474
|
+
title: retainedRawTitle,
|
|
475
|
+
kind: parsed.kind,
|
|
476
|
+
rawInput: parsed.toolCall?.data["rawInput"],
|
|
477
|
+
});
|
|
478
|
+
const permissionRequest =
|
|
479
|
+
overriddenKind === undefined ? parsed : { ...parsed, kind: overriddenKind };
|
|
480
|
+
if (options.autoApprove?.(permissionRequest) === true) {
|
|
481
|
+
// 选项表认不出唯一的「允许」项就照常弹给用户,不替它猜。
|
|
482
|
+
const optionId = selectAutoApprovedAcpPermissionOption(params);
|
|
483
|
+
if (optionId !== undefined) {
|
|
484
|
+
if (hasOwnMutation) {
|
|
485
|
+
supersedePermissionTrackedState(trackingKey);
|
|
486
|
+
}
|
|
487
|
+
hooks.onPermissionRequestSettled?.({
|
|
488
|
+
sessionId: params.sessionId,
|
|
489
|
+
toolCallId: params.toolCall.toolCallId,
|
|
490
|
+
accepted: true,
|
|
491
|
+
requestId: settlementId,
|
|
492
|
+
});
|
|
493
|
+
return { autoApprovedOptionId: optionId } as const;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
const permissionContext = {
|
|
497
|
+
request: params,
|
|
498
|
+
permissionRequest,
|
|
499
|
+
previousToolCall: effectiveTracked?.state,
|
|
500
|
+
};
|
|
501
|
+
const statement =
|
|
502
|
+
hooks.permissionStatement?.(permissionContext) ??
|
|
503
|
+
defaultPermissionStatement(permissionRequest);
|
|
504
|
+
const fileChanges =
|
|
505
|
+
hooks.permissionFileChanges?.(permissionContext) ??
|
|
506
|
+
fileChangesFromAcpPermission(permissionRequest);
|
|
507
|
+
const requestId = randomId();
|
|
508
|
+
const openedEvent = makeAcpRequestOpenedEvent({
|
|
238
509
|
stamp: stamp(),
|
|
239
510
|
provider: options.provider,
|
|
240
511
|
threadId: options.threadId,
|
|
@@ -249,8 +520,62 @@ export function createExecutorRequests({
|
|
|
249
520
|
source: "acp.jsonrpc",
|
|
250
521
|
method: "session/request_permission",
|
|
251
522
|
rawPayload: params,
|
|
252
|
-
})
|
|
253
|
-
|
|
523
|
+
});
|
|
524
|
+
return { requestId, permissionRequest, openedEvent } as const;
|
|
525
|
+
} catch (error) {
|
|
526
|
+
if (mutatedToolCall) {
|
|
527
|
+
restoreTrackedState(trackingKey, immediateHadPrevious, immediatePrevious);
|
|
528
|
+
}
|
|
529
|
+
hooks.onPermissionRequestSettled?.({
|
|
530
|
+
sessionId: params.sessionId,
|
|
531
|
+
toolCallId: params.toolCall.toolCallId,
|
|
532
|
+
accepted: false,
|
|
533
|
+
requestId: settlementId,
|
|
534
|
+
});
|
|
535
|
+
throw error;
|
|
536
|
+
}
|
|
537
|
+
})();
|
|
538
|
+
if ("autoApprovedOptionId" in prepared) {
|
|
539
|
+
return { outcome: { outcome: "selected", optionId: prepared.autoApprovedOptionId } };
|
|
540
|
+
}
|
|
541
|
+
const hadApplied = toolCalls.has(trackingKey);
|
|
542
|
+
const appliedTracked = toolCalls.get(trackingKey);
|
|
543
|
+
const { requestId, permissionRequest, openedEvent } = prepared;
|
|
544
|
+
const outcome = await new Promise<PermissionOutcome>((resolve, reject) => {
|
|
545
|
+
const pending: PendingApproval = {
|
|
546
|
+
requestId,
|
|
547
|
+
settlementId,
|
|
548
|
+
request: params,
|
|
549
|
+
permissionRequest,
|
|
550
|
+
turnId: shared.activeTurnId,
|
|
551
|
+
settled: false,
|
|
552
|
+
resolve,
|
|
553
|
+
};
|
|
554
|
+
if (mutatedToolCall) {
|
|
555
|
+
recordPermissionMutation({
|
|
556
|
+
requestId,
|
|
557
|
+
trackingKey,
|
|
558
|
+
hasOwnMutation,
|
|
559
|
+
hadPrevious: immediateHadPrevious,
|
|
560
|
+
previousTracked: immediatePrevious,
|
|
561
|
+
hadApplied,
|
|
562
|
+
appliedTracked,
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
approvals.add(pending);
|
|
566
|
+
try {
|
|
567
|
+
emit(openedEvent);
|
|
568
|
+
} catch (error) {
|
|
569
|
+
reject(error);
|
|
570
|
+
approvals.settle(pending, { outcome: "cancelled" });
|
|
571
|
+
releasePermissionTrackedState(requestId, false);
|
|
572
|
+
hooks.onPermissionRequestSettled?.({
|
|
573
|
+
sessionId: params.sessionId,
|
|
574
|
+
toolCallId: params.toolCall.toolCallId,
|
|
575
|
+
accepted: false,
|
|
576
|
+
requestId: settlementId,
|
|
577
|
+
});
|
|
578
|
+
}
|
|
254
579
|
});
|
|
255
580
|
return { outcome };
|
|
256
581
|
};
|
|
@@ -319,5 +644,7 @@ export function createExecutorRequests({
|
|
|
319
644
|
handlePermissionRequest,
|
|
320
645
|
respondToRequest,
|
|
321
646
|
respondToUserInput,
|
|
647
|
+
supersedePermissionTrackedState,
|
|
648
|
+
runWithCommittedPermissionBaseline,
|
|
322
649
|
};
|
|
323
650
|
}
|
|
@@ -8,13 +8,20 @@ import {
|
|
|
8
8
|
makeAcpPlanUpdatedEvent,
|
|
9
9
|
makeAcpToolCallEvent,
|
|
10
10
|
retainToolCallUpdate,
|
|
11
|
+
toolCallTrackingKey,
|
|
11
12
|
} from "../events.ts";
|
|
12
13
|
import { adoptReportedConfigOptions } from "../model-selection.ts";
|
|
13
14
|
import { type SessionNotification } from "../protocol.ts";
|
|
14
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
parseAvailableCommandsUpdate,
|
|
17
|
+
parseSessionUpdateEvent,
|
|
18
|
+
type AcpToolCallState,
|
|
19
|
+
} from "../runtime-model.ts";
|
|
15
20
|
import { createToolSubagentTracker, type AcpToolSubagentSettlement } from "../tool-subagents.ts";
|
|
21
|
+
import { isRecord } from "../util.ts";
|
|
16
22
|
import type { AcpObservedEvent } from "../vendor-hooks.ts";
|
|
17
23
|
import { createSubagentSessionTracker } from "./subagent-sessions.ts";
|
|
24
|
+
import { makeToolCallState } from "../tool-call-state.ts";
|
|
18
25
|
import type { createExecutorRuntime } from "./runtime.ts";
|
|
19
26
|
import type { AcpExecutorSupport, CompactionRecord } from "./support.ts";
|
|
20
27
|
|
|
@@ -35,6 +42,8 @@ export function createExecutorUpdates({
|
|
|
35
42
|
closeActiveAssistantSegment,
|
|
36
43
|
ensureActiveAssistantSegment,
|
|
37
44
|
shared,
|
|
45
|
+
supersedePermissionTrackedState,
|
|
46
|
+
runWithCommittedPermissionBaseline,
|
|
38
47
|
}: Pick<
|
|
39
48
|
AcpExecutorSupport,
|
|
40
49
|
"options" | "nowMs" | "compactions" | "randomId" | "toolCalls" | "hooks" | "shared"
|
|
@@ -46,7 +55,18 @@ export function createExecutorUpdates({
|
|
|
46
55
|
| "emitArtifacts"
|
|
47
56
|
| "closeActiveAssistantSegment"
|
|
48
57
|
| "ensureActiveAssistantSegment"
|
|
49
|
-
>
|
|
58
|
+
> & {
|
|
59
|
+
readonly supersedePermissionTrackedState?: (
|
|
60
|
+
trackingKey: string,
|
|
61
|
+
options?: { readonly restoreCommitted?: boolean },
|
|
62
|
+
) => void;
|
|
63
|
+
readonly runWithCommittedPermissionBaseline?: <T>(
|
|
64
|
+
trackingKey: string,
|
|
65
|
+
applyUpdate: () => { readonly nextToolCall: AcpToolCallState; readonly reset: boolean } & T,
|
|
66
|
+
passiveNextState: AcpToolCallState,
|
|
67
|
+
rawTitle?: unknown,
|
|
68
|
+
) => { readonly nextToolCall: AcpToolCallState; readonly reset: boolean } & T;
|
|
69
|
+
}) {
|
|
50
70
|
/** 已经发过工具事件的调用 id,有界。晚到的更新靠它认出「不是新调用」。 */
|
|
51
71
|
const shownToolCallIds = new Set<string>();
|
|
52
72
|
const markChildSession = shared.childSessions.mark;
|
|
@@ -218,7 +238,7 @@ export function createExecutorUpdates({
|
|
|
218
238
|
markChildSession,
|
|
219
239
|
});
|
|
220
240
|
|
|
221
|
-
const handleSessionUpdate = (params: SessionNotification): void => {
|
|
241
|
+
const handleSessionUpdate = (params: SessionNotification, rawWireUpdate?: unknown): void => {
|
|
222
242
|
const update = params.update as unknown as Record<string, unknown>;
|
|
223
243
|
const reportedOptions = update["configOptions"];
|
|
224
244
|
if (update["sessionUpdate"] === "config_option_update" && Array.isArray(reportedOptions)) {
|
|
@@ -254,11 +274,77 @@ export function createExecutorUpdates({
|
|
|
254
274
|
}
|
|
255
275
|
for (const event of parsed.events) {
|
|
256
276
|
if (event._tag === "ToolCallUpdated") {
|
|
257
|
-
const
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
277
|
+
const isNewCall =
|
|
278
|
+
(params.update as { readonly sessionUpdate?: unknown }).sessionUpdate === "tool_call";
|
|
279
|
+
const shownKey = toolCallTrackingKey(params.sessionId, event.toolCall.toolCallId);
|
|
280
|
+
const wireRecord = isRecord(rawWireUpdate) ? rawWireUpdate : undefined;
|
|
281
|
+
const wireMeta =
|
|
282
|
+
wireRecord !== undefined && isRecord(wireRecord["_meta"])
|
|
283
|
+
? wireRecord["_meta"]
|
|
284
|
+
: undefined;
|
|
285
|
+
const wireParentMeta = isRecord((params as { readonly _meta?: unknown })._meta)
|
|
286
|
+
? (params as { readonly _meta: Record<string, unknown> })._meta
|
|
287
|
+
: undefined;
|
|
288
|
+
const hasWireIdentity =
|
|
289
|
+
wireRecord !== undefined &&
|
|
290
|
+
((typeof wireRecord["name"] === "string" && wireRecord["name"].trim().length > 0) ||
|
|
291
|
+
wireRecord["name"] === null ||
|
|
292
|
+
(typeof wireRecord["mcpServer"] === "string" &&
|
|
293
|
+
wireRecord["mcpServer"].trim().length > 0) ||
|
|
294
|
+
wireRecord["mcpServer"] === null ||
|
|
295
|
+
(typeof wireRecord["mcpTool"] === "string" &&
|
|
296
|
+
wireRecord["mcpTool"].trim().length > 0) ||
|
|
297
|
+
wireRecord["mcpTool"] === null ||
|
|
298
|
+
(typeof wireRecord["kind"] === "string" &&
|
|
299
|
+
wireRecord["kind"].trim().length > 0 &&
|
|
300
|
+
wireRecord["kind"].trim() !== "other") ||
|
|
301
|
+
wireMeta?.["is_mcp_tool_call"] === false ||
|
|
302
|
+
wireParentMeta?.["is_mcp_tool_call"] === false);
|
|
303
|
+
const rawTitle = (params.update as { readonly title?: unknown }).title;
|
|
304
|
+
const supersedesPendingPermission =
|
|
305
|
+
isNewCall ||
|
|
306
|
+
event.toolCall.status === "completed" ||
|
|
307
|
+
event.toolCall.status === "failed" ||
|
|
308
|
+
hasWireIdentity;
|
|
309
|
+
if (supersedesPendingPermission) {
|
|
310
|
+
supersedePermissionTrackedState?.(shownKey, { restoreCommitted: true });
|
|
311
|
+
}
|
|
312
|
+
const runRetain = () =>
|
|
313
|
+
retainToolCallUpdate(toolCalls, event.toolCall, {
|
|
314
|
+
sessionId: params.sessionId,
|
|
315
|
+
turnId: shared.activeTurnId,
|
|
316
|
+
rawTitle,
|
|
317
|
+
isNewCall,
|
|
318
|
+
nowMs,
|
|
319
|
+
});
|
|
320
|
+
const updateRecord = params.update as unknown as Record<string, unknown>;
|
|
321
|
+
const passiveRawTitle =
|
|
322
|
+
wireRecord !== undefined ? wireRecord["title"] : updateRecord["title"];
|
|
323
|
+
const passiveWireState =
|
|
324
|
+
!supersedesPendingPermission && runWithCommittedPermissionBaseline !== undefined
|
|
325
|
+
? makeToolCallState({
|
|
326
|
+
toolCallId: event.toolCall.toolCallId,
|
|
327
|
+
title: typeof passiveRawTitle === "string" ? passiveRawTitle : undefined,
|
|
328
|
+
status: updateRecord["status"],
|
|
329
|
+
rawInput: updateRecord["rawInput"],
|
|
330
|
+
rawOutput: updateRecord["rawOutput"],
|
|
331
|
+
content: updateRecord["content"],
|
|
332
|
+
locations: updateRecord["locations"],
|
|
333
|
+
})
|
|
334
|
+
: undefined;
|
|
335
|
+
const { nextToolCall, decision, reset } =
|
|
336
|
+
!supersedesPendingPermission && runWithCommittedPermissionBaseline !== undefined
|
|
337
|
+
? runWithCommittedPermissionBaseline(
|
|
338
|
+
shownKey,
|
|
339
|
+
runRetain,
|
|
340
|
+
passiveWireState ?? event.toolCall,
|
|
341
|
+
rawTitle,
|
|
342
|
+
)
|
|
343
|
+
: runRetain();
|
|
344
|
+
if (reset) {
|
|
345
|
+
supersedePermissionTrackedState?.(shownKey);
|
|
346
|
+
shownToolCallIds.delete(shownKey);
|
|
347
|
+
}
|
|
262
348
|
// 后台任务(监视器、后台 shell)的生命周期只在工具结果里:不论这次更新发不发工具事件,都要过一遍,轮询通知常常只带 rawOutput。
|
|
263
349
|
const hasToolHooks = toolSubagents !== undefined || hooks.onToolCallUpdated !== undefined;
|
|
264
350
|
const toolName =
|
|
@@ -296,8 +382,8 @@ export function createExecutorUpdates({
|
|
|
296
382
|
shared.activeTurnId;
|
|
297
383
|
// 当前轮的新工具调用才是回答里的分界:已经露过面的调用再报进度(比如后台命令跑完)、
|
|
298
384
|
// 属于更早一轮的晚到结果都不是,不能把这一轮的回答从中间劈开。
|
|
299
|
-
if (!shownToolCallIds.has(
|
|
300
|
-
rememberShownToolCall(
|
|
385
|
+
if (isNewCall || !shownToolCallIds.has(shownKey)) {
|
|
386
|
+
rememberShownToolCall(shownKey);
|
|
301
387
|
if (toolTurnId === shared.activeTurnId) closeActiveAssistantSegment(shared.activeTurnId);
|
|
302
388
|
}
|
|
303
389
|
emit(
|
|
@@ -406,7 +492,7 @@ export function createExecutorUpdates({
|
|
|
406
492
|
* 「别的会话」= 已知的子会话,或本执行器从没开过的会话;有开会话请求在途时(新会话 id 还不知道),
|
|
407
493
|
* 没见过的 id 不分辨,已知子会话照旧隔离。
|
|
408
494
|
*/
|
|
409
|
-
const receiveSessionUpdate = (params: SessionNotification): void => {
|
|
495
|
+
const receiveSessionUpdate = (params: SessionNotification, rawWireUpdate?: unknown): void => {
|
|
410
496
|
const foreign =
|
|
411
497
|
!shared.ownSessionIds.has(params.sessionId) &&
|
|
412
498
|
(shared.childSessions.has(params.sessionId) ||
|
|
@@ -425,7 +511,7 @@ export function createExecutorUpdates({
|
|
|
425
511
|
emitHookEvents(hooks.onSessionUpdate(params, { ...observeContext(), foreign }));
|
|
426
512
|
}
|
|
427
513
|
}
|
|
428
|
-
if (!foreign) handleSessionUpdate(params);
|
|
514
|
+
if (!foreign) handleSessionUpdate(params, rawWireUpdate);
|
|
429
515
|
};
|
|
430
516
|
|
|
431
517
|
/** 不是收口信号的扩展通知;重放窗口内不交给钩子。 */
|