opensymphony 1.2.0

A Rust implementation of the OpenAI Symphony orchestration design
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
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
use std::{
    collections::HashSet,
    env, io,
    io::Write,
    path::{Path, PathBuf},
    process::ExitCode,
    time::Duration,
};

use crate::opensymphony_openhands::{
    ConversationLaunchProfile, EventEnvelope, IssueConversationManifest, IssueSessionRunnerConfig,
    KnownEvent, LocalServerSupervisor, LocalServerTooling, OpenHandsClient, OpenHandsError,
    RuntimeEventStream, SendMessageRequest, SupervisedServerConfig, SupervisorConfig,
    SupervisorError, TerminalExecutionStatus, TransportConfig,
};
use crate::opensymphony_workflow::{ProcessEnvironment, ResolvedWorkflow, WorkflowDefinition};
use crate::opensymphony_workspace::{
    CleanupConfig, HookConfig, HookDefinition, WorkspaceError, WorkspaceHandle, WorkspaceManager,
    WorkspaceManagerConfig,
};
use clap::Args;
use serde::Deserialize;
use thiserror::Error;
use tokio::{fs, time::timeout_at};
use url::Url;
use uuid::Uuid;

const DEFAULT_CONFIG_FILE: &str = "config.yaml";
const RECENT_HISTORY_LIMIT: usize = 8;

#[derive(Debug, Args, Clone)]
pub struct DebugArgs {
    #[arg(help = "Linear issue identifier or persisted issue ID to resume")]
    pub issue_id: String,
    #[arg(help = "Runtime config YAML path; defaults to ./config.yaml when present")]
    #[arg(long)]
    pub config: Option<PathBuf>,
}

#[derive(Debug, Default, Deserialize)]
struct DebugConfigFile {
    #[serde(default)]
    target_repo: Option<String>,
    #[serde(default)]
    openhands: DebugOpenHandsConfigFile,
}

#[derive(Debug, Default, Deserialize)]
struct DebugOpenHandsConfigFile {
    #[serde(default)]
    tool_dir: Option<String>,
}

struct DebugRuntimeConfig {
    workflow: ResolvedWorkflow,
    tool_dir: Option<PathBuf>,
}

