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
193 changes: 153 additions & 40 deletions crates/dim-agent-capabilities/src/timeline_tree.rs

Large diffs are not rendered by default.

1,263 changes: 1,254 additions & 9 deletions crates/dim-agent-capabilities/tests/branch_flow.rs

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions crates/dim-agent-runtime/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ pub enum AgentError {
Multiple(Vec<AgentError>),
}

/// Why the tree refused an operation on a line, named the way the protocol's
/// `appError` names it ([timeline branches §14](../../../docs/design/timeline-branches.md#14-clients)).
/// Why the tree refused an operation on a line, named the way the protocol
/// names it: the codes `session.branch_set_default` answers as its `appError`
/// and the codes the branch tool answers as its own result
/// ([timeline branches §11](../../../docs/design/timeline-branches.md#11-the-branch-tool),
/// [§14](../../../docs/design/timeline-branches.md#14-clients)).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BranchRefusal {
/// The target is already the Session's default branch.
Expand All @@ -65,6 +68,12 @@ pub enum BranchRefusal {
/// the default branch. The root's own refusal is the operation's input
/// error, which its callers already answer.
CannotEnd,
/// The caller may not open a line: only the root, and the current default
/// branch, open one.
NotOpener,
/// The Session holds its limit of open tool lines, counting the admitted
/// reservations.
LimitReached,
}

impl BranchRefusal {
Expand All @@ -75,6 +84,8 @@ impl BranchRefusal {
Self::NotSelectable => "branch_not_selectable",
Self::Protected => "branch_target_protected",
Self::CannotEnd => "branch_cannot_end",
Self::NotOpener => "branch_not_opener",
Self::LimitReached => "branch_limit_reached",
}
}
}
Expand Down
32 changes: 24 additions & 8 deletions docs/architecture/rust-agent/plugins/BranchTool.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ It is installed with the default execution loop; the minimal execution profile
leaves it out. The facts it emits and consumes are BranchLine's, the branch
part below.

`create` passes the optional idle notice policy to TimelineTree. The child
restores it from its opening identity before BranchLine handles turn ends. `create` admits
two callers and refuses every other line: the root, in every state, and the current
default branch. The root's right never changes, which is what lets a user ask it for a
line while another line holds the role. The cap is a total over the Session and counts
the open tool lines that are not on the protected path, so a line the user keeps
continuing on costs no slot.
`create` passes the optional idle notice policy and the Session's cap to TimelineTree.
The child restores the idle notice from its opening identity before BranchLine handles
turn ends. `create` admits two callers and refuses every other line: the root, in every
state, and the current default branch. The root's right never changes, which is what
lets a user ask it for a line while another line holds the role. The cap is a total
over the Session and counts the open tool lines that are not on the protected path, so
a line the user keeps continuing on costs no slot. TimelineTree charges the caller's
eligibility and the cap in the lock that reserves the new line, so a switch or a second
create cannot slip between a check and the reservation; publication charges nothing a
second time, and a creation that fails releases its slot.

