Skip to content

Architecture conventions

Rules that hold across Hotaru. The reasons are in decisions/. The big picture is in overview.md.

Processes and databases

  • Only the web process writes durable.db. Scripts, tests against real data, and admin CLIs may open it only with readOnly: true. pi-durable has no cross-process locking (ADR-0001).
  • Run one replica. Never scale the web app horizontally (ADR-0001).
  • Open the harness once per process. Use a module-level singleton (on globalThis in development, so Vite’s hot reload doesn’t open a second one).
  • No SQL joins across the two databases. Link them by ID: a pipeline task stores the item ID, and app rows may store a task or conversation ID (ADR-0002).
  • Write transactions on hotaru.db are IMMEDIATE (db.transaction(fn, IMMEDIATE) from db/client.ts). A deferred transaction that reads first fails at once, without waiting for busy_timeout, when another connection wrote in between, such as a CLI script next to the running server.
  • Pipeline writes are idempotent. A durable phase reruns from its top after a crash, so every write to hotaru.db from a phase is an upsert keyed by its natural ID (such as user + item + step). Never an append.
  • Every paid call in a pipeline stage goes through context.once(unit, key, call). It stores the result in durable.db before returning, so a stage that reruns after a crash doesn’t pay again (pipeline.md).
  • Every model and jev call goes through the OpenRouter client (context.ai in a stage). It checks the user’s monthly budget and records the call in the cost ledger. Never call OpenRouter with fetch directly (costs.md).
  • The one exception is the agent chat: pi-durable makes its model turns through pi-ai. The chat checks the budget before each message and records each answer in the ledger itself (chat.md).

Multi-user

  • Every user-owned table has a user_id column, and every query filters by it. Services take the user as an argument. They never read it from global state.
  • Every API route and every agent tool has a case in the isolation suites, which call it as one user with another’s IDs (accounts.md). A new one without a case fails the tests.
  • Limits on what an account may add are checked in packages/core, where it’s added (subscribe(), addLink(), setAiBudget()), not in the routes (accounts.md).
  • No personal configuration in code, environment variables or seed data. Topics, weights, sources and thresholds are per-user rows.

Packages

  • packages/core never imports SvelteKit ($app/*, @sveltejs/kit).
  • Clients (apps/cli, apps/tui, Pi agents) depend on packages/sdk only, never on core.
  • adapter-node bundles the workspace packages into the server build. Native or heavy modules they import (better-sqlite3, drizzle-orm, later @earendil-works/*) must also be in apps/web’s dependencies, so they stay external.
  • Nothing at runtime reads files relative to a package’s sources: the bundle moves them. Migrations are embedded in packages/core/src/db/migrations.gen.ts by pnpm db:generate.
  • Pin exact versions of all @earendil-works/* packages and upgrade them together. The API changes without notice between releases (SI-512).