#[derive(Debug, Error)]
enum DebugCommandError {
    #[error("failed to determine the current working directory: {0}")]
    CurrentDir(#[source] io::Error),
    #[error("failed to read {path}: {source}")]
    ReadConfig {
        path: PathBuf,
        #[source]
        source: io::Error,
    },
    #[error("failed to parse {path}: {source}")]
    ParseConfig {
        path: PathBuf,
        #[source]
        source: serde_yaml::Error,
    },
    #[error("failed to expand {path}: {detail}")]
    ResolveConfig { path: PathBuf, detail: String },
    #[error("failed to load workflow {path}: {source}")]
    LoadWorkflow {
        path: PathBuf,
        #[source]
        source: crate::opensymphony_workflow::WorkflowLoadError,
    },
    #[error("failed to resolve workflow {path}: {source}")]
    ResolveWorkflow {
        path: PathBuf,
        #[source]
        source: crate::opensymphony_workflow::WorkflowConfigError,
    },
    #[error("failed to create workspace manager: {0}")]
    WorkspaceManager(#[from] WorkspaceError),
    #[error(
        "no managed workspace for issue reference `{issue_reference}` exists under {workspace_root}"
    )]
    WorkspaceNotFound {
        issue_reference: String,
        workspace_root: PathBuf,
    },
    #[error("conversation manifest is missing: {path}")]
    ConversationManifestMissing { path: PathBuf },
    #[error("failed to decode conversation manifest {path}: {source}")]
    DecodeConversationManifest {
        path: PathBuf,
        #[source]
        source: serde_json::Error,
    },
    #[error("conversation manifest contains invalid conversation id `{value}`: {source}")]
    InvalidConversationId {
        value: String,
        #[source]
        source: uuid::Error,
    },
    #[error("failed to build conversation launch profile: {detail}")]
    LaunchProfile { detail: String },
    #[error(transparent)]
    Transport(#[from] OpenHandsError),
    #[error(transparent)]
    Tooling(#[from] crate::opensymphony_openhands::LocalToolingError),
    #[error(transparent)]
    Supervisor(#[from] SupervisorError),
    #[error(
        "workflow config requires a managed local OpenHands server, but `openhands.tool_dir` is missing from config.yaml (recommended: ~/.opensymphony/openhands-server)"
    )]
    MissingToolDir,
    #[error(
        "managed local OpenHands tooling at {tool_dir} is missing or invalid: {detail}. Run `opensymphony install openhands` or `opensymphony doctor --config <path>`."
    )]
    ToolingSetupRequired { tool_dir: PathBuf, detail: String },
    #[error(
        "OpenHands transport URL `{value}` does not include an explicit port and has no default port"
    )]
    MissingTransportPort { value: String },
    #[error("runtime rehydration returned conversation {actual}, expected {expected}")]
    RehydratedConversationMismatch { expected: Uuid, actual: Uuid },
    #[error("rehydrated conversation {conversation_id} did not expose persisted history")]
    PersistedHistoryMissing { conversation_id: Uuid },
    #[error(
        "conversation {conversation_id} remained active past the wait timeout ({timeout_ms} ms)"
    )]
    ActiveTurnTimeout {
        conversation_id: Uuid,
        timeout_ms: u128,
    },
    #[error("conversation {conversation_id} ended before the debug turn reached a terminal status")]
    StreamEnded { conversation_id: Uuid },
    #[error("conversation {conversation_id} emitted ConversationErrorEvent {event_id}")]
    ConversationError {
        conversation_id: Uuid,
        event_id: String,
    },
    #[error("conversation {conversation_id} reported terminal execution_status `{status}`")]
    TerminalStatus {
        conversation_id: Uuid,
        status: String,
    },
    #[error(
        "debug interaction timed out waiting for new runtime activity on conversation {conversation_id}"
    )]
    DebugTurnTimeout { conversation_id: Uuid },
    #[error("terminal I/O failed: {0}")]
    TerminalIo(#[source] io::Error),
}

#[derive(Clone, Copy)]
enum TranscriptRole {
    User,
    Assistant,
    Action,
    Observation,
}

impl TranscriptRole {
    fn label(self) -> &'static str {
        match self {
            Self::User => "user",
            Self::Assistant => "assistant",
            Self::Action => "action",
            Self::Observation => "observation",
        }
    }
}

struct TranscriptEntry {
    event_id: String,
    role: TranscriptRole,
    text: String,
}

pub async fn run_command(args: DebugArgs) -> ExitCode {
    match run_debug_session(args).await {
        Ok(()) => ExitCode::SUCCESS,
        Err(error) => {
            eprintln!("{error}");
            ExitCode::from(1)
        }
    }
}

