create-lathe 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +135 -2
- package/bin/create-lathe.js +80 -0
- package/dist/framework.lock +113 -0
- package/dist/package/crates/instrument/Cargo.lock +415 -0
- package/dist/package/crates/instrument/Cargo.toml +27 -0
- package/dist/package/crates/instrument/examples/instrument.rs +73 -0
- package/dist/package/crates/instrument/src/client.rs +128 -0
- package/dist/package/crates/instrument/src/lib.rs +32 -0
- package/dist/package/crates/instrument/src/logging.rs +109 -0
- package/dist/package/crates/instrument/src/protocol.rs +49 -0
- package/dist/package/crates/instrument/src/registry.rs +166 -0
- package/dist/package/crates/instrument/src/selftest.rs +314 -0
- package/dist/package/crates/instrument/src/server.rs +303 -0
- package/dist/package/crates/instrument/tests/command_log.rs +60 -0
- package/dist/package/crates/instrument/tests/round_trip.rs +134 -0
- package/dist/package/crates/instrument/tests/selftest_fail.rs +25 -0
- package/dist/package/crates/instrument/tests/selftest_pass.rs +24 -0
- package/dist/package/crates/instrument/tests/support/mod.rs +38 -0
- package/dist/package/pi/context/journal-de-lot.md +24 -0
- package/dist/package/pi/context/tech-skill.md +19 -0
- package/dist/package/pi/context/ticket-closed.md +18 -0
- package/dist/package/scripts/framework.py +1314 -0
- package/dist/package/scripts/gates.py +1134 -0
- package/dist/package/scripts/setup.py +195 -0
- package/dist/package/skills/bootstrap/SKILL.md +110 -0
- package/dist/package/skills/documentation/SKILL.md +110 -0
- package/dist/package/skills/documentation/references/agents-md.md +70 -0
- package/dist/package/skills/documentation/references/corpus.md +112 -0
- package/dist/package/skills/journal/SKILL.md +81 -0
- package/dist/package/skills/journal/references/entry.md +172 -0
- package/dist/package/skills/lathe/SKILL.md +168 -0
- package/dist/package/skills/lathe/evals/routing.md +196 -0
- package/dist/package/skills/lathe/references/install.md +128 -0
- package/dist/package/skills/lathe/references/invariants.md +31 -0
- package/dist/package/skills/mutants/SKILL.md +85 -0
- package/dist/package/skills/mutants/references/cargo-mutants.md +176 -0
- package/dist/package/skills/mutants/references/triage.md +88 -0
- package/dist/package/skills/optimize/SKILL.md +87 -0
- package/dist/package/skills/optimize/references/codspeed.md +126 -0
- package/dist/package/skills/probe/SKILL.md +82 -0
- package/dist/package/skills/probe/references/wait-and-read.md +53 -0
- package/dist/package/skills/replicate-ui/SKILL.md +64 -0
- package/dist/package/skills/rust/SKILL.md +147 -0
- package/dist/package/skills/rust/references/errors.md +63 -0
- package/dist/package/skills/rust/references/unsafe.md +65 -0
- package/dist/package/skills/scaffold/SKILL.md +78 -0
- package/dist/package/skills/scaffold/profiles/rust-cli/.cargo/config.toml +2 -0
- package/dist/package/skills/scaffold/profiles/rust-cli/AGENTS.md +22 -0
- package/dist/package/skills/scaffold/profiles/rust-cli/Cargo.lock +7 -0
- package/dist/package/skills/scaffold/profiles/rust-cli/Cargo.toml +17 -0
- package/dist/package/skills/scaffold/profiles/rust-cli/README.md +15 -0
- package/dist/package/skills/scaffold/profiles/rust-cli/deny.toml +14 -0
- package/dist/package/skills/scaffold/profiles/rust-cli/rustfmt.toml +2 -0
- package/dist/package/skills/scaffold/profiles/rust-cli/src/main.rs +38 -0
- package/dist/package/skills/scaffold/profiles/rust-cli/tests/cli.rs +27 -0
- package/dist/package/skills/scaffold/profiles/rust-library/.cargo/config.toml +2 -0
- package/dist/package/skills/scaffold/profiles/rust-library/AGENTS.md +21 -0
- package/dist/package/skills/scaffold/profiles/rust-library/Cargo.lock +7 -0
- package/dist/package/skills/scaffold/profiles/rust-library/Cargo.toml +18 -0
- package/dist/package/skills/scaffold/profiles/rust-library/README.md +15 -0
- package/dist/package/skills/scaffold/profiles/rust-library/deny.toml +14 -0
- package/dist/package/skills/scaffold/profiles/rust-library/rustfmt.toml +2 -0
- package/dist/package/skills/scaffold/profiles/rust-library/src/lib.rs +25 -0
- package/dist/package/skills/scaffold/profiles/rust-native-desktop/.cargo/config.toml +2 -0
- package/dist/package/skills/scaffold/profiles/rust-native-desktop/AGENTS.md +23 -0
- package/dist/package/skills/scaffold/profiles/rust-native-desktop/Cargo.lock +9252 -0
- package/dist/package/skills/scaffold/profiles/rust-native-desktop/Cargo.toml +24 -0
- package/dist/package/skills/scaffold/profiles/rust-native-desktop/README.md +15 -0
- package/dist/package/skills/scaffold/profiles/rust-native-desktop/deny.toml +35 -0
- package/dist/package/skills/scaffold/profiles/rust-native-desktop/rustfmt.toml +2 -0
- package/dist/package/skills/scaffold/profiles/rust-native-desktop/src/main.rs +74 -0
- package/dist/package/skills/scaffold/references/gates.md +29 -0
- package/dist/package/skills/scaffold/references/profiles.md +36 -0
- package/dist/package/skills/update/SKILL.md +115 -0
- package/dist/package/skills/verify-visual/SKILL.md +82 -0
- package/dist/package/skills/verify-visual/references/baseline.md +46 -0
- package/dist/package/skills/visual-loop/SKILL.md +72 -0
- package/dist/package/workflows/batch-to-review.md +41 -0
- package/dist/package/workflows/framework-improvement.md +38 -0
- package/dist/package/workflows/hard-bug.md +37 -0
- package/dist/package/workflows/new-rust-cli.md +33 -0
- package/dist/package/workflows/new-rust-desktop-app.md +41 -0
- package/dist/package/workflows/new-rust-library.md +34 -0
- package/dist/package/workflows/performance-work.md +41 -0
- package/dist/package/workflows/ui-change.md +40 -0
- package/package.json +33 -4
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
use std::{
|
|
2
|
+
fmt,
|
|
3
|
+
io::{self, BufRead, BufReader, Write},
|
|
4
|
+
path::{Path, PathBuf},
|
|
5
|
+
sync::{
|
|
6
|
+
Arc,
|
|
7
|
+
atomic::{AtomicBool, Ordering},
|
|
8
|
+
},
|
|
9
|
+
thread::{self, JoinHandle},
|
|
10
|
+
time::{Duration, Instant},
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
use interprocess::local_socket::{ListenerNonblockingMode, ListenerOptions, prelude::*};
|
|
14
|
+
|
|
15
|
+
use crate::{
|
|
16
|
+
protocol::{Request, Response},
|
|
17
|
+
registry::Instrument,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/// Chemin du socket Unix ou nom de named pipe, lu par l'exemple et par
|
|
21
|
+
/// l'application hôte.
|
|
22
|
+
pub const SOCKET_PATH_ENV: &str = "APP_SOCKET";
|
|
23
|
+
|
|
24
|
+
/// Un socket Unix tient dans environ 103 octets de chemin, en comptant le zéro
|
|
25
|
+
/// final. On garde une marge explicite à 100 octets et on refuse au-delà avec
|
|
26
|
+
/// un message qui nomme la limite, plutôt que de laisser l'OS tronquer le nom.
|
|
27
|
+
pub const MAX_SOCKET_PATH_BYTES: usize = 100;
|
|
28
|
+
|
|
29
|
+
const ACCEPT_POLL: Duration = Duration::from_millis(10);
|
|
30
|
+
/// Un client qui se connecte puis se tait est libéré : le serveur ne peut pas
|
|
31
|
+
/// être immobilisé par une connexion inactive.
|
|
32
|
+
const CONNECTION_IDLE_TIMEOUT: Duration = Duration::from_millis(1_000);
|
|
33
|
+
|
|
34
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
35
|
+
pub enum PathError {
|
|
36
|
+
Empty,
|
|
37
|
+
TooLong { bytes: usize, limit: usize },
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
impl fmt::Display for PathError {
|
|
41
|
+
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
42
|
+
match self {
|
|
43
|
+
Self::Empty => write!(formatter, "le chemin du socket est vide"),
|
|
44
|
+
Self::TooLong { bytes, limit } => write!(
|
|
45
|
+
formatter,
|
|
46
|
+
"chemin de socket trop long: {bytes} octets (limite {limit})"
|
|
47
|
+
),
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
impl std::error::Error for PathError {}
|
|
53
|
+
|
|
54
|
+
/// Vérifie les deux limites que l'OS ne signale pas clairement : un chemin vide
|
|
55
|
+
/// et un chemin au-delà de la taille d'un `sockaddr_un`.
|
|
56
|
+
pub fn validate_socket_path(path: &Path) -> Result<(), PathError> {
|
|
57
|
+
if path.as_os_str().is_empty() {
|
|
58
|
+
return Err(PathError::Empty);
|
|
59
|
+
}
|
|
60
|
+
let bytes = path.as_os_str().as_encoded_bytes().len();
|
|
61
|
+
if bytes > MAX_SOCKET_PATH_BYTES {
|
|
62
|
+
return Err(PathError::TooLong {
|
|
63
|
+
bytes,
|
|
64
|
+
limit: MAX_SOCKET_PATH_BYTES,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
Ok(())
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
#[cfg(unix)]
|
|
71
|
+
pub(crate) fn socket_name(path: &Path) -> io::Result<interprocess::local_socket::Name<'_>> {
|
|
72
|
+
path.to_fs_name::<interprocess::local_socket::GenericFilePath>()
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/// Sur Windows, un « chemin » de socket est un nom de named pipe : l'API
|
|
76
|
+
/// namespaced préfixe `\\.\pipe\` et n'utilise que le dernier composant. Le
|
|
77
|
+
/// client et le serveur appliquent la même règle, donc ils se retrouvent.
|
|
78
|
+
#[cfg(windows)]
|
|
79
|
+
pub(crate) fn socket_name(path: &Path) -> io::Result<interprocess::local_socket::Name<'_>> {
|
|
80
|
+
let name = path
|
|
81
|
+
.file_name()
|
|
82
|
+
.filter(|name| !name.is_empty())
|
|
83
|
+
.unwrap_or_else(|| path.as_os_str());
|
|
84
|
+
name.to_ns_name::<interprocess::local_socket::GenericNamespaced>()
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
#[derive(Debug)]
|
|
88
|
+
pub enum ServerError {
|
|
89
|
+
Path(PathError),
|
|
90
|
+
Name { path: PathBuf, source: io::Error },
|
|
91
|
+
Bind { path: PathBuf, source: io::Error },
|
|
92
|
+
Permissions { path: PathBuf, source: io::Error },
|
|
93
|
+
Spawn { source: io::Error },
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
impl fmt::Display for ServerError {
|
|
97
|
+
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
98
|
+
match self {
|
|
99
|
+
Self::Path(error) => error.fmt(formatter),
|
|
100
|
+
Self::Name { path, source } => write!(
|
|
101
|
+
formatter,
|
|
102
|
+
"nom de socket invalide pour « {} »: {source}",
|
|
103
|
+
path.display()
|
|
104
|
+
),
|
|
105
|
+
Self::Bind { path, source } => write!(
|
|
106
|
+
formatter,
|
|
107
|
+
"liaison du socket « {} » impossible: {source}",
|
|
108
|
+
path.display()
|
|
109
|
+
),
|
|
110
|
+
Self::Permissions { path, source } => write!(
|
|
111
|
+
formatter,
|
|
112
|
+
"chmod 0600 sur « {} » impossible: {source}",
|
|
113
|
+
path.display()
|
|
114
|
+
),
|
|
115
|
+
Self::Spawn { source } => {
|
|
116
|
+
write!(
|
|
117
|
+
formatter,
|
|
118
|
+
"démarrage du thread serveur impossible: {source}"
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
impl std::error::Error for ServerError {
|
|
126
|
+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
127
|
+
match self {
|
|
128
|
+
Self::Path(error) => Some(error),
|
|
129
|
+
Self::Name { source, .. }
|
|
130
|
+
| Self::Bind { source, .. }
|
|
131
|
+
| Self::Permissions { source, .. }
|
|
132
|
+
| Self::Spawn { source } => Some(source),
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/// Serveur possédé : le socket est lié avant le retour de `start`, et le thread
|
|
138
|
+
/// d'acceptation s'arrête avec `shutdown` ou avec le `Drop`.
|
|
139
|
+
pub struct Server {
|
|
140
|
+
path: PathBuf,
|
|
141
|
+
shutdown: Arc<AtomicBool>,
|
|
142
|
+
thread: Option<JoinHandle<()>>,
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
impl Server {
|
|
146
|
+
pub fn start(instrument: Instrument, path: impl Into<PathBuf>) -> Result<Self, ServerError> {
|
|
147
|
+
let path = path.into();
|
|
148
|
+
validate_socket_path(&path).map_err(ServerError::Path)?;
|
|
149
|
+
|
|
150
|
+
let name = socket_name(&path).map_err(|source| ServerError::Name {
|
|
151
|
+
path: path.clone(),
|
|
152
|
+
source,
|
|
153
|
+
})?;
|
|
154
|
+
let listener = ListenerOptions::new()
|
|
155
|
+
.name(name)
|
|
156
|
+
.nonblocking(ListenerNonblockingMode::Accept)
|
|
157
|
+
.create_sync()
|
|
158
|
+
.map_err(|source| ServerError::Bind {
|
|
159
|
+
path: path.clone(),
|
|
160
|
+
source,
|
|
161
|
+
})?;
|
|
162
|
+
|
|
163
|
+
#[cfg(unix)]
|
|
164
|
+
set_socket_permissions(&path)?;
|
|
165
|
+
|
|
166
|
+
let shutdown = Arc::new(AtomicBool::new(false));
|
|
167
|
+
let flag = Arc::clone(&shutdown);
|
|
168
|
+
let instrument = Arc::new(instrument);
|
|
169
|
+
let thread = thread::Builder::new()
|
|
170
|
+
.name("instrument-server".to_owned())
|
|
171
|
+
.spawn(move || accept_loop(&listener, &instrument, &flag))
|
|
172
|
+
.map_err(|source| ServerError::Spawn { source })?;
|
|
173
|
+
|
|
174
|
+
Ok(Self {
|
|
175
|
+
path,
|
|
176
|
+
shutdown,
|
|
177
|
+
thread: Some(thread),
|
|
178
|
+
})
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
pub fn path(&self) -> &Path {
|
|
182
|
+
&self.path
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/// Arrête l'acceptation de nouvelles connexions et attend la fin du thread.
|
|
186
|
+
/// La connexion en cours est libérée par son propre timeout.
|
|
187
|
+
pub fn shutdown(&mut self) {
|
|
188
|
+
self.shutdown.store(true, Ordering::Release);
|
|
189
|
+
if let Some(thread) = self.thread.take()
|
|
190
|
+
&& thread.join().is_err()
|
|
191
|
+
{
|
|
192
|
+
tracing::error!("thread serveur terminé sur un panic");
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/// Attend la fin du serveur. Utilisé par un binaire qui sert jusqu'à son
|
|
197
|
+
/// arrêt ; une interface graphique préfère `shutdown` depuis son thread.
|
|
198
|
+
pub fn wait(mut self) {
|
|
199
|
+
if let Some(thread) = self.thread.take()
|
|
200
|
+
&& thread.join().is_err()
|
|
201
|
+
{
|
|
202
|
+
tracing::error!("thread serveur terminé sur un panic");
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
impl Drop for Server {
|
|
208
|
+
fn drop(&mut self) {
|
|
209
|
+
self.shutdown();
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
#[cfg(unix)]
|
|
214
|
+
fn set_socket_permissions(path: &Path) -> Result<(), ServerError> {
|
|
215
|
+
use std::os::unix::fs::PermissionsExt;
|
|
216
|
+
|
|
217
|
+
std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600)).map_err(|source| {
|
|
218
|
+
ServerError::Permissions {
|
|
219
|
+
path: path.to_path_buf(),
|
|
220
|
+
source,
|
|
221
|
+
}
|
|
222
|
+
})
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Les connexions sont traitées l'une après l'autre : c'est un canal de
|
|
226
|
+
// diagnostic, pas un serveur généraliste, et l'inactivité d'un client est
|
|
227
|
+
// bornée par `CONNECTION_IDLE_TIMEOUT`.
|
|
228
|
+
fn accept_loop(listener: &LocalSocketListener, instrument: &Instrument, shutdown: &AtomicBool) {
|
|
229
|
+
while !shutdown.load(Ordering::Acquire) {
|
|
230
|
+
match listener.accept() {
|
|
231
|
+
Ok(stream) => {
|
|
232
|
+
if let Err(error) = serve_connection(stream, instrument) {
|
|
233
|
+
tracing::warn!(%error, "connexion interrompue");
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
Err(error) if error.kind() == io::ErrorKind::WouldBlock => thread::sleep(ACCEPT_POLL),
|
|
237
|
+
Err(error) => {
|
|
238
|
+
tracing::error!(%error, "accept impossible, serveur arrêté");
|
|
239
|
+
break;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
fn serve_connection(stream: LocalSocketStream, instrument: &Instrument) -> io::Result<()> {
|
|
246
|
+
// Le listener est en accept non bloquant. Sur macOS, le socket accepté
|
|
247
|
+
// hérite de O_NONBLOCK (contrairement à Linux): on le remet explicitement
|
|
248
|
+
// en bloquant pour que les timeouts de lecture soient honorés.
|
|
249
|
+
stream.set_nonblocking(false)?;
|
|
250
|
+
stream.set_recv_timeout(Some(CONNECTION_IDLE_TIMEOUT))?;
|
|
251
|
+
stream.set_send_timeout(Some(CONNECTION_IDLE_TIMEOUT))?;
|
|
252
|
+
let mut stream = BufReader::new(stream);
|
|
253
|
+
|
|
254
|
+
loop {
|
|
255
|
+
let mut line = String::new();
|
|
256
|
+
match stream.read_line(&mut line) {
|
|
257
|
+
Ok(0) => return Ok(()),
|
|
258
|
+
Ok(_) => {}
|
|
259
|
+
Err(error) if is_timeout(&error) => return Ok(()),
|
|
260
|
+
Err(error) => return Err(error),
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
let request = match serde_json::from_str::<Request>(&line) {
|
|
264
|
+
Ok(request) => request,
|
|
265
|
+
Err(error) => {
|
|
266
|
+
let response = Response::failure(format!("requête JSON invalide: {error}"));
|
|
267
|
+
write_response(&mut stream, &response)?;
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
let started = Instant::now();
|
|
273
|
+
let response = match instrument.invoke(&request.command, request.params) {
|
|
274
|
+
Ok(data) => Response::success(data),
|
|
275
|
+
Err(message) => Response::failure(message),
|
|
276
|
+
};
|
|
277
|
+
tracing::info!(
|
|
278
|
+
command = %request.command,
|
|
279
|
+
ok = response.ok,
|
|
280
|
+
elapsed_ms = started.elapsed().as_millis() as u64,
|
|
281
|
+
"commande traitée"
|
|
282
|
+
);
|
|
283
|
+
write_response(&mut stream, &response)?;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
fn write_response(
|
|
288
|
+
stream: &mut BufReader<LocalSocketStream>,
|
|
289
|
+
response: &Response,
|
|
290
|
+
) -> io::Result<()> {
|
|
291
|
+
let body = serde_json::to_string(response).map_err(io::Error::other)?;
|
|
292
|
+
let inner = stream.get_mut();
|
|
293
|
+
inner.write_all(body.as_bytes())?;
|
|
294
|
+
inner.write_all(b"\n")?;
|
|
295
|
+
inner.flush()
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
fn is_timeout(error: &io::Error) -> bool {
|
|
299
|
+
matches!(
|
|
300
|
+
error.kind(),
|
|
301
|
+
io::ErrorKind::WouldBlock | io::ErrorKind::TimedOut
|
|
302
|
+
)
|
|
303
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
mod support;
|
|
2
|
+
|
|
3
|
+
use std::{fs, time::Duration};
|
|
4
|
+
|
|
5
|
+
use instrument::{Client, Instrument, Server, logging};
|
|
6
|
+
use serde_json::{Value, json};
|
|
7
|
+
use support::TempDir;
|
|
8
|
+
use tracing_subscriber::filter::LevelFilter;
|
|
9
|
+
|
|
10
|
+
#[test]
|
|
11
|
+
fn chaque_commande_ecrit_une_ligne_json_dans_app_log() {
|
|
12
|
+
let temp = TempDir::new("in-log").expect("répertoire temporaire");
|
|
13
|
+
let socket = temp.socket();
|
|
14
|
+
let log_path = temp.path().join("app.log");
|
|
15
|
+
|
|
16
|
+
let guard = logging::init(&log_path, LevelFilter::INFO).expect("journalisation");
|
|
17
|
+
|
|
18
|
+
let mut instrument = Instrument::new();
|
|
19
|
+
instrument
|
|
20
|
+
.command("echo", |params| Ok(json!({ "echo": params })))
|
|
21
|
+
.expect("enregistrement de echo");
|
|
22
|
+
let mut server = Server::start(instrument, &socket).expect("démarrage du serveur");
|
|
23
|
+
|
|
24
|
+
let mut client = Client::connect(&socket, Duration::from_secs(1)).expect("connexion");
|
|
25
|
+
client.call("state", Value::Null).expect("état");
|
|
26
|
+
client
|
|
27
|
+
.call("action", json!({ "key": "a", "value": 1 }))
|
|
28
|
+
.expect("action");
|
|
29
|
+
client.call("echo", json!({ "x": 1 })).expect("echo");
|
|
30
|
+
|
|
31
|
+
drop(client);
|
|
32
|
+
server.shutdown();
|
|
33
|
+
// Déposer la garde vide la file d'écriture avant de lire le fichier.
|
|
34
|
+
drop(guard);
|
|
35
|
+
|
|
36
|
+
let content = fs::read_to_string(&log_path).expect("lecture du fichier de log");
|
|
37
|
+
let mut commands = Vec::new();
|
|
38
|
+
for line in content.lines() {
|
|
39
|
+
let value: Value = serde_json::from_str(line).expect("chaque ligne est un objet JSON");
|
|
40
|
+
let fields = value
|
|
41
|
+
.get("fields")
|
|
42
|
+
.expect("ligne JSON avec un objet 'fields'");
|
|
43
|
+
let command = fields
|
|
44
|
+
.get("command")
|
|
45
|
+
.and_then(Value::as_str)
|
|
46
|
+
.unwrap_or_else(|| panic!("ligne sans champ 'command': {line}"));
|
|
47
|
+
assert_eq!(
|
|
48
|
+
fields.get("ok").and_then(Value::as_bool),
|
|
49
|
+
Some(true),
|
|
50
|
+
"commande non marquée ok: {line}"
|
|
51
|
+
);
|
|
52
|
+
commands.push(command.to_owned());
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
assert_eq!(
|
|
56
|
+
commands,
|
|
57
|
+
["state", "action", "echo"],
|
|
58
|
+
"une ligne par commande, dans l'ordre: {content}"
|
|
59
|
+
);
|
|
60
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
mod support;
|
|
2
|
+
|
|
3
|
+
use std::{fs, path::PathBuf, time::Duration};
|
|
4
|
+
|
|
5
|
+
use instrument::{
|
|
6
|
+
Client, ClientError, Instrument, Server, ServerError,
|
|
7
|
+
server::{MAX_SOCKET_PATH_BYTES, PathError},
|
|
8
|
+
};
|
|
9
|
+
use serde_json::{Value, json};
|
|
10
|
+
use support::TempDir;
|
|
11
|
+
|
|
12
|
+
#[test]
|
|
13
|
+
fn aller_retour_de_commande_sur_le_socket() {
|
|
14
|
+
let temp = TempDir::new("in-round").expect("répertoire temporaire");
|
|
15
|
+
let socket = temp.socket();
|
|
16
|
+
|
|
17
|
+
let mut instrument = Instrument::new();
|
|
18
|
+
instrument
|
|
19
|
+
.command("echo", |params| Ok(json!({ "echo": params })))
|
|
20
|
+
.expect("enregistrement de echo");
|
|
21
|
+
instrument
|
|
22
|
+
.command("boom", |_| Err("échec volontaire".to_owned()))
|
|
23
|
+
.expect("enregistrement de boom");
|
|
24
|
+
|
|
25
|
+
let mut server = Server::start(instrument, &socket).expect("démarrage du serveur");
|
|
26
|
+
assert_eq!(server.path(), socket.as_path());
|
|
27
|
+
|
|
28
|
+
let mut client = Client::connect(&socket, Duration::from_secs(1)).expect("connexion");
|
|
29
|
+
|
|
30
|
+
let listed = client
|
|
31
|
+
.call("list", Value::Null)
|
|
32
|
+
.expect("liste des commandes");
|
|
33
|
+
let names = listed["commands"]
|
|
34
|
+
.as_array()
|
|
35
|
+
.expect("tableau 'commands'")
|
|
36
|
+
.iter()
|
|
37
|
+
.filter_map(Value::as_str)
|
|
38
|
+
.collect::<Vec<_>>();
|
|
39
|
+
for expected in ["list", "state", "action", "wait", "echo", "boom"] {
|
|
40
|
+
assert!(names.contains(&expected), "commande absente: {expected}");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let initial = client.call("state", Value::Null).expect("état initial");
|
|
44
|
+
assert_eq!(initial["revision"], json!(0));
|
|
45
|
+
assert_eq!(initial["values"], json!({}));
|
|
46
|
+
|
|
47
|
+
let action = client
|
|
48
|
+
.call("action", json!({ "key": "compteur", "value": 3 }))
|
|
49
|
+
.expect("action");
|
|
50
|
+
assert_eq!(action["revision"], json!(1));
|
|
51
|
+
|
|
52
|
+
let after = client
|
|
53
|
+
.call("state", Value::Null)
|
|
54
|
+
.expect("état après action");
|
|
55
|
+
assert_eq!(after["revision"], json!(1));
|
|
56
|
+
assert_eq!(after["values"]["compteur"], json!(3));
|
|
57
|
+
|
|
58
|
+
let echoed = client
|
|
59
|
+
.call("echo", json!({ "x": [1, 2] }))
|
|
60
|
+
.expect("commande personnalisée");
|
|
61
|
+
assert_eq!(echoed, json!({ "echo": { "x": [1, 2] } }));
|
|
62
|
+
|
|
63
|
+
match client.call("boom", Value::Null) {
|
|
64
|
+
Err(ClientError::Remote(message)) => assert!(message.contains("échec volontaire")),
|
|
65
|
+
other => panic!("erreur distante attendue, reçu {other:?}"),
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
match client.call("inconnue", Value::Null) {
|
|
69
|
+
Err(ClientError::Remote(message)) => assert!(message.contains("inconnue")),
|
|
70
|
+
other => panic!("erreur de commande inconnue attendue, reçu {other:?}"),
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
#[cfg(unix)]
|
|
74
|
+
{
|
|
75
|
+
use std::os::unix::fs::PermissionsExt;
|
|
76
|
+
|
|
77
|
+
let mode = fs::metadata(&socket)
|
|
78
|
+
.expect("métadonnées du socket")
|
|
79
|
+
.permissions()
|
|
80
|
+
.mode()
|
|
81
|
+
& 0o777;
|
|
82
|
+
assert_eq!(mode, 0o600, "le socket doit être privé");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
drop(client);
|
|
86
|
+
server.shutdown();
|
|
87
|
+
assert!(!socket.exists(), "le socket doit disparaître à l'arrêt");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
#[test]
|
|
91
|
+
fn un_client_impatient_ne_bloque_pas_indefiniment() {
|
|
92
|
+
let temp = TempDir::new("in-timeout").expect("répertoire temporaire");
|
|
93
|
+
let socket = temp.socket();
|
|
94
|
+
let mut server = Server::start(Instrument::new(), &socket).expect("démarrage du serveur");
|
|
95
|
+
|
|
96
|
+
let mut impatient =
|
|
97
|
+
Client::connect(&socket, Duration::from_millis(100)).expect("connexion impatiente");
|
|
98
|
+
let started = std::time::Instant::now();
|
|
99
|
+
match impatient.call("wait", json!({ "milliseconds": 400 })) {
|
|
100
|
+
Err(ClientError::Timeout) => {}
|
|
101
|
+
other => panic!("timeout attendu, reçu {other:?}"),
|
|
102
|
+
}
|
|
103
|
+
assert!(
|
|
104
|
+
started.elapsed() < Duration::from_secs(1),
|
|
105
|
+
"le client a attendu trop longtemps"
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
drop(impatient);
|
|
109
|
+
server.shutdown();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
#[test]
|
|
113
|
+
fn un_chemin_de_socket_trop_long_est_refuse_explicitement() {
|
|
114
|
+
let mut too_long = PathBuf::new();
|
|
115
|
+
too_long.push("a".repeat(MAX_SOCKET_PATH_BYTES + 20));
|
|
116
|
+
too_long.push("s");
|
|
117
|
+
let bytes = too_long.as_os_str().as_encoded_bytes().len();
|
|
118
|
+
|
|
119
|
+
match Server::start(Instrument::new(), &too_long) {
|
|
120
|
+
Err(ServerError::Path(PathError::TooLong { bytes: seen, limit })) => {
|
|
121
|
+
assert_eq!(seen, bytes);
|
|
122
|
+
assert_eq!(limit, MAX_SOCKET_PATH_BYTES);
|
|
123
|
+
}
|
|
124
|
+
Err(error) => panic!("refus de chemin trop long attendu, reçu {error}"),
|
|
125
|
+
Ok(_) => panic!("un serveur a démarré sur un chemin trop long"),
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
let message = ServerError::Path(PathError::TooLong {
|
|
129
|
+
bytes,
|
|
130
|
+
limit: MAX_SOCKET_PATH_BYTES,
|
|
131
|
+
})
|
|
132
|
+
.to_string();
|
|
133
|
+
assert!(message.contains("limite 100"), "message: {message}");
|
|
134
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
use instrument::selftest::{self, SelftestOptions};
|
|
2
|
+
|
|
3
|
+
#[test]
|
|
4
|
+
fn un_selftest_qui_echoue_rend_fail_et_un_rapport_faux() {
|
|
5
|
+
let options = SelftestOptions {
|
|
6
|
+
break_assertion: Some("state-initial".to_owned()),
|
|
7
|
+
};
|
|
8
|
+
let outcomes = selftest::run(&options);
|
|
9
|
+
let mut output = Vec::new();
|
|
10
|
+
let passed = selftest::report(&outcomes, &mut output);
|
|
11
|
+
let text = String::from_utf8(output).expect("sortie UTF-8");
|
|
12
|
+
|
|
13
|
+
assert!(
|
|
14
|
+
!passed,
|
|
15
|
+
"un échec injecté doit faire échouer le rapport:\n{text}"
|
|
16
|
+
);
|
|
17
|
+
assert!(
|
|
18
|
+
text.contains("FAIL state-initial — échec injecté"),
|
|
19
|
+
"ligne FAIL nommée absente:\n{text}"
|
|
20
|
+
);
|
|
21
|
+
assert!(
|
|
22
|
+
text.contains("PASS "),
|
|
23
|
+
"les autres assertions doivent tourner:\n{text}"
|
|
24
|
+
);
|
|
25
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
use instrument::selftest::{self, SelftestOptions};
|
|
2
|
+
|
|
3
|
+
#[test]
|
|
4
|
+
fn le_selftest_nominal_passe_et_rapporte_chaque_assertion() {
|
|
5
|
+
let outcomes = selftest::run(&SelftestOptions::default());
|
|
6
|
+
let mut output = Vec::new();
|
|
7
|
+
let passed = selftest::report(&outcomes, &mut output);
|
|
8
|
+
let text = String::from_utf8(output).expect("sortie UTF-8");
|
|
9
|
+
|
|
10
|
+
assert!(passed, "le selftest doit passer:\n{text}");
|
|
11
|
+
assert!(!outcomes.is_empty(), "aucune assertion exécutée");
|
|
12
|
+
assert!(
|
|
13
|
+
outcomes.iter().all(|outcome| outcome.result.is_ok()),
|
|
14
|
+
"assertion en échec:\n{text}"
|
|
15
|
+
);
|
|
16
|
+
for outcome in &outcomes {
|
|
17
|
+
assert!(
|
|
18
|
+
text.contains(&format!("PASS {}", outcome.name)),
|
|
19
|
+
"ligne PASS absente pour {}:\n{text}",
|
|
20
|
+
outcome.name
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
assert!(!text.contains("FAIL "), "sortie inattendue:\n{text}");
|
|
24
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#![allow(dead_code)] // chaque binaire de test n'utilise qu'une partie du module
|
|
2
|
+
|
|
3
|
+
use std::{
|
|
4
|
+
fs, io,
|
|
5
|
+
path::{Path, PathBuf},
|
|
6
|
+
sync::atomic::{AtomicU64, Ordering},
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
static COUNTER: AtomicU64 = AtomicU64::new(0);
|
|
10
|
+
|
|
11
|
+
/// Répertoire temporaire propre à ce test, supprimé au Drop. Les noms restent
|
|
12
|
+
/// courts: un chemin de socket Unix est limité à une centaine d'octets.
|
|
13
|
+
pub struct TempDir {
|
|
14
|
+
path: PathBuf,
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
impl TempDir {
|
|
18
|
+
pub fn new(prefix: &str) -> io::Result<Self> {
|
|
19
|
+
let counter = COUNTER.fetch_add(1, Ordering::Relaxed);
|
|
20
|
+
let path = std::env::temp_dir().join(format!("{prefix}-{}-{counter}", std::process::id()));
|
|
21
|
+
fs::create_dir(&path)?;
|
|
22
|
+
Ok(Self { path })
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
pub fn path(&self) -> &Path {
|
|
26
|
+
&self.path
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
pub fn socket(&self) -> PathBuf {
|
|
30
|
+
self.path.join("s")
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
impl Drop for TempDir {
|
|
35
|
+
fn drop(&mut self) {
|
|
36
|
+
let _ = fs::remove_dir_all(&self.path);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: lathe-journal-de-lot
|
|
3
|
+
description: Inject the journal rule when someone asks for the lot's entry
|
|
4
|
+
events: [before_agent_start]
|
|
5
|
+
match:
|
|
6
|
+
input:
|
|
7
|
+
contains:
|
|
8
|
+
- "close the lot"
|
|
9
|
+
- "close out the lot"
|
|
10
|
+
- "the lot's journal"
|
|
11
|
+
- "write the journal"
|
|
12
|
+
- "fin du lot"
|
|
13
|
+
- "clôture du lot"
|
|
14
|
+
- "cloture du lot"
|
|
15
|
+
- "journal du lot"
|
|
16
|
+
- "bilan du lot"
|
|
17
|
+
action: {type: inject}
|
|
18
|
+
priority: high
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## The lot's entry was asked for
|
|
22
|
+
|
|
23
|
+
Close it with the `journal` skill: that skill holds the form, the author and the one file a closed lot
|
|
24
|
+
leaves uncommitted. Read it rather than reconstructing it from memory.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: lathe-tech-skill
|
|
3
|
+
description: Explain that an official skill comes before the first write in its technology
|
|
4
|
+
events: [tool_call]
|
|
5
|
+
match:
|
|
6
|
+
tool: [edit, write]
|
|
7
|
+
input:
|
|
8
|
+
regex: ["\"path\"\\s*:\\s*\"[^\"]*Cargo\\.toml\""]
|
|
9
|
+
action: {type: inject, once: true}
|
|
10
|
+
priority: normal
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## The manifest is the moment
|
|
14
|
+
|
|
15
|
+
- The manifest names a technology that ships an official skill. Read that skill before the write that
|
|
16
|
+
touches the technology, the same way `rust` is read before a signature is shaped.
|
|
17
|
+
- Which technology owns which skill is written nowhere here: `gates.py deps` reads the pair, so the
|
|
18
|
+
command is what names the skill. The rule it applies is the `gates.py deps` clause of
|
|
19
|
+
`docs/execution/BACKLOG-WAVE.md`. When it comes back red, install the skill it names, then write.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: lathe-ticket-closed
|
|
3
|
+
description: Speak when a box in a ticket is ticked, at the moment a ticket closes
|
|
4
|
+
events: [tool_result]
|
|
5
|
+
match:
|
|
6
|
+
tool: [edit, write]
|
|
7
|
+
input:
|
|
8
|
+
regex: ["(?=[\\s\\S]*(?:issues?/|tickets?/|todos?/|\\.scratch/))(?=[\\s\\S]*- \\[x\\])"]
|
|
9
|
+
action: {type: inject}
|
|
10
|
+
priority: high
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## A ticket box was just ticked
|
|
14
|
+
|
|
15
|
+
- A tick is a claim. Run the command that proves it, or untick it — the journal records what a command
|
|
16
|
+
showed, not what the session hoped.
|
|
17
|
+
- The journal is a lot, not a ticket. Read `journal` when the lot's last ticket closes; one entry per
|
|
18
|
+
ticket is the form the rule refuses.
|