repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by bugabinga

refactor: compress pass over sealed wasi, alloc, http, session, agent

10387faaaa9b17f4222a566a55ae8f9f0ccf3125

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (4)

+96 / −134 text lines

Diff layout

smith-alloc/src/lib.rs

Modified

+18 / −13 text lines

182182 let Some((outer, pad)) = padded(layout) else {
183183 return std::ptr::null_mut();
184184 };
185 let base = raw(outer);
185 // SAFETY: `raw` returned null or a block of `outer`.
186 unsafe { Self::stamp(raw(outer), pad, layout.size()) }
187 }
189 /// Tag a fresh `System` block with the current scope and charge `size`
190 /// to it; null passes through.
191 ///
192 /// # Safety
193 /// `base` is null or a block spanning `pad + size` bytes, `pad >= 1`.
194 unsafe fn stamp(base: *mut u8, pad: usize, size: usize) -> *mut u8 {
186195 if base.is_null() {
187196 return base;
188197 }
189198 let scope = current();
190 // SAFETY: `base` spans `pad + size` bytes and `pad >= 1`, so the user
191 // pointer and its tag byte lie inside the block; `pad` keeps alignment.
199 // SAFETY: the user pointer and its tag byte lie inside the block;
200 // `pad` is the alignment, so the user pointer stays aligned.
192201 let user = unsafe { base.add(pad) };
193202 unsafe { user.sub(1).write(scope as u8) };
194 record_alloc(scope, layout.size());
203 record_alloc(scope, size);
195204 user
196205 }
229238 else {
230239 return std::ptr::null_mut();
231240 };
232 // SAFETY: as `dealloc`; on success `System` moved the header with the data.
241 // SAFETY: as `dealloc`; on success `System` moved the header with the
242 // data and the old block is gone, so its charge is released first.
233243 unsafe {
234244 let tag = ptr.sub(1).read();
235245 let base = System.realloc(ptr.sub(pad), outer, new_outer.size());
236 if base.is_null() {
237 return base;
246 if !base.is_null() {
247 record_free(tag, layout.size());
238248 }
239 let user = base.add(pad);
240 let scope = current();
241 user.sub(1).write(scope as u8);
242 record_free(tag, layout.size());
243 record_alloc(scope, new_size);
244 user
249 Self::stamp(base, pad, new_size)
245250 }
246251 }

smith-harness/src/http.rs

Modified

+6 / −14 text lines

