Skip to content

Latest commit

 

History

History
102 lines (91 loc) · 6.95 KB

architecture.md

File metadata and controls

102 lines (91 loc) · 6.95 KB

How SketchCraft is built

core/        the engine (Rust): model, commands, importers, exporters, renderers, CLI, MCP
app/         the desktop app: React + three.js interface (src/), Tauri shell (src-tauri/)
examples/    models built from command scripts
tools/       demo recording, IFC checks, tombstones (dead-code finder: [tombstones.md](tombstones.md))
docs/        this documentation
brand/       logo, icon and banner (generated by brand/beaver.py)

The engine (core/)

  • Model (model.rs, components.rs, topology.rs): welded vertices, edges and planar faces with holes; groups (nested), components (definitions and placements), tags, guides, views. Push/pull, face splitting and auto-folding live here.
  • Commands (commands.rs; the enum itself is commands/command.rs): one enum, one JSON command per variant. Engine::exec_json runs a command and handles undo (mutates() lists the read-only ones). Engine::run is the dispatch table: it sends each command to the file for its area in commands/ (draw, shape, build, groups, tags, paint, rendering, sections, annotate, files, library, media, session).
  • Scene (scene.rs): what a viewer needs to draw a model: triangles with their owners (for picking), edges, outlines, the outliner tree, face-me figures. The viewport gets it as bytes (Bulk::encode: the big lists as typed arrays, the faces and edges as columns; decodeScene in app/src/engine.ts), the tests, MCP and the command line as JSON.
  • Live model (live.rs): the model shared through a file, so the app, the browser bridge and MCP agents see each other's changes.
  • Files: SketchUp (skp.rs legacy, skp_vff.rs 2013+, clean-room readers worked out from sample files), DXF/DWG (dxf.rs, dwg.rs), IFC (ifcin.rs with IfcOpenShell, ifc.rs out), FBX, OBJ, point clouds (cloud.rs), and exporters (export.rs, export2.rs).
  • Pictures: a rasterizer for quick renders and drawings (render.rs) and a path tracer with a denoiser for photoreal ones (pt.rs, see RENDERER.md).
  • Plugins (plugins.rs): listing, installing and the parts measurements. Plugins themselves run in the app's sandbox (PLUGINS.md).

The app (app/)

  • Viewport.tsx: the three.js view and every tool (one step function per tool for a press and for a move), snapping (inference.ts), the axis gizmo (gizmo.tsx). Its types, colors, edge styling, materials and pure geometry are in viewport/.
  • App.tsx: layout, menus, the command palette (palette.tsx). Its state and actions are in hooks: useplugins.ts, usefiles.ts (file actions and imports), uselook.ts (skin, layout, workspace); the File menu is filemenu.tsx.
  • Panels, one file each: buildpanels.tsx, library.tsx, environments.tsx, views.tsx, entityinfo.tsx, contextmenu.tsx, materialspanel.tsx, renderdialog.tsx, dialogs.tsx, shortcuts.tsx (what they share is in panelkit.tsx); dialogs use sheet.tsx.
  • toolbar.tsx, appearance.tsx (themes), learn.tsx (lessons), updater.tsx.
  • Plugins: plugins.tsx (types and dialogs), pluginhost.ts (the host side of a run), sandbox.ts and lockdown.ts (the worker every plugin runs in, JavaScript and Ruby alike), ruby.ts.
  • Styles are SCSS: src/styles/main.scss lists the partials in cascade order (later ones override earlier ones); _mixins.scss and _variables.scss hold what is repeated at compile time. Anything a theme or skin changes at runtime stays a CSS custom property (_tokens.scss, set from skins.ts).
  • In the desktop app, commands go to the engine through Tauri; in the browser they go to the bridge on localhost (engine.ts).

Security

The bridge needs a per-launch token and only accepts its own page (no other websites, no DNS rebinding), the app has a strict content security policy, importers are fuzzed, plugins are sandboxed and run under an allowlist that the engine enforces (core/src/policy.rs), agents are held to a workspace folder, the live model file is saved atomically with backups, and updates are signed. See SECURITY.md.

Writing the shared model file

Several programs can edit one model file (the app, the bridge, an agent over MCP). Every one of them writes it the same way (Live::save_inner in core/src/live.rs), so a save somebody was told succeeded is never lost:

  1. The model is serialised first, with no lock held (this is the slow part).
  2. A lock file beside the model (.<name>.lock, an advisory lock from the standard library) is taken.
  3. With the lock held, the file must still be what this program last read or wrote (modification time, size and file identity). If it is not, nothing is written; the caller plans again on the newer file (the app runs the command again on it, an agent's flush sets its pending work aside and takes theirs).
  4. The outgoing file is backed up when the schedule says so, and for the first write of a call that has run for a while.
  5. The new bytes go to a temp file and are flushed, the stamp is checked once more immediately before the rename, and the file is renamed over the old one. A crash at any step leaves the old file or the whole new one.

An agent call that runs out of time is taken back without depending on the (capped) undo list. When nobody else has written the file, the model goes back to what the call began with. Otherwise what the call wrote is worked out entry by entry (points, edges, faces, groups, placements, ... by id, as a history::Patch), measured from the model the call began with or, after somebody else's save was loaded, from that save. The rollback reloads the file as it is now and takes those changes off it. An entry that only the call changed goes back; an entry somebody else changed too stays as they have it. When anything stays, or the result would not be a consistent model, or the call is too big to track, the person's data wins: the call's last written version is set aside in an unsaved-* file (private to the owner) and the agent is told the rollback was not complete. If the file moves while the rollback is being written, the plan is made again on the newer file. Two more rules keep that true when things go wrong. Work is only replaced by somebody else's file after it has been set aside successfully (the set-aside file is created new under a unique name, never through a link, shortened for a very long model name, private to the owner); when that fails, their file is left as it is, memory keeps the work, the person is told, and the next look at the file tries again. The last two tries of a rollback hold the lock from reading the file to writing it, so a person who saves all the time cannot keep it from finishing; if it still cannot finish, the person is told as well as the agent. A lock file that is a link, a folder or unreadable is repaired where possible, and the person is told once when no lock can be made at all. The file's stamp also holds a quick fingerprint of its first and last 64 KB.