harn-serve 0.10.166

Shared outbound workflow server core for Harn adapters
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
//! ACP transport output and host-bridge client call plumbing.
use super::*;

pub(super) fn verbose_bridge_logs_enabled() -> bool {
    matches!(
        std::env::var("HARN_ACP_VERBOSE").ok().as_deref(),
        Some("1" | "true" | "TRUE" | "yes" | "YES")
    ) || matches!(
        std::env::var("HARN_ACP_TRACE_CALLS").ok().as_deref(),
        Some("1")
    )
}

pub(super) fn host_call_timeout(method: &str) -> std::time::Duration {
    let configured = std::env::var("HARN_HOST_CALL_TIMEOUT_SECS")
        .ok()
        .and_then(|value| value.parse::<u64>().ok())
        .filter(|seconds| *seconds > 0);
    if let Some(seconds) = configured {
        return std::time::Duration::from_secs(seconds);
    }
    if method == "host/call" {
        return std::time::Duration::from_mins(5);
    }
    std::time::Duration::from_mins(1)
}

pub(super) fn suppress_default_info_log(message: &str) -> bool {
    if verbose_bridge_logs_enabled() {
        return false;
    }
    [
        "ACP_BOOT:",
        "span_end ",
        "WORKFLOW_POLICY:",
        "HINTS:",
        "AGENT_CONTEXT:",
        "SIBLING_OUTLINES:",
        "PROVIDERS: count=",
        "AUTO: base context start",
        "AUTO: base context done",
    ]
    .iter()
    .any(|prefix| message.starts_with(prefix))
}

#[derive(Clone)]
#[non_exhaustive]
pub enum AcpOutput {
    /// Write ACP JSON-RPC lines to process stdout, one line per message.
    Stdout(Arc<std::sync::Mutex<()>>),
    /// Send ACP JSON-RPC lines through an in-process channel.
    Channel(mpsc::UnboundedSender<String>),
    /// Invoke a host callback for each ACP JSON-RPC line.
    Callback(Arc<dyn Fn(&str) + Send + Sync + 'static>),
}

impl AcpOutput {
    /// Create an output sink that writes one JSON-RPC line per stdout line.
    pub fn stdout() -> Self {
        Self::Stdout(Arc::new(std::sync::Mutex::new(())))
    }

    /// Create an output sink backed by a Tokio unbounded channel.
    pub fn channel(tx: mpsc::UnboundedSender<String>) -> Self {
        Self::Channel(tx)
    }

    /// Create an output sink backed by a host callback.
    ///
    /// The callback receives the serialized JSON-RPC message without a
    /// trailing newline.
    pub fn callback(write_line: impl Fn(&str) + Send + Sync + 'static) -> Self {
        Self::Callback(Arc::new(write_line))
    }

    /// Capture the admitted task and prompt identity before any asynchronous producer
    /// can outlive this turn. Session-global and replay outputs stay unwrapped.
    pub(super) fn for_prompt(
        &self,
        correlation: AcpPromptCorrelation,
        task: Option<TaskPromptTarget>,
    ) -> Self {
        if correlation.message_id.is_none() && task.is_none() {
            return self.clone();
        }
        let output = self.clone();
        Self::callback(move |line| {
            match correlate_prompt_frame(line, &correlation, task.as_ref()) {
                Ok(line) => output.write_line(&line),
                Err(error) => tracing::warn!(%error, "Refused invalid prompt-owned ACP frame"),
            }
        })
    }

