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.
Files changed (49) hide show
  1. package/dist/adapter-support.d.ts.map +1 -1
  2. package/dist/adapter-support.js +12 -3
  3. package/dist/adapter-support.js.map +1 -1
  4. package/dist/approvals.d.ts +1 -0
  5. package/dist/approvals.d.ts.map +1 -1
  6. package/dist/approvals.js.map +1 -1
  7. package/dist/events.d.ts +18 -0
  8. package/dist/events.d.ts.map +1 -1
  9. package/dist/events.js +226 -40
  10. package/dist/events.js.map +1 -1
  11. package/dist/executor/requests.d.ts +12 -1
  12. package/dist/executor/requests.d.ts.map +1 -1
  13. package/dist/executor/requests.js +341 -45
  14. package/dist/executor/requests.js.map +1 -1
  15. package/dist/executor/updates.d.ts +14 -2
  16. package/dist/executor/updates.d.ts.map +1 -1
  17. package/dist/executor/updates.js +65 -10
  18. package/dist/executor/updates.js.map +1 -1
  19. package/dist/executor.d.ts.map +1 -1
  20. package/dist/executor.js +16 -13
  21. package/dist/executor.js.map +1 -1
  22. package/dist/index.d.ts +3 -2
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +2 -2
  25. package/dist/index.js.map +1 -1
  26. package/dist/runtime-model.d.ts.map +1 -1
  27. package/dist/runtime-model.js +54 -10
  28. package/dist/runtime-model.js.map +1 -1
  29. package/dist/source/adapter-support.ts +15 -3
  30. package/dist/source/approvals.ts +1 -0
  31. package/dist/source/events.ts +277 -41
  32. package/dist/source/executor/requests.ts +372 -45
  33. package/dist/source/executor/updates.ts +98 -12
  34. package/dist/source/executor.ts +25 -20
  35. package/dist/source/index.ts +4 -1
  36. package/dist/source/runtime-model.ts +75 -10
  37. package/dist/source/tool-call-state.ts +171 -4
  38. package/dist/source/types.ts +6 -1
  39. package/dist/source/vendor-hooks.ts +19 -0
  40. package/dist/tool-call-state.d.ts +27 -0
  41. package/dist/tool-call-state.d.ts.map +1 -1
  42. package/dist/tool-call-state.js +113 -4
  43. package/dist/tool-call-state.js.map +1 -1
  44. package/dist/types.d.ts +6 -1
  45. package/dist/types.d.ts.map +1 -1
  46. package/dist/vendor-hooks.d.ts +18 -0
  47. package/dist/vendor-hooks.d.ts.map +1 -1
  48. package/dist/vendor-hooks.js.map +1 -1
  49. package/package.json +4 -4
@@ -96,8 +96,20 @@ export function acpPromptSettlementBelongsToContext(input: {
96
96
  );
97
97
  }
98
98
 
99
+ function isAcpPermissionOption(value: unknown): value is AcpPermissionOption {
100
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
101
+ return false;
102
+ }
103
+ const option = value as Record<string, unknown>;
104
+ return (
105
+ typeof option["optionId"] === "string" &&
106
+ typeof option["name"] === "string" &&
107
+ typeof option["kind"] === "string"
108
+ );
109
+ }
110
+
99
111
  export function isAcpRequestPermissionParams(value: unknown): value is AcpRequestPermissionParams {
100
- if (typeof value !== "object" || value === null) {
112
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
101
113
  return false;
102
114
  }
103
115
  const record = value as Record<string, unknown>;
@@ -105,11 +117,11 @@ export function isAcpRequestPermissionParams(value: unknown): value is AcpReques
105
117
  return false;
106
118
  }
107
119
  const toolCall = record["toolCall"];
108
- if (typeof toolCall !== "object" || toolCall === null) {
120
+ if (typeof toolCall !== "object" || toolCall === null || Array.isArray(toolCall)) {
109
121
  return false;
110
122
  }
111
123
  if (typeof (toolCall as Record<string, unknown>)["toolCallId"] !== "string") {
112
124
  return false;
113
125
  }
114
- return Array.isArray(record["options"]);
126
+ return Array.isArray(record["options"]) && record["options"].every(isAcpPermissionOption);
115
127
  }
