@pikku/cli 0.12.40 → 0.12.42

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 (195) hide show
  1. package/dist/.pikku/agent/pikku-agent-types.gen.d.ts +1 -1
  2. package/dist/.pikku/channel/pikku-channel-types.gen.d.ts +3 -3
  3. package/dist/.pikku/channel/pikku-channel-types.gen.js +1 -1
  4. package/dist/.pikku/cli/pikku-cli-channel.d.ts +6 -6
  5. package/dist/.pikku/cli/pikku-cli-channel.js +21 -1
  6. package/dist/.pikku/cli/pikku-cli-types.gen.d.ts +1 -1
  7. package/dist/.pikku/cli/pikku-cli-types.gen.js +1 -1
  8. package/dist/.pikku/cli/pikku-cli-wirings-meta.gen.js +1 -1
  9. package/dist/.pikku/cli/pikku-cli-wirings-meta.gen.json +68 -0
  10. package/dist/.pikku/cli/pikku-cli-wirings.gen.d.ts +1 -1
  11. package/dist/.pikku/cli/pikku-cli-wirings.gen.js +1 -1
  12. package/dist/.pikku/cli/pikku-cli.gen.d.ts +1 -1
  13. package/dist/.pikku/cli/pikku-cli.gen.js +1 -1
  14. package/dist/.pikku/console/pikku-node-types.gen.d.ts +1 -1
  15. package/dist/.pikku/function/pikku-function-types.gen.d.ts +2 -2
  16. package/dist/.pikku/function/pikku-function-types.gen.js +1 -1
  17. package/dist/.pikku/function/pikku-functions-meta.gen.js +1 -1
  18. package/dist/.pikku/function/pikku-functions-meta.gen.json +152 -82
  19. package/dist/.pikku/function/pikku-functions.gen.js +1 -1
  20. package/dist/.pikku/http/pikku-http-types.gen.d.ts +1 -1
  21. package/dist/.pikku/http/pikku-http-types.gen.js +1 -1
  22. package/dist/.pikku/http/pikku-http-wirings-meta.gen.js +1 -1
  23. package/dist/.pikku/http/pikku-http-wirings.gen.d.ts +1 -1
  24. package/dist/.pikku/http/pikku-http-wirings.gen.js +1 -1
  25. package/dist/.pikku/mcp/pikku-mcp-types.gen.d.ts +1 -1
  26. package/dist/.pikku/mcp/pikku-mcp-types.gen.js +1 -1
  27. package/dist/.pikku/pikku-bootstrap.gen.d.ts +1 -1
  28. package/dist/.pikku/pikku-bootstrap.gen.js +1 -1
  29. package/dist/.pikku/pikku-meta-service.gen.d.ts +1 -1
  30. package/dist/.pikku/pikku-meta-service.gen.js +1 -1
  31. package/dist/.pikku/pikku-services.gen.d.ts +1 -1
  32. package/dist/.pikku/pikku-types.gen.d.ts +1 -1
  33. package/dist/.pikku/pikku-types.gen.js +1 -1
  34. package/dist/.pikku/queue/pikku-queue-types.gen.d.ts +1 -1
  35. package/dist/.pikku/queue/pikku-queue-types.gen.js +1 -1
  36. package/dist/.pikku/queue/pikku-queue-workers-wirings-meta.gen.js +1 -1
  37. package/dist/.pikku/queue/pikku-queue-workers-wirings.gen.d.ts +1 -1
  38. package/dist/.pikku/queue/pikku-queue-workers-wirings.gen.js +1 -1
  39. package/dist/.pikku/rpc/pikku-rpc-wirings-meta.internal.gen.js +1 -1
  40. package/dist/.pikku/rpc/pikku-rpc-wirings-meta.internal.gen.json +4 -0
  41. package/dist/.pikku/scheduler/pikku-scheduler-types.gen.d.ts +1 -1
  42. package/dist/.pikku/scheduler/pikku-scheduler-types.gen.js +1 -1
  43. package/dist/.pikku/schemas/register.gen.js +13 -5
  44. package/dist/.pikku/schemas/schemas/FabricLinkOutput.schema.json +1 -1
  45. package/dist/.pikku/schemas/schemas/FabricSmokeInput.schema.json +1 -0
  46. package/dist/.pikku/schemas/schemas/FabricSmokeOutput.schema.json +1 -0
  47. package/dist/.pikku/schemas/schemas/LogoutInput.schema.json +1 -0
  48. package/dist/.pikku/schemas/schemas/WhoamiInput.schema.json +1 -0
  49. package/dist/.pikku/secrets/pikku-secret-types.gen.d.ts +1 -1
  50. package/dist/.pikku/secrets/pikku-secret-types.gen.js +1 -1
  51. package/dist/.pikku/secrets/pikku-secrets.gen.d.ts +1 -1
  52. package/dist/.pikku/secrets/pikku-secrets.gen.js +1 -1
  53. package/dist/.pikku/trigger/pikku-trigger-types.gen.d.ts +1 -1
  54. package/dist/.pikku/trigger/pikku-trigger-types.gen.js +1 -1
  55. package/dist/.pikku/variables/pikku-variable-types.gen.d.ts +1 -1
  56. package/dist/.pikku/variables/pikku-variable-types.gen.js +1 -1
  57. package/dist/.pikku/variables/pikku-variables.gen.d.ts +1 -1
  58. package/dist/.pikku/variables/pikku-variables.gen.js +1 -1
  59. package/dist/.pikku/workflow/pikku-workflow-types.gen.d.ts +1 -1
  60. package/dist/.pikku/workflow/pikku-workflow-types.gen.js +1 -1
  61. package/dist/.pikku/workflow/pikku-workflow-wirings-meta.gen.js +1 -1
  62. package/dist/.pikku/workflow/pikku-workflow-wirings.gen.js +1 -1
  63. package/dist/bin/pikku-bin.mjs +2 -2
  64. package/dist/src/cli.wiring.js +45 -0
  65. package/dist/src/fabric/fabric-commands.d.ts +130 -63
  66. package/dist/src/fabric/fabric-commands.js +21 -0
  67. package/dist/src/fabric/functions/db-schema.function.d.ts +3 -3
  68. package/dist/src/fabric/functions/deploy-list.function.d.ts +3 -3
  69. package/dist/src/fabric/functions/deploy-units.function.d.ts +3 -3
  70. package/dist/src/fabric/functions/deploy.function.d.ts +6 -6
  71. package/dist/src/fabric/functions/domains-add.function.d.ts +3 -3
  72. package/dist/src/fabric/functions/domains-list.function.d.ts +3 -3
  73. package/dist/src/fabric/functions/domains-remove.function.d.ts +3 -3
  74. package/dist/src/fabric/functions/errors.function.d.ts +3 -3
  75. package/dist/src/fabric/functions/init.function.d.ts +3 -3
  76. package/dist/src/fabric/functions/link.function.d.ts +7 -3
  77. package/dist/src/fabric/functions/link.function.js +3 -1
  78. package/dist/src/fabric/functions/llm-key.function.d.ts +3 -3
  79. package/dist/src/fabric/functions/login.function.d.ts +3 -3
  80. package/dist/src/fabric/functions/logs.function.d.ts +3 -3
  81. package/dist/src/fabric/functions/metrics.function.d.ts +3 -3
  82. package/dist/src/fabric/functions/rollback.function.d.ts +3 -3
  83. package/dist/src/fabric/functions/secrets-list.function.d.ts +3 -3
  84. package/dist/src/fabric/functions/secrets-set.function.d.ts +3 -3
  85. package/dist/src/fabric/functions/smoke.function.d.ts +93 -0
  86. package/dist/src/fabric/functions/smoke.function.js +860 -0
  87. package/dist/src/fabric/functions/status.function.d.ts +3 -3
  88. package/dist/src/fabric/functions/trace.function.d.ts +3 -3
  89. package/dist/src/fabric/functions/validate.function.d.ts +3 -3
  90. package/dist/src/functions/commands/all.d.ts +1 -1
  91. package/dist/src/functions/commands/binary.d.ts +3 -3
  92. package/dist/src/functions/commands/bootstrap.d.ts +1 -1
  93. package/dist/src/functions/commands/console.d.ts +3 -3
  94. package/dist/src/functions/commands/db-audit.d.ts +1 -1
  95. package/dist/src/functions/commands/db-generate.d.ts +1 -1
  96. package/dist/src/functions/commands/db-migrate.d.ts +1 -1
  97. package/dist/src/functions/commands/db-reset.d.ts +1 -1
  98. package/dist/src/functions/commands/db-seed.d.ts +1 -1
  99. package/dist/src/functions/commands/deploy-apply.d.ts +3 -3
  100. package/dist/src/functions/commands/deploy-info.d.ts +1 -1
  101. package/dist/src/functions/commands/deploy-plan.d.ts +3 -3
  102. package/dist/src/functions/commands/dev.d.ts +3 -3
  103. package/dist/src/functions/commands/emails-init.d.ts +1 -1
  104. package/dist/src/functions/commands/enable.d.ts +5 -5
  105. package/dist/src/functions/commands/info.d.ts +4 -4
  106. package/dist/src/functions/commands/login.d.ts +37 -0
  107. package/dist/src/functions/commands/login.js +85 -0
  108. package/dist/src/functions/commands/meta.d.ts +31 -31
  109. package/dist/src/functions/commands/new-addon.d.ts +3 -3
  110. package/dist/src/functions/commands/new-function.d.ts +3 -3
  111. package/dist/src/functions/commands/new-middleware.d.ts +3 -3
  112. package/dist/src/functions/commands/new-permission.d.ts +3 -3
  113. package/dist/src/functions/commands/new-wiring.d.ts +3 -3
  114. package/dist/src/functions/commands/pikku-command-bootstrap.d.ts +1 -1
  115. package/dist/src/functions/commands/pikku-command-summary.d.ts +1 -1
  116. package/dist/src/functions/commands/skills.d.ts +6 -6
  117. package/dist/src/functions/commands/tests-coverage.d.ts +3 -3
  118. package/dist/src/functions/commands/tests-init.d.ts +3 -3
  119. package/dist/src/functions/commands/versions-check.d.ts +1 -1
  120. package/dist/src/functions/commands/versions-init.d.ts +3 -3
  121. package/dist/src/functions/commands/versions-update.d.ts +1 -1
  122. package/dist/src/functions/commands/watch.d.ts +3 -3
  123. package/dist/src/functions/commands/workspace-validate.d.ts +3 -3
  124. package/dist/src/functions/db/local-db.js +20 -75
  125. package/dist/src/functions/runtimes/fetch/index.d.ts +1 -1
  126. package/dist/src/functions/runtimes/nextjs/pikku-command-nextjs.d.ts +1 -1
  127. package/dist/src/functions/runtimes/tanstack-start/pikku-command-tanstack-start.d.ts +1 -1
  128. package/dist/src/functions/runtimes/websocket/pikku-command-websocket-typed.d.ts +1 -1
  129. package/dist/src/functions/wirings/ai-agent/pikku-command-ai-agent-types.d.ts +1 -1
  130. package/dist/src/functions/wirings/ai-agent/pikku-command-ai-agent.d.ts +1 -1
  131. package/dist/src/functions/wirings/ai-agent/pikku-command-public-agent.d.ts +1 -1
  132. package/dist/src/functions/wirings/auth/pikku-command-auth.d.ts +1 -1
  133. package/dist/src/functions/wirings/channels/pikku-channels.d.ts +1 -1
  134. package/dist/src/functions/wirings/channels/pikku-command-channel-types.d.ts +1 -1
  135. package/dist/src/functions/wirings/channels/pikku-command-channels-map.d.ts +1 -1
  136. package/dist/src/functions/wirings/channels/pikku-command-channels.d.ts +1 -1
  137. package/dist/src/functions/wirings/cli/pikku-command-cli-entry.d.ts +1 -1
  138. package/dist/src/functions/wirings/cli/pikku-command-cli-types.d.ts +1 -1
  139. package/dist/src/functions/wirings/cli/pikku-command-cli.d.ts +1 -1
  140. package/dist/src/functions/wirings/cli/serialize-channel-cli-client.js +38 -7
  141. package/dist/src/functions/wirings/console/pikku-command-console-functions.d.ts +1 -1
  142. package/dist/src/functions/wirings/console/pikku-command-node-types.d.ts +1 -1
  143. package/dist/src/functions/wirings/console/pikku-command-nodes-meta.d.ts +1 -1
  144. package/dist/src/functions/wirings/credentials/pikku-command-credentials.d.ts +1 -1
  145. package/dist/src/functions/wirings/emails/pikku-command-emails.d.ts +1 -1
  146. package/dist/src/functions/wirings/functions/pikku-command-addon-types.d.ts +1 -1
  147. package/dist/src/functions/wirings/functions/pikku-command-function-types-split.d.ts +3 -3
  148. package/dist/src/functions/wirings/functions/pikku-command-function-types.d.ts +3 -3
  149. package/dist/src/functions/wirings/functions/pikku-command-functions.d.ts +1 -1
  150. package/dist/src/functions/wirings/functions/pikku-command-services.d.ts +1 -1
  151. package/dist/src/functions/wirings/functions/schemas.d.ts +1 -1
  152. package/dist/src/functions/wirings/gateway/pikku-command-gateway.d.ts +1 -1
  153. package/dist/src/functions/wirings/http/pikku-command-http-map.d.ts +1 -1
  154. package/dist/src/functions/wirings/http/pikku-command-http-routes.d.ts +1 -1
  155. package/dist/src/functions/wirings/http/pikku-command-http-types.d.ts +1 -1
  156. package/dist/src/functions/wirings/http/pikku-command-openapi.d.ts +1 -1
  157. package/dist/src/functions/wirings/http/pikku-http-routes.d.ts +1 -1
  158. package/dist/src/functions/wirings/mcp/pikku-command-mcp-json.d.ts +1 -1
  159. package/dist/src/functions/wirings/mcp/pikku-command-mcp-types.d.ts +1 -1
  160. package/dist/src/functions/wirings/mcp/pikku-command-mcp.d.ts +1 -1
  161. package/dist/src/functions/wirings/middleware/pikku-command-middleware.d.ts +1 -1
  162. package/dist/src/functions/wirings/package/pikku-command-package-types.d.ts +2 -2
  163. package/dist/src/functions/wirings/package/pikku-command-package.d.ts +1 -1
  164. package/dist/src/functions/wirings/permissions/pikku-command-permissions.d.ts +1 -1
  165. package/dist/src/functions/wirings/queue/pikku-command-queue-map.d.ts +1 -1
  166. package/dist/src/functions/wirings/queue/pikku-command-queue-service.d.ts +1 -1
  167. package/dist/src/functions/wirings/queue/pikku-command-queue-types.d.ts +1 -1
  168. package/dist/src/functions/wirings/queue/pikku-command-queue.d.ts +1 -1
  169. package/dist/src/functions/wirings/queue/pikku-queue-map.d.ts +1 -1
  170. package/dist/src/functions/wirings/queue/pikku-queue.d.ts +1 -1
  171. package/dist/src/functions/wirings/realtime/pikku-command-events-scaffold.d.ts +1 -1
  172. package/dist/src/functions/wirings/realtime/pikku-command-realtime.d.ts +1 -1
  173. package/dist/src/functions/wirings/rpc/pikku-command-public-rpc.d.ts +1 -1
  174. package/dist/src/functions/wirings/rpc/pikku-command-react-query.d.ts +1 -1
  175. package/dist/src/functions/wirings/rpc/pikku-command-remote-rpc.d.ts +1 -1
  176. package/dist/src/functions/wirings/rpc/pikku-command-rpc-client.d.ts +1 -1
  177. package/dist/src/functions/wirings/rpc/pikku-command-rpc-map.d.ts +2 -2
  178. package/dist/src/functions/wirings/rpc/pikku-command-rpc.d.ts +1 -1
  179. package/dist/src/functions/wirings/scheduler/pikku-command-scheduler-types.d.ts +1 -1
  180. package/dist/src/functions/wirings/scheduler/pikku-command-scheduler.d.ts +1 -1
  181. package/dist/src/functions/wirings/secrets/pikku-command-secrets.d.ts +1 -1
  182. package/dist/src/functions/wirings/triggers/pikku-command-trigger-types.d.ts +3 -3
  183. package/dist/src/functions/wirings/triggers/pikku-command-trigger.d.ts +1 -1
  184. package/dist/src/functions/wirings/variables/pikku-command-variables.d.ts +1 -1
  185. package/dist/src/functions/wirings/workflow/pikku-command-workflow-routes.d.ts +1 -1
  186. package/dist/src/functions/wirings/workflow/pikku-command-workflow.d.ts +1 -1
  187. package/dist/src/scaffold/rpc-remote.gen.d.ts +3 -3
  188. package/dist/src/scaffold/rpc-remote.gen.js +1 -1
  189. package/dist/src/utils/cli-session.d.ts +40 -0
  190. package/dist/src/utils/cli-session.js +73 -0
  191. package/dist/src/utils/device-auth.d.ts +28 -0
  192. package/dist/src/utils/device-auth.js +111 -0
  193. package/dist/tsconfig.tsbuildinfo +1 -1
  194. package/package.json +3 -3
  195. package/skills/pikku-machine-auth/SKILL.md +177 -0
