Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 113 additions & 3 deletions apps/studio/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
path::{Path, PathBuf},
time::{SystemTime, UNIX_EPOCH},
};
use tauri::Emitter;

const MAX_STUDIO_ARTIFACT_BYTES: u64 = 25 * 1024 * 1024;
const PROJECT_MANIFEST_FILE: &str = "ignitionrl.project.json";
Expand All @@ -16,6 +17,7 @@ const CHECKPOINT_INDEX_FILE: &str = "index.json";
const STUDIO_LOCAL_TRAINING_ALGORITHM: &str = "studio-local-headless";
const DEFAULT_LOCAL_TRAINING_EPISODES: u64 = 4;
const DEFAULT_LOCAL_TRAINING_MAX_STEPS: u64 = 300;
const TRAINING_METRIC_EVENT: &str = "ignitionrl://training-metric";

#[derive(Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -93,25 +95,35 @@ fn open_ignition_project_directory(path: String) -> Result<IgnitionProjectDirect

#[tauri::command]
fn start_local_training_run(
app: tauri::AppHandle,
path: String,
env_id: String,
episodes: Option<u64>,
max_steps: Option<u64>,
) -> Result<LocalTrainingRunResult, String> {
start_local_training_path(
let result = start_local_training_path(
PathBuf::from(path),
env_id,
episodes.unwrap_or(DEFAULT_LOCAL_TRAINING_EPISODES),
max_steps.unwrap_or(DEFAULT_LOCAL_TRAINING_MAX_STEPS),
)
)?;

emit_training_metric_event(&app, &result.project.path, &result.training_session)?;

Ok(result)
}

#[tauri::command]
fn cancel_local_training_run(
app: tauri::AppHandle,
path: String,
run_id: String,
) -> Result<LocalTrainingRunResult, String> {
cancel_local_training_path(PathBuf::from(path), run_id)
let result = cancel_local_training_path(PathBuf::from(path), run_id)?;

emit_training_metric_event(&app, &result.project.path, &result.training_session)?;

Ok(result)
}

fn read_studio_artifact_path(path: PathBuf) -> Result<StudioArtifactFile, String> {
Expand Down Expand Up @@ -905,6 +917,74 @@ fn training_session_view(
Ok(session)
}

fn emit_training_metric_event(
app: &tauri::AppHandle,
project_path: &str,
training_session: &Value,
) -> Result<(), String> {
let event = training_metric_event(project_path, training_session)?;

app.emit(TRAINING_METRIC_EVENT, event)
.map_err(|error| format!("Unable to emit local training metric event: {error}"))
}

fn training_metric_event(project_path: &str, training_session: &Value) -> Result<Value, String> {
let run = training_session
.get("run")
.ok_or_else(|| "Training session metric event requires a run.".to_string())?;
let completed_episodes = training_session
.get("completedEpisodes")
.and_then(Value::as_u64)
.unwrap_or(0);
let updated_at = training_session
.get("updatedAt")
.and_then(Value::as_str)
.or_else(|| run.get("updatedAt").and_then(Value::as_str))
.unwrap_or_else(|| json_string(run, "createdAt").unwrap_or("unknown"));
let values = training_session
.get("latestEpisode")
.and_then(|episode| episode.get("metrics"))
.cloned()
.unwrap_or_else(|| json!({}));
let mut event = json!({
"projectPath": project_path,
"sessionId": json_string(training_session, "id")?,
"runId": json_string(run, "id")?,
"envId": json_string(run, "envId")?,
"algorithm": json_string(run, "algorithm")?,
"status": json_string(training_session, "status")?,
"requestedEpisodes": training_session
.get("requestedEpisodes")
.and_then(Value::as_u64)
.unwrap_or(DEFAULT_LOCAL_TRAINING_EPISODES),
"completedEpisodes": completed_episodes,
"progress": training_session
.get("progress")
.and_then(Value::as_f64)
.unwrap_or(0.0),
"t": completed_episodes,
"values": values,
"createdAt": updated_at,
});
let event_object = event
.as_object_mut()
.ok_or_else(|| "Training metric event must be a JSON object.".to_string())?;

if let Some(latest_episode) = training_session.get("latestEpisode") {
event_object.insert("latestEpisode".to_string(), latest_episode.clone());
}

if let Some(summary) = training_session.get("summary") {
event_object.insert("summary".to_string(), summary.clone());
}

if let Some(error) = training_session.get("error") {
event_object.insert("error".to_string(), error.clone());
}

Ok(event)
}

fn json_string<'a>(value: &'a Value, key: &str) -> Result<&'a str, String> {
value
.get(key)
Expand Down Expand Up @@ -1245,6 +1325,21 @@ mod tests {
.and_then(Value::as_bool),
Some(true),
);
let start_event =
training_metric_event(&started.project.path, &started.training_session).unwrap();

assert_eq!(
start_event.get("status").and_then(Value::as_str),
Some("running"),
);
assert_eq!(
start_event
.get("values")
.and_then(Value::as_object)
.unwrap()
.len(),
0,
);

let duplicate =
start_local_training_path(root.clone(), "Target2D-v0".to_string(), 4, 300).unwrap_err();
Expand Down Expand Up @@ -1287,6 +1382,21 @@ mod tests {
.and_then(Value::as_bool),
Some(false),
);
let cancel_event =
training_metric_event(&cancelled.project.path, &cancelled.training_session).unwrap();