@@ -7,6 +7,7 @@ export type PermissionOutcome =
7
7
 
8
8
  export type PendingApproval = {
9
9
  readonly requestId: string;
10
+ readonly settlementId?: string;
10
11
  readonly request: AcpRequestPermissionParams;
11
12
  readonly permissionRequest: AcpPermissionRequest;
12
13
  readonly turnId: string | undefined;
@@ -9,6 +9,7 @@ import {
9
9
  type AcpToolCallEmitDecision,
10
10
  type AcpToolCallState,
11
11
  } from "./runtime-model.ts";
12
+ import { readToolCallIdentityFields, toolCallIdentityChanged } from "./tool-call-state.ts";
12
13
  import { canonicalItemTypeFromAcpToolKind } from "./tool-presentation.ts";
13
14
  import type { AcpRequestStatement } from "./permission-statement.ts";
14
15
  import type {
@@ -39,11 +40,66 @@ export function canonicalRequestTypeFromAcpKind(kind: string | "unknown"): AcpCa
39
40
  }
40
41
  }
41
42
 
43
+ const MAX_ACP_FILE_CHANGES = 256;
44
+ const MAX_ACP_FILE_CHANGE_PATH_CHARS = 4_096;
45
+ const MAX_DISPLAY_FILE_CHANGES = 20;
46
+
47
+ const isValidAcpFilePath = (value: unknown): value is string =>
48
+ typeof value === "string" &&
49
+ value.trim().length > 0 &&
50
+ value.length <= MAX_ACP_FILE_CHANGE_PATH_CHARS;
51
+
52
+ const isTruncatedFileChangesMarker = (value: unknown): boolean =>
53
+ value !== null &&
54
+ typeof value === "object" &&
55
+ (value as Record<string, unknown>)["ycodiumFileChangesTruncated"] === true;
56
+
57
+ export interface AcpClippedDiffStats {
58
+ readonly additions: number;
59
+ readonly deletions: number;
60
+ }
61
+
62
+ const clippedDiffBlocks = new WeakMap<object, AcpClippedDiffStats>();
63
+
64
+ /** 在内存里记录某份被裁剪的 diff 块及其裁剪前行数,避免依赖可由线上 JSON 伪造的属性键。 */
65
+ export function markAcpDiffTooLarge<T extends object>(block: T, stats: AcpClippedDiffStats): T {
66
+ clippedDiffBlocks.set(block, stats);
67
+ return block;
68
+ }
69
+
70
+ export function readAcpDiffTooLarge(block: unknown): AcpClippedDiffStats | undefined {
71
+ return block !== null && typeof block === "object" ? clippedDiffBlocks.get(block) : undefined;
72
+ }
73
+
74
+ function acpFileChangeFromDiffBlock(block: Record<string, unknown>, path: string): AcpFileChange {
75
+ const oldText = typeof block["oldText"] === "string" ? block["oldText"] : undefined;
76
+ const changeKind = oldText === undefined ? ("add" as const) : ("modify" as const);
77
+ const clipped = readAcpDiffTooLarge(block);
78
+ if (clipped !== undefined) {
79
+ return {
80
+ path,
81
+ changeKind,
82
+ additions: clipped.additions,
83
+ deletions: clipped.deletions,
84
+ omitted: "too-large" as const,
85
+ };
86
+ }
87
+ return {
88
+ path,
89
+ changeKind,
90
+ content: {
91
+ kind: "replace" as const,
92
+ ...(oldText !== undefined ? { oldText } : {}),
93
+ newText: block["newText"] as string,
94
+ },
95
+ };
96
+ }
97
+
42
98
  export function fileChangesFromAcpPermission(
43
99
  permissionRequest: AcpPermissionRequest,
44
100
  ): ReadonlyArray<AcpFileChange> | undefined {
45
101
  const content = permissionRequest.toolCall?.data["content"];
46
- const changes = Array.isArray(content)
102
+ const changes: AcpFileChange[] = Array.isArray(content)
47
103
  ? content.flatMap((entry) => {
48
104
  if (entry === null || typeof entry !== "object") {
49
105
  return [];
@@ -51,42 +107,139 @@ export function fileChangesFromAcpPermission(
51
107
  const block = entry as Record<string, unknown>;
52
108
  if (
53
109
  block["type"] !== "diff" ||
54
- typeof block["path"] !== "string" ||
110
+ !isValidAcpFilePath(block["path"]) ||
55
111
  typeof block["newText"] !== "string"
56
112
  ) {
57
113
  return [];
58
114
  }
59
- const oldText = typeof block["oldText"] === "string" ? block["oldText"] : undefined;
60
- return [
61
- {
62
- path: block["path"],
63
- changeKind: oldText === undefined ? ("add" as const) : ("modify" as const),
64
- content: {
65
- kind: "replace" as const,
66
- ...(oldText !== undefined ? { oldText } : {}),
67
- newText: block["newText"],
68
- },
69
- },
70
- ];
115
+ return [acpFileChangeFromDiffBlock(block, block["path"])];
71
116
  })
72
117
  : [];
73
- if (changes.length > 0) {
74
- return changes;
118
+ const boundedChanges =
119
+ changes.length > MAX_ACP_FILE_CHANGES ? changes.slice(0, MAX_ACP_FILE_CHANGES) : changes;
120
+ const requestType = canonicalRequestTypeFromAcpKind(permissionRequest.kind);
121
+ if (
122
+ boundedChanges.length > 0 ||
123
+ requestType === "file_change_approval" ||
124
+ requestType === "file_read_approval"
125
+ ) {
126
+ const locations = permissionRequest.toolCall?.data["locations"];
127
+ const seenPaths = new Set(boundedChanges.map((change) => change.path));
128
+ if (Array.isArray(locations)) {
129
+ for (const entry of locations) {
130
+ if (boundedChanges.length >= MAX_ACP_FILE_CHANGES) break;
131
+ const rawPath = (entry as Record<string, unknown> | null)?.["path"];
132
+ if (!isValidAcpFilePath(rawPath)) continue;
133
+ if (!seenPaths.has(rawPath)) {
134
+ seenPaths.add(rawPath);
135
+ boundedChanges.push({ path: rawPath, omitted: "unavailable" as const });
136
+ }
137
+ }
138
+ }
139
+ }
140
+ return boundedChanges.length > 0 ? boundedChanges : undefined;
141
+ }
142
+
143
+ export function inspectAcpPermissionFileChanges(
144
+ permissionRequest: AcpPermissionRequest,
145
+ inputFileChanges: ReadonlyArray<AcpFileChange> | undefined,
146
+ ): {
147
+ readonly fileChanges: ReadonlyArray<AcpFileChange> | undefined;
148
+ readonly fileChangesTruncated: boolean;
149
+ } {
150
+ const merged: AcpFileChange[] = [];
151
+ let fileChangesTruncated = false;
152
+ for (const entry of inputFileChanges ?? []) {
153
+ if (!isValidAcpFilePath(entry.path) || merged.length >= MAX_ACP_FILE_CHANGES) {
154
+ fileChangesTruncated = true;
155
+ continue;
156
+ }
157
+ merged.push(entry);
75
158
  }
159
+ const seenPaths = new Set(merged.map((change) => change.path));
76
160
 
77
- if (canonicalRequestTypeFromAcpKind(permissionRequest.kind) !== "file_change_approval") {
78
- return undefined;
161
+ const content = permissionRequest.toolCall?.data["content"];
162
+ if (Array.isArray(content)) {
163
+ let diffCount = 0;
164
+ for (const entry of content) {
165
+ if (isTruncatedFileChangesMarker(entry)) {
166
+ fileChangesTruncated = true;
167
+ continue;
168
+ }
169
+ if (entry === null || typeof entry !== "object") continue;
170
+ const block = entry as Record<string, unknown>;
171
+ if (block["type"] !== "diff") continue;
172
+ diffCount += 1;
173
+ const diffPath = block["path"];
174
+ const newText = block["newText"];
175
+ if (
176
+ diffCount > MAX_ACP_FILE_CHANGES ||
177
+ !isValidAcpFilePath(diffPath) ||
178
+ typeof newText !== "string"
179
+ ) {
180
+ fileChangesTruncated = true;
181
+ continue;
182
+ }
183
+ if (!seenPaths.has(diffPath)) {
184
+ if (merged.length >= MAX_ACP_FILE_CHANGES) {
185
+ fileChangesTruncated = true;
186
+ continue;
187
+ }
188
+ seenPaths.add(diffPath);
189
+ merged.push(acpFileChangeFromDiffBlock(block, diffPath));
190
+ }
191
+ }
79
192
  }
193
+
194
+ const requestType = canonicalRequestTypeFromAcpKind(permissionRequest.kind);
80
195
  const locations = permissionRequest.toolCall?.data["locations"];
81
- const paths = Array.isArray(locations)
82
- ? locations.flatMap((entry) => {
83
- const path = (entry as Record<string, unknown> | null)?.["path"];
84
- return typeof path === "string" && path.trim().length > 0 ? [path] : [];
85
- })
86
- : [];
87
- return paths.length > 0
88
- ? paths.map((path) => ({ path, omitted: "unavailable" as const }))
89
- : undefined;
196
+ const hasLocationTruncationMarker =
197
+ Array.isArray(locations) && locations.some(isTruncatedFileChangesMarker);
198
+ if (
199
+ Array.isArray(locations) &&
200
+ (merged.length > 0 ||
201
+ requestType === "file_change_approval" ||
202
+ requestType === "file_read_approval" ||
203
+ hasLocationTruncationMarker)
204
+ ) {
205
+ let locationEntries = 0;
206
+ for (const entry of locations) {
207
+ if (isTruncatedFileChangesMarker(entry)) {
208
+ fileChangesTruncated = true;
209
+ continue;
210
+ }
211
+ locationEntries += 1;
212
+ if (locationEntries > MAX_ACP_FILE_CHANGES) {
213
+ fileChangesTruncated = true;
214
+ continue;
215
+ }
216
+ const rawPath =
217
+ entry !== null && typeof entry === "object"
218
+ ? (entry as Record<string, unknown>)["path"]
219
+ : undefined;
220
+ if (!isValidAcpFilePath(rawPath)) {
221
+ fileChangesTruncated = true;
222
+ continue;
223
+ }
224
+ if (!seenPaths.has(rawPath)) {
225
+ if (merged.length >= MAX_ACP_FILE_CHANGES) {
226
+ fileChangesTruncated = true;
227
+ continue;
228
+ }
229
+ seenPaths.add(rawPath);
230
+ merged.push({ path: rawPath, omitted: "unavailable" });
231
+ }
232
+ }
233
+ }
234
+
235
+ if (merged.length > MAX_DISPLAY_FILE_CHANGES) {
236
+ fileChangesTruncated = true;
237
+ }
238
+
239
+ return {
240
+ fileChanges: merged.length > 0 ? merged : undefined,
241
+ fileChangesTruncated,
242
+ };
90
243
  }
91
244
 
92
245
  function runtimeItemStatusFromAcpToolStatus(
@@ -122,8 +275,54 @@ export function makeAcpRequestOpenedEvent(input: {
122
275
  readonly rawPayload: unknown;
123
276
  }): ExecutorEvent {
124
277
  const requestType = canonicalRequestTypeFromAcpKind(input.permissionRequest.kind);
278
+ const isDynamicToolKind =
279
+ input.permissionRequest.kind === "other" || input.permissionRequest.kind === "unknown";
280
+ const cleared = input.permissionRequest.toolCall
281
+ ? readToolCallIdentityFields(input.permissionRequest.toolCall)
282
+ : undefined;
283
+ const rawRecord =
284
+ input.rawPayload !== null && typeof input.rawPayload === "object"
285
+ ? (input.rawPayload as Record<string, unknown>)
286
+ : undefined;
287
+ const rawToolCall =
288
+ rawRecord !== undefined &&
289
+ rawRecord["toolCall"] !== null &&
290
+ typeof rawRecord["toolCall"] === "object"
291
+ ? (rawRecord["toolCall"] as Record<string, unknown>)
292
+ : undefined;
293
+ const rawToolMeta =
294
+ rawToolCall !== undefined &&
295
+ rawToolCall["_meta"] !== null &&
296
+ typeof rawToolCall["_meta"] === "object"
297
+ ? (rawToolCall["_meta"] as Record<string, unknown>)
298
+ : undefined;
299
+ const rawParentMeta =
300
+ rawRecord !== undefined && rawRecord["_meta"] !== null && typeof rawRecord["_meta"] === "object"
301
+ ? (rawRecord["_meta"] as Record<string, unknown>)
302
+ : undefined;
303
+ const negativeMcp =
304
+ cleared?.negativeMcp === true ||
305
+ cleared?.mcpServer === null ||
306
+ cleared?.mcpTool === null ||
307
+ rawToolCall?.["mcpServer"] === null ||
308
+ rawToolCall?.["mcpTool"] === null ||
309
+ rawToolMeta?.["is_mcp_tool_call"] === false ||
310
+ rawParentMeta?.["is_mcp_tool_call"] === false;
311
+ const candidateToolName =
312
+ requestType === "dynamic_tool_call" &&
313
+ isDynamicToolKind &&
314
+ typeof input.permissionRequest.toolCall?.data["name"] === "string"
315
+ ? input.permissionRequest.toolCall.data["name"]
316
+ : undefined;
125
317
  const toolName =
126
- requestType === "dynamic_tool_call" ? input.permissionRequest.toolCall?.title : undefined;
318
+ candidateToolName !== undefined &&
319
+ !(negativeMcp && /^(?:(?:Running|Run)\s+)?mcp_/u.test(candidateToolName.trim()))
320
+ ? candidateToolName
321
+ : undefined;
322
+ const { fileChanges, fileChangesTruncated } = inspectAcpPermissionFileChanges(
323
+ input.permissionRequest,
324
+ input.fileChanges,
325
+ );
127
326
  return {
128
327
  type: "request.opened",
129
328
  ...input.stamp,
@@ -138,7 +337,8 @@ export function makeAcpRequestOpenedEvent(input: {
138
337
  args: input.args,
139
338
  ...(input.statement !== undefined ? { statement: input.statement } : {}),
140
339
  ...(input.options ? { options: input.options } : {}),
141
- ...(input.fileChanges ? { fileChanges: input.fileChanges } : {}),
340
+ ...(fileChanges ? { fileChanges } : {}),
341
+ ...(fileChangesTruncated ? { fileChangesTruncated: true as const } : {}),
142
342
  declineReasonRelay: "queued",
143
343
  },
144
344
  raw: {
@@ -208,7 +408,10 @@ export function makeAcpToolCallEvent(input: {
208
408
  readonly rawPayload: unknown;
209
409
  }): ExecutorEvent {
210
410
  const runtimeStatus = runtimeItemStatusFromAcpToolStatus(input.toolCall.status);
211
- const { data } = input.toolCall;
411
+ const rawData = input.toolCall.data;
412
+ const data = readToolCallIdentityFields(input.toolCall).negativeMcp
413
+ ? { ...rawData, mcpServer: null, mcpTool: null }
414
+ : rawData;
212
415
  return {
213
416
  type:
214
417
  input.toolCall.status === "completed" || input.toolCall.status === "failed"
@@ -289,6 +492,8 @@ export type AcpToolCallTrackedState = {
289
492
  readonly state: AcpToolCallState;
290
493
  readonly lastEmittedDetailLength: number | undefined;
291
494
  readonly skippedSinceEmit: number;
495
+ /** 这条调用所属的 ACP sessionId(多会话共享同一连接时与 toolCallId 联合隔离)。 */
496
+ readonly sessionId?: string | undefined;
292
497
  /** 第一次见到这条调用时所在的轮。 */
293
498
  readonly turnId: string | undefined;
294
499
  /** 第一次进入运行态的时刻;通知里没有可靠的开始时刻,取收到那一刻,之后不改。 */
@@ -297,6 +502,11 @@ export type AcpToolCallTrackedState = {
297
502
  readonly rawTitle: string | undefined;
298
503
  };
299
504
 
505
+ /** 用 `(sessionId, toolCallId)` 区分同一连接上的父会话与子会话工具调用。 */
506
+ export function toolCallTrackingKey(sessionId: string | undefined, toolCallId: string): string {
507
+ return sessionId === undefined ? toolCallId : `${sessionId}\0${toolCallId}`;
508
+ }
509
+
300
510
  /**
301
511
  * 终态从这张表去掉。不改事件本身。`seen` 是这条通知带来的、状态里没有的事实;只在第一次进入
302
512
  * 运行态时调用 `nowMs`。
@@ -305,37 +515,63 @@ export function retainToolCallUpdate(
305
515
  toolCalls: Map<string, AcpToolCallTrackedState>,
306
516
  next: AcpToolCallState,
307
517
  seen: {
518
+ readonly sessionId?: string | undefined;
308
519
  readonly turnId: string | undefined;
309
520
  readonly rawTitle: unknown;
521
+ readonly isNewCall?: boolean | undefined;
310
522
  readonly nowMs: () => number;
311
523
  },
312
524
  ): {
313
525
  readonly nextToolCall: AcpToolCallState;
314
526
  readonly decision: AcpToolCallEmitDecision;
527
+ readonly reset: boolean;
315
528
  } {
316
- const tracked = toolCalls.get(next.toolCallId);
317
- const previous = tracked?.state;
529
+ const key = toolCallTrackingKey(seen.sessionId, next.toolCallId);
530
+ const tracked = toolCalls.get(key);
531
+ const identityChanged =
532
+ tracked !== undefined &&
533
+ toolCallIdentityChanged(
534
+ {
535
+ name: tracked.state.data["name"],
536
+ kind: tracked.state.kind,
537
+ mcpServer: tracked.state.data["mcpServer"],
538
+ mcpTool: tracked.state.data["mcpTool"],
539
+ },
540
+ readToolCallIdentityFields(next),
541
+ );
542
+ const reset = seen.isNewCall === true || identityChanged;
543
+ const effectiveTracked = reset ? undefined : tracked;
544
+ const previous = effectiveTracked?.state;
318
545
  const nextToolCall = mergeToolCallState(previous, next);
319
546
  const decision = decideToolCallUpdateEmission({
320
547
  previous,
321
548
  next: nextToolCall,
322
- lastEmittedDetailLength: tracked?.lastEmittedDetailLength,
323
- skippedSinceEmit: tracked?.skippedSinceEmit ?? 0,
549
+ lastEmittedDetailLength: effectiveTracked?.lastEmittedDetailLength,
550
+ skippedSinceEmit: effectiveTracked?.skippedSinceEmit ?? 0,
324
551
  });
325
552
  if (nextToolCall.status === "completed" || nextToolCall.status === "failed") {
326
- toolCalls.delete(nextToolCall.toolCallId);
553
+ toolCalls.delete(key);
327
554
  } else {
328
- toolCalls.set(nextToolCall.toolCallId, {
555
+ toolCalls.set(key, {
329
556
  state: nextToolCall,
330
557
  lastEmittedDetailLength: decision.emit
331
558
  ? nextToolCall.detail?.length
332
- : tracked?.lastEmittedDetailLength,
559
+ : effectiveTracked?.lastEmittedDetailLength,
333
560
  skippedSinceEmit: decision.skippedSinceEmit,
334
- turnId: tracked === undefined ? seen.turnId : tracked.turnId,
561
+ sessionId: seen.sessionId ?? effectiveTracked?.sessionId,
562
+ turnId: effectiveTracked === undefined ? seen.turnId : effectiveTracked.turnId,
335
563
  startedAtMs:
336
- tracked?.startedAtMs ?? (nextToolCall.status === "inProgress" ? seen.nowMs() : undefined),
337
- rawTitle: typeof seen.rawTitle === "string" ? seen.rawTitle : tracked?.rawTitle,
564
+ effectiveTracked?.startedAtMs ??
565
+ (nextToolCall.status === "inProgress" ? seen.nowMs() : undefined),
566
+ rawTitle:
567
+ typeof seen.rawTitle === "string"
568
+ ? seen.rawTitle
569
+ : readToolCallIdentityFields(nextToolCall).negativeMcp &&
570
+ effectiveTracked?.rawTitle !== undefined &&
571
+ /^(?:(?:Running|Run)\s+)?mcp_/u.test(effectiveTracked.rawTitle.trim())
572
+ ? undefined
573
+ : effectiveTracked?.rawTitle,
338
574
  });
339
575
  }
340
- return { nextToolCall, decision };
576
+ return { nextToolCall, decision, reset };
341
577
  }