File size: 9,718 Bytes
2109241 18de23f 2109241 4a54697 2109241 18de23f 2109241 18de23f 2109241 18de23f 2109241 4a54697 2109241 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | //! Strict JSON operation schema, path ownership and workflow dispatch.
use std::path::{Path, PathBuf};
use lispm_llm::{runtime, workflow};
use serde::Deserialize;
use serde_json::{Value, json};
use crate::{ApiError, jobs};
/// Single protocol definition shared by native Emacs and arbitrary C FFI clients.
#[derive(Deserialize)]
#[serde(tag = "op", rename_all = "kebab-case", deny_unknown_fields)]
enum Request {
/// Discover ABI version and capabilities without touching model state.
Version {},
/// Copy immutable inputs into a new user-owned training project.
ProjectCreate {
project: PathBuf,
base_model: PathBuf,
training_data: PathBuf,
benchmark_data: PathBuf,
#[serde(default)]
task_suite: Option<workflow::TaskSuiteOptions>,
},
/// Inspect project generations, runs and current activation.
ProjectInfo { project: PathBuf },
/// Append a single supported or rejection example with optimistic concurrency.
AddExample {
project: PathBuf,
generation: String,
instruction: String,
label: String,
#[serde(default)]
hard: bool,
},
/// Import an explicit versioned examples file with the same validation rules.
ImportDataset {
project: PathBuf,
generation: String,
path: PathBuf,
},
/// Replace only authored examples, allowing explicit correction and removal.
ReplaceDataset {
project: PathBuf,
generation: String,
path: PathBuf,
},
/// Reopen durable benchmark evidence after the creating job was released.
RunReport { project: PathBuf, run: String },
/// Start fresh-weight native training against a frozen data generation.
Train {
project: PathBuf,
steps: u32,
seed: u64,
threads: i32,
},
/// Compare one candidate and its baseline on the identical frozen benchmark.
Benchmark {
project: PathBuf,
run: String,
threads: i32,
},
/// Freeze only a benchmark-qualified candidate into a finalized artifact.
Finalize {
project: PathBuf,
run: String,
threads: i32,
},
/// Explicitly choose a finalized artifact for the next inference session.
Activate { project: PathBuf, run: String },
/// Restore the previous verified activation, including the initial baseline.
Rollback { project: PathBuf },
/// Inspect continual-learning quarantine and episodic status for a project.
LearnStatus { project: PathBuf },
/// Record one external oracle observation into quarantine and episodic memory.
LearnObserve {
project: PathBuf,
observation: workflow::ObservationInput,
},
/// Synchronize clean quarantine supervision into examples.json and expunge conflicts.
LearnSync { project: PathBuf },
/// Merge newly harvested Emacs or lispm-kit actions into <project>/catalog.json.
CatalogSync {
project: PathBuf,
catalog: lispm_llm::catalog::ActionCatalog,
},
/// Observe a nonblocking worker snapshot.
JobPoll { job: u64 },
/// Request cooperative cancellation and return its current state.
JobCancel { job: u64 },
/// Release a retained handle and cancel any uncommitted work.
JobRelease { job: u64 },
}
/// Reject ambient relative paths before any filesystem or worker side effect.
fn absolute(path: &Path) -> Result<(), ApiError> {
if !path.is_absolute() {
return Err(ApiError::RelativePath);
}
Ok(())
}
/// Serialize one concrete domain value while preserving typed serde failures.
fn value(result: impl serde::Serialize) -> Result<Value, ApiError> {
Ok(serde_json::to_value(result)?)
}
/// Parse the operation and select one explicit side-effect boundary.
pub(crate) fn dispatch(input: &str) -> Result<Value, ApiError> {
if input.len() > 8 * 1024 * 1024 {
return Err(ApiError::RequestTooLarge);
}
match serde_json::from_str(input)? {
Request::Version {} => {
Ok(json!({"abi":1, "version":env!("CARGO_PKG_VERSION"), "actions":48}))
}
Request::ProjectCreate {
project,
base_model,
training_data,
benchmark_data,
task_suite,
} => {
for path in [&project, &base_model, &training_data, &benchmark_data] {
absolute(path)?;
}
if let Some(tasks) = &task_suite {
for path in [&tasks.data, &tasks.runner, &tasks.emacs] {
absolute(path)?;
}
}
value(workflow::init_project_with_tasks(
&project,
&base_model,
&training_data,
&benchmark_data,
task_suite.as_ref(),
)?)
}
Request::ProjectInfo { project } => {
absolute(&project)?;
value(workflow::project_info(&project)?)
}
Request::AddExample {
project,
generation,
instruction,
label,
hard,
} => {
absolute(&project)?;
let examples = serde_json::to_vec(
&json!({"format_version":1,"examples":[{"instruction":instruction,"label":label,"hard":hard}]}),
)?;
value(workflow::import_examples(&project, &examples, &generation)?)
}
Request::ImportDataset {
project,
generation,
path,
} => import_dataset(project, generation, path, false),
Request::ReplaceDataset {
project,
generation,
path,
} => import_dataset(project, generation, path, true),
Request::RunReport { project, run } => {
absolute(&project)?;
value(workflow::benchmark_report(&project, &run)?)
}
Request::Train {
project,
steps,
seed,
threads,
} => train_job(
project,
workflow::RunOptions {
steps,
seed,
threads,
},
),
Request::Benchmark {
project,
run,
threads,
} => benchmark_job(project, run, threads),
Request::Finalize {
project,
run,
threads,
} => finalize_job(project, run, threads),
Request::Activate { project, run } => {
absolute(&project)?;
value(workflow::activate_run(&project, &run)?)
}
Request::Rollback { project } => {
absolute(&project)?;
value(workflow::rollback(&project)?)
}
Request::LearnStatus { project } => {
absolute(&project)?;
value(workflow::learn_status(&project)?)
}
Request::LearnObserve {
project,
observation,
} => {
absolute(&project)?;
value(workflow::learn_observe(&project, &observation)?)
}
Request::LearnSync { project } => {
absolute(&project)?;
value(workflow::learn_sync(&project)?)
}
Request::CatalogSync { project, catalog } => {
absolute(&project)?;
value(workflow::sync_catalog(&project, &catalog)?)
}
Request::JobPoll { job } => value(jobs::poll(job)?),
Request::JobCancel { job } => value(jobs::cancel(job)?),
Request::JobRelease { job } => {
jobs::release(job)?;
Ok(json!({}))
}
}
}
/// Import or explicitly replace user-authored examples after optimistic validation.
fn import_dataset(
project: PathBuf,
generation: String,
path: PathBuf,
replace: bool,
) -> Result<Value, ApiError> {
absolute(&project)?;
absolute(&path)?;
let examples = std::fs::read(path)?;
if replace {
value(workflow::replace_examples(
&project,
&examples,
&generation,
)?)
} else {
value(workflow::import_examples(&project, &examples, &generation)?)
}
}
/// Start one training operation with owned paths and native option types.
fn train_job(project: PathBuf, options: workflow::RunOptions) -> Result<Value, ApiError> {
absolute(&project)?;
runtime::configure_threads(options.threads)?;
let job = jobs::start(move |cancelled, progress| {
value(workflow::train_project(
&project, &options, cancelled, progress,
)?)
})?;
Ok(json!({"job":job}))
}
/// Benchmark one existing candidate without blocking a foreign caller.
fn benchmark_job(project: PathBuf, run: String, threads: i32) -> Result<Value, ApiError> {
absolute(&project)?;
runtime::configure_threads(threads)?;
let job = jobs::start(move |cancelled, progress| {
value(workflow::benchmark_run(
&project, &run, cancelled, progress,
)?)
})?;
Ok(json!({"job":job}))
}
/// Finalize recorded eligible evidence, retaining committed output on a late cancel.
fn finalize_job(project: PathBuf, run: String, threads: i32) -> Result<Value, ApiError> {
absolute(&project)?;
runtime::configure_threads(threads)?;
let job = jobs::start(move |cancelled, progress| {
use std::sync::atomic::Ordering;
if cancelled.load(Ordering::Acquire) {
return Err(ApiError::Cancelled);
}
progress(json!({"phase":"finalizing", "run":run}));
value(workflow::finalize_run_with_control(
&project, &run, cancelled,
)?)
})?;
Ok(json!({"job":job}))
}
|