summaryrefslogtreecommitdiffstats
path: root/tests/loop.rs
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-11-15 03:11:22 +0100
committersalmonik <salmonik@tuta.io>2026-11-15 03:11:22 +0100
commit343f2a8a6148b8004d6f4d74fb779b4f30c9686a (patch)
tree2ea1ddab346565309da644517c3478d2a9096e55 /tests/loop.rs
parentM0: streaming provider, SSE decoder, mock provider, i686 build gate (diff)
downloadagent686-343f2a8a6148b8004d6f4d74fb779b4f30c9686a.tar.gz
agent686-343f2a8a6148b8004d6f4d74fb779b4f30c9686a.zip
M1-M5: sessions, six tools, approvals, prompt, Ctrl-C, target gate
- append-only JSONL sessions with --continue/--session; reasoning stored, stripped on replay - read/write/edit/grep/glob + bash with timeout and capped output - diff-preview approval for the first edit of a session, per-call approval for bash - system prompt frozen per session with AGENTS.md snapshot and change warning - SIGINT aborts the turn, second Ctrl-C exits - glibc gate ignores weak symbols; verified in linux/386 debian:12 - acceptance test (calculator in C) passes on both x86-64 and i386
Diffstat (limited to '')
-rw-r--r--tests/loop.rs192
1 files changed, 192 insertions, 0 deletions
diff --git a/tests/loop.rs b/tests/loop.rs
new file mode 100644
index 0000000..d77d890
--- /dev/null
+++ b/tests/loop.rs
@@ -0,0 +1,192 @@
1use std::fs;
2use std::io::{BufRead, BufReader, Read, Write};
3use std::net::TcpListener;
4use std::path::PathBuf;
5use std::sync::atomic::AtomicBool;
6use std::thread;
7
8use serde_json::json;
9
10use agent686::agent;
11use agent686::config::Config;
12use agent686::session::Session;
13use agent686::ui::Approver;
14
15fn sse(body: &str) -> String {
16 format!(
17 "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nConnection: close\r\n\r\n{body}"
18 )
19}
20
21/// Serves one canned SSE response per connection, in order.
22fn serve_sequence(responses: Vec<String>) -> String {
23 let listener = TcpListener::bind("127.0.0.1:0").unwrap();
24 let addr = listener.local_addr().unwrap();
25 thread::spawn(move || {
26 for response in responses {
27 let (mut stream, _) = listener.accept().unwrap();
28 let mut reader = BufReader::new(stream.try_clone().unwrap());
29 let mut length = 0usize;
30 loop {
31 let mut line = String::new();
32 if reader.read_line(&mut line).unwrap_or(0) == 0 {
33 break;
34 }
35 if line == "\r\n" || line == "\n" {
36 break;
37 }
38 if let Some(value) = line.to_ascii_lowercase().strip_prefix("content-length:") {
39 length = value.trim().parse().unwrap_or(0);
40 }
41 }
42 let mut body = vec![0u8; length];
43 let _ = reader.read_exact(&mut body);
44 let _ = stream.write_all(response.as_bytes());
45 let _ = stream.flush();
46 }
47 });
48 format!("http://{addr}")
49}
50
51fn temp_dir(tag: &str) -> PathBuf {
52 let dir = std::env::temp_dir().join(format!("agent686-loop-{tag}-{}", std::process::id()));
53 let _ = fs::remove_dir_all(&dir);
54 fs::create_dir_all(&dir).unwrap();
55 dir
56}
57
58#[test]
59fn runs_a_tool_call_then_finishes_and_records_every_message() {
60 let dir = temp_dir("write");
61 let target = dir.join("hello.txt");
62 let arguments = json!({
63 "path": target.display().to_string(),
64 "content": "hello from the mock\n"
65 })
66 .to_string();
67
68 let tool_round = sse(&format!(
69 "data: {}\n\ndata: [DONE]\n\n",
70 json!({
71 "choices": [{
72 "delta": { "tool_calls": [{
73 "index": 0,
74 "id": "call_1",
75 "function": { "name": "write", "arguments": arguments }
76 }]},
77 "finish_reason": "tool_calls"
78 }]
79 })
80 ));
81 let final_round = sse(
82 "data: {\"choices\":[{\"delta\":{\"content\":\"done\"},\"finish_reason\":\"stop\"}]}\n\n\
83 data: [DONE]\n\n",
84 );
85
86 let base = serve_sequence(vec![tool_round, final_round]);
87
88 std::env::set_var("AGENT686_DATA", dir.join("data"));
89
90 let system = agent::system_prompt();
91 let mut session = Session::create("test-model", &system).unwrap();
92 let cfg = Config {
93 base_url: base,
94 api_key: "test-key".into(),
95 model: "test-model".into(),
96 session: session.id.clone(),
97 yolo: true,
98 };
99 let mut history = agent::new_history(system);
100 let mut approver = Approver::new(true);
101
102 agent::run_turn(
103 &cfg,
104 &mut session,
105 &mut history,
106 "write a file",
107 &AtomicBool::new(false),
108 &mut approver,
109 )
110 .expect("turn should complete");
111
112 assert_eq!(
113 fs::read_to_string(&target).unwrap(),
114 "hello from the mock\n"
115 );
116
117 // system, user, assistant(tool_calls), tool result, assistant(final)
118 assert_eq!(history.len(), 5);
119 assert_eq!(history[3]["role"], "tool");
120 assert_eq!(history[3]["tool_call_id"], "call_1");
121
122 let log = fs::read_to_string(session.path()).unwrap();
123 let lines: Vec<&str> = log.lines().collect();
124 assert_eq!(lines.len(), 5, "header plus four messages: {log}");
125 assert!(lines[0].contains("\"t\":\"header\""));
126 assert_eq!(lines[3].contains("\"role\":\"tool\""), true);
127}
128
129#[test]
130fn a_denied_tool_call_is_returned_as_data() {
131 let dir = temp_dir("denied");
132 let target = dir.join("never.txt");
133 let arguments = json!({
134 "path": target.display().to_string(),
135 "content": "should not exist"
136 })
137 .to_string();
138
139 let tool_round = sse(&format!(
140 "data: {}\n\ndata: [DONE]\n\n",
141 json!({
142 "choices": [{
143 "delta": { "tool_calls": [{
144 "index": 0,
145 "id": "call_1",
146 "function": { "name": "write", "arguments": arguments }
147 }]},
148 "finish_reason": "tool_calls"
149 }]
150 })
151 ));
152 let final_round = sse(
153 "data: {\"choices\":[{\"delta\":{\"content\":\"understood\"},\"finish_reason\":\"stop\"}]}\n\n\
154 data: [DONE]\n\n",
155 );
156
157 let base = serve_sequence(vec![tool_round, final_round]);
158 std::env::set_var("AGENT686_DATA", dir.join("data"));
159
160 let system = agent::system_prompt();
161 let mut session = Session::create("test-model", &system).unwrap();
162 let cfg = Config {
163 base_url: base,
164 api_key: "test-key".into(),
165 model: "test-model".into(),
166 session: session.id.clone(),
167 yolo: false,
168 };
169 let mut history = agent::new_history(system);
170 // Not a terminal, so the approver must deny rather than block.
171 let mut approver = Approver::new(false);
172
173 agent::run_turn(
174 &cfg,
175 &mut session,
176 &mut history,
177 "write a file",
178 &AtomicBool::new(false),
179 &mut approver,
180 )
181 .expect("a denial is not a failed turn");
182
183 assert!(!target.exists());
184 assert_eq!(history[3]["role"], "tool");
185 assert!(
186 history[3]["content"]
187 .as_str()
188 .unwrap()
189 .contains("denied by the user")
190 );
191 assert_eq!(history[4]["content"], "understood");
192}