async fn run_debug_session(args: DebugArgs) -> Result<(), DebugCommandError> {
    let runtime = resolve_runtime_config(&args).await?;
    let manager = WorkspaceManager::new(build_workspace_manager_config(&runtime.workflow))?;
    let workspace = manager
        .find_workspace_by_issue_reference(&args.issue_id)
        .await?
        .ok_or_else(|| DebugCommandError::WorkspaceNotFound {
            issue_reference: args.issue_id.clone(),
            workspace_root: runtime.workflow.config.workspace.root.clone(),
        })?;
    let manifest = load_conversation_manifest(&manager, &workspace).await?;
    let (client, mut supervisor, server_message) = build_debug_client(&runtime)?;
    let config = IssueSessionRunnerConfig::from_workflow(&runtime.workflow);
    let conversation_id = parse_conversation_id(&manifest)?;
    let mut stream =
        attach_or_rehydrate_stream(&client, &runtime.workflow, &workspace, &manifest, &config)
            .await?;

    println!(
        "Resumed conversation {} for issue {} in {}",
        manifest.conversation_id,
        workspace.identifier(),
        workspace.workspace_path().display()
    );
    println!("{server_message}");

    if turn_is_in_progress(stream.state_mirror().execution_status().unwrap_or("idle")) {
        println!("Waiting for the current OpenHands turn to finish before accepting input...");
        wait_for_turn_to_stop(&mut stream, conversation_id, config.terminal_wait_timeout).await?;
    }

    print_recent_history(stream.event_cache().items());
    println!(
        "Type a prompt to continue the conversation. Use /history to reprint recent context and /exit to quit."
    );

    let result = interactive_debug_loop(
        &client,
        &mut stream,
        conversation_id,
        config.terminal_wait_timeout,
    )
    .await;

    let close_result = stream.close().await;
    drop(supervisor.take());
    result?;
    close_result?;
    Ok(())
}

async fn resolve_runtime_config(args: &DebugArgs) -> Result<DebugRuntimeConfig, DebugCommandError> {
    let current_dir = env::current_dir().map_err(DebugCommandError::CurrentDir)?;
    let config_path = match &args.config {
        Some(path) => Some(resolve_cli_path(&current_dir, path)),
        None => {
            let default = current_dir.join(DEFAULT_CONFIG_FILE);
            default.is_file().then_some(default)
        }
    };

    let repo_root_hint = super::find_cargo_workspace_root(&current_dir);
    let default_target_repo = if current_dir.join("WORKFLOW.md").is_file() {
        current_dir.clone()
    } else if let Some(repo_root) = repo_root_hint
        .as_ref()
        .filter(|repo_root| repo_root.join("WORKFLOW.md").is_file())
    {
        repo_root.clone()
    } else {
        current_dir.clone()
    };

    let (target_repo, configured_tool_dir) = if let Some(path) = config_path.as_ref() {
        let config = load_config(path).await?;
        let config_root = path.parent().unwrap_or(&current_dir);
        let target_repo = config
            .target_repo
            .as_deref()
            .map(|value| resolve_config_path(path, config_root, value))
            .transpose()?
            .unwrap_or_else(|| default_target_repo.clone());
        let tool_dir = config
            .openhands
            .tool_dir
            .as_deref()
            .map(|value| resolve_config_path(path, config_root, value))
            .transpose()?;
        (target_repo, tool_dir)
    } else {
        (default_target_repo, None)
    };

    let workflow_path = target_repo.join("WORKFLOW.md");
    let workflow = WorkflowDefinition::load_from_path(&workflow_path).map_err(|source| {
        DebugCommandError::LoadWorkflow {
            path: workflow_path.clone(),
            source,
        }
    })?;
    let workflow = workflow
        .resolve_with_process_env(&target_repo)
        .map_err(|source| DebugCommandError::ResolveWorkflow {
            path: workflow_path.clone(),
            source,
        })?;

    Ok(DebugRuntimeConfig {
        workflow,
        tool_dir: configured_tool_dir,
    })
}

async fn load_config(path: &Path) -> Result<DebugConfigFile, DebugCommandError> {
    let raw = fs::read_to_string(path)
        .await
        .map_err(|source| DebugCommandError::ReadConfig {
            path: path.to_path_buf(),
            source,
        })?;
    serde_yaml::from_str(&raw).map_err(|source| DebugCommandError::ParseConfig {
        path: path.to_path_buf(),
        source,
    })
}

fn resolve_config_path(
    config_path: &Path,
    config_root: &Path,
    raw: &str,
) -> Result<PathBuf, DebugCommandError> {
    let expanded =
        super::expand_env_tokens(raw).map_err(|error| DebugCommandError::ResolveConfig {
            path: config_path.to_path_buf(),
            detail: error.to_string(),
        })?;
    Ok(super::resolve_path(config_root, &expanded))
}

