server: route Ticket mutation notifications

This commit is contained in:
2026-07-31 20:15:11 +09:00
parent 32d3f8f75e
commit 005f6cb498
16 changed files with 2385 additions and 367 deletions
+17 -33
View File
@@ -26,7 +26,7 @@ use crate::shutdown_after_idle::{
use crate::spawn::comm_tools::{read_worker_output_tool, send_to_worker_tool, stop_worker_tool};
use crate::spawn::registry::SpawnedWorkerRegistry;
use crate::spawn::tool::spawn_worker_tool;
use crate::worker::{SystemItemCommitter, Worker, WorkerError, WorkerRunResult, WorkspaceClient};
use crate::worker::{SystemItemCommitter, Worker, WorkerError, WorkerRunResult};
use protocol::{
AlertLevel, AlertSource, ErrorCode, Event, Method, RewindTargetId, RunResult, Segment,
TurnResult, WorkerStatus,
@@ -627,21 +627,16 @@ where
// Ticket tools are typed operations over the current workspace Ticket backend.
// Workspace access must be authority-bound to the Backend Workspace API; the
// Worker must not fall back to a local `.yoi/tickets` store.
let ticket_backend = match worker.workspace_client() {
WorkspaceClient::Http {
workspace_id,
base_url,
} => crate::feature::builtin::ticket::TicketFeatureBackend::WorkspaceHttp {
workspace_id: workspace_id.clone(),
base_url: base_url.clone(),
},
_ => {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"ticket tools require Backend Workspace API authority",
));
}
};
let workspace_client = worker.workspace_client_handle();
if !workspace_client.is_available() || workspace_client.workspace_id().is_none() {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"ticket tools require Backend Workspace API authority",
));
}
let ticket_backend = crate::feature::builtin::ticket::TicketFeatureBackend::WorkspaceClient(
workspace_client,
);
feature_registry.add_module(
crate::feature::builtin::ticket::ticket_tools_feature_with_backend(
ticket_backend,
@@ -668,21 +663,16 @@ where
}
{
let workspace_client = worker.workspace_client().clone();
let workspace_client = worker.workspace_client_handle();
let engine = worker.engine_mut();
// Objective tools expose read-only project Objective context through the
// Backend Workspace API. Workers must not guess local `.yoi/objectives`
// paths or read Objective files directly.
if feature_config.objective.enabled {
if let WorkspaceClient::Http {
workspace_id,
base_url,
} = &workspace_client
{
if workspace_client.is_available() && workspace_client.workspace_id().is_some() {
for definition in crate::feature::builtin::objective::workspace_http_objective_tools(
workspace_id.clone(),
base_url.clone(),
workspace_client.clone(),
) {
engine.register_tool(definition);
}
@@ -705,20 +695,14 @@ where
"[feature.memory].enabled = true requires a [memory] configuration section",
)
})?;
if let WorkspaceClient::Http {
workspace_id,
base_url,
} = workspace_client
{
if workspace_client.is_available() && workspace_client.workspace_id().is_some() {
let definitions = if feature_config.memory.staging {
crate::feature::builtin::memory::workspace_http_memory_consolidation_tools(
workspace_id,
base_url,
workspace_client.clone(),
)
} else {
crate::feature::builtin::memory::workspace_http_memory_tools(
workspace_id,
base_url,
workspace_client.clone(),
)
};
for definition in definitions {
+76 -103
View File
@@ -20,27 +20,25 @@ use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use serde_json::json;
use crate::worker::WorkspaceClient;
use crate::worker::{
WorkspaceClient, WorkspaceClientError, WorkspaceRequest, WorkspaceRequestMethod,
};
#[derive(Clone, Debug)]
pub struct WorkspaceHttpMemoryBackend {
workspace_id: String,
base_url: String,
client: Arc<dyn WorkspaceClient>,
}
impl WorkspaceHttpMemoryBackend {
pub fn new(workspace_id: impl Into<String>, base_url: impl Into<String>) -> Self {
Self {
workspace_id: workspace_id.into(),
base_url: base_url.into(),
}
pub fn new(client: Arc<dyn WorkspaceClient>) -> Self {
Self { client }
}
pub async fn execute_operation(
&self,
operation: MemoryBackendOperation,
) -> Result<MemoryBackendOperationResult, WorkspaceMemoryBackendError> {
execute_http_memory_backend(&self.workspace_id, &self.base_url, operation).await
execute_memory_backend(self.client.as_ref(), operation).await
}
async fn execute(&self, operation: MemoryBackendOperation) -> Result<ToolOutput, ToolError> {
@@ -59,7 +57,7 @@ pub enum WorkspaceMemoryBackendError {
#[error("workspace memory backend is unavailable: {reason}")]
Unavailable { reason: String },
#[error("workspace memory backend request failed: {0}")]
Request(#[from] reqwest::Error),
Request(#[from] WorkspaceClientError),
#[error("workspace memory backend returned HTTP {status}: {body}")]
Http {
status: reqwest::StatusCode,
@@ -71,73 +69,49 @@ pub enum WorkspaceMemoryBackendError {
Backend(String),
}
impl WorkspaceClient {
impl dyn WorkspaceClient + '_ {
pub async fn execute_memory_backend_operation(
&self,
operation: MemoryBackendOperation,
) -> Result<MemoryBackendOperationResult, WorkspaceMemoryBackendError> {
match self {
WorkspaceClient::Http {
workspace_id,
base_url,
} => execute_http_memory_backend(workspace_id, base_url, operation).await,
WorkspaceClient::Available { kind } => Err(WorkspaceMemoryBackendError::Unavailable {
reason: format!(
"workspace client kind `{kind}` does not expose the Backend Workspace API"
),
}),
WorkspaceClient::Unavailable { reason } => {
Err(WorkspaceMemoryBackendError::Unavailable {
reason: reason.clone(),
})
}
}
execute_memory_backend(self, operation).await
}
pub async fn request_memory_staging_consolidation(
&self,
operation: MemoryConsolidateStagingOperation,
) -> Result<MemoryConsolidationOutput, WorkspaceMemoryBackendError> {
match self {
WorkspaceClient::Http {
workspace_id,
base_url,
} => execute_http_memory_consolidation(workspace_id, base_url, operation).await,
WorkspaceClient::Available { kind } => Err(WorkspaceMemoryBackendError::Unavailable {
reason: format!(
"workspace client kind `{kind}` does not expose the Backend Workspace API"
),
}),
WorkspaceClient::Unavailable { reason } => {
Err(WorkspaceMemoryBackendError::Unavailable {
reason: reason.clone(),
})
}
}
execute_memory_consolidation(self, operation).await
}
}
async fn execute_http_memory_backend(
workspace_id: &str,
base_url: &str,
async fn execute_memory_backend(
client: &dyn WorkspaceClient,
operation: MemoryBackendOperation,
) -> Result<MemoryBackendOperationResult, WorkspaceMemoryBackendError> {
let url = format!(
"{}/api/w/{}/memory/backend",
base_url.trim_end_matches('/'),
workspace_id
);
let response = reqwest::Client::new()
.post(url)
.json(&operation)
.send()
.await?;
let status = response.status();
let body = response.text().await?;
if !status.is_success() {
return Err(WorkspaceMemoryBackendError::Http { status, body });
let workspace_id =
client
.workspace_id()
.ok_or_else(|| WorkspaceMemoryBackendError::Unavailable {
reason: format!(
"workspace client kind `{}` has no workspace id",
client.kind()
),
})?;
let response = client.execute(WorkspaceRequest::json(
WorkspaceRequestMethod::Post,
format!("/api/w/{workspace_id}/memory/backend"),
serde_json::to_string(&operation)?,
))?;
let status = reqwest::StatusCode::from_u16(response.status)
.unwrap_or(reqwest::StatusCode::INTERNAL_SERVER_ERROR);
if !response.is_success() {
return Err(WorkspaceMemoryBackendError::Http {
status,
body: response.body,
});
}
match serde_json::from_str::<MemoryBackendHttpResponse>(&body)? {
match serde_json::from_str::<MemoryBackendHttpResponse>(&response.body)? {
MemoryBackendHttpResponse::Ok { result } => Ok(result),
MemoryBackendHttpResponse::Error { message } => {
Err(WorkspaceMemoryBackendError::Backend(message))
@@ -145,34 +119,37 @@ async fn execute_http_memory_backend(
}
}
async fn execute_http_memory_consolidation(
workspace_id: &str,
base_url: &str,
async fn execute_memory_consolidation(
client: &dyn WorkspaceClient,
operation: MemoryConsolidateStagingOperation,
) -> Result<MemoryConsolidationOutput, WorkspaceMemoryBackendError> {
let url = format!(
"{}/api/w/{}/memory/consolidation",
base_url.trim_end_matches('/'),
workspace_id
);
let response = reqwest::Client::new()
.post(url)
.json(&operation)
.send()
.await?;
let status = response.status();
let body = response.text().await?;
if !status.is_success() {
return Err(WorkspaceMemoryBackendError::Http { status, body });
let workspace_id =
client
.workspace_id()
.ok_or_else(|| WorkspaceMemoryBackendError::Unavailable {
reason: format!(
"workspace client kind `{}` has no workspace id",
client.kind()
),
})?;
let response = client.execute(WorkspaceRequest::json(
WorkspaceRequestMethod::Post,
format!("/api/w/{workspace_id}/memory/consolidation"),
serde_json::to_string(&operation)?,
))?;
let status = reqwest::StatusCode::from_u16(response.status)
.unwrap_or(reqwest::StatusCode::INTERNAL_SERVER_ERROR);
if !response.is_success() {
return Err(WorkspaceMemoryBackendError::Http {
status,
body: response.body,
});
}
serde_json::from_str::<MemoryConsolidationOutput>(&body).map_err(Into::into)
serde_json::from_str::<MemoryConsolidationOutput>(&response.body).map_err(Into::into)
}
pub fn workspace_http_memory_tools(
workspace_id: impl Into<String>,
base_url: impl Into<String>,
) -> Vec<ToolDefinition> {
let backend = WorkspaceHttpMemoryBackend::new(workspace_id, base_url);
pub fn workspace_http_memory_tools(client: Arc<dyn WorkspaceClient>) -> Vec<ToolDefinition> {
let backend = WorkspaceHttpMemoryBackend::new(client);
vec![
memory_tool(
"MemoryReadDocument",
@@ -215,13 +192,10 @@ pub fn workspace_http_memory_tools(
}
pub fn workspace_http_memory_consolidation_tools(
workspace_id: impl Into<String>,
base_url: impl Into<String>,
client: Arc<dyn WorkspaceClient>,
) -> Vec<ToolDefinition> {
let workspace_id = workspace_id.into();
let base_url = base_url.into();
let mut tools = workspace_http_memory_tools(workspace_id.clone(), base_url.clone());
let backend = WorkspaceHttpMemoryBackend::new(workspace_id, base_url);
let mut tools = workspace_http_memory_tools(client.clone());
let backend = WorkspaceHttpMemoryBackend::new(client);
tools.extend([
memory_tool(
"MemoryStagingList",
@@ -370,6 +344,14 @@ mod tests {
use super::*;
use llm_engine::tool::ToolDefinition;
fn test_client() -> Arc<dyn WorkspaceClient> {
Arc::new(crate::worker::RuntimeWorkspaceHttpClient::new(
"workspace",
"http://backend",
"test-worker",
))
}
fn tool_names(definitions: Vec<ToolDefinition>) -> Vec<String> {
let mut names = definitions
.into_iter()
@@ -390,10 +372,7 @@ mod tests {
#[test]
fn normal_workspace_memory_tools_do_not_include_staging_tools() {
let names = tool_names(workspace_http_memory_tools(
"workspace".to_string(),
"http://backend".to_string(),
));
let names = tool_names(workspace_http_memory_tools(test_client()));
assert!(names.contains(&"MemoryQuery".to_string()));
assert!(names.contains(&"MemoryReadDocument".to_string()));
@@ -410,7 +389,7 @@ mod tests {
#[test]
fn document_update_schema_is_edit_like_and_staging_close_has_no_legacy_kinds() {
let update_schema = tool_meta(
workspace_http_memory_tools("workspace".to_string(), "http://backend".to_string()),
workspace_http_memory_tools(test_client()),
"MemoryUpdateDocument",
);
assert_eq!(
@@ -423,10 +402,7 @@ mod tests {
assert!(update_schema["properties"].get("body_md").is_none());
let close_schema_text = tool_meta(
workspace_http_memory_consolidation_tools(
"workspace".to_string(),
"http://backend".to_string(),
),
workspace_http_memory_consolidation_tools(test_client()),
"MemoryStagingClose",
)
.to_string();
@@ -440,10 +416,7 @@ mod tests {
#[test]
fn consolidation_workspace_memory_tools_include_staging_tools() {
let names = tool_names(workspace_http_memory_consolidation_tools(
"workspace".to_string(),
"http://backend".to_string(),
));
let names = tool_names(workspace_http_memory_consolidation_tools(test_client()));
assert!(names.contains(&"MemoryQuery".to_string()));
assert!(names.contains(&"MemoryReadDocument".to_string()));
+80 -54
View File
@@ -14,26 +14,27 @@ use llm_engine::tool::{
use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::worker::{WorkspaceClient, WorkspaceRequest, WorkspaceRequestMethod};
#[derive(Clone, Debug)]
pub struct WorkspaceHttpObjectiveBackend {
workspace_id: String,
base_url: String,
client: Arc<dyn WorkspaceClient>,
}
impl WorkspaceHttpObjectiveBackend {
pub fn new(workspace_id: impl Into<String>, base_url: impl Into<String>) -> Self {
Self {
workspace_id: workspace_id.into(),
base_url: base_url.into().trim_end_matches('/').to_string(),
}
pub fn new(client: Arc<dyn WorkspaceClient>) -> Self {
Self { client }
}
async fn list(&self, input: ObjectiveListInput) -> Result<ToolOutput, ToolError> {
let mut url = format!("{}/api/w/{}/objectives", self.base_url, self.workspace_id);
let mut url = format!(
"/api/w/{}/objectives",
self.client.workspace_id().unwrap_or_default()
);
if let Some(limit) = input.limit {
url.push_str(&format!("?limit={}", limit.min(1000)));
}
let response = get_json::<ObjectiveListResponse>(&url)
let response = get_json::<ObjectiveListResponse>(self.client.as_ref(), &url)
.await
.map_err(backend_error)?;
let count = response.items.len();
@@ -46,7 +47,7 @@ impl WorkspaceHttpObjectiveBackend {
async fn show(&self, input: ObjectiveShowInput) -> Result<ToolOutput, ToolError> {
let id = validate_id(&input.id, "ObjectiveShow")?;
let url = self.objective_url(id);
let response = get_json::<ObjectiveDetail>(&url)
let response = get_json::<ObjectiveDetail>(self.client.as_ref(), &url)
.await
.map_err(backend_error)?;
Ok(objective_output(
@@ -61,11 +62,18 @@ impl WorkspaceHttpObjectiveBackend {
"ObjectiveCreate requires non-empty title".to_string(),
));
}
let url = format!("{}/api/w/{}/objectives", self.base_url, self.workspace_id);
let response =
send_json::<ObjectiveCreateInput, ObjectiveDetail>(reqwest::Method::POST, &url, &input)
.await
.map_err(backend_error)?;
let url = format!(
"/api/w/{}/objectives",
self.client.workspace_id().unwrap_or_default()
);
let response = send_json::<ObjectiveCreateInput, ObjectiveDetail>(
self.client.as_ref(),
reqwest::Method::POST,
&url,
&input,
)
.await
.map_err(backend_error)?;
Ok(objective_output(
format!("Created objective {}", response.id),
response,
@@ -86,10 +94,14 @@ impl WorkspaceHttpObjectiveBackend {
new_string: input.new_string,
replace_all: input.replace_all,
};
let response =
send_json::<ObjectiveEditRequest, ObjectiveDetail>(reqwest::Method::PATCH, &url, &body)
.await
.map_err(backend_error)?;
let response = send_json::<ObjectiveEditRequest, ObjectiveDetail>(
self.client.as_ref(),
reqwest::Method::PATCH,
&url,
&body,
)
.await
.map_err(backend_error)?;
Ok(objective_output(
format!("Edited objective {}", response.id),
response,
@@ -105,6 +117,7 @@ impl WorkspaceHttpObjectiveBackend {
}
let url = format!("{}/state", self.objective_url(id));
let response = send_json::<ObjectiveSetStateRequest, ObjectiveDetail>(
self.client.as_ref(),
reqwest::Method::POST,
&url,
&ObjectiveSetStateRequest { state: input.state },
@@ -122,6 +135,7 @@ impl WorkspaceHttpObjectiveBackend {
let ticket_id = validate_id(&input.ticket_id, "ObjectiveLinkTicket")?;
let url = format!("{}/ticket-links", self.objective_url(id));
let response = send_json::<ObjectiveLinkTicketRequest, ObjectiveDetail>(
self.client.as_ref(),
reqwest::Method::POST,
&url,
&ObjectiveLinkTicketRequest {
@@ -143,7 +157,7 @@ impl WorkspaceHttpObjectiveBackend {
let id = validate_id(&input.id, "ObjectiveUnlinkTicket")?;
let ticket_id = validate_id(&input.ticket_id, "ObjectiveUnlinkTicket")?;
let url = format!("{}/ticket-links/{}", self.objective_url(id), ticket_id);
let response = delete_json::<ObjectiveDetail>(&url)
let response = delete_json::<ObjectiveDetail>(self.client.as_ref(), &url)
.await
.map_err(backend_error)?;
Ok(objective_output(
@@ -153,17 +167,15 @@ impl WorkspaceHttpObjectiveBackend {
}
fn objective_url(&self, id: &str) -> String {
format!(
"{}/api/w/{}/objectives/{}",
self.base_url, self.workspace_id, id
)
let workspace_id = self.client.workspace_id().unwrap_or_default();
format!("/api/w/{workspace_id}/objectives/{id}")
}
}
#[derive(Debug, thiserror::Error)]
pub enum WorkspaceObjectiveBackendError {
#[error("workspace objective backend request failed: {0}")]
Request(#[from] reqwest::Error),
Request(#[from] crate::worker::WorkspaceClientError),
#[error("workspace objective backend returned HTTP {status}: {body}")]
Http {
status: reqwest::StatusCode,
@@ -182,41 +194,55 @@ fn backend_error(error: WorkspaceObjectiveBackendError) -> ToolError {
}
async fn get_json<T: for<'de> Deserialize<'de>>(
url: &str,
client: &dyn WorkspaceClient,
path: &str,
) -> Result<T, WorkspaceObjectiveBackendError> {
let response = reqwest::Client::new().get(url).send().await?;
decode_response(response).await
decode_response(client.execute(WorkspaceRequest::get(path))?)
}
async fn send_json<B: Serialize, T: for<'de> Deserialize<'de>>(
client: &dyn WorkspaceClient,
method: reqwest::Method,
url: &str,
path: &str,
body: &B,
) -> Result<T, WorkspaceObjectiveBackendError> {
let response = reqwest::Client::new()
.request(method, url)
.json(body)
.send()
.await?;
decode_response(response).await
let method = match method {
reqwest::Method::POST => WorkspaceRequestMethod::Post,
reqwest::Method::PUT => WorkspaceRequestMethod::Put,
reqwest::Method::PATCH => WorkspaceRequestMethod::Patch,
reqwest::Method::DELETE => WorkspaceRequestMethod::Delete,
_ => WorkspaceRequestMethod::Get,
};
decode_response(client.execute(WorkspaceRequest::json(
method,
path,
serde_json::to_string(body)?,
))?)
}
async fn delete_json<T: for<'de> Deserialize<'de>>(
url: &str,
client: &dyn WorkspaceClient,
path: &str,
) -> Result<T, WorkspaceObjectiveBackendError> {
let response = reqwest::Client::new().delete(url).send().await?;
decode_response(response).await
decode_response(client.execute(WorkspaceRequest {
method: WorkspaceRequestMethod::Delete,
path: path.to_string(),
body: None,
})?)
}
async fn decode_response<T: for<'de> Deserialize<'de>>(
response: reqwest::Response,
fn decode_response<T: for<'de> Deserialize<'de>>(
response: crate::worker::WorkspaceResponse,
) -> Result<T, WorkspaceObjectiveBackendError> {
let status = response.status();
let body = response.text().await?;
if !status.is_success() {
return Err(WorkspaceObjectiveBackendError::Http { status, body });
let status = reqwest::StatusCode::from_u16(response.status)
.unwrap_or(reqwest::StatusCode::INTERNAL_SERVER_ERROR);
if !response.is_success() {
return Err(WorkspaceObjectiveBackendError::Http {
status,
body: response.body,
});
}
serde_json::from_str(&body).map_err(Into::into)
serde_json::from_str(&response.body).map_err(Into::into)
}
fn objective_output(summary: String, response: ObjectiveDetail) -> Result<ToolOutput, ToolError> {
@@ -236,11 +262,8 @@ fn validate_id<'a>(id: &'a str, tool_name: &str) -> Result<&'a str, ToolError> {
Ok(id)
}
pub fn workspace_http_objective_tools(
workspace_id: impl Into<String>,
base_url: impl Into<String>,
) -> Vec<ToolDefinition> {
let backend = WorkspaceHttpObjectiveBackend::new(workspace_id, base_url);
pub fn workspace_http_objective_tools(client: Arc<dyn WorkspaceClient>) -> Vec<ToolDefinition> {
let backend = WorkspaceHttpObjectiveBackend::new(client);
vec![
objective_tool(
"ObjectiveList",
@@ -600,10 +623,13 @@ mod tests {
#[test]
fn workspace_http_objective_tools_include_objective_crud_tools() {
let names = tool_names(workspace_http_objective_tools(
"workspace".to_string(),
"http://backend".to_string(),
));
let names = tool_names(workspace_http_objective_tools(Arc::new(
crate::worker::RuntimeWorkspaceHttpClient::new(
"workspace",
"http://backend",
"test-worker",
),
)));
assert_eq!(
names,
@@ -29,7 +29,7 @@ const FINISH_EXTRACTION_DESCRIPTION: &str = "Finish the extract worker run after
#[derive(Clone)]
pub(crate) struct SessionExploreState {
view: Arc<SessionReferenceView>,
workspace_client: WorkspaceClient,
workspace_client: Arc<dyn WorkspaceClient>,
source: SourceRef,
extract_run_id: String,
staged: Arc<Mutex<Vec<String>>>,
@@ -39,7 +39,7 @@ pub(crate) struct SessionExploreState {
impl SessionExploreState {
pub(crate) fn new(
view: SessionReferenceView,
workspace_client: WorkspaceClient,
workspace_client: Arc<dyn WorkspaceClient>,
source: SourceRef,
) -> Self {
Self {
@@ -615,7 +615,7 @@ mod tests {
fn stub_memory_backend_response(
body: &'static str,
) -> (WorkspaceClient, mpsc::Receiver<String>) {
) -> (Arc<dyn WorkspaceClient>, mpsc::Receiver<String>) {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = listener.local_addr().unwrap();
let (tx, rx) = mpsc::channel();
@@ -659,7 +659,11 @@ mod tests {
stream.write_all(response.as_bytes()).unwrap();
});
(
WorkspaceClient::http("test-workspace", format!("http://{addr}")),
Arc::new(crate::worker::RuntimeWorkspaceHttpClient::new(
"test-workspace",
format!("http://{addr}"),
"test-worker",
)),
rx,
)
}
@@ -668,7 +672,7 @@ mod tests {
fn descriptor_declares_session_explore_tools() {
let state = SessionExploreState::new(
SessionReferenceView::new("segment-1", vec![Item::user_message("remember this")]),
WorkspaceClient::available("test-backend"),
crate::worker::marker_workspace_client(None, "test-backend"),
SourceRef {
segment_id: "segment-1".to_string(),
range: [0, 0],
+43 -47
View File
@@ -4,7 +4,10 @@
//! module only resolves the local backend root, declares the built-in feature,
//! and contributes those tools through the normal feature registry path.
use std::path::{Path, PathBuf};
use std::{
path::{Path, PathBuf},
sync::Arc,
};
use ticket::{
LocalTicketBackend, MarkdownText, NewOrchestrationPlanRecord, NewTicket, NewTicketEvent,
@@ -22,6 +25,7 @@ use crate::feature::{
FeatureInstructionContribution, FeatureInstructionDeclaration, FeatureInstructionId,
FeatureModule, ToolContribution, ToolDeclaration,
};
use crate::worker::{WorkspaceClient, WorkspaceRequest, WorkspaceRequestMethod};
const FEATURE_ID: &str = "ticket";
const FEATURE_NAME: &str = "Ticket tools";
@@ -183,13 +187,8 @@ const ORCHESTRATION_CONTROL_ADDITIONAL_TOOL_NAMES: &[&str] = &[
#[derive(Clone, Debug)]
pub enum TicketFeatureBackend {
Local {
root: PathBuf,
},
WorkspaceHttp {
workspace_id: String,
base_url: String,
},
Local { root: PathBuf },
WorkspaceClient(Arc<dyn WorkspaceClient>),
}
impl From<PathBuf> for TicketFeatureBackend {
@@ -274,7 +273,7 @@ impl TicketFeature {
pub fn backend_root(&self) -> Option<&Path> {
match &self.backend {
TicketFeatureBackend::Local { root } => Some(root),
TicketFeatureBackend::WorkspaceHttp { .. } => None,
TicketFeatureBackend::WorkspaceClient(_) => None,
}
}
@@ -321,15 +320,9 @@ impl TicketFeature {
.into(),
)
}
TicketFeatureBackend::WorkspaceHttp {
workspace_id,
base_url,
} => Some(
TicketToolBackend::new(WorkspaceHttpTicketBackend::new(
workspace_id.clone(),
base_url.clone(),
))
.with_record_language(self.record_language.as_deref()),
TicketFeatureBackend::WorkspaceClient(client) => Some(
TicketToolBackend::new(WorkspaceHttpTicketBackend::new(client.clone()))
.with_record_language(self.record_language.as_deref()),
),
}
}
@@ -386,22 +379,18 @@ impl FeatureModule for TicketFeature {
#[derive(Clone, Debug)]
struct WorkspaceHttpTicketBackend {
workspace_id: String,
base_url: String,
client: Arc<dyn WorkspaceClient>,
}
impl WorkspaceHttpTicketBackend {
fn new(workspace_id: String, base_url: String) -> Self {
Self {
workspace_id,
base_url: base_url.trim_end_matches('/').to_string(),
}
fn new(client: Arc<dyn WorkspaceClient>) -> Self {
Self { client }
}
fn endpoint(&self) -> String {
format!(
"{}/api/w/{}/tickets/backend",
self.base_url, self.workspace_id
"/api/w/{}/tickets/backend",
self.client.workspace_id().unwrap_or_default()
)
}
@@ -409,44 +398,44 @@ impl WorkspaceHttpTicketBackend {
&self,
operation: TicketBackendOperation,
) -> TicketResult<TicketBackendOperationResult> {
let client = self.client.clone();
let endpoint = self.endpoint();
if tokio::runtime::Handle::try_current().is_ok() {
return std::thread::spawn(move || Self::invoke_http(endpoint, operation))
return std::thread::spawn(move || Self::invoke_client(client, endpoint, operation))
.join()
.map_err(|_| {
TicketError::Conflict("ticket backend request thread panicked".to_string())
})?;
}
Self::invoke_http(endpoint, operation)
Self::invoke_client(client, endpoint, operation)
}
fn invoke_http(
fn invoke_client(
client: Arc<dyn WorkspaceClient>,
endpoint: String,
operation: TicketBackendOperation,
) -> TicketResult<TicketBackendOperationResult> {
let body = serde_json::to_string(&operation).map_err(|error| {
TicketError::Conflict(format!("serialize ticket operation: {error}"))
})?;
let response = reqwest::blocking::Client::new()
.post(endpoint)
.header(reqwest::header::CONTENT_TYPE, "application/json")
.body(body)
.send()
let response = client
.execute(WorkspaceRequest::json(
WorkspaceRequestMethod::Post,
endpoint,
body,
))
.map_err(|error| {
TicketError::Conflict(format!("ticket backend request failed: {error}"))
})?;
let status = response.status();
let text = response.text().map_err(|error| {
TicketError::Conflict(format!("ticket backend response failed: {error}"))
})?;
if !status.is_success() {
if !response.is_success() {
return Err(TicketError::Conflict(format!(
"ticket backend returned HTTP {status}: {text}"
"ticket backend returned HTTP {}: {}",
response.status, response.body
)));
}
match serde_json::from_str::<TicketBackendHttpResponse>(&text).map_err(|error| {
TicketError::Conflict(format!("decode ticket backend response: {error}"))
})? {
match serde_json::from_str::<TicketBackendHttpResponse>(&response.body).map_err(
|error| TicketError::Conflict(format!("decode ticket backend response: {error}")),
)? {
TicketBackendHttpResponse::Ok { result } => Ok(result),
TicketBackendHttpResponse::Error { message } => Err(TicketError::Conflict(message)),
}
@@ -1126,8 +1115,13 @@ provider = "github"
#[tokio::test(flavor = "multi_thread")]
async fn workspace_http_backend_invoke_is_safe_inside_async_context() {
let backend =
WorkspaceHttpTicketBackend::new("workspace-a".to_string(), "not-a-url".to_string());
let backend = WorkspaceHttpTicketBackend::new(Arc::new(
crate::worker::RuntimeWorkspaceHttpClient::new(
"workspace-a",
"not-a-url",
"test-worker",
),
));
let error = backend
.invoke(TicketBackendOperation::DefaultIntakeReadyStateChangeBody {
@@ -1167,7 +1161,9 @@ provider = "github"
.unwrap();
});
let backend = WorkspaceHttpTicketBackend::new("workspace-a".to_string(), base_url);
let backend = WorkspaceHttpTicketBackend::new(Arc::new(
crate::worker::RuntimeWorkspaceHttpClient::new("workspace-a", base_url, "test-worker"),
));
let created = backend.create(NewTicket::new("HTTP ticket")).unwrap();
server.join().unwrap();
+5 -2
View File
@@ -40,6 +40,9 @@ pub use runtime::dir::RuntimeDir;
pub use segment_log_sink::SegmentLogSink;
pub use shared_state::WorkerSharedState;
pub use worker::{
LocalWorkingDirectory, Worker, WorkerError, WorkerFilesystemAuthority, WorkerRunResult,
WorkerWorkspaceContext, WorkspaceClient, WorkspaceId, WorkspaceIdError, apply_worker_manifest,
LocalWorkingDirectory, RuntimeWorkspaceHttpClient, Worker, WorkerError,
WorkerFilesystemAuthority, WorkerRunResult, WorkerWorkspaceContext, WorkspaceClient,
WorkspaceClientError, WorkspaceId, WorkspaceIdError, WorkspaceRequest, WorkspaceRequestMethod,
WorkspaceResponse, apply_worker_manifest, marker_workspace_client,
unavailable_workspace_client,
};
+33 -26
View File
@@ -128,7 +128,7 @@ pub enum SkillClientError {
#[error("workspace client kind `{0}` does not expose direct Skill HTTP operations")]
UnsupportedClient(String),
#[error("Skill request failed: {0}")]
Request(#[from] reqwest::Error),
Request(#[from] crate::worker::WorkspaceClientError),
#[error("Skill API response JSON is invalid: {0}")]
Json(#[from] serde_json::Error),
#[error("Skill API returned HTTP {status}: {body}")]
@@ -140,7 +140,7 @@ pub enum SkillClientError {
InvalidBaseUrl(String),
}
impl WorkspaceClient {
impl dyn WorkspaceClient + '_ {
pub fn list_skills(&self) -> Result<SkillCatalogResponse, SkillClientError> {
self.get_skill_json("skills")
}
@@ -157,29 +157,21 @@ impl WorkspaceClient {
&self,
path: &str,
) -> Result<T, SkillClientError> {
let Self::Http {
workspace_id,
base_url,
} = self
else {
return match self {
Self::Available { kind } => Err(SkillClientError::UnsupportedClient(kind.clone())),
Self::Unavailable { reason } => Err(SkillClientError::Unavailable(reason.clone())),
Self::Http { .. } => unreachable!(),
};
};
if base_url.trim().is_empty() {
return Err(SkillClientError::InvalidBaseUrl(base_url.clone()));
let workspace_id = self
.workspace_id()
.ok_or_else(|| SkillClientError::UnsupportedClient(self.kind().to_string()))?;
let response = self.execute(crate::worker::WorkspaceRequest::get(format!(
"/api/w/{workspace_id}/{path}"
)))?;
let status = reqwest::StatusCode::from_u16(response.status)
.unwrap_or(reqwest::StatusCode::INTERNAL_SERVER_ERROR);
if !response.is_success() {
return Err(SkillClientError::Http {
status,
body: response.body,
});
}
let base = base_url.trim_end_matches('/');
let url = format!("{base}/api/w/{workspace_id}/{path}");
let response = reqwest::blocking::Client::new().get(url).send()?;
let status = response.status();
let body = response.text()?;
if !status.is_success() {
return Err(SkillClientError::Http { status, body });
}
Ok(serde_json::from_str(&body)?)
Ok(serde_json::from_str(&response.body)?)
}
}
@@ -201,13 +193,23 @@ mod tests {
let mut request_line = String::new();
reader.read_line(&mut request_line).unwrap();
assert!(request_line.starts_with("GET /api/w/ws-1/skills HTTP/1.1"));
let mut worker_header = None;
let mut authorization = None;
loop {
let mut line = String::new();
reader.read_line(&mut line).unwrap();
if let Some(value) = line.strip_prefix("x-yoi-worker-id: ") {
worker_header = Some(value.trim().to_string());
}
if let Some(value) = line.strip_prefix("authorization: ") {
authorization = Some(value.trim().to_string());
}
if line == "\r\n" || line.is_empty() {
break;
}
}
assert_eq!(worker_header.as_deref(), Some("test-worker"));
assert_eq!(authorization.as_deref(), Some("Bearer test-credential"));
let body = serde_json::json!({
"authority": "workspace-backend-skills-v0",
"entries": [{
@@ -229,8 +231,13 @@ mod tests {
.unwrap();
});
let client = WorkspaceClient::http("ws-1", format!("http://{addr}"));
let catalog = client.list_skills().unwrap();
let client = crate::worker::RuntimeWorkspaceHttpClient::new(
"ws-1",
format!("http://{addr}"),
"test-worker",
)
.with_access_token(Some("test-credential".to_string()));
let catalog = (&client as &dyn WorkspaceClient).list_skills().unwrap();
assert_eq!(catalog.entries[0].name, "triage-errors");
assert_eq!(catalog.entries[0].provenance.id, "workspace:triage-errors");
handle.join().unwrap();
+284 -45
View File
@@ -143,77 +143,296 @@ pub enum WorkspaceIdError {
Empty,
}
/// Narrow path-free workspace API handle injected by Runtime/host code.
///
/// This is deliberately not a filesystem authority surface. A Worker may have a
/// workspace client without local filesystem authority, or neither. Local
/// path-backed implementations are represented only as a capability marker here;
/// the actual paths remain under [`WorkerFilesystemAuthority::Local`] or in host
/// adapter code.
/// One authority-bound operation sent through the Runtime-supplied Workspace client.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WorkspaceClient {
/// Runtime/host supplied an HTTP workspace API endpoint.
Http {
workspace_id: String,
base_url: String,
},
/// Runtime/host supplied a workspace API handle. The string is an opaque
/// diagnostic/backend kind, not an endpoint, path, or secret-bearing value.
Available { kind: String },
/// Workspace-aware operations must fail closed or stay disabled.
Unavailable { reason: String },
pub struct WorkspaceRequest {
pub method: WorkspaceRequestMethod,
pub path: String,
pub body: Option<String>,
}
impl WorkspaceClient {
pub fn available(kind: impl Into<String>) -> Self {
Self::Available { kind: kind.into() }
impl WorkspaceRequest {
pub fn get(path: impl Into<String>) -> Self {
Self {
method: WorkspaceRequestMethod::Get,
path: path.into(),
body: None,
}
}
pub fn http(workspace_id: impl Into<String>, base_url: impl Into<String>) -> Self {
Self::Http {
pub fn json(
method: WorkspaceRequestMethod,
path: impl Into<String>,
body: impl Into<String>,
) -> Self {
Self {
method,
path: path.into(),
body: Some(body.into()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WorkspaceRequestMethod {
Get,
Post,
Put,
Patch,
Delete,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WorkspaceResponse {
pub status: u16,
pub body: String,
}
impl WorkspaceResponse {
pub fn is_success(&self) -> bool {
(200..300).contains(&self.status)
}
}
#[derive(Debug, thiserror::Error)]
pub enum WorkspaceClientError {
#[error("workspace client is unavailable: {0}")]
Unavailable(String),
#[error("workspace request path must start with '/': {0}")]
InvalidPath(String),
#[error("workspace request failed: {0}")]
Request(String),
}
/// Path-free Workspace operation authority injected by Runtime/host code.
///
/// Workers receive this trait object rather than a Backend URL. The concrete
/// implementation is responsible for binding Runtime/Worker identity and
/// forwarding operations to the Workspace authority.
pub trait WorkspaceClient: std::fmt::Debug + Send + Sync {
fn workspace_id(&self) -> Option<&str>;
fn kind(&self) -> &str;
fn is_available(&self) -> bool;
fn execute(&self, request: WorkspaceRequest)
-> Result<WorkspaceResponse, WorkspaceClientError>;
}
/// HTTP forwarding client created by Runtime for one concrete Worker execution.
///
/// The upstream endpoint and source headers are private implementation details;
/// model-visible tools can only submit [`WorkspaceRequest`] values through the
/// [`WorkspaceClient`] trait.
pub struct RuntimeWorkspaceHttpClient {
workspace_id: String,
base_url: String,
worker_id: String,
access_token: Option<String>,
}
impl std::fmt::Debug for RuntimeWorkspaceHttpClient {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("RuntimeWorkspaceHttpClient")
.field("workspace_id", &self.workspace_id)
.field("base_url", &self.base_url)
.field("worker_id", &self.worker_id)
.field(
"access_token",
&self.access_token.as_ref().map(|_| "[redacted]"),
)
.finish()
}
}
impl RuntimeWorkspaceHttpClient {
pub fn new(
workspace_id: impl Into<String>,
base_url: impl Into<String>,
worker_id: impl Into<String>,
) -> Self {
Self {
workspace_id: workspace_id.into(),
base_url: base_url.into(),
base_url: base_url.into().trim_end_matches('/').to_string(),
worker_id: worker_id.into(),
access_token: None,
}
}
pub fn unavailable(reason: impl Into<String>) -> Self {
Self::Unavailable {
reason: reason.into(),
pub fn with_access_token(mut self, access_token: Option<String>) -> Self {
self.access_token = access_token;
self
}
}
impl WorkspaceClient for RuntimeWorkspaceHttpClient {
fn workspace_id(&self) -> Option<&str> {
Some(&self.workspace_id)
}
fn kind(&self) -> &str {
"runtime-http-proxy"
}
fn is_available(&self) -> bool {
true
}
fn execute(
&self,
request: WorkspaceRequest,
) -> Result<WorkspaceResponse, WorkspaceClientError> {
let base_url = self.base_url.clone();
let worker_id = self.worker_id.clone();
let access_token = self.access_token.clone();
if tokio::runtime::Handle::try_current().is_ok() {
return std::thread::spawn(move || {
execute_runtime_workspace_http(
&base_url,
&worker_id,
access_token.as_deref(),
request,
)
})
.join()
.map_err(|_| {
WorkspaceClientError::Request("workspace request thread panicked".to_string())
})?;
}
execute_runtime_workspace_http(&base_url, &worker_id, access_token.as_deref(), request)
}
}
fn execute_runtime_workspace_http(
base_url: &str,
worker_id: &str,
access_token: Option<&str>,
request: WorkspaceRequest,
) -> Result<WorkspaceResponse, WorkspaceClientError> {
if !request.path.starts_with('/') || request.path.starts_with("//") {
return Err(WorkspaceClientError::InvalidPath(request.path));
}
let url = format!("{base_url}{}", request.path);
let method = match request.method {
WorkspaceRequestMethod::Get => reqwest::Method::GET,
WorkspaceRequestMethod::Post => reqwest::Method::POST,
WorkspaceRequestMethod::Put => reqwest::Method::PUT,
WorkspaceRequestMethod::Patch => reqwest::Method::PATCH,
WorkspaceRequestMethod::Delete => reqwest::Method::DELETE,
};
let client = reqwest::blocking::Client::new();
let mut request_builder = client
.request(method, url)
.header("x-yoi-worker-id", worker_id);
if let Some(access_token) = access_token {
request_builder = request_builder.bearer_auth(access_token);
}
if let Some(body) = request.body {
request_builder = request_builder
.header(reqwest::header::CONTENT_TYPE, "application/json")
.body(body);
}
let response = request_builder
.send()
.map_err(|error| WorkspaceClientError::Request(error.to_string()))?;
let status = response.status().as_u16();
let body = response
.text()
.map_err(|error| WorkspaceClientError::Request(error.to_string()))?;
Ok(WorkspaceResponse { status, body })
}
#[derive(Debug)]
struct MarkerWorkspaceClient {
workspace_id: Option<String>,
kind: String,
available: bool,
reason: String,
}
impl WorkspaceClient for MarkerWorkspaceClient {
fn workspace_id(&self) -> Option<&str> {
self.workspace_id.as_deref()
}
pub fn local_filesystem() -> Self {
Self::available("local-filesystem")
fn kind(&self) -> &str {
&self.kind
}
pub fn is_available(&self) -> bool {
matches!(self, Self::Available { .. } | Self::Http { .. })
fn is_available(&self) -> bool {
self.available
}
fn execute(
&self,
_request: WorkspaceRequest,
) -> Result<WorkspaceResponse, WorkspaceClientError> {
Err(WorkspaceClientError::Unavailable(self.reason.clone()))
}
}
pub fn unavailable_workspace_client(
workspace_id: Option<&WorkspaceId>,
reason: impl Into<String>,
) -> Arc<dyn WorkspaceClient> {
Arc::new(MarkerWorkspaceClient {
workspace_id: workspace_id.map(|id| id.as_str().to_string()),
kind: "unavailable".to_string(),
available: false,
reason: reason.into(),
})
}
pub fn marker_workspace_client(
workspace_id: Option<&WorkspaceId>,
kind: impl Into<String>,
) -> Arc<dyn WorkspaceClient> {
let kind = kind.into();
Arc::new(MarkerWorkspaceClient {
workspace_id: workspace_id.map(|id| id.as_str().to_string()),
reason: format!("workspace client kind `{kind}` does not expose Workspace operations"),
kind,
available: true,
})
}
/// Workspace context supplied to a Worker separately from filesystem authority.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Clone)]
pub struct WorkerWorkspaceContext {
workspace_id: Option<WorkspaceId>,
client: WorkspaceClient,
client: Arc<dyn WorkspaceClient>,
}
impl std::fmt::Debug for WorkerWorkspaceContext {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("WorkerWorkspaceContext")
.field("workspace_id", &self.workspace_id)
.field("client_kind", &self.client.kind())
.field("client_available", &self.client.is_available())
.finish()
}
}
impl WorkerWorkspaceContext {
pub fn no_workspace() -> Self {
Self {
workspace_id: None,
client: WorkspaceClient::unavailable("no workspace configured"),
client: unavailable_workspace_client(None, "no workspace configured"),
}
}
pub fn unavailable(workspace_id: Option<WorkspaceId>, reason: impl Into<String>) -> Self {
let client = unavailable_workspace_client(workspace_id.as_ref(), reason);
Self {
workspace_id,
client: WorkspaceClient::unavailable(reason),
client,
}
}
pub fn with_client(workspace_id: Option<WorkspaceId>, client: WorkspaceClient) -> Self {
pub fn with_client(
workspace_id: Option<WorkspaceId>,
client: Arc<dyn WorkspaceClient>,
) -> Self {
Self {
workspace_id,
client,
@@ -221,15 +440,23 @@ impl WorkerWorkspaceContext {
}
pub fn local_filesystem(workspace_id: Option<WorkspaceId>) -> Self {
Self::with_client(workspace_id, WorkspaceClient::local_filesystem())
let client = marker_workspace_client(workspace_id.as_ref(), "local-filesystem");
Self {
workspace_id,
client,
}
}
pub fn workspace_id(&self) -> Option<&WorkspaceId> {
self.workspace_id.as_ref()
}
pub fn client(&self) -> &WorkspaceClient {
&self.client
pub fn client(&self) -> &dyn WorkspaceClient {
self.client.as_ref()
}
pub fn client_handle(&self) -> Arc<dyn WorkspaceClient> {
self.client.clone()
}
}
@@ -926,10 +1153,14 @@ impl<C: LlmClient, St: Store> Worker<C, St> {
/// Narrow workspace client/availability handle injected by Runtime/host.
/// This never grants local filesystem authority.
pub fn workspace_client(&self) -> &WorkspaceClient {
pub fn workspace_client(&self) -> &dyn WorkspaceClient {
self.workspace_context.client()
}
pub fn workspace_client_handle(&self) -> Arc<dyn WorkspaceClient> {
self.workspace_context.client_handle()
}
async fn resident_summary_from_workspace_authority(
&self,
) -> Result<Option<String>, WorkerError> {
@@ -3197,7 +3428,7 @@ impl<C: LlmClient, St: Store> Worker<C, St> {
items_to_extract,
);
let session_explore_state =
SessionExploreState::new(session_view, self.workspace_client().clone(), source);
SessionExploreState::new(session_view, self.workspace_client_handle(), source);
let input_text = render_extract_input(session_explore_state.view());
let mut internal_tools = Vec::new();
let mut internal_hook_builder = HookRegistryBuilder::new();
@@ -3464,7 +3695,7 @@ impl WorkerAuditBase {
async fn emit(
&self,
workspace_client: &WorkspaceClient,
workspace_client: &dyn WorkspaceClient,
event_tx: Option<&broadcast::Sender<Event>>,
status: memory::audit::WorkerLifecycleStatus,
reason: impl Into<String>,
@@ -4936,7 +5167,7 @@ mod spawned_context_tests {
false,
WorkerWorkspaceContext::with_client(
Some(workspace_id.clone()),
WorkspaceClient::available("test-api"),
marker_workspace_client(Some(&workspace_id), "test-api"),
),
WorkerFilesystemAuthority::None,
manifest.scope.clone(),
@@ -5773,7 +6004,11 @@ mod build_summary_prompt_tests {
});
WorkerWorkspaceContext::with_client(
Some(WorkspaceId::new("test-memory").unwrap()),
WorkspaceClient::http("test-memory", format!("http://{addr}")),
Arc::new(RuntimeWorkspaceHttpClient::new(
"test-memory",
format!("http://{addr}"),
"test-worker",
)),
)
}
@@ -5905,7 +6140,11 @@ mod build_summary_prompt_tests {
store,
WorkerWorkspaceContext::with_client(
Some(WorkspaceId::new("ws-skill").unwrap()),
WorkspaceClient::http("ws-skill", format!("http://{addr}")),
Arc::new(RuntimeWorkspaceHttpClient::new(
"ws-skill",
format!("http://{addr}"),
"test-worker",
)),
),
authority,
scope,