Skip to main content

glass_dev/
lib.rs

1//! Glass Development Environment runtime.
2//!
3//! This crate owns the resident software-development workspace used by the
4//! `glass` product. Browser intelligence remains provided by the one-way
5//! `glass-browser` dependency.
6//!
7//! The interactive TUI separates terminal rendering from workspace work. A
8//! snapshot worker hydrates resident projections and executes governed browser,
9//! Git, agent, and project operations off the input loop. The UI applies
10//! versioned snapshots, keeps drafts and modal state local, and restores the
11//! terminal without waiting for an active bounded job.
12
13pub mod agents;
14pub mod browser;
15pub mod cli;
16pub mod customization;
17pub mod daemon;
18pub mod debugger;
19pub mod development;
20pub mod experiments;
21pub mod git;
22pub mod intelligence;
23pub mod kernels;
24pub mod lsp;
25pub mod mcp;
26pub mod pi_runtime;
27pub mod tasks;
28pub mod testing;
29pub mod tools;
30pub mod trust;
31pub mod tui;
32pub mod workspace;
33
34use glass_browser::cli::args::Cli;
35
36pub use agents::{
37    AgentEvent, AgentId, AgentRegistry, AgentSnapshot, AgentSpec, AgentStatus, ResidentAgentBroker,
38};
39pub use browser::{BrowserRuntimeState, BrowserService, BrowserStartConfig};
40pub use customization::{Customization, GlassConfig, Skill};
41pub use experiments::{
42    ExperimentComparison, ExperimentEvidence, ExperimentManager, ExperimentRanking,
43    ExperimentSnapshot, ExperimentState, ExperimentTrustPolicy, ExperimentWeights,
44};
45pub use intelligence::{
46    CausalPath, DevelopmentEdge, DevelopmentIntelligence, DevelopmentNode, DevelopmentNodeKind,
47    ObservableDevelopmentEvent, ObservableEventInput, ReplayDiff,
48};
49pub use lsp::{LanguageServerConfig, LanguageService, LanguageServiceEvent};
50pub use pi_runtime::{
51    PINNED_PI_SDK_VERSION, PiReadiness, PiReadinessComponent, PiReadinessState, PiSessionRequest,
52};
53pub use tasks::{
54    RetryPolicy, TaskBudget, TaskEvidence, TaskId, TaskScheduler, TaskSnapshot, TaskSpec,
55    TaskState, VerificationRequirement,
56};
57pub use tools::{DevelopmentToolContext, DevelopmentToolRouter};
58pub use trust::{LocalTrustDecision, WorkspaceIdentity, WorkspaceTrust, WorkspaceTrustStore};
59pub use workspace::{DevelopmentWorkspace, SharedDevelopmentWorkspace};
60
61/// Dispatch the full Glass Development Environment.
62///
63/// Resident development commands are handled here. Browser-only commands use
64/// the public browser runtime through the one-way crate dependency.
65pub async fn dispatch(cli: Cli) -> Result<(), Box<dyn std::error::Error>> {
66    if cli.mcp {
67        let backend = std::sync::Arc::new(mcp::DevelopmentMcpBackend::open(
68            std::env::current_dir()?,
69            cli.yolo,
70        )?);
71        return glass_browser::mcp::server::run_mcp_server_with_backend(&cli, backend).await;
72    }
73    if let Some(glass_browser::cli::args::Commands::Agent { action }) = &cli.command
74        && matches!(
75            action,
76            glass_browser::cli::args::AgentCommand::Tool { .. }
77                | glass_browser::cli::args::AgentCommand::ToolFile { .. }
78        )
79    {
80        let result = dispatch_external_tool(action, cli.yolo).await?;
81        println!("{}", serde_json::to_string_pretty(&result)?);
82        return Ok(());
83    }
84    if let Some(glass_browser::cli::args::Commands::Agent { action }) = &cli.command {
85        enforce_legacy_development_trust(&cli)?;
86        return cli::dispatch_agent(action, cli.yolo);
87    }
88    if let Some(glass_browser::cli::args::Commands::Daemon { action }) = &cli.command {
89        return daemon::dispatch(action).await;
90    }
91    if let Some(glass_browser::cli::args::Commands::Project { action }) = &cli.command {
92        enforce_legacy_development_trust(&cli)?;
93        return cli::dispatch_project(action);
94    }
95    if cli.command.is_none() && cli.prompt.is_none() && !cli.mcp {
96        return tui::run(std::env::current_dir()?, cli.tui_layout);
97    }
98    enforce_legacy_development_trust(&cli)?;
99    glass_browser::cli::runner::dispatch(cli).await
100}
101
102fn enforce_legacy_development_trust(cli: &Cli) -> Result<(), Box<dyn std::error::Error>> {
103    use glass_browser::cli::args::{AgentCommand, AgentHarness, Commands, ProjectCommand};
104
105    let (root, safe_static) = match cli.command.as_ref() {
106        Some(Commands::Project { action }) => {
107            let root = match action {
108                ProjectCommand::Inspect { root }
109                | ProjectCommand::Files { root }
110                | ProjectCommand::Search { root, .. }
111                | ProjectCommand::Read { root, .. }
112                | ProjectCommand::Edit { root, .. }
113                | ProjectCommand::Mkdir { root, .. }
114                | ProjectCommand::Rename { root, .. }
115                | ProjectCommand::Delete { root, .. }
116                | ProjectCommand::Diagnostics { root, .. }
117                | ProjectCommand::Run { root, .. }
118                | ProjectCommand::Test { root }
119                | ProjectCommand::Lint { root }
120                | ProjectCommand::Process { root, .. }
121                | ProjectCommand::Diff { root }
122                | ProjectCommand::Link { root, .. }
123                | ProjectCommand::Graph { root, .. }
124                | ProjectCommand::Breakpoint { root, .. }
125                | ProjectCommand::Timeline { root }
126                | ProjectCommand::Replay { root, .. }
127                | ProjectCommand::Neovim { root, .. }
128                | ProjectCommand::Experiment { root, .. }
129                | ProjectCommand::Attach { root, .. } => root,
130            };
131            let safe = matches!(
132                action,
133                ProjectCommand::Inspect { .. }
134                    | ProjectCommand::Files { .. }
135                    | ProjectCommand::Search { .. }
136                    | ProjectCommand::Read { .. }
137                    | ProjectCommand::Diff { .. }
138                    | ProjectCommand::Graph { .. }
139                    | ProjectCommand::Breakpoint { .. }
140                    | ProjectCommand::Timeline { .. }
141                    | ProjectCommand::Replay { .. }
142            );
143            (root, safe)
144        }
145        Some(Commands::Agent { action }) => {
146            let (root, safe) = match action {
147                AgentCommand::Doctor | AgentCommand::Setup { .. } | AgentCommand::Status => {
148                    return Ok(());
149                }
150                AgentCommand::Hello { root, harness } => {
151                    (root, matches!(harness, AgentHarness::Local))
152                }
153                AgentCommand::Prompt { root, harness, .. } => {
154                    (root, matches!(harness, AgentHarness::Local))
155                }
156                AgentCommand::Tool { .. } | AgentCommand::ToolFile { .. } => return Ok(()),
157                AgentCommand::Steer { root, .. }
158                | AgentCommand::FollowUp { root, .. }
159                | AgentCommand::Models { root }
160                | AgentCommand::SetModel { root, .. }
161                | AgentCommand::Thinking { root, .. }
162                | AgentCommand::Abort { root }
163                | AgentCommand::NewSession { root } => (root, false),
164            };
165            (root, safe)
166        }
167        _ => return Ok(()),
168    };
169    let workspace = DevelopmentWorkspace::open(root)?;
170    if workspace.trust() == WorkspaceTrust::Untrusted && !safe_static {
171        return Err(format!(
172            "repository-controlled development execution is blocked for {}; open the TUI to inspect and trust this workspace",
173            workspace.root().display()
174        )
175        .into());
176    }
177    Ok(())
178}
179
180async fn dispatch_external_tool(
181    action: &glass_browser::cli::args::AgentCommand,
182    unrestricted: bool,
183) -> Result<serde_json::Value, Box<dyn std::error::Error>> {
184    use crate::development::{Actor, ToolAuthorization, ToolCall};
185    use glass_browser::cli::args::AgentCommand;
186
187    let (call, root, allow_mutation, confirmed) = match action {
188        AgentCommand::Tool {
189            call,
190            root,
191            allow_mutation,
192            yes,
193        } => (
194            serde_json::from_str::<ToolCall>(call)?,
195            root,
196            *allow_mutation || unrestricted,
197            *yes || unrestricted,
198        ),
199        AgentCommand::ToolFile {
200            path,
201            root,
202            allow_mutation,
203            yes,
204        } if std::env::var_os("GLASS_DEV_DAEMON_SOCKET").is_some() => {
205            return daemon::forward_resident_tool_file(
206                path,
207                root,
208                *allow_mutation || unrestricted,
209                *yes || unrestricted,
210            )
211            .await;
212        }
213        AgentCommand::ToolFile {
214            path,
215            root,
216            allow_mutation,
217            yes,
218        } => (
219            daemon::read_private_tool_call(path)?,
220            root,
221            *allow_mutation || unrestricted,
222            *yes || unrestricted,
223        ),
224        _ => return Err("expected an external agent tool action".into()),
225    };
226    let mut workspace = DevelopmentWorkspace::open(root)?;
227    let context = DevelopmentToolContext {
228        authorization: ToolAuthorization {
229            actor: Actor::external("cli"),
230            allow_mutation,
231            confirmed,
232        },
233        initiator: None,
234        expected_generation: workspace.generation(),
235        expected_project_revision: workspace.project().revision(),
236    };
237    Ok(workspace.execute_tool(&call, &context)?)
238}