opencode-plugboard 0.16.0 → 0.17.0

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.
package/src/rpc.ts ADDED
@@ -0,0 +1,65 @@
1
+ /**
2
+ * The usage RPC, `opencode-plugboard.usage` (package entry `./rpc`): the report `GET /v1/usage` serves,
3
+ * for OpenCode's own clients. A TUI plugin reads it with `client.rpc(UsageRpc).get({ ... })` over the
4
+ * server's password-authenticated API, so the method sees every app.
5
+ *
6
+ * The server plugin registers the handler in every accepted generation (`index.ts`). Registration is
7
+ * synchronous and touches no storage; the handler reads the same store as the route, from request
8
+ * time only. Input and output are JSON Schema (the host's `Rpc.define`): the input is the query
9
+ * (`from`, `to`, `app`, all optional), the output is the `UsageReport` as documented in `src/usage.ts`.
10
+ * A bad query is the declared error `invalid_query` with `{ param }`, naming the parameter.
11
+ */
12
+
13
+ import { Rpc } from "@opencode/plugin/rpc"
14
+ import type { RpcHandlers } from "@opencode/plugin/effect/rpc"
15
+ import { Effect } from "effect"
16
+ import { isPlugError, type PlugError } from "./contract.js"
17
+ import type { UsageQuery, UsageReport } from "./usage.js"
18
+
19
+ export const UsageRpc = Rpc.define({
20
+ id: "opencode-plugboard.usage",
21
+ methods: {
22
+ get: {
23
+ input: {
24
+ type: "object",
25
+ properties: {
26
+ from: { type: "string", description: "First UTC day, YYYY-MM-DD. Default: 29 days before `to`." },
27
+ to: { type: "string", description: "Last UTC day, YYYY-MM-DD. Default: today." },
28
+ app: { type: "string", description: "One app name. Default: every app." },
29
+ },
30
+ additionalProperties: false,
31
+ },
32
+ output: {
33
+ type: "object",
34
+ description: "A UsageReport (see src/usage.ts).",
35
+ additionalProperties: true,
36
+ },
37
+ errors: {
38
+ invalid_query: {
39
+ type: "object",
40
+ properties: { param: { type: "string", description: "The query parameter that is wrong: from, to or app." } },
41
+ required: ["param"],
42
+ additionalProperties: false,
43
+ },
44
+ },
45
+ },
46
+ },
47
+ events: {},
48
+ })
49
+
50
+ /** The query fields of an RPC input. The input was validated against JSON Schema by the host; anything else is ignored. */
51
+ function queryOf(input: unknown): UsageQuery {
52
+ const record: Record<string, unknown> = typeof input === "object" && input !== null ? (input as Record<string, unknown>) : {}
53
+ const text = (value: unknown) => (typeof value === "string" ? value : undefined)
54
+ return { from: text(record.from), to: text(record.to), app: text(record.app) }
55
+ }
56
+
57
+ /** The handlers for `UsageRpc`, reading the given usage query (the generation's recorder). */
58
+ export function usageHandlers(usage: { query(input: UsageQuery): Promise<UsageReport | PlugError> }): RpcHandlers<typeof UsageRpc> {
59
+ return {
60
+ get: (input, context) =>
61
+ Effect.promise(() => usage.query(queryOf(input))).pipe(
62
+ Effect.flatMap((result) => (isPlugError(result) ? Effect.fail(context.error("invalid_query", result.message, { param: result.param ?? "query" })) : Effect.succeed(result))),
63
+ ),
64
+ }
65
+ }
package/src/runtime.ts CHANGED
@@ -25,8 +25,10 @@
25
25
  * - the metrics registry (src/metrics.ts) and the runtime start time, so counters and uptime
26
26
  * outlive any one generation. `processMetrics` / `processStatus` read them for `/metrics`,
27
27
  * `/v1/status` and the discovery pages;
28
- * - the request limiter's queues and rate windows (src/limits.ts), so a cap holds across
29
- * generations. `processLimits` reads them.
28
+ * - the request limiter's queues and rate windows (src/limits.ts), so a cap holds across
29
+ * generations. `processLimits` reads them;
30
+ * - the usage store's live days (src/usage.ts), so the in-memory aggregates survive a generation.
31
+ * `processUsage` reads them.
30
32
  *
31
33
  * Registration is explicit: `registerGeneration` returns whether the generation was accepted. A
32
34
  * rejected one (different `listen`, options that differ from the live ones, foreign runtime) never
@@ -60,6 +62,7 @@ import { ensureAppDir } from "./session/history.js"
60
62
  import type { HistoryPort } from "./session/host.js"
61
63
  import { recordSession, sweepHistory, type HistoryLimits } from "./session/prune.js"
62
64
  import { createSessionStore, type SessionStore } from "./session/state.js"
65
+ import { createUsageState, type UsageState } from "./usage.js"
63
66
 
64
67
  export type Handler = (request: IncomingMessage, response: ServerResponse) => void
65
68
 
@@ -112,8 +115,8 @@ const BACKEND_POLL_MS = 25
112
115
  const BIND_RETRY_MS = 15_000
