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