agentwerk 0.1.11

A minimal Rust crate that gives any application agentic capabilities.
Documentation
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Ticket tools — give an agent a call surface for reading and mutating
//! the surrounding `TicketSystem`. Two multi-action tools share one
//! dispatch helper: `ReadTicketsTool` (read-only) and `ManageTicketsTool`
//! (read + write). `WriteResultTool` is the sole way for an agent to
//! finish its current ticket.

use std::fs::OpenOptions;
use std::io::Write as _;

use serde_json::Value;

use crate::agents::tickets::{Status, Ticket, TicketError, TicketSystem};
use crate::schemas::{format_violations, Schema};

use super::tool::{ToolContext, ToolResult};

mod manage_tickets;
mod read_tickets;
mod write_result;

pub use manage_tickets::ManageTicketsTool;
pub use read_tickets::ReadTicketsTool;
pub use write_result::WriteResultTool;

/// Action sets each multi-action tool exposes. Keeps the dispatch logic
/// in one place and lets each tool reject actions outside its
/// allow-list with a uniform error message.
pub(super) const READ_ACTIONS: &[&str] = &["get", "list", "search"];
pub(super) const WRITE_ACTIONS: &[&str] = &["create", "edit"];

pub(super) fn dispatch(input: Value, ctx: &ToolContext, allowed: &[&str]) -> ToolResult {
    let action = match input["action"].as_str() {
        Some(a) => a,
        None => return ToolResult::error("Missing required parameter: action"),
    };
    if !allowed.contains(&action) {
        return ToolResult::error(format!(
            "Action `{action}` is not supported by this tool. Allowed: {}",
            allowed.join(", ")
        ));
    }
    let Some(ticket_system) = ctx.ticket_system_handle().cloned() else {
        return ToolResult::error("Ticket system unavailable in this context");
    };

    match action {
        "get" => action_get(&ticket_system, &input, ctx),
        "list" => action_list(&ticket_system, &input),
        "search" => action_search(&ticket_system, &input),
        "create" => action_create(&ticket_system, &input, ctx),
        "edit" => action_edit(&ticket_system, &input, ctx),
        other => ToolResult::error(format!("Unknown action `{other}`")),
    }
}

fn resolve_key(
    ticket_system: &TicketSystem,
    input: &Value,
    ctx: &ToolContext,
) -> Result<String, ToolResult> {
    if let Some(k) = input["key"].as_str() {
        return Ok(k.to_string());
    }
    resolve_current_key(ticket_system, ctx)
}

pub(super) fn resolve_current_key(
    ticket_system: &TicketSystem,
    ctx: &ToolContext,
) -> Result<String, ToolResult> {
    let agent_name = ctx.agent_name_str().ok_or_else(|| {
        ToolResult::error("Missing `key` and no agent_name set on this tool context")
    })?;
    match ticket_system.find(|t| t.status == Status::InProgress && t.has_label(agent_name)) {
        Some(t) => Ok(t.key().to_string()),
        None => Err(ToolResult::error(
            "Missing `key` and no current ticket assigned to this agent",
        )),
    }
}

pub(super) fn ticket_error_message(err: TicketError) -> String {
    err.to_string()
}

fn render_ticket(t: &Ticket) -> String {
    let mut out = String::new();
    out.push_str(&format!("# {}\n", t.key()));
    out.push_str(&format!("- status: {}\n", status_label(t.status)));
    out.push_str(&format!("- reporter: {}\n", t.reporter()));
    let labels_label = if t.labels.is_empty() {
        "(none)".to_string()
    } else {
        t.labels.join(", ")
    };
    out.push_str(&format!("- labels: {labels_label}\n"));
    out.push('\n');
    match &t.task {
        serde_json::Value::String(s) => {
            out.push_str(s);
            out.push('\n');
        }
        other => {
            out.push_str("```json\n");
            out.push_str(&serde_json::to_string_pretty(other).unwrap_or_default());
            out.push_str("\n```\n");
        }
    }
    out.push_str("\n## Result\n");
    match t.result_string() {
        Some(s) => out.push_str(&s),
        None => out.push_str("(no result)"),
    }
    out.push('\n');
    out
}

