config: complete virtual tree editor contract
This commit is contained in:
@@ -1,9 +1,102 @@
|
||||
use config_source::{
|
||||
ConfigTreeSnapshot, EvaluationResult, SnapshotEnvironment, ToolchainContract, VirtualPath,
|
||||
ConfigTreeChange, ConfigTreeSnapshot, EvaluationResult, SnapshotEnvironment, ToolchainContract,
|
||||
VirtualPath,
|
||||
};
|
||||
use serde_wasm_bindgen::{Serializer, from_value};
|
||||
use std::cell::RefCell;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
thread_local! {
|
||||
static SESSION: RefCell<Option<ConfigTreeSnapshot>> = const { RefCell::new(None) };
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn set_snapshot(snapshot: JsValue) -> Result<(), JsValue> {
|
||||
let snapshot: ConfigTreeSnapshot = decode(snapshot)?;
|
||||
SESSION.with(|session| session.replace(Some(snapshot)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn apply_changes(changes: JsValue) -> Result<JsValue, JsValue> {
|
||||
let changes: Vec<ConfigTreeChange> = decode(changes)?;
|
||||
SESSION.with(|session| {
|
||||
let mut session = session.borrow_mut();
|
||||
let snapshot = session
|
||||
.as_ref()
|
||||
.ok_or_else(|| JsValue::from_str("config source snapshot is not initialized"))?
|
||||
.apply(&changes)
|
||||
.map_err(js_error)?;
|
||||
*session = Some(snapshot.clone());
|
||||
encode(snapshot)
|
||||
})
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn evaluate_current(contract: JsValue) -> Result<JsValue, JsValue> {
|
||||
let contract: ToolchainContract = decode(contract)?;
|
||||
SESSION.with(|session| {
|
||||
let session = session.borrow();
|
||||
let snapshot = session
|
||||
.as_ref()
|
||||
.ok_or_else(|| JsValue::from_str("config source snapshot is not initialized"))?;
|
||||
encode(
|
||||
SnapshotEnvironment::new(snapshot.clone())
|
||||
.evaluate_contract(&contract)
|
||||
.map_err(|diagnostics| {
|
||||
encode(&diagnostics).unwrap_or_else(|_| JsValue::from_str("evaluation failed"))
|
||||
})?,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct WasmCompletionResult {
|
||||
from: usize,
|
||||
items: Vec<WasmCompletionItem>,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct WasmCompletionItem {
|
||||
label: String,
|
||||
kind: String,
|
||||
detail: Option<String>,
|
||||
priority: i32,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn complete_current(
|
||||
entrypoint: String,
|
||||
source: String,
|
||||
utf8_byte_offset: usize,
|
||||
explicit: bool,
|
||||
) -> Result<JsValue, JsValue> {
|
||||
let entrypoint = VirtualPath::parse(entrypoint).map_err(js_error)?;
|
||||
SESSION.with(|session| {
|
||||
let session = session.borrow();
|
||||
let snapshot = session
|
||||
.as_ref()
|
||||
.ok_or_else(|| JsValue::from_str("config source snapshot is not initialized"))?;
|
||||
let result = SnapshotEnvironment::new(snapshot.clone())
|
||||
.complete(&entrypoint, &source, utf8_byte_offset, explicit)
|
||||
.map_err(|error| JsValue::from_str(&format!("{error:?}")))?
|
||||
.map(|result| WasmCompletionResult {
|
||||
from: result.from,
|
||||
items: result
|
||||
.items
|
||||
.into_iter()
|
||||
.map(|item| WasmCompletionItem {
|
||||
label: item.label,
|
||||
kind: format!("{:?}", item.kind).to_lowercase(),
|
||||
detail: item.detail,
|
||||
priority: item.priority,
|
||||
})
|
||||
.collect(),
|
||||
});
|
||||
encode(result)
|
||||
})
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn evaluate_snapshot(snapshot: JsValue, contract: JsValue) -> Result<JsValue, JsValue> {
|
||||
let snapshot: ConfigTreeSnapshot = decode(snapshot)?;
|
||||
|
||||
@@ -33,6 +33,7 @@ pub struct ConfigCommitRequest {
|
||||
pub base_digest: String,
|
||||
pub changes: Vec<ConfigTreeChange>,
|
||||
pub entrypoints: Vec<VirtualPath>,
|
||||
pub toolchain_fingerprint: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
@@ -65,6 +66,17 @@ impl SqliteWorkspaceStore {
|
||||
current.snapshot.revision
|
||||
)));
|
||||
}
|
||||
let expected_contract = ToolchainContract::new(
|
||||
DEFAULT_SCHEMA_VERSION,
|
||||
request.entrypoints.clone(),
|
||||
DEFAULT_IMPORT_POLICY_VERSION,
|
||||
);
|
||||
if expected_contract.fingerprint != request.toolchain_fingerprint {
|
||||
return Err(config_conflict(format!(
|
||||
"toolchain fingerprint mismatch; current fingerprint is {}",
|
||||
expected_contract.fingerprint
|
||||
)));
|
||||
}
|
||||
evaluate_candidate(current, &request.changes, request.entrypoints.clone())
|
||||
}
|
||||
|
||||
@@ -378,6 +390,12 @@ mod tests {
|
||||
content: "{ broken = ; }".into(),
|
||||
}],
|
||||
entrypoints: vec![path(DEFAULT_CONFIG_ENTRYPOINT)],
|
||||
toolchain_fingerprint: ToolchainContract::new(
|
||||
DEFAULT_SCHEMA_VERSION,
|
||||
vec![path(DEFAULT_CONFIG_ENTRYPOINT)],
|
||||
DEFAULT_IMPORT_POLICY_VERSION,
|
||||
)
|
||||
.fingerprint,
|
||||
},
|
||||
)
|
||||
.unwrap_err();
|
||||
@@ -402,6 +420,12 @@ mod tests {
|
||||
content: "{ answer = 42; }".into(),
|
||||
}],
|
||||
entrypoints: vec![path(DEFAULT_CONFIG_ENTRYPOINT)],
|
||||
toolchain_fingerprint: ToolchainContract::new(
|
||||
DEFAULT_SCHEMA_VERSION,
|
||||
vec![path(DEFAULT_CONFIG_ENTRYPOINT)],
|
||||
DEFAULT_IMPORT_POLICY_VERSION,
|
||||
)
|
||||
.fingerprint,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
@@ -426,6 +450,12 @@ mod tests {
|
||||
content: "{ answer = 42; }".into(),
|
||||
}],
|
||||
entrypoints: vec![path(DEFAULT_CONFIG_ENTRYPOINT)],
|
||||
toolchain_fingerprint: ToolchainContract::new(
|
||||
DEFAULT_SCHEMA_VERSION,
|
||||
vec![path(DEFAULT_CONFIG_ENTRYPOINT)],
|
||||
DEFAULT_IMPORT_POLICY_VERSION,
|
||||
)
|
||||
.fingerprint,
|
||||
};
|
||||
let candidate = store
|
||||
.evaluate_workspace_config_candidate("w-config", &request)
|
||||
@@ -439,6 +469,31 @@ mod tests {
|
||||
assert!(matches!(error, Error::WorkspaceConfigConflict(_)));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn commit_rejects_mismatched_toolchain_fingerprint() {
|
||||
let store = SqliteWorkspaceStore::in_memory().unwrap();
|
||||
store.upsert_workspace(&workspace()).await.unwrap();
|
||||
let empty = ConfigTreeSnapshot::empty();
|
||||
let error = store
|
||||
.evaluate_and_commit_workspace_config(
|
||||
"w-config",
|
||||
&ConfigCommitRequest {
|
||||
base_revision: 0,
|
||||
base_digest: empty.digest,
|
||||
changes: vec![ConfigTreeChange::Create {
|
||||
path: path(DEFAULT_CONFIG_ENTRYPOINT),
|
||||
content_type: ConfigContentType::Decodal,
|
||||
content: "{ answer = 42; }".into(),
|
||||
}],
|
||||
entrypoints: vec![path(DEFAULT_CONFIG_ENTRYPOINT)],
|
||||
toolchain_fingerprint: "sha256:stale-toolchain".into(),
|
||||
},
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(matches!(error, Error::WorkspaceConfigConflict(_)));
|
||||
assert!(store.load_workspace_config("w-config").unwrap().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn migration_creates_config_authority_without_changing_applied_migrations() {
|
||||
let store = SqliteWorkspaceStore::in_memory().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user