@pikku/cli 0.12.59 → 0.12.62

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 (72) 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 +1 -1
  3. package/dist/.pikku/channel/pikku-channel-types.gen.js +1 -1
  4. package/dist/.pikku/cli/pikku-cli-channel.js +6 -1
  5. package/dist/.pikku/cli/pikku-cli-client.gen.d.ts +1 -1
  6. package/dist/.pikku/cli/pikku-cli-client.gen.js +1 -1
  7. package/dist/.pikku/cli/pikku-cli-contracts-meta.gen.d.ts +1 -1
  8. package/dist/.pikku/cli/pikku-cli-contracts-meta.gen.js +1 -1
  9. package/dist/.pikku/cli/pikku-cli-types.gen.d.ts +1 -1
  10. package/dist/.pikku/cli/pikku-cli-types.gen.js +1 -1
  11. package/dist/.pikku/cli/pikku-cli-wirings-meta.gen.js +1 -1
  12. package/dist/.pikku/cli/pikku-cli-wirings-meta.gen.json +11 -0
  13. package/dist/.pikku/cli/pikku-cli-wirings.gen.d.ts +1 -1
  14. package/dist/.pikku/cli/pikku-cli-wirings.gen.js +1 -1
  15. package/dist/.pikku/cli/pikku-cli.gen.d.ts +1 -1
  16. package/dist/.pikku/cli/pikku-cli.gen.js +1 -1
  17. package/dist/.pikku/console/pikku-node-types.gen.d.ts +1 -1
  18. package/dist/.pikku/function/pikku-function-types.gen.d.ts +1 -1
  19. package/dist/.pikku/function/pikku-function-types.gen.js +1 -1
  20. package/dist/.pikku/function/pikku-functions-meta.gen.js +1 -1
  21. package/dist/.pikku/function/pikku-functions-meta.gen.json +18 -1
  22. package/dist/.pikku/function/pikku-functions.gen.js +1 -1
  23. package/dist/.pikku/http/pikku-http-types.gen.d.ts +1 -1
  24. package/dist/.pikku/http/pikku-http-types.gen.js +1 -1
  25. package/dist/.pikku/http/pikku-http-wirings-meta.gen.js +1 -1
  26. package/dist/.pikku/http/pikku-http-wirings.gen.d.ts +1 -1
  27. package/dist/.pikku/http/pikku-http-wirings.gen.js +1 -1
  28. package/dist/.pikku/mcp/pikku-mcp-types.gen.d.ts +1 -1
  29. package/dist/.pikku/mcp/pikku-mcp-types.gen.js +1 -1
  30. package/dist/.pikku/pikku-bootstrap.gen.d.ts +1 -1
  31. package/dist/.pikku/pikku-bootstrap.gen.js +1 -1
  32. package/dist/.pikku/pikku-meta-service.gen.d.ts +1 -1
  33. package/dist/.pikku/pikku-meta-service.gen.js +1 -1
  34. package/dist/.pikku/pikku-services.gen.d.ts +1 -1
  35. package/dist/.pikku/pikku-types.gen.d.ts +1 -1
  36. package/dist/.pikku/pikku-types.gen.js +1 -1
  37. package/dist/.pikku/queue/pikku-queue-types.gen.d.ts +1 -1
  38. package/dist/.pikku/queue/pikku-queue-types.gen.js +1 -1
  39. package/dist/.pikku/queue/pikku-queue-workers-wirings-meta.gen.js +1 -1
  40. package/dist/.pikku/queue/pikku-queue-workers-wirings.gen.d.ts +1 -1
  41. package/dist/.pikku/queue/pikku-queue-workers-wirings.gen.js +1 -1
  42. package/dist/.pikku/rpc/pikku-rpc-wirings-meta.internal.gen.js +1 -1
  43. package/dist/.pikku/rpc/pikku-rpc-wirings-meta.internal.gen.json +1 -0
  44. package/dist/.pikku/scheduler/pikku-scheduler-types.gen.d.ts +1 -1
  45. package/dist/.pikku/scheduler/pikku-scheduler-types.gen.js +1 -1
  46. package/dist/.pikku/schemas/register.gen.js +1 -1
  47. package/dist/.pikku/secrets/pikku-secret-types.gen.d.ts +1 -1
  48. package/dist/.pikku/secrets/pikku-secret-types.gen.js +1 -1
  49. package/dist/.pikku/secrets/pikku-secrets.gen.d.ts +1 -1
  50. package/dist/.pikku/secrets/pikku-secrets.gen.js +1 -1
  51. package/dist/.pikku/trigger/pikku-trigger-types.gen.d.ts +1 -1
  52. package/dist/.pikku/trigger/pikku-trigger-types.gen.js +1 -1
  53. package/dist/.pikku/variables/pikku-variable-types.gen.d.ts +1 -1
  54. package/dist/.pikku/variables/pikku-variable-types.gen.js +1 -1
  55. package/dist/.pikku/variables/pikku-variables.gen.d.ts +1 -1
  56. package/dist/.pikku/variables/pikku-variables.gen.js +1 -1
  57. package/dist/.pikku/workflow/pikku-workflow-types.gen.d.ts +1 -1
  58. package/dist/.pikku/workflow/pikku-workflow-types.gen.js +1 -1
  59. package/dist/.pikku/workflow/pikku-workflow-wirings-meta.gen.js +1 -1
  60. package/dist/.pikku/workflow/pikku-workflow-wirings.gen.js +1 -1
  61. package/dist/bin/pikku-bin.mjs +2 -2
  62. package/dist/src/cli.wiring.js +11 -0
  63. package/dist/src/fabric/functions/validate.function.js +107 -0
  64. package/dist/src/functions/commands/audit.d.ts +5 -0
  65. package/dist/src/functions/commands/audit.js +165 -0
  66. package/dist/src/functions/workflows/all.workflow.js +4 -0
  67. package/dist/src/services.d.ts +1 -0
  68. package/dist/src/utils/assert-single-core-version.d.ts +14 -0
  69. package/dist/src/utils/assert-single-core-version.js +80 -0
  70. package/dist/tsconfig.tsbuildinfo +1 -1
  71. package/package.json +17 -17
  72. package/skills/pikku-audit/SKILL.md +168 -0