113
116
  const BIND_REMINDER_MS = 5 * 60_000
114
117
 
115
- /** 10: the response cache store (`src/cache.ts`) lives here too. 9: the request limiter state (`src/limits.ts`) lives here, so the concurrency caps and rate windows survive a generation. 8: `Metrics.render` is async and the registry is @prometheus-io/client's. 7: the options fingerprint comes from safe-stable-stringify, so it must not be compared with an older one. 6: the listener retries a failed bind (new fields on `Listener`). 5: adds the metrics registry and the start time. 4: the history sweep state and the session store's register hook. 3: confirmed directories and rejected generations. */
116
- const VERSION = 10
118
+ /** 12: the live key set (`keys`, `Runtime.keys`) that a stricter newer generation replaces at once (#90). 11: the usage store's live days (`src/usage.ts`) live here too. 10: the response cache store (`src/cache.ts`) lives here too. 9: the request limiter state (`src/limits.ts`) lives here, so the concurrency caps and rate windows survive a generation. 8: `Metrics.render` is async and the registry is @prometheus-io/client's. 7: the options fingerprint comes from safe-stable-stringify, so it must not be compared with an older one. 6: the listener retries a failed bind (new fields on `Listener`). 5: adds the metrics registry and the start time. 4: the history sweep state and the session store's register hook. 3: confirmed directories and rejected generations. */
119
+ const VERSION = 12
117
120
  const KEY = Symbol.for("opencode-plugboard.runtime")
118
121
 
