sanoma 0.2.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/README.md +81 -25
  2. package/dist/check.d.ts +10 -5
  3. package/dist/check.js +77 -17
  4. package/dist/check.js.map +1 -1
  5. package/dist/cli.js +63 -23
  6. package/dist/cli.js.map +1 -1
  7. package/dist/config.d.ts +5 -0
  8. package/dist/config.js +29 -3
  9. package/dist/config.js.map +1 -1
  10. package/dist/data.d.ts +1 -0
  11. package/dist/data.js +4 -0
  12. package/dist/data.js.map +1 -0
  13. package/dist/dev.d.ts +3 -1
  14. package/dist/dev.js +5 -2
  15. package/dist/dev.js.map +1 -1
  16. package/dist/env.js +2 -3
  17. package/dist/env.js.map +1 -1
  18. package/dist/index.d.ts +1 -1
  19. package/dist/index.js +2 -2
  20. package/dist/index.js.map +1 -1
  21. package/dist/init.d.ts +19 -2
  22. package/dist/init.js +57 -2
  23. package/dist/init.js.map +1 -1
  24. package/dist/oxlint-plugin.d.ts +7 -1
  25. package/dist/oxlint-plugin.js +11 -4
  26. package/dist/oxlint-plugin.js.map +1 -1
  27. package/dist/resources.d.ts +1 -1
  28. package/dist/resources.js +3 -2
  29. package/dist/resources.js.map +1 -1
  30. package/dist/serve.d.ts +18 -12
  31. package/dist/serve.js +118 -26
  32. package/dist/serve.js.map +1 -1
  33. package/dist/site-lint.d.ts +33 -0
  34. package/dist/site-lint.js +70 -0
  35. package/dist/site-lint.js.map +1 -0
  36. package/dist/sites.d.ts +7 -0
  37. package/dist/sites.js +11 -0
  38. package/dist/sites.js.map +1 -0
  39. package/oxlint.json +117 -22
  40. package/package.json +19 -9
  41. package/site.d.ts +3 -0
  42. package/site.js +3 -0
  43. package/template/.env.example +2 -3
  44. package/template/AGENTS.md +14 -7
  45. package/template/package.json +2 -2
  46. package/template/policies/policy.cedar +17 -0
  47. package/template/resources/data.ts +21 -0
  48. package/template/resources/people.ts +20 -0
  49. package/template/sanoma.config.ts +0 -2
  50. package/template/scenarios/publish-post/publish.feature +16 -6
  51. package/template/tsconfig.json +1 -1
  52. package/template/workflows/publish-post.ts +2 -4
  53. package/template/policies/policy.ts +0 -19
@@ -1,19 +1,22 @@
1
1
  # A company on Sanoma
2
2
 
3
- This repository is a company's operations as code, run by [Sanoma](https://github.com/Sanoma-AI/sanoma): workflows that call vendors through connectors, a policy that decides which calls need a person's approval, and the vendor objects the company owns, declared as data. `npx sanoma` in an empty directory started it; `sanoma` from npm runs it. The `sanoma` package's README (`node_modules/sanoma/README.md`) is the manual.
3
+ This repository is a company's operations as code, run by [Sanoma](https://github.com/Sanoma-AI/sanoma): workflows that call vendors through connectors, the people who use it, Cedar policies that decide which calls and runs wait for someone's approval or are refused, and the vendor objects the company owns, declared as data. `npx sanoma` in an empty directory started it; `sanoma` from npm runs it. The `sanoma` package's README (`node_modules/sanoma/README.md`) is the manual.
4
4
 
5
5
  ## Layout
6
6
 
7
7
  ```
8
- sanoma.config.ts the workflows, the connectors and the policy
8
+ sanoma.config.ts the workflows and the connectors
9
9
  workflows/ one workflow per file (defineWorkflow)
10
- policies/policy.ts which calls need whose approval (definePolicy)
10
+ policies/*.cedar who may do what: Cedar forbids, each a hold for an approver (@hold) or a deny (@reason)
11
11
  resources/ data files: the vendor objects this company declares, by area
12
+ resources/data.ts collections: the company's own records (collection, from sanoma/data)
13
+ resources/people.ts who may use the app, the agents, and the groups (sanoma.person, .agent and .group, from sanoma/resources)
12
14
  scenarios/ Gherkin: how each workflow should go against the connectors' fakes
15
+ sites/ pages the app serves under /sites/<name>: sites/<name>/routes/ holds TanStack Router file routes (none yet)
13
16
  .env this machine's settings and secrets, never committed (SANOMA_SECRETS_KEY encrypts stored credentials)
14
17
  ```
15
18
 
