ycodium-acp-executor 0.2.5634 → 0.2.5751
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/_private/ycodium-jsonl-stream/appendedJsonl.d.ts +47 -0
- package/dist/_private/ycodium-jsonl-stream/appendedJsonl.js +172 -0
- package/dist/_private/ycodium-jsonl-stream/claudeSessions.d.ts +63 -0
- package/dist/_private/ycodium-jsonl-stream/claudeSessions.js +220 -0
- package/dist/_private/ycodium-jsonl-stream/codexSessions.d.ts +41 -0
- package/dist/_private/ycodium-jsonl-stream/codexSessions.js +197 -0
- package/dist/_private/ycodium-jsonl-stream/history.d.ts +6 -0
- package/dist/_private/ycodium-jsonl-stream/history.js +389 -0
- package/dist/_private/ycodium-jsonl-stream/historyErrors.d.ts +6 -0
- package/dist/_private/ycodium-jsonl-stream/historyErrors.js +6 -0
- package/dist/_private/ycodium-jsonl-stream/historyFile.d.ts +19 -0
- package/dist/_private/ycodium-jsonl-stream/historyFile.js +117 -0
- package/dist/_private/ycodium-jsonl-stream/historyRecord.d.ts +31 -0
- package/dist/_private/ycodium-jsonl-stream/historyRecord.js +144 -0
- package/dist/_private/ycodium-jsonl-stream/historyWaiting.d.ts +8 -0
- package/dist/_private/ycodium-jsonl-stream/historyWaiting.js +142 -0
- package/dist/_private/ycodium-jsonl-stream/jsonObjectLine.d.ts +5 -0
- package/dist/_private/ycodium-jsonl-stream/jsonObjectLine.js +18 -0
- package/dist/_private/ycodium-jsonl-stream/jsonlStream.d.ts +79 -0
- package/dist/_private/ycodium-jsonl-stream/jsonlStream.js +603 -0
- package/dist/_private/ycodium-jsonl-stream/scanCache.d.ts +69 -0
- package/dist/_private/ycodium-jsonl-stream/scanCache.js +106 -0
- package/dist/_private/ycodium-jsonl-stream/sessionDirectory.d.ts +5 -0
- package/dist/_private/ycodium-jsonl-stream/sessionDirectory.js +32 -0
- package/dist/_private/ycodium-jsonl-stream/usageFields.d.ts +5 -0
- package/dist/_private/ycodium-jsonl-stream/usageFields.js +11 -0
- package/dist/_private/ycodium-workspace-path/directoryScope.d.ts +5 -0
- package/dist/_private/ycodium-workspace-path/directoryScope.js +28 -0
- package/dist/_private/ycodium-workspace-path/workspacePath.d.ts +19 -0
- package/dist/_private/ycodium-workspace-path/workspacePath.js +90 -0
- package/dist/adapter-support.d.ts.map +1 -1
- package/dist/adapter-support.js.map +1 -1
- package/dist/apply-effort.d.ts.map +1 -1
- package/dist/apply-effort.js.map +1 -1
- package/dist/approvals.d.ts.map +1 -1
- package/dist/approvals.js.map +1 -1
- package/dist/artifacts.d.ts.map +1 -1
- package/dist/artifacts.js.map +1 -1
- package/dist/card-defaults.d.ts.map +1 -1
- package/dist/card-defaults.js.map +1 -1
- package/dist/config-option-wiring.d.ts.map +1 -1
- package/dist/config-option-wiring.js.map +1 -1
- package/dist/config-options.d.ts.map +1 -1
- package/dist/config-options.js.map +1 -1
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js.map +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/event-stream.d.ts.map +1 -1
- package/dist/event-stream.js.map +1 -1
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js.map +1 -1
- package/dist/executor/child-sessions.d.ts.map +1 -1
- package/dist/executor/child-sessions.js.map +1 -1
- package/dist/executor/lifecycle.d.ts.map +1 -1
- package/dist/executor/lifecycle.js.map +1 -1
- package/dist/executor/prompt-results.d.ts.map +1 -1
- package/dist/executor/prompt-results.js.map +1 -1
- package/dist/executor/prompt.d.ts.map +1 -1
- package/dist/executor/prompt.js.map +1 -1
- package/dist/executor/requests.d.ts.map +1 -1
- package/dist/executor/requests.js.map +1 -1
- package/dist/executor/runtime.d.ts.map +1 -1
- package/dist/executor/runtime.js.map +1 -1
- package/dist/executor/session.d.ts.map +1 -1
- package/dist/executor/session.js +17 -1
- package/dist/executor/session.js.map +1 -1
- package/dist/executor/settlement.d.ts.map +1 -1
- package/dist/executor/settlement.js.map +1 -1
- package/dist/executor/subagent-sessions.d.ts.map +1 -1
- package/dist/executor/subagent-sessions.js.map +1 -1
- package/dist/executor/support.d.ts.map +1 -1
- package/dist/executor/support.js.map +1 -1
- package/dist/executor/updates.d.ts.map +1 -1
- package/dist/executor/updates.js.map +1 -1
- package/dist/executor.d.ts.map +1 -1
- package/dist/executor.js.map +1 -1
- package/dist/failureCategory.d.ts.map +1 -1
- package/dist/failureCategory.js.map +1 -1
- package/dist/handshake.d.ts.map +1 -1
- package/dist/handshake.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/instance-kit.d.ts.map +1 -1
- package/dist/instance-kit.js.map +1 -1
- package/dist/jsonrpc.d.ts.map +1 -1
- package/dist/jsonrpc.js.map +1 -1
- package/dist/line-buffer.d.ts.map +1 -1
- package/dist/line-buffer.js.map +1 -1
- package/dist/mcp-servers.d.ts +26 -4
- package/dist/mcp-servers.d.ts.map +1 -1
- package/dist/mcp-servers.js +25 -4
- package/dist/mcp-servers.js.map +1 -1
- package/dist/model-selection.d.ts.map +1 -1
- package/dist/model-selection.js.map +1 -1
- package/dist/model-send.d.ts.map +1 -1
- package/dist/model-send.js.map +1 -1
- package/dist/mutex.d.ts.map +1 -1
- package/dist/mutex.js.map +1 -1
- package/dist/native-session-transcript.d.ts.map +1 -1
- package/dist/native-session-transcript.js.map +1 -1
- package/dist/native-sessions.d.ts.map +1 -1
- package/dist/native-sessions.js.map +1 -1
- package/dist/openCommands.d.ts.map +1 -1
- package/dist/openCommands.js.map +1 -1
- package/dist/permission-statement.d.ts.map +1 -1
- package/dist/permission-statement.js.map +1 -1
- package/dist/probe-process.d.ts.map +1 -1
- package/dist/probe-process.js.map +1 -1
- package/dist/probe-session.d.ts.map +1 -1
- package/dist/probe-session.js.map +1 -1
- package/dist/probe.d.ts.map +1 -1
- package/dist/probe.js.map +1 -1
- package/dist/prompt-dispatch.d.ts.map +1 -1
- package/dist/prompt-dispatch.js.map +1 -1
- package/dist/protocol-log.d.ts.map +1 -1
- package/dist/protocol-log.js.map +1 -1
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js.map +1 -1
- package/dist/refreshDeadline.d.ts.map +1 -1
- package/dist/refreshDeadline.js.map +1 -1
- package/dist/runtime-mode.d.ts.map +1 -1
- package/dist/runtime-mode.js.map +1 -1
- package/dist/runtime-model.d.ts.map +1 -1
- package/dist/runtime-model.js.map +1 -1
- package/dist/session-cleanup.d.ts.map +1 -1
- package/dist/session-cleanup.js.map +1 -1
- package/dist/session-registry.d.ts.map +1 -1
- package/dist/session-registry.js.map +1 -1
- package/dist/session-update.d.ts.map +1 -1
- package/dist/session-update.js.map +1 -1
- package/dist/sessionProcess.d.ts.map +1 -1
- package/dist/sessionProcess.js.map +1 -1
- package/dist/source/_private/ycodium-executor-common/catalogModel.ts +40 -0
- package/dist/source/_private/ycodium-executor-common/eventTypes.ts +97 -0
- package/dist/source/_private/ycodium-executor-common/modelSelection.ts +24 -0
- package/dist/source/_private/ycodium-executor-common/nodeStreams.ts +212 -0
- package/dist/source/_private/ycodium-executor-common/raceAbort.ts +25 -0
- package/dist/source/_private/ycodium-executor-common/reasoningLevelFallback.ts +28 -0
- package/dist/source/_private/ycodium-executor-common/stderrTail.ts +27 -0
- package/dist/source/_private/ycodium-executor-common/toolPresentation.ts +268 -0
- package/dist/source/_private/ycodium-jsonl-stream/appendedJsonl.ts +222 -0
- package/dist/source/_private/ycodium-jsonl-stream/claudeSessions.ts +263 -0
- package/dist/source/_private/ycodium-jsonl-stream/codexSessions.ts +254 -0
- package/dist/source/_private/ycodium-jsonl-stream/history.ts +492 -0
- package/dist/source/_private/ycodium-jsonl-stream/historyErrors.ts +6 -0
- package/dist/source/_private/ycodium-jsonl-stream/historyFile.ts +139 -0
- package/dist/source/_private/ycodium-jsonl-stream/historyRecord.ts +180 -0
- package/dist/source/_private/ycodium-jsonl-stream/historyWaiting.ts +140 -0
- package/dist/source/_private/ycodium-jsonl-stream/jsonObjectLine.ts +15 -0
- package/dist/source/_private/ycodium-jsonl-stream/jsonlStream.ts +650 -0
- package/dist/source/_private/ycodium-jsonl-stream/scanCache.ts +163 -0
- package/dist/source/_private/ycodium-jsonl-stream/sessionDirectory.ts +39 -0
- package/dist/source/_private/ycodium-jsonl-stream/sessionTranscript.ts +136 -0
- package/dist/source/_private/ycodium-jsonl-stream/usageFields.ts +13 -0
- package/dist/source/_private/ycodium-workspace-path/directoryScope.ts +34 -0
- package/dist/source/_private/ycodium-workspace-path/workspacePath.ts +110 -0
- package/dist/source/adapter-support.ts +115 -0
- package/dist/source/apply-effort.ts +66 -0
- package/dist/source/approvals.ts +94 -0
- package/dist/source/artifacts.ts +88 -0
- package/dist/source/card-defaults.ts +25 -0
- package/dist/source/config-option-wiring.ts +119 -0
- package/dist/source/config-options.ts +270 -0
- package/dist/source/connection.ts +553 -0
- package/dist/source/errors.ts +207 -0
- package/dist/source/event-stream.ts +91 -0
- package/dist/source/events.ts +341 -0
- package/dist/source/executor/child-sessions.ts +87 -0
- package/dist/source/executor/lifecycle.ts +282 -0
- package/dist/source/executor/prompt-results.ts +123 -0
- package/dist/source/executor/prompt.ts +606 -0
- package/dist/source/executor/requests.ts +323 -0
- package/dist/source/executor/runtime.ts +137 -0
- package/dist/source/executor/session.ts +553 -0
- package/dist/source/executor/settlement.ts +92 -0
- package/dist/source/executor/subagent-sessions.ts +183 -0
- package/dist/source/executor/support.ts +129 -0
- package/dist/source/executor/updates.ts +427 -0
- package/dist/source/executor.ts +537 -0
- package/dist/source/failureCategory.ts +74 -0
- package/dist/source/handshake.ts +66 -0
- package/dist/source/index.ts +210 -0
- package/dist/source/instance-kit.ts +478 -0
- package/dist/source/jsonrpc.ts +59 -0
- package/dist/source/line-buffer.ts +54 -0
- package/dist/source/mcp-servers.ts +83 -0
- package/dist/source/model-selection.ts +138 -0
- package/dist/source/model-send.ts +80 -0
- package/dist/source/mutex.ts +47 -0
- package/dist/source/native-session-transcript.ts +167 -0
- package/dist/source/native-sessions.ts +271 -0
- package/dist/source/openCommands.ts +39 -0
- package/dist/source/permission-statement.ts +49 -0
- package/dist/source/probe-process.ts +95 -0
- package/dist/source/probe-session.ts +81 -0
- package/dist/source/probe.ts +261 -0
- package/dist/source/prompt-dispatch.ts +42 -0
- package/dist/source/protocol-log.ts +143 -0
- package/dist/source/protocol.ts +36 -0
- package/dist/source/refreshDeadline.ts +2 -0
- package/dist/source/runtime-mode.ts +125 -0
- package/dist/source/runtime-model.ts +598 -0
- package/dist/source/session-cleanup.ts +82 -0
- package/dist/source/session-registry.ts +62 -0
- package/dist/source/session-update.ts +88 -0
- package/dist/source/sessionProcess.ts +29 -0
- package/dist/source/start-abort.ts +19 -0
- package/dist/source/thread-operations.ts +145 -0
- package/dist/source/tool-call-state.ts +322 -0
- package/dist/source/tool-presentation.ts +20 -0
- package/dist/source/tool-subagents.ts +491 -0
- package/dist/source/turn-usage.ts +78 -0
- package/dist/source/types.ts +146 -0
- package/dist/source/user-input-pending.ts +31 -0
- package/dist/source/util.ts +38 -0
- package/dist/source/vendor-hooks.ts +394 -0
- package/dist/start-abort.d.ts.map +1 -1
- package/dist/start-abort.js.map +1 -1
- package/dist/thread-operations.d.ts.map +1 -1
- package/dist/thread-operations.js.map +1 -1
- package/dist/tool-call-state.d.ts.map +1 -1
- package/dist/tool-call-state.js.map +1 -1
- package/dist/tool-presentation.d.ts.map +1 -1
- package/dist/tool-presentation.js.map +1 -1
- package/dist/tool-subagents.d.ts +1 -0
- package/dist/tool-subagents.d.ts.map +1 -1
- package/dist/tool-subagents.js +2 -0
- package/dist/tool-subagents.js.map +1 -1
- package/dist/turn-usage.d.ts.map +1 -1
- package/dist/turn-usage.js.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/user-input-pending.d.ts.map +1 -1
- package/dist/user-input-pending.js.map +1 -1
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js.map +1 -1
- package/dist/vendor-hooks.d.ts +9 -0
- package/dist/vendor-hooks.d.ts.map +1 -1
- package/dist/vendor-hooks.js.map +1 -1
- package/package.json +5 -2
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 子会话身份登记:追踪器(`subagent_update`)与厂商钩子(`markChildSession`)共用这一份,
|
|
3
|
+
* 路由「这条更新是不是子会话发的」也问它,不另存一份会各自淘汰到分歧的集合。
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export type ChildStatus = "running" | "waiting" | "idle" | "cancelled" | "failed";
|
|
7
|
+
|
|
8
|
+
export type ChildRecord = {
|
|
9
|
+
/** 首见时的轮次;首见时没有轮次就取之后第一个有的,之后固定。 */
|
|
10
|
+
turnId: string | undefined;
|
|
11
|
+
title: string | undefined;
|
|
12
|
+
description: string | undefined;
|
|
13
|
+
readonly parentAgentId: string | undefined;
|
|
14
|
+
/** 最近一次观察到的状态(含 `session/load` 重放里的);没见过明确状态为 undefined。 */
|
|
15
|
+
observed: ChildStatus | undefined;
|
|
16
|
+
/** 已向宿主报告过的状态与错误详情;重放观察到的不算。 */
|
|
17
|
+
reported: ChildStatus | undefined;
|
|
18
|
+
reportedError: string | undefined;
|
|
19
|
+
/** 宿主已经有这一行(发过 `task.started`,或重放里恢复的):之后不再发 started。 */
|
|
20
|
+
announced: boolean;
|
|
21
|
+
/** 标题、描述变了但还没随带状态的事件发出去。 */
|
|
22
|
+
pendingMetadata: boolean;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type ChildSessionRegistry = {
|
|
26
|
+
readonly has: (sessionId: string) => boolean;
|
|
27
|
+
readonly get: (sessionId: string) => ChildRecord | undefined;
|
|
28
|
+
/** 只登记身份(厂商钩子用);已有记录不动。 */
|
|
29
|
+
readonly mark: (sessionId: string) => void;
|
|
30
|
+
/** 只接收状态已初始化的完整记录;淘汰在登记之后做,且新登记的这条本次不淘汰。 */
|
|
31
|
+
readonly add: (sessionId: string, record: ChildRecord) => void;
|
|
32
|
+
readonly entries: () => IterableIterator<[string, ChildRecord]>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const CHILD_LIMIT = 256;
|
|
36
|
+
|
|
37
|
+
const active = (status: ChildStatus | undefined): boolean =>
|
|
38
|
+
status === "running" || status === "waiting";
|
|
39
|
+
|
|
40
|
+
export function createChildSessionRegistry(limit = CHILD_LIMIT): ChildSessionRegistry {
|
|
41
|
+
const records = new Map<string, ChildRecord>();
|
|
42
|
+
|
|
43
|
+
/** 还在跑、等待中、或有活后代的不淘汰;其余取最老的。全在用就让它超出上限,不丢活的。 */
|
|
44
|
+
const evictOne = (keep: string): void => {
|
|
45
|
+
const protectedIds = new Set<string>();
|
|
46
|
+
for (const [id, record] of records) {
|
|
47
|
+
if (!active(record.observed)) continue;
|
|
48
|
+
protectedIds.add(id);
|
|
49
|
+
let parent = record.parentAgentId;
|
|
50
|
+
while (parent !== undefined && !protectedIds.has(parent)) {
|
|
51
|
+
protectedIds.add(parent);
|
|
52
|
+
parent = records.get(parent)?.parentAgentId;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
for (const id of records.keys()) {
|
|
56
|
+
if (id === keep || protectedIds.has(id)) continue;
|
|
57
|
+
records.delete(id);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const add: ChildSessionRegistry["add"] = (sessionId, record) => {
|
|
63
|
+
records.set(sessionId, record);
|
|
64
|
+
if (records.size > limit) evictOne(sessionId);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
has: (sessionId) => records.has(sessionId),
|
|
69
|
+
get: (sessionId) => records.get(sessionId),
|
|
70
|
+
mark: (sessionId) => {
|
|
71
|
+
if (records.has(sessionId)) return;
|
|
72
|
+
add(sessionId, {
|
|
73
|
+
turnId: undefined,
|
|
74
|
+
title: undefined,
|
|
75
|
+
description: undefined,
|
|
76
|
+
parentAgentId: undefined,
|
|
77
|
+
observed: undefined,
|
|
78
|
+
reported: undefined,
|
|
79
|
+
reportedError: undefined,
|
|
80
|
+
announced: false,
|
|
81
|
+
pendingMetadata: false,
|
|
82
|
+
});
|
|
83
|
+
},
|
|
84
|
+
add,
|
|
85
|
+
entries: () => records.entries(),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
// @effect-diagnostics nodeBuiltinImport:off
|
|
2
|
+
// @effect-diagnostics globalDate:off
|
|
3
|
+
// @effect-diagnostics globalTimers:off
|
|
4
|
+
import {
|
|
5
|
+
AcpExecutorCancelTimeoutError,
|
|
6
|
+
AcpExecutorDisposedError,
|
|
7
|
+
AcpExecutorError,
|
|
8
|
+
} from "../errors.ts";
|
|
9
|
+
import { type PromptResponse } from "../protocol.ts";
|
|
10
|
+
import type { createExecutorPrompt } from "./prompt.ts";
|
|
11
|
+
import type { createPromptResults } from "./prompt-results.ts";
|
|
12
|
+
import type { createExecutorRequests } from "./requests.ts";
|
|
13
|
+
import type { createExecutorRuntime } from "./runtime.ts";
|
|
14
|
+
import type { createExecutorSession } from "./session.ts";
|
|
15
|
+
import type { createExecutorSettlement } from "./settlement.ts";
|
|
16
|
+
import {
|
|
17
|
+
DEFAULT_RESULT_DRAIN_TIMEOUT_MS,
|
|
18
|
+
type AcpExecutorSupport,
|
|
19
|
+
type CompactionRecord,
|
|
20
|
+
} from "./support.ts";
|
|
21
|
+
import type { createExecutorUpdates } from "./updates.ts";
|
|
22
|
+
|
|
23
|
+
export function createExecutorLifecycle({
|
|
24
|
+
failIfDisposed,
|
|
25
|
+
requireStarted,
|
|
26
|
+
interruptedTurnIds,
|
|
27
|
+
settleAllApprovals,
|
|
28
|
+
settleAllUserInputs,
|
|
29
|
+
abortPendingSettlement,
|
|
30
|
+
connection,
|
|
31
|
+
waitForPromptIdle,
|
|
32
|
+
cancelGraceMs,
|
|
33
|
+
isLive,
|
|
34
|
+
closeActiveAssistantSegment,
|
|
35
|
+
emit,
|
|
36
|
+
stamp,
|
|
37
|
+
options,
|
|
38
|
+
promptMutex,
|
|
39
|
+
randomId,
|
|
40
|
+
nowMs,
|
|
41
|
+
emitCompactionState,
|
|
42
|
+
sendPromptRpc,
|
|
43
|
+
compactions,
|
|
44
|
+
notifyPromptSettled,
|
|
45
|
+
eventStream,
|
|
46
|
+
onInputDead,
|
|
47
|
+
shared,
|
|
48
|
+
promptResults,
|
|
49
|
+
settleToolSubagents,
|
|
50
|
+
settleSessionEnded,
|
|
51
|
+
}: Pick<
|
|
52
|
+
AcpExecutorSupport,
|
|
53
|
+
| "interruptedTurnIds"
|
|
54
|
+
| "connection"
|
|
55
|
+
| "cancelGraceMs"
|
|
56
|
+
| "options"
|
|
57
|
+
| "promptMutex"
|
|
58
|
+
| "randomId"
|
|
59
|
+
| "nowMs"
|
|
60
|
+
| "compactions"
|
|
61
|
+
| "eventStream"
|
|
62
|
+
| "onInputDead"
|
|
63
|
+
| "shared"
|
|
64
|
+
> &
|
|
65
|
+
Pick<
|
|
66
|
+
ReturnType<typeof createExecutorRuntime>,
|
|
67
|
+
| "failIfDisposed"
|
|
68
|
+
| "waitForPromptIdle"
|
|
69
|
+
| "isLive"
|
|
70
|
+
| "closeActiveAssistantSegment"
|
|
71
|
+
| "emit"
|
|
72
|
+
| "stamp"
|
|
73
|
+
| "notifyPromptSettled"
|
|
74
|
+
> &
|
|
75
|
+
Pick<
|
|
76
|
+
ReturnType<typeof createExecutorUpdates>,
|
|
77
|
+
"emitCompactionState" | "settleToolSubagents" | "settleSessionEnded"
|
|
78
|
+
> &
|
|
79
|
+
Pick<ReturnType<typeof createExecutorRequests>, "settleAllApprovals" | "settleAllUserInputs"> &
|
|
80
|
+
Pick<ReturnType<typeof createExecutorSettlement>, "abortPendingSettlement"> &
|
|
81
|
+
Pick<ReturnType<typeof createExecutorSession>, "requireStarted"> &
|
|
82
|
+
Pick<ReturnType<typeof createExecutorPrompt>, "sendPromptRpc"> & {
|
|
83
|
+
promptResults: ReturnType<typeof createPromptResults>;
|
|
84
|
+
}) {
|
|
85
|
+
const cancel = async (turnId?: string): Promise<void> => {
|
|
86
|
+
failIfDisposed();
|
|
87
|
+
const started = requireStarted();
|
|
88
|
+
const targetTurnId = turnId ?? shared.activeTurnId;
|
|
89
|
+
promptResults.stop(targetTurnId);
|
|
90
|
+
if (targetTurnId !== undefined) {
|
|
91
|
+
interruptedTurnIds.add(targetTurnId);
|
|
92
|
+
}
|
|
93
|
+
const waiting = shared.localPromptWait;
|
|
94
|
+
if (waiting !== undefined && (targetTurnId === undefined || waiting.turnId === targetTurnId)) {
|
|
95
|
+
waiting.stop();
|
|
96
|
+
}
|
|
97
|
+
settleAllApprovals("cancel");
|
|
98
|
+
settleAllUserInputs();
|
|
99
|
+
abortPendingSettlement("turn cancelled");
|
|
100
|
+
|
|
101
|
+
const runCancel = async (): Promise<void> => {
|
|
102
|
+
if (shared.sessionId) {
|
|
103
|
+
await connection.notify("session/cancel", { sessionId: shared.sessionId });
|
|
104
|
+
}
|
|
105
|
+
await promptResults.wait(targetTurnId);
|
|
106
|
+
if (
|
|
107
|
+
targetTurnId !== undefined &&
|
|
108
|
+
shared.activeTurnId !== targetTurnId &&
|
|
109
|
+
!interruptedTurnIds.has(targetTurnId)
|
|
110
|
+
)
|
|
111
|
+
return;
|
|
112
|
+
const settled = await waitForPromptIdle(cancelGraceMs);
|
|
113
|
+
if (!settled) {
|
|
114
|
+
throw new AcpExecutorCancelTimeoutError();
|
|
115
|
+
}
|
|
116
|
+
if (targetTurnId !== undefined && isLive(started.generation)) {
|
|
117
|
+
if (shared.activeTurnId === targetTurnId || interruptedTurnIds.has(targetTurnId)) {
|
|
118
|
+
if (shared.status === "running") {
|
|
119
|
+
closeActiveAssistantSegment(targetTurnId);
|
|
120
|
+
shared.promptsInFlight = 0;
|
|
121
|
+
shared.activeTurnId = undefined;
|
|
122
|
+
shared.status = "ready";
|
|
123
|
+
settleToolSubagents("turn-interrupted", targetTurnId);
|
|
124
|
+
emit({
|
|
125
|
+
type: "turn.completed",
|
|
126
|
+
...stamp(),
|
|
127
|
+
provider: options.provider,
|
|
128
|
+
threadId: options.threadId,
|
|
129
|
+
turnId: targetTurnId,
|
|
130
|
+
payload: { state: "cancelled", stopReason: "cancelled" },
|
|
131
|
+
});
|
|
132
|
+
interruptedTurnIds.delete(targetTurnId);
|
|
133
|
+
promptResults.forget(targetTurnId);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// 取消不走 thread 锁。今天 thread 锁只罩开轮与结算两段短临界区,prompt RPC 在锁外,
|
|
140
|
+
// 所以排进去也不会被 RPC 堵住——但那是「锁范围恰好正确」带来的,不是设计保证。
|
|
141
|
+
// 让取消独立于这把锁,是为了以后有人加长临界区时,Stop 仍然到得了。
|
|
142
|
+
await runCancel();
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const compact = async (input: { readonly commandName: string }): Promise<void> => {
|
|
146
|
+
failIfDisposed();
|
|
147
|
+
const started = requireStarted();
|
|
148
|
+
const trimmedName = input.commandName.trim();
|
|
149
|
+
const commandName = trimmedName.startsWith("/") ? trimmedName.slice(1) : trimmedName;
|
|
150
|
+
if (!shared.availableCommands.some((command) => command.name === commandName)) {
|
|
151
|
+
throw new AcpExecutorError(
|
|
152
|
+
"command-unavailable",
|
|
153
|
+
`The agent has not advertised the /${commandName} command`,
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
if (
|
|
157
|
+
shared.promptsInFlight > 0 ||
|
|
158
|
+
promptMutex.isLocked ||
|
|
159
|
+
shared.manualCompaction !== undefined
|
|
160
|
+
) {
|
|
161
|
+
throw new AcpExecutorError(
|
|
162
|
+
"busy",
|
|
163
|
+
"Cannot compact while a turn or another compaction is running",
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
const record: CompactionRecord = {
|
|
167
|
+
snapshotId: randomId(),
|
|
168
|
+
trigger: "manual",
|
|
169
|
+
startedAtMs: nowMs(),
|
|
170
|
+
summary: undefined,
|
|
171
|
+
announced: true,
|
|
172
|
+
finished: false,
|
|
173
|
+
adopted: false,
|
|
174
|
+
};
|
|
175
|
+
// 先占住 promptMutex 再宣布开始:并发的 prompt 排在压缩后面,不会插进命令中间。
|
|
176
|
+
await promptMutex.acquire();
|
|
177
|
+
shared.manualCompaction = record;
|
|
178
|
+
const emitSessionState = (state: "compacting" | "ready"): void => {
|
|
179
|
+
emit({
|
|
180
|
+
type: "session.state.changed",
|
|
181
|
+
...stamp(),
|
|
182
|
+
provider: options.provider,
|
|
183
|
+
threadId: options.threadId,
|
|
184
|
+
payload: { state, reason: "compaction" },
|
|
185
|
+
});
|
|
186
|
+
};
|
|
187
|
+
try {
|
|
188
|
+
failIfDisposed();
|
|
189
|
+
emitSessionState("compacting");
|
|
190
|
+
emitCompactionState(record, "compacting");
|
|
191
|
+
let response: PromptResponse;
|
|
192
|
+
try {
|
|
193
|
+
response = await sendPromptRpc(started.sessionId, [
|
|
194
|
+
{ type: "text", text: `/${commandName}` },
|
|
195
|
+
]);
|
|
196
|
+
} catch (error) {
|
|
197
|
+
if (shared.disposed) throw new AcpExecutorDisposedError();
|
|
198
|
+
throw error;
|
|
199
|
+
}
|
|
200
|
+
await connection.awaitInboundIdle();
|
|
201
|
+
if (response.stopReason === "cancelled") {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
if (!record.finished) {
|
|
205
|
+
record.finished = true;
|
|
206
|
+
emitCompactionState(record, "compacted");
|
|
207
|
+
}
|
|
208
|
+
} finally {
|
|
209
|
+
for (const [agentId, tracked] of compactions) {
|
|
210
|
+
if (tracked === record) compactions.delete(agentId);
|
|
211
|
+
}
|
|
212
|
+
shared.manualCompaction = undefined;
|
|
213
|
+
promptMutex.release();
|
|
214
|
+
notifyPromptSettled();
|
|
215
|
+
if (!shared.disposed) emitSessionState("ready");
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
let disposing: Promise<void> | undefined;
|
|
220
|
+
const dispose = (exitCode?: number | null): Promise<void> => {
|
|
221
|
+
if (disposing) return disposing;
|
|
222
|
+
shared.disposing = true;
|
|
223
|
+
disposing = (async () => {
|
|
224
|
+
shared.localPromptWait?.stop();
|
|
225
|
+
if (exitCode !== undefined) {
|
|
226
|
+
// 进程退出仍须读完管道与已登记的终态,不能把在途失败改判成用户取消。
|
|
227
|
+
// 孙进程可能还握着 stdout,管道不一定关得掉:等管道关闭也套同一个尾部上限。
|
|
228
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
229
|
+
await Promise.race([
|
|
230
|
+
connection.closed.catch(() => undefined),
|
|
231
|
+
new Promise<void>((resolve) => {
|
|
232
|
+
timer = setTimeout(
|
|
233
|
+
resolve,
|
|
234
|
+
options.resultDrainTimeoutMs ?? DEFAULT_RESULT_DRAIN_TIMEOUT_MS,
|
|
235
|
+
);
|
|
236
|
+
}),
|
|
237
|
+
]);
|
|
238
|
+
clearTimeout(timer);
|
|
239
|
+
await promptResults.wait(undefined, true);
|
|
240
|
+
}
|
|
241
|
+
promptResults.stop();
|
|
242
|
+
settleAllApprovals("cancel");
|
|
243
|
+
settleAllUserInputs();
|
|
244
|
+
abortPendingSettlement("executor disposed");
|
|
245
|
+
notifyPromptSettled();
|
|
246
|
+
shared.abortLocalPrompt?.();
|
|
247
|
+
promptResults.finishUnreceived();
|
|
248
|
+
await promptResults.wait();
|
|
249
|
+
settleSessionEnded();
|
|
250
|
+
emit({
|
|
251
|
+
type: "session.exited",
|
|
252
|
+
...stamp(),
|
|
253
|
+
provider: options.provider,
|
|
254
|
+
threadId: options.threadId,
|
|
255
|
+
payload:
|
|
256
|
+
exitCode === undefined
|
|
257
|
+
? { reason: "disposed" }
|
|
258
|
+
: {
|
|
259
|
+
reason:
|
|
260
|
+
exitCode === null
|
|
261
|
+
? "process exited without an exit code"
|
|
262
|
+
: `process exited with code ${exitCode}`,
|
|
263
|
+
exitKind: exitCode === 0 ? "graceful" : "error",
|
|
264
|
+
},
|
|
265
|
+
});
|
|
266
|
+
shared.disposed = true;
|
|
267
|
+
shared.generation += 1;
|
|
268
|
+
shared.status = "disposed";
|
|
269
|
+
eventStream.close();
|
|
270
|
+
options.readable.off("end", onInputDead);
|
|
271
|
+
options.readable.off("error", onInputDead);
|
|
272
|
+
options.readable.pause();
|
|
273
|
+
if (!shared.connectionClosed) {
|
|
274
|
+
await connection.awaitInboundIdle().catch(() => undefined);
|
|
275
|
+
await connection.close().catch(() => undefined);
|
|
276
|
+
}
|
|
277
|
+
})();
|
|
278
|
+
return disposing;
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
return { cancel, compact, dispose };
|
|
282
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// @effect-diagnostics globalTimers:off
|
|
2
|
+
import type { PromptResponse } from "../protocol.ts";
|
|
3
|
+
import { DEFAULT_RESULT_DRAIN_TIMEOUT_MS, type AcpExecutorSupport } from "./support.ts";
|
|
4
|
+
|
|
5
|
+
type PromptIdentity = {
|
|
6
|
+
readonly promptId: string;
|
|
7
|
+
sessionId: string;
|
|
8
|
+
readonly generation: number;
|
|
9
|
+
readonly turnId: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type PromptResult = PromptIdentity & {
|
|
13
|
+
response?: PromptResponse;
|
|
14
|
+
rejected: boolean;
|
|
15
|
+
interrupted: boolean;
|
|
16
|
+
finished: boolean;
|
|
17
|
+
readonly done: Promise<void>;
|
|
18
|
+
readonly resolve: () => void;
|
|
19
|
+
readonly finish: () => void;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export function createPromptResults({
|
|
23
|
+
options,
|
|
24
|
+
shared,
|
|
25
|
+
}: Pick<AcpExecutorSupport, "options" | "shared">) {
|
|
26
|
+
const prompts = new Map<string, PromptResult>();
|
|
27
|
+
const add = (identity: PromptIdentity, finish: () => void): PromptResult => {
|
|
28
|
+
const { promise: done, resolve } = Promise.withResolvers<void>();
|
|
29
|
+
const record: PromptResult = {
|
|
30
|
+
...identity,
|
|
31
|
+
finish,
|
|
32
|
+
done,
|
|
33
|
+
resolve,
|
|
34
|
+
rejected: false,
|
|
35
|
+
interrupted: false,
|
|
36
|
+
finished: false,
|
|
37
|
+
};
|
|
38
|
+
prompts.set(record.promptId, record);
|
|
39
|
+
return record;
|
|
40
|
+
};
|
|
41
|
+
// 接收边界同步认领;提前通知和 RPC 应答共用同一条记录,先到的结果不会被迟到帧覆盖。
|
|
42
|
+
const receive = (promptId: string, response: PromptResponse): void => {
|
|
43
|
+
const record = prompts.get(promptId);
|
|
44
|
+
if (!record || record.finished || record.rejected || record.response !== undefined) return;
|
|
45
|
+
if (
|
|
46
|
+
shared.disposed ||
|
|
47
|
+
shared.generation !== record.generation ||
|
|
48
|
+
shared.sessionId !== record.sessionId
|
|
49
|
+
)
|
|
50
|
+
return;
|
|
51
|
+
record.response = response;
|
|
52
|
+
};
|
|
53
|
+
const reject = (promptId: string): void => {
|
|
54
|
+
const record = prompts.get(promptId);
|
|
55
|
+
if (record && record.response === undefined) record.rejected = true;
|
|
56
|
+
};
|
|
57
|
+
const stop = (turnId?: string): void => {
|
|
58
|
+
for (const record of prompts.values()) {
|
|
59
|
+
if (
|
|
60
|
+
!record.finished &&
|
|
61
|
+
(turnId === undefined || record.turnId === turnId) &&
|
|
62
|
+
record.response === undefined
|
|
63
|
+
) {
|
|
64
|
+
record.interrupted = true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const complete = (record: PromptResult): void => {
|
|
69
|
+
record.finished = true;
|
|
70
|
+
record.resolve();
|
|
71
|
+
};
|
|
72
|
+
const hasInterrupted = (turnId: string): boolean =>
|
|
73
|
+
[...prompts.values()].some((record) => record.turnId === turnId && record.interrupted);
|
|
74
|
+
const forget = (turnId: string): void => {
|
|
75
|
+
for (const [id, record] of prompts) {
|
|
76
|
+
if (record.turnId === turnId) {
|
|
77
|
+
complete(record);
|
|
78
|
+
prompts.delete(id);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
// 连接关闭后才允许等无应答记录;其余调用只等已认领结果。超时跳过可选补充信息,已认领结果不改判。
|
|
83
|
+
const wait = async (turnId?: string, includeUnreceived = false): Promise<void> => {
|
|
84
|
+
const claimed = [...prompts.values()].filter(
|
|
85
|
+
(record) =>
|
|
86
|
+
!record.finished &&
|
|
87
|
+
(includeUnreceived || record.response !== undefined) &&
|
|
88
|
+
(turnId === undefined || record.turnId === turnId),
|
|
89
|
+
);
|
|
90
|
+
if (claimed.length === 0) return;
|
|
91
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
92
|
+
try {
|
|
93
|
+
const drained = await Promise.race([
|
|
94
|
+
Promise.all(claimed.map((record) => record.done)).then(() => true),
|
|
95
|
+
new Promise<false>((resolve) => {
|
|
96
|
+
timer = setTimeout(
|
|
97
|
+
() => resolve(false),
|
|
98
|
+
options.resultDrainTimeoutMs ?? DEFAULT_RESULT_DRAIN_TIMEOUT_MS,
|
|
99
|
+
);
|
|
100
|
+
}),
|
|
101
|
+
]);
|
|
102
|
+
if (drained) return;
|
|
103
|
+
options.log?.warn?.("ACP result settlement timed out", { threadId: options.threadId });
|
|
104
|
+
for (const record of claimed) if (!record.finished) record.finish();
|
|
105
|
+
} finally {
|
|
106
|
+
clearTimeout(timer);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
const finishUnreceived = (): void => {
|
|
110
|
+
const claimedTurns = new Set(
|
|
111
|
+
[...prompts.values()]
|
|
112
|
+
.filter((record) => !record.finished && record.response !== undefined)
|
|
113
|
+
.map((record) => record.turnId),
|
|
114
|
+
);
|
|
115
|
+
for (const record of prompts.values()) {
|
|
116
|
+
if (!record.finished && record.response === undefined) {
|
|
117
|
+
if (claimedTurns.has(record.turnId)) record.finish();
|
|
118
|
+
else complete(record);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
return { add, receive, reject, stop, complete, hasInterrupted, forget, wait, finishUnreceived };
|
|
123
|
+
}
|