harn-serve 0.10.121

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
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
use super::*;

impl AcpServer {
    pub(super) fn handle_session_new(
        &mut self,
        id: &serde_json::Value,
        params: &serde_json::Value,
    ) {
        let cwd = params
            .get("cwd")
            .and_then(|v| v.as_str())
            .map(PathBuf::from)
            .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));

        // Resolve the declared environment policy at the launch
        // boundary, snapshotting the server environment for env-source grants.
        // A malformed config or rejected launch fails the session loudly.
        let environment_policy = match self.resolve_session_environment(params) {
            Ok(environment) => environment,
            Err((message, data)) => {
                self.send_error_with_data(id, -32602, &message, data);
                return;
            }
        };

        let session_id = self.next_session_id();
        self.insert_session(session_id.clone(), cwd, SessionInfo::default());
        if let Some(session) = self.sessions.get_mut(&session_id) {
            session.environment_policy = environment_policy;
        }
        let session = self
            .session_item_json(&session_id, "live", None)
            .unwrap_or_else(|| serde_json::json!({"sessionId": session_id}));

        self.send_response(
            id,
            serde_json::json!({
                "sessionId": session_id,
                "session": session,
                "modes": modes::session_mode_state(modes::DEFAULT_MODE_ID),
                "configOptions": self.config_options_for_session(&session_id, modes::DEFAULT_MODE_ID),
            }),
        );

        self.emit_available_commands(&session_id);
    }

    /// Parse and launch the `environmentPolicy` block of a `session/new`
    /// request. Omission selects `inherited`. Env-source grants are snapshotted
    /// from the server environment here, at the launch boundary.
    fn resolve_session_environment(
        &self,
        params: &serde_json::Value,
    ) -> Result<harn_vm::security::SessionEnvironment, (String, serde_json::Value)> {
        let Some(raw) = params.get("environmentPolicy") else {
            return Ok(harn_vm::security::SessionEnvironment::inherited());
        };
        let config: AcpSessionEnvironmentConfig =
            serde_json::from_value(raw.clone()).map_err(|error| {
                let message =
                    format!("[environment_policy.invalid] invalid environment policy: {error}");
                (
                    message.clone(),
                    serde_json::json!({
                        "code": "environment_policy.invalid",
                        "message": message,
                    }),
                )
            })?;
        let environment =
            harn_vm::security::SessionEnvironment::launch(config.kind, config.grants, &|name| {
                std::env::var(name).ok()
            })
            .map_err(|error| (error.to_string(), error.to_json()))?;
        Ok(environment)
    }

    pub(super) fn ensure_workspace_anchor(
        &self,
        session_id: &str,
    ) -> Result<harn_vm::workspace_anchor::WorkspaceAnchor, String> {
        if let Some(anchor) = harn_vm::agent_sessions::workspace_anchor(session_id) {
            return Ok(anchor);
        }
        let Some(session) = self.sessions.get(session_id) else {
            return Err(format!("Unknown session: {session_id}"));
        };
        let anchor = harn_vm::workspace_anchor::WorkspaceAnchor {
            primary: session.cwd.clone(),
            additional_roots: Vec::new(),
            anchored_at: now_rfc3339(),
        };
        harn_vm::agent_sessions::set_workspace_anchor(session_id, Some(anchor.clone()))?;
        Ok(anchor)
    }

    pub(super) fn handle_harn_session_workspace_roots(
        &mut self,
        id: &serde_json::Value,
        params: &serde_json::Value,
    ) {
        let Some(session_id) = session_id_param(params) else {
            self.send_error(id, -32602, "Missing session_id");
            return;
        };
        let anchor = match self.ensure_workspace_anchor(&session_id) {
            Ok(anchor) => anchor,
            Err(message) => {
                self.send_error(id, -32602, &message);
                return;
            }
        };
        self.send_response(
            id,
            serde_json::json!({
                "sessionId": session_id,
                "workspaceAnchor": anchor.to_json(),
            }),
        );
    }

    pub(super) fn handle_harn_session_add_root(
        &mut self,
        id: &serde_json::Value,
        params: &serde_json::Value,
    ) {
        let Some(session_id) = session_id_param(params) else {
            self.send_error(id, -32602, "Missing session_id");
            return;
        };
        if let Err(message) = self.ensure_workspace_anchor(&session_id) {
            self.send_error(id, -32602, &message);
            return;
        }
        let Some(path) =
            string_param(params, "path", "path").or_else(|| string_param(params, "root", "root"))
        else {
            self.send_error(id, -32602, "Missing path");
            return;
        };
        let mount_mode = match mount_mode_param(params) {
            Ok(mount_mode) => mount_mode,
            Err(message) => {
                self.send_error(id, -32602, &message);
                return;
            }
        };
        let reason = string_param(params, "reason", "reason");
        let mounted_at = match harn_vm::agent_sessions::add_workspace_root(
            &session_id,
            &path,
            mount_mode,
            reason,
        ) {
            Ok(mounted_at) => mounted_at,
            Err(message) => {
                self.send_error(id, -32602, &message);
                return;
            }
        };
        let workspace_anchor = harn_vm::agent_sessions::workspace_anchor(&session_id)
            .map(|anchor| anchor.to_json())
            .unwrap_or(serde_json::Value::Null);
        self.send_response(
            id,
            serde_json::json!({
                "sessionId": session_id,
                "mountedAt": mounted_at,
                "workspaceAnchor": workspace_anchor,
            }),
        );
    }

    pub(super) fn handle_harn_session_reanchor(
        &mut self,
        id: &serde_json::Value,
        params: &serde_json::Value,
    ) {
        let Some(session_id) = session_id_param(params) else {
            self.send_error(id, -32602, "Missing session_id");
            return;
        };
        if let Err(message) = self.ensure_workspace_anchor(&session_id) {
            self.send_error(id, -32602, &message);
            return;
        }
        let Some(path) = string_param(params, "path", "path")
            .or_else(|| string_param(params, "primary", "primary"))
        else {
            self.send_error(id, -32602, "Missing path");
            return;
        };
        let compact = params
            .get("compact")
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(false);
        if compact {
            self.send_error(
                id,
                -32602,
                "harn.session_reanchor does not support compact yet",
            );
            return;
        }
        let carry_transcript = params
            .get("carryTranscript")
            .or_else(|| params.get("carry_transcript"))
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(true);
        if !carry_transcript {
            self.send_error(
                id,
                -32602,
                "harn.session_reanchor does not support carryTranscript=false yet",
            );
            return;
        }
        let reason = string_param(params, "reason", "reason");
        let next_project_root = session_project_root_for_cwd(&PathBuf::from(&path));
        if let Err(message) = self.ensure_session_root_can_move(&session_id, &next_project_root) {
            self.send_error(id, -32602, &message);
            return;
        }
        let anchor = harn_vm::workspace_anchor::WorkspaceAnchor {
            primary: PathBuf::from(path),
            additional_roots: Vec::new(),
            anchored_at: now_rfc3339(),
        };
        let outcome = match harn_vm::agent_sessions::reanchor_session(
            &session_id,
            anchor,
            carry_transcript,
            false,
            reason,
        ) {
            Ok(outcome) => outcome,
            Err(message) => {
                self.send_error(id, -32602, &message);
                return;
            }
        };
        if let Err(message) = self.sync_session_root_from_workspace_anchor(&session_id) {
            self.send_error(id, -32602, &message);
            return;
        }
        self.send_response(
            id,
            serde_json::json!({
                "sessionId": session_id,
                "changed": outcome.changed,
                "previousWorkspaceAnchor": outcome.previous.map(|anchor| anchor.to_json()),
                "workspaceAnchor": outcome.current.to_json(),
            }),
        );
    }

    pub(super) fn ensure_session_root_can_move(
        &self,
        session_id: &str,
        next_project_root: &Path,
    ) -> Result<(), String> {
        let Some(session) = self.sessions.get(session_id) else {
            return Err(format!("Unknown session: {session_id}"));
        };
        if session.project_root.as_path() == next_project_root {
            return Ok(());
        }
        #[cfg(feature = "hostlib")]
        {
            let status = harn_hostlib::fs::staged_status(session_id).map_err(|error| {
                format!("failed to inspect staged filesystem state for {session_id}: {error}")
            })?;
            if !status.pending_writes.is_empty() {
                return Err(format!(
                    "cannot change session project root with {} staged filesystem change(s) pending; commit or discard staged changes first",
                    status.pending_writes.len()
                ));
            }
        }
        Ok(())
    }

    pub(super) fn sync_session_root_from_workspace_anchor(
        &mut self,
        session_id: &str,
    ) -> Result<(), String> {
        let Some(anchor) = harn_vm::agent_sessions::workspace_anchor(session_id) else {
            return Ok(());
        };
        let next_cwd = anchor.primary;
        let next_project_root = session_project_root_for_cwd(&next_cwd);
        let needs_update = match self.sessions.get(session_id) {
            Some(session) => session.cwd != next_cwd || session.project_root != next_project_root,
            None => return Err(format!("Unknown session: {session_id}")),
        };
        if !needs_update {
            return Ok(());
        }
        self.ensure_session_root_can_move(session_id, &next_project_root)?;
        let Some(session) = self.sessions.get_mut(session_id) else {
            return Err(format!("Unknown session: {session_id}"));
        };
        session.cwd = next_cwd;
        session.project_root = next_project_root;
        #[cfg(feature = "hostlib")]
        harn_hostlib::fs::configure_session_root(session_id, &session.project_root);
        Ok(())
    }

    /// Read the configured pipeline source for `session_id`. Returns
    /// `None` for inline-prompt sessions (no `--pipeline`) and on read
    /// error — the regular prompt path will surface the error to the
    /// client at execution time.
    pub(super) fn read_pipeline_source(&self, session_id: &str) -> Option<String> {
        let pipeline_path = self.pipeline.as_deref()?;
        let cwd = &self.sessions.get(session_id)?.cwd;
        let full_path = if std::path::Path::new(pipeline_path).is_absolute() {
            PathBuf::from(pipeline_path)
        } else {
            cwd.join(pipeline_path)
        };
        std::fs::read_to_string(&full_path).ok()
    }

    /// Discover and emit `available_commands_update` if the command set
    /// has changed since the last emission for this session.
    pub(super) fn emit_available_commands(&mut self, session_id: &str) {
        let Some(source) = self.read_pipeline_source(session_id) else {
            return;
        };
        self.refresh_advertised_commands(session_id, &source);
    }

    /// Hot-reload variant of [`Self::emit_available_commands`] that uses
    /// pre-loaded source instead of re-reading from disk. Driven from
    /// `handle_session_prompt` on every prompt so editor changes between
    /// prompts propagate to the client without a restart.
    pub(super) fn refresh_advertised_commands(&mut self, session_id: &str, source: &str) {
        let commands = discover_commands(source);
        let Some(session) = self.sessions.get_mut(session_id) else {
            return;
        };
        if session.advertised_commands == commands {
            return;
        }
        session.advertised_commands = commands.clone();
        self.send_notification(
            "session/update",
            serde_json::json!({
                "sessionId": session_id,
                "update": {
                    "sessionUpdate": "available_commands_update",
                    "availableCommands": render_available_commands(&commands),
                },
            }),
        );
    }

    pub(super) fn emit_session_info_update(&self, session_id: &str, info: &SessionInfo) {
        self.send_notification(
            "session/update",
            session_info_update_params(session_id, info.title.as_deref(), &info.meta),
        );
    }

    pub(super) fn begin_profile_turn(&mut self, session_id: &str) -> u64 {
        if !self.profile.is_enabled() {
            return 0;
        }
        let Some(session) = self.sessions.get_mut(session_id) else {
            return 0;
        };
        session.profile_turn += 1;
        harn_vm::tracing::set_tracing_enabled(true);
        session.profile_turn
    }

    pub(super) fn finish_profile_turn(&self, session_id: &str, turn: u64) {
        if turn == 0 || !self.profile.is_enabled() {
            return;
        }
        let spans = harn_vm::tracing::take_spans();
        let rollup = harn_vm::profile::build(&spans);
        if self.profile.text {
            eprintln!("[harn] ACP profile session={session_id} turn={turn}");
            eprint!("{}", harn_vm::profile::render(&rollup));
        }
        if let Some(path) = self.profile.json_path.as_ref() {
            if let Err(error) = append_profile_json_line(path, session_id, turn, &rollup) {
                eprintln!("warning: failed to write ACP profile: {error}");
            }
        }
    }

    pub(super) fn handle_session_fork(
        &mut self,
        id: &serde_json::Value,
        params: &serde_json::Value,
    ) {
        let src_id = session_id_param(params);
        let Some(src_id) = src_id else {
            self.send_error(id, -32602, "Missing session_id");
            return;
        };
        let Some(src_cwd) = self
            .sessions
            .get(&src_id)
            .map(|session| session.cwd.clone())
        else {
            self.send_error(id, -32602, &format!("Unknown session: {src_id}"));
            return;
        };

        if !harn_vm::agent_sessions::exists(&src_id) {
            harn_vm::agent_sessions::open_or_create(Some(src_id.clone()));
        }

        let keep_first =
            match nonnegative_usize_param(params, &["keep_first", "keepFirst"], "keep_first") {
                Ok(value) => value,
                Err(message) => {
                    self.send_error(id, -32602, &message);
                    return;
                }
            };
        let dst_id = params
            .get("id")
            .and_then(|value| value.as_str())
            .map(str::to_string);
        if let Some(dst_id) = dst_id.as_deref() {
            if self.sessions.contains_key(dst_id) {
                self.send_error(id, -32602, &format!("Session already exists: {dst_id}"));
                return;
            }
            if harn_vm::agent_sessions::exists(dst_id) {
                self.send_error(id, -32602, &format!("Session already exists: {dst_id}"));
                return;
            }
        }
        let branch_name = params
            .get("branch_name")
            .and_then(|value| value.as_str())
            .map(str::to_string);

        let parent_environment = self
            .sessions
            .get(&src_id)
            .map(|session| session.environment_policy.clone())
            .unwrap_or_else(harn_vm::security::SessionEnvironment::inherited);
        let child_environment = match params.get("environmentPolicy") {
            None => parent_environment,
            Some(raw) => {
                let config: AcpSessionEnvironmentConfig = match serde_json::from_value(raw.clone())
                {
                    Ok(config) => config,
                    Err(error) => {
                        let message = format!(
                            "[environment_policy.invalid] invalid child environment policy: {error}"
                        );
                        self.send_error_with_data(
                            id,
                            -32602,
                            &message,
                            serde_json::json!({
                                "code": "environment_policy.invalid",
                                "message": message,
                            }),
                        );
                        return;
                    }
                };
                match parent_environment.narrow(config.kind, config.grants) {
                    Ok(environment) => environment,
                    Err(error) => {
                        self.send_error_with_data(id, -32602, &error.to_string(), error.to_json());
                        return;
                    }
                }
            }
        };

        let new_session_id = match keep_first {
            Some(keep_first) => harn_vm::agent_sessions::fork_at(&src_id, keep_first, dst_id),
            None => harn_vm::agent_sessions::fork(&src_id, dst_id),
        };
        let Some(new_session_id) = new_session_id else {
            self.send_error(id, -32000, &format!("Failed to fork session: {src_id}"));
            return;
        };

        let snapshot = harn_vm::agent_sessions::snapshot(&new_session_id)
            .and_then(|value| serde_json::to_value(harn_vm::llm::vm_value_to_json(&value)).ok())
            .unwrap_or_else(|| serde_json::json!({}));
        let branched_at = snapshot
            .get("branched_at_event_index")
            .cloned()
            .unwrap_or(serde_json::Value::Null);

        let mut meta = serde_json::Map::new();
        meta.insert("state".to_string(), serde_json::json!("forked"));
        meta.insert("parent_id".to_string(), serde_json::json!(src_id));
        meta.insert("branched_at".to_string(), branched_at.clone());
        if let Some(branch_name) = &branch_name {
            meta.insert("branch_name".to_string(), serde_json::json!(branch_name));
        }
        let info = SessionInfo {
            title: branch_name,
            meta,
        };

        let parent_mode_id = self
            .sessions
            .get(&src_id)
            .map(|session| session.current_mode_id.clone())
            .unwrap_or_else(|| modes::DEFAULT_MODE_ID.to_string());
        let parent_budget = self
            .sessions
            .get(&src_id)
            .map(|session| session.budget.clone())
            .unwrap_or_default();
        // A fork is the same session lineage: it inherits the parent's
        // environment policy (and thus its grants), not a fresh legacy env.
        let cancellation = self.register_session_cancellation(&new_session_id);
        let concurrent_control = ConcurrentSessionControl::new();
        self.concurrent_controls
            .register(&new_session_id, concurrent_control.clone());
        let fork_cwd = harn_vm::agent_sessions::workspace_anchor(&new_session_id)
            .map(|anchor| anchor.primary)
            .unwrap_or(src_cwd);
        let project_root = session_project_root_for_cwd(&fork_cwd);
        self.track_known_session(&new_session_id);
        self.sessions.insert(
            new_session_id.clone(),
            Session {
                cwd: fork_cwd,
                project_root,
                cancellation,
                host_bridge: None,
                inject_state: concurrent_control.inject_state.clone(),
                concurrent_control,
                info: info.clone(),
                advertised_commands: Vec::new(),
                current_mode_id: parent_mode_id.clone(),
                budget: parent_budget,
                profile_turn: 0,
                environment_policy: child_environment,
            },
        );
        self.emit_session_info_update(&new_session_id, &info);
        self.emit_available_commands(&new_session_id);
        self.send_response(
            id,
            serde_json::json!({
                "sessionId": new_session_id,
                "state": "forked",
                "parent_id": src_id,
                "branched_at": branched_at,
                "modes": modes::session_mode_state(&parent_mode_id),
                "configOptions": self.config_options_for_session(&new_session_id, &parent_mode_id),
            }),
        );
    }

    pub(super) fn handle_session_truncate(
        &mut self,
        id: &serde_json::Value,
        params: &serde_json::Value,
    ) {
        let Some(session_id) = session_id_param(params) else {
            self.send_error(id, -32602, "Missing sessionId");
            return;
        };
        let keep_first =
            match nonnegative_usize_param(params, &["keepFirst", "keep_first"], "keepFirst") {
                Ok(Some(value)) => value,
                Ok(None) => {
                    self.send_error(id, -32602, "Missing keepFirst");
                    return;
                }
                Err(message) => {
                    self.send_error(id, -32602, &message);
                    return;
                }
            };
        let Some(cancellation) = self
            .sessions
            .get(&session_id)
            .map(|session| session.cancellation.clone())
        else {
            self.send_error(id, -32602, &format!("Unknown session: {session_id}"));
            return;
        };

        cancellation.cancel();
        if !harn_vm::agent_sessions::exists(&session_id) {
            harn_vm::agent_sessions::open_or_create(Some(session_id.clone()));
        }
        let result = match harn_vm::agent_sessions::truncate(&session_id, keep_first) {
            Ok(Some(result)) => result,
            Ok(None) => {
                self.send_error(
                    id,
                    -32000,
                    &format!("Failed to truncate session: {session_id}"),
                );
                return;
            }
            Err(message) => {
                self.send_error(id, -32000, &message);
                return;
            }
        };

        let mut update = serde_json::json!({
            "sessionUpdate": "session_truncated",
            "keptTurnCount": result.kept_turn_count,
            "removedTurnCount": result.removed_turn_count,
            "newTipTurnId": result.new_tip_turn_id,
        });
        if let Some(reason) = params.get("reason").and_then(|value| value.as_str()) {
            update["reason"] = serde_json::json!(reason);
        }
        self.send_notification(
            "session/update",
            serde_json::json!({
                "sessionId": session_id,
                "update": update,
            }),
        );
        self.send_response(
            id,
            serde_json::json!({
                "sessionId": session_id,
                "keptTurnCount": result.kept_turn_count,
                "removedTurnCount": result.removed_turn_count,
                "newTipTurnId": result.new_tip_turn_id,
            }),
        );
    }
}

/// Build the `session_info_update` notification body.
///
/// Free-standing so the fork path and the store-change observer emit the same
/// frame. A second construction site is how a client ends up handling two
/// shapes for one update kind.
pub(super) fn session_info_update_params(
    session_id: &str,
    title: Option<&str>,
    meta: &serde_json::Map<String, serde_json::Value>,
) -> serde_json::Value {
    let mut update = serde_json::json!({
        "sessionUpdate": "session_info_update",
    });
    if let Some(title) = title {
        update["title"] = serde_json::json!(title);
    }
    if !meta.is_empty() {
        update["_meta"] = serde_json::Value::Object(meta.clone());
    }
    serde_json::json!({
        "sessionId": session_id,
        "update": update,
    })
}