assert_eq!(
cancel_event.get("status").and_then(Value::as_str),
Some("cancelled"),
);
assert_eq!(
cancel_event
.get("values")
.and_then(|values| values.get("episodeLength"))
.and_then(Value::as_u64),
Some(1),
);
assert!(cancel_event.get("latestEpisode").is_some());

let run_file = root
.join("runs")
Expand Down
153 changes: 152 additions & 1 deletion apps/studio/src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import type {
StudioWorkspaceView,
} from "@ignitionrl/sdk";
import { App } from "./App";
import type { NativeIgnitionProjectDirectory, NativeIgnitionProjectRunDirectory, NativeStudioApi } from "./native";
import type {
NativeIgnitionProjectDirectory,
NativeIgnitionProjectRunDirectory,
NativeStudioApi,
NativeTrainingMetricEvent,
} from "./native";
import { sampleWorkspace } from "./sample-workspace";

(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
Expand Down Expand Up @@ -442,6 +447,122 @@ describe("Studio app shell", () => {
act(() => renderer?.unmount());
});

test("ingests live training metrics and preserves sparse terminal updates", async () => {
let renderer: ReactTestRenderer | undefined;
let currentProject = nativeProjectDirectory();
let metricListener: ((event: NativeTrainingMetricEvent) => void) | undefined;
const nativeApi: NativeStudioApi = {
isAvailable: () => true,
openIgnitionProjectDirectory: async () => currentProject,
openStudioArtifactFile: async () => undefined,
startLocalTrainingRun: async () => {
currentProject = nativeProjectWithLocalTraining("running");

return {
project: currentProject,
trainingSession: nativeTrainingSession("running"),
};
},
cancelLocalTrainingRun: async () => {
throw new Error("Unexpected local training cancellation.");
},
listenToTrainingMetricEvents: async (listener) => {
metricListener = listener;

return () => {
metricListener = undefined;
};
},
};

act(() => {
renderer = create(React.createElement(App, {
initialWorkspace: sampleWorkspace,
nativeApi,
}));
});

await act(async () => {});

await act(async () => {
await renderer?.root.findByProps({
"aria-label": "Open native project directory",
}).props.onClick();
});

await act(async () => {
await renderer?.root.findByProps({
"aria-label": "Start local training",
}).props.onClick();
});

expect(metricListener).toBeDefined();

await act(async () => {
metricListener?.(liveMetricEvent({
status: "running",
completedEpisodes: 1,
progress: 0.25,
t: 1,
values: {
totalReward: 3.5,
success: 0,
},
latestEpisode: {
episode: 0,
episodeId: "studio-local-run:episode:0",
trace: {
episodeId: "studio-local-run:episode:0",
path: "runs/studio-local-run/traces/studio-local-run_episode_0.json",
},
totalReward: 3.5,
length: 7,
success: false,
terminated: false,
truncated: false,
metrics: {
totalReward: 3.5,
success: 0,
},
},
}));
});

let text = JSON.stringify(renderer?.toJSON());
expect(renderer?.root.findByProps({ className: "training-progress-label" })
.findByType("span").children.join("")).toBe("1 / 4 episodes");
expect(text).toContain("3.5");
expect(text).toContain("studio-local-run:episode:0");
expect(text).toContain("totalReward");
expect(chartPointLabel(renderer)).toBe("2 pts");
expect(text).not.toContain("No metric points.");

await act(async () => {
metricListener?.(liveMetricEvent({
status: "cancelled",
completedEpisodes: 1,
progress: 0.25,
t: 2,
values: {},
createdAt: "2026-05-28T00:03:00.000Z",
}));
});

text = JSON.stringify(renderer?.toJSON());

expect(text).toContain("cancelled");
expect(text).toContain("3.5");
expect(renderer?.root.findByProps({ className: "training-progress-label" })
.findByType("span").children.join("")).toBe("1 / 4 episodes");
expect(renderer?.root.findByProps({
"aria-label": "Cancel local training",
}).props.disabled).toBe(true);
expect(chartPointLabel(renderer)).toBe("2 pts");
expect(text).not.toContain("No metric points.");

act(() => renderer?.unmount());
});

test("loads another run episode artifact and switches replay plus reward views", async () => {
let renderer: ReactTestRenderer | undefined;
const episodeOneView = secondEpisodeRunView();
Expand Down Expand Up @@ -780,6 +901,36 @@ describe("Studio app shell", () => {
});
});

function liveMetricEvent(
overrides: Partial<NativeTrainingMetricEvent>,
): NativeTrainingMetricEvent {
return {
projectPath: "/tmp/project.ignitionrl",
sessionId: "studio-local-run:session",
runId: "studio-local-run",
envId: "Target2D-v0",
algorithm: "studio-local-headless",
status: "running",
requestedEpisodes: 4,
completedEpisodes: 0,
progress: 0,
t: 0,
values: {},
createdAt: "2026-05-28T00:02:30.000Z",
...overrides,
};
}

function chartPointLabel(renderer: ReactTestRenderer | undefined): string {
const chartPanel = renderer?.root.findByProps({ className: "chart-panel" });

return chartPanel
?.findByProps({ className: "panel-heading compact" })
.findByType("span")
.children
.join("") ?? "";
}

function nativeProjectDirectory(): NativeIgnitionProjectDirectory {
const createdAt = "2026-05-28T00:00:00.000Z";
const runUpdatedAt = "2026-05-28T00:01:00.000Z";
Expand Down
Loading
Loading