16
- The app's name is `package.json`'s `name`. `.env.example` (written by `sanoma env`) and `.oxlintrc.json` (written by `sanoma check` when missing) are generated.
19
+ The app's name is `package.json`'s `name`. `.env.example` (written by `sanoma env`) and `.oxlintrc.json` (written by `sanoma check` when missing) are generated, and so are `sites/__virtual.ts` and `sites/tsconfig.json` once there is a site: commit them, never edit them. `.sanoma/` holds what `sanoma` builds (ignored), `.sanoma/api.ts` among it: the typed `api` for sites, and `.sanoma/schema.cedarschema`, the schema the policies are checked against, both generated from the config by `sanoma check` and at start, never edited.
17
20
 
18
21
  ## Run
19
22
 
@@ -21,14 +24,18 @@ With the package manager whose lockfile is here (`npm run <script>`, `pnpm <scri
21
24
 
22
25
  ```sh
23
26
  npm run dev # Postgres (in Docker when nothing answers), the worker and the app; the app at the printed address
24
- npm run check # tsc, the lint, what the worker would refuse, and every scenario
27
+ npm run check # tsc, the lint, the policies, what the worker would refuse, the sites' build and types, and every scenario
25
28
  npm run env # rewrites .env.example from the connectors' variables
26
29
  ```
27
30
 
28
31
  ## Rules
29
32
 
30
- - A workflow imports only from `sanoma`, its connectors and relative files, and does the same thing every time it is replayed: `ctx.now()` and `ctx.sleep()`, never `Date` or timers; configuration comes in as input, never from `process.env`. The `check` script holds workflows, policies and data files to this.
33
+ - A workflow imports only from `sanoma`, its connectors and relative files, and does the same thing every time it is replayed: `ctx.now()` and `ctx.sleep()`, never `Date.now()` or `new Date()` (`new Date(await ctx.now())` for a date, read in UTC) or timers; configuration comes in as input, never from `process.env`. The `check` script holds workflows and data files to this.
31
34
  - Define each workflow's `name` as a string literal: the app finds the workflow in its file by it.
32
35
  - Every operation a workflow calls is listed in its `uses`; add the connector to `sanoma.config.ts` and to `package.json`.
33
- - Describe each workflow's expected runs in `scenarios/<workflow>/<name>.feature`, and run the `check` script after every change.
36
+ - Declare the company's own records as collections in `resources/data.ts`, which imports only `collection` from `sanoma/data` and holds nothing else: the runtime imports it, and so does a workflow that names a collection's operations (`import { outbound } from "../resources/data.ts"`, the one file outside `workflows/` a workflow may import). Every other data file is read, never run.
37
+ - Only a person declared with `sanoma.person` (in `resources/people.ts` by convention, a data file like any other) signs in to the app (by `id` on this machine; with GitHub sign-in by email, then `github` login), starts a run, calls an operation or decides an approval, in the groups that list them (`sanoma.group({ name, members: [ada, refundBot] })`, the one place a membership is written). An agent (`sanoma.agent({ id, onBehalfOf: bo })`) is in the group `agents` besides; it never signs in. A new colleague is an edit to that file before their first sign-in. Nobody decides an approval on a run they started while someone else in the group could; alone in it, they decide, and the ledger says so.
38
+ - The policies are `policies/*.cedar`, written in [Cedar](https://docs.cedarpolicy.com) against `.sanoma/schema.cedarschema`. Everything is allowed unless a `forbid` refuses it; the runtime writes the one `permit`, so a `permit` here is a problem. A `forbid` with `@hold("<group>")` (or a person's id, or `"on-behalf-of"` for whom an agent acts) waits for that approver, with `@title` as its title in the Inbox, and the run goes on once they approve; one without `@hold` is denied, with `@reason` as the message. Actions are each operation's id (`Sanoma::Action::"ghost.post.publish"`), in its effect's group (`Sanoma::Action::"effect:publish"`), and `Sanoma::Action::"workflow:<name>"` for starting a run; `principal in Sanoma::Group::"editor"` is membership; `context.input` is the call's input. The app's Permissions page shows what they answer for each group. The `check` script reports a policy problem (a field, action or group the schema lacks, a `@hold` naming no one declared) at its `file:line:col`, and runs no scenario until there are none; `npm run dev` refuses to start on them.
39
+ - Describe each workflow's expected runs in `scenarios/<workflow>/<name>.feature`, and run the `check` script after every change. A scenario runs as the runtime's own agent `scenarios` unless it says who is acting (`Given reviewer is acting`), and checks the policy with `Then <op> was held for group "<group>"` and `Then <op> was denied with "<reason>"`; a scenario's approver (`is approved by reviewer`) must be declared. `reviewer` in `resources/people.ts` is a stand-in who can sign in and approve: replace them with a real colleague, in the scenarios too.
40
+ - A site, `sites/<name>/routes/` (pages the app serves under `/sites/<name>/`, with an optional `sites/<name>/site.ts` naming it with `defineSite`), is browser code: no server code, credential or `process.env` under `sites/`, which the `check` script holds it to, and the folder's name is its URL segment (lowercase letters, digits and dashes). Its route files import `createFileRoute` from `@tanstack/react-router`, routing (`Link`, `Outlet`, `useNavigate`, `defineSite`, …) from `sanoma/site`, and UI from `@sanoma-ai/app/ui`: the app's own components (`Button`, `Card`, `Table`, `Region`, `Facts`, …) and `<RunForm workflow="<name>" />`, the app's start form for a workflow. A page reaches data and workflows only through `api` from `sanoma/site`, as the person on the page: `api.data.<collection>.list()`, a vendor's read such as `api.<vendor>.<resource>.get(…)`, `await api.<workflow>(input)` or `api.<workflow>.start(input)` (the workflow's name in camelCase); see the app package's README (Sites). Name what a page calls with literals (`workflow="announce"`, `api.announce.start(…)`, not a variable): the app reads a site's calls from its code to list them under each workflow's Used by, and the `check` script fails on a call to a workflow, collection or operation that does not exist. While the `dev` script runs, an edit to a site shows in the open page as it is saved, and a new site folder is live without a restart.
34
41
  - Never put credentials in a file here. They live in `.env` or on the app's connector pages, which store them encrypted in the database.
@@ -8,8 +8,8 @@
8
8
  "env": "sanoma env"
9
9
  },
10
10
  "dependencies": {
11
- "@sanoma-ai/connector-ghost": "^0.2.0",
12
- "sanoma": "^0.2.0"
11
+ "@sanoma-ai/connector-ghost": "^0.4.1",
12
+ "sanoma": "^0.4.1"
13
13
  },
14
14
  "engines": {
15
15
  "node": "^24.2 || >=26"
@@ -0,0 +1,17 @@
1
+ // Who may do what. Everything a login admits is allowed unless a `forbid` here refuses it: one
2
+ // with @hold waits for that group's (or person's) approval, with @title as the Inbox's title;
3
+ // one without is denied, with @reason as the message. Every operation call and run start is
4
+ // decided here. Write against .sanoma/schema.cedarschema, which `sanoma check` writes: the
5
+ // actions (each operation's id, in its effect's group, and `workflow:<name>`), the groups
6
+ // resources/people.ts declares, and each operation's input as `context.input`.
7
+
8
+ // Publishing or sending waits for someone in editor, unless someone in editor asked for it.
9
+ @id("editor-reviews")
10
+ @hold("editor")
11
+ @title("Review before it goes out")
12
+ forbid (
13
+ principal,
14
+ action in [Sanoma::Action::"effect:publish", Sanoma::Action::"effect:send"],
15
+ resource
16
+ )
17
+ unless { principal in Sanoma::Group::"editor" };
@@ -0,0 +1,21 @@
1
+ // The company's own records, kept in its database. A collection's operations (`data.<name>.get`,
2
+ // `.list`, `.save` and `.delete`) are what a workflow names in `uses` and calls on
3
+ // `ctx.data.<name>`, under the policy and in the ledger. Workflows import this file
4
+ // (`import { outbound } from "../resources/data.ts"`), so it imports only from "sanoma/data" and
5
+ // holds only collections.
6
+ import { collection } from "sanoma/data";
7
+
8
+ /** The outbound pipeline: which lead is at which stage, and who owns it. */
9
+ export const outbound = collection({
10
+ name: "outbound",
11
+ title: "Outbound",
12
+ // Each record is found by its company: a required text field.
13
+ key: "company",
14
+ fields: {
15
+ company: "text",
16
+ contact: "text",
17
+ stage: { enum: ["researched", "contacted", "replied", "meeting", "won", "lost"] },
18
+ owner: "person",
19
+ lastTouched: { type: "date", optional: true },
20
+ },
21
+ });
@@ -0,0 +1,20 @@
1
+ // Who may use this company's app, the agents that act in it, and the groups: resources like any
2
+ // other, read without running. Only a declared person signs in (the loopback sign-in by id,
3
+ // GitHub sign-in by email, then login), and acts in the groups that list them; an agent is in
4
+ // the group `agents` besides. Policies (policies/*.cedar) and approvals name people by id and
5
+ // groups by name.
6
+ import { sanoma } from "sanoma/resources";
7
+
8
+ /** Who started the company: `sanoma init` wrote them here. */
9
+ export const founder = sanoma.person({ id: "founder" });
10
+
11
+ /**
12
+ * Who reviews a post the scenarios publish: a stand-in, who can sign in and approve like anyone
13
+ * declared. Replace them with a real colleague, here and in the scenarios.
14
+ */
15
+ export const reviewer = sanoma.person({ id: "reviewer", name: "Reviewer" });
16
+
17
+ export const admin = sanoma.group({ name: "admin", members: [founder] });
18
+
19
+ /** Whoever publishes or sends without a review, and reviews everyone else's (policies/policy.cedar). */
20
+ export const editor = sanoma.group({ name: "editor", members: [founder, reviewer] });
@@ -2,11 +2,9 @@
2
2
  // environment or from the app's connector pages (see .env.example).
3
3
  import { ghost } from "@sanoma-ai/connector-ghost";
4
4
  import { defineConfig } from "sanoma";
5
- import policy from "./policies/policy.ts";
6
5
  import publishPost from "./workflows/publish-post.ts";
7
6
 
8
7
  export default defineConfig({
9
8
  workflows: [publishPost],
10
9
  connectors: [ghost],
11
- policy,
12
10
  });
@@ -1,18 +1,28 @@
1
1
  Feature: Publish a post
2
- The post is drafted, the editor reviews it, and it is published.
2
+ The post is drafted and published. The policy holds the publish for the editor group, unless
3
+ someone in editor started the run. A scenario runs as the agent `scenarios` unless it says who
4
+ is acting.
3
5
 
4
6
  Scenario: The editor approves
5
7
  When publish-post runs with
6
- | title | Hello from us |
7
- | html | <p>Our first post.</p> |
8
- And "Review the post" is approved by editor
9
- Then a post titled "Hello from us" is created
8
+ | title | Hello from us |
9
+ | html | <p>Our first post.</p> |
10
+ And "Review before it goes out" is approved by reviewer
11
+ Then ghost.post.publish was held for group "editor"
12
+ And a post titled "Hello from us" is created
10
13
  And step "Publish the post" ran 1 time
11
14
  And the run succeeds
12
15
 
13
16
  Scenario: The editor rejects
14
17
  When publish-post runs with
15
18
  | title | Hello from us |
16
- And "Review the post" is rejected by editor
19
+ And "Review before it goes out" is rejected by reviewer
17
20
  Then ghost.post.publish was not called
18
21
  And the run fails with "approval_rejected"
22
+
23
+ Scenario: An editor publishes without a review
24
+ Given reviewer is acting
25
+ When publish-post runs with
26
+ | title | Hello from us |
27
+ Then ghost.post.publish was called
28
+ And the run succeeds
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "extends": "sanoma/tsconfig.json",
3
- "include": ["sanoma.config.ts", "workflows", "policies", "resources", "scenarios"]
3
+ "include": ["sanoma.config.ts", "workflows", "resources", "scenarios"]
4
4
  }
