@pikku/core 0.12.74 → 0.12.78

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 (150) hide show
  1. package/CHANGELOG.md +397 -0
  2. package/dist/column-form.d.ts +32 -0
  3. package/dist/column-form.js +42 -0
  4. package/dist/crypto-utils.d.ts +15 -4
  5. package/dist/crypto-utils.js +18 -2
  6. package/dist/data-classification.d.ts +44 -0
  7. package/dist/function/functions.types.d.ts +23 -10
  8. package/dist/function/index.d.ts +1 -1
  9. package/dist/index.d.ts +6 -3
  10. package/dist/index.js +3 -1
  11. package/dist/middleware/auth-bearer.js +2 -1
  12. package/dist/middleware/remote-auth.js +1 -1
  13. package/dist/remote.js +1 -1
  14. package/dist/secret-value.d.ts +56 -0
  15. package/dist/secret-value.js +46 -0
  16. package/dist/services/audit-service.d.ts +74 -4
  17. package/dist/services/audit-service.js +7 -5
  18. package/dist/services/credential-wire-service.d.ts +5 -0
  19. package/dist/services/credential-wire-service.js +9 -1
  20. package/dist/services/email-service.d.ts +2 -1
  21. package/dist/services/in-memory-workflow-service.d.ts +1 -0
  22. package/dist/services/in-memory-workflow-service.js +20 -0
  23. package/dist/services/index.d.ts +3 -3
  24. package/dist/services/index.js +1 -1
  25. package/dist/services/local-content-request-handler.d.ts +29 -0
  26. package/dist/services/local-content-request-handler.js +176 -0
  27. package/dist/services/local-secrets.d.ts +4 -3
  28. package/dist/services/local-secrets.js +7 -3
  29. package/dist/services/logger.d.ts +22 -5
  30. package/dist/services/queue-webhook-service.js +1 -1
  31. package/dist/services/scoped-secret-service.d.ts +4 -3
  32. package/dist/services/secret-service.d.ts +8 -3
  33. package/dist/services/typed-secret-service.d.ts +5 -4
  34. package/dist/services/webhook-service.d.ts +2 -1
  35. package/dist/testing/service-tests.js +6 -6
  36. package/dist/types/core.types.d.ts +25 -4
  37. package/dist/wirings/ai-agent/ai-agent-agui.js +13 -1
  38. package/dist/wirings/ai-agent/ai-agent-prepare.js +7 -1
  39. package/dist/wirings/ai-agent/ai-agent-runner.js +14 -2
  40. package/dist/wirings/ai-agent/ai-agent-stream.js +27 -1
  41. package/dist/wirings/ai-agent/ai-agent.types.d.ts +40 -0
  42. package/dist/wirings/ai-agent/index.d.ts +1 -1
  43. package/dist/wirings/ai-agent/index.js +1 -1
  44. package/dist/wirings/ai-agent/voice-input.d.ts +20 -0
  45. package/dist/wirings/ai-agent/voice-input.js +44 -9
  46. package/dist/wirings/ai-agent/voice-output.d.ts +15 -0
  47. package/dist/wirings/ai-agent/voice-output.js +10 -1
  48. package/dist/wirings/cli/channel/cli-raw-client-runner.d.ts +21 -3
  49. package/dist/wirings/cli/channel/cli-raw-client-runner.js +13 -5
  50. package/dist/wirings/cli/channel/index.d.ts +1 -0
  51. package/dist/wirings/persona/define-personas.d.ts +4 -0
  52. package/dist/wirings/persona/define-personas.js +4 -0
  53. package/dist/wirings/persona/persona.types.d.ts +11 -0
  54. package/dist/wirings/queue/queue-identity.js +2 -1
  55. package/dist/wirings/queue/queue.types.d.ts +2 -1
  56. package/dist/wirings/queue/signed-queue-service.d.ts +2 -1
  57. package/dist/wirings/rpc/remote-addon-auth.d.ts +2 -1
  58. package/dist/wirings/rpc/remote-addon-auth.js +6 -2
  59. package/dist/wirings/virtual-user/index.d.ts +3 -0
  60. package/dist/wirings/virtual-user/index.js +2 -0
  61. package/dist/wirings/virtual-user/prepare-virtual-user-run.d.ts +54 -0
  62. package/dist/wirings/virtual-user/prepare-virtual-user-run.js +49 -0
  63. package/dist/wirings/virtual-user/virtual-user-run-store.d.ts +90 -0
  64. package/dist/wirings/virtual-user/virtual-user-run-store.js +1 -0
  65. package/dist/wirings/workflow/dsl/workflow-dsl.types.d.ts +14 -10
  66. package/dist/wirings/workflow/pikku-scenario-service.js +1 -2
  67. package/dist/wirings/workflow/pikku-workflow-service.d.ts +35 -0
  68. package/dist/wirings/workflow/pikku-workflow-service.js +51 -0
  69. package/dist/wirings/workflow/scenario-prose.js +1 -1
  70. package/dist/wirings/workflow/scenario-step.types.d.ts +13 -7
  71. package/dist/wirings/workflow/workflow.types.d.ts +7 -0
  72. package/knowledge/decisions/internals/a-virtual-user-run-is-not-a-workflow-and-not-a-queued-job.md +48 -0
  73. package/knowledge/decisions/internals/core-column-form-is-an-axis-of-its-own.md +84 -0
  74. package/knowledge/decisions/internals/core-data-classification-brand-is-an-optional-property.md +9 -2
  75. package/knowledge/decisions/internals/index.md +4 -0
  76. package/knowledge/decisions/internals/one-project-shape-check-two-validators.md +53 -0
  77. package/knowledge/decisions/internals/scenarios-live-in-files-named-for-them.md +48 -0
  78. package/knowledge/decisions/internals/validate-checks-personas-through-a-shared-module.md +43 -0
  79. package/package.json +3 -2
  80. package/src/column-form.test.ts +97 -0
  81. package/src/column-form.ts +58 -0
  82. package/src/crypto-utils.ts +25 -6
  83. package/src/data-classification.ts +44 -0
  84. package/src/function/functions.types.ts +47 -10
  85. package/src/function/index.ts +1 -0
  86. package/src/index.ts +24 -2
  87. package/src/middleware/auth-bearer.test.ts +3 -2
  88. package/src/middleware/auth-bearer.ts +2 -1
  89. package/src/middleware/remote-auth.test.ts +2 -1
  90. package/src/middleware/remote-auth.ts +1 -1
  91. package/src/remote.test.ts +2 -1
  92. package/src/remote.ts +1 -1
  93. package/src/secret-value.test.ts +204 -0
  94. package/src/secret-value.ts +111 -0
  95. package/src/services/audit-service.ts +87 -9
  96. package/src/services/credential-wire-service.ts +9 -1
  97. package/src/services/email-service.ts +3 -1
  98. package/src/services/in-memory-workflow-service.ts +29 -0
  99. package/src/services/index.ts +3 -3
  100. package/src/services/local-content-request-handler.test.ts +202 -0
  101. package/src/services/local-content-request-handler.ts +267 -0
  102. package/src/services/local-secrets.test.ts +20 -5
  103. package/src/services/local-secrets.ts +15 -7
  104. package/src/services/logger.ts +27 -7
  105. package/src/services/queue-webhook-service.test.ts +2 -1
  106. package/src/services/queue-webhook-service.ts +1 -1
  107. package/src/services/scoped-secret-service.ts +4 -3
  108. package/src/services/secret-service.ts +8 -3
  109. package/src/services/typed-secret-service.ts +11 -7
  110. package/src/services/webhook-service.ts +4 -1
  111. package/src/testing/service-tests.ts +6 -6
  112. package/src/types/core.types.ts +25 -4
  113. package/src/wirings/ai-agent/ai-agent-agui.test.ts +16 -0
  114. package/src/wirings/ai-agent/ai-agent-agui.ts +14 -1
  115. package/src/wirings/ai-agent/ai-agent-prepare.ts +7 -1
  116. package/src/wirings/ai-agent/ai-agent-runner.ts +18 -2
  117. package/src/wirings/ai-agent/ai-agent-stream.ts +32 -1
  118. package/src/wirings/ai-agent/ai-agent.types.ts +45 -1
  119. package/src/wirings/ai-agent/index.ts +2 -0
  120. package/src/wirings/ai-agent/voice-input.test.ts +65 -0
  121. package/src/wirings/ai-agent/voice-input.ts +48 -9
  122. package/src/wirings/ai-agent/voice-output.test.ts +91 -1
  123. package/src/wirings/ai-agent/voice-output.ts +28 -1
  124. package/src/wirings/cli/channel/cli-raw-client-runner.ts +39 -9
  125. package/src/wirings/cli/channel/index.ts +4 -0
  126. package/src/wirings/persona/define-personas.ts +4 -0
  127. package/src/wirings/persona/persona.types.ts +11 -0
  128. package/src/wirings/queue/queue-identity.test.ts +2 -1
  129. package/src/wirings/queue/queue-identity.ts +4 -1
  130. package/src/wirings/queue/queue.types.ts +6 -1
  131. package/src/wirings/queue/signed-queue-service.ts +2 -1
  132. package/src/wirings/rpc/remote-addon-auth.ts +8 -3
  133. package/src/wirings/rpc/rpc-runner.test.ts +6 -4
  134. package/src/wirings/virtual-user/index.ts +12 -0
  135. package/src/wirings/virtual-user/prepare-virtual-user-run.test.ts +115 -0
  136. package/src/wirings/virtual-user/prepare-virtual-user-run.ts +95 -0
  137. package/src/wirings/virtual-user/virtual-user-run-store.ts +98 -0
  138. package/src/wirings/workflow/dsl/workflow-dsl.types.ts +14 -16
  139. package/src/wirings/workflow/pikku-scenario-service.ts +1 -10
  140. package/src/wirings/workflow/pikku-workflow-service.ts +67 -0
  141. package/src/wirings/workflow/scenario-prose.test.ts +5 -7
  142. package/src/wirings/workflow/scenario-prose.ts +1 -1
  143. package/src/wirings/workflow/scenario-service.test.ts +0 -1
  144. package/src/wirings/workflow/scenario-step.test.ts +4 -5
  145. package/src/wirings/workflow/scenario-step.types.ts +13 -7
  146. package/src/wirings/workflow/scenario-surface.test.ts +6 -5
  147. package/src/wirings/workflow/workflow-stalled-recovery.test.ts +106 -0
  148. package/src/wirings/workflow/workflow.types.ts +7 -0
  149. package/tsconfig.tsbuildinfo +1 -1
  150. package/tsconfig.type-tests.json +12 -0
