8000 Clarify browser and error documentation on main by adityamukho · Pull Request #984 · project-minigraf/minigraf · GitHub
[go: up one dir, main page]

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
35 changes: 20 additions & 15 deletions src/browser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,10 @@ impl Db {
));
}

/// Reserve the durability boundary synchronously, before a commit's JS
/// promise body is deferred. Barriers always resolve: each operation
/// reports its own error, with no unhandled predecessor rejection.
/// Reserve the flush's place in the IndexedDB sequence synchronously,
/// before returning the JavaScript promise that does the work. Internal
/// barriers resolve even on failure, so an earlier flush cannot cause an
/// unhandled promise rejection. Each operation reports its own error.
fn flush(self: &Rc<Self>, work: FlushWork) -> js_sys::Promise {
let idb = self
.borrow()
Expand Down Expand Up @@ -477,7 +478,7 @@ impl BrowserDb {
/// transact/retract calls reject with `[STG-046]` without changing memory.
/// Queries and rules remain available; close and reopen to recover.
///
/// Returns a `Promise<string>` in JavaScript. The JSON shape is:
/// Returns a `Promise<string>` in JavaScript. The string contains one of these JSON results:
/// - Query: `{"variables": [...], "results": [[...], ...]}`
/// - Transact: `{"transacted": <tx_id>}`
/// - Retract: `{"retracted": <tx_id>}`
Expand Down Expand Up @@ -572,9 +573,10 @@ impl BrowserDb {

/// Flush all dirty pages to IndexedDB.
///
/// Waits for every IndexedDB write applied before this call, even if it
/// has no pages of its own. Later writes are outside this boundary. Its
/// own flush failure is `[STG-048]`; an earlier failed flush is `[STG-046]`.
/// Waits for every IndexedDB write applied before this call, even if this
/// checkpoint has no pages to flush. It does not wait for later writes.
/// If this call's flush fails, it returns
/// `[STG-048]`. If an earlier flush failed, it returns `[STG-046]`.
/// Close and reopen to recover. With no earlier work or dirty pages it
/// resolves immediately. In memory and OPFS every commit is already stored.
pub async fn checkpoint(&self) -> Result<(), JsValue> {
Expand Down Expand Up @@ -677,8 +679,8 @@ impl BrowserDb {
.filter(|id| !new_ids.contains(id))
.collect()
});
// Preserve the existing memory contract: an import becomes
// visible before its flush. Failure belongs to Db, not this pfs.
// Make the imported data visible in memory before flushing it.
// Record a flush failure on Db, not on the replacement storage.
inner.fact_storage = new_pfs.storage().clone();
inner.pfs = new_pfs;
inner.withheld.clear();
Expand Down Expand Up @@ -979,12 +981,14 @@ fn commit_stamped(
/// while open, discards them. After either, the transaction is finished:
/// `execute`, `incomingRefs` and `commit` fail with `[API-018]`, and `rollback`
/// does nothing. Calls back into this transaction from a user function of its
/// own statement fail with `[INT-001]` before lifecycle or argument checks.
/// own statement fail with `[INT-001]` before checking whether the transaction
/// has finished or validating arguments.
///
/// `incomingRefs(target, maxLinks, maxRecords)` synchronously returns a plain
/// object discriminated by `status`: complete links or an exhausted budget,
/// never a partial list. It reads earlier statements without staging writes,
/// taking a `tx_count` or starting a flush, even after a failed IndexedDB flush.
/// `incomingRefs(target, maxLinks, maxRecords)` returns a plain object, not a
/// promise. Check its `status` to tell whether it contains the complete list or
/// a limit was reached. It never returns a partial list. It reads earlier
/// statements without staging writes, taking a `tx_count`, or starting a flush,
/// even after a failed IndexedDB flush.
#[wasm_bindgen]
pub struct BrowserWriteTransaction {
db: Rc<Db>,
Expand Down Expand Up @@ -1227,7 +1231,8 @@ fn tx_execute(
}
}

/// Validate before applying. Empty staging still has a durability boundary.
/// Validate staged facts before applying them. With no facts, return empty
/// flush work so the caller can still wait for earlier IndexedDB writes.
fn tx_commit(inner: &mut BrowserDbInner, staged: crate::db::Staged) -> Result<FlushWork, JsValue> {
let facts = staged.pending_facts;
if facts.is_empty() {
Expand Down
25 changes: 9 additions & 16 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Structured runtime error codes surfaced through `MinigrafError`.
//! Runtime error codes returned in `MinigrafError`.
//!
//! See `docs/superpowers/specs/2026-08-25-structured-error-codes-design.md`
//! and `docs/ERROR_REFERENCE.md` for the design and the full code registry.
Expand Down Expand Up @@ -346,7 +346,7 @@ pub(crate) enum ErrorCode {
Int056,
}

/// Single source of truth: (code, code string, message template, category).
/// The canonical list of error codes, display strings, message templates, and categories.
///
/// The message template is verified against `docs/ERROR_REFERENCE.md`'s
/// "Error text" lines by the sync test in this module.
Expand Down Expand Up @@ -2079,26 +2079,19 @@ pub(crate) fn format_template(template: &str, args: &[&dyn fmt::Display]) -> Str

/// An `anyhow`-compatible error carrying a compile-time-checked [`ErrorCode`].
///
/// Built exclusively through [`bail_coded!`]/[`err_coded!`] and wrapped as an
/// `anyhow::Error`, so it rides through the crate's existing `anyhow::Result`
/// plumbing (`?`, `.context()`) unchanged. Recovered at the public API
/// boundary by [`MinigrafError::from`]`(anyhow::Error)` walking the error
/// chain for the first `CodedError` via `downcast_ref`.
/// Only [`bail_coded!`] and [`err_coded!`] build this error. They wrap it in
/// `anyhow::Error`, so `?` and `.context()` still work. At the public API,
/// [`MinigrafError::from`] uses `downcast_ref` to find the first `CodedError`
/// in the error chain.
pub(crate) struct CodedError {
code: ErrorCode,
message: String,
}

impl CodedError {
/// `#[cold]`/`#[inline(never)]`: `bail_coded!`/`err_coded!` expand to a
/// call to this at every one of the (roughly 300, now that #277's six
/// category PRs have all landed) error call sites across the crate.
/// Error construction is by definition the cold path, so forcing
/// it out of line keeps the lookup + `format_template` call from being
/// duplicated inline at each one — pure binary-size cost for a path
/// that, unlike the argument-array construction immediately at the call
/// site (which still inlines, and is cheap), has no per-call-site
/// specialization to gain from inlining.
/// Keep the registry lookup and message formatting out of line so all
/// error sites share the same code. Each site still builds its own small
/// argument array.
#[cold]
#[inline(never)]
pub(crate) fn new(code: ErrorCode, args: &[&dyn fmt::Display]) -> Self {
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ pub fn __input_order(on: bool) -> InputOrderGuard {
query::engine::set_input_order(on)
}

/// This thread's path events since the last call, which clears them: how
/// often queries took the paths the nightly acceptance tests guard (#863),
/// such as `ancestor_alternative_selected` and `bound_value_avet_range`.
/// Only for tests; built with the `test-hooks` feature only.
/// Return and reset this thread's query event counts. Nightly tests use them
/// to check which execution strategies ran (#863), such as
/// `ancestor_alternative_selected` and `bound_value_avet_range`.
/// For tests only. Requires the `test-hooks` feature.
#[cfg(feature = "test-hooks")]
#[doc(hidden)]
pub fn __path_events() -> std::collections::BTreeMap<&'static str, u64> {
Expand Down
Loading
0