7474 }
7575 let response = prepared
7676 .send(&exchange.body[..])
77 .map_err(|e| transport_fault(&e.to_string()))?;
77 .map_err(|e| transient(e.to_string()))?;
7878 let head = HttpHead {
7979 status: response.status().as_u16(),
8080 };
155155fn admit(exchange: &HttpExchange, cancel: &CancelHandle) -> Result<()> {
156156 if exchange.body.len() > MAX_REQUEST_BYTES {
157 return Err(bounded("request body exceeds the size bound"));
157 return Err(transient(
158 "http effect: request body exceeds the size bound".to_string(),
159 ));
158160 }
159161 if cancel.is_cancelled() {
160162 return Err(SmithError::Cancelled);
199201 }
202fn transport_fault(message: &str) -> SmithError {
204const fn transient(message: String) -> SmithError {
203205 SmithError::Provider {
204 fault: ProviderFault::Transient {
205 message: message.to_string(),
206 },
207 }
208}
210fn bounded(message: &str) -> SmithError {
211 SmithError::Provider {
212 fault: ProviderFault::Transient {
213 message: format!("http effect: {message}"),
214 },
206 fault: ProviderFault::Transient { message },
215207 }

smith-harness/src/wasm.rs

Modified

+3 / −2 text lines

151151 /// Returns a plugin fault naming the failed lifecycle stage.
152152 pub fn load(engine: &PluginEngine, artifact: &Path) -> Result<Self> {
153153 let component = engine.validate(artifact)?;
154 let ceilings = PluginCeilings::default();
154155 let mut store = Store::new(
155156 engine.engine(),
156157 PluginHostState {
157158 monotonic_now: 0,
158 ceilings: PluginCeilings::default(),
159 ceilings,
159160 },
160161 );
161162 store.limiter(PluginHostState::limits);
162163 store
163 .set_fuel(PluginCeilings::default().fuel)
164 .set_fuel(ceilings.fuel)
164165 .map_err(|e| plugin_fault(format!("fuel setup failed: {e}")))?;
165166 let mut linker: Linker<PluginHostState> = Linker::new(engine.engine());
166167 PluginHost::add_to_linker::<PluginHostState, PluginHasSelf>(&mut linker, |state| state)

xtask/src/main.rs

Modified

+69 / −105 text lines

563563 return fail(&format!("missing crate manifest: {crate_name}\n"));
564564 }
565565 }
566 for rule in dependency_rules() {
566 for rule in DEPENDENCY_RULES {
567567 if !wanted.is_empty() && !wanted.iter().any(|name| name == rule.crate_name) {
568568 continue;
569569 }
576576 ));
577577 }
578578 };
579 for denied in rule.denied {
579 let denied = CRATES
580 .iter()
581 .filter(|name| **name != rule.crate_name && !rule.allowed.contains(name));
582 for denied in denied {
580583 if has_dependency(&manifest, denied) {
581584 return fail(&format!(
582585 "{} must not depend on {denied}\n",
588591 ExitCode::SUCCESS
594/// Library edges a crate may have; every other workspace crate is denied.
591595struct DependencyRule {
592596 crate_name: &'static str,
593 denied: &'static [&'static str],
597 allowed: &'static [&'static str],
596const fn dependency_rules() -> &'static [DependencyRule] {
597 &[
598 DependencyRule {
599 crate_name: "smith",
600 denied: &[
601 "smith-core",
602 "smith-ai",
603 "smith-ui",
604 "smith-tui",
605 "smith-harness",
606 "smith-cli",
607 "smith-alloc",
608 ],
609 },
610 DependencyRule {
611 crate_name: "smith-core",
612 denied: &[
613 "smith-ai",
614 "smith-ui",
615 "smith-tui",
616 "smith-harness",
617 "smith-cli",
618 "smith-alloc",
619 ],
620 },
621 DependencyRule {
622 crate_name: "smith-ai",
623 denied: &[
624 "smith-core",
625 "smith-ui",
626 "smith-tui",
627 "smith-harness",
628 "smith-cli",
629 "smith-alloc",
630 ],
631 },
632 DependencyRule {
633 crate_name: "smith-ui",
634 denied: &[
635 "smith-core",
636 "smith-ai",
637 "smith-tui",
638 "smith-harness",
639 "smith-cli",
640 "smith-alloc",
641 ],
642 },
643 DependencyRule {
644 crate_name: "smith-tui",
645 denied: &[
646 "smith-core",
647 "smith-ai",
648 "smith-harness",
649 "smith-cli",
650 "smith-alloc",
651 ],
652 },
653 DependencyRule {
654 crate_name: "smith-rpc",
655 denied: &[
656 "smith-ai",
657 "smith-ui",
658 "smith-tui",
659 "smith-harness",
660 "smith-cli",
661 "smith-alloc",
662 ],
663 },
664 DependencyRule {
665 crate_name: "smith-cli",
666 denied: &["smith-core", "smith-ai", "smith-ui", "smith-tui"],
667 },
668 DependencyRule {
669 crate_name: "xtask",
670 denied: &[
671 "smith",
672 "smith-core",
673 "smith-ai",
674 "smith-ui",
675 "smith-tui",
676 "smith-harness",
677 "smith-cli",
678 "smith-alloc",
679 ],
680 },
681 DependencyRule {
682 crate_name: "smith-alloc",
683 denied: &[
684 "smith",
685 "smith-core",
686 "smith-ai",
687 "smith-ui",
688 "smith-tui",
689 "smith-rpc",
690 "smith-harness",
691 "smith-cli",
692 "xtask",
693 ],
694 },
695 ]
696}
600/// The allowed-deps table of `.system/RULES.md`, architecture section.
601const DEPENDENCY_RULES: &[DependencyRule] = &[
602 DependencyRule {
603 crate_name: "smith",
604 allowed: &[],
605 },
606 DependencyRule {
607 crate_name: "smith-ui",
608 allowed: &["smith"],
609 },
610 DependencyRule {
611 crate_name: "smith-core",
612 allowed: &["smith"],
613 },
614 DependencyRule {
615 crate_name: "smith-ai",
616 allowed: &["smith"],
617 },
618 DependencyRule {
619 crate_name: "smith-tui",
620 allowed: &["smith", "smith-ui"],
621 },
622 DependencyRule {
623 crate_name: "smith-rpc",
624 allowed: &["smith", "smith-core"],
625 },
626 DependencyRule {
627 crate_name: "smith-harness",
628 allowed: &["smith", "smith-core", "smith-ai", "smith-tui", "smith-ui"],
629 },
630 DependencyRule {
631 crate_name: "smith-cli",
632 allowed: &["smith", "smith-harness", "smith-rpc", "smith-alloc"],
633 },
634 DependencyRule {
635 crate_name: "smith-alloc",
636 allowed: &[],
637 },
638 DependencyRule {
639 crate_name: "xtask",
640 allowed: &[],
641 },
642];
698644fn has_dependency(manifest: &str, crate_name: &str) -> bool {
699645 // Library edges only: the manifest up to the dev-dependency table.
717663#[cfg(test)]
718664mod tests {
719 use super::{binary_name, dev_home_dirs, has_dependency, target_dir};
665 use super::{CRATES, DEPENDENCY_RULES, binary_name, dev_home_dirs, has_dependency, target_dir};
720666 use std::path::PathBuf;
668 #[test]
669 fn every_crate_has_one_rule_over_known_crates() {
670 for name in CRATES {
671 let rules = DEPENDENCY_RULES
672 .iter()
673 .filter(|rule| rule.crate_name == *name);
674 assert_eq!(rules.count(), 1, "{name}");
675 }
676 assert_eq!(DEPENDENCY_RULES.len(), CRATES.len());
677 for rule in DEPENDENCY_RULES {
678 assert!(
679 rule.allowed
680 .iter()
681 .all(|dep| CRATES.contains(dep) && *dep != rule.crate_name)
682 );
683 }
684 }
722686 #[test]
723687 fn dev_home_covers_all_xdg_roots() {
724688 let keys: Vec<&str> = dev_home_dirs().iter().map(|(key, _)| *key).collect();