package/CHANGELOG.md CHANGED
@@ -1,3 +1,400 @@
1
+ ## 0.12.78
2
+
3
+ ### Patch Changes
4
+
5
+ - f5ce870: Recover workflow runs stalled by a crash mid-dispatch.
6
+
7
+ Arming a step is two writes to two systems — the step row, then the queue or
8
+ scheduler job — so a process that died between them left a run `running` with
9
+ nothing in flight. It parked on a step that would never complete and never
10
+ error, so the run neither finished nor failed, and nothing swept it up.
11
+
12
+ `workflowService.recoverStalledRuns()` re-drives those runs through
13
+ `resumeWorkflow`. Replay is memoized per step, so resuming a run that was not
14
+ actually stuck changes nothing; runs mid-sleep or with a step in flight are
15
+ excluded outright. It is not self-starting — call it from a scheduled task.
16
+
17
+ Stores opt in by overriding `findStalledRunIds`; implemented here for the
18
+ Kysely and in-memory services, and a no-op elsewhere.
19
+
20
+ ## 0.12.77
21
+
22
+ ### Patch Changes
23
+
24
+ - 3df4f95: Scaffold virtual user runs as RPCs, backed by a run store.
25
+
26
+ `pikku persona run` could already turn a declared persona loose on a running
27
+ stage, but only from a terminal, and the result only existed in that terminal's
28
+ output. There was no way for CI, a console, or a scheduled job to start a run —
29
+ and nothing kept what a run found, so this week's findings could not be compared
30
+ against last week's.
31
+
32
+ `scaffold.virtualUser` now generates two RPCs and the function behind them:
33
+ - `runVirtualUser({ persona, goals?, memory?, disposition?, budget?, seed? })
34
+ -> { runId }`
35
+ - `getVirtualUserRun({ runId }) -> { status, findings, tally, memory, … }`
36
+
37
+ They are gated on separate scopes — `virtualUser:run` and `virtualUser:read` —
38
+ because an adversarial run's findings are working exploits carrying live ids,
39
+ which makes reading them the more sensitive of the two. Production refuses every
40
+ disposition but `accountable`, checked against the effective one so the
41
+ per-run override cannot smuggle another in.
42
+
43
+ **A run is not a workflow and not a queued job.** It explores, so no two
44
+ attempts take the same steps and there is nothing to replay; and the record
45
+ already carries the progress a queue would only be holding on the way to the
46
+ same place. `runVirtualUser` writes the record, dispatches without awaiting, and
47
+ returns the id. The cost is stated on the type: a restart mid-run strands a
48
+ record at `running`, so a run older than its budget window and still `running`
49
+ is dead rather than working.
50
+
51
+ `@pikku/core` gains `VirtualUserRunStore` (with `virtualUserRunStore` on
52
+ `CoreSingletonServices`), and `@pikku/kysely` ships
53
+ `KyselyVirtualUserRunStore`, which creates its own table on first use like the
54
+ audit sink — the runtime never needs it, so it arrives with the feature that
55
+ fills it rather than in every database.
56
+
57
+ Also in core: `prepareVirtualUserRun`, which derives the catalogue, intents,
58
+ scopes and reachable agents in one place. `pikku persona run` reads the
59
+ inspector state and the generated RPC reads `metaService`, and the two have to
60
+ agree — otherwise the same persona and seed explore a different API depending on
61
+ how the run was started. `personaScopes` moved here from the CLI for the same
62
+ reason and is still re-exported from its old home. `PRODUCTION_DISPOSITION` is
63
+ now exported from `@pikku/core/virtual-user`, which it should always have been.
64
+
65
+ ## 0.12.76
66
+
67
+ ### Patch Changes
68
+
69
+ - 62ea4cc: The audit trail is now readable — in the generated meta, through an RPC, and as
70
+ a page in the console.
71
+
72
+ `audit: true` reaches `FunctionRuntimeMeta.audit` as its resolved form
73
+ (`{ durability }`), so which functions record anything is answerable without
74
+ running them. It is informational: the runner still resolves audit from the live
75
+ function config, so meta and runtime cannot disagree.
76
+
77
+ `AuditService` grows an optional read side — `query(AuditQuery)` and `facets()`.
78
+ Optional because a sink can legitimately be write-only: a queue producer that
79
+ hands events to another system has nothing to read back, and a reader that finds
80
+ these absent should say the trail is not readable here rather than that it is
81
+ empty. The two are very different answers to give someone auditing a system.
82
+
83
+ `KyselyAuditService` implements both, newest first with offset paging, filtered
84
+ by user, action and time window. Two things it now gets right that are easy to
85
+ get wrong: an empty filter array means "match nothing" rather than "no filter",
86
+ and results are read by physical _and_ camelCase key, because `CamelCasePlugin`
87
+ is on most pikku Kysely instances and renames result keys on the way out — the
88
+ mismatch does not throw, it returns a page of `undefined`. `init()` creates the
89
+ `audit` table for projects that do not migrate it themselves, from a new
90
+ exported `auditSchema` that stays out of `pikkuSchemas` because the runtime does
91
+ not need it.
92
+
93
+ The console addon exposes `console:getAudits` and `console:getAuditFilters`
94
+ behind a new `pikku:audit:read` scope, and forwards the application's `audit`
95
+ service into the addon's own services — without that last part every install
96
+ reported the trail as unreadable, whatever sink it had configured.
97
+
98
+ The console gets an Audit trail page: an infinite list filtered server-side by
99
+ user and action, and a row that opens the whole event, metadata rendered as a
100
+ JSON tree. Refused, unreadable and empty are three different screens, because
101
+ "you may not read this", "nobody can read this" and "nothing happened" are three
102
+ different facts.
103
+
104
+ Events name the person who caused them. The trail records a user id — the only
105
+ thing stable enough to record, since a name can change after the event — so
106
+ `getAudits` resolves those ids against better-auth's user directory at read
107
+ time, and the page shows the name while keeping the recorded id on the event.
108
+ The filter follows: pick a colleague by name, filter by the id. A scenario
109
+ actor is labelled as one, so synthetic traffic is not mistaken for real, and a
110
+ caller who was signed out shows the wire identity pikku resolved for them
111
+ rather than being credited to the system.
112
+
113
+ **Breaking, for anyone already reading `AuditEvent`:** `actor` is now
114
+ `userIdentity`, and its type `AuditActor` is `AuditUserIdentity`; `AuditQuery`
115
+ takes `userIds`/`orgId` in place of `actorUserIds`/`actorOrgId`, and
116
+ `AuditFacets` returns `userIds`. In pikku an _actor_ is a synthetic person a
117
+ scenario drives, flagged on the user row — so naming the causer of an event
118
+ `actor` made the synthetic case unsayable (`actor.actor === true`) and implied
119
+ every recorded action was a test. The overwhelming majority are ordinary
120
+ customers. The `audit` table follows: `actor_user_id` / `actor_org_id`
121
+ are now `user_id` / `org_id`, and a `pikku_user_id` column joins them so the
122
+ wire identity of a caller who never signed in survives the round trip — the
123
+ sink was dropping it, which left the console's Session field permanently
124
+ blank. A project that already migrated the table needs to rename the two
125
+ columns and add the third; `KyselyAuditService.init()` creates the new shape
126
+ for anyone who did not.
127
+
128
+ - 9dddff8: Split a column's at-rest form out of its classification.
129
+
130
+ `security: 'encrypted'` sat beside `'secret'` as though the two were
131
+ alternatives, which made the field unanswerable: a token hash and a live bearer
132
+ token are both secret, one must never be encrypted — the digest _is_ the lookup
133
+ key — and the other must always be. A column now carries a second, independent
134
+ `form: 'plain' | 'hashed' | 'wrapped' | 'sealed'` saying how the bytes are held.
135
+
136
+ Declaring a form other than `plain` makes the column's INSERT/UPDATE type
137
+ nominal — `WrappedValue`, `SealedValue`, `HashedValue` — so a plain string no
138
+ longer compiles there and the only way to write the column is with something an
139
+ encrypt, seal or hash call produced. `envelopeEncrypt`, `envelopeRewrap` and
140
+ `wrapDEK` now return the brand, and a new `hashToken` produces `HashedValue`, so
141
+ the round trip needs no casts; `column-form.ts` exports deliberately-named
142
+ `unsafeAs*` assertions for backfills, fixtures and values sealed elsewhere.
143
+ Reads are unaffected — the brands widen to `string` and compose with the
144
+ classification brand as `Secret<WrappedValue>`.
145
+
146
+ `wrapped` and `sealed` stay distinct because a sealed value is one the
147
+ application cannot read back; storing one where the other belongs is a row
148
+ nobody can open.
149
+
150
+ A `secret` column that has not declared a form now warns (PKU483), and a form on
151
+ a non-text column warns and is dropped (PKU484). Both are warnings, so existing
152
+ projects keep migrating — `pikku db --fail-on-warn` opts into the ratchet, and
153
+ an explicit `form: 'plain'` is the acknowledgement that silences it. The legacy
154
+ `security: 'encrypted'` keeps working and now expands to the pair it always
155
+ meant, `secret` + `wrapped`.
156
+
157
+ - 78b29f0: `SecretService` now returns a `SecretValue<T>` rather than the bare value, so a
158
+ vault secret cannot reach a sink by accident.
159
+
160
+ `SecretValue` is nominally typed, which means it is not assignable to `string`
161
+ (or to any other concretely-typed field). Every sink with a real type — a
162
+ database column, an email body, a session payload — rejects it with no lint
163
+ rule involved. The sinks typed `any`, `unknown`, or a free generic — the logger,
164
+ queue payloads, webhook and email inputs, and a function's own output — are
165
+ guarded with `Safe<T>`, which collapses a `SecretValue` found anywhere inside
166
+ `T`, however deeply nested, to `never`.
167
+
168
+ Unwrap deliberately at the point the secret reaches the wire:
169
+
170
+ ```ts
171
+ const secret = await secrets.getSecret('BETTER_AUTH_SECRET')
172
+ betterAuth({ secret: secret.reveal() })
173
+ ```
174
+
175
+ Two behaviours cover what types cannot see. Structured serialization redacts —
176
+ `JSON.stringify` and node's inspect both yield `[secret]`, so an audit or log
177
+ write stays honest without crashing the request. String coercion throws
178
+ `SecretCoercionError`, because a template literal is always a leak.
179
+
180
+ `AuditLog.write` is guarded the same way as the logger, since an audit event
181
+ carries `input` and `metadata` as `unknown` and nominality alone cannot stop a
182
+ secret landing in one.
183
+
184
+ `.reveal()` is the deliberate escape hatch, and what it hands back is an
185
+ ordinary string as far as every sink signature is concerned. **PKU953** closes
186
+ that gap: under `pikku all --security` the inspector reports a revealed secret
187
+ that flows into a logger, an audit, a queue, an email or a webhook — `console` included.
188
+
189
+ This also fixed a real one: `remote-addon-auth.ts` called `String(token)` on an
190
+ `unknown` and wrote the result straight into an `Authorization` header.
191
+
192
+ ## 0.12.75
193
+
194
+ ### Patch Changes
195
+
196
+ - 32277d5: Make a voice conversation with an agent something a chat surface can turn on, rather than
197
+ something each consumer reassembles.
198
+
199
+ The server half already worked — `voiceInput` transcribed, `voiceOutput` synthesized a
200
+ sentence at a time, and the AG-UI mapper forwarded the audio. What was missing was the
201
+ turn's own words. The client sends audio, so only the server ever knows what was said,
202
+ and nothing carried that back: a spoken turn rendered as an empty user bubble followed by
203
+ an answer to a question nobody could see, and thread history recorded the base64 audio
204
+ blob instead of the transcript — megabytes of unreadable data in place of the only
205
+ readable record of the turn.
206
+
207
+ `voiceInput` now records what it heard, the stream emits it as a `transcript` event ahead
208
+ of the run (the reply starts within a few hundred milliseconds, and a question that
209
+ appears after its answer reads as the wrong question), and it reaches the browser as
210
+ `pikku:transcript`. Both run paths persist the transcribed message rather than the one
211
+ that arrived on the wire. `audio-delta` also carries the sentence it says, which is what
212
+ a barge-in needs to report the part the user actually heard — a reply cut off after "I'll
213
+ delete the staging database and" is answered very differently depending on whether the
214
+ model knows the sentence never landed.
215
+
216
+ `@pikku/voice-agents` gains the two things a voice UI needs and could not get: a live
217
+ input level, attached to the source rather than to a detector so it keeps reading on the
218
+ Silero path, and the microphone list — re-readable on demand, because device labels are
219
+ empty until permission is granted and nothing fires when it is. `VoiceSession` also
220
+ learned manual turn boundaries, so push-to-talk is a mode rather than a detector fought
221
+ to a standstill: holding the key through a three-second pause is someone thinking, and
222
+ any endpointer worth having would cut them off.
223
+
224
+ `<PikkuAgentChat voice />` puts a microphone beside the send button, promotes it to
225
+ primary when nothing is typed, and opens an indicator with a live level bar, a device
226
+ picker and a hold-to-record toggle. It plays the agent's speech, and cancels the run on
227
+ barge-in — talking over the agent should stop the bill, not just the sound.
228
+
229
+ Opt-in, because the component cannot check the two things it depends on: the agent has to
230
+ be wired with `voiceInput` for the audio to be understood and `voiceOutput` for anything
231
+ to come back.
232
+
233
+ - ea8aabf: Serve `LocalContent` uploads and signed reads under Bun.
234
+
235
+ `LocalContent` hands the browser a `PUT <uploadUrlPrefix>/<key>` upload URL and a signed
236
+ `GET <assetUrlPrefix>/<key>` read URL, but it is a `ContentService` and cannot answer
237
+ either — something in the serving path has to. Only `@pikku/node-http-server` did. The
238
+ same project served under Bun handed out upload URLs that 404ed, with nothing naming the
239
+ cause: the config was accepted, the service was constructed, and the URLs looked right.
240
+
241
+ `@pikku/core` now exports `createLocalContentRequestHandler` from
242
+ `@pikku/core/services/local-content-request-handler` — the server half of `LocalContent`,
243
+ expressed in Web `Request`/`Response` so every runtime shares one implementation of the
244
+ signature check rather than each re-deriving it. It returns `null` for anything that is
245
+ not a content request, which is the caller's signal to carry on with its normal routing.
246
+
247
+ `PikkuBunServer` accepts `config.content` and a `contentSigningJWT` option, mirroring
248
+ `PikkuNodeHTTPServer`, and answers both prefixes ahead of static mounts and routing.
249
+ `BunServerRunner` was dropping `contentSigningJWT` on the floor, which silently disabled
250
+ signed asset reads for every Bun project even once the prefixes were served — the config
251
+ arrived, the service that verifies its signatures did not.
252
+
253
+ Signed reads are refused unless every claim matches, the path included: without that, a
254
+ signature minted for one asset would read any other.
255
+
256
+ - 33e96ab: Make a CLI served over a channel typecheck in a real project.
257
+
258
+ Both of these are unreachable for a hand-written `wireChannel`, whose routes are usually
259
+ bare identifiers, and unavoidable for a CLI one, whose routes are command ids.
260
+
261
+ `ChannelsMap` emitted route and message keys unquoted. A command id is a kebab or dotted
262
+ name far more often than not — `app-smoke`, `registry.search`, `package.upgrade-pikku` —
263
+ and each one ends the property early, so the generated map is not parseable TypeScript at
264
+ all. One project's map came out with 107 syntax errors from a single CLI channel. Keys are
265
+ now quoted when they are not bare identifiers, and left alone when they are, so existing
266
+ generated output is unchanged.
267
+
268
+ `executeRawCLIViaChannel` typed its renderers `Record<string, CorePikkuCLIRender<any>>`,
269
+ whose services parameter defaults to `CoreServices`. The renderers a generated client
270
+ passes are the app's own, typed against its `SingletonServices`, and a function taking
271
+ those is not assignable to one taking `CoreServices` — so the generated client failed to
272
+ compile for any app that adds a service, which is every app.
273
+
274
+ Rather than widen the type, it now says what is actually true on that side of the socket:
275
+ a renderer running on the client gets a logger and nothing else, because there is no
276
+ service container there to resolve anything from. `CorePikkuCLIClientRender` and
277
+ `ClientCLIRenderServices` are new exports of `@pikku/core/cli/channel`. They are not
278
+ expressible as `CorePikkuCLIRender`, whose `Services` parameter is constrained to
279
+ `CoreSingletonServices` and so demands a `config`, `variables` and `secrets` the client
280
+ cannot invent. The one cast from the app's renderer type to that shape is localised to the
281
+ generated client, where it is sound: generation refuses to emit the file at all if a
282
+ renderer reaches for a service other than `logger`.
283
+
284
+ - fd72e58: Drop `scenario.step` — a scenario step is now always a `given`, `when` or
285
+ `then`.
286
+
287
+ `step` rendered no keyword, which made it the phase to reach for whenever a
288
+ step did not obviously fit one of the three. That is exactly the step a reader
289
+ cannot check: a scenario is read by people deciding whether it describes the
290
+ behaviour they wanted, and a row that says what it does without saying whether
291
+ it is setup, action or claim tells them nothing to agree or disagree with. It
292
+ was also the escape hatch from the assertion lint — a scenario with no `then`
293
+ could be made to stop complaining by demoting its steps rather than by
294
+ asserting anything.
295
+
296
+ Replace `scenario.step(...)` with whichever of `given`, `when` or `then` the
297
+ step actually is. `then` is not a rename: it makes the step's bindings
298
+ witnesses rather than alternatives, so every declared surface runs and they
299
+ must agree.
300
+
301
+ - fd72e58: Make personas a first-class surface rather than a detail of the test runner.
302
+
303
+ A persona is now read in three places — the knowledge base resolves `persona:`
304
+ URIs against it, scenarios cast it as an actor, and a virtual user runs as it —
305
+ so it gets its own page at `/personas` under a new **People** section in the
306
+ rail, alongside Users. The card is a profile: avatar, name, job title, computed
307
+ address, the system roles they hold, and how many scenarios cast them. Opening
308
+ one expands each role to the scopes it confers, which is the half of the picture
309
+ that explains a 403.
310
+
311
+ `definePersonas` takes an optional `avatarUrl` — any URL a browser can load.
312
+ Nothing is derived from the address: a persona's address is synthetic, so a
313
+ derived identicon would be the same shrug for everyone. Omitted, the console
314
+ keeps drawing the deterministic colour-and-icon avatar from the persona's id.
315
+
316
+ - fd72e58: Read the actors that are not people on the personas page.
317
+
318
+ The platform — the app acting on itself, what `pikkuPlatformScenarioStep`
319
+ declares — now has a row of its own, alongside one per addon whose system a
320
+ step makes act. They sit behind a People / System / All filter that opens on
321
+ the people: a subject holds no roles and signs in as nobody, so leading with it
322
+ would put the rows nothing is authorized through above the ones that are.
323
+
324
+ The platform row is built in rather than derived. A project that has never
325
+ written a platform step still has a platform, and a card that appeared the
326
+ moment somebody declared their first step would read as a feature they had
327
+ switched on.
328
+
329
+ Also: PKU680 now counts `expectService`, `expectError` and `expectEventually`
330
+ as assertions. They are inline steps and carry no phase, so a scenario whose
331
+ only witness was a recorded service call was being told it never asserts.
332
+
333
+ - 894b2f8: `defineScope` and `defineSystemRole` accumulate across call sites again. Only `definePersonas` is one-per-codebase.
334
+
335
+ The previous release made all three single-declaration constructs, which no project scaffolding user-admin could satisfy: the CLI generates a `defineScope` of its own in `user-admin.gen.ts` carrying the whole `admin` tree, and `@pikku/addon-console` spells the same tree out again, so a second hand-written declaration failed the build with PKU583 — and the losing file's scopes were dropped from the metadata rather than merged.
336
+
337
+ Exempting generated files would have reinstated exactly the ambiguity the rule removes, only for the files nobody can read the rule from. The real fix is for `admin` to be a default scope nobody declares, at which point the rule can come back for scopes and roles.
338
+
339
+ `definePersonas` is unaffected: nothing generates one, so its single call site stands.
340
+
341
+ - dd19aa7: Drop `scopes` from sessionless functions, rename `selfAuthenticated`, and make both
342
+ escape hatches opt-in.
343
+
344
+ **`scopes` are gone from `pikkuSessionlessFunc`.** They are AND-ed and `verifyScopes`
345
+ fails closed on a session that does not exist, so every scope listed on a sessionless
346
+ function rejected the anonymous caller it exists to serve. `CorePikkuSessionlessFunctionConfig`
347
+ now states this once in core, and the generated `pikkuSessionlessFunc` / `pikkuVoidFunc`
348
+ configs derive from it — so the field is absent rather than subtracted.
349
+
350
+ `@pikku/addon-console`'s `installAddon` and `installOpenapiAddon` are now `pikkuFunc`.
351
+ Both set `auth: true` and `scopes: ['admin']`, and a test exercises that gate, so the
352
+ scopes were load-bearing — they only compiled as sessionless because the config accepted
353
+ a field it could not honour. No behaviour change: both already required a session.
354
+
355
+ **`selfAuthenticated` is now `permissionsInBody`.** It never described authentication:
356
+ what it records is that the permission check lives in the function body rather than in a
357
+ declared `permissions` entry.
358
+
359
+ **Both escape hatches must be opted into**, via a new `allow` block in
360
+ `pikku.config.json`:
361
+
362
+ ```json
363
+ "allow": { "permissionsInBody": true, "complexWorkflows": true }
364
+ ```
365
+
366
+ Unset means unavailable, and using the feature is a build error naming the flag that
367
+ would permit it — PKU576 for `permissionsInBody`, PKU643 for `pikkuWorkflowComplexFunc`.
368
+ Both trade something the tooling can inspect for something only a reader can verify: a
369
+ permission check buried in a body, or workflow steps that cannot be serialized into the
370
+ graph, replayed, or migrated. Both are occasionally right, and both are the path of least
371
+ resistance whenever the declarative form is merely inconvenient. Whoever owns the project
372
+ makes that call once, in writing, instead of every author making it silently at the call
373
+ site.
374
+
375
+ **PKU574's message no longer contradicts any of this.** Every function it reports is
376
+ sessionless — that is how the population is selected, not a finding — yet it opened by
377
+ reporting that they "require neither a session", then advised adding scopes. It now names
378
+ them as sessionless and recommends only gates an anonymous caller can meet:
379
+ `permissions`, `auth: true`, `wireAddon({ auth: true })`, or dropping `expose: true`.
380
+ `permissionsInBody` is deliberately absent from that list: a diagnostic should not
381
+ advertise its own escape hatch.
382
+
383
+ - 50ec500: Make `defineScope`, `defineSystemRole` and `definePersonas` single-declaration constructs
384
+ — exactly one call site per codebase, the rule `pikkuBetterAuth` has always had.
385
+
386
+ Each of the three already takes a keyed object, so one call declares as many entries as
387
+ you like. Spreading the calls across files bought nothing and cost the thing that matters:
388
+ there was no answer to "where do I add a persona?", so downstream tooling and agents had
389
+ nowhere unambiguous to read from or append to. The only duplicate handling that existed
390
+ caught a narrow case — the same id declared twice with different content — and said
391
+ nothing about the same id declared twice in two files.
392
+
393
+ A second call now fails the build with `PKU583` (`defineScope`), `PKU584`
394
+ (`defineSystemRole`) or `PKU585` (`definePersonas`), naming both source files and saying
395
+ to declare them all in one call. A second call in the _same_ file is refused too: "the
396
+ file" is not an answer either when the file holds two calls.
397
+
1
398
  ## 0.12.74