119
122
  interface Listener {
@@ -167,8 +170,16 @@ interface Runtime {
167
170
  readonly limits: LimitState
168
171
  /** When this runtime was created, epoch ms. */
169
172
  readonly startedAt: number
173
+ /**
174
+ * The app keys authentication checks (app name -> key), once a generation has registered. Replaced
175
+ * when a newer generation only revokes or rotates keys (`registerGeneration`, #90). Absent means
176
+ * the handler's own options are used.
177
+ */
178
+ keys?: Readonly<Record<string, string>>
170
179
  /** The response cache store (src/cache.ts) and the fingerprint of the config and `apps` policy it was built for. */
171
180
  cache?: { readonly fingerprint: string; readonly store: CacheStore }
181
+ /** The usage store's live days and retention clock (src/usage.ts). */
182
+ readonly usage: UsageState
172
183
  listener?: Listener
173
184
  /** Present while a fiber is subscribed to the server event bus. */
174
185
  pump?: Pump
@@ -181,6 +192,7 @@ const detachedSessions = createSessionStore()
181
192
  const detachedMetrics = createMetrics()
182
193
  const detachedStartedAt = Date.now()
183
194
  const detachedLimits = createLimitState()
195
+ const detachedUsage = createUsageState()
184
196
  let foreignVersionLogged = false
185
197
 
186
198
  /** The process runtime, created on first use; undefined when a different version already owns the process. */
@@ -197,6 +209,7 @@ function runtime(): Runtime | undefined {
197
209
  sweep: { nextAt: 0, running: false },
198
210
  metrics: createMetrics(),
199
211
  limits: createLimitState(),
212
+ usage: createUsageState(),
200
213
  startedAt: Date.now(),
201
214
  }
202
215
  slot[KEY] = created
@@ -275,6 +288,18 @@ export const processMetrics = (): Metrics => runtime()?.metrics ?? detachedMetri
275
288
  /** The request limiter's state shared by every generation in the process (detached under a foreign-version runtime). */
276
289
  export const processLimits = (): LimitState => runtime()?.limits ?? detachedLimits
277
290
 
291
+ /** The usage store's live days shared by every generation in the process (detached under a foreign-version runtime). */
292
+ export const processUsage = (): UsageState => runtime()?.usage ?? detachedUsage
293
+
294
+ /**
295
+ * The options authentication reads: `keys` is the live key set, not the serving generation's own
296
+ * (a generation that is still live after a key revocation keeps its old keys otherwise, #90).
297
+ */
298
+ export function liveOptions(options: PlugboardOptions): PlugboardOptions {
299
+ const keys = runtime()?.keys
300
+ return keys === undefined || keys === options.keys ? options : { ...options, keys }
301
+ }
302
+
278
303
  /**
279
304
  * Wraps `caller` with the response cache when option `cache` is set (a no-op without it). The store
280
305
  * lives on the runtime, so it survives plugin generations. It is replaced when the cache option or
@@ -423,6 +448,12 @@ function closeListener(rt: Runtime, listener: Listener): void {
423
448
  * generation's options are adopted. A different `listen` needs a server restart and is rejected.
424
449
  * A rejected generation is not in the generation list: it never serves, supplies no catalog, and
425
450
  * the caller must not install hooks or seeds for it.
451
+ *
452
+ * The key set (`keys`) has one more rule (#90). While generations are live, a newer generation whose
453
+ * keys only revoke or rotate those of apps the listener already has (no app added, at least one
454
+ * left) replaces `Runtime.keys` at once, so a Location still on an older generation stops accepting
455
+ * a removed key. Any other key change is rejected like any other option difference. The listener's
456
+ * `fingerprint` covers the options without `keys`.
426
457
  */
427
458
  export function registerGeneration(input: GenerationInput): Registration {
428
459
  const rt = runtime()
@@ -444,7 +475,8 @@ export function registerGeneration(input: GenerationInput): Registration {
444
475
  },
445
476
  }
446
477
  }
447
- const fingerprint = canonicalJson(input.options)
478
+ const fingerprint = canonicalJson({ ...(input.options as Record<string, unknown>), keys: undefined })
479
+ const keys = (input.options as { readonly keys?: Readonly<Record<string, string>> } | null)?.keys ?? {}
448
480
  const listener = (rt.listener ??= startListener(rt, listen, fingerprint))
449
481
  if (listener.listen.host !== listen.host || listener.listen.port !== listen.port) {
450
482
  if (!listener.mismatchLogged) {
@@ -457,16 +489,28 @@ export function registerGeneration(input: GenerationInput): Registration {
457
489
  }
458
490
  return reject()
459
491
  }
460
- if (listener.fingerprint !== fingerprint) {
461
- if (rt.generations.length > 0) {
462
- if (!listener.mismatchLogged) {
463
- listener.mismatchLogged = true
464
- logError("listener.options_mismatch", { action: "this Location is rejected; global config is authoritative" })
465
- }
466
- return reject()
492
+ const rejectOptions = (): Registration => {
493
+ if (!listener.mismatchLogged) {
494
+ listener.mismatchLogged = true
495
+ logError("listener.options_mismatch", { action: "this Location is rejected; global config is authoritative" })
467
496
  }
497
+ return reject()
498
+ }
499
+ if (listener.fingerprint !== fingerprint) {
500
+ if (rt.generations.length > 0) return rejectOptions()
468
501
  listener.fingerprint = fingerprint
469
502
  listener.mismatchLogged = false
503
+ rt.keys = keys
504
+ } else if (canonicalJson(keys) !== canonicalJson(rt.keys ?? {})) {
505
+ const live = rt.keys ?? {}
506
+ if (rt.generations.length === 0) rt.keys = keys
507
+ else if (revokesOrRotatesOnly(live, keys)) {
508
+ log("listener.keys_updated", {
509
+ removed: Object.keys(live).filter((app) => !Object.hasOwn(keys, app)).length,
510
+ rotated: Object.keys(keys).filter((app) => Object.hasOwn(live, app) && live[app] !== keys[app]).length,
511
+ })
512
+ rt.keys = keys
513
+ } else return rejectOptions()
470
514
  }
471
515
  if (listener.graceTimer) clearTimeout(listener.graceTimer)
472
516
  listener.graceTimer = undefined
@@ -510,6 +554,12 @@ export function registerGeneration(input: GenerationInput): Registration {
510
554
  }
511
555
  }
512
556
 
557
+ /** #90: `next` names no app that `live` lacks, and names at least one app: it only removes or changes keys. */
558
+ function revokesOrRotatesOnly(live: Readonly<Record<string, string>>, next: Readonly<Record<string, string>>): boolean {
559
+ const apps = Object.keys(next)
560
+ return apps.length > 0 && apps.every((app) => Object.hasOwn(live, app))
561
+ }
562
+
513
563
  /**
514
564
  * Start the process-wide event pump if it is not running. The bus is server-wide, so the store
515
565
  * drops everything that is not a registered request session before it is buffered. The pump is
@@ -129,6 +129,30 @@ export function createHistoryPort(ctx: HostContext): HistoryPort {
129
129
  }
130
130
  }
131
131
 
132
+ /**
133
+ * `ctx.storage` as a Promise port for string values (the usage store keeps one JSON string per app
134
+ * and day). Same host table and namespace as `HistoryPort`; the keys never overlap (`usage/` vs `history/`).
135
+ */
136
+ export interface StoragePort {
137
+ get(key: string): Promise<string | undefined>
138
+ put(key: string, value: string): Promise<void>
139
+ drop(key: string): Promise<void>
140
+ scan(input: { readonly prefix: string; readonly after?: string; readonly limit: number }): Promise<{ readonly keys: ReadonlyArray<string>; readonly next?: string }>
141
+ }
142
+
143
+ export function createStoragePort(ctx: HostContext): StoragePort {
144
+ return {
145
+ get: (key) => Effect.runPromise(ctx.storage.get(key)).then((value) => (typeof value === "string" ? value : undefined)),
146
+ put: (key, value) => Effect.runPromise(ctx.storage.set(key, value)),
147
+ drop: (key) => Effect.runPromise(ctx.storage.remove(key)),
148
+ scan: (input) =>
149
+ Effect.runPromise(ctx.storage.scan(input)).then((result) => ({
150
+ keys: result.entries.map((entry) => entry.key),
151
+ ...(result.next === undefined ? {} : { next: result.next }),
152
+ })),
153
+ }
154
+ }
155
+
132
156
  const record = (value: unknown): Record<string, unknown> | undefined =>
133
157
  typeof value === "object" && value !== null ? (value as Record<string, unknown>) : undefined
134
158