Skip to content

Commit f90deaf

Browse files
authored
Add reader-first Engine quick starts (#24)
1 parent 996ca0f commit f90deaf

2 files changed

Lines changed: 119 additions & 52 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Contributing
2+
3+
Thank you for helping improve the Stack Engine. Keep native Rust and browser WebAssembly behavior aligned, and include regression coverage for observable changes.
4+
5+
## Development setup
6+
7+
The workspace uses Rust 2024 with Rust 1.85 as its minimum supported version. Browser package work also requires Node.js and `wasm-bindgen`.
8+
9+
```sh
10+
rustup target add wasm32-unknown-unknown wasm32-wasip1
11+
npm ci
12+
```
13+
14+
## Quality gates
15+
16+
Run the checks relevant to your change, then the full repository suite before opening a pull request:
17+
18+
```sh
19+
cargo test --workspace
20+
STACK_SPECIFICATION_DIR=../specification cargo test -p stack-formatter --features conformance --test conformance
21+
STACK_SPECIFICATION_DIR=../specification cargo test -p stack-engine --features conformance
22+
python3 scripts/validate-svg.py
23+
cargo build -p stack-engine-wasm --target wasm32-unknown-unknown
24+
npm run layout:validate
25+
npm run build:wasm
26+
npm test
27+
npm run typecheck
28+
npm run pack:check
29+
CARGO_TARGET_WASM32_WASIP1_RUNNER=wasmtime cargo test -p stack-engine --lib --target wasm32-wasip1 cross_target_numeric_fixture
30+
cargo fmt --check
31+
cargo clippy --workspace --all-targets --all-features -- -D warnings
32+
cargo doc --workspace --no-deps
33+
```
34+
35+
CI includes the repository-only conformance corpus in its 95% line, function, and region coverage gates. Packaged crate unit tests remain independent of the repository corpus.
36+
37+
## Layout changes
38+
39+
The versioned layout corpus covers small, medium, and dense diagrams; groups and nested groups; authored rank and order; cross-boundary edges; labels; and caller-owned provider icons.
40+
41+
```sh
42+
cargo test -p stack-engine --test layout_corpus --locked
43+
cargo test --release -p stack-engine --test layout_corpus layout_runtime_stays_within_budget --locked -- --ignored --nocapture
44+
cargo test --release -p stack-engine --test language_intelligence language_intelligence_runtime_stays_within_budget --locked -- --ignored --nocapture
45+
npm run layout:gallery
46+
```
47+
48+
Follow the [review-first snapshot policy](./layout-corpus/README.md). Set `UPDATE_STACK_SNAPSHOTS=1` or `UPDATE_STACK_LAYOUT_SNAPSHOTS=1` only when intentionally regenerating a reviewed reference, and inspect the resulting SVGs before committing them.
49+
50+
## Releases
51+
52+
Public Cargo and npm artifacts are immutable and must come from an exact reviewed commit. Follow [RELEASING.md](./RELEASING.md) and the [Cargo release procedure](./docs/cargo-releasing.md); do not overwrite an existing tag or package version.
53+
54+
Keep changes focused, use English commit and pull request descriptions, and do not commit credentials, tokens, customer data, signing material, build output, or downloaded runtimes.

‎README.md‎

Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,95 @@
11
# Stack Engine
22

3-
`stack-sh/engine` is the pure Rust execution engine for Stack architecture diagrams.
3+
`stack-sh/engine` is the shared execution engine behind the [Stack CLI](https://github.com/stack-sh/cli) and [browser Playground](https://stack-diagram.com/). It formats and validates Stack source, calculates deterministic theme-aware layouts, and renders safe standalone SVGs with the same behavior in native Rust and browser WebAssembly.
44

5-
The workspace provides canonical Stack source formatting, protocol-neutral language intelligence, the pure `stack-engine` operation facade, deterministic theme-aware scene layout and orthogonal edge routing, safe standalone SVG rendering, and a typed browser WebAssembly adapter.
5+
Use this repository when embedding Stack rendering or language intelligence in an application. If you only want to create a diagram, start with the [CLI](https://github.com/stack-sh/cli#install) or [Playground](https://stack-diagram.com/).
66

7-
## Workspace
7+
## Choose an interface
88

9-
- `stack-engine`: operation/output boundary, theme and provider-aware completion catalogs, semantic hover, theme and icon fallback resolution, deterministic scene layout, edge routing, validation beyond the compiler stage, and standalone SVG rendering;
10-
- `stack-formatter`: comment-preserving canonical formatting for Stack source files (implemented);
11-
- `stack-engine-wasm` and npm `@stack-sh/engine`: a thin browser adapter exposing the same pure operations and portable result model.
9+
| Environment | Package | API documentation |
10+
| --- | --- | --- |
11+
| Rust | [`stack-engine`](https://crates.io/crates/stack-engine) | [docs.rs](https://docs.rs/stack-engine) |
12+
| Browser JavaScript / TypeScript | [`@stack-sh/engine`](https://www.npmjs.com/package/@stack-sh/engine) | [Package guide](./packages/engine/README.md) |
1213

13-
The native CLI will link the Rust engine directly. Web clients will use the WASM adapter. Shared fixtures will verify that both targets produce equivalent diagnostics, formatted source, and SVG output.
14+
Both packages provide format, check, render, completion, and hover operations over caller-owned source. The browser package exposes the same engine through WebAssembly.
1415

15-
## Boundaries
16+
## Rust quick start
1617

17-
The engine may depend on `stack-sh/compiler` and `stack-sh/theme`. Core operations must be deterministic and must not require filesystem, environment, clock, random, or network access.
18+
```sh
19+
cargo add stack-engine@0.8.0
20+
```
1821

19-
CLI filesystem behavior, process exit codes, user authentication, billing, entitlement checks, and paid-theme delivery are outside this repository.
22+
```rust
23+
use stack_engine::Engine;
2024

21-
## Development
25+
fn main() -> Result<(), Box<dyn std::error::Error>> {
26+
let source = b"stack 1.0 diagram \"API\" { node api \"API\" { icon \"api\" } }";
27+
let output = Engine::bundled().render(source)?;
2228

23-
The workspace uses Rust 2024 with Rust 1.85 as its minimum supported version. Run:
29+
if let Some(svg) = output.svg {
30+
std::fs::write("api.svg", svg)?;
31+
} else {
32+
for diagnostic in output.diagnostics {
33+
eprintln!("{}: {}", diagnostic.code, diagnostic.message);
34+
}
35+
}
2436

25-
```sh
26-
cargo test --workspace
27-
STACK_SPECIFICATION_DIR=../specification cargo test -p stack-formatter --features conformance --test conformance
28-
STACK_SPECIFICATION_DIR=../specification cargo test -p stack-engine --features conformance
29-
python3 scripts/validate-svg.py
30-
rustup target add wasm32-unknown-unknown wasm32-wasip1
31-
cargo build -p stack-engine-wasm --target wasm32-unknown-unknown
32-
wasm-bindgen --version
33-
npm ci
34-
npm run layout:validate
35-
npm run build:wasm
36-
npm test
37-
npm run typecheck
38-
npm run pack:check
39-
CARGO_TARGET_WASM32_WASIP1_RUNNER=wasmtime cargo test -p stack-engine --lib --target wasm32-wasip1 cross_target_numeric_fixture
40-
cargo fmt --check
41-
cargo clippy --workspace --all-targets --all-features -- -D warnings
42-
cargo doc --workspace --no-deps
37+
Ok(())
38+
}
4339
```
4440

45-
The versioned representative layout corpus covers small, medium, and dense diagrams plus groups, nested groups, authored rank and order constraints, cross-boundary edges, labels, and caller-owned provider icons. Run its exact geometry comparison, release-mode performance budget, and local static review gallery with:
41+
The crate supports Rust 1.85 or newer.
42+
43+
## Browser quick start
4644

4745
```sh
48-
cargo test -p stack-engine --test layout_corpus --locked
49-
cargo test --release -p stack-engine --test layout_corpus layout_runtime_stays_within_budget --locked -- --ignored --nocapture
50-
cargo test --release -p stack-engine --test language_intelligence language_intelligence_runtime_stays_within_budget --locked -- --ignored --nocapture
51-
npm run layout:gallery
46+
npm install @stack-sh/engine@0.8.0
5247
```
5348

54-
The review-first snapshot policy and corpus contract are documented in [`layout-corpus/README.md`](./layout-corpus/README.md).
49+
```js
50+
import init, { render } from "@stack-sh/engine";
51+
52+
await init();
5553

56-
The independent text, edge, and frame quality gates run under the `conformance` feature with the repository's source corpus. CI runs these gates explicitly and includes them in its 95% line, function, and region coverage checks. Packaged crate unit tests do not require the repository-only corpus. See the [0.8.0 release notes](./docs/releases/v0.8.0.md) for the approved layout and SVG changes.
54+
const result = render(
55+
'stack 1.0 diagram "API" { node api "API" { icon "api" } }',
56+
);
5757

58-
`stack-formatter` is pure and accepts source bytes or UTF-8 text. Lexical and syntax errors return diagnostics without formatted output. Syntactically valid source remains formattable when semantic diagnostics exist.
58+
if (result.svg) {
59+
console.log(result.svg);
60+
} else {
61+
console.error(result.diagnostics);
62+
}
63+
```
5964

60-
`stack-engine` exposes byte-oriented `format`, `check`, and `render` methods plus UTF-8 `completion` and `hover` methods through an engine bound to the embedded or a caller-provided validated catalog. Language-intelligence results implement schema version 1.0 from the pinned compiler and echo the caller's document version. The Engine derives completion entries from its core theme catalog and validated provider packs, while the compiler remains the single owner of grammar, context, diagnostics, hover semantics, and text edits. `ProviderPack::new` accepts a typed user-imported manifest and caller-owned SVG strings, verifies exact asset hashes and safe SVG structure, and computes a deterministic content revision before `Engine::with_provider_packs` can resolve namespaced IDs. Every normal format, check, or render output carries engine, authored language, theme catalog version, and theme catalog revision metadata. User-source failures stay in ordered portable diagnostics. Invalid provided catalogs or provider packs, invalid language-intelligence positions, and violated normalized pipeline invariants use a separate operational-error channel. Checks and renders resolve the requested theme and provider packs, validate deterministic integer geometry, and route ordered edges outside node interiors. Missing themes and icons produce source-mapped `STK6001` and `STK5001` warnings while a fallback SVG remains available. An unsatisfied authored order hint produces `STK4001` at its source-map range; a satisfied hint does not.
65+
Initialization loads the WebAssembly module once. Operations are synchronous afterward and accept source already held by the caller; the adapter does not read files, access the DOM, or contact a network service.
6166

62-
The renderer emits fixed-dimension standalone SVG with embedded catalog or provider icons, local marker references, escaped authored text, accessible title and description metadata, and no script, event handler, external URL, host font measurement, or runtime I/O. Provider artwork preserves the authored node `kind`; each render returns the exact used-asset notices and writes provider ID, icon IDs, and pack revision into SVG metadata. The bundled catalog provides 30 first-party explicit icon identifiers in every core theme: `api`, `web`, `mobile`, `desktop`, `server`, `container`, `cluster`, `cloud`, `scheduler`, `webhook`, `identity`, `observability`, `gateway`, `load-balancer`, `dns`, `cdn`, `firewall`, `network`, `event`, `stream`, `search`, `analytics`, `repository`, `pipeline`, `secret`, `document`, `task`, `chat`, `email`, and `ai`. Canonical renderer and representative-layout SVG snapshots are byte-stable and parsed by `scripts/validate-svg.py`; set `UPDATE_STACK_SNAPSHOTS=1` or `UPDATE_STACK_LAYOUT_SNAPSHOTS=1` only when intentionally regenerating the corresponding reviewed references. CI also executes one exact numeric geometry fixture in both the native suite and a WASI build.
67+
## What the engine guarantees
6368

64-
The npm package exports synchronous `format`, `check`, `render`, `completion`, and `hover` functions after asynchronous module initialization, with provider-aware variants for check, render, and completion. Provider-pack operations accept JSON-compatible local manifest and SVG data; they never discover a path or initiate a request. Format, check, and render accept `string | Uint8Array`; completion and hover require a UTF-8 string plus a safe-integer document version and a `{ byteOffset, line, column }` position. Results use explicit TypeScript contracts, camel-case fields, plain-text documentation, end-exclusive UTF-8 ranges, and ordered portable diagnostics. Invalid UTF-8 remains a normal `STK1001` result for byte-oriented operations. Unsupported JavaScript input types, inconsistent positions, and internal operational failures throw at the adapter boundary. Shared fixtures exercise native and WebAssembly parity for provider resolution, contextual completion, document-version echo, multilingual positions, and hover. Artifact validation audits WebAssembly imports and package contents; browser consumers retain responsibility for module loading, stale-result suppression, and every DOM, filesystem, network, or clock interaction.
69+
- Deterministic integer layout and orthogonal routing using versioned font metrics and themes.
70+
- Standalone SVG with embedded icons, accessible metadata, escaped authored text, and no script or external URL.
71+
- Ordered, source-mapped diagnostics for invalid source, missing resources, and unsatisfied layout hints.
72+
- Exact native/WebAssembly result parity over shared fixtures.
73+
- Caller-owned provider packs that are validated in memory and never discovered, downloaded, or stored by the engine.
6574

66-
Public npm releases are produced from GitHub Releases after the repository checks pass. See [RELEASING.md](./RELEASING.md) for the first-release bootstrap and subsequent trusted-publishing flow.
75+
Every result includes engine, language, theme catalog version, and catalog revision metadata. See the [npm package guide](./packages/engine/README.md) for JavaScript types and provider-pack APIs, and [docs.rs](https://docs.rs/stack-engine) for the Rust facade.
76+
77+
## Workspace
78+
79+
- `stack-engine`: the pure operation facade, layout, routing, language intelligence, and SVG rendering.
80+
- `stack-formatter`: canonical comment-preserving Stack source formatting.
81+
- `stack-engine-wasm` and `@stack-sh/engine`: the typed browser adapter.
82+
83+
The engine consumes the public [Stack compiler](https://github.com/stack-sh/compiler) and [theme catalog](https://github.com/stack-sh/theme). Language syntax and normalized IR remain owned by the [Stack specification](https://github.com/stack-sh/specification).
84+
85+
Filesystem behavior, process exit codes, user authentication, billing, entitlement checks, and paid-theme delivery belong to host applications and are outside this repository.
6786

6887
## Architecture
6988

70-
- [`docs/decisions/0001-build-the-formatter-from-compiler-models.md`](./docs/decisions/0001-build-the-formatter-from-compiler-models.md)
71-
- [`docs/decisions/0002-use-a-pure-versioned-engine-facade.md`](./docs/decisions/0002-use-a-pure-versioned-engine-facade.md)
72-
- [`docs/decisions/0003-use-integer-ranked-scene-layout.md`](./docs/decisions/0003-use-integer-ranked-scene-layout.md)
73-
- [`docs/decisions/0004-route-orthogonal-edges-on-a-visibility-grid.md`](./docs/decisions/0004-route-orthogonal-edges-on-a-visibility-grid.md)
74-
- [`docs/decisions/0005-serialize-safe-standalone-svg.md`](./docs/decisions/0005-serialize-safe-standalone-svg.md)
75-
- [`docs/decisions/0006-expose-one-typed-browser-wasm-adapter.md`](./docs/decisions/0006-expose-one-typed-browser-wasm-adapter.md)
76-
- [`docs/decisions/0007-adapt-language-intelligence-with-engine-catalogs.md`](./docs/decisions/0007-adapt-language-intelligence-with-engine-catalogs.md)
77-
- [`docs/decisions/0008-compose-graphs-with-reserved-label-geometry.md`](./docs/decisions/0008-compose-graphs-with-reserved-label-geometry.md)
78-
- [`docs/dependency-audit.md`](./docs/dependency-audit.md)
89+
The design records cover the [pure engine facade](./docs/decisions/0002-use-a-pure-versioned-engine-facade.md), [deterministic layout](./docs/decisions/0003-use-integer-ranked-scene-layout.md), [orthogonal routing](./docs/decisions/0004-route-orthogonal-edges-on-a-visibility-grid.md), [safe SVG](./docs/decisions/0005-serialize-safe-standalone-svg.md), [browser adapter](./docs/decisions/0006-expose-one-typed-browser-wasm-adapter.md), [language intelligence](./docs/decisions/0007-adapt-language-intelligence-with-engine-catalogs.md), and [label-aware graph composition](./docs/decisions/0008-compose-graphs-with-reserved-label-geometry.md).
90+
91+
See [CONTRIBUTING.md](./CONTRIBUTING.md) for repository setup, quality gates, the reviewed layout corpus, and release verification.
7992

8093
## Licensing
8194

82-
This repository is licensed under the [Apache License 2.0](./LICENSE). Third-party dependencies or bundled assets must be tracked in [THIRD_PARTY_LICENSES.md](./THIRD_PARTY_LICENSES.md) before a distributable native or WASM artifact is published.
95+
This repository is licensed under the [Apache License 2.0](./LICENSE). Third-party dependencies and bundled assets are recorded in [THIRD_PARTY_LICENSES.md](./THIRD_PARTY_LICENSES.md).

0 commit comments

Comments
 (0)