use crate::agents::AgentSpec;
use crate::browser::BrowserStartConfig;
use crate::debugger::{DebugAdapterConfig, SourceBreakpoint};
use crate::development::{
AgentToolGateway, DevelopmentError, DevelopmentResult, ToolAuthorization, ToolCall,
ToolDescriptor,
};
use crate::kernels::KernelKind;
use crate::pi_runtime::PiSessionRequest;
use crate::tasks::{TaskId, TaskSpec};
use crate::workspace::DevelopmentWorkspace;
use crate::{DevelopmentNode, DevelopmentNodeKind, ObservableEventInput, WorkspaceTrust};
use glass_browser::browser::session::{
SemanticObservationLevel, VerificationPredicate, WorkflowRecordingSession,
record_semantic_events,
};
use glass_browser::workspace::{WorkspaceId, WorkspaceStore};
use serde_json::Value;
use std::path::{Path, PathBuf};
use std::time::Duration;
const RESULT_LIMIT: usize = 512 * 1024;
#[derive(Debug, Clone)]
pub struct DevelopmentToolContext {
pub authorization: ToolAuthorization,
pub initiator: Option<crate::development::Actor>,
pub expected_generation: u64,
pub expected_project_revision: u64,
}
#[derive(Debug, Clone)]
pub struct DevelopmentToolRouter {
core: AgentToolGateway,
descriptors: Vec<ToolDescriptor>,
}
impl Default for DevelopmentToolRouter {
fn default() -> Self {
let core = AgentToolGateway::default();
let service = service_descriptors();
let mut descriptors = core
.descriptors()
.into_iter()
.filter(|descriptor| {
!service
.iter()
.any(|candidate| candidate.name == descriptor.name)
})
.collect::<Vec<_>>();
descriptors.extend(service);
Self { core, descriptors }
}
}
impl DevelopmentToolRouter {
pub fn with_customization(customization: &crate::Customization, trust: WorkspaceTrust) -> Self {
let mut router = Self::default();
let custom = customization.descriptors(trust);
router
.descriptors
.retain(|descriptor| !custom.iter().any(|item| item.name == descriptor.name));
router.descriptors.extend(custom);
router
}
pub fn descriptors(&self) -> Vec<ToolDescriptor> {
self.descriptors.clone()
}
pub fn execute(
&self,
workspace: &mut DevelopmentWorkspace,
call: &ToolCall,
context: &DevelopmentToolContext,
) -> DevelopmentResult<Value> {
if workspace.generation() != context.expected_generation {
return Err(DevelopmentError::Conflict(format!(
"stale workspace generation {}; current generation is {}",
context.expected_generation,
workspace.generation()
)));
}
if workspace.project().revision() != context.expected_project_revision {
return Err(DevelopmentError::Conflict(format!(
"stale project revision {}; current revision is {}",
context.expected_project_revision,
workspace.project().revision()
)));
}
let descriptor = self
.descriptors
.iter()
.find(|descriptor| descriptor.name == call.name)
.ok_or_else(|| DevelopmentError::NotFound(format!("tool {}", call.name)))?;
if !descriptor.available {
return Err(DevelopmentError::Conflict(
descriptor
.unavailable_reason
.clone()
.unwrap_or_else(|| format!("tool {} is unavailable", call.name)),
));
}
if workspace.trust() == WorkspaceTrust::Untrusted && !untrusted_tool_allowed(&call.name) {
return Err(DevelopmentError::Conflict(format!(
"tool {} is blocked until the workspace is trusted",
call.name
)));
}
if descriptor.mutating
&& (!context.authorization.allow_mutation || !context.authorization.confirmed)
{
return Err(DevelopmentError::Conflict(format!(
"tool {} requires mutation authority and confirmation",
call.name
)));
}
let actor_id = context.authorization.actor.id.clone();
let initiator_id = context
.initiator
.as_ref()
.map(|actor| actor.id.as_str())
.unwrap_or(actor_id.as_str());
let project_revision = workspace.project().revision();
let resource = format!("tool:{}", call.id);
let argument_bytes = serde_json::to_vec(&call.arguments)?.len();
workspace.intelligence_mut().upsert_node(DevelopmentNode {
id: resource.clone(),
kind: DevelopmentNodeKind::ToolCall,
label: call.name.clone(),
revision: project_revision,
stale: false,
evidence: serde_json::json!({
"toolCallId":call.id,
"name":call.name,
"mutating":descriptor.mutating,
"argumentBytes":argument_bytes
}),
})?;
workspace.intelligence_mut().link(
"repository:root",
&resource,
"receivedToolCall",
project_revision,
&actor_id,
serde_json::json!({"name":call.name,"initiator":initiator_id,"executor":actor_id}),
)?;
workspace.intelligence_mut().record(ObservableEventInput {
actor: &actor_id,
subsystem: "tool",
kind: "called",
resource: Some(&resource),
workspace_revision: project_revision,
evidence: serde_json::json!({"name":call.name,"mutating":descriptor.mutating,"initiator":initiator_id,"executor":actor_id}),
rationale: None,
})?;
if workspace.trust().permits_project_execution() {
workspace.customization().run_hooks(
"tool.before",
&serde_json::json!({"id":call.id,"name":call.name,"actor":&actor_id}),
workspace.trust(),
&actor_id,
)?;
}
let result = if service_descriptors()
.iter()
.any(|descriptor| descriptor.name == call.name)
{
self.execute_service(workspace, call, context)
} else if workspace.customization().custom_tool(&call.name).is_some() {
workspace.customization().execute_tool(
&call.name,
&call.arguments,
workspace.trust(),
&actor_id,
)
} else if let Some(name) = call.name.strip_prefix("glass.command.") {
workspace
.customization()
.execute_command(name, workspace.trust(), &actor_id)
} else {
self.core
.execute(workspace.project_mut(), call, &context.authorization)
};
match result {
Ok(result) => {
if workspace.trust().permits_project_execution() {
workspace.customization().run_hooks(
"tool.after",
&serde_json::json!({"id":call.id,"name":call.name,"actor":&actor_id,"ok":true}),
workspace.trust(),
&actor_id,
)?;
}
let result_bytes = serde_json::to_vec(&result)?.len();
if result_bytes > RESULT_LIMIT {
return Err(DevelopmentError::InvalidInput(format!(
"tool result exceeds the {RESULT_LIMIT} byte limit"
)));
}
let resulting_revision = workspace.project().revision();
project_observable_result(
workspace,
call,
&actor_id,
project_revision,
resulting_revision,
&result,
)?;
workspace.intelligence_mut().record(ObservableEventInput {
actor: &actor_id,
subsystem: "tool",
kind: "completed",
resource: Some(&resource),
workspace_revision: resulting_revision,
evidence: serde_json::json!({"name":call.name,"resultBytes":result_bytes}),
rationale: None,
})?;
Ok(result)
}
Err(error) => {
if workspace.trust().permits_project_execution() {
workspace.customization().run_hooks(
"tool.after",
&serde_json::json!({"id":call.id,"name":call.name,"actor":&actor_id,"ok":false,"error":error.to_string()}),
workspace.trust(),
&actor_id,
)?;
}
let resulting_revision = workspace.project().revision();
workspace.intelligence_mut().record(ObservableEventInput {
actor: &actor_id,
subsystem: "tool",
kind: "failed",
resource: Some(&resource),
workspace_revision: resulting_revision,
evidence: serde_json::json!({"name":call.name,"error":error.to_string()}),
rationale: None,
})?;
Err(error)
}
}
}
fn execute_service(
&self,
workspace: &mut DevelopmentWorkspace,
call: &ToolCall,
context: &DevelopmentToolContext,
) -> DevelopmentResult<Value> {
let string = |name: &str| required_string(call, name);
let actor = normalized_actor_id(&context.authorization.actor.id);
match call.name.as_str() {
"glass.agent.delegate" => {
let sandbox = crate::external_agents::ExternalSandbox::parse(
optional_string(call, "sandbox").unwrap_or("read-only"),
)
.map_err(DevelopmentError::InvalidInput)?;
if sandbox == crate::external_agents::ExternalSandbox::WorkspaceWrite
&& (!context.authorization.allow_mutation
|| !context.authorization.confirmed)
{
return Err(DevelopmentError::Conflict(
"workspace-write delegation requires explicit mutation authority and confirmation"
.into(),
));
}
let timeout = call
.arguments
.get("timeoutSeconds")
.and_then(Value::as_u64)
.unwrap_or(crate::external_agents::DEFAULT_TIMEOUT_SECS);
let result = crate::external_agents::delegate(
crate::external_agents::ExternalAgentRequest {
harness: string("harness")?.into(),
root: workspace.root().to_path_buf(),
prompt: string("prompt")?.into(),
sandbox,
timeout: Duration::from_secs(timeout),
allow_mutation: context.authorization.allow_mutation
&& context.authorization.confirmed,
},
)
.map_err(DevelopmentError::Process)?;
Ok(serde_json::to_value(result)?)
}
"glass.editor.open" => map_service(
workspace
.project_mut()
.open_buffer(string("path")?, context.authorization.actor.clone()),
),
"glass.editor.selection" => {
let path = string("path")?;
let buffer = workspace
.project()
.buffer(path)
.cloned()
.ok_or_else(|| DevelopmentError::NotFound(format!("buffer {path}")))?;
let selected_text = workspace.project().selected_editor_text(path)?;
Ok(serde_json::json!({
"path":buffer.path,
"line":buffer.cursor_line,
"column":buffer.cursor_column,
"selection":buffer.selection,
"selectedText":selected_text,
"actor":buffer.actor,
"dirty":buffer.dirty
}))
}
"glass.editor.replace_selection" => {
let buffer = workspace.project_mut().replace_buffer_selection(
string("path")?,
string("replacement")?.to_string(),
context.authorization.actor.clone(),
)?;
Ok(serde_json::to_value(buffer)?)
}
"glass.editor.replace" => {
let matches = workspace.project_mut().replace_in_buffer(
string("path")?,
string("oldText")?,
string("newText")?,
context.authorization.actor.clone(),
)?;
Ok(serde_json::json!({"replacements":matches}))
}
"glass.editor.save" => {
map_service(workspace.project_mut().save_buffer(string("path")?))
}
"glass.editor.diff" => map_service(workspace.project_mut().diff()),
"glass.editor.buffers" => Ok(serde_json::to_value(
workspace.project().buffers().cloned().collect::<Vec<_>>(),
)?),
"glass.editor.comments" => Ok(serde_json::to_value(
workspace.project().editor_comments(optional_string(call, "path")),
)?),
"glass.editor.proposals" => Ok(serde_json::to_value(
workspace.project().editor_proposals(),
)?),
"glass.editor.fim" => {
let provider = crate::fim::FimProvider::from_editor(
&workspace.customization().config().editor,
)
.ok_or_else(|| {
DevelopmentError::NotFound("FIM is disabled".into())
})?;
match provider.backend {
crate::fim::FimBackend::Stub => {
let text = provider.complete(
string("prefix")?,
optional_string(call, "suffix").unwrap_or(""),
)
.map_err(DevelopmentError::InvalidInput)?;
Ok(serde_json::json!({"text": text, "backend": "stub"}))
}
crate::fim::FimBackend::Pi => {
let prefix = string("prefix")?.to_string();
let suffix = optional_string(call, "suffix")
.unwrap_or("")
.to_string();
let snapshots = workspace.agents().list()?;
let id = snapshots
.into_iter()
.find(|item| item.status == crate::AgentStatus::Idle)
.map(|item| item.id)
.ok_or_else(|| {
DevelopmentError::NotFound(
"no idle Pi session for FIM; start an agent first".into(),
)
})?;
workspace.agents().request(
&id,
crate::pi_runtime::PiSessionRequest::Complete { prefix, suffix },
)?;
Ok(serde_json::json!({
"queued": true,
"backend": "pi",
"agentId": id.as_str(),
}))
}
}
},
"glass.editor.checkpoints" => Ok(serde_json::to_value(
workspace.project().editor_checkpoints(),
)?),
"glass.editor.comment.add" => map_service(workspace.project_mut().add_editor_comment(
string("path")?,
u32_value(call, "startLine")?,
u32_value(call, "endLine")?,
string("text")?.to_string(),
context.authorization.actor.clone(),
)),
"glass.editor.comment.resolve" => map_service(
workspace
.project_mut()
.resolve_editor_comment(string("id")?, context.authorization.actor.clone()),
),
"glass.editor.proposal.create" => map_service(
workspace.project_mut().propose_editor_change(
string("path")?,
string("original")?.to_string(),
string("proposed")?.to_string(),
string("summary")?.to_string(),
context.authorization.actor.clone(),
),
),
"glass.editor.proposal.accept" => map_service(
workspace
.project_mut()
.accept_editor_proposal(string("id")?, context.authorization.actor.clone()),
),
"glass.editor.proposal.accept_pack" => map_service(
workspace
.project_mut()
.accept_pending_editor_proposals(context.authorization.actor.clone()),
),
"glass.editor.proposal.reject" => map_service(
workspace
.project_mut()
.reject_editor_proposal(string("id")?, context.authorization.actor.clone()),
),
"glass.editor.checkpoint.create" => map_service(
workspace
.project_mut()
.create_editor_checkpoint(
string("name")?.to_string(),
context.authorization.actor.clone(),
),
),
"glass.editor.checkpoint.restore" => map_service(
workspace
.project_mut()
.restore_editor_checkpoint(string("id")?, context.authorization.actor.clone()),
),
"glass.process.start" => map_service(workspace.project_mut().start_process(
string("name")?,
required_string(call, "command")?,
)),
"glass.process.stop" => {
map_service(workspace.project_mut().stop_process(string("name")?))
}
"glass.process.logs" => {
let name = string("name")?;
let processes = workspace.project_mut().processes();
processes.poll()?;
Ok(serde_json::json!({
"name": name,
"output": processes.output(name)?,
}))
}
"glass.process.list" => {
map_service(workspace.project_mut().processes().list_checked())
}
"glass.process.restart" => {
map_service(workspace.project_mut().processes().restart(string("name")?))
}
"glass.process.input" => {
workspace
.project_mut()
.processes()
.send_input(string("name")?, string("input")?)?;
Ok(serde_json::json!({"sent":true}))
}
"glass.process.resize" => {
let cols = u16_value(call, "cols")?;
let rows = u16_value(call, "rows")?;
workspace
.project_mut()
.processes()
.resize(string("name")?, cols, rows)?;
Ok(serde_json::json!({"resized":true,"cols":cols,"rows":rows}))
}
"glass.process.health" => {
let name = string("name")?;
let snapshot = workspace
.project_mut()
.processes()
.list_checked()?
.into_iter()
.find(|snapshot| snapshot.name == name)
.ok_or_else(|| DevelopmentError::NotFound(format!("process {name}")))?;
Ok(serde_json::json!({
"name":snapshot.name,
"pid":snapshot.pid,
"state":snapshot.state,
"health":snapshot.health,
"detectedUrls":snapshot.detected_urls
}))
}
"glass.process.ports" => {
let snapshots = workspace.project_mut().processes().list_checked()?;
Ok(serde_json::json!({
"processes":snapshots.into_iter().map(|snapshot| serde_json::json!({
"name":snapshot.name,
"pid":snapshot.pid,
"urls":snapshot.detected_urls
})).collect::<Vec<_>>()
}))
}
"glass.browser.start" => {
let config: BrowserStartConfig = serde_json::from_value(call.arguments.clone())?;
workspace.browser().start(config)
}
"glass.browser.stop" => workspace.browser().stop(),
"glass.browser.attach" => {
let mut config: BrowserStartConfig = serde_json::from_value(call.arguments.clone())?;
config.attach = true;
workspace.browser().start(config)
}
"glass.browser.reconnect" => workspace.browser().reconnect(),
"glass.browser.state" => workspace.browser().state(),
"glass.browser.observe" => workspace.browser().observe(),
"glass.browser.verify" => {
let predicate: VerificationPredicate = serde_json::from_value(
required_value(call, "predicate")?.clone(),
)
.map_err(|error| {
DevelopmentError::InvalidInput(format!(
"glass.browser.verify predicate is invalid: {error}"
))
})?;
let timeout = timeout(call, 30)?.unwrap_or(Duration::from_secs(8));
workspace.browser().verify_predicate(predicate, timeout)
}
"glass.browser.snapshot" => workspace.browser().snapshot(),
"glass.browser.semantic" => workspace.browser().semantic(match optional_string(call, "level").unwrap_or("structured") {
"summary" => SemanticObservationLevel::Summary,
"interactive" => SemanticObservationLevel::Interactive,
"structured" => SemanticObservationLevel::Structured,
"detailed" => SemanticObservationLevel::Detailed,
"raw" => SemanticObservationLevel::Raw,
_ => return Err(DevelopmentError::InvalidInput("browser semantic level must be summary, interactive, structured, detailed, or raw".into())),
}),
"glass.browser.web_ir" => workspace.browser().web_ir(),
"glass.browser.diff" => workspace.browser().diff(),
"glass.browser.targets" => workspace.browser().targets(),
"glass.browser.target.select" => workspace
.browser()
.select_target(string("targetId")?.into()),
"glass.browser.navigate" => workspace.browser().navigate(
string("url")?.into(),
unsigned(call, "browserRevision", 0)?,
timeout(call, 180)?.unwrap_or(Duration::from_secs(30)),
),
"glass.browser.act" => {
let revision = unsigned(call, "browserRevision", 0)?;
match string("action")? {
"back" => workspace.browser().back(revision),
"forward" => workspace.browser().forward(revision),
"reload" => workspace.browser().reload(revision),
"stopLoading" => workspace.browser().stop_loading(revision),
"click" => workspace
.browser()
.click(string("target")?.into(), revision),
"type" => workspace.browser().type_text(
string("text")?.into(),
optional_string(call, "target").map(str::to_string),
revision,
),
"scroll" => workspace.browser().scroll(
number(call, "dx", 0.0)?,
number(call, "dy", 0.0)?,
revision,
),
action => Err(DevelopmentError::InvalidInput(format!(
"unsupported browser action {action}; expected back, forward, reload, stopLoading, click, type, or scroll"
))),
}
}
"glass.browser.screenshot" => workspace.browser().screenshot(),
"glass.browser.remote-view.open" => workspace.browser().open_remote_view(),
"glass.browser.remote-view.status" => workspace.browser().remote_view_status(),
"glass.browser.remote-view.revoke" => workspace.browser().revoke_remote_view(),
"glass.workflow.list" => workspace.browser().list_workflows(),
"glass.workflow.run" => workspace.browser().run_workflow(
required_value(call, "definition")?.clone(),
value_map(call, "inputs")?,
),
"glass.workflow.pause" => workspace.browser().pause_workflow(),
"glass.workflow.resume" => workspace.browser().resume_workflow(
required_value(call, "definition")?.clone(),
value_map(call, "inputs")?,
required_value(call, "checkpoint")?.clone(),
),
"glass.workflow.cancel" => workspace.browser().cancel_workflow(),
"glass.workflow.verify" => workspace.browser().verify_workflow(),
"glass.workflow.record" => {
let session: WorkflowRecordingSession = serde_json::from_value(
required_value(call, "session")?.clone(),
)?;
Ok(serde_json::to_value(record_semantic_events(session).map_err(|error| {
DevelopmentError::InvalidInput(error.to_string())
})?)?)
}
"glass.memory.retrieve" => {
if let Some(record_id) = optional_string(call, "recordId") {
Ok(serde_json::to_value(
workspace
.knowledge()
.get(record_id)
.ok_or_else(|| DevelopmentError::NotFound(format!("memory record {record_id}")))?,
)?)
} else {
Ok(serde_json::json!({
"records":workspace.knowledge().records(),
"stats":workspace.knowledge().stats().map_err(|error| DevelopmentError::Process(error.to_string()))?
}))
}
}
"glass.memory.explain" => {
let record_id = string("recordId")?;
let record = workspace
.knowledge()
.get(record_id)
.ok_or_else(|| DevelopmentError::NotFound(format!("memory record {record_id}")))?;
Ok(serde_json::json!({
"record":record,
"advisory":true,
"authority":"fresh browser evidence remains authoritative"
}))
}
"glass.memory.forget" => {
let record_id = string("recordId")?;
let change = workspace
.knowledge_mut()
.remove(record_id)
.map_err(|error| DevelopmentError::Process(error.to_string()))?;
Ok(serde_json::to_value(change)?)
}
"glass.semantic.diff" => workspace.browser().diff(),
"glass.semantic.links" => Ok(serde_json::json!({
"nodes":workspace.intelligence().nodes().collect::<Vec<_>>(),
"edges":workspace.intelligence().edges().collect::<Vec<_>>()
})),
"glass.github.review" => Ok(serde_json::to_value(crate::github::review(
workspace.root(),
)?)?),
"glass.github.ship" => {
let request: crate::github::GitHubShipRequest =
serde_json::from_value(call.arguments.clone())?;
Ok(serde_json::to_value(crate::github::ship(
workspace.root(),
&request,
)?)?)
}
"glass.git.status" => git_value(workspace.git(), |git| git.status()),
"glass.git.diff" => git_value(workspace.git(), |git| {
git.diff(
boolean(call, "staged", false),
optional_string(call, "path"),
)
}),
"glass.git.stage" => {
let paths = string_array(call, "paths")?;
git_mutation(workspace.git(), |git| git.stage(&paths))?;
Ok(serde_json::json!({"staged":paths}))
}
"glass.git.unstage" => {
let paths = string_array(call, "paths")?;
git_mutation(workspace.git(), |git| git.unstage(&paths))?;
Ok(serde_json::json!({"unstaged":paths}))
}
"glass.git.discard" => {
let paths = string_array(call, "paths")?;
git_mutation(workspace.git(), |git| git.discard(&paths))?;
Ok(serde_json::json!({"discarded":paths}))
}
"glass.git.push" => {
git_mutation(workspace.git(), |git| {
git.push(optional_string(call, "remote"), optional_string(call, "branch"))
})?;
Ok(serde_json::json!({"pushed":true}))
}
"glass.git.fetch" => {
git_mutation(workspace.git(), |git| {
git.fetch(optional_string(call, "remote"))
})?;
Ok(serde_json::json!({"fetched":true}))
}
"glass.git.pull" => {
git_mutation(workspace.git(), |git| {
git.pull(optional_string(call, "remote"), optional_string(call, "branch"))
})?;
Ok(serde_json::json!({"pulled":true}))
}
"glass.git.merge" => {
let branch = string("branch")?;
git_mutation(workspace.git(), |git| git.merge(branch))?;
Ok(serde_json::json!({"merged":branch}))
}
"glass.git.rebase" => {
let onto = string("onto")?;
git_mutation(workspace.git(), |git| git.rebase(onto))?;
Ok(serde_json::json!({"rebased":onto}))
}
"glass.git.commit" => {
let message = string("message")?;
git_value(workspace.git(), |git| git.commit(message))
}
"glass.git.branches" => git_value(workspace.git(), |git| git.branches()),
"glass.git.branch.create" => {
let name = string("name")?;
let start_point = optional_string(call, "startPoint");
git_mutation(workspace.git(), |git| git.create_branch(name, start_point))?;
Ok(serde_json::json!({"created":name}))
}
"glass.git.branch.switch" => {
let name = string("name")?;
let create = boolean(call, "create", false);
git_mutation(workspace.git(), |git| git.switch_branch(name, create))?;
Ok(serde_json::json!({"current":name}))
}
"glass.git.blame" => {
let path = string("path")?;
let start = unsigned(call, "startLine", 1)?;
let end = unsigned(call, "endLine", 1)?;
git_value(workspace.git(), |git| git.blame(path, start, end))
}
"glass.git.conflicts" => git_value(workspace.git(), |git| git.conflicts()),
"glass.git.stash.list" => git_value(workspace.git(), |git| git.stash_list()),
"glass.git.stash.push" => {
let message = string("message")?;
let include_untracked = boolean(call, "includeUntracked", false);
git_mutation(workspace.git(), |git| {
git.stash_push(message, include_untracked)
})?;
Ok(serde_json::json!({"stashed":true}))
}
"glass.git.stash.pop" => {
let reference = string("reference")?;
git_mutation(workspace.git(), |git| git.stash_pop(reference))?;
Ok(serde_json::json!({"restored":reference}))
}
"glass.git.worktree.list" => git_value(workspace.git(), |git| git.worktrees()),
"glass.git.worktree.create" => {
let path = confined_worktree_path(workspace.root(), Path::new(string("path")?))?;
let branch = string("branch")?;
let create_branch = boolean(call, "createBranch", true);
git_mutation(workspace.git(), |git| {
git.create_worktree(&path, branch, create_branch)
})?;
Ok(serde_json::json!({"path":path,"created":true}))
}
"glass.git.worktree.remove" => {
let path = confined_worktree_path(workspace.root(), Path::new(string("path")?))?;
git_mutation(workspace.git(), |git| {
git.remove_worktree(&path, boolean(call, "force", false))
})?;
Ok(serde_json::json!({"path":path,"removed":true}))
}
"glass.test.discover" => Ok(serde_json::to_value(
workspace.tests().suites().collect::<Vec<_>>(),
)?),
"glass.test.run" => {
let revision = workspace.project().revision();
map_service(workspace.tests_mut().start(
string("runId")?,
string("suiteId")?,
&actor,
revision,
timeout(call, 3_600)?,
))
}
"glass.test.run-affected" => {
let paths = string_array(call, "changedPaths")?;
let suite_ids = workspace
.tests()
.affected_suites(paths.iter().map(String::as_str))
.into_iter()
.map(|suite| suite.id.clone())
.collect::<Vec<_>>();
let revision = workspace.project().revision();
let prefix = optional_string(call, "runPrefix").unwrap_or("affected");
let mut runs = Vec::new();
for (index, suite_id) in suite_ids.into_iter().enumerate() {
runs.push(map_service(workspace.tests_mut().start(
&format!("{prefix}-{index}"),
&suite_id,
&actor,
revision,
timeout(call, 3_600)?,
))?);
}
Ok(serde_json::to_value(runs)?)
}
"glass.test.cancel" => {
map_service(workspace.tests_mut().cancel(string("runId")?))?;
Ok(serde_json::json!({"cancelled":string("runId")?}))
}
"glass.test.results" => {
let finished = map_service(workspace.tests_mut().poll())?;
Ok(serde_json::json!({
"finished":finished,
"running":workspace.tests().running().collect::<Vec<_>>(),
"results":workspace.tests().results().collect::<Vec<_>>()
}))
}
"glass.test.watch" => {
let revision = workspace.project().revision();
map_service(workspace.tests_mut().watch(string("suiteId")?, revision))?;
Ok(serde_json::json!({"watching":string("suiteId")?}))
}
"glass.eval.start" => {
let kind = parse_kernel_kind(string("kind")?)?;
let name = string("name")?;
let capabilities = string_array_or_empty(call, "capabilities")?;
let mutation_authority = boolean(call, "mutationAuthority", false)
&& context.authorization.allow_mutation
&& context.authorization.confirmed;
map_service(workspace.kernels_mut().start_governed(
name,
kind,
&format!("kernel:{name}"),
&capabilities,
mutation_authority,
))?;
Ok(serde_json::json!({"started":name,"kind":kind,"capabilities":capabilities,"mutationAuthority":mutation_authority}))
}
"glass.eval.execute" => {
map_service(workspace.execute_kernel(
string("name")?,
string("code")?,
&context.authorization,
timeout(call, 600)?,
))
}
"glass.eval.list" => Ok(serde_json::to_value(
workspace.kernels().snapshots().collect::<Vec<_>>(),
)?),
"glass.eval.reset" => {
map_service(workspace.kernels_mut().reset(string("name")?, &actor))?;
Ok(serde_json::json!({"reset":string("name")?}))
}
"glass.eval.cancel" => map_service(workspace.kernels_mut().cancel(string("name")?)),
"glass.eval.stop" => map_service(workspace.kernels_mut().stop(string("name")?)),
"glass.lsp.start" => {
let server = string("server")?;
let configured = workspace.customization().config().lsp.get(server).cloned();
let config = match (optional_string(call, "command"), configured) {
(Some(command), _) => crate::lsp::LanguageServerConfig {
command: command.to_string(),
arguments: string_array_or_empty(call, "arguments")?,
},
(None, Some(config)) => crate::lsp::LanguageServerConfig {
command: config.command,
arguments: config.args,
},
(None, None) => return Err(DevelopmentError::InvalidInput(
"lsp start requires command or a matching glass.toml lsp entry".into(),
)),
};
map_service(workspace.language().start(server, &config))?;
Ok(serde_json::json!({"started":server}))
}
"glass.lsp.stop" => {
map_service(workspace.language().stop(string("server")?))?;
Ok(serde_json::json!({"stopped":string("server")?}))
}
"glass.lsp.list" => Ok(serde_json::json!({
"servers":workspace.language().names().collect::<Vec<_>>()
})),
"glass.lsp.events" => Ok(serde_json::to_value(
workspace.language().events(unsigned(call, "since", 0)?),
)?),
"glass.lsp.raw" => map_service(workspace.language().raw_request(
string("server")?,
&actor,
string("method")?,
call.arguments
.get("params")
.cloned()
.unwrap_or_else(|| serde_json::json!({})),
timeout(call, 60)?.unwrap_or(Duration::from_secs(10)),
)),
"glass.lsp.diagnostics" => map_service(workspace.language().diagnostics(
string("server")?,
&actor,
string("path")?,
)),
"glass.lsp.hover" => map_service(workspace.language().hover(
string("server")?,
&actor,
string("path")?,
unsigned(call, "line", 1)? as u32,
unsigned(call, "character", 1)? as u32,
)),
"glass.lsp.completion" => map_service(workspace.language().completion(
string("server")?,
&actor,
string("path")?,
unsigned(call, "line", 1)? as u32,
unsigned(call, "character", 1)? as u32,
)),
"glass.lsp.definition" => map_service(workspace.language().definition(
string("server")?,
&actor,
string("path")?,
u32_value(call, "line")?,
u32_value(call, "character")?,
)),
"glass.lsp.declaration" => map_service(workspace.language().declaration(
string("server")?,
&actor,
string("path")?,
u32_value(call, "line")?,
u32_value(call, "character")?,
)),
"glass.lsp.implementation" => map_service(workspace.language().implementation(
string("server")?,
&actor,
string("path")?,
u32_value(call, "line")?,
u32_value(call, "character")?,
)),
"glass.lsp.references" => map_service(workspace.language().references(
string("server")?,
&actor,
string("path")?,
u32_value(call, "line")?,
u32_value(call, "character")?,
)),
"glass.lsp.document_symbols" => map_service(workspace.language().document_symbols(
string("server")?,
&actor,
string("path")?,
)),
"glass.lsp.workspace_symbols" => map_service(workspace.language().workspace_symbols(
string("server")?,
&actor,
string("query")?,
)),
"glass.lsp.inlay_hints" => map_service(workspace.language().inlay_hints(
string("server")?,
&actor,
string("path")?,
)),
"glass.lsp.signature_help" => map_service(workspace.language().signature_help(
string("server")?,
&actor,
string("path")?,
u32_value(call, "line")?,
u32_value(call, "character")?,
)),
"glass.lsp.code_actions" => map_service(workspace.language().code_actions(
string("server")?,
&actor,
string("path")?,
position(call, "start")?,
position(call, "end")?,
value_array_or_empty(call, "diagnostics")?,
)),
"glass.lsp.formatting" => map_service(workspace.language().formatting(
string("server")?,
&actor,
string("path")?,
)),
"glass.lsp.range_formatting" => map_service(workspace.language().range_formatting(
string("server")?,
&actor,
string("path")?,
position(call, "start")?,
position(call, "end")?,
)),
"glass.lsp.semantic_tokens" => map_service(workspace.language().semantic_tokens(
string("server")?,
&actor,
string("path")?,
)),
"glass.lsp.rename" => map_service(workspace.language().rename(
string("server")?,
&actor,
string("path")?,
u32_value(call, "line")?,
u32_value(call, "character")?,
string("newName")?,
)),
"glass.debug.start" => {
let session = string("session")?;
let configured = workspace.customization().config().dap.get(session).cloned();
let (mut config, configured_tcp, configured_connect_timeout_ms) =
match (optional_string(call, "command"), configured) {
(Some(command), _) => (
DebugAdapterConfig::new(
command,
string_array_or_empty(call, "arguments")?,
),
None,
5_000,
),
(None, Some(config)) => (
DebugAdapterConfig::new(config.command, config.args),
config.tcp_address,
config.connect_timeout_ms,
),
(None, None) => return Err(DevelopmentError::InvalidInput(
"debug start requires command or a matching glass.toml dap entry".into(),
)),
};
if let Some(address) = optional_string(call, "tcpAddress")
.map(str::to_string)
.or(configured_tcp)
{
let address = address.parse().map_err(|_| {
DevelopmentError::InvalidInput(
"debug tcpAddress must be a loopback IP socket address".into(),
)
})?;
config = config.with_tcp(
address,
Duration::from_millis(unsigned(
call,
"connectTimeoutMs",
configured_connect_timeout_ms,
)?),
);
}
map_debug(workspace.start_debugger(
session,
&config,
Duration::from_secs(unsigned(call, "timeoutSeconds", 30)?),
))?;
Ok(serde_json::json!({"started":session}))
}
"glass.debug.launch" => map_debug(
debugger(workspace, string("session")?)?.launch(
call.arguments
.get("configuration")
.cloned()
.unwrap_or(Value::Null),
),
),
"glass.debug.attach" => map_debug(
debugger(workspace, string("session")?)?.attach(
call.arguments
.get("configuration")
.cloned()
.unwrap_or(Value::Null),
),
),
"glass.debug.configuration_done" => {
map_debug(debugger(workspace, string("session")?)?.configuration_done())
}
"glass.debug.restart" => map_debug(
debugger(workspace, string("session")?)?.restart(
call.arguments
.get("configuration")
.cloned()
.unwrap_or_else(|| serde_json::json!({})),
),
),
"glass.debug.breakpoint.set" => {
let lines = unsigned_array(call, "lines")?;
let breakpoints = lines
.iter()
.copied()
.map(SourceBreakpoint::line)
.collect::<Vec<_>>();
map_debug(
debugger(workspace, string("session")?)?
.set_source_breakpoints(&PathBuf::from(string("path")?), &breakpoints),
)
}
"glass.debug.breakpoint.remove" => map_debug(
debugger(workspace, string("session")?)?
.set_source_breakpoints(&PathBuf::from(string("path")?), &[]),
),
"glass.debug.exception.set" => map_debug(
debugger(workspace, string("session")?)?
.set_exception_breakpoints(&string_array_or_empty(call, "filters")?),
),
"glass.debug.continue" => map_debug(
debugger(workspace, string("session")?)?
.continue_thread(integer(call, "threadId")?),
),
"glass.debug.pause" => map_debug(
debugger(workspace, string("session")?)?.pause(integer(call, "threadId")?),
),
"glass.debug.step" => {
let debugger = debugger(workspace, string("session")?)?;
let thread = integer(call, "threadId")?;
match string("kind")? {
"over" => map_debug(debugger.next(thread)),
"in" => map_debug(debugger.step_in(thread)),
"out" => map_debug(debugger.step_out(thread)),
_ => Err(DevelopmentError::InvalidInput(
"debug step kind must be over, in, or out".into(),
)),
}
}
"glass.debug.threads" => map_debug(debugger(workspace, string("session")?)?.threads()),
"glass.debug.stack" => map_debug(
debugger(workspace, string("session")?)?.stack_trace(integer(call, "threadId")?),
),
"glass.debug.scopes" => map_debug(
debugger(workspace, string("session")?)?.scopes(integer(call, "frameId")?),
),
"glass.debug.variables" => map_debug(
debugger(workspace, string("session")?)?
.variables(integer(call, "variablesReference")?),
),
"glass.debug.evaluate" => map_debug(debugger(workspace, string("session")?)?.evaluate(
string("expression")?,
call.arguments.get("frameId").and_then(Value::as_i64),
optional_string(call, "context").unwrap_or("repl"),
)),
"glass.debug.events" => {
map_debug(debugger(workspace, string("session")?)?.poll_events())
}
"glass.debug.inspect" => {
map_debug(debugger(workspace, string("session")?)?.snapshot())
}
"glass.debug.processes" => map_debug(
debugger(workspace, string("session")?)?.debuggee_processes(),
),
"glass.debug.disconnect" => map_debug(
debugger(workspace, string("session")?)?
.disconnect(boolean(call, "terminateDebuggee", false)),
),
"glass.debug.terminate" => map_debug(
debugger(workspace, string("session")?)?
.terminate(boolean(call, "restart", false)),
),
"glass.debug.stop" => {
map_debug(workspace.stop_debugger(string("session")?))?;
Ok(serde_json::json!({"stopped":string("session")?}))
}
"glass.agent.list" => map_service(workspace.agents().list()),
"glass.agent.setup" => map_service(crate::pi_runtime::setup_pi_runtime(
None,
None,
boolean(call, "update", false),
boolean(call, "login", false),
)),
"glass.agent.spawn" => {
let spec: AgentSpec =
serde_json::from_value(call.arguments.get("spec").cloned().ok_or_else(
|| DevelopmentError::InvalidInput("agent spawn requires spec".into()),
)?)?;
map_service(workspace.agents().create(spec))
}
"glass.agent.prompt" => {
let id = agent_id(workspace, string("agentId")?)?;
let text = string("text")?;
map_service(workspace.agents().prompt_with_context(
&id,
text,
call.arguments.get("context").cloned(),
))?;
Ok(serde_json::json!({"queued":true}))
}
"glass.agent.send" => {
let text = string("text")?;
let context = call.arguments.get("context").cloned();
let mode = optional_string(call, "mode").unwrap_or("follow-up");
let selected = optional_string(call, "agentId")
.map(|value| agent_id(workspace, value))
.transpose()?;
let agent = selected.or_else(|| {
workspace
.agents()
.list()
.ok()?
.into_iter()
.find(|item| {
!matches!(
item.status,
crate::AgentStatus::Completed
| crate::AgentStatus::Failed
| crate::AgentStatus::Cancelled
)
})
.map(|item| item.id)
});
let agent = match agent {
Some(agent) => agent,
None => {
let readiness = crate::pi_runtime::pi_readiness()?;
if !readiness.ready {
return Err(DevelopmentError::Conflict(
"Pi is not ready; use Agent [s] to install the runtime and [l] to sign in"
.into(),
));
}
let id = workspace
.agents()
.create(AgentSpec::new(
"assistant",
"interactive Glass Agent session",
))?;
map_service(workspace.agents().prompt_with_context(&id, text, context))?;
return Ok(serde_json::json!({"queued":true,"agentId":id}));
}
};
let mut status = workspace.agents().snapshot(&agent)?.status;
let restarted = matches!(
status,
crate::AgentStatus::Failed | crate::AgentStatus::Cancelled
);
if restarted {
workspace.agents().restart(&agent)?;
status = workspace.agents().snapshot(&agent)?.status;
}
if mode == "steer" && status == crate::AgentStatus::Working {
map_service(workspace.agents().steer_with_context(&agent, text, context.clone()))?;
} else if mode == "follow-up" && status == crate::AgentStatus::Working {
map_service(workspace.agents().follow_up_with_context(&agent, text, context.clone()))?;
} else {
map_service(workspace.agents().prompt_with_context(&agent, text, context))?;
}
let mut response = serde_json::json!({"queued":true,"agentId":agent});
if restarted {
response["restarted"] = Value::Bool(true);
}
Ok(response)
}
"glass.agent.steer" => {
let id = agent_id(workspace, string("agentId")?)?;
let text = string("text")?;
map_service(workspace.agents().steer_with_context(
&id,
text,
call.arguments.get("context").cloned(),
))?;
Ok(serde_json::json!({"queued":true}))
}
"glass.agent.follow-up" => {
let id = agent_id(workspace, string("agentId")?)?;
let text = string("text")?;
map_service(workspace.agents().follow_up_with_context(
&id,
text,
call.arguments.get("context").cloned(),
))?;
Ok(serde_json::json!({"queued":true}))
}
"glass.agent.approve" => {
let id = agent_id(workspace, string("agentId")?)?;
map_service(workspace.agents().approve_tool(
&id,
string("frameId")?,
boolean(call, "approved", false),
))?;
Ok(serde_json::json!({"queued":true}))
}
"glass.agent.abort" => {
let id = agent_id(workspace, string("agentId")?)?;
map_service(workspace.agents().cancel(&id))?;
Ok(serde_json::json!({"cancelled":true}))
}
"glass.agent.compact" => {
let id = agent_id(workspace, string("agentId")?)?;
map_service(workspace.agents().request(
&id,
PiSessionRequest::Compact {
instructions: optional_string(call, "instructions").map(str::to_string),
},
))?;
Ok(serde_json::json!({"queued":true}))
}
"glass.agent.model" => {
let id = agent_id(workspace, string("agentId")?)?;
map_service(workspace.agents().request(
&id,
PiSessionRequest::SetModel {
provider: string("provider")?.into(),
model_id: string("modelId")?.into(),
},
))?;
Ok(serde_json::json!({"queued":true}))
}
"glass.agent.thinking" => {
let id = agent_id(workspace, string("agentId")?)?;
map_service(workspace.agents().request(
&id,
PiSessionRequest::SetThinking {
level: string("level")?.into(),
},
))?;
Ok(serde_json::json!({"queued":true}))
}
"glass.agent.new-session" => {
agent_request(workspace, call, PiSessionRequest::NewSession)
}
"glass.agent.clone-session" => {
agent_request(workspace, call, PiSessionRequest::CloneSession)
}
"glass.agent.rewind" => agent_request(
workspace,
call,
PiSessionRequest::Rewind {
entry_id: string("entryId")?.into(),
},
),
"glass.agent.fork" => agent_request(
workspace,
call,
PiSessionRequest::Fork {
entry_id: string("entryId")?.into(),
},
),
"glass.agent.switch-session" => agent_request(
workspace,
call,
PiSessionRequest::SwitchSession {
path: string("path")?.into(),
},
),
"glass.agent.sessions" => agent_request(workspace, call, PiSessionRequest::ListSessions),
"glass.agent.tree" => agent_request(workspace, call, PiSessionRequest::Tree),
"glass.agent.messages" => agent_request(workspace, call, PiSessionRequest::Messages),
"glass.agent.entries" => agent_request(
workspace,
call,
PiSessionRequest::Entries {
since: optional_string(call, "since").map(str::to_string),
},
),
"glass.agent.stats" => agent_request(workspace, call, PiSessionRequest::SessionStats),
"glass.agent.name" => agent_request(
workspace,
call,
PiSessionRequest::SetSessionName {
name: string("name")?.into(),
},
),
"glass.todo.list" => Ok(serde_json::to_value(workspace.todos())?),
"glass.todo.write" => {
let items = call
.arguments
.get("items")
.cloned()
.ok_or_else(|| DevelopmentError::InvalidInput("todo write requires items".into()))?;
let items: Vec<crate::SessionTodo> = serde_json::from_value(items)?;
Ok(serde_json::to_value(workspace.write_todos(items)?)?)
}
"glass.todo.complete" => {
Ok(serde_json::to_value(workspace.complete_todo(string("id")?)?)?)
}
"glass.task.create" => {
let spec = serde_json::from_value::<TaskSpec>(call.arguments.clone())?;
Ok(serde_json::to_value(workspace.create_task(spec)?)?)
}
"glass.task.crew" => {
let goal = string("goal")?;
let wake = workspace.create_crew(goal)?;
let crew = wake.tasks.clone();
Ok(serde_json::json!({
"goal": goal,
"wake": wake,
"crew": crew,
}))
}
"glass.task.list" => Ok(serde_json::to_value(workspace.tasks()?)?),
"glass.task.wake" => Ok(serde_json::to_value(workspace.latest_crew_wake())?),
"glass.task.get" | "glass.task.inspect" => {
let id = TaskId::parse(string("taskId")?)?;
Ok(serde_json::to_value(workspace.task(&id)?)?)
}
"glass.task.pause" => task_action(workspace, call, |workspace, id| {
workspace.pause_task(id)
}),
"glass.task.resume" => task_action(workspace, call, |workspace, id| {
workspace.resume_task(id)
}),
"glass.task.cancel" => task_action(workspace, call, |workspace, id| {
workspace.cancel_task(id)
}),
"glass.task.retry" => task_action(workspace, call, |workspace, id| {
workspace.retry_task(id)
}),
"glass.task.override-blocked" => task_action(workspace, call, |workspace, id| {
workspace.override_blocked_task(id)
}),
"glass.task.reassign" => {
let id = TaskId::parse(string("taskId")?)?;
workspace.reassign_task(
&id,
string("role")?.into(),
optional_string(call, "model").map(str::to_string),
optional_string(call, "thinking").map(str::to_string),
)?;
Ok(serde_json::json!({"reassigned":true}))
}
"glass.task.evidence" | "glass.task.verify" => {
let id = TaskId::parse(string("taskId")?)?;
workspace.submit_task_evidence(
&id,
string("kind")?.into(),
context.authorization.actor.id.clone(),
boolean(call, "passed", false),
call.arguments
.get("details")
.cloned()
.unwrap_or(Value::Null),
)?;
Ok(serde_json::json!({"recorded":true}))
}
"glass.experiment.create" => {
let port = call
.arguments
.get("port")
.and_then(Value::as_u64)
.map(u16::try_from)
.transpose()
.map_err(|_| DevelopmentError::InvalidInput("invalid experiment port".into()))?;
Ok(serde_json::to_value(workspace.experiments()?.create(
string("id")?,
string("branch")?,
port,
)?)?)
}
"glass.experiment.collect" => Ok(serde_json::to_value(
workspace
.experiments()?
.collect_automatic(string("id")?)?,
)?),
"glass.experiment.list" => Ok(serde_json::to_value(
workspace.experiments()?.snapshots(),
)?),
"glass.experiment.compare" => Ok(serde_json::to_value(
workspace.experiments()?.compare(),
)?),
"glass.experiment.select" => Ok(serde_json::to_value(
workspace.experiments()?.select(string("id")?)?,
)?),
"glass.experiment.remove" => {
workspace
.experiments()?
.remove(string("id")?, boolean(call, "force", false))?;
Ok(serde_json::json!({"removed":true}))
}
"glass.graph.query" => Ok(serde_json::to_value(
workspace.intelligence().node(string("id")?),
)?),
"glass.graph.source" => {
let line = call
.arguments
.get("line")
.and_then(Value::as_u64)
.map(u32::try_from)
.transpose()
.map_err(|_| DevelopmentError::InvalidInput("invalid graph line".into()))?;
Ok(serde_json::to_value(
workspace
.project()
.graph()
.entities_for_source(string("path")?, line),
)?)
}
"glass.graph.path" | "glass.graph.explain" => Ok(serde_json::to_value(
workspace
.intelligence()
.path(string("from")?, string("to")?)?,
)?),
"glass.graph.link" => {
let revision = workspace.project().revision();
let edge = workspace.intelligence_mut().link(
string("from")?,
string("to")?,
string("relation")?,
revision,
&actor,
call.arguments
.get("evidence")
.cloned()
.unwrap_or(Value::Null),
)?;
workspace.intelligence_mut().record(ObservableEventInput {
actor: &actor,
subsystem: "graph",
kind: "linked",
resource: None,
workspace_revision: revision,
evidence: serde_json::json!({"edgeId":edge,"from":string("from")?,"to":string("to")?,"relation":string("relation")?}),
rationale: None,
})?;
Ok(serde_json::json!({"edgeId":edge}))
}
"glass.replay.list" | "glass.replay.inspect" => {
Ok(serde_json::to_value(workspace.intelligence().replay(
unsigned(call, "since", 0)?,
usize::try_from(unsigned(call, "limit", 128)?).unwrap_or(4096),
)?)?)
}
"glass.replay.diff" => Ok(serde_json::to_value(
workspace
.intelligence()
.replay_diff(unsigned(call, "from", 0)?, unsigned(call, "to", 1)?)?,
)?),
"glass.workspace.list" => {
let store = WorkspaceStore::open_default()
.map_err(|error| DevelopmentError::Process(error.to_string()))?;
Ok(serde_json::to_value(store.list().map_err(|error| {
DevelopmentError::Process(error.to_string())
})?)?)
}
"glass.workspace.inspect" => {
let id = WorkspaceId::new(string("id")?)
.map_err(|error| DevelopmentError::InvalidInput(error.to_string()))?;
let store = WorkspaceStore::open_default()
.map_err(|error| DevelopmentError::Process(error.to_string()))?;
Ok(serde_json::to_value(
store
.open(&id)
.map_err(|error| DevelopmentError::Process(error.to_string()))?,
)?)
}
"glass.workspace.suspend" | "glass.workspace.resume" => {
let id = WorkspaceId::new(string("id")?)
.map_err(|error| DevelopmentError::InvalidInput(error.to_string()))?;
let store = WorkspaceStore::open_default()
.map_err(|error| DevelopmentError::Process(error.to_string()))?;
let workspace = if call.name.ends_with("suspend") {
store.suspend(&id)
} else {
store.resume(&id)
}
.map_err(|error| DevelopmentError::Process(error.to_string()))?;
Ok(serde_json::to_value(workspace)?)
}
"glass.workspace.delete" => {
let id = WorkspaceId::new(string("id")?)
.map_err(|error| DevelopmentError::InvalidInput(error.to_string()))?;
let store = WorkspaceStore::open_default()
.map_err(|error| DevelopmentError::Process(error.to_string()))?;
store
.delete(&id)
.map_err(|error| DevelopmentError::Process(error.to_string()))?;
Ok(serde_json::json!({"id":id,"deleted":true}))
}
"glass.daemon.start" => {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.map_err(|error| DevelopmentError::Process(error.to_string()))?;
map_service(runtime.block_on(crate::daemon::start(None, None)))
}
"glass.daemon.status" | "glass.daemon.doctor" => map_service(
crate::daemon::read_status_checked(None, None)
.map_err(|error| DevelopmentError::Process(error.to_string())),
),
"glass.daemon.stop" => map_service(
crate::daemon::stop(None, None)
.map(|_| serde_json::json!({"status":"stopped"}))
.map_err(|error| DevelopmentError::Process(error.to_string())),
),
"glass.daemon.logs" => map_service(
crate::daemon::log_tail(None)
.map_err(|error| DevelopmentError::Process(error.to_string())),
),
"glass.agent.hello" | "glass.agent.models" => {
let id = optional_string(call, "agentId")
.map(|value| agent_id(workspace, value))
.transpose()?
.or_else(|| active_agent_id(workspace))
.ok_or_else(|| {
DevelopmentError::Conflict(
"start an interactive agent session before requesting Pi state"
.into(),
)
})?;
let request = if call.name.ends_with("hello") {
PiSessionRequest::Hello
} else {
PiSessionRequest::Models
};
map_service(workspace.agents().request(&id, request))?;
Ok(serde_json::json!({"queued":true,"agentId":id.as_str()}))
}
"glass.project.attach" => {
let actor = crate::development::Actor::external(string("actor")?);
workspace.project_mut().attach_actor(actor.clone())?;
Ok(serde_json::to_value(actor)?)
}
"glass.workspace.trust.status" => Ok(serde_json::json!({
"trust":workspace.trust(),
"identity":workspace.trust_identity(),
"store":workspace.trust_store_path(),
"localDecisionRequired":true
})),
"glass.workspace.trust.inspect" => Ok(serde_json::json!({
"trust":workspace.trust(),
"items":workspace.trust_inspection(),
"localDecisionRequired":true
})),
_ => Err(DevelopmentError::NotFound(format!("tool {}", call.name))),
}
}
}
fn service_descriptors() -> Vec<ToolDescriptor> {
const READ: &[&str] = &[
"glass.process.list",
"glass.process.logs",
"glass.github.review",
"glass.git.status",
"glass.git.branches",
"glass.git.blame",
"glass.git.conflicts",
"glass.git.diff",
"glass.git.stash.list",
"glass.git.worktree.list",
"glass.editor.selection",
"glass.editor.diff",
"glass.editor.buffers",
"glass.editor.comments",
"glass.editor.proposals",
"glass.editor.fim",
"glass.editor.checkpoints",
"glass.eval.list",
"glass.lsp.diagnostics",
"glass.workspace.list",
"glass.workspace.inspect",
"glass.daemon.status",
"glass.daemon.doctor",
"glass.daemon.logs",
"glass.agent.hello",
"glass.agent.models",
"glass.workspace.trust.status",
"glass.workspace.trust.inspect",
"glass.lsp.list",
"glass.lsp.events",
"glass.lsp.hover",
"glass.lsp.completion",
"glass.lsp.definition",
"glass.lsp.declaration",
"glass.lsp.implementation",
"glass.lsp.references",
"glass.lsp.document_symbols",
"glass.lsp.workspace_symbols",
"glass.lsp.inlay_hints",
"glass.lsp.signature_help",
"glass.lsp.code_actions",
"glass.lsp.formatting",
"glass.lsp.range_formatting",
"glass.lsp.semantic_tokens",
"glass.lsp.rename",
"glass.agent.list",
"glass.agent.sessions",
"glass.agent.tree",
"glass.agent.messages",
"glass.agent.entries",
"glass.agent.stats",
"glass.debug.events",
"glass.debug.inspect",
"glass.debug.processes",
"glass.debug.threads",
"glass.graph.source",
"glass.todo.list",
"glass.task.list",
"glass.task.wake",
"glass.task.get",
"glass.task.inspect",
"glass.test.results",
"glass.test.discover",
"glass.experiment.list",
"glass.experiment.compare",
"glass.graph.query",
"glass.graph.path",
"glass.graph.explain",
"glass.replay.list",
"glass.replay.inspect",
"glass.replay.diff",
"glass.process.health",
"glass.process.ports",
"glass.browser.state",
"glass.browser.observe",
"glass.browser.verify",
"glass.browser.snapshot",
"glass.browser.semantic",
"glass.browser.web_ir",
"glass.browser.diff",
"glass.browser.targets",
"glass.browser.screenshot",
"glass.browser.remote-view.status",
"glass.workflow.list",
"glass.workflow.verify",
"glass.workflow.record",
"glass.memory.retrieve",
"glass.memory.explain",
"glass.semantic.diff",
"glass.semantic.links",
];
const MUTATE: &[&str] = &[
"glass.workspace.suspend",
"glass.workspace.resume",
"glass.workspace.delete",
"glass.daemon.start",
"glass.daemon.stop",
"glass.project.attach",
"glass.github.ship",
"glass.git.stage",
"glass.git.unstage",
"glass.git.discard",
"glass.git.push",
"glass.git.fetch",
"glass.git.pull",
"glass.git.merge",
"glass.git.rebase",
"glass.git.commit",
"glass.todo.write",
"glass.todo.complete",
"glass.git.branch.create",
"glass.git.branch.switch",
"glass.git.stash.push",
"glass.git.stash.pop",
"glass.git.worktree.create",
"glass.git.worktree.remove",
"glass.graph.link",
"glass.test.run",
"glass.test.run-affected",
"glass.test.cancel",
"glass.test.watch",
"glass.eval.start",
"glass.eval.execute",
"glass.eval.cancel",
"glass.eval.reset",
"glass.eval.stop",
"glass.debug.start",
"glass.lsp.start",
"glass.lsp.stop",
"glass.lsp.raw",
"glass.debug.launch",
"glass.debug.attach",
"glass.debug.configuration_done",
"glass.debug.restart",
"glass.debug.breakpoint.set",
"glass.debug.breakpoint.remove",
"glass.debug.exception.set",
"glass.debug.continue",
"glass.debug.pause",
"glass.debug.step",
"glass.debug.disconnect",
"glass.debug.terminate",
"glass.debug.stop",
"glass.agent.spawn",
"glass.agent.setup",
"glass.agent.delegate",
"glass.agent.prompt",
"glass.agent.send",
"glass.agent.steer",
"glass.agent.follow-up",
"glass.agent.approve",
"glass.agent.abort",
"glass.agent.compact",
"glass.agent.model",
"glass.agent.thinking",
"glass.agent.new-session",
"glass.agent.clone-session",
"glass.agent.rewind",
"glass.agent.fork",
"glass.agent.switch-session",
"glass.agent.name",
"glass.task.create",
"glass.task.crew",
"glass.task.pause",
"glass.task.resume",
"glass.task.cancel",
"glass.task.retry",
"glass.task.reassign",
"glass.task.override-blocked",
"glass.task.evidence",
"glass.task.verify",
"glass.experiment.create",
"glass.experiment.collect",
"glass.experiment.select",
"glass.experiment.remove",
"glass.editor.open",
"glass.editor.replace",
"glass.editor.replace_selection",
"glass.editor.save",
"glass.editor.comment.add",
"glass.editor.comment.resolve",
"glass.editor.proposal.create",
"glass.editor.proposal.accept",
"glass.editor.proposal.accept_pack",
"glass.editor.proposal.reject",
"glass.editor.checkpoint.create",
"glass.editor.checkpoint.restore",
"glass.process.start",
"glass.process.stop",
"glass.process.restart",
"glass.process.input",
"glass.process.resize",
"glass.browser.start",
"glass.browser.attach",
"glass.browser.reconnect",
"glass.browser.stop",
"glass.browser.target.select",
"glass.browser.navigate",
"glass.browser.act",
"glass.browser.remote-view.open",
"glass.browser.remote-view.revoke",
"glass.workflow.run",
"glass.workflow.pause",
"glass.workflow.resume",
"glass.workflow.cancel",
"glass.memory.forget",
];
READ.iter()
.map(|name| service_descriptor(name, false))
.chain(MUTATE.iter().map(|name| service_descriptor(name, true)))
.collect()
}
pub(crate) fn tool_requires_mutation(name: &str) -> bool {
static MUTATING: std::sync::LazyLock<Vec<String>> = std::sync::LazyLock::new(|| {
service_descriptors()
.into_iter()
.filter(|descriptor| descriptor.mutating)
.map(|descriptor| descriptor.name)
.collect()
});
MUTATING.iter().any(|candidate| candidate == name)
}
fn untrusted_tool_allowed(name: &str) -> bool {
matches!(
name,
"glass.file.read"
| "glass.file.list"
| "glass.file.search"
| "glass.file.grep"
| "glass.file.find"
| "glass.github.review"
| "glass.git.status"
| "glass.git.diff"
| "glass.git.branches"
| "glass.git.blame"
| "glass.git.conflicts"
| "glass.git.stash.list"
| "glass.git.worktree.list"
| "glass.browser.start"
| "glass.browser.attach"
| "glass.browser.reconnect"
| "glass.browser.stop"
| "glass.browser.state"
| "glass.browser.observe"
| "glass.browser.verify"
| "glass.browser.snapshot"
| "glass.browser.semantic"
| "glass.browser.web_ir"
| "glass.browser.diff"
| "glass.browser.targets"
| "glass.browser.target.select"
| "glass.browser.navigate"
| "glass.browser.act"
| "glass.browser.screenshot"
| "glass.browser.remote-view.open"
| "glass.browser.remote-view.status"
| "glass.browser.remote-view.revoke"
| "glass.agent.setup"
| "glass.agent.list"
| "glass.agent.sessions"
| "glass.agent.tree"
| "glass.agent.messages"
| "glass.agent.entries"
| "glass.agent.stats"
| "glass.graph.source"
| "glass.agent.hello"
| "glass.agent.models"
| "glass.editor.open"
| "glass.editor.selection"
| "glass.editor.diff"
| "glass.editor.buffers"
| "glass.editor.comments"
| "glass.editor.proposals"
| "glass.editor.fim"
| "glass.editor.checkpoints"
| "glass.daemon.logs"
| "glass.neovim.probe"
| "glass.memory.retrieve"
| "glass.memory.explain"
| "glass.semantic.inspect"
| "glass.semantic.diff"
| "glass.semantic.links"
| "glass.graph.query"
| "glass.graph.path"
| "glass.graph.explain"
| "glass.replay.list"
| "glass.replay.inspect"
| "glass.replay.diff"
| "glass.process.logs"
| "glass.process.list"
| "glass.process.health"
| "glass.process.ports"
| "glass.workspace.trust.status"
| "glass.workspace.trust.inspect"
| "glass.runtime.inspect"
| "glass.todo.list"
| "glass.task.list"
| "glass.task.wake"
| "glass.task.get"
| "glass.task.inspect"
)
}
fn confined_worktree_path(workspace_root: &Path, path: &Path) -> DevelopmentResult<PathBuf> {
if path.as_os_str().is_empty() {
return Err(DevelopmentError::InvalidInput(
"worktree path must be a non-empty relative or workspace worktree path".into(),
));
}
let base = workspace_root.join(".glass").join("worktrees");
std::fs::create_dir_all(&base)?;
let canonical_base = base.canonicalize()?;
let candidate = if path.is_absolute() {
path.to_path_buf()
} else if path.components().any(|component| {
matches!(
component,
std::path::Component::ParentDir
| std::path::Component::RootDir
| std::path::Component::Prefix(_)
)
}) {
return Err(DevelopmentError::PathOutsideWorkspace(path.to_path_buf()));
} else {
base.join(path)
};
let name = candidate.file_name().ok_or_else(|| {
DevelopmentError::InvalidInput("worktree path must name a directory".into())
})?;
if name == "." || name == ".." {
return Err(DevelopmentError::InvalidInput(
"worktree path must name a directory".into(),
));
}
let parent = candidate.parent().ok_or_else(|| {
DevelopmentError::InvalidInput("worktree path must name a directory".into())
})?;
let mut existing_parent = parent;
while !existing_parent.exists() {
existing_parent = existing_parent
.parent()
.ok_or_else(|| DevelopmentError::PathOutsideWorkspace(candidate.clone()))?;
}
let canonical_existing = existing_parent.canonicalize()?;
if !canonical_existing.starts_with(&canonical_base) {
return Err(DevelopmentError::PathOutsideWorkspace(candidate));
}
std::fs::create_dir_all(parent)?;
let canonical_parent = parent.canonicalize()?;
if !canonical_parent.starts_with(&canonical_base) {
return Err(DevelopmentError::PathOutsideWorkspace(candidate));
}
Ok(canonical_parent.join(name))
}
fn service_descriptor(name: &str, mutating: bool) -> ToolDescriptor {
let (description, input_schema) = if name == "glass.agent.delegate" {
(
"Delegate one bounded prompt to a temporary installed external agent".to_string(),
serde_json::json!({
"type": "object",
"properties": {
"harness": {"type": "string", "enum": ["codex", "claude", "opencode"]},
"prompt": {"type": "string", "minLength": 1, "maxLength": 65536},
"sandbox": {"type": "string", "enum": ["read-only", "workspace-write"]},
"timeoutSeconds": {"type": "integer", "minimum": 1, "maximum": 3600}
},
"required": ["harness", "prompt"],
"additionalProperties": false
}),
)
} else {
(
format!("Resident Glass Dev operation {name}"),
serde_json::json!({"type": "object"}),
)
};
ToolDescriptor {
name: name.into(),
description,
input_schema,
mutating,
available: true,
unavailable_reason: None,
}
}
fn required_string<'a>(call: &'a ToolCall, name: &str) -> DevelopmentResult<&'a str> {
call.arguments
.get(name)
.and_then(Value::as_str)
.filter(|value| !value.is_empty() && value.len() <= 64 * 1024 && !value.contains('\0'))
.ok_or_else(|| {
DevelopmentError::InvalidInput(format!("{} requires bounded string {name}", call.name))
})
}
fn optional_string<'a>(call: &'a ToolCall, name: &str) -> Option<&'a str> {
call.arguments.get(name).and_then(Value::as_str)
}
fn required_value<'a>(call: &'a ToolCall, name: &str) -> DevelopmentResult<&'a Value> {
call.arguments
.get(name)
.ok_or_else(|| DevelopmentError::InvalidInput(format!("{} requires {name}", call.name)))
}
fn value_map(
call: &ToolCall,
name: &str,
) -> DevelopmentResult<std::collections::BTreeMap<String, Value>> {
match call.arguments.get(name) {
None => Ok(std::collections::BTreeMap::new()),
Some(value) => serde_json::from_value(value.clone()).map_err(DevelopmentError::from),
}
}
fn boolean(call: &ToolCall, name: &str, default: bool) -> bool {
call.arguments
.get(name)
.and_then(Value::as_bool)
.unwrap_or(default)
}
fn unsigned(call: &ToolCall, name: &str, default: u64) -> DevelopmentResult<u64> {
let value = call
.arguments
.get(name)
.and_then(Value::as_u64)
.unwrap_or(default);
(value <= i64::MAX as u64).then_some(value).ok_or_else(|| {
DevelopmentError::InvalidInput(format!("{}.{} is out of range", call.name, name))
})
}
fn integer(call: &ToolCall, name: &str) -> DevelopmentResult<i64> {
call.arguments
.get(name)
.and_then(Value::as_i64)
.ok_or_else(|| DevelopmentError::InvalidInput(format!("{} requires {name}", call.name)))
}
fn u32_value(call: &ToolCall, name: &str) -> DevelopmentResult<u32> {
u32::try_from(unsigned(call, name, 0)?).map_err(|_| {
DevelopmentError::InvalidInput(format!("{}.{} is out of range", call.name, name))
})
}
fn u16_value(call: &ToolCall, name: &str) -> DevelopmentResult<u16> {
let value = u16::try_from(unsigned(call, name, 0)?).map_err(|_| {
DevelopmentError::InvalidInput(format!("{}.{} is out of range", call.name, name))
})?;
(value > 0).then_some(value).ok_or_else(|| {
DevelopmentError::InvalidInput(format!("{}.{} must be non-zero", call.name, name))
})
}
fn position(
call: &ToolCall,
name: &str,
) -> DevelopmentResult<crate::development::DiagnosticPosition> {
serde_json::from_value(required_value(call, name)?.clone()).map_err(DevelopmentError::from)
}
fn value_array_or_empty(call: &ToolCall, name: &str) -> DevelopmentResult<Vec<Value>> {
match call.arguments.get(name) {
None => Ok(Vec::new()),
Some(Value::Array(values)) if values.len() <= 512 => Ok(values.clone()),
Some(_) => Err(DevelopmentError::InvalidInput(format!(
"{}.{} must be an array of at most 512 values",
call.name, name
))),
}
}
fn number(call: &ToolCall, name: &str, default: f64) -> DevelopmentResult<f64> {
let value = call
.arguments
.get(name)
.and_then(Value::as_f64)
.unwrap_or(default);
value.is_finite().then_some(value).ok_or_else(|| {
DevelopmentError::InvalidInput(format!("{}.{} must be finite", call.name, name))
})
}
fn string_array(call: &ToolCall, name: &str) -> DevelopmentResult<Vec<String>> {
let values = call
.arguments
.get(name)
.and_then(Value::as_array)
.filter(|values| !values.is_empty() && values.len() <= 256)
.ok_or_else(|| DevelopmentError::InvalidInput(format!("{} requires {name}", call.name)))?;
values
.iter()
.map(|value| {
value
.as_str()
.filter(|value| !value.is_empty() && value.len() <= 4096 && !value.contains('\0'))
.map(str::to_string)
.ok_or_else(|| DevelopmentError::InvalidInput(format!("invalid {name} item")))
})
.collect()
}
fn string_array_or_empty(call: &ToolCall, name: &str) -> DevelopmentResult<Vec<String>> {
if call.arguments.get(name).is_none() {
Ok(Vec::new())
} else {
string_array(call, name)
}
}
fn unsigned_array(call: &ToolCall, name: &str) -> DevelopmentResult<Vec<u64>> {
call.arguments
.get(name)
.and_then(Value::as_array)
.filter(|values| !values.is_empty() && values.len() <= 512)
.ok_or_else(|| DevelopmentError::InvalidInput(format!("{} requires {name}", call.name)))?
.iter()
.map(|value| {
value
.as_u64()
.filter(|value| *value > 0)
.ok_or_else(|| DevelopmentError::InvalidInput(format!("invalid {name} item")))
})
.collect()
}
fn timeout(call: &ToolCall, maximum_seconds: u64) -> DevelopmentResult<Option<Duration>> {
call.arguments
.get("timeoutSeconds")
.map(|_| unsigned(call, "timeoutSeconds", maximum_seconds))
.transpose()?
.map(|seconds| {
if seconds == 0 || seconds > maximum_seconds {
Err(DevelopmentError::InvalidInput(format!(
"timeoutSeconds must be 1..={maximum_seconds}"
)))
} else {
Ok(Duration::from_secs(seconds))
}
})
.transpose()
}
fn parse_kernel_kind(value: &str) -> DevelopmentResult<KernelKind> {
match value {
"python" => Ok(KernelKind::Python),
"javascript" => Ok(KernelKind::JavaScript),
"shell" => Ok(KernelKind::Shell),
"sql" => Ok(KernelKind::Sql),
_ => Err(DevelopmentError::InvalidInput(
"kernel kind must be python, javascript, shell, or sql".into(),
)),
}
}
fn normalized_actor_id(value: &str) -> String {
let normalized: String = value
.chars()
.filter(|character| character.is_ascii_alphanumeric() || "-_.".contains(*character))
.take(128)
.collect();
if normalized.is_empty() {
"actor".into()
} else {
normalized
}
}
fn agent_id(
workspace: &mut DevelopmentWorkspace,
value: &str,
) -> DevelopmentResult<crate::agents::AgentId> {
workspace
.agents()
.list()?
.into_iter()
.find(|snapshot| snapshot.id.as_str() == value)
.map(|snapshot| snapshot.id)
.ok_or_else(|| DevelopmentError::NotFound(format!("agent {value}")))
}
fn active_agent_id(workspace: &mut DevelopmentWorkspace) -> Option<crate::agents::AgentId> {
workspace
.agents()
.list()
.ok()?
.into_iter()
.find(|snapshot| {
!matches!(
snapshot.status,
crate::agents::AgentStatus::Completed
| crate::agents::AgentStatus::Failed
| crate::agents::AgentStatus::Cancelled
)
})
.map(|snapshot| snapshot.id)
}
fn agent_request(
workspace: &mut DevelopmentWorkspace,
call: &ToolCall,
request: PiSessionRequest,
) -> DevelopmentResult<Value> {
let id = agent_id(workspace, required_string(call, "agentId")?)?;
map_service(workspace.agents().request(&id, request))?;
Ok(serde_json::json!({"queued":true}))
}
fn task_action(
workspace: &mut DevelopmentWorkspace,
call: &ToolCall,
action: impl FnOnce(&mut DevelopmentWorkspace, &TaskId) -> DevelopmentResult<()>,
) -> DevelopmentResult<Value> {
let id = TaskId::parse(required_string(call, "taskId")?)?;
action(workspace, &id)?;
Ok(serde_json::json!({"updated":true}))
}
fn map_service<T: serde::Serialize, E: std::fmt::Display>(
result: Result<T, E>,
) -> DevelopmentResult<Value> {
result
.map_err(|error| DevelopmentError::Process(error.to_string()))
.and_then(|value| serde_json::to_value(value).map_err(DevelopmentError::from))
}
fn map_debug<T: serde::Serialize>(
result: crate::debugger::DebugResult<T>,
) -> DevelopmentResult<Value> {
map_service(result)
}
fn debugger<'a>(
workspace: &'a mut DevelopmentWorkspace,
name: &str,
) -> DevelopmentResult<&'a mut crate::debugger::DebuggerSession> {
workspace
.debugger_mut(name)
.map_err(|error| DevelopmentError::Process(error.to_string()))
}
fn git_value<T: serde::Serialize>(
git: Option<&crate::git::GitService>,
operation: impl FnOnce(&crate::git::GitService) -> crate::git::GitResult<T>,
) -> DevelopmentResult<Value> {
map_service(operation(git.ok_or_else(|| {
DevelopmentError::NotFound("Git repository".into())
})?))
}
fn git_mutation(
git: Option<&crate::git::GitService>,
operation: impl FnOnce(&crate::git::GitService) -> crate::git::GitResult<()>,
) -> DevelopmentResult<()> {
operation(git.ok_or_else(|| DevelopmentError::NotFound("Git repository".into()))?)
.map_err(|error| DevelopmentError::Process(error.to_string()))
}
fn project_observable_result(
workspace: &mut DevelopmentWorkspace,
call: &ToolCall,
actor: &str,
before_revision: u64,
after_revision: u64,
result: &Value,
) -> DevelopmentResult<()> {
let tool_node = format!("tool:{}", call.id);
let result_bytes = serde_json::to_vec(result)?.len();
let summary = serde_json::json!({
"tool":call.name,
"callId":call.id,
"beforeRevision":before_revision,
"afterRevision":after_revision,
"resultBytes":result_bytes,
});
let argument = |name: &str| {
call.arguments.get(name).and_then(|value| {
value
.as_str()
.map(str::to_string)
.or_else(|| value.as_u64().map(|value| value.to_string()))
.or_else(|| value.as_i64().map(|value| value.to_string()))
})
};
let resource_name = |preferred: &[&str], fallback: &str| {
preferred
.iter()
.find_map(|name| argument(name).filter(|value| !value.is_empty()))
.unwrap_or_else(|| fallback.to_string())
};
let mut observed = Vec::<(String, DevelopmentNodeKind, String, String)>::new();
if call.name.starts_with("glass.file.") || call.name.starts_with("glass.editor.") {
let path = resource_name(&["path", "from", "to"], &call.id);
let file = format!("file:{}", id_component(&path));
observed.push((
file.clone(),
DevelopmentNodeKind::File,
path,
"referencesFile".into(),
));
if call.name.starts_with("glass.editor.") {
observed.push((
format!("editorRevision:{}", id_component(&call.id)),
DevelopmentNodeKind::EditorRevision,
format!("editor revision {after_revision}"),
"producedEditorRevision".into(),
));
}
}
if call.name.starts_with("glass.lsp.") {
let path = resource_name(&["path"], &call.id);
if call.name == "glass.lsp.diagnostics" || call.name == "glass.diagnostics.run" {
observed.push((
format!("diagnostic:{}", id_component(&call.id)),
DevelopmentNodeKind::Diagnostic,
path,
"reportedDiagnostic".into(),
));
} else if !matches!(
call.name.as_str(),
"glass.lsp.start" | "glass.lsp.stop" | "glass.lsp.list" | "glass.lsp.events"
) {
observed.push((
format!("symbol:{}", id_component(&call.id)),
DevelopmentNodeKind::Symbol,
path,
"resolvedSymbol".into(),
));
}
}
if call.name.starts_with("glass.git.") {
let kind = if call.name == "glass.git.commit" {
DevelopmentNodeKind::GitCommit
} else {
DevelopmentNodeKind::GitChange
};
observed.push((
format!("gitChange:{}", id_component(&call.id)),
kind,
call.name.clone(),
"observedGitChange".into(),
));
}
if call.name.starts_with("glass.process.") {
let name = resource_name(&["name"], &call.id);
observed.push((
format!("process:{}", id_component(&name)),
DevelopmentNodeKind::Process,
name,
"observedProcess".into(),
));
if let Some(port) = find_scalar(result, &["port", "debugPort", "assignedPort"], 0) {
observed.push((
format!("port:{}", id_component(&port)),
DevelopmentNodeKind::Port,
port,
"observedPort".into(),
));
}
}
if call.name.starts_with("glass.debug.") {
let session = resource_name(&["session"], &call.id);
observed.push((
format!("debugger:{}", id_component(&session)),
DevelopmentNodeKind::Debugger,
session.clone(),
"observedDebugSession".into(),
));
if call.name.contains("breakpoint") {
observed.push((
format!("breakpoint:{}", id_component(&call.id)),
DevelopmentNodeKind::Breakpoint,
resource_name(&["path"], &call.id),
"configuredBreakpoint".into(),
));
}
if let Some(thread) = argument("threadId").or_else(|| find_scalar(result, &["threadId"], 0))
{
observed.push((
format!(
"thread:{}:{}",
id_component(&session),
id_component(&thread)
),
DevelopmentNodeKind::Thread,
thread,
"observedThread".into(),
));
}
if let Some(frame) =
argument("frameId").or_else(|| find_scalar(result, &["frameId", "id"], 0))
{
observed.push((
format!(
"stackFrame:{}:{}",
id_component(&session),
id_component(&frame)
),
DevelopmentNodeKind::StackFrame,
frame,
"observedStackFrame".into(),
));
}
}
if call.name.starts_with("glass.browser.") {
let target = argument("targetId")
.or_else(|| find_scalar(result, &["targetId", "id"], 0))
.unwrap_or_else(|| "active".into());
observed.push((
format!("browserTarget:{}", id_component(&target)),
DevelopmentNodeKind::BrowserTarget,
target,
"observedBrowserTarget".into(),
));
if let Some(revision) = find_scalar(result, &["revision", "browserRevision"], 0) {
observed.push((
format!("browserRevision:{}", id_component(&revision)),
DevelopmentNodeKind::BrowserRevision,
revision,
"observedBrowserRevision".into(),
));
}
}
if call.name.starts_with("glass.semantic.")
|| call.name.starts_with("glass.web_ir.")
|| call.name == "glass.browser.web_ir"
{
observed.push((
format!("webIr:{}", id_component(&call.id)),
DevelopmentNodeKind::WebIrEntity,
call.name.clone(),
"observedWebIr".into(),
));
}
if call.name.starts_with("glass.workflow.") {
let run = resource_name(&["runId", "id", "name"], &call.id);
observed.push((
format!("workflow:{}", id_component(&run)),
DevelopmentNodeKind::Workflow,
run,
"observedWorkflow".into(),
));
}
if call.name.starts_with("glass.test.") {
let run = resource_name(&["runId", "suiteId"], &call.id);
observed.push((
format!("test:{}", id_component(&run)),
DevelopmentNodeKind::Test,
run,
"observedTest".into(),
));
}
if call.name.starts_with("glass.eval.") {
let name = resource_name(&["name"], &call.id);
observed.push((
format!("kernel:{}", id_component(&name)),
DevelopmentNodeKind::Kernel,
name,
"observedKernel".into(),
));
}
if call.name.starts_with("glass.agent.") {
let agent = argument("agentId")
.or_else(|| find_scalar(result, &["agentId", "id"], 0))
.unwrap_or_else(|| call.id.clone());
observed.push((
format!("agent:{}", id_component(&agent)),
DevelopmentNodeKind::Agent,
agent,
"observedAgent".into(),
));
}
if call.name.starts_with("glass.task.") {
let task = resource_name(&["taskId", "id"], &call.id);
observed.push((
format!("task:{}", id_component(&task)),
DevelopmentNodeKind::Task,
task,
"observedTask".into(),
));
}
if call.name.starts_with("glass.experiment.") {
let experiment = resource_name(&["id"], &call.id);
observed.push((
format!("experiment:{}", id_component(&experiment)),
DevelopmentNodeKind::Experiment,
experiment,
"observedExperiment".into(),
));
}
let mut previous: Option<String> = None;
for (id, kind, label, relation) in observed {
workspace.intelligence_mut().upsert_node(DevelopmentNode {
id: id.clone(),
kind,
label,
revision: after_revision,
stale: false,
evidence: summary.clone(),
})?;
workspace.intelligence_mut().link(
previous.as_deref().unwrap_or(&tool_node),
&id,
&relation,
after_revision,
actor,
summary.clone(),
)?;
workspace.intelligence_mut().record(ObservableEventInput {
actor,
subsystem: subsystem_for_tool(&call.name),
kind: "observed",
resource: Some(&id),
workspace_revision: after_revision,
evidence: summary.clone(),
rationale: None,
})?;
previous = Some(id);
}
Ok(())
}
fn subsystem_for_tool(tool: &str) -> &str {
tool.strip_prefix("glass.")
.and_then(|value| value.split('.').next())
.unwrap_or("project")
}
fn id_component(value: &str) -> String {
value
.chars()
.map(|character| {
if character.is_control() {
'_'
} else {
character
}
})
.take(240)
.collect()
}
fn find_scalar(value: &Value, keys: &[&str], depth: usize) -> Option<String> {
if depth > 6 {
return None;
}
match value {
Value::Object(values) => {
for key in keys {
if let Some(value) = values.get(*key) {
if let Some(value) = value.as_str() {
return Some(value.to_string());
}
if let Some(value) = value.as_u64() {
return Some(value.to_string());
}
}
}
values
.values()
.find_map(|value| find_scalar(value, keys, depth + 1))
}
Value::Array(values) => values
.iter()
.find_map(|value| find_scalar(value, keys, depth + 1)),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::development::Actor;
use serde_json::json;
use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};
static NEXT_ROOT: AtomicU64 = AtomicU64::new(0);
fn workspace() -> DevelopmentWorkspace {
let sequence = NEXT_ROOT.fetch_add(1, Ordering::Relaxed);
let root = std::env::temp_dir().join(format!(
"glass-tool-router-{}-{sequence}",
std::process::id()
));
std::fs::create_dir_all(&root).unwrap();
std::fs::write(
root.join("Cargo.toml"),
"[package]\nname='x'\nversion='0.1.0'\n",
)
.unwrap();
let mut workspace = DevelopmentWorkspace::open(root).unwrap();
workspace
.apply_local_trust_decision(crate::LocalTrustDecision::TrustOnce)
.unwrap();
workspace
}
fn context(workspace: &DevelopmentWorkspace, mutate: bool) -> DevelopmentToolContext {
DevelopmentToolContext {
authorization: ToolAuthorization {
actor: Actor::embedded(),
allow_mutation: mutate,
confirmed: mutate,
unrestricted: mutate,
},
initiator: None,
expected_generation: workspace.generation(),
expected_project_revision: workspace.project().revision(),
}
}
#[test]
fn worktree_paths_are_confined_to_glass_worktrees() {
let root = std::env::temp_dir().join(format!(
"glass-worktree-confine-{}-{}",
std::process::id(),
NEXT_ROOT.fetch_add(1, Ordering::Relaxed)
));
std::fs::create_dir_all(&root).unwrap();
let root = root.canonicalize().unwrap();
let relative = confined_worktree_path(&root, Path::new("feature")).unwrap();
assert!(relative.starts_with(root.join(".glass").join("worktrees")));
assert_eq!(relative.file_name().unwrap(), "feature");
let nested = confined_worktree_path(&root, Path::new("nested/leaf")).unwrap();
assert_eq!(nested.file_name().unwrap(), "leaf");
assert!(nested.starts_with(root.join(".glass").join("worktrees").join("nested")));
let allowed_abs = root.join(".glass").join("worktrees").join("abs");
let confined_abs = confined_worktree_path(&root, &allowed_abs).unwrap();
assert_eq!(confined_abs.file_name().unwrap(), "abs");
assert!(confined_worktree_path(&root, Path::new("../escape")).is_err());
assert!(confined_worktree_path(&root, Path::new("")).is_err());
let outside = std::env::temp_dir().join(format!(
"glass-worktree-outside-{}-{}",
std::process::id(),
NEXT_ROOT.fetch_add(1, Ordering::Relaxed)
));
let outside_child = outside.join("leaf");
assert!(confined_worktree_path(&root, &outside_child).is_err());
assert!(!outside.exists());
std::fs::remove_dir_all(&root).unwrap();
}
#[test]
fn git_unstage_is_registered_as_a_mutating_tool() {
let unstage = service_descriptors()
.into_iter()
.find(|descriptor| descriptor.name == "glass.git.unstage")
.expect("glass.git.unstage must be advertised");
assert!(unstage.mutating);
assert!(unstage.available);
assert!(tool_requires_mutation("glass.git.unstage"));
assert!(tool_requires_mutation("glass.lsp.raw"));
}
#[test]
fn root_file_listing_with_empty_path_keeps_tool_successful() {
let mut workspace = workspace();
let router = DevelopmentToolRouter::default();
let call = ToolCall {
id: "list-root".into(),
name: "glass.file.list".into(),
arguments: json!({"path":""}),
};
let read_only = context(&workspace, false);
let result = router
.execute(&mut workspace, &call, &read_only)
.expect("listing the project root must succeed");
assert!(result["entries"].is_array());
assert_eq!(
workspace
.intelligence()
.node("file:list-root")
.unwrap()
.label,
"list-root"
);
}
#[test]
fn router_rejects_stale_or_unauthorized_mutations() {
let mut workspace = workspace();
let router = DevelopmentToolRouter::default();
let call = ToolCall {
id: "write-1".into(),
name: "glass.file.write".into(),
arguments: serde_json::json!({"path":"src/lib.rs","content":"pub fn ok() {}\n"}),
};
let read_only = context(&workspace, false);
assert!(router.execute(&mut workspace, &call, &read_only).is_err());
let mut stale = context(&workspace, true);
stale.expected_project_revision = stale.expected_project_revision.saturating_add(1);
assert!(router.execute(&mut workspace, &call, &stale).is_err());
let valid = context(&workspace, true);
workspace.execute_tool(&call, &valid).unwrap();
assert!(workspace.root().join("src/lib.rs").exists());
let replay = workspace.intelligence().replay(0, 16).unwrap();
assert_eq!(
replay
.iter()
.map(|event| event.kind.as_str())
.collect::<Vec<_>>(),
vec!["called", "observed", "completed"]
);
assert!(
workspace
.intelligence()
.path("repository:root", "tool:write-1")
.is_ok()
);
}
#[test]
fn router_uses_resident_kernel_and_test_services() {
let mut workspace = workspace();
let router = DevelopmentToolRouter::default();
let start = ToolCall {
id: "kernel-1".into(),
name: "glass.eval.start".into(),
arguments: serde_json::json!({"name":"analysis","kind":"sql"}),
};
let mutation = context(&workspace, true);
router.execute(&mut workspace, &start, &mutation).unwrap();
let execute = ToolCall {
id: "kernel-2".into(),
name: "glass.eval.execute".into(),
arguments: serde_json::json!({"name":"analysis","code":"SELECT 40 + 2 AS answer"}),
};
let result = router.execute(&mut workspace, &execute, &mutation).unwrap();
assert_eq!(result["value"][0]["answer"], 42);
assert!(
router
.descriptors()
.iter()
.any(|descriptor| descriptor.name == "glass.debug.launch")
);
}
#[test]
fn kernel_bindings_reenter_the_same_router_with_dual_provenance() {
let mut workspace = workspace();
std::fs::write(workspace.root().join("evidence.txt"), "governed\n").unwrap();
let router = DevelopmentToolRouter::default();
let mutation = context(&workspace, true);
router
.execute(
&mut workspace,
&ToolCall {
id: "kernel-binding-start".into(),
name: "glass.eval.start".into(),
arguments: serde_json::json!({
"name":"analysis",
"kind":"sql",
"capabilities":["glass.file.read","glass.file.write"]
}),
},
&mutation,
)
.unwrap();
let result = router
.execute(
&mut workspace,
&ToolCall {
id: "kernel-binding-execute".into(),
name: "glass.eval.execute".into(),
arguments: serde_json::json!({
"name":"analysis",
"code":r#"GLASS CALL {"id":"sql-1","tool":"glass.file.read","arguments":{"path":"evidence.txt"}}"#
}),
},
&mutation,
)
.unwrap();
assert_eq!(result["value"]["content"], "governed\n");
assert_eq!(result["initiatorId"], "embedded:glass-agent");
assert_eq!(result["executorId"], "kernel:analysis");
let replay = workspace.intelligence().replay(0, 32).unwrap();
let nested = replay
.iter()
.find(|event| event.resource.as_deref() == Some("tool:kernel-analysis-sql-1"))
.unwrap();
assert_eq!(nested.actor, "kernel:analysis");
assert_eq!(nested.evidence["initiator"], "embedded:glass-agent");
assert_eq!(nested.evidence["executor"], "kernel:analysis");
let denied = router.execute(
&mut workspace,
&ToolCall {
id: "kernel-binding-denied".into(),
name: "glass.eval.execute".into(),
arguments: serde_json::json!({
"name":"analysis",
"code":r#"GLASS CALL {"id":"sql-2","tool":"glass.file.write","arguments":{"path":"denied.txt","content":"no"}}"#
}),
},
&mutation,
);
assert!(denied.unwrap_err().to_string().contains("mutation"));
assert!(!workspace.root().join("denied.txt").exists());
}
#[test]
fn governed_python_queries_browser_tests_and_graph_before_approved_mutation() {
let mut workspace = workspace();
let router = DevelopmentToolRouter::default();
let mutation = context(&workspace, true);
router
.execute(
&mut workspace,
&ToolCall {
id: "governed-python-start".into(),
name: "glass.eval.start".into(),
arguments: serde_json::json!({
"name":"analysis",
"kind":"python",
"capabilities":[
"glass.browser.state",
"glass.test.results",
"glass.graph.path",
"glass.file.write"
]
}),
},
&mutation,
)
.unwrap();
for (id, code, expected) in [
(
"governed-python-browser",
"glass.call('glass.browser.state')",
"'connected': False",
),
(
"governed-python-tests",
"glass.call('glass.test.results')",
"results",
),
(
"governed-python-graph",
"glass.call('glass.graph.path', {'from': 'repository:root', 'to': 'tool:kernel-analysis-python-1'})",
"repository:root",
),
] {
let result = router
.execute(
&mut workspace,
&ToolCall {
id: id.into(),
name: "glass.eval.execute".into(),
arguments: serde_json::json!({"name":"analysis","code":code}),
},
&mutation,
)
.unwrap();
assert!(
result["value"].as_str().unwrap().contains(expected),
"{id} must return governed {expected} evidence: {result}"
);
assert_eq!(result["initiatorId"], "embedded:glass-agent");
assert_eq!(result["executorId"], "kernel:analysis");
}
let denied = router.execute(
&mut workspace,
&ToolCall {
id: "governed-python-denied".into(),
name: "glass.eval.execute".into(),
arguments: serde_json::json!({
"name":"analysis",
"code":"glass.call('glass.file.write', {'path': 'denied.py', 'content': 'no'})"
}),
},
&mutation,
);
assert!(denied.unwrap_err().to_string().contains("mutation"));
assert!(!workspace.root().join("denied.py").exists());
router
.execute(
&mut workspace,
&ToolCall {
id: "approved-python-start".into(),
name: "glass.eval.start".into(),
arguments: serde_json::json!({
"name":"approved",
"kind":"python",
"capabilities":["glass.file.write"],
"mutationAuthority":true
}),
},
&mutation,
)
.unwrap();
let approved = router
.execute(
&mut workspace,
&ToolCall {
id: "approved-python-write".into(),
name: "glass.eval.execute".into(),
arguments: serde_json::json!({
"name":"approved",
"code":"glass.call('glass.file.write', {'path': 'approved.py', 'content': 'approved\\n'})"
}),
},
&mutation,
)
.unwrap();
assert_eq!(approved["initiatorId"], "embedded:glass-agent");
assert_eq!(approved["executorId"], "kernel:approved");
assert_eq!(
std::fs::read_to_string(workspace.root().join("approved.py")).unwrap(),
"approved\n"
);
let replay = workspace.intelligence().replay(0, 128).unwrap();
for resource in [
"tool:kernel-analysis-python-1",
"tool:kernel-analysis-python-2",
"tool:kernel-analysis-python-3",
"tool:kernel-approved-python-1",
] {
let nested = replay
.iter()
.find(|event| event.resource.as_deref() == Some(resource))
.unwrap_or_else(|| panic!("missing provenance for {resource}"));
assert_eq!(nested.evidence["initiator"], "embedded:glass-agent");
assert!(
nested.evidence["executor"]
.as_str()
.unwrap()
.starts_with("kernel:")
);
}
}
#[test]
fn router_exposes_shared_editor_process_browser_and_complete_lsp_tools() {
let mut workspace = workspace();
std::fs::create_dir_all(workspace.root().join("src")).unwrap();
std::fs::write(workspace.root().join("src/lib.rs"), "pub fn old() {}\n").unwrap();
let router = DevelopmentToolRouter::default();
let descriptors = router.descriptors();
for name in [
"glass.editor.open",
"glass.editor.selection",
"glass.editor.replace",
"glass.editor.replace_selection",
"glass.editor.save",
"glass.editor.comments",
"glass.editor.proposals",
"glass.editor.fim",
"glass.editor.checkpoints",
"glass.editor.comment.add",
"glass.editor.comment.resolve",
"glass.editor.proposal.create",
"glass.editor.proposal.accept",
"glass.editor.proposal.accept_pack",
"glass.editor.proposal.reject",
"glass.editor.checkpoint.create",
"glass.editor.checkpoint.restore",
"glass.process.list",
"glass.process.logs",
"glass.process.start",
"glass.github.review",
"glass.github.ship",
"glass.process.stop",
"glass.process.restart",
"glass.browser.observe",
"glass.browser.verify",
"glass.browser.snapshot",
"glass.browser.attach",
"glass.browser.reconnect",
"glass.browser.semantic",
"glass.browser.web_ir",
"glass.browser.diff",
"glass.browser.navigate",
"glass.browser.act",
"glass.workflow.list",
"glass.workflow.run",
"glass.workflow.pause",
"glass.workflow.resume",
"glass.workflow.cancel",
"glass.workflow.verify",
"glass.workflow.record",
"glass.memory.retrieve",
"glass.memory.explain",
"glass.memory.forget",
"glass.semantic.inspect",
"glass.semantic.diff",
"glass.semantic.links",
"glass.lsp.start",
"glass.lsp.stop",
"glass.lsp.list",
"glass.lsp.events",
"glass.lsp.definition",
"glass.lsp.declaration",
"glass.lsp.implementation",
"glass.lsp.references",
"glass.lsp.document_symbols",
"glass.lsp.workspace_symbols",
"glass.lsp.inlay_hints",
"glass.lsp.signature_help",
"glass.lsp.code_actions",
"glass.lsp.formatting",
"glass.lsp.range_formatting",
"glass.lsp.semantic_tokens",
"glass.lsp.rename",
"glass.lsp.raw",
"glass.debug.configuration_done",
"glass.debug.restart",
"glass.debug.breakpoint.remove",
"glass.debug.exception.set",
"glass.debug.threads",
"glass.debug.disconnect",
"glass.debug.terminate",
"glass.agent.model",
"glass.agent.thinking",
"glass.agent.new-session",
"glass.agent.clone-session",
"glass.agent.rewind",
"glass.agent.fork",
"glass.agent.switch-session",
"glass.agent.sessions",
"glass.agent.tree",
"glass.agent.messages",
"glass.agent.entries",
"glass.agent.stats",
"glass.agent.name",
"glass.todo.list",
"glass.todo.write",
"glass.todo.complete",
"glass.task.create",
"glass.task.crew",
"glass.task.list",
"glass.task.wake",
"glass.task.get",
"glass.task.inspect",
"glass.task.pause",
"glass.task.resume",
"glass.task.cancel",
"glass.task.retry",
"glass.task.reassign",
"glass.task.override-blocked",
"glass.task.evidence",
"glass.task.verify",
"glass.experiment.create",
"glass.experiment.collect",
"glass.experiment.list",
"glass.experiment.compare",
"glass.experiment.select",
"glass.experiment.remove",
] {
let descriptor = descriptors
.iter()
.find(|descriptor| descriptor.name == name)
.unwrap_or_else(|| panic!("missing {name}"));
assert!(
descriptor.available,
"{name} must execute, not be a placeholder"
);
}
let semantic_descriptors = descriptors
.iter()
.filter(|descriptor| descriptor.name == "glass.semantic.inspect")
.collect::<Vec<_>>();
assert_eq!(semantic_descriptors.len(), 1);
assert_eq!(
semantic_descriptors[0].input_schema["required"],
serde_json::json!(["entity"])
);
for (id, name, arguments) in [
(
"open",
"glass.editor.open",
serde_json::json!({"path":"src/lib.rs"}),
),
(
"replace",
"glass.editor.replace",
serde_json::json!({"path":"src/lib.rs","oldText":"old","newText":"resident"}),
),
(
"save",
"glass.editor.save",
serde_json::json!({"path":"src/lib.rs"}),
),
] {
let call = ToolCall {
id: id.into(),
name: name.into(),
arguments,
};
let authorized = context(&workspace, true);
router.execute(&mut workspace, &call, &authorized).unwrap();
}
assert_eq!(
std::fs::read_to_string(workspace.root().join("src/lib.rs")).unwrap(),
"pub fn resident() {}\n"
);
}
#[test]
fn editor_selection_tool_returns_text_and_replaces_only_that_range() {
let mut workspace = workspace();
std::fs::create_dir_all(workspace.root().join("src")).unwrap();
std::fs::write(workspace.root().join("src/lib.rs"), "alpha\nbeta\n").unwrap();
let router = DevelopmentToolRouter::default();
let authorized = context(&workspace, true);
router
.execute(
&mut workspace,
&ToolCall {
id: "selection-open".into(),
name: "glass.editor.open".into(),
arguments: serde_json::json!({"path":"src/lib.rs"}),
},
&authorized,
)
.unwrap();
workspace
.project_mut()
.set_buffer_selection(
"src/lib.rs",
Some(crate::development::TextSelection {
anchor: crate::development::TextPosition { line: 1, column: 2 },
active: crate::development::TextPosition { line: 2, column: 3 },
}),
crate::development::Actor::local(),
)
.unwrap();
let read_only = context(&workspace, false);
let selection = router
.execute(
&mut workspace,
&ToolCall {
id: "selection-read".into(),
name: "glass.editor.selection".into(),
arguments: serde_json::json!({"path":"src/lib.rs"}),
},
&read_only,
)
.unwrap();
assert_eq!(selection["selectedText"], "lpha\nbe");
let authorized = context(&workspace, true);
router
.execute(
&mut workspace,
&ToolCall {
id: "selection-replace".into(),
name: "glass.editor.replace_selection".into(),
arguments: serde_json::json!({
"path":"src/lib.rs",
"replacement":"X\nY"
}),
},
&authorized,
)
.unwrap();
assert_eq!(
workspace.project().buffer("src/lib.rs").unwrap().content,
"aX\nYta\n"
);
assert!(
workspace
.project()
.buffer("src/lib.rs")
.unwrap()
.selection
.is_none()
);
}
#[test]
fn live_web_ir_route_is_read_only_and_requires_browser() {
let mut workspace = workspace();
let router = DevelopmentToolRouter::default();
let descriptor = router
.descriptors()
.into_iter()
.find(|descriptor| descriptor.name == "glass.browser.web_ir")
.expect("live Web IR descriptor");
assert!(descriptor.available);
assert!(!descriptor.mutating);
let read_only = context(&workspace, false);
let error = router
.execute(
&mut workspace,
&ToolCall {
id: "web-ir-disconnected".into(),
name: "glass.browser.web_ir".into(),
arguments: json!({}),
},
&read_only,
)
.unwrap_err();
assert!(error.to_string().contains("glass.browser.start"));
}
#[test]
fn observable_projection_populates_every_issue_35_graph_resource_kind() {
let mut workspace = workspace();
let samples = vec![
("glass.editor.save", json!({"path":"src/lib.rs"}), json!({})),
("glass.lsp.hover", json!({"path":"src/lib.rs"}), json!({})),
(
"glass.lsp.diagnostics",
json!({"path":"src/lib.rs"}),
json!([]),
),
("glass.git.diff", json!({}), json!({"changed":true})),
(
"glass.process.ports",
json!({"name":"web"}),
json!({"port":8080}),
),
(
"glass.debug.stack",
json!({"session":"rust","threadId":3}),
json!({"stackFrames":[{"id":9}]}),
),
(
"glass.browser.state",
json!({}),
json!({"targetId":"page-1","revision":12}),
),
("glass.web_ir.inspect", json!({}), json!({"entities":[]})),
(
"glass.browser.web_ir",
json!({}),
json!({"targetId":"page-1","revision":12,"entityCount":3}),
),
(
"glass.workflow.verify",
json!({"runId":"checkout"}),
json!({"passed":false}),
),
(
"glass.test.results",
json!({"runId":"unit"}),
json!({"passed":true}),
),
(
"glass.eval.execute",
json!({"name":"analysis"}),
json!({"value":42}),
),
("glass.agent.list", json!({}), json!({"agentId":"agent-1"})),
(
"glass.task.get",
json!({"taskId":"task-1"}),
json!({"state":"failed"}),
),
(
"glass.experiment.collect",
json!({"id":"approach-a"}),
json!({"measured":true}),
),
];
for (index, (name, arguments, result)) in samples.into_iter().enumerate() {
let call = ToolCall {
id: format!("projection-{index}"),
name: name.into(),
arguments,
};
let tool_node = format!("tool:{}", call.id);
workspace
.intelligence_mut()
.upsert_node(DevelopmentNode {
id: tool_node.clone(),
kind: DevelopmentNodeKind::ToolCall,
label: call.name.clone(),
revision: 1,
stale: false,
evidence: Value::Null,
})
.unwrap();
workspace
.intelligence_mut()
.link(
"repository:root",
&tool_node,
"receivedToolCall",
1,
"test",
Value::Null,
)
.unwrap();
project_observable_result(&mut workspace, &call, "test", 1, 1, &result).unwrap();
}
assert!(
workspace
.intelligence()
.query_kind(DevelopmentNodeKind::WebIrEntity)
.iter()
.any(|node| node.id == "webIr:projection-8"),
"live browser Web IR must be projected into the graph"
);
for kind in [
DevelopmentNodeKind::Repository,
DevelopmentNodeKind::File,
DevelopmentNodeKind::Symbol,
DevelopmentNodeKind::EditorRevision,
DevelopmentNodeKind::GitChange,
DevelopmentNodeKind::Process,
DevelopmentNodeKind::Port,
DevelopmentNodeKind::Diagnostic,
DevelopmentNodeKind::Debugger,
DevelopmentNodeKind::Thread,
DevelopmentNodeKind::StackFrame,
DevelopmentNodeKind::BrowserTarget,
DevelopmentNodeKind::BrowserRevision,
DevelopmentNodeKind::WebIrEntity,
DevelopmentNodeKind::Workflow,
DevelopmentNodeKind::Test,
DevelopmentNodeKind::Kernel,
DevelopmentNodeKind::Agent,
DevelopmentNodeKind::Task,
DevelopmentNodeKind::Experiment,
DevelopmentNodeKind::ToolCall,
] {
assert!(
!workspace.intelligence().query_kind(kind).is_empty(),
"missing {kind:?}"
);
}
let router = DevelopmentToolRouter::default();
for (index, (from, to, relation)) in [
("file:src/lib.rs", "gitChange:projection-3", "changedBy"),
("gitChange:projection-3", "test:unit", "verifiedBy"),
("test:unit", "webIr:projection-7", "observedRegression"),
]
.into_iter()
.enumerate()
{
let context = context(&workspace, true);
router
.execute(
&mut workspace,
&ToolCall {
id: format!("causal-link-{index}"),
name: "glass.graph.link".into(),
arguments: json!({
"from":from,
"to":to,
"relation":relation,
"evidence":{"source":"resident-verification"}
}),
},
&context,
)
.unwrap();
}
let path = workspace
.intelligence()
.path("file:src/lib.rs", "webIr:projection-7")
.unwrap();
assert_eq!(
path.edges
.iter()
.map(|edge| edge.relation.as_str())
.collect::<Vec<_>>(),
vec!["changedBy", "verifiedBy", "observedRegression"]
);
assert!(path.edges.iter().all(|edge| !edge.provenance.is_empty()));
let replay = workspace.intelligence().replay(0, 128).unwrap();
assert!(replay.iter().any(|event| event.subsystem == "task"));
assert!(replay.iter().all(|event| event.rationale.is_none()));
}
#[test]
fn external_delegate_is_registered_as_approval_gated_capability() {
let descriptor = DevelopmentToolRouter::default()
.descriptors()
.into_iter()
.find(|descriptor| descriptor.name == "glass.agent.delegate")
.expect("external delegate descriptor");
assert!(descriptor.mutating);
assert_eq!(
descriptor.input_schema["properties"]["sandbox"]["enum"],
json!(["read-only", "workspace-write"])
);
assert!(tool_requires_mutation("glass.agent.delegate"));
}
const AGENT_HIDDEN_TOOLS: &[&str] = &[
"glass.workspace.list",
"glass.workspace.inspect",
"glass.workspace.suspend",
"glass.workspace.resume",
"glass.workspace.delete",
"glass.daemon.start",
"glass.daemon.stop",
"glass.daemon.status",
"glass.daemon.doctor",
"glass.daemon.logs",
"glass.workspace.trust.status",
"glass.workspace.trust.inspect",
"glass.project.attach",
"glass.agent.setup",
];
#[test]
fn pi_named_catalog_covers_agent_visible_glass_tools() {
let tools = include_str!("../assets/pi-glass-tools.ts");
let prompt = include_str!("../assets/pi-glass-system.md");
let runtime = include_str!("../assets/pi-runtime.mjs");
for required in [
"glass.browser.verify",
"glass.editor.fim",
"glass.editor.proposal.accept_pack",
"glass.task.crew",
"glass.github.ship",
"glass.file.search",
"glass.lsp.inlay_hints",
] {
assert!(
tools.contains(&format!("\"{required}\"")),
"Pi named catalog missing {required}"
);
assert!(
prompt.contains(required),
"system prompt missing {required}"
);
}
assert!(runtime.contains("glass.browser.verify"));
assert!(runtime.contains("glass.task.crew"));
let registered = registered_glass_names(tools);
for descriptor in service_descriptors() {
if AGENT_HIDDEN_TOOLS.contains(&descriptor.name.as_str()) {
continue;
}
assert!(
registered.contains(&descriptor.name),
"Pi named catalog missing agent-visible {}",
descriptor.name
);
}
}
fn registered_glass_names(source: &str) -> std::collections::BTreeSet<String> {
let mut names = std::collections::BTreeSet::new();
let mut rest = source;
while let Some(start) = rest.find("register(") {
rest = &rest[start + 9..];
let Some(first) = quoted_string(rest) else {
continue;
};
let after_first = rest
.find(&format!("\"{first}\""))
.map(|index| index + first.len() + 2)
.unwrap_or(0);
let Some(glass) = quoted_string(&rest[after_first..]) else {
continue;
};
if glass.starts_with("glass.") {
names.insert(glass);
}
}
names
}
fn quoted_string(source: &str) -> Option<String> {
let start = source.find('"')?;
let rest = &source[start + 1..];
let end = rest.find('"')?;
Some(rest[..end].to_string())
}
}