fn resolve_cli_path(base: &Path, path: &Path) -> PathBuf {
    if path.is_absolute() {
        path.to_path_buf()
    } else {
        base.join(path)
    }
}

fn build_workspace_manager_config(workflow: &ResolvedWorkflow) -> WorkspaceManagerConfig {
    let hooks = &workflow.config.hooks;
    WorkspaceManagerConfig {
        root: workflow.config.workspace.root.clone(),
        hooks: HookConfig {
            after_create: hooks.after_create.clone().map(HookDefinition::shell),
            before_run: hooks.before_run.clone().map(HookDefinition::shell),
            after_run: hooks.after_run.clone().map(HookDefinition::shell),
            before_remove: hooks.before_remove.clone().map(HookDefinition::shell),
            timeout: Duration::from_millis(hooks.timeout_ms),
        },
        cleanup: CleanupConfig {
            remove_terminal_workspaces: false,
        },
    }
}

fn build_debug_client(
    runtime: &DebugRuntimeConfig,
) -> Result<(OpenHandsClient, Option<LocalServerSupervisor>, String), DebugCommandError> {
    let transport = TransportConfig::from_workflow(&runtime.workflow, &ProcessEnvironment)?;
    let Some(supervisor_base_url) = transport.managed_local_server_base_url()? else {
        let message = format!(
            "Using configured OpenHands server at {}.",
            transport.base_url()
        );
        return Ok((OpenHandsClient::new(transport), None, message));
    };

    let tool_dir = runtime
        .tool_dir
        .clone()
        .ok_or(DebugCommandError::MissingToolDir)?;
    let tooling = LocalServerTooling::load(tool_dir.clone()).map_err(|error| {
        DebugCommandError::ToolingSetupRequired {
            tool_dir,
            detail: error.to_string(),
        }
    })?;
    let url =
        Url::parse(&supervisor_base_url).expect("validated managed supervisor URL should parse");
    let mut config = SupervisedServerConfig::new(tooling);
    config.extra_env = runtime
        .workflow
        .extensions
        .openhands
        .local_server
        .env
        .clone();
    config.startup_timeout = Duration::from_millis(
        runtime
            .workflow
            .extensions
            .openhands
            .local_server
            .startup_timeout_ms,
    );
    config.probe.path = runtime
        .workflow
        .extensions
        .openhands
        .local_server
        .readiness_probe_path
        .clone();
    config.port_override = Some(transport_port_override(&url)?);

    let mut supervisor = LocalServerSupervisor::new(SupervisorConfig::Supervised(Box::new(config)));
    match supervisor.start() {
        Ok(status) => {
            let base_url = status.base_url.clone();
            let transport = TransportConfig::new(&base_url).with_auth(transport.auth().clone());
            Ok((
                OpenHandsClient::new(transport),
                Some(supervisor),
                format!("Started local OpenHands server at {base_url} for the debug session."),
            ))
        }
        Err(SupervisorError::ExistingReadyServer { base_url, .. }) => {
            let transport = TransportConfig::new(&base_url).with_auth(transport.auth().clone());
            Ok((
                OpenHandsClient::new(transport),
                None,
                format!("Using existing OpenHands server at {base_url}."),
            ))
        }
        Err(error) => Err(DebugCommandError::Supervisor(error)),
    }
}

fn transport_port_override(url: &Url) -> Result<u16, DebugCommandError> {
    url.port_or_known_default()
        .ok_or_else(|| DebugCommandError::MissingTransportPort {
            value: url.as_str().to_string(),
        })
}

async fn load_conversation_manifest(
    manager: &WorkspaceManager,
    workspace: &WorkspaceHandle,
) -> Result<IssueConversationManifest, DebugCommandError> {
    let path = workspace.conversation_manifest_path();
    let raw = manager
        .read_text_artifact(workspace, &path)
        .await?
        .ok_or_else(|| DebugCommandError::ConversationManifestMissing { path: path.clone() })?;
    serde_json::from_str(&raw)
        .map_err(|source| DebugCommandError::DecodeConversationManifest { path, source })
}