2
399
 
3
400
  ### Patch Changes
@@ -0,0 +1,32 @@
1
+ import type { HashedValue, SealedValue, WrappedValue } from './data-classification.js';
2
+ /**
3
+ * SHA-256 of a credential, lowercase hex — the sole producer of `HashedValue`.
4
+ *
5
+ * Deliberately unpeppered. These are high-entropy random bearer tokens, not
6
+ * passwords: there is no candidate set to search, so a pepper would add a key
7
+ * to manage and a rotation story to own while buying nothing. A column holding
8
+ * a *low*-entropy secret is not a `hashed` column — it wants a password KDF and
9
+ * a different type entirely.
10
+ *
11
+ * Async because it runs on WebCrypto rather than `node:crypto`, so the same
12
+ * code path works in a Worker. Callers that hash a bearer token on every
13
+ * request are already async.
14
+ */
15
+ export declare const hashToken: (raw: string) => Promise<HashedValue>;
16
+ /**
17
+ * Assert that a string already in storage is ciphertext of the given form.
18
+ *
19
+ * The brands exist so that new writes must come from a real encrypt/hash call,
20
+ * but three paths legitimately hold such a value as a bare `string`: a
21
+ * migration backfilling rows written before the column declared its form, a
22
+ * test fixture, and a value arriving over the wire from a service that sealed
23
+ * it elsewhere. Each of those is a promise the caller is making, not something
24
+ * the type system can check — which is why these are named to be greppable and
25
+ * why there is no non-`unsafe` spelling. If one of these appears in ordinary
26
+ * request-handling code, that is the bug.
27
+ */
28
+ export declare const unsafeAsWrapped: (stored: string) => WrappedValue;
29
+ /** See `unsafeAsWrapped`. */
30
+ export declare const unsafeAsSealed: (stored: string) => SealedValue;
31
+ /** See `unsafeAsWrapped`. */
32
+ export declare const unsafeAsHashed: (stored: string) => HashedValue;
@@ -0,0 +1,42 @@
1
+ /**
2
+ * SHA-256 of a credential, lowercase hex — the sole producer of `HashedValue`.
3
+ *
4
+ * Deliberately unpeppered. These are high-entropy random bearer tokens, not
5
+ * passwords: there is no candidate set to search, so a pepper would add a key
6
+ * to manage and a rotation story to own while buying nothing. A column holding
7
+ * a *low*-entropy secret is not a `hashed` column — it wants a password KDF and
8
+ * a different type entirely.
9
+ *
10
+ * Async because it runs on WebCrypto rather than `node:crypto`, so the same
11
+ * code path works in a Worker. Callers that hash a bearer token on every
12
+ * request are already async.
13
+ */
14
+ export const hashToken = async (raw) => {
15
+ const subtle = globalThis.crypto?.subtle;
16
+ if (!subtle) {
17
+ throw new Error('WebCrypto not available');
18
+ }
19
+ const digest = await subtle.digest('SHA-256', new TextEncoder().encode(raw));
20
+ let hex = '';
21
+ for (const byte of new Uint8Array(digest)) {
22
+ hex += byte.toString(16).padStart(2, '0');
23
+ }
24
+ return hex;
25
+ };
26
+ /**
27
+ * Assert that a string already in storage is ciphertext of the given form.
28
+ *
29
+ * The brands exist so that new writes must come from a real encrypt/hash call,
30
+ * but three paths legitimately hold such a value as a bare `string`: a
31
+ * migration backfilling rows written before the column declared its form, a
32
+ * test fixture, and a value arriving over the wire from a service that sealed
33
+ * it elsewhere. Each of those is a promise the caller is making, not something
34
+ * the type system can check — which is why these are named to be greppable and
35
+ * why there is no non-`unsafe` spelling. If one of these appears in ordinary
36
+ * request-handling code, that is the bug.
37
+ */
38
+ export const unsafeAsWrapped = (stored) => stored;
39
+ /** See `unsafeAsWrapped`. */
40
+ export const unsafeAsSealed = (stored) => stored;
41
+ /** See `unsafeAsWrapped`. */
42
+ export const unsafeAsHashed = (stored) => stored;
@@ -1,3 +1,4 @@
1
+ import type { WrappedValue } from './data-classification.js';
1
2
  export declare const encryptJSON: (secret: string, value: unknown) => Promise<string>;
