@bolt-cli/cli 0.15.1 → 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 +6 -0
- 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
|
@@ -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`);
|
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