merge: integrate ticket assignment notifications
# Conflicts: # crates/workspace-server/src/store.rs
This commit is contained in:
@@ -172,10 +172,29 @@ pub struct WorkingDirectoryStatus {
|
||||
pub summary: WorkingDirectorySummary,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct WorkspaceApiRef {
|
||||
pub workspace_id: String,
|
||||
pub base_url: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub runtime_id: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub access_token: Option<String>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for WorkspaceApiRef {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
formatter
|
||||
.debug_struct("WorkspaceApiRef")
|
||||
.field("workspace_id", &self.workspace_id)
|
||||
.field("base_url", &self.base_url)
|
||||
.field("runtime_id", &self.runtime_id)
|
||||
.field(
|
||||
"access_token",
|
||||
&self.access_token.as_ref().map(|_| "[redacted]"),
|
||||
)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
/// Canonical Runtime Worker creation request.
|
||||
@@ -189,6 +208,10 @@ pub struct WorkspaceApiRef {
|
||||
/// summarized without exposing raw host paths.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct CreateWorkerRequest {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub idempotency_key: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub idempotency_fingerprint: Option<String>,
|
||||
pub profile: ProfileSelector,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub display_name: Option<String>,
|
||||
|
||||
@@ -1153,6 +1153,8 @@ mod tests {
|
||||
request.workspace_api = Some(WorkspaceApiRef {
|
||||
workspace_id: workspace_id.to_string(),
|
||||
base_url: format!("https://workspace.example/{workspace_id}"),
|
||||
runtime_id: None,
|
||||
access_token: None,
|
||||
});
|
||||
request
|
||||
}
|
||||
@@ -1410,6 +1412,8 @@ mod tests {
|
||||
let profile = ProfileSelector::Builtin("builtin:coder".to_string());
|
||||
let bundle = test_bundle(profile.clone());
|
||||
CreateWorkerRequest {
|
||||
idempotency_key: None,
|
||||
idempotency_fingerprint: None,
|
||||
profile,
|
||||
display_name: None,
|
||||
profile_source: crate::catalog::ProfileSourceArchiveSource::Http {
|
||||
@@ -1810,6 +1814,8 @@ mod ws_tests {
|
||||
fn ws_create_request() -> CreateWorkerRequest {
|
||||
let bundle = ws_test_bundle(ProfileSelector::Builtin("builtin:companion".to_string()));
|
||||
CreateWorkerRequest {
|
||||
idempotency_key: None,
|
||||
idempotency_fingerprint: None,
|
||||
profile: ProfileSelector::Builtin("builtin:companion".to_string()),
|
||||
display_name: None,
|
||||
profile_source: crate::catalog::ProfileSourceArchiveSource::Http {
|
||||
|
||||
@@ -354,6 +354,11 @@ impl Runtime {
|
||||
request: CreateWorkerRequest,
|
||||
scope: Option<&RuntimeWorkspaceScope>,
|
||||
) -> Result<WorkerDetail, RuntimeError> {
|
||||
if request.idempotency_key.is_some() != request.idempotency_fingerprint.is_some() {
|
||||
return Err(RuntimeError::InvalidRequest(
|
||||
"idempotency_key and idempotency_fingerprint must be provided together".to_string(),
|
||||
));
|
||||
}
|
||||
let (backend, worker_ref, spawn_request) = {
|
||||
let mut state = self.lock()?;
|
||||
state.ensure_running()?;
|
||||
@@ -365,6 +370,20 @@ impl Runtime {
|
||||
if let Some(scope) = scope {
|
||||
state.ensure_workspace_owner(scope, true)?;
|
||||
};
|
||||
if let Some(idempotency_key) = request.idempotency_key.as_deref() {
|
||||
let workspace_id = scope.map(|scope| scope.workspace_id.as_str());
|
||||
if let Some(existing) = state.workers.values().find(|record| {
|
||||
record.workspace_id.as_deref() == workspace_id
|
||||
&& record.request.idempotency_key.as_deref() == Some(idempotency_key)
|
||||
}) {
|
||||
if existing.request.idempotency_fingerprint != request.idempotency_fingerprint {
|
||||
return Err(RuntimeError::InvalidRequest(format!(
|
||||
"worker creation idempotency key {idempotency_key} was already used with different input"
|
||||
)));
|
||||
}
|
||||
return Ok(existing.detail());
|
||||
}
|
||||
}
|
||||
state.validate_worker_config_boundary(&request)?;
|
||||
if let Some(working_directory_id) = requested_primary_workdir_id(&request) {
|
||||
if let Some(owner_worker_id) =
|
||||
@@ -2108,7 +2127,9 @@ fn input_protocol_event(input: &WorkerInput) -> protocol::Event {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::catalog::{ConfigBundleRef, ProfileSelector, WorkspaceApiRef};
|
||||
use crate::catalog::{
|
||||
ConfigBundleRef, ProfileSelector, WorkingDirectoryClaim, WorkspaceApiRef,
|
||||
};
|
||||
use crate::config_bundle::{
|
||||
ConfigBundle, ConfigBundleMetadata, ConfigBundleProvenance, ConfigDeclaration,
|
||||
ConfigDeclarationKind, ConfigProfileDescriptor,
|
||||
@@ -2126,6 +2147,8 @@ mod tests {
|
||||
let profile = ProfileSelector::Builtin("builtin:coder".to_string());
|
||||
let bundle = test_bundle_for_profile(profile.clone());
|
||||
CreateWorkerRequest {
|
||||
idempotency_key: None,
|
||||
idempotency_fingerprint: None,
|
||||
profile,
|
||||
display_name: None,
|
||||
profile_source: crate::catalog::ProfileSourceArchiveSource::Http {
|
||||
@@ -2161,6 +2184,8 @@ mod tests {
|
||||
request.workspace_api = Some(WorkspaceApiRef {
|
||||
workspace_id: workspace_id.to_string(),
|
||||
base_url: format!("https://workspace.example/{workspace_id}"),
|
||||
runtime_id: None,
|
||||
access_token: None,
|
||||
});
|
||||
request
|
||||
}
|
||||
@@ -2612,6 +2637,33 @@ mod tests {
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_worker_idempotency_reuses_worker_and_rejects_different_input() {
|
||||
let runtime = runtime_with_backend();
|
||||
let mut request = task_request("idempotent");
|
||||
request.idempotency_key = Some("operation-1".to_string());
|
||||
request.idempotency_fingerprint = Some("sha256:input-1".to_string());
|
||||
request.working_directory = Some(WorkingDirectoryClaim {
|
||||
working_directory_id: "workdir-idempotent".to_string(),
|
||||
relative_cwd: None,
|
||||
});
|
||||
|
||||
let first = runtime.create_worker(request.clone()).unwrap();
|
||||
let workdir_count_after_first = runtime.list_working_directories().unwrap().len();
|
||||
let replayed = runtime.create_worker(request.clone()).unwrap();
|
||||
assert_eq!(replayed.worker_ref, first.worker_ref);
|
||||
assert_eq!(runtime.list_workers().unwrap().len(), 1);
|
||||
assert_eq!(
|
||||
runtime.list_working_directories().unwrap().len(),
|
||||
workdir_count_after_first
|
||||
);
|
||||
|
||||
request.idempotency_fingerprint = Some("sha256:different".to_string());
|
||||
let error = runtime.create_worker(request).unwrap_err();
|
||||
assert!(matches!(error, RuntimeError::InvalidRequest(_)));
|
||||
assert_eq!(runtime.list_workers().unwrap().len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_worker_rejects_system_initial_input_without_persisting_worker() {
|
||||
let runtime = runtime_with_backend();
|
||||
|
||||
@@ -23,6 +23,7 @@ use crate::execution::{
|
||||
WorkerExecutionRestoreRequest, WorkerExecutionResult, WorkerExecutionRunState,
|
||||
WorkerExecutionSpawnRequest, WorkerExecutionSpawnResult,
|
||||
};
|
||||
use crate::identity::WorkerRef;
|
||||
use crate::interaction::{WorkerInput, WorkerInputKind};
|
||||
use crate::resource::{BackendResourceClient, ProfileSourceArchiveCache};
|
||||
use crate::working_directory::{
|
||||
@@ -40,8 +41,8 @@ use tokio::sync::broadcast;
|
||||
#[cfg(feature = "ws-server")]
|
||||
use worker::ipc::protocol_session::{live_log_entry_event, subscribe_worker_protocol_session};
|
||||
use worker::{
|
||||
PromptLoader, Worker, WorkerController, WorkerError, WorkerFilesystemAuthority, WorkerHandle,
|
||||
WorkerWorkspaceContext, WorkspaceClient, WorkspaceId,
|
||||
PromptLoader, RuntimeWorkspaceHttpClient, Worker, WorkerController, WorkerError,
|
||||
WorkerFilesystemAuthority, WorkerHandle, WorkerWorkspaceContext, WorkspaceId,
|
||||
};
|
||||
|
||||
const DEFAULT_BACKEND_ID: &str = "worker-crate";
|
||||
@@ -259,6 +260,7 @@ enum RuntimeWorkspaceBackendRef {
|
||||
Http {
|
||||
workspace_id: String,
|
||||
base_url: String,
|
||||
access_token: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -268,20 +270,29 @@ impl RuntimeWorkspaceBackendRef {
|
||||
return Self::Http {
|
||||
workspace_id: api.workspace_id.clone(),
|
||||
base_url: api.base_url.clone(),
|
||||
access_token: api.access_token.clone(),
|
||||
};
|
||||
}
|
||||
Self::None
|
||||
}
|
||||
|
||||
fn worker_context(&self) -> WorkerWorkspaceContext {
|
||||
fn worker_context(&self, worker_ref: &WorkerRef) -> WorkerWorkspaceContext {
|
||||
match self {
|
||||
Self::None => WorkerWorkspaceContext::no_workspace(),
|
||||
Self::Http {
|
||||
workspace_id,
|
||||
base_url,
|
||||
access_token,
|
||||
} => WorkerWorkspaceContext::with_client(
|
||||
WorkspaceId::new(workspace_id.clone()).ok(),
|
||||
WorkspaceClient::http(workspace_id.clone(), base_url.clone()),
|
||||
Arc::new(
|
||||
RuntimeWorkspaceHttpClient::new(
|
||||
workspace_id.clone(),
|
||||
base_url.clone(),
|
||||
worker_ref.worker_id.to_string(),
|
||||
)
|
||||
.with_access_token(access_token.clone()),
|
||||
),
|
||||
),
|
||||
}
|
||||
}
|
||||
@@ -368,7 +379,7 @@ impl RuntimeWorkerFactory for ProfileRuntimeWorkerFactory {
|
||||
.unwrap_or(WorkerFilesystemAuthority::None);
|
||||
let workspace_backend_ref =
|
||||
RuntimeWorkspaceBackendRef::from_worker_request(&request.request);
|
||||
let workspace_context = workspace_backend_ref.worker_context();
|
||||
let workspace_context = workspace_backend_ref.worker_context(&request.worker_ref);
|
||||
let selector = profile.as_ref();
|
||||
let archive = self
|
||||
.resolve_profile_source_archive(&request.request.profile_source)
|
||||
@@ -442,7 +453,7 @@ impl RuntimeWorkerFactory for ProfileRuntimeWorkerFactory {
|
||||
.unwrap_or(WorkerFilesystemAuthority::None);
|
||||
let workspace_backend_ref =
|
||||
RuntimeWorkspaceBackendRef::from_worker_request(&request.request);
|
||||
let workspace_context = workspace_backend_ref.worker_context();
|
||||
let workspace_context = workspace_backend_ref.worker_context(&request.worker_ref);
|
||||
let (manifest, loader) = Self::restore_fallback_manifest(&worker_name)?;
|
||||
|
||||
let store_dir = self.store_dir()?;
|
||||
@@ -1276,7 +1287,7 @@ mod tests {
|
||||
store_dir: PathBuf,
|
||||
worker_metadata_dir: PathBuf,
|
||||
observed_cwds: Arc<Mutex<Vec<PathBuf>>>,
|
||||
observed_workspace_clients: Arc<Mutex<Vec<WorkspaceClient>>>,
|
||||
observed_workspace_clients: Arc<Mutex<Vec<(String, Option<String>, bool)>>>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@@ -1325,11 +1336,13 @@ mod tests {
|
||||
.unwrap_or_else(|| self.cwd.clone());
|
||||
let workspace_backend_ref =
|
||||
RuntimeWorkspaceBackendRef::from_worker_request(&request.request);
|
||||
let workspace_context = workspace_backend_ref.worker_context();
|
||||
self.observed_workspace_clients
|
||||
.lock()
|
||||
.unwrap()
|
||||
.push(workspace_context.client().clone());
|
||||
let workspace_context = workspace_backend_ref.worker_context(&request.worker_ref);
|
||||
let workspace_client = workspace_context.client();
|
||||
self.observed_workspace_clients.lock().unwrap().push((
|
||||
workspace_client.kind().to_string(),
|
||||
workspace_client.workspace_id().map(str::to_string),
|
||||
workspace_client.is_available(),
|
||||
));
|
||||
let scope = Scope::writable(&scope_root).map_err(|err| err.to_string())?;
|
||||
let worker = Worker::new(
|
||||
manifest,
|
||||
@@ -1438,6 +1451,8 @@ mod tests {
|
||||
fn create_request(_name: &str) -> CreateWorkerRequest {
|
||||
let bundle = test_bundle();
|
||||
CreateWorkerRequest {
|
||||
idempotency_key: None,
|
||||
idempotency_fingerprint: None,
|
||||
profile: ProfileSelector::Builtin("builtin:companion".to_string()),
|
||||
display_name: None,
|
||||
profile_source: crate::catalog::ProfileSourceArchiveSource::Embedded {
|
||||
@@ -1673,6 +1688,8 @@ mod tests {
|
||||
request.workspace_api = Some(crate::catalog::WorkspaceApiRef {
|
||||
workspace_id: "ws-test".to_string(),
|
||||
base_url: "http://127.0.0.1:3999".to_string(),
|
||||
runtime_id: None,
|
||||
access_token: None,
|
||||
});
|
||||
let detail = runtime.create_worker(request).unwrap();
|
||||
|
||||
@@ -1704,7 +1721,11 @@ mod tests {
|
||||
assert!(observed_cwds.lock().unwrap().is_empty());
|
||||
assert_eq!(
|
||||
observed_workspace_clients.lock().unwrap().as_slice(),
|
||||
&[WorkspaceClient::http("ws-test", "http://127.0.0.1:3999")]
|
||||
&[(
|
||||
"runtime-http-proxy".to_string(),
|
||||
Some("ws-test".to_string()),
|
||||
true,
|
||||
)]
|
||||
);
|
||||
let names = captured_tool_names(&client, 0);
|
||||
for forbidden in core_filesystem_tool_names() {
|
||||
@@ -1781,9 +1802,7 @@ mod tests {
|
||||
assert!(cwd.join("README.md").exists());
|
||||
assert_eq!(
|
||||
observed_workspace_clients.lock().unwrap().as_slice(),
|
||||
&[WorkspaceClient::Unavailable {
|
||||
reason: "no workspace configured".to_string()
|
||||
}]
|
||||
&[("unavailable".to_string(), None, false)]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user