fn status_label(s: Status) -> &'static str {
    match s {
        Status::Todo => "Todo",
        Status::InProgress => "InProgress",
        Status::Done => "Done",
        Status::Failed => "Failed",
    }
}

fn parse_status_for_list(s: &str) -> Result<Status, ToolResult> {
    match s {
        "Todo" => Ok(Status::Todo),
        "InProgress" => Ok(Status::InProgress),
        "Done" => Ok(Status::Done),
        "Failed" => Ok(Status::Failed),
        other => Err(ToolResult::error(format!(
            "Invalid status `{other}`. Expected one of Todo, InProgress, Done, Failed"
        ))),
    }
}

fn truncate_for_preview(s: &str, max: usize) -> String {
    let one_line = s.lines().next().unwrap_or("");
    if one_line.chars().count() <= max {
        one_line.to_string()
    } else {
        let cut: String = one_line.chars().take(max).collect();
        format!("{cut}")
    }
}

type SummaryRow<'a> = (&'a str, &'a str, Status, &'a [String]);

fn render_summary_list(tickets: &[SummaryRow<'_>]) -> String {
    let mut out = String::new();
    for (key, task_preview, status, labels) in tickets {
        let labels_label = if labels.is_empty() {
            String::new()
        } else {
            format!("[{}] ", labels.join(","))
        };
        out.push_str(&format!(
            "- {key} [{status}] {labels_label}{task_preview}\n",
            status = status_label(*status),
        ));
    }
    out
}

fn task_preview(task: &serde_json::Value) -> String {
    let raw = match task {
        serde_json::Value::String(s) => s.clone(),
        other => other.to_string(),
    };
    truncate_for_preview(&raw, 80)
}

fn action_get(ticket_system: &TicketSystem, input: &Value, ctx: &ToolContext) -> ToolResult {
    let key = match resolve_key(ticket_system, input, ctx) {
        Ok(k) => k,
        Err(e) => return e,
    };
    match ticket_system.get(&key) {
        Some(t) => ToolResult::success(render_ticket(&t)),
        None => ToolResult::error(format!("Ticket {key} not found")),
    }
}

fn action_list(ticket_system: &TicketSystem, input: &Value) -> ToolResult {
    let label = input["label"].as_str().map(String::from);
    let status = input["status"].as_str().map(parse_status_for_list);
    let status = match status {
        Some(Ok(s)) => Some(s),
        Some(Err(e)) => return e,
        None => None,
    };

    let pool: Vec<Ticket> = ticket_system.filter(|t| {
        let status_ok = match status {
            Some(s) => t.status == s,
            None => true,
        };
        let label_ok = match label.as_deref() {
            Some(l) => t.has_label(l),
            None => true,
        };
        status_ok && label_ok
    });

    if pool.is_empty() {
        return ToolResult::success("(no matching tickets)".to_string());
    }
    let previews: Vec<String> = pool
        .iter()
        .take(50)
        .map(|t| task_preview(&t.task))
        .collect();
    let rows: Vec<SummaryRow<'_>> = pool
        .iter()
        .take(50)
        .zip(previews.iter())
        .map(|(t, p)| (t.key(), p.as_str(), t.status, t.labels.as_slice()))
        .collect();
    ToolResult::success(render_summary_list(&rows))
}

fn action_search(ticket_system: &TicketSystem, input: &Value) -> ToolResult {
    let query = match input["query"].as_str() {
        Some(q) => q,
        None => return ToolResult::error("Missing required parameter: query"),
    };
    let hits = ticket_system.search(query);
    if hits.is_empty() {
        return ToolResult::success("(no matching tickets)".to_string());
    }
    let previews: Vec<String> = hits
        .iter()
        .take(50)
        .map(|t| task_preview(&t.task))
        .collect();
    let rows: Vec<SummaryRow<'_>> = hits
        .iter()
        .take(50)
        .zip(previews.iter())
        .map(|(t, p)| (t.key(), p.as_str(), t.status, t.labels.as_slice()))
        .collect();
    ToolResult::success(render_summary_list(&rows))
}

fn action_create(ticket_system: &TicketSystem, input: &Value, ctx: &ToolContext) -> ToolResult {
    let task = match input.get("task") {
        Some(v) => v.clone(),
        None => return ToolResult::error("Missing required parameter: task"),
    };

    let labels: Vec<String> = match input.get("labels") {
        Some(Value::Array(arr)) => arr
            .iter()
            .filter_map(|v| v.as_str().map(|s| s.to_string()))
            .collect(),
        Some(Value::Null) | None => Vec::new(),
        Some(_) => return ToolResult::error("`labels` must be an array of strings"),
    };

    let schema = match input.get("schema") {
        Some(doc) if !doc.is_null() => match Schema::parse(doc.clone()) {
            Ok(s) => Some(s),
            Err(e) => {
                return ToolResult::error(format!(
                    "Cannot create: supplied `schema` is invalid: {e}"
                ));
            }
        },
        _ => None,
    };

    let mut ticket = Ticket::new(task).labels(labels);
    if let Some(schema) = schema {
        ticket = ticket.schema(schema);
    }

    let reporter = ctx
        .agent_name_str()
        .expect("agent_name on ToolContext")
        .to_string();
    let key = ticket_system.insert(ticket, reporter);
    ToolResult::success(format!("Created ticket {key}"))
}

fn action_edit(ticket_system: &TicketSystem, input: &Value, ctx: &ToolContext) -> ToolResult {
    let key = match resolve_key(ticket_system, input, ctx) {
        Ok(k) => k,
        Err(e) => return e,
    };

    let new_task = input.get("task").cloned();
    let new_labels: Option<Vec<String>> = match input.get("labels") {
        Some(Value::Array(arr)) => Some(
            arr.iter()
                .filter_map(|v| v.as_str().map(|s| s.to_string()))
                .collect(),
        ),
        Some(Value::Null) | None => None,
        Some(_) => return ToolResult::error("`labels` must be an array of strings"),
    };
    let new_schema: Option<Option<Schema>> = match input.get("schema") {
        Some(Value::Null) => Some(None),
        Some(doc) => match Schema::parse(doc.clone()) {
            Ok(s) => Some(Some(s)),
            Err(e) => {
                return ToolResult::error(format!(
                    "Cannot edit {key}: supplied `schema` is invalid: {e}"
                ));
            }
        },
        None => None,
    };

    if new_task.is_none() && new_labels.is_none() && new_schema.is_none() {
        return ToolResult::error("Edit needs at least one of `task`, `labels`, or `schema`");
    }

    match ticket_system.edit(&key, new_task, new_labels, new_schema) {
        Ok(()) => ToolResult::success(format!("Edited ticket {key}")),
        Err(e) => ToolResult::error(ticket_error_message(e)),
    }
}

/// Validate `result` against the ticket's schema (or against the
/// "non-empty string" rule when there is no schema), append an NDJSON
/// `{agent, ticket, result}` line to the configured results directory,
/// attach the payload to the ticket, and transition the ticket to
/// `Done`. The `agent` field is taken from the calling context; the
/// `ticket` field is the resolved key. Shared by `WriteResultTool` and
/// the loop's terminal-reply path.
pub(super) fn write_result(
    ticket_system: &TicketSystem,
    ctx: &ToolContext,
    key: &str,
    result: Value,
) -> ToolResult {
    let agent = match ctx.agent_name_str() {
        Some(a) => a.to_string(),
        None => {
            return ToolResult::error("No agent_name set on this tool context");
        }
    };

    let schema = ticket_system.get(key).and_then(|t| t.schema.clone());
    if let Some(schema) = schema.as_ref() {
        if let Err(violations) = schema.validate(&result) {
            return ToolResult::schema_error(format_violations(&violations));
        }
    } else {
        match &result {
            Value::Null => return ToolResult::error("`result` must not be null"),
            Value::String(s) if s.is_empty() => {
                return ToolResult::error("`result` must not be an empty string");
            }
            Value::Object(m) if m.is_empty() => {
                return ToolResult::error("`result` must not be an empty object");
            }
            Value::Array(a) if a.is_empty() => {
                return ToolResult::error("`result` must not be an empty array");
            }
            _ => {}
        }
    }

    let log_line = serde_json::json!({
        "agent": agent,
        "ticket": key,
        "result": result,
    });

    let target_dir = ticket_system.dir_value().unwrap_or_else(|| ctx.dir.clone());
    {
        let _guard = write_result::results_write_lock().lock().unwrap();
        if let Err(e) = append_ndjson(&target_dir, &log_line) {
            return ToolResult::error(format!(
                "Cannot write result to {}: {e}",
                target_dir.display()
            ));
        }
    }

    if let Err(e) = ticket_system.set_result(key, result) {
        return ToolResult::error(ticket_error_message(e));
    }
    match ticket_system.set_done(key) {
        Ok(()) => ToolResult::success(format!("Ticket {key} marked done")),
        Err(e) => ToolResult::error(ticket_error_message(e)),
    }
}

const RESULTS_FILE: &str = "results.jsonl";

fn append_ndjson(dir: &std::path::Path, line: &Value) -> std::io::Result<()> {
    std::fs::create_dir_all(dir)?;
    let mut text = serde_json::to_string(line).map_err(std::io::Error::other)?;
    text.push('\n');
    let mut file = OpenOptions::new()
        .create(true)
        .append(true)
        .open(dir.join(RESULTS_FILE))?;
    file.write_all(text.as_bytes())
}

#[cfg(test)]
mod tests {
    use super::super::tool::ToolLike;
    use super::*;
    use crate::agents::tickets::TicketSystem;
    use std::path::PathBuf;
    use std::sync::Arc;

    /// Build a context for a tool test, optionally with a "current
    /// ticket" already InProgress and assigned to `agent`.
    fn ctx_with(ticket_system: Arc<TicketSystem>, agent: &str) -> ToolContext {
        ToolContext::new(PathBuf::from("/tmp"))
            .ticket_system(ticket_system)
            .agent_name(agent.to_string())
    }

    /// Insert one Todo ticket, claim it for `agent` (atomically labels +
    /// transitions to InProgress), so `sys.find(...)` resolves it as the
    /// current ticket for `agent`.
    fn shared_with_one_ticket(agent: &str) -> (Arc<TicketSystem>, String) {
        let sys = TicketSystem::new();
        sys.insert(Ticket::new("body").label(agent), "tester".into());
        let key = sys
            .claim(|t| t.status == Status::Todo, agent)
            .expect("claim must succeed");
        (sys, key)
    }

    async fn call(tool: &dyn ToolLike, input: serde_json::Value, ctx: &ToolContext) -> ToolResult {
        tool.call(input, ctx).await.unwrap()
    }

    fn unwrap_text(result: &ToolResult) -> &str {
        let (ToolResult::Success(s) | ToolResult::Error(s) | ToolResult::SchemaError(s)) = result;
        s
    }

    #[tokio::test]
    async fn read_get_defaults_key_to_current_ticket() {
        let (sys, key) = shared_with_one_ticket("alice");
        let ctx = ctx_with(Arc::clone(&sys), "alice");
        let result = call(&ReadTicketsTool, serde_json::json!({"action": "get"}), &ctx).await;
        let text = unwrap_text(&result);
        assert!(text.contains(&key), "expected key in output: {text}");
        assert!(text.contains("body"));
    }

    #[tokio::test]
    async fn read_list_filters_by_status() {
        let sys = TicketSystem::new();
        sys.insert(Ticket::new("a"), "tester".into());
        sys.insert(Ticket::new("b"), "tester".into());
        sys.claim(|t| t.key() == "TICKET-1", "alice");

        let ctx = ctx_with(Arc::clone(&sys), "alice");
        let result = call(
            &ReadTicketsTool,
            serde_json::json!({"action": "list", "status": "InProgress"}),
            &ctx,
        )
        .await;
        let text = unwrap_text(&result);
        assert!(text.contains("TICKET-1"));
        assert!(!text.contains("TICKET-2"));
    }

    #[tokio::test]
    async fn manage_create_stamps_reporter_from_agent_name() {
        let sys = TicketSystem::new();
        let ctx = ctx_with(Arc::clone(&sys), "alice");
        let result = call(
            &ManageTicketsTool,
            serde_json::json!({"action": "create", "task": "new ticket"}),
            &ctx,
        )
        .await;
        assert!(matches!(result, ToolResult::Success(_)));
        let t = sys.get("TICKET-1").unwrap();
        assert_eq!(t.task, serde_json::Value::String("new ticket".into()));
        assert_eq!(t.reporter(), "alice");
    }

    #[tokio::test]
    async fn manage_create_with_labels_attaches_them() {
        let sys = TicketSystem::new();
        let ctx = ctx_with(Arc::clone(&sys), "alice");
        let result = call(
            &ManageTicketsTool,
            serde_json::json!({
                "action": "create",
                "task": "new",
                "labels": ["research"]
            }),
            &ctx,
        )
        .await;
        assert!(matches!(result, ToolResult::Success(_)));
        let t = sys.get("TICKET-1").unwrap();
        assert_eq!(t.labels, vec!["research".to_string()]);
        assert_eq!(t.status, Status::Todo);
    }

    #[tokio::test]
    async fn manage_create_with_named_label_routes_to_agent() {
        let sys = TicketSystem::new();
        let ctx = ctx_with(Arc::clone(&sys), "alice");
        let result = call(
            &ManageTicketsTool,
            serde_json::json!({
                "action": "create",
                "task": "new",
                "labels": ["alice"]
            }),
            &ctx,
        )
        .await;
        assert!(matches!(result, ToolResult::Success(_)));
        let t = sys.get("TICKET-1").unwrap();
        assert!(t.has_label("alice"));
        assert_eq!(t.status, Status::Todo);
    }

    #[tokio::test]
    async fn manage_create_with_schema_field_stores_schema() {
        let sys = TicketSystem::new();
        let ctx = ctx_with(Arc::clone(&sys), "alice");
        let result = call(
            &ManageTicketsTool,
            serde_json::json!({
                "action": "create",
                "task": "new",
                "schema": {"type": "string"}
            }),
            &ctx,
        )
        .await;
        assert!(matches!(result, ToolResult::Success(_)));
        assert!(sys.get("TICKET-1").unwrap().schema.is_some());
    }

    #[tokio::test]
    async fn manage_edit_updates_task_and_labels() {
        let (sys, key) = shared_with_one_ticket("alice");
        let ctx = ctx_with(Arc::clone(&sys), "alice");
        let result = call(
            &ManageTicketsTool,
            serde_json::json!({
                "action": "edit",
                "task": "new body",
                "labels": ["urgent", "review"]
            }),
            &ctx,
        )
        .await;
        assert!(matches!(result, ToolResult::Success(_)));
        let t = sys.get(&key).unwrap();
        assert_eq!(t.task, serde_json::Value::String("new body".into()));
        assert_eq!(t.labels, vec!["urgent".to_string(), "review".to_string()]);
    }

    #[tokio::test]
    async fn manage_rejects_unsupported_actions() {
        let (sys, _key) = shared_with_one_ticket("alice");
        let ctx = ctx_with(Arc::clone(&sys), "alice");
        for action in ["done", "transition", "comment", "assign", "attach"] {
            let result = call(
                &ManageTicketsTool,
                serde_json::json!({"action": action}),
                &ctx,
            )
            .await;
            assert!(
                matches!(result, ToolResult::Error(_)),
                "{action}: {result:?}"
            );
        }
    }
}