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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! Budget enforcement at the model-call boundary (ARCH §6, v0.7): a
//! conversation that exhausts `max_total_tokens` is stopped before the
//! next adapter invocation and gets the
//! `refs/litany/budget-exhausted/<branch>` marker; an unbounded workflow
//! never triggers a stop.
use super::fixtures::*;
use crate::prompt::run;
use brazen::FinishReason;
use serde_json::json;
/// `budgets: {max_total_tokens: 8}` — exactly one `stream_of` step's
/// `Usage{input:5, output:3}` = 8 tokens, so the step-2 boundary check
/// trips at `8 >= 8`.
const WORKFLOW_WITH_TOKEN_BUDGET: &str = "events: {}\nbudgets:\n max_total_tokens: 8\n";
fn tool_use_stream() -> Vec<u8> {
// Finishes `tool_use` so the loop advances toward a second step.
stream_of(
FinishReason::ToolUse,
&[Block::ToolUse {
id: "toolu_01",
name: "bash",
input: json!({ "cmd": "ls" }),
}],
)
}
#[test]
fn exhausted_conversation_stops_before_next_model_call_and_marks_the_ref() {
let repo = scaffold_repo_with_workflow(
VALID_PER_REPO_PROVIDERS_YAML,
WORKFLOW_WITH_TOKEN_BUDGET,
Some("body"),
);
let harness = scaffold_harness_root();
// Three adapter replies are scripted: the step-1 version guard, step
// 1's model call, and the step-2 boundary's own version guard — its
// resolution precedes the budget check, because the ceiling being
// checked is the freshly followed one (bl-e580). Had the check failed
// to stop the loop, step 2's model call would invoke the adapter a
// fourth time and the stub would panic — so "no model call" is
// enforced structurally as well as asserted below.
let adapter = StubAdapter::scripted([
StubAdapter::reply_ok(&version_line()),
StubAdapter::reply_ok(&tool_use_stream()),
StubAdapter::reply_ok(&version_line()),
]);
let git = StubGit::ok();
let (clock, id) = (FixedClock::default(), FixedIdGen);
let (sleeper, tool_executor) = (StubSleeper::default(), StubToolExecutor::ok());
let branch = run(
repo.path(),
"go",
None,
None,
None,
crate::prompt::PinnedDocs::none(),
None,
&valid_deps(
&adapter,
&sleeper,
&git,
&clock,
&id,
&tool_executor,
harness.path(),
),
)
.unwrap();
assert_eq!(branch, "ct-1-deadbeef");
// Step 1 landed 8 tokens; the step-2 boundary check tripped (8 >= 8),
// so the adapter was invoked exactly three times: the two version
// guards and step 1's model call — never a step-2 model call.
assert_eq!(adapter.observed.borrow().len(), 3);
assert!(repo.path().join("steps/ct-1-deadbeef/001").exists());
// Step 2 was abandoned before its model call — no step-2 record.
assert!(!repo.path().join("steps/ct-1-deadbeef/002").exists());
// The git-native marker was written at the branch tip (§6).
let runs = git.runs.borrow();
assert!(
runs.iter().any(|(_, args)| args
== &vec![
"update-ref".to_string(),
"refs/litany/budget-exhausted/ct-1-deadbeef".to_string(),
"HEAD".to_string(),
]),
"expected budget-exhausted update-ref; got {runs:?}"
);
// Terminal-by-exhaustion: no compaction dispatch, no rebase/merge.
assert!(
!runs.iter().any(|(_, args)| {
let head = args.first().map(String::as_str);
head == Some("rebase") || head == Some("merge")
}),
"exhausted conversation must not merge back; got {runs:?}"
);
}
#[test]
fn unbounded_workflow_never_triggers_a_budget_stop() {
// No `budgets:` block → every limit unbounded → the loop runs to a
// normal terminal completion with no budget stop (baseline).
let repo = scaffold_repo(VALID_PER_REPO_PROVIDERS_YAML, Some("body"));
let harness = scaffold_harness_root();
let adapter = StubAdapter::happy(&happy_response_bytes());
let git = StubGit::ok();
let (clock, id) = (FixedClock::default(), FixedIdGen);
let (sleeper, tool_executor) = (StubSleeper::default(), StubToolExecutor::ok());
run(
repo.path(),
"go",
None,
None,
None,
crate::prompt::PinnedDocs::none(),
None,
&valid_deps(
&adapter,
&sleeper,
&git,
&clock,
&id,
&tool_executor,
harness.path(),
),
)
.unwrap();
// Ran to a normal terminal completion — no budget stop, no ref.
let runs = git.runs.borrow();
assert!(
!runs.iter().any(|(_, args)| {
args.iter()
.any(|a| a.starts_with("refs/litany/budget-exhausted/"))
}),
"no budget-exhausted ref expected under an unbounded workflow"
);
}
#[test]
fn budget_ref_write_failure_surfaces_as_a_git_error() {
// Indices below are relative to the first post-control op; the
// start's preamble precedes them with ten: the fork-point
// lineage query (§2.3), the `config/*` head enumeration and its
// merge-base (the governing ancestry derivation, §2.2), the
// followed-tip enumeration and its containment merge-base (§2.2,
// bl-403b), and
// five `show` reads (`version` first, the §10 schema-version guard;
// manifest.yaml last before the soul, §5.2).
// The marker `update-ref` is git op #34 in the exhaustion path (0
// worktree add, 1 control rm, 2-6 the descriptor derivation (§3.3 —
// four `cat-file -e` existence reads against the governing config
// commit and one `checkout`), 7/8 the facts cut's own existence
// read and `checkout` (§5.5), 9 dispatch add, 10 dispatch commit, 11
// step-1 drain stray-probe, 12/13 user-message delivery add+commit,
// 14 step-1 rev-parse, 15/16 step-1 model-output transcript
// add+commit, 17 the tool window's hold-mark probe (§3.3 *Tool
// control*), 18/19 the tool transcript add+commit, 20-31 the step-2
// boundary's own config resolution (bl-e580 — the ceiling checked
// below is the freshly followed one, so it is read before the
// check), 32 step-2 drain stray-probe, 33 step-2 rev-parse, 34
// mark_exhausted update-ref).
// Failing it surfaces the §6 exhaustion write's error arm.
let repo = scaffold_repo_with_workflow(
VALID_PER_REPO_PROVIDERS_YAML,
WORKFLOW_WITH_TOKEN_BUDGET,
Some("body"),
);
let harness = scaffold_harness_root();
let adapter = StubAdapter::scripted([
StubAdapter::reply_ok(&version_line()),
StubAdapter::reply_ok(&tool_use_stream()),
StubAdapter::reply_ok(&version_line()),
]);
let git = StubGit::failing_at(46);
let (clock, id) = (FixedClock::default(), FixedIdGen);
let (sleeper, tool_executor) = (StubSleeper::default(), StubToolExecutor::ok());
let err = run(
repo.path(),
"go",
None,
None,
None,
crate::prompt::PinnedDocs::none(),
None,
&valid_deps(
&adapter,
&sleeper,
&git,
&clock,
&id,
&tool_executor,
harness.path(),
),
)
.unwrap_err();
assert!(
matches!(
err,
crate::prompt::Error::Git {
op: "budget-exhausted update-ref",
..
}
),
"got {err:?}"
);
}