fn parse_conversation_id(manifest: &IssueConversationManifest) -> Result<Uuid, DebugCommandError> {
    Uuid::parse_str(manifest.conversation_id.as_str()).map_err(|source| {
        DebugCommandError::InvalidConversationId {
            value: manifest.conversation_id.to_string(),
            source,
        }
    })
}

async fn attach_or_rehydrate_stream(
    client: &OpenHandsClient,
    workflow: &ResolvedWorkflow,
    workspace: &WorkspaceHandle,
    manifest: &IssueConversationManifest,
    config: &IssueSessionRunnerConfig,
) -> Result<RuntimeEventStream, DebugCommandError> {
    let conversation_id = parse_conversation_id(manifest)?;
    let stream_config = config.runtime_stream.clone();
    match client
        .attach_runtime_stream(conversation_id, stream_config.clone())
        .await
    {
        Ok(stream) => Ok(stream),
        Err(error) if should_rehydrate_after_attach_failure(&error) => {
            let launch_profile = resolve_launch_profile(manifest, workflow)
                .map_err(|detail| DebugCommandError::LaunchProfile { detail })?;
            let request = launch_profile
                .to_create_request(
                    &ProcessEnvironment,
                    workspace.workspace_path(),
                    &manifest.persistence_dir,
                    Some(conversation_id),
                )
                .map_err(|detail| DebugCommandError::LaunchProfile { detail })?;
            let conversation = client.create_conversation(&request).await?;
            if conversation.conversation_id != conversation_id {
                return Err(DebugCommandError::RehydratedConversationMismatch {
                    expected: conversation_id,
                    actual: conversation.conversation_id,
                });
            }

            let stream = client
                .attach_runtime_stream(conversation_id, stream_config)
                .await?;
            if stream.event_cache().items().len() <= 1 {
                return Err(DebugCommandError::PersistedHistoryMissing { conversation_id });
            }

            Ok(stream)
        }
        Err(error) => Err(error.into()),
    }
}

fn should_rehydrate_after_attach_failure(error: &OpenHandsError) -> bool {
    matches!(
        error,
        OpenHandsError::HttpStatus {
            status_code: 404,
            ..
        }
    )
}

fn resolve_launch_profile(
    manifest: &IssueConversationManifest,
    workflow: &ResolvedWorkflow,
) -> Result<ConversationLaunchProfile, String> {
    manifest
        .launch_profile
        .clone()
        .map(Ok)
        .unwrap_or_else(|| ConversationLaunchProfile::from_workflow(workflow))
}

async fn interactive_debug_loop(
    client: &OpenHandsClient,
    stream: &mut RuntimeEventStream,
    conversation_id: Uuid,
    wait_timeout: Duration,
) -> Result<(), DebugCommandError> {
    let stdin = io::stdin();
    let mut stdout = io::stdout();
    let mut line = String::new();

    loop {
        print!("debug> ");
        stdout.flush().map_err(DebugCommandError::TerminalIo)?;
        line.clear();
        let read = stdin
            .read_line(&mut line)
            .map_err(DebugCommandError::TerminalIo)?;
        if read == 0 {
            return Ok(());
        }

        let input = line.trim();
        if input.is_empty() {
            continue;
        }
        if matches!(input, "/exit" | "exit" | "quit") {
            return Ok(());
        }
        if input == "/history" {
            print_recent_history(stream.event_cache().items());
            continue;
        }

        run_debug_turn(client, stream, conversation_id, input, wait_timeout).await?;
    }
}