`end` ends the calling line and nothing else, so it guards the caller as well as the
target: only a tool line ends itself, a plugin line follows its own protocol, and a line
Expand Down Expand Up @@ -64,6 +67,15 @@ specific target from its own `step/tool-use` and `step/tool-result` (design [§6
[branch_flow.rs](../../../../crates/dim-agent-capabilities/tests/branch_flow.rs):
`a_stop_settles_the_branch_tool_while_the_tree_still_builds_the_child`,
`a_refused_branch_call_writes_nothing`,
`the_root_opens_a_line_while_another_line_is_default`,
`the_default_branch_opens_a_line`,
`a_former_default_branch_keeps_its_lines_and_opens_none`,
`a_plugin_line_never_opens_a_line`,
`a_tool_line_still_cannot_open_a_line`,
`the_cap_does_not_count_the_default_branch_or_its_ancestors`,
`a_switch_over_the_cap_closes_nothing`,
`a_switch_that_frees_a_slot_admits_the_next_open`,
`the_cap_is_refused_under_the_tree_lock`,
`a_tool_line_ends_itself_and_main_learns_it_from_the_roster`,
`a_message_reaches_its_receiver_in_the_mode_it_asked_for`,
`a_first_parent_read_starts_after_the_cut`,
Expand Down Expand Up @@ -130,7 +142,8 @@ Consumes `branch/create`, `branch/create-failed`, `branch/ended`, `branch/end`,
filled when the move committed, woken by its move signal. It
restates the line's birth identity byte for byte and then the sentences the move changes; the
root never receives one, and a load whose latest block disagrees with the tree writes the
missing note once (design [§5](../../../design/timeline-branches.md#5-parts), [§12.1](../../../design/timeline-branches.md#121-context-mapping)).
missing note once; on the tree's current default branch the load also compares that block's text
with the note this code writes, and replaces a note an older rule set left once (design [§5](../../../design/timeline-branches.md#5-parts), [§12.1](../../../design/timeline-branches.md#121-context-mapping)).
- **Roster.** On a line that has opened tool lines, a keyed Append under `branch:roster` of `<dim-branch-lines>` lists the lines it opened with the tool that are still open, in the order it opened them, with the
first line of each purpose; when none is open the key is retired. A plugin line is never on a roster, and neither is a line the user opened: it stays in the Session's directory and coordinates its own tool lines. A child whose inherited prefix holds a live roster gets one block in its opening input that retires that key, so a line that opened no tool line of its own keeps no list; TimelineTree writes it with the identity block (design [§7.1](../../../design/timeline-branches.md#71-keyed-state-branchline-branchroster-and-branchparent)).
- **Parent block.** On a line that is not the root, one keyed Append of
Expand Down Expand Up @@ -176,6 +189,9 @@ BranchLine mounts at the committed Dispatcher cutoff without reading historical
`a_roster_and_an_identity_replay_separately_after_a_compaction`,
`a_child_cut_after_a_roster_retires_it_and_one_cut_before_gets_nothing`,
`the_roots_roster_from_before_the_key_split_is_retired_at_load`,
`an_opener_that_is_not_the_root_keeps_its_identity_and_its_roster`,
`a_status_note_and_a_live_roster_do_not_retire_each_other`,
`a_user_opened_default_branch_coordinates_the_line_it_opens`,
`a_line_messages_and_reads_its_parent_and_the_lines_it_opened`,
`a_sibling_or_grandparent_is_refused_with_nothing_written`,
`an_idle_line_is_told_its_parent_moved_without_being_woken`,
Expand Down
2 changes: 1 addition & 1 deletion docs/architecture/rust-agent/plugins/TimelineTree.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ only active lines read their opening source. Parent-child adjacency orders the d
A Session with one line is on its root without a query; a Session with more reads the root's
newest `branch/default` or `session/forked` through the kind index to find the default branch.
The App Server only maps these owner-produced properties into its wire fields.
`TreeCommands.page_end` resolves a raw-event page endpoint using atomic-batch metadata through the live Timeline or the ended line’s store. It decodes no event body and has no wire binding. The default branch's answer and its write are `TreeCommands` methods too: `default_branch()` answers the line the Session's conversation is on, restored from the root's newest `branch/default` or `session/forked`, and `set_default_branch(id, source)` admits an open conversation line whose chain is not ending, then writes `branch/default` on the root. Its refusals are `branch_already_default`, `branch_not_selectable` and `branch_target_protected`, and it writes nothing for any of them. A move queues its transition on every participant that is not the root, so a line owes its note from the moment it can take part, and signals every line; each line's own BranchLine drains its queue and writes the one status note its role earns; a load whose latest `branch:line` block disagrees with the tree writes the missing note once.
`TreeCommands.page_end` resolves a raw-event page endpoint using atomic-batch metadata through the live Timeline or the ended line’s store. It decodes no event body and has no wire binding. The default branch's answer and its write are `TreeCommands` methods too: `default_branch()` answers the line the Session's conversation is on, restored from the root's newest `branch/default` or `session/forked`, and `set_default_branch(id, source)` admits an open conversation line whose chain is not ending, then writes `branch/default` on the root. Its refusals are `branch_already_default`, `branch_not_selectable` and `branch_target_protected`, and it writes nothing for any of them. A move queues its transition on every participant that is not the root, so a line owes its note from the moment it can take part, and signals every line; each line's own BranchLine drains its queue and writes the one status note its role earns; a load whose latest `branch:line` block disagrees with the tree writes the missing note once, and on the tree's current default branch a block whose text differs from the note this code writes is replaced once.

## TimelineTree

Expand Down
2 changes: 1 addition & 1 deletion docs/design/timeline-branches.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ BranchLine injects `B:Dispatcher`, `B:TimelineIdentity`, `B:BranchLifecycle` and

It decides the way Goal does: from consumed state, through an owned write task that rechecks before writing, and only on live events after its load cutoff. A reload therefore writes nothing twice, with one exception: a load whose own history ends with a live roster key the fold does not match writes the roster once, because a restart that completed an ended line (§20.7) recorded `branch/ended` before `main`'s replay, so no live event follows it (§6.2).

The same exception reconciles a move that stopped between its fact and its notes. `branch/default` and each status note are separate batches (§4.5), so a daemon that stops after the fact commits leaves the fact in force and a note unwritten. On load, BranchLine compares two things for its own line: the `default` attribute of the latest live `branch:line` block on it, where a missing attribute means not default, and whether `TreeCommands` reports this line as the default branch now. When they differ it writes the note that matches the tree once, before the line's next model request, and a root never writes one. The new-default note names the line the move started from, so a reconciling load has to know that line: `branch/default` carries it, under the `from` field the note already needs (§16), which is what §4.9 admits a fact for, since the reconciled note decides with it. A load that finds its line's block and the tree in agreement writes nothing, so a second restart writes nothing.
The same exception reconciles a move that stopped between its fact and its notes. `branch/default` and each status note are separate batches (§4.5), so a daemon that stops after the fact commits leaves the fact in force and a note unwritten. On load, BranchLine compares two things for its own line: the `default` attribute of the latest live `branch:line` block on it, where a missing attribute means not default, and whether `TreeCommands` reports this line as the default branch now. When they differ it writes the note that matches the tree once, before the line's next model request, and a root never writes one. The new-default note names the line the move started from, so a reconciling load has to know that line: `branch/default` carries it, under the `from` field the note already needs (§16), which is what §4.9 admits a fact for, since the reconciled note decides with it. A load that finds its line's block and the tree in agreement writes nothing, so a second restart writes nothing. On the line the tree reports as the default branch alone, the comparison also reads the text of that block, so a note an older rule set wrote is replaced once at load.

**Restoring the default branch.** A Session whose directory holds one line is on its root and reads nothing for it. A Session with more lines reads one fact: the newest `branch/default` or `session/forked` among the root's own events, through the existing `timeline_events_by_kind` index. A `branch/default` whose target is still open and can be selected names the default branch; a `session/forked` means the default branch is the root, because every `branch/default` the fork copied was written before its `session/forked` and is void; nothing recorded means the root. A target that is no longer open, one naming a plugin line and one naming a line created through a client before the upgrade all fall back to the root (§4).

Expand Down
2 changes: 1 addition & 1 deletion docs/zh/design/timeline-branches.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@

BranchLine 注入 `B:Dispatcher`、`B:TimelineIdentity`、`B:BranchLifecycle` 和 `S:TreeCommands`,提供 `B:BranchLine`。

它的决定方式与 Goal 相同:依据已消费的状态,通过一个写入前会复核的自有写任务,并且只对加载截点之后的实时事件作决定。所以重载时什么都不会写两次,只有一个例外:如果加载时自己的历史里最后有一个仍在生效的名册键,而与折叠出的名册不一致,它就把名册写一次——因为补完一条已结束线的重启(§20.7)在根重放之前就记下了 `branch/ended`,之后没有实时事件跟随(§6.2)。同一条例外补上一次停在事实与说明之间的移动。`branch/default` 和每条状态说明分别是独立的批次(§4.5),所以进程在事实提交之后、某条说明写出之前停下时,事实照样生效、说明却缺了。加载时 BranchLine 对自己这条线比较两件事:它上面最新的有效 `branch:line` 块的 `default` 属性(没有这个属性就算不是默认分支),以及 `TreeCommands` 现在是否报告这条线是默认分支。两者不一致时,它在这条线的下一次模型请求之前补写与树一致的那条说明,一次;根永远不写。给新默认分支的说明会写出移动从哪条线开始,所以补写的加载必须知道那条线:`branch/default` 带着它,放在说明本来就需要的 `from` 字段里(§16),这正是 §4.9 允许一条事实存在的理由——补写的说明要用它来决定。把这条线的块与树对上的加载什么也不写,所以再次重启也不写。
它的决定方式与 Goal 相同:依据已消费的状态,通过一个写入前会复核的自有写任务,并且只对加载截点之后的实时事件作决定。所以重载时什么都不会写两次,只有一个例外:如果加载时自己的历史里最后有一个仍在生效的名册键,而与折叠出的名册不一致,它就把名册写一次——因为补完一条已结束线的重启(§20.7)在根重放之前就记下了 `branch/ended`,之后没有实时事件跟随(§6.2)。同一条例外补上一次停在事实与说明之间的移动。`branch/default` 和每条状态说明分别是独立的批次(§4.5),所以进程在事实提交之后、某条说明写出之前停下时,事实照样生效、说明却缺了。加载时 BranchLine 对自己这条线比较两件事:它上面最新的有效 `branch:line` 块的 `default` 属性(没有这个属性就算不是默认分支),以及 `TreeCommands` 现在是否报告这条线是默认分支。两者不一致时,它在这条线的下一次模型请求之前补写与树一致的那条说明,一次;根永远不写。给新默认分支的说明会写出移动从哪条线开始,所以补写的加载必须知道那条线:`branch/default` 带着它,放在说明本来就需要的 `from` 字段里(§16),这正是 §4.9 允许一条事实存在的理由——补写的说明要用它来决定。把这条线的块与树对上的加载什么也不写,所以再次重启也不写。只有对树报告为默认分支的那条线,比较还会读这个块的文本,所以旧规则写下的说明在加载时被替换一次。

**默认分支的恢复。** 目录里只有一条线的 Session 就在根上,不为它读任何事实。有多条线的 Session 只读一条事实:在根自己的事件里,最新的一条 `branch/default` 或 `session/forked`,走现有的 `timeline_events_by_kind` 索引。目标仍未结束且可以被选中的 `branch/default` 指明默认分支;最新的一条是 `session/forked` 时,默认分支就是根,因为 fork 拷过来的所有 `branch/default` 都写在它的 `session/forked` 之前、一律失效;什么也没记就是根。目标已经不再是开着的线、指向插件线、以及指向升级前经客户端开出的线时,都回退到根(§4)。

Expand Down
1 change: 1 addition & 0 deletions xtask/src/app_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ pub async fn serve_home(
dim_agent_capabilities::timeline_tree::BranchToolConfig {
description: run.overrides.branch_description.clone(),
roster_closing: run.overrides.roster_closing.clone(),
max_open_lines: None,
},
);
dim_app_server::permission_rules::install(&mut builder);
Expand Down