heartbit-core 2026.613.1

The Rust agentic framework — agents, tools, LLM providers, memory, evaluation.
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
//! The `intake` workflow recipe: the completion-loop FRONT HALF. Turn a raw
//! feature request into observable ACCEPTANCE CRITERIA (stage 1) plus the
//! load-bearing GAPS the request leaves unspecified (stage 2), classified into
//! safe-to-assume vs ask-the-user. Output is a markdown block the entry agent
//! consumes in-context — the recipe never asks the user itself (flat hierarchy:
//! `OnQuestion` belongs to the entry agent, which owns the user channel).
//!
//! Two ordered LLM leaves, run sequentially (this is the conceptual "pipeline"
//! shape — NOT the [`pipeline`](super::flow::pipeline) combinator, which fans N
//! items through stages and would silently drop a stage failure; here a single
//! request flows through two stages exactly like `deep_research`'s talk-only
//! stages). The gap leaf's reply is parsed defensively: a malformed critic JSON
//! degrades to zero questions plus the raw text as one assumption, and NEVER
//! fails the recipe.

use std::sync::Arc;

use serde_json::json;

use super::flow::agent;
use super::workflow_tool::WorkflowRecipe;
use crate::error::Error;
use crate::tool::builtins::Question;

/// System prompt for stage 1 (criteria extraction). Phrased so the model emits
/// observable, testable behavior — never implementation detail.
const CRITERIA_PROMPT: &str = "\
You are a requirements analyst. From the user's feature request below, produce \
3 to 8 ACCEPTANCE CRITERIA as markdown bullets. Each criterion MUST be phrased \
as OBSERVABLE BEHAVIOR — something a human or a command can verify (\"a human or \
a command can verify X\"), concrete and testable. Do NOT mention implementation \
details (no file names, function names, libraries, or internal design). Output \
ONLY the bulleted criteria, one per line, nothing else.\n\nFeature request:\n";

/// System prompt for stage 2 (gap elicitation — the INVERSE/completeness
/// critic). Asks what the request implies but leaves unspecified, classifies
/// each gap, and emits STRICT JSON whose `questions` match the `question`
/// builtin's input schema exactly.
const GAP_PROMPT: &str = "\
You are a completeness critic. The question is NOT \"is this done?\" but \"what \
does this request IMPLY yet leave UNSPECIFIED?\". Examine the feature request \
and its extracted acceptance criteria below, then surface every gap.\n\n\
Classify each gap:\n\
- ALREADY SPECIFIED in the request (a stated location, language, scope, \
constraint): NOT a gap — never raise it as a question or re-litigate it.\n\
- HIGH-GUESS-RATE (output format / naming / wording): safe to assume — state a \
reasonable default as an ASSUMPTION, do NOT ask.\n\
- LOW-GUESS-RATE (scope / risk / user intent / destructive-vs-additive): asking \
beats guessing — raise it as a QUESTION.\n\
Every option you offer MUST honor every stated constraint — never include a \
choice that contradicts the request.\n\n\
Output STRICT JSON ONLY (no prose, no markdown fences), exactly this shape:\n\
{\"assumptions\": [\"...\"], \"questions\": [{\"question\": \"...\", \"header\": \
\"...\", \"options\": [{\"label\": \"...\", \"description\": \"...\"}], \
\"multiple\": false}]}\n\
Rules: 0 to 4 questions, ONLY for low-guess gaps; each question has a short \
header (<=12 chars) and 2 to 4 options; `multiple` is a boolean. If nothing is \
unspecified, return empty arrays.\n\n";

/// Parsed shape of the gap critic's STRICT JSON reply.
#[derive(serde::Deserialize, Default)]
struct GapReply {
    #[serde(default)]
    assumptions: Vec<String>,
    #[serde(default)]
    questions: Vec<Question>,
}

