yichus 0.2.0 → 0.3.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 (36) hide show
  1. package/README.md +45 -16
  2. package/dist/cli.js +17 -2123
  3. package/dist/cli.js.map +1 -1
  4. package/dist/main.js +3762 -0
  5. package/dist/main.js.map +1 -0
  6. package/dist/studio-page/dom.js +22 -0
  7. package/dist/studio-page/label-fit.js +7 -0
  8. package/dist/studio-page/layer-diagram.js +458 -0
  9. package/dist/studio-page/layer-groups.js +129 -0
  10. package/dist/studio-page/source-link.js +17 -0
  11. package/dist/studio-page/studio-views.js +256 -0
  12. package/dist/studio-page/system-map-record.js +124 -10
  13. package/dist/studio-page/system-map.js +476 -87
  14. package/dist/studio-page/what-happens-record.js +224 -0
  15. package/dist/studio-page/what-happens.js +771 -0
  16. package/dist/studio-reference/balance-listed-laws.bosatsu +29 -0
  17. package/dist/studio-reference/balance.bosatsu +7 -0
  18. package/dist/studio-reference/calculator.md +20 -0
  19. package/dist/studio-reference/consent-laws.bosatsu +37 -0
  20. package/dist/studio-reference/consent.bosatsu +15 -0
  21. package/dist/studio-reference/deploy-platforms.md +79 -0
  22. package/dist/studio-reference/laws.md +62 -0
  23. package/dist/studio-reference/portable-programs.md +58 -0
  24. package/dist/studio-reference/porting.md +34 -0
  25. package/dist/studio-reference/protocol-cases.md +11 -0
  26. package/dist/studio-reference/{protocol-specification.txt → protocol-specification.md} +16 -18
  27. package/dist/studio-reference/refund-desk-one-round.bosatsu +196 -0
  28. package/dist/studio-reference/refund-desk.bosatsu +194 -0
  29. package/dist/studio-reference/refund-policy.bosatsu +97 -0
  30. package/dist/studio-reference/refund-test.bosatsu +75 -0
  31. package/dist/studio-reference/render.yaml +42 -0
  32. package/dist/studio-reference/server.mjs +33 -0
  33. package/dist/studio-reference/store.mjs +16 -0
  34. package/dist/studio-reference/the-page.md +130 -0
  35. package/dist/studio-reference/worker-authority.md +159 -0
  36. package/package.json +2 -2