@@ -0,0 +1,177 @@
1
+ ---
2
+ name: pikku-machine-auth
3
+ description: 'Use when authenticating a CLI/agent/service against a Pikku server, adding machine-to-machine (M2M) auth, issuing scoped API keys for sandboxes/agents/workers, or wiring better-auth sessions into Pikku middleware. Covers `pikku login` (device-authorization), the better-auth API Key plugin, machine identities, and `betterAuthSession` with the api-key branch.
4
+ TRIGGER when: user asks about CLI login, `pikku login`, machine agents, service-to-service auth, API keys, client credentials, sandbox/worker tokens, or resolving a better-auth session in a Pikku function.
5
+ DO NOT TRIGGER when: user asks about end-user HTTP session/cookie auth only (use pikku-http + the app betterAuth config) or about WebSocket channel mechanics (use pikku-websocket).'
6
+ ---
7
+
8
+ # Pikku Machine Auth
9
+
10
+ Unified authentication for humans **and** machines against a Pikku + better-auth
11
+ server. Two paths, two headers, one resolver:
12
+
13
+ | Caller | Credential | Header | Obtained by |
14
+ |---|---|---|---|
15
+ | **Human** (CLI, dev) | better-auth session token | `Authorization: Bearer <token>` | `pikku login` (device flow) → `~/.pikku/session.json` |
16
+ | **Machine** (agent, sandbox, worker) | scoped API key | `x-api-key: <key>` | `createApiKey` (server-side, at provision/spawn) |
17
+
18
+ Both resolve to a Pikku `UserSession` through one middleware:
19
+ `betterAuthSession({ mapSession, apiKey: { mapKey } })`.
20
+
21
+ > The literal OAuth `client_credentials` grant is **not** implemented in
22
+ > better-auth's oidc-provider. The API Key plugin gives the same capability (a
23
+ > baked secret a service presents for scoped access), not the wire protocol.
24
+
25
+ ## Agent Operating Procedure
26
+
27
+ 1. Discover before editing — inspect the app's `betterAuth({ plugins: [...] })`
28
+ config and existing middleware wiring before adding anything.
29
+ 2. Server changes go in the auth factory + a middleware wiring file; never put
30
+ auth checks in a function body (use `permissions`).
31
+ 3. The API Key plugin contributes an `apikey` table — add the matching SQL
32
+ migration and regenerate DB types before relying on it.
33
+ 4. Validate with the narrowest command, then `pikku all`.
34
+
35
+ ## Human path — `pikku login`
36
+
37
+ ```bash
38
+ pikku login --url https://app.example.com # device-authorization flow
39
+ pikku whoami # show current session + expiry
40
+ pikku logout # remove stored session
41
+ ```
42
+
43
+ `pikku login` runs the RFC 8628 device flow: it requests a code, opens the
44
+ browser to the verification URL, polls until you approve, then stores the
45
+ session token (keyed by base URL) at `~/.pikku/session.json` with its expiry.
46
+
47
+ **Server requirement** — enable the `deviceAuthorization` and `bearer` plugins:
48
+
49
+ ```typescript
50
+ import { deviceAuthorization, bearer } from 'better-auth/plugins'
51
+
52
+ betterAuth({
53
+ // ...
54
+ plugins: [
55
+ deviceAuthorization({ expiresIn: '5min', interval: '5s', schema: {} }),
56
+ bearer(), // lets `Authorization: Bearer <session-token>` resolve a session
57
+ ],
58
+ })
59
+ ```
60
+
61
+ The browser approval is two steps the user's browser does automatically:
62
+ `GET /auth/device?user_code=XXXX` (claims the code while signed in) then
63
+ `POST /auth/device/approve`. The CLI only requests the code and polls
64
+ `POST /auth/device/token`.
65
+
66
+ ## Machine path — API keys
67
+
68
+ Install the plugin (separate official package) and enable it:
69
+
70
+ ```bash
71
+ yarn add @better-auth/api-key # peer: better-auth ^1.6.19
72
+ ```
73
+
74
+ ```typescript
75
+ import { apiKey } from '@better-auth/api-key'
76
+
77
+ betterAuth({
78
+ plugins: [
79
+ apiKey({
80
+ enableMetadata: true, // REQUIRED to store scope on the key
81
+ enableSessionForAPIKeys: true, // lets a key resolve via getSession too
82
+ }),
83
+ ],
84
+ })
85
+ ```
86
+
87
+ ### Identity model
88
+
89
+ A **machine is an API key, not a throwaway user.** Keys are owned by a small set
90
+ of stable **service-user** identities you provision once (e.g. `orchestrator`,
91
+ `machine-agent`, `builder`, `sandbox-runtime`). Per-machine scope rides on the
92
+ key's `metadata`/`permissions`. A key requires a real owning user row — minting
93
+ one for a non-existent `userId` is created but will not resolve.
94
+
95
+ ### Mint a scoped key (server-side, at spawn/provision)
96
+
97
+ ```typescript
98
+ // `auth` is the better-auth instance (injected service)
99
+ const { key } = await auth.api.createApiKey({
100
+ body: {
101
+ userId: sandboxRuntimeUserId, // a stable service user
102
+ name: `sandbox:${sandboxId}`,
103
+ expiresIn: 60 * 60, // seconds
104
+ metadata: { sandboxId }, // keep only STABLE ids here
105
+ permissions: { sandbox: ['read', 'write'] },
106
+ },
107
+ })
108
+ // inject `key` into the machine's env; it sends it as `x-api-key`.
109
+ ```
110
+
111
+ Rotate by minting a new key and expiring/deleting the old (`deleteApiKey`);
112
+ multiple active keys per identity allow zero-downtime rotation.
113
+
114
+ ### Resolve scope — `verifyApiKey`, not `getSession`
115
+
116
+ `getSession(x-api-key)` returns only a bare mock session **without** the
117
+ metadata. Scope must come from `verifyApiKey`, which returns
118
+ `{ valid, key: { userId, metadata, permissions } }`. The
119
+ `betterAuthSession` api-key branch does this for you:
120
+
121
+ ```typescript
122
+ import { betterAuthSession } from '@pikku/better-auth'
123
+ import { addHTTPMiddleware } from '@pikku/core/http'
124
+
125
+ addHTTPMiddleware([
126
+ betterAuthSession({
127
+ // human path: getSession result -> app session
128
+ mapSession: ({ user }) => ({ userId: user.id }),
129
+ // machine path: verified key -> app session. `services` lets you resolve
130
+ // CURRENT scope (e.g. look up the owning row) instead of trusting only the
131
+ // baked metadata.
132
+ apiKey: {
133
+ header: 'x-api-key', // default
134
+ mapKey: async (key, services) => {
135
+ const sandboxId = key.metadata?.sandboxId
136
+ if (!sandboxId) return null // reject
137
+ const row = await services.kysely
138
+ .selectFrom('sandboxInstance')
139
+ .innerJoin('sandbox', 'sandbox.id', 'sandboxInstance.sandboxId')
140
+ .select(['sandbox.orgId', 'sandbox.projectId'])
141
+ .where('sandboxInstance.sandboxId', '=', sandboxId)
142
+ .where('sandboxInstance.stoppedAt', 'is', null)
143
+ .executeTakeFirst()
144
+ if (!row) return null
145
+ return { userId: sandboxId, orgId: row.orgId, role: 'sandbox' }
146
+ },
147
+ },
148
+ }),
149
+ ])
150
+ ```
151
+
152
+ When the api-key header is present it is authoritative — the middleware never
153
+ falls through to `getSession` (a bare mock session would shadow the scoped one).
154
+ When it is absent, the human `getSession` path runs as normal.
155
+
156
+ ### WebSocket channels authenticate on the upgrade handshake
157
+
158
+ Generated channel CLI clients attach the credential as a connection header
159
+ (`x-api-key` for `PIKKU_API_KEY`, else `Authorization: Bearer` from
160
+ `~/.pikku/session.json`). The `@pikku/ws` server copies the upgrade-request
161
+ headers into the channel's `http.request` and runs the inherited HTTP `*`
162
+ middleware during `runUpgradeMiddleware`, so `betterAuthSession` resolves the
163
+ session before the channel opens. For this to work the app must register
164
+ `betterAuthSession` via `addHTTPMiddleware([...])` (the `*` group) — not only on
165
+ specific routes — so it is inherited into the channel upgrade. Browser clients
166
+ cannot set WebSocket headers, so header-auth only covers the Node CLI path; a
167
+ browser channel needs a query-param/subprotocol vector instead.
168
+
169
+ ## Gotchas
170
+
171
+ - `apiKey()` rejects `metadata` unless `enableMetadata: true`.
172
+ - `deviceAuthorization()` requires a `schema` option (pass `schema: {}`).
173
+ - Keep the two paths on **different headers** — `x-api-key` (machine) vs
174
+ `Authorization: Bearer` (human). One header for both reintroduces ambiguity.
175
+ - The `apikey` table is plugin-contributed — add the SQL migration + regen types.
176
+ - `~/.pikku/session.json` is written `0600` and stores the token + expiry; the
177
+ CLI uses the expiry to detect when a re-login is needed.