/// Render the final markdown block the entry agent consumes. `questions` is
/// pretty-printed JSON (or the literal `(none — proceed)` when empty).
fn render(criteria: &str, assumptions: &[String], questions: &[Question]) -> String {
    let criteria = {
        let t = criteria.trim();
        if t.is_empty() {
            "(no criteria extracted)"
        } else {
            t
        }
    };
    let assumptions_block = if assumptions.is_empty() {
        "- (none)".to_string()
    } else {
        assumptions
            .iter()
            .map(|a| format!("- {}", a.trim()))
            .collect::<Vec<_>>()
            .join("\n")
    };
    let questions_block = if questions.is_empty() {
        "(none — proceed)".to_string()
    } else {
        // Pretty-print the typed questions back to JSON — guarantees the shape
        // matches the `question` builtin's input exactly.
        serde_json::to_string_pretty(questions).unwrap_or_else(|_| "(none — proceed)".to_string())
    };
    format!(
        "## Acceptance criteria\n{criteria}\n\n\
         ## Assumptions (proceeding without asking)\n{assumptions_block}\n\n\
         ## Questions for the user (ask via the question tool BEFORE building)\n\
         {questions_block}"
    )
}

/// Build the `intake` recipe: criteria extraction → gap elicitation, returning
/// the markdown brief the entry agent reads in-context.
pub(crate) fn recipe() -> WorkflowRecipe {
    WorkflowRecipe {
        name: "intake".into(),
        description: "Turn a feature request into observable acceptance criteria \
                      + load-bearing gaps before building — the completion-loop \
                      front half."
            .into(),
        args_schema: json!({
            "type": "object",
            "properties": {
                "request": {
                    "type": "string",
                    "description": "the user's feature request, verbatim"
                }
            },
            "required": ["request"]
        }),
        run: Arc::new(|ctx, args| {
            Box::pin(async move {
                let request = args
                    .get("request")
                    .and_then(|v| v.as_str())
                    .map(str::trim)
                    .filter(|r| !r.is_empty())
                    .ok_or_else(|| Error::Agent("intake: 'request' is required".into()))?
                    .to_string();

                // Stage 1 — criteria extraction. Cheap classification → "fast"
                // role (degrades to the default provider without a factory).
                let criteria = agent(&ctx, format!("{CRITERIA_PROMPT}{request}"))
                    .label("intake:criteria")
                    .model("fast")
                    .run()
                    .await?
                    .unwrap_or_default();

                // Stage 2 — gap elicitation (completeness critic). Parsed
                // defensively in pure Rust AFTER the leaf: a provider error
                // fails the recipe (like deep_research), but a MALFORMED reply
                // degrades to zero questions + the raw text as one assumption.
                let gap_text = agent(
                    &ctx,
                    format!(
                        "{GAP_PROMPT}Feature request:\n{request}\n\n\
                         Extracted acceptance criteria:\n{criteria}"
                    ),
                )
                .label("intake:gaps")
                .model("fast")
                .run()
                .await?
                .unwrap_or_default();

                let (assumptions, questions) = parse_gaps(&gap_text);
                Ok(render(&criteria, &assumptions, &questions))
            })
        }),
    }
}

/// Parse the gap critic's reply into `(assumptions, questions)`. On unparseable
/// JSON, degrade gracefully: zero questions, and the raw (trimmed) reply
/// surfaced as the single assumption — NEVER an error.
fn parse_gaps(text: &str) -> (Vec<String>, Vec<Question>) {
    let trimmed = text.trim();
    match parse_gap_json(trimmed) {
        Some(reply) => {
            let questions = reply
                .questions
                .into_iter()
                .filter(valid_question)
                .take(4)
                .collect();
            (reply.assumptions, questions)
        }
        None => {
            // Malformed (or empty) — degrade. Surface whatever the critic said
            // (if anything) as an assumption so nothing is silently lost.
            let assumption = if trimmed.is_empty() {
                Vec::new()
            } else {
                vec![trimmed.to_string()]
            };
            (assumption, Vec::new())
        }
    }
}

