diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e6e967..0b4f164 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,13 @@ Work after the 1.0.0 candidate. It is not part of the staged 1.0.0 release. ### Added +- Native console: a spec view. **TOML** in the setup pane, or the palette's + **Toggle spec view (TOML)**, shows the experiment as the + `ember.experiment.v1` intervention spec that Run executes beside its + baseline. The view follows the form, and an edit in the view changes the + form when the form can hold it. The view keeps any other edit with the + reason, until **Revert**. **Copy** copies the spec + (`docs/v06-gui.md#spec-view-toml`). - Sweeps: `features = { dictionary, ids }` in `[sweep]` sweeps the features of a feature dictionary, one per point: each point gives the swept interventions `features = { dictionary, ids = [F] }`. `ids` is a list or diff --git a/docs/v06-gui.md b/docs/v06-gui.md index 5a20011..9ee603a 100644 --- a/docs/v06-gui.md +++ b/docs/v06-gui.md @@ -510,6 +510,34 @@ without `#`, puts that run first; other queries match the whole prompt, then the change and the model. Type `>` to switch it to the command list. Settings › Keyboard lists the same keys. +### Spec view (TOML) + +**TOML** at the top of the setup pane (or the palette's **Toggle spec view +(TOML)**) shows the experiment as the `ember.experiment.v1` intervention +spec that Run executes. Run also executes a baseline spec without the +intervention, and compares the two. The view takes the place of the form +sections, and the pane becomes wider. **Form** goes back to the form. + +- The view follows the form. A change from the examples, the palette or + the keys (a rerun, for example) shows in the view at once. +- An edit in the view changes the form when the form can hold it: the model + path, the prompt, the token limit, the execution mode, the site, the + layer, the operation and its value, the source and its layer, and the + target tokens. +- The view keeps any other edit, and a line under it gives the reason. For + example, a second input, another token selector or a changed `seed` is + not a form setting. The form keeps its values, and the view does not + follow the form until **Revert**. +- The view shows `runs/gui/intervention` as the output directory. A run + from the console writes to its own directory, as before. +- **Copy** copies the text in the view. Save it to a file to run it with + `ember experiment run` or to make it a sweep. That run writes only the + intervention bundle, without the console's baseline. + +The console compares the edited spec with the spec that the new form +generates, once both are resolved. Only the name, the description and the +recorded defaults may differ, because the console sets them. + ### What changed from the wizard The contextual inspector is gone (it restated the setup). Its advanced controls diff --git a/src/gui.rs b/src/gui.rs index 9a18a4e..94e5c7f 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -1452,6 +1452,221 @@ fn build_and_resolve_spec( Ok((spec_text, resolved)) } +// --------------------------------------------------------------------------- +// the spec view: form -> TOML, and TOML -> form where the form can hold it +// --------------------------------------------------------------------------- + +/// The output directory the spec view shows. A run writes to its own +/// timestamped directory instead. +pub(crate) const SPEC_VIEW_OUTPUT_DIR: &str = "runs/gui/intervention"; + +/// The intervention run's spec for `cfg`, as TOML: what the console runs, +/// with [`SPEC_VIEW_OUTPUT_DIR`] as the output directory. +#[cfg_attr( + not(feature = "gui"), + allow(dead_code, reason = "only the `gui` feature uses it") +)] +pub(crate) fn intervention_spec_text(cfg: &RunConfig) -> Result { + build_and_resolve_spec(cfg, RunKind::Intervention, SPEC_VIEW_OUTPUT_DIR).map(|(text, _)| text) +} + +/// The fields of the console's form that an intervention spec sets. `None` +/// means the spec does not set that field, so the form keeps its value. +#[cfg_attr( + not(feature = "gui"), + allow(dead_code, reason = "only the `gui` feature uses it") +)] +#[derive(Debug, Clone, PartialEq)] +pub(crate) struct SpecFields { + pub model_path: String, + pub prompt: String, + pub max_new_tokens: usize, + pub execution: String, + /// The v0.4 hook stage id, as the form names sites. + pub site: String, + pub layer: Option, + pub operation: String, + /// The scale factor or the interpolate alpha. + pub value: Option, + /// `capture` or `zero`, for the operations that take a source. + pub source: Option, + pub source_layer: Option, + /// `prompt-final` or `matched-span`. + pub token: String, + pub span: Option, +} + +/// Read the form's fields from an edited spec. This fails, with the reason, +/// when the spec has a shape that the form cannot hold: other than one +/// input and one intervention, or another token selector or source. Whether +/// every other field matches is checked by [`check_same_experiment`]. +#[cfg_attr( + not(feature = "gui"), + allow(dead_code, reason = "only the `gui` feature uses it") +)] +pub(crate) fn spec_fields(text: &str) -> Result { + let spec = RawExperimentSpec::from_toml_str(text) + .and_then(|raw| raw.resolve()) + .map_err(|error| error.to_string())?; + let [input] = spec.inputs.as_slice() else { + return Err(format!( + "The console runs one prompt. This spec has {} inputs.", + spec.inputs.len() + )); + }; + let [intervention] = spec.interventions.as_slice() else { + return Err(format!( + "The console runs one intervention. This spec has {}.", + spec.interventions.len() + )); + }; + let one_layer = |selector: &LayerSelector, what: &str| match selector { + LayerSelector::List(layers) if layers.len() == 1 => Ok(layers[0]), + _ => Err(format!( + "The console sets one {what} layer. Write layers = [L]." + )), + }; + let site = intervention.site; + let layer = if site.is_per_layer() { + Some(one_layer(&intervention.layers, "intervention")?) + } else { + None + }; + let operation = intervention.operation.kind_name().to_string(); + if matches!( + GuiOperation::parse(&operation)?, + GuiOperation::RestoreOriginal + ) { + return Err( + "The console's operations are replace, zero, scale, interpolate and \ + add-delta. This spec uses restore-original." + .to_string(), + ); + } + let value = match intervention.operation { + InterventionOperation::Scale { factor } => Some(factor), + InterventionOperation::Interpolate { alpha } => Some(alpha), + _ => None, + }; + let (source, source_layer) = match &intervention.source { + None => (None, None), + Some(InterventionSource::Zero) => (Some("zero".to_string()), None), + Some(InterventionSource::CaptureFromCurrentRun { capture_id }) + if capture_id == "cap-src" => + { + let capture = spec + .captures + .iter() + .find(|capture| capture.id == "cap-src") + .ok_or("The spec has no source capture cap-src.")?; + let source_layer = if site.is_per_layer() { + Some(one_layer(&capture.layers, "source capture")?) + } else { + None + }; + (Some("capture".to_string()), source_layer) + } + Some(other) => { + return Err(format!( + "The console's source is the zero tensor or its own capture cap-src. This spec \ + uses {}.", + other.kind_name() + )) + } + }; + let (token, span) = match &intervention.tokens { + TokenSelector::PromptFinal => ("prompt-final".to_string(), None), + TokenSelector::MatchedTextSpan { text, .. } => { + ("matched-span".to_string(), Some(text.clone())) + } + other => { + let kind = serde_json::to_value(other) + .ok() + .and_then(|value| value["kind"].as_str().map(str::to_string)) + .unwrap_or_default(); + return Err(format!( + "The console selects the final prompt token or a matched text span. This spec \ + uses {kind}." + )); + } + }; + Ok(SpecFields { + model_path: spec.model.path.to_string_lossy().into_owned(), + prompt: input.text.clone(), + max_new_tokens: spec.generation.max_new_tokens, + execution: spec.execution.mode.name().to_string(), + site: site.stage_id().to_string(), + layer, + operation, + value, + source, + source_layer, + token, + span, + }) +} + +/// Check that an edited spec runs the same experiment as the one the form +/// generates: equal once resolved, apart from the name, the description and +/// the recorded defaults, which the console sets itself. The error names the +/// first field that differs. +#[cfg_attr( + not(feature = "gui"), + allow(dead_code, reason = "only the `gui` feature uses it") +)] +pub(crate) fn check_same_experiment(edited: &str, generated: &str) -> Result<(), String> { + let normalize = |text: &str| -> Result { + let mut spec = RawExperimentSpec::from_toml_str(text) + .and_then(|raw| raw.resolve()) + .map_err(|error| error.to_string())?; + spec.experiment.name.clear(); + spec.experiment.description.clear(); + spec.defaults.clear(); + serde_json::to_value(&spec).map_err(|error| error.to_string()) + }; + let (edited, generated) = (normalize(edited)?, normalize(generated)?); + match first_difference(&edited, &generated, "") { + None => Ok(()), + Some(path) => Err(format!( + "The form cannot hold `{path}` as written. Save the spec to a file and run it \ + with `ember experiment run`." + )), + } +} + +/// The dotted path of the first place where `a` and `b` differ. +fn first_difference(a: &serde_json::Value, b: &serde_json::Value, path: &str) -> Option { + use serde_json::Value; + let join = |key: &str| { + if path.is_empty() { + key.to_string() + } else { + format!("{path}.{key}") + } + }; + match (a, b) { + (Value::Object(a), Value::Object(b)) => { + let keys: BTreeSet<&String> = a.keys().chain(b.keys()).collect(); + keys.into_iter() + .find_map(|key| match (a.get(key), b.get(key)) { + (Some(x), Some(y)) => first_difference(x, y, &join(key)), + _ => Some(join(key)), + }) + } + (Value::Array(a), Value::Array(b)) if a.len() == b.len() => a + .iter() + .zip(b) + .enumerate() + .find_map(|(index, (x, y))| first_difference(x, y, &format!("{path}[{index}]"))), + _ if a == b => None, + _ => Some(if path.is_empty() { + "spec".into() + } else { + path.to_string() + }), + } +} + // --------------------------------------------------------------------------- // execution // --------------------------------------------------------------------------- @@ -2642,6 +2857,91 @@ mod tests { ); } + #[test] + fn the_spec_view_reads_back_what_the_console_writes() { + for (operation, source) in [ + ("replace", "capture"), + ("replace", "zero"), + ("zero", "capture"), + ("scale", "capture"), + ("interpolate", "zero"), + ("add-delta", "capture"), + ] { + let mut req = base_request(); + req.operation = operation.to_string(); + req.source = source.to_string(); + req.factor = (operation == "scale").then_some(0.25); + req.alpha = (operation == "interpolate").then_some(0.4); + let text = intervention_spec_text(&cfg_of(&req)).expect("spec"); + let fields = spec_fields(&text).expect("fields"); + assert_eq!(fields.operation, operation); + assert_eq!(fields.site, req.site); + assert_eq!(fields.layer, req.layer); + assert_eq!(fields.prompt, req.prompt); + let takes_source = matches!(operation, "replace" | "interpolate" | "add-delta"); + assert_eq!(fields.source.as_deref(), takes_source.then_some(source)); + assert_eq!( + fields.value, + match operation { + "scale" => Some(0.25), + "interpolate" => Some(0.4), + _ => None, + } + ); + assert_eq!(check_same_experiment(&text, &text), Ok(())); + } + } + + #[test] + fn the_spec_view_says_what_the_form_cannot_hold() { + let text = intervention_spec_text(&cfg_of(&base_request())).expect("spec"); + let error = |edited: &str| { + spec_fields(edited) + .and_then(|_| check_same_experiment(edited, &text)) + .unwrap_err() + }; + // Metadata the console owns is not a difference. + let renamed = text.replace("name = \"gui-intervention\"", "name = \"mine\""); + assert_ne!(renamed, text); + assert_eq!(check_same_experiment(&renamed, &text), Ok(())); + // A field the form has no control for is named. + let seeded = text.replace("seed = 0", "seed = 7"); + assert_ne!(seeded, text); + assert!( + error(&seeded).contains("experiment.seed"), + "{}", + error(&seeded) + ); + let second_input = text.replacen( + "[[inputs]]", + "[[inputs]]\nid = \"prompt-2\"\ntext = \"more\"\n\n[[inputs]]", + 1, + ); + assert!(error(&second_input).contains("one prompt")); + let absolute = text.replace( + "kind = \"prompt-final\"", + "kind = \"absolute-token\"\nindex = 1", + ); + assert_ne!(absolute, text); + assert!( + error(&absolute).contains("absolute-token"), + "{}", + error(&absolute) + ); + // The form's picker does not offer restore-original. + let restore = text + .replace("kind = \"scale\"", "kind = \"restore-original\"") + .replace("factor = 0.5\n", ""); + assert_ne!(restore, text); + assert!( + error(&restore).contains("uses restore-original"), + "{}", + error(&restore) + ); + // A spec that does not parse says why. + assert!(spec_fields("schema = 1").is_err()); + } + #[test] fn every_operation_builds_valid_specs() { for (operation, extra) in [ diff --git a/src/gui_native.rs b/src/gui_native.rs index ce2e54c..4b4f4f5 100644 --- a/src/gui_native.rs +++ b/src/gui_native.rs @@ -497,6 +497,16 @@ struct Console { palette_index: usize, palette_mode: PaletteMode, palette_input: Entity, + // spec view + spec_open: bool, + spec_input: Entity, + /// The form's spec as the view last showed it. + spec_shown: String, + /// The view holds an edit the form could not take; it follows the form + /// again after Revert or a valid edit. + spec_held: bool, + /// Why the view holds its edit, or why the form has no spec. + spec_note: Option, step: WorkspaceStep, view: View, /// The Review page is showing the built-in illustrative sample, not a run. @@ -733,6 +743,21 @@ impl Console { console.input_changed(event, cx); }) .detach(); + let spec_input = cx.new(|cx| { + TextInput::new( + InputId::Spec, + InputKind::Code, + String::new(), + "", + &colors, + window, + cx, + ) + }); + cx.subscribe(&spec_input, |console, _input, event: &InputEvent, cx| { + console.input_changed(event, cx); + }) + .detach(); let pickers = [ ComboId::Model, ComboId::Site, @@ -806,6 +831,11 @@ impl Console { palette_index: 0, palette_mode: PaletteMode::Commands, palette_input, + spec_open: false, + spec_input, + spec_shown: String::new(), + spec_held: false, + spec_note: None, step: WorkspaceStep::Prompt, // Land on Home rather than inside a half-configured experiment. view: View::Home, @@ -1708,6 +1738,17 @@ impl Console { Command::ShowLayers => self.show_result_view(ResultView::Layers, cx), Command::ShowTokens => self.show_result_view(ResultView::Tokens, cx), Command::ShowTrace => self.show_result_view(ResultView::Trace, cx), + Command::ToggleSpec => { + // From another page, the command shows the view. + if self.view != View::Experiment { + self.goto(View::Experiment, cx); + if !self.spec_open { + self.toggle_spec(cx); + } + } else { + self.toggle_spec(cx); + } + } Command::ToggleSidebar => self.toggle_sidebar(cx), Command::ToggleTheme => self.cycle_appearance(cx), Command::TogglePresentation => self.toggle_presentation(cx), @@ -1778,6 +1819,10 @@ impl Console { InputId::Span => self.span.clone_from(&event.value), InputId::MaxTokens => self.max_tokens.clone_from(&event.value), InputId::Prompt => self.prompt.clone_from(&event.value), + InputId::Spec => { + self.spec_edited(&event.value, cx); + return; + } InputId::PaletteQuery => { // A new query means a new candidate list; the cursor restarts // so Enter always runs what the eye is resting on. @@ -2085,6 +2130,72 @@ impl Console { cx.notify(); } + // -- spec view --------------------------------------------------------- + + /// Open or close the spec view. When it opens, it shows the form's spec. + fn toggle_spec(&mut self, cx: &mut Context) { + self.spec_open = !self.spec_open; + self.revert_spec(cx); + } + + /// Drop a held edit: the view shows the form's spec again. + fn revert_spec(&mut self, cx: &mut Context) { + self.spec_held = false; + self.spec_note = None; + self.spec_shown.clear(); + cx.notify(); + } + + /// Keep the spec view in step with the form, unless it holds an edit + /// that the form could not take. Runs before each render. + fn sync_spec_view(&mut self, cx: &mut Context) { + if !self.spec_open || self.spec_held { + return; + } + match self.form_values().spec_text() { + Ok(text) => { + self.spec_note = None; + if text != self.spec_shown { + self.spec_shown = text.clone(); + self.set_input_value(self.spec_input.clone(), text, cx); + } + } + Err(error) => self.spec_note = Some(format!("The form is not complete: {error}")), + } + } + + /// An edit in the spec view: the form takes it when it can, and the + /// view keeps the text as typed either way. + fn spec_edited(&mut self, text: &str, cx: &mut Context) { + if !self.spec_open { + return; + } + if text == self.spec_shown { + // The text is the form's spec again: there is no edit to hold. + if self.spec_held { + self.spec_held = false; + self.spec_note = None; + cx.notify(); + } + return; + } + match self.form_values().with_spec(text) { + Ok(values) => { + self.apply_form_values(values, cx); + self.spec_held = false; + self.spec_note = None; + // The form's own text can put the same spec in a different + // layout. The view keeps the typed text. + self.spec_shown = self.form_values().spec_text().unwrap_or_default(); + } + Err(error) => { + self.spec_held = true; + self.spec_note = Some(error); + } + } + cx.notify(); + } + /// Put a captured form configuration back into the console. The resident /// session only resets when the model actually changed, so duplicating a /// run on the same model does not pay for a reload. diff --git a/src/gui_native/form.rs b/src/gui_native/form.rs index 382a986..85981ce 100644 --- a/src/gui_native/form.rs +++ b/src/gui_native/form.rs @@ -61,6 +61,45 @@ impl FormValues { } } + /// The intervention spec this form runs, as TOML (the spec view). + pub(super) fn spec_text(&self) -> Result { + let request = self.build_run_request()?; + let config = crate::gui::parse_run_request(&request)?; + crate::gui::intervention_spec_text(&config) + } + + /// This form with the fields that the edited spec `text` sets. A field + /// the spec does not set (the factor of a `zero`, say) keeps its value, + /// but a spec without a source gives the default source, `capture`: the + /// zero source needs an operation that takes a source. Fails, with the + /// reason, unless the new form generates the same experiment as `text`. + pub(super) fn with_spec(&self, text: &str) -> Result { + let fields = crate::gui::spec_fields(text)?; + let mut form = self.clone(); + form.model_path = fields.model_path; + form.prompt = fields.prompt; + form.max_tokens = fields.max_new_tokens.to_string(); + form.execution = fields.execution; + form.site = fields.site; + if let Some(layer) = fields.layer { + form.layer = layer.to_string(); + } + form.op = fields.operation; + if let Some(value) = fields.value { + form.value = value.to_string(); + } + form.source = fields.source.unwrap_or_else(|| "capture".to_string()); + if let Some(layer) = fields.source_layer { + form.source_layer = layer.to_string(); + } + form.token = fields.token; + if let Some(span) = fields.span { + form.span = span; + } + crate::gui::check_same_experiment(text, &form.spec_text()?)?; + Ok(form) + } + pub(super) fn build_run_request(&self) -> Result { let layer = if per_layer(&self.site) { Some( diff --git a/src/gui_native/harness.rs b/src/gui_native/harness.rs index 6b9b66e..6809a2e 100644 --- a/src/gui_native/harness.rs +++ b/src/gui_native/harness.rs @@ -988,6 +988,39 @@ pub(super) fn render_test_artifacts(directory: &std::path::Path) -> anyhow::Resu context.update_window(handle.into(), |_, _, cx| { console.update(cx, |console, _| console.palette_open = false); })?; + // The spec view: the experiment as TOML in the setup pane, in + // step with the form, then with an edit that the form cannot + // hold. + for (file, held) in [("spec", false), ("spec-held", true)] { + context.update_window(handle.into(), |_, window, cx| { + console.update(cx, |console, cx| { + console.view = View::Experiment; + if !console.spec_open { + console.toggle_spec(cx); + } + if held { + let edited = console.spec_shown.replace("seed = 0", "seed = 7"); + console.spec_edited(&edited, cx); + // As if typed: the view shows the edit. + console + .spec_input + .update(cx, |input, cx| input.set_value(edited, cx)); + } + cx.notify(); + }); + window.draw(cx).clear(cx); + })?; + context.run_until_parked(); + context.update_window(handle.into(), |_, window, cx| { + window.draw(cx).clear(cx); + })?; + context + .capture_screenshot(handle.into())? + .save(directory.join(format!("{name}-{appearance}-{file}.png")))?; + } + context.update_window(handle.into(), |_, _, cx| { + console.update(cx, |console, cx| console.toggle_spec(cx)); + })?; // Go to run lists saved runs, so only the seeded store has // anything for it to show. if seed_runs_requested() { diff --git a/src/gui_native/input.rs b/src/gui_native/input.rs index c0d9e75..f31638c 100644 --- a/src/gui_native/input.rs +++ b/src/gui_native/input.rs @@ -16,11 +16,15 @@ pub(super) enum InputId { MaxTokens, Prompt, PaletteQuery, + /// The spec view's TOML editor. + Spec, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(super) enum InputKind { Text, Multiline, + /// Multi-line, in the monospace face: the spec view's TOML. + Code, Integer, Decimal, } @@ -40,6 +44,8 @@ pub(super) struct TextInput { // Apply them at the next render; Kit set_value deliberately emits no Change. pending: Option, pending_placeholder: Option<&'static str>, + /// Drawn in the monospace face (`InputKind::Code`). + code: bool, } impl EventEmitter for TextInput {} impl TextInput { @@ -52,7 +58,7 @@ impl TextInput { window: &mut Window, cx: &mut Context, ) -> Self { - let control = if kind == InputKind::Multiline { + let control = if matches!(kind, InputKind::Multiline | InputKind::Code) { let state = cx.new(|cx| { let mut state = TextareaState::new(window, cx).placeholder(placeholder); state.set_value(value, window, cx); @@ -89,6 +95,7 @@ impl TextInput { control, pending: None, pending_placeholder: None, + code: kind == InputKind::Code, } } #[cfg(all(test, feature = "gui-tests"))] @@ -106,6 +113,18 @@ impl TextInput { handle.focus(window, cx); } + /// The text the control holds: the value set for the next render, if + /// there is one, else the text on screen. + pub(super) fn value(&self, cx: &App) -> String { + if let Some(value) = &self.pending { + return value.to_string(); + } + match &self.control { + Control::Line(state) => state.read(cx).value().to_string(), + Control::Paragraph(state) => state.read(cx).value().to_string(), + } + } + pub(super) fn set_value(&mut self, value: impl Into, cx: &mut Context) { self.pending = Some(value.into()); cx.notify(); @@ -141,12 +160,17 @@ impl Render for TextInput { state.set_placeholder(placeholder, window, cx) }); } + let (font, size, line) = if self.code { + (super::FONT_MONO_NAME, 13.0, 20.0) + } else { + (super::FONT_ARABIC_NAME, 18.0, 30.0) + }; Textarea::new(state) .h_full() .w_full() - .font_family(super::FONT_ARABIC_NAME) - .text_size(px(super::theme::scaled(18.0))) - .line_height(px(super::theme::scaled(30.0))) + .font_family(font) + .text_size(px(super::theme::scaled(size))) + .line_height(px(super::theme::scaled(line))) .into_any_element() } } diff --git a/src/gui_native/kit_tests.rs b/src/gui_native/kit_tests.rs index 7876098..152867d 100644 --- a/src/gui_native/kit_tests.rs +++ b/src/gui_native/kit_tests.rs @@ -1234,3 +1234,115 @@ async fn editor_keys_drive_the_palette_go_to_run_and_the_result_tabs(cx: &mut Te }) .unwrap(); } + +#[gpui_kit::test] +async fn the_spec_view_follows_the_form_and_edits_it_back(cx: &mut TestAppContext) { + let cmd = |key: &str| { + if cfg!(target_os = "macos") { + format!("cmd-{key}") + } else { + format!("ctrl-{key}") + } + }; + let (handle, console) = console_window(cx).await; + cx.update_window(handle, |_, window, cx| { + console.update(cx, |console, cx| { + console.model_path = "model.gguf".into(); + console.layer = "8".into(); + console.value = "0.5".into(); + console.goto(View::Experiment, cx); + console.focus_handle.focus(window, cx); + }); + window.draw(cx).clear(cx); + assert!(window + .try_find(SharedString::from("setup-advanced")) + .is_some()); + // The palette opens the view. + window.press(&cmd("shift-p"), cx); + window.input("spec view", cx); + }) + .unwrap(); + cx.update_window(handle, |_, window, cx| { + assert_eq!( + console.read(cx).palette_candidates().first(), + Some(&super::palette::Command::ToggleSpec) + ); + window.press("enter", cx); + window.draw(cx).clear(cx); + assert!(console.read(cx).spec_open); + assert!(window.try_find(SharedString::from("spec-copy")).is_some()); + assert!( + window + .try_find(SharedString::from("setup-advanced")) + .is_none(), + "the view takes the place of the form" + ); + let shown = console.read(cx).spec_shown.clone(); + assert!(shown.contains("factor = 0.5"), "{shown}"); + assert_eq!(console.read(cx).spec_input.read(cx).value(cx), shown); + + // An edit that the form can hold changes the form. + let edited = shown + .replace("factor = 0.5", "factor = 0.25") + .replace("layers = [8]", "layers = [3]"); + assert_ne!(edited, shown); + console.update(cx, |console, cx| console.spec_edited(&edited, cx)); + { + let console = console.read(cx); + assert_eq!( + (console.value.as_str(), console.layer.as_str()), + ("0.25", "3") + ); + assert!(!console.spec_held); + assert_eq!(console.spec_note, None); + } + + // An edit that the form cannot hold stays in the view, with the + // reason, and the form keeps its values. + let seeded = edited.replace("seed = 0", "seed = 7"); + assert_ne!(seeded, edited); + console.update(cx, |console, cx| console.spec_edited(&seeded, cx)); + { + let console = console.read(cx); + assert!(console.spec_held); + let note = console.spec_note.clone().unwrap_or_default(); + assert!(note.contains("experiment.seed"), "{note}"); + assert_eq!(console.value, "0.25"); + } + window.draw(cx).clear(cx); + assert!(window.try_find(SharedString::from("spec-revert")).is_some()); + // An edit back to the form's spec drops the hold. + let back = console.read(cx).spec_shown.clone(); + console.update(cx, |console, cx| console.spec_edited(&back, cx)); + assert!(!console.read(cx).spec_held); + assert_eq!(console.read(cx).spec_note, None); + console.update(cx, |console, cx| console.spec_edited(&seeded, cx)); + assert!(console.read(cx).spec_held); + // While it holds the edit, the view does not follow the form. + console.update(cx, |console, _| console.value = "2".into()); + window.draw(cx).clear(cx); + assert!(!console.read(cx).spec_shown.contains("factor = 2")); + + // Revert drops the edit and shows the form's spec again. + console.update(cx, |console, cx| console.revert_spec(cx)); + window.draw(cx).clear(cx); + { + let console = console.read(cx); + assert!(!console.spec_held); + assert!( + console.spec_shown.contains("factor = 2"), + "{}", + console.spec_shown + ); + } + assert!(window.try_find(SharedString::from("spec-revert")).is_none()); + + // The header button closes the view. + console.update(cx, |console, cx| console.toggle_spec(cx)); + window.draw(cx).clear(cx); + assert!(window + .try_find(SharedString::from("setup-advanced")) + .is_some()); + }) + .unwrap(); +} diff --git a/src/gui_native/palette.rs b/src/gui_native/palette.rs index d1ac95e..c776ec2 100644 --- a/src/gui_native/palette.rs +++ b/src/gui_native/palette.rs @@ -47,6 +47,7 @@ pub(super) enum Command { ShowTokens, ShowTrace, KeyboardShortcuts, + ToggleSpec, ToggleSidebar, ToggleTheme, TogglePresentation, @@ -56,7 +57,7 @@ impl Command { /// Catalog order: creation first, then navigation, then view toggles, /// then the experiment flow -- the order a new user discovers in and a /// regular user stops reading. - pub(super) const ALL: [Command; 20] = [ + pub(super) const ALL: [Command; 21] = [ Command::NewExperiment, Command::LoadModel, Command::RunExperiment, @@ -74,6 +75,7 @@ impl Command { Command::ShowTokens, Command::ShowTrace, Command::KeyboardShortcuts, + Command::ToggleSpec, Command::ToggleSidebar, Command::ToggleTheme, Command::TogglePresentation, @@ -98,6 +100,7 @@ impl Command { Command::ShowTokens => "Show Tokens", Command::ShowTrace => "Show Raw trace", Command::KeyboardShortcuts => "Keyboard shortcuts", + Command::ToggleSpec => "Toggle spec view (TOML)", Command::ToggleSidebar => "Toggle sidebar", Command::ToggleTheme => "Toggle theme", Command::TogglePresentation => "Presentation mode", @@ -124,6 +127,7 @@ impl Command { Command::ShowTokens => "Result tab: token-by-token comparison", Command::ShowTrace => "Result tab: the raw event trace", Command::KeyboardShortcuts => "Every key the console answers to", + Command::ToggleSpec => "Read and edit the experiment as ember.experiment.v1 TOML", Command::ToggleSidebar => "Show or hide the navigation sidebar", Command::ToggleTheme => "Cycle system, dark, light", Command::TogglePresentation => "Larger text, sidebar hidden", diff --git a/src/gui_native/tests.rs b/src/gui_native/tests.rs index 760eb94..c234415 100644 --- a/src/gui_native/tests.rs +++ b/src/gui_native/tests.rs @@ -368,3 +368,21 @@ fn a_mark_is_split_across_the_lines_it_covers() { vec![("ab", None), ("", None), ("cd", None)] ); } + +#[test] +fn a_spec_without_a_source_gives_the_default_source() { + let mut zeroed = form(); + zeroed.op = "replace".to_string(); + zeroed.source = "zero".to_string(); + // The spec of a `zero` operation has no source. + let mut target = form(); + target.op = "zero".to_string(); + let text = target.spec_text().expect("spec"); + let edited = zeroed + .with_spec(&text) + .expect("the form holds a zero operation"); + assert_eq!( + (edited.op.as_str(), edited.source.as_str()), + ("zero", "capture") + ); +} diff --git a/src/gui_native/views.rs b/src/gui_native/views.rs index 2ba7ec8..3635ace 100644 --- a/src/gui_native/views.rs +++ b/src/gui_native/views.rs @@ -484,7 +484,11 @@ impl Console { // can scroll it; a no-op outside `test-support`. .test_support() .flex_1() - .min_w(px(0.0)) + .min_w(px(if self.spec_open { + super::workspace::SPEC_RESULTS_MIN_WIDTH + } else { + 0.0 + })) .h_full() // Vertical scroll only: a wide page must not push the // inspector off the window (see the width tests). @@ -2861,6 +2865,7 @@ const SIDEBAR_MIN_WINDOW: f32 = 1240.0; impl Render for Console { fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { + self.sync_spec_view(cx); // Narrow windows give up the sidebar so the two workspace panes keep // the room they need. The stored flag is untouched: widening the // window brings it back exactly as the user left it. diff --git a/src/gui_native/workspace.rs b/src/gui_native/workspace.rs index d8dd174..938fe1c 100644 --- a/src/gui_native/workspace.rs +++ b/src/gui_native/workspace.rs @@ -26,6 +26,12 @@ use std::sync::Arc; /// Width of the setup pane. pub(super) const SETUP_WIDTH: f32 = 372.0; +/// Width of the setup pane while it shows the spec view: room for a TOML +/// line such as a `source = { ... }` table. It shrinks toward +/// `SETUP_WIDTH` before the results go below `SPEC_RESULTS_MIN_WIDTH`. +pub(super) const SPEC_WIDTH: f32 = 560.0; +/// The narrowest the results column becomes to make room for the spec view. +pub(super) const SPEC_RESULTS_MIN_WIDTH: f32 = 520.0; /// A result kept to judge later runs against. Only what the comparison shows: /// the per-layer series, the landmarks and the intervention's words. @@ -648,12 +654,20 @@ impl Console { ) }); + let spec_view = self.spec_open.then(|| self.spec_view(colors, cx)); let rule = || rule_h(colors); div() .flex() .flex_col() - .w(px(SETUP_WIDTH)) - .flex_none() + .map(|pane| { + if self.spec_open { + pane.w(px(SPEC_WIDTH)) + .min_w(px(SETUP_WIDTH)) + .flex_shrink(1.0) + } else { + pane.w(px(SETUP_WIDTH)).flex_none() + } + }) .min_h(px(0.0)) .h_full() .bg(colors.surface) @@ -685,25 +699,99 @@ impl Console { console.examples_open = !console.examples_open; cx.notify(); }), - )), + )) + .child( + text_button( + "setup-spec", + if self.spec_open { "Form" } else { "TOML" }, + cx.listener(|console, _: &ClickEvent, _window, cx| { + console.toggle_spec(cx); + }), + ) + .tooltip(if self.spec_open { + "Show the form" + } else { + "Show the experiment as ember.experiment.v1 TOML" + }), + ), ) .when(self.examples_open, |pane| { pane.child(self.examples_list(colors, cx)) }) - .child(if collapse_model { model_summary } else { model }) - .child(rule()) - .child(prompt) - .child(length) - .child(rule()) - .child(change) - .child(where_) - .child(target) - .child(rule()) - .child(advanced), + .when_some(spec_view, |pane, view| pane.child(view)) + .when(!self.spec_open, |pane| { + pane.child(if collapse_model { model_summary } else { model }) + .child(rule()) + .child(prompt) + .child(length) + .child(rule()) + .child(change) + .child(where_) + .child(target) + .child(rule()) + .child(advanced) + }), ) .child(footer) } + /// The spec view: the experiment that the form runs, as editable + /// `ember.experiment.v1` TOML. An edit that the form can hold updates + /// the form; the view keeps any other edit, with the reason, until + /// Revert. + fn spec_view(&self, colors: &Colors, cx: &mut Context) -> Div { + let status = match &self.spec_note { + Some(note) => label(note.clone(), Type::META, colors.warn), + None => label( + "Matches the form. An edit here changes the form.", + Type::META, + colors.text_faint, + ), + }; + div() + .flex() + .flex_col() + .flex_1() + .gap(px(Space::SM)) + .child( + div() + .flex_1() + .min_h(px(super::theme::scaled(360.0))) + .w_full() + .child(self.spec_input.clone()), + ) + .child(status) + .child( + div() + .flex() + .flex_row() + .gap(px(Space::SM)) + .child( + text_button( + "spec-copy", + "Copy", + cx.listener(|console, _: &ClickEvent, _window, cx| { + let text = console.spec_input.read(cx).value(cx); + cx.write_to_clipboard(ClipboardItem::new_string(text)); + }), + ) + .tooltip("Copy the TOML, to save it and run it with ember experiment run"), + ) + .when(self.spec_held, |row| { + row.child( + text_button( + "spec-revert", + "Revert", + cx.listener(|console, _: &ClickEvent, _window, cx| { + console.revert_spec(cx); + }), + ) + .tooltip("Drop the edit and show the form's spec again"), + ) + }), + ) + } + /// One cell per layer: where the chosen layer sits in the model, and a /// click target for every layer. After a sweep the cells are tinted by how /// far changing that layer moved the model, so the track doubles as the