Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions crates/dim-agent-capabilities/src/narrative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1501,10 +1501,15 @@ fn write_line(write: &LineWrite<'_>, working: bool, person: Person) -> String {
}
}

/// The first line of a block's body: after its opening tag and without its
/// closing tag, or of its text when it opens with no tag (§3.4).
/// The first line of a block's body (§3.4).
fn body_line(text: &str) -> &str {
let body = match tag(text) {
body(text).lines().next().unwrap_or_default()
}

/// A block's body: after its opening tag and without its own closing tag, or
/// its text when it opens with no tag (§3.4).
fn body(text: &str) -> &str {
match tag(text) {
Some(head) => {
let name = head.split(' ').next().unwrap_or_default();
let (_, rest) = text.split_once('>').unwrap_or_default();
Expand All @@ -1514,8 +1519,13 @@ fn body_line(text: &str) -> &str {
.trim()
}
None => text,
};
body.lines().next().unwrap_or_default()
}
}

/// A block's body as one line: each run of whitespace, line breaks included,
/// becomes one space, and nothing else is added (§3.4).
fn flat_body(text: &str) -> String {
body(text).split_whitespace().collect::<Vec<_>>().join(" ")
}

/// The value of one attribute of an opening tag, when it has one.
Expand All @@ -1535,16 +1545,17 @@ fn tag(text: &str) -> Option<&str> {
(!head.is_empty() && !head.starts_with('/')).then_some(head)
}

/// An input with blocks only, told by its blocks and the first line of the
/// first block's body, or by its blocks alone when that body is empty (§3.4);
/// a turn it opens is titled the same (§3.6).
/// An input with blocks only, told by its blocks and the first block's body
/// as one line, or by its blocks alone when that body is empty (§3.4); a turn
/// it opens is titled the same (§3.6).
fn blocks_line(blocks: &[InputBlock], live: &[bool]) -> String {
let names = part(&blocks_name(blocks, live));
match blocks.first() {
Some(InputBlock::Raw { text, .. }) if !body_line(text).is_empty() => {
format!("{names}: {}", part(body_line(text)))
}
_ => names,
let Some(InputBlock::Raw { text, .. }) = blocks.first() else {
return names;
};
match flat_body(text) {
body if body.is_empty() => names,
body => format!("{names}: {}", part(&body)),
}
}

Expand Down
6 changes: 4 additions & 2 deletions crates/dim-agent-capabilities/tests/recall_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1903,7 +1903,7 @@ fn opening_a_call_shows_the_effects_it_declared() {
// §3.4, §3.6: a block's body is what lies between its own opening and
// closing tags; a tag inside the body stays, an empty body leaves the
// blocks alone, and a block with no tag is told by its text.
fn a_blocks_line_keeps_its_body_and_drops_only_its_own_closing_tag() {
fn a_blocks_line_keeps_its_body_as_one_line_and_drops_only_its_own_closing_tag() {
let mut recorded = Recorded::new();
recorded.at(10, 0);
let mut turns = Vec::new();
Expand All @@ -1913,6 +1913,7 @@ fn a_blocks_line_keeps_its_body_and_drops_only_its_own_closing_tag() {
"<note><b></b></note>",
"<note></note>",
"plain first line\nsecond line",
"<note>\n 1 client attached:\n\n tui\tpid=4123 \r\n</note>",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 (optional). The new case does not exercise the 120-character cut. This body is short, so the test passes whether the code truncates before or after flattening. Add a long multi-line body and assert the rendered line is 120 characters and ends with an ellipsis; the agreed direction applies the existing cut after flattening.

]
.into_iter()
.enumerate()
Expand All @@ -1935,7 +1936,8 @@ fn a_blocks_line_keeps_its_body_and_drops_only_its_own_closing_tag() {
"note: Close it with </note>",
"note: <b></b>",
"note",
"block: plain first line",
"block: plain first line second line",
"note: 1 client attached: tui pid=4123",
]) {
assert_eq!(
line_of(&index, started),
Expand Down
2 changes: 1 addition & 1 deletion crates/dim-app-server/tests/storage_flow/recall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ async fn sqlite_recall_verifies_indexed_candidates_and_fallbacks_against_the_sam
" turn: note: <note>Ready</note> — ",
" turn: note: <b></b> — ",
" turn: note — ",
" turn: block: plain first line — ",
" turn: block: plain first line second line — ",
] {
assert!(index.contains(title), "{title} in {index}");
}
Expand Down
6 changes: 4 additions & 2 deletions crates/dim-app-server/tests/transport_flow/collaboration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3623,8 +3623,10 @@ async fn member_work(mut config: ServerConfig) {
"the latest turns come first and name their calls: {turns}"
);
assert!(
turns.contains(": Agent default, your team leader, assigned you this work: — called hold"),
"a turn a message opened is titled by its first line: {turns}"
turns.contains(
": Agent default, your team leader, assigned you this work: tool:hold {} — called hold"
),
"a turn a message opened is titled by its body as one line: {turns}"
);
assert!(
turns
Expand Down
7 changes: 4 additions & 3 deletions docs/design/plugins/Recall.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ context is the origin of, or joined to, a thing (§14).

| Thing | Line |
| --- | --- |
| Input | with user content: `user: <text>` (images as `[image]`), followed by `+ <blocks>` when it also carries blocks; with blocks only: `<blocks>: <first line of the first block's body>`, or `<blocks>` alone when that body is empty. A block's body is the text between its opening tag and its own closing tag, so a tag inside the body stays; a block whose text opens with no tag has that text as its body. `<blocks>` names each block in order by its opening tag and attributes as written, or `block` for a block whose text opens with no tag, with `(current)` for a live block and `(ended)` for one that retires its `key` |
| Input | with user content: `user: <text>` (images as `[image]`), followed by `+ <blocks>` when it also carries blocks; with blocks only: `<blocks>: <the first block's body as one line>`, or `<blocks>` alone when that body is empty. A block's body is the text between its opening tag and its own closing tag, so a tag inside the body stays; a block whose text opens with no tag has that text as its body. The body reads as one line: each run of whitespace in it, line breaks included, becomes one space, and nothing else is added, so a body whose first line only leads into the rest still says what follows. `<blocks>` names each block in order by its opening tag and attributes as written, or `block` for a block whose text opens with no tag, with `(current)` for a live block and `(ended)` for one that retires its `key` |
| Your reply | `you: <text>`, thinking left out; an output that did not complete is `your reply was cut off (<reason>): <partial text>`, with the committed text and the outcome of its `step/model-output-end`; an output the head ends inside is `you (still writing): <text so far>` |
| Tool call | `you called <tool> <input brief> → <ending>`; a call whose arguments never arrived is `you started calling <tool> (arguments not received) → <ending>` |
| Interrupt | `the user interrupted the turn` |
Expand Down Expand Up @@ -292,7 +292,8 @@ context)` or `(ended)` where it applies, paged by characters like any thing.
**The input brief** is generic, for every tool alike: the scalar fields of `input` as
`key=value`, nested values as `{…}`. Opening the call shows the full input.

**Every line has a fixed maximum length.** A line is one line: a quoted text is its first line.
**Every line has a fixed maximum length.** A line is one line: a quoted text is its first line,
except a block's body, which reads as one line with its whitespace joined (§3.4).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 (optional). This exception is easy to read as every block body, but a line write still uses its first line. A multi-line message from another line is read by line_write and body_line at crates/dim-agent-capabilities/src/narrative.rs:1442, so Recall shows only the first line, while this sentence says a block's body joins its whitespace. Scope the exception to the first block's body on a blocks-only input and a turn title, and update docs/zh/design/plugins/Recall.md:129, or say that a line write keeps its first line.

Each variable part of a line is cut at 120 characters with `…`: a quoted text (user text, a reply,
a block's body, a result), the input brief as a whole, `<blocks>`, a tool name, a turn's tool
summary, a reason, a task id, an agent name, a mode or a parent line. No line has more than four
Expand Down Expand Up @@ -335,7 +336,7 @@ The inputs that open a turn are the ones it consumed before its first model requ
later, such as a steer while it works, does not open it. The line gives the first user text among
the opening inputs; when none of them carries user text (a Goal continuation, a message from
another line), the first of them that carries a line write (§3.4), or else the driving input as its line
reads, `<blocks>: <first line of the first block's body>`: the first opening input that is not Append, since an Append input joins the request
reads, `<blocks>: <the first block's body as one line>`: the first opening input that is not Append, since an Append input joins the request
but does not open the turn. A turn with no opening input has none of these: its line reads
`turn: no opening input`.

Expand Down
6 changes: 3 additions & 3 deletions docs/zh/design/plugins/Recall.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Recall 就像客户端里的会话视图,只是讲给模型而不是展示给

| 事 | 行 |
| --- | --- |
| 输入 | 有用户内容时:`user: <文本>`(图片为 `[image]`),同时带块时后接 `+ <blocks>`;只有块时:`<blocks>: <第一个块正文的第一行>`,正文为空时只有 `<blocks>`。块的正文是它的开头标签与它自己的结束标签之间的文本,所以正文里的标签保留;文本不以标签开头的块,正文就是它的文本。`<blocks>` 按顺序列出每个块的开头标签和属性(照写),文本不以标签开头的块写作 `block`;生效中的块加 `(current)`,让其 `key` 失效的块加 `(ended)` |
| 输入 | 有用户内容时:`user: <文本>`(图片为 `[image]`),同时带块时后接 `+ <blocks>`;只有块时:`<blocks>: <第一个块的正文,压成一行>`,正文为空时只有 `<blocks>`。块的正文是它的开头标签与它自己的结束标签之间的文本,所以正文里的标签保留;文本不以标签开头的块,正文就是它的文本。正文压成一行:其中每一段连续空白(包括换行)变成一个空格,不加任何别的字,所以第一行只是引子的正文,行里仍能看到后面的内容。`<blocks>` 按顺序列出每个块的开头标签和属性(照写),文本不以标签开头的块写作 `block`;生效中的块加 `(current)`,让其 `key` 失效的块加 `(ended)` |
| 你的回复 | `you: <文本>`,省略 thinking;未完成的输出为 `your reply was cut off (<原因>): <部分文本>`,带已提交的文本和其 `step/model-output-end` 的结果;head 停在其中的输出为 `you (still writing): <目前的文本>` |
| 工具调用 | `you called <tool> <参数简述> → <结局>`;参数从未到达的调用为 `you started calling <tool> (arguments not received) → <结局>` |
| 中断 | `the user interrupted the turn` |
Expand Down Expand Up @@ -126,7 +126,7 @@ Recall 按块文本的开头标签与属性,以及 `InputBlock::Raw` 的 `key`

**参数简述**对所有工具通用:`input` 的标量字段写作 `key=value`,嵌套的值显示为 `{…}`。展开这次调用时给出完整参数。

**每行都有固定的最大长度。** 一行就是一行:引用的文本取其第一行。行中每个可变部分超过 120 个字符时截断并加 `…`:引用的文本(用户文本、回复、块的正文、结果)、整个参数简述、`<blocks>`、工具名、一轮的工具汇总、原因、任务 id、agent 名、模式或父行。一行最多有四个可变部分(最多的是工具调用:工具名、参数简述、任务和结果文本),其时间、id 和固定文字最多占 120 个字符,所以**一行绝不超过 600 个字符**。因此一页 20 个条目,连同日期行和最后一行,不超过 13,000 个字符。每个部分的全文都可用 `open` 展开。这是行格式,不是可调的限制。
**每行都有固定的最大长度。** 一行就是一行:引用的文本取其第一行,块的正文除外,它的空白合并后压成一行(§3.4)。行中每个可变部分超过 120 个字符时截断并加 `…`:引用的文本(用户文本、回复、块的正文、结果)、整个参数简述、`<blocks>`、工具名、一轮的工具汇总、原因、任务 id、agent 名、模式或父行。一行最多有四个可变部分(最多的是工具调用:工具名、参数简述、任务和结果文本),其时间、id 和固定文字最多占 120 个字符,所以**一行绝不超过 600 个字符**。因此一页 20 个条目,连同日期行和最后一行,不超过 13,000 个字符。每个部分的全文都可用 `open` 展开。这是行格式,不是可调的限制。

**结果**是 `content` 的文本;blob 和图片为 `[file]` 与 `[image]`;工具带入的上下文消息为 `with <n> context messages`。展开这次调用时给出全部内容。结果比调用晚到不同的分钟时,结局带上结果的时间。

Expand All @@ -142,7 +142,7 @@ Recall 按块文本的开头标签与属性,以及 `InputBlock::Raw` 的 `key`

### 3.6 轮次

一轮是同一个 `turn_id` 从 `turn/start` 到 `turn/end` 的跨度;它消费的输入属于它。任何轮次之外的事(两轮之间的压缩、开通本行、Session fork)是轮次之间独立的行。索引中一轮的那一行给出它的第一段用户文本、调用过的工具及次数,以及 `turn/end` 中的结局(`completed`、`failed`、`cancelled`)。开启一轮的输入是它在第一次模型请求之前消费的那些;之后才读到的输入,例如它工作时到达的 steer,不开启这一轮。这一行给出开启它的输入中最先的一段用户文本;这些输入都没有用户文本时(Goal 续行、其他行发来的消息),给出其中第一个携带行写的输入(§3.4),否则按驱动这一轮的输入那一行的样子给出 `<blocks>: <第一个块正文的第一行>`,驱动的输入即第一个不是 Append 的开启输入:Append 输入只加入请求,不开启这一轮。没有开启输入的轮次没有这些:它的索引行读作 `turn: no opening input`。
一轮是同一个 `turn_id` 从 `turn/start` 到 `turn/end` 的跨度;它消费的输入属于它。任何轮次之外的事(两轮之间的压缩、开通本行、Session fork)是轮次之间独立的行。索引中一轮的那一行给出它的第一段用户文本、调用过的工具及次数,以及 `turn/end` 中的结局(`completed`、`failed`、`cancelled`)。开启一轮的输入是它在第一次模型请求之前消费的那些;之后才读到的输入,例如它工作时到达的 steer,不开启这一轮。这一行给出开启它的输入中最先的一段用户文本;这些输入都没有用户文本时(Goal 续行、其他行发来的消息),给出其中第一个携带行写的输入(§3.4),否则按驱动这一轮的输入那一行的样子给出 `<blocks>: <第一个块的正文,压成一行>`,驱动的输入即第一个不是 Append 的开启输入:Append 输入只加入请求,不开启这一轮。没有开启输入的轮次没有这些:它的索引行读作 `turn: no opening input`。

固定历史中没有 `turn/end` 的一轮:若是发起调用的这一轮,结局为 `in progress`;若是子分支继承前缀截断的父分支那一轮,结局为 `continued on the parent line`。发起调用的这次 `recall` 本身也在这一轮中,结局为 `no result yet`。

Expand Down