async fn run_debug_turn(
    client: &OpenHandsClient,
    stream: &mut RuntimeEventStream,
    conversation_id: Uuid,
    prompt: &str,
    wait_timeout: Duration,
) -> Result<(), DebugCommandError> {
    if turn_is_in_progress(stream.state_mirror().execution_status().unwrap_or("idle")) {
        wait_for_turn_to_stop(stream, conversation_id, wait_timeout).await?;
    }

    let baseline_event_ids = stream
        .event_cache()
        .items()
        .iter()
        .map(|event| event.id.clone())
        .collect::<HashSet<_>>();

    client
        .send_message(conversation_id, &SendMessageRequest::user_text(prompt))
        .await?;
    loop {
        match client.run_conversation(conversation_id).await {
            Ok(_) => break,
            Err(OpenHandsError::HttpStatus {
                status_code: 409, ..
            }) => {
                wait_for_turn_to_stop(stream, conversation_id, wait_timeout).await?;
                let _ = stream.reconcile_events().await;
            }
            Err(error) => return Err(DebugCommandError::Transport(error)),
        }
    }

    wait_for_turn_terminal(stream, &baseline_event_ids, conversation_id, wait_timeout).await?;
    let new_entries = transcript_entries(stream.event_cache().items())
        .into_iter()
        .filter(|entry| !baseline_event_ids.contains(&entry.event_id))
        .filter(|entry| !matches!(entry.role, TranscriptRole::User))
        .collect::<Vec<_>>();

    if new_entries.is_empty() {
        println!("assistant> (no printable assistant text was emitted for this turn)");
    } else {
        for entry in new_entries {
            println!("{}> {}", entry.role.label(), entry.text);
        }
    }

    Ok(())
}

async fn wait_for_turn_to_stop(
    stream: &mut RuntimeEventStream,
    conversation_id: Uuid,
    wait_timeout: Duration,
) -> Result<(), DebugCommandError> {
    if stream
        .state_mirror()
        .execution_status()
        .is_none_or(turn_has_stopped)
    {
        return Ok(());
    }

    let deadline = tokio::time::Instant::now() + wait_timeout;
    loop {
        if stream
            .state_mirror()
            .execution_status()
            .is_some_and(turn_has_stopped)
        {
            return Ok(());
        }

        match timeout_at(deadline, stream.next_event()).await {
            Err(_) => {
                return Err(DebugCommandError::ActiveTurnTimeout {
                    conversation_id,
                    timeout_ms: wait_timeout.as_millis(),
                });
            }
            Ok(Ok(Some(_))) => {}
            Ok(Ok(None)) => {
                if let Ok(inserted) = stream.reconcile_events().await
                    && inserted > 0
                {
                    continue;
                }
            }
            Ok(Err(error)) => {
                if stream
                    .state_mirror()
                    .execution_status()
                    .is_some_and(turn_has_stopped)
                {
                    return Ok(());
                }
                return Err(DebugCommandError::Transport(error));
            }
        }
    }
}

async fn wait_for_turn_terminal(
    stream: &mut RuntimeEventStream,
    baseline_event_ids: &HashSet<String>,
    conversation_id: Uuid,
    wait_timeout: Duration,
) -> Result<(), DebugCommandError> {
    let deadline = tokio::time::Instant::now() + wait_timeout;
    loop {
        if let Some(result) = current_turn_outcome(stream, baseline_event_ids, conversation_id) {
            return result;
        }

        match timeout_at(deadline, stream.next_event()).await {
            Err(_) => {
                if let Ok(inserted) = stream.reconcile_events().await
                    && inserted > 0
                {
                    continue;
                }
                return Err(DebugCommandError::DebugTurnTimeout { conversation_id });
            }
            Ok(Ok(Some(_))) => {}
            Ok(Ok(None)) => {
                if let Ok(inserted) = stream.reconcile_events().await
                    && inserted > 0
                {
                    continue;
                }
                if let Some(result) =
                    current_turn_outcome(stream, baseline_event_ids, conversation_id)
                {
                    return result;
                }
                return Err(DebugCommandError::StreamEnded { conversation_id });
            }
            Ok(Err(error)) => {
                if let Some(result) =
                    current_turn_outcome(stream, baseline_event_ids, conversation_id)
                {
                    return result;
                }
                return Err(DebugCommandError::Transport(error));
            }
        }
    }
}

