@alexwkleung/reika 0.5.0 → 0.7.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 +1 -1
- package/README.zh-CN.md +18 -18
- package/dist/agent/bashread.js +177 -0
- package/dist/agent/compaction.js +5 -0
- package/dist/agent/compactionreport.js +184 -0
- package/dist/agent/loop.js +191 -43
- package/dist/agent/noteslotmonitor.js +82 -0
- package/dist/agent/readtrace.js +18 -8
- package/dist/approval.js +6 -0
- package/dist/check/typecheck.js +29 -0
- package/dist/config.js +30 -17
- package/dist/context/bootstrap.js +3 -1
- package/dist/provider/client.js +15 -0
- package/dist/provider/latches.js +3 -0
- package/dist/provider/noteslot.js +239 -0
- package/dist/provider/templatekwargs.js +31 -0
- package/dist/provider/toolcall.js +22 -1
- package/dist/provider/transport.js +12 -9
- package/dist/search/_chrome.js +92 -19
- package/dist/search/cdp.js +6 -1
- package/dist/session.js +13 -1
- package/dist/tools/_danger.js +350 -45
- package/dist/tools/bash.js +2 -1
- package/dist/ui/App.js +111 -14
- package/dist/ui/Approval.js +15 -5
- package/dist/ui/Scrollback.js +7 -1
- package/dist/ui/commands.js +46 -0
- package/dist/ui/framedrops.js +34 -0
- package/dist/ui/syncframe.js +7 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -141,7 +141,7 @@ The same pages are on the website, [reikacode.com](https://reikacode.com).
|
|
|
141
141
|
|
|
142
142
|
## Inspired by
|
|
143
143
|
|
|
144
|
-
Claude Code, Codex, Crush, OpenCode, Pi, Aider, DeepSeek Harness, Qwen Code, Kimi Code CLI, Gemini CLI, Junie, and
|
|
144
|
+
Claude Code, Codex, Crush, OpenCode, Pi, Aider, DeepSeek Harness, Qwen Code, Kimi Code CLI, Gemini CLI, Junie, DS4, and Ante.
|
|
145
145
|
|
|
146
146
|
## License
|
|
147
147
|
|
package/README.zh-CN.md
CHANGED
|
@@ -11,10 +11,10 @@
|
|
|
11
11
|
</p>
|
|
12
12
|
|
|
13
13
|
<p align="center">
|
|
14
|
-
<a href="https://reikacode.com">网站</a> ·
|
|
14
|
+
<a href="https://reikacode.com/zh/">网站</a> ·
|
|
15
15
|
<a href="#快速开始">快速开始</a> ·
|
|
16
16
|
<a href="#文档">文档</a> ·
|
|
17
|
-
<a href="docs/models.md">测试过的模型</a> ·
|
|
17
|
+
<a href="docs/zh/models.md">测试过的模型</a> ·
|
|
18
18
|
<a href="README.md">English</a>
|
|
19
19
|
</p>
|
|
20
20
|
|
|
@@ -51,7 +51,7 @@ Reika 还处于 1.0 之前,命令、设置和行为在不同版本之间仍可
|
|
|
51
51
|
|
|
52
52
|
## 要求
|
|
53
53
|
|
|
54
|
-
- Node.js 22 或更新版本(macOS 11+,或 glibc 2.28+ 的 Linux —— 见[平台](docs/platforms.md))。
|
|
54
|
+
- Node.js 22 或更新版本(macOS 11+,或 glibc 2.28+ 的 Linux —— 见[平台](docs/zh/platforms.md))。
|
|
55
55
|
- 一个 OpenAI 兼容的模型服务器:llama.cpp、MLX、vLLM 或云端 API。
|
|
56
56
|
- macOS 是主要平台。Linux 可用,但没有 shell 沙箱和图片粘贴 OCR(可以设置 `REIKA_VISION_MODEL`,改用视觉模型读取粘贴的图片)。Windows 或许能在 WSL2 下运行。
|
|
57
57
|
- 现代终端(iTerm2、Ghostty、Kitty 等)。想要正确的 TUI 渲染,建议使用它们。
|
|
@@ -123,24 +123,24 @@ cd your-project && reika
|
|
|
123
123
|
|
|
124
124
|
## 文档
|
|
125
125
|
|
|
126
|
-
这些页面也在网站 [reikacode.com](https://reikacode.com)
|
|
127
|
-
|
|
128
|
-
| 文档
|
|
129
|
-
|
|
|
130
|
-
| [配置](docs/configuration.md)
|
|
131
|
-
| [模式、无头与命令](docs/usage.md)
|
|
132
|
-
| [工具](docs/tools.md)
|
|
133
|
-
| [指令与技能](docs/skills.md)
|
|
134
|
-
| [测试过的模型](docs/models.md) | Reika 实际跑过的本地量化版本和 API |
|
|
135
|
-
| [测量结果](docs/findings.md)
|
|
136
|
-
| [平台](docs/platforms.md) | 运行要求、在弱机器上运行、Linux 和 Windows 上的差异 |
|
|
137
|
-
| [架构与注意事项](docs/architecture.md) | 框架如何工作,以及已知限制 |
|
|
138
|
-
| [贡献](CONTRIBUTING.md)
|
|
139
|
-
| [更新日志](CHANGELOG.md)(英文)
|
|
126
|
+
这些页面也在网站 [reikacode.com/zh](https://reikacode.com/zh/) 上。模型、平台和架构已翻译为中文,其余页面暂为英文。
|
|
127
|
+
|
|
128
|
+
| 文档 | 内容 |
|
|
129
|
+
| ----------------------------------------- | ---------------------------------------------------------------- |
|
|
130
|
+
| [配置](docs/configuration.md) | 每一个 `.env` 键、实验性开关、具名 profile、上次会话状态 |
|
|
131
|
+
| [模式、无头与命令](docs/usage.md) | 各模式详解、`reika -p`、斜杠命令、`/save` 会话记录 |
|
|
132
|
+
| [工具](docs/tools.md) | 模型可用的工具、审批提示、网络搜索设置、MCP 服务器、`.gitignore` |
|
|
133
|
+
| [指令与技能](docs/skills.md) | `AGENTS.md`、作为斜杠命令的技能、自然语言路由、粘贴的链接 |
|
|
134
|
+
| [测试过的模型](docs/zh/models.md) | Reika 实际跑过的本地量化版本和 API |
|
|
135
|
+
| [测量结果](docs/findings.md) | 实测记录:什么坏了、什么撑住了,以及每个数字是怎么得到的 |
|
|
136
|
+
| [平台](docs/zh/platforms.md) | 运行要求、在弱机器上运行、Linux 和 Windows 上的差异 |
|
|
137
|
+
| [架构与注意事项](docs/zh/architecture.md) | 框架如何工作,以及已知限制 |
|
|
138
|
+
| [贡献](CONTRIBUTING.md) | 脚本、设计理念、`AGENTS.md` 指引,以及外部贡献者指南 |
|
|
139
|
+
| [更新日志](CHANGELOG.md)(英文) | 每个版本的变更 |
|
|
140
140
|
|
|
141
141
|
## 灵感来自
|
|
142
142
|
|
|
143
|
-
Claude Code、Codex、Crush、OpenCode、Pi、Aider、DeepSeek Harness、Qwen Code、Kimi Code CLI、Gemini CLI、Junie 和
|
|
143
|
+
Claude Code、Codex、Crush、OpenCode、Pi、Aider、DeepSeek Harness、Qwen Code、Kimi Code CLI、Gemini CLI、Junie、DS4 和 Ante。
|
|
144
144
|
|
|
145
145
|
## 许可
|
|
146
146
|
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { readFile, realpath, stat } from 'node:fs/promises';
|
|
3
|
+
import { isAbsolute, relative } from 'node:path';
|
|
4
|
+
import { resolveUserPath } from '../tools/_paths.js';
|
|
5
|
+
import { hasExecutableSubstitution, maskQuoted, words } from '../tools/_readonly.js';
|
|
6
|
+
const CAT_FLAGS = /^-[nbsuvetAE]+$/;
|
|
7
|
+
const SED_PRINT_FLAG = /^-n$|^--quiet$|^--silent$/;
|
|
8
|
+
const SED_LINE_SCRIPT = /^(\d+)(?:,(\d+|\$))?p$/;
|
|
9
|
+
const HEAD_DEFAULT_LINES = 10;
|
|
10
|
+
export function parseBashFileRead(command) {
|
|
11
|
+
const trimmed = command.trim();
|
|
12
|
+
if (!trimmed || hasExecutableSubstitution(trimmed))
|
|
13
|
+
return undefined;
|
|
14
|
+
const lead = splitLeadingCd(trimmed);
|
|
15
|
+
if (!lead)
|
|
16
|
+
return undefined;
|
|
17
|
+
const read = parseOneRead(lead.rest);
|
|
18
|
+
return read && lead.dir !== undefined ? { ...read, dir: lead.dir } : read;
|
|
19
|
+
}
|
|
20
|
+
// One `cd <dir> &&` hop with a literal target. Joined by `;` the read runs even when the cd failed,
|
|
21
|
+
// so the path would resolve against the wrong directory; a second hop is left to the separator rule.
|
|
22
|
+
function splitLeadingCd(command) {
|
|
23
|
+
if (!/^cd\s/.test(command))
|
|
24
|
+
return { rest: command };
|
|
25
|
+
const masked = maskQuoted(command);
|
|
26
|
+
const at = masked.indexOf('&&');
|
|
27
|
+
if (at < 0 || /[;&|<>\r\n]/.test(masked.slice(0, at)))
|
|
28
|
+
return undefined;
|
|
29
|
+
const hop = words(command.slice(0, at));
|
|
30
|
+
if (hop.length !== 2 || hop[1] === '-' || /[$`]/.test(hop[1]))
|
|
31
|
+
return undefined;
|
|
32
|
+
return { dir: hop[1], rest: command.slice(at + 2).trim() };
|
|
33
|
+
}
|
|
34
|
+
function parseOneRead(command) {
|
|
35
|
+
const masked = maskQuoted(command);
|
|
36
|
+
const pipe = masked.indexOf('|');
|
|
37
|
+
if (pipe >= 0) {
|
|
38
|
+
if (/[;&|<>\r\n]/.test(masked.slice(0, pipe) + masked.slice(pipe + 1)))
|
|
39
|
+
return undefined;
|
|
40
|
+
return parseHeadTailWindow(command.slice(0, pipe), command.slice(pipe + 1));
|
|
41
|
+
}
|
|
42
|
+
if (/[;&<>\r\n]/.test(masked))
|
|
43
|
+
return undefined;
|
|
44
|
+
const [name, ...args] = words(command);
|
|
45
|
+
if (name === 'cat') {
|
|
46
|
+
const files = args.filter(a => !CAT_FLAGS.test(a));
|
|
47
|
+
if (files.length !== 1 || files[0].startsWith('-'))
|
|
48
|
+
return undefined;
|
|
49
|
+
return { path: files[0], range: { from: 1 } };
|
|
50
|
+
}
|
|
51
|
+
if (name === 'head' || name === 'tail')
|
|
52
|
+
return parseHeadTail(name, args);
|
|
53
|
+
if (name === 'sed')
|
|
54
|
+
return parseSedPrint(args);
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
function parseHeadTail(name, args) {
|
|
58
|
+
const parsed = parseLineCount(args);
|
|
59
|
+
if (!parsed || parsed.files.length !== 1)
|
|
60
|
+
return undefined;
|
|
61
|
+
const { n, plus, files } = parsed;
|
|
62
|
+
const path = files[0];
|
|
63
|
+
if (name === 'head')
|
|
64
|
+
return plus ? undefined : { path, range: { from: 1, count: n } };
|
|
65
|
+
return { path, range: plus ? { from: n } : { last: n } };
|
|
66
|
+
}
|
|
67
|
+
// `head -n A f | tail -n B`, the slice a model pages a region with when it has no `sed -n`.
|
|
68
|
+
function parseHeadTailWindow(left, right) {
|
|
69
|
+
const [headName, ...headArgs] = words(left);
|
|
70
|
+
const [tailName, ...tailArgs] = words(right);
|
|
71
|
+
if (headName !== 'head' || tailName !== 'tail')
|
|
72
|
+
return undefined;
|
|
73
|
+
const head = parseLineCount(headArgs);
|
|
74
|
+
const tail = parseLineCount(tailArgs);
|
|
75
|
+
if (!head || !tail || head.plus || tail.plus)
|
|
76
|
+
return undefined;
|
|
77
|
+
if (head.files.length !== 1 || tail.files.length !== 0)
|
|
78
|
+
return undefined;
|
|
79
|
+
return { path: head.files[0], range: { last: tail.n, within: head.n } };
|
|
80
|
+
}
|
|
81
|
+
function parseLineCount(args) {
|
|
82
|
+
let n = HEAD_DEFAULT_LINES;
|
|
83
|
+
let plus = false;
|
|
84
|
+
const files = [];
|
|
85
|
+
for (let i = 0; i < args.length; i++) {
|
|
86
|
+
const a = args[i];
|
|
87
|
+
let value;
|
|
88
|
+
if (a === '-n' || a === '--lines')
|
|
89
|
+
value = args[++i];
|
|
90
|
+
else if (a.startsWith('--lines='))
|
|
91
|
+
value = a.slice('--lines='.length);
|
|
92
|
+
else if (/^-n.+/.test(a))
|
|
93
|
+
value = a.slice(2);
|
|
94
|
+
else if (/^-\d+$/.test(a))
|
|
95
|
+
value = a.slice(1);
|
|
96
|
+
else if (a.startsWith('-'))
|
|
97
|
+
return undefined;
|
|
98
|
+
else {
|
|
99
|
+
files.push(a);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const m = value?.match(/^(\+)?(\d+)$/);
|
|
103
|
+
if (!m)
|
|
104
|
+
return undefined;
|
|
105
|
+
plus = !!m[1];
|
|
106
|
+
n = Number(m[2]);
|
|
107
|
+
}
|
|
108
|
+
return n < 1 ? undefined : { n, plus, files };
|
|
109
|
+
}
|
|
110
|
+
function parseSedPrint(args) {
|
|
111
|
+
if (args.length !== 3 || !SED_PRINT_FLAG.test(args[0]))
|
|
112
|
+
return undefined;
|
|
113
|
+
const m = args[1].match(SED_LINE_SCRIPT);
|
|
114
|
+
const path = args[2];
|
|
115
|
+
if (!m || path.startsWith('-'))
|
|
116
|
+
return undefined;
|
|
117
|
+
const from = Number(m[1]);
|
|
118
|
+
if (from < 1)
|
|
119
|
+
return undefined;
|
|
120
|
+
if (m[2] === undefined)
|
|
121
|
+
return { path, range: { from, count: 1 } };
|
|
122
|
+
if (m[2] === '$')
|
|
123
|
+
return { path, range: { from } };
|
|
124
|
+
const to = Number(m[2]);
|
|
125
|
+
return to >= from ? { path, range: { from, count: to - from + 1 } } : undefined;
|
|
126
|
+
}
|
|
127
|
+
// The key ReadTrace files a path under: project-relative when inside the project, so `read src/a.ts`
|
|
128
|
+
// and `cat /repo/src/a.ts` deepen one file rather than two.
|
|
129
|
+
export function traceReadPath(cwd, path) {
|
|
130
|
+
const full = resolveUserPath(cwd, path);
|
|
131
|
+
const rel = relative(cwd, full);
|
|
132
|
+
return rel && !rel.startsWith('..') && !isAbsolute(rel) ? rel : full;
|
|
133
|
+
}
|
|
134
|
+
// A path spelled in another case on a case-insensitive disk (`cd /repo/WT && cat a.ts`, observed)
|
|
135
|
+
// escapes the project by string; the real paths file it under the key `read` uses.
|
|
136
|
+
async function traceKey(cwd, full) {
|
|
137
|
+
const key = traceReadPath(cwd, full);
|
|
138
|
+
if (!isAbsolute(key))
|
|
139
|
+
return key;
|
|
140
|
+
try {
|
|
141
|
+
const real = traceReadPath(await realpath(cwd), await realpath(full));
|
|
142
|
+
return isAbsolute(real) ? key : real;
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
return key;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
// A file larger than this is not hashed: a cat of it is not the re-read loop this tracks.
|
|
149
|
+
const MAX_TRACED_BYTES = 2 * 1024 * 1024;
|
|
150
|
+
// What ReadTrace.record needs for a successful shell read, or undefined when the command is not a
|
|
151
|
+
// plain one-file read or the file cannot be read now. Hashed exactly as `read` hashes, so the two
|
|
152
|
+
// share one file entry and an edit in between resets it.
|
|
153
|
+
export async function traceBashRead(command, cwd) {
|
|
154
|
+
const parsed = parseBashFileRead(command);
|
|
155
|
+
if (!parsed)
|
|
156
|
+
return undefined;
|
|
157
|
+
const full = resolveUserPath(parsed.dir === undefined ? cwd : resolveUserPath(cwd, parsed.dir), parsed.path);
|
|
158
|
+
try {
|
|
159
|
+
const info = await stat(full);
|
|
160
|
+
if (!info.isFile() || info.size > MAX_TRACED_BYTES)
|
|
161
|
+
return undefined;
|
|
162
|
+
const text = await readFile(full, 'utf8');
|
|
163
|
+
const hash = createHash('sha1').update(text).digest('hex');
|
|
164
|
+
const path = await traceKey(cwd, full);
|
|
165
|
+
const r = parsed.range;
|
|
166
|
+
if ('last' in r) {
|
|
167
|
+
const lines = text.split('\n');
|
|
168
|
+
const length = lines.length > 1 && lines.at(-1) === '' ? lines.length - 1 : lines.length;
|
|
169
|
+
const total = r.within === undefined ? length : Math.min(length, r.within);
|
|
170
|
+
return { path, offset: Math.max(1, total - r.last + 1), limit: r.last, hash };
|
|
171
|
+
}
|
|
172
|
+
return { path, offset: r.from, limit: r.count, hash };
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
}
|
package/dist/agent/compaction.js
CHANGED
|
@@ -66,6 +66,11 @@ export function foldPoint(history, contextWindow, calibration = 1, minGen = DEFA
|
|
|
66
66
|
break;
|
|
67
67
|
keepFrom = i;
|
|
68
68
|
}
|
|
69
|
+
// #822: a newest message larger than the whole keep budget (a big paste or @-mention on a small
|
|
70
|
+
// window) left keepFrom past the end. Keep it anyway and fold what precedes it: returning null
|
|
71
|
+
// instead would send the over-threshold request unshrunk.
|
|
72
|
+
if (keepFrom === history.length)
|
|
73
|
+
keepFrom = history.length - 1;
|
|
69
74
|
// Snap keepFrom back to the start of its tool-call group so a kept tool result is never orphaned
|
|
70
75
|
// from the assistant tool_call it answers (a bare leading tool message is an API error). Walking
|
|
71
76
|
// *back* to a group boundary — rather than the old *forward* snap to a user message — is what lets
|
|
@@ -34,6 +34,190 @@ export function compactionReportEnabled() {
|
|
|
34
34
|
export const COMPACTION_REPORT_RETRY = '(reika: your reply carried no note — the content was empty or was a tool call, and tools are ' +
|
|
35
35
|
'withdrawn for this reply. Write the compaction note now, as your reply, not as a tool call: ' +
|
|
36
36
|
'what you have established with file paths and function names, then what is still open.)';
|
|
37
|
+
// What a call made in the note round gets back (#806). Re-sending the same request with a longer
|
|
38
|
+
// directive left the model never seeing its call refused, and LFM2.5, a pruned qwen3.6-35b-a3b
|
|
39
|
+
// and qwen3.8-27b each called again; banning the call token at the sampler only moved the call
|
|
40
|
+
// into other dialects. A refused call in its own dialect is what these models recover from (the
|
|
41
|
+
// read-withdrawal refusal is the same move), and appended it costs no re-prefill.
|
|
42
|
+
export const COMPACTION_CALL_REFUSED = 'Not run: tools are unavailable while the compaction note is written. Write the note as your ' +
|
|
43
|
+
'reply now.';
|
|
44
|
+
// The turn the retry appends after a reply that called: that reply, and a refusal for each call.
|
|
45
|
+
// Request-only — never pushed to history. Empty when the reply made no call.
|
|
46
|
+
export function refusedCallTurn(reply) {
|
|
47
|
+
if (!reply.toolCalls?.length)
|
|
48
|
+
return [];
|
|
49
|
+
return [
|
|
50
|
+
{
|
|
51
|
+
role: 'assistant',
|
|
52
|
+
content: reply.content ?? '',
|
|
53
|
+
reasoning: reply.reasoning,
|
|
54
|
+
toolCalls: reply.toolCalls,
|
|
55
|
+
},
|
|
56
|
+
...reply.toolCalls.map((c) => ({
|
|
57
|
+
role: 'tool',
|
|
58
|
+
callId: c.id,
|
|
59
|
+
summary: COMPACTION_CALL_REFUSED,
|
|
60
|
+
})),
|
|
61
|
+
];
|
|
62
|
+
}
|
|
63
|
+
// A reply that made a call has no note in its content, only the call's preamble ("Let me examine
|
|
64
|
+
// `_net.ts` in detail:"): observed recapped as note 1 under the own-words header while that reply's
|
|
65
|
+
// 970-char reasoning — the actual findings — was dropped (#799). Text-format callers (LFM) call
|
|
66
|
+
// through `tool_choice: 'none'`, so this is not rare.
|
|
67
|
+
export function compactionNoteContent(reply) {
|
|
68
|
+
if (reply.toolCalls?.length)
|
|
69
|
+
return '';
|
|
70
|
+
return reply.content?.trim() ?? '';
|
|
71
|
+
}
|
|
72
|
+
// Content from a call-free reply wins; otherwise the longer reasoning of the two replies. Longer
|
|
73
|
+
// rather than first: the first was assumed the less nagged thinking, but a reply that bails into a
|
|
74
|
+
// call early is short exactly because it stopped thinking (#799: 111 chars against 970).
|
|
75
|
+
export function pickCompactionNote(first, retry) {
|
|
76
|
+
for (const r of [first, retry]) {
|
|
77
|
+
if (!r)
|
|
78
|
+
continue;
|
|
79
|
+
const text = compactionNoteContent(r);
|
|
80
|
+
if (text)
|
|
81
|
+
return { text, reasoning: r.reasoning?.trim() || undefined, src: 'content' };
|
|
82
|
+
}
|
|
83
|
+
const a = first.reasoning?.trim() ?? '';
|
|
84
|
+
const b = retry?.reasoning?.trim() ?? '';
|
|
85
|
+
const best = b.length > a.length ? b : a;
|
|
86
|
+
// No trace beside it: the note IS the reasoning, and the UI would draw the same text twice.
|
|
87
|
+
return best ? { text: best, src: 'reasoning' } : { text: '', src: 'empty' };
|
|
88
|
+
}
|
|
89
|
+
// The note round's reply is started for the model, on a local engine (#806). The round continues an
|
|
90
|
+
// agent transcript, and a small model's strongest prior there is the next action: LFM2.5 called a
|
|
91
|
+
// tool instead of writing the note on every T2 fold (0 of 21 notes from the reply), through the
|
|
92
|
+
// directive, the sampler ban and the refused-call retry alike. Handed a reply that is already a
|
|
93
|
+
// note, it continued it: 3 of 3 notes, 0 calls, on llama.cpp (the reasoning block is skipped).
|
|
94
|
+
// Ends on a newline — ending on "- " started bullets with junk. `0` is the baseline arm.
|
|
95
|
+
export function notePrefillEnabled() {
|
|
96
|
+
return process.env.REIKA_NOTE_PREFILL !== '0';
|
|
97
|
+
}
|
|
98
|
+
const NOTE_PREFILL_BODY = 'Established:\n';
|
|
99
|
+
export function compactionNotePrefill(n) {
|
|
100
|
+
return `Compaction note ${n}\n\n${NOTE_PREFILL_BODY}`;
|
|
101
|
+
}
|
|
102
|
+
// Below this, a prefilled reply that ends in a call is a lead-in ("Established:\nLet me check
|
|
103
|
+
// X"), not a note, and stays a call.
|
|
104
|
+
export const PREFILLED_NOTE_MIN_CHARS = 200;
|
|
105
|
+
// A prefilled reply as the note it continues. llama.cpp returns the prefill inside `content`, an
|
|
106
|
+
// engine that does not continue it returns only what came after, so both shapes are accepted. The
|
|
107
|
+
// title line is dropped (the recap carries its own header), and a reply that wrote nothing after
|
|
108
|
+
// the prefill is no note: the prefill alone would otherwise pass as one. A call after a written
|
|
109
|
+
// note is dropped (`callsDropped`) rather than voiding it: LFM2.5 wrote 1206 chars of note and then
|
|
110
|
+
// called, on both attempts, and #799's rule threw both notes away. That rule is about an
|
|
111
|
+
// unprefilled reply, whose text is the call's preamble; here the text is the note by construction.
|
|
112
|
+
//
|
|
113
|
+
// The prefill skips the reasoning block, and LFM2.5 then writes its next-step thinking after the
|
|
114
|
+
// note and closes it with a bare `</think>` (both folds of lfm16-prefill-2), so the reply is cut at
|
|
115
|
+
// that tag. Thinking just before it is cut too when it opens like a next action (#839, above).
|
|
116
|
+
// LFM2.5 also writes its next-step thinking into the note with no `</think>` after it (#839): 8 of
|
|
117
|
+
// 10 notes on the T2 bench ended in a "Let me run the tests now:" lead-in, and one ran 11k chars of
|
|
118
|
+
// planning and 11 text-form calls past two bullets of note, clamped to 4000 chars of mostly plan.
|
|
119
|
+
// The note has no structural edge to cut at — its own prose paragraphs ("Still open: …", "Next:
|
|
120
|
+
// read headless.ts …") are real findings — but the thinking opens with a first-person next action
|
|
121
|
+
// and the note does not. Over all 50 prefilled notes in saved sessions, every prose paragraph after
|
|
122
|
+
// the first such opener was thinking or a pasted file, never a finding. The first paragraph is
|
|
123
|
+
// never cut: a reply that is only a lead-in is PREFILLED_NOTE_MIN_CHARS's case.
|
|
124
|
+
const NEXT_ACTION_OPENER = /^(?:Let me|Let's|Now,? (?:I|let)|I'll|I will|I need to|I should|I'm going to|First,? (?:let me|I)|Next,? (?:let me|I)|Okay|OK[,.]|Wait|Hmm|So I|Actually|Alright)\b/;
|
|
125
|
+
// Where the note's own text ends: the start of the first paragraph after the first that opens with
|
|
126
|
+
// a next action, or the text's length.
|
|
127
|
+
export function noteEndBeforeNextAction(text) {
|
|
128
|
+
const re = /\n[ \t]*\n[ \t]*/g;
|
|
129
|
+
for (let m = re.exec(text); m; m = re.exec(text)) {
|
|
130
|
+
const start = m.index + m[0].length;
|
|
131
|
+
if (NEXT_ACTION_OPENER.test(text.slice(start)))
|
|
132
|
+
return m.index;
|
|
133
|
+
}
|
|
134
|
+
return text.length;
|
|
135
|
+
}
|
|
136
|
+
export function prefilledReply(reply, prefill) {
|
|
137
|
+
const raw = reply.content ?? '';
|
|
138
|
+
const continued = raw.startsWith(prefill) ? raw.slice(prefill.length) : raw;
|
|
139
|
+
const close = continued.indexOf('</think>');
|
|
140
|
+
const before = close === -1 ? continued : continued.slice(0, close).trimEnd();
|
|
141
|
+
// A tag closed at once puts the note after it instead.
|
|
142
|
+
const rest = before.trim() || close === -1 ? before : continued.slice(close + 8).trimStart();
|
|
143
|
+
if (!rest.trim())
|
|
144
|
+
return { ...reply, content: '' };
|
|
145
|
+
const content = NOTE_PREFILL_BODY + rest.slice(0, noteEndBeforeNextAction(rest)).trimEnd();
|
|
146
|
+
const calls = reply.toolCalls?.length ?? 0;
|
|
147
|
+
// Decided on the whole reply, before the cut: two bullets and a run-on is a note the model wrote,
|
|
148
|
+
// where a bare "Let me check X" is a lead-in, and the cut leaves the first paragraph either way.
|
|
149
|
+
if (calls && rest.trim().length >= PREFILLED_NOTE_MIN_CHARS) {
|
|
150
|
+
return { ...reply, content, toolCalls: undefined, callsDropped: calls };
|
|
151
|
+
}
|
|
152
|
+
return { ...reply, content };
|
|
153
|
+
}
|
|
154
|
+
const NOTE_STREAM_STOPS = ['</think>', '<tool_call>'];
|
|
155
|
+
// The live view of a prefilled reply, read the way `prefilledReply` reads the finished one (#819):
|
|
156
|
+
// streamed raw, the next-step thinking, the `</think>` and the call after the note drew and then
|
|
157
|
+
// vanished when the note committed, which read as a call the harness had hidden. A tail that could
|
|
158
|
+
// still become a stop tag is held back until the next delta settles it. One filter per attempt.
|
|
159
|
+
export function prefilledNoteStream(prefill, emit) {
|
|
160
|
+
let raw = '';
|
|
161
|
+
let shown = 0;
|
|
162
|
+
let stopped = false;
|
|
163
|
+
return delta => {
|
|
164
|
+
if (stopped)
|
|
165
|
+
return;
|
|
166
|
+
raw += delta;
|
|
167
|
+
// Still possibly the echo: wait until it is settled one way or the other.
|
|
168
|
+
if (prefill.startsWith(raw))
|
|
169
|
+
return;
|
|
170
|
+
let rest = raw.startsWith(prefill) ? raw.slice(prefill.length) : raw;
|
|
171
|
+
let visible = rest;
|
|
172
|
+
for (;;) {
|
|
173
|
+
const hits = NOTE_STREAM_STOPS.map(t => [rest.indexOf(t), t.length]).filter(([i]) => i !== -1);
|
|
174
|
+
if (!hits.length) {
|
|
175
|
+
// Trailing whitespace is held too: the committed note is trimmed, and it may precede a tag.
|
|
176
|
+
visible = rest.slice(0, rest.length - partialStopTail(rest)).trimEnd();
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
const [at, len] = hits.reduce((a, b) => (b[0] < a[0] ? b : a));
|
|
180
|
+
if (rest.slice(0, at).trim()) {
|
|
181
|
+
visible = rest.slice(0, at).trimEnd();
|
|
182
|
+
stopped = true;
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
// A tag closed at once: the note comes after it.
|
|
186
|
+
rest = rest.slice(at + len).trimStart();
|
|
187
|
+
}
|
|
188
|
+
// The same edge the commit cuts at (#839). A paragraph whose opening words have not all arrived
|
|
189
|
+
// yet is held back until they settle it, so a lead-in never draws and then vanishes.
|
|
190
|
+
const end = noteEndBeforeNextAction(visible);
|
|
191
|
+
if (end < visible.length) {
|
|
192
|
+
visible = visible.slice(0, end).trimEnd();
|
|
193
|
+
stopped = true;
|
|
194
|
+
}
|
|
195
|
+
else if (!stopped) {
|
|
196
|
+
visible = visible.slice(0, visible.length - undecidedParagraphTail(visible)).trimEnd();
|
|
197
|
+
}
|
|
198
|
+
if (!visible)
|
|
199
|
+
return;
|
|
200
|
+
const text = NOTE_PREFILL_BODY + visible;
|
|
201
|
+
if (text.length > shown)
|
|
202
|
+
emit(text.slice(shown));
|
|
203
|
+
shown = Math.max(shown, text.length);
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
// How many trailing chars are a new paragraph too short yet to tell whether it opens with a next
|
|
207
|
+
// action. 24 covers the longest opener ("I'm going to", "First, let me") with room for the word after.
|
|
208
|
+
function undecidedParagraphTail(text) {
|
|
209
|
+
const m = /\n[ \t]*\n[ \t]*([^\n]*)$/.exec(text);
|
|
210
|
+
return m && m[1].length < 24 ? text.length - m.index : 0;
|
|
211
|
+
}
|
|
212
|
+
// How many trailing chars could be the start of a stop tag.
|
|
213
|
+
function partialStopTail(text) {
|
|
214
|
+
for (let n = Math.min(text.length, 10); n > 0; n--) {
|
|
215
|
+
const tail = text.slice(-n);
|
|
216
|
+
if (NOTE_STREAM_STOPS.some(t => t.startsWith(tail)))
|
|
217
|
+
return n;
|
|
218
|
+
}
|
|
219
|
+
return 0;
|
|
220
|
+
}
|
|
37
221
|
// Hard cap on a note — a runaway guard only. The directive asks for ~1500 chars and the fold sizes
|
|
38
222
|
// the note under NOTE_SHARE of the recap budget (`fitNote`), which is the real bound. This used to
|
|
39
223
|
// be 2400 and bit first: a 2400+ char note was cut here while the recap still had room, and the
|