feat(updates): per-tenant self-update policy for Werkpilot #149

Open
sharang wants to merge 1 commits from feat/update-policy into main
pull from: feat/update-policy
Owner

What

The registry half of controlled self-update (platform/werkpilot#89). A workspace sets a per-product update policy, and the CLI receives it with every grant and heartbeat:

  • auto: the default, also when nothing is saved;

  • off;

  • a pinned release version.

  • Setting: GET/PUT /v1/tenants/{id}/products/{product}/updates with {"policy": "auto" | "off" | "<semver>"}.

  • Flag: an updates string in the grants of POST /v1/licences and POST /v1/licences/ci, in a heartbeat's renewed grant, and at the top of the heartbeat answer next to status.

Why

Refs platform/werkpilot#89. The user chose a registry setting that lets a tenant turn updates off or pin them, carried to the CLI the same way as the telemetry flag (#148).

Linked milestone: n/a (werkpilot#89)

How

Setting

  • Storage: the updates key of the product's tenant_product_settings document (0025), so no migration. The PUT reads the document, sets the key and writes it back; other keys survive (tested with telemetry and an unrelated key).
  • Products: only products that update themselves take it, today werkpilot; others get 404 on GET and PUT. The Prüfwerk settings PUT replaces its whole document, so the key must never land there.
  • Body: exactly {"policy": …}, else 400 invalid_input. The policy is auto, off, or a version matching the semver.org 2.0.0 regex, at most 64 characters. That means no v prefix, no ranges (^, ~, >=, a - b, ||), no wildcards (x, *), no leading zeros, no surrounding whitespace, and no other words (latest, on, Auto).
  • Answer: {tenant_id, product, policy, updated_by, updated_at}.
  • Who may: user tokens need IT_ADMIN or OWNER in their own workspace (403 FORBIDDEN_ROLE; another workspace gets 404). Services read with tenants.read and write with tenants.write; the portal calls as its service client.
  • Audit: a change is audited as product_settings.changed (setting: updates, old, new), the same shape as the Prüfwerk and telemetry settings. A save that changes nothing writes nothing.
  • The portal toggle is a follow-up; this PR has no UI.

Flag

  • updates is always present and is a plain JSON string, not a JWT claim; the CLI applies it.
  • It is read fresh on every grant and heartbeat from the licence's tenant and product.
  • It is auto for products without the setting (e.g. a firmwerk CI grant) and on the heartbeat's revoked or unknown-tenant answers.
  • If the stored value can't be read, the flag falls back to auto and a warning is logged; the licence call doesn't fail.

Deviations from the issue: none in the contract. Two things the issue left open:

  • Build metadata: a pin may carry build metadata (1.0.0+build.5), because semver 2.0.0 allows it.
  • auto is stored: setting auto stores "auto" rather than removing the key; both read as auto.

Overlap with #147 (feedback) and #148 (telemetry): no identifiers are shared. The text overlaps in places:

  • #148: this PR's new licenceResp and heartbeatResp fields sit at the same spot as #148's telemetry fields, in licences.go, licences_ci.go, the route block in server.go, and the LicenceIssued / LicenceHeartbeat schemas and the path list in openapi.yaml.
  • #147 and #148: the authz_test and openapi_test case lists.

Whichever merges later resolves these by keeping both sides.

Test plan

  • Flag (update_policy_test.go, through eachStore, so CI's Postgres run covers it):
    • the grant, heartbeat and CI grant start as auto;
    • after pinning 0.4.0-rc2, the old licence's heartbeat, a new grant and the werkpilot CI grant carry it, while firmwerk's CI grant stays auto;
    • after off, a heartbeat past half-life renews and both the top level and the renewed grant say off;
    • back to auto.
  • Audit: exactly 3 product_settings.changed events for 3 changes, with the right old and new values. The repeated save writes nothing.
  • Validation: 30 bad bodies give 400, covering empty, null, non-string, extra keys, other words, v prefix, short or long versions, leading zeros, a dangling - or +, whitespace, ranges, wildcards and over-long versions. Valid pins are accepted, including 0.3.1, 0.4.0-rc2, 1.0.0-alpha.1, 1.0.0+build.5 and 10.20.30. Other products give 404 on GET and PUT, and an unknown tenant gives 404. Other document keys are preserved.
  • Roles (update_policy_scope_test.go): USER, CXO and FINANCE get 403 on GET and PUT, another workspace's admin gets 404, and IT_ADMIN and OWNER pass. The policy changes only on an allowed PUT. The audit event names the admin (actor u-1, type user).
  • Route tables: authz_test route groups and openapi_test route cases.
  • Mutation checks: the tests fail with any of these removed: the semver check, the admin gate, the heartbeat read, or the document merge.
  • Local checks: gofmt -l . clean, go vet ./... clean, golangci-lint run (v2.12.2) 0 issues, and go test -race -count=1 ./... green.
  • Postgres: the eachStore Postgres variants skipped locally because Docker isn't reachable here, so they run only in CI. Only the existing GetTenantProductSettings / PutTenantProductSettings / AppendAudit are used.
  • End to end: not tried with a real CLI; the werkpilot side is being built in parallel.

Risk

Blast radius: licence answers gain one field, which old clients ignore. The setting does nothing until an admin changes it, and the CLI applies it.

What could break: each licence issue and heartbeat does one more settings read. A read error falls back to auto and is logged; it does not fail the call.

Rollback plan: set the policy back to auto, or revert the PR and redeploy.

Checklist

  • Docs updated (OpenAPI; no new env vars)
  • Audit events emitted for state changes (product_settings.changed)
  • Secrets via Infisical, never in repo (no secrets)
  • Migration is forward-only + idempotent (or no migration): no migration
  • Tenant scoping enforced on every DB query (or no DB access): tenant from the scoped path
  • OpenAPI spec updated
  • featureFlags.evaluate() used for any toggleable behavior: n/a, the policy is stored tenant data
  • CHANGELOG entry under "Unreleased": n/a, the file is generated by git-cliff on release

Screenshots / recordings

n/a (no UI; the portal toggle is a follow-up)

🤖 Generated with Claude Code

https://claude.ai/code/session_01Q9inRayW7ztyk1uAtwMJPR

## What The registry half of controlled self-update (platform/werkpilot#89). A workspace sets a per-product update policy, and the CLI receives it with every grant and heartbeat: - `auto`: the default, also when nothing is saved; - `off`; - a pinned release version. - **Setting:** `GET/PUT /v1/tenants/{id}/products/{product}/updates` with `{"policy": "auto" | "off" | "<semver>"}`. - **Flag:** an `updates` string in the grants of `POST /v1/licences` and `POST /v1/licences/ci`, in a heartbeat's `renewed` grant, and at the top of the heartbeat answer next to `status`. ## Why Refs platform/werkpilot#89. The user chose a registry setting that lets a tenant turn updates off or pin them, carried to the CLI the same way as the telemetry flag (#148). Linked milestone: **n/a** (werkpilot#89) ## How **Setting** - **Storage:** the `updates` key of the product's `tenant_product_settings` document (0025), so no migration. The PUT reads the document, sets the key and writes it back; other keys survive (tested with `telemetry` and an unrelated key). - **Products:** only products that update themselves take it, today `werkpilot`; others get 404 on GET and PUT. The Prüfwerk settings PUT replaces its whole document, so the key must never land there. - **Body:** exactly `{"policy": …}`, else 400 `invalid_input`. The policy is `auto`, `off`, or a version matching the semver.org 2.0.0 regex, at most 64 characters. That means no `v` prefix, no ranges (`^`, `~`, `>=`, `a - b`, `||`), no wildcards (`x`, `*`), no leading zeros, no surrounding whitespace, and no other words (`latest`, `on`, `Auto`). - **Answer:** `{tenant_id, product, policy, updated_by, updated_at}`. - **Who may:** user tokens need IT_ADMIN or OWNER in their own workspace (403 `FORBIDDEN_ROLE`; another workspace gets 404). Services read with `tenants.read` and write with `tenants.write`; the portal calls as its service client. - **Audit:** a change is audited as `product_settings.changed` (`setting: updates`, old, new), the same shape as the Prüfwerk and telemetry settings. A save that changes nothing writes nothing. - **The portal toggle is a follow-up**; this PR has no UI. **Flag** - `updates` is always present and is a plain JSON string, not a JWT claim; the CLI applies it. - It is read fresh on every grant and heartbeat from the licence's tenant and product. - It is `auto` for products without the setting (e.g. a firmwerk CI grant) and on the heartbeat's revoked or unknown-tenant answers. - If the stored value can't be read, the flag falls back to `auto` and a warning is logged; the licence call doesn't fail. **Deviations from the issue:** none in the contract. Two things the issue left open: - **Build metadata:** a pin may carry build metadata (`1.0.0+build.5`), because semver 2.0.0 allows it. - **`auto` is stored:** setting `auto` stores `"auto"` rather than removing the key; both read as `auto`. **Overlap with #147 (feedback) and #148 (telemetry):** no identifiers are shared. The text overlaps in places: - **#148:** this PR's new `licenceResp` and `heartbeatResp` fields sit at the same spot as #148's `telemetry` fields, in `licences.go`, `licences_ci.go`, the route block in `server.go`, and the `LicenceIssued` / `LicenceHeartbeat` schemas and the path list in `openapi.yaml`. - **#147 and #148:** the `authz_test` and `openapi_test` case lists. Whichever merges later resolves these by keeping both sides. ## Test plan - [x] **Flag** (`update_policy_test.go`, through `eachStore`, so CI's Postgres run covers it): - the grant, heartbeat and CI grant start as `auto`; - after pinning `0.4.0-rc2`, the old licence's heartbeat, a new grant and the werkpilot CI grant carry it, while firmwerk's CI grant stays `auto`; - after `off`, a heartbeat past half-life renews and both the top level and the renewed grant say `off`; - back to `auto`. - [x] **Audit:** exactly 3 `product_settings.changed` events for 3 changes, with the right old and new values. The repeated save writes nothing. - [x] **Validation:** 30 bad bodies give 400, covering empty, null, non-string, extra keys, other words, `v` prefix, short or long versions, leading zeros, a dangling `-` or `+`, whitespace, ranges, wildcards and over-long versions. Valid pins are accepted, including `0.3.1`, `0.4.0-rc2`, `1.0.0-alpha.1`, `1.0.0+build.5` and `10.20.30`. Other products give 404 on GET and PUT, and an unknown tenant gives 404. Other document keys are preserved. - [x] **Roles** (`update_policy_scope_test.go`): USER, CXO and FINANCE get 403 on GET and PUT, another workspace's admin gets 404, and IT_ADMIN and OWNER pass. The policy changes only on an allowed PUT. The audit event names the admin (actor `u-1`, type `user`). - [x] **Route tables:** `authz_test` route groups and `openapi_test` route cases. - [x] **Mutation checks:** the tests fail with any of these removed: the semver check, the admin gate, the heartbeat read, or the document merge. - [x] **Local checks:** `gofmt -l .` clean, `go vet ./...` clean, `golangci-lint run` (v2.12.2) 0 issues, and `go test -race -count=1 ./...` green. - [ ] **Postgres:** the `eachStore` Postgres variants skipped locally because Docker isn't reachable here, so they run only in CI. Only the existing `GetTenantProductSettings` / `PutTenantProductSettings` / `AppendAudit` are used. - [ ] **End to end:** not tried with a real CLI; the werkpilot side is being built in parallel. ## Risk **Blast radius:** licence answers gain one field, which old clients ignore. The setting does nothing until an admin changes it, and the CLI applies it. **What could break:** each licence issue and heartbeat does one more settings read. A read error falls back to `auto` and is logged; it does not fail the call. **Rollback plan:** set the policy back to `auto`, or revert the PR and redeploy. ## Checklist - [x] Docs updated (OpenAPI; no new env vars) - [x] Audit events emitted for state changes (`product_settings.changed`) - [x] Secrets via Infisical, never in repo (no secrets) - [x] Migration is forward-only + idempotent (or no migration): no migration - [x] Tenant scoping enforced on every DB query (or no DB access): tenant from the scoped path - [x] OpenAPI spec updated - [ ] `featureFlags.evaluate()` used for any toggleable behavior: n/a, the policy is stored tenant data - [ ] CHANGELOG entry under "Unreleased": n/a, the file is generated by git-cliff on release ## Screenshots / recordings n/a (no UI; the portal toggle is a follow-up) 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01Q9inRayW7ztyk1uAtwMJPR
sharang added 1 commit 2026-11-23 15:39:04 +00:00
feat(updates): per-tenant self-update policy for Werkpilot
ci / shared (pull_request) Successful in 11s
ci / test (pull_request) Successful in 6m39s
ci / image (pull_request) Skipped
361e748f06
A workspace can leave Werkpilot's self-update on ("auto", the default),
turn it off, or pin a release version.

- GET/PUT /v1/tenants/{id}/products/{product}/updates with
  {"policy": "auto" | "off" | "<semver>"}: the "updates" key of the
  product's settings document (0025, no migration); IT_ADMIN/OWNER or
  services; audited as product_settings.changed; werkpilot only
- "updates" in licence grants (POST /v1/licences, /v1/licences/ci,
  heartbeat renewals) and at the top of the heartbeat answer, read fresh
  each time, "auto" when unset

Refs platform/werkpilot#89

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q9inRayW7ztyk1uAtwMJPR
All checks were successful
ci / shared (pull_request) Successful in 11s
Required
Details
ci / test (pull_request) Successful in 6m39s
ci / image (pull_request) Skipped
Checking for merge conflicts…
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin feat/update-policy:feat/update-policy
git checkout feat/update-policy
Sign in to join this conversation.