fn current_turn_outcome(
    stream: &RuntimeEventStream,
    baseline_event_ids: &HashSet<String>,
    conversation_id: Uuid,
) -> Option<Result<(), DebugCommandError>> {
    let current_turn_events = stream
        .event_cache()
        .items()
        .iter()
        .filter(|event| !baseline_event_ids.contains(&event.id))
        .collect::<Vec<_>>();
    if current_turn_events.is_empty() {
        return None;
    }

    if let Some(error_event) = current_turn_events.iter().find(|event| {
        matches!(
            KnownEvent::from_envelope(event),
            KnownEvent::ConversationError(_)
        )
    }) {
        return Some(Err(DebugCommandError::ConversationError {
            conversation_id,
            event_id: error_event.id.clone(),
        }));
    }

    match stream.state_mirror().terminal_status() {
        Some(TerminalExecutionStatus::Finished) => Some(Ok(())),
        Some(TerminalExecutionStatus::Error) | Some(TerminalExecutionStatus::Stuck) => {
            Some(Err(DebugCommandError::TerminalStatus {
                conversation_id,
                status: stream
                    .state_mirror()
                    .execution_status()
                    .unwrap_or("unknown")
                    .to_string(),
            }))
        }
        None => None,
    }
}

fn print_recent_history(events: &[EventEnvelope]) {
    let entries = transcript_entries(events);
    if entries.is_empty() {
        println!("No prior printable transcript entries were found in the resumed conversation.");
        return;
    }

    println!("Recent conversation history:");
    let start = entries.len().saturating_sub(RECENT_HISTORY_LIMIT);
    for entry in &entries[start..] {
        println!(
            "{}> {}",
            entry.role.label(),
            summarize_history_text(&entry.text)
        );
    }
}

fn transcript_entries(events: &[EventEnvelope]) -> Vec<TranscriptEntry> {
    events.iter().filter_map(extract_transcript_entry).collect()
}

fn extract_transcript_entry(event: &EventEnvelope) -> Option<TranscriptEntry> {
    match event.kind.as_str() {
        "MessageEvent" => {
            let (role, content) = if let Some(message) = event.payload.get("llm_message") {
                (
                    TranscriptRole::Assistant,
                    message
                        .get("content")?
                        .as_array()?
                        .first()?
                        .get("text")?
                        .as_str()?,
                )
            } else {
                let role = match event
                    .payload
                    .get("role")
                    .and_then(serde_json::Value::as_str)
                {
                    Some("user") => TranscriptRole::User,
                    _ => TranscriptRole::Assistant,
                };
                (
                    role,
                    event
                        .payload
                        .get("content")?
                        .as_array()?
                        .first()?
                        .get("text")?
                        .as_str()?,
                )
            };
            Some(TranscriptEntry {
                event_id: event.id.clone(),
                role,
                text: normalize_text(content),
            })
        }
        "ActionEvent" => Some(TranscriptEntry {
            event_id: event.id.clone(),
            role: TranscriptRole::Action,
            text: normalize_text(
                event
                    .payload
                    .get("action")
                    .and_then(|action| action.get("message"))?
                    .as_str()?,
            ),
        }),
        "ObservationEvent" => Some(TranscriptEntry {
            event_id: event.id.clone(),
            role: TranscriptRole::Observation,
            text: normalize_text(
                event
                    .payload
                    .get("observation")
                    .and_then(|observation| observation.get("content"))?
                    .as_array()?
                    .first()?
                    .get("text")?
                    .as_str()?,
            ),
        }),
        _ => None,
    }
}