/// Try to extract the gap reply from `text`, tolerating a model that wraps the
/// JSON in prose or markdown fences by slicing the outermost `{ .. }`.
fn parse_gap_json(text: &str) -> Option<GapReply> {
    if let Ok(reply) = serde_json::from_str::<GapReply>(text) {
        return Some(reply);
    }
    let start = text.find('{')?;
    let end = text.rfind('}')?;
    if end <= start {
        return None;
    }
    serde_json::from_str::<GapReply>(&text[start..=end]).ok()
}

/// A question is only usable if the `question` builtin would accept it: a
/// non-empty prompt and at least 2 options (cap the options at 4).
fn valid_question(q: &Question) -> bool {
    !q.question.trim().is_empty() && q.options.len() >= 2
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::BoxedProvider;
    use crate::agent::flow::WorkflowCtx;
    use crate::llm::LlmProvider;
    use crate::llm::types::{
        CompletionRequest, CompletionResponse, ContentBlock, StopReason, TokenUsage,
    };
    use std::sync::{Arc, Mutex};

    /// Content-routed mock: stages run in a fixed order here, but routing by
    /// PROMPT substring (never call index) keeps the tests robust and mirrors
    /// `deep_research`'s harness. The criteria prompt and the gap prompt have
    /// disjoint marker phrases, so the gap stage (which embeds the criteria
    /// OUTPUT) never mis-routes to the criteria reply.
    struct RoutedProvider {
        criteria_reply: String,
        gap_reply: String,
        captured: Arc<Mutex<Vec<CompletionRequest>>>,
    }

    impl RoutedProvider {
        fn text(t: &str) -> CompletionResponse {
            CompletionResponse {
                content: vec![ContentBlock::Text { text: t.into() }],
                stop_reason: StopReason::EndTurn,
                reasoning: None,
                usage: TokenUsage::default(),
                model: None,
            }
        }
    }

    impl LlmProvider for RoutedProvider {
        async fn complete(&self, request: CompletionRequest) -> Result<CompletionResponse, Error> {
            let prompt: String = request
                .messages
                .iter()
                .flat_map(|m| m.content.iter())
                .filter_map(|b| match b {
                    ContentBlock::Text { text } => Some(text.as_str()),
                    _ => None,
                })
                .collect();
            self.captured.lock().expect("capture lock").push(request);
            // "completeness critic" is unique to the gap stage; the criteria
            // stage uses "requirements analyst". Order doesn't matter.
            let reply = if prompt.contains("completeness critic") {
                self.gap_reply.as_str()
            } else if prompt.contains("requirements analyst") {
                self.criteria_reply.as_str()
            } else {
                "unexpected prompt"
            };
            Ok(Self::text(reply))
        }
    }

    async fn run_recipe(
        criteria_reply: &str,
        gap_reply: &str,
        args: serde_json::Value,
    ) -> Result<String, Error> {
        let captured = Arc::new(Mutex::new(Vec::new()));
        let provider = Arc::new(BoxedProvider::from_arc(Arc::new(RoutedProvider {
            criteria_reply: criteria_reply.to_string(),
            gap_reply: gap_reply.to_string(),
            captured,
        })));
        let ctx = WorkflowCtx::builder(provider).build().expect("ctx");
        let r = recipe();
        (r.run)(ctx, args).await
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn extracts_observable_acceptance_criteria() {
        let criteria = "- the /health endpoint returns 200\n\
                        - a command can curl /health and see OK\n\
                        - the response body contains a status field";
        let out = run_recipe(
            criteria,
            r#"{"assumptions": [], "questions": []}"#,
            serde_json::json!({"request": "add a /health endpoint"}),
        )
        .await
        .unwrap();
        assert!(
            out.contains("## Acceptance criteria"),
            "missing criteria header: {out}"
        );
        assert!(out.contains("the /health endpoint returns 200"), "{out}");
        assert!(
            out.contains("the response body contains a status field"),
            "{out}"
        );
        // No questions/assumptions → the proceed sentinel.
        assert!(out.contains("(none — proceed)"), "{out}");
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn elicits_missing_low_guess_requirements() {
        let gap = r#"{
            "assumptions": ["output is rendered as markdown bullets"],
            "questions": [{
                "question": "Should deleting a record be destructive or soft-delete?",
                "header": "Delete",
                "options": [
                    {"label": "Hard delete", "description": "remove the row permanently"},
                    {"label": "Soft delete", "description": "mark a deleted flag"}
                ],
                "multiple": false
            }]
        }"#;
        let out = run_recipe(
            "- a record can be deleted",
            gap,
            serde_json::json!({"request": "let users delete records"}),
        )
        .await
        .unwrap();
        // The question text surfaces under the questions section.
        assert!(
            out.contains("Should deleting a record be destructive or soft-delete?"),
            "question text missing: {out}"
        );
        // The assumption surfaces under its own section.
        assert!(
            out.contains("## Assumptions (proceeding without asking)"),
            "{out}"
        );
        assert!(
            out.contains("output is rendered as markdown bullets"),
            "assumption missing: {out}"
        );
        // The options come through (pretty-printed JSON).
        assert!(out.contains("Soft delete"), "options missing: {out}");
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn malformed_critic_json_degrades_gracefully() {
        let garbage = "I think you should probably ask about auth, maybe? not sure.";
        let out = run_recipe(
            "- the feature works",
            garbage,
            serde_json::json!({"request": "build a thing"}),
        )
        .await
        .unwrap();
        // The recipe still succeeds; no questions are emitted.
        assert!(out.contains("(none — proceed)"), "{out}");
        // The garbage is surfaced (never lost) as an assumption.
        assert!(
            out.contains("I think you should probably ask about auth"),
            "raw critic text must surface as an assumption: {out}"
        );
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn empty_request_is_rejected() {
        let err = run_recipe("-", "{}", serde_json::json!({"request": "   "}))
            .await
            .unwrap_err();
        assert!(err.to_string().contains("request"), "{err}");

        let err2 = run_recipe("-", "{}", serde_json::json!({}))
            .await
            .unwrap_err();
        assert!(err2.to_string().contains("request"), "{err2}");
    }

    #[test]
    fn intake_recipe_registered_in_default_registry() {
        let reg = crate::agent::workflow_tool::default_registry();
        assert!(reg.get("intake").is_some(), "intake must be registered");
        let meta = reg.meta();
        assert!(
            meta.iter().any(|(n, _)| n == "intake"),
            "intake must appear in registry meta"
        );
    }

    #[test]
    fn questions_with_too_few_options_are_dropped() {
        // A low-guess question with a single option can't be asked → filtered.
        let (assumptions, questions) = parse_gaps(
            r#"{"assumptions": ["x"], "questions": [
                {"question": "Q?", "header": "H", "options": [{"label": "a", "description": "d"}], "multiple": false}
            ]}"#,
        );
        assert_eq!(assumptions, vec!["x".to_string()]);
        assert!(questions.is_empty(), "1-option question must be dropped");
    }

    #[test]
    fn extra_questions_are_capped_at_four() {
        let one = r#"{"question":"Q?","header":"H","options":[{"label":"a","description":"d"},{"label":"b","description":"e"}],"multiple":false}"#;
        let json =
            format!(r#"{{"assumptions": [], "questions": [{one},{one},{one},{one},{one},{one}]}}"#);
        let (_a, questions) = parse_gaps(&json);
        assert_eq!(questions.len(), 4, "questions cap at 4");
    }

    #[test]
    fn json_wrapped_in_prose_is_recovered() {
        let (assumptions, questions) = parse_gaps(
            "Here is my analysis:\n{\"assumptions\": [\"a1\"], \"questions\": []}\nDone.",
        );
        assert_eq!(assumptions, vec!["a1".to_string()]);
        assert!(questions.is_empty());
    }
}