    /// Write a serialized ACP JSON-RPC message to this sink.
    pub fn write_line(&self, line: &str) {
        match self {
            Self::Stdout(lock) => {
                let _guard = lock.lock().unwrap_or_else(|e| e.into_inner());
                let mut stdout = std::io::stdout().lock();
                let _ = stdout.write_all(line.as_bytes());
                let _ = stdout.write_all(b"\n");
                let _ = stdout.flush();
            }
            Self::Channel(tx) => {
                let _ = tx.send(line.to_string());
            }
            Self::Callback(write_line) => write_line(line),
        }
    }
}

fn correlate_prompt_frame(
    line: &str,
    correlation: &AcpPromptCorrelation,
    task: Option<&TaskPromptTarget>,
) -> Result<String, String> {
    let mut frame: serde_json::Value =
        serde_json::from_str(line).map_err(|_| "Malformed ACP frame".to_string())?;
    let object = frame.as_object_mut().ok_or("ACP frame must be an object")?;
    let Some(method) = object.get("method") else {
        // Responses retain their JSON-RPC request identity and result shape.
        return Ok(line.to_string());
    };
    if !method.is_string() {
        return Err("ACP method must be a string".into());
    }
    let params = object
        .get_mut("params")
        .ok_or("Missing prompt-owned ACP params")?;
    if let Some(task) = task {
        task.project_into(params)
            .map_err(|error| error.message().to_owned())?;
    }
    let params = params
        .as_object_mut()
        .ok_or("Prompt-owned ACP params must be an object")?;
    if correlation.message_id.is_none() {
        return serde_json::to_string(&frame).map_err(|_| "ACP frame encoding failed".to_string());
    }
    if let Some(supplied) = params.get("promptCorrelation") {
        let supplied: AcpPromptCorrelation = serde_json::from_value(supplied.clone())
            .map_err(|_| "Invalid ACP prompt correlation".to_string())?;
        if supplied != *correlation {
            return Err("ACP frame conflicts with its owning prompt".into());
        }
    }
    params.insert(
        "promptCorrelation".into(),
        serde_json::to_value(correlation)
            .map_err(|_| "ACP prompt correlation encoding failed".to_string())?,
    );
    serde_json::to_string(&frame).map_err(|_| "ACP frame encoding failed".to_string())
}

/// Shared state that bridge-style builtins use to communicate with the
/// ACP client (editor) over JSON-RPC.
pub(super) struct AcpBridge {
    pub(super) session_id: String,
    pub(super) output: AcpOutput,
    pub(super) pending: Arc<Mutex<HashMap<u64, oneshot::Sender<serde_json::Value>>>>,
    pub(super) next_id_counter: AtomicU64,
    pub(super) cancellation: Arc<PromptCancellation>,
    /// Name of the currently executing Harn script (without .harn suffix).
    pub(super) script_name: std::sync::Mutex<String>,
    pub(super) assistant_state: std::sync::Mutex<VisibleTextState>,
}

impl AcpBridge {
    /// Write a complete JSON-RPC line to stdout.
    pub(super) fn write_line(&self, line: &str) {
        self.output.write_line(line);
    }

    /// Send a JSON-RPC notification.
    fn send_notification(&self, method: &str, params: serde_json::Value) {
        let notification = harn_vm::jsonrpc::notification(method, params);
        if let Ok(line) = serde_json::to_string(&notification) {
            self.write_line(&line);
        }
    }

    /// Send a `session/update` with agent_message_chunk.
    pub(super) fn send_update(&self, text: &str) {
        let (visible_text, visible_delta) = self
            .assistant_state
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .push(text, true);
        let mut content = serde_json::json!({
            "type": "text",
            "text": text,
        });
        let mut content_meta = serde_json::Map::new();
        content_meta.insert(
            "visible_text".to_string(),
            serde_json::Value::String(visible_text),
        );
        content_meta.insert(
            "visible_delta".to_string(),
            serde_json::Value::String(visible_delta),
        );
        events::merge_harn_meta(&mut content, content_meta);
        self.send_notification(
            "session/update",
            serde_json::json!({
                "sessionId": self.session_id,
                "update": {
                    "sessionUpdate": "agent_message_chunk",
                    "content": content,
                },
            }),
        );
    }

    /// Send a structured `session/update` with progress phase, message,
    /// and data. `progress` is a harn vendor-extension session-update
    /// variant; canonical ACP has no progress-phase concept, so all
    /// vendor fields ride under `update._meta.harn`.
    pub(super) fn send_progress(
        &self,
        phase: &str,
        message: &str,
        progress: Option<i64>,
        total: Option<i64>,
        data: Option<serde_json::Value>,
    ) {
        let update = progress_update(phase, message, progress, total, data);
        self.send_notification(
            "session/update",
            serde_json::json!({
                "sessionId": self.session_id,
                "update": update,
            }),
        );
    }

    /// Send a canonical ACP `plan` update. Per ACP, each plan update is
    /// a full replacement for the client's current plan state.
    #[allow(dead_code)]
    pub(super) fn send_plan(&self, entries: serde_json::Value) {
        let update = plan_update(entries);
        self.send_notification(
            "session/update",
            serde_json::json!({
                "sessionId": self.session_id,
                "update": update,
            }),
        );
    }

    /// Send a structured `session/update` with log level, message, and
    /// fields. `log` is a harn vendor-extension; canonical ACP has no
    /// log channel on the session-update stream, so all vendor fields
    /// ride under `update._meta.harn`.
    pub(super) fn send_log(&self, level: &str, message: &str, fields: Option<serde_json::Value>) {
        if level == "info" && suppress_default_info_log(message) {
            return;
        }
        let mut update = serde_json::json!({
            "sessionUpdate": "log",
        });
        let mut harn_meta = serde_json::Map::new();
        harn_meta.insert(
            "level".to_string(),
            serde_json::Value::String(level.to_string()),
        );
        harn_meta.insert(
            "message".to_string(),
            serde_json::Value::String(message.to_string()),
        );
        if let Some(f) = fields {
            harn_meta.insert("fields".to_string(), f);
        }
        events::merge_harn_meta(&mut update, harn_meta);
        self.send_notification(
            "session/update",
            serde_json::json!({
                "sessionId": self.session_id,
                "update": update,
            }),
        );
    }

