@zvada/agent-server 0.3.27 → 0.3.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/README.md +6 -4
- package/dist/core/agents/acp/account.js +111 -0
- package/dist/core/agents/acp/acp-agent.js +247 -158
- package/dist/core/agents/acp/adapter.js +103 -34
- package/dist/core/agents/acp/client.js +169 -42
- package/dist/core/agents/acp/known-agents.js +12 -5
- package/dist/core/agents/acp/mappings.js +84 -20
- package/dist/core/agents/acp/models.js +59 -0
- package/dist/core/agents/acp/profile.js +36 -0
- package/dist/core/agents/acp/prompts.js +248 -0
- package/dist/core/agents/acp/requests.js +4 -4
- package/dist/core/agents/claude-code/login.js +4 -1
- package/dist/core/agents/claude-code/session.js +38 -10
- package/dist/core/agents/claude-code/spawn.js +0 -26
- package/dist/core/agents/codex-app-server/client.js +12 -13
- package/dist/core/agents/codex-app-server/login.js +4 -1
- package/dist/core/agents/gemini/failures.js +133 -0
- package/dist/core/agents/gemini/login.js +173 -0
- package/dist/core/agents/gemini/profile.js +137 -0
- package/dist/core/agents/gemini/tools.js +91 -0
- package/dist/core/agents/login.js +8 -6
- package/dist/core/agents/stderr-tail.js +5 -2
- package/dist/core/presets.js +13 -0
- package/dist/core/processes/native-processes.js +23 -1
- package/dist/core/provision/install-build.js +70 -26
- package/dist/core/provision/pins.js +5 -0
- package/dist/core/provision/provisioner.js +39 -16
- package/dist/core/provision/releases.js +39 -1
- package/dist/core/provision/tools.js +87 -28
- package/dist/operator/errors.js +2 -2
- package/dist/operator/operator.js +171 -74
- package/dist/operator/render.js +33 -25
- package/dist/operator/session-tools.js +2 -1
- package/dist/protocol/account.js +5 -1
- package/dist/protocol/auth.js +5 -1
- package/dist/protocol/credentials.js +9 -1
- package/dist/protocol/harness.js +9 -1
- package/dist/server/bin.js +22 -7
- package/dist/server/install.js +6 -4
- package/dist/types/core/agents/acp/account.d.ts +55 -0
- package/dist/types/core/agents/acp/account.d.ts.map +1 -0
- package/dist/types/core/agents/acp/acp-agent.d.ts +65 -36
- package/dist/types/core/agents/acp/acp-agent.d.ts.map +1 -1
- package/dist/types/core/agents/acp/adapter.d.ts +28 -3
- package/dist/types/core/agents/acp/adapter.d.ts.map +1 -1
- package/dist/types/core/agents/acp/client.d.ts +66 -4
- package/dist/types/core/agents/acp/client.d.ts.map +1 -1
- package/dist/types/core/agents/acp/known-agents.d.ts +4 -2
- package/dist/types/core/agents/acp/known-agents.d.ts.map +1 -1
- package/dist/types/core/agents/acp/mappings.d.ts +53 -7
- package/dist/types/core/agents/acp/mappings.d.ts.map +1 -1
- package/dist/types/core/agents/acp/models.d.ts +22 -0
- package/dist/types/core/agents/acp/models.d.ts.map +1 -0
- package/dist/types/core/agents/acp/profile.d.ts +92 -0
- package/dist/types/core/agents/acp/profile.d.ts.map +1 -0
- package/dist/types/core/agents/acp/prompts.d.ts +108 -0
- package/dist/types/core/agents/acp/prompts.d.ts.map +1 -0
- package/dist/types/core/agents/acp/requests.d.ts +7 -1
- package/dist/types/core/agents/acp/requests.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/login.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/session.d.ts +3 -0
- package/dist/types/core/agents/claude-code/session.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/spawn.d.ts +0 -8
- package/dist/types/core/agents/claude-code/spawn.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/client.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/login.d.ts.map +1 -1
- package/dist/types/core/agents/gemini/failures.d.ts +25 -0
- package/dist/types/core/agents/gemini/failures.d.ts.map +1 -0
- package/dist/types/core/agents/gemini/login.d.ts +28 -0
- package/dist/types/core/agents/gemini/login.d.ts.map +1 -0
- package/dist/types/core/agents/gemini/profile.d.ts +30 -0
- package/dist/types/core/agents/gemini/profile.d.ts.map +1 -0
- package/dist/types/core/agents/gemini/tools.d.ts +18 -0
- package/dist/types/core/agents/gemini/tools.d.ts.map +1 -0
- package/dist/types/core/agents/login.d.ts +15 -5
- package/dist/types/core/agents/login.d.ts.map +1 -1
- package/dist/types/core/agents/stderr-tail.d.ts +1 -1
- package/dist/types/core/agents/stderr-tail.d.ts.map +1 -1
- package/dist/types/core/presets.d.ts +2 -2
- package/dist/types/core/presets.d.ts.map +1 -1
- package/dist/types/core/processes/native-processes.d.ts +6 -0
- package/dist/types/core/processes/native-processes.d.ts.map +1 -1
- package/dist/types/core/provision/checksums.d.ts +7 -1
- package/dist/types/core/provision/checksums.d.ts.map +1 -1
- package/dist/types/core/provision/install-build.d.ts +32 -6
- package/dist/types/core/provision/install-build.d.ts.map +1 -1
- package/dist/types/core/provision/pins.d.ts +4 -0
- package/dist/types/core/provision/pins.d.ts.map +1 -1
- package/dist/types/core/provision/provisioner.d.ts +18 -7
- package/dist/types/core/provision/provisioner.d.ts.map +1 -1
- package/dist/types/core/provision/releases.d.ts +2 -1
- package/dist/types/core/provision/releases.d.ts.map +1 -1
- package/dist/types/core/provision/tools.d.ts +36 -7
- package/dist/types/core/provision/tools.d.ts.map +1 -1
- package/dist/types/operator/errors.d.ts +5 -1
- package/dist/types/operator/errors.d.ts.map +1 -1
- package/dist/types/operator/operator.d.ts.map +1 -1
- package/dist/types/operator/render.d.ts +11 -6
- package/dist/types/operator/render.d.ts.map +1 -1
- package/dist/types/operator/session-tools.d.ts.map +1 -1
- package/dist/types/operator/types.d.ts +1 -0
- package/dist/types/operator/types.d.ts.map +1 -1
- package/dist/types/protocol/account.d.ts +4 -2
- package/dist/types/protocol/account.d.ts.map +1 -1
- package/dist/types/protocol/auth.d.ts +10 -7
- package/dist/types/protocol/auth.d.ts.map +1 -1
- package/dist/types/protocol/clis.d.ts +4 -3
- package/dist/types/protocol/clis.d.ts.map +1 -1
- package/dist/types/protocol/config.d.ts +2 -0
- package/dist/types/protocol/config.d.ts.map +1 -1
- package/dist/types/protocol/conversation.d.ts +3 -3
- package/dist/types/protocol/credentials.d.ts +8 -0
- package/dist/types/protocol/credentials.d.ts.map +1 -1
- package/dist/types/protocol/execution.d.ts +1 -1
- package/dist/types/protocol/harness.d.ts +5 -2
- package/dist/types/protocol/harness.d.ts.map +1 -1
- package/dist/types/protocol/lifecycle.d.ts +8 -8
- package/dist/types/protocol/models.d.ts +3 -0
- package/dist/types/protocol/models.d.ts.map +1 -1
- package/dist/types/protocol/wire-api.d.ts +39 -29
- package/dist/types/protocol/wire-api.d.ts.map +1 -1
- package/dist/types/protocol/wire.d.ts +13 -10
- package/dist/types/protocol/wire.d.ts.map +1 -1
- package/dist/types/server/install.d.ts +2 -1
- package/dist/types/server/install.d.ts.map +1 -1
- package/docs/consuming.md +6 -0
- package/docs/deploy.md +17 -8
- package/docs/harnesses.md +166 -44
- package/package.json +1 -1
- package/src/core/agents/acp/account.ts +158 -0
- package/src/core/agents/acp/acp-agent.ts +288 -178
- package/src/core/agents/acp/adapter.ts +126 -35
- package/src/core/agents/acp/client.ts +196 -49
- package/src/core/agents/acp/known-agents.ts +14 -5
- package/src/core/agents/acp/mappings.ts +112 -21
- package/src/core/agents/acp/models.ts +72 -0
- package/src/core/agents/acp/profile.ts +127 -0
- package/src/core/agents/acp/prompts.ts +287 -0
- package/src/core/agents/acp/requests.ts +16 -9
- package/src/core/agents/claude-code/login.ts +4 -1
- package/src/core/agents/claude-code/session.ts +38 -8
- package/src/core/agents/claude-code/spawn.ts +0 -26
- package/src/core/agents/codex-app-server/client.ts +11 -18
- package/src/core/agents/codex-app-server/login.ts +4 -5
- package/src/core/agents/gemini/failures.ts +148 -0
- package/src/core/agents/gemini/login.ts +188 -0
- package/src/core/agents/gemini/profile.ts +162 -0
- package/src/core/agents/gemini/tools.ts +99 -0
- package/src/core/agents/login.ts +14 -8
- package/src/core/agents/stderr-tail.ts +5 -2
- package/src/core/presets.ts +15 -2
- package/src/core/processes/native-processes.ts +32 -1
- package/src/core/provision/checksums.ts +12 -2
- package/src/core/provision/install-build.ts +122 -55
- package/src/core/provision/pins.ts +5 -0
- package/src/core/provision/provisioner.ts +62 -27
- package/src/core/provision/releases.ts +47 -1
- package/src/core/provision/tools.ts +124 -31
- package/src/operator/errors.ts +5 -2
- package/src/operator/operator.ts +181 -77
- package/src/operator/render.ts +60 -28
- package/src/operator/session-tools.ts +2 -1
- package/src/operator/types.ts +1 -0
- package/src/protocol/account.ts +5 -2
- package/src/protocol/auth.ts +5 -1
- package/src/protocol/credentials.ts +11 -2
- package/src/protocol/harness.ts +9 -1
- package/src/server/bin.ts +20 -6
- package/src/server/install.ts +6 -3
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TOOL_KINDS } from "../../../protocol/index.js";
|
|
1
|
+
import { TOOL_KINDS, fileKind } from "../../../protocol/index.js";
|
|
2
2
|
import { isRecord } from "../../utils/is-record.js";
|
|
3
3
|
import { UnsupportedAttachmentError, attachedText, inlineTextAttachment, isTextMedia, } from "../attachments.js";
|
|
4
4
|
/**
|
|
@@ -66,6 +66,20 @@ export function toAcpMcpServers(mcpServers) {
|
|
|
66
66
|
};
|
|
67
67
|
});
|
|
68
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Refuse a file of a kind the harness does not offer (`capabilities.files`),
|
|
71
|
+
* before any agent starts: what an agent says it embeds is not what it reads,
|
|
72
|
+
* so the harness's own list decides (LLP 0020, decision 13).
|
|
73
|
+
*/
|
|
74
|
+
export function refuseFiles(input, harness, files) {
|
|
75
|
+
if (typeof input === "string")
|
|
76
|
+
return;
|
|
77
|
+
for (const part of input) {
|
|
78
|
+
if (part.type !== "file" || part.url !== undefined || files.includes(fileKind(part.mimeType)))
|
|
79
|
+
continue;
|
|
80
|
+
throw new UnsupportedAttachmentError(`The ${harness} harness cannot take ${part.mimeType} file attachments`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
69
83
|
/**
|
|
70
84
|
* Engine turn input → ACP prompt content blocks. Every agent takes text and
|
|
71
85
|
* resource links; image data needs the `image` capability and inline files
|
|
@@ -108,16 +122,20 @@ export function toPromptBlocks(input, prompt) {
|
|
|
108
122
|
}
|
|
109
123
|
return blocks.length ? blocks : [{ type: "text", text: "" }];
|
|
110
124
|
}
|
|
111
|
-
/**
|
|
112
|
-
|
|
125
|
+
/**
|
|
126
|
+
* ACP permission request → the engine's PermissionToolCall (SDK-validated, no
|
|
127
|
+
* casts). `described` is what the harness's tool rules read of the call
|
|
128
|
+
* (`AcpProfile.stream`): an ACP agent names its tools by no rule of its own.
|
|
129
|
+
*/
|
|
130
|
+
export function permissionToolCall(request, described = {}) {
|
|
113
131
|
const toolCall = request.toolCall;
|
|
114
132
|
const kind = toolKindOf(toolCall.kind);
|
|
115
133
|
const locations = toToolLocations(toolCall.locations);
|
|
116
134
|
return {
|
|
117
135
|
...(toolCall.toolCallId && { toolCallId: toolCall.toolCallId }),
|
|
118
|
-
// No `mcp`: an ACP agent names its tools by no rule, so no server or tool can be read from it.
|
|
119
136
|
toolName: toolCall.name ?? toolCall.title ?? toolCall.kind ?? "tool",
|
|
120
137
|
...(kind && { kind }),
|
|
138
|
+
...described,
|
|
121
139
|
...(toolCall.title && { title: toolCall.title }),
|
|
122
140
|
...(locations && { locations }),
|
|
123
141
|
...(isRecord(toolCall.rawInput) && { rawInput: toolCall.rawInput }),
|
|
@@ -142,27 +160,73 @@ export function chooseOutcome(options, allow) {
|
|
|
142
160
|
}
|
|
143
161
|
return CANCELLED_OUTCOME;
|
|
144
162
|
}
|
|
163
|
+
/** An edit or a read: what `accept_edits` allows unasked. */
|
|
164
|
+
function editOrRead(request) {
|
|
165
|
+
const kind = request.toolCall.kind;
|
|
166
|
+
return kind === "edit" || kind === "read";
|
|
167
|
+
}
|
|
168
|
+
// @ref LLP 0016.000#an-acp-agents-permission-mode — accept_edits asks for the rest, as Claude and Codex do
|
|
169
|
+
/**
|
|
170
|
+
* Whether a request is the user's to answer in `mode`, when the turn has a
|
|
171
|
+
* broker: everything in `default`; in `accept_edits` what is neither an edit
|
|
172
|
+
* nor a read, as Claude and Codex ask for a command in that mode. Every other
|
|
173
|
+
* mode decides by itself (`autoDecide`).
|
|
174
|
+
*/
|
|
175
|
+
export function asksInMode(request, mode) {
|
|
176
|
+
return mode === "default" || (mode === "accept_edits" && !editOrRead(request));
|
|
177
|
+
}
|
|
145
178
|
/**
|
|
146
|
-
* Decide an ACP permission request without the engine broker:
|
|
147
|
-
*
|
|
179
|
+
* Decide an ACP permission request without the engine broker:
|
|
180
|
+
* `bypass_permissions` allows everything, `accept_edits` edits and reads, and
|
|
181
|
+
* every other mode denies.
|
|
148
182
|
*/
|
|
149
183
|
export function autoDecide(request, mode) {
|
|
150
|
-
const
|
|
151
|
-
const allowed = mode === "bypass_permissions" ||
|
|
152
|
-
(mode === "accept_edits" && (kind === "edit" || kind === "read"));
|
|
184
|
+
const allowed = mode === "bypass_permissions" || (mode === "accept_edits" && editOrRead(request));
|
|
153
185
|
return chooseOutcome(request.options, allowed);
|
|
154
186
|
}
|
|
155
|
-
/**
|
|
187
|
+
/** The engine's modes by their ids without case or separators: `acceptEdits`, `accept_edits` and `accept-edits` are one. */
|
|
188
|
+
const MODE_IDS = {
|
|
189
|
+
default: "default",
|
|
190
|
+
plan: "plan",
|
|
191
|
+
acceptedits: "accept_edits",
|
|
192
|
+
dontask: "dont_ask",
|
|
193
|
+
bypasspermissions: "bypass_permissions",
|
|
194
|
+
};
|
|
195
|
+
/** An ACP mode id the engine has a name for: its own, or Claude's (claude-agent-acp), however it is cased and separated. */
|
|
156
196
|
export function permissionModeOf(modeId) {
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
197
|
+
const key = modeId.replace(/[-_\s]/g, "").toLowerCase();
|
|
198
|
+
return Object.hasOwn(MODE_IDS, key) ? MODE_IDS[key] : undefined;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* An agent's mode ids and the engine's modes, each way: the agent's own ids
|
|
202
|
+
* first (`own`), then the ids that name a mode (`permissionModeOf`).
|
|
203
|
+
*/
|
|
204
|
+
export function modeNames(own = {}) {
|
|
205
|
+
const modeOf = (modeId) => Object.keys(own).find((mode) => own[mode] === modeId) ??
|
|
206
|
+
permissionModeOf(modeId);
|
|
207
|
+
return {
|
|
208
|
+
/** The engine's mode an agent's mode id is, when the engine can name it. */
|
|
209
|
+
modeOf,
|
|
210
|
+
/** The agent's id for `mode`, of the ids it offers. */
|
|
211
|
+
idOf: (mode, offered) => {
|
|
212
|
+
const id = own[mode];
|
|
213
|
+
if (id !== undefined && offered.includes(id))
|
|
214
|
+
return id;
|
|
215
|
+
return offered.find((candidate) => modeOf(candidate) === mode);
|
|
216
|
+
},
|
|
166
217
|
};
|
|
167
|
-
|
|
218
|
+
}
|
|
219
|
+
/** Every choice a select option offers, whether listed flat or in groups. */
|
|
220
|
+
export function selectableChoices(option) {
|
|
221
|
+
return option.options.flatMap((entry) => "options" in entry
|
|
222
|
+
? entry.options.map(({ value, name }) => ({ value, name }))
|
|
223
|
+
: [{ value: entry.value, name: entry.name }]);
|
|
224
|
+
}
|
|
225
|
+
/** Every value a select option offers. */
|
|
226
|
+
export function selectableValues(option) {
|
|
227
|
+
return selectableChoices(option).map(({ value }) => value);
|
|
228
|
+
}
|
|
229
|
+
/** The agent's select option of a category, as it stands. */
|
|
230
|
+
export function selectOf(options, category) {
|
|
231
|
+
return options.find((option) => option.category === category && option.type === "select");
|
|
168
232
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { listedThinkingLevels } from "../thinking-levels.js";
|
|
2
|
+
import { SESSION_REQUEST_TIMEOUT_MS } from "./account.js";
|
|
3
|
+
import { selectOf, selectableChoices, selectableValues } from "./mappings.js";
|
|
4
|
+
// @ref LLP 0016.000#an-acp-agents-model-and-thinking-level — a harness that lists its models runs only one of them
|
|
5
|
+
/**
|
|
6
|
+
* The model a turn that names none runs, for a harness that lists its
|
|
7
|
+
* models: the agent's current one when the harness offers it, else the first
|
|
8
|
+
* the agent lists that it offers (a person's own default may be another
|
|
9
|
+
* vendor's model), else none.
|
|
10
|
+
*/
|
|
11
|
+
export function defaultModel(option, offers) {
|
|
12
|
+
const listed = selectableValues(option);
|
|
13
|
+
// The current one only while the agent still lists it.
|
|
14
|
+
if (listed.includes(option.currentValue) && offers(option.currentValue))
|
|
15
|
+
return option.currentValue;
|
|
16
|
+
return listed.find(offers);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The models an agent offers, from a session with no turn: each value of its
|
|
20
|
+
* `model` option that `offers` takes, labelled by the option's name, the
|
|
21
|
+
* default the one a turn naming none runs (`defaultModel`). A model's levels
|
|
22
|
+
* are those its `thought_level` offers once it is set, which change with the
|
|
23
|
+
* model, so each is set in turn; only the default model's level is its own,
|
|
24
|
+
* the others carrying over the one set before. The read stops between models
|
|
25
|
+
* once `signal` aborts.
|
|
26
|
+
*/
|
|
27
|
+
export function listAcpModels(account, harness, offers, options) {
|
|
28
|
+
const { env, signal } = options;
|
|
29
|
+
return account.open({ ...(env && { env }), ...(signal && { signal }) }, async (client, read) => {
|
|
30
|
+
const initial = read.session.configOptions ?? [];
|
|
31
|
+
const model = selectOf(initial, "model");
|
|
32
|
+
if (!model)
|
|
33
|
+
return [];
|
|
34
|
+
const fallback = defaultModel(model, offers);
|
|
35
|
+
const rows = [];
|
|
36
|
+
for (const { value, name } of selectableChoices(model).filter((c) => offers(c.value))) {
|
|
37
|
+
signal?.throwIfAborted();
|
|
38
|
+
let now = initial;
|
|
39
|
+
if (value !== model.currentValue) {
|
|
40
|
+
const set = await client.request("session/set_config_option", { sessionId: read.session.sessionId, configId: model.id, value }, SESSION_REQUEST_TIMEOUT_MS);
|
|
41
|
+
now = set.configOptions;
|
|
42
|
+
}
|
|
43
|
+
const isDefault = value === fallback;
|
|
44
|
+
const thought = selectOf(now, "thought_level");
|
|
45
|
+
const level = isDefault ? thought?.currentValue : undefined;
|
|
46
|
+
rows.push({
|
|
47
|
+
id: value,
|
|
48
|
+
harness,
|
|
49
|
+
label: name,
|
|
50
|
+
...(isDefault && { isDefault }),
|
|
51
|
+
thinking: {
|
|
52
|
+
levels: listedThinkingLevels(thought ? selectableValues(thought) : []),
|
|
53
|
+
...(level && { default: level }),
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return rows;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const CAPABILITIES = {
|
|
2
|
+
multiTurn: true,
|
|
3
|
+
// A requested resume fails if the target agent cannot load saved sessions.
|
|
4
|
+
sessionResume: true,
|
|
5
|
+
// v1 has no portable model parameter. A requested model or thinking level
|
|
6
|
+
// is applied only when the agent offers a `model` / `thought_level` config
|
|
7
|
+
// option, so neither is promised here.
|
|
8
|
+
modelSwitch: "unsupported",
|
|
9
|
+
thinkingLevels: false,
|
|
10
|
+
// The most an agent can take: its `initialize` decides, image data with its
|
|
11
|
+
// `image` prompt capability and files beyond text with `embeddedContext`.
|
|
12
|
+
images: true,
|
|
13
|
+
files: ["text", "pdf", "binary"],
|
|
14
|
+
mcpServers: true,
|
|
15
|
+
permissionRequests: true,
|
|
16
|
+
// The answer is an option id.
|
|
17
|
+
rejectionMessages: false,
|
|
18
|
+
ignoredSettings: [
|
|
19
|
+
// ACP v1 has no system prompt: session/new takes the folder, the MCP
|
|
20
|
+
// servers and the extra directories only (`ProfiledAcpAgent.spawn`).
|
|
21
|
+
"systemPromptAppend",
|
|
22
|
+
// A prompt runs until the agent ends it: ACP v1 has no turn limit.
|
|
23
|
+
"maxTurns",
|
|
24
|
+
// session/resume and session/load name a session, not a message in it.
|
|
25
|
+
"resumeSessionAt",
|
|
26
|
+
// The agent's credentials are its launch command's: the client gets the
|
|
27
|
+
// launch, the folder and the env only (`AcpClientOptions`).
|
|
28
|
+
"apiKey",
|
|
29
|
+
// ACP v1 cannot ask an agent to run without its tools.
|
|
30
|
+
"disableTools",
|
|
31
|
+
],
|
|
32
|
+
};
|
|
33
|
+
/** The `acp` harness: any ACP v1 agent the operator launches, taken at its `initialize`'s word. */
|
|
34
|
+
export function genericAcpProfile(launch) {
|
|
35
|
+
return { harness: "acp", capabilities: CAPABILITIES, launch };
|
|
36
|
+
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
const sum = (a, b) => (a ?? 0) + (b ?? 0);
|
|
2
|
+
/** Two responses' usage, summed: each counts its own model steps. */
|
|
3
|
+
function addUsage(a, b) {
|
|
4
|
+
if (!b)
|
|
5
|
+
return a;
|
|
6
|
+
if (!a)
|
|
7
|
+
return b;
|
|
8
|
+
return {
|
|
9
|
+
totalTokens: a.totalTokens + b.totalTokens,
|
|
10
|
+
inputTokens: a.inputTokens + b.inputTokens,
|
|
11
|
+
outputTokens: a.outputTokens + b.outputTokens,
|
|
12
|
+
thoughtTokens: sum(a.thoughtTokens, b.thoughtTokens),
|
|
13
|
+
cachedReadTokens: sum(a.cachedReadTokens, b.cachedReadTokens),
|
|
14
|
+
cachedWriteTokens: sum(a.cachedWriteTokens, b.cachedWriteTokens),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
// @ref LLP 0016.000#an-acp-agent-steered-by-prompt — the turn's prompts and its steers, as one state machine
|
|
18
|
+
/**
|
|
19
|
+
* The prompts a turn has in flight: its own, then each steer, sent as a
|
|
20
|
+
* prompt of its own to an agent that takes a prompt while one runs as input
|
|
21
|
+
* to the running turn (Antigravity). Such an agent answers the prompt before
|
|
22
|
+
* at once (`end_turn`, the usage of the steps it finished) and goes on; it
|
|
23
|
+
* refuses a prompt while it asks the person.
|
|
24
|
+
*
|
|
25
|
+
* - Order: steers wait in order, and the head stays the head until it is
|
|
26
|
+
* settled. It is sent once the agent asks nothing (`asking`); it is taken
|
|
27
|
+
* when the prompt before it answers, and refused when its own prompt
|
|
28
|
+
* fails. A refused head goes again, in place, only when a request of the
|
|
29
|
+
* agent's arrived after it was sent (the two crossed); refused with none
|
|
30
|
+
* between, it is undelivered, and the next steer is the head.
|
|
31
|
+
* - Read: a taken steer is read by the first model call that starts after
|
|
32
|
+
* it was taken; the call running then goes on without it. A call ends with
|
|
33
|
+
* a usage report (`usage_update`, after every call; its tool calls are
|
|
34
|
+
* announced after it), and its output is its message and thought chunks.
|
|
35
|
+
* Taken while a call streams (from its first chunk to its usage report),
|
|
36
|
+
* it waits for that report, whatever tool calls are open (a command left
|
|
37
|
+
* running in the background). Otherwise, while a tool call of the turn is
|
|
38
|
+
* open, or after a call's usage report or the end of its last open tool
|
|
39
|
+
* call with no chunk since, no call is known to run: it is read at the
|
|
40
|
+
* next chunk, or the next usage report for a call that made none. After a tool ends the wire cannot
|
|
41
|
+
* tell whether Antigravity's next call carries it (one run of three did):
|
|
42
|
+
* the echo is then one message early at worst, never after its reply. Otherwise one usage report must pass first, then the same. A steer's
|
|
43
|
+
* own answer reads nothing (a later steer supersedes it at once); one still
|
|
44
|
+
* unread when the turn ends with its prompts answered, cancelled included,
|
|
45
|
+
* is read then, and none when the turn fails. The echo goes out just before
|
|
46
|
+
* what it is read at (`update`). One whose own prompt fails was never read,
|
|
47
|
+
* and is undelivered.
|
|
48
|
+
* - Result: the turn ends when no prompt is in flight, with the stop reason
|
|
49
|
+
* of the newest prompt sent that answered and the usage of every answer.
|
|
50
|
+
*/
|
|
51
|
+
export class TurnPrompts {
|
|
52
|
+
options;
|
|
53
|
+
flying = [];
|
|
54
|
+
/** Steers not yet taken, the head first. */
|
|
55
|
+
waiting = [];
|
|
56
|
+
/** Steers taken and not yet read, in order. */
|
|
57
|
+
taken = [];
|
|
58
|
+
/** The turn's tool calls not yet `completed` or `failed`: while one is, no model call runs. */
|
|
59
|
+
tools = new Set();
|
|
60
|
+
/** The turn has made a chunk or a tool update. */
|
|
61
|
+
produced = false;
|
|
62
|
+
/** No call's output since a call (its usage report, after output) or the last open tool call ended. */
|
|
63
|
+
betweenCalls = false;
|
|
64
|
+
/** A model call is streaming, for certain: from a chunk to the next usage report. */
|
|
65
|
+
streaming = false;
|
|
66
|
+
sent = 0;
|
|
67
|
+
newest;
|
|
68
|
+
usage;
|
|
69
|
+
failure;
|
|
70
|
+
done = false;
|
|
71
|
+
pumping = false;
|
|
72
|
+
settle = () => { };
|
|
73
|
+
/** Settles once no prompt is in flight, or the agent is gone. */
|
|
74
|
+
settled = new Promise((resolve) => {
|
|
75
|
+
this.settle = resolve;
|
|
76
|
+
});
|
|
77
|
+
constructor(options) {
|
|
78
|
+
this.options = options;
|
|
79
|
+
}
|
|
80
|
+
/** Whether the turn still takes a prompt. */
|
|
81
|
+
get open() {
|
|
82
|
+
return !this.done;
|
|
83
|
+
}
|
|
84
|
+
/** The turn's own prompt. */
|
|
85
|
+
start(prompt) {
|
|
86
|
+
this.send(prompt);
|
|
87
|
+
}
|
|
88
|
+
/** A steer: it waits behind the steers before it. */
|
|
89
|
+
steer(steerId, prompt) {
|
|
90
|
+
this.waiting.push({ steerId, prompt });
|
|
91
|
+
void this.pump();
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* An update of the turn's session, before it goes out: a chunk reads the
|
|
95
|
+
* steers taken whose call has started, a usage report ends a call.
|
|
96
|
+
*/
|
|
97
|
+
update(update) {
|
|
98
|
+
switch (update.sessionUpdate) {
|
|
99
|
+
case "tool_call":
|
|
100
|
+
case "tool_call_update":
|
|
101
|
+
this.produced = true;
|
|
102
|
+
// An update carries only what changed: one with no status moves nothing.
|
|
103
|
+
if (update.status === "completed" || update.status === "failed") {
|
|
104
|
+
// The turn's last open tool call ended: no call's output has come since.
|
|
105
|
+
// One a streaming call leaves behind ends nothing: that call goes on.
|
|
106
|
+
if (this.tools.delete(update.toolCallId) && this.tools.size === 0 && !this.streaming)
|
|
107
|
+
this.betweenCalls = true;
|
|
108
|
+
}
|
|
109
|
+
else if (update.status || update.sessionUpdate === "tool_call") {
|
|
110
|
+
this.tools.add(update.toolCallId);
|
|
111
|
+
this.betweenCalls = false;
|
|
112
|
+
}
|
|
113
|
+
return;
|
|
114
|
+
case "agent_message_chunk":
|
|
115
|
+
case "agent_thought_chunk":
|
|
116
|
+
[this.produced, this.betweenCalls, this.streaming] = [true, false, true];
|
|
117
|
+
this.read(this.ready());
|
|
118
|
+
return;
|
|
119
|
+
case "usage_update":
|
|
120
|
+
// The report before any output (a session's first prompt) ends no call.
|
|
121
|
+
this.betweenCalls ||= this.produced;
|
|
122
|
+
this.streaming = false;
|
|
123
|
+
// A call that made no chunk read them at its end.
|
|
124
|
+
this.read(this.ready());
|
|
125
|
+
for (const steer of this.taken)
|
|
126
|
+
steer.reports = Math.max(0, (steer.reports ?? 0) - 1);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** How many steers taken, in order, are past every usage report they waited for. */
|
|
131
|
+
ready() {
|
|
132
|
+
const waiting = this.taken.findIndex((steer) => (steer.reports ?? 0) > 0);
|
|
133
|
+
return waiting === -1 ? this.taken.length : waiting;
|
|
134
|
+
}
|
|
135
|
+
/** The agent is gone: nothing more is told. */
|
|
136
|
+
close() {
|
|
137
|
+
this.done = true;
|
|
138
|
+
this.settle();
|
|
139
|
+
}
|
|
140
|
+
send(prompt, steer) {
|
|
141
|
+
const sent = { order: ++this.sent, ...(steer && { steer }) };
|
|
142
|
+
this.flying.push(sent);
|
|
143
|
+
this.options.send(prompt).then((response) => this.answered(sent, response), (error) => this.refused(sent, error));
|
|
144
|
+
}
|
|
145
|
+
answered(prompt, response) {
|
|
146
|
+
if (this.done)
|
|
147
|
+
return;
|
|
148
|
+
this.usage = addUsage(this.usage, response.usage);
|
|
149
|
+
if (!this.newest || prompt.order > this.newest.order)
|
|
150
|
+
this.newest = { order: prompt.order, stopReason: response.stopReason };
|
|
151
|
+
const next = this.flying[this.flying.indexOf(prompt) + 1];
|
|
152
|
+
this.remove(prompt);
|
|
153
|
+
const { steer } = prompt;
|
|
154
|
+
// Answered before the prompt before it: taken all the same. Its answer
|
|
155
|
+
// reads nothing: a later steer supersedes it as one supersedes the turn's own.
|
|
156
|
+
if (steer && this.waiting[0] === steer)
|
|
157
|
+
this.take();
|
|
158
|
+
// Superseded: the agent took the steer after it.
|
|
159
|
+
if (next?.steer && this.waiting[0] === next.steer)
|
|
160
|
+
this.take();
|
|
161
|
+
this.endIfIdle();
|
|
162
|
+
}
|
|
163
|
+
refused(prompt, error) {
|
|
164
|
+
if (this.done)
|
|
165
|
+
return;
|
|
166
|
+
this.remove(prompt);
|
|
167
|
+
const { steer } = prompt;
|
|
168
|
+
if (!steer)
|
|
169
|
+
this.failure = { error };
|
|
170
|
+
else if (this.waiting[0] === steer) {
|
|
171
|
+
const crossed = this.options.asking?.arrivedSince(steer.mark ?? 0) ?? false;
|
|
172
|
+
steer.mark = undefined;
|
|
173
|
+
// Refused for a reason of its own: undelivered.
|
|
174
|
+
if (!crossed)
|
|
175
|
+
this.waiting.shift();
|
|
176
|
+
}
|
|
177
|
+
else if (this.taken.includes(steer))
|
|
178
|
+
this.taken.splice(this.taken.indexOf(steer), 1);
|
|
179
|
+
this.endIfIdle();
|
|
180
|
+
void this.pump();
|
|
181
|
+
}
|
|
182
|
+
/** The head is taken: the next steer is the head. */
|
|
183
|
+
take() {
|
|
184
|
+
const steer = this.waiting.shift();
|
|
185
|
+
if (!steer)
|
|
186
|
+
return;
|
|
187
|
+
steer.mark = undefined;
|
|
188
|
+
// With a tool call open, or a call ended and nothing since, no model call runs;
|
|
189
|
+
// otherwise the running one began without it.
|
|
190
|
+
// A call streaming waits for its report, whatever tool calls are open.
|
|
191
|
+
steer.reports = !this.streaming && (this.tools.size > 0 || this.betweenCalls) ? 0 : 1;
|
|
192
|
+
// It starts the next call: a steer taken after it waits for that call's report.
|
|
193
|
+
this.betweenCalls = false;
|
|
194
|
+
this.taken.push(steer);
|
|
195
|
+
void this.pump();
|
|
196
|
+
}
|
|
197
|
+
/** Read the first `count` steers taken. */
|
|
198
|
+
read(count) {
|
|
199
|
+
for (const steer of this.taken.splice(0, count))
|
|
200
|
+
this.options.ends.read(steer.steerId);
|
|
201
|
+
}
|
|
202
|
+
/** Send the head once the agent asks nothing, unless it is in flight already. */
|
|
203
|
+
async pump() {
|
|
204
|
+
if (this.pumping)
|
|
205
|
+
return;
|
|
206
|
+
this.pumping = true;
|
|
207
|
+
try {
|
|
208
|
+
for (;;) {
|
|
209
|
+
const head = this.waiting[0];
|
|
210
|
+
if (!head || head.mark !== undefined || !this.sending)
|
|
211
|
+
return;
|
|
212
|
+
const { asking } = this.options;
|
|
213
|
+
if (asking?.pending) {
|
|
214
|
+
// The turn's end releases a steer still waiting: it is undelivered.
|
|
215
|
+
await Promise.race([asking.idle(), this.settled]);
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
head.mark = asking?.arrived ?? 0;
|
|
219
|
+
this.send(head.prompt, head);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
finally {
|
|
223
|
+
this.pumping = false;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
/** Whether a steer may still go: the turn runs, and its own prompt has not failed. */
|
|
227
|
+
get sending() {
|
|
228
|
+
return !this.done && !this.failure && !this.options.signal.aborted;
|
|
229
|
+
}
|
|
230
|
+
remove(prompt) {
|
|
231
|
+
this.flying.splice(this.flying.indexOf(prompt), 1);
|
|
232
|
+
}
|
|
233
|
+
endIfIdle() {
|
|
234
|
+
if (this.flying.length > 0)
|
|
235
|
+
return;
|
|
236
|
+
this.close();
|
|
237
|
+
if (this.failure) {
|
|
238
|
+
this.options.ends.failed(this.failure.error);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
// The turn is over with its prompts answered, cancelled included: what the agent took, it has.
|
|
242
|
+
this.read(this.taken.length);
|
|
243
|
+
this.options.ends.ended({
|
|
244
|
+
stopReason: this.newest?.stopReason ?? "end_turn",
|
|
245
|
+
...(this.usage && { usage: this.usage }),
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { inputReply, inputRequest } from "../elicitation.js";
|
|
2
|
-
import { CANCELLED_OUTCOME, autoDecide, chooseOutcome, permissionModeOf, permissionToolCall, } from "./mappings.js";
|
|
2
|
+
import { CANCELLED_OUTCOME, asksInMode, autoDecide, chooseOutcome, permissionModeOf, permissionToolCall, } from "./mappings.js";
|
|
3
3
|
/** Answer an agent permission request against the session's current turn. */
|
|
4
4
|
export async function answerPermission(scope, request) {
|
|
5
5
|
if (request.sessionId !== scope.acpSessionId)
|
|
@@ -9,14 +9,14 @@ export async function answerPermission(scope, request) {
|
|
|
9
9
|
// Leaving a mode (claude-agent-acp's plan approval) is the user's choice,
|
|
10
10
|
// from the agent's own options, in any mode that asks: they say how it
|
|
11
11
|
// goes on. A turn that never asks (`dont_ask`, `bypass_permissions`)
|
|
12
|
-
// decides it as before; other requests ask
|
|
12
|
+
// decides it as before; other requests ask as the mode says (`asksInMode`).
|
|
13
13
|
const plan = request.toolCall.kind === "switch_mode" &&
|
|
14
14
|
turn?.mode !== "dont_ask" &&
|
|
15
15
|
turn?.mode !== "bypass_permissions";
|
|
16
|
-
if (!turn?.broker || !(plan || (turn.mode ?? "default")
|
|
16
|
+
if (!turn?.broker || !(plan || asksInMode(request, turn.mode ?? "default"))) {
|
|
17
17
|
return autoDecide(request, turn?.mode);
|
|
18
18
|
}
|
|
19
|
-
const decision = await turn.broker(permissionToolCall(request), {
|
|
19
|
+
const decision = await turn.broker(permissionToolCall(request, scope.describe?.(request.toolCall)), {
|
|
20
20
|
signal: turn.signal,
|
|
21
21
|
...(plan && {
|
|
22
22
|
options: request.options.map(({ optionId, name, kind }) => ({ optionId, name, kind })),
|
|
@@ -87,7 +87,10 @@ export class ClaudeCliLogin {
|
|
|
87
87
|
const cliPath = await this.commandPath();
|
|
88
88
|
return [
|
|
89
89
|
...LOGINS,
|
|
90
|
-
terminalMethod([cliPath, "auth", "login"],
|
|
90
|
+
terminalMethod([cliPath, "auth", "login"], {
|
|
91
|
+
variable: STORE_VARIABLE,
|
|
92
|
+
env: this.env(options),
|
|
93
|
+
}),
|
|
91
94
|
];
|
|
92
95
|
}
|
|
93
96
|
async status(options) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { AsyncQueue, generateUUIDv7 } from "../../../protocol/index.js";
|
|
1
|
+
import { AsyncQueue, classifyError, generateUUIDv7 } from "../../../protocol/index.js";
|
|
2
2
|
import { onAbort, untilAborted, withTimeout } from "../../utils/deadline.js";
|
|
3
|
-
import { SessionResumeError } from "../../utils/errors.js";
|
|
3
|
+
import { AgentExitedError, SessionResumeError } from "../../utils/errors.js";
|
|
4
4
|
import { CANCEL_CONFIRM_MS } from "../base.js";
|
|
5
5
|
import { IDLE_TIMEOUT_MS, ProcessPool, } from "../process-pool.js";
|
|
6
6
|
import { createCanUseTool, createOnElicitation, startQuery } from "./approvals.js";
|
|
@@ -10,7 +10,6 @@ import { ClaudeMcpConnections } from "./mcp.js";
|
|
|
10
10
|
import { buildClaudeOptions, claudeSpawnKey, fromSdkPermissionMode, toSdkPermissionMode, } from "./options.js";
|
|
11
11
|
import { planUsedUp } from "./outcome.js";
|
|
12
12
|
import { notePairedReleases } from "./sdk-pair.js";
|
|
13
|
-
import { withStderr } from "./spawn.js";
|
|
14
13
|
import { ClaudeSpend } from "./spend.js";
|
|
15
14
|
import { ClaudeTurn } from "./turn.js";
|
|
16
15
|
/** Claude repeats a message it was sent, where it reads it (`--replay-user-messages`). */
|
|
@@ -72,6 +71,8 @@ export class ClaudeSession {
|
|
|
72
71
|
planWindows = new Map();
|
|
73
72
|
/** The `claude` process the engine started for it; none under an operator's spawner. */
|
|
74
73
|
native;
|
|
74
|
+
/** The CLI was still running when the session asked it to stop. */
|
|
75
|
+
stoppedCli = false;
|
|
75
76
|
/** Its end, once it began: settles when its process is stopped. */
|
|
76
77
|
ending;
|
|
77
78
|
closeHandlers = new Set();
|
|
@@ -436,6 +437,8 @@ export class ClaudeSession {
|
|
|
436
437
|
this.promptQueue.end();
|
|
437
438
|
this.turn?.end();
|
|
438
439
|
this.turn = null;
|
|
440
|
+
const child = this.native?.child;
|
|
441
|
+
this.stoppedCli = child?.exitCode === null && child.signalCode === null;
|
|
439
442
|
try {
|
|
440
443
|
// The SDK documents close() as the forceful subprocess/MCP cleanup path.
|
|
441
444
|
query?.close();
|
|
@@ -459,25 +462,49 @@ export class ClaudeSession {
|
|
|
459
462
|
async consumeEvents() {
|
|
460
463
|
if (!this.query)
|
|
461
464
|
return;
|
|
465
|
+
let initializing = true;
|
|
466
|
+
// The SDK already sent initialize. Its receipt is independent of the first turn event.
|
|
467
|
+
void this.query.initializationResult?.().then(() => {
|
|
468
|
+
initializing = false;
|
|
469
|
+
}, () => { });
|
|
462
470
|
try {
|
|
463
471
|
for await (const event of this.query) {
|
|
464
472
|
this.cliStarted = true;
|
|
465
473
|
this.observe(event);
|
|
466
474
|
this.route(event);
|
|
475
|
+
if (this.startupError)
|
|
476
|
+
return;
|
|
467
477
|
}
|
|
478
|
+
// @ref LLP 0016.000#when-a-process-exits-before-answering — only the session knows who ended this query
|
|
479
|
+
if (!this.closed && (this.turn || !this.cliStarted || this.state === "starting"))
|
|
480
|
+
throw new AgentExitedError("Claude ended the turn without a result");
|
|
468
481
|
}
|
|
469
482
|
catch (thrown) {
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
if (this.turn)
|
|
473
|
-
this.turn.fail(err);
|
|
474
|
-
else
|
|
475
|
-
this.startupError = err;
|
|
483
|
+
const pending = initializing ? ["initialize"] : this.turn ? ["the turn"] : [];
|
|
484
|
+
await this.failStream(thrown, pending);
|
|
476
485
|
}
|
|
477
486
|
finally {
|
|
478
487
|
await this.terminate();
|
|
479
488
|
}
|
|
480
489
|
}
|
|
490
|
+
async failStream(cause, pending) {
|
|
491
|
+
const native = this.native;
|
|
492
|
+
const exit = await native?.exitAfterDisconnect();
|
|
493
|
+
if (this.stoppedCli || this.turn?.cancelled)
|
|
494
|
+
return;
|
|
495
|
+
let error = cause;
|
|
496
|
+
if (native && exit && (exit.code !== null || exit.signal !== null)) {
|
|
497
|
+
const resume = this.config.resumeSessionId;
|
|
498
|
+
error =
|
|
499
|
+
resume && !this.queryBegun && classifyError(native.stderrTail) === "resume_failed"
|
|
500
|
+
? new SessionResumeError(resume, native.stderrTail.trim())
|
|
501
|
+
: native.exitError("Claude Code", exit, pending);
|
|
502
|
+
}
|
|
503
|
+
if (this.turn)
|
|
504
|
+
this.turn.fail(error);
|
|
505
|
+
else
|
|
506
|
+
this.startupError = error;
|
|
507
|
+
}
|
|
481
508
|
/** What an event says of the session: its native id, its mode, plan windows, spend, steers read. */
|
|
482
509
|
observe(event) {
|
|
483
510
|
if (event.type === "system" && event.subtype === "init") {
|
|
@@ -550,11 +577,12 @@ export class ClaudeSession {
|
|
|
550
577
|
settleUnshown(result) {
|
|
551
578
|
if (result.subtype !== "success" && result.origin === undefined) {
|
|
552
579
|
const reason = result.errors.join("; ") || "Claude failed during startup";
|
|
553
|
-
|
|
580
|
+
this.startupError = this.config.resumeSessionId
|
|
554
581
|
? new SessionResumeError(this.config.resumeSessionId, reason, {
|
|
555
582
|
cause: new Error(reason),
|
|
556
583
|
})
|
|
557
584
|
: new Error(reason);
|
|
585
|
+
return;
|
|
558
586
|
}
|
|
559
587
|
this.spend.charge(result);
|
|
560
588
|
if (this.state === "idle")
|
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import { EventEmitter } from "node:events";
|
|
2
2
|
import { StringDecoder } from "node:string_decoder";
|
|
3
|
-
import { redactSecrets } from "../stderr-tail.js";
|
|
4
|
-
/** The longest stderr tail an SDK exit error carries. */
|
|
5
|
-
const SDK_TAIL_MAX = 2048;
|
|
6
3
|
/**
|
|
7
4
|
* The CLI as the SDK sees it: the child's pipes, and its `exit` once stderr
|
|
8
5
|
* has closed too (at most 200 ms after it), as the SDK's own spawner delivers
|
|
@@ -100,26 +97,3 @@ export function claudeSpawner(spawn, operator, stderr) {
|
|
|
100
97
|
return new SpawnedClaude(native, stdin, stdout);
|
|
101
98
|
};
|
|
102
99
|
}
|
|
103
|
-
/** The SDK's bare exit error: what its own spawner would have followed with `. stderr: <tail>`. */
|
|
104
|
-
const BARE_EXIT = /^Claude Code process (?:exited with code \S+|terminated by signal \S+)$/;
|
|
105
|
-
/**
|
|
106
|
-
* Put the CLI's own words back into the SDK's exit error, as the SDK's own
|
|
107
|
-
* spawner words it ("Claude Code process exited with code 1. stderr: …"):
|
|
108
|
-
* the last 2 KB, trimmed, with its secrets redacted (the SDK redacts its own;
|
|
109
|
-
* this marks each `[REDACTED]`). The SDK builds that tail only from its own
|
|
110
|
-
* spawner. Its message alone changes, so the error keeps its class and fields.
|
|
111
|
-
*/
|
|
112
|
-
export function withStderr(error, tail) {
|
|
113
|
-
if (!(error instanceof Error) || !BARE_EXIT.test(error.message))
|
|
114
|
-
return error;
|
|
115
|
-
let kept = tail.length > SDK_TAIL_MAX ? tail.slice(-SDK_TAIL_MAX) : tail;
|
|
116
|
-
// A cut that splits a surrogate pair drops its half, as the SDK's does.
|
|
117
|
-
const first = kept.charCodeAt(0);
|
|
118
|
-
if (first >= 0xdc00 && first <= 0xdfff)
|
|
119
|
-
kept = kept.slice(1);
|
|
120
|
-
// As the SDK redacts its own tail, before it quotes it.
|
|
121
|
-
kept = redactSecrets(kept).trim();
|
|
122
|
-
if (kept)
|
|
123
|
-
error.message = `${error.message}. stderr: ${kept}`;
|
|
124
|
-
return error;
|
|
125
|
-
}
|