@@ -0,0 +1,168 @@
1
+ ---
2
+ name: pikku-audit
3
+ description: 'Use when adding audit / activity-history / change-tracking to a Pikku app, or when a function needs to record who changed what. Covers the built-in AuditService sink, the per-invocation auditLog buffer (createInvocationAudit / pikkuWireServices), the `audit: true` function flag, explicit `auditLog.write()` domain events, automatic query-level capture via createAuditedKysely, and the durable KyselyAuditService sink.
4
+ TRIGGER when: user asks for an audit log, change history, activity feed, "who did this", or a custom audit/history table; code uses auditLog, createInvocationAudit, createAuditedKysely, or AuditService.
5
+ DO NOT TRIGGER when: user wants app logging/telemetry (use the logger) or DB migrations in general (use pikku-kysely).'
6
+ installGroups: [core]
7
+ ---
8
+
9
+ # Pikku Audit
10
+
11
+ ## Agent Operating Procedure
12
+
13
+ Use this skill as an execution checklist, not reference material.
14
+
15
+ 1. Discover before editing. Check how services are wired (`services.ts`) and whether an `audit` table migration exists before adding audit calls.
16
+ 2. NEVER hand-roll a custom `audit_log` / history table with direct `insertInto('audit_log')` calls. The framework owns audit. A bespoke table drifts from the runtime (missing actor/trace/wire context, hand-written CHECK constraints that reject valid events, no prod sink). Use the built-in path below.
17
+ 3. Make the smallest source change: mark the function `audit: true`, inject `auditLog`, call `auditLog.write(...)`. Do not invent a new service.
18
+ 4. Validate with `pikku all` (regenerates the service flags) then run the app / e2e.
19
+
20
+ ## Mental model — two layers
21
+
22
+ - **`audit` (singleton `AuditService`)** — the durable **sink**. Write-only: `audit(event)` + optional `write(batch)`. Defaults to `NoopAuditService` (discards). Swap in a real sink to persist (see Sinks).
23
+ - **`auditLog` (wire service `AuditLog`)** — a per-invocation **buffer** built from the sink via `createInvocationAudit(audit, wire)`. `auditLog.write(input)` enriches each event with `functionId`, `wireType`, `traceId`, `occurredAt`, and `actor` (from the wire session) automatically, then flushes to the sink when the invocation ends.
24
+
25
+ An event only persists when the function opts in with **`audit: true`** — otherwise `auditLog` is a no-op that warns.
26
+
27
+ ## Wiring (services.ts)
28
+
29
+ ```typescript
30
+ import { NoopAuditService, createInvocationAudit } from '@pikku/core/services'
31
+
32
+ export const createSingletonServices = pikkuServices(async (config, existing) => {
33
+ // Prod platforms may inject a queue-backed sink as existing.audit.
34
+ const audit = existing?.audit ?? new NoopAuditService()
35
+ return { ...existing, config, /* ... */ audit }
36
+ })
37
+
38
+ // auditLog is created per invocation from the sink. Returned unconditionally so
39
+ // a write from a function that forgot `audit: true` warns instead of vanishing.
40
+ export const createWireServices = pikkuWireServices(async (services, wire) => {
41
+ if (!services.audit) return {}
42
+ return { auditLog: createInvocationAudit(services.audit, wire) }
43
+ })
44
+ ```
45
+
46
+ `audit` and `auditLog` are already declared on `CoreSingletonServices` / `CoreServices`, so no type change is needed to inject them.
47
+
48
+ ## Recording events — explicit domain events (default)
49
+
50
+ Mark the function `audit: true` and call `auditLog?.write(...)`. Domain history goes in `metadata`; the actor is derived from the session, so do NOT pass it manually.
51
+
52
+ ```typescript
53
+ export const cancelInvoice = pikkuFunc({
54
+ audit: true, // REQUIRED — else write() is a no-op
55
+ input: CancelInvoiceInput,
56
+ output: CancelInvoiceOutput,
57
+ func: async ({ kysely, auditLog }, { invoiceId }, { session }) => {
58
+ const inv = await kysely.selectFrom('invoice')/* ... */.executeTakeFirstOrThrow()
59
+ await kysely.updateTable('invoice').set({ status: 'cancelled' })/* ... */.execute()
60
+
61
+ await auditLog?.write({
62
+ type: 'invoice.update',
63
+ source: 'explicit',
64
+ metadata: {
65
+ entity: 'invoice',
66
+ entityId: invoiceId,
67
+ action: 'update',
68
+ field: 'status',
69
+ before: inv.status,
70
+ after: 'cancelled',
71
+ },
72
+ })
73
+ return { ok: true }
74
+ },
75
+ })
76
+ ```
77
+
78
+ For a **system/cron** function there is no session, so `actor` is simply absent (nulls out `actor_user_id`). Use `pikkuVoidFunc({ audit: true, func: async ({ auditLog }) => { ... } })` — the void/config form accepts `audit`.
79
+
80
+ Helper functions (in `lib/`) that record audit take `auditLog?: AuditLog` in their services arg and are passed it from a `audit: true` caller — never import a service.
81
+
82
+ Note: events buffer and flush on invocation close. For a write inside a DB transaction, call `auditLog.write()` **after** the transaction commits — the sink is not part of your `trx`, so only record committed state.
83
+
84
+ ## Recording events — automatic query capture (optional)
85
+
86
+ To audit every DB mutation without explicit calls, wrap kysely so each query emits an event. Note this captures table/column changes only — it cannot see semantic events that do no DB write (e.g. "email sent"), so combine with explicit writes when you need those.
87
+
88
+ ```typescript
89
+ import { createAuditedKysely } from '@pikku/kysely'
90
+ export const createWireServices = pikkuWireServices(async (services, wire) => {
91
+ if (!services.audit) return {}
92
+ const auditLog = createInvocationAudit(services.audit, wire)
93
+ return { auditLog, kysely: createAuditedKysely(services.kysely, { audit: auditLog }) }
94
+ })
95
+ ```
96
+
97
+ ## Sinks
98
+
99
+ - **`NoopAuditService`** (`@pikku/core/services`) — default; discards events. Fine when audit isn't needed.
100
+ - **`KyselyAuditService`** (`@pikku/kysely`) — durable: persists events to an `audit` table via kysely. Use as the local/dev sink so events are queryable without a platform queue: `new KyselyAuditService(kysely)`.
101
+ - **Platform-injected sink** — a deploy platform may inject its own queue-backed `audit` (hence the `existing?.audit ??` fallback above). Its rows land in the same `audit` table shape.
102
+
103
+ ### The `audit` table (add this migration if you persist audit)
104
+
105
+ ```sql
106
+ CREATE TABLE IF NOT EXISTS audit (
107
+ audit_id TEXT NOT NULL PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),
108
+ occurred_at TEXT NOT NULL DEFAULT (datetime('now')),
109
+ type TEXT NOT NULL,
110
+ source TEXT NOT NULL DEFAULT 'auto',
111
+ outcome TEXT,
112
+ function_id TEXT,
113
+ wire_type TEXT,
114
+ trace_id TEXT,
115
+ transaction_id TEXT,
116
+ query_id TEXT,
117
+ actor_user_id TEXT,
118
+ actor_org_id TEXT,
119
+ tables TEXT, -- JSON: table names touched (auto capture)
120
+ changed_cols TEXT, -- JSON: changed column names (auto capture)
121
+ event TEXT, -- custom event label
122
+ old TEXT, -- JSON: previous values
123
+ data TEXT -- JSON: metadata / new values / event payload
124
+ );
125
+ ```
126
+
127
+ `auditLog.write({ metadata })` lands in the `data` column. Read history back by filtering it (SQLite `json_extract`, Postgres `->>`):
128
+
129
+ ```typescript
130
+ const rows = await kysely
131
+ .selectFrom('audit')
132
+ .leftJoin('user', 'user.id', 'audit.actorUserId')
133
+ .where(sql<boolean>`json_extract(audit.data, '$.entity') = 'invoice'`)
134
+ .where(sql<boolean>`json_extract(audit.data, '$.entityId') = ${invoiceId}`)
135
+ .orderBy('audit.occurredAt', 'desc')
136
+ .select([
137
+ 'audit.auditId',
138
+ sql<string>`json_extract(audit.data, '$.action')`.as('action'),
139
+ 'audit.occurredAt as at',
140
+ 'user.name as userName',
141
+ ])
142
+ .execute()
143
+ ```
144
+
145
+ ## AuditEvent shape
146
+
147
+ ```typescript
148
+ type AuditEvent = {
149
+ type: string // e.g. 'invoice.update'
150
+ source: 'auto' | 'explicit'
151
+ occurredAt: string // auto-filled by auditLog
152
+ outcome?: string
153
+ functionId?; wireType?; wireId?; traceId?; transactionId?; queryId? // auto
154
+ actor?: { userId?; orgId? } // auto from wire session
155
+ input?: unknown
156
+ metadata?: Record<string, unknown> // your domain payload
157
+ }
158
+ ```
159
+
160
+ `auditLog.write()` takes `Omit<AuditEvent, 'occurredAt'>` — you only supply `type`, `source`, and `metadata` (and `actor` if overriding the session default).
161
+
162
+ ## Do / Don't
163
+
164
+ - DO mark recording functions `audit: true`, inject `auditLog`, and call `auditLog.write({ type, source: 'explicit', metadata })`.
165
+ - DO let the actor come from the session — don't thread `userId` into metadata for the actor.
166
+ - DON'T create a custom `audit_log`/history table or `insertInto('audit_log')` by hand.
167
+ - DON'T annotate the function's I/O from audit; audit is a side channel, not part of `input`/`output`.
168
+ - DON'T write audit inside a DB transaction expecting rollback — record after commit.