@testsprite/testsprite-cli 0.16.0 → 0.17.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/README.md +51 -147
- package/dist/commands/agent.d.ts +12 -4
- package/dist/commands/agent.js +48 -63
- package/dist/commands/auth.d.ts +6 -10
- package/dist/commands/auth.js +38 -54
- package/dist/commands/ci.d.ts +23 -100
- package/dist/commands/ci.js +293 -487
- package/dist/commands/conversation.d.ts +27 -0
- package/dist/commands/conversation.js +702 -0
- package/dist/commands/doctor.d.ts +5 -1
- package/dist/commands/doctor.js +129 -34
- package/dist/commands/init.d.ts +4 -45
- package/dist/commands/init.js +108 -66
- package/dist/commands/project-env.d.ts +62 -2
- package/dist/commands/project-env.js +388 -37
- package/dist/commands/project-sign-in.d.ts +0 -48
- package/dist/commands/project-sign-in.js +1 -229
- package/dist/commands/project.d.ts +15 -186
- package/dist/commands/project.js +89 -924
- package/dist/commands/result.d.ts +10 -0
- package/dist/commands/result.js +215 -0
- package/dist/commands/schedule.d.ts +0 -29
- package/dist/commands/schedule.js +28 -87
- package/dist/commands/test.d.ts +301 -1816
- package/dist/commands/test.js +1951 -13902
- package/dist/commands/testlist.d.ts +128 -5
- package/dist/commands/testlist.js +269 -339
- package/dist/commands/tunnel.d.ts +2 -2
- package/dist/commands/tunnel.js +23 -19
- package/dist/commands/usage.d.ts +12 -45
- package/dist/commands/usage.js +50 -131
- package/dist/index.js +182 -104
- package/dist/lib/agent-detect.js +9 -5
- package/dist/lib/agent-targets.d.ts +21 -2
- package/dist/lib/agent-targets.js +88 -11
- package/dist/lib/args.d.ts +13 -0
- package/dist/lib/args.js +49 -0
- package/dist/lib/basic-auth.d.ts +3 -0
- package/dist/lib/basic-auth.js +15 -0
- package/dist/lib/billing-refusal.js +4 -3
- package/dist/lib/ci-linkage.d.ts +4 -82
- package/dist/lib/ci-linkage.js +4 -157
- package/dist/lib/ci-run.js +1 -1
- package/dist/lib/client-factory.d.ts +10 -2
- package/dist/lib/client-factory.js +38 -26
- package/dist/lib/commit-signal.d.ts +65 -0
- package/dist/lib/commit-signal.js +305 -0
- package/dist/lib/concurrency-limiter.d.ts +6 -0
- package/dist/lib/concurrency-limiter.js +29 -0
- package/dist/lib/config.js +10 -5
- package/dist/lib/conflict-reason.d.ts +9 -3
- package/dist/lib/conflict-reason.js +6 -3
- package/dist/lib/conversation/answers.d.ts +9 -0
- package/dist/lib/conversation/answers.js +49 -0
- package/dist/lib/conversation/api.d.ts +71 -0
- package/dist/lib/conversation/api.js +150 -0
- package/dist/lib/conversation/change.d.ts +31 -0
- package/dist/lib/conversation/change.js +124 -0
- package/dist/lib/conversation/context.d.ts +19 -0
- package/dist/lib/conversation/context.js +70 -0
- package/dist/lib/conversation/environment.d.ts +15 -0
- package/dist/lib/conversation/environment.js +33 -0
- package/dist/lib/conversation/follow.d.ts +24 -0
- package/dist/lib/conversation/follow.js +42 -0
- package/dist/lib/conversation/render.d.ts +33 -0
- package/dist/lib/conversation/render.js +315 -0
- package/dist/lib/conversation/title.d.ts +2 -0
- package/dist/lib/conversation/title.js +18 -0
- package/dist/lib/conversation/types.d.ts +156 -0
- package/dist/lib/conversation/types.js +6 -0
- package/dist/lib/credentials.js +67 -11
- package/dist/lib/cron.d.ts +1 -1
- package/dist/lib/cron.js +2 -2
- package/dist/lib/dispatch-stop.d.ts +20 -0
- package/dist/lib/dispatch-stop.js +30 -0
- package/dist/lib/download.d.ts +71 -0
- package/dist/lib/download.js +274 -0
- package/dist/lib/dry-run/fetch.d.ts +10 -0
- package/dist/lib/dry-run/fetch.js +20 -0
- package/dist/lib/dry-run/samples.d.ts +6 -34
- package/dist/lib/dry-run/samples.js +375 -747
- package/dist/lib/environment-tunnel.d.ts +16 -27
- package/dist/lib/environment-tunnel.js +161 -154
- package/dist/lib/errors.d.ts +20 -4
- package/dist/lib/errors.js +36 -16
- package/dist/lib/facade.d.ts +7 -29
- package/dist/lib/facade.js +7 -36
- package/dist/lib/failing-fe-resolver.d.ts +13 -58
- package/dist/lib/failing-fe-resolver.js +47 -132
- package/dist/lib/first-suite-hint.d.ts +16 -22
- package/dist/lib/first-suite-hint.js +38 -47
- package/dist/lib/gh-output.d.ts +76 -21
- package/dist/lib/gh-output.js +171 -90
- package/dist/lib/git-utils.d.ts +56 -0
- package/dist/lib/git-utils.js +212 -0
- package/dist/lib/github-context.d.ts +2 -0
- package/dist/lib/github-context.js +40 -8
- package/dist/lib/history-retention.d.ts +6 -2
- package/dist/lib/history-retention.js +10 -7
- package/dist/lib/http.d.ts +58 -134
- package/dist/lib/http.js +352 -289
- package/dist/lib/interrupt.d.ts +14 -8
- package/dist/lib/interrupt.js +28 -10
- package/dist/lib/junit-report.d.ts +57 -4
- package/dist/lib/junit-report.js +202 -32
- package/dist/lib/local-target.d.ts +3 -3
- package/dist/lib/local-target.js +3 -3
- package/dist/lib/output.d.ts +1 -40
- package/dist/lib/output.js +11 -49
- package/dist/lib/pagination.d.ts +2 -5
- package/dist/lib/pagination.js +2 -5
- package/dist/lib/presigned-url.d.ts +22 -0
- package/dist/lib/presigned-url.js +90 -0
- package/dist/lib/prompt.js +36 -2
- package/dist/lib/proxy.js +2 -1
- package/dist/lib/redact.d.ts +2 -2
- package/dist/lib/redact.js +3 -4
- package/dist/lib/removed-commands.d.ts +18 -0
- package/dist/lib/removed-commands.js +154 -0
- package/dist/lib/render-error.d.ts +31 -6
- package/dist/lib/render-error.js +88 -25
- package/dist/lib/response-schemas.d.ts +38 -30
- package/dist/lib/response-schemas.js +207 -207
- package/dist/lib/result/api.d.ts +24 -0
- package/dist/lib/result/api.js +28 -0
- package/dist/lib/result/evidence.d.ts +71 -0
- package/dist/lib/result/evidence.js +420 -0
- package/dist/lib/result/render.d.ts +20 -0
- package/dist/lib/result/render.js +194 -0
- package/dist/lib/result/types.d.ts +122 -0
- package/dist/lib/result/types.js +19 -0
- package/dist/lib/run-output.d.ts +275 -0
- package/dist/lib/run-output.js +227 -0
- package/dist/lib/run-render.d.ts +101 -0
- package/dist/lib/run-render.js +183 -0
- package/dist/lib/run-wait.d.ts +192 -0
- package/dist/lib/run-wait.js +452 -0
- package/dist/lib/runs.types.d.ts +118 -367
- package/dist/lib/runs.types.js +0 -14
- package/dist/lib/secret-file.d.ts +1 -1
- package/dist/lib/secret-file.js +11 -2
- package/dist/lib/skill-nudge.d.ts +7 -27
- package/dist/lib/skill-nudge.js +9 -37
- package/dist/lib/target-path.d.ts +15 -0
- package/dist/lib/target-path.js +35 -0
- package/dist/lib/target-url-advisory.d.ts +19 -0
- package/dist/lib/target-url-advisory.js +31 -0
- package/dist/lib/target-url-preflight.d.ts +9 -0
- package/dist/lib/target-url-preflight.js +214 -106
- package/dist/lib/target-url.d.ts +1 -1
- package/dist/lib/target-url.js +6 -16
- package/dist/lib/telemetry.d.ts +31 -11
- package/dist/lib/telemetry.js +37 -8
- package/dist/lib/terminal-text.d.ts +41 -0
- package/dist/lib/terminal-text.js +127 -0
- package/dist/lib/testlist.types.d.ts +16 -8
- package/dist/lib/testlist.types.js +1 -1
- package/dist/lib/text-equality.d.ts +2 -0
- package/dist/lib/text-equality.js +5 -0
- package/dist/lib/text-file.d.ts +16 -0
- package/dist/lib/text-file.js +41 -0
- package/dist/lib/text-table.d.ts +46 -0
- package/dist/lib/text-table.js +72 -4
- package/dist/lib/ticker.d.ts +9 -2
- package/dist/lib/ticker.js +16 -3
- package/dist/lib/transport-failure.d.ts +22 -0
- package/dist/lib/transport-failure.js +126 -0
- package/dist/lib/tunnel-session.d.ts +7 -1
- package/dist/lib/tunnel-session.js +22 -9
- package/dist/lib/update-check.js +3 -2
- package/dist/lib/url-template.d.ts +43 -0
- package/dist/lib/url-template.js +151 -0
- package/dist/lib/validate.d.ts +0 -64
- package/dist/lib/validate.js +2 -131
- package/dist/lib/version-notice.js +4 -3
- package/dist/lib/wait-exit.d.ts +37 -2
- package/dist/lib/wait-exit.js +88 -30
- package/dist/lib/workspace-status.js +10 -2
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -3
- package/skills/testsprite-ci.skill.md +48 -18
- package/skills/testsprite-onboard.skill.md +66 -151
- package/skills/testsprite-verify.codex.md +50 -52
- package/skills/testsprite-verify.skill.md +184 -351
- package/dist/lib/browser.d.ts +0 -6
- package/dist/lib/browser.js +0 -54
- package/dist/lib/bundle.d.ts +0 -234
- package/dist/lib/bundle.js +0 -781
- package/dist/lib/deprecate.d.ts +0 -32
- package/dist/lib/deprecate.js +0 -39
- package/dist/lib/flaky.d.ts +0 -91
- package/dist/lib/flaky.js +0 -71
- package/dist/lib/plan-poll.d.ts +0 -171
- package/dist/lib/plan-poll.js +0 -441
- package/dist/lib/plans.types.d.ts +0 -130
- package/dist/lib/plans.types.js +0 -31
- package/dist/lib/poll.d.ts +0 -100
- package/dist/lib/poll.js +0 -290
- package/dist/lib/rate-throttle.d.ts +0 -49
- package/dist/lib/rate-throttle.js +0 -75
- package/dist/lib/run-progress.d.ts +0 -41
- package/dist/lib/run-progress.js +0 -56
- package/dist/lib/v3-advisory.d.ts +0 -55
- package/dist/lib/v3-advisory.js +0 -74
- package/schemas/plan.schema.json +0 -61
|
@@ -0,0 +1,702 @@
|
|
|
1
|
+
import { readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { basename, extname } from 'node:path';
|
|
3
|
+
import { Command, Option } from 'commander';
|
|
4
|
+
import { detectCallerFromEnv } from '../lib/agent-detect.js';
|
|
5
|
+
import { makeHttpClient, parseRequestTimeoutFlag, resolveRequestTimeoutMs, } from '../lib/client-factory.js';
|
|
6
|
+
import { resolveProfileName } from '../lib/config.js';
|
|
7
|
+
import { CLIError, InterruptError } from '../lib/errors.js';
|
|
8
|
+
import { answersFor } from '../lib/conversation/answers.js';
|
|
9
|
+
import { globalShutdown } from '../lib/interrupt.js';
|
|
10
|
+
import { waitForRun, resolveRunWaitFailure, } from '../lib/run-wait.js';
|
|
11
|
+
import { GLOBAL_OPTS_HINT, Output, resolveOutputMode } from '../lib/output.js';
|
|
12
|
+
import { promptSecret } from '../lib/prompt.js';
|
|
13
|
+
import { conversationApi } from '../lib/conversation/api.js';
|
|
14
|
+
import { readChange, readRepository, runGit } from '../lib/conversation/change.js';
|
|
15
|
+
import { rememberCard, rememberConversation, resolveConversationId, resolveProjectId, shownCard, } from '../lib/conversation/context.js';
|
|
16
|
+
import { environmentNotes } from '../lib/conversation/environment.js';
|
|
17
|
+
import { followConversation } from '../lib/conversation/follow.js';
|
|
18
|
+
import { titleFrom } from '../lib/conversation/title.js';
|
|
19
|
+
import { nextSteps, prSummary, renderApplied, renderConversation, renderConversationList, renderEvent, waitingNotes, } from '../lib/conversation/render.js';
|
|
20
|
+
import { jsonForTerminal, terminalSafeLine, terminalSafeWriter } from '../lib/terminal-text.js';
|
|
21
|
+
/** Under the ten minutes a coding agent's shell command may take, with room to print. */
|
|
22
|
+
const AGENT_WAIT_SECONDS = 540;
|
|
23
|
+
/** The most files one message carries, the change included. */
|
|
24
|
+
const MAX_FILES = 10;
|
|
25
|
+
const MAX_FILE_BYTES = 50 * 1024 * 1024;
|
|
26
|
+
function context(command, deps) {
|
|
27
|
+
const globals = command.optsWithGlobals();
|
|
28
|
+
const opts = {
|
|
29
|
+
profile: resolveProfileName(globals.profile, deps.env),
|
|
30
|
+
output: resolveOutputMode(globals.output),
|
|
31
|
+
endpointUrl: globals.endpointUrl,
|
|
32
|
+
debug: globals.debug ?? false,
|
|
33
|
+
verbose: globals.verbose ?? false,
|
|
34
|
+
dryRun: globals.dryRun ?? false,
|
|
35
|
+
requestTimeoutMs: parseRequestTimeoutFlag(globals.requestTimeout),
|
|
36
|
+
};
|
|
37
|
+
const stderr = terminalSafeWriter(deps.stderr);
|
|
38
|
+
const client = makeHttpClient(opts, {
|
|
39
|
+
env: deps.env,
|
|
40
|
+
credentialsPath: deps.credentialsPath,
|
|
41
|
+
fetchImpl: deps.fetchImpl,
|
|
42
|
+
stderr: deps.stderr,
|
|
43
|
+
});
|
|
44
|
+
const cwd = deps.cwd ?? process.cwd();
|
|
45
|
+
return {
|
|
46
|
+
opts,
|
|
47
|
+
out: new Output(opts.output, { stdout: deps.stdout, stderr: deps.stderr }),
|
|
48
|
+
stderr,
|
|
49
|
+
stderrJson: value => (deps.stderr ?? ((line) => process.stderr.write(`${line}\n`)))(jsonForTerminal(value)),
|
|
50
|
+
client,
|
|
51
|
+
api: conversationApi(client, deps.fetchImpl ?? globalThis.fetch.bind(globalThis), {
|
|
52
|
+
// A file's PUT to storage goes around the client, so it is handed the
|
|
53
|
+
// per-request timeout the client itself resolved from the same inputs.
|
|
54
|
+
requestTimeoutMs: resolveRequestTimeoutMs(opts, deps.env ?? process.env),
|
|
55
|
+
}),
|
|
56
|
+
root: readRepository(cwd, deps.git ?? runGit)?.root ?? cwd,
|
|
57
|
+
interactive: deps.interactive ?? (process.stdin.isTTY === true && process.stdout.isTTY === true),
|
|
58
|
+
deps,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function timeoutMs(ctx, flags) {
|
|
62
|
+
if (flags.timeout !== undefined) {
|
|
63
|
+
const seconds = Number(flags.timeout);
|
|
64
|
+
if (!Number.isFinite(seconds) || seconds <= 0) {
|
|
65
|
+
throw new CLIError('--timeout must be a positive number of seconds.', 5, 'VALIDATION_ERROR');
|
|
66
|
+
}
|
|
67
|
+
return seconds * 1000;
|
|
68
|
+
}
|
|
69
|
+
return ctx.interactive ? null : AGENT_WAIT_SECONDS * 1000;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Follow the conversation until the agent needs someone, printing its
|
|
73
|
+
* progress on stderr, then print where it stands on stdout. A conversation the
|
|
74
|
+
* agent stopped short, or one still running at the deadline, exits non-zero.
|
|
75
|
+
*/
|
|
76
|
+
async function finishTurn(ctx, projectId, id, afterSeq, flags, extra = {}, first = []) {
|
|
77
|
+
if (flags.wait === false) {
|
|
78
|
+
const conversation = await ctx.api.get(projectId, id);
|
|
79
|
+
print(ctx, conversation, [], flags.steps, extra, first);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const result = await follow(ctx, projectId, id, afterSeq, flags.deadlineMs);
|
|
83
|
+
settle(ctx, result, flags.steps, extra, first);
|
|
84
|
+
}
|
|
85
|
+
/** Follow the agent until it needs someone, printing its progress on stderr. */
|
|
86
|
+
async function follow(ctx, projectId, id, afterSeq, timeoutMs) {
|
|
87
|
+
const json = ctx.opts.output === 'json';
|
|
88
|
+
try {
|
|
89
|
+
return await followConversation(ctx.api, projectId, id, {
|
|
90
|
+
afterSeq,
|
|
91
|
+
timeoutMs,
|
|
92
|
+
onEvent: event => {
|
|
93
|
+
if (json) {
|
|
94
|
+
ctx.stderrJson(event);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const line = renderEvent(event);
|
|
98
|
+
if (line !== null)
|
|
99
|
+
ctx.stderr(line);
|
|
100
|
+
},
|
|
101
|
+
now: ctx.deps.now,
|
|
102
|
+
sleep: ctx.deps.sleep,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
catch (err) {
|
|
106
|
+
if (err instanceof InterruptError) {
|
|
107
|
+
ctx.stderr('Stopped following. The agent keeps working: `testsprite status --wait` picks it up again, `testsprite stop` stops it.');
|
|
108
|
+
}
|
|
109
|
+
throw err;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/** Print where the conversation stands, then exit as the exit codes say. */
|
|
113
|
+
function settle(ctx, result, steps, extra, first) {
|
|
114
|
+
print(ctx, result.conversation, result.events, steps, extra, first);
|
|
115
|
+
const { conversation } = result;
|
|
116
|
+
if (result.timedOut) {
|
|
117
|
+
throw new CLIError('The agent is still working. Follow it with `testsprite status --wait`.', 7, 'TIMEOUT');
|
|
118
|
+
}
|
|
119
|
+
failIfOver(conversation);
|
|
120
|
+
}
|
|
121
|
+
/** Exit 1 when the agent stopped short or the conversation ended, as the exit codes say. */
|
|
122
|
+
function failIfOver(conversation) {
|
|
123
|
+
if (conversation.state === 'stopped' || conversation.state === 'ended') {
|
|
124
|
+
throw new CLIError(conversation.state === 'ended'
|
|
125
|
+
? 'This conversation has ended.'
|
|
126
|
+
: `The agent stopped before it finished (${conversation.stopReason ?? 'unknown reason'}).`, 1, 'AGENT_STOPPED');
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function print(ctx, conversation, events, steps, extra = {}, first = []) {
|
|
130
|
+
const replyShown = events.some(e => e.kind === 'message' && e.from === 'agent' && e.text === conversation.reply);
|
|
131
|
+
const next = [...first, ...nextSteps(conversation)];
|
|
132
|
+
ctx.out.print({ ...extra, ...conversation, events, next }, () => renderConversation(conversation, { steps, replyShown, next }));
|
|
133
|
+
// What was shown here is what `apply` and `reject` will decide.
|
|
134
|
+
if (conversation.proposal && !ctx.opts.dryRun) {
|
|
135
|
+
rememberCard(ctx.root, conversation.id, conversation.proposal.id);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* The card to decide: the one named, else the one last shown in this folder,
|
|
140
|
+
* else the one waiting now. A card shown here that no longer waits is refused,
|
|
141
|
+
* so nobody decides a card they haven't seen.
|
|
142
|
+
*/
|
|
143
|
+
function cardToDecide(ctx, now, flag) {
|
|
144
|
+
const card = flag?.trim() || shownCard(ctx.root, now.id) || now.proposal?.id;
|
|
145
|
+
if (card && now.proposal && card !== now.proposal.id) {
|
|
146
|
+
throw new CLIError(`The card shown here before is not the one waiting now: the agent now waits on "${now.proposal.summary}", which you haven't seen. Look at it with \`testsprite status\`, then decide again.`, 6, 'CONFLICT');
|
|
147
|
+
}
|
|
148
|
+
return card;
|
|
149
|
+
}
|
|
150
|
+
function readAttachments(ctx, files, diff) {
|
|
151
|
+
const attachments = [];
|
|
152
|
+
const notes = [];
|
|
153
|
+
const repo = readRepository(ctx.deps.cwd ?? process.cwd(), ctx.deps.git ?? runGit);
|
|
154
|
+
const brief = repo
|
|
155
|
+
? { repository: { name: repo.name, branch: repo.branch, commit: repo.commit } }
|
|
156
|
+
: {};
|
|
157
|
+
if (diff !== undefined && diff !== false) {
|
|
158
|
+
if (!repo) {
|
|
159
|
+
throw new CLIError('--diff needs a git repository.', 5, 'VALIDATION_ERROR');
|
|
160
|
+
}
|
|
161
|
+
const change = readChange(repo, typeof diff === 'string' ? diff : undefined, ctx.deps.git);
|
|
162
|
+
if (!change.patch) {
|
|
163
|
+
notes.push(`No change since ${change.base.slice(0, 12)}; nothing attached.`);
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
attachments.push({
|
|
167
|
+
name: 'change.patch',
|
|
168
|
+
contentType: 'text/x-diff',
|
|
169
|
+
bytes: new TextEncoder().encode(change.patch),
|
|
170
|
+
});
|
|
171
|
+
brief.change = 'change.patch';
|
|
172
|
+
notes.push(`Attached the change since ${change.base.slice(0, 12)}: ${change.files.length} ${change.files.length === 1 ? 'file' : 'files'}.`);
|
|
173
|
+
}
|
|
174
|
+
if (change.left.length) {
|
|
175
|
+
notes.push(`Left out (may hold secrets, or not source): ${change.left.join(', ')}.`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
for (const path of files) {
|
|
179
|
+
let size;
|
|
180
|
+
try {
|
|
181
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- a path the user names with --file to attach; reading it is the point.
|
|
182
|
+
size = statSync(path).size;
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
throw new CLIError(`--file ${path}: no such file.`, 5, 'VALIDATION_ERROR');
|
|
186
|
+
}
|
|
187
|
+
if (size > MAX_FILE_BYTES) {
|
|
188
|
+
throw new CLIError(`--file ${path} is over 50 MB.`, 5, 'VALIDATION_ERROR');
|
|
189
|
+
}
|
|
190
|
+
attachments.push({
|
|
191
|
+
name: basename(path),
|
|
192
|
+
contentType: contentTypeOf(path),
|
|
193
|
+
// eslint-disable-next-line security/detect-non-literal-fs-filename -- a path the user names with --file to attach; reading it is the point.
|
|
194
|
+
bytes: readFileSync(path),
|
|
195
|
+
});
|
|
196
|
+
notes.push(`Attached ${basename(path)}.`);
|
|
197
|
+
}
|
|
198
|
+
if (attachments.length > MAX_FILES) {
|
|
199
|
+
throw new CLIError(`A message carries at most ${MAX_FILES} files.`, 5, 'VALIDATION_ERROR');
|
|
200
|
+
}
|
|
201
|
+
return { attachments, brief, notes };
|
|
202
|
+
}
|
|
203
|
+
async function upload(ctx, projectId, id, attachments) {
|
|
204
|
+
if (ctx.opts.dryRun)
|
|
205
|
+
return attachments.map(a => a.name);
|
|
206
|
+
const names = [];
|
|
207
|
+
for (const attachment of attachments)
|
|
208
|
+
names.push(await ctx.api.upload(projectId, id, attachment));
|
|
209
|
+
return names;
|
|
210
|
+
}
|
|
211
|
+
const CONTENT_TYPES = {
|
|
212
|
+
'.md': 'text/markdown',
|
|
213
|
+
'.txt': 'text/plain',
|
|
214
|
+
'.pdf': 'application/pdf',
|
|
215
|
+
'.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
216
|
+
'.json': 'application/json',
|
|
217
|
+
'.yaml': 'application/yaml',
|
|
218
|
+
'.yml': 'application/yaml',
|
|
219
|
+
'.csv': 'text/csv',
|
|
220
|
+
'.png': 'image/png',
|
|
221
|
+
'.jpg': 'image/jpeg',
|
|
222
|
+
'.jpeg': 'image/jpeg',
|
|
223
|
+
'.patch': 'text/x-diff',
|
|
224
|
+
'.diff': 'text/x-diff',
|
|
225
|
+
};
|
|
226
|
+
const contentTypeOf = (path) => CONTENT_TYPES[extname(path).toLowerCase()] ?? 'application/octet-stream';
|
|
227
|
+
function caller(env, interactive) {
|
|
228
|
+
return detectCallerFromEnv(env).length > 0 || !interactive ? 'agent' : 'person';
|
|
229
|
+
}
|
|
230
|
+
async function current(ctx, flags) {
|
|
231
|
+
const projectId = resolveProjectId(flags.project, { env: ctx.deps.env, root: ctx.root });
|
|
232
|
+
const id = resolveConversationId(flags.conversation, projectId, {
|
|
233
|
+
env: ctx.deps.env,
|
|
234
|
+
root: ctx.root,
|
|
235
|
+
});
|
|
236
|
+
return { projectId, id };
|
|
237
|
+
}
|
|
238
|
+
const CARD_HELP = 'the card to decide, by the id the conversation showed (default: the one last shown in this folder); one decided meanwhile is refused';
|
|
239
|
+
const collect = (value, previous = []) => [...previous, value];
|
|
240
|
+
/**
|
|
241
|
+
* A question card is answered with `--answer`, every question at once, so the
|
|
242
|
+
* card is never decided empty; any other card takes no answers. A card shown
|
|
243
|
+
* here that no longer waits is not judged: it goes to the server, which says
|
|
244
|
+
* how it was decided meanwhile.
|
|
245
|
+
*/
|
|
246
|
+
/**
|
|
247
|
+
* The environment `--env` names for a run card. On a card shown here that
|
|
248
|
+
* starts no run it is refused before anything is sent; a card not shown here
|
|
249
|
+
* goes to the server, which says how it was decided.
|
|
250
|
+
*/
|
|
251
|
+
function environmentOn(now, card, given) {
|
|
252
|
+
if (given === undefined)
|
|
253
|
+
return undefined;
|
|
254
|
+
if (!given.trim()) {
|
|
255
|
+
throw new CLIError('`--env` needs the name of an environment.', 5, 'VALIDATION_ERROR');
|
|
256
|
+
}
|
|
257
|
+
if (now.proposal?.id === card && now.proposal.kind !== 'run') {
|
|
258
|
+
throw new CLIError('This card starts no run; `--env` is for a card that does.', 5, 'VALIDATION_ERROR');
|
|
259
|
+
}
|
|
260
|
+
return given.trim();
|
|
261
|
+
}
|
|
262
|
+
function answersOn(now, card, given) {
|
|
263
|
+
if (now.proposal?.id !== card)
|
|
264
|
+
return undefined;
|
|
265
|
+
if (now.proposal.kind !== 'question') {
|
|
266
|
+
if (given?.length) {
|
|
267
|
+
throw new CLIError('This card asks no questions; `--answer` is for a card that does.', 5, 'VALIDATION_ERROR');
|
|
268
|
+
}
|
|
269
|
+
return undefined;
|
|
270
|
+
}
|
|
271
|
+
return answersFor(now.proposal.questions ?? [], given ?? []);
|
|
272
|
+
}
|
|
273
|
+
const refs = (values) => values
|
|
274
|
+
?.flatMap(v => v.split(','))
|
|
275
|
+
.map(v => v.trim())
|
|
276
|
+
.filter(Boolean);
|
|
277
|
+
function addWaitOptions(cmd) {
|
|
278
|
+
return cmd
|
|
279
|
+
.option('--no-wait', 'return as soon as the agent has it, without following it')
|
|
280
|
+
.option('--timeout <seconds>', `stop following after this long (default: no limit in a terminal, ${AGENT_WAIT_SECONDS}s otherwise); the agent keeps working`);
|
|
281
|
+
}
|
|
282
|
+
function addTarget(cmd, conversation = true) {
|
|
283
|
+
cmd.option('--project <id>', 'project id (default: TESTSPRITE_PROJECT_ID, else the one last used in this folder)');
|
|
284
|
+
if (conversation) {
|
|
285
|
+
cmd.option('--conversation <id>', 'conversation id (default: the one last started from this folder)');
|
|
286
|
+
}
|
|
287
|
+
return cmd;
|
|
288
|
+
}
|
|
289
|
+
const EXIT_CODES = '\nExit codes:\n' +
|
|
290
|
+
' 0 the agent proposed something or replied\n' +
|
|
291
|
+
' 1 the agent stopped before it finished, or the conversation ended\n' +
|
|
292
|
+
' 5 invalid input (no project, a missing file, --diff outside a repository)\n' +
|
|
293
|
+
' 6 the conversation is not waiting for this (e.g. apply with nothing proposed)\n' +
|
|
294
|
+
' 7 still working at --timeout; follow it with `testsprite status --wait`\n' +
|
|
295
|
+
' 13 the agent is not enabled for this account\n';
|
|
296
|
+
async function planNotes(ctx, projectId) {
|
|
297
|
+
const quietly = async (read) => {
|
|
298
|
+
try {
|
|
299
|
+
return await read();
|
|
300
|
+
}
|
|
301
|
+
catch (err) {
|
|
302
|
+
if (err instanceof InterruptError)
|
|
303
|
+
throw err;
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
const [environments, conversations] = await Promise.all([
|
|
308
|
+
quietly(() => ctx.api.environments(projectId)),
|
|
309
|
+
quietly(() => ctx.api.list(projectId)),
|
|
310
|
+
]);
|
|
311
|
+
const environment = environments?.find(e => e.isDefault);
|
|
312
|
+
return [
|
|
313
|
+
...(environment ? environmentNotes(environment) : []),
|
|
314
|
+
...waitingNotes(conversations ?? []),
|
|
315
|
+
];
|
|
316
|
+
}
|
|
317
|
+
export function createPlanCommand(deps = {}) {
|
|
318
|
+
const cmd = new Command('plan')
|
|
319
|
+
.summary('Ask the agent to plan tests: it explores your app and proposes changes to the suite')
|
|
320
|
+
.description("Start a conversation with the project's agent, the same one the portal has. It explores your app, " +
|
|
321
|
+
'writes or fixes test cases, and proposes them as a plan you apply or reject. Say what to test, ' +
|
|
322
|
+
'or what went wrong; attach your change with --diff and documents with --file.')
|
|
323
|
+
.argument('[request...]', 'what to test, in your own words')
|
|
324
|
+
.option('--diff [base]', 'attach your change since <base> (default: where this branch left the default branch)')
|
|
325
|
+
.option('--file <path>', 'attach a document: a PRD, a ticket, a spec (repeatable)', collect)
|
|
326
|
+
.addOption(new Option('--model <id>', 'the model the agent runs on, like claude-haiku-5-5 (default: the portal default)'));
|
|
327
|
+
addTarget(cmd, false);
|
|
328
|
+
addWaitOptions(cmd);
|
|
329
|
+
return cmd
|
|
330
|
+
.option('--steps', 'show each proposed test step by step')
|
|
331
|
+
.addHelpText('after', '\nExamples:\n' +
|
|
332
|
+
' testsprite plan "cover the new coupon rules" --diff --file docs/coupons-prd.md\n' +
|
|
333
|
+
' testsprite plan "the checkout tests fail since the redesign; fix the tests, report real bugs"\n' +
|
|
334
|
+
' testsprite plan --diff --no-wait # start it and come back with `testsprite status --wait`\n' +
|
|
335
|
+
EXIT_CODES +
|
|
336
|
+
GLOBAL_OPTS_HINT)
|
|
337
|
+
.action(async (words, flags, command) => {
|
|
338
|
+
const ctx = context(command, deps);
|
|
339
|
+
const deadlineMs = timeoutMs(ctx, flags);
|
|
340
|
+
const projectId = resolveProjectId(flags.project, {
|
|
341
|
+
env: ctx.deps.env,
|
|
342
|
+
root: ctx.root,
|
|
343
|
+
});
|
|
344
|
+
const { attachments, brief, notes: attached, } = readAttachments(ctx, flags.file ?? [], flags.diff);
|
|
345
|
+
const notes = await planNotes(ctx, projectId);
|
|
346
|
+
const text = words.join(' ').trim() ||
|
|
347
|
+
(brief.change
|
|
348
|
+
? 'Plan tests for the change I am working on.'
|
|
349
|
+
: 'Plan tests for this project.');
|
|
350
|
+
const started = await ctx.api.start(projectId, {
|
|
351
|
+
model: flags.model,
|
|
352
|
+
title: titleFrom(text),
|
|
353
|
+
});
|
|
354
|
+
const files = await upload(ctx, projectId, started.id, attachments);
|
|
355
|
+
await ctx.api.send(projectId, started.id, {
|
|
356
|
+
text,
|
|
357
|
+
...(files.length ? { files } : {}),
|
|
358
|
+
brief: {
|
|
359
|
+
caller: caller(ctx.deps.env ?? process.env, ctx.interactive),
|
|
360
|
+
...brief,
|
|
361
|
+
},
|
|
362
|
+
});
|
|
363
|
+
if (!ctx.opts.dryRun)
|
|
364
|
+
rememberConversation(ctx.root, projectId, started.id);
|
|
365
|
+
for (const note of [...attached, ...notes])
|
|
366
|
+
ctx.stderr(note);
|
|
367
|
+
ctx.stderr(`The agent is on it: ${terminalSafeLine(started.url)}`);
|
|
368
|
+
await finishTurn(ctx, projectId, started.id, 0, { ...flags, deadlineMs }, notes.length ? { notices: notes } : {});
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
export function createReplyCommand(deps = {}) {
|
|
372
|
+
const cmd = new Command('reply')
|
|
373
|
+
.summary('Answer the agent, or ask it for more, in the same conversation')
|
|
374
|
+
.argument('<message...>', 'what to tell the agent')
|
|
375
|
+
.option('--diff [base]', 'attach your change since <base>')
|
|
376
|
+
.option('--file <path>', 'attach a document (repeatable)', collect);
|
|
377
|
+
addTarget(cmd);
|
|
378
|
+
addWaitOptions(cmd);
|
|
379
|
+
return cmd
|
|
380
|
+
.option('--steps', 'show each proposed test step by step')
|
|
381
|
+
.addHelpText('after', '\nExamples:\n' +
|
|
382
|
+
' testsprite reply "use the qa-admin account for the admin pages"\n' +
|
|
383
|
+
' testsprite reply "the last run failed; which failures are real bugs?"\n' +
|
|
384
|
+
EXIT_CODES +
|
|
385
|
+
GLOBAL_OPTS_HINT)
|
|
386
|
+
.action(async (words, flags, command) => {
|
|
387
|
+
const ctx = context(command, deps);
|
|
388
|
+
const deadlineMs = timeoutMs(ctx, flags);
|
|
389
|
+
const { projectId, id } = await current(ctx, flags);
|
|
390
|
+
const { attachments, notes } = readAttachments(ctx, flags.file ?? [], flags.diff);
|
|
391
|
+
const now = await ctx.api.get(projectId, id);
|
|
392
|
+
if (now.state === 'ended') {
|
|
393
|
+
throw new CLIError('This conversation has ended. Start a new one with `testsprite plan "<what to test>"`.', 6, 'CONFLICT');
|
|
394
|
+
}
|
|
395
|
+
if (now.state === 'proposed') {
|
|
396
|
+
throw new CLIError('The agent is waiting for you to apply or reject what it proposed. To ask for changes to it: testsprite reject "<what to change>".', 6, 'CONFLICT');
|
|
397
|
+
}
|
|
398
|
+
const files = await upload(ctx, projectId, id, attachments);
|
|
399
|
+
await ctx.api.send(projectId, id, {
|
|
400
|
+
text: words.join(' '),
|
|
401
|
+
...(files.length ? { files } : {}),
|
|
402
|
+
});
|
|
403
|
+
for (const note of notes)
|
|
404
|
+
ctx.stderr(note);
|
|
405
|
+
await finishTurn(ctx, projectId, id, now.lastSeq, { ...flags, deadlineMs });
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
export function createStatusCommand(deps = {}) {
|
|
409
|
+
const cmd = new Command('status')
|
|
410
|
+
.summary('Where the agent is: working, waiting for you, or done')
|
|
411
|
+
.option('--wait', 'follow it until it needs you')
|
|
412
|
+
.option('--timeout <seconds>', `with --wait, stop following after this long (default: no limit in a terminal, ${AGENT_WAIT_SECONDS}s otherwise)`)
|
|
413
|
+
.option('--steps', 'show each proposed test step by step')
|
|
414
|
+
.option('--list', 'list your conversations with the agent on the project');
|
|
415
|
+
addTarget(cmd);
|
|
416
|
+
return cmd
|
|
417
|
+
.addHelpText('after', EXIT_CODES + GLOBAL_OPTS_HINT)
|
|
418
|
+
.action(async (flags, command) => {
|
|
419
|
+
const ctx = context(command, deps);
|
|
420
|
+
const deadlineMs = timeoutMs(ctx, flags);
|
|
421
|
+
if (flags.list) {
|
|
422
|
+
const projectId = resolveProjectId(flags.project, {
|
|
423
|
+
env: ctx.deps.env,
|
|
424
|
+
root: ctx.root,
|
|
425
|
+
});
|
|
426
|
+
const conversations = await ctx.api.list(projectId);
|
|
427
|
+
ctx.out.print({ conversations }, () => renderConversationList(conversations));
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
const { projectId, id } = await current(ctx, flags);
|
|
431
|
+
const now = await ctx.api.get(projectId, id);
|
|
432
|
+
if (!flags.wait || now.state !== 'working') {
|
|
433
|
+
print(ctx, now, [], flags.steps);
|
|
434
|
+
failIfOver(now);
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
await finishTurn(ctx, projectId, id, now.lastSeq, {
|
|
438
|
+
steps: flags.steps,
|
|
439
|
+
deadlineMs,
|
|
440
|
+
});
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
export function createApplyCommand(deps = {}) {
|
|
444
|
+
const cmd = new Command('apply')
|
|
445
|
+
.summary('Apply what the agent proposed, as the portal card does: save the plan, the environment, or start the run')
|
|
446
|
+
.description('Apply what the agent proposed, as the portal card does: save the plan, the environment, the ' +
|
|
447
|
+
"schedule or the project's settings, or start the run. A run it starts is waited for, and its " +
|
|
448
|
+
'results go back to the agent, which reads them and replies.')
|
|
449
|
+
.option('--only <tests>', 'apply only these tests of the plan, by the refs it lists (comma-separated)', collect)
|
|
450
|
+
.option('--exclude <tests>', 'apply the plan without these tests (comma-separated)', collect)
|
|
451
|
+
.option('--secret-stdin', 'for a card that asks for a secret (a password, a token): read it from what is piped in')
|
|
452
|
+
.option('--answer <n=choice>', "for a card that asks questions: answer question n with an option's letter (letters comma-separated when it takes several) or your own words (repeatable)", collect)
|
|
453
|
+
.option('--env <name>', 'for a card that starts a run: run it on this environment instead of the one the card names')
|
|
454
|
+
.option('--card <id>', CARD_HELP);
|
|
455
|
+
addTarget(cmd);
|
|
456
|
+
addWaitOptions(cmd);
|
|
457
|
+
return cmd
|
|
458
|
+
.option('--steps', 'show each proposed test step by step')
|
|
459
|
+
.addHelpText('after', '\nExamples:\n' +
|
|
460
|
+
' testsprite apply\n' +
|
|
461
|
+
' testsprite apply --exclude 3,CHK-009\n' +
|
|
462
|
+
' testsprite apply --answer 1=b --answer 2=a,c\n' +
|
|
463
|
+
' testsprite apply --env staging\n' +
|
|
464
|
+
EXIT_CODES +
|
|
465
|
+
GLOBAL_OPTS_HINT)
|
|
466
|
+
.action(async (flags, command) => {
|
|
467
|
+
const ctx = context(command, deps);
|
|
468
|
+
const deadlineMs = timeoutMs(ctx, flags);
|
|
469
|
+
const startedAt = (deps.now ?? Date.now)();
|
|
470
|
+
const { projectId, id } = await current(ctx, flags);
|
|
471
|
+
const now = await ctx.api.get(projectId, id);
|
|
472
|
+
// A card shown here that no longer waits goes to the server, which says how it was decided.
|
|
473
|
+
const card = cardToDecide(ctx, now, flags.card);
|
|
474
|
+
if (!card) {
|
|
475
|
+
throw new CLIError(`Nothing is waiting to be applied: ${now.state === 'working' ? 'the agent is still working' : `the conversation is ${now.state}`}. See \`testsprite status\`.`, 6, 'CONFLICT');
|
|
476
|
+
}
|
|
477
|
+
const answers = answersOn(now, card, flags.answer);
|
|
478
|
+
const environment = environmentOn(now, card, flags.env);
|
|
479
|
+
const secret = await secretFor(ctx, now, card, flags.secretStdin === true);
|
|
480
|
+
const applied = await ctx.api.apply(projectId, id, {
|
|
481
|
+
card,
|
|
482
|
+
only: refs(flags.only),
|
|
483
|
+
exclude: refs(flags.exclude),
|
|
484
|
+
...(secret !== undefined ? { secrets: { value: secret } } : {}),
|
|
485
|
+
...(answers ? { answers } : {}),
|
|
486
|
+
...(environment !== undefined ? { environment } : {}),
|
|
487
|
+
});
|
|
488
|
+
const forPr = prSummary(applied, now.url);
|
|
489
|
+
// Everything below prints this one object, which names the launch of a
|
|
490
|
+
// run the proposal started `run.runId`.
|
|
491
|
+
const printed = applied;
|
|
492
|
+
if (ctx.opts.output === 'text') {
|
|
493
|
+
ctx.stderr(renderApplied(printed));
|
|
494
|
+
const forPrText = prSummary(applied, now.url, terminalSafeLine);
|
|
495
|
+
if (forPrText)
|
|
496
|
+
ctx.stderr(`\nFor the pull request description:\n\n${forPrText}\n`);
|
|
497
|
+
}
|
|
498
|
+
if (!applied.ok) {
|
|
499
|
+
if (ctx.opts.output === 'json')
|
|
500
|
+
ctx.out.print({ applied: printed });
|
|
501
|
+
throw new CLIError(applied.failed?.length
|
|
502
|
+
? 'Nothing was saved (see above). The agent has been told and follows up in the conversation: testsprite status --wait'
|
|
503
|
+
: `Could not apply it: ${applied.message ?? 'see above'}.`, 1, 'APPLY_FAILED');
|
|
504
|
+
}
|
|
505
|
+
if (applied.run && flags.wait !== false) {
|
|
506
|
+
await handOverRun(ctx, projectId, id, now.lastSeq, printed, applied.run.runId, {
|
|
507
|
+
steps: flags.steps,
|
|
508
|
+
timeoutMs: deadlineMs === null
|
|
509
|
+
? null
|
|
510
|
+
: Math.max(0, deadlineMs - ((deps.now ?? Date.now)() - startedAt)),
|
|
511
|
+
});
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
const saved = applied.tests ?? [];
|
|
515
|
+
const run = saved.length
|
|
516
|
+
? [
|
|
517
|
+
{
|
|
518
|
+
command: `testsprite run ${saved.join(' ')} --wait`,
|
|
519
|
+
why: 'run what it saved; add --local <port> for an app on this machine',
|
|
520
|
+
},
|
|
521
|
+
]
|
|
522
|
+
: [];
|
|
523
|
+
await finishTurn(ctx, projectId, id, now.lastSeq, { ...flags, deadlineMs }, { applied: printed, ...(forPr ? { prSummary: forPr } : {}) }, run);
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* The agent hears that a run it started has finished only when someone next
|
|
528
|
+
* writes. Wait for the run here and tell it, so one `apply` goes from "run
|
|
529
|
+
* these" to the agent's read of the results, within the one --timeout.
|
|
530
|
+
*/
|
|
531
|
+
/**
|
|
532
|
+
* What a secret card asks for, read before anything is sent: a card applied without it is
|
|
533
|
+
* refused and keeps waiting, so the secret is piped in or typed by a person, never a flag.
|
|
534
|
+
*/
|
|
535
|
+
/** A card's summary inside a sentence: "The password of staging" reads "the password of staging". */
|
|
536
|
+
const midSentence = (text) => /^[A-Z][a-z]/.test(text) ? text.charAt(0).toLowerCase() + text.slice(1) : text;
|
|
537
|
+
async function secretFor(ctx, now, card, fromStdin) {
|
|
538
|
+
const proposal = now.proposal?.id === card ? now.proposal : null;
|
|
539
|
+
if (proposal?.kind !== 'secret') {
|
|
540
|
+
if (fromStdin) {
|
|
541
|
+
throw new CLIError('--secret-stdin is for a card that asks for a secret; this card does not.', 5, 'VALIDATION_ERROR');
|
|
542
|
+
}
|
|
543
|
+
return undefined;
|
|
544
|
+
}
|
|
545
|
+
if (ctx.opts.dryRun)
|
|
546
|
+
return undefined;
|
|
547
|
+
let value;
|
|
548
|
+
if (fromStdin) {
|
|
549
|
+
value = await (ctx.deps.readStdin ?? readPipedStdin)();
|
|
550
|
+
}
|
|
551
|
+
else if (ctx.interactive) {
|
|
552
|
+
const ask = ctx.deps.promptSecret ?? ((q) => promptSecret(q));
|
|
553
|
+
value = await ask(`Type ${terminalSafeLine(midSentence(proposal.summary))}: `);
|
|
554
|
+
}
|
|
555
|
+
else {
|
|
556
|
+
throw new CLIError(`This card asks for ${midSentence(proposal.summary)}, typed by a person. Run \`testsprite apply\` in a terminal, pipe it to \`testsprite apply --secret-stdin\`, or apply it in the portal: ${now.url}`, 5, 'VALIDATION_ERROR');
|
|
557
|
+
}
|
|
558
|
+
if (!value.trim()) {
|
|
559
|
+
throw new CLIError('Nothing was entered, so nothing was applied; the card still waits.', 5, 'VALIDATION_ERROR');
|
|
560
|
+
}
|
|
561
|
+
return value;
|
|
562
|
+
}
|
|
563
|
+
/** What is piped in, without the newline `echo` ends it with. A terminal is not a pipe. */
|
|
564
|
+
async function readPipedStdin() {
|
|
565
|
+
if (process.stdin.isTTY) {
|
|
566
|
+
throw new CLIError('--secret-stdin reads what is piped in. In a terminal, leave it out and type the secret when asked.', 5, 'VALIDATION_ERROR');
|
|
567
|
+
}
|
|
568
|
+
const chunks = [];
|
|
569
|
+
for await (const chunk of process.stdin)
|
|
570
|
+
chunks.push(Buffer.from(chunk));
|
|
571
|
+
return Buffer.concat(chunks)
|
|
572
|
+
.toString('utf8')
|
|
573
|
+
.replace(/\r?\n$/, '');
|
|
574
|
+
}
|
|
575
|
+
async function handOverRun(ctx, projectId, id, afterSeq,
|
|
576
|
+
/** What `apply` did, as it is printed. */
|
|
577
|
+
applied,
|
|
578
|
+
/** The launch of the run to wait for. */
|
|
579
|
+
runId, { steps, timeoutMs }) {
|
|
580
|
+
const clock = ctx.deps.now ?? Date.now;
|
|
581
|
+
const end = timeoutMs === null ? null : clock() + timeoutMs;
|
|
582
|
+
const left = () => (end === null ? null : Math.max(0, end - clock()));
|
|
583
|
+
const ack = await follow(ctx, projectId, id, afterSeq, left());
|
|
584
|
+
if (ack.timedOut || ack.conversation.state !== 'replied') {
|
|
585
|
+
settle(ctx, ack, steps, { applied }, []);
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
ctx.stderr(`Waiting for run ${terminalSafeLine(runId)} to finish…`);
|
|
589
|
+
let snapshot;
|
|
590
|
+
let outcome;
|
|
591
|
+
const printPartial = () => print(ctx, ack.conversation, ack.events, steps, {
|
|
592
|
+
applied,
|
|
593
|
+
run: {
|
|
594
|
+
runId,
|
|
595
|
+
...(snapshot ? { status: snapshot.run.status, cancelled: snapshot.run.cancelled } : {}),
|
|
596
|
+
...(outcome?.timedOut ? { timedOut: true } : {}),
|
|
597
|
+
},
|
|
598
|
+
results: snapshot?.results ?? [],
|
|
599
|
+
}, [
|
|
600
|
+
{
|
|
601
|
+
command: `testsprite run wait ${runId}`,
|
|
602
|
+
why: 'resume waiting for this execution before handing its results to the agent',
|
|
603
|
+
},
|
|
604
|
+
]);
|
|
605
|
+
try {
|
|
606
|
+
outcome = await waitForRun(ctx.client, runId, {
|
|
607
|
+
deadline: Date.now() + (left() ?? Infinity),
|
|
608
|
+
shutdown: globalShutdown,
|
|
609
|
+
sleep: ctx.deps.sleep,
|
|
610
|
+
onRead: value => {
|
|
611
|
+
snapshot = value;
|
|
612
|
+
},
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
catch (error) {
|
|
616
|
+
printPartial();
|
|
617
|
+
throw error;
|
|
618
|
+
}
|
|
619
|
+
const failure = resolveRunWaitFailure([outcome], { timeoutSeconds: (timeoutMs ?? 0) / 1000 });
|
|
620
|
+
if (outcome.timedOut || outcome.error) {
|
|
621
|
+
printPartial();
|
|
622
|
+
throw failure;
|
|
623
|
+
}
|
|
624
|
+
// The agent reads results itself; report generation is not a prerequisite for handoff.
|
|
625
|
+
const finished = outcome.run?.cancelled
|
|
626
|
+
? `The run ${runId} was cancelled. Go through its results.`
|
|
627
|
+
: `The run ${runId} has finished. Go through its results.`;
|
|
628
|
+
await ctx.api.send(projectId, id, { text: finished });
|
|
629
|
+
const result = await follow(ctx, projectId, id, ack.conversation.lastSeq, left());
|
|
630
|
+
settle(ctx, { ...result, events: [...ack.events, ...result.events] }, steps, { applied }, []);
|
|
631
|
+
if (failure)
|
|
632
|
+
throw failure;
|
|
633
|
+
}
|
|
634
|
+
export function createRejectCommand(deps = {}) {
|
|
635
|
+
const cmd = new Command('reject')
|
|
636
|
+
.summary('Turn down what the agent proposed; it reads your reason and revises')
|
|
637
|
+
.argument('[reason...]', 'what to change')
|
|
638
|
+
.option('--card <id>', CARD_HELP);
|
|
639
|
+
addTarget(cmd);
|
|
640
|
+
addWaitOptions(cmd);
|
|
641
|
+
return cmd
|
|
642
|
+
.option('--steps', 'show each proposed test step by step')
|
|
643
|
+
.addHelpText('after', '\nExample:\n testsprite reject "split the coupon test: one for the limit, one for expired codes"\n' +
|
|
644
|
+
EXIT_CODES +
|
|
645
|
+
GLOBAL_OPTS_HINT)
|
|
646
|
+
.action(async (words, flags, command) => {
|
|
647
|
+
const ctx = context(command, deps);
|
|
648
|
+
const deadlineMs = timeoutMs(ctx, flags);
|
|
649
|
+
const { projectId, id } = await current(ctx, flags);
|
|
650
|
+
const now = await ctx.api.get(projectId, id);
|
|
651
|
+
const card = cardToDecide(ctx, now, flags.card);
|
|
652
|
+
if (!card) {
|
|
653
|
+
throw new CLIError(`Nothing is waiting to be rejected: the conversation is ${now.state}. To tell the agent something, use \`testsprite reply\`.`, 6, 'CONFLICT');
|
|
654
|
+
}
|
|
655
|
+
const reason = words.join(' ').trim();
|
|
656
|
+
const { rejected } = await ctx.api.reject(projectId, id, card, reason || undefined);
|
|
657
|
+
ctx.stderr(`Rejected: ${terminalSafeLine(rejected)}`);
|
|
658
|
+
await finishTurn(ctx, projectId, id, now.lastSeq, { ...flags, deadlineMs });
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
export function createStopCommand(deps = {}) {
|
|
662
|
+
const cmd = new Command('stop').summary('Stop the agent where it is');
|
|
663
|
+
addTarget(cmd);
|
|
664
|
+
return cmd.addHelpText('after', GLOBAL_OPTS_HINT).action(async (flags, command) => {
|
|
665
|
+
const ctx = context(command, deps);
|
|
666
|
+
const { projectId, id } = await current(ctx, flags);
|
|
667
|
+
const { stopped } = await ctx.api.stop(projectId, id);
|
|
668
|
+
ctx.out.print({ stopped: id, ...(stopped === false ? { nothingToStop: true } : {}) }, () => stopped === false
|
|
669
|
+
? 'Nothing to stop: the agent was not working.'
|
|
670
|
+
: 'Stopped. `testsprite reply "<message>"` picks it up again.');
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
export function createDiscardCommand(deps = {}) {
|
|
674
|
+
const cmd = new Command('discard').summary("Drop the conversation's task: stop the agent, turn down what waits, release what it holds");
|
|
675
|
+
addTarget(cmd);
|
|
676
|
+
return cmd
|
|
677
|
+
.description('Stop the agent, turn down a card waiting on you, and put back what the conversation changed ' +
|
|
678
|
+
'in the project and never saved, so other conversations are not held by it. Nothing saved ' +
|
|
679
|
+
'to the suite is touched. The conversation stays readable, and reads as discarded until a ' +
|
|
680
|
+
'reply picks it up again.')
|
|
681
|
+
.addHelpText('after', GLOBAL_OPTS_HINT)
|
|
682
|
+
.action(async (flags, command) => {
|
|
683
|
+
const ctx = context(command, deps);
|
|
684
|
+
const { projectId, id } = await current(ctx, flags);
|
|
685
|
+
const { released } = await ctx.api.discard(projectId, id);
|
|
686
|
+
ctx.out.print({ discarded: id, released }, () => released.length
|
|
687
|
+
? `Discarded. Put back ${released.length} unsaved ${released.length === 1 ? 'file' : 'files'}: ${terminalSafeLine(released.join(', '))}`
|
|
688
|
+
: 'Discarded. It held nothing unsaved.');
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
export function createConversationCommands(deps = {}) {
|
|
692
|
+
return [
|
|
693
|
+
createPlanCommand(deps),
|
|
694
|
+
createApplyCommand(deps),
|
|
695
|
+
createRejectCommand(deps),
|
|
696
|
+
createReplyCommand(deps),
|
|
697
|
+
createStatusCommand(deps),
|
|
698
|
+
createDiscardCommand(deps),
|
|
699
|
+
createStopCommand(deps),
|
|
700
|
+
];
|
|
701
|
+
}
|
|
702
|
+
//# sourceMappingURL=conversation.js.map
|