@@ -1,7 +1,7 @@
1
1
  import { ghost } from "@sanoma-ai/connector-ghost";
2
2
  import { defineWorkflow, z } from "sanoma";
3
3
 
4
- /** Draft a blog post, have the editor review it, then publish it. */
4
+ /** Draft a blog post, then publish it: the policy has an editor review it first (policies/policy.cedar). */
5
5
  export default defineWorkflow({
6
6
  name: "publish-post",
7
7
  title: "Publish a post",
@@ -11,11 +11,9 @@ export default defineWorkflow({
11
11
  /** The post's body, as HTML. */
12
12
  html: z.string().min(1),
13
13
  }),
14
- uses: [ghost.post.create, ghost.post.publish, "approval"],
14
+ uses: [ghost.post.create, ghost.post.publish],
15
15
  run: async (ctx, { title, html }) => {
16
16
  const post = await ctx.ghost.post.create({ title, html, status: "draft" }, { name: "Draft the post" });
17
- // Covers the publish, so the policy lets it through on this one sign-off.
18
- await ctx.approval("Review the post", { approver: "editor", covers: [ghost.post.publish], links: [post.url] });
19
17
  const published = await ctx.ghost.post.publish({ id: post.id }, { name: "Publish the post" });
20
18
  return { url: published.url };
21
19
  },
@@ -1,19 +0,0 @@
1
- import { allow, approve, approvedFor, definePolicy } from "sanoma";
2
-
3
- const EDITOR = "editor";
4
-
5
- /**
6
- * Checked before every call a workflow makes: `allow()`, `deny(reason)` (the run fails) or
7
- * `approve(who)` (the run waits until that person approves).
8
- *
9
- * Anything that publishes or sends needs the editor; everything else is allowed. A call passes
10
- * when the editor approved an approval in this run that was addressed to them and covers it
11
- * (`approvedFor`); otherwise it is held until they approve it.
12
- *
13
- * Deterministic: the decision depends on the call alone (no clock, randomness or network), so
14
- * a replayed run decides the same way.
15
- */
16
- export default definePolicy(({ op, effect, run }) => {
17
- if (effect !== "publish" && effect !== "send") return allow();
18
- return approvedFor(run.approvals, op.id, EDITOR) ? allow() : approve(EDITOR);
19
- });