    /// Set the currently executing script name (without .harn suffix).
    pub(super) fn set_script_name(&self, name: &str) {
        *self.script_name.lock().unwrap_or_else(|e| e.into_inner()) = name.to_string();
    }

    /// Get the current script name.
    #[allow(dead_code)]
    pub(super) fn get_script_name(&self) -> String {
        self.script_name
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .clone()
    }

    /// Send a JSON-RPC request to the client and await the response.
    pub(super) async fn call_client(
        &self,
        method: &str,
        params: serde_json::Value,
    ) -> Result<serde_json::Value, harn_vm::VmError> {
        self.call_client_inner(method, params, true).await
    }

    pub(super) async fn call_client_for_cleanup(
        &self,
        method: &str,
        params: serde_json::Value,
    ) -> Result<serde_json::Value, harn_vm::VmError> {
        self.call_client_inner(method, params, false).await
    }

    async fn call_client_inner(
        &self,
        method: &str,
        params: serde_json::Value,
        abort_on_cancel: bool,
    ) -> Result<serde_json::Value, harn_vm::VmError> {
        if abort_on_cancel && self.cancellation.cancelled.load(Ordering::SeqCst) {
            return Err(harn_vm::VmError::Runtime("Cancelled".into()));
        }

        let id = self.next_id_counter.fetch_add(1, Ordering::SeqCst);

        let request = serde_json::json!({
            "jsonrpc": "2.0",
            "id": id,
            "method": method,
            "params": params,
        });

        let (tx, rx) = oneshot::channel();
        {
            let mut pending = self.pending.lock().await;
            pending.insert(id, tx);
        }

        if let Ok(line) = serde_json::to_string(&request) {
            self.write_line(&line);
        }

        let timeout = host_call_timeout(method);
        let cancellation = self.cancellation.clone();
        let wait_cancelled = async move {
            loop {
                if cancellation.cancelled.load(Ordering::SeqCst) {
                    return;
                }
                cancellation.notify.notified().await;
            }
        };
        tokio::pin!(wait_cancelled);

        tokio::select! {
            result = rx => {
                let msg = result
                    .map_err(|_| harn_vm::VmError::Runtime("Client closed connection".into()))?;
                if let Some(error) = msg.get("error") {
                    let message = error["message"].as_str().unwrap_or("Unknown client error");
                    Err(harn_vm::VmError::Runtime(format!(
                        "Client error: {message}"
                    )))
                } else {
                    Ok(msg["result"].clone())
                }
            }
            _ = &mut wait_cancelled, if abort_on_cancel => {
                let mut pending = self.pending.lock().await;
                pending.remove(&id);
                Err(harn_vm::VmError::Runtime("Cancelled".into()))
            }
            _ = tokio::time::sleep(timeout) => {
                let mut pending = self.pending.lock().await;
                pending.remove(&id);
                Err(harn_vm::VmError::Runtime(format!(
                    "Client did not respond to '{method}' within {timeout:?}"
                )))
            }
        }
    }
}

pub(super) fn plan_update(entries: serde_json::Value) -> serde_json::Value {
    serde_json::json!({
        "sessionUpdate": "plan",
        "entries": entries,
    })
}

pub(super) fn progress_update(
    phase: &str,
    message: &str,
    progress: Option<i64>,
    total: Option<i64>,
    data: Option<serde_json::Value>,
) -> serde_json::Value {
    let mut update = serde_json::json!({
        "sessionUpdate": "progress",
    });
    let mut harn_meta = serde_json::Map::new();
    harn_meta.insert(
        "phase".to_string(),
        serde_json::Value::String(phase.to_string()),
    );
    harn_meta.insert(
        "message".to_string(),
        serde_json::Value::String(message.to_string()),
    );
    if let Some(p) = progress {
        harn_meta.insert("progress".to_string(), serde_json::Value::from(p));
    }
    if let Some(t) = total {
        harn_meta.insert("total".to_string(), serde_json::Value::from(t));
    }
    if let Some(d) = data {
        harn_meta.insert("data".to_string(), d);
    }
    events::merge_harn_meta(&mut update, harn_meta);
    update
}

#[cfg(test)]
#[path = "bridge/prompt_correlation_tests.rs"]
mod prompt_correlation_tests;

#[cfg(test)]
#[path = "bridge/event_transport_tests.rs"]
mod event_transport_tests;