2
3
  export declare const decryptJSON: <T>(secret: string, token: string) => Promise<T>;
3
4
  /**
@@ -26,12 +27,22 @@ export declare const generateDEK: () => Promise<string>;
26
27
  */
27
28
  export declare const generateKEKSalt: () => string;
28
29
  export declare const deriveKEK: (passphrase: string, salt: string) => Promise<CryptoKey>;
29
- export declare const wrapDEK: (kek: CryptoKey, plaintextDEK: string) => Promise<string>;
30
+ export declare const wrapDEK: (kek: CryptoKey, plaintextDEK: string) => Promise<WrappedValue>;
30
31
  export declare const unwrapDEK: (kek: CryptoKey, wrappedDEK: string) => Promise<string>;
31
32
  export interface EnvelopeEncryptResult {
32
- ciphertext: string;
33
- wrappedDEK: string;
33
+ ciphertext: WrappedValue;
34
+ wrappedDEK: WrappedValue;
34
35
  }
35
36
  export declare const envelopeEncrypt: (kek: CryptoKey, value: unknown) => Promise<EnvelopeEncryptResult>;
37
+ /**
38
+ * Note the inputs are plain `string`, not `WrappedValue`.
39
+ *
40
+ * The brand exists to stop plaintext being *written* to a wrapped column, and
41
+ * a `WrappedValue` is assignable to `string`, so a branded caller still passes
42
+ * without a cast. Demanding the brand here would buy nothing — feeding the
43
+ * wrong string in already fails at the AEAD tag — while forcing a cast into
44
+ * every path that reads ciphertext back out of a row, a parsed envelope, or the
45
+ * wire, which is exactly where casts are least reviewable.
46
+ */
36
47
  export declare const envelopeDecrypt: <T>(kek: CryptoKey, ciphertext: string, wrappedDEK: string) => Promise<T>;
