@e2e-dev/acp 0.0.0-stage → 0.0.0
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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +28 -2
- package/dist/.build.json +1 -0
- package/dist/executor.d.ts +13 -0
- package/dist/executor.js +284 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/mcp.d.ts +35 -0
- package/dist/mcp.js +89 -0
- package/dist/presets.d.ts +18 -0
- package/dist/presets.js +136 -0
- package/dist/session.d.ts +58 -0
- package/dist/session.js +287 -0
- package/dist/tools.d.ts +53 -0
- package/dist/tools.js +131 -0
- package/dist/types.d.ts +29 -0
- package/dist/types.js +1 -0
- package/package.json +62 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @e2e-dev/acp
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Run `agent.act` and `agent.assert` on a coding agent you already use, such as
|
|
4
|
+
Claude Code, Codex, or Cursor, over the
|
|
5
|
+
[Agent Client Protocol](https://agentclientprotocol.com). The agent signs in
|
|
6
|
+
with its own login, so e2e needs no model provider.
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install -D @e2e-dev/acp @agentclientprotocol/claude-agent-acp
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import type { E2EConfig } from 'e2e';
|
|
14
|
+
import { web } from '@e2e-dev/web';
|
|
15
|
+
import { claudeCode } from '@e2e-dev/acp';
|
|
16
|
+
|
|
17
|
+
export default {
|
|
18
|
+
targets: [{ engine: web(), app: { url: 'http://localhost:3000' } }],
|
|
19
|
+
agents: { default: { executor: claudeCode({ model: 'sonnet' }) } },
|
|
20
|
+
} satisfies E2EConfig;
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
- `claudeCode()` starts Claude Code through `@agentclientprotocol/claude-agent-acp`.
|
|
24
|
+
- `codex()` starts Codex through `@agentclientprotocol/codex-acp`.
|
|
25
|
+
- `acp({ command, args })` starts any other agent that speaks ACP on stdio.
|
|
26
|
+
|
|
27
|
+
The runner records every action, so steps replay from the cache without the
|
|
28
|
+
agent. See the [guide](https://e2e.tester.army/docs/acp) for options,
|
|
29
|
+
containment, and limits.
|
package/dist/.build.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":"0.0.0"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type StepExecutor } from 'e2e';
|
|
2
|
+
import type { AcpAgentOptions, AcpOptions } from './types.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Runs `agent.act` and `agent.assert` on any agent that speaks ACP on stdio:
|
|
5
|
+
* one agent session per test attempt, started by its first agent step and
|
|
6
|
+
* closed when the attempt ends. `claudeCode()` and `codex()` start those
|
|
7
|
+
* agents with the step tools as their only tools.
|
|
8
|
+
*/
|
|
9
|
+
export declare function acp(options: AcpOptions): StepExecutor;
|
|
10
|
+
/** Claude Code through Zed's adapter (`@agentclientprotocol/claude-agent-acp`), signed in with your `claude` login. */
|
|
11
|
+
export declare function claudeCode(options?: AcpAgentOptions): StepExecutor;
|
|
12
|
+
/** Codex through its adapter (`@agentclientprotocol/codex-acp`), signed in with your `codex` login. */
|
|
13
|
+
export declare function codex(options?: AcpAgentOptions): StepExecutor;
|
package/dist/executor.js
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { AgentError } from 'e2e';
|
|
3
|
+
import { BASE_RULES, VERDICT_RULES } from 'e2e/agent';
|
|
4
|
+
import { ConfigurationError } from 'e2e/engine';
|
|
5
|
+
import { claudeCodeLaunch, codexLaunch } from './presets.js';
|
|
6
|
+
import { startSession } from './session.js';
|
|
7
|
+
import { activeStep, missingTools, openingScreen, stepTools, verdictOf } from './tools.js';
|
|
8
|
+
/** What the session is, before the built-in agent's rules for the same tools. */
|
|
9
|
+
const SESSION_RULES = `This conversation drives a real application for an end-to-end test, one step per message: an action to perform, or an assertion to judge, with the current screen.
|
|
10
|
+
Use only the tools of this conversation. You have no access to the application's source, files, shell, or network.
|
|
11
|
+
The rules below are for each step. A step ends when you call complete_step; then wait for the next message. For an assertion, judge the condition from the screen without changing anything.`;
|
|
12
|
+
const RULES = `${SESSION_RULES}\n\n${BASE_RULES}\n\n${VERDICT_RULES}`;
|
|
13
|
+
/** How long a step waits for the turn of a step the runner gave up on to stop. */
|
|
14
|
+
const LAST_TURN_GRACE_MS = 5_000;
|
|
15
|
+
/**
|
|
16
|
+
* Runs `agent.act` and `agent.assert` on any agent that speaks ACP on stdio:
|
|
17
|
+
* one agent session per test attempt, started by its first agent step and
|
|
18
|
+
* closed when the attempt ends. `claudeCode()` and `codex()` start those
|
|
19
|
+
* agents with the step tools as their only tools.
|
|
20
|
+
*/
|
|
21
|
+
export function acp(options) {
|
|
22
|
+
if (typeof options !== 'object' || options === null || typeof options.command !== 'string' || options.command === '') {
|
|
23
|
+
throw new ConfigurationError('INVALID_CONFIG', "acp() takes { command, args }, e.g. acp({ command: 'agent', args: ['acp'] }), or use claudeCode() or codex()");
|
|
24
|
+
}
|
|
25
|
+
const launch = {
|
|
26
|
+
command: options.command,
|
|
27
|
+
args: options.args ?? [],
|
|
28
|
+
env: options.env,
|
|
29
|
+
model: options.model,
|
|
30
|
+
mode: options.mode,
|
|
31
|
+
sessionMeta: options.sessionMeta,
|
|
32
|
+
};
|
|
33
|
+
return executor(options.name ?? 'acp', options, async () => launch);
|
|
34
|
+
}
|
|
35
|
+
/** Claude Code through Zed's adapter (`@agentclientprotocol/claude-agent-acp`), signed in with your `claude` login. */
|
|
36
|
+
export function claudeCode(options = {}) {
|
|
37
|
+
return executor(options.name ?? 'claude-code', options, claudeCodeLaunch(options));
|
|
38
|
+
}
|
|
39
|
+
/** Codex through its adapter (`@agentclientprotocol/codex-acp`), signed in with your `codex` login. */
|
|
40
|
+
export function codex(options = {}) {
|
|
41
|
+
return executor(options.name ?? 'codex', options, codexLaunch(options));
|
|
42
|
+
}
|
|
43
|
+
function executor(name, options, launch) {
|
|
44
|
+
// Where an attempt's session lives in `attempt.memory`: one key per executor, so two ACP agents never share a session.
|
|
45
|
+
const memoryKey = `@e2e-dev/acp.session.${randomUUID()}`;
|
|
46
|
+
return {
|
|
47
|
+
name,
|
|
48
|
+
version: '1',
|
|
49
|
+
cache: 'inherit',
|
|
50
|
+
async runStep(ctx) {
|
|
51
|
+
const { held, session } = await ready(ctx, launch, memoryKey);
|
|
52
|
+
const active = activeStep(ctx);
|
|
53
|
+
const text = await stepMessage(active, held.tools, held.introduced ? undefined : [RULES, options.system, ctx.agentContext]);
|
|
54
|
+
// A step that ended while its screen was read sends no prompt, which no cancel would reach.
|
|
55
|
+
ctx.signal.throwIfAborted();
|
|
56
|
+
held.introduced = true;
|
|
57
|
+
held.slot.active = active;
|
|
58
|
+
const cancel = () => session.cancel();
|
|
59
|
+
ctx.signal.addEventListener('abort', cancel, { once: true });
|
|
60
|
+
let report;
|
|
61
|
+
try {
|
|
62
|
+
const turn = session.prompt(text);
|
|
63
|
+
held.turn = turn;
|
|
64
|
+
report = await untilAborted(turn, ctx.signal);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
ctx.signal.throwIfAborted();
|
|
68
|
+
session.close();
|
|
69
|
+
ctx.attempt.memory.delete(memoryKey);
|
|
70
|
+
throw new AgentError('MODEL_PROVIDER_FAILED', `The ACP agent session failed: ${message(error)}`, { cause: error });
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
if (held.slot.active === active)
|
|
74
|
+
held.slot.active = undefined;
|
|
75
|
+
ctx.signal.removeEventListener('abort', cancel);
|
|
76
|
+
}
|
|
77
|
+
ctx.attachTranscript(transcript(text, active, report));
|
|
78
|
+
if (!ctx.signal.aborted)
|
|
79
|
+
recordTurn(ctx, session, report);
|
|
80
|
+
if (active.halted !== undefined)
|
|
81
|
+
throw active.halted;
|
|
82
|
+
ctx.signal.throwIfAborted();
|
|
83
|
+
if (active.denied !== undefined) {
|
|
84
|
+
// The conversation holds what the tool read: the next step starts a fresh agent.
|
|
85
|
+
session.close();
|
|
86
|
+
ctx.attempt.memory.delete(memoryKey);
|
|
87
|
+
return ownToolUsed(active.denied);
|
|
88
|
+
}
|
|
89
|
+
return verdictOf(active) ?? noConclusion(report);
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/** The attempt's session once the turn of the step before it stopped, or a fresh one when that turn's end closed it. */
|
|
94
|
+
async function ready(ctx, launch, memoryKey) {
|
|
95
|
+
const held = hold(ctx, launch, memoryKey);
|
|
96
|
+
let session;
|
|
97
|
+
try {
|
|
98
|
+
session = await untilAborted(held.session, ctx.signal);
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
// The next step starts a fresh agent rather than wait on this one.
|
|
102
|
+
ctx.attempt.memory.delete(memoryKey);
|
|
103
|
+
ctx.signal.throwIfAborted();
|
|
104
|
+
if (error instanceof ConfigurationError)
|
|
105
|
+
throw error;
|
|
106
|
+
throw new AgentError('MODEL_PROVIDER_FAILED', `The ACP agent did not start: ${message(error)}`, { cause: error });
|
|
107
|
+
}
|
|
108
|
+
if (!(await settled(held.turn, LAST_TURN_GRACE_MS))) {
|
|
109
|
+
session.close();
|
|
110
|
+
ctx.attempt.memory.delete(memoryKey);
|
|
111
|
+
throw new AgentError('MODEL_PROVIDER_FAILED', 'The ACP agent did not stop the turn of a step that already ended');
|
|
112
|
+
}
|
|
113
|
+
// A tool of the agent's own that ran as that turn stopped closed the session and dropped it from the attempt.
|
|
114
|
+
if (ctx.attempt.memory.get(memoryKey) !== held)
|
|
115
|
+
return ready(ctx, launch, memoryKey);
|
|
116
|
+
return { held, session };
|
|
117
|
+
}
|
|
118
|
+
/** Counts one prompt turn as one model call, with what the agent reported of it. */
|
|
119
|
+
function recordTurn(ctx, session, report) {
|
|
120
|
+
const usage = report.usage;
|
|
121
|
+
ctx.budgets.recordModelCall({
|
|
122
|
+
startedAt: report.startedAt,
|
|
123
|
+
durationMs: report.durationMs,
|
|
124
|
+
...(session.agentName === undefined ? {} : { provider: session.agentName }),
|
|
125
|
+
...(session.modelId === undefined ? {} : { modelId: session.modelId }),
|
|
126
|
+
...(usage === undefined
|
|
127
|
+
? {}
|
|
128
|
+
: {
|
|
129
|
+
inputTokens: inputTokens(usage),
|
|
130
|
+
outputTokens: usage.outputTokens,
|
|
131
|
+
...(usage.cachedReadTokens == null ? {} : { cacheReadTokens: usage.cachedReadTokens }),
|
|
132
|
+
...(usage.cachedWriteTokens == null ? {} : { cacheWriteTokens: usage.cachedWriteTokens }),
|
|
133
|
+
}),
|
|
134
|
+
...(report.costUsd === undefined ? {} : { estimatedCostUsd: report.costUsd }),
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
/** The attempt's session, started on its first step and closed when the attempt ends. */
|
|
138
|
+
function hold(ctx, launch, memoryKey) {
|
|
139
|
+
const existing = ctx.attempt.memory.get(memoryKey);
|
|
140
|
+
if (existing !== undefined)
|
|
141
|
+
return existing;
|
|
142
|
+
let session;
|
|
143
|
+
let closed = false;
|
|
144
|
+
const slot = { active: undefined, halt: () => session?.cancel() };
|
|
145
|
+
const tools = stepTools(slot, ctx);
|
|
146
|
+
const ownTool = (title) => {
|
|
147
|
+
if (slot.active === undefined) {
|
|
148
|
+
// Run in the tail of a step that already ended: no step to fail, so no later step gets this conversation.
|
|
149
|
+
session?.close();
|
|
150
|
+
if (ctx.attempt.memory.get(memoryKey) === held)
|
|
151
|
+
ctx.attempt.memory.delete(memoryKey);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
slot.active.denied ??= title;
|
|
155
|
+
session?.cancel();
|
|
156
|
+
};
|
|
157
|
+
const held = {
|
|
158
|
+
slot,
|
|
159
|
+
tools,
|
|
160
|
+
introduced: false,
|
|
161
|
+
turn: undefined,
|
|
162
|
+
session: launch()
|
|
163
|
+
// The first step's end stops a startup still in progress; an open session lives until the attempt ends.
|
|
164
|
+
.then((how) => startSession(how, tools, AbortSignal.any([ctx.attempt.signal, ctx.signal]), ownTool))
|
|
165
|
+
.then((started) => {
|
|
166
|
+
session = started;
|
|
167
|
+
if (closed)
|
|
168
|
+
started.close();
|
|
169
|
+
return started;
|
|
170
|
+
}),
|
|
171
|
+
};
|
|
172
|
+
held.session.catch(() => undefined);
|
|
173
|
+
ctx.attempt.memory.set(memoryKey, held);
|
|
174
|
+
ctx.attempt.signal.addEventListener('abort', () => {
|
|
175
|
+
closed = true;
|
|
176
|
+
session?.close();
|
|
177
|
+
// Serial members share one memory, each with its own attempt: the next one starts its own session.
|
|
178
|
+
if (ctx.attempt.memory.get(memoryKey) === held)
|
|
179
|
+
ctx.attempt.memory.delete(memoryKey);
|
|
180
|
+
}, { once: true });
|
|
181
|
+
return held;
|
|
182
|
+
}
|
|
183
|
+
/** Waits for `promise`, or rejects when the step ends first. */
|
|
184
|
+
function untilAborted(promise, signal) {
|
|
185
|
+
if (signal.aborted)
|
|
186
|
+
return Promise.reject(signal.reason);
|
|
187
|
+
let onAbort;
|
|
188
|
+
const aborted = new Promise((_, reject) => {
|
|
189
|
+
onAbort = () => reject(signal.reason);
|
|
190
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
191
|
+
});
|
|
192
|
+
return Promise.race([promise, aborted]).finally(() => signal.removeEventListener('abort', onAbort));
|
|
193
|
+
}
|
|
194
|
+
/** The message for one step: the rules on the first, then the step, what already ran, and the screen. */
|
|
195
|
+
async function stepMessage(active, tools, introduction) {
|
|
196
|
+
const { ctx } = active;
|
|
197
|
+
const parts = [];
|
|
198
|
+
if (introduction !== undefined)
|
|
199
|
+
parts.push(...introduction.filter((part) => part !== undefined && part.trim() !== ''));
|
|
200
|
+
const { step } = ctx;
|
|
201
|
+
parts.push(`${step.kind === 'assert' ? 'Assertion' : 'Step'}: ${step.instruction}`);
|
|
202
|
+
const params = plainParams(step.params);
|
|
203
|
+
if (params !== undefined)
|
|
204
|
+
parts.push(`Parameters: ${JSON.stringify(params)}`);
|
|
205
|
+
if (step.secrets.length > 0) {
|
|
206
|
+
parts.push(`Secrets to fill with type_secret, by name: ${step.secrets.map((secret) => `${secret.name} (${secret.purpose})`).join(', ')}`);
|
|
207
|
+
}
|
|
208
|
+
const missing = missingTools(active, tools);
|
|
209
|
+
if (missing.length > 0)
|
|
210
|
+
parts.push(`Not offered in this step: ${missing.join(', ')}.`);
|
|
211
|
+
if (ctx.ledger !== '')
|
|
212
|
+
parts.push(`Steps completed so far in this test, including any replayed without you:\n${ctx.ledger}`);
|
|
213
|
+
const prefix = ctx.replayedPrefix;
|
|
214
|
+
if (prefix !== undefined) {
|
|
215
|
+
parts.push(`A cached replay already performed these actions for this step: ${prefix.replayedActions.join('; ')}. It stopped (${prefix.stopReason}). Continue from the current screen; do not redo them.`);
|
|
216
|
+
if (prefix.uncertainAction !== undefined) {
|
|
217
|
+
parts.push(`This replayed action may have taken effect although it failed: ${prefix.uncertainAction}. Check the screen before doing anything like it again.`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
parts.push(await openingScreen(active));
|
|
221
|
+
return parts.join('\n\n');
|
|
222
|
+
}
|
|
223
|
+
/** The params without secret placeholders, which are listed by name instead. */
|
|
224
|
+
function plainParams(params) {
|
|
225
|
+
if (params === undefined)
|
|
226
|
+
return undefined;
|
|
227
|
+
const plain = Object.fromEntries(Object.entries(params).filter(([, value]) => !isSecretPlaceholder(value)));
|
|
228
|
+
return Object.keys(plain).length === 0 ? undefined : plain;
|
|
229
|
+
}
|
|
230
|
+
/** A secret in the params, as the runner projects it: `{ kind: 'secret', name, purpose }`. */
|
|
231
|
+
function isSecretPlaceholder(value) {
|
|
232
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value) && value['kind'] === 'secret';
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* The turn's input tokens with the cached ones included, as e2e counts them.
|
|
236
|
+
* ACP leaves open whether an agent's `inputTokens` include them: the Claude
|
|
237
|
+
* adapter's do not (its total adds the cache on top), Codex's do (its total
|
|
238
|
+
* is input and output alone).
|
|
239
|
+
*/
|
|
240
|
+
function inputTokens(usage) {
|
|
241
|
+
const cached = (usage.cachedReadTokens ?? 0) + (usage.cachedWriteTokens ?? 0);
|
|
242
|
+
const cacheApart = cached > 0 && usage.totalTokens >= usage.inputTokens + usage.outputTokens + cached;
|
|
243
|
+
return cacheApart ? usage.inputTokens + cached : usage.inputTokens;
|
|
244
|
+
}
|
|
245
|
+
/** Whether `promise` settles within `ms`; no promise has nothing to wait for. */
|
|
246
|
+
async function settled(promise, ms) {
|
|
247
|
+
if (promise === undefined)
|
|
248
|
+
return true;
|
|
249
|
+
let timer;
|
|
250
|
+
const timeout = new Promise((resolve) => {
|
|
251
|
+
timer = setTimeout(() => resolve(false), ms);
|
|
252
|
+
});
|
|
253
|
+
const done = promise.then(() => true, () => true);
|
|
254
|
+
return Promise.race([done, timeout]).finally(() => clearTimeout(timer));
|
|
255
|
+
}
|
|
256
|
+
/** The verdict of a step where the agent acted with a tool of its own, which the runner neither saw nor recorded. */
|
|
257
|
+
function ownToolUsed(title) {
|
|
258
|
+
return {
|
|
259
|
+
status: 'failed',
|
|
260
|
+
errorCode: 'POLICY_DENIED',
|
|
261
|
+
summary: `The agent ran a tool of its own without asking (${title}); only the step tools may act, so the step was stopped.`,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
function noConclusion(report) {
|
|
265
|
+
const said = report.text.trim().split('\n').at(-1)?.slice(0, 300);
|
|
266
|
+
return {
|
|
267
|
+
status: 'failed',
|
|
268
|
+
errorCode: 'STEP_NO_CONCLUSION',
|
|
269
|
+
summary: `The agent ended its turn (${report.stopReason}) without complete_step${said === undefined || said === '' ? '' : `: ${said}`}`,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
function transcript(prompt, active, report) {
|
|
273
|
+
return [
|
|
274
|
+
`> ${prompt}`,
|
|
275
|
+
`tools: ${active.calls.join(', ') || 'none'}`,
|
|
276
|
+
...(report.rejected.length === 0 ? [] : [`rejected tools of the agent's own: ${report.rejected.join(', ')}`]),
|
|
277
|
+
...(report.ran.length === 0 ? [] : [`tools of the agent's own that ran without asking: ${report.ran.join(', ')}`]),
|
|
278
|
+
`stop: ${report.stopReason}`,
|
|
279
|
+
report.text,
|
|
280
|
+
].join('\n');
|
|
281
|
+
}
|
|
282
|
+
function message(error) {
|
|
283
|
+
return error instanceof Error ? error.message : String(error);
|
|
284
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { acp, claudeCode, codex } from './executor.js';
|
package/dist/mcp.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serves the step tools to the agent as one MCP server over streamable HTTP
|
|
3
|
+
* on 127.0.0.1. An ACP agent takes tools only as MCP servers, and a stdio
|
|
4
|
+
* server would run in another process, away from the step's context; over
|
|
5
|
+
* HTTP every call runs here, in the test worker. The port is ephemeral and
|
|
6
|
+
* the path random per session, so nothing else on the machine guesses it.
|
|
7
|
+
*/
|
|
8
|
+
import type { z } from 'zod';
|
|
9
|
+
/** The MCP server name the agent sees. Distinct from `e2e mcp`'s `e2e`, so a permission request naming it is about these tools. */
|
|
10
|
+
export declare const MCP_SERVER_NAME = "e2e_step";
|
|
11
|
+
/** What a tool answers: text, with a screenshot once the step shows pixels. */
|
|
12
|
+
export interface ToolResult {
|
|
13
|
+
readonly text: string;
|
|
14
|
+
/** A base64 image. */
|
|
15
|
+
readonly image?: {
|
|
16
|
+
readonly data: string;
|
|
17
|
+
readonly mimeType: string;
|
|
18
|
+
};
|
|
19
|
+
readonly isError?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/** One tool the agent can call: a closed input schema and its handler. */
|
|
22
|
+
export interface ServedTool {
|
|
23
|
+
readonly name: string;
|
|
24
|
+
readonly description: string;
|
|
25
|
+
readonly inputSchema: z.ZodObject;
|
|
26
|
+
readonly readOnly: boolean;
|
|
27
|
+
run(args: Record<string, unknown>): Promise<ToolResult>;
|
|
28
|
+
}
|
|
29
|
+
/** A running tool server. */
|
|
30
|
+
export interface ToolServer {
|
|
31
|
+
readonly url: string;
|
|
32
|
+
close(): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
/** Starts the tool server on an ephemeral 127.0.0.1 port. */
|
|
35
|
+
export declare function serveTools(tools: readonly ServedTool[]): Promise<ToolServer>;
|