@bolt-cli/cli 0.15.0 → 0.15.2
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/lib/agent.js +10 -2
- package/lib/api.js +9 -2
- package/lib/cancel.js +63 -9
- package/lib/cli.js +76 -13
- package/lib/session.js +33 -5
- package/package.json +1 -1
package/lib/agent.js
CHANGED
|
@@ -113,7 +113,7 @@ function buildAgentPrompt({ target, objective, evidence, history, memory, sessio
|
|
|
113
113
|
return `${api.buildPrompt({ target, objective, evidence, history, memory, session, account })}\n\n${TOOL_PROTOCOL}`;
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
-
const EMPTY_REPLY_NUDGE = `Your previous reply arrived at this terminal empty - no prose and no tool block. That is a protocol failure, not an answer,
|
|
116
|
+
const EMPTY_REPLY_NUDGE = `Your previous reply arrived at this terminal empty - no prose and no tool block. That is a protocol failure, not an answer. The ORIGINAL TASK is still in this message, above this note: do it now. Do not ask what the task is, do not ask for a target, and do not describe what you could do. Do not re-send internal reasoning, 自知 blocks or tool call syntax in any other shape. Either continue the task with one or more \`\`\`tool blocks, or give the finished answer in plain prose.`;
|
|
117
117
|
|
|
118
118
|
function buildFollowup({ iteration, calls, results, errors = [], seenDetails = null, maxIters = DEFAULT_MAX_ITERS, syntaxNote = null }) {
|
|
119
119
|
const lines = [`TOOL_RESULTS for iteration ${iteration} (ground truth from this terminal - do not invent, continue the task):`];
|
|
@@ -187,6 +187,11 @@ async function runAgentLoop({ cfg, objective, mode, target, evidence, sess, stre
|
|
|
187
187
|
};
|
|
188
188
|
const ctx = () => ({ session: sessionId || (sess ? sess.id : ""), account: account || (cfg && cfg.email) || "" });
|
|
189
189
|
let prompt = scrub(buildAgentPrompt({ target, objective, evidence, history: sess ? session.contextBlock(sess) : "", memory, ...ctx() }));
|
|
190
|
+
// Kept verbatim for the one empty-reply recovery. Nudging with only the
|
|
191
|
+
// recovery note used to erase the objective, so the model answered "what
|
|
192
|
+
// would you like me to do?" after a 40-second round trip. The retry now
|
|
193
|
+
// resends the whole original task plus the note.
|
|
194
|
+
const basePrompt = prompt;
|
|
190
195
|
let text = "";
|
|
191
196
|
let done = null;
|
|
192
197
|
let iters = 0;
|
|
@@ -327,9 +332,12 @@ async function runAgentLoop({ cfg, objective, mode, target, evidence, sess, stre
|
|
|
327
332
|
if (cleaned.trim()) break; // normal final answer
|
|
328
333
|
// Empty reply — the classic "replied with nothing" failure. Nudge
|
|
329
334
|
// once; a second consecutive silent reply stops the run honestly.
|
|
335
|
+
// Resend the original objective WITH the note: a bare nudge costs two
|
|
336
|
+
// full inference round trips and still loses the task.
|
|
330
337
|
if (nudged) break;
|
|
331
338
|
nudged = true;
|
|
332
|
-
|
|
339
|
+
emit({ type: "retry", reason: "empty", message: "empty reply from the model - retrying with the original task" });
|
|
340
|
+
prompt = `${basePrompt}\n\n${EMPTY_REPLY_NUDGE}`;
|
|
333
341
|
continue;
|
|
334
342
|
}
|
|
335
343
|
nudged = false;
|
package/lib/api.js
CHANGED
|
@@ -157,8 +157,15 @@ async function streamGenerate({ url, token, prompt, mode, onToken, signal, sourc
|
|
|
157
157
|
const waitSec = retryAfterSecFrom(res, data);
|
|
158
158
|
if (waitSec > 0) {
|
|
159
159
|
try {
|
|
160
|
-
|
|
161
|
-
|
|
160
|
+
// Keep one live line counting down instead of a single frozen
|
|
161
|
+
// estimate followed by a silent wait that looks like a hang.
|
|
162
|
+
const live = Boolean(process.stderr.isTTY);
|
|
163
|
+
const report = (sec) => {
|
|
164
|
+
if (live) process.stderr.write(`\rRate-limited - retrying in ~${sec}s...`);
|
|
165
|
+
};
|
|
166
|
+
report(Math.ceil(waitSec));
|
|
167
|
+
await cancel.sleep(waitSec * 1000, { signal, onTick: report });
|
|
168
|
+
if (live) process.stderr.write("\r\u001b[2K");
|
|
162
169
|
} catch (error) {
|
|
163
170
|
if (error && error.code === "CANCELLED") throw error;
|
|
164
171
|
}
|
package/lib/cancel.js
CHANGED
|
@@ -31,22 +31,75 @@ function canWatchKeys() {
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
// A lone Esc cancels; an Esc that starts an arrow/function/Alt sequence does
|
|
35
|
+
// not. Treating every ESC byte as a cancel made Up/Down/Home during a run kill
|
|
36
|
+
// it. Pure so the classification is pinned by tests without a real terminal.
|
|
37
|
+
// "escape" means a bare ESC arrived: it is only a cancel if nothing else
|
|
38
|
+
// follows within the grace window (terminals may split a sequence across
|
|
39
|
+
// writes).
|
|
40
|
+
const ESC_GRACE_MS = 40;
|
|
41
|
+
|
|
42
|
+
// Patterns are built from character codes so the source contains no literal
|
|
43
|
+
// control bytes (linters reject those in regex literals, and they are easy to
|
|
44
|
+
// lose in an editor).
|
|
45
|
+
const ESC = String.fromCharCode(27);
|
|
46
|
+
const BEL = String.fromCharCode(7);
|
|
47
|
+
const CTRL_C = String.fromCharCode(3);
|
|
48
|
+
const CSI_RE = new RegExp(`${ESC}\\[[0-9;?]*[ -/]*[@-Z\\^-~]`, "g");
|
|
49
|
+
const SS3_RE = new RegExp(`${ESC}O[A-P]`, "g");
|
|
50
|
+
const OSC_RE = new RegExp(`${ESC}\\][^${BEL}${ESC}]*(?:${BEL}|${ESC}\\\\)`, "g");
|
|
51
|
+
const ALT_RE = new RegExp(`${ESC}[ -Z\\^-~]`, "g");
|
|
52
|
+
|
|
53
|
+
function classifyInput(chunk) {
|
|
54
|
+
const s = String(chunk ?? "");
|
|
55
|
+
if (s.includes(CTRL_C)) return "abort";
|
|
56
|
+
const bare = s
|
|
57
|
+
.replace(CSI_RE, "") // arrows, Home, PageUp, etc.
|
|
58
|
+
.replace(SS3_RE, "") // application-mode arrows
|
|
59
|
+
.replace(OSC_RE, "") // terminal title / clipboard replies
|
|
60
|
+
.replace(ALT_RE, ""); // Alt+printable (leaves a split CSI pending)
|
|
61
|
+
return bare.includes(ESC) ? "escape" : "ignore";
|
|
62
|
+
}
|
|
63
|
+
|
|
34
64
|
function createCanceller() {
|
|
35
65
|
const controller = new AbortController();
|
|
36
66
|
let attached = false;
|
|
37
67
|
let wasRaw = false;
|
|
38
68
|
let handler = null;
|
|
69
|
+
let escTimer = null;
|
|
70
|
+
|
|
71
|
+
function abortNow() {
|
|
72
|
+
try {
|
|
73
|
+
controller.abort();
|
|
74
|
+
} catch {
|
|
75
|
+
// abort() never throws in practice; cancellation must not crash.
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function clearEsc() {
|
|
80
|
+
if (escTimer) clearTimeout(escTimer);
|
|
81
|
+
escTimer = null;
|
|
82
|
+
}
|
|
39
83
|
|
|
40
84
|
function onData(chunk) {
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
85
|
+
const intent = classifyInput(chunk);
|
|
86
|
+
if (intent === "abort") {
|
|
87
|
+
clearEsc();
|
|
88
|
+
abortNow();
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (intent === "escape") {
|
|
92
|
+
clearEsc();
|
|
93
|
+
escTimer = setTimeout(() => {
|
|
94
|
+
escTimer = null;
|
|
95
|
+
abortNow();
|
|
96
|
+
}, ESC_GRACE_MS);
|
|
97
|
+
if (escTimer.unref) escTimer.unref();
|
|
98
|
+
return;
|
|
49
99
|
}
|
|
100
|
+
// An escape sequence completed across two writes: its tail cancels the
|
|
101
|
+
// pending lone-Esc abort.
|
|
102
|
+
clearEsc();
|
|
50
103
|
}
|
|
51
104
|
|
|
52
105
|
function attach() {
|
|
@@ -68,6 +121,7 @@ function createCanceller() {
|
|
|
68
121
|
}
|
|
69
122
|
|
|
70
123
|
function detach() {
|
|
124
|
+
clearEsc();
|
|
71
125
|
if (!attached) return;
|
|
72
126
|
try {
|
|
73
127
|
if (handler) process.stdin.removeListener("data", handler);
|
|
@@ -150,4 +204,4 @@ function sleep(ms, { signal, onTick } = {}) {
|
|
|
150
204
|
});
|
|
151
205
|
}
|
|
152
206
|
|
|
153
|
-
module.exports = { createCanceller, isCancelError, cancelError, canWatchKeys, sleep };
|
|
207
|
+
module.exports = { createCanceller, isCancelError, cancelError, canWatchKeys, classifyInput, ESC_GRACE_MS, sleep };
|
package/lib/cli.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
const fs = require("fs");
|
|
5
5
|
const os = require("os");
|
|
6
6
|
const path = require("path");
|
|
7
|
-
const { spawn } = require("node:child_process");
|
|
7
|
+
const { spawn, execFileSync } = require("node:child_process");
|
|
8
8
|
const config = require("./config");
|
|
9
9
|
const api = require("./api");
|
|
10
10
|
const browser = require("./browser");
|
|
@@ -914,6 +914,12 @@ function handleToolEvent(event) {
|
|
|
914
914
|
if (n > 0) ui.info(`redacted ${n} possible secret${n === 1 ? "" : "s"} from the prompt before sending`);
|
|
915
915
|
return;
|
|
916
916
|
}
|
|
917
|
+
if (event.type === "retry") {
|
|
918
|
+
// Say why the second round trip is happening instead of leaving a long
|
|
919
|
+
// silent gap that looks like a hang.
|
|
920
|
+
ui.info(String(event.message || "retrying after an empty reply"));
|
|
921
|
+
return;
|
|
922
|
+
}
|
|
917
923
|
if (event.type === "call") {
|
|
918
924
|
if (!event.approved) {
|
|
919
925
|
if (event.reason && event.reason.startsWith("refused")) ui.warn(`blocked ${tools.describeCall(event.call)} — destructive, no approval path`);
|
|
@@ -1394,31 +1400,88 @@ async function cmdDoctor() {
|
|
|
1394
1400
|
}
|
|
1395
1401
|
|
|
1396
1402
|
// ── update ────────────────────────────────────────────────────────────────
|
|
1397
|
-
// `bolt update`
|
|
1398
|
-
//
|
|
1399
|
-
//
|
|
1403
|
+
// `bolt update` runs npm to upgrade the globally installed package. On Windows
|
|
1404
|
+
// npm is a .cmd: newer Node refuses to spawn it directly (EINVAL), while
|
|
1405
|
+
// `shell: true` triggers DEP0190 and concatenates the arguments unescaped.
|
|
1406
|
+
// cmd.exe with one fixed command line avoids both. No argument here is
|
|
1407
|
+
// user-controlled; stdio is inherited so npm's progress shows as-is.
|
|
1408
|
+
function npmInvocation(args) {
|
|
1409
|
+
return process.platform === "win32"
|
|
1410
|
+
? { command: "cmd.exe", args: ["/d", "/s", "/c", ["npm", ...args].join(" ")] }
|
|
1411
|
+
: { command: "npm", args };
|
|
1412
|
+
}
|
|
1413
|
+
|
|
1414
|
+
// What is actually on disk in the global prefix? A zero npm exit does not
|
|
1415
|
+
// guarantee the package was replaced — Windows can keep the running CLI's
|
|
1416
|
+
// files locked — so the install is verified before claiming success. "" means
|
|
1417
|
+
// the prefix could not be read and no claim is made.
|
|
1418
|
+
function installedCliVersion() {
|
|
1419
|
+
try {
|
|
1420
|
+
const inv = npmInvocation(["root", "-g"]);
|
|
1421
|
+
const root = execFileSync(inv.command, inv.args, { encoding: "utf8", windowsHide: true }).trim();
|
|
1422
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(root, "@bolt-cli", "cli", "package.json"), "utf8"));
|
|
1423
|
+
return String(pkg.version || "");
|
|
1424
|
+
} catch {
|
|
1425
|
+
return "";
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
// Latest published version, used only to explain an "unchanged" install:
|
|
1430
|
+
// already-current is fine, but being behind means npm failed to replace us.
|
|
1431
|
+
function registryLatest() {
|
|
1432
|
+
try {
|
|
1433
|
+
const inv = npmInvocation(["view", "@bolt-cli/cli", "version"]);
|
|
1434
|
+
return execFileSync(inv.command, inv.args, { encoding: "utf8", timeout: 8000, windowsHide: true }).trim();
|
|
1435
|
+
} catch {
|
|
1436
|
+
return "";
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
// Pure outcome classifier for the self-update. "unchanged" is the important
|
|
1441
|
+
// case: npm exited zero but the old version is still installed. Tested.
|
|
1442
|
+
function updateOutcome(installed, current) {
|
|
1443
|
+
if (!installed) return "unknown";
|
|
1444
|
+
return String(installed) === String(current) ? "unchanged" : "upgraded";
|
|
1445
|
+
}
|
|
1446
|
+
|
|
1400
1447
|
function cmdUpdate() {
|
|
1401
1448
|
console.log(`running: npm install -g @bolt-cli/cli (current: v${VERSION})`);
|
|
1402
|
-
const
|
|
1449
|
+
const inv = npmInvocation(["install", "-g", "@bolt-cli/cli"]);
|
|
1450
|
+
const child = spawn(inv.command, inv.args, { stdio: "inherit", windowsHide: true });
|
|
1403
1451
|
return new Promise((resolve) => {
|
|
1404
1452
|
child.on("error", (error) => {
|
|
1405
1453
|
ui.err(`update failed: ${error.message} - try: npm install -g @bolt-cli/cli`);
|
|
1406
1454
|
resolve();
|
|
1407
1455
|
});
|
|
1408
1456
|
child.on("close", (code) => {
|
|
1409
|
-
if (code
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1457
|
+
if (code !== 0) {
|
|
1458
|
+
ui.err(`update failed (exit ${code}) - try: npm install -g @bolt-cli/cli`);
|
|
1459
|
+
resolve();
|
|
1460
|
+
return;
|
|
1461
|
+
}
|
|
1462
|
+
const installed = installedCliVersion();
|
|
1463
|
+
const outcome = updateOutcome(installed, VERSION);
|
|
1464
|
+
if (outcome === "upgraded") {
|
|
1465
|
+
// Clear the cached check only after a verified upgrade, so the next
|
|
1466
|
+
// session re-verifies instead of greeting itself with stale news.
|
|
1413
1467
|
try {
|
|
1414
1468
|
const cfg = config.load();
|
|
1415
1469
|
config.save({ ...cfg, updateCheckedAt: 0, updateLatest: "" });
|
|
1416
1470
|
} catch {
|
|
1417
|
-
// Cache hygiene is best-effort; the
|
|
1471
|
+
// Cache hygiene is best-effort; the install itself succeeded.
|
|
1472
|
+
}
|
|
1473
|
+
ui.ok(`update complete - v${installed} installed; restart bolt to run it`);
|
|
1474
|
+
} else if (outcome === "unchanged") {
|
|
1475
|
+
const latest = registryLatest();
|
|
1476
|
+
if (latest && String(latest) === String(installed)) {
|
|
1477
|
+
ui.ok(`already on the latest version (v${installed})`);
|
|
1478
|
+
} else {
|
|
1479
|
+
// Still behind, so the notice stays true. Say what to do rather than
|
|
1480
|
+
// reporting a success that did not take.
|
|
1481
|
+
ui.warn(`npm finished but v${installed} is still installed - run: npm install -g @bolt-cli/cli from a new terminal`);
|
|
1418
1482
|
}
|
|
1419
|
-
ui.ok("update complete - restart bolt to run the new version");
|
|
1420
1483
|
} else {
|
|
1421
|
-
ui.
|
|
1484
|
+
ui.ok("update finished - check with: bolt --version");
|
|
1422
1485
|
}
|
|
1423
1486
|
resolve();
|
|
1424
1487
|
});
|
|
@@ -2622,4 +2685,4 @@ async function run(argv) {
|
|
|
2622
2685
|
throw new Error(`Unknown command "${cmd}".\n${help()}`);
|
|
2623
2686
|
}
|
|
2624
2687
|
|
|
2625
|
-
module.exports = { run, resolveUrl, isLoopbackUrl, backendReachable, completer, completeFileFrag, completeWithDescriptions, filterPaletteItems, isPaletteRunnable, COMMANDS, AGENTS, NO_ARG_COMMANDS, makeAsker, makeAskQuestion, handleToolEvent, estimateTokens, buildDryRunReport, printDryRun, dryRunTools, argvSecretWarning, cmdDoctor, cmdCompletion, cmdTemplates, cmdExport, cmdUndo, reportRunError, warnTruncated, stripBoltPrefix, printPaletteNumbered, resolvePalettePick, replWelcomeLines, printHelpBox, printSessionTable, printKeysBox, pickTip, TIPS, formatElapsed, backendHost };
|
|
2688
|
+
module.exports = { run, resolveUrl, isLoopbackUrl, backendReachable, completer, completeFileFrag, completeWithDescriptions, filterPaletteItems, isPaletteRunnable, COMMANDS, AGENTS, NO_ARG_COMMANDS, makeAsker, makeAskQuestion, handleToolEvent, estimateTokens, buildDryRunReport, printDryRun, dryRunTools, argvSecretWarning, cmdDoctor, cmdCompletion, cmdTemplates, cmdExport, cmdUndo, reportRunError, warnTruncated, stripBoltPrefix, printPaletteNumbered, resolvePalettePick, replWelcomeLines, printHelpBox, printSessionTable, printKeysBox, pickTip, TIPS, formatElapsed, backendHost, updateOutcome };
|
package/lib/session.js
CHANGED
|
@@ -40,17 +40,45 @@ function create(title) {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
function load(id) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
const target = file(id);
|
|
44
|
+
// A process killed mid-save can leave the main file truncated. The staged
|
|
45
|
+
// copy is complete by construction, so recover from it instead of silently
|
|
46
|
+
// treating the session as gone.
|
|
47
|
+
for (const candidate of [target, `${target}.tmp`]) {
|
|
48
|
+
try {
|
|
49
|
+
return JSON.parse(fs.readFileSync(candidate, "utf8"));
|
|
50
|
+
} catch {
|
|
51
|
+
// try the other copy
|
|
52
|
+
}
|
|
47
53
|
}
|
|
54
|
+
return null;
|
|
48
55
|
}
|
|
49
56
|
|
|
50
57
|
function save(session) {
|
|
51
58
|
fs.mkdirSync(dir(), { recursive: true, mode: 0o700 });
|
|
52
59
|
session.updatedAt = new Date().toISOString();
|
|
53
|
-
|
|
60
|
+
const target = file(session.id);
|
|
61
|
+
const staged = `${target}.tmp`;
|
|
62
|
+
// Write the whole document to a sibling file first, then move it into
|
|
63
|
+
// place. A direct write truncates the only copy before the new bytes land,
|
|
64
|
+
// so an Esc/Ctrl-C or a power loss at the wrong moment loses the session.
|
|
65
|
+
fs.writeFileSync(staged, JSON.stringify(session, null, 2), { mode: 0o600 });
|
|
66
|
+
try {
|
|
67
|
+
fs.renameSync(staged, target);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
// Some Windows filesystems refuse a rename over an open destination.
|
|
70
|
+
try {
|
|
71
|
+
fs.copyFileSync(staged, target);
|
|
72
|
+
fs.unlinkSync(staged);
|
|
73
|
+
} catch {
|
|
74
|
+
try {
|
|
75
|
+
fs.unlinkSync(staged);
|
|
76
|
+
} catch {
|
|
77
|
+
// best effort
|
|
78
|
+
}
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
54
82
|
return session;
|
|
55
83
|
}
|
|
56
84
|
|
package/package.json
CHANGED