37
- export declare const envelopeRewrap: (oldKEK: CryptoKey, newKEK: CryptoKey, wrappedDEK: string) => Promise<string>;
48
+ export declare const envelopeRewrap: (oldKEK: CryptoKey, newKEK: CryptoKey, wrappedDEK: string) => Promise<WrappedValue>;
@@ -217,8 +217,14 @@ export const generateKEKSalt = () => {
217
217
  export const deriveKEK = async (passphrase, salt) => {
218
218
  return deriveKey(passphrase, fromBase64Url(salt));
219
219
  };
220
+ /**
221
+ * The one place a `WrappedValue` is minted. Every branded return below routes
222
+ * through here, so the assertion "these bytes really are ciphertext" is made
223
+ * once and audited once instead of at each call site.
224
+ */
225
+ const asWrapped = (ciphertext) => ciphertext;
220
226
  export const wrapDEK = async (kek, plaintextDEK) => {
221
- return encryptWithCryptoKey(kek, plaintextDEK);
227
+ return asWrapped(await encryptWithCryptoKey(kek, plaintextDEK));
222
228
  };
223
229
  export const unwrapDEK = async (kek, wrappedDEK) => {
224
230
  return decryptWithCryptoKey(kek, wrappedDEK);
@@ -231,10 +237,20 @@ const decryptWithDEK = async (dekBase64, token) => {
231
237
  };
232
238
  export const envelopeEncrypt = async (kek, value) => {
233
239
  const dek = await generateDEK();
234
- const ciphertext = await encryptWithDEK(dek, value);
240
+ const ciphertext = asWrapped(await encryptWithDEK(dek, value));
235
241
  const wrappedDEK = await wrapDEK(kek, dek);
236
242
  return { ciphertext, wrappedDEK };
237
243
  };
244
+ /**
245
+ * Note the inputs are plain `string`, not `WrappedValue`.
246
+ *
247
+ * The brand exists to stop plaintext being *written* to a wrapped column, and
248
+ * a `WrappedValue` is assignable to `string`, so a branded caller still passes
249
+ * without a cast. Demanding the brand here would buy nothing — feeding the
250
+ * wrong string in already fails at the AEAD tag — while forcing a cast into
251
+ * every path that reads ciphertext back out of a row, a parsed envelope, or the
252
+ * wire, which is exactly where casts are least reviewable.
253
+ */
238
254
  export const envelopeDecrypt = async (kek, ciphertext, wrappedDEK) => {
239
255
  const dek = await unwrapDEK(kek, wrappedDEK);
240
256
  return decryptWithDEK(dek, ciphertext);
@@ -9,12 +9,56 @@ export type Secret<T> = T & {
9
9
  };
10
10
  export type Classification = 'public' | 'private' | 'pii' | 'secret';
11
11
  export type AnonymizeStrategy = 'fake:email' | 'fake:name' | 'hash' | 'keep' | null;
12
+ /**
13
+ * How a column's bytes are represented at rest, as distinct from how sensitive
14
+ * the value is (`Classification`). The two are independent: a token hash is
15
+ * `secret` + `hashed` and must never be encrypted, because the hash *is* the
16
+ * lookup key; a live bearer token is `secret` + `plain` today and should not be.
17
+ *
18
+ * `wrapped` and `sealed` are siblings rather than one being "encrypted": both
19
+ * are ciphertext, and what separates them is who can read it back. Wrapped is
20
+ * symmetric and the application holds the key. Sealed is asymmetric and the
21
+ * application holds only the public half, so it can write the value and never
22
+ * read it. Storing one where the other is expected is silent, permanent data
23
+ * loss, which is why they are not collapsed into a single `encrypted`.
24
+ */
25
+ export type ColumnForm = 'plain' | 'hashed' | 'wrapped' | 'sealed';
26
+ declare const wrappedBrand: unique symbol;
27
+ declare const sealedBrand: unique symbol;
28
+ declare const hashedBrand: unique symbol;
29
+ /**
30
+ * Ciphertext under a symmetric key the application holds — the output of
31
+ * `envelopeEncrypt`, `wrapDEK` or `envelopeRewrap`.
32
+ *
33
+ * Unlike `Secret<T>` the brand is REQUIRED, so a plain `string` is not
34
+ * assignable and a column declared `form: 'wrapped'` cannot be written with
35
+ * anything but genuine ciphertext. It stays assignable *to* `string`, so it
36
+ * still works as a query operand and serializes normally — the constraint is on
37
+ * construction, not on use.
38
+ */
39
+ export type WrappedValue = string & {
40
+ readonly [wrappedBrand]: true;
41
+ };
42
+ /** Ciphertext under a public key whose private half the application does not
43
+ * hold. Deliberately not assignable to `WrappedValue`: writing one where the
44
+ * other belongs produces a row nobody can ever open. */
45
+ export type SealedValue = string & {
46
+ readonly [sealedBrand]: true;
47
+ };
48
+ /** A one-way digest of a secret input. The brand's job is narrow — stop a *raw*
49
+ * credential being written into the column that should hold its hash. */
50
+ export type HashedValue = string & {
51
+ readonly [hashedBrand]: true;
52
+ };
12
53
  export interface ColumnClassification {
13
54
  classification: Classification;
14
55
  anonymize_strategy: AnonymizeStrategy;
56
+ /** At-rest representation. Absent means `plain`. */
57
+ form?: ColumnForm;
15
58
  description?: string;
16
59
  }
17
60
  export type ClassificationManifest = {
18
61
  version: 1;
19
62
  tables: Record<string, Record<string, ColumnClassification>>;
20
63
  };
64
+ export {};