1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//! The **reply debt** (ARCH §2.6 *A reply is owed once*): whether this
//! branch's terminal response answers anything at all.
//!
//! A reply is an answer, and an answer is owed to a question. Nothing
//! made that a *bound*, so two agents that spoke to each other never
//! stopped: each one's terminal response was deposited into the other's
//! inbox, woke it, and produced a terminal response deposited back. One
//! `message` call in 333 transcript rows; 13.4M tokens on work that
//! finished in four steps, and neither participant could stop it from
//! inside — both said so in the responses that kept being delivered
//! (bl-82d8).
//!
//! The brake is an invariant, not a counter: **a prompt is answered
//! once.** What makes it derivable — never stored — is that the branch's
//! own transcript already records both halves of the exchange in one
//! sequence (§2.3): a delivered message is `messages/NNN-<sender>.md`,
//! and this branch's own model output is `messages/NNN-<model>.json`,
//! whose entry is a **terminal response** exactly when it declares no
//! `tool_use` (§2.5 — that is what ends a step loop). So the window this
//! terminal event answers is *everything delivered since the previous
//! terminal response*, and the debt is whether anything in that window
//! asked this agent for something:
//!
//! - a **prompt** — a delivered message from somebody else — does;
//! - an **own child's return** does: its work-product transfer lands on
//! this branch (§2.6), so it is this agent's own commissioned work
//! arriving toward the answer it is still composing. Read by the same
//! predicate the drain and the interpreter read, `own_result_ref`;
//! - a **foreign reply** — a result message from an agent this one did
//! not dispatch, a sibling's answer above all — does **not**. It is
//! the answer to a question this agent asked, and being answered asks
//! nothing. This one line is the loop's floor;
//! - a **self-note** (§2.11) does not: its answer is the agent's own
//! next step, which has already happened.
//!
//! **A branch that has never answered always owes**, which is the same
//! rule with an empty window rather than a bootstrap case: with no
//! previous terminal response there is nothing this reply could be a
//! repetition of. That also covers a transcript compaction squashed the
//! record out of (§2.6) — erring toward answering, since the cost of a
//! spurious reply is one deposit and the cost of a swallowed one is a
//! conversation that never hears back.
use own_result_ref;
use MESSAGES_DIR;
use crateError;
use Content;
use Path;
/// The one reserved `.json` origin token (§2.3): a tool call's result,
/// never model output, so never a terminal response.
const TOOL_ORIGIN: &str = "tool";
/// One entry under `messages/`, named by the branch's transcript counter
/// (§2.3 — order lives in the filename and nowhere else). **The one
/// reading of the transcript directory** for the whole addressing rule:
/// the debt below and [`super::last_prompter`] walk the same list, so
/// `messages/` is enumerated once per terminal event and neither can
/// parse a name the other would not.
pub
/// Whether this terminal event's reply is owed to anybody (module docs),
/// over the branch's `entries` newest-first. `false` is a structural
/// no-op at the deposit *and* at the wake-up: the terminal response still
/// stands in this agent's own conversation, which is where a reply to the
/// operator is read too (§2.6).
pub
/// The sequence of the terminal response *before* this event's own — the
/// lower bound of the window this reply answers. `None` when the branch
/// has no earlier one (module docs: it has never answered).
///
/// This event's own terminal entry is already committed when the
/// addressing rule is asked (§2.5 — the assistant entry lands before the
/// deposit, and again before [`super::super::terminal::conclude`]
/// re-derives), so it is the newest terminal response and is dropped.
/// Reading stops at the second one found: the window is one run long.
/// A committed model-output entry is a **terminal response** exactly when
/// it declares no `tool_use` block — the §2.5 condition that ends a step
/// loop, read off the entry rather than restated anywhere.
/// Whether a delivered entry asks this agent for something (module docs):
/// a prompt from somebody else, or an own child's return. A foreign
/// reply and a self-note ask nothing.
/// Every legible entry under `messages/`, **newest first** — order lives
/// in the filename (§2.3), and both readers want it descending. An absent
/// directory is an empty transcript (the general path with empty inputs),
/// and a name that is neither `NNN-<origin>.md` nor `NNN-<origin>.json`
/// is not an entry.
pub
/// Split `NNN-<origin>.<md|json>`. The origin token carries hyphens of
/// its own (an agent id's descent, §2.3), so the split is at the *first*
/// hyphen and the remainder is the whole token.