fn normalize_text(text: &str) -> String {
    text.lines()
        .map(str::trim)
        .filter(|line| !line.is_empty())
        .collect::<Vec<_>>()
        .join(" ")
}

fn summarize_history_text(text: &str) -> String {
    const LIMIT: usize = 160;
    if text.chars().count() <= LIMIT {
        text.to_string()
    } else {
        let shortened = text.chars().take(LIMIT - 3).collect::<String>();
        format!("{shortened}...")
    }
}

fn turn_is_in_progress(status: &str) -> bool {
    !matches!(status, "idle" | "finished" | "error" | "stuck")
}

fn turn_has_stopped(status: &str) -> bool {
    !turn_is_in_progress(status)
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use crate::opensymphony_openhands::OpenHandsError;
    use crate::opensymphony_workflow::WorkflowDefinition;
    use tempfile::TempDir;

    use super::{
        DebugCommandError, DebugRuntimeConfig, build_debug_client,
        should_rehydrate_after_attach_failure,
    };

    #[test]
    fn rehydrate_only_when_conversation_is_missing() {
        assert!(should_rehydrate_after_attach_failure(
            &OpenHandsError::HttpStatus {
                operation: "fetch conversation",
                status_code: 404,
                body: "missing".to_string(),
            }
        ));
        assert!(!should_rehydrate_after_attach_failure(
            &OpenHandsError::HttpStatus {
                operation: "fetch conversation",
                status_code: 401,
                body: "unauthorized".to_string(),
            }
        ));
        assert!(!should_rehydrate_after_attach_failure(
            &OpenHandsError::Transport {
                operation: "fetch conversation",
                detail: "connection refused".to_string(),
            }
        ));
    }

    #[test]
    fn build_debug_client_requires_tool_dir_for_managed_local_transport() {
        let runtime = sample_debug_runtime(None);

        let error = match build_debug_client(&runtime) {
            Err(error) => error,
            Ok(_) => panic!("managed-local debug should require tool_dir"),
        };

        assert!(matches!(error, DebugCommandError::MissingToolDir));
    }

    #[test]
    fn build_debug_client_reports_install_guidance_for_invalid_tooling() {
        let temp_dir = TempDir::new().expect("temp dir should exist");
        let tool_dir = temp_dir.path().join("missing/openhands-server");
        let runtime = sample_debug_runtime(Some(tool_dir.clone()));

        let error = match build_debug_client(&runtime) {
            Err(error) => error,
            Ok(_) => panic!("invalid tooling should be reported"),
        };

        match error {
            DebugCommandError::ToolingSetupRequired {
                tool_dir: reported,
                detail,
            } => {
                assert_eq!(reported, tool_dir);
                assert!(
                    detail.contains("required local OpenHands tooling file is missing"),
                    "tooling detail should explain the missing managed-local file: {detail}",
                );
            }
            other => panic!("expected tooling setup guidance, got {other}"),
        }
    }

    fn sample_debug_runtime(tool_dir: Option<PathBuf>) -> DebugRuntimeConfig {
        let temp_dir = TempDir::new().expect("temp dir should exist");
        let target_repo = temp_dir.path().join("target-repo");
        std::fs::create_dir_all(&target_repo).expect("target repo should exist");
        let workflow = WorkflowDefinition::parse(
            r#"---
tracker:
  kind: linear
  project_slug: sample-project
  active_states:
    - Todo
    - In Progress
  terminal_states:
    - Done
workspace:
  root: ./var/workspaces
openhands:
  transport:
    base_url: http://127.0.0.1:8000
---

# Debug Session
"#,
        )
        .expect("workflow should parse")
        .resolve(
            &target_repo,
            &super::super::DoctorWorkflowEnvironment {
                fallback_linear_api_key: true,
            },
        )
        .expect("workflow should resolve");

        DebugRuntimeConfig { workflow, tool_dir }
    }
}