@@ -0,0 +1,159 @@
1
+ # Worker authority in generated APIs
2
+
3
+ A worker is trusted deployment code, but its network caller still needs an
4
+ identity. `worker_route` declares that identity separately from a user's roles.
5
+ The declaration participates in the same typed route, permission and access
6
+ analysis as other service routes:
7
+
8
+ ```bosatsu
9
+ worker_route("/complete", handler("complete", complete),
10
+ "judgment", ["job-manager"], [WritePerm("results")])
11
+ ```
12
+
13
+ Import `worker_route`, `handler` and `WritePerm` from `Yichus/Service`.
14
+ The named handler takes the usual `Db`, injected `Principal` and typed inputs,
15
+ and returns `IO`. The name and role list must be statically readable, nonblank
16
+ literals or aliases; roles are nonempty and distinct. A matching
17
+ `GuardedOrRoles` or `PublicReadRoleWrite` resource policy still governs the
18
+ handler's effects. Shared `Table` and `TxTable` helper parameters retain the
19
+ calling route's exact resource and authority; a dynamic table or an ordinary
20
+ unprivileged caller does not inherit another route's grants.
21
+
22
+ The name `judgment` is a logical deployment binding. It is not a credential,
23
+ a client-supplied field or a session claim. A user session with `job-manager`
24
+ cannot call this route. The server adapter maps a verified machine subject to
25
+ the declared logical name and deployment-owned role grants. Worker credentials
26
+ can call only worker routes for that name; they cannot impersonate users on
27
+ ordinary routes. Worker principals have an opaque namespaced identity, derived
28
+ from the logical name and verified subject; application code must not parse it.
29
+
30
+ ## Secret bootstrap
31
+
32
+ A deployment without a machine identity provider can give each worker a long
33
+ random secret instead. The worker sends it as its Bearer token, and the API
34
+ holds the same secrets in its environment. The generated engine exports the
35
+ verifier:
36
+
37
+ ```js
38
+ import {
39
+ attachYichusWorkerVerifier, yichusSecretWorkerVerifier, startYichusApiEngine,
40
+ } from './engine.mjs';
41
+
42
+ attachYichusWorkerVerifier(yichusSecretWorkerVerifier([
43
+ { name: 'ingest', secret: process.env.INGEST_WORKER_SECRET, roles: ['ingest'] },
44
+ ]));
45
+ await startYichusApiEngine(8080);
46
+ ```
47
+
48
+ Each secret is at least 32 characters, and no two workers share one. Make it with
49
+ `openssl rand -base64 48`, or with any other source of random bytes. A token is
50
+ compared by its SHA-256 digest against every worker's in full. Rotating a secret
51
+ means restarting the engine with the new one. Unlike Clerk M2M tokens, a secret
52
+ does not expire on its own, so keep it on servers only.
53
+
54
+ This fits a page host such as Vercel that receives a provider's webhook or a
55
+ public form. Its route handler checks the provider's signature, or the form's
56
+ bot check, and then calls the engine's worker route with the secret.
57
+
58
+ ## Clerk bootstrap
59
+
60
+ The workspace package `@yichus/clerk-worker-auth` verifies **opaque Clerk M2M
61
+ tokens** online. It does not accept session JWTs or M2M JWTs. Configure a
62
+ receiving machine for the API and a sending machine for the judgment worker;
63
+ the token's scopes must include the receiving machine. Keep the receiving
64
+ machine's secret on the API server and the sending credential on the worker.
65
+ Grant creation and token issuance are deployment setup, not frontend actions.
66
+
67
+ With Node 20 or newer, first set the listener's authentication environment
68
+ using the [session configuration](hosted-sessions.md), then attach the adapter
69
+ before starting the generated engine:
70
+
71
+ ```js
72
+ import {
73
+ attachYichusWorkerVerifier, attachYichusStore, startYichusApiEngine,
74
+ } from './engine.mjs';
75
+ import { createClerkWorkerVerifier } from '@yichus/clerk-worker-auth';
76
+
77
+ attachYichusWorkerVerifier(createClerkWorkerVerifier({
78
+ machineSecretKey: process.env.CLERK_API_MACHINE_SECRET,
79
+ targetMachine: process.env.CLERK_API_MACHINE_ID,
80
+ workers: [{
81
+ name: 'judgment',
82
+ subject: process.env.CLERK_JUDGMENT_MACHINE_ID,
83
+ roles: ['job-manager'],
84
+ }],
85
+ }));
86
+ await attachYichusStore('memory'); // Local example; configure the production store separately.
87
+ await startYichusApiEngine(8080);
88
+ ```
89
+
90
+ These are illustrative environment variable names. No Clerk resource or
91
+ secret is provisioned by this example. The package is currently a workspace
92
+ package; deployment packaging must include it, rather than assuming a registry
93
+ release. The listener always requires a supported `YICHUS_AUTH` mode, even when all
94
+ routes are worker routes; the worker verifier supplements that configuration.
95
+ The engine refuses to start with worker routes but no worker verifier.
96
+
97
+ A worker sends its opaque token as `Authorization: Bearer <token>` on the
98
+ worker route and supplies an `Idempotency-Key` for a mutation it may retry.
99
+ Body and result JSON come from the generated OpenAPI operation. Its
100
+ `x-yichus-worker` extension identifies the required worker; the operation
101
+ description lists every required grant.
102
+ The user-authenticated `/mcp` tool list excludes worker operations. This change
103
+ does not introduce machine authentication for that MCP endpoint. Generated
104
+ user frontends refuse worker routes as views rather than suggesting that a
105
+ user session could supply their authority; ordinary user views can coexist
106
+ with separately authenticated worker routes in the same API.
107
+
108
+ The in-process counterpart is
109
+ `yichusApiEngine.handleWithWorkerToken(path, token, args, { idempotencyKey })`.
110
+ Direct `handle`, raw role arrays and forged proof objects cannot obtain worker
111
+ authority. Installing a verifier is a trusted host capability: client code
112
+ running its own local engine cannot confer authority on the deployed API.
113
+ Never ship the Clerk adapter configured with a secret into a browser.
114
+
115
+ ## Refusals, retries and trust limits
116
+
117
+ Each request, including an idempotent replay, calls Clerk's
118
+ `POST /v1/m2m_tokens/verify` with the receiving machine's secret. The adapter
119
+ checks the provider's subject, target scopes, revocation and expiry. Custom
120
+ claims never select a worker name or role. The Clerk adapter copies configured grants at construction; attaching it stores
121
+ the verifier reference. Replacing the verifier invalidates earlier and pending
122
+ authority proofs.
123
+ The runtime preserves its private proof through async dispatch and rechecks
124
+ route authority before reading an idempotency record and before executing.
125
+ A restart during authentication refuses the in-flight request.
126
+
127
+ Invalid credentials return 401; an unconfigured sender, wrong target or
128
+ insufficient route grant returns 403. Provider outages, malformed provider
129
+ responses and verifier failures return 503 without returning credentials or
130
+ provider error bodies. Verification defaults to a five-second timeout and
131
+ has no successful-authentication cache. An outage therefore prevents even a
132
+ previously completed request from being replayed through this entry.
133
+
134
+ Revocation is checked at request authentication, not continuously throughout
135
+ an already executing request. A worker is trusted to implement its program;
136
+ its credential does not prove that its result is correct. The handler must
137
+ still enforce leases, fences, current revisions, payload validation and atomic
138
+ state transitions. Static access verification covers declared service entries;
139
+ it does not certify every exported function a custom host could invoke.
140
+
141
+ ## Executed evidence and Icetakes integration
142
+
143
+ `ApiWorkerAuthorityTest` compiles the real Bosatsu fixture, reads its authority
144
+ through API MCP, and exercises the generated HTTP and in-process engine with
145
+ the actual adapter and a local Clerk stand-in. It covers forged/user authority,
146
+ wrong sender/target, revoked retry, outage, restart and role loss before replay.
147
+ Runtime tests also cover proof replacement and malformed verifier results;
148
+ adapter tests cover timeout, provider failures and ignored custom grants.
149
+ The CLI proves all nine API properties for the fixture; negative variants
150
+ reject dynamic tables and an unprivileged caller to its shared helper.
151
+
152
+ These are local fixtures, not a live Clerk deployment. Icetakes still needs
153
+ its worker entry wired and audited against the actual stage protocol, live
154
+ machine provisioning, token rotation and outage/retry checks. Keep its combined
155
+ provider-dispatch path until that conversion is validated. The whole-product
156
+ G34 deployment verification work remains separate.
157
+
158
+ Provider contracts: [Clerk M2M verification](https://clerk.com/docs/reference/backend/m2m-tokens/verify)
159
+ and [M2M tokens](https://clerk.com/docs/guides/development/machine-auth/m2m-tokens).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yichus",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "Local Bosatsu compilation, Yichus analysis, and generated apps using the WebMCP engine",
5
5
  "license": "MIT",
6
6
  "homepage": "https://yich.us",
@@ -20,7 +20,7 @@
20
20
  "dependencies": {
21
21
  "zod": "^4.3.6",
22
22
  "@modelcontextprotocol/sdk": "^1.26.0",
23
- "@yichus/api-mcp": "0.1.1"
23
+ "@yichus/api-mcp": "